Changeset 9 for trunk/zoo-kernel/service_internal_js.c
- Timestamp:
- Sep 14, 2010, 2:04:55 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-kernel/service_internal_js.c
r1 r9 30 30 maps **inputs,maps **outputs) 31 31 { 32 maps* main=*main_conf; 33 maps* _inputs=*inputs; 34 maps* _outputs=*outputs; 35 32 36 /* The class of the global object. */ 33 37 JSClass global_class = { … … 47 51 if (rt == NULL) 48 52 return 1; 49 53 50 54 /* Create a context. */ 51 cx = JS_NewContext(rt, 55 cx = JS_NewContext(rt,8192); 52 56 if (cx == NULL){ 53 57 return 1; … … 78 82 map* tmpm1=getMap(request,"metapath"); 79 83 map* tmpm2=getMap(s->content,"serviceProvider"); 80 char *filename[strlen(tmpm1->value)+strlen(tmpm2->value)+6];84 char filename[strlen(tmpm1->value)+strlen(tmpm2->value)+6]; 81 85 char ntmp[1024]; 82 86 getcwd(ntmp,1024); … … 87 91 uint16 lineno; 88 92 jsval rval; 89 FILE *jsfile=fopen(filename,"r b");93 FILE *jsfile=fopen(filename,"r"); 90 94 JSBool ok ; 91 JSScript *script = JS_CompileFileHandle(cx, global, filename, 92 if(script ){95 JSScript *script = JS_CompileFileHandle(cx, global, filename,jsfile); 96 if(script!=NULL){ 93 97 (void)JS_ExecuteScript(cx, global, script, &rval); 94 98 } … … 102 106 JS_DestroyRuntime(rt); 103 107 JS_ShutDown(); 108 fclose(jsfile); 104 109 exit(-1); 105 110 } 106 111 /* Call a function in obj's scope. */ 107 112 jsval argv[3]; 108 argv[0] = OBJECT_TO_JSVAL(JSObject_FromMaps(cx,*main_conf)); 109 argv[1] = OBJECT_TO_JSVAL(JSObject_FromMaps(cx,*inputs)); 110 argv[2] = OBJECT_TO_JSVAL(JSObject_FromMaps(cx,*outputs)); 111 jsval rval1; 113 JSObject *jsargv1=JSObject_FromMaps(cx,*main_conf); 114 argv[0] = OBJECT_TO_JSVAL(jsargv1); 115 JSObject *jsargv2=JSObject_FromMaps(cx,*inputs); 116 argv[1] = OBJECT_TO_JSVAL(jsargv2); 117 JSObject *jsargv3=JSObject_FromMaps(cx,*outputs); 118 argv[2] = OBJECT_TO_JSVAL(jsargv3); 119 jsval rval1=JSVAL_NULL; 112 120 #ifdef JS_DEBUG 113 121 fprintf(stderr, "object %p\n", (void *) argv[2]); … … 121 129 122 130 JSObject *d; 123 if (ok==JS_TRUE ) {131 if (ok==JS_TRUE && JSVAL_IS_OBJECT(rval1)==JS_TRUE) { 124 132 #ifdef JS_DEBUG 125 133 fprintf(stderr,"Function run sucessfully !\n"); … … 127 135 /* Should get a number back from the service function call. */ 128 136 ok = JS_ValueToObject(cx, rval1, &d); 129 if (ok==JS_TRUE) {130 #ifdef JS_DEBUG131 fprintf(stderr,"Function run sucessfully !\n");132 #endif133 /*char tmpres[10];134 sprintf(tmpres,"%p",(void *)d);135 res=atoi(tmpres);*/136 }137 137 }else{ 138 138 /* Unable to run JS function */ 139 139 char tmp1[1024]; 140 //sprintf(tmp1,"Unable to run %s from the JavScript file %s",s->name,filename); 140 if(strlen(dbg)==0) 141 sprintf(dbg,"No result was found after the function call"); 141 142 sprintf(tmp1,"Unable to run %s from the JavScript file %s : \n %s",s->name,filename,dbg); 142 143 fprintf(stderr,"%s",tmp1); 143 144 map* err=createMap("text",tmp1); 144 add MapToMap(err,createMap("code","NoApplicableCode"));145 addToMap(err,"code","NoApplicableCode"); 145 146 printExceptionReportResponse(*main_conf,err); 147 freeMap(&err); 148 free(err); 146 149 JS_DestroyContext(cx); 147 150 JS_DestroyRuntime(rt); 148 151 JS_ShutDown(); 152 // Should return -1 here but the unallocation won't work from zoo_service_loader.c line 1847 149 153 exit(-1); 150 154 } 151 155 152 156 jsval t=OBJECT_TO_JSVAL(d); 153 //JSVAL_TO_OBJECT(t); 154 if(JS_IsArrayObject(cx,t)){ 157 if(JS_IsArrayObject(cx,d)){ 155 158 #ifdef JS_DEBUG 156 159 fprintf(stderr,"An array was returned !\n"); 157 160 #endif 158 js doublelen;159 if((JS_GetArrayLength(cx, t, &len)==JS_FALSE)){161 jsint len; 162 if((JS_GetArrayLength(cx, d, &len)==JS_FALSE)){ 160 163 #ifdef JS_DEBUG 161 164 fprintf(stderr,"outputs array is empty\n"); … … 163 166 } 164 167 jsval tmp1; 165 JSBool hasResult=JS_GetElement(cx, t,0,&tmp1);168 JSBool hasResult=JS_GetElement(cx,d,0,&tmp1); 166 169 res=JSVAL_TO_INT(tmp1); 167 170 #ifdef JS_DEBUG … … 169 172 #endif 170 173 jsval tmp2; 171 JSBool hasElement=JS_GetElement(cx,t,1,&tmp2); 172 out=mapsFromJSObject(cx,tmp2); 173 *outputs=out; 174 JSBool hasElement=JS_GetElement(cx,d,1,&tmp2); 175 if(hasElement==JS_TRUE){ 176 *outputs=mapsFromJSObject(cx,tmp2); 177 } 174 178 } 175 179 else{ 176 //#ifdef JS_DEBUG177 fprintf(stderr,"The ser vice didn't return an array !\n");178 //#endif180 #ifdef JS_DEBUG 181 fprintf(stderr,"The serice didn't return an array !\n"); 182 #endif 179 183 jsval tmp1; 180 JSBool hasResult=JS_GetProperty(cx, t,"result",&tmp1);184 JSBool hasResult=JS_GetProperty(cx,d,"result",&tmp1); 181 185 res=JSVAL_TO_INT(tmp1); 182 186 183 //#ifdef JS_DEBUG187 #ifdef JS_DEBUG 184 188 fprintf(stderr," * %d * \n",res); 185 //#endif189 #endif 186 190 jsval tmp2; 187 JSBool hasElement=JS_GetProperty(cx, t,"outputs",&tmp2);191 JSBool hasElement=JS_GetProperty(cx,d,"outputs",&tmp2); 188 192 if(!hasElement) 189 193 fprintf(stderr,"No outputs property returned\n"); 190 if(JS_IsArrayObject(cx, tmp2))194 if(JS_IsArrayObject(cx,JSVAL_TO_OBJECT(tmp2))) 191 195 fprintf(stderr,"outputs is array an as expected\n"); 192 196 else 193 197 fprintf(stderr,"outputs is not an array as expected\n"); 194 out=mapsFromJSObject(cx,tmp2);198 *outputs=mapsFromJSObject(cx,tmp2); 195 199 #ifdef JS_DEBUG 196 200 dumpMaps(out); 197 201 #endif 198 *outputs=out; 199 } 200 201 *inputs=mapsFromJSObject(cx,argv[1]); 202 *main_conf=mapsFromJSObject(cx,argv[0]); 203 204 205 //} 206 //else 207 // ERROR 208 //; 202 } 209 203 210 204 /* Cleanup. */ 211 JS_MaybeGC(cx); 205 // The JS_MaybeGC call imply segmentation fault 206 //JS_MaybeGC(cx); 212 207 JS_DestroyScript(cx, script); 213 JS_DestroyContext(cx); 208 // If we use the DestroyContext as requested to release memory then we get 209 // issue getting back the main configuration maps after coming back to the 210 // runRequest function ... 211 //JS_DestroyContext(cx); 214 212 JS_DestroyRuntime(rt); 215 213 JS_ShutDown(); 216 fflush(stderr);217 214 #ifdef JS_DEBUG 218 215 fprintf(stderr,"Returned value %d\n",res); … … 223 220 JSObject* JSObject_FromMaps(JSContext *cx,maps* t){ 224 221 JSObject *res = JS_NewArrayObject(cx, 0, NULL); 222 if(res==NULL) 223 fprintf(stderr,"Array Object is NULL!\n"); 225 224 maps* tmp=t; 226 225 while(tmp!=NULL){ … … 247 246 while(tmpm!=NULL){ 248 247 jsval jsstr = STRING_TO_JSVAL(JS_NewString(cx,tmpm->value,strlen(tmpm->value))); 249 JS_SetProperty(cx, res f, tmpm->name,&jsstr);248 JS_SetProperty(cx, res, tmpm->name,&jsstr); 250 249 #ifdef JS_DEBUG 251 250 fprintf(stderr,"%s => %s\n",tmpm->name,tmpm->value); … … 259 258 maps *res=NULL; 260 259 maps *tres=NULL; 261 JSVAL_TO_OBJECT(t); 260 jsint oi=0; 261 JSObject* tt=JSVAL_TO_OBJECT(t); 262 262 #ifdef JS_DEBUG 263 263 fprintf(stderr,"Is finally an array ?\n"); 264 #endif 265 if(JS_IsArrayObject(cx,t)){ 266 #ifdef JS_DEBUG 264 if(JS_IsArrayObject(cx,tt)){ 267 265 fprintf(stderr,"Is finally an array !\n"); 268 #endif 269 } 270 #ifdef JS_DEBUG 266 } 271 267 else 272 268 fprintf(stderr,"Is not an array !\n"); 273 269 #endif 274 jsdouble len; 275 if((JS_GetArrayLength(cx, t, &len)==JS_FALSE)){ 270 jsint len; 271 JSBool hasLen=JS_GetArrayLength(cx, tt, &len); 272 if(hasLen==JS_FALSE){ 276 273 #ifdef JS_DEBUG 277 274 fprintf(stderr,"outputs array is empty\n"); 278 275 #endif 279 276 } 280 int i=0;281 char tmp[10];282 sprintf(tmp,"%d",len);283 int clen=atoi(tmp);284 277 #ifdef JS_DEBUG 285 278 fprintf(stderr,"outputs array length : %d\n",len); 286 279 #endif 287 for( i=0;i<len;i++){288 #ifdef JS_DEBUG 289 fprintf(stderr,"outputs array length : %d step %d \n",len,i);280 for(oi=0;oi < len;oi++){ 281 #ifdef JS_DEBUG 282 fprintf(stderr,"outputs array length : %d step %d \n",len,oi); 290 283 #endif 291 284 jsval tmp1; 292 JSBool hasElement=JS_GetElement(cx,t ,i,&tmp1);293 JS VAL_TO_OBJECT(tmp1);294 JSIdArray *idp=JS_Enumerate(cx, tmp1);285 JSBool hasElement=JS_GetElement(cx,tt,oi,&tmp1); 286 JSObject *otmp1=JSVAL_TO_OBJECT(tmp1); 287 JSIdArray *idp=JS_Enumerate(cx,otmp1); 295 288 if(idp!=NULL) { 296 289 int index; … … 300 293 #endif 301 294 for (index=0,argNum=idp->length;index<argNum;index++) { 302 js id*id = idp->vector[index];295 jsval id = idp->vector[index]; 303 296 jsval vp; 304 297 JSString* str; … … 310 303 len1 = JS_GetStringLength(jsmsg); 311 304 #ifdef JS_DEBUG 312 fprintf(stderr,"Enumerate id : %d => %s\n", i,JS_GetStringBytes(jsmsg));313 #endif 314 jsval nvp ;315 if((JS_GetProperty(cx, tmp1, JS_GetStringBytes(jsmsg), &nvp)==JS_FALSE))316 #ifdef JS_DEBUG 317 fprintf(stderr,"Enumerate id : %d => %s => No more value\n", i,JS_GetStringBytes(jsmsg));305 fprintf(stderr,"Enumerate id : %d => %s\n",oi,JS_GetStringBytes(jsmsg)); 306 #endif 307 jsval nvp=JSVAL_NULL; 308 if((JS_GetProperty(cx, JSVAL_TO_OBJECT(tmp1), JS_GetStringBytes(jsmsg), &nvp)==JS_FALSE)) 309 #ifdef JS_DEBUG 310 fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,JS_GetStringBytes(jsmsg)); 318 311 #endif 319 312 if(JSVAL_IS_OBJECT(nvp)){ … … 333 326 fprintf(stderr,"JSVAL NVP1J IS OBJECT\n"); 334 327 #endif 335 tres=(maps*)malloc( sizeof(maps*));328 tres=(maps*)malloc(MAPS_SIZE); 336 329 tres->name=strdup(JS_GetStringBytes(jsmsg)); 337 330 tres->content=mapFromJSObject(cx,nvp1j); … … 341 334 #endif 342 335 if(res==NULL) 343 res= tres;336 res=dupMaps(&tres); 344 337 else 345 338 addMapsToMaps(&res,tres); 339 freeMaps(&tres); 340 free(tres); 341 tres=NULL; 346 342 #ifdef JS_DEBUG 347 343 dumpMaps(res); … … 363 359 map* mapFromJSObject(JSContext *cx,jsval t){ 364 360 map *res=NULL; 365 JSIdArray *idp=JS_Enumerate(cx, t);361 JSIdArray *idp=JS_Enumerate(cx,JSVAL_TO_OBJECT(t)); 366 362 #ifdef JS_DEBUG 367 363 fprintf(stderr,"Properties %p\n",(void*)t); … … 374 370 #endif 375 371 for (index=0,argNum=idp->length;index<argNum;index++) { 376 js id*id = idp->vector[index];372 jsval id = idp->vector[index]; 377 373 jsval vp; 378 374 JSString* str; … … 384 380 len = JS_GetStringLength(jsmsg); 385 381 jsval nvp; 386 JS_GetProperty(cx, t, JS_GetStringBytes(jsmsg), &nvp);382 JS_GetProperty(cx, JSVAL_TO_OBJECT(t), JS_GetStringBytes(jsmsg), &nvp); 387 383 jsmsg1 = JS_ValueToString(cx,nvp); 388 384 len1 = JS_GetStringLength(jsmsg1); … … 390 386 fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1)); 391 387 #endif 392 if(res!=NULL) 393 addMapToMap(&res,createMap(JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1))); 394 else 388 if(res!=NULL){ 389 fprintf(stderr,"%s - %s\n",JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1)); 390 addToMap(res,JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1)); 391 } 392 else{ 395 393 res=createMap(JS_GetStringBytes(jsmsg),JS_GetStringBytes(jsmsg1)); 394 res->next=NULL; 395 } 396 dumpMap(res); 396 397 } 397 398 } … … 401 402 return res; 402 403 } 403 404 404 405 405 /* The error reporter callback. */
Note: See TracChangeset
for help on using the changeset viewer.