source: trunk/zoo-kernel/service_internal_js.c @ 277

Last change on this file since 277 was 277, checked in by djay, 13 years ago

Remove uneeded verbose debug messages.

File size: 13.9 KB
Line 
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.h"
26
27static char dbg[1024];
28
29JSBool
30JSAlert(JSContext *cx, uintN argc, jsval *argv1)
31{
32  jsval *argv = JS_ARGV(cx,argv1);
33  int i=0;
34  JS_MaybeGC(cx);
35  for(i=0;i<argc;i++){
36    JSString* jsmsg = JS_ValueToString(cx,argv[i]);
37    fprintf(stderr,"[ZOO-API:JS] %s\n",JS_EncodeString(cx,jsmsg));
38  }
39  JS_MaybeGC(cx);
40 
41  return JS_TRUE;
42}
43
44int zoo_js_support(maps** main_conf,map* request,service* s,
45                   maps **inputs,maps **outputs)
46{
47  maps* main=*main_conf;
48  maps* _inputs=*inputs;
49  maps* _outputs=*outputs;
50
51  /* The class of the global object. */
52  JSClass global_class = {
53    "global", JSCLASS_GLOBAL_FLAGS,
54    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
55    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
56    JSCLASS_NO_OPTIONAL_MEMBERS
57  };
58
59  /* JS variables. */
60  JSRuntime *rt;
61  JSContext *cx;
62  JSObject  *global;
63
64  /* Create a JS runtime. */
65  rt = JS_NewRuntime(8L * 1024L * 1024L);
66  if (rt == NULL)
67    return 1;
68 
69  /* Create a context. */
70  cx = JS_NewContext(rt,8192);
71  if (cx == NULL){
72    return 1;
73  }
74  JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
75  JS_SetVersion(cx, JSVERSION_LATEST);
76  JS_SetErrorReporter(cx, reportError);
77
78  /* Create the global object. */
79  global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
80
81  /* Populate the global object with the standard globals,
82     like Object and Array. */
83  if (!JS_InitStandardClasses(cx, global)){
84    return 1;
85  }
86
87  if (!JS_DefineFunction(cx, global, "ZOORequest", JSRequest, 4, 0))
88    return 1;
89  if (!JS_DefineFunction(cx, global, "ZOOUpdateStatus", JSUpdateStatus, 2, 0))
90    return 1;
91  if (!JS_DefineFunction(cx, global, "alert", JSAlert, 2, 0))
92    return 1;
93
94  map* tmpm1=getMap(request,"metapath");
95  char ntmp[1024];
96  getcwd(ntmp,1024);
97
98  /**
99   * Load the first part of the ZOO-API
100   */
101  char api0[strlen(tmpm1->value)+strlen(ntmp)+15];
102  sprintf(api0,"%s/%sZOO-proj4js.js",ntmp,tmpm1->value);
103#ifdef JS_DEBUG
104  fprintf(stderr,"Trying to load %s\n",api0);
105#endif
106  JSObject *api_script1=loadZooApiFile(cx,global,api0);
107  fflush(stderr);
108
109  char api1[strlen(tmpm1->value)+strlen(ntmp)+11];
110  sprintf(api1,"%s/%sZOO-api.js",ntmp,tmpm1->value);
111#ifdef JS_DEBUG
112  fprintf(stderr,"Trying to load %s\n",api1);
113#endif
114  JSObject *api_script2=loadZooApiFile(cx,global,api1);
115  fflush(stderr);
116
117  /* Your application code here. This may include JSAPI calls
118     to create your own custom JS objects and run scripts. */
119  maps* out=*outputs;
120  int res=SERVICE_FAILED;
121  maps* mc=*main_conf;
122  map* tmpm2=getMap(s->content,"serviceProvider");
123
124  char filename[strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+2];
125  sprintf(filename,"%s/%s%s",ntmp,tmpm1->value,tmpm2->value);
126  filename[strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+1]=0;
127#ifdef JS_DEBUG
128  fprintf(stderr,"FILENAME %s\n",filename);
129#endif
130  struct stat file_status;
131  stat(filename, &file_status);
132  char source[file_status.st_size];
133  uint16 lineno;
134  jsval rval;
135  JSBool ok ;
136  JSObject *script = JS_CompileFile(cx, global, filename);
137  if(script!=NULL){
138    (void)JS_ExecuteScript(cx, global, script, &rval);
139  }
140  else{
141    char tmp1[1024];
142    sprintf(tmp1,"Unable to load JavaScript file %s",filename);
143    map* err=createMap("text",tmp1);
144    addMapToMap(&err,createMap("code","NoApplicableCode"));
145    printExceptionReportResponse(mc,err);
146    JS_DestroyContext(cx);
147    JS_DestroyRuntime(rt);
148    JS_ShutDown();
149    exit(-1);
150  }
151  /* Call a function in obj's scope. */
152  jsval argv[3];
153  JSObject *jsargv1=JSObject_FromMaps(cx,*main_conf);
154  argv[0] = OBJECT_TO_JSVAL(jsargv1);
155  JSObject *jsargv2=JSObject_FromMaps(cx,*inputs);
156  argv[1] = OBJECT_TO_JSVAL(jsargv2);
157  JSObject *jsargv3=JSObject_FromMaps(cx,*outputs);
158  argv[2] = OBJECT_TO_JSVAL(jsargv3);
159  jsval rval1=JSVAL_NULL;
160#ifdef JS_DEBUG
161  fprintf(stderr, "object %p\n", (void *) argv[2]);
162#endif
163
164  ok = JS_CallFunctionName(cx, global, s->name, 3, argv, &rval1);
165
166#ifdef JS_DEBUG
167  fprintf(stderr, "object %p\n", (void *) argv[2]);
168#endif
169
170  JSObject *d;
171  if (ok==JS_TRUE && JSVAL_IS_OBJECT(rval1)==JS_TRUE) {
172#ifdef JS_DEBUG
173    fprintf(stderr,"Function run sucessfully !\n");
174#endif
175    /* Should get a number back from the service function call. */
176    ok = JS_ValueToObject(cx, rval1, &d);
177  }else{
178    /* Unable to run JS function */
179    char tmp1[1024];
180    if(strlen(dbg)==0)
181      sprintf(dbg,"No result was found after the function call");
182    sprintf(tmp1,"Unable to run %s from the JavaScript file %s : \n %s",s->name,filename,dbg);
183#ifdef JS_DEBUG
184    fprintf(stderr,"%s",tmp1);
185#endif
186    map* err=createMap("text",tmp1);
187    addToMap(err,"code","NoApplicableCode");
188    printExceptionReportResponse(*main_conf,err);
189    freeMap(&err);
190    free(err);
191    JS_DestroyContext(cx);
192    JS_DestroyRuntime(rt);
193    JS_ShutDown();
194    // Should return -1 here but the unallocation won't work from zoo_service_loader.c line 1847
195    exit(-1);
196  }
197
198  jsval t=OBJECT_TO_JSVAL(d);
199  if(JS_IsArrayObject(cx,d)){
200#ifdef JS_DEBUG
201    fprintf(stderr,"An array was returned !\n");
202#endif
203    jsint len;
204    if((JS_GetArrayLength(cx, d, &len)==JS_FALSE)){
205#ifdef JS_DEBUG
206      fprintf(stderr,"outputs array is empty\n");
207#endif
208    }
209    jsval tmp1;
210    JSBool hasResult=JS_GetElement(cx,d,0,&tmp1);
211    res=JSVAL_TO_INT(tmp1);
212#ifdef JS_DEBUG
213    fprintf(stderr," * %d * \n",res);
214#endif
215    jsval tmp2;
216    JSBool hasElement=JS_GetElement(cx,d,1,&tmp2);
217    if(hasElement==JS_TRUE){
218      *outputs=mapsFromJSObject(cx,tmp2);
219    }
220  }
221  else{
222#ifdef JS_DEBUG
223    fprintf(stderr,"The serice didn't return an array !\n");
224#endif
225    jsval tmp1;
226    JSBool hasResult=JS_GetProperty(cx,d,"result",&tmp1);
227    res=JSVAL_TO_INT(tmp1);
228
229#ifdef JS_DEBUG
230    fprintf(stderr," * %d * \n",res);
231#endif
232    jsval tmp2;
233    JSBool hasElement=JS_GetProperty(cx,d,"outputs",&tmp2);
234#ifdef JS_DEBUG
235    if(!hasElement)
236      fprintf(stderr,"No outputs property returned\n");
237    if(JS_IsArrayObject(cx,JSVAL_TO_OBJECT(tmp2)))
238      fprintf(stderr,"outputs is array an as expected\n");
239    else
240      fprintf(stderr,"outputs is not an array as expected\n");
241#endif
242    *outputs=mapsFromJSObject(cx,tmp2);
243#ifdef JS_DEBUG
244    dumpMaps(outputs);
245#endif
246  }
247
248  /* Cleanup. */
249  JS_DestroyContext(cx);
250  JS_DestroyRuntime(rt);
251  JS_ShutDown();
252#ifdef JS_DEBUG
253  fprintf(stderr,"Returned value %d\n",res);
254#endif
255  return res;
256}
257
258JSObject * loadZooApiFile(JSContext *cx,JSObject  *global, char* filename){
259  struct stat api_status;
260  int s=stat(filename, &api_status);
261  if(s==0){
262    jsval rval;
263    JSBool ok ;
264    JSObject *script = JS_CompileFile(cx, JS_GetGlobalObject(cx), filename);
265    if(script!=NULL){
266      (void)JS_ExecuteScript(cx, JS_GetGlobalObject(cx), script, &rval);
267#ifdef JS_DEBUG
268      fprintf(stderr,"**************\n%s correctly loaded\n**************\n",filename);
269#endif
270      return script;
271    }
272#ifdef JS_DEBUG
273    else
274      fprintf(stderr,"\n**************\nUnable to run %s\n**************\n",filename);
275#endif
276  }
277#ifdef JS_DEBUG
278  else
279    fprintf(stderr,"\n**************\nUnable to load %s\n**************\n",filename);
280#endif
281  return NULL;
282}
283
284JSObject* JSObject_FromMaps(JSContext *cx,maps* t){
285  JSObject *res = JS_NewArrayObject(cx, 0, NULL);
286  if(res==NULL)
287    fprintf(stderr,"Array Object is NULL!\n");
288  maps* tmp=t;
289  while(tmp!=NULL){
290    jsuint len;
291    JSObject* res1=JS_NewObject(cx, NULL, NULL, NULL);
292    JSObject *pval=JSObject_FromMap(cx,tmp->content);
293    jsval pvalj=OBJECT_TO_JSVAL(pval);
294    JS_SetProperty(cx, res1, tmp->name, &pvalj);
295    JS_GetArrayLength(cx, res, &len);
296    jsval res1j = OBJECT_TO_JSVAL(res1);
297    JS_SetElement(cx,res,len,&res1j);
298#ifdef JS_DEBUG
299    fprintf(stderr,"Length of the Array %d, element : %s added \n",len,tmp->name);
300#endif
301    tmp=tmp->next;
302  } 
303  return res;
304}
305
306JSObject* JSObject_FromMap(JSContext *cx,map* t){
307  JSObject* res=JS_NewObject(cx, NULL, NULL, NULL);
308  jsval resf =  OBJECT_TO_JSVAL(res);
309  map* tmpm=t;
310  while(tmpm!=NULL){
311    jsval jsstr = STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpm->value,strlen(tmpm->value)));
312    JS_SetProperty(cx, res, tmpm->name,&jsstr);
313#ifdef JS_DEBUG
314    fprintf(stderr,"%s => %s\n",tmpm->name,tmpm->value);
315#endif
316    tmpm=tmpm->next;
317  }
318  return res;
319}
320
321maps* mapsFromJSObject(JSContext *cx,jsval t){
322  maps *res=NULL;
323  maps *tres=NULL;
324  jsint oi=0;
325  JSObject* tt=JSVAL_TO_OBJECT(t);
326#ifdef JS_DEBUG
327  fprintf(stderr,"Is finally an array ?\n");
328  if(JS_IsArrayObject(cx,tt)){
329    fprintf(stderr,"Is finally an array !\n");
330  }
331  else
332    fprintf(stderr,"Is not an array !\n");
333#endif
334  jsint len;
335  JSBool hasLen=JS_GetArrayLength(cx, tt, &len);
336  if(hasLen==JS_FALSE){
337#ifdef JS_DEBUG
338    fprintf(stderr,"outputs array is empty\n");
339#endif
340  }
341#ifdef JS_DEBUG
342  fprintf(stderr,"outputs array length : %d\n",len);
343#endif
344  for(oi=0;oi < len;oi++){
345#ifdef JS_DEBUG
346    fprintf(stderr,"outputs array length : %d step %d \n",len,oi);
347#endif
348    jsval tmp1;
349    JSBool hasElement=JS_GetElement(cx,tt,oi,&tmp1);
350    JSObject *otmp1=JSVAL_TO_OBJECT(tmp1);
351    JSIdArray *idp=JS_Enumerate(cx,otmp1);
352    if(idp!=NULL) {
353      int index;
354      jsdouble argNum;
355#ifdef JS_DEBUG
356      fprintf(stderr,"Properties length :  %d \n",idp->length);
357#endif
358      tres=(maps*)malloc(MAPS_SIZE);
359      tres->name=NULL;
360      tres->content=NULL;
361      tres->next=NULL;
362
363      for (index=0,argNum=idp->length;index<argNum;index++) { 
364        jsval id = idp->vector[index];
365        jsval vp;
366        JSString* str; 
367        JS_IdToValue(cx,id,&vp);
368        char *c, *tmp;
369        JSString *jsmsg;
370        size_t len1;
371        jsmsg = JS_ValueToString(cx,vp);
372        len1 = JS_GetStringLength(jsmsg);
373#ifdef JS_DEBUG
374        fprintf(stderr,"Enumerate id : %d => %s\n",oi,JS_EncodeString(cx,jsmsg));
375#endif
376        jsval nvp=JSVAL_NULL;
377        if((JS_GetProperty(cx, JSVAL_TO_OBJECT(tmp1), JS_EncodeString(cx,jsmsg), &nvp)==JS_FALSE)){
378#ifdef JS_DEBUG
379          fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,JS_EncodeString(cx,jsmsg));
380#endif
381        }
382       
383        if(JSVAL_IS_OBJECT(nvp)){
384#ifdef JS_DEBUG
385          fprintf(stderr,"JSVAL NVP IS OBJECT\n");
386#endif
387        }
388
389        JSObject *nvp1=JSVAL_NULL;
390        JS_ValueToObject(cx,nvp,&nvp1);
391        jsval nvp1j=OBJECT_TO_JSVAL(nvp1);
392        if(JSVAL_IS_OBJECT(nvp1j)){
393          JSString *jsmsg1;
394          JSObject *nvp2=JSVAL_NULL;
395          jsmsg1 = JS_ValueToString(cx,nvp1j);
396          len1 = JS_GetStringLength(jsmsg1);
397#ifdef JS_DEBUG
398          fprintf(stderr,"JSVAL NVP1J IS OBJECT %s = %s\n",JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
399#endif
400          if(strcasecmp(JS_EncodeString(cx,jsmsg1),"[object Object]")==0){
401            tres->name=strdup(JS_EncodeString(cx,jsmsg));
402            tres->content=mapFromJSObject(cx,nvp1j);
403          }
404          else
405            if(strcasecmp(JS_EncodeString(cx,jsmsg),"name")==0){
406              tres->name=strdup(JS_EncodeString(cx,jsmsg1));
407            }
408            else{
409              if(tres->content==NULL)
410                tres->content=createMap(JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
411              else
412                addToMap(tres->content,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
413            }
414        }
415#ifdef JS_DEBUG
416        else
417          fprintf(stderr,"JSVAL NVP1J IS NOT OBJECT !!\n");
418#endif
419
420      }
421#ifdef JS_DEBUG
422      dumpMaps(tres);
423#endif
424      if(res==NULL)
425        res=dupMaps(&tres);
426      else
427        addMapsToMaps(&res,tres);
428      freeMaps(&tres);
429      free(tres);
430      tres=NULL;
431
432    }
433  }
434#ifdef JS_DEBUG
435  dumpMaps(res);
436#endif
437  return res;
438}
439
440map* mapFromJSObject(JSContext *cx,jsval t){
441  map *res=NULL;
442  JSIdArray *idp=JS_Enumerate(cx,JSVAL_TO_OBJECT(t));
443#ifdef JS_DEBUG
444  fprintf(stderr,"Properties %p\n",(void*)t);
445#endif
446  if(idp!=NULL) {
447    int index;
448    jsdouble argNum;
449#ifdef JS_DEBUG
450    fprintf(stderr,"Properties length :  %d \n",idp->length);
451#endif
452    for (index=0,argNum=idp->length;index<argNum;index++) { 
453      jsval id = idp->vector[index];
454      jsval vp;
455      JSString* str; 
456      JS_IdToValue(cx,id,&vp);
457      char *c, *tmp;
458      JSString *jsmsg,*jsmsg1;
459      size_t len,len1;
460      jsmsg = JS_ValueToString(cx,vp);
461      len = JS_GetStringLength(jsmsg);
462      jsval nvp;
463      JS_GetProperty(cx, JSVAL_TO_OBJECT(t), JS_EncodeString(cx,jsmsg), &nvp);
464      jsmsg1 = JS_ValueToString(cx,nvp);
465      len1 = JS_GetStringLength(jsmsg1);
466#ifdef JS_DEBUG
467      fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
468#endif
469      if(res!=NULL){
470#ifdef JS_DEBUG
471        fprintf(stderr,"%s - %s\n",JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
472#endif
473        addToMap(res,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
474      }
475      else{
476        res=createMap(JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
477        res->next=NULL;
478      }
479#ifdef JS_DEBUG
480      dumpMap(res);
481#endif
482    }
483  }
484#ifdef JS_DEBUG
485  dumpMap(res);
486#endif
487  return res;
488}
489
490/* The error reporter callback. */
491void reportError(JSContext *cx, const char *message, JSErrorReport *report)
492{
493  sprintf(dbg,"%s:%u:%s\n",
494          report->filename ? report->filename : "<no filename>",
495          (unsigned int) report->lineno,
496          message);
497#ifdef JS_DEBUG
498  fprintf(stderr,"%s",dbg);
499#endif
500  fflush(stderr);
501}
502
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png