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 };
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)
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 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 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);
163 if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
165 naRef n = naNumValue(val);
167 naRuntimeError(c, "props.setValue() with non-number");
168 (*node)->setDoubleValue(naNumValue(val).num);
173 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
177 // int iv = (int)naNumValue(naVec_get(argv, 0)).num;
179 // Junk to pacify the gcc-2.95.3 optimizer:
180 naRef tmp0 = naVec_get(argv, 0);
181 naRef tmp1 = naNumValue(tmp0);
183 naRuntimeError(c, "props.setIntValue() with non-number");
184 double tmp2 = tmp1.num;
187 (*node)->setIntValue(iv);
191 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
194 naRef val = naVec_get(argv, 0);
195 (*node)->setBoolValue(naTrue(val) ? true : false);
199 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
202 naRef r = naNumValue(naVec_get(argv, 0));
204 naRuntimeError(c, "props.setDoubleValue() with non-number");
205 (*node)->setDoubleValue(r.num);
209 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
212 SGPropertyNode* n = (*node)->getParent();
213 if(!n) return naNil();
214 return propNodeGhostCreate(c, n);
217 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
220 naRef child = naVec_get(argv, 0);
221 if(!naIsString(child)) return naNil();
222 naRef idx = naNumValue(naVec_get(argv, 1));
223 bool create = naTrue(naVec_get(argv, 2));
226 if(naIsNil(idx) || !naIsNum(idx)) {
227 n = (*node)->getChild(naStr_data(child), create);
229 n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
231 } catch (const string& err) {
232 naRuntimeError(c, (char *)err.c_str());
235 if(!n) return naNil();
236 return propNodeGhostCreate(c, n);
239 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
242 naRef result = naNewVector(c);
243 if(naIsNil(argv) || naVec_size(argv) == 0) {
245 for(int i=0; i<(*node)->nChildren(); i++)
246 naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
248 // Get all children of a specified name
249 naRef name = naVec_get(argv, 0);
250 if(!naIsString(name)) return naNil();
252 vector<SGPropertyNode_ptr> children
253 = (*node)->getChildren(naStr_data(name));
254 for(unsigned int i=0; i<children.size(); i++)
255 naVec_append(result, propNodeGhostCreate(c, children[i]));
256 } catch (const string& err) {
257 naRuntimeError(c, (char *)err.c_str());
264 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
267 naRef child = naVec_get(argv, 0);
268 naRef index = naVec_get(argv, 1);
269 if(!naIsString(child) || !naIsNum(index)) return naNil();
270 SGPropertyNode* n = 0;
272 n = (*node)->removeChild(naStr_data(child), (int)index.num, false);
273 } catch (const string& err) {
274 naRuntimeError(c, (char *)err.c_str());
276 return propNodeGhostCreate(c, n);
279 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
282 naRef result = naNewVector(c);
283 if(naIsNil(argv) || naVec_size(argv) == 0) {
284 // Remove all children
285 for(int i = (*node)->nChildren() - 1; i >=0; i--)
286 naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
288 // Remove all children of a specified name
289 naRef name = naVec_get(argv, 0);
290 if(!naIsString(name)) return naNil();
292 vector<SGPropertyNode_ptr> children
293 = (*node)->removeChildren(naStr_data(name), false);
294 for(unsigned int i=0; i<children.size(); i++)
295 naVec_append(result, propNodeGhostCreate(c, children[i]));
296 } catch (const string& err) {
297 naRuntimeError(c, (char *)err.c_str());
304 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
307 naRef path = naVec_get(argv, 0);
308 bool create = naTrue(naVec_get(argv, 1));
309 if(!naIsString(path)) return naNil();
312 n = (*node)->getNode(naStr_data(path), create);
313 } catch (const string& err) {
314 naRuntimeError(c, (char *)err.c_str());
317 return propNodeGhostCreate(c, n);
320 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
322 return propNodeGhostCreate(c, new SGPropertyNode());
325 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
327 return propNodeGhostCreate(c, globals->get_props());
334 { f_getType, "_getType" },
335 { f_getAttribute, "_getAttribute" },
336 { f_setAttribute, "_setAttribute" },
337 { f_getName, "_getName" },
338 { f_getIndex, "_getIndex" },
339 { f_getValue, "_getValue" },
340 { f_setValue, "_setValue" },
341 { f_setIntValue, "_setIntValue" },
342 { f_setBoolValue, "_setBoolValue" },
343 { f_setDoubleValue, "_setDoubleValue" },
344 { f_getParent, "_getParent" },
345 { f_getChild, "_getChild" },
346 { f_getChildren, "_getChildren" },
347 { f_removeChild, "_removeChild" },
348 { f_removeChildren, "_removeChildren" },
349 { f_getNode, "_getNode" },
351 { f_globals, "_globals" },
355 naRef FGNasalSys::genPropsModule()
357 naRef namespc = naNewHash(_context);
358 for(int i=0; propfuncs[i].name; i++)
359 hashset(namespc, propfuncs[i].name,
360 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));