Ignore:
Timestamp:
Nov 5, 2014, 4:58:11 PM (10 years ago)
Author:
david
Message:
  • remove metapath parameter for python and js support
  • use conf in memory for C,python and js services
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_js.c

    r505 r531  
    3232
    3333JSBool
    34 JSAlert(JSContext *cx, uintN argc, jsval *argv1)
    35 {
    36   jsval *argv = JS_ARGV(cx,argv1);
    37   int i=0;
    38   JS_MaybeGC(cx);
    39   for(i=0;i<argc;i++){
    40     JSString* jsmsg = JS_ValueToString(cx,argv[i]);
    41     char *tmp=JS_EncodeString(cx,jsmsg);
    42     fprintf(stderr,"[ZOO-API:JS] %s\n",tmp);
    43     free(tmp);
    44   }
    45   JS_MaybeGC(cx);
    46  
     34JSAlert (JSContext * cx, uintN argc, jsval * argv1)
     35{
     36  jsval *argv = JS_ARGV (cx, argv1);
     37  int i = 0;
     38  JS_MaybeGC (cx);
     39  for (i = 0; i < argc; i++)
     40    {
     41      JSString *jsmsg = JS_ValueToString (cx, argv[i]);
     42      char *tmp = JS_EncodeString (cx, jsmsg);
     43      fprintf (stderr, "[ZOO-API:JS] %s\n", tmp);
     44      free (tmp);
     45    }
     46  JS_MaybeGC (cx);
     47
    4748  return JS_TRUE;
    4849}
    4950
    50 JSBool 
    51 JSLoadScripts(JSContext *cx, uintN argc, jsval *argv1)
     51JSBool
     52JSLoadScripts (JSContext * cx, uintN argc, jsval * argv1)
    5253{
    5354  //map* request = JS_GetContextPrivate(cx);
    5455  //map* tmpm1=getMap(request,"metapath");
    55   JS_MaybeGC(cx);
     56  JS_MaybeGC (cx);
    5657
    5758  char ntmp[1024];
    58   getcwd(ntmp,1024);
    59 
    60   jsval *argv = JS_ARGV(cx,argv1);
    61   int i=0;
    62   JS_MaybeGC(cx);
    63   for(i=0;i<argc;i++){
    64     JSString* jsmsg = JS_ValueToString(cx,argv[i]);
    65     char *filename = JSValToChar(cx,&argv[i]);
    66     char *api0=(char*)malloc((strlen(ntmp)+strlen(filename)+2)*sizeof(char));
    67     sprintf(api0,"%s/%s",ntmp,filename);
    68 #ifdef JS_DEBUG
    69     fprintf(stderr,"Trying to load %s\n",api0);
    70     fflush(stderr);
    71 #endif
    72     JSObject *api_script1=loadZooApiFile(cx,JS_GetGlobalObject(cx),api0);
    73     free(api0);
    74   }
    75   JS_MaybeGC(cx);
    76   JS_SET_RVAL(cx, argv1, JSVAL_VOID);
    77  
     59  getcwd (ntmp, 1024);
     60
     61  jsval *argv = JS_ARGV (cx, argv1);
     62  int i = 0;
     63  JS_MaybeGC (cx);
     64  for (i = 0; i < argc; i++)
     65    {
     66      JSString *jsmsg = JS_ValueToString (cx, argv[i]);
     67      char *filename = JSValToChar (cx, &argv[i]);
     68      char *api0 =
     69        (char *) malloc ((strlen (ntmp) + strlen (filename) + 2) *
     70                         sizeof (char));
     71      sprintf (api0, "%s/%s", ntmp, filename);
     72#ifdef JS_DEBUG
     73      fprintf (stderr, "Trying to load %s\n", api0);
     74      fflush (stderr);
     75#endif
     76      JSObject *api_script1 =
     77        loadZooApiFile (cx, JS_GetGlobalObject (cx), api0);
     78      free (api0);
     79    }
     80  JS_MaybeGC (cx);
     81  JS_SET_RVAL (cx, argv1, JSVAL_VOID);
     82
    7883  return JS_TRUE;
    7984}
    8085
    8186
    82 int zoo_js_support(maps** main_conf,map* request,service* s,
    83                    maps **inputs,maps **outputs)
    84 {
    85   maps* main=*main_conf;
    86   maps* _inputs=*inputs;
    87   maps* _outputs=*outputs;
     87int
     88zoo_js_support (maps ** main_conf, map * request, service * s,
     89                maps ** inputs, maps ** outputs)
     90{
     91  maps *main = *main_conf;
     92  maps *_inputs = *inputs;
     93  maps *_outputs = *outputs;
    8894
    8995  /* The class of the global object. */
    90   JSClass global_class= {
     96  JSClass global_class = {
    9197    "global", JSCLASS_GLOBAL_FLAGS,
    9298    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
     
    98104  JSRuntime *rt;
    99105  JSContext *cx;
    100   JSObject  *global;
     106  JSObject *global;
    101107
    102108  /* Create a JS runtime. */
    103   rt = JS_NewRuntime(8L * 1024L * 1024L);
     109  rt = JS_NewRuntime (8L * 1024L * 1024L);
    104110  if (rt == NULL)
    105111    return 1;
    106  
     112
    107113  /* Create a context. */
    108   cx = JS_NewContext(rt,8192);
    109   if (cx == NULL){
    110     return 1;
    111   }
    112   JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
    113   JS_SetVersion(cx, JSVERSION_LATEST);
    114   JS_SetErrorReporter(cx, reportError);
     114  cx = JS_NewContext (rt, 8192);
     115  if (cx == NULL)
     116    {
     117      return 1;
     118    }
     119  JS_SetOptions (cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
     120  JS_SetVersion (cx, JSVERSION_LATEST);
     121  JS_SetErrorReporter (cx, reportError);
    115122
    116123  /* Create the global object. */
    117   global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
     124  global = JS_NewCompartmentAndGlobalObject (cx, &global_class, NULL);
    118125
    119126  /* Populate the global object with the standard globals,
    120127     like Object and Array. */
    121   if (!JS_InitStandardClasses(cx, global)){
    122     return 1;
    123   }
     128  if (!JS_InitStandardClasses (cx, global))
     129    {
     130      return 1;
     131    }
    124132
    125133  /* Define specific function and global variable to share with JS runtime
    126134   */
    127   jsval tmp=INT_TO_JSVAL(3);
    128   if (!JS_SetProperty(cx, global, "SERVICE_SUCCEEDED", &tmp))
    129     return 1;
    130   tmp=INT_TO_JSVAL(4);
    131   if (!JS_SetProperty(cx, global, "SERVICE_FAILED", &tmp))
    132     return 1;
    133   if (!JS_DefineFunction(cx, global, "ZOORequest", JSRequest, 4, 0))
    134     return 1;
    135   if (!JS_DefineFunction(cx, global, "ZOOTranslate", JSTranslate, 4, 0))
    136     return 1;
    137   if (!JS_DefineFunction(cx, global, "ZOOUpdateStatus", JSUpdateStatus, 2, 0))
    138     return 1;
    139   if (!JS_DefineFunction(cx, global, "alert", JSAlert, 2, 0))
    140     return 1; 
    141   if (!JS_DefineFunction(cx, global, "importScripts", JSLoadScripts, 1, 0))
    142     return 1;
     135  jsval tmp = INT_TO_JSVAL (3);
     136  if (!JS_SetProperty (cx, global, "SERVICE_SUCCEEDED", &tmp))
     137      return 1;
     138  tmp = INT_TO_JSVAL (4);
     139  if (!JS_SetProperty (cx, global, "SERVICE_FAILED", &tmp))
     140      return 1;
     141  if (!JS_DefineFunction (cx, global, "ZOORequest", JSRequest, 4, 0))
     142      return 1;
     143  if (!JS_DefineFunction (cx, global, "ZOOTranslate", JSTranslate, 4, 0))
     144      return 1;
     145  if (!JS_DefineFunction
     146      (cx, global, "ZOOUpdateStatus", JSUpdateStatus, 2, 0))
     147      return 1;
     148  if (!JS_DefineFunction (cx, global, "alert", JSAlert, 2, 0))
     149      return 1;
     150  if (!JS_DefineFunction (cx, global, "importScripts", JSLoadScripts, 1, 0))
     151      return 1;
    143152
    144153  /**
    145154   * Add private context object
    146155   */
    147   void* cxPrivate = request;
    148   JS_SetContextPrivate(cx,cxPrivate);
    149 
    150   map* tmpm1=getMap(request,"metapath");
     156  void *cxPrivate = request;
     157  JS_SetContextPrivate (cx, cxPrivate);
     158
    151159  char ntmp[1024];
    152   getcwd(ntmp,1024);
     160  getcwd (ntmp, 1024);
    153161
    154162  /**
    155163   * Load the first part of the ZOO-API
    156164   */
    157   char *api0=(char*)malloc((strlen(ntmp)+17)*sizeof(char));
    158   sprintf(api0,"%s/ZOO-proj4js.js",ntmp);
    159 #ifdef JS_DEBUG
    160   fprintf(stderr,"Trying to load %s\n",api0);
    161 #endif
    162   JSObject *api_script1=loadZooApiFile(cx,global,api0);
    163   free(api0);
    164   fflush(stderr);
    165 
    166   char *api1=(char*)malloc((strlen(ntmp)+13)*sizeof(char));
    167   sprintf(api1,"%s/ZOO-api.js",ntmp);
    168 #ifdef JS_DEBUG
    169   fprintf(stderr,"Trying to load %s\n",api1);
    170 #endif
    171   JSObject *api_script2=loadZooApiFile(cx,global,api1);
    172   free(api1);
    173   fflush(stderr);
     165  char *api0 = (char *) malloc ((strlen (ntmp) + 17) * sizeof (char));
     166  sprintf (api0, "%s/ZOO-proj4js.js", ntmp);
     167#ifdef JS_DEBUG
     168  fprintf (stderr, "Trying to load %s\n", api0);
     169#endif
     170  JSObject *api_script1 = loadZooApiFile (cx, global, api0);
     171  free (api0);
     172  fflush (stderr);
     173
     174  char *api1 = (char *) malloc ((strlen (ntmp) + 13) * sizeof (char));
     175  sprintf (api1, "%s/ZOO-api.js", ntmp);
     176#ifdef JS_DEBUG
     177  fprintf (stderr, "Trying to load %s\n", api1);
     178#endif
     179  JSObject *api_script2 = loadZooApiFile (cx, global, api1);
     180  free (api1);
     181  fflush (stderr);
    174182
    175183  /* Your application code here. This may include JSAPI calls
    176184     to create your own custom JS objects and run scripts. */
    177   maps* out=*outputs;
    178   int res=SERVICE_FAILED;
    179   maps* mc=*main_conf;
    180   map* tmpm2=getMap(s->content,"serviceProvider");
    181 
    182   char *filename=(char*)malloc(strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+3);
    183   sprintf(filename,"%s/%s/%s",ntmp,tmpm1->value,tmpm2->value);
    184   filename[strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+2]=0;
    185 #ifdef JS_DEBUG
    186   fprintf(stderr,"FILENAME %s\n",filename);
     185  maps *out = *outputs;
     186  int res = SERVICE_FAILED;
     187  maps *mc = *main_conf;
     188  map *tmpm2 = getMap (s->content, "serviceProvider");
     189  char *tmp_path = (char *) malloc ((strlen (s->zcfg) + 1) * sizeof (char *));
     190  sprintf (tmp_path, "%s", s->zcfg);
     191  char *dir = dirname (tmp_path);
     192  char *filename =
     193    (char *) malloc ((strlen (dir) + strlen (tmpm2->value) + 2) *
     194                     sizeof (char *));
     195  sprintf (filename, "%s/%s", dir, tmpm2->value);
     196  free (tmp_path);
     197#ifdef JS_DEBUG
     198  fprintf (stderr, "FILENAME %s\n", filename);
    187199#endif
    188200  struct stat file_status;
    189   stat(filename, &file_status);
     201  stat (filename, &file_status);
    190202  //char *source=(char*)malloc(file_status.st_size);
    191203  uint16 lineno;
    192204  jsval rval;
    193   JSBool ok ;
    194   JSObject *script = JS_CompileFile(cx, global, filename);
    195   if(script!=NULL){
    196     (void)JS_ExecuteScript(cx, global, script, &rval);
    197   }
    198   else{
    199     char tmp1[1024];
    200     sprintf(tmp1,"Unable to load JavaScript file %s",filename);
    201     free(filename);
    202     map* err=createMap("text",tmp1);
    203     addToMap(err,"code","NoApplicableCode");
    204     printExceptionReportResponse(mc,err);
    205     freeMap(&err);
    206     free(err);
    207     JS_MaybeGC(cx);
    208     JS_DestroyContext(cx);
    209     JS_DestroyRuntime(rt);
    210     JS_ShutDown();
    211     return -1;
    212   }
    213  
     205  JSBool ok;
     206  JSObject *script = JS_CompileFile (cx, global, filename);
     207  if (script != NULL)
     208    {
     209      (void) JS_ExecuteScript (cx, global, script, &rval);
     210    }
     211  else
     212    {
     213      char tmp1[1024];
     214      sprintf (tmp1, "Unable to load JavaScript file %s", filename);
     215      free (filename);
     216      map *err = createMap ("text", tmp1);
     217      addToMap (err, "code", "NoApplicableCode");
     218      printExceptionReportResponse (mc, err);
     219      freeMap (&err);
     220      free (err);
     221      JS_MaybeGC (cx);
     222      JS_DestroyContext (cx);
     223      JS_DestroyRuntime (rt);
     224      JS_ShutDown ();
     225      return -1;
     226    }
     227
    214228
    215229  /* Call a function in obj's scope. */
    216230  jsval argv[3];
    217   JSObject *jsargv1=JSObject_FromMaps(cx,*main_conf);
    218   argv[0] = OBJECT_TO_JSVAL(jsargv1);
    219   JSObject *jsargv2=JSObject_FromMaps(cx,*inputs);
    220   argv[1] = OBJECT_TO_JSVAL(jsargv2);
    221   JSObject *jsargv3=JSObject_FromMaps(cx,*outputs);
    222   argv[2] = OBJECT_TO_JSVAL(jsargv3);
    223   jsval rval1=JSVAL_NULL;
    224 #ifdef JS_DEBUG
    225   fprintf(stderr, "object %p\n", (void *) argv[2]);
    226 #endif
    227 
    228   ok = JS_CallFunctionName(cx, global, s->name, 3, argv, &rval1);
    229 
    230 #ifdef JS_DEBUG
    231   fprintf(stderr, "object %p\n", (void *) argv[2]);
     231  JSObject *jsargv1 = JSObject_FromMaps (cx, *main_conf);
     232  argv[0] = OBJECT_TO_JSVAL (jsargv1);
     233  JSObject *jsargv2 = JSObject_FromMaps (cx, *inputs);
     234  argv[1] = OBJECT_TO_JSVAL (jsargv2);
     235  JSObject *jsargv3 = JSObject_FromMaps (cx, *outputs);
     236  argv[2] = OBJECT_TO_JSVAL (jsargv3);
     237  jsval rval1 = JSVAL_NULL;
     238#ifdef JS_DEBUG
     239  fprintf (stderr, "object %p\n", (void *) argv[2]);
     240#endif
     241
     242  ok = JS_CallFunctionName (cx, global, s->name, 3, argv, &rval1);
     243
     244#ifdef JS_DEBUG
     245  fprintf (stderr, "object %p\n", (void *) argv[2]);
    232246#endif
    233247
    234248  JSObject *d;
    235   if (ok==JS_TRUE && JSVAL_IS_OBJECT(rval1)==JS_TRUE) {
    236 #ifdef JS_DEBUG
    237     fprintf(stderr,"Function run sucessfully !\n");
    238 #endif
    239     /* Should get a number back from the service function call. */
    240     ok = JS_ValueToObject(cx, rval1, &d);
    241   }else{
    242     /* Unable to run JS function */
    243     char tmp1[1024];
    244     if(strlen(dbg)==0)
    245       sprintf(dbg,"No result was found after the function call");
    246     sprintf(tmp1,"Unable to run %s from the JavaScript file %s : \n %s",s->name,filename,dbg);
    247 #ifdef JS_DEBUG
    248     fprintf(stderr,"%s",tmp1);
    249 #endif
    250     map* err=createMap("text",tmp1);
    251     addToMap(err,"code","NoApplicableCode");
    252     printExceptionReportResponse(*main_conf,err);
    253     freeMap(&err);
    254     free(err);
    255     free(filename);
    256     JS_MaybeGC(cx);
    257     JS_DestroyContext(cx);
    258     JS_DestroyRuntime(rt);
    259     JS_ShutDown();
    260     // Should return -1 here but the unallocation won't work from zoo_service_loader.c line 1847
    261     return -1;
    262   }
    263 
    264   jsval t=OBJECT_TO_JSVAL(d);
    265   if(JS_IsArrayObject(cx,d)){
    266 #ifdef JS_DEBUG
    267     fprintf(stderr,"An array was returned !\n");
    268 #endif
    269     jsuint       len;
    270     if((JS_GetArrayLength(cx, d, &len)==JS_FALSE)){
    271 #ifdef JS_DEBUG
    272       fprintf(stderr,"outputs array is empty\n");
    273 #endif
    274     }
    275     jsval tmp1;
    276     JSBool hasResult=JS_GetElement(cx,d,0,&tmp1);
    277     res=JSVAL_TO_INT(tmp1);
    278 #ifdef JS_DEBUG
    279     fprintf(stderr," * %d * \n",res);
    280 #endif
    281     if(res==SERVICE_SUCCEEDED){
    282       jsval tmp2;
    283       JSBool hasElement=JS_GetElement(cx,d,1,&tmp2);
    284       if(hasElement==JS_TRUE){
    285         freeMaps(outputs);
    286         free(*outputs);
    287         *outputs=mapsFromJSObject(cx,tmp2);
    288       }
    289     }else{
    290       jsval tmp3;
    291       JSBool hasConf=JS_GetElement(cx,d,1,&tmp3);
    292       if(hasConf==JS_TRUE){
    293         freeMaps(main_conf);
    294         free(*main_conf);
    295         *main_conf=mapsFromJSObject(cx,tmp3);
    296       }
    297     }
    298 
    299   }
    300   else{
    301 #ifdef JS_DEBUG
    302     fprintf(stderr,"The service didn't return an array !\n");
     249  if (ok == JS_TRUE && JSVAL_IS_OBJECT (rval1) == JS_TRUE)
     250    {
     251#ifdef JS_DEBUG
     252      fprintf (stderr, "Function run sucessfully !\n");
     253#endif
     254      /* Should get a number back from the service function call. */
     255      ok = JS_ValueToObject (cx, rval1, &d);
     256    }
     257  else
     258    {
     259      /* Unable to run JS function */
     260      char tmp1[1024];
     261      if (strlen (dbg) == 0)
     262        sprintf (dbg, "No result was found after the function call");
     263      sprintf (tmp1, "Unable to run %s from the JavaScript file %s : \n %s",
     264               s->name, filename, dbg);
     265#ifdef JS_DEBUG
     266      fprintf (stderr, "%s", tmp1);
     267#endif
     268      map *err = createMap ("text", tmp1);
     269      addToMap (err, "code", "NoApplicableCode");
     270      printExceptionReportResponse (*main_conf, err);
     271      freeMap (&err);
     272      free (err);
     273      free (filename);
     274      JS_MaybeGC (cx);
     275      JS_DestroyContext (cx);
     276      JS_DestroyRuntime (rt);
     277      JS_ShutDown ();
     278      // Should return -1 here but the unallocation won't work from zoo_service_loader.c line 1847
     279      return -1;
     280    }
     281
     282  jsval t = OBJECT_TO_JSVAL (d);
     283  if (JS_IsArrayObject (cx, d))
     284    {
     285#ifdef JS_DEBUG
     286      fprintf (stderr, "An array was returned !\n");
     287#endif
     288      jsuint len;
     289      if ((JS_GetArrayLength (cx, d, &len) == JS_FALSE))
     290        {
     291#ifdef JS_DEBUG
     292          fprintf (stderr, "outputs array is empty\n");
     293#endif
     294        }
     295      jsval tmp1;
     296      JSBool hasResult = JS_GetElement (cx, d, 0, &tmp1);
     297      res = JSVAL_TO_INT (tmp1);
     298#ifdef JS_DEBUG
     299      fprintf (stderr, " * %d * \n", res);
     300#endif
     301      if (res == SERVICE_SUCCEEDED)
     302        {
     303          jsval tmp2;
     304          JSBool hasElement = JS_GetElement (cx, d, 1, &tmp2);
     305          if (hasElement == JS_TRUE)
     306            {
     307              freeMaps (outputs);
     308              free (*outputs);
     309              *outputs = mapsFromJSObject (cx, tmp2);
     310            }
     311        }
     312      else
     313        {
     314          jsval tmp3;
     315          JSBool hasConf = JS_GetElement (cx, d, 1, &tmp3);
     316          if (hasConf == JS_TRUE)
     317            {
     318              freeMaps (main_conf);
     319              free (*main_conf);
     320              *main_conf = mapsFromJSObject (cx, tmp3);
     321            }
     322        }
     323
     324    }
     325  else
     326    {
     327#ifdef JS_DEBUG
     328      fprintf (stderr, "The service didn't return an array !\n");
    303329#endif
    304330    /**
    305331     * Extract result
    306332     */
    307     jsval tmp1;
    308     JSBool hasResult=JS_GetProperty(cx,d,"result",&tmp1);
    309     res=JSVAL_TO_INT(tmp1);
    310 
    311 #ifdef JS_DEBUG
    312     fprintf(stderr," * %d * \n",res);
     333      jsval tmp1;
     334      JSBool hasResult = JS_GetProperty (cx, d, "result", &tmp1);
     335      res = JSVAL_TO_INT (tmp1);
     336
     337#ifdef JS_DEBUG
     338      fprintf (stderr, " * %d * \n", res);
    313339#endif
    314340    /**
    315341     * Extract outputs when available.
    316342     */
    317     jsval tmp2;
    318     JSBool hasElement=JS_GetProperty(cx,d,"outputs",&tmp2);
    319     if(!JSVAL_IS_VOID(tmp2) && hasElement==JS_TRUE){
    320       freeMaps(outputs);
    321       free(*outputs);   
    322       *outputs=mapsFromJSObject(cx,tmp2);
    323     }
    324     JS_MaybeGC(cx);
    325 #ifdef JS_DEBUG
    326     if(JSVAL_IS_VOID(tmp2))
    327       fprintf(stderr,"No outputs property returned\n");
    328     else{
    329       if(JS_IsArrayObject(cx,JSVAL_TO_OBJECT(tmp2)))
    330         fprintf(stderr,"outputs is an array as expected\n");
     343      jsval tmp2;
     344      JSBool hasElement = JS_GetProperty (cx, d, "outputs", &tmp2);
     345      if (!JSVAL_IS_VOID (tmp2) && hasElement == JS_TRUE)
     346        {
     347          freeMaps (outputs);
     348          free (*outputs);
     349          *outputs = mapsFromJSObject (cx, tmp2);
     350        }
     351      JS_MaybeGC (cx);
     352#ifdef JS_DEBUG
     353      if (JSVAL_IS_VOID (tmp2))
     354        fprintf (stderr, "No outputs property returned\n");
    331355      else
    332         fprintf(stderr,"outputs is not an array as expected\n");
    333     }
    334     JS_MaybeGC(cx);
     356        {
     357          if (JS_IsArrayObject (cx, JSVAL_TO_OBJECT (tmp2)))
     358            fprintf (stderr, "outputs is an array as expected\n");
     359          else
     360            fprintf (stderr, "outputs is not an array as expected\n");
     361        }
     362      JS_MaybeGC (cx);
    335363#endif
    336364
     
    338366     * Extract conf when available.
    339367     */
    340     jsval tmp3;
    341     JSBool hasConf=JS_GetProperty(cx,d,"conf",&tmp3);
    342     if(!JSVAL_IS_VOID(tmp3) && hasConf==JS_TRUE){
    343       freeMaps(main_conf);
    344       free(*main_conf);
    345       *main_conf=mapsFromJSObject(cx,tmp3);
    346     }
    347     JS_MaybeGC(cx);
    348 
    349 #ifdef JS_DEBUG
    350     dumpMaps(*outputs);
    351 #endif
    352   }
     368      jsval tmp3;
     369      JSBool hasConf = JS_GetProperty (cx, d, "conf", &tmp3);
     370      if (!JSVAL_IS_VOID (tmp3) && hasConf == JS_TRUE)
     371        {
     372          freeMaps (main_conf);
     373          free (*main_conf);
     374          *main_conf = mapsFromJSObject (cx, tmp3);
     375        }
     376      JS_MaybeGC (cx);
     377
     378#ifdef JS_DEBUG
     379      dumpMaps (*outputs);
     380#endif
     381    }
    353382  /* Cleanup. */
    354   JS_MaybeGC(cx);
    355   JS_DestroyContext(cx);
    356   JS_DestroyRuntime(rt);
    357   JS_ShutDown();
    358   free(filename);
    359 #ifdef JS_DEBUG
    360   fprintf(stderr,"Returned value %d\n",res);
     383  JS_MaybeGC (cx);
     384  JS_DestroyContext (cx);
     385  JS_DestroyRuntime (rt);
     386  JS_ShutDown ();
     387  free (filename);
     388#ifdef JS_DEBUG
     389  fprintf (stderr, "Returned value %d\n", res);
    361390#endif
    362391  return res;
    363392}
    364393
    365 JSObject * loadZooApiFile(JSContext *cx,JSObject  *global, char* filename){
     394JSObject *
     395loadZooApiFile (JSContext * cx, JSObject * global, char *filename)
     396{
    366397  struct stat api_status;
    367   int s=stat(filename, &api_status);
    368   if(s==0){
    369     jsval rval;
    370     JSBool ok ;
    371     JSObject *script = JS_CompileFile(cx, JS_GetGlobalObject(cx), filename);
    372     if(script!=NULL){
    373       (void)JS_ExecuteScript(cx, JS_GetGlobalObject(cx), script, &rval);
    374 #ifdef JS_DEBUG
    375       fprintf(stderr,"**************\n%s correctly loaded\n**************\n",filename);
    376 #endif
    377       return script;
    378     }
    379 #ifdef JS_DEBUG
    380     else
    381       fprintf(stderr,"\n**************\nUnable to run %s\n**************\n",filename);
    382 #endif
    383   }
     398  int s = stat (filename, &api_status);
     399  if (s == 0)
     400    {
     401      jsval rval;
     402      JSBool ok;
     403      JSObject *script =
     404        JS_CompileFile (cx, JS_GetGlobalObject (cx), filename);
     405      if (script != NULL)
     406        {
     407          (void) JS_ExecuteScript (cx, JS_GetGlobalObject (cx), script,
     408                                   &rval);
     409#ifdef JS_DEBUG
     410          fprintf (stderr,
     411                   "**************\n%s correctly loaded\n**************\n",
     412                   filename);
     413#endif
     414          return script;
     415        }
     416#ifdef JS_DEBUG
     417      else
     418        fprintf (stderr,
     419                 "\n**************\nUnable to run %s\n**************\n",
     420                 filename);
     421#endif
     422    }
    384423#ifdef JS_DEBUG
    385424  else
    386     fprintf(stderr,"\n**************\nUnable to load %s\n**************\n",filename);
     425    fprintf (stderr, "\n**************\nUnable to load %s\n**************\n",
     426             filename);
    387427#endif
    388428  return NULL;
    389429}
    390430
    391 JSObject* JSObject_FromMaps(JSContext *cx,maps* t){
    392 
    393   JSObject* res=JS_NewObject(cx, NULL, NULL, NULL);
     431JSObject *
     432JSObject_FromMaps (JSContext * cx, maps * t)
     433{
     434
     435  JSObject *res = JS_NewObject (cx, NULL, NULL, NULL);
    394436  //JSObject *res = JS_NewArrayObject(cx, 0, NULL);
    395   if(res==NULL)
    396     fprintf(stderr,"Array Object is NULL!\n");
    397   maps* tmp=t;
    398   while(tmp!=NULL){
    399     jsuint len;
    400     JSObject* res1=JS_NewObject(cx, NULL, NULL, NULL);
    401     JSObject *pval=JSObject_FromMap(cx,tmp->content);
    402     jsval pvalj=OBJECT_TO_JSVAL(pval);
    403     JS_SetProperty(cx, res, tmp->name, &pvalj);
    404 #ifdef JS_DEBUG
    405     fprintf(stderr,"Length of the Array %d, element : %s added \n",len,tmp->name);
    406 #endif
    407     tmp=tmp->next;
    408   } 
     437  if (res == NULL)
     438    fprintf (stderr, "Array Object is NULL!\n");
     439  maps *tmp = t;
     440  while (tmp != NULL)
     441    {
     442      jsuint len;
     443      JSObject *res1 = JS_NewObject (cx, NULL, NULL, NULL);
     444      JSObject *pval = JSObject_FromMap (cx, tmp->content);
     445      jsval pvalj = OBJECT_TO_JSVAL (pval);
     446      JS_SetProperty (cx, res, tmp->name, &pvalj);
     447#ifdef JS_DEBUG
     448      fprintf (stderr, "Length of the Array %d, element : %s added \n", len,
     449               tmp->name);
     450#endif
     451      tmp = tmp->next;
     452    }
    409453  return res;
    410454}
    411455
    412 JSObject* JSObject_FromMap(JSContext *cx,map* t){
    413   JSObject* res=JS_NewObject(cx, NULL, NULL, NULL);
    414   jsval resf =  OBJECT_TO_JSVAL(res);
    415   map* tmpm=t;
    416   map* isArray=getMap(t,"isArray");
    417   map* isBinary=getMap(t,"size");
    418   map* tmap=getMapType(t);
    419 #ifdef JS_DEBUG
    420   if(tmap==NULL)
    421     fprintf(stderr,"tmap is null !\n");
     456JSObject *
     457JSObject_FromMap (JSContext * cx, map * t)
     458{
     459  JSObject *res = JS_NewObject (cx, NULL, NULL, NULL);
     460  jsval resf = OBJECT_TO_JSVAL (res);
     461  map *tmpm = t;
     462  map *isArray = getMap (t, "isArray");
     463  map *isBinary = getMap (t, "size");
     464  map *tmap = getMapType (t);
     465#ifdef JS_DEBUG
     466  if (tmap == NULL)
     467    fprintf (stderr, "tmap is null !\n");
    422468  else
    423     fprintf(stderr,"tmap is not null ! (%s = %s)\n",tmap->name,tmap->value);
    424 #endif
    425   while(tmpm!=NULL){
    426     jsval jsstr;
    427     if((isArray==NULL && isBinary!=NULL && strncasecmp(tmpm->name,"value",5)==0))
    428       jsstr = STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpm->value,atoi(isBinary->value)));
    429     else
    430       jsstr = STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpm->value,strlen(tmpm->value)));
    431     JS_SetProperty(cx, res, tmpm->name,&jsstr);
    432 #ifdef JS_DEBUG
    433     fprintf(stderr,"[JS] %s => %s\n",tmpm->name,tmpm->value);
    434 #endif
    435     tmpm=tmpm->next;
    436   }
    437   if(isArray!=NULL){
    438     map* len=getMap(t,"length");
    439     int cnt=atoi(len->value);
    440     JSObject* values=JS_NewArrayObject( cx, cnt, NULL );
    441     JSObject* mvalues=JS_NewArrayObject( cx, cnt, NULL );
    442     map *tmpm1,*tmpm2;
    443     int i=0;
    444     for(i=0;i<cnt;i++){
    445       tmpm1=getMapArray(t,"value",i);
    446       tmpm2=getMapArray(t,tmap->name,i);
    447       if(tmpm1!=NULL){
    448         jsval jsstr = STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpm1->value,strlen(tmpm1->value)));
    449         JS_SetElement( cx, values, i, &jsstr );
    450       }
    451       if(tmpm2!=NULL){
    452         jsval jsstr = STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpm2->value,strlen(tmpm2->value)));
    453         JS_SetElement( cx, mvalues, i, &jsstr );
    454       }
    455     }
    456     jsval jvalues=OBJECT_TO_JSVAL(values);
    457     jsval jmvalues=OBJECT_TO_JSVAL(mvalues);
    458     JS_SetProperty(cx, res,"value",&jvalues);
    459     JS_SetProperty(cx, res,tmap->name,&jmvalues);
    460   }
     469    fprintf (stderr, "tmap is not null ! (%s = %s)\n", tmap->name,
     470             tmap->value);
     471#endif
     472  while (tmpm != NULL)
     473    {
     474      jsval jsstr;
     475      if ((isArray == NULL && isBinary != NULL
     476           && strncasecmp (tmpm->name, "value", 5) == 0))
     477        jsstr =
     478          STRING_TO_JSVAL (JS_NewStringCopyN
     479                           (cx, tmpm->value, atoi (isBinary->value)));
     480      else
     481        jsstr =
     482          STRING_TO_JSVAL (JS_NewStringCopyN
     483                           (cx, tmpm->value, strlen (tmpm->value)));
     484      JS_SetProperty (cx, res, tmpm->name, &jsstr);
     485#ifdef JS_DEBUG
     486      fprintf (stderr, "[JS] %s => %s\n", tmpm->name, tmpm->value);
     487#endif
     488      tmpm = tmpm->next;
     489    }
     490  if (isArray != NULL)
     491    {
     492      map *len = getMap (t, "length");
     493      int cnt = atoi (len->value);
     494      JSObject *values = JS_NewArrayObject (cx, cnt, NULL);
     495      JSObject *mvalues = JS_NewArrayObject (cx, cnt, NULL);
     496      map *tmpm1, *tmpm2;
     497      int i = 0;
     498      for (i = 0; i < cnt; i++)
     499        {
     500          tmpm1 = getMapArray (t, "value", i);
     501          tmpm2 = getMapArray (t, tmap->name, i);
     502          if (tmpm1 != NULL)
     503            {
     504              jsval jsstr =
     505                STRING_TO_JSVAL (JS_NewStringCopyN
     506                                 (cx, tmpm1->value, strlen (tmpm1->value)));
     507              JS_SetElement (cx, values, i, &jsstr);
     508            }
     509          if (tmpm2 != NULL)
     510            {
     511              jsval jsstr =
     512                STRING_TO_JSVAL (JS_NewStringCopyN
     513                                 (cx, tmpm2->value, strlen (tmpm2->value)));
     514              JS_SetElement (cx, mvalues, i, &jsstr);
     515            }
     516        }
     517      jsval jvalues = OBJECT_TO_JSVAL (values);
     518      jsval jmvalues = OBJECT_TO_JSVAL (mvalues);
     519      JS_SetProperty (cx, res, "value", &jvalues);
     520      JS_SetProperty (cx, res, tmap->name, &jmvalues);
     521    }
    461522  return res;
    462523}
    463524
    464 maps* mapsFromJSObject(JSContext *cx,jsval t){
    465   maps *res=NULL;
    466   maps *tres=NULL;
    467   jsint oi=0;
    468   JSObject* tt=JSVAL_TO_OBJECT(t);
    469   if(JS_IsArrayObject(cx,tt)){
    470 #ifdef JS_DEBUG
    471     fprintf(stderr,"Is finally an array !\n");
    472 #endif
    473   }
    474   else{
    475 #ifdef JS_DEBUG
    476     fprintf(stderr,"Is not an array !\n");
    477 #endif
    478     JSIdArray *idp=JS_Enumerate(cx,tt);
    479     if(idp!=NULL) {
     525maps *
     526mapsFromJSObject (JSContext * cx, jsval t)
     527{
     528  maps *res = NULL;
     529  maps *tres = NULL;
     530  jsint oi = 0;
     531  JSObject *tt = JSVAL_TO_OBJECT (t);
     532  if (JS_IsArrayObject (cx, tt))
     533    {
     534#ifdef JS_DEBUG
     535      fprintf (stderr, "Is finally an array !\n");
     536#endif
     537    }
     538  else
     539    {
     540#ifdef JS_DEBUG
     541      fprintf (stderr, "Is not an array !\n");
     542#endif
     543      JSIdArray *idp = JS_Enumerate (cx, tt);
     544      if (idp != NULL)
     545        {
     546          int index;
     547          jsdouble argNum;
     548#ifdef JS_DEBUG
     549          fprintf (stderr, "Properties length :  %d \n", idp->length);
     550#endif
     551
     552          for (index = 0, argNum = idp->length; index < argNum; index++)
     553            {
     554              jsval id = idp->vector[index];
     555              jsval vp;
     556              JSString *str;
     557              JS_IdToValue (cx, id, &vp);
     558              char *c, *tmp;
     559              JSString *jsmsg;
     560              size_t len1;
     561              jsmsg = JS_ValueToString (cx, vp);
     562              len1 = JS_GetStringLength (jsmsg);
     563
     564              tmp = JS_EncodeString (cx, jsmsg);
     565              tres = (maps *) malloc (MAPS_SIZE);
     566              tres->name = zStrdup (tmp);
     567              tres->content = NULL;
     568              tres->next = NULL;
     569
     570              jsval nvp = JSVAL_NULL;
     571              if ((JS_GetProperty (cx, tt, tmp, &nvp) == JS_FALSE))
     572                {
     573#ifdef JS_DEBUG
     574                  fprintf (stderr,
     575                           "Enumerate id : %d => %s => No more value\n", oi,
     576                           tmp);
     577#endif
     578                }
     579              free (tmp);
     580              JSObject *nvp1 = JSVAL_TO_OBJECT (JSVAL_NULL);
     581              JS_ValueToObject (cx, nvp, &nvp1);
     582              jsval nvp1j = OBJECT_TO_JSVAL (nvp1);
     583              if (JSVAL_IS_OBJECT (nvp1j))
     584                {
     585                  tres->content = mapFromJSObject (cx, nvp1j);
     586                }
     587
     588              if (res == NULL)
     589                res = dupMaps (&tres);
     590              else
     591                addMapsToMaps (&res, tres);
     592              freeMaps (&tres);
     593              free (tres);
     594              tres = NULL;
     595
     596            }
     597          JS_DestroyIdArray (cx, idp);
     598        }
     599    }
     600
     601  jsuint len;
     602  JSBool hasLen = JS_GetArrayLength (cx, tt, &len);
     603#ifdef JS_DEBUG
     604  if (hasLen == JS_FALSE)
     605    {
     606      fprintf (stderr, "outputs array is empty\n");
     607    }
     608  fprintf (stderr, "outputs array length : %d\n", len);
     609#endif
     610  for (oi = 0; oi < len; oi++)
     611    {
     612#ifdef JS_DEBUG
     613      fprintf (stderr, "outputs array length : %d step %d \n", len, oi);
     614#endif
     615      jsval tmp1;
     616      JSBool hasElement = JS_GetElement (cx, tt, oi, &tmp1);
     617      JSObject *otmp1 = JSVAL_TO_OBJECT (tmp1);
     618      JSIdArray *idp = JS_Enumerate (cx, otmp1);
     619      if (idp != NULL)
     620        {
     621          int index;
     622          jsdouble argNum;
     623#ifdef JS_DEBUG
     624          fprintf (stderr, "Properties length :  %d \n", idp->length);
     625#endif
     626          tres = (maps *) malloc (MAPS_SIZE);
     627          tres->name = NULL;
     628          tres->content = NULL;
     629          tres->next = NULL;
     630
     631          for (index = 0, argNum = idp->length; index < argNum; index++)
     632            {
     633              jsval id = idp->vector[index];
     634              jsval vp;
     635              JSString *str;
     636              JS_IdToValue (cx, id, &vp);
     637              char *c, *tmp;
     638              JSString *jsmsg;
     639              size_t len1;
     640              jsmsg = JS_ValueToString (cx, vp);
     641              len1 = JS_GetStringLength (jsmsg);
     642              tmp = JS_EncodeString (cx, jsmsg);
     643#ifdef JS_DEBUG
     644              fprintf (stderr, "Enumerate id : %d => %s\n", oi, tmp);
     645#endif
     646              jsval nvp = JSVAL_NULL;
     647              if ((JS_GetProperty (cx, JSVAL_TO_OBJECT (tmp1), tmp, &nvp) ==
     648                   JS_FALSE))
     649                {
     650#ifdef JS_DEBUG
     651                  fprintf (stderr,
     652                           "Enumerate id : %d => %s => No more value\n", oi,
     653                           tmp);
     654#endif
     655                }
     656              free (tmp);
     657              if (JSVAL_IS_OBJECT (nvp))
     658                {
     659#ifdef JS_DEBUG
     660                  fprintf (stderr, "JSVAL NVP IS OBJECT\n");
     661#endif
     662                }
     663
     664              JSObject *nvp1 = JSVAL_TO_OBJECT (JSVAL_NULL);
     665              JS_ValueToObject (cx, nvp, &nvp1);
     666              jsval nvp1j = OBJECT_TO_JSVAL (nvp1);
     667              if (JSVAL_IS_OBJECT (nvp1j))
     668                {
     669                  JSString *jsmsg1;
     670                  char *tmp1, *tmp2;
     671                  JSObject *nvp2 = JSVAL_TO_OBJECT (JSVAL_NULL);
     672                  jsmsg1 = JS_ValueToString (cx, nvp1j);
     673                  len1 = JS_GetStringLength (jsmsg1);
     674                  tmp1 = JS_EncodeString (cx, jsmsg1);
     675                  tmp2 = JS_EncodeString (cx, jsmsg);
     676#ifdef JS_DEBUG
     677                  fprintf (stderr, "JSVAL NVP1J IS OBJECT %s = %s\n",
     678                           JS_EncodeString (cx, jsmsg), tmp1);
     679#endif
     680                  if (strcasecmp (tmp1, "[object Object]") == 0)
     681                    {
     682                      tres->name = zStrdup (tmp2);
     683                      tres->content = mapFromJSObject (cx, nvp1j);
     684                    }
     685                  else if (strcasecmp (tmp2, "name") == 0)
     686                    {
     687                      tres->name = zStrdup (tmp1);
     688                    }
     689                  else
     690                    {
     691                      if (tres->content == NULL)
     692                        tres->content = createMap (tmp2, tmp1);
     693                      else
     694                        addToMap (tres->content, tmp2, tmp1);
     695                    }
     696                  free (tmp1);
     697                  free (tmp2);
     698                }
     699#ifdef JS_DEBUG
     700              else
     701                fprintf (stderr, "JSVAL NVP1J IS NOT OBJECT !!\n");
     702#endif
     703            }
     704#ifdef JS_DEBUG
     705          dumpMaps (tres);
     706#endif
     707          if (res == NULL)
     708            res = dupMaps (&tres);
     709          else
     710            addMapsToMaps (&res, tres);
     711          freeMaps (&tres);
     712          free (tres);
     713          tres = NULL;
     714          JS_DestroyIdArray (cx, idp);
     715        }
     716    }
     717#ifdef JS_DEBUG
     718  dumpMaps (res);
     719#endif
     720  return res;
     721}
     722
     723map *
     724mapFromJSObject (JSContext * cx, jsval t)
     725{
     726  map *res = NULL;
     727  JSIdArray *idp = JS_Enumerate (cx, JSVAL_TO_OBJECT (t));
     728#ifdef JS_DEBUG
     729  fprintf (stderr, "Properties %p\n", (void *) t);
     730#endif
     731  if (idp != NULL)
     732    {
    480733      int index;
    481734      jsdouble argNum;
    482735#ifdef JS_DEBUG
    483       fprintf(stderr,"Properties length :  %d \n",idp->length);
    484 #endif
    485      
    486       for (index=0,argNum=idp->length;index<argNum;index++) {
    487         jsval id = idp->vector[index];
    488         jsval vp;
    489         JSString* str;
    490         JS_IdToValue(cx,id,&vp);
    491         char *c, *tmp;
    492         JSString *jsmsg;
    493         size_t len1;
    494         jsmsg = JS_ValueToString(cx,vp);
    495         len1 = JS_GetStringLength(jsmsg);
    496        
    497         tmp=JS_EncodeString(cx,jsmsg);
    498         tres=(maps*)malloc(MAPS_SIZE);
    499         tres->name=zStrdup(tmp);
    500         tres->content=NULL;
    501         tres->next=NULL;
    502 
    503         jsval nvp=JSVAL_NULL;
    504         if((JS_GetProperty(cx, tt, tmp, &nvp)==JS_FALSE)){
    505 #ifdef JS_DEBUG
    506           fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,tmp);
    507 #endif
    508         }
    509         free(tmp);
    510         JSObject *nvp1=JSVAL_TO_OBJECT(JSVAL_NULL);
    511         JS_ValueToObject(cx,nvp,&nvp1);
    512         jsval nvp1j=OBJECT_TO_JSVAL(nvp1);
    513         if(JSVAL_IS_OBJECT(nvp1j)){
    514           tres->content=mapFromJSObject(cx,nvp1j);
    515         }
    516 
    517         if(res==NULL)
    518           res=dupMaps(&tres);
    519         else
    520           addMapsToMaps(&res,tres);
    521         freeMaps(&tres);
    522         free(tres);
    523         tres=NULL;
    524                
    525       }
    526       JS_DestroyIdArray(cx,idp);
    527     }
    528   }
    529 
    530   jsuint len;
    531   JSBool hasLen=JS_GetArrayLength(cx, tt, &len);
    532 #ifdef JS_DEBUG
    533   if(hasLen==JS_FALSE){
    534     fprintf(stderr,"outputs array is empty\n");
    535   }
    536   fprintf(stderr,"outputs array length : %d\n",len);
    537 #endif
    538   for(oi=0;oi < len;oi++){
    539 #ifdef JS_DEBUG
    540     fprintf(stderr,"outputs array length : %d step %d \n",len,oi);
    541 #endif
    542     jsval tmp1;
    543     JSBool hasElement=JS_GetElement(cx,tt,oi,&tmp1);
    544     JSObject *otmp1=JSVAL_TO_OBJECT(tmp1);
    545     JSIdArray *idp=JS_Enumerate(cx,otmp1);
    546     if(idp!=NULL) {
    547       int index;
    548       jsdouble argNum;
    549 #ifdef JS_DEBUG
    550       fprintf(stderr,"Properties length :  %d \n",idp->length);
    551 #endif
    552       tres=(maps*)malloc(MAPS_SIZE);
    553       tres->name=NULL;
    554       tres->content=NULL;
    555       tres->next=NULL;
    556 
    557       for (index=0,argNum=idp->length;index<argNum;index++) {
    558         jsval id = idp->vector[index];
    559         jsval vp;
    560         JSString* str;
    561         JS_IdToValue(cx,id,&vp);
    562         char *c, *tmp;
    563         JSString *jsmsg;
    564         size_t len1;
    565         jsmsg = JS_ValueToString(cx,vp);
    566         len1 = JS_GetStringLength(jsmsg);
    567         tmp=JS_EncodeString(cx,jsmsg);
    568 #ifdef JS_DEBUG
    569         fprintf(stderr,"Enumerate id : %d => %s\n",oi,tmp);
    570 #endif
    571         jsval nvp=JSVAL_NULL;
    572         if((JS_GetProperty(cx, JSVAL_TO_OBJECT(tmp1), tmp, &nvp)==JS_FALSE)){
    573 #ifdef JS_DEBUG
    574           fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,tmp);
    575 #endif
    576         }
    577         free(tmp);
    578         if(JSVAL_IS_OBJECT(nvp)){
    579 #ifdef JS_DEBUG
    580           fprintf(stderr,"JSVAL NVP IS OBJECT\n");
    581 #endif
    582         }
    583 
    584         JSObject *nvp1=JSVAL_TO_OBJECT(JSVAL_NULL);
    585         JS_ValueToObject(cx,nvp,&nvp1);
    586         jsval nvp1j=OBJECT_TO_JSVAL(nvp1);
    587         if(JSVAL_IS_OBJECT(nvp1j)){
    588           JSString *jsmsg1;
    589           char *tmp1, *tmp2;
    590           JSObject *nvp2=JSVAL_TO_OBJECT(JSVAL_NULL);
    591           jsmsg1 = JS_ValueToString(cx,nvp1j);
    592           len1 = JS_GetStringLength(jsmsg1);
    593           tmp1=JS_EncodeString(cx,jsmsg1);
    594           tmp2=JS_EncodeString(cx,jsmsg);
    595 #ifdef JS_DEBUG
    596           fprintf(stderr,"JSVAL NVP1J IS OBJECT %s = %s\n",JS_EncodeString(cx,jsmsg),tmp1);
    597 #endif
    598           if(strcasecmp(tmp1,"[object Object]")==0){
    599             tres->name=zStrdup(tmp2);
    600             tres->content=mapFromJSObject(cx,nvp1j);
    601           }
    602           else
    603             if(strcasecmp(tmp2,"name")==0){
    604               tres->name=zStrdup(tmp1);
    605             }
    606             else{
    607               if(tres->content==NULL)
    608                 tres->content=createMap(tmp2,tmp1);
    609               else
    610                 addToMap(tres->content,tmp2,tmp1);
    611             }
    612           free(tmp1);
    613           free(tmp2);
    614         }
    615 #ifdef JS_DEBUG
    616         else
    617           fprintf(stderr,"JSVAL NVP1J IS NOT OBJECT !!\n");
    618 #endif
    619       }
    620 #ifdef JS_DEBUG
    621       dumpMaps(tres);
    622 #endif
    623       if(res==NULL)
    624         res=dupMaps(&tres);
    625       else
    626         addMapsToMaps(&res,tres);
    627       freeMaps(&tres);
    628       free(tres);
    629       tres=NULL;
    630       JS_DestroyIdArray(cx,idp);
    631     }
    632   }
    633 #ifdef JS_DEBUG
    634   dumpMaps(res);
     736      fprintf (stderr, "Properties length :  %d \n", idp->length);
     737#endif
     738      for (index = 0, argNum = idp->length; index < argNum; index++)
     739        {
     740          jsval id = idp->vector[index];
     741          jsval vp;
     742          JSString *str;
     743          JS_IdToValue (cx, id, &vp);
     744          char *c, *tmp, *tmp1;
     745          JSString *jsmsg, *jsmsg1;
     746          size_t len, len1;
     747          jsmsg = JS_ValueToString (cx, vp);
     748          len = JS_GetStringLength (jsmsg);
     749          jsval nvp;
     750          tmp = JS_EncodeString (cx, jsmsg);
     751          JS_GetProperty (cx, JSVAL_TO_OBJECT (t), tmp, &nvp);
     752          jsmsg1 = JS_ValueToString (cx, nvp);
     753          len1 = JS_GetStringLength (jsmsg1);
     754          tmp1 = JS_EncodeString (cx, jsmsg1);
     755#ifdef JS_DEBUG
     756          fprintf (stderr, "Enumerate id : %d [ %s => %s ]\n", index, tmp,
     757                   tmp1);
     758#endif
     759          if (res != NULL)
     760            {
     761#ifdef JS_DEBUG
     762              fprintf (stderr, "%s - %s\n", tmp, tmp1);
     763#endif
     764              addToMap (res, tmp, tmp1);
     765            }
     766          else
     767            {
     768              res = createMap (tmp, tmp1);
     769              res->next = NULL;
     770            }
     771          free (tmp);
     772          free (tmp1);
     773#ifdef JS_DEBUG
     774          dumpMap (res);
     775#endif
     776        }
     777      JS_DestroyIdArray (cx, idp);
     778    }
     779#ifdef JS_DEBUG
     780  dumpMap (res);
    635781#endif
    636782  return res;
    637783}
    638784
    639 map* mapFromJSObject(JSContext *cx,jsval t){
    640   map *res=NULL;
    641   JSIdArray *idp=JS_Enumerate(cx,JSVAL_TO_OBJECT(t));
    642 #ifdef JS_DEBUG
    643   fprintf(stderr,"Properties %p\n",(void*)t);
    644 #endif
    645   if(idp!=NULL) {
    646     int index;
    647     jsdouble argNum;
    648 #ifdef JS_DEBUG
    649     fprintf(stderr,"Properties length :  %d \n",idp->length);
    650 #endif
    651     for (index=0,argNum=idp->length;index<argNum;index++) {
    652       jsval id = idp->vector[index];
    653       jsval vp;
    654       JSString* str;
    655       JS_IdToValue(cx,id,&vp);
    656       char *c, *tmp, *tmp1;
    657       JSString *jsmsg,*jsmsg1;
    658       size_t len,len1;
    659       jsmsg = JS_ValueToString(cx,vp);
    660       len = JS_GetStringLength(jsmsg);
    661       jsval nvp;
    662       tmp=JS_EncodeString(cx,jsmsg);
    663       JS_GetProperty(cx, JSVAL_TO_OBJECT(t), tmp, &nvp);
    664       jsmsg1 = JS_ValueToString(cx,nvp);
    665       len1 = JS_GetStringLength(jsmsg1);
    666       tmp1=JS_EncodeString(cx,jsmsg1);
    667 #ifdef JS_DEBUG
    668       fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,tmp,tmp1);
    669 #endif
    670       if(res!=NULL){
    671 #ifdef JS_DEBUG
    672         fprintf(stderr,"%s - %s\n",tmp,tmp1);
    673 #endif
    674         addToMap(res,tmp,tmp1);
    675       }
    676       else{
    677         res=createMap(tmp,tmp1);
    678         res->next=NULL;
    679       }
    680       free(tmp);
    681       free(tmp1);
    682 #ifdef JS_DEBUG
    683       dumpMap(res);
    684 #endif
    685     }
    686     JS_DestroyIdArray(cx,idp);
    687   }
    688 #ifdef JS_DEBUG
    689   dumpMap(res);
    690 #endif
    691   return res;
    692 }
    693 
    694785/* The error reporter callback. */
    695 void reportError(JSContext *cx, const char *message, JSErrorReport *report)
    696 {
    697   sprintf(dbg,"%s:%u:%s\n",
    698           report->filename ? report->filename : "<no filename>",
    699           (unsigned int) report->lineno,
    700           message);
    701 #ifdef JS_DEBUG
    702   fprintf(stderr,"%s",dbg);
    703 #endif
    704   fflush(stderr);
    705 }
    706 
    707 char* JSValToChar(JSContext* context, jsval* arg) {
     786void
     787reportError (JSContext * cx, const char *message, JSErrorReport * report)
     788{
     789  sprintf (dbg, "%s:%u:%s\n",
     790           report->filename ? report->filename : "<no filename>",
     791           (unsigned int) report->lineno, message);
     792#ifdef JS_DEBUG
     793  fprintf (stderr, "%s", dbg);
     794#endif
     795  fflush (stderr);
     796}
     797
     798char *
     799JSValToChar (JSContext * context, jsval * arg)
     800{
    708801  char *c;
    709802  char *tmp;
     
    711804  size_t len;
    712805  int i;
    713   if(!JSVAL_IS_STRING(*arg)) {
    714     return NULL;
    715   }
    716   jsmsg = JS_ValueToString(context,*arg);
    717   len = JS_GetStringLength(jsmsg);
    718   tmp = JS_EncodeString(context,jsmsg);
    719   c = (char*)malloc((len+1)*sizeof(char));
     806  if (!JSVAL_IS_STRING (*arg))
     807    {
     808      return NULL;
     809    }
     810  jsmsg = JS_ValueToString (context, *arg);
     811  len = JS_GetStringLength (jsmsg);
     812  tmp = JS_EncodeString (context, jsmsg);
     813  c = (char *) malloc ((len + 1) * sizeof (char));
    720814  c[len] = '\0';
    721815#ifdef ULINET_DEBUG
    722   fprintf(stderr,"%d \n",len);
    723 #endif
    724   for(i = 0;i < len;i++) {
    725     c[i] = tmp[i];
    726     c[i+1] = 0;
    727   }
     816  fprintf (stderr, "%d \n", len);
     817#endif
     818  for (i = 0; i < len; i++)
     819    {
     820      c[i] = tmp[i];
     821      c[i + 1] = 0;
     822    }
    728823#ifdef ULINET_DEBUG
    729   fprintf(stderr,"%s \n",c);
     824  fprintf (stderr, "%s \n", c);
    730825#endif
    731826  return c;
    732827}
    733828
    734 HINTERNET setHeader(HINTERNET* handle,JSContext *cx,JSObject *header){
    735   jsuint length=0;
    736   jsint i=0;
     829HINTERNET
     830setHeader (HINTERNET * handle, JSContext * cx, JSObject * header)
     831{
     832  jsuint length = 0;
     833  jsint i = 0;
    737834  char *tmp1;
    738835#ifdef ULINET_DEBUG
    739   fprintf(stderr,"setHeader\n");
    740 #endif
    741   if(JS_IsArrayObject(cx,header)){
     836  fprintf (stderr, "setHeader\n");
     837#endif
     838  if (JS_IsArrayObject (cx, header))
     839    {
    742840#ifdef ULINET_DEBUG
    743     fprintf(stderr,"header is an array\n");
    744 #endif
    745     JS_GetArrayLength(cx,header,&length);
     841      fprintf (stderr, "header is an array\n");
     842#endif
     843      JS_GetArrayLength (cx, header, &length);
    746844#ifdef ULINET_DEBUG
    747     fprintf(stderr,"header is an array of %d elements\n",length);
    748 #endif
    749     handle->ihandle[handle->nb].header=NULL;
    750     for(i=0;i<length;i++){
    751       jsval tmp;
    752       JS_GetElement(cx,header,i,&tmp);
    753       tmp1=JSValToChar(cx,&tmp);
     845      fprintf (stderr, "header is an array of %d elements\n", length);
     846#endif
     847      handle->ihandle[handle->nb].header = NULL;
     848      for (i = 0; i < length; i++)
     849        {
     850          jsval tmp;
     851          JS_GetElement (cx, header, i, &tmp);
     852          tmp1 = JSValToChar (cx, &tmp);
    754853#ifdef ULINET_DEBUG
    755       curl_easy_setopt(handle->ihandle[handle->nb].handle,CURLOPT_VERBOSE,1);
    756       fprintf(stderr,"Element of array n° %d, value : %s\n",i,tmp1);
    757 #endif
    758       handle->ihandle[handle->nb].header=curl_slist_append(handle->ihandle[handle->nb].header, tmp1);
    759       free(tmp1);
    760     }
    761   }
    762   else{
    763     fprintf(stderr,"not an array !!!!!!!\n");
    764   }
     854          curl_easy_setopt (handle->ihandle[handle->nb].handle,
     855                            CURLOPT_VERBOSE, 1);
     856          fprintf (stderr, "Element of array n° %d, value : %s\n", i, tmp1);
     857#endif
     858          handle->ihandle[handle->nb].header =
     859            curl_slist_append (handle->ihandle[handle->nb].header, tmp1);
     860          free (tmp1);
     861        }
     862    }
     863  else
     864    {
     865      fprintf (stderr, "not an array !!!!!!!\n");
     866    }
    765867  return *handle;
    766868}
    767869
    768870JSBool
    769 JSTranslate(JSContext *cx, uintN argc, jsval *argv1)
    770 {
    771   jsval *argv = JS_ARGV(cx,argv1);
    772   char *str=JSValToChar(cx,&argv[0]);
    773   char *tmpValue=_ss(str);
    774   JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpValue,strlen(tmpValue)))); 
    775   JS_MaybeGC(cx);
     871JSTranslate (JSContext * cx, uintN argc, jsval * argv1)
     872{
     873  jsval *argv = JS_ARGV (cx, argv1);
     874  char *str = JSValToChar (cx, &argv[0]);
     875  char *tmpValue = _ss (str);
     876  JS_SET_RVAL (cx, argv1,
     877               STRING_TO_JSVAL (JS_NewStringCopyN
     878                                (cx, tmpValue, strlen (tmpValue))));
     879  JS_MaybeGC (cx);
    776880  return JS_TRUE;
    777881}
    778882
    779883JSBool
    780 JSRequest(JSContext *cx, uintN argc, jsval *argv1)
    781 {
    782   jsval *argv = JS_ARGV(cx,argv1);
     884JSRequest (JSContext * cx, uintN argc, jsval * argv1)
     885{
     886  jsval *argv = JS_ARGV (cx, argv1);
    783887  HINTERNET hInternet;
    784888  HINTERNET res;
     
    787891  char *url;
    788892  char *method;
    789   char* tmpValue;
     893  char *tmpValue;
    790894  size_t dwRead;
    791   int i=0;
    792   JS_MaybeGC(cx);
    793   hInternet=InternetOpen("ZooWPSClient\0",
    794                          INTERNET_OPEN_TYPE_PRECONFIG,
    795                          NULL,NULL, 0);
    796   if(!CHECK_INET_HANDLE(hInternet))
     895  int i = 0;
     896  JS_MaybeGC (cx);
     897  hInternet = InternetOpen ("ZooWPSClient\0",
     898                            INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     899  if (!CHECK_INET_HANDLE (hInternet))
    797900    return JS_FALSE;
    798   if(argc>=2){
    799     method=JSValToChar(cx,&argv[0]);
    800     url=JSValToChar(cx,&argv[1]);
    801   }
    802   else{
    803     method=zStrdup("GET");
    804     url=JSValToChar(cx,argv);
    805   }
    806   hInternet.waitingRequests[hInternet.nb]=strdup(url);
    807   if(argc==4){
    808     char *body;
    809     body=JSValToChar(cx,&argv[2]);
    810     header=JSVAL_TO_OBJECT(argv[3]);
     901  if (argc >= 2)
     902    {
     903      method = JSValToChar (cx, &argv[0]);
     904      url = JSValToChar (cx, &argv[1]);
     905    }
     906  else
     907    {
     908      method = zStrdup ("GET");
     909      url = JSValToChar (cx, argv);
     910    }
     911  hInternet.waitingRequests[hInternet.nb] = strdup (url);
     912  if (argc == 4)
     913    {
     914      char *body;
     915      body = JSValToChar (cx, &argv[2]);
     916      header = JSVAL_TO_OBJECT (argv[3]);
    811917#ifdef ULINET_DEBUG
    812     fprintf(stderr,"URL (%s) \nBODY (%s)\n",url,body);
    813 #endif
    814     if(JS_IsArrayObject(cx,header))
    815       setHeader(&hInternet,cx,header);
     918      fprintf (stderr, "URL (%s) \nBODY (%s)\n", url, body);
     919#endif
     920      if (JS_IsArrayObject (cx, header))
     921        setHeader (&hInternet, cx, header);
    816922#ifdef ULINET_DEBUG
    817     fprintf(stderr,"BODY (%s)\n",body);
    818 #endif
    819     InternetOpenUrl(&hInternet,hInternet.waitingRequests[hInternet.nb],body,strlen(body),
    820                     INTERNET_FLAG_NO_CACHE_WRITE,0);   
    821     processDownloads(&hInternet);
    822     free(body);
    823   }else{
    824     if(argc==3){
    825       char *body=JSValToChar(cx,&argv[2]);
    826       InternetOpenUrl(&hInternet,hInternet.waitingRequests[hInternet.nb],body,strlen(body),
    827                       INTERNET_FLAG_NO_CACHE_WRITE,0);
    828       processDownloads(&hInternet);
    829       free(body);
    830     }else{
    831       InternetOpenUrl(&hInternet,hInternet.waitingRequests[hInternet.nb],NULL,0,
    832                       INTERNET_FLAG_NO_CACHE_WRITE,0);
    833       processDownloads(&hInternet);
    834     }
    835   }
    836   tmpValue=(char*)malloc((hInternet.ihandle[0].nDataLen+1)*sizeof(char));
    837   InternetReadFile(hInternet.ihandle[0],(LPVOID)tmpValue,hInternet.ihandle[0].nDataLen,&dwRead);
     923      fprintf (stderr, "BODY (%s)\n", body);
     924#endif
     925      InternetOpenUrl (&hInternet, hInternet.waitingRequests[hInternet.nb],
     926                       body, strlen (body), INTERNET_FLAG_NO_CACHE_WRITE, 0);
     927      processDownloads (&hInternet);
     928      free (body);
     929    }
     930  else
     931    {
     932      if (argc == 3)
     933        {
     934          char *body = JSValToChar (cx, &argv[2]);
     935          InternetOpenUrl (&hInternet,
     936                           hInternet.waitingRequests[hInternet.nb], body,
     937                           strlen (body), INTERNET_FLAG_NO_CACHE_WRITE, 0);
     938          processDownloads (&hInternet);
     939          free (body);
     940        }
     941      else
     942        {
     943          InternetOpenUrl (&hInternet,
     944                           hInternet.waitingRequests[hInternet.nb], NULL, 0,
     945                           INTERNET_FLAG_NO_CACHE_WRITE, 0);
     946          processDownloads (&hInternet);
     947        }
     948    }
     949  tmpValue =
     950    (char *) malloc ((hInternet.ihandle[0].nDataLen + 1) * sizeof (char));
     951  InternetReadFile (hInternet.ihandle[0], (LPVOID) tmpValue,
     952                    hInternet.ihandle[0].nDataLen, &dwRead);
    838953#ifdef ULINET_DEBUG
    839   fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
    840 #endif
    841   if(dwRead==0){
    842     JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,"Unable to access the file.",strlen("Unable to access the file."))));
    843     return JS_TRUE;
    844   }
     954  fprintf (stderr, "content downloaded (%d) (%s) \n", dwRead, tmpValue);
     955#endif
     956  if (dwRead == 0)
     957    {
     958      JS_SET_RVAL (cx, argv1,
     959                   STRING_TO_JSVAL (JS_NewStringCopyN
     960                                    (cx, "Unable to access the file.",
     961                                     strlen ("Unable to access the file."))));
     962      return JS_TRUE;
     963    }
    845964
    846965#ifdef ULINET_DEBUG
    847   fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
    848 #endif
    849   JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpValue,strlen(tmpValue))));
    850   free(url);
    851   if(argc>=2)
    852     free(method);
    853   InternetCloseHandle(&hInternet);
    854   JS_MaybeGC(cx);
     966  fprintf (stderr, "content downloaded (%d) (%s) \n", dwRead, tmpValue);
     967#endif
     968  JS_SET_RVAL (cx, argv1,
     969               STRING_TO_JSVAL (JS_NewStringCopyN
     970                                (cx, tmpValue, strlen (tmpValue))));
     971  free (url);
     972  if (argc >= 2)
     973    free (method);
     974  InternetCloseHandle (&hInternet);
     975  JS_MaybeGC (cx);
    855976  return JS_TRUE;
    856977}
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