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