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, "tied")) return naNum((*node)->isTied());
87 else if(!strcmp(a, "alias")) return naNum((*node)->isAlias());
88 else if(!strcmp(a, "read")) attr = SGPropertyNode::READ;
89 else if(!strcmp(a, "write")) attr = SGPropertyNode::WRITE;
90 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
91 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
92 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
93 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
95 naRuntimeError(c, "props.getAttribute() with invalid attribute");
98 return naNum((*node)->getAttribute(attr));
101 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
104 naRef val = naVec_get(argv, 0);
105 if(naVec_size(argv) == 1 && naIsNum(val)) {
106 naRef ret = naNum((*node)->getAttributes());
107 (*node)->setAttributes((int)val.num);
110 SGPropertyNode::Attribute attr;
111 char *a = naStr_data(val);
113 if(!strcmp(a, "read")) attr = SGPropertyNode::READ;
114 else if(!strcmp(a, "write")) attr = SGPropertyNode::WRITE;
115 else if(!strcmp(a, "archive")) attr = SGPropertyNode::ARCHIVE;
116 else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
117 else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
118 else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
120 naRuntimeError(c, "props.setAttribute() with invalid attribute");
123 naRef ret = naNum((*node)->getAttribute(attr));
124 (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
128 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
131 return NASTR((*node)->getName());
134 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
137 return naNum((*node)->getIndex());
140 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
143 switch((*node)->getType()) {
144 case SGPropertyNode::BOOL: case SGPropertyNode::INT:
145 case SGPropertyNode::LONG: case SGPropertyNode::FLOAT:
146 case SGPropertyNode::DOUBLE:
147 return naNum((*node)->getDoubleValue());
148 case SGPropertyNode::STRING:
149 case SGPropertyNode::UNSPECIFIED:
150 return NASTR((*node)->getStringValue());
156 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
159 naRef val = naVec_get(argv, 0);
160 if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
162 naRef n = naNumValue(val);
164 naRuntimeError(c, "props.setValue() with non-number");
165 (*node)->setDoubleValue(naNumValue(val).num);
170 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
174 // int iv = (int)naNumValue(naVec_get(argv, 0)).num;
176 // Junk to pacify the gcc-2.95.3 optimizer:
177 naRef tmp0 = naVec_get(argv, 0);
178 naRef tmp1 = naNumValue(tmp0);
180 naRuntimeError(c, "props.setIntValue() with non-number");
181 double tmp2 = tmp1.num;
184 (*node)->setIntValue(iv);
188 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
191 naRef val = naVec_get(argv, 0);
192 (*node)->setBoolValue(naTrue(val) ? true : false);
196 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
199 naRef r = naNumValue(naVec_get(argv, 0));
201 naRuntimeError(c, "props.setDoubleValue() with non-number");
202 (*node)->setDoubleValue(r.num);
206 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
209 SGPropertyNode* n = (*node)->getParent();
210 if(!n) return naNil();
211 return propNodeGhostCreate(c, n);
214 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
217 naRef child = naVec_get(argv, 0);
218 if(!naIsString(child)) return naNil();
219 naRef idx = naNumValue(naVec_get(argv, 1));
220 bool create = naTrue(naVec_get(argv, 2));
223 if(naIsNil(idx) || !naIsNum(idx)) {
224 n = (*node)->getChild(naStr_data(child), create);
226 n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
228 } catch (const string& err) {
229 naRuntimeError(c, (char *)err.c_str());
232 if(!n) return naNil();
233 return propNodeGhostCreate(c, n);
236 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
239 naRef result = naNewVector(c);
240 if(naIsNil(argv) || naVec_size(argv) == 0) {
242 for(int i=0; i<(*node)->nChildren(); i++)
243 naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
245 // Get all children of a specified name
246 naRef name = naVec_get(argv, 0);
247 if(!naIsString(name)) return naNil();
249 vector<SGPropertyNode_ptr> children
250 = (*node)->getChildren(naStr_data(name));
251 for(unsigned int i=0; i<children.size(); i++)
252 naVec_append(result, propNodeGhostCreate(c, children[i]));
253 } catch (const string& err) {
254 naRuntimeError(c, (char *)err.c_str());
261 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
264 naRef child = naVec_get(argv, 0);
265 naRef index = naVec_get(argv, 1);
266 if(!naIsString(child) || !naIsNum(index)) return naNil();
268 (*node)->removeChild(naStr_data(child), (int)index.num, false);
269 } catch (const string& err) {
270 naRuntimeError(c, (char *)err.c_str());
275 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
278 naRef result = naNewVector(c);
279 if(naIsNil(argv) || naVec_size(argv) == 0) {
280 // Remove all children
281 for(int i = (*node)->nChildren() - 1; i >=0; i--)
282 naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
284 // Remove all children of a specified name
285 naRef name = naVec_get(argv, 0);
286 if(!naIsString(name)) return naNil();
288 vector<SGPropertyNode_ptr> children
289 = (*node)->removeChildren(naStr_data(name), false);
290 for(unsigned int i=0; i<children.size(); i++)
291 naVec_append(result, propNodeGhostCreate(c, children[i]));
292 } catch (const string& err) {
293 naRuntimeError(c, (char *)err.c_str());
300 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
303 naRef path = naVec_get(argv, 0);
304 bool create = naTrue(naVec_get(argv, 1));
305 if(!naIsString(path)) return naNil();
308 n = (*node)->getNode(naStr_data(path), create);
309 } catch (const string& err) {
310 naRuntimeError(c, (char *)err.c_str());
313 return propNodeGhostCreate(c, n);
316 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
318 return propNodeGhostCreate(c, new SGPropertyNode());
321 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
323 return propNodeGhostCreate(c, globals->get_props());
330 { f_getType, "_getType" },
331 { f_getAttribute, "_getAttribute" },
332 { f_setAttribute, "_setAttribute" },
333 { f_getName, "_getName" },
334 { f_getIndex, "_getIndex" },
335 { f_getValue, "_getValue" },
336 { f_setValue, "_setValue" },
337 { f_setIntValue, "_setIntValue" },
338 { f_setBoolValue, "_setBoolValue" },
339 { f_setDoubleValue, "_setDoubleValue" },
340 { f_getParent, "_getParent" },
341 { f_getChild, "_getChild" },
342 { f_getChildren, "_getChildren" },
343 { f_removeChild, "_removeChild" },
344 { f_removeChildren, "_removeChildren" },
345 { f_getNode, "_getNode" },
347 { f_globals, "_globals" },
351 naRef FGNasalSys::genPropsModule()
353 naRef namespc = naNewHash(_context);
354 for(int i=0; propfuncs[i].name; i++)
355 hashset(namespc, propfuncs[i].name,
356 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));