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