1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2009-2010 GeoLabs SARL |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "service_internal_js.h" |
---|
26 | |
---|
27 | static char dbg[1024]; |
---|
28 | |
---|
29 | int zoo_js_support(maps** main_conf,map* request,service* s, |
---|
30 | maps **inputs,maps **outputs) |
---|
31 | { |
---|
32 | /* The class of the global object. */ |
---|
33 | JSClass global_class = { |
---|
34 | "global", JSCLASS_GLOBAL_FLAGS, |
---|
35 | JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, |
---|
36 | JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, |
---|
37 | JSCLASS_NO_OPTIONAL_MEMBERS |
---|
38 | }; |
---|
39 | |
---|
40 | /* JS variables. */ |
---|
41 | JSRuntime *rt; |
---|
42 | JSContext *cx; |
---|
43 | JSObject *global; |
---|
44 | |
---|
45 | /* Create a JS runtime. */ |
---|
46 | rt = JS_NewRuntime(8L * 1024L * 1024L); |
---|
47 | if (rt == NULL) |
---|
48 | return 1; |
---|
49 | |
---|
50 | /* Create a context. */ |
---|
51 | cx = JS_NewContext(rt, 8192); |
---|
52 | if (cx == NULL){ |
---|
53 | return 1; |
---|
54 | } |
---|
55 | JS_SetOptions(cx, JSOPTION_VAROBJFIX); |
---|
56 | JS_SetVersion(cx, JSVERSION_LATEST); |
---|
57 | JS_SetErrorReporter(cx, reportError); |
---|
58 | |
---|
59 | /* Create the global object. */ |
---|
60 | global = JS_NewObject(cx, &global_class, NULL, NULL); |
---|
61 | if (global == NULL){ |
---|
62 | return 1; |
---|
63 | } |
---|
64 | |
---|
65 | /* Populate the global object with the standard globals, |
---|
66 | like Object and Array. */ |
---|
67 | if (!JS_InitStandardClasses(cx, global)){ |
---|
68 | return 1; |
---|
69 | } |
---|
70 | if (!JS_DefineFunction(cx, global, "ZOORequest", JSRequest, 4, 0)) |
---|
71 | return 1; |
---|
72 | |
---|
73 | /* Your application code here. This may include JSAPI calls |
---|
74 | to create your own custom JS objects and run scripts. */ |
---|
75 | maps* out=*outputs; |
---|
76 | int res=SERVICE_FAILED; |
---|
77 | maps* mc=*main_conf; |
---|
78 | map* tmpm1=getMap(request,"metapath"); |
---|
79 | map* tmpm2=getMap(s->content,"serviceProvider"); |
---|
80 | char *filename[strlen(tmpm1->value)+strlen(tmpm2->value)+6]; |
---|
81 | char ntmp[1024]; |
---|
82 | getcwd(ntmp,1024); |
---|
83 | sprintf(filename,"%s/%s%s",ntmp,tmpm1->value,tmpm2->value); |
---|
84 | struct stat file_status; |
---|
85 | stat(filename, &file_status); |
---|
86 | char source[file_status.st_size]; |
---|
87 | uint16 lineno; |
---|
88 | jsval rval; |
---|
89 | FILE *jsfile=fopen(filename,"rb"); |
---|
90 | JSBool ok ; |
---|
91 | JSScript *script = JS_CompileFileHandle(cx, global, filename, jsfile); |
---|
92 | if(script){ |
---|
93 | (void)JS_ExecuteScript(cx, global, script, &rval); |
---|
94 | } |
---|
95 | else{ |
---|
96 | char tmp1[1024]; |
---|
97 | sprintf(tmp1,"Unable to load JavaScript file %s",filename); |
---|
98 | map* err=createMap("text",tmp1); |
---|
99 | addMapToMap(&err,createMap("code","NoApplicableCode")); |
---|
100 | printExceptionReportResponse(mc,err); |
---|
101 | JS_DestroyContext(cx); |
---|
102 | JS_DestroyRuntime(rt); |
---|
103 | JS_ShutDown(); |
---|
104 | exit(-1); |
---|
105 | } |
---|
106 | /* Call a function in obj's scope. */ |
---|
107 | jsval argv[3]; |
---|
108 | argv[0] = OBJECT_TO_JSVAL(JSObject_FromMaps(cx,*main_conf)); |
---|
109 | argv[1] = OBJECT_TO_JSVAL(JSObject_FromMaps(cx,*inputs)); |
---|
110 | argv[2] = OBJECT_TO_JSVAL(JSObject_FromMaps(cx,*outputs)); |
---|
111 | jsval rval1; |
---|
112 | #ifdef JS_DEBUG |
---|
113 | fprintf(stderr, "object %p\n", (void *) argv[2]); |
---|
114 | #endif |
---|
115 | |
---|
116 | ok = JS_CallFunctionName(cx, global, s->name, 3, argv, &rval1); |
---|
117 | |
---|
118 | #ifdef JS_DEBUG |
---|
119 | fprintf(stderr, "object %p\n", (void *) argv[2]); |
---|
120 | #endif |
---|
121 | |
---|
122 | JSObject *d; |
---|
123 | if (ok==JS_TRUE) { |
---|
124 | #ifdef JS_DEBUG |
---|
125 | fprintf(stderr,"Function run sucessfully !\n"); |
---|
126 | #endif |
---|
127 | /* Should get a number back from the service function call. */ |
---|
128 | ok = JS_ValueToObject(cx, rval1, &d); |
---|
129 | if (ok==JS_TRUE) { |
---|
130 | #ifdef JS_DEBUG |
---|
131 | fprintf(stderr,"Function run sucessfully !\n"); |
---|
132 | #endif |
---|
133 | /*char tmpres[10]; |
---|
134 | sprintf(tmpres,"%p",(void *)d); |
---|
135 | res=atoi(tmpres);*/ |
---|
136 | } |
---|
137 | }else{ |
---|
138 | /* Unable to run JS function */ |
---|
139 | char tmp1[1024]; |
---|
140 | //sprintf(tmp1,"Unable to run %s from the JavScript file %s",s->name,filename); |
---|
141 | sprintf(tmp1,"Unable to run %s from the JavScript file %s : \n %s",s->name,filename,dbg); |
---|
142 | fprintf(stderr,"%s",tmp1); |
---|
143 | map* err=createMap("text",tmp1); |
---|
144 | addMapToMap(err,createMap("code","NoApplicableCode")); |
---|
145 | printExceptionReportResponse(*main_conf,err); |
---|
146 | JS_DestroyContext(cx); |
---|
147 | JS_DestroyRuntime(rt); |
---|
148 | JS_ShutDown(); |
---|
149 | exit(-1); |
---|
150 | } |
---|
151 | |
---|
152 | jsval t=OBJECT_TO_JSVAL(d); |
---|
153 | //JSVAL_TO_OBJECT(t); |
---|
154 | if(JS_IsArrayObject(cx,t)){ |
---|
155 | #ifdef JS_DEBUG |
---|
156 | fprintf(stderr,"An array was returned !\n"); |
---|
157 | #endif |
---|
158 | jsdouble len; |
---|
159 | if((JS_GetArrayLength(cx, t, &len)==JS_FALSE)){ |
---|
160 | #ifdef JS_DEBUG |
---|
161 | fprintf(stderr,"outputs array is empty\n"); |
---|
162 | #endif |
---|
163 | } |
---|
164 | jsval tmp1; |
---|
165 | JSBool hasResult=JS_GetElement(cx,t,0,&tmp1); |
---|
166 | res=JSVAL_TO_INT(tmp1); |
---|
167 | #ifdef JS_DEBUG |
---|
168 | fprintf(stderr," * %d * \n",res); |
---|
169 | #endif |
---|
170 | jsval tmp2; |
---|
171 | JSBool hasElement=JS_GetElement(cx,t,1,&tmp2); |
---|
172 | out=mapsFromJSObject(cx,tmp2); |
---|
173 | *outputs=out; |
---|
174 | } |
---|
175 | else{ |
---|
176 | //#ifdef JS_DEBUG |
---|
177 | fprintf(stderr,"The service didn't return an array !\n"); |
---|
178 | //#endif |
---|
179 | jsval tmp1; |
---|
180 | JSBool hasResult=JS_GetProperty(cx,t,"result",&tmp1); |
---|
181 | res=JSVAL_TO_INT(tmp1); |
---|
182 | |
---|
183 | //#ifdef JS_DEBUG |
---|
184 | fprintf(stderr," * %d * \n",res); |
---|
185 | //#endif |
---|
186 | jsval tmp2; |
---|
187 | JSBool hasElement=JS_GetProperty(cx,t,"outputs",&tmp2); |
---|
188 | if(!hasElement) |
---|
189 | fprintf(stderr,"No outputs property returned\n"); |
---|
190 | if(JS_IsArrayObject(cx,tmp2)) |
---|
191 | fprintf(stderr,"outputs is array an as expected\n"); |
---|
192 | else |
---|
193 | fprintf(stderr,"outputs is not an array as expected\n"); |
---|
194 | out=mapsFromJSObject(cx,tmp2); |
---|
195 | #ifdef JS_DEBUG |
---|
196 | dumpMaps(out); |
---|
197 | #endif |
---|
198 | *outputs=out; |
---|
199 | } |
---|
200 | |
---|
201 | *inputs=mapsFromJSObject(cx,argv[1]); |
---|
202 | *main_conf=mapsFromJSObject(cx,argv[0]); |
---|
203 | |
---|
204 | |
---|
205 | //} |
---|
206 | //else |
---|
207 | // ERROR |
---|
208 | //; |
---|
209 | |
---|
210 | /* Cleanup. */ |
---|
211 | JS_MaybeGC(cx); |
---|
212 | JS_DestroyScript(cx, script); |
---|
213 | JS_DestroyContext(cx); |
---|
214 | JS_DestroyRuntime(rt); |
---|
215 | JS_ShutDown(); |
---|
216 | fflush(stderr); |
---|
217 | #ifdef JS_DEBUG |
---|
218 | fprintf(stderr,"Returned value %d\n",res); |
---|
219 | #endif |
---|
220 | return res; |
---|
221 | } |
---|
222 | |
---|
223 | JSObject* JSObject_FromMaps(JSContext *cx,maps* t){ |
---|
224 | JSObject *res = JS_NewArrayObject(cx, 0, NULL); |
---|
225 | maps* tmp=t; |
---|
226 | while(tmp!=NULL){ |
---|
227 | jsuint len; |
---|
228 | JSObject* res1=JS_NewObject(cx, NULL, NULL, NULL); |
---|
229 | JSObject *pval=JSObject_FromMap(cx,tmp->content); |
---|
230 | jsval pvalj=OBJECT_TO_JSVAL(pval); |
---|
231 | JS_SetProperty(cx, res1, tmp->name, &pvalj); |
---|
232 | JS_GetArrayLength(cx, res, &len); |
---|
233 | jsval res1j = OBJECT_TO_JSVAL(res1); |
---|
234 | JS_SetElement(cx,res,len,&res1j); |
---|
235 | #ifdef JS_DEBUG |
---|
236 | fprintf(stderr,"Length of the Array %d, element : %s added \n",len,tmp->name); |
---|
237 | #endif |
---|
238 | tmp=tmp->next; |
---|
239 | } |
---|
240 | return res; |
---|
241 | } |
---|
242 | |
---|
243 | JSObject* JSObject_FromMap(JSContext *cx,map* t){ |
---|
244 | JSObject* res=JS_NewObject(cx, NULL, NULL, NULL); |
---|
245 | jsval resf = OBJECT_TO_JSVAL(res); |
---|
246 | map* tmpm=t; |
---|
247 | while(tmpm!=NULL){ |
---|
248 | jsval jsstr = STRING_TO_JSVAL(JS_NewString(cx,tmpm->value,strlen(tmpm->value))); |
---|
249 | JS_SetProperty(cx, resf, tmpm->name,&jsstr); |
---|
250 | #ifdef JS_DEBUG |
---|
251 | fprintf(stderr,"%s => %s\n",tmpm->name,tmpm->value); |
---|
252 | #endif |
---|
253 | tmpm=tmpm->next; |
---|
254 | } |
---|
255 | return res; |
---|
256 | } |
---|
257 | |
---|
258 | maps* mapsFromJSObject(JSContext *cx,jsval t){ |
---|
259 | maps *res=NULL; |
---|
260 | maps *tres=NULL; |
---|
261 | JSVAL_TO_OBJECT(t); |
---|
262 | #ifdef JS_DEBUG |
---|
263 | fprintf(stderr,"Is finally an array ?\n"); |
---|
264 | #endif |
---|
265 | if(JS_IsArrayObject(cx,t)){ |
---|
266 | #ifdef JS_DEBUG |
---|
267 | fprintf(stderr,"Is finally an array !\n"); |
---|
268 | #endif |
---|
269 | } |
---|
270 | #ifdef JS_DEBUG |
---|
271 | else |
---|
272 | fprintf(stderr,"Is not an array !\n"); |
---|
273 | #endif |
---|
274 | jsdouble len; |
---|
275 | if((JS_GetArrayLength(cx, t, &len)==JS_FALSE)){ |
---|
276 | #ifdef JS_DEBUG |
---|
277 | fprintf(stderr,"outputs array is empty\n"); |
---|
278 | #endif |
---|
279 | } |
---|
280 | int i=0; |
---|
281 | char tmp[10]; |
---|
282 | sprintf(tmp,"%d",len); |
---|
283 | int clen=atoi(tmp); |
---|
284 | #ifdef JS_DEBUG |
---|
285 | fprintf(stderr,"outputs array length : %d\n",len); |
---|
286 | #endif |
---|
287 | for(i=0;i<len;i++){ |
---|
288 | #ifdef JS_DEBUG |
---|
289 | fprintf(stderr,"outputs array length : %d step %d\n",len,i); |
---|
290 | #endif |
---|
291 | jsval tmp1; |
---|
292 | JSBool hasElement=JS_GetElement(cx,t,i,&tmp1); |
---|
293 | JSVAL_TO_OBJECT(tmp1); |
---|
294 | JSIdArray *idp=JS_Enumerate(cx,tmp1); |
---|
295 | if(idp!=NULL) { |
---|
296 | int index; |
---|
297 | jsdouble argNum; |
---|
298 | #ifdef JS_DEBUG |
---|
299 | fprintf(stderr,"Properties length : %d \n",idp->length); |
---|
300 | #endif |
---|
301 | for (index=0,argNum=idp->length;index<argNum;index++) { |
---|
302 | jsid* id = idp->vector[index]; |
---|
303 | jsval vp; |
---|
304 | JSString* str; |
---|
305 | JS_IdToValue(cx,id,&vp); |
---|
306 | char *c, *tmp; |
---|
307 | JSString *jsmsg; |
---|
308 | size_t len1; |
---|
309 | jsmsg = JS_ValueToString(cx,vp); |
---|
310 | len1 = JS_GetStringLength(jsmsg); |
---|
311 | #ifdef JS_DEBUG |
---|
312 | fprintf(stderr,"Enumerate id : %d => %s\n",i,JS_GetStringBytes(jsmsg)); |
---|
313 | #endif |
---|
314 | jsval nvp; |
---|
315 | if((JS_GetProperty(cx, tmp1, JS_GetStringBytes(jsmsg), &nvp)==JS_FALSE)) |
---|
316 | #ifdef JS_DEBUG |
---|
317 | fprintf(stderr,"Enumerate id : %d => %s => No more value\n",i,JS_GetStringBytes(jsmsg)); |
---|
318 | #endif |
---|
319 | if(JSVAL_IS_OBJECT(nvp)){ |
---|
320 | #ifdef JS_DEBUG |
---|
321 | fprintf(stderr,"JSVAL NVP IS OBJECT\n"); |
---|
322 | #endif |
---|
323 | } |
---|
324 | #ifdef JS_DEBUG |
---|
325 | else |
---|
326 | fprintf(stderr,"JSVAL NVP IS NOT OBJECT !!\n"); |
---|
327 | #endif |
---|
328 | JSObject *nvp1; |
---|
329 | JS_ValueToObject(cx,nvp,&nvp1); |
---|
330 | jsval nvp1j=OBJECT_TO_JSVAL(nvp1); |
---|
331 | if(JSVAL_IS_OBJECT(nvp1j)){ |
---|
332 | #ifdef JS_DEBUG |
---|
333 | fprintf(stderr,"JSVAL NVP1J IS OBJECT\n"); |
---|
334 | #endif |
---|
335 | tres=(maps*)malloc(sizeof(maps*)); |
---|
336 | tres->name=strdup(JS_GetStringBytes(jsmsg)); |
---|
337 | tres->content=mapFromJSObject(cx,nvp1j); |
---|
338 | tres->next=NULL; |
---|
339 | #ifdef JS_DEBUG |
---|
340 | dumpMaps(res); |
---|
341 | #endif |
---|
342 | if(res==NULL) |
---|
343 | res=tres; |
---|
344 | else |
---|
345 | addMapsToMaps(&res,tres); |
---|
346 | #ifdef JS_DEBUG |
---|
347 | dumpMaps(res); |
---|
348 | #endif |
---|
349 | } |
---|
350 | #ifdef JS_DEBUG |
---|
351 | else |
---|
352 | fprintf(stderr,"JSVAL NVP1J IS NOT OBJECT !!\n"); |
---|
353 | #endif |
---|
354 | } |
---|
355 | } |
---|
356 | } |
---|
357 | #ifdef JS_DEBUG |
---|
358 | dumpMaps(res); |
---|
359 | #endif |
---|
360 | return res; |
---|
361 | } |
---|
362 | |
---|
363 | map* mapFromJSObject(JSContext *cx,jsval t){ |
---|
364 | map *res=NULL; |
---|
365 | JSIdArray *idp=JS_Enumerate(cx,t); |
---|
366 | #ifdef JS_DEBUG |
---|
367 | fprintf(stderr,"Properties %p\n",(void*)t); |
---|
368 | #endif |
---|
369 | if(idp!=NULL) { |
---|
370 | int index; |
---|
371 | jsdouble argNum; |
---|
372 | #ifdef JS_DEBUG |
---|
373 | fprintf(stderr,"Properties length : %d \n",idp->length); |
---|
374 | #endif |
---|
375 | for (index=0,argNum=idp->length;index<argNum;index++) { |
---|
376 | jsid* id = idp->vector[index]; |
---|
377 | jsval vp; |
---|
378 | JSString* str; |
---|
379 | JS_IdToValue(cx,id,&vp); |
---|
380 | char *c, *tmp; |
---|
381 | JSString *jsmsg,*jsmsg1; |
---|
382 | size_t len,len1; |
---|
383 | jsmsg = JS_ValueToString(cx,vp); |
---|
384 | len = JS_GetStringLength(jsmsg); |
---|
385 | jsval nvp; |
---|
386 | JS_GetProperty(cx, t, JS_GetStringBytes(jsmsg), &nvp); |
---|
387 | jsmsg1 = JS_ValueToString(cx,nvp); |
---|
388 | len1 = JS_GetStringLength(jsmsg1); |
---|
389 | #ifdef JS_DEBUG |
---|
390 | fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1)); |
---|
391 | #endif |
---|
392 | if(res!=NULL) |
---|
393 | addMapToMap(&res,createMap(JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1))); |
---|
394 | else |
---|
395 | res=createMap(JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1)); |
---|
396 | } |
---|
397 | } |
---|
398 | #ifdef JS_DEBUG |
---|
399 | dumpMap(res); |
---|
400 | #endif |
---|
401 | return res; |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | /* The error reporter callback. */ |
---|
406 | void reportError(JSContext *cx, const char *message, JSErrorReport *report) |
---|
407 | { |
---|
408 | sprintf(dbg,"%s:%u:%s\n", |
---|
409 | report->filename ? report->filename : "<no filename>", |
---|
410 | (unsigned int) report->lineno, |
---|
411 | message); |
---|
412 | #ifdef JS_DEBUG |
---|
413 | fprintf(stderr,"%s",dbg); |
---|
414 | #endif |
---|
415 | fflush(stderr); |
---|
416 | } |
---|
417 | |
---|