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