8 #include <simgear/math/SGMath.hxx>
9 #include <simgear/nasal/nasal.h>
10 #include <simgear/props/props.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 #define NASTR(s) s ? naStr_fromdata(naNewString(c),(char*)(s),strlen(s)) : naNil()
51 // Standard header for the extension functions. It turns the "ghost"
52 // found in arg[0] into a SGPropertyNode_ptr*, and then "unwraps" the
53 // vector found in the second argument into a normal-looking args
54 // array. This allows the Nasal handlers to do things like:
55 // Node.getChild = func { _getChild(me.ghost, arg) }
58 if(argc < 2 || !naIsGhost(args[0]) || \
59 naGhost_type(args[0]) != &PropNodeGhostType) \
60 naRuntimeError(c, "bad argument to props function"); \
61 SGPropertyNode_ptr* node = (SGPropertyNode_ptr*)naGhost_ptr(args[0]); \
64 static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
66 using namespace simgear;
68 const char* t = "unknown";
69 switch((*node)->getType()) {
70 case props::NONE: t = "NONE"; break;
71 case props::ALIAS: t = "ALIAS"; break;
72 case props::BOOL: t = "BOOL"; break;
73 case props::INT: t = "INT"; break;
74 case props::LONG: t = "LONG"; break;
75 case props::FLOAT: t = "FLOAT"; break;
76 case props::DOUBLE: t = "DOUBLE"; break;
77 case props::STRING: t = "STRING"; break;
78 case props::UNSPECIFIED: t = "UNSPECIFIED"; break;
79 case props::VEC3D: t = "VEC3D"; break;
80 case props::VEC4D: t = "VEC4D"; break;
81 case props::EXTENDED: t = "EXTENDED"; break; // shouldn't happen
86 static naRef f_getAttribute(naContext c, naRef me, int argc, naRef* args)
89 if(naVec_size(argv) == 0) return naNum(unsigned((*node)->getAttributes()));
90 naRef val = naVec_get(argv, 0);
91 const char *a = naStr_data(val);
92 SGPropertyNode::Attribute attr;
94 if(!strcmp(a, "last")) return naNum(SGPropertyNode::LAST_USED_ATTRIBUTE);
95 else if(!strcmp(a, "children")) return naNum((*node)->nChildren());
96 else if(!strcmp(a, "listeners")) return naNum((*node)->nListeners());
97 else if(!strcmp(a, "references")) return naNum(node->getNumRefs());
98 else if(!strcmp(a, "tied")) return naNum((*node)->isTied());
99 else if(!strcmp(a, "alias")) return naNum((*node)->isAlias());
100 else if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
101 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
102 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
103 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
104 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
105 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
107 naRuntimeError(c, "props.getAttribute() with invalid attribute");
110 return naNum((*node)->getAttribute(attr));
113 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
116 naRef val = naVec_get(argv, 0);
117 if(naVec_size(argv) == 1 && naIsNum(val)) {
118 naRef ret = naNum((*node)->getAttributes());
119 (*node)->setAttributes((int)val.num);
122 SGPropertyNode::Attribute attr;
123 const char *a = naStr_data(val);
125 if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
126 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
127 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
128 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
129 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
130 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
132 naRuntimeError(c, "props.setAttribute() with invalid attribute");
135 naRef ret = naNum((*node)->getAttribute(attr));
136 (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
140 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
143 return NASTR((*node)->getName());
146 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
149 return naNum((*node)->getIndex());
153 naRef makeVectorFromVec(naContext c, const T& vec)
155 const int num_components
156 = sizeof(vec.data()) / sizeof(typename T::value_type);
157 naRef vector = naNewVector(c);
158 naVec_setsize(vector, num_components);
159 for (int i = 0; i < num_components; ++i)
160 naVec_set(vector, i, naNum(vec[i]));
164 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
166 using namespace simgear;
168 switch((*node)->getType()) {
169 case props::BOOL: case props::INT:
170 case props::LONG: case props::FLOAT:
172 return naNum((*node)->getDoubleValue());
174 case props::UNSPECIFIED:
175 return NASTR((*node)->getStringValue());
177 return makeVectorFromVec(c, (*node)->getValue<SGVec3d>());
179 return makeVectorFromVec(c, (*node)->getValue<SGVec4d>());
186 T makeVecFromVector(naRef vector)
189 const int num_components
190 = sizeof(vec.data()) / sizeof(typename T::value_type);
191 int size = naVec_size(vector);
193 for (int i = 0; i < num_components && i < size; ++i) {
194 naRef element = naVec_get(vector, i);
195 naRef n = naNumValue(element);
202 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
205 naRef val = naVec_get(argv, 0);
207 if(naIsString(val)) {
208 result = (*node)->setStringValue(naStr_data(val));
209 } else if(naIsVector(val)) {
210 if(naVec_size(val) == 3)
211 result = (*node)->setValue(makeVecFromVector<SGVec3d>(val));
212 else if(naVec_size(val) == 4)
213 result = (*node)->setValue(makeVecFromVector<SGVec4d>(val));
215 naRuntimeError(c, "props.setValue() vector value has wrong size");
217 naRef n = naNumValue(val);
219 naRuntimeError(c, "props.setValue() with non-number");
220 result = (*node)->setDoubleValue(naNumValue(val).num);
222 return naNum(result);
225 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
229 // int iv = (int)naNumValue(naVec_get(argv, 0)).num;
231 // Junk to pacify the gcc-2.95.3 optimizer:
232 naRef tmp0 = naVec_get(argv, 0);
233 naRef tmp1 = naNumValue(tmp0);
235 naRuntimeError(c, "props.setIntValue() with non-number");
236 double tmp2 = tmp1.num;
239 return naNum((*node)->setIntValue(iv));
242 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
245 naRef val = naVec_get(argv, 0);
246 return naNum((*node)->setBoolValue(naTrue(val) ? true : false));
249 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
252 naRef r = naNumValue(naVec_get(argv, 0));
254 naRuntimeError(c, "props.setDoubleValue() with non-number");
255 return naNum((*node)->setDoubleValue(r.num));
258 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
261 SGPropertyNode* n = (*node)->getParent();
262 if(!n) return naNil();
263 return propNodeGhostCreate(c, n);
266 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
269 naRef child = naVec_get(argv, 0);
270 if(!naIsString(child)) return naNil();
271 naRef idx = naNumValue(naVec_get(argv, 1));
272 bool create = naTrue(naVec_get(argv, 2));
275 if(naIsNil(idx) || !naIsNum(idx)) {
276 n = (*node)->getChild(naStr_data(child), create);
278 n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
280 } catch (const string& err) {
281 naRuntimeError(c, (char *)err.c_str());
284 if(!n) return naNil();
285 return propNodeGhostCreate(c, n);
288 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
291 naRef result = naNewVector(c);
292 if(naIsNil(argv) || naVec_size(argv) == 0) {
294 for(int i=0; i<(*node)->nChildren(); i++)
295 naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
297 // Get all children of a specified name
298 naRef name = naVec_get(argv, 0);
299 if(!naIsString(name)) return naNil();
301 vector<SGPropertyNode_ptr> children
302 = (*node)->getChildren(naStr_data(name));
303 for(unsigned int i=0; i<children.size(); i++)
304 naVec_append(result, propNodeGhostCreate(c, children[i]));
305 } catch (const string& err) {
306 naRuntimeError(c, (char *)err.c_str());
313 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
316 naRef child = naVec_get(argv, 0);
317 naRef index = naVec_get(argv, 1);
318 if(!naIsString(child) || !naIsNum(index)) return naNil();
319 SGPropertyNode_ptr n = 0;
321 n = (*node)->removeChild(naStr_data(child), (int)index.num, false);
322 } catch (const string& err) {
323 naRuntimeError(c, (char *)err.c_str());
325 return propNodeGhostCreate(c, n);
328 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
331 naRef result = naNewVector(c);
332 if(naIsNil(argv) || naVec_size(argv) == 0) {
333 // Remove all children
334 for(int i = (*node)->nChildren() - 1; i >=0; i--)
335 naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
337 // Remove all children of a specified name
338 naRef name = naVec_get(argv, 0);
339 if(!naIsString(name)) return naNil();
341 vector<SGPropertyNode_ptr> children
342 = (*node)->removeChildren(naStr_data(name), false);
343 for(unsigned int i=0; i<children.size(); i++)
344 naVec_append(result, propNodeGhostCreate(c, children[i]));
345 } catch (const string& err) {
346 naRuntimeError(c, (char *)err.c_str());
353 static naRef f_alias(naContext c, naRef me, int argc, naRef* args)
357 naRef prop = naVec_get(argv, 0);
359 if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
360 else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
361 else throw string("props.alias() with bad argument");
362 } catch (const string& err) {
363 naRuntimeError(c, (char *)err.c_str());
366 return naNum((*node)->alias(al));
369 static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
372 return naNum((*node)->unalias());
375 static naRef f_getAliasTarget(naContext c, naRef me, int argc, naRef* args)
378 return propNodeGhostCreate(c, (*node)->getAliasTarget());
381 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
384 naRef path = naVec_get(argv, 0);
385 bool create = naTrue(naVec_get(argv, 1));
386 if(!naIsString(path)) return naNil();
389 n = (*node)->getNode(naStr_data(path), create);
390 } catch (const string& err) {
391 naRuntimeError(c, (char *)err.c_str());
394 return propNodeGhostCreate(c, n);
397 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
399 return propNodeGhostCreate(c, new SGPropertyNode());
402 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
404 return propNodeGhostCreate(c, globals->get_props());
411 { f_getType, "_getType" },
412 { f_getAttribute, "_getAttribute" },
413 { f_setAttribute, "_setAttribute" },
414 { f_getName, "_getName" },
415 { f_getIndex, "_getIndex" },
416 { f_getValue, "_getValue" },
417 { f_setValue, "_setValue" },
418 { f_setIntValue, "_setIntValue" },
419 { f_setBoolValue, "_setBoolValue" },
420 { f_setDoubleValue, "_setDoubleValue" },
421 { f_getParent, "_getParent" },
422 { f_getChild, "_getChild" },
423 { f_getChildren, "_getChildren" },
424 { f_removeChild, "_removeChild" },
425 { f_removeChildren, "_removeChildren" },
426 { f_alias, "_alias" },
427 { f_unalias, "_unalias" },
428 { f_getAliasTarget, "_getAliasTarget" },
429 { f_getNode, "_getNode" },
431 { f_globals, "_globals" },
435 naRef FGNasalSys::genPropsModule()
437 naRef namespc = naNewHash(_context);
438 for(int i=0; propfuncs[i].name; i++)
439 hashset(namespc, propfuncs[i].name,
440 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));