]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/nasal-props.cxx
4ffc4d6016a4536298d2c8fec2ee86848280d354
[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, "prop" };
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     using namespace simgear;
62     NODEARG();
63     const char* t = "unknown";
64     switch((*node)->getType()) {
65     case props::NONE:   t = "NONE";   break;
66     case props::ALIAS:  t = "ALIAS";  break;
67     case props::BOOL:   t = "BOOL";   break;
68     case props::INT:    t = "INT";    break;
69     case props::LONG:   t = "LONG";   break;
70     case props::FLOAT:  t = "FLOAT";  break;
71     case props::DOUBLE: t = "DOUBLE"; break;
72     case props::STRING: t = "STRING"; break;
73     case props::UNSPECIFIED: t = "UNSPECIFIED"; break;
74     case props::VEC3D:  t = "VEC3D";  break;
75     case props::VEC4D:  t = "VEC4D";  break;
76     case props::EXTENDED: t = "EXTENDED";  break; // shouldn't happen
77     }
78     return NASTR(t);
79 }
80
81 static naRef f_getAttribute(naContext c, naRef me, int argc, naRef* args)
82 {
83     NODEARG();
84     if(naVec_size(argv) == 0) return naNum(unsigned((*node)->getAttributes()));
85     naRef val = naVec_get(argv, 0);
86     const char *a = naStr_data(val);
87     SGPropertyNode::Attribute attr;
88     if(!a) a = "";
89     if(!strcmp(a, "last")) return naNum(SGPropertyNode::LAST_USED_ATTRIBUTE);
90     else if(!strcmp(a, "children"))    return naNum((*node)->nChildren());
91     else if(!strcmp(a, "listeners"))   return naNum((*node)->nListeners());
92     else if(!strcmp(a, "references"))  return naNum(node->getNumRefs());
93     else if(!strcmp(a, "tied"))        return naNum((*node)->isTied());
94     else if(!strcmp(a, "alias"))       return naNum((*node)->isAlias());
95     else if(!strcmp(a, "readable"))    attr = SGPropertyNode::READ;
96     else if(!strcmp(a, "writable"))    attr = SGPropertyNode::WRITE;
97     else if(!strcmp(a, "archive"))     attr = SGPropertyNode::ARCHIVE;
98     else if(!strcmp(a, "trace-read"))  attr = SGPropertyNode::TRACE_READ;
99     else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
100     else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
101     else {
102         naRuntimeError(c, "props.getAttribute() with invalid attribute");
103         return naNil();
104     }
105     return naNum((*node)->getAttribute(attr));
106 }
107
108 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
109 {
110     NODEARG();
111     naRef val = naVec_get(argv, 0);
112     if(naVec_size(argv) == 1 && naIsNum(val)) {
113         naRef ret = naNum((*node)->getAttributes());
114         (*node)->setAttributes((int)val.num);
115         return ret;
116     }
117     SGPropertyNode::Attribute attr;
118     const char *a = naStr_data(val);
119     if(!a) a = "";
120     if(!strcmp(a, "readable"))         attr = SGPropertyNode::READ;
121     else if(!strcmp(a, "writable"))    attr = SGPropertyNode::WRITE;
122     else if(!strcmp(a, "archive"))     attr = SGPropertyNode::ARCHIVE;
123     else if(!strcmp(a, "trace-read"))  attr = SGPropertyNode::TRACE_READ;
124     else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
125     else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
126     else {
127         naRuntimeError(c, "props.setAttribute() with invalid attribute");
128         return naNil();
129     }
130     naRef ret = naNum((*node)->getAttribute(attr));
131     (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
132     return ret;
133 }
134
135 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
136 {
137     NODEARG();
138     return NASTR((*node)->getName());
139 }
140
141 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
142 {
143     NODEARG();
144     return naNum((*node)->getIndex());
145 }
146
147 template<typename T>
148 naRef makeVectorFromVec(naContext c, const T& vec)
149 {
150     const int num_components
151         = sizeof(vec.data()) / sizeof(typename T::value_type);
152     naRef vector = naNewVector(c);
153     naVec_setsize(vector, num_components);
154     for (int i = 0; i < num_components; ++i)
155         naVec_set(vector, i, naNum(vec[i]));
156     return vector;
157 }
158
159 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
160 {
161     using namespace simgear;
162     NODEARG();
163     switch((*node)->getType()) {
164     case props::BOOL:   case props::INT:
165     case props::LONG:   case props::FLOAT:
166     case props::DOUBLE:
167         return naNum((*node)->getDoubleValue());
168     case props::STRING:
169     case props::UNSPECIFIED:
170         return NASTR((*node)->getStringValue());
171     case props::VEC3D:
172         return makeVectorFromVec(c, (*node)->getValue<SGVec3d>());
173     case props::VEC4D:
174         return makeVectorFromVec(c, (*node)->getValue<SGVec4d>());
175     default:
176         return naNil();
177     }
178 }
179
180 template<typename T>
181 T makeVecFromVector(naRef vector)
182 {
183     T vec;
184     const int num_components
185         = sizeof(vec.data()) / sizeof(typename T::value_type);
186     int size = naVec_size(vector);
187
188     for (int i = 0; i < num_components && i < size; ++i) {
189         naRef element = naVec_get(vector, i);
190         naRef n = naNumValue(element);
191         if (!naIsNil(n))
192             vec[i] = n.num;
193     }
194     return vec;
195 }
196
197 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
198 {
199     NODEARG();
200     naRef val = naVec_get(argv, 0);
201     bool result = false;
202     if(naIsString(val)) {
203         result = (*node)->setStringValue(naStr_data(val));
204     } else if(naIsVector(val)) {
205         if(naVec_size(val) == 3)
206             result = (*node)->setValue(makeVecFromVector<SGVec3d>(val));
207         else if(naVec_size(val) == 4)
208             result = (*node)->setValue(makeVecFromVector<SGVec4d>(val));
209         else
210             naRuntimeError(c, "props.setValue() vector value has wrong size");
211     } else {
212         naRef n = naNumValue(val);
213         if(naIsNil(n))
214             naRuntimeError(c, "props.setValue() with non-number");
215         result = (*node)->setDoubleValue(naNumValue(val).num);
216     }
217     return naNum(result);
218 }
219
220 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
221 {
222     NODEARG();
223     // Original code:
224     //   int iv = (int)naNumValue(naVec_get(argv, 0)).num;
225
226     // Junk to pacify the gcc-2.95.3 optimizer:
227     naRef tmp0 = naVec_get(argv, 0);
228     naRef tmp1 = naNumValue(tmp0);
229     if(naIsNil(tmp1))
230         naRuntimeError(c, "props.setIntValue() with non-number");
231     double tmp2 = tmp1.num;
232     int iv = (int)tmp2;
233
234     return naNum((*node)->setIntValue(iv));
235 }
236
237 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
238 {
239     NODEARG();
240     naRef val = naVec_get(argv, 0);
241     return naNum((*node)->setBoolValue(naTrue(val) ? true : false));
242 }
243
244 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
245 {
246     NODEARG();
247     naRef r = naNumValue(naVec_get(argv, 0));
248     if(naIsNil(r))
249         naRuntimeError(c, "props.setDoubleValue() with non-number");
250     return naNum((*node)->setDoubleValue(r.num));
251 }
252
253 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
254 {
255     NODEARG();
256     SGPropertyNode* n = (*node)->getParent();
257     if(!n) return naNil();
258     return propNodeGhostCreate(c, n);
259 }
260
261 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
262 {
263     NODEARG();
264     naRef child = naVec_get(argv, 0);
265     if(!naIsString(child)) return naNil();
266     naRef idx = naNumValue(naVec_get(argv, 1));
267     bool create = naTrue(naVec_get(argv, 2));
268     SGPropertyNode* n;
269     try {
270         if(naIsNil(idx) || !naIsNum(idx)) {
271             n = (*node)->getChild(naStr_data(child), create);
272         } else {
273             n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
274         }
275     } catch (const string& err) {
276         naRuntimeError(c, (char *)err.c_str());
277         return naNil();
278     }
279     if(!n) return naNil();
280     return propNodeGhostCreate(c, n);
281 }
282
283 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
284 {
285     NODEARG();
286     naRef result = naNewVector(c);
287     if(naIsNil(argv) || naVec_size(argv) == 0) {
288         // Get all children
289         for(int i=0; i<(*node)->nChildren(); i++)
290             naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
291     } else {
292         // Get all children of a specified name
293         naRef name = naVec_get(argv, 0);
294         if(!naIsString(name)) return naNil();
295         try {
296             vector<SGPropertyNode_ptr> children
297                 = (*node)->getChildren(naStr_data(name));
298             for(unsigned int i=0; i<children.size(); i++)
299                 naVec_append(result, propNodeGhostCreate(c, children[i]));
300         } catch (const string& err) {
301             naRuntimeError(c, (char *)err.c_str());
302             return naNil();
303         }
304     }
305     return result;
306 }
307
308 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
309 {
310     NODEARG();
311     naRef child = naVec_get(argv, 0);
312     naRef index = naVec_get(argv, 1);
313     if(!naIsString(child) || !naIsNum(index)) return naNil();
314     SGPropertyNode_ptr n = 0;
315     try {
316         n = (*node)->removeChild(naStr_data(child), (int)index.num, false);
317     } catch (const string& err) {
318         naRuntimeError(c, (char *)err.c_str());
319     }
320     return propNodeGhostCreate(c, n);
321 }
322
323 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
324 {
325     NODEARG();
326     naRef result = naNewVector(c);
327     if(naIsNil(argv) || naVec_size(argv) == 0) {
328         // Remove all children
329         for(int i = (*node)->nChildren() - 1; i >=0; i--)
330             naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
331     } else {
332         // Remove all children of a specified name
333         naRef name = naVec_get(argv, 0);
334         if(!naIsString(name)) return naNil();
335         try {
336             vector<SGPropertyNode_ptr> children
337                 = (*node)->removeChildren(naStr_data(name), false);
338             for(unsigned int i=0; i<children.size(); i++)
339                 naVec_append(result, propNodeGhostCreate(c, children[i]));
340         } catch (const string& err) {
341             naRuntimeError(c, (char *)err.c_str());
342             return naNil();
343         }
344     }
345     return result;
346 }
347
348 static naRef f_alias(naContext c, naRef me, int argc, naRef* args)
349 {
350     NODEARG();
351     SGPropertyNode* al;
352     naRef prop = naVec_get(argv, 0);
353     try {
354         if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
355         else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
356         else throw string("props.alias() with bad argument");
357     } catch (const string& err) {
358         naRuntimeError(c, (char *)err.c_str());
359         return naNil();
360     }
361     return naNum((*node)->alias(al));
362 }
363
364 static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
365 {
366     NODEARG();
367     return naNum((*node)->unalias());
368 }
369
370 static naRef f_getAliasTarget(naContext c, naRef me, int argc, naRef* args)
371 {
372     NODEARG();
373     return propNodeGhostCreate(c, (*node)->getAliasTarget());
374 }
375
376 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
377 {
378     NODEARG();
379     naRef path = naVec_get(argv, 0);
380     bool create = naTrue(naVec_get(argv, 1));
381     if(!naIsString(path)) return naNil();
382     SGPropertyNode* n;
383     try {
384         n = (*node)->getNode(naStr_data(path), create);
385     } catch (const string& err) {
386         naRuntimeError(c, (char *)err.c_str());
387         return naNil();
388     }
389     return propNodeGhostCreate(c, n);
390 }
391
392 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
393 {
394     return propNodeGhostCreate(c, new SGPropertyNode());
395 }
396
397 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
398 {
399     return propNodeGhostCreate(c, globals->get_props());
400 }
401
402 static struct {
403     naCFunction func;
404     const char* name;
405 } propfuncs[] = {
406     { f_getType, "_getType" },
407     { f_getAttribute, "_getAttribute" },
408     { f_setAttribute, "_setAttribute" },
409     { f_getName, "_getName" },
410     { f_getIndex, "_getIndex" },
411     { f_getValue, "_getValue" },
412     { f_setValue, "_setValue" },
413     { f_setIntValue, "_setIntValue" },
414     { f_setBoolValue, "_setBoolValue" },
415     { f_setDoubleValue, "_setDoubleValue" },
416     { f_getParent, "_getParent" },
417     { f_getChild, "_getChild" },
418     { f_getChildren, "_getChildren" },
419     { f_removeChild, "_removeChild" },
420     { f_removeChildren, "_removeChildren" },
421     { f_alias, "_alias" },
422     { f_unalias, "_unalias" },
423     { f_getAliasTarget, "_getAliasTarget" },
424     { f_getNode, "_getNode" },
425     { f_new, "_new" },
426     { f_globals, "_globals" },
427     { 0, 0 }
428 };
429
430 naRef FGNasalSys::genPropsModule()
431 {
432     naRef namespc = naNewHash(_context);
433     for(int i=0; propfuncs[i].name; i++)
434         hashset(namespc, propfuncs[i].name,
435                 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));
436     return namespc;
437 }