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)
62 const char* t = "unknown";
63 switch((*node)->getType()) {
64 case SGPropertyNode::NONE: t = "NONE"; break;
65 case SGPropertyNode::ALIAS: t = "ALIAS"; break;
66 case SGPropertyNode::BOOL: t = "BOOL"; break;
67 case SGPropertyNode::INT: t = "INT"; break;
68 case SGPropertyNode::LONG: t = "LONG"; break;
69 case SGPropertyNode::FLOAT: t = "FLOAT"; break;
70 case SGPropertyNode::DOUBLE: t = "DOUBLE"; break;
71 case SGPropertyNode::STRING: t = "STRING"; break;
72 case SGPropertyNode::UNSPECIFIED: t = "UNSPECIFIED"; break;
77 static naRef f_getAttribute(naContext c, naRef me, int argc, naRef* args)
80 if(naVec_size(argv) == 0) return naNum(unsigned((*node)->getAttributes()));
81 naRef val = naVec_get(argv, 0);
82 const char *a = naStr_data(val);
83 SGPropertyNode::Attribute attr;
85 if(!strcmp(a, "last")) return naNum(SGPropertyNode::LAST_USED_ATTRIBUTE);
86 else if(!strcmp(a, "children")) return naNum((*node)->nChildren());
87 else if(!strcmp(a, "listeners")) return naNum((*node)->nListeners());
88 else if(!strcmp(a, "references")) return naNum(node->getNumRefs());
89 else if(!strcmp(a, "tied")) return naNum((*node)->isTied());
90 else if(!strcmp(a, "alias")) return naNum((*node)->isAlias());
91 else if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
92 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
93 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
94 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
95 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
96 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
98 naRuntimeError(c, "props.getAttribute() with invalid attribute");
101 return naNum((*node)->getAttribute(attr));
104 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
107 naRef val = naVec_get(argv, 0);
108 if(naVec_size(argv) == 1 && naIsNum(val)) {
109 naRef ret = naNum((*node)->getAttributes());
110 (*node)->setAttributes((int)val.num);
113 SGPropertyNode::Attribute attr;
114 const char *a = naStr_data(val);
116 if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
117 else if(!strcmp(a, "writable")) attr = SGPropertyNode::WRITE;
118 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
119 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
120 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
121 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
123 naRuntimeError(c, "props.setAttribute() with invalid attribute");
126 naRef ret = naNum((*node)->getAttribute(attr));
127 (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
131 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
134 return NASTR((*node)->getName());
137 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
140 return naNum((*node)->getIndex());
143 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
146 switch((*node)->getType()) {
147 case SGPropertyNode::BOOL: case SGPropertyNode::INT:
148 case SGPropertyNode::LONG: case SGPropertyNode::FLOAT:
149 case SGPropertyNode::DOUBLE:
150 return naNum((*node)->getDoubleValue());
151 case SGPropertyNode::STRING:
152 case SGPropertyNode::UNSPECIFIED:
153 return NASTR((*node)->getStringValue());
159 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
162 naRef val = naVec_get(argv, 0);
164 if(naIsString(val)) result = (*node)->setStringValue(naStr_data(val));
166 naRef n = naNumValue(val);
168 naRuntimeError(c, "props.setValue() with non-number");
169 result = (*node)->setDoubleValue(naNumValue(val).num);
171 return naNum(result);
174 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
178 // int iv = (int)naNumValue(naVec_get(argv, 0)).num;
180 // Junk to pacify the gcc-2.95.3 optimizer:
181 naRef tmp0 = naVec_get(argv, 0);
182 naRef tmp1 = naNumValue(tmp0);
184 naRuntimeError(c, "props.setIntValue() with non-number");
185 double tmp2 = tmp1.num;
188 return naNum((*node)->setIntValue(iv));
191 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
194 naRef val = naVec_get(argv, 0);
195 return naNum((*node)->setBoolValue(naTrue(val) ? true : false));
198 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
201 naRef r = naNumValue(naVec_get(argv, 0));
203 naRuntimeError(c, "props.setDoubleValue() with non-number");
204 return naNum((*node)->setDoubleValue(r.num));
207 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
210 SGPropertyNode* n = (*node)->getParent();
211 if(!n) return naNil();
212 return propNodeGhostCreate(c, n);
215 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
218 naRef child = naVec_get(argv, 0);
219 if(!naIsString(child)) return naNil();
220 naRef idx = naNumValue(naVec_get(argv, 1));
221 bool create = naTrue(naVec_get(argv, 2));
224 if(naIsNil(idx) || !naIsNum(idx)) {
225 n = (*node)->getChild(naStr_data(child), create);
227 n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
229 } catch (const string& err) {
230 naRuntimeError(c, (char *)err.c_str());
233 if(!n) return naNil();
234 return propNodeGhostCreate(c, n);
237 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
240 naRef result = naNewVector(c);
241 if(naIsNil(argv) || naVec_size(argv) == 0) {
243 for(int i=0; i<(*node)->nChildren(); i++)
244 naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
246 // Get all children of a specified name
247 naRef name = naVec_get(argv, 0);
248 if(!naIsString(name)) return naNil();
250 vector<SGPropertyNode_ptr> children
251 = (*node)->getChildren(naStr_data(name));
252 for(unsigned int i=0; i<children.size(); i++)
253 naVec_append(result, propNodeGhostCreate(c, children[i]));
254 } catch (const string& err) {
255 naRuntimeError(c, (char *)err.c_str());
262 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
265 naRef child = naVec_get(argv, 0);
266 naRef index = naVec_get(argv, 1);
267 if(!naIsString(child) || !naIsNum(index)) return naNil();
268 SGPropertyNode_ptr n = 0;
270 n = (*node)->removeChild(naStr_data(child), (int)index.num, false);
271 } catch (const string& err) {
272 naRuntimeError(c, (char *)err.c_str());
274 return propNodeGhostCreate(c, n);
277 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
280 naRef result = naNewVector(c);
281 if(naIsNil(argv) || naVec_size(argv) == 0) {
282 // Remove all children
283 for(int i = (*node)->nChildren() - 1; i >=0; i--)
284 naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
286 // Remove all children of a specified name
287 naRef name = naVec_get(argv, 0);
288 if(!naIsString(name)) return naNil();
290 vector<SGPropertyNode_ptr> children
291 = (*node)->removeChildren(naStr_data(name), false);
292 for(unsigned int i=0; i<children.size(); i++)
293 naVec_append(result, propNodeGhostCreate(c, children[i]));
294 } catch (const string& err) {
295 naRuntimeError(c, (char *)err.c_str());
302 static naRef f_alias(naContext c, naRef me, int argc, naRef* args)
306 naRef prop = naVec_get(argv, 0);
308 if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
309 else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
310 else throw string("props.alias() with bad argument");
311 } catch (const string& err) {
312 naRuntimeError(c, (char *)err.c_str());
315 return naNum((*node)->alias(al));
318 static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
321 return naNum((*node)->unalias());
324 static naRef f_getAliasTarget(naContext c, naRef me, int argc, naRef* args)
327 return propNodeGhostCreate(c, (*node)->getAliasTarget());
330 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
333 naRef path = naVec_get(argv, 0);
334 bool create = naTrue(naVec_get(argv, 1));
335 if(!naIsString(path)) return naNil();
338 n = (*node)->getNode(naStr_data(path), create);
339 } catch (const string& err) {
340 naRuntimeError(c, (char *)err.c_str());
343 return propNodeGhostCreate(c, n);
346 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
348 return propNodeGhostCreate(c, new SGPropertyNode());
351 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
353 return propNodeGhostCreate(c, globals->get_props());
360 { f_getType, "_getType" },
361 { f_getAttribute, "_getAttribute" },
362 { f_setAttribute, "_setAttribute" },
363 { f_getName, "_getName" },
364 { f_getIndex, "_getIndex" },
365 { f_getValue, "_getValue" },
366 { f_setValue, "_setValue" },
367 { f_setIntValue, "_setIntValue" },
368 { f_setBoolValue, "_setBoolValue" },
369 { f_setDoubleValue, "_setDoubleValue" },
370 { f_getParent, "_getParent" },
371 { f_getChild, "_getChild" },
372 { f_getChildren, "_getChildren" },
373 { f_removeChild, "_removeChild" },
374 { f_removeChildren, "_removeChildren" },
375 { f_alias, "_alias" },
376 { f_unalias, "_unalias" },
377 { f_getAliasTarget, "_getAliasTarget" },
378 { f_getNode, "_getNode" },
380 { f_globals, "_globals" },
384 naRef FGNasalSys::genPropsModule()
386 naRef namespc = naNewHash(_context);
387 for(int i=0; propfuncs[i].name; i++)
388 hashset(namespc, propfuncs[i].name,
389 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));