6 #include <simgear/nasal/nasal.h>
7 #include <simgear/props/props.hxx>
9 #include <Main/globals.hxx>
11 #include "NasalSys.hxx"
13 // Implementation of a Nasal wrapper for the SGPropertyNode class,
14 // using the Nasal "ghost" (er... Garbage collection Handle for
15 // OutSide Thingy) facility.
17 // Note that these functions appear in Nasal with prepended
18 // underscores. They work on the low-level "ghost" objects and aren't
19 // intended for use from user code, but from Nasal code you will find
20 // in props.nas. That is where the Nasal props.Node class is defined,
21 // which provides a saner interface along the lines of SGPropertyNode.
23 static void propNodeGhostDestroy(void* ghost)
25 SGPropertyNode_ptr* prop = (SGPropertyNode_ptr*)ghost;
29 naGhostType PropNodeGhostType = { propNodeGhostDestroy, "prop" };
31 static naRef propNodeGhostCreate(naContext c, SGPropertyNode* n)
33 if(!n) return naNil();
34 SGPropertyNode_ptr* ghost = new SGPropertyNode_ptr(n);
35 return naNewGhost(c, &PropNodeGhostType, ghost);
38 naRef FGNasalSys::propNodeGhost(SGPropertyNode* handle)
40 return propNodeGhostCreate(_context, handle);
43 #define NASTR(s) s ? naStr_fromdata(naNewString(c),(char*)(s),strlen(s)) : naNil()
46 // Standard header for the extension functions. It turns the "ghost"
47 // found in arg[0] into a SGPropertyNode_ptr*, and then "unwraps" the
48 // vector found in the second argument into a normal-looking args
49 // array. This allows the Nasal handlers to do things like:
50 // Node.getChild = func { _getChild(me.ghost, arg) }
53 if(argc < 2 || !naIsGhost(args[0]) || \
54 naGhost_type(args[0]) != &PropNodeGhostType) \
55 naRuntimeError(c, "bad argument to props function"); \
56 SGPropertyNode_ptr* node = (SGPropertyNode_ptr*)naGhost_ptr(args[0]); \
59 static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
61 using namespace simgear;
63 const char* t = "unknown";
64 switch((*node)->getType()) {
65 case props::NONE: t = "NONE"; break;
66 case props::ALIAS: t = "ALIAS"; break;
67 case props::BOOL: t = "BOOL"; break;
68 case props::INT: t = "INT"; break;
69 case props::LONG: t = "LONG"; break;
70 case props::FLOAT: t = "FLOAT"; break;
71 case props::DOUBLE: t = "DOUBLE"; break;
72 case props::STRING: t = "STRING"; break;
73 case props::UNSPECIFIED: t = "UNSPECIFIED"; break;
74 case props::VEC3D: t = "VEC3D"; break;
75 case props::VEC4D: t = "VEC4D"; break;
76 case props::EXTENDED: t = "EXTENDED"; break; // shouldn't happen
81 static naRef f_getAttribute(naContext c, naRef me, int argc, naRef* args)
84 if(naVec_size(argv) == 0) return naNum(unsigned((*node)->getAttributes()));
85 naRef val = naVec_get(argv, 0);
86 const char *a = naStr_data(val);
87 SGPropertyNode::Attribute attr;
89 if(!strcmp(a, "last")) return naNum(SGPropertyNode::LAST_USED_ATTRIBUTE);
90 else if(!strcmp(a, "children")) return naNum((*node)->nChildren());
91 else if(!strcmp(a, "listeners")) return naNum((*node)->nListeners());
92 else if(!strcmp(a, "references")) return naNum(node->getNumRefs());
93 else if(!strcmp(a, "tied")) return naNum((*node)->isTied());
94 else if(!strcmp(a, "alias")) return naNum((*node)->isAlias());
95 else if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
96 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
97 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
98 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
99 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
100 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
102 naRuntimeError(c, "props.getAttribute() with invalid attribute");
105 return naNum((*node)->getAttribute(attr));
108 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
111 naRef val = naVec_get(argv, 0);
112 if(naVec_size(argv) == 1 && naIsNum(val)) {
113 naRef ret = naNum((*node)->getAttributes());
114 (*node)->setAttributes((int)val.num);
117 SGPropertyNode::Attribute attr;
118 const char *a = naStr_data(val);
120 if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
121 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
122 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
123 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
124 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
125 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
127 naRuntimeError(c, "props.setAttribute() with invalid attribute");
130 naRef ret = naNum((*node)->getAttribute(attr));
131 (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
135 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
138 return NASTR((*node)->getName());
141 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
144 return naNum((*node)->getIndex());
148 naRef makeVectorFromVec(naContext c, const T& vec)
150 const int num_components
151 = sizeof(vec.data()) / sizeof(typename T::value_type);
152 naRef vector = naNewVector(c);
153 naVec_setsize(vector, num_components);
154 for (int i = 0; i < num_components; ++i)
155 naVec_set(vector, i, naNum(vec[i]));
159 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
161 using namespace simgear;
163 switch((*node)->getType()) {
164 case props::BOOL: case props::INT:
165 case props::LONG: case props::FLOAT:
167 return naNum((*node)->getDoubleValue());
169 case props::UNSPECIFIED:
170 return NASTR((*node)->getStringValue());
172 return makeVectorFromVec(c, (*node)->getValue<SGVec3d>());
174 return makeVectorFromVec(c, (*node)->getValue<SGVec4d>());
181 T makeVecFromVector(naRef vector)
184 const int num_components
185 = sizeof(vec.data()) / sizeof(typename T::value_type);
186 int size = naVec_size(vector);
188 for (int i = 0; i < num_components && i < size; ++i) {
189 naRef element = naVec_get(vector, i);
190 naRef n = naNumValue(element);
197 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
200 naRef val = naVec_get(argv, 0);
202 if(naIsString(val)) {
203 result = (*node)->setStringValue(naStr_data(val));
204 } else if(naIsVector(val)) {
205 if(naVec_size(val) == 3)
206 result = (*node)->setValue(makeVecFromVector<SGVec3d>(val));
207 else if(naVec_size(val) == 4)
208 result = (*node)->setValue(makeVecFromVector<SGVec4d>(val));
210 naRuntimeError(c, "props.setValue() vector value has wrong size");
212 naRef n = naNumValue(val);
214 naRuntimeError(c, "props.setValue() with non-number");
215 result = (*node)->setDoubleValue(naNumValue(val).num);
217 return naNum(result);
220 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
224 // int iv = (int)naNumValue(naVec_get(argv, 0)).num;
226 // Junk to pacify the gcc-2.95.3 optimizer:
227 naRef tmp0 = naVec_get(argv, 0);
228 naRef tmp1 = naNumValue(tmp0);
230 naRuntimeError(c, "props.setIntValue() with non-number");
231 double tmp2 = tmp1.num;
234 return naNum((*node)->setIntValue(iv));
237 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
240 naRef val = naVec_get(argv, 0);
241 return naNum((*node)->setBoolValue(naTrue(val) ? true : false));
244 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
247 naRef r = naNumValue(naVec_get(argv, 0));
249 naRuntimeError(c, "props.setDoubleValue() with non-number");
250 return naNum((*node)->setDoubleValue(r.num));
253 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
256 SGPropertyNode* n = (*node)->getParent();
257 if(!n) return naNil();
258 return propNodeGhostCreate(c, n);
261 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
264 naRef child = naVec_get(argv, 0);
265 if(!naIsString(child)) return naNil();
266 naRef idx = naNumValue(naVec_get(argv, 1));
267 bool create = naTrue(naVec_get(argv, 2));
270 if(naIsNil(idx) || !naIsNum(idx)) {
271 n = (*node)->getChild(naStr_data(child), create);
273 n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
275 } catch (const string& err) {
276 naRuntimeError(c, (char *)err.c_str());
279 if(!n) return naNil();
280 return propNodeGhostCreate(c, n);
283 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
286 naRef result = naNewVector(c);
287 if(naIsNil(argv) || naVec_size(argv) == 0) {
289 for(int i=0; i<(*node)->nChildren(); i++)
290 naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
292 // Get all children of a specified name
293 naRef name = naVec_get(argv, 0);
294 if(!naIsString(name)) return naNil();
296 vector<SGPropertyNode_ptr> children
297 = (*node)->getChildren(naStr_data(name));
298 for(unsigned int i=0; i<children.size(); i++)
299 naVec_append(result, propNodeGhostCreate(c, children[i]));
300 } catch (const string& err) {
301 naRuntimeError(c, (char *)err.c_str());
308 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
311 naRef child = naVec_get(argv, 0);
312 naRef index = naVec_get(argv, 1);
313 if(!naIsString(child) || !naIsNum(index)) return naNil();
314 SGPropertyNode_ptr n = 0;
316 n = (*node)->removeChild(naStr_data(child), (int)index.num, false);
317 } catch (const string& err) {
318 naRuntimeError(c, (char *)err.c_str());
320 return propNodeGhostCreate(c, n);
323 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
326 naRef result = naNewVector(c);
327 if(naIsNil(argv) || naVec_size(argv) == 0) {
328 // Remove all children
329 for(int i = (*node)->nChildren() - 1; i >=0; i--)
330 naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
332 // Remove all children of a specified name
333 naRef name = naVec_get(argv, 0);
334 if(!naIsString(name)) return naNil();
336 vector<SGPropertyNode_ptr> children
337 = (*node)->removeChildren(naStr_data(name), false);
338 for(unsigned int i=0; i<children.size(); i++)
339 naVec_append(result, propNodeGhostCreate(c, children[i]));
340 } catch (const string& err) {
341 naRuntimeError(c, (char *)err.c_str());
348 static naRef f_alias(naContext c, naRef me, int argc, naRef* args)
352 naRef prop = naVec_get(argv, 0);
354 if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
355 else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
356 else throw string("props.alias() with bad argument");
357 } catch (const string& err) {
358 naRuntimeError(c, (char *)err.c_str());
361 return naNum((*node)->alias(al));
364 static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
367 return naNum((*node)->unalias());
370 static naRef f_getAliasTarget(naContext c, naRef me, int argc, naRef* args)
373 return propNodeGhostCreate(c, (*node)->getAliasTarget());
376 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
379 naRef path = naVec_get(argv, 0);
380 bool create = naTrue(naVec_get(argv, 1));
381 if(!naIsString(path)) return naNil();
384 n = (*node)->getNode(naStr_data(path), create);
385 } catch (const string& err) {
386 naRuntimeError(c, (char *)err.c_str());
389 return propNodeGhostCreate(c, n);
392 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
394 return propNodeGhostCreate(c, new SGPropertyNode());
397 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
399 return propNodeGhostCreate(c, globals->get_props());
406 { f_getType, "_getType" },
407 { f_getAttribute, "_getAttribute" },
408 { f_setAttribute, "_setAttribute" },
409 { f_getName, "_getName" },
410 { f_getIndex, "_getIndex" },
411 { f_getValue, "_getValue" },
412 { f_setValue, "_setValue" },
413 { f_setIntValue, "_setIntValue" },
414 { f_setBoolValue, "_setBoolValue" },
415 { f_setDoubleValue, "_setDoubleValue" },
416 { f_getParent, "_getParent" },
417 { f_getChild, "_getChild" },
418 { f_getChildren, "_getChildren" },
419 { f_removeChild, "_removeChild" },
420 { f_removeChildren, "_removeChildren" },
421 { f_alias, "_alias" },
422 { f_unalias, "_unalias" },
423 { f_getAliasTarget, "_getAliasTarget" },
424 { f_getNode, "_getNode" },
426 { f_globals, "_globals" },
430 naRef FGNasalSys::genPropsModule()
432 naRef namespc = naNewHash(_context);
433 for(int i=0; propfuncs[i].name; i++)
434 hashset(namespc, propfuncs[i].name,
435 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));