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));
154 if(naIsNil(idx) || !naIsNum(idx)) {
155 n = (*node)->getChild(naStr_data(child));
157 n = (*node)->getChild(naStr_data(child), (int)idx.num);
159 if(!n) return naNil();
160 return propNodeGhostCreate(c, n);
163 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
166 naRef result = naNewVector(c);
167 if(naIsNil(argv) || naVec_size(argv) == 0) {
169 for(int i=0; i<(*node)->nChildren(); i++)
170 naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
172 // Get all children of a specified name
173 naRef name = naVec_get(argv, 0);
174 if(!naIsString(name)) return naNil();
175 vector<SGPropertyNode_ptr> children
176 = (*node)->getChildren(naStr_data(name));
177 for(unsigned int i=0; i<children.size(); i++)
178 naVec_append(result, propNodeGhostCreate(c, children[i]));
183 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
186 naRef child = naVec_get(argv, 0);
187 naRef index = naVec_get(argv, 1);
188 if(!naIsString(child) || !naIsNum(index)) return naNil();
189 (*node)->removeChild(naStr_data(child), (int)index.num);
193 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
196 naRef path = naVec_get(argv, 0);
197 bool create = naTrue(naVec_get(argv, 1));
198 if(!naIsString(path)) return naNil();
199 SGPropertyNode* n = (*node)->getNode(naStr_data(path), create);
200 return propNodeGhostCreate(c, n);
203 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
205 return propNodeGhostCreate(c, new SGPropertyNode());
208 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
210 return propNodeGhostCreate(c, globals->get_props());
217 { f_getType, "_getType" },
218 { f_getName, "_getName" },
219 { f_getIndex, "_getIndex" },
220 { f_getValue, "_getValue" },
221 { f_setValue, "_setValue" },
222 { f_setIntValue, "_setIntValue" },
223 { f_setBoolValue, "_setBoolValue" },
224 { f_setDoubleValue, "_setDoubleValue" },
225 { f_getParent, "_getParent" },
226 { f_getChild, "_getChild" },
227 { f_getChildren, "_getChildren" },
228 { f_removeChild, "_removeChild" },
229 { f_getNode, "_getNode" },
231 { f_globals, "_globals" },
235 naRef FGNasalSys::genPropsModule()
237 naRef namespc = naNewHash(_context);
238 for(int i=0; propfuncs[i].name; i++)
239 hashset(namespc, propfuncs[i].name,
240 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));