]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/nasal-props.cxx
5577d983b41ddda97a293ea1fa04e4244d09639e
[flightgear.git] / src / Scripting / nasal-props.cxx
1
2 #ifdef HAVE_CONFIG_H
3 #  include "config.h"
4 #endif
5
6 #include <cstring>
7
8 #include <simgear/math/SGMath.hxx>
9 #include <simgear/nasal/nasal.h>
10 #include <simgear/props/props.hxx>
11 #include <simgear/props/vectorPropTemplates.hxx>
12
13 #include <Main/globals.hxx>
14
15 #include "NasalSys.hxx"
16
17 using namespace std;
18
19 // Implementation of a Nasal wrapper for the SGPropertyNode class,
20 // using the Nasal "ghost" (er... Garbage collection Handle for
21 // OutSide Thingy) facility.
22 //
23 // Note that these functions appear in Nasal with prepended
24 // underscores.  They work on the low-level "ghost" objects and aren't
25 // intended for use from user code, but from Nasal code you will find
26 // in props.nas.  That is where the Nasal props.Node class is defined,
27 // which provides a saner interface along the lines of SGPropertyNode.
28
29 static void propNodeGhostDestroy(void* ghost)
30 {
31     SGPropertyNode_ptr* prop = (SGPropertyNode_ptr*)ghost;
32     delete prop;
33 }
34
35 naGhostType PropNodeGhostType = { propNodeGhostDestroy, "prop" };
36
37 static naRef propNodeGhostCreate(naContext c, SGPropertyNode* n)
38 {
39     if(!n) return naNil();
40     SGPropertyNode_ptr* ghost = new SGPropertyNode_ptr(n);
41     return naNewGhost(c, &PropNodeGhostType, ghost);
42 }
43
44 naRef FGNasalSys::propNodeGhost(SGPropertyNode* handle)
45 {
46     return propNodeGhostCreate(_context, handle);
47 }
48
49 #define NASTR(s) s ? naStr_fromdata(naNewString(c),(char*)(s),strlen(s)) : naNil()
50
51 //
52 // Standard header for the extension functions.  It turns the "ghost"
53 // found in arg[0] into a SGPropertyNode_ptr*, and then "unwraps" the
54 // vector found in the second argument into a normal-looking args
55 // array.  This allows the Nasal handlers to do things like:
56 //   Node.getChild = func { _getChild(me.ghost, arg) }
57 //
58 #define NODEARG()                                                       \
59     if(argc < 2 || !naIsGhost(args[0]) ||                               \
60        naGhost_type(args[0]) != &PropNodeGhostType)                       \
61         naRuntimeError(c, "bad argument to props function");            \
62     SGPropertyNode_ptr* node = (SGPropertyNode_ptr*)naGhost_ptr(args[0]); \
63     naRef argv = args[1]
64
65 static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
66 {
67     using namespace simgear;
68     NODEARG();
69     const char* t = "unknown";
70     switch((*node)->getType()) {
71     case props::NONE:   t = "NONE";   break;
72     case props::ALIAS:  t = "ALIAS";  break;
73     case props::BOOL:   t = "BOOL";   break;
74     case props::INT:    t = "INT";    break;
75     case props::LONG:   t = "LONG";   break;
76     case props::FLOAT:  t = "FLOAT";  break;
77     case props::DOUBLE: t = "DOUBLE"; break;
78     case props::STRING: t = "STRING"; break;
79     case props::UNSPECIFIED: t = "UNSPECIFIED"; break;
80     case props::VEC3D:  t = "VEC3D";  break;
81     case props::VEC4D:  t = "VEC4D";  break;
82     case props::EXTENDED: t = "EXTENDED";  break; // shouldn't happen
83     }
84     return NASTR(t);
85 }
86
87 static naRef f_getAttribute(naContext c, naRef me, int argc, naRef* args)
88 {
89     NODEARG();
90     if(naVec_size(argv) == 0) return naNum(unsigned((*node)->getAttributes()));
91     naRef val = naVec_get(argv, 0);
92     const char *a = naStr_data(val);
93     SGPropertyNode::Attribute attr;
94     if(!a) a = "";
95     if(!strcmp(a, "last")) return naNum(SGPropertyNode::LAST_USED_ATTRIBUTE);
96     else if(!strcmp(a, "children"))    return naNum((*node)->nChildren());
97     else if(!strcmp(a, "listeners"))   return naNum((*node)->nListeners());
98     else if(!strcmp(a, "references"))  return naNum(node->getNumRefs());
99     else if(!strcmp(a, "tied"))        return naNum((*node)->isTied());
100     else if(!strcmp(a, "alias"))       return naNum((*node)->isAlias());
101     else if(!strcmp(a, "readable"))    attr = SGPropertyNode::READ;
102     else if(!strcmp(a, "writable"))    attr = SGPropertyNode::WRITE;
103     else if(!strcmp(a, "archive"))     attr = SGPropertyNode::ARCHIVE;
104     else if(!strcmp(a, "trace-read"))  attr = SGPropertyNode::TRACE_READ;
105     else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
106     else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
107     else if(!strcmp(a, "preserve"))    attr = SGPropertyNode::PRESERVE;
108     else {
109         naRuntimeError(c, "props.getAttribute() with invalid attribute");
110         return naNil();
111     }
112     return naNum((*node)->getAttribute(attr));
113 }
114
115 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
116 {
117     NODEARG();
118     naRef val = naVec_get(argv, 0);
119     if(naVec_size(argv) == 1 && naIsNum(val)) {
120         naRef ret = naNum((*node)->getAttributes());
121         (*node)->setAttributes((int)val.num);
122         return ret;
123     }
124     SGPropertyNode::Attribute attr;
125     const char *a = naStr_data(val);
126     if(!a) a = "";
127     if(!strcmp(a, "readable"))         attr = SGPropertyNode::READ;
128     else if(!strcmp(a, "writable"))    attr = SGPropertyNode::WRITE;
129     else if(!strcmp(a, "archive"))     attr = SGPropertyNode::ARCHIVE;
130     else if(!strcmp(a, "trace-read"))  attr = SGPropertyNode::TRACE_READ;
131     else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
132     else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
133     else if(!strcmp(a, "preserve"))    attr = SGPropertyNode::PRESERVE;
134     else {
135         naRuntimeError(c, "props.setAttribute() with invalid attribute");
136         return naNil();
137     }
138     naRef ret = naNum((*node)->getAttribute(attr));
139     (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
140     return ret;
141 }
142
143 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
144 {
145     NODEARG();
146     return NASTR((*node)->getName());
147 }
148
149 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
150 {
151     NODEARG();
152     return naNum((*node)->getIndex());
153 }
154
155 template<typename T>
156 naRef makeVectorFromVec(naContext c, const T& vec)
157 {
158     const int num_components
159         = sizeof(vec.data()) / sizeof(typename T::value_type);
160     naRef vector = naNewVector(c);
161     naVec_setsize(c, vector, num_components);
162     for (int i = 0; i < num_components; ++i)
163         naVec_set(vector, i, naNum(vec[i]));
164     return vector;
165 }
166
167 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
168 {
169     using namespace simgear;
170     NODEARG();
171     switch((*node)->getType()) {
172     case props::BOOL:   case props::INT:
173     case props::LONG:   case props::FLOAT:
174     case props::DOUBLE:
175     {
176         double dv = (*node)->getDoubleValue();
177         if (osg::isNaN(dv)) {
178           SG_LOG(SG_NASAL, SG_ALERT, "Nasal getValue: property " << (*node)->getPath() << " is NaN");
179           return naNil();
180         }
181         
182         return naNum(dv);
183     }
184     
185     case props::STRING:
186     case props::UNSPECIFIED:
187         return NASTR((*node)->getStringValue());
188     case props::VEC3D:
189         return makeVectorFromVec(c, (*node)->getValue<SGVec3d>());
190     case props::VEC4D:
191         return makeVectorFromVec(c, (*node)->getValue<SGVec4d>());
192     default:
193         return naNil();
194     }
195 }
196
197 template<typename T>
198 T makeVecFromVector(naRef vector)
199 {
200     T vec;
201     const int num_components
202         = sizeof(vec.data()) / sizeof(typename T::value_type);
203     int size = naVec_size(vector);
204
205     for (int i = 0; i < num_components && i < size; ++i) {
206         naRef element = naVec_get(vector, i);
207         naRef n = naNumValue(element);
208         if (!naIsNil(n))
209             vec[i] = n.num;
210     }
211     return vec;
212 }
213
214 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
215 {
216     NODEARG();
217     naRef val = naVec_get(argv, 0);
218     bool result = false;
219     if(naIsString(val)) {
220         result = (*node)->setStringValue(naStr_data(val));
221     } else if(naIsVector(val)) {
222         if(naVec_size(val) == 3)
223             result = (*node)->setValue(makeVecFromVector<SGVec3d>(val));
224         else if(naVec_size(val) == 4)
225             result = (*node)->setValue(makeVecFromVector<SGVec4d>(val));
226         else
227             naRuntimeError(c, "props.setValue() vector value has wrong size");
228     } else {
229         naRef n = naNumValue(val);
230         if(naIsNil(n))
231             naRuntimeError(c, "props.setValue() with non-number");
232             
233         double d = naNumValue(val).num;
234         if (osg::isNaN(d)) {
235           naRuntimeError(c, "props.setValue() passed a NaN");
236         }
237         
238         result = (*node)->setDoubleValue(d);
239     }
240     return naNum(result);
241 }
242
243 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
244 {
245     NODEARG();
246     // Original code:
247     //   int iv = (int)naNumValue(naVec_get(argv, 0)).num;
248
249     // Junk to pacify the gcc-2.95.3 optimizer:
250     naRef tmp0 = naVec_get(argv, 0);
251     naRef tmp1 = naNumValue(tmp0);
252     if(naIsNil(tmp1))
253         naRuntimeError(c, "props.setIntValue() with non-number");
254     double tmp2 = tmp1.num;
255     int iv = (int)tmp2;
256
257     return naNum((*node)->setIntValue(iv));
258 }
259
260 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
261 {
262     NODEARG();
263     naRef val = naVec_get(argv, 0);
264     return naNum((*node)->setBoolValue(naTrue(val) ? true : false));
265 }
266
267 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
268 {
269     NODEARG();
270     naRef r = naNumValue(naVec_get(argv, 0));
271     if (naIsNil(r))
272         naRuntimeError(c, "props.setDoubleValue() with non-number");
273         
274     if (osg::isNaN(r.num)) {
275       naRuntimeError(c, "props.setDoubleValue() passed a NaN");
276     }
277         
278     return naNum((*node)->setDoubleValue(r.num));
279 }
280
281 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
282 {
283     NODEARG();
284     SGPropertyNode* n = (*node)->getParent();
285     if(!n) return naNil();
286     return propNodeGhostCreate(c, n);
287 }
288
289 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
290 {
291     NODEARG();
292     naRef child = naVec_get(argv, 0);
293     if(!naIsString(child)) return naNil();
294     naRef idx = naNumValue(naVec_get(argv, 1));
295     bool create = naTrue(naVec_get(argv, 2));
296     SGPropertyNode* n;
297     try {
298         if(naIsNil(idx) || !naIsNum(idx)) {
299             n = (*node)->getChild(naStr_data(child), create);
300         } else {
301             n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
302         }
303     } catch (const string& err) {
304         naRuntimeError(c, (char *)err.c_str());
305         return naNil();
306     }
307     if(!n) return naNil();
308     return propNodeGhostCreate(c, n);
309 }
310
311 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
312 {
313     NODEARG();
314     naRef result = naNewVector(c);
315     if(naIsNil(argv) || naVec_size(argv) == 0) {
316         // Get all children
317         for(int i=0; i<(*node)->nChildren(); i++)
318             naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
319     } else {
320         // Get all children of a specified name
321         naRef name = naVec_get(argv, 0);
322         if(!naIsString(name)) return naNil();
323         try {
324             vector<SGPropertyNode_ptr> children
325                 = (*node)->getChildren(naStr_data(name));
326             for(unsigned int i=0; i<children.size(); i++)
327                 naVec_append(result, propNodeGhostCreate(c, children[i]));
328         } catch (const string& err) {
329             naRuntimeError(c, (char *)err.c_str());
330             return naNil();
331         }
332     }
333     return result;
334 }
335
336 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
337 {
338     NODEARG();
339     naRef child = naVec_get(argv, 0);
340     naRef index = naVec_get(argv, 1);
341     if(!naIsString(child) || !naIsNum(index)) return naNil();
342     SGPropertyNode_ptr n = 0;
343     try {
344         n = (*node)->removeChild(naStr_data(child), (int)index.num, false);
345     } catch (const string& err) {
346         naRuntimeError(c, (char *)err.c_str());
347     }
348     return propNodeGhostCreate(c, n);
349 }
350
351 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
352 {
353     NODEARG();
354     naRef result = naNewVector(c);
355     if(naIsNil(argv) || naVec_size(argv) == 0) {
356         // Remove all children
357         for(int i = (*node)->nChildren() - 1; i >=0; i--)
358             naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
359     } else {
360         // Remove all children of a specified name
361         naRef name = naVec_get(argv, 0);
362         if(!naIsString(name)) return naNil();
363         try {
364             vector<SGPropertyNode_ptr> children
365                 = (*node)->removeChildren(naStr_data(name), false);
366             for(unsigned int i=0; i<children.size(); i++)
367                 naVec_append(result, propNodeGhostCreate(c, children[i]));
368         } catch (const string& err) {
369             naRuntimeError(c, (char *)err.c_str());
370             return naNil();
371         }
372     }
373     return result;
374 }
375
376 static naRef f_alias(naContext c, naRef me, int argc, naRef* args)
377 {
378     NODEARG();
379     SGPropertyNode* al;
380     naRef prop = naVec_get(argv, 0);
381     try {
382         if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
383         else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
384         else throw string("props.alias() with bad argument");
385     } catch (const string& err) {
386         naRuntimeError(c, (char *)err.c_str());
387         return naNil();
388     }
389     return naNum((*node)->alias(al));
390 }
391
392 static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
393 {
394     NODEARG();
395     return naNum((*node)->unalias());
396 }
397
398 static naRef f_getAliasTarget(naContext c, naRef me, int argc, naRef* args)
399 {
400     NODEARG();
401     return propNodeGhostCreate(c, (*node)->getAliasTarget());
402 }
403
404 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
405 {
406     NODEARG();
407     naRef path = naVec_get(argv, 0);
408     bool create = naTrue(naVec_get(argv, 1));
409     if(!naIsString(path)) return naNil();
410     SGPropertyNode* n;
411     try {
412         n = (*node)->getNode(naStr_data(path), create);
413     } catch (const string& err) {
414         naRuntimeError(c, (char *)err.c_str());
415         return naNil();
416     }
417     return propNodeGhostCreate(c, n);
418 }
419
420 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
421 {
422     return propNodeGhostCreate(c, new SGPropertyNode());
423 }
424
425 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
426 {
427     return propNodeGhostCreate(c, globals->get_props());
428 }
429
430 static struct {
431     naCFunction func;
432     const char* name;
433 } propfuncs[] = {
434     { f_getType, "_getType" },
435     { f_getAttribute, "_getAttribute" },
436     { f_setAttribute, "_setAttribute" },
437     { f_getName, "_getName" },
438     { f_getIndex, "_getIndex" },
439     { f_getValue, "_getValue" },
440     { f_setValue, "_setValue" },
441     { f_setIntValue, "_setIntValue" },
442     { f_setBoolValue, "_setBoolValue" },
443     { f_setDoubleValue, "_setDoubleValue" },
444     { f_getParent, "_getParent" },
445     { f_getChild, "_getChild" },
446     { f_getChildren, "_getChildren" },
447     { f_removeChild, "_removeChild" },
448     { f_removeChildren, "_removeChildren" },
449     { f_alias, "_alias" },
450     { f_unalias, "_unalias" },
451     { f_getAliasTarget, "_getAliasTarget" },
452     { f_getNode, "_getNode" },
453     { f_new, "_new" },
454     { f_globals, "_globals" },
455     { 0, 0 }
456 };
457
458 naRef FGNasalSys::genPropsModule()
459 {
460     naRef namespc = naNewHash(_context);
461     for(int i=0; propfuncs[i].name; i++)
462         hashset(namespc, propfuncs[i].name,
463                 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));
464     return namespc;
465 }