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 naRef ghost = naVec_get(args, 0); \
49 SGPropertyNode_ptr* node = (SGPropertyNode_ptr*)naGhost_ptr(ghost); \
50 if(!node || naGhost_type(ghost) != &PropNodeGhostType) \
52 if(naVec_size(args) > 1) { \
53 args = naVec_get(args, 1); \
54 if(!naIsVector(args)) return naNil(); \
55 } else { args = naNil(); }
57 static naRef f_getType(naContext c, naRef args)
61 switch((*node)->getType()) {
62 case SGPropertyNode::NONE: t = "NONE"; break;
63 case SGPropertyNode::ALIAS: t = "ALIAS"; break;
64 case SGPropertyNode::BOOL: t = "BOOL"; break;
65 case SGPropertyNode::INT: t = "INT"; break;
66 case SGPropertyNode::LONG: t = "LONG"; break;
67 case SGPropertyNode::FLOAT: t = "FLOAT"; break;
68 case SGPropertyNode::DOUBLE: t = "DOUBLE"; break;
69 case SGPropertyNode::STRING: t = "STRING"; break;
70 case SGPropertyNode::UNSPECIFIED: t = "UNSPECIFIED"; break;
75 static naRef f_getName(naContext c, naRef args)
78 return NASTR((*node)->getName());
81 static naRef f_getIndex(naContext c, naRef args)
84 return naNum((*node)->getIndex());
87 static naRef f_getValue(naContext c, naRef args)
90 switch((*node)->getType()) {
91 case SGPropertyNode::BOOL: case SGPropertyNode::INT:
92 case SGPropertyNode::LONG: case SGPropertyNode::FLOAT:
93 case SGPropertyNode::DOUBLE:
94 return naNum((*node)->getDoubleValue());
95 case SGPropertyNode::STRING:
96 case SGPropertyNode::UNSPECIFIED:
97 return NASTR((*node)->getStringValue());
102 static naRef f_setValue(naContext c, naRef args)
105 naRef val = naVec_get(args, 0);
106 if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
107 else (*node)->setDoubleValue(naNumValue(val).num);
111 static naRef f_setIntValue(naContext c, naRef args)
114 int iv = (int)naNumValue(naVec_get(args, 0)).num;
115 (*node)->setIntValue(iv);
119 static naRef f_setBoolValue(naContext c, naRef args)
122 naRef val = naVec_get(args, 0);
123 (*node)->setBoolValue(naTrue(val) ? true : false);
127 static naRef f_setDoubleValue(naContext c, naRef args)
130 (*node)->setDoubleValue(naNumValue(naVec_get(args, 0)).num);
134 static naRef f_getParent(naContext c, naRef args)
137 SGPropertyNode* n = (*node)->getParent();
138 if(!n) return naNil();
139 return propNodeGhostCreate(c, n);
142 static naRef f_getChild(naContext c, naRef args)
145 naRef child = naVec_get(args, 0);
146 if(!naIsString(child)) return naNil();
147 SGPropertyNode* n = (*node)->getChild(naStr_data(child));
148 if(!n) return naNil();
149 return propNodeGhostCreate(c, n);
152 static naRef f_getChildren(naContext c, naRef args)
155 naRef result = naNewVector(c);
156 if(naIsNil(args) || naVec_size(args) == 0) {
158 for(int i=0; i<(*node)->nChildren(); i++)
159 naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
161 // Get all children of a specified name
162 naRef name = naVec_get(args, 0);
163 if(!naIsString(name)) return naNil();
164 vector<SGPropertyNode_ptr> children
165 = (*node)->getChildren(naStr_data(name));
166 for(int i=0; i<children.size(); i++)
167 naVec_append(result, propNodeGhostCreate(c, children[i]));
172 static naRef f_removeChild(naContext c, naRef args)
175 naRef child = naVec_get(args, 0);
176 naRef index = naVec_get(args, 1);
177 if(!naIsString(child) || !naIsNum(index)) return naNil();
178 (*node)->removeChild(naStr_data(child), (int)index.num);
182 static naRef f_getNode(naContext c, naRef args)
185 naRef path = naVec_get(args, 0);
186 bool create = naTrue(naVec_get(args, 1));
187 if(!naIsString(path)) return naNil();
188 SGPropertyNode* n = (*node)->getNode(naStr_data(path), create);
189 return propNodeGhostCreate(c, n);
192 static naRef f_new(naContext c, naRef args)
194 return propNodeGhostCreate(c, new SGPropertyNode());
197 static naRef f_globals(naContext c, naRef args)
199 return propNodeGhostCreate(c, globals->get_props());
206 { f_getType, "_getType" },
207 { f_getName, "_getName" },
208 { f_getIndex, "_getIndex" },
209 { f_getValue, "_getValue" },
210 { f_setValue, "_setValue" },
211 { f_setIntValue, "_setIntValue" },
212 { f_setBoolValue, "_setBoolValue" },
213 { f_setDoubleValue, "_setDoubleValue" },
214 { f_getParent, "_getParent" },
215 { f_getChild, "_getChild" },
216 { f_getChildren, "_getChildren" },
217 { f_removeChild, "_removeChild" },
218 { f_getNode, "_getNode" },
220 { f_globals, "_globals" },
224 naRef FGNasalSys::genPropsModule()
226 naRef namespc = naNewHash(_context);
227 for(int i=0; propfuncs[i].name; i++)
228 hashset(namespc, propfuncs[i].name,
229 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));