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