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