source: trunk/zoo-project/zoo-kernel/service_internal_js.c @ 360

Last change on this file since 360 was 360, checked in by djay, 12 years ago

Add support for multiple inputs values for the same identifier.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 17.3 KB
RevLine 
[1]1/**
2 * Author : Gérald FENOY
3 *
[360]4 * Copyright (c) 2009-2012 GeoLabs SARL
[1]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
[26]25#include "service_internal.h"
[1]26
27static char dbg[1024];
28
[274]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
[336]44JSBool
45JSLoadScripts(JSContext *cx, uintN argc, jsval *argv1)
46{
47  map* request = JS_GetContextPrivate(cx);
48  map* tmpm1=getMap(request,"metapath");
49  JS_MaybeGC(cx);
50
51  char ntmp[1024];
52  getcwd(ntmp,1024);
53
54  jsval *argv = JS_ARGV(cx,argv1);
55  int i=0;
56  JS_MaybeGC(cx);
57  for(i=0;i<argc;i++){
58    JSString* jsmsg = JS_ValueToString(cx,argv[i]);
59    char *filename = JSValToChar(cx,&argv[i]);
60    char api0[strlen(tmpm1->value)+strlen(ntmp)+strlen(filename)+2];
61    sprintf(api0,"%s/%s/%s",ntmp,tmpm1->value,filename);
62#ifdef JS_DEBUG
63    fprintf(stderr,"Trying to load %s\n",api0);
64#endif
65    JSObject *api_script1=loadZooApiFile(cx,JS_GetGlobalObject(cx),api0);
66    fflush(stderr);
67  }
68  JS_MaybeGC(cx);
69  JS_SET_RVAL(cx, argv1, JSVAL_VOID);
70 
71  return JS_TRUE;
72}
73
74
[1]75int zoo_js_support(maps** main_conf,map* request,service* s,
76                   maps **inputs,maps **outputs)
77{
[9]78  maps* main=*main_conf;
79  maps* _inputs=*inputs;
80  maps* _outputs=*outputs;
81
[1]82  /* The class of the global object. */
83  JSClass global_class = {
84    "global", JSCLASS_GLOBAL_FLAGS,
85    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
86    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
87    JSCLASS_NO_OPTIONAL_MEMBERS
88  };
89
90  /* JS variables. */
91  JSRuntime *rt;
92  JSContext *cx;
93  JSObject  *global;
94
95  /* Create a JS runtime. */
96  rt = JS_NewRuntime(8L * 1024L * 1024L);
97  if (rt == NULL)
98    return 1;
[9]99 
[1]100  /* Create a context. */
[9]101  cx = JS_NewContext(rt,8192);
[1]102  if (cx == NULL){
103    return 1;
104  }
[287]105  JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT );//| JSOPTION_METHODJIT);
[1]106  JS_SetVersion(cx, JSVERSION_LATEST);
107  JS_SetErrorReporter(cx, reportError);
108
109  /* Create the global object. */
[328]110  //#ifdef JS_NewCompartmentAndGlobalObject
111  global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
112  //#else
113  //global = JS_NewObject(cx, &global_class, NULL,NULL);
114  //#endif
[1]115
116  /* Populate the global object with the standard globals,
117     like Object and Array. */
118  if (!JS_InitStandardClasses(cx, global)){
119    return 1;
120  }
[274]121
[1]122  if (!JS_DefineFunction(cx, global, "ZOORequest", JSRequest, 4, 0))
123    return 1;
[26]124  if (!JS_DefineFunction(cx, global, "ZOOUpdateStatus", JSUpdateStatus, 2, 0))
125    return 1;
[274]126  if (!JS_DefineFunction(cx, global, "alert", JSAlert, 2, 0))
[336]127    return 1; 
128  if (!JS_DefineFunction(cx, global, "importScripts", JSLoadScripts, 1, 0))
[274]129    return 1;
[1]130
[336]131  /**
132   * Add private context object
133   */
134  void* cxPrivate = request;
135  JS_SetContextPrivate(cx,cxPrivate);
136   
[42]137  map* tmpm1=getMap(request,"metapath");
138  char ntmp[1024];
139  getcwd(ntmp,1024);
140
141  /**
142   * Load the first part of the ZOO-API
143   */
[329]144  char api0[strlen(tmpm1->value)+strlen(ntmp)+16];
145  sprintf(api0,"%s/%s/ZOO-proj4js.js",ntmp,tmpm1->value);
[274]146#ifdef JS_DEBUG
[42]147  fprintf(stderr,"Trying to load %s\n",api0);
[274]148#endif
149  JSObject *api_script1=loadZooApiFile(cx,global,api0);
[42]150  fflush(stderr);
151
152  char api1[strlen(tmpm1->value)+strlen(ntmp)+11];
[329]153  sprintf(api1,"%s/%s/ZOO-api.js",ntmp,tmpm1->value);
[274]154#ifdef JS_DEBUG
[42]155  fprintf(stderr,"Trying to load %s\n",api1);
[274]156#endif
157  JSObject *api_script2=loadZooApiFile(cx,global,api1);
[42]158  fflush(stderr);
159
[1]160  /* Your application code here. This may include JSAPI calls
161     to create your own custom JS objects and run scripts. */
162  maps* out=*outputs;
163  int res=SERVICE_FAILED;
164  maps* mc=*main_conf;
165  map* tmpm2=getMap(s->content,"serviceProvider");
[42]166
[329]167  char filename[strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+3];
168  sprintf(filename,"%s/%s/%s",ntmp,tmpm1->value,tmpm2->value);
169  filename[strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+2]=0;
[274]170#ifdef JS_DEBUG
[26]171  fprintf(stderr,"FILENAME %s\n",filename);
[274]172#endif
[1]173  struct stat file_status;
174  stat(filename, &file_status);
175  char source[file_status.st_size];
176  uint16 lineno;
177  jsval rval;
178  JSBool ok ;
[274]179  JSObject *script = JS_CompileFile(cx, global, filename);
[9]180  if(script!=NULL){
[1]181    (void)JS_ExecuteScript(cx, global, script, &rval);
182  }
183  else{
184    char tmp1[1024];
185    sprintf(tmp1,"Unable to load JavaScript file %s",filename);
186    map* err=createMap("text",tmp1);
187    addMapToMap(&err,createMap("code","NoApplicableCode"));
188    printExceptionReportResponse(mc,err);
189    JS_DestroyContext(cx);
190    JS_DestroyRuntime(rt);
191    JS_ShutDown();
192    exit(-1);
193  }
194  /* Call a function in obj's scope. */
195  jsval argv[3];
[9]196  JSObject *jsargv1=JSObject_FromMaps(cx,*main_conf);
197  argv[0] = OBJECT_TO_JSVAL(jsargv1);
198  JSObject *jsargv2=JSObject_FromMaps(cx,*inputs);
199  argv[1] = OBJECT_TO_JSVAL(jsargv2);
200  JSObject *jsargv3=JSObject_FromMaps(cx,*outputs);
201  argv[2] = OBJECT_TO_JSVAL(jsargv3);
202  jsval rval1=JSVAL_NULL;
[1]203#ifdef JS_DEBUG
204  fprintf(stderr, "object %p\n", (void *) argv[2]);
205#endif
206
207  ok = JS_CallFunctionName(cx, global, s->name, 3, argv, &rval1);
208
209#ifdef JS_DEBUG
210  fprintf(stderr, "object %p\n", (void *) argv[2]);
211#endif
212
213  JSObject *d;
[9]214  if (ok==JS_TRUE && JSVAL_IS_OBJECT(rval1)==JS_TRUE) {
[1]215#ifdef JS_DEBUG
216    fprintf(stderr,"Function run sucessfully !\n");
217#endif
218    /* Should get a number back from the service function call. */
219    ok = JS_ValueToObject(cx, rval1, &d);
220  }else{
221    /* Unable to run JS function */
222    char tmp1[1024];
[9]223    if(strlen(dbg)==0)
224      sprintf(dbg,"No result was found after the function call");
[274]225    sprintf(tmp1,"Unable to run %s from the JavaScript file %s : \n %s",s->name,filename,dbg);
226#ifdef JS_DEBUG
[1]227    fprintf(stderr,"%s",tmp1);
[274]228#endif
[1]229    map* err=createMap("text",tmp1);
[9]230    addToMap(err,"code","NoApplicableCode");
[1]231    printExceptionReportResponse(*main_conf,err);
[9]232    freeMap(&err);
233    free(err);
[1]234    JS_DestroyContext(cx);
235    JS_DestroyRuntime(rt);
236    JS_ShutDown();
[9]237    // Should return -1 here but the unallocation won't work from zoo_service_loader.c line 1847
[1]238    exit(-1);
239  }
240
241  jsval t=OBJECT_TO_JSVAL(d);
[9]242  if(JS_IsArrayObject(cx,d)){
[1]243#ifdef JS_DEBUG
244    fprintf(stderr,"An array was returned !\n");
245#endif
[9]246    jsint len;
247    if((JS_GetArrayLength(cx, d, &len)==JS_FALSE)){
[1]248#ifdef JS_DEBUG
249      fprintf(stderr,"outputs array is empty\n");
250#endif
251    }
252    jsval tmp1;
[9]253    JSBool hasResult=JS_GetElement(cx,d,0,&tmp1);
[1]254    res=JSVAL_TO_INT(tmp1);
255#ifdef JS_DEBUG
256    fprintf(stderr," * %d * \n",res);
257#endif
258    jsval tmp2;
[9]259    JSBool hasElement=JS_GetElement(cx,d,1,&tmp2);
260    if(hasElement==JS_TRUE){
261      *outputs=mapsFromJSObject(cx,tmp2);
262    }
[1]263  }
264  else{
[9]265#ifdef JS_DEBUG
266    fprintf(stderr,"The serice didn't return an array !\n");
267#endif
[1]268    jsval tmp1;
[9]269    JSBool hasResult=JS_GetProperty(cx,d,"result",&tmp1);
[1]270    res=JSVAL_TO_INT(tmp1);
271
[9]272#ifdef JS_DEBUG
[1]273    fprintf(stderr," * %d * \n",res);
[9]274#endif
[1]275    jsval tmp2;
[9]276    JSBool hasElement=JS_GetProperty(cx,d,"outputs",&tmp2);
[274]277#ifdef JS_DEBUG
[1]278    if(!hasElement)
279      fprintf(stderr,"No outputs property returned\n");
[9]280    if(JS_IsArrayObject(cx,JSVAL_TO_OBJECT(tmp2)))
[1]281      fprintf(stderr,"outputs is array an as expected\n");
282    else
283      fprintf(stderr,"outputs is not an array as expected\n");
[274]284#endif
[9]285    *outputs=mapsFromJSObject(cx,tmp2);
[1]286#ifdef JS_DEBUG
[274]287    dumpMaps(outputs);
[1]288#endif
289  }
290
291  /* Cleanup. */
[274]292  JS_DestroyContext(cx);
[1]293  JS_DestroyRuntime(rt);
294  JS_ShutDown();
295#ifdef JS_DEBUG
296  fprintf(stderr,"Returned value %d\n",res);
297#endif
298  return res;
299}
300
[274]301JSObject * loadZooApiFile(JSContext *cx,JSObject  *global, char* filename){
[42]302  struct stat api_status;
303  int s=stat(filename, &api_status);
304  if(s==0){
305    jsval rval;
306    JSBool ok ;
[274]307    JSObject *script = JS_CompileFile(cx, JS_GetGlobalObject(cx), filename);
[42]308    if(script!=NULL){
[274]309      (void)JS_ExecuteScript(cx, JS_GetGlobalObject(cx), script, &rval);
310#ifdef JS_DEBUG
[42]311      fprintf(stderr,"**************\n%s correctly loaded\n**************\n",filename);
[274]312#endif
[42]313      return script;
314    }
[274]315#ifdef JS_DEBUG
[42]316    else
317      fprintf(stderr,"\n**************\nUnable to run %s\n**************\n",filename);
[274]318#endif
[42]319  }
[274]320#ifdef JS_DEBUG
[42]321  else
322    fprintf(stderr,"\n**************\nUnable to load %s\n**************\n",filename);
[274]323#endif
[42]324  return NULL;
325}
326
[1]327JSObject* JSObject_FromMaps(JSContext *cx,maps* t){
[328]328
329  JSObject* res=JS_NewObject(cx, NULL, NULL, NULL);
330  //JSObject *res = JS_NewArrayObject(cx, 0, NULL);
[9]331  if(res==NULL)
332    fprintf(stderr,"Array Object is NULL!\n");
[1]333  maps* tmp=t;
[328]334
[1]335  while(tmp!=NULL){
336    jsuint len;
337    JSObject* res1=JS_NewObject(cx, NULL, NULL, NULL);
338    JSObject *pval=JSObject_FromMap(cx,tmp->content);
339    jsval pvalj=OBJECT_TO_JSVAL(pval);
[328]340    JS_SetProperty(cx, res, tmp->name, &pvalj);
341
[1]342#ifdef JS_DEBUG
343    fprintf(stderr,"Length of the Array %d, element : %s added \n",len,tmp->name);
344#endif
345    tmp=tmp->next;
346  } 
347  return res;
348}
349
350JSObject* JSObject_FromMap(JSContext *cx,map* t){
351  JSObject* res=JS_NewObject(cx, NULL, NULL, NULL);
352  jsval resf =  OBJECT_TO_JSVAL(res);
353  map* tmpm=t;
[360]354  map* isArray=getMap(t,"isArray");
355  map* isBinary=getMap(t,"size");
356  map* tmap=getMapType(t);
357  if(tmap==NULL)
358    fprintf(stderr,"tmap is null !\n");
359  else
360    fprintf(stderr,"tmap is not null ! (%s = %s)\n",tmap->name,tmap->value);
361
362  /* Avoid gesture of binary content which failed due to strlen function use */
363  if(isBinary!=NULL){
364    return res;
365  }
[1]366  while(tmpm!=NULL){
[360]367    if(isArray==NULL || strncasecmp(tmpm->name,"value",5)!=0 || 
368       (tmap!=NULL && strncasecmp(tmpm->name,tmap->name,strlen(tmap->name))!=0)){
369      jsval jsstr = STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpm->value,strlen(tmpm->value)));
370      JS_SetProperty(cx, res, tmpm->name,&jsstr);
[1]371#ifdef JS_DEBUG
[360]372      fprintf(stderr,"%s => %s\n",tmpm->name,tmpm->value);
[1]373#endif
[360]374    }
[1]375    tmpm=tmpm->next;
376  }
[360]377  if(isArray!=NULL){
378    map* len=getMap(t,"length");
379    int cnt=atoi(len->value);
380    JSObject* values=JS_NewArrayObject( cx, cnt, NULL );
381    JSObject* mvalues=JS_NewArrayObject( cx, cnt, NULL );
382    map *tmpm1,*tmpm2;
383    int i=0;
384    for(i=0;i<cnt;i++){
385      tmpm1=getMapArray(t,"value",i);
386      tmpm2=getMapArray(t,tmap->name,i);
387      if(tmpm1!=NULL){
388        jsval jsstr = STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpm1->value,strlen(tmpm1->value)));
389        JS_SetElement( cx, values, i, &jsstr );
390      }
391      if(tmpm2!=NULL){
392        jsval jsstr = STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpm2->value,strlen(tmpm2->value)));
393        JS_SetElement( cx, mvalues, i, &jsstr );
394      }
395    }
396    jsval jvalues=OBJECT_TO_JSVAL(values);
397    jsval jmvalues=OBJECT_TO_JSVAL(mvalues);
398    JS_SetProperty(cx, res,"value",&jvalues);
399    JS_SetProperty(cx, res,tmap->name,&jmvalues);
400  }
[1]401  return res;
402}
403
404maps* mapsFromJSObject(JSContext *cx,jsval t){
405  maps *res=NULL;
406  maps *tres=NULL;
[9]407  jsint oi=0;
408  JSObject* tt=JSVAL_TO_OBJECT(t);
[333]409  if(JS_IsArrayObject(cx,tt)){
[1]410#ifdef JS_DEBUG
411    fprintf(stderr,"Is finally an array !\n");
[333]412#endif
[1]413  }
[333]414  else{
415#ifdef JS_DEBUG
[1]416    fprintf(stderr,"Is not an array !\n");
417#endif
[333]418    JSIdArray *idp=JS_Enumerate(cx,tt);
419    if(idp!=NULL) {
420      int index;
421      jsdouble argNum;
422#ifdef JS_DEBUG
423      fprintf(stderr,"Properties length :  %d \n",idp->length);
424#endif
425     
426      for (index=0,argNum=idp->length;index<argNum;index++) { 
427        jsval id = idp->vector[index];
428        jsval vp;
429        JSString* str; 
430        JS_IdToValue(cx,id,&vp);
431        char *c, *tmp;
432        JSString *jsmsg;
433        size_t len1;
434        jsmsg = JS_ValueToString(cx,vp);
435        len1 = JS_GetStringLength(jsmsg);
436
437        tres=(maps*)malloc(MAPS_SIZE);
438        tres->name=strdup(JS_EncodeString(cx,jsmsg));
439        tres->content=NULL;
440        tres->next=NULL;
441
442        jsval nvp=JSVAL_NULL;
443        if((JS_GetProperty(cx, JSVAL_TO_OBJECT(tt), JS_EncodeString(cx,jsmsg), &nvp)==JS_FALSE)){
444#ifdef JS_DEBUG
445          fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,JS_EncodeString(cx,jsmsg));
446#endif
447        }
448       
449        JSObject *nvp1=JSVAL_NULL;
450        JS_ValueToObject(cx,nvp,&nvp1);
451        jsval nvp1j=OBJECT_TO_JSVAL(nvp1);
452        if(JSVAL_IS_OBJECT(nvp1j)){
453          tres->content=mapFromJSObject(cx,nvp1j);
454        }
455
456        if(res==NULL)
457          res=dupMaps(&tres);
458        else
459          addMapsToMaps(&res,tres);
460        freeMaps(&tres);
461        free(tres);
462        tres=NULL;
463       
464       
465      }
466    }
467  }
468
[9]469  jsint len;
470  JSBool hasLen=JS_GetArrayLength(cx, tt, &len);
471  if(hasLen==JS_FALSE){
[1]472#ifdef JS_DEBUG
473    fprintf(stderr,"outputs array is empty\n");
474#endif
475  }
476#ifdef JS_DEBUG
477  fprintf(stderr,"outputs array length : %d\n",len);
478#endif
[9]479  for(oi=0;oi < len;oi++){
[1]480#ifdef JS_DEBUG
[9]481    fprintf(stderr,"outputs array length : %d step %d \n",len,oi);
[1]482#endif
483    jsval tmp1;
[9]484    JSBool hasElement=JS_GetElement(cx,tt,oi,&tmp1);
485    JSObject *otmp1=JSVAL_TO_OBJECT(tmp1);
486    JSIdArray *idp=JS_Enumerate(cx,otmp1);
[1]487    if(idp!=NULL) {
488      int index;
489      jsdouble argNum;
490#ifdef JS_DEBUG
491      fprintf(stderr,"Properties length :  %d \n",idp->length);
492#endif
[274]493      tres=(maps*)malloc(MAPS_SIZE);
494      tres->name=NULL;
495      tres->content=NULL;
496      tres->next=NULL;
497
[1]498      for (index=0,argNum=idp->length;index<argNum;index++) { 
[9]499        jsval id = idp->vector[index];
[1]500        jsval vp;
501        JSString* str; 
502        JS_IdToValue(cx,id,&vp);
503        char *c, *tmp;
504        JSString *jsmsg;
505        size_t len1;
506        jsmsg = JS_ValueToString(cx,vp);
507        len1 = JS_GetStringLength(jsmsg);
508#ifdef JS_DEBUG
[274]509        fprintf(stderr,"Enumerate id : %d => %s\n",oi,JS_EncodeString(cx,jsmsg));
[1]510#endif
[9]511        jsval nvp=JSVAL_NULL;
[274]512        if((JS_GetProperty(cx, JSVAL_TO_OBJECT(tmp1), JS_EncodeString(cx,jsmsg), &nvp)==JS_FALSE)){
[1]513#ifdef JS_DEBUG
[274]514          fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,JS_EncodeString(cx,jsmsg));
[1]515#endif
[274]516        }
517       
[1]518        if(JSVAL_IS_OBJECT(nvp)){
519#ifdef JS_DEBUG
520          fprintf(stderr,"JSVAL NVP IS OBJECT\n");
521#endif
522        }
[274]523
524        JSObject *nvp1=JSVAL_NULL;
[1]525        JS_ValueToObject(cx,nvp,&nvp1);
526        jsval nvp1j=OBJECT_TO_JSVAL(nvp1);
527        if(JSVAL_IS_OBJECT(nvp1j)){
[274]528          JSString *jsmsg1;
529          JSObject *nvp2=JSVAL_NULL;
530          jsmsg1 = JS_ValueToString(cx,nvp1j);
531          len1 = JS_GetStringLength(jsmsg1);
[277]532#ifdef JS_DEBUG
[274]533          fprintf(stderr,"JSVAL NVP1J IS OBJECT %s = %s\n",JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
[277]534#endif
[274]535          if(strcasecmp(JS_EncodeString(cx,jsmsg1),"[object Object]")==0){
536            tres->name=strdup(JS_EncodeString(cx,jsmsg));
537            tres->content=mapFromJSObject(cx,nvp1j);
538          }
[1]539          else
[274]540            if(strcasecmp(JS_EncodeString(cx,jsmsg),"name")==0){
541              tres->name=strdup(JS_EncodeString(cx,jsmsg1));
542            }
543            else{
544              if(tres->content==NULL)
545                tres->content=createMap(JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
546              else
547                addToMap(tres->content,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
548            }
[1]549        }
550#ifdef JS_DEBUG
551        else
552          fprintf(stderr,"JSVAL NVP1J IS NOT OBJECT !!\n");
553#endif
[274]554
[1]555      }
[277]556#ifdef JS_DEBUG
[274]557      dumpMaps(tres);
[277]558#endif
[274]559      if(res==NULL)
560        res=dupMaps(&tres);
561      else
562        addMapsToMaps(&res,tres);
563      freeMaps(&tres);
564      free(tres);
565      tres=NULL;
566
[1]567    }
568  }
[277]569#ifdef JS_DEBUG
[1]570  dumpMaps(res);
[277]571#endif
[1]572  return res;
573}
574
575map* mapFromJSObject(JSContext *cx,jsval t){
576  map *res=NULL;
[9]577  JSIdArray *idp=JS_Enumerate(cx,JSVAL_TO_OBJECT(t));
[1]578#ifdef JS_DEBUG
579  fprintf(stderr,"Properties %p\n",(void*)t);
580#endif
581  if(idp!=NULL) {
582    int index;
583    jsdouble argNum;
584#ifdef JS_DEBUG
585    fprintf(stderr,"Properties length :  %d \n",idp->length);
586#endif
587    for (index=0,argNum=idp->length;index<argNum;index++) { 
[9]588      jsval id = idp->vector[index];
[1]589      jsval vp;
590      JSString* str; 
591      JS_IdToValue(cx,id,&vp);
592      char *c, *tmp;
593      JSString *jsmsg,*jsmsg1;
594      size_t len,len1;
595      jsmsg = JS_ValueToString(cx,vp);
596      len = JS_GetStringLength(jsmsg);
597      jsval nvp;
[274]598      JS_GetProperty(cx, JSVAL_TO_OBJECT(t), JS_EncodeString(cx,jsmsg), &nvp);
[1]599      jsmsg1 = JS_ValueToString(cx,nvp);
600      len1 = JS_GetStringLength(jsmsg1);
601#ifdef JS_DEBUG
[274]602      fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
[1]603#endif
[9]604      if(res!=NULL){
[26]605#ifdef JS_DEBUG
[274]606        fprintf(stderr,"%s - %s\n",JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
[26]607#endif
[274]608        addToMap(res,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
[9]609      }
610      else{
[274]611        res=createMap(JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
[9]612        res->next=NULL;
613      }
[26]614#ifdef JS_DEBUG
[9]615      dumpMap(res);
[26]616#endif
[1]617    }
618  }
619#ifdef JS_DEBUG
620  dumpMap(res);
621#endif
622  return res;
623}
624
625/* The error reporter callback. */
626void reportError(JSContext *cx, const char *message, JSErrorReport *report)
627{
628  sprintf(dbg,"%s:%u:%s\n",
629          report->filename ? report->filename : "<no filename>",
630          (unsigned int) report->lineno,
631          message);
632#ifdef JS_DEBUG
633  fprintf(stderr,"%s",dbg);
634#endif
635  fflush(stderr);
636}
637
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