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_getName(naContext c, naRef me, int argc, naRef* args)
80 return NASTR((*node)->getName());
83 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
86 return naNum((*node)->getIndex());
89 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
92 switch((*node)->getType()) {
93 case SGPropertyNode::BOOL: case SGPropertyNode::INT:
94 case SGPropertyNode::LONG: case SGPropertyNode::FLOAT:
95 case SGPropertyNode::DOUBLE:
96 return naNum((*node)->getDoubleValue());
97 case SGPropertyNode::STRING:
98 case SGPropertyNode::UNSPECIFIED:
99 return NASTR((*node)->getStringValue());
105 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
108 naRef val = naVec_get(argv, 0);
109 if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
110 else (*node)->setDoubleValue(naNumValue(val).num);
114 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
118 // int iv = (int)naNumValue(naVec_get(argv, 0)).num;
120 // Junk to pacify the gcc-2.95.3 optimizer:
121 naRef tmp0 = naVec_get(argv, 0);
122 naRef tmp1 = naNumValue(tmp0);
123 double tmp2 = tmp1.num;
126 (*node)->setIntValue(iv);
130 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
133 naRef val = naVec_get(argv, 0);
134 (*node)->setBoolValue(naTrue(val) ? true : false);
138 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
141 (*node)->setDoubleValue(naNumValue(naVec_get(argv, 0)).num);
145 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
148 SGPropertyNode* n = (*node)->getParent();
149 if(!n) return naNil();
150 return propNodeGhostCreate(c, n);
153 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
156 naRef child = naVec_get(argv, 0);
157 if(!naIsString(child)) return naNil();
158 naRef idx = naNumValue(naVec_get(argv, 1));
159 bool create = naTrue(naVec_get(argv, 2));
162 if(naIsNil(idx) || !naIsNum(idx)) {
163 n = (*node)->getChild(naStr_data(child), create);
165 n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
167 } catch (const string& err) {
168 naRuntimeError(c, (char *)err.c_str());
171 if(!n) return naNil();
172 return propNodeGhostCreate(c, n);
175 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
178 naRef result = naNewVector(c);
179 if(naIsNil(argv) || naVec_size(argv) == 0) {
181 for(int i=0; i<(*node)->nChildren(); i++)
182 naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
184 // Get all children of a specified name
185 naRef name = naVec_get(argv, 0);
186 if(!naIsString(name)) return naNil();
188 vector<SGPropertyNode_ptr> children
189 = (*node)->getChildren(naStr_data(name));
190 for(unsigned int i=0; i<children.size(); i++)
191 naVec_append(result, propNodeGhostCreate(c, children[i]));
192 } catch (const string& err) {
193 naRuntimeError(c, (char *)err.c_str());
200 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
203 naRef child = naVec_get(argv, 0);
204 naRef index = naVec_get(argv, 1);
205 if(!naIsString(child) || !naIsNum(index)) return naNil();
207 (*node)->removeChild(naStr_data(child), (int)index.num, false);
208 } catch (const string& err) {
209 naRuntimeError(c, (char *)err.c_str());
214 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
217 naRef result = naNewVector(c);
218 if(naIsNil(argv) || naVec_size(argv) == 0) {
219 // Remove all children
220 for(int i = (*node)->nChildren() - 1; i >=0; i--)
221 naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
223 // Remove all children of a specified name
224 naRef name = naVec_get(argv, 0);
225 if(!naIsString(name)) return naNil();
227 vector<SGPropertyNode_ptr> children
228 = (*node)->removeChildren(naStr_data(name), false);
229 for(unsigned int i=0; i<children.size(); i++)
230 naVec_append(result, propNodeGhostCreate(c, children[i]));
231 } catch (const string& err) {
232 naRuntimeError(c, (char *)err.c_str());
239 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
242 naRef path = naVec_get(argv, 0);
243 bool create = naTrue(naVec_get(argv, 1));
244 if(!naIsString(path)) return naNil();
247 n = (*node)->getNode(naStr_data(path), create);
248 } catch (const string& err) {
249 naRuntimeError(c, (char *)err.c_str());
252 return propNodeGhostCreate(c, n);
255 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
257 return propNodeGhostCreate(c, new SGPropertyNode());
260 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
262 return propNodeGhostCreate(c, globals->get_props());
269 { f_getType, "_getType" },
270 { f_getName, "_getName" },
271 { f_getIndex, "_getIndex" },
272 { f_getValue, "_getValue" },
273 { f_setValue, "_setValue" },
274 { f_setIntValue, "_setIntValue" },
275 { f_setBoolValue, "_setBoolValue" },
276 { f_setDoubleValue, "_setDoubleValue" },
277 { f_getParent, "_getParent" },
278 { f_getChild, "_getChild" },
279 { f_getChildren, "_getChildren" },
280 { f_removeChild, "_removeChild" },
281 { f_removeChildren, "_removeChildren" },
282 { f_getNode, "_getNode" },
284 { f_globals, "_globals" },
288 naRef FGNasalSys::genPropsModule()
290 naRef namespc = naNewHash(_context);
291 for(int i=0; propfuncs[i].name; i++)
292 hashset(namespc, propfuncs[i].name,
293 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));