]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/nasal-props.cxx
Modified Files:
[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, "references"))  return naNum(node->getNumRefs());
88     else if(!strcmp(a, "tied"))        return naNum((*node)->isTied());
89     else if(!strcmp(a, "alias"))       return naNum((*node)->isAlias());
90     else if(!strcmp(a, "read"))        attr = SGPropertyNode::READ;
91     else if(!strcmp(a, "write"))       attr = SGPropertyNode::WRITE;
92     else if(!strcmp(a, "archive"))     attr = SGPropertyNode::ARCHIVE;
93     else if(!strcmp(a, "trace-read"))  attr = SGPropertyNode::TRACE_READ;
94     else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
95     else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
96     else {
97         naRuntimeError(c, "props.getAttribute() with invalid attribute");
98         return naNil();
99     }
100     return naNum((*node)->getAttribute(attr));
101 }
102
103 static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
104 {
105     NODEARG();
106     naRef val = naVec_get(argv, 0);
107     if(naVec_size(argv) == 1 && naIsNum(val)) {
108         naRef ret = naNum((*node)->getAttributes());
109         (*node)->setAttributes((int)val.num);
110         return ret;
111     }
112     SGPropertyNode::Attribute attr;
113     char *a = naStr_data(val);
114     if(!a) a = "";
115     if(!strcmp(a, "read"))             attr = SGPropertyNode::READ;
116     else if(!strcmp(a, "write"))       attr = SGPropertyNode::WRITE;
117     else if(!strcmp(a, "archive"))     attr = SGPropertyNode::ARCHIVE;
118     else if(!strcmp(a, "trace-read"))  attr = SGPropertyNode::TRACE_READ;
119     else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
120     else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
121     else {
122         naRuntimeError(c, "props.setAttribute() with invalid attribute");
123         return naNil();
124     }
125     naRef ret = naNum((*node)->getAttribute(attr));
126     (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
127     return ret;
128 }
129
130 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
131 {
132     NODEARG();
133     return NASTR((*node)->getName());
134 }
135
136 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
137 {
138     NODEARG();
139     return naNum((*node)->getIndex());
140 }
141
142 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
143 {
144     NODEARG();
145     switch((*node)->getType()) {
146     case SGPropertyNode::BOOL:   case SGPropertyNode::INT:
147     case SGPropertyNode::LONG:   case SGPropertyNode::FLOAT:
148     case SGPropertyNode::DOUBLE:
149         return naNum((*node)->getDoubleValue());
150     case SGPropertyNode::STRING:
151     case SGPropertyNode::UNSPECIFIED:
152         return NASTR((*node)->getStringValue());
153     default:
154         return naNil();
155     }
156 }
157
158 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
159 {
160     NODEARG();
161     naRef val = naVec_get(argv, 0);
162     if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
163     else {
164         naRef n = naNumValue(val);
165         if(naIsNil(n))
166             naRuntimeError(c, "props.setValue() with non-number");
167         (*node)->setDoubleValue(naNumValue(val).num);
168     }
169     return naNil();
170 }
171
172 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
173 {
174     NODEARG();
175     // Original code:
176     //   int iv = (int)naNumValue(naVec_get(argv, 0)).num;
177
178     // Junk to pacify the gcc-2.95.3 optimizer:
179     naRef tmp0 = naVec_get(argv, 0);
180     naRef tmp1 = naNumValue(tmp0);
181     if(naIsNil(tmp1))
182         naRuntimeError(c, "props.setIntValue() with non-number");
183     double tmp2 = tmp1.num;
184     int iv = (int)tmp2;
185
186     (*node)->setIntValue(iv);
187     return naNil();
188 }
189
190 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
191 {
192     NODEARG();
193     naRef val = naVec_get(argv, 0);
194     (*node)->setBoolValue(naTrue(val) ? true : false);
195     return naNil();
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     (*node)->setDoubleValue(r.num);
205     return naNil();
206 }
207
208 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
209 {
210     NODEARG();
211     SGPropertyNode* n = (*node)->getParent();
212     if(!n) return naNil();
213     return propNodeGhostCreate(c, n);
214 }
215
216 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
217 {
218     NODEARG();
219     naRef child = naVec_get(argv, 0);
220     if(!naIsString(child)) return naNil();
221     naRef idx = naNumValue(naVec_get(argv, 1));
222     bool create = naTrue(naVec_get(argv, 2));
223     SGPropertyNode* n;
224     try {
225         if(naIsNil(idx) || !naIsNum(idx)) {
226             n = (*node)->getChild(naStr_data(child), create);
227         } else {
228             n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
229         }
230     } catch (const string& err) {
231         naRuntimeError(c, (char *)err.c_str());
232         return naNil();
233     }
234     if(!n) return naNil();
235     return propNodeGhostCreate(c, n);
236 }
237
238 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
239 {
240     NODEARG();
241     naRef result = naNewVector(c);
242     if(naIsNil(argv) || naVec_size(argv) == 0) {
243         // Get all children
244         for(int i=0; i<(*node)->nChildren(); i++)
245             naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
246     } else {
247         // Get all children of a specified name
248         naRef name = naVec_get(argv, 0);
249         if(!naIsString(name)) return naNil();
250         try {
251             vector<SGPropertyNode_ptr> children
252                 = (*node)->getChildren(naStr_data(name));
253             for(unsigned int i=0; i<children.size(); i++)
254                 naVec_append(result, propNodeGhostCreate(c, children[i]));
255         } catch (const string& err) {
256             naRuntimeError(c, (char *)err.c_str());
257             return naNil();
258         }
259     }
260     return result;
261 }
262
263 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
264 {
265     NODEARG();
266     naRef child = naVec_get(argv, 0);
267     naRef index = naVec_get(argv, 1);
268     if(!naIsString(child) || !naIsNum(index)) return naNil();
269     try {
270         (*node)->removeChild(naStr_data(child), (int)index.num, false);
271     } catch (const string& err) {
272         naRuntimeError(c, (char *)err.c_str());
273     }
274     return naNil();
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_getNode(naContext c, naRef me, int argc, naRef* args)
303 {
304     NODEARG();
305     naRef path = naVec_get(argv, 0);
306     bool create = naTrue(naVec_get(argv, 1));
307     if(!naIsString(path)) return naNil();
308     SGPropertyNode* n;
309     try {
310         n = (*node)->getNode(naStr_data(path), create);
311     } catch (const string& err) {
312         naRuntimeError(c, (char *)err.c_str());
313         return naNil();
314     }
315     return propNodeGhostCreate(c, n);
316 }
317
318 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
319 {
320     return propNodeGhostCreate(c, new SGPropertyNode());
321 }
322
323 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
324 {
325     return propNodeGhostCreate(c, globals->get_props());
326 }
327
328 static struct {
329     naCFunction func;
330     const char* name;
331 } propfuncs[] = {
332     { f_getType, "_getType" },
333     { f_getAttribute, "_getAttribute" },
334     { f_setAttribute, "_setAttribute" },
335     { f_getName, "_getName" },
336     { f_getIndex, "_getIndex" },
337     { f_getValue, "_getValue" },
338     { f_setValue, "_setValue" },
339     { f_setIntValue, "_setIntValue" },
340     { f_setBoolValue, "_setBoolValue" },
341     { f_setDoubleValue, "_setDoubleValue" },
342     { f_getParent, "_getParent" },
343     { f_getChild, "_getChild" },
344     { f_getChildren, "_getChildren" },
345     { f_removeChild, "_removeChild" },
346     { f_removeChildren, "_removeChildren" },
347     { f_getNode, "_getNode" },
348     { f_new, "_new" },
349     { f_globals, "_globals" },
350     { 0, 0 }
351 };
352
353 naRef FGNasalSys::genPropsModule()
354 {
355     naRef namespc = naNewHash(_context);
356     for(int i=0; propfuncs[i].name; i++)
357         hashset(namespc, propfuncs[i].name,
358                 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));
359     return namespc;
360 }