]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/nasal-props.cxx
3d0788bea83f3368f27d41fa5ed730dde3b57a5e
[flightgear.git] / src / Scripting / nasal-props.cxx
1 #include <simgear/nasal/nasal.h>
2 #include <simgear/props/props.hxx>
3
4 #include <Main/globals.hxx>
5
6 #include "NasalSys.hxx"
7
8 // Implementation of a Nasal wrapper for the SGPropertyNode class,
9 // using the Nasal "ghost" (er... Garbage collection Handle for
10 // OutSide Thingy) facility.
11 //
12 // Note that these functions appear in Nasal with prepended
13 // underscores.  They work on the low-level "ghost" objects and aren't
14 // intended for use from user code, but from Nasal code you will find
15 // in props.nas.  That is where the Nasal props.Node class is defined,
16 // which provides a saner interface along the lines of SGPropertyNode.
17
18 static void propNodeGhostDestroy(void* ghost)
19 {
20     SGPropertyNode_ptr* prop = (SGPropertyNode_ptr*)ghost;
21     delete prop;
22 }
23
24 naGhostType PropNodeGhostType = { propNodeGhostDestroy };
25
26 static naRef propNodeGhostCreate(naContext c, SGPropertyNode* n)
27 {
28     if(!n) return naNil();
29     SGPropertyNode_ptr* ghost = new SGPropertyNode_ptr(n);
30     return naNewGhost(c, &PropNodeGhostType, ghost);
31 }
32
33 naRef FGNasalSys::propNodeGhost(SGPropertyNode* handle)
34 {
35     return propNodeGhostCreate(_context, handle);
36 }
37
38 #define NASTR(s) s ? naStr_fromdata(naNewString(c),(char*)(s),strlen(s)) : naNil()
39
40 //
41 // Standard header for the extension functions.  It turns the "ghost"
42 // found in arg[0] into a SGPropertyNode_ptr*, and then "unwraps" the
43 // vector found in the second argument into a normal-looking args
44 // array.  This allows the Nasal handlers to do things like:
45 //   Node.getChild = func { _getChild(me.ghost, arg) }
46 //
47 #define NODEARG()                                                       \
48     if(argc < 2 || !naIsGhost(args[0]) ||                               \
49        naGhost_type(args[0]) != &PropNodeGhostType)                       \
50         naRuntimeError(c, "bad argument to props function");            \
51     SGPropertyNode_ptr* node = (SGPropertyNode_ptr*)naGhost_ptr(args[0]); \
52     naRef argv = args[1]
53
54 static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
55 {
56     NODEARG();
57     char* t = "unknown";
58     switch((*node)->getType()) {
59     case SGPropertyNode::NONE:   t = "NONE";   break;
60     case SGPropertyNode::ALIAS:  t = "ALIAS";  break;
61     case SGPropertyNode::BOOL:   t = "BOOL";   break;
62     case SGPropertyNode::INT:    t = "INT";    break;
63     case SGPropertyNode::LONG:   t = "LONG";   break;
64     case SGPropertyNode::FLOAT:  t = "FLOAT";  break;
65     case SGPropertyNode::DOUBLE: t = "DOUBLE"; break;
66     case SGPropertyNode::STRING: t = "STRING"; break;
67     case SGPropertyNode::UNSPECIFIED: t = "UNSPECIFIED"; break;
68     }
69     return NASTR(t);
70 }
71
72 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
73 {
74     NODEARG();
75     return NASTR((*node)->getName());
76 }
77
78 static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
79 {
80     NODEARG();
81     return naNum((*node)->getIndex());
82 }
83
84 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
85 {
86     NODEARG();
87     switch((*node)->getType()) {
88     case SGPropertyNode::BOOL:   case SGPropertyNode::INT:
89     case SGPropertyNode::LONG:   case SGPropertyNode::FLOAT:
90     case SGPropertyNode::DOUBLE:
91         return naNum((*node)->getDoubleValue());
92     case SGPropertyNode::STRING:
93     case SGPropertyNode::UNSPECIFIED:
94         return NASTR((*node)->getStringValue());
95     }
96     return naNil();
97 }
98
99 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
100 {
101     NODEARG();
102     naRef val = naVec_get(argv, 0);
103     if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
104     else                (*node)->setDoubleValue(naNumValue(val).num);
105     return naNil();
106 }
107
108 static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
109 {
110     NODEARG();
111     // Original code:
112     //   int iv = (int)naNumValue(naVec_get(argv, 0)).num;
113
114     // Junk to pacify the gcc-2.95.3 optimizer:
115     naRef tmp0 = naVec_get(argv, 0);
116     naRef tmp1 = naNumValue(tmp0);
117     double tmp2 = tmp1.num;
118     int iv = (int)tmp2;
119
120     (*node)->setIntValue(iv);
121     return naNil();
122 }
123
124 static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
125 {
126     NODEARG();
127     naRef val = naVec_get(argv, 0);
128     (*node)->setBoolValue(naTrue(val) ? true : false);
129     return naNil();
130 }
131
132 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
133 {
134     NODEARG();
135     (*node)->setDoubleValue(naNumValue(naVec_get(argv, 0)).num);
136     return naNil();
137 }
138
139 static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
140 {
141     NODEARG();
142     SGPropertyNode* n = (*node)->getParent();
143     if(!n) return naNil();
144     return propNodeGhostCreate(c, n);
145 }
146
147 static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
148 {
149     NODEARG();
150     naRef child = naVec_get(argv, 0);
151     if(!naIsString(child)) return naNil();
152     naRef idx = naNumValue(naVec_get(argv, 1));
153     bool create = naTrue(naVec_get(argv, 2));
154     SGPropertyNode* n;
155     if(naIsNil(idx) || !naIsNum(idx)) {
156         n = (*node)->getChild(naStr_data(child), create);
157     } else {
158         n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
159     }
160     if(!n) return naNil();
161     return propNodeGhostCreate(c, n);
162 }
163
164 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
165 {
166     NODEARG();
167     naRef result = naNewVector(c);
168     if(naIsNil(argv) || naVec_size(argv) == 0) {
169         // Get all children
170         for(int i=0; i<(*node)->nChildren(); i++)
171             naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
172     } else {
173         // Get all children of a specified name
174         naRef name = naVec_get(argv, 0);
175         if(!naIsString(name)) return naNil();
176         vector<SGPropertyNode_ptr> children
177             = (*node)->getChildren(naStr_data(name));
178         for(unsigned int i=0; i<children.size(); i++)
179             naVec_append(result, propNodeGhostCreate(c, children[i]));
180     }
181     return result;
182 }
183
184 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
185 {
186     NODEARG();
187     naRef child = naVec_get(argv, 0);
188     naRef index = naVec_get(argv, 1);
189     if(!naIsString(child) || !naIsNum(index)) return naNil();
190     (*node)->removeChild(naStr_data(child), (int)index.num, false);
191     return naNil();
192 }
193
194 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
195 {
196     NODEARG();
197     naRef result = naNewVector(c);
198     if(naIsNil(argv) || naVec_size(argv) == 0) {
199         // Remove all children
200         for(int i = (*node)->nChildren() - 1; i >=0; i--)
201             naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
202     } else {
203         // Remove all children of a specified name
204         naRef name = naVec_get(argv, 0);
205         if(!naIsString(name)) return naNil();
206         vector<SGPropertyNode_ptr> children
207             = (*node)->removeChildren(naStr_data(name), false);
208         for(unsigned int i=0; i<children.size(); i++)
209             naVec_append(result, propNodeGhostCreate(c, children[i]));
210     }
211     return result;
212 }
213
214 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
215 {
216     NODEARG();
217     naRef path = naVec_get(argv, 0);
218     bool create = naTrue(naVec_get(argv, 1));
219     if(!naIsString(path)) return naNil();
220     SGPropertyNode* n = (*node)->getNode(naStr_data(path), create);
221     return propNodeGhostCreate(c, n);
222 }
223
224 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
225 {
226     return propNodeGhostCreate(c, new SGPropertyNode());
227 }
228
229 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
230 {
231     return propNodeGhostCreate(c, globals->get_props());
232 }
233
234 static struct {
235     naCFunction func;
236     char* name;
237 } propfuncs[] = {
238     { f_getType, "_getType" },
239     { f_getName, "_getName" },
240     { f_getIndex, "_getIndex" },
241     { f_getValue, "_getValue" },
242     { f_setValue, "_setValue" },
243     { f_setIntValue, "_setIntValue" },
244     { f_setBoolValue, "_setBoolValue" },
245     { f_setDoubleValue, "_setDoubleValue" },
246     { f_getParent, "_getParent" },
247     { f_getChild, "_getChild" },
248     { f_getChildren, "_getChildren" },
249     { f_removeChild, "_removeChild" },
250     { f_removeChildren, "_removeChildren" },
251     { f_getNode, "_getNode" },
252     { f_new, "_new" },
253     { f_globals, "_globals" },
254     { 0, 0 }
255 };
256
257 naRef FGNasalSys::genPropsModule()
258 {
259     naRef namespc = naNewHash(_context);
260     for(int i=0; propfuncs[i].name; i++)
261         hashset(namespc, propfuncs[i].name,
262                 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));
263     return namespc;
264 }