source: branches/branch-1.2/zoo-kernel/service_internal_js.c @ 834

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

Merge branch-1.2 r268:r296.

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

Search

Context Navigation

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