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