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