]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/nasal-props.cxx
f_alias: catch SGPropertyNode exceptions and generate runtime error
[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     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     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     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     if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
164     else {
165         naRef n = naNumValue(val);
166         if(naIsNil(n))
167             naRuntimeError(c, "props.setValue() with non-number");
168         (*node)->setDoubleValue(naNumValue(val).num);
169     }
170     return naNil();
171 }
172
173 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
174 {
175     NODEARG();
176     // Original code:
177     //   int iv = (int)naNumValue(naVec_get(argv, 0)).num;
178
179     // Junk to pacify the gcc-2.95.3 optimizer:
180     naRef tmp0 = naVec_get(argv, 0);
181     naRef tmp1 = naNumValue(tmp0);
182     if(naIsNil(tmp1))
183         naRuntimeError(c, "props.setIntValue() with non-number");
184     double tmp2 = tmp1.num;
185     int iv = (int)tmp2;
186
187     (*node)->setIntValue(iv);
188     return naNil();
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     (*node)->setBoolValue(naTrue(val) ? true : false);
196     return naNil();
197 }
198
199 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
200 {
201     NODEARG();
202     naRef r = naNumValue(naVec_get(argv, 0));
203     if(naIsNil(r))
204         naRuntimeError(c, "props.setDoubleValue() with non-number");
205     (*node)->setDoubleValue(r.num);
206     return naNil();
207 }
208
209 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
210 {
211     NODEARG();
212     SGPropertyNode* n = (*node)->getParent();
213     if(!n) return naNil();
214     return propNodeGhostCreate(c, n);
215 }
216
217 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
218 {
219     NODEARG();
220     naRef child = naVec_get(argv, 0);
221     if(!naIsString(child)) return naNil();
222     naRef idx = naNumValue(naVec_get(argv, 1));
223     bool create = naTrue(naVec_get(argv, 2));
224     SGPropertyNode* n;
225     try {
226         if(naIsNil(idx) || !naIsNum(idx)) {
227             n = (*node)->getChild(naStr_data(child), create);
228         } else {
229             n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
230         }
231     } catch (const string& err) {
232         naRuntimeError(c, (char *)err.c_str());
233         return naNil();
234     }
235     if(!n) return naNil();
236     return propNodeGhostCreate(c, n);
237 }
238
239 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
240 {
241     NODEARG();
242     naRef result = naNewVector(c);
243     if(naIsNil(argv) || naVec_size(argv) == 0) {
244         // Get all children
245         for(int i=0; i<(*node)->nChildren(); i++)
246             naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
247     } else {
248         // Get all children of a specified name
249         naRef name = naVec_get(argv, 0);
250         if(!naIsString(name)) return naNil();
251         try {
252             vector<SGPropertyNode_ptr> children
253                 = (*node)->getChildren(naStr_data(name));
254             for(unsigned int i=0; i<children.size(); i++)
255                 naVec_append(result, propNodeGhostCreate(c, children[i]));
256         } catch (const string& err) {
257             naRuntimeError(c, (char *)err.c_str());
258             return naNil();
259         }
260     }
261     return result;
262 }
263
264 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
265 {
266     NODEARG();
267     naRef child = naVec_get(argv, 0);
268     naRef index = naVec_get(argv, 1);
269     if(!naIsString(child) || !naIsNum(index)) return naNil();
270     SGPropertyNode_ptr n = 0;
271     try {
272         n = (*node)->removeChild(naStr_data(child), (int)index.num, false);
273     } catch (const string& err) {
274         naRuntimeError(c, (char *)err.c_str());
275     }
276     return propNodeGhostCreate(c, n);
277 }
278
279 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
280 {
281     NODEARG();
282     naRef result = naNewVector(c);
283     if(naIsNil(argv) || naVec_size(argv) == 0) {
284         // Remove all children
285         for(int i = (*node)->nChildren() - 1; i >=0; i--)
286             naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
287     } else {
288         // Remove all children of a specified name
289         naRef name = naVec_get(argv, 0);
290         if(!naIsString(name)) return naNil();
291         try {
292             vector<SGPropertyNode_ptr> children
293                 = (*node)->removeChildren(naStr_data(name), false);
294             for(unsigned int i=0; i<children.size(); i++)
295                 naVec_append(result, propNodeGhostCreate(c, children[i]));
296         } catch (const string& err) {
297             naRuntimeError(c, (char *)err.c_str());
298             return naNil();
299         }
300     }
301     return result;
302 }
303
304 static naRef f_alias(naContext c, naRef me, int argc, naRef* args)
305 {
306     NODEARG();
307     SGPropertyNode* al;
308     naRef prop = naVec_get(argv, 0);
309     try {
310         if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
311         else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
312         else throw string("props.alias() with bad argument");
313     } catch (const string& err) {
314         naRuntimeError(c, (char *)err.c_str());
315         return naNil();
316     }
317     return naNum((*node)->alias(al));
318 }
319
320 static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
321 {
322     NODEARG();
323     return naNum((*node)->unalias());
324 }
325
326 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
327 {
328     NODEARG();
329     naRef path = naVec_get(argv, 0);
330     bool create = naTrue(naVec_get(argv, 1));
331     if(!naIsString(path)) return naNil();
332     SGPropertyNode* n;
333     try {
334         n = (*node)->getNode(naStr_data(path), create);
335     } catch (const string& err) {
336         naRuntimeError(c, (char *)err.c_str());
337         return naNil();
338     }
339     return propNodeGhostCreate(c, n);
340 }
341
342 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
343 {
344     return propNodeGhostCreate(c, new SGPropertyNode());
345 }
346
347 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
348 {
349     return propNodeGhostCreate(c, globals->get_props());
350 }
351
352 static struct {
353     naCFunction func;
354     const char* name;
355 } propfuncs[] = {
356     { f_getType, "_getType" },
357     { f_getAttribute, "_getAttribute" },
358     { f_setAttribute, "_setAttribute" },
359     { f_getName, "_getName" },
360     { f_getIndex, "_getIndex" },
361     { f_getValue, "_getValue" },
362     { f_setValue, "_setValue" },
363     { f_setIntValue, "_setIntValue" },
364     { f_setBoolValue, "_setBoolValue" },
365     { f_setDoubleValue, "_setDoubleValue" },
366     { f_getParent, "_getParent" },
367     { f_getChild, "_getChild" },
368     { f_getChildren, "_getChildren" },
369     { f_removeChild, "_removeChild" },
370     { f_removeChildren, "_removeChildren" },
371     { f_alias, "_alias" },
372     { f_unalias, "_unalias" },
373     { f_getNode, "_getNode" },
374     { f_new, "_new" },
375     { f_globals, "_globals" },
376     { 0, 0 }
377 };
378
379 naRef FGNasalSys::genPropsModule()
380 {
381     naRef namespc = naNewHash(_context);
382     for(int i=0; propfuncs[i].name; i++)
383         hashset(namespc, propfuncs[i].name,
384                 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));
385     return namespc;
386 }