1 #include <simgear/nasal/nasal.h>
2 #include <simgear/props/props.hxx>
4 #include <Main/globals.hxx>
6 #include "NasalSys.hxx"
8 // Implementation of a Nasal wrapper for the SGPropertyNode class,
9 // using the Nasal "ghost" (er... Garbage collection Handle for
10 // OutSide Thingy) facility.
12 // Note that these functions appear in Nasal with prepended
13 // underscores. They work on the low-level "ghost" objects and aren't
14 // intended for use from user code, but from Nasal code you will find
15 // in props.nas. That is where the Nasal props.Node class is defined,
16 // which provides a saner interface along the lines of SGPropertyNode.
18 static void propNodeGhostDestroy(void* ghost)
20 SGPropertyNode_ptr* prop = (SGPropertyNode_ptr*)ghost;
24 naGhostType PropNodeGhostType = { propNodeGhostDestroy };
26 static naRef propNodeGhostCreate(naContext c, SGPropertyNode* n)
28 if(!n) return naNil();
29 SGPropertyNode_ptr* ghost = new SGPropertyNode_ptr(n);
30 return naNewGhost(c, &PropNodeGhostType, ghost);
33 naRef FGNasalSys::propNodeGhost(SGPropertyNode* handle)
35 return propNodeGhostCreate(_context, handle);
38 #define NASTR(s) s ? naStr_fromdata(naNewString(c),(char*)(s),strlen(s)) : naNil()
41 // Standard header for the extension functions. It turns the "ghost"
42 // found in arg[0] into a SGPropertyNode_ptr*, and then "unwraps" the
43 // vector found in the second argument into a normal-looking args
44 // array. This allows the Nasal handlers to do things like:
45 // Node.getChild = func { _getChild(me.ghost, arg) }
48 if(argc < 2 || !naIsGhost(args[0]) || \
49 naGhost_type(args[0]) != &PropNodeGhostType) \
50 naRuntimeError(c, "bad argument to props function"); \
51 SGPropertyNode_ptr* node = (SGPropertyNode_ptr*)naGhost_ptr(args[0]); \
54 static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
58 switch((*node)->getType()) {
59 case SGPropertyNode::NONE: t = "NONE"; break;
60 case SGPropertyNode::ALIAS: t = "ALIAS"; break;
61 case SGPropertyNode::BOOL: t = "BOOL"; break;
62 case SGPropertyNode::INT: t = "INT"; break;
63 case SGPropertyNode::LONG: t = "LONG"; break;
64 case SGPropertyNode::FLOAT: t = "FLOAT"; break;
65 case SGPropertyNode::DOUBLE: t = "DOUBLE"; break;
66 case SGPropertyNode::STRING: t = "STRING"; break;
67 case SGPropertyNode::UNSPECIFIED: t = "UNSPECIFIED"; break;
72 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
75 return NASTR((*node)->getName());
78 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
81 return naNum((*node)->getIndex());
84 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
87 switch((*node)->getType()) {
88 case SGPropertyNode::BOOL: case SGPropertyNode::INT:
89 case SGPropertyNode::LONG: case SGPropertyNode::FLOAT:
90 case SGPropertyNode::DOUBLE:
91 return naNum((*node)->getDoubleValue());
92 case SGPropertyNode::STRING:
93 case SGPropertyNode::UNSPECIFIED:
94 return NASTR((*node)->getStringValue());
99 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
102 naRef val = naVec_get(argv, 0);
103 if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
104 else (*node)->setDoubleValue(naNumValue(val).num);
108 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
112 // int iv = (int)naNumValue(naVec_get(argv, 0)).num;
114 // Junk to pacify the gcc-2.95.3 optimizer:
115 naRef tmp0 = naVec_get(argv, 0);
116 naRef tmp1 = naNumValue(tmp0);
117 double tmp2 = tmp1.num;
120 (*node)->setIntValue(iv);
124 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
127 naRef val = naVec_get(argv, 0);
128 (*node)->setBoolValue(naTrue(val) ? true : false);
132 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
135 (*node)->setDoubleValue(naNumValue(naVec_get(argv, 0)).num);
139 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
142 SGPropertyNode* n = (*node)->getParent();
143 if(!n) return naNil();
144 return propNodeGhostCreate(c, n);
147 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
150 naRef child = naVec_get(argv, 0);
151 if(!naIsString(child)) return naNil();
152 naRef idx = naNumValue(naVec_get(argv, 1));
153 bool create = naTrue(naVec_get(argv, 2));
155 if(naIsNil(idx) || !naIsNum(idx)) {
156 n = (*node)->getChild(naStr_data(child), create);
158 n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
160 if(!n) return naNil();
161 return propNodeGhostCreate(c, n);
164 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
167 naRef result = naNewVector(c);
168 if(naIsNil(argv) || naVec_size(argv) == 0) {
170 for(int i=0; i<(*node)->nChildren(); i++)
171 naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
173 // Get all children of a specified name
174 naRef name = naVec_get(argv, 0);
175 if(!naIsString(name)) return naNil();
176 vector<SGPropertyNode_ptr> children
177 = (*node)->getChildren(naStr_data(name));
178 for(unsigned int i=0; i<children.size(); i++)
179 naVec_append(result, propNodeGhostCreate(c, children[i]));
184 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
187 naRef child = naVec_get(argv, 0);
188 naRef index = naVec_get(argv, 1);
189 if(!naIsString(child) || !naIsNum(index)) return naNil();
190 (*node)->removeChild(naStr_data(child), (int)index.num, false);
194 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
197 naRef result = naNewVector(c);
198 if(naIsNil(argv) || naVec_size(argv) == 0) {
199 // Remove all children
200 for(int i = (*node)->nChildren() - 1; i >=0; i--)
201 naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
203 // Remove all children of a specified name
204 naRef name = naVec_get(argv, 0);
205 if(!naIsString(name)) return naNil();
206 vector<SGPropertyNode_ptr> children
207 = (*node)->removeChildren(naStr_data(name), false);
208 for(unsigned int i=0; i<children.size(); i++)
209 naVec_append(result, propNodeGhostCreate(c, children[i]));
214 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
217 naRef path = naVec_get(argv, 0);
218 bool create = naTrue(naVec_get(argv, 1));
219 if(!naIsString(path)) return naNil();
220 SGPropertyNode* n = (*node)->getNode(naStr_data(path), create);
221 return propNodeGhostCreate(c, n);
224 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
226 return propNodeGhostCreate(c, new SGPropertyNode());
229 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
231 return propNodeGhostCreate(c, globals->get_props());
238 { f_getType, "_getType" },
239 { f_getName, "_getName" },
240 { f_getIndex, "_getIndex" },
241 { f_getValue, "_getValue" },
242 { f_setValue, "_setValue" },
243 { f_setIntValue, "_setIntValue" },
244 { f_setBoolValue, "_setBoolValue" },
245 { f_setDoubleValue, "_setDoubleValue" },
246 { f_getParent, "_getParent" },
247 { f_getChild, "_getChild" },
248 { f_getChildren, "_getChildren" },
249 { f_removeChild, "_removeChild" },
250 { f_removeChildren, "_removeChildren" },
251 { f_getNode, "_getNode" },
253 { f_globals, "_globals" },
257 naRef FGNasalSys::genPropsModule()
259 naRef namespc = naNewHash(_context);
260 for(int i=0; propfuncs[i].name; i++)
261 hashset(namespc, propfuncs[i].name,
262 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));