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