Ignore:
Timestamp:
Aug 5, 2011, 3:02:43 PM (13 years ago)
Author:
djay
Message:

Merge branch-1.2 r268:r296.

Location:
branches/branch-1.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/branch-1.2

  • branches/branch-1.2/zoo-kernel/service_internal_js.c

    r42 r301  
    2727static char dbg[1024];
    2828
     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
    2944int zoo_js_support(maps** main_conf,map* request,service* s,
    3045                   maps **inputs,maps **outputs)
     
    5772    return 1;
    5873  }
    59   JS_SetOptions(cx, JSOPTION_VAROBJFIX);
     74  JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT );//| JSOPTION_METHODJIT);
    6075  JS_SetVersion(cx, JSVERSION_LATEST);
    6176  JS_SetErrorReporter(cx, reportError);
    6277
    6378  /* Create the global object. */
    64   global = JS_NewObject(cx, &global_class, NULL, NULL);
    65   if (global == NULL){
    66     return 1;
    67   }
     79  //global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
     80  global = JS_NewObject(cx, &global_class, NULL,NULL);
    6881
    6982  /* Populate the global object with the standard globals,
     
    7285    return 1;
    7386  }
     87
    7488  if (!JS_DefineFunction(cx, global, "ZOORequest", JSRequest, 4, 0))
    7589    return 1;
    7690  if (!JS_DefineFunction(cx, global, "ZOOUpdateStatus", JSUpdateStatus, 2, 0))
     91    return 1;
     92  if (!JS_DefineFunction(cx, global, "alert", JSAlert, 2, 0))
    7793    return 1;
    7894
     
    86102  char api0[strlen(tmpm1->value)+strlen(ntmp)+15];
    87103  sprintf(api0,"%s/%sZOO-proj4js.js",ntmp,tmpm1->value);
     104#ifdef JS_DEBUG
    88105  fprintf(stderr,"Trying to load %s\n",api0);
    89   JSScript *api_script1=loadZooApiFile(cx,global,api0);
     106#endif
     107  JSObject *api_script1=loadZooApiFile(cx,global,api0);
    90108  fflush(stderr);
    91109
    92110  char api1[strlen(tmpm1->value)+strlen(ntmp)+11];
    93111  sprintf(api1,"%s/%sZOO-api.js",ntmp,tmpm1->value);
     112#ifdef JS_DEBUG
    94113  fprintf(stderr,"Trying to load %s\n",api1);
    95   JSScript *api_script2=loadZooApiFile(cx,global,api1);
     114#endif
     115  JSObject *api_script2=loadZooApiFile(cx,global,api1);
    96116  fflush(stderr);
    97117
     
    106126  sprintf(filename,"%s/%s%s",ntmp,tmpm1->value,tmpm2->value);
    107127  filename[strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+1]=0;
     128#ifdef JS_DEBUG
    108129  fprintf(stderr,"FILENAME %s\n",filename);
     130#endif
    109131  struct stat file_status;
    110132  stat(filename, &file_status);
     
    112134  uint16 lineno;
    113135  jsval rval;
    114   FILE *jsfile=fopen(filename,"r");
    115136  JSBool ok ;
    116   JSScript *script = JS_CompileFileHandle(cx, global, filename,jsfile);
     137  JSObject *script = JS_CompileFile(cx, global, filename);
    117138  if(script!=NULL){
    118139    (void)JS_ExecuteScript(cx, global, script, &rval);
     
    127148    JS_DestroyRuntime(rt);
    128149    JS_ShutDown();
    129     fclose(jsfile);
    130150    exit(-1);
    131151  }
     
    161181    if(strlen(dbg)==0)
    162182      sprintf(dbg,"No result was found after the function call");
    163     sprintf(tmp1,"Unable to run %s from the JavScript file %s : \n %s",s->name,filename,dbg);
     183    sprintf(tmp1,"Unable to run %s from the JavaScript file %s : \n %s",s->name,filename,dbg);
     184#ifdef JS_DEBUG
    164185    fprintf(stderr,"%s",tmp1);
     186#endif
    165187    map* err=createMap("text",tmp1);
    166188    addToMap(err,"code","NoApplicableCode");
     
    211233    jsval tmp2;
    212234    JSBool hasElement=JS_GetProperty(cx,d,"outputs",&tmp2);
     235#ifdef JS_DEBUG
    213236    if(!hasElement)
    214237      fprintf(stderr,"No outputs property returned\n");
     
    217240    else
    218241      fprintf(stderr,"outputs is not an array as expected\n");
     242#endif
    219243    *outputs=mapsFromJSObject(cx,tmp2);
    220244#ifdef JS_DEBUG
    221     dumpMaps(out);
     245    dumpMaps(outputs);
    222246#endif
    223247  }
    224248
    225249  /* Cleanup. */
    226   JS_DestroyScript(cx, script);
    227   JS_DestroyScript(cx, api_script1);
    228   JS_DestroyScript(cx, api_script2);
    229   //JS_MaybeGC(cx);
    230   // If we use the DestroyContext as requested to release memory then we get
    231   // issue getting back the main configuration maps after coming back to the
    232   // runRequest function ...
    233   //JS_DestroyContext(cx);
     250  JS_DestroyContext(cx);
    234251  JS_DestroyRuntime(rt);
    235252  JS_ShutDown();
     
    240257}
    241258
    242 JSScript * loadZooApiFile(JSContext *cx,JSObject  *global, char* filename){
     259JSObject * loadZooApiFile(JSContext *cx,JSObject  *global, char* filename){
    243260  struct stat api_status;
    244261  int s=stat(filename, &api_status);
    245262  if(s==0){
    246263    jsval rval;
    247     FILE *jsfile=fopen(filename,"r");
    248264    JSBool ok ;
    249     JSScript *script = JS_CompileFileHandle(cx, global, filename,jsfile);
     265    JSObject *script = JS_CompileFile(cx, JS_GetGlobalObject(cx), filename);
    250266    if(script!=NULL){
    251       (void)JS_ExecuteScript(cx, global, script, &rval);
     267      (void)JS_ExecuteScript(cx, JS_GetGlobalObject(cx), script, &rval);
     268#ifdef JS_DEBUG
    252269      fprintf(stderr,"**************\n%s correctly loaded\n**************\n",filename);
     270#endif
    253271      return script;
    254272    }
     273#ifdef JS_DEBUG
    255274    else
    256275      fprintf(stderr,"\n**************\nUnable to run %s\n**************\n",filename);
    257   }
     276#endif
     277  }
     278#ifdef JS_DEBUG
    258279  else
    259280    fprintf(stderr,"\n**************\nUnable to load %s\n**************\n",filename);
     281#endif
    260282  return NULL;
    261283}
     
    288310  map* tmpm=t;
    289311  while(tmpm!=NULL){
    290     jsval jsstr = STRING_TO_JSVAL(JS_NewString(cx,tmpm->value,strlen(tmpm->value)));
     312    jsval jsstr = STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpm->value,strlen(tmpm->value)));
    291313    JS_SetProperty(cx, res, tmpm->name,&jsstr);
    292314#ifdef JS_DEBUG
     
    335357      fprintf(stderr,"Properties length :  %d \n",idp->length);
    336358#endif
     359      tres=(maps*)malloc(MAPS_SIZE);
     360      tres->name=NULL;
     361      tres->content=NULL;
     362      tres->next=NULL;
     363
    337364      for (index=0,argNum=idp->length;index<argNum;index++) {
    338365        jsval id = idp->vector[index];
     
    346373        len1 = JS_GetStringLength(jsmsg);
    347374#ifdef JS_DEBUG
    348         fprintf(stderr,"Enumerate id : %d => %s\n",oi,JS_GetStringBytes(jsmsg));
     375        fprintf(stderr,"Enumerate id : %d => %s\n",oi,JS_EncodeString(cx,jsmsg));
    349376#endif
    350377        jsval nvp=JSVAL_NULL;
    351         if((JS_GetProperty(cx, JSVAL_TO_OBJECT(tmp1), JS_GetStringBytes(jsmsg), &nvp)==JS_FALSE))
    352 #ifdef JS_DEBUG
    353         fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,JS_GetStringBytes(jsmsg));
    354 #endif
     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       
    355384        if(JSVAL_IS_OBJECT(nvp)){
    356385#ifdef JS_DEBUG
     
    358387#endif
    359388        }
    360 #ifdef JS_DEBUG
    361         else
    362           fprintf(stderr,"JSVAL NVP IS NOT OBJECT !!\n");
    363 #endif
    364         JSObject *nvp1;
     389
     390        JSObject *nvp1=JSVAL_NULL;
    365391        JS_ValueToObject(cx,nvp,&nvp1);
    366392        jsval nvp1j=OBJECT_TO_JSVAL(nvp1);
    367393        if(JSVAL_IS_OBJECT(nvp1j)){
    368 #ifdef JS_DEBUG
    369           fprintf(stderr,"JSVAL NVP1J IS OBJECT\n");
    370 #endif
    371           tres=(maps*)malloc(MAPS_SIZE);
    372           tres->name=strdup(JS_GetStringBytes(jsmsg));
    373           tres->content=mapFromJSObject(cx,nvp1j);
    374           tres->next=NULL;
    375 #ifdef JS_DEBUG
    376           dumpMaps(res);
    377 #endif
    378           if(res==NULL)
    379             res=dupMaps(&tres);
     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          }
    380405          else
    381             addMapsToMaps(&res,tres);
    382           freeMaps(&tres);
    383           free(tres);
    384           tres=NULL;
    385 #ifdef JS_DEBUG
    386           dumpMaps(res);
    387 #endif
     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            }
    388415        }
    389416#ifdef JS_DEBUG
     
    391418          fprintf(stderr,"JSVAL NVP1J IS NOT OBJECT !!\n");
    392419#endif
     420
    393421      }
     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
    394433    }
    395434  }
     
    423462      len = JS_GetStringLength(jsmsg);
    424463      jsval nvp;
    425       JS_GetProperty(cx, JSVAL_TO_OBJECT(t), JS_GetStringBytes(jsmsg), &nvp);
     464      JS_GetProperty(cx, JSVAL_TO_OBJECT(t), JS_EncodeString(cx,jsmsg), &nvp);
    426465      jsmsg1 = JS_ValueToString(cx,nvp);
    427466      len1 = JS_GetStringLength(jsmsg1);
    428467#ifdef JS_DEBUG
    429       fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1));
     468      fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
    430469#endif
    431470      if(res!=NULL){
    432471#ifdef JS_DEBUG
    433         fprintf(stderr,"%s - %s\n",JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1));
    434 #endif
    435         addToMap(res,JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1));
     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));
    436475      }
    437476      else{
    438         res=createMap(JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1));
     477        res=createMap(JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
    439478        res->next=NULL;
    440479      }
Note: See TracChangeset for help on using the changeset viewer.

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