]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/nasal-props.cxx
87afb1d93160d2c28ecf7cac0612d71249f011b7
[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 };
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((*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, "children"))         return naNum((*node)->nChildren());
86     else if(!strcmp(a, "listeners"))   return naNum((*node)->nListeners());
87     else if(!strcmp(a, "tied"))        return naNum((*node)->isTied());
88     else if(!strcmp(a, "alias"))       return naNum((*node)->isAlias());
89     else if(!strcmp(a, "read"))        attr = SGPropertyNode::READ;
90     else if(!strcmp(a, "write"))       attr = SGPropertyNode::WRITE;
91     else if(!strcmp(a, "archive"))     attr = SGPropertyNode::ARCHIVE;
92     else if(!strcmp(a, "trace-read"))  attr = SGPropertyNode::TRACE_READ;
93     else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
94     else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
95     else {
96         naRuntimeError(c, "props.getAttribute() with invalid attribute");
97         return naNil();
98     }
99     return naNum((*node)->getAttribute(attr));
100 }
101
102 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
103 {
104     NODEARG();
105     naRef val = naVec_get(argv, 0);
106     if(naVec_size(argv) == 1 && naIsNum(val)) {
107         naRef ret = naNum((*node)->getAttributes());
108         (*node)->setAttributes((int)val.num);
109         return ret;
110     }
111     SGPropertyNode::Attribute attr;
112     char *a = naStr_data(val);
113     if(!a) a = "";
114     if(!strcmp(a, "read"))             attr = SGPropertyNode::READ;
115     else if(!strcmp(a, "write"))       attr = SGPropertyNode::WRITE;
116     else if(!strcmp(a, "archive"))     attr = SGPropertyNode::ARCHIVE;
117     else if(!strcmp(a, "trace-read"))  attr = SGPropertyNode::TRACE_READ;
118     else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
119     else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
120     else {
121         naRuntimeError(c, "props.setAttribute() with invalid attribute");
122         return naNil();
123     }
124     naRef ret = naNum((*node)->getAttribute(attr));
125     (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
126     return ret;
127 }
128
129 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
130 {
131     NODEARG();
132     return NASTR((*node)->getName());
133 }
134
135 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
136 {
137     NODEARG();
138     return naNum((*node)->getIndex());
139 }
140
141 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
142 {
143     NODEARG();
144     switch((*node)->getType()) {
145     case SGPropertyNode::BOOL:   case SGPropertyNode::INT:
146     case SGPropertyNode::LONG:   case SGPropertyNode::FLOAT:
147     case SGPropertyNode::DOUBLE:
148         return naNum((*node)->getDoubleValue());
149     case SGPropertyNode::STRING:
150     case SGPropertyNode::UNSPECIFIED:
151         return NASTR((*node)->getStringValue());
152     default:
153         return naNil();
154     }
155 }
156
157 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
158 {
159     NODEARG();
160     naRef val = naVec_get(argv, 0);
161     if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
162     else {
163         naRef n = naNumValue(val);
164         if(naIsNil(n))
165             naRuntimeError(c, "props.setValue() with non-number");
166         (*node)->setDoubleValue(naNumValue(val).num);
167     }
168     return naNil();
169 }
170
171 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
172 {
173     NODEARG();
174     // Original code:
175     //   int iv = (int)naNumValue(naVec_get(argv, 0)).num;
176
177     // Junk to pacify the gcc-2.95.3 optimizer:
178     naRef tmp0 = naVec_get(argv, 0);
179     naRef tmp1 = naNumValue(tmp0);
180     if(naIsNil(tmp1))
181         naRuntimeError(c, "props.setIntValue() with non-number");
182     double tmp2 = tmp1.num;
183     int iv = (int)tmp2;
184
185     (*node)->setIntValue(iv);
186     return naNil();
187 }
188
189 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
190 {
191     NODEARG();
192     naRef val = naVec_get(argv, 0);
193     (*node)->setBoolValue(naTrue(val) ? true : false);
194     return naNil();
195 }
196
197 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
198 {
199     NODEARG();
200     naRef r = naNumValue(naVec_get(argv, 0));
201     if(naIsNil(r))
202         naRuntimeError(c, "props.setDoubleValue() with non-number");
203     (*node)->setDoubleValue(r.num);
204     return naNil();
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     try {
269         (*node)->removeChild(naStr_data(child), (int)index.num, false);
270     } catch (const string& err) {
271         naRuntimeError(c, (char *)err.c_str());
272     }
273     return naNil();
274 }
275
276 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
277 {
278     NODEARG();
279     naRef result = naNewVector(c);
280     if(naIsNil(argv) || naVec_size(argv) == 0) {
281         // Remove all children
282         for(int i = (*node)->nChildren() - 1; i >=0; i--)
283             naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
284     } else {
285         // Remove all children of a specified name
286         naRef name = naVec_get(argv, 0);
287         if(!naIsString(name)) return naNil();
288         try {
289             vector<SGPropertyNode_ptr> children
290                 = (*node)->removeChildren(naStr_data(name), false);
291             for(unsigned int i=0; i<children.size(); i++)
292                 naVec_append(result, propNodeGhostCreate(c, children[i]));
293         } catch (const string& err) {
294             naRuntimeError(c, (char *)err.c_str());
295             return naNil();
296         }
297     }
298     return result;
299 }
300
301 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
302 {
303     NODEARG();
304     naRef path = naVec_get(argv, 0);
305     bool create = naTrue(naVec_get(argv, 1));
306     if(!naIsString(path)) return naNil();
307     SGPropertyNode* n;
308     try {
309         n = (*node)->getNode(naStr_data(path), create);
310     } catch (const string& err) {
311         naRuntimeError(c, (char *)err.c_str());
312         return naNil();
313     }
314     return propNodeGhostCreate(c, n);
315 }
316
317 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
318 {
319     return propNodeGhostCreate(c, new SGPropertyNode());
320 }
321
322 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
323 {
324     return propNodeGhostCreate(c, globals->get_props());
325 }
326
327 static struct {
328     naCFunction func;
329     char* name;
330 } propfuncs[] = {
331     { f_getType, "_getType" },
332     { f_getAttribute, "_getAttribute" },
333     { f_setAttribute, "_setAttribute" },
334     { f_getName, "_getName" },
335     { f_getIndex, "_getIndex" },
336     { f_getValue, "_getValue" },
337     { f_setValue, "_setValue" },
338     { f_setIntValue, "_setIntValue" },
339     { f_setBoolValue, "_setBoolValue" },
340     { f_setDoubleValue, "_setDoubleValue" },
341     { f_getParent, "_getParent" },
342     { f_getChild, "_getChild" },
343     { f_getChildren, "_getChildren" },
344     { f_removeChild, "_removeChild" },
345     { f_removeChildren, "_removeChildren" },
346     { f_getNode, "_getNode" },
347     { f_new, "_new" },
348     { f_globals, "_globals" },
349     { 0, 0 }
350 };
351
352 naRef FGNasalSys::genPropsModule()
353 {
354     naRef namespc = naNewHash(_context);
355     for(int i=0; propfuncs[i].name; i++)
356         hashset(namespc, propfuncs[i].name,
357                 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));
358     return namespc;
359 }