8 #include <simgear/nasal/nasal.h>
9 #include <simgear/props/props.hxx>
10 #include <simgear/props/vectorPropTemplates.hxx>
12 #include <Main/globals.hxx>
14 #include "NasalSys.hxx"
18 // Implementation of a Nasal wrapper for the SGPropertyNode class,
19 // using the Nasal "ghost" (er... Garbage collection Handle for
20 // OutSide Thingy) facility.
22 // Note that these functions appear in Nasal with prepended
23 // underscores. They work on the low-level "ghost" objects and aren't
24 // intended for use from user code, but from Nasal code you will find
25 // in props.nas. That is where the Nasal props.Node class is defined,
26 // which provides a saner interface along the lines of SGPropertyNode.
28 static void propNodeGhostDestroy(void* ghost)
30 SGPropertyNode_ptr* prop = (SGPropertyNode_ptr*)ghost;
34 naGhostType PropNodeGhostType = { propNodeGhostDestroy, "prop" };
36 static naRef propNodeGhostCreate(naContext c, SGPropertyNode* n)
38 if(!n) return naNil();
39 SGPropertyNode_ptr* ghost = new SGPropertyNode_ptr(n);
40 return naNewGhost(c, &PropNodeGhostType, ghost);
43 naRef FGNasalSys::propNodeGhost(SGPropertyNode* handle)
45 return propNodeGhostCreate(_context, handle);
48 SGPropertyNode* ghostToPropNode(naRef ref)
50 if (!naIsGhost(ref) || (naGhost_type(ref) != &PropNodeGhostType))
53 SGPropertyNode_ptr* pp = (SGPropertyNode_ptr*) naGhost_ptr(ref);
57 #define NASTR(s) s ? naStr_fromdata(naNewString(c),(char*)(s),strlen(s)) : naNil()
60 // Standard header for the extension functions. It turns the "ghost"
61 // found in arg[0] into a SGPropertyNode_ptr*, and then "unwraps" the
62 // vector found in the second argument into a normal-looking args
63 // array. This allows the Nasal handlers to do things like:
64 // Node.getChild = func { _getChild(me.ghost, arg) }
67 if(argc < 2 || !naIsGhost(args[0]) || \
68 naGhost_type(args[0]) != &PropNodeGhostType) \
69 naRuntimeError(c, "bad argument to props function"); \
70 SGPropertyNode_ptr* node = (SGPropertyNode_ptr*)naGhost_ptr(args[0]);
76 static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
78 using namespace simgear;
80 const char* t = "unknown";
81 switch((*node)->getType()) {
82 case props::NONE: t = "NONE"; break;
83 case props::ALIAS: t = "ALIAS"; break;
84 case props::BOOL: t = "BOOL"; break;
85 case props::INT: t = "INT"; break;
86 case props::LONG: t = "LONG"; break;
87 case props::FLOAT: t = "FLOAT"; break;
88 case props::DOUBLE: t = "DOUBLE"; break;
89 case props::STRING: t = "STRING"; break;
90 case props::UNSPECIFIED: t = "UNSPECIFIED"; break;
91 case props::VEC3D: t = "VEC3D"; break;
92 case props::VEC4D: t = "VEC4D"; break;
93 case props::EXTENDED: t = "EXTENDED"; break; // shouldn't happen
98 static naRef f_getAttribute(naContext c, naRef me, int argc, naRef* args)
101 if(naVec_size(argv) == 0) return naNum(unsigned((*node)->getAttributes()));
102 naRef val = naVec_get(argv, 0);
103 const char *a = naStr_data(val);
104 SGPropertyNode::Attribute attr;
106 if(!strcmp(a, "last")) return naNum(SGPropertyNode::LAST_USED_ATTRIBUTE);
107 else if(!strcmp(a, "children")) return naNum((*node)->nChildren());
108 else if(!strcmp(a, "listeners")) return naNum((*node)->nListeners());
109 else if(!strcmp(a, "references")) return naNum(node->getNumRefs());
110 else if(!strcmp(a, "tied")) return naNum((*node)->isTied());
111 else if(!strcmp(a, "alias")) return naNum((*node)->isAlias());
112 else if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
113 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
114 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
115 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
116 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
117 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
118 else if(!strcmp(a, "preserve")) attr = SGPropertyNode::PRESERVE;
120 naRuntimeError(c, "props.getAttribute() with invalid attribute");
123 return naNum((*node)->getAttribute(attr));
126 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
129 naRef val = naVec_get(argv, 0);
130 if(naVec_size(argv) == 1 && naIsNum(val)) {
131 naRef ret = naNum((*node)->getAttributes());
132 (*node)->setAttributes((int)val.num);
135 SGPropertyNode::Attribute attr;
136 const char *a = naStr_data(val);
138 if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
139 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
140 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
141 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
142 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
143 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
144 else if(!strcmp(a, "preserve")) attr = SGPropertyNode::PRESERVE;
146 naRuntimeError(c, "props.setAttribute() with invalid attribute");
149 naRef ret = naNum((*node)->getAttribute(attr));
150 (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
154 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
157 return NASTR((*node)->getName());
160 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
163 return naNum((*node)->getIndex());
167 naRef makeVectorFromVec(naContext c, const T& vec)
169 const int num_components
170 = sizeof(vec.data()) / sizeof(typename T::value_type);
171 naRef vector = naNewVector(c);
172 naVec_setsize(c, vector, num_components);
173 for (int i = 0; i < num_components; ++i)
174 naVec_set(vector, i, naNum(vec[i]));
178 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
180 using namespace simgear;
182 switch((*node)->getType()) {
183 case props::BOOL: case props::INT:
184 case props::LONG: case props::FLOAT:
187 double dv = (*node)->getDoubleValue();
188 if (osg::isNaN(dv)) {
189 SG_LOG(SG_NASAL, SG_ALERT, "Nasal getValue: property " << (*node)->getPath() << " is NaN");
197 case props::UNSPECIFIED:
198 return NASTR((*node)->getStringValue());
200 return makeVectorFromVec(c, (*node)->getValue<SGVec3d>());
202 return makeVectorFromVec(c, (*node)->getValue<SGVec4d>());
209 T makeVecFromVector(naRef vector)
212 const int num_components
213 = sizeof(vec.data()) / sizeof(typename T::value_type);
214 int size = naVec_size(vector);
216 for (int i = 0; i < num_components && i < size; ++i) {
217 naRef element = naVec_get(vector, i);
218 naRef n = naNumValue(element);
225 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
228 naRef val = naVec_get(argv, 0);
230 if(naIsString(val)) {
231 result = (*node)->setStringValue(naStr_data(val));
232 } else if(naIsVector(val)) {
233 if(naVec_size(val) == 3)
234 result = (*node)->setValue(makeVecFromVector<SGVec3d>(val));
235 else if(naVec_size(val) == 4)
236 result = (*node)->setValue(makeVecFromVector<SGVec4d>(val));
238 naRuntimeError(c, "props.setValue() vector value has wrong size");
240 naRef n = naNumValue(val);
242 naRuntimeError(c, "props.setValue() with non-number");
244 double d = naNumValue(val).num;
246 naRuntimeError(c, "props.setValue() passed a NaN");
249 result = (*node)->setDoubleValue(d);
251 return naNum(result);
254 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
258 // int iv = (int)naNumValue(naVec_get(argv, 0)).num;
260 // Junk to pacify the gcc-2.95.3 optimizer:
261 naRef tmp0 = naVec_get(argv, 0);
262 naRef tmp1 = naNumValue(tmp0);
264 naRuntimeError(c, "props.setIntValue() with non-number");
265 double tmp2 = tmp1.num;
268 return naNum((*node)->setIntValue(iv));
271 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
274 naRef val = naVec_get(argv, 0);
275 return naNum((*node)->setBoolValue(naTrue(val) ? true : false));
278 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
281 naRef r = naNumValue(naVec_get(argv, 0));
283 naRuntimeError(c, "props.setDoubleValue() with non-number");
285 if (osg::isNaN(r.num)) {
286 naRuntimeError(c, "props.setDoubleValue() passed a NaN");
289 return naNum((*node)->setDoubleValue(r.num));
292 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
295 SGPropertyNode* n = (*node)->getParent();
296 if(!n) return naNil();
297 return propNodeGhostCreate(c, n);
300 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
303 naRef child = naVec_get(argv, 0);
304 if(!naIsString(child)) return naNil();
305 naRef idx = naNumValue(naVec_get(argv, 1));
306 bool create = naTrue(naVec_get(argv, 2));
309 if(naIsNil(idx) || !naIsNum(idx)) {
310 n = (*node)->getChild(naStr_data(child), create);
312 n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
314 } catch (const string& err) {
315 naRuntimeError(c, (char *)err.c_str());
318 if(!n) return naNil();
319 return propNodeGhostCreate(c, n);
322 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
325 naRef result = naNewVector(c);
326 if(naIsNil(argv) || naVec_size(argv) == 0) {
328 for(int i=0; i<(*node)->nChildren(); i++)
329 naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
331 // Get all children of a specified name
332 naRef name = naVec_get(argv, 0);
333 if(!naIsString(name)) return naNil();
335 vector<SGPropertyNode_ptr> children
336 = (*node)->getChildren(naStr_data(name));
337 for(unsigned int i=0; i<children.size(); i++)
338 naVec_append(result, propNodeGhostCreate(c, children[i]));
339 } catch (const string& err) {
340 naRuntimeError(c, (char *)err.c_str());
347 static naRef f_addChild(naContext c, naRef me, int argc, naRef* args)
350 naRef child = naVec_get(argv, 0);
351 if(!naIsString(child)) return naNil();
352 naRef ref_min_index = naNumValue(naVec_get(argv, 1));
353 naRef ref_append = naVec_get(argv, 2);
358 if( !naIsNil(ref_min_index) && naIsNum(ref_min_index) )
359 min_index = ref_min_index.num;
362 if( !naIsNil(ref_append) )
363 append = naTrue(ref_append);
365 n = (*node)->addChild(naStr_data(child), min_index, append);
367 catch (const string& err)
369 naRuntimeError(c, (char *)err.c_str());
373 return propNodeGhostCreate(c, n);
376 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
379 naRef child = naVec_get(argv, 0);
380 naRef index = naVec_get(argv, 1);
381 if(!naIsString(child) || !naIsNum(index)) return naNil();
382 SGPropertyNode_ptr n = 0;
384 n = (*node)->removeChild(naStr_data(child), (int)index.num, false);
385 } catch (const string& err) {
386 naRuntimeError(c, (char *)err.c_str());
388 return propNodeGhostCreate(c, n);
391 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
394 naRef result = naNewVector(c);
395 if(naIsNil(argv) || naVec_size(argv) == 0) {
396 // Remove all children
397 for(int i = (*node)->nChildren() - 1; i >=0; i--)
398 naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
400 // Remove all children of a specified name
401 naRef name = naVec_get(argv, 0);
402 if(!naIsString(name)) return naNil();
404 vector<SGPropertyNode_ptr> children
405 = (*node)->removeChildren(naStr_data(name), false);
406 for(unsigned int i=0; i<children.size(); i++)
407 naVec_append(result, propNodeGhostCreate(c, children[i]));
408 } catch (const string& err) {
409 naRuntimeError(c, (char *)err.c_str());
416 static naRef f_alias(naContext c, naRef me, int argc, naRef* args)
420 naRef prop = naVec_get(argv, 0);
422 if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
423 else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
424 else throw string("props.alias() with bad argument");
425 } catch (const string& err) {
426 naRuntimeError(c, (char *)err.c_str());
429 return naNum((*node)->alias(al));
432 static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
435 return naNum((*node)->unalias());
438 static naRef f_getAliasTarget(naContext c, naRef me, int argc, naRef* args)
441 return propNodeGhostCreate(c, (*node)->getAliasTarget());
444 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
447 naRef path = naVec_get(argv, 0);
448 bool create = naTrue(naVec_get(argv, 1));
449 if(!naIsString(path)) return naNil();
452 n = (*node)->getNode(naStr_data(path), create);
453 } catch (const string& err) {
454 naRuntimeError(c, (char *)err.c_str());
457 return propNodeGhostCreate(c, n);
460 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
462 return propNodeGhostCreate(c, new SGPropertyNode());
465 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
467 return propNodeGhostCreate(c, globals->get_props());
474 { f_getType, "_getType" },
475 { f_getAttribute, "_getAttribute" },
476 { f_setAttribute, "_setAttribute" },
477 { f_getName, "_getName" },
478 { f_getIndex, "_getIndex" },
479 { f_getValue, "_getValue" },
480 { f_setValue, "_setValue" },
481 { f_setIntValue, "_setIntValue" },
482 { f_setBoolValue, "_setBoolValue" },
483 { f_setDoubleValue, "_setDoubleValue" },
484 { f_getParent, "_getParent" },
485 { f_getChild, "_getChild" },
486 { f_getChildren, "_getChildren" },
487 { f_addChild, "_addChild" },
488 { f_removeChild, "_removeChild" },
489 { f_removeChildren, "_removeChildren" },
490 { f_alias, "_alias" },
491 { f_unalias, "_unalias" },
492 { f_getAliasTarget, "_getAliasTarget" },
493 { f_getNode, "_getNode" },
495 { f_globals, "_globals" },
499 naRef FGNasalSys::genPropsModule()
501 naRef namespc = naNewHash(_context);
502 for(int i=0; propfuncs[i].name; i++)
503 hashset(namespc, propfuncs[i].name,
504 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));