7 #include <simgear/nasal/nasal.h>
8 #include <simgear/props/props.hxx>
9 #include <simgear/props/vectorPropTemplates.hxx>
11 #include <Main/globals.hxx>
13 #include "NasalSys.hxx"
17 // Implementation of a Nasal wrapper for the SGPropertyNode class,
18 // using the Nasal "ghost" (er... Garbage collection Handle for
19 // OutSide Thingy) facility.
21 // Note that these functions appear in Nasal with prepended
22 // underscores. They work on the low-level "ghost" objects and aren't
23 // intended for use from user code, but from Nasal code you will find
24 // in props.nas. That is where the Nasal props.Node class is defined,
25 // which provides a saner interface along the lines of SGPropertyNode.
27 static void propNodeGhostDestroy(void* ghost)
29 SGPropertyNode_ptr* prop = (SGPropertyNode_ptr*)ghost;
33 naGhostType PropNodeGhostType = { propNodeGhostDestroy, "prop" };
35 naRef propNodeGhostCreate(naContext c, SGPropertyNode* n)
37 if(!n) return naNil();
38 SGPropertyNode_ptr* ghost = new SGPropertyNode_ptr(n);
39 return naNewGhost(c, &PropNodeGhostType, ghost);
42 naRef FGNasalSys::propNodeGhost(SGPropertyNode* handle)
44 return propNodeGhostCreate(_context, handle);
47 SGPropertyNode* ghostToPropNode(naRef ref)
49 if (!naIsGhost(ref) || (naGhost_type(ref) != &PropNodeGhostType))
52 SGPropertyNode_ptr* pp = (SGPropertyNode_ptr*) naGhost_ptr(ref);
56 #define NASTR(s) s ? naStr_fromdata(naNewString(c),(char*)(s),strlen(s)) : naNil()
59 // Standard header for the extension functions. It turns the "ghost"
60 // found in arg[0] into a SGPropertyNode_ptr*, and then "unwraps" the
61 // vector found in the second argument into a normal-looking args
62 // array. This allows the Nasal handlers to do things like:
63 // Node.getChild = func { _getChild(me.ghost, arg) }
66 if(argc < 2 || !naIsGhost(args[0]) || \
67 naGhost_type(args[0]) != &PropNodeGhostType) \
68 naRuntimeError(c, "bad argument to props function"); \
69 SGPropertyNode_ptr node = *(SGPropertyNode_ptr*)naGhost_ptr(args[0]);
76 // Pops the first argument as a relative path if the first condition
77 // is true (e.g. argc > 1 for getAttribute) and if it is a string.
78 // If the second confition is true, then another is popped to specify
79 // if the node should be created (i.e. like the second argument to
82 // Note that this makes the function return nil if the node doesn't
83 // exist, so all functions with a relative_path parameter will
84 // return nil if the specified node does not exist.
86 #define MOVETARGET(cond1, create) \
88 naRef name = naVec_get(argv, 0); \
89 if(naIsString(name)) { \
91 node = node->getNode(naStr_data(name), create); \
92 } catch(const string& err) { \
93 naRuntimeError(c, (char *)err.c_str()); \
96 if(!node) return naNil(); \
97 naVec_removefirst(argv); /* pop only if we were successful */ \
102 // Get the type of a property (returns a string).
104 // props.Node.getType(string relative_path);
105 // props.Node.getType();
106 static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
108 using namespace simgear;
110 MOVETARGET(naVec_size(argv) > 0, false);
111 const char* t = "unknown";
112 switch(node->getType()) {
113 case props::NONE: t = "NONE"; break;
114 case props::ALIAS: t = "ALIAS"; break;
115 case props::BOOL: t = "BOOL"; break;
116 case props::INT: t = "INT"; break;
117 case props::LONG: t = "LONG"; break;
118 case props::FLOAT: t = "FLOAT"; break;
119 case props::DOUBLE: t = "DOUBLE"; break;
120 case props::STRING: t = "STRING"; break;
121 case props::UNSPECIFIED: t = "UNSPECIFIED"; break;
122 case props::VEC3D: t = "VEC3D"; break;
123 case props::VEC4D: t = "VEC4D"; break;
124 case props::EXTENDED: t = "EXTENDED"; break; // shouldn't happen
130 // Get an attribute of a property by name (returns true/false).
132 // props.Node.getType(string relative_path,
133 // string attribute_name);
134 // props.Node.getType(string attribute_name);
135 static naRef f_getAttribute(naContext c, naRef me, int argc, naRef* args)
138 if(naVec_size(argv) == 0) return naNum(unsigned(node->getAttributes()));
139 MOVETARGET(naVec_size(argv) > 1, false);
140 naRef val = naVec_get(argv, 0);
141 const char *a = naStr_data(val);
142 SGPropertyNode::Attribute attr;
144 if(!strcmp(a, "last")) return naNum(SGPropertyNode::LAST_USED_ATTRIBUTE);
145 else if(!strcmp(a, "children")) return naNum(node->nChildren());
146 else if(!strcmp(a, "listeners")) return naNum(node->nListeners());
147 // Number of references without instance used in this function
148 else if(!strcmp(a, "references")) return naNum(node.getNumRefs() - 1);
149 else if(!strcmp(a, "tied")) return naNum(node->isTied());
150 else if(!strcmp(a, "alias")) return naNum(node->isAlias());
151 else if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
152 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
153 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
154 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
155 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
156 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
157 else if(!strcmp(a, "preserve")) attr = SGPropertyNode::PRESERVE;
159 naRuntimeError(c, "props.getAttribute() with invalid attribute");
162 return naNum(node->getAttribute(attr));
166 // Set an attribute by name and boolean value or raw (bitmasked) number.
168 // props.Node.setAttribute(string relative_path,
169 // string attribute_name,
171 // props.Node.setAttribute(string attribute_name,
173 // props.Node.setArtribute(int attributes);
174 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
177 MOVETARGET(naVec_size(argv) > 2, false);
178 naRef val = naVec_get(argv, 0);
179 if(naVec_size(argv) == 1 && naIsNum(val)) {
180 naRef ret = naNum(node->getAttributes());
181 node->setAttributes((int)val.num);
184 SGPropertyNode::Attribute attr;
185 const char *a = naStr_data(val);
187 if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
188 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
189 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
190 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
191 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
192 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
193 else if(!strcmp(a, "preserve")) attr = SGPropertyNode::PRESERVE;
195 naRuntimeError(c, "props.setAttribute() with invalid attribute");
198 naRef ret = naNum(node->getAttribute(attr));
199 node->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
204 // Get the simple name of this node.
206 // props.Node.getName();
207 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
210 return NASTR(node->getName());
214 // Get the index of this node.
216 // props.Node.getIndex();
217 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
220 return naNum(node->getIndex());
223 // Check if other_node refers to the same as this node.
225 // props.Node.equals(other_node);
226 static naRef f_equals(naContext c, naRef me, int argc, naRef* args)
230 naRef rhs = naVec_get(argv, 0);
231 if( !naIsGhost(rhs) || naGhost_type(rhs) != &PropNodeGhostType )
234 SGPropertyNode_ptr node_rhs = *(SGPropertyNode_ptr*)naGhost_ptr(rhs);
235 return naNum(node == node_rhs);
239 naRef makeVectorFromVec(naContext c, const T& vec)
241 const int num_components
242 = sizeof(vec.data()) / sizeof(typename T::value_type);
243 naRef vector = naNewVector(c);
244 naVec_setsize(c, vector, num_components);
245 for (int i = 0; i < num_components; ++i)
246 naVec_set(vector, i, naNum(vec[i]));
251 // Get the value of a node, with or without a relative path.
253 // props.Node.getValue(string relative_path);
254 // props.Node.getValue();
255 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
257 using namespace simgear;
259 MOVETARGET(naVec_size(argv) > 0, false);
260 switch(node->getType()) {
261 case props::BOOL: case props::INT:
262 case props::LONG: case props::FLOAT:
265 double dv = node->getDoubleValue();
266 if (SGMisc<double>::isNaN(dv)) {
267 SG_LOG(SG_NASAL, SG_ALERT, "Nasal getValue: property " << node->getPath() << " is NaN");
275 case props::UNSPECIFIED:
276 return NASTR(node->getStringValue());
278 return makeVectorFromVec(c, node->getValue<SGVec3d>());
280 return makeVectorFromVec(c, node->getValue<SGVec4d>());
287 T makeVecFromVector(naRef vector)
290 const int num_components
291 = sizeof(vec.data()) / sizeof(typename T::value_type);
292 int size = naVec_size(vector);
294 for (int i = 0; i < num_components && i < size; ++i) {
295 naRef element = naVec_get(vector, i);
296 naRef n = naNumValue(element);
304 // Set the value of a node; returns true if it succeeded or
305 // false if it failed. <val> can be a string, number, or a
306 // vector or numbers (for SGVec3D/4D types).
308 // props.Node.setValue(string relative_path,
310 // props.Node.setValue(val);
311 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
314 MOVETARGET(naVec_size(argv) > 1, true);
315 naRef val = naVec_get(argv, 0);
317 if(naIsString(val)) {
318 result = node->setStringValue(naStr_data(val));
319 } else if(naIsVector(val)) {
320 if(naVec_size(val) == 3)
321 result = node->setValue(makeVecFromVector<SGVec3d>(val));
322 else if(naVec_size(val) == 4)
323 result = node->setValue(makeVecFromVector<SGVec4d>(val));
325 naRuntimeError(c, "props.setValue() vector value has wrong size");
328 naRuntimeError(c, "props.setValue() with non-number");
330 double d = naNumValue(val).num;
331 if (SGMisc<double>::isNaN(d)) {
332 naRuntimeError(c, "props.setValue() passed a NaN");
335 result = node->setDoubleValue(d);
337 return naNum(result);
340 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
343 MOVETARGET(naVec_size(argv) > 1, true);
345 // int iv = (int)naNumValue(naVec_get(argv, 0)).num;
347 // Junk to pacify the gcc-2.95.3 optimizer:
348 naRef tmp0 = naVec_get(argv, 0);
349 naRef tmp1 = naNumValue(tmp0);
351 naRuntimeError(c, "props.setIntValue() with non-number");
352 double tmp2 = tmp1.num;
355 return naNum(node->setIntValue(iv));
358 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
361 MOVETARGET(naVec_size(argv) > 1, true);
362 naRef val = naVec_get(argv, 0);
363 return naNum(node->setBoolValue(naTrue(val) ? true : false));
366 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
369 MOVETARGET(naVec_size(argv) > 1, true);
370 naRef r = naNumValue(naVec_get(argv, 0));
372 naRuntimeError(c, "props.setDoubleValue() with non-number");
374 if (SGMisc<double>::isNaN(r.num)) {
375 naRuntimeError(c, "props.setDoubleValue() passed a NaN");
378 return naNum(node->setDoubleValue(r.num));
382 // Get the parent of this node as a ghost.
384 // props.Node.getParent();
385 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
388 SGPropertyNode* n = node->getParent();
389 if(!n) return naNil();
390 return propNodeGhostCreate(c, n);
394 // Get a child by name and optional index=0, creating if specified (by default it
395 // does not create it). If the node does not exist and create is false, then it
396 // returns nil, else it returns a (possibly new) property ghost.
398 // props.Node.getChild(string relative_path,
400 // bool create=false);
401 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
404 naRef child = naVec_get(argv, 0);
405 if(!naIsString(child)) return naNil();
406 naRef idx = naNumValue(naVec_get(argv, 1));
407 bool create = naTrue(naVec_get(argv, 2)) != 0;
411 n = node->getChild(naStr_data(child), create);
413 n = node->getChild(naStr_data(child), (int)idx.num, create);
415 } catch (const string& err) {
416 naRuntimeError(c, (char *)err.c_str());
419 if(!n) return naNil();
420 return propNodeGhostCreate(c, n);
424 // Get all children with a specified name as a vector of ghosts.
426 // props.Node.getChildren(string relative_path);
427 // props.Node.getChildren(); #get all children
428 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
431 naRef result = naNewVector(c);
432 if(naIsNil(argv) || naVec_size(argv) == 0) {
434 for(int i=0; i<node->nChildren(); i++)
435 naVec_append(result, propNodeGhostCreate(c, node->getChild(i)));
437 // Get all children of a specified name
438 naRef name = naVec_get(argv, 0);
439 if(!naIsString(name)) return naNil();
441 vector<SGPropertyNode_ptr> children
442 = node->getChildren(naStr_data(name));
443 for(unsigned int i=0; i<children.size(); i++)
444 naVec_append(result, propNodeGhostCreate(c, children[i]));
445 } catch (const string& err) {
446 naRuntimeError(c, (char *)err.c_str());
454 // Append a named child at the first unused index...
456 // props.Node.addChild(string name,
458 // bool append=true);
459 static naRef f_addChild(naContext c, naRef me, int argc, naRef* args)
462 naRef child = naVec_get(argv, 0);
463 if(!naIsString(child)) return naNil();
464 naRef ref_min_index = naNumValue(naVec_get(argv, 1));
465 naRef ref_append = naVec_get(argv, 2);
470 if(!naIsNil(ref_min_index))
471 min_index = ref_min_index.num;
474 if(!naIsNil(ref_append))
475 append = naTrue(ref_append) != 0;
477 n = node->addChild(naStr_data(child), min_index, append);
479 catch (const string& err)
481 naRuntimeError(c, (char *)err.c_str());
485 return propNodeGhostCreate(c, n);
488 static naRef f_addChildren(naContext c, naRef me, int argc, naRef* args)
491 naRef child = naVec_get(argv, 0);
492 if(!naIsString(child)) return naNil();
493 naRef ref_count = naNumValue(naVec_get(argv, 1));
494 naRef ref_min_index = naNumValue(naVec_get(argv, 2));
495 naRef ref_append = naVec_get(argv, 3);
499 if( !naIsNum(ref_count) )
500 throw string("props.addChildren() missing number of children");
501 count = ref_count.num;
504 if(!naIsNil(ref_min_index))
505 min_index = ref_min_index.num;
508 if(!naIsNil(ref_append))
509 append = naTrue(ref_append) != 0;
511 const simgear::PropertyList& nodes =
512 node->addChildren(naStr_data(child), count, min_index, append);
514 naRef result = naNewVector(c);
515 for( size_t i = 0; i < nodes.size(); ++i )
516 naVec_append(result, propNodeGhostCreate(c, nodes[i]));
519 catch (const string& err)
521 naRuntimeError(c, (char *)err.c_str());
528 // Remove a child by name and index. Returns it as a ghost.
530 // props.Node.removeChild(string relative_path,
532 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
535 naRef child = naVec_get(argv, 0);
536 naRef index = naVec_get(argv, 1);
537 if(!naIsString(child) || !naIsNum(index)) return naNil();
538 SGPropertyNode_ptr n = 0;
540 n = node->removeChild(naStr_data(child), (int)index.num);
541 } catch (const string& err) {
542 naRuntimeError(c, (char *)err.c_str());
544 return propNodeGhostCreate(c, n);
548 // Remove all children with specified name. Returns a vector of all the nodes
549 // removed as ghosts.
551 // props.Node.removeChildren(string relative_path);
552 // props.Node.removeChildren(); #remove all children
553 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
556 naRef result = naNewVector(c);
557 if(naIsNil(argv) || naVec_size(argv) == 0) {
558 // Remove all children
559 for(int i = node->nChildren() - 1; i >=0; i--)
560 naVec_append(result, propNodeGhostCreate(c, node->removeChild(i)));
562 // Remove all children of a specified name
563 naRef name = naVec_get(argv, 0);
564 if(!naIsString(name)) return naNil();
566 vector<SGPropertyNode_ptr> children
567 = node->removeChildren(naStr_data(name));
568 for(unsigned int i=0; i<children.size(); i++)
569 naVec_append(result, propNodeGhostCreate(c, children[i]));
570 } catch (const string& err) {
571 naRuntimeError(c, (char *)err.c_str());
578 // Remove all children of a property node.
580 // props.Node.removeAllChildren();
581 static naRef f_removeAllChildren(naContext c, naRef me, int argc, naRef* args)
584 node->removeAllChildren();
585 return propNodeGhostCreate(c, node);
588 // Alias this property to another one; returns 1 on success or 0 on failure
589 // (only applicable to tied properties).
591 // props.Node.alias(string global_path);
592 // props.Node.alias(prop_ghost node);
593 // props.Node.alias(props.Node node); #added by props.nas
594 static naRef f_alias(naContext c, naRef me, int argc, naRef* args)
598 naRef prop = naVec_get(argv, 0);
600 if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
601 else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
602 else throw string("props.alias() with bad argument");
603 } catch (const string& err) {
604 naRuntimeError(c, (char *)err.c_str());
607 return naNum(node->alias(al));
611 // Un-alias this property. Returns 1 on success or 0 on failure (only
612 // applicable to tied properties).
614 // props.Node.unalias();
615 static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
618 return naNum(node->unalias());
622 // Get the alias of this node as a ghost.
624 // props.Node.getAliasTarget();
625 static naRef f_getAliasTarget(naContext c, naRef me, int argc, naRef* args)
628 return propNodeGhostCreate(c, node->getAliasTarget());
632 // Get a relative node. Returns nil if it does not exist and create is false,
633 // or a ghost object otherwise (wrapped into a props.Node object by props.nas).
635 // props.Node.getNode(string relative_path,
636 // bool create=false);
637 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
640 naRef path = naVec_get(argv, 0);
641 bool create = naTrue(naVec_get(argv, 1)) != 0;
642 if(!naIsString(path)) return naNil();
645 n = node->getNode(naStr_data(path), create);
646 } catch (const string& err) {
647 naRuntimeError(c, (char *)err.c_str());
650 return propNodeGhostCreate(c, n);
654 // Create a new property node.
657 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
659 return propNodeGhostCreate(c, new SGPropertyNode());
663 // Get the global root node (cached by props.nas so that it does
664 // not require a function call).
668 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
670 return propNodeGhostCreate(c, globals->get_props());
677 { f_getType, "_getType" },
678 { f_getAttribute, "_getAttribute" },
679 { f_setAttribute, "_setAttribute" },
680 { f_getName, "_getName" },
681 { f_getIndex, "_getIndex" },
682 { f_equals, "_equals" },
683 { f_getValue, "_getValue" },
684 { f_setValue, "_setValue" },
685 { f_setIntValue, "_setIntValue" },
686 { f_setBoolValue, "_setBoolValue" },
687 { f_setDoubleValue, "_setDoubleValue" },
688 { f_getParent, "_getParent" },
689 { f_getChild, "_getChild" },
690 { f_getChildren, "_getChildren" },
691 { f_addChild, "_addChild" },
692 { f_addChildren, "_addChildren" },
693 { f_removeChild, "_removeChild" },
694 { f_removeChildren, "_removeChildren" },
695 { f_removeAllChildren, "_removeAllChildren" },
696 { f_alias, "_alias" },
697 { f_unalias, "_unalias" },
698 { f_getAliasTarget, "_getAliasTarget" },
699 { f_getNode, "_getNode" },
701 { f_globals, "_globals" },
705 naRef FGNasalSys::genPropsModule()
707 naRef namespc = naNewHash(_context);
708 for(int i=0; propfuncs[i].name; i++)
709 hashset(namespc, propfuncs[i].name,
710 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));