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((*node)->getAttributes());
81 naRef val = naVec_get(argv, 0);
82 char *a = naStr_data(val);
83 SGPropertyNode::Attribute attr;
85 if(!strcmp(a, "children")) return naNum((*node)->nChildren());
86 else if(!strcmp(a, "listeners")) return naNum((*node)->nListeners());
87 else if(!strcmp(a, "tied")) return naNum((*node)->isTied());
88 else if(!strcmp(a, "alias")) return naNum((*node)->isAlias());
89 else if(!strcmp(a, "read")) attr = SGPropertyNode::READ;
90 else if(!strcmp(a, "write")) attr = SGPropertyNode::WRITE;
91 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
92 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
93 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
94 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
96 naRuntimeError(c, "props.getAttribute() with invalid attribute");
99 return naNum((*node)->getAttribute(attr));
102 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
105 naRef val = naVec_get(argv, 0);
106 if(naVec_size(argv) == 1 && naIsNum(val)) {
107 naRef ret = naNum((*node)->getAttributes());
108 (*node)->setAttributes((int)val.num);
111 SGPropertyNode::Attribute attr;
112 char *a = naStr_data(val);
114 if(!strcmp(a, "read")) attr = SGPropertyNode::READ;
115 else if(!strcmp(a, "write")) attr = SGPropertyNode::WRITE;
116 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
117 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
118 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
119 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
121 naRuntimeError(c, "props.setAttribute() with invalid attribute");
124 naRef ret = naNum((*node)->getAttribute(attr));
125 (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
129 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
132 return NASTR((*node)->getName());
135 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
138 return naNum((*node)->getIndex());
141 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
144 switch((*node)->getType()) {
145 case SGPropertyNode::BOOL: case SGPropertyNode::INT:
146 case SGPropertyNode::LONG: case SGPropertyNode::FLOAT:
147 case SGPropertyNode::DOUBLE:
148 return naNum((*node)->getDoubleValue());
149 case SGPropertyNode::STRING:
150 case SGPropertyNode::UNSPECIFIED:
151 return NASTR((*node)->getStringValue());
157 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
160 naRef val = naVec_get(argv, 0);
161 if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
163 naRef n = naNumValue(val);
165 naRuntimeError(c, "props.setValue() with non-number");
166 (*node)->setDoubleValue(naNumValue(val).num);
171 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
175 // int iv = (int)naNumValue(naVec_get(argv, 0)).num;
177 // Junk to pacify the gcc-2.95.3 optimizer:
178 naRef tmp0 = naVec_get(argv, 0);
179 naRef tmp1 = naNumValue(tmp0);
181 naRuntimeError(c, "props.setIntValue() with non-number");
182 double tmp2 = tmp1.num;
185 (*node)->setIntValue(iv);
189 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
192 naRef val = naVec_get(argv, 0);
193 (*node)->setBoolValue(naTrue(val) ? true : false);
197 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
200 naRef r = naNumValue(naVec_get(argv, 0));
202 naRuntimeError(c, "props.setDoubleValue() with non-number");
203 (*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();
269 (*node)->removeChild(naStr_data(child), (int)index.num, false);
270 } catch (const string& err) {
271 naRuntimeError(c, (char *)err.c_str());
276 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
279 naRef result = naNewVector(c);
280 if(naIsNil(argv) || naVec_size(argv) == 0) {
281 // Remove all children
282 for(int i = (*node)->nChildren() - 1; i >=0; i--)
283 naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
285 // Remove all children of a specified name
286 naRef name = naVec_get(argv, 0);
287 if(!naIsString(name)) return naNil();
289 vector<SGPropertyNode_ptr> children
290 = (*node)->removeChildren(naStr_data(name), false);
291 for(unsigned int i=0; i<children.size(); i++)
292 naVec_append(result, propNodeGhostCreate(c, children[i]));
293 } catch (const string& err) {
294 naRuntimeError(c, (char *)err.c_str());
301 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
304 naRef path = naVec_get(argv, 0);
305 bool create = naTrue(naVec_get(argv, 1));
306 if(!naIsString(path)) return naNil();
309 n = (*node)->getNode(naStr_data(path), create);
310 } catch (const string& err) {
311 naRuntimeError(c, (char *)err.c_str());
314 return propNodeGhostCreate(c, n);
317 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
319 return propNodeGhostCreate(c, new SGPropertyNode());
322 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
324 return propNodeGhostCreate(c, globals->get_props());
331 { f_getType, "_getType" },
332 { f_getAttribute, "_getAttribute" },
333 { f_setAttribute, "_setAttribute" },
334 { f_getName, "_getName" },
335 { f_getIndex, "_getIndex" },
336 { f_getValue, "_getValue" },
337 { f_setValue, "_setValue" },
338 { f_setIntValue, "_setIntValue" },
339 { f_setBoolValue, "_setBoolValue" },
340 { f_setDoubleValue, "_setDoubleValue" },
341 { f_getParent, "_getParent" },
342 { f_getChild, "_getChild" },
343 { f_getChildren, "_getChildren" },
344 { f_removeChild, "_removeChild" },
345 { f_removeChildren, "_removeChildren" },
346 { f_getNode, "_getNode" },
348 { f_globals, "_globals" },
352 naRef FGNasalSys::genPropsModule()
354 naRef namespc = naNewHash(_context);
355 for(int i=0; propfuncs[i].name; i++)
356 hashset(namespc, propfuncs[i].name,
357 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));