]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/nasal-props.cxx
Vivian MEAZZA:
[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     SGPropertyNode* n;
154     if(naIsNil(idx) || !naIsNum(idx)) {
155         n = (*node)->getChild(naStr_data(child));
156     } else {
157         n = (*node)->getChild(naStr_data(child), (int)idx.num);
158     }
159     if(!n) return naNil();
160     return propNodeGhostCreate(c, n);
161 }
162
163 static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
164 {
165     NODEARG();
166     naRef result = naNewVector(c);
167     if(naIsNil(argv) || naVec_size(argv) == 0) {
168         // Get all children
169         for(int i=0; i<(*node)->nChildren(); i++)
170             naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
171     } else {
172         // Get all children of a specified name
173         naRef name = naVec_get(argv, 0);
174         if(!naIsString(name)) return naNil();
175         vector<SGPropertyNode_ptr> children
176             = (*node)->getChildren(naStr_data(name));
177         for(unsigned int i=0; i<children.size(); i++)
178             naVec_append(result, propNodeGhostCreate(c, children[i]));
179     }
180     return result;
181 }
182
183 static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
184 {
185     NODEARG();
186     naRef child = naVec_get(argv, 0);
187     naRef index = naVec_get(argv, 1);
188     if(!naIsString(child) || !naIsNum(index)) return naNil();
189     (*node)->removeChild(naStr_data(child), (int)index.num, false);
190     return naNil();
191 }
192
193 static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
194 {
195     NODEARG();
196     naRef result = naNewVector(c);
197     if(naIsNil(argv) || naVec_size(argv) == 0) {
198         // Remove all children
199         for(int i = (*node)->nChildren() - 1; i >=0; i--)
200             naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
201     } else {
202         // Remove all children of a specified name
203         naRef name = naVec_get(argv, 0);
204         if(!naIsString(name)) return naNil();
205         vector<SGPropertyNode_ptr> children
206             = (*node)->removeChildren(naStr_data(name), false);
207         for(unsigned int i=0; i<children.size(); i++)
208             naVec_append(result, propNodeGhostCreate(c, children[i]));
209     }
210     return result;
211 }
212
213 static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
214 {
215     NODEARG();
216     naRef path = naVec_get(argv, 0);
217     bool create = naTrue(naVec_get(argv, 1));
218     if(!naIsString(path)) return naNil();
219     SGPropertyNode* n = (*node)->getNode(naStr_data(path), create);
220     return propNodeGhostCreate(c, n);
221 }
222
223 static naRef f_new(naContext c, naRef me, int argc, naRef* args)
224 {
225     return propNodeGhostCreate(c, new SGPropertyNode());
226 }
227
228 static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
229 {
230     return propNodeGhostCreate(c, globals->get_props());
231 }
232
233 static struct {
234     naCFunction func;
235     char* name;
236 } propfuncs[] = {
237     { f_getType, "_getType" },
238     { f_getName, "_getName" },
239     { f_getIndex, "_getIndex" },
240     { f_getValue, "_getValue" },
241     { f_setValue, "_setValue" },
242     { f_setIntValue, "_setIntValue" },
243     { f_setBoolValue, "_setBoolValue" },
244     { f_setDoubleValue, "_setDoubleValue" },
245     { f_getParent, "_getParent" },
246     { f_getChild, "_getChild" },
247     { f_getChildren, "_getChildren" },
248     { f_removeChild, "_removeChild" },
249     { f_removeChildren, "_removeChildren" },
250     { f_getNode, "_getNode" },
251     { f_new, "_new" },
252     { f_globals, "_globals" },
253     { 0, 0 }
254 };
255
256 naRef FGNasalSys::genPropsModule()
257 {
258     naRef namespc = naNewHash(_context);
259     for(int i=0; propfuncs[i].name; i++)
260         hashset(namespc, propfuncs[i].name,
261                 naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));
262     return namespc;
263 }