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