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_removeChild(naContext c, naRef me, int argc, naRef* args)
350 naRef child = naVec_get(argv, 0);
351 naRef index = naVec_get(argv, 1);
352 if(!naIsString(child) || !naIsNum(index)) return naNil();
353 SGPropertyNode_ptr n = 0;
355 n = (*node)->removeChild(naStr_data(child), (int)index.num, false);
356 } catch (const string& err) {
357 naRuntimeError(c, (char *)err.c_str());
359 return propNodeGhostCreate(c, n);
362 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
365 naRef result = naNewVector(c);
366 if(naIsNil(argv) || naVec_size(argv) == 0) {
367 // Remove all children
368 for(int i = (*node)->nChildren() - 1; i >=0; i--)
369 naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
371 // Remove all children of a specified name
372 naRef name = naVec_get(argv, 0);
373 if(!naIsString(name)) return naNil();
375 vector<SGPropertyNode_ptr> children
376 = (*node)->removeChildren(naStr_data(name), false);
377 for(unsigned int i=0; i<children.size(); i++)
378 naVec_append(result, propNodeGhostCreate(c, children[i]));
379 } catch (const string& err) {
380 naRuntimeError(c, (char *)err.c_str());
387 static naRef f_alias(naContext c, naRef me, int argc, naRef* args)
391 naRef prop = naVec_get(argv, 0);
393 if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
394 else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
395 else throw string("props.alias() with bad argument");
396 } catch (const string& err) {
397 naRuntimeError(c, (char *)err.c_str());
400 return naNum((*node)->alias(al));
403 static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
406 return naNum((*node)->unalias());
409 static naRef f_getAliasTarget(naContext c, naRef me, int argc, naRef* args)
412 return propNodeGhostCreate(c, (*node)->getAliasTarget());
415 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
418 naRef path = naVec_get(argv, 0);
419 bool create = naTrue(naVec_get(argv, 1));
420 if(!naIsString(path)) return naNil();
423 n = (*node)->getNode(naStr_data(path), create);
424 } catch (const string& err) {
425 naRuntimeError(c, (char *)err.c_str());
428 return propNodeGhostCreate(c, n);
431 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
433 return propNodeGhostCreate(c, new SGPropertyNode());
436 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
438 return propNodeGhostCreate(c, globals->get_props());
445 { f_getType, "_getType" },
446 { f_getAttribute, "_getAttribute" },
447 { f_setAttribute, "_setAttribute" },
448 { f_getName, "_getName" },
449 { f_getIndex, "_getIndex" },
450 { f_getValue, "_getValue" },
451 { f_setValue, "_setValue" },
452 { f_setIntValue, "_setIntValue" },
453 { f_setBoolValue, "_setBoolValue" },
454 { f_setDoubleValue, "_setDoubleValue" },
455 { f_getParent, "_getParent" },
456 { f_getChild, "_getChild" },
457 { f_getChildren, "_getChildren" },
458 { f_removeChild, "_removeChild" },
459 { f_removeChildren, "_removeChildren" },
460 { f_alias, "_alias" },
461 { f_unalias, "_unalias" },
462 { f_getAliasTarget, "_getAliasTarget" },
463 { f_getNode, "_getNode" },
465 { f_globals, "_globals" },
469 naRef FGNasalSys::genPropsModule()
471 naRef namespc = naNewHash(_context);
472 for(int i=0; propfuncs[i].name; i++)
473 hashset(namespc, propfuncs[i].name,
474 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));