]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/nasal-props.cxx
- fix two bugs
[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_getName(naContext c, naRef me, int argc, naRef* args)
78 {
79     NODEARG();
80     return NASTR((*node)->getName());
81 }
82
83 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
84 {
85     NODEARG();
86     return naNum((*node)->getIndex());
87 }
88
89 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
90 {
91     NODEARG();
92     switch((*node)->getType()) {
93     case SGPropertyNode::BOOL:   case SGPropertyNode::INT:
94     case SGPropertyNode::LONG:   case SGPropertyNode::FLOAT:
95     case SGPropertyNode::DOUBLE:
96         return naNum((*node)->getDoubleValue());
97     case SGPropertyNode::STRING:
98     case SGPropertyNode::UNSPECIFIED:
99         return NASTR((*node)->getStringValue());
100     }
101     return naNil();
102 }
103
104 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
105 {
106     NODEARG();
107     naRef val = naVec_get(argv, 0);
108     if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
109     else                (*node)->setDoubleValue(naNumValue(val).num);
110     return naNil();
111 }
112
113 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
114 {
115     NODEARG();
116     // Original code:
117     //   int iv = (int)naNumValue(naVec_get(argv, 0)).num;
118
119     // Junk to pacify the gcc-2.95.3 optimizer:
120     naRef tmp0 = naVec_get(argv, 0);
121     naRef tmp1 = naNumValue(tmp0);
122     double tmp2 = tmp1.num;
123     int iv = (int)tmp2;
124
125     (*node)->setIntValue(iv);
126     return naNil();
127 }
128
129 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
130 {
131     NODEARG();
132     naRef val = naVec_get(argv, 0);
133     (*node)->setBoolValue(naTrue(val) ? true : false);
134     return naNil();
135 }
136
137 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
138 {
139     NODEARG();
140     (*node)->setDoubleValue(naNumValue(naVec_get(argv, 0)).num);
141     return naNil();
142 }
143
144 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
145 {
146     NODEARG();
147     SGPropertyNode* n = (*node)->getParent();
148     if(!n) return naNil();
149     return propNodeGhostCreate(c, n);
150 }
151
152 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
153 {
154     NODEARG();
155     naRef child = naVec_get(argv, 0);
156     if(!naIsString(child)) return naNil();
157     naRef idx = naNumValue(naVec_get(argv, 1));
158     bool create = naTrue(naVec_get(argv, 2));
159     SGPropertyNode* n;
160     try {
161         if(naIsNil(idx) || !naIsNum(idx)) {
162             n = (*node)->getChild(naStr_data(child), create);
163         } else {
164             n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
165         }
166     } catch (const string& err) {
167         naRuntimeError(c, (char *)err.c_str());
168         return naNil();
169     }
170     if(!n) return naNil();
171     return propNodeGhostCreate(c, n);
172 }
173
174 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
175 {
176     NODEARG();
177     naRef result = naNewVector(c);
178     if(naIsNil(argv) || naVec_size(argv) == 0) {
179         // Get all children
180         for(int i=0; i<(*node)->nChildren(); i++)
181             naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
182     } else {
183         // Get all children of a specified name
184         naRef name = naVec_get(argv, 0);
185         if(!naIsString(name)) return naNil();
186         try {
187             vector<SGPropertyNode_ptr> children
188                 = (*node)->getChildren(naStr_data(name));
189             for(unsigned int i=0; i<children.size(); i++)
190                 naVec_append(result, propNodeGhostCreate(c, children[i]));
191         } catch (const string& err) {
192             naRuntimeError(c, (char *)err.c_str());
193             return naNil();
194         }
195     }
196     return result;
197 }
198
199 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
200 {
201     NODEARG();
202     naRef child = naVec_get(argv, 0);
203     naRef index = naVec_get(argv, 1);
204     if(!naIsString(child) || !naIsNum(index)) return naNil();
205     try {
206         (*node)->removeChild(naStr_data(child), (int)index.num, false);
207     } catch (const string& err) {
208         naRuntimeError(c, (char *)err.c_str());
209     }
210     return naNil();
211 }
212
213 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
214 {
215     NODEARG();
216     naRef result = naNewVector(c);
217     if(naIsNil(argv) || naVec_size(argv) == 0) {
218         // Remove all children
219         for(int i = (*node)->nChildren() - 1; i >=0; i--)
220             naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
221     } else {
222         // Remove all children of a specified name
223         naRef name = naVec_get(argv, 0);
224         if(!naIsString(name)) return naNil();
225         try {
226             vector<SGPropertyNode_ptr> children
227                 = (*node)->removeChildren(naStr_data(name), false);
228             for(unsigned int i=0; i<children.size(); i++)
229                 naVec_append(result, propNodeGhostCreate(c, children[i]));
230         } catch (const string& err) {
231             naRuntimeError(c, (char *)err.c_str());
232             return naNil();
233         }
234     }
235     return result;
236 }
237
238 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
239 {
240     NODEARG();
241     naRef path = naVec_get(argv, 0);
242     bool create = naTrue(naVec_get(argv, 1));
243     if(!naIsString(path)) return naNil();
244     SGPropertyNode* n;
245     try {
246         n = (*node)->getNode(naStr_data(path), create);
247     } catch (const string& err) {
248         naRuntimeError(c, (char *)err.c_str());
249         return naNil();
250     }
251     return propNodeGhostCreate(c, n);
252 }
253
254 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
255 {
256     return propNodeGhostCreate(c, new SGPropertyNode());
257 }
258
259 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
260 {
261     return propNodeGhostCreate(c, globals->get_props());
262 }
263
264 static struct {
265     naCFunction func;
266     char* name;
267 } propfuncs[] = {
268     { f_getType, "_getType" },
269     { f_getName, "_getName" },
270     { f_getIndex, "_getIndex" },
271     { f_getValue, "_getValue" },
272     { f_setValue, "_setValue" },
273     { f_setIntValue, "_setIntValue" },
274     { f_setBoolValue, "_setBoolValue" },
275     { f_setDoubleValue, "_setDoubleValue" },
276     { f_getParent, "_getParent" },
277     { f_getChild, "_getChild" },
278     { f_getChildren, "_getChildren" },
279     { f_removeChild, "_removeChild" },
280     { f_removeChildren, "_removeChildren" },
281     { f_getNode, "_getNode" },
282     { f_new, "_new" },
283     { f_globals, "_globals" },
284     { 0, 0 }
285 };
286
287 naRef FGNasalSys::genPropsModule()
288 {
289     naRef namespc = naNewHash(_context);
290     for(int i=0; propfuncs[i].name; i++)
291         hashset(namespc, propfuncs[i].name,
292                 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));
293     return namespc;
294 }