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::props;
63 const char* t = "unknown";
64 switch((*node)->getType()) {
65 case NONE: t = "NONE"; break;
66 case ALIAS: t = "ALIAS"; break;
67 case BOOL: t = "BOOL"; break;
68 case INT: t = "INT"; break;
69 case LONG: t = "LONG"; break;
70 case FLOAT: t = "FLOAT"; break;
71 case DOUBLE: t = "DOUBLE"; break;
72 case STRING: t = "STRING"; break;
73 case UNSPECIFIED: t = "UNSPECIFIED"; break;
74 case VEC3D: t = "VEC3D"; break;
75 case VEC4D: t = "VEC4D"; break;
80 static naRef f_getAttribute(naContext c, naRef me, int argc, naRef* args)
83 if(naVec_size(argv) == 0) return naNum(unsigned((*node)->getAttributes()));
84 naRef val = naVec_get(argv, 0);
85 const char *a = naStr_data(val);
86 SGPropertyNode::Attribute attr;
88 if(!strcmp(a, "last")) return naNum(SGPropertyNode::LAST_USED_ATTRIBUTE);
89 else if(!strcmp(a, "children")) return naNum((*node)->nChildren());
90 else if(!strcmp(a, "listeners")) return naNum((*node)->nListeners());
91 else if(!strcmp(a, "references")) return naNum(node->getNumRefs());
92 else if(!strcmp(a, "tied")) return naNum((*node)->isTied());
93 else if(!strcmp(a, "alias")) return naNum((*node)->isAlias());
94 else if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
95 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
96 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
97 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
98 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
99 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
101 naRuntimeError(c, "props.getAttribute() with invalid attribute");
104 return naNum((*node)->getAttribute(attr));
107 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
110 naRef val = naVec_get(argv, 0);
111 if(naVec_size(argv) == 1 && naIsNum(val)) {
112 naRef ret = naNum((*node)->getAttributes());
113 (*node)->setAttributes((int)val.num);
116 SGPropertyNode::Attribute attr;
117 const char *a = naStr_data(val);
119 if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
120 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
121 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
122 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
123 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
124 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
126 naRuntimeError(c, "props.setAttribute() with invalid attribute");
129 naRef ret = naNum((*node)->getAttribute(attr));
130 (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
134 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
137 return NASTR((*node)->getName());
140 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
143 return naNum((*node)->getIndex());
147 naRef makeVectorFromVec(naContext c, const T& vec)
149 const unsigned num_components
150 = sizeof(vec.data()) / sizeof(typename T::value_type);
151 naRef vector = naNewVector(c);
152 naVec_setsize(vector, num_components);
153 for (int i = 0; i < num_components; ++i)
154 naVec_set(vector, i, naNum(vec[i]));
158 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
160 using namespace simgear::props;
162 switch((*node)->getType()) {
164 case LONG: case FLOAT:
166 return naNum((*node)->getDoubleValue());
169 return NASTR((*node)->getStringValue());
171 return makeVectorFromVec(c, (*node)->getValue<SGVec3d>());
173 return makeVectorFromVec(c, (*node)->getValue<SGVec4d>());
180 T makeVecFromVector(naRef vector)
183 const unsigned num_components
184 = sizeof(vec.data()) / sizeof(typename T::value_type);
185 int size = naVec_size(vector);
187 for (int i = 0; i < num_components && i < size; ++i) {
188 naRef element = naVec_get(vector, i);
189 naRef n = naNumValue(element);
196 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
199 naRef val = naVec_get(argv, 0);
201 if(naIsString(val)) {
202 result = (*node)->setStringValue(naStr_data(val));
203 } else if(naIsVector(val)) {
204 if(naVec_size(val) == 3)
205 result = (*node)->setValue(makeVecFromVector<SGVec3d>(val));
206 else if(naVec_size(val) == 4)
207 result = (*node)->setValue(makeVecFromVector<SGVec4d>(val));
209 naRuntimeError(c, "props.setValue() vector value has wrong size");
211 naRef n = naNumValue(val);
213 naRuntimeError(c, "props.setValue() with non-number");
214 result = (*node)->setDoubleValue(naNumValue(val).num);
216 return naNum(result);
219 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
223 // int iv = (int)naNumValue(naVec_get(argv, 0)).num;
225 // Junk to pacify the gcc-2.95.3 optimizer:
226 naRef tmp0 = naVec_get(argv, 0);
227 naRef tmp1 = naNumValue(tmp0);
229 naRuntimeError(c, "props.setIntValue() with non-number");
230 double tmp2 = tmp1.num;
233 return naNum((*node)->setIntValue(iv));
236 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
239 naRef val = naVec_get(argv, 0);
240 return naNum((*node)->setBoolValue(naTrue(val) ? true : false));
243 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
246 naRef r = naNumValue(naVec_get(argv, 0));
248 naRuntimeError(c, "props.setDoubleValue() with non-number");
249 return naNum((*node)->setDoubleValue(r.num));
252 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
255 SGPropertyNode* n = (*node)->getParent();
256 if(!n) return naNil();
257 return propNodeGhostCreate(c, n);
260 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
263 naRef child = naVec_get(argv, 0);
264 if(!naIsString(child)) return naNil();
265 naRef idx = naNumValue(naVec_get(argv, 1));
266 bool create = naTrue(naVec_get(argv, 2));
269 if(naIsNil(idx) || !naIsNum(idx)) {
270 n = (*node)->getChild(naStr_data(child), create);
272 n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
274 } catch (const string& err) {
275 naRuntimeError(c, (char *)err.c_str());
278 if(!n) return naNil();
279 return propNodeGhostCreate(c, n);
282 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
285 naRef result = naNewVector(c);
286 if(naIsNil(argv) || naVec_size(argv) == 0) {
288 for(int i=0; i<(*node)->nChildren(); i++)
289 naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
291 // Get all children of a specified name
292 naRef name = naVec_get(argv, 0);
293 if(!naIsString(name)) return naNil();
295 vector<SGPropertyNode_ptr> children
296 = (*node)->getChildren(naStr_data(name));
297 for(unsigned int i=0; i<children.size(); i++)
298 naVec_append(result, propNodeGhostCreate(c, children[i]));
299 } catch (const string& err) {
300 naRuntimeError(c, (char *)err.c_str());
307 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
310 naRef child = naVec_get(argv, 0);
311 naRef index = naVec_get(argv, 1);
312 if(!naIsString(child) || !naIsNum(index)) return naNil();
313 SGPropertyNode_ptr n = 0;
315 n = (*node)->removeChild(naStr_data(child), (int)index.num, false);
316 } catch (const string& err) {
317 naRuntimeError(c, (char *)err.c_str());
319 return propNodeGhostCreate(c, n);
322 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
325 naRef result = naNewVector(c);
326 if(naIsNil(argv) || naVec_size(argv) == 0) {
327 // Remove all children
328 for(int i = (*node)->nChildren() - 1; i >=0; i--)
329 naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
331 // Remove 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)->removeChildren(naStr_data(name), false);
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_alias(naContext c, naRef me, int argc, naRef* args)
351 naRef prop = naVec_get(argv, 0);
353 if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
354 else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
355 else throw string("props.alias() with bad argument");
356 } catch (const string& err) {
357 naRuntimeError(c, (char *)err.c_str());
360 return naNum((*node)->alias(al));
363 static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
366 return naNum((*node)->unalias());
369 static naRef f_getAliasTarget(naContext c, naRef me, int argc, naRef* args)
372 return propNodeGhostCreate(c, (*node)->getAliasTarget());
375 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
378 naRef path = naVec_get(argv, 0);
379 bool create = naTrue(naVec_get(argv, 1));
380 if(!naIsString(path)) return naNil();
383 n = (*node)->getNode(naStr_data(path), create);
384 } catch (const string& err) {
385 naRuntimeError(c, (char *)err.c_str());
388 return propNodeGhostCreate(c, n);
391 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
393 return propNodeGhostCreate(c, new SGPropertyNode());
396 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
398 return propNodeGhostCreate(c, globals->get_props());
405 { f_getType, "_getType" },
406 { f_getAttribute, "_getAttribute" },
407 { f_setAttribute, "_setAttribute" },
408 { f_getName, "_getName" },
409 { f_getIndex, "_getIndex" },
410 { f_getValue, "_getValue" },
411 { f_setValue, "_setValue" },
412 { f_setIntValue, "_setIntValue" },
413 { f_setBoolValue, "_setBoolValue" },
414 { f_setDoubleValue, "_setDoubleValue" },
415 { f_getParent, "_getParent" },
416 { f_getChild, "_getChild" },
417 { f_getChildren, "_getChildren" },
418 { f_removeChild, "_removeChild" },
419 { f_removeChildren, "_removeChildren" },
420 { f_alias, "_alias" },
421 { f_unalias, "_unalias" },
422 { f_getAliasTarget, "_getAliasTarget" },
423 { f_getNode, "_getNode" },
425 { f_globals, "_globals" },
429 naRef FGNasalSys::genPropsModule()
431 naRef namespc = naNewHash(_context);
432 for(int i=0; propfuncs[i].name; i++)
433 hashset(namespc, propfuncs[i].name,
434 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));