]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/nasal-props.cxx
8a467db9a2ba9105a38e49be424c359452246054
[flightgear.git] / src / Scripting / nasal-props.cxx
1
2 #ifdef HAVE_CONFIG_H
3 #  include "config.h"
4 #endif
5
6 #include <simgear/nasal/nasal.h>
7 #include <simgear/props/props.hxx>
8
9 #include <Main/globals.hxx>
10
11 #include "NasalSys.hxx"
12
13 // Implementation of a Nasal wrapper for the SGPropertyNode class,
14 // using the Nasal "ghost" (er... Garbage collection Handle for
15 // OutSide Thingy) facility.
16 //
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.
22
23 static void propNodeGhostDestroy(void* ghost)
24 {
25     SGPropertyNode_ptr* prop = (SGPropertyNode_ptr*)ghost;
26     delete prop;
27 }
28
29 naGhostType PropNodeGhostType = { propNodeGhostDestroy };
30
31 static naRef propNodeGhostCreate(naContext c, SGPropertyNode* n)
32 {
33     if(!n) return naNil();
34     SGPropertyNode_ptr* ghost = new SGPropertyNode_ptr(n);
35     return naNewGhost(c, &PropNodeGhostType, ghost);
36 }
37
38 naRef FGNasalSys::propNodeGhost(SGPropertyNode* handle)
39 {
40     return propNodeGhostCreate(_context, handle);
41 }
42
43 #define NASTR(s) s ? naStr_fromdata(naNewString(c),(char*)(s),strlen(s)) : naNil()
44
45 //
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) }
51 //
52 #define NODEARG()                                                       \
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]); \
57     naRef argv = args[1]
58
59 static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
60 {
61     NODEARG();
62     char* t = "unknown";
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;
73     }
74     return NASTR(t);
75 }
76
77 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
78 {
79     NODEARG();
80     return NASTR((*node)->getName());
81 }
82
83 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
84 {
85     NODEARG();
86     return naNum((*node)->getIndex());
87 }
88
89 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
90 {
91     NODEARG();
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());
100     default:
101         return naNil();
102     }
103 }
104
105 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
106 {
107     NODEARG();
108     naRef val = naVec_get(argv, 0);
109     if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
110     else {
111         naRef n = naNumValue(val);
112         if(naIsNil(n))
113             naRuntimeError(c, "props.setValue() with non-number");
114         (*node)->setDoubleValue(naNumValue(val).num);
115     }
116     return naNil();
117 }
118
119 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
120 {
121     NODEARG();
122     // Original code:
123     //   int iv = (int)naNumValue(naVec_get(argv, 0)).num;
124
125     // Junk to pacify the gcc-2.95.3 optimizer:
126     naRef tmp0 = naVec_get(argv, 0);
127     naRef tmp1 = naNumValue(tmp0);
128     if(naIsNil(tmp1))
129         naRuntimeError(c, "props.setIntValue() with non-number");
130     double tmp2 = tmp1.num;
131     int iv = (int)tmp2;
132
133     (*node)->setIntValue(iv);
134     return naNil();
135 }
136
137 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
138 {
139     NODEARG();
140     naRef val = naVec_get(argv, 0);
141     (*node)->setBoolValue(naTrue(val) ? true : false);
142     return naNil();
143 }
144
145 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
146 {
147     NODEARG();
148     naRef r = naNumValue(naVec_get(argv, 0));
149     if(naIsNil(r))
150         naRuntimeError(c, "props.setDoubleValue() with non-number");
151     (*node)->setDoubleValue(r.num);
152     return naNil();
153 }
154
155 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
156 {
157     NODEARG();
158     SGPropertyNode* n = (*node)->getParent();
159     if(!n) return naNil();
160     return propNodeGhostCreate(c, n);
161 }
162
163 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
164 {
165     NODEARG();
166     naRef child = naVec_get(argv, 0);
167     if(!naIsString(child)) return naNil();
168     naRef idx = naNumValue(naVec_get(argv, 1));
169     bool create = naTrue(naVec_get(argv, 2));
170     SGPropertyNode* n;
171     try {
172         if(naIsNil(idx) || !naIsNum(idx)) {
173             n = (*node)->getChild(naStr_data(child), create);
174         } else {
175             n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
176         }
177     } catch (const string& err) {
178         naRuntimeError(c, (char *)err.c_str());
179         return naNil();
180     }
181     if(!n) return naNil();
182     return propNodeGhostCreate(c, n);
183 }
184
185 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
186 {
187     NODEARG();
188     naRef result = naNewVector(c);
189     if(naIsNil(argv) || naVec_size(argv) == 0) {
190         // Get all children
191         for(int i=0; i<(*node)->nChildren(); i++)
192             naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
193     } else {
194         // Get all children of a specified name
195         naRef name = naVec_get(argv, 0);
196         if(!naIsString(name)) return naNil();
197         try {
198             vector<SGPropertyNode_ptr> children
199                 = (*node)->getChildren(naStr_data(name));
200             for(unsigned int i=0; i<children.size(); i++)
201                 naVec_append(result, propNodeGhostCreate(c, children[i]));
202         } catch (const string& err) {
203             naRuntimeError(c, (char *)err.c_str());
204             return naNil();
205         }
206     }
207     return result;
208 }
209
210 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
211 {
212     NODEARG();
213     naRef child = naVec_get(argv, 0);
214     naRef index = naVec_get(argv, 1);
215     if(!naIsString(child) || !naIsNum(index)) return naNil();
216     try {
217         (*node)->removeChild(naStr_data(child), (int)index.num, false);
218     } catch (const string& err) {
219         naRuntimeError(c, (char *)err.c_str());
220     }
221     return naNil();
222 }
223
224 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
225 {
226     NODEARG();
227     naRef result = naNewVector(c);
228     if(naIsNil(argv) || naVec_size(argv) == 0) {
229         // Remove all children
230         for(int i = (*node)->nChildren() - 1; i >=0; i--)
231             naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
232     } else {
233         // Remove all children of a specified name
234         naRef name = naVec_get(argv, 0);
235         if(!naIsString(name)) return naNil();
236         try {
237             vector<SGPropertyNode_ptr> children
238                 = (*node)->removeChildren(naStr_data(name), false);
239             for(unsigned int i=0; i<children.size(); i++)
240                 naVec_append(result, propNodeGhostCreate(c, children[i]));
241         } catch (const string& err) {
242             naRuntimeError(c, (char *)err.c_str());
243             return naNil();
244         }
245     }
246     return result;
247 }
248
249 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
250 {
251     NODEARG();
252     naRef path = naVec_get(argv, 0);
253     bool create = naTrue(naVec_get(argv, 1));
254     if(!naIsString(path)) return naNil();
255     SGPropertyNode* n;
256     try {
257         n = (*node)->getNode(naStr_data(path), create);
258     } catch (const string& err) {
259         naRuntimeError(c, (char *)err.c_str());
260         return naNil();
261     }
262     return propNodeGhostCreate(c, n);
263 }
264
265 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
266 {
267     return propNodeGhostCreate(c, new SGPropertyNode());
268 }
269
270 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
271 {
272     return propNodeGhostCreate(c, globals->get_props());
273 }
274
275 static struct {
276     naCFunction func;
277     char* name;
278 } propfuncs[] = {
279     { f_getType, "_getType" },
280     { f_getName, "_getName" },
281     { f_getIndex, "_getIndex" },
282     { f_getValue, "_getValue" },
283     { f_setValue, "_setValue" },
284     { f_setIntValue, "_setIntValue" },
285     { f_setBoolValue, "_setBoolValue" },
286     { f_setDoubleValue, "_setDoubleValue" },
287     { f_getParent, "_getParent" },
288     { f_getChild, "_getChild" },
289     { f_getChildren, "_getChildren" },
290     { f_removeChild, "_removeChild" },
291     { f_removeChildren, "_removeChildren" },
292     { f_getNode, "_getNode" },
293     { f_new, "_new" },
294     { f_globals, "_globals" },
295     { 0, 0 }
296 };
297
298 naRef FGNasalSys::genPropsModule()
299 {
300     naRef namespc = naNewHash(_context);
301     for(int i=0; propfuncs[i].name; i++)
302         hashset(namespc, propfuncs[i].name,
303                 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));
304     return namespc;
305 }