FreeWRL / FreeX3D 4.3.0
jsUtils_sm.cpp
1/*
2
3
4A substantial amount of code has been adapted from js/src/js.c,
5which is the sample application included with the javascript engine.
6
7*/
8
9/****************************************************************************
10 This file is part of the FreeWRL/FreeX3D Distribution.
11
12 Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
13
14 FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
15 it under the terms of the GNU Lesser Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 FreeWRL/FreeX3D is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
26****************************************************************************/
27
28
29#include <config.h>
30#ifdef JAVASCRIPT_SM
31#if defined(JS_SMCPP)
32#undef DEBUG
33//#define DEBUG 1 //challenge it with lots of ASSERTS, just for cleaning up code correctness, not production
34# include <jsapi.h> /* JS compiler */
35//# include <jsdbgapi.h> /* JS debugger */
36
37//#if !(defined(JAVASCRIPT_STUB) || defined(JAVASCRIPT_DUK))
38#define JS_VERSION 187
39//#define JS_THREADSAFE 1 //by default in 186+
40
41#define STRING_SIZE 256
42#define uintN unsigned
43#define intN int
44#define jsint int32_t
45#define jsuint uint32_t
46#define int32 int32_t
47#define jsdouble double
48
49#define JS_FinalizeStub NULL
50#ifndef IBOOL
51typedef int IBOOL;
52#endif
53typedef IBOOL _Bool;
54
55extern "C" {
56#include <system.h>
57//#if !(defined(JAVASCRIPT_STUB) || defined(JAVASCRIPT_DUK))
58
59#include <display.h>
60#include <internal.h>
61
62//#include <libFreeWRL.h>
63
64//#include "../vrml_parser/Structs.h"
65//#include "../main/headers.h"
66//#include "../vrml_parser/CParseGeneral.h"
67//#include "../main/Snapshot.h"
68//#include "../scenegraph/Collision.h"
69//#include "../scenegraph/quaternion.h"
70//#include "../scenegraph/Viewer.h"
71//#include "../input/EAIHelpers.h"
72//#include "../input/SensInterps.h"
73//#include "../x3d_parser/Bindable.h"
74
75#include "JScript.h"
76#include "CScripts.h"
77#include "jsNative.h"
78
79#include "fieldSet.h"
80#ifdef _MSC_VER
81#include <pthread.h> // win32 needs the strtok_r
82#endif
83} //extern "C"
84#include "jsUtils_sm.h"
85#include "jsVRMLClasses_sm.h"
86extern "C" {
87#ifdef WANT_OSC
88 #include "../scenegraph/ringbuf.h"
89 #define USE_OSC 1
90 #define TRACK_FIFO_MSG 0
91#else
92 #define USE_OSC 0
93 #define TRACK_FIFO_MSG 0
94#endif
95
96extern void dump_scene (FILE *fp, int level, struct X3D_Node* node); // in GeneratedCode.c
97extern char *parser_getNameFromNode(struct X3D_Node *ptr) ; /* vi +/dump_scene src/lib/scenegraph/GeneratedCode.c */
98
99/********************** Javascript to X3D Scenegraph ****************************/
100
101
102/* a SF value has changed in an MF; eg, xx[10] = new SFVec3f(...); */
103/* These values were created below; new SF values to this MF are not accessed here, but in
104 the MFVec3fSetProperty (or equivalent). Note that we do NOT use something like JS_SetElement
105 on these because it would be a recursive call; thus we set the private data */
106
107
108//static int insetSFStr = FALSE;
109//static JSBool reportWarnings = JS_TRUE;
110
111typedef struct pjsUtils{
112 int insetSFStr;// = FALSE;
113 JSBool reportWarnings;// = JS_TRUE;
114
115}* ppjsUtils;
116void *jsUtils_constructor(){
117 void *v = MALLOCV(sizeof(struct pjsUtils));
118 memset(v,0,sizeof(struct pjsUtils));
119 return v;
120}
121void jsUtils_init(struct iiglobal::tjsUtils *t){
122 //public
123 //private
124 t->prv = jsUtils_constructor();
125 {
126 ppjsUtils p = (ppjsUtils)t->prv;
127 p->insetSFStr = FALSE;
128 p->reportWarnings = JS_TRUE;
129 }
130}
131
132} //extern "C"
133
134int JS_SetPrivateFw(JSContext *cx, JSObject* obj, void *data){
135 int iret = TRUE;
136 JS_SetPrivate(obj, data);
137 return iret;
138}
139JSObject* JS_NewGlobalObjectFw(JSContext *cx, JSClass *clasp){
140 return JS_NewGlobalObject(cx, clasp, NULL);
141}
142void * JS_GetPrivateFw(JSContext *cx,JSObject* obj){
143 return JS_GetPrivate(obj);
144}
145JSObject* JS_GetParentFw(JSContext *cx, JSObject *obj){
146 return JS_GetParent(obj);
147}
148
149
150//https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_reference/JS_NewNumberValue
151JSBool JS_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval){
152 *rval = JS_NumberValue(d);
153 return JS_TRUE;
154}
155
156
157
158JSObject *
159JS_ConstructObjectWithArgumentsFw(JSContext *cx, JSClass *clasp,
160 JSObject *parent, unsigned argc, jsval *argv)
161{
162 JSObject *global = JS_GetGlobalForScopeChain(cx);
163 jsval v;
164 if (!global || !JS_GetProperty(cx, global, clasp->name, &v))
165 return NULL;
166 if (JSVAL_IS_PRIMITIVE(v)) {
167 JS_ReportError(cx, "cannot construct object: constructor is gone");
168 return NULL;
169 }
170 return JS_New(cx, JSVAL_TO_OBJECT(v), argc, argv);
171}
172
173JSObject *
174JS_ConstructObjectFw(JSContext *cx, JSClass *clasp, void *whatever, JSObject *parent)
175{
176 return JS_ConstructObjectWithArgumentsFw(cx, clasp, parent, 0, NULL);
177}
178JSClass* JS_GetClassFw(JSContext *cx, JSObject *obj){
179 return JS_GetClass(obj);
180}
181JSObject * JS_GetPrototypeFw(JSContext *cx, JSObject * obj){
182 JSObject *proto;
183 if( JS_GetPrototype(cx,obj,&proto))
184 return proto;
185 else
186 return NULL;
187}
188
189
190// ppjsUtils p = (ppjsUtils)gglobal()->jsUtils.prv;
191static JSBool setSF_in_MF (JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
192 JSObject *obj = *hobj.address();
193 jsid iid = *hiid.address();
194 jsval *vp = hvp.address();
195
196 long num;
197 jsval pf;
198 jsval nf;
199 JSObject *me;
200 JSObject *par;
201 jsval ele;
202 ppjsUtils p = (ppjsUtils)gglobal()->jsUtils.prv;
203 jsid oid;
204
205 char *tmp;
206 jsval id;
207
208 UNUSED(tmp); // compiler warning mitigation
209
210 if (!JS_IdToValue(cx,iid,&id)) {
211 printf("setSF_in_MF: JS_IdToValue failed.\n");
212 return JS_FALSE;
213 }
214
215
216 /* when we save the value, we will be called again, so we make sure that we
217 know if we are being called from within, or from without */
218 if (p->insetSFStr) {
219 #ifdef JSVRMLCLASSESVERBOSE
220 printf ("setSF_in_MF: already caught this value; this is our JS_SetElement call\n");
221 #endif
222 return JS_TRUE;
223 }
224
225 /* ok, we are really called to replace an existing SFNode MF value assignment */
226 p->insetSFStr = TRUE;
227
228 if (JSVAL_IS_INT(id)) {
229 //if (!JS::ToInt32(cx,id,&num)) {
230 // printf ("setSF_in_MF: error converting to number...\n");
231 // return JS_FALSE;
232 //}
233 num = id.toInt32();
234 /* get a pointer to the object at the index in the parent */
235 if (!JS_GetElement(cx, obj, num, &ele)) {
236 printf ("error getting child %ld in setSF_in_MF\n",num);
237 return JS_FALSE;
238 }
239 /* THIS is the touching that will cause us to be called recursively,
240 which is why insetSFStr is TRUE right here */
241 if (!JS_SetElement(cx,obj,num,vp)) {
242 printf ("can not set element %ld in MFString\n",num);
243 return JS_FALSE;
244 }
245 } else {
246 printf ("expect an integer id in setSF_in_MF\n");
247 return JS_FALSE;
248 }
249
250
251 /* copy this value out to the X3D scene graph */
252 me = obj;
253 par = JS_GetParentFw(cx, me);
254 while (par != NULL) {
255 #ifdef JSVRMLCLASSESVERBOSE
256 printf ("for obj %u: ",me);
257 printJSNodeType(cx,me);
258 printf ("... parent %u\n",par);
259 printJSNodeType(cx,par);
260 #endif
261
262 if (JS_InstanceOf (cx, par, &SFNodeClass, NULL)) {
263 #ifdef JSVRMLCLASSESVERBOSE
264 printf (" the parent IS AN SFNODE - it is %u\n",par);
265 #endif
266
267
268 if (!JS_GetProperty(cx, obj, "_parentField", &pf)) {
269 printf ("doMFSetProperty, can not get parent field from this object\n");
270 return JS_FALSE;
271 }
272
273 nf = OBJECT_TO_JSVAL(me);
274
275 if (!JS_ValueToId(cx,pf,&oid)) {
276 printf("setSF_in_MF: JS_ValueToId failed.\n");
277 return JS_FALSE;
278 }
279 /* OUCH
280 {
281 JS::Handle<JSObject*> hobj(&par);// = new JS::Handle<JSObject*>(&par);
282 JS::Handle<jsid> hiid;// = new JS::Handle<jsid>(&oid);
283 JS::MutableHandle<JS::Value> hvp;// = new JS::MutableHandle<JS::Value>(&nf);
284 //hobj._ = &par;
285 //hiid._ = &oid;
286 //hvp._ = &nf;
287 hobj.fromMarkedLocation(&par);
288 hiid.fromMarkedLocation(&oid);
289 hvp.fromMarkedLocation(&nf);
290 setSFNodeField(cx,hobj,hiid,JS_FALSE,hvp);
291 }
292 */
293 }
294 me = par;
295 par = JS_GetParentFw(cx, me);
296 }
297 p->insetSFStr = FALSE;
298 return JS_TRUE;
299}
300
301/* take an ECMA value in the X3D Scenegraph, and return a jsval with it in */
302/* This is FAST as w deal just with pointers */
303void JS_ECMA_TO_X3D(JSContext *cx, void *Data, unsigned datalen, int dataType, jsval *newval) {
304 float fl;
305 double dl;
306 int il;
307
308 #ifdef JSVRMLCLASSESVERBOSE
309 printf ("calling JS_ECMA_TO_X3D on type %s\n",FIELDTYPES[dataType]);
310 #endif
311
312 switch (dataType) {
313
314 case FIELDTYPE_SFFloat: {
315 if (!JS_ValueToNumber(cx,*newval,&dl)) {
316 printf ("problems converting Javascript val to number\n");
317 return;
318 }
319 fl = (float) dl;
320 memcpy (Data, (void *) &fl, datalen);
321 break;
322 }
323 case FIELDTYPE_SFDouble:
324 case FIELDTYPE_SFTime: {
325 if (!JS_ValueToNumber(cx,*newval,&dl)) {
326 printf ("problems converting Javascript val to number\n");
327 return;
328 }
329 memcpy (Data, (void *) &dl, datalen);
330 break;
331 }
332 case FIELDTYPE_SFBool: {
333 il = JSVAL_TO_BOOLEAN (*newval);
334 memcpy (Data, (void *) &il, datalen);
335 break;
336 }
337
338 case FIELDTYPE_SFInt32: {
339 il = JSVAL_TO_INT (*newval);
340 memcpy (Data, (void *) &il, datalen);
341 break;
342 }
343
344 case FIELDTYPE_SFString: {
345 struct Uni_String *oldS;
346 JSString *_idStr;
347 char *_id_c;
348
349 _idStr = JS_ValueToString(cx, *newval);
350 _id_c = JS_EncodeString(cx,_idStr);
351
352 oldS = (struct Uni_String *) *((intptr_t *)Data);
353 if(oldS == NULL) {
354 *(struct Uni_String **)Data = newASCIIString(_id_c);
355 }else{
356 #ifdef JSVRMLCLASSESVERBOSE
357 printf ("JS_ECMA_TO_X3D, replacing \"%s\" with \"%s\" \n", oldS->strptr, _id_c);
358 #endif
359
360 /* replace the C string if it needs to be replaced. */
361 verify_Uni_String (oldS,_id_c);
362 }
363 JS_free(cx,_id_c);
364 break;
365 }
366 default: { printf("WARNING: SHOULD NOT BE HERE in JS_ECMA_TO_X3D! %d\n",dataType); }
367 }
368}
369
370
371/* take a Javascript ECMA value and put it in the X3D Scenegraph. */
372void JS_SF_TO_X3D(JSContext *cx, void *Data, unsigned datalen, int dataType, jsval *newval) {
373 SFColorNative *Cptr;
374 SFVec3fNative *V3ptr;
375 SFVec3dNative *V3dptr;
376 SFVec2fNative *V2ptr;
377 SFRotationNative *VRptr;
378 SFNodeNative *VNptr;
379
380 void *VPtr;
381
382 #ifdef JSVRMLCLASSESVERBOSE
383 printf ("calling JS_SF_TO_X3D on type %s\n",FIELDTYPES[dataType]);
384 #endif
385
386 /* get a pointer to the internal private data */
387 if ((VPtr = JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*newval))) == NULL) {
388 printf( "JS_GetPrivate failed in JS_SF_TO_X3D.\n");
389 return;
390 }
391
392 /* copy over the data from the X3D node to this new Javascript object */
393 switch (dataType) {
394 case FIELDTYPE_SFColor:
395 Cptr = (SFColorNative *)VPtr;
396 memcpy (Data, (void *)((Cptr->v).c), datalen);
397 break;
398 case FIELDTYPE_SFVec3d:
399 V3dptr = (SFVec3dNative *)VPtr;
400 memcpy (Data, (void *)((V3dptr->v).c), datalen);
401 break;
402 case FIELDTYPE_SFVec3f:
403 V3ptr = (SFVec3fNative *)VPtr;
404 memcpy (Data, (void *)((V3ptr->v).c), datalen);
405 break;
406 case FIELDTYPE_SFVec2f:
407 V2ptr = (SFVec2fNative *)VPtr;
408 memcpy (Data, (void *)((V2ptr->v).c), datalen);
409 break;
410 case FIELDTYPE_SFRotation:
411 VRptr = (SFRotationNative *)VPtr;
412 memcpy (Data,(void *)((VRptr->v).c), datalen);
413 break;
414 case FIELDTYPE_SFNode:
415 VNptr = (SFNodeNative *)VPtr;
416 memcpy (Data, (void *)(VNptr->handle), datalen);
417 break;
418
419 default: { printf("WARNING: SHOULD NOT BE HERE! %d\n",dataType); }
420 }
421}
422
423void JS_SF_TO_X3D_B(JSContext *cx, void *Data, int dataType, int *valueChanged, jsval *newval) {
424 AnyNative *ptr;
425 union anyVrml *anyv;
426
427 /* get a pointer to the internal private data */
428 if ((ptr = (AnyNative*)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*newval))) == NULL) {
429 printf( "JS_GetPrivate failed in JS_SF_TO_X3D_B.\n");
430 return;
431 }
432 if(ptr->type != dataType){
433 printf("JS assigning type %d to %d failed\n",ptr->type,dataType);
434 return;
435 }
436 shallow_copy_field(dataType,ptr->v,(union anyVrml*)Data);
437}
438void JS_SF_TO_X3D_BNode(JSContext *cx, void *Data, int dataType, int *valueChanged, jsval *newval) {
439 AnyNative *ptr;
440 union anyVrml *anyv;
441 if(JSVAL_IS_NULL(*newval)){
442 union anyVrml any;
443 any.sfnode = NULL;
444 shallow_copy_field(dataType,&any,(union anyVrml*)Data);
445 }else{
446 JS_SF_TO_X3D_B(cx, Data, dataType, valueChanged, newval);
447 }
448
449 /* get a pointer to the internal private data */
450}
451
452void getJSMultiNumType(JSContext *, struct Multi_Vec3f *, int);
453
454/* make an MF type from the X3D node. This can be fairly slow... */
455void JS_MF_TO_X3D(JSContext *cx, JSObject * obj, void *Data, int dataType, jsval *newval) {
456 ttglobal tg = gglobal();
457 #ifdef JSVRMLCLASSESVERBOSE
458 printf ("calling JS_MF_TO_X3D on type %s\n",FIELDTYPES[dataType]);
459 printf ("JS_MF_TO_X3D, we have object %u, newval %u\n",obj,*newval);
460 printf ("JS_MF_TO_X3D, obj is an:\n");
461 if (JSVAL_IS_OBJECT(OBJECT_TO_JSVAL(obj))) { printf ("JS_MF_TO_X3D - obj is an OBJECT\n"); }
462 if (JSVAL_IS_PRIMITIVE(OBJECT_TO_JSVAL(obj))) { printf ("JS_MF_TO_X3D - obj is an PRIMITIVE\n"); }
463 printf ("JS_MF_TO_X3D, obj is a "); printJSNodeType(cx,obj);
464 printf ("JS_MF_TO_X3D, vp is an:\n");
465 if (JSVAL_IS_OBJECT(*newval)) { printf ("JS_MF_TO_X3D - vp is an OBJECT\n"); }
466 if (JSVAL_IS_PRIMITIVE(*newval)) { printf ("JS_MF_TO_X3D - vp is an PRIMITIVE\n"); }
467 printf ("JS_MF_TO_X3D, vp is a "); printJSNodeType(cx,JSVAL_TO_OBJECT(*newval));
468 #endif
469
470 *(jsval *)tg->JScript.JSglobal_return_val = *newval;
471 getJSMultiNumType (cx, (struct Multi_Vec3f*) Data, convertToSFType(dataType));
472
473}
474
475/********************** X3D Scenegraph to Javascript ****************************/
476
477/* take an ECMA value in the X3D Scenegraph, and return a jsval with it in */
478/* This is FAST as w deal just with pointers */
479void X3D_ECMA_TO_JS(JSContext *cx, void *Data, int datalen, int dataType, jsval *newval) {
480 float fl;
481 double dl;
482 int il;
483
484 /* NOTE - caller of this function has already defined a BeginRequest */
485
486 #ifdef JSVRMLCLASSESVERBOSE
487 printf ("calling X3D_ECMA_TO_JS on type %s\n",FIELDTYPES[dataType]);
488 #endif
489
490 switch (dataType) {
491
492 case FIELDTYPE_SFFloat: {
493 memcpy ((void *) &fl, Data, datalen);
494 JS_NewNumberValue(cx,(double)fl,newval);
495 break;
496 }
497 case FIELDTYPE_SFDouble:
498 case FIELDTYPE_SFTime: {
499 memcpy ((void *) &dl, Data, datalen);
500 JS_NewNumberValue(cx,dl,newval);
501 break;
502 }
503 case FIELDTYPE_SFBool:
504 case FIELDTYPE_SFInt32: {
505 memcpy ((void *) &il,Data, datalen);
506 *newval = INT_TO_JSVAL(il);
507 break;
508 }
509
510 case FIELDTYPE_SFString: {
511 struct Uni_String *ms;
512
513 /* datalen will be ROUTING_SFSTRING here; or at least should be! We
514 copy over the data, which is a UniString pointer, and use the pointer
515 value here */
516 memcpy((void *) &ms,Data, sizeof(void *));
517 *newval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,ms->strptr));
518 break;
519 }
520 default: { printf("WARNING: SHOULD NOT BE HERE in X3D_ECMA_TO_JS! %d\n",dataType); }
521 }
522}
523
524/* take an ECMA value in the X3D Scenegraph, and return a jsval with it in */
525/* this is not so fast; we call a script to make a default type, then we fill it in */
526void X3D_SF_TO_JS(JSContext *cx, JSObject *obj, void *Data, unsigned datalen, int dataType, jsval *newval) {
527 SFColorNative *Cptr;
528 SFVec3fNative *V3ptr;
529 SFVec3dNative *V3dptr;
530 SFVec2fNative *V2ptr;
531 SFRotationNative *VRptr;
532 SFNodeNative *VNptr;
533
534 void *VPtr;
535 jsval rval;
536 const char *script = NULL;
537
538 /* NOTE - caller is (eventually) a class constructor, no need to BeginRequest */
539
540 #ifdef JSVRMLCLASSESVERBOSE
541 printf ("calling X3D_SF_TO_JS on type %s, newval %u\n",FIELDTYPES[dataType],*newval);
542 #endif
543
544 //if (!JSVAL_IS_OBJECT(*newval)) {
545 if(!(*newval).isObject()) {
546 /* find a script to create the correct object */
547 switch (dataType) {
548 case FIELDTYPE_SFVec3f: script = "new SFVec3f()"; break;
549 case FIELDTYPE_SFVec3d: script = "new SFVec3d()"; break;
550 case FIELDTYPE_SFColor: script = "new SFColor()"; break;
551 case FIELDTYPE_SFNode: script = "new SFNode()"; break;
552 case FIELDTYPE_SFVec2f: script = "new SFVec2f()"; break;
553 case FIELDTYPE_SFRotation: script = "new SFRotation()"; break;
554 default: printf ("invalid type in X3D_SF_TO_JS\n"); return;
555 }
556
557 /* create the object */
558
559
560 #ifdef JSVRMLCLASSESVERBOSE
561 printf ("X3D_SF_TO_JS, have to run script to make new object: \"%s\"\n",script);
562 #endif
563
564 if (!JS_EvaluateScript(cx, obj, script, (int) strlen(script), FNAME_STUB, LINENO_STUB, &rval)) {
565 printf ("error creating the new object in X3D_SF_TO_JS, script :%s:\n",script);
566 return;
567 }
568
569 /* this is the return pointer, lets save it right now */
570 *newval = rval;
571
572 #ifdef JSVRMLCLASSESVERBOSE
573 printf ("X3D_SF_TO_JS, so, newval now is %u\n",*newval);
574 #endif
575
576 }
577 /* get a pointer to the internal private data */
578 if ((VPtr = JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*newval))) == NULL) {
579 printf( "JS_GetPrivate failed in X3D_SF_TO_JS.\n");
580 return;
581 }
582
583
584 /* copy over the data from the X3D node to this new Javascript object */
585 switch (dataType) {
586 case FIELDTYPE_SFColor:
587 Cptr = (SFColorNative *)VPtr;
588 memcpy ((void *)((Cptr->v).c), Data, datalen);
589 Cptr->valueChanged = 1;
590 break;
591 case FIELDTYPE_SFVec3f:
592 V3ptr = (SFVec3fNative *)VPtr;
593 memcpy ((void *)((V3ptr->v).c), Data, datalen);
594 V3ptr->valueChanged = 1;
595 break;
596 case FIELDTYPE_SFVec3d:
597 V3dptr = (SFVec3dNative *)VPtr;
598 memcpy ((void *)((V3dptr->v).c), Data, datalen);
599 V3dptr->valueChanged = 1;
600 break;
601 case FIELDTYPE_SFVec2f:
602 V2ptr = (SFVec2fNative *)VPtr;
603 memcpy ((void *)((V2ptr->v).c), Data, datalen);
604 V2ptr->valueChanged = 1;
605 break;
606 case FIELDTYPE_SFRotation:
607 VRptr = (SFRotationNative *)VPtr;
608 memcpy ((void *)((VRptr->v).c), Data, datalen);
609 VRptr->valueChanged = 1;
610 break;
611 case FIELDTYPE_SFNode:
612 VNptr = (SFNodeNative *)VPtr;
613 memcpy ((void *)(&(VNptr->handle)), Data, datalen);
614 VNptr->valueChanged = 1;
615 break;
616
617 default: { printf("WARNING: SHOULD NOT BE HERE! %d\n",dataType); }
618 }
619}
620
621void X3D_SF_TO_JS_B(JSContext *cx, void *Data, unsigned datalen, int dataType, int *valueChanged, jsval *newval)
622{
623 //for SM_method() == 2
624 // this copies pointers rather than deep copying values
625 jsval rval;
626
627 /* NOTE - caller is (eventually) a class constructor, no need to BeginRequest */
628
629 #ifdef JSVRMLCLASSESVERBOSE
630 printf ("calling X3D_SF_TO_JS on type %s, newval %u\n",FIELDTYPES[dataType],*newval);
631 #endif
632
633 //if (!JSVAL_IS_OBJECT(*newval)) {
634 /* find a script to create the correct object */
635 JSObject *newobj;
636 AnyNative *ptr;
637 switch (dataType) {
638 case FIELDTYPE_SFVec3f:
639 newobj = JS_NewObject(cx,&SFVec3fClass,NULL,NULL); break;
640 case FIELDTYPE_SFVec3d:
641 newobj = JS_NewObject(cx,&SFVec3dClass,NULL,NULL); break;
642 case FIELDTYPE_SFColor:
643 newobj = JS_NewObject(cx,&SFColorClass,NULL,NULL); break;
644 case FIELDTYPE_SFNode:
645 newobj = JS_NewObject(cx,&SFNodeClass,NULL,NULL); break;
646 case FIELDTYPE_SFVec2f:
647 newobj = JS_NewObject(cx,&SFVec2fClass,NULL,NULL); break;
648 case FIELDTYPE_SFRotation:
649 newobj = JS_NewObject(cx,&SFRotationClass,NULL,NULL); break;
650 default: printf ("invalid type in X3D_SF_TO_JS\n"); return;
651 }
652
653 /* create the object */
654 //set private
655 if ((ptr = (AnyNative *) AnyNativeNew(dataType,(union anyVrml*)Data,valueChanged)) == NULL) {
656 printf( "AnyNativeNew failed in X3D_MF_TO_SF_B.\n");
657 return;
658 }
659
660 if (!JS_SetPrivateFw(cx, newobj, ptr)) {
661 printf( "JS_SetPrivate failed in X3D_MF_TO_SF_B.\n");
662 return;
663 }
664
665 /* this is the return pointer, lets save it right now */
666 *newval = OBJECT_TO_JSVAL(newobj);
667 #ifdef JSVRMLCLASSESVERBOSE
668 printf ("X3D_SF_TO_JS_B, so, newval now is %u\n",*newval);
669 #endif
670
671 //}
672}
673void X3D_SF_TO_JS_BNode(JSContext *cx, void *Data, unsigned datalen, int dataType, int *valueChanged, jsval *newval)
674{
675 union anyVrml *any = (union anyVrml *)Data;
676 if(any->sfnode == NULL){
677 *newval = JSVAL_NULL;
678 }else{
679 X3D_SF_TO_JS_B(cx, Data, datalen, dataType, valueChanged, newval);
680 }
681
682}
683/* make an MF type from the X3D node. This can be fairly slow... */
684void X3D_MF_TO_JS(JSContext *cx, JSObject *obj, void *Data, int dataType, jsval *newval, char *fieldName) {
685 int i;
686 jsval rval;
687 const char *script = NULL;
688 struct Multi_Int32 *MIptr;
689 struct Multi_Float *MFptr;
690 struct Multi_Time *MTptr;
691 jsval fieldNameAsJSVAL;
692
693 /* NOTE - caller is (eventually) a JS class constructor, no need to BeginRequest */
694
695 /* so, obj should be an __SFNode_proto, and newval should be a __MFString_proto (or whatever) */
696
697 #ifdef JSVRMLCLASSESVERBOSE
698 printf ("calling X3D_MF_TO_JS on type %s\n",FIELDTYPES[dataType]);
699 printf ("X3D_MF_TO_JS, we have object %u, newval %u\n",obj,*newval);
700 printf ("X3D_MF_TO_JS, obj is an:\n");
701 if (JSVAL_IS_OBJECT(OBJECT_TO_JSVAL(obj))) { printf ("X3D_MF_TO_JS - obj is an OBJECT\n");
702 printf ("X3D_MF_TO_JS, obj is a "); printJSNodeType(cx,obj);
703 }
704 if (JSVAL_IS_PRIMITIVE(OBJECT_TO_JSVAL(obj))) { printf ("X3D_MF_TO_JS - obj is an PRIMITIVE\n"); }
705 printf ("X3D_MF_TO_JS, vp is an:\n");
706 if (JSVAL_IS_OBJECT(*newval)) { printf ("X3D_MF_TO_JS - newval is an OBJECT\n");
707 printf ("X3D_MF_TO_JS, newval is a "); printJSNodeType(cx,JSVAL_TO_OBJECT(*newval));
708 }
709 if (JSVAL_IS_PRIMITIVE(*newval)) { printf ("X3D_MF_TO_JS - newval is an PRIMITIVE\n"); }
710 #endif
711
712
713#ifdef JSVRMLCLASSESVERBOSE
714 printf ("X3D_MF_TO_JS - is this already expanded? \n");
715 {
716 SFNodeNative *VNptr;
717
718 /* get a pointer to the internal private data */
719 if ((VNptr = JS_GetPrivate(cx, obj)) == NULL) {
720 printf( "JS_GetPrivate failed in X3D_MF_TO_JS.\n");
721 return;
722 }
723 if (VNptr->fieldsExpanded) printf ("FIELDS EXPANDED\n");
724 else printf ("FIELDS NOT EXPANDED\n");
725 }
726#endif
727
728
729 //if (!JSVAL_IS_OBJECT(*newval)) {
730 if (!(*newval).isObject()) {
731 #ifdef JSVRMLCLASSESVERBOSE
732 printf ("X3D_MF_TO_JS - have to create empty MF type \n");
733 #endif
734
735 /* find a script to create the correct object */
736 switch (dataType) {
737 case FIELDTYPE_MFString: script = "new MFString()"; break;
738 case FIELDTYPE_MFFloat: script = "new MFFloat()"; break;
739 case FIELDTYPE_MFTime: script = "new MFTime()"; break;
740 case FIELDTYPE_MFInt32: script = "new MFInt32()"; break;
741 case FIELDTYPE_SFImage: script = "new SFImage()"; break;
742 case FIELDTYPE_MFVec3f: script = "new MFVec3f()"; break;
743 case FIELDTYPE_MFColor: script = "new MFColor()"; break;
744 case FIELDTYPE_MFNode: script = "new MFNode()"; break;
745 case FIELDTYPE_MFVec2f: script = "new MFVec2f()"; break;
746 case FIELDTYPE_MFRotation: script = "new MFRotation()"; break;
747 default: printf ("invalid type in X3D_MF_TO_JS\n"); return;
748 }
749
750 if (!JS_EvaluateScript(cx, obj, script, (int) strlen(script), FNAME_STUB, LINENO_STUB, &rval)) {
751 printf ("error creating the new object in X3D_MF_TO_JS\n");
752 return;
753 }
754
755 /* this is the return pointer, lets save it right now */
756 *newval = rval;
757 }
758
759 #ifdef JSVRMLCLASSESVERBOSE
760 printf ("setting parent for %u to %u\n", *newval, obj);
761 #endif
762
763 /* ok - if we are setting an MF* field by a thing like myField[10] = new String(); the
764 set method does not really get called. So, we go up the parental chain until we get
765 either no parent, or a SFNode. If we get a SFNode, we call the "save this" function
766 so that the X3D scene graph gets the updated array value. To make a long story short,
767 here's the call to set the parent for the above. */
768 if (!JS_SetParent (cx, JSVAL_TO_OBJECT(*newval), obj)) {
769 printf ("X3D_MF_TO_JS - can not set parent!\n");
770 }
771
772 #ifdef JSVRMLCLASSESVERBOSE
773 printf ("telling %u that it is a child \"%s\" of parent %u\n",*newval, fieldName, obj);
774 #endif
775
776 fieldNameAsJSVAL = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,fieldName));
777
778 if (!JS_DefineProperty(cx, JSVAL_TO_OBJECT(*newval), "_parentField", fieldNameAsJSVAL,
779 JS_GET_PROPERTY_STUB, JS_SET_PROPERTY_STUB5, JSPROP_READONLY)) {
780 printf("JS_DefineProperty failed for \"%s\" in X3D_MF_TO_JS.\n", fieldName);
781 return;
782 }
783
784
785 #ifdef JSVRMLCLASSESVERBOSE
786 printf ("X3D_MF_TO_JS - object is %u, copying over data\n",*newval);
787 #endif
788
789
790 /* copy over the data from the X3D node to this new Javascript object */
791 switch (dataType) {
792 case FIELDTYPE_MFInt32:
793 MIptr = (struct Multi_Int32*) Data;
794 for (i=0; i<MIptr->n; i++) {
795 if (!JS_DefineElement(cx, JSVAL_TO_OBJECT(*newval), (jsint) i, INT_TO_JSVAL(MIptr->p[i]),
796 JS_GET_PROPERTY_STUB, setSF_in_MF, JSPROP_ENUMERATE)) {
797 printf( "JS_DefineElement failed for arg %u in MFInt32Constr.\n", i);
798 return;
799 }
800 }
801 break;
802 case FIELDTYPE_MFFloat:
803 MFptr = (struct Multi_Float*) Data;
804 for (i=0; i<MFptr->n; i++) {
805 if (!JS_DefineElement(cx, JSVAL_TO_OBJECT(*newval), (jsint) i, INT_TO_JSVAL(MFptr->p[i]),
806 JS_GET_PROPERTY_STUB, setSF_in_MF, JSPROP_ENUMERATE)) {
807 printf( "JS_DefineElement failed for arg %u in MFFloatConstr.\n", i);
808 return;
809 }
810 }
811 break;
812 case FIELDTYPE_MFTime:
813 MTptr = (struct Multi_Time*) Data;
814 for (i=0; i<MTptr->n; i++) {
815 if (!JS_DefineElement(cx, JSVAL_TO_OBJECT(*newval), (jsint) i, INT_TO_JSVAL(MTptr->p[i]),
816 JS_GET_PROPERTY_STUB, setSF_in_MF, JSPROP_ENUMERATE)) {
817 printf( "JS_DefineElement failed for arg %u in MFTimeConstr.\n", i);
818 return;
819 }
820 }
821 break;
822 case FIELDTYPE_MFColor:
823 case FIELDTYPE_MFVec3f: {
824 struct Multi_Vec3f* MCptr;
825 char newline[100];
826 jsval xf;
827
828 MCptr = (struct Multi_Vec3f *) Data;
829 for (i=0; i<MCptr->n; i++) {
830 if (dataType == FIELDTYPE_MFColor)
831 sprintf (newline,"new SFColor(%f, %f, %f)", MCptr->p[i].c[0], MCptr->p[i].c[1], MCptr->p[i].c[2]);
832 else
833 sprintf (newline,"new SFColor(%f, %f, %f)", MCptr->p[i].c[0], MCptr->p[i].c[1], MCptr->p[i].c[2]);
834 if (!JS_EvaluateScript(cx, JSVAL_TO_OBJECT(*newval), newline, (int) strlen(newline), FNAME_STUB, LINENO_STUB, &xf)) {
835 printf ("error creating the new object in X3D_MF_TO_JS string :%s:\n",newline);
836 return;
837 }
838 if (!JS_DefineElement(cx, JSVAL_TO_OBJECT(*newval), (jsint) i, xf,
839 JS_GET_PROPERTY_STUB, setSF_in_MF, JSPROP_ENUMERATE)) {
840 printf( "JS_DefineElement failed for arg %u .\n", i);
841 return;
842 }
843 }
844 } break;
845
846 case FIELDTYPE_MFVec2f: {
847 struct Multi_Vec2f* MCptr;
848 char newline[100];
849 jsval xf;
850
851 MCptr = (struct Multi_Vec2f *) Data;
852 for (i=0; i<MCptr->n; i++) {
853 sprintf (newline,"new SFVec2f(%f, %f)", MCptr->p[i].c[0], MCptr->p[i].c[1]);
854 if (!JS_EvaluateScript(cx, JSVAL_TO_OBJECT(*newval), newline, (int) strlen(newline), FNAME_STUB, LINENO_STUB, &xf)) {
855 printf ("error creating the new object in X3D_MF_TO_JS string :%s:\n",newline);
856 return;
857 }
858 if (!JS_DefineElement(cx, JSVAL_TO_OBJECT(*newval), (jsint) i, xf,
859 JS_GET_PROPERTY_STUB, setSF_in_MF, JSPROP_ENUMERATE)) {
860 printf( "JS_DefineElement failed for arg %u .\n", i);
861 return;
862 }
863 }
864 } break;
865 case FIELDTYPE_MFRotation: {
866 struct Multi_Rotation* MCptr;
867 char newline[100];
868 jsval xf;
869
870 MCptr = (struct Multi_Rotation*) Data;
871 for (i=0; i<MCptr->n; i++) {
872 sprintf (newline,"new SFRotation(%f, %f, %f, %f)", MCptr->p[i].c[0], MCptr->p[i].c[1], MCptr->p[i].c[2], MCptr->p[i].c[3]);
873 if (!JS_EvaluateScript(cx, JSVAL_TO_OBJECT(*newval), newline, (int) strlen(newline), FNAME_STUB, LINENO_STUB, &xf)) {
874 printf ("error creating the new object in X3D_MF_TO_JS string :%s:\n",newline);
875 return;
876 }
877 if (!JS_DefineElement(cx, JSVAL_TO_OBJECT(*newval), (jsint) i, xf,
878 JS_GET_PROPERTY_STUB, setSF_in_MF, JSPROP_ENUMERATE)) {
879 printf( "JS_DefineElement failed for arg %u .\n", i);
880 return;
881 }
882 }
883 } break;
884
885 case FIELDTYPE_MFNode: {
886 struct Multi_Node* MCptr;
887 char newline[100];
888 jsval xf;
889
890 MCptr = (struct Multi_Node *) Data;
891
892 for (i=0; i<MCptr->n; i++) {
893 /* purge out null nodes */
894 if (MCptr->p[i] != NULL) {
895 sprintf (newline,"new SFNode(\"%p\")", MCptr->p[i]);
896
897 if (!JS_EvaluateScript(cx, JSVAL_TO_OBJECT(*newval), newline, (int) strlen(newline), FNAME_STUB, LINENO_STUB, &xf)) {
898 printf ("error creating the new object in X3D_MF_TO_JS string :%s:\n",newline);
899 return;
900 }
901 if (!JS_DefineElement(cx, JSVAL_TO_OBJECT(*newval), (jsint) i, xf,
902 JS_GET_PROPERTY_STUB, setSF_in_MF, JSPROP_ENUMERATE)) {
903 printf( "JS_DefineElement failed for arg %u .\n", i);
904 return;
905 }
906 } else {
907 /* printf ("X3DMF, ignoring NULL node here \n"); */
908 }
909 }
910 } break;
911
912
913 case FIELDTYPE_MFString: {
914 struct Multi_String* MCptr;
915 char newline[100];
916 jsval xf;
917
918 MCptr = (struct Multi_String *) Data;
919 for (i=0; i<MCptr->n; i++) {
920 #ifdef JSVRMLCLASSESVERBOSE
921 printf ("X3D_MF_TO_JS, working on %d of %d, p %u\n",i, MCptr->n, MCptr->p[i]);
922 #endif
923
924 if (((struct Uni_String *)MCptr->p[i])->strptr != NULL)
925 sprintf (newline,"new String('%s')", ((struct Uni_String *)MCptr->p[i])->strptr);
926 else sprintf (newline,"new String('(NULL)')");
927
928 #ifdef JSVRMLCLASSESVERBOSE
929 printf ("X3D_MF_TO_JS, we have a new script to evaluate: \"%s\"\n",newline);
930 #endif
931
932 if (!JS_EvaluateScript(cx, JSVAL_TO_OBJECT(*newval), newline, (int) strlen(newline), FNAME_STUB, LINENO_STUB, &xf)) {
933 printf ("error creating the new object in X3D_MF_TO_JS string :%s:\n",newline);
934 return;
935 }
936 if (!JS_DefineElement(cx, JSVAL_TO_OBJECT(*newval), (jsint) i, xf,
937 JS_GET_PROPERTY_STUB, setSF_in_MF, JSPROP_ENUMERATE)) {
938 printf( "JS_DefineElement failed for arg %u .\n", i);
939 return;
940 }
941 }
942 } break;
943
944 case FIELDTYPE_SFImage: {
945 struct Multi_Int32* MCptr;
946 char newline[10000];
947 jsval xf;
948
949 /* look at the PixelTexture internals, an image is just a bunch of Int32s */
950 MCptr = (struct Multi_Int32 *) Data;
951 sprintf (newline, "new SFImage(");
952
953 for (i=0; i<MCptr->n; i++) {
954 char sl[20];
955 sprintf (sl,"0x%x ", MCptr->p[i]);
956 strcat (newline,sl);
957
958 if (i != ((MCptr->n)-1)) strcat (newline,",");
959 if (i == 2) strcat (newline, " new MFInt32(");
960
961 }
962 strcat (newline, "))");
963
964 if (!JS_EvaluateScript(cx, JSVAL_TO_OBJECT(*newval), newline, (int) strlen(newline), FNAME_STUB, LINENO_STUB, &xf)) {
965 printf ("error creating the new object in X3D_MF_TO_JS string :%s:\n",newline);
966 return;
967 }
968 *newval = xf; /* save this version */
969 } break;
970 default: { printf("WARNING: SHOULD NOT BE HERE! %d\n",dataType); }
971 }
972
973 #ifdef JSVRMLCLASSESVERBOSE
974 printf ("returning from X3D_MF_TO_JS\n");
975 #endif
976}
977
978
979void X3D_MF_TO_JS_B(JSContext *cx, union anyVrml* Data, int dataType, int *valueChanged, jsval *newval) {
980 // for SM_method == 2, simplifies MF handling
981 //1. create object, with MF getter/setter that looks for [i] or ["length"]
982 //2. add the AnyNative (pass in more details please)
983 //3. set as return value
984
985 jsval rval;
986 char *script = NULL;
987 AnyNative *ptr;
988 JSObject *newobj = NULL;
989
990
991// if (!JSVAL_IS_OBJECT(*newval)) { //don't know what this guards against
992 switch (dataType) {
993 case FIELDTYPE_MFString:
994 newobj = JS_NewObject(cx,&MFStringClass,NULL,NULL); break;
995 case FIELDTYPE_MFFloat:
996 newobj = JS_NewObject(cx,&MFFloatClass,NULL,NULL); break;
997 case FIELDTYPE_MFTime:
998 newobj = JS_NewObject(cx,&MFTimeClass,NULL,NULL); break;
999 case FIELDTYPE_MFInt32:
1000 newobj = JS_NewObject(cx,&MFInt32Class,NULL,NULL); break;
1001 case FIELDTYPE_SFImage:
1002 newobj = JS_NewObject(cx,&SFImageClass,NULL,NULL); break;
1003 case FIELDTYPE_MFVec3f:
1004 newobj = JS_NewObject(cx,&MFVec3fClass,NULL,NULL); break;
1005 case FIELDTYPE_MFColor:
1006 newobj = JS_NewObject(cx,&MFColorClass,NULL,NULL); break;
1007 case FIELDTYPE_MFNode:
1008 newobj = JS_NewObject(cx,&MFNodeClass,NULL,NULL); break;
1009 case FIELDTYPE_MFVec2f:
1010 newobj = JS_NewObject(cx,&MFVec2fClass,NULL,NULL); break;
1011 case FIELDTYPE_MFRotation:
1012 newobj = JS_NewObject(cx,&MFRotationClass,NULL,NULL); break;
1013 default: printf ("invalid type in X3D_MF_TO_JS\n"); return;
1014 }
1015 //set private
1016 if ((ptr = (AnyNative *) AnyNativeNew(dataType,Data,valueChanged)) == NULL) {
1017 printf( "AnyNativeNew failed in X3D_MF_TO_SF_B.\n");
1018 return;
1019 }
1020
1021 if (!JS_SetPrivateFw(cx, newobj, ptr)) {
1022 printf( "JS_SetPrivate failed in X3D_MF_TO_SF_B.\n");
1023 return;
1024 }
1025
1026 /* this is the return pointer, lets save it right now */
1027 *newval = OBJECT_TO_JSVAL(newobj);
1028 if(0){
1029 //check if ptr is on object constructed from newval, or is it just on the object?
1030 AnyNative *ptr2;
1031 JSObject *obj2 = JSVAL_TO_OBJECT(*newval);
1032 if( (ptr2 = (AnyNative*)JS_GetPrivateFw(cx,obj2)) == NULL){
1033 printf("native pointer doesn't survive reduction to jsval\n");
1034 }else{
1035 printf("OK native pointer survives reduction to jsval");
1036 printf("ptr->v->mf.n=%d\n",ptr2->v->mfbool.n);
1037 }
1038 }
1039 return;
1040
1041}
1042
1043
1044
1045void
1046reportWarningsOn() {
1047 ppjsUtils p = (ppjsUtils)gglobal()->jsUtils.prv;
1048 p->reportWarnings = JS_TRUE;
1049}
1050
1051
1052void
1053reportWarningsOff() {
1054 ppjsUtils p = (ppjsUtils)gglobal()->jsUtils.prv;
1055 p->reportWarnings = JS_FALSE;
1056}
1057
1058
1059void
1060errorReporter(JSContext *context, const char *message, JSErrorReport *report)
1061{
1062 char *errorReport = 0;
1063 int len = 0, charPtrSize = (int) sizeof(char *);
1064 ppjsUtils p = (ppjsUtils)gglobal()->jsUtils.prv;
1065
1066//printf ("*** errorReporter ***\n");
1067
1068 if (!report) {
1069 fprintf(stderr, "%s\n", message);
1070 return;
1071 }
1072
1073 /* Conditionally ignore reported warnings. */
1074 if (JSREPORT_IS_WARNING(report->flags) && !p->reportWarnings) {
1075 return;
1076 }
1077
1078 if (report->filename == NULL) {
1079 len = (int) (strlen(message) + 1);
1080 } else {
1081 len = (int) ((strlen(report->filename) + 1) + (strlen(message) + 1));
1082 }
1083
1084 errorReport = (char *) JS_malloc(context, (len + STRING) * charPtrSize);
1085 if (!errorReport) {
1086 return;
1087 }
1088
1089
1090 if (JSREPORT_IS_WARNING(report->flags)) {
1091 sprintf(errorReport,
1092 "%swarning in %s at line %u:\n\t%s\n",
1093 JSREPORT_IS_STRICT(report->flags) ? "strict " : "",
1094 report->filename ? report->filename : "",
1095 report->lineno ? report->lineno : 0,
1096 message ? message : "No message.");
1097 } else {
1098 sprintf(errorReport,
1099 "error in %s at line %u:\n\t%s\n",
1100 report->filename ? report->filename : "",
1101 report->lineno ? report->lineno : 0,
1102 message ? message : "No message.");
1103 }
1104
1105 fprintf(stderr, "Javascript -- %s", errorReport);
1106
1107 JS_free(context, errorReport);
1108}
1109
1110
1111/* SFNode - find the fieldOffset pointer for this field within this node */
1112static int *getFOP (struct X3D_Node *node, const char *str) {
1113 int *fieldOffsetsPtr;
1114
1115
1116 if (node != NULL) {
1117 #ifdef JSVRMLCLASSESVERBOSE
1118 printf ("...getFOP... it is a %s\n",stringNodeType(node->_nodeType));
1119 #endif
1120
1121 fieldOffsetsPtr = (int *) NODE_OFFSETS[node->_nodeType];
1122 /*go thru all field*/
1123 /* what we have is a list of 4 numbers, representing:
1124 FIELDNAMES__parentResource, offsetof (struct X3D_Anchor, __parenturl), FIELDTYPE_SFString, KW_initializeOnly,
1125 */
1126
1127 while (*fieldOffsetsPtr != -1) {
1128 #ifdef JSVRMLCLASSESVERBOSE
1129 printf ("getFOP, looking at field %s type %s to match %s\n",FIELDNAMES[*fieldOffsetsPtr],FIELDTYPES[*(fieldOffsetsPtr+2)],str);
1130 #endif
1131
1132 /* skip any fieldNames starting with an underscore, as these are "internal" ones */
1133 /* There is in fact nothing in this function that actually enforces this, which is good!! */
1134 if (strcmp(str,FIELDNAMES[*fieldOffsetsPtr]) == 0) {
1135 #ifdef JSVRMLCLASSESVERBOSE
1136 printf ("getFOP, found entry for %s, it is %u (%p)\n",str,fieldOffsetsPtr,fieldOffsetsPtr);
1137 #endif
1138 return fieldOffsetsPtr;
1139 }
1140 fieldOffsetsPtr += FIELDOFFSET_LENGTH;
1141 }
1142
1143 /* failed to find field?? */
1144 #if TRACK_FIFO_MSG
1145 printf ("getFOP, could not find field \"%s\" in nodeType \"%s\"\n", str, stringNodeType(node->_nodeType));
1146 #endif
1147 } else {
1148 printf ("getFOP, passed in X3D node was NULL!\n");
1149 }
1150 return NULL;
1151}
1152
1153
1154/* getter for SFNode accesses */
1155static JSBool getSFNodeField(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JS::MutableHandle<JS::Value> hvp){
1156 JSObject *obj = *hobj.address();
1157 jsid id = *hiid.address();
1158 jsval *vp = hvp.address();
1159
1160 //JSString *_idStr;
1161 char *_id_c;
1162 SFNodeNative *ptr;
1163 int *fieldOffsetsPtr;
1164 struct X3D_Node *node;
1165
1166
1167 _id_c = JS_EncodeString(cx,JSID_TO_STRING(id));
1168
1169 if ((ptr = (SFNodeNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1170 printf( "JS_GetPrivate failed in getSFNodeField.\n");
1171 JS_free(cx,_id_c);
1172 return JS_FALSE;
1173 }
1174 node = X3D_NODE(ptr->handle);
1175
1176 if (node == NULL) {
1177 printf ("getSFNodeField, can not set field \"%s\", NODE is NULL!\n",_id_c);
1178 JS_free(cx,_id_c);
1179 return JS_FALSE;
1180 }
1181#if USE_OSC
1182 /* Is this a ring buffer item? */
1183 fieldOffsetsPtr = getFOP(ptr->handle,"FIFOsize");
1184 if (fieldOffsetsPtr == NULL) {
1185 #if TRACK_FIFO_MSG
1186 printf("getSFNodeField : This is not a ringBuffer type\n");
1187 #endif
1188 } else {
1189 struct X3D_OSC_Sensor *OSCnode ;
1190 OSCnode = (struct X3D_OSC_Sensor *) X3D_NODE(ptr->handle);
1191 char *_id_buffer_c = NULL ;
1192 RingBuffer * buffer ;
1193
1194 int iVal ;
1195 float fVal ;
1196 char * strPtr ;
1197
1198 #if TRACK_FIFO_MSG
1199 printf("getSFNodeField : This could be a ringBuffer type (found FIFOsize)\n");
1200 #endif
1201
1202 if (0 == strcmp(_id_c,"int32Inp")) {
1203 #if TRACK_FIFO_MSG
1204 printf("getSFNodeField %d : ptr->handle=%p (which corresponds to realnode in scenegraph/Component_Networking.c,314) node=%p see X3D_NODE(ptr->handle)\n",__LINE__,ptr->handle , node);
1205 #endif
1206 /* fieldOffsetsPtr = getFOP(ptr->handle,_id_buffer_c); */
1207 buffer = (RingBuffer *) OSCnode->_int32InpFIFO ;
1208 #if TRACK_FIFO_MSG
1209 printf("getSFNodeField %d : buffer=%p\n",__LINE__,buffer) ;
1210 #endif
1211
1212 if (!RingBuffer_testEmpty(buffer)) {
1213 _id_buffer_c = "_int32InpFIFO" ;
1214 iVal = RingBuffer_pullUnion(buffer)->i ;
1215 #if TRACK_FIFO_MSG
1216 printf("getSFNodeField %d : iVal=%d\n",__LINE__,iVal);
1217 #endif
1218
1219 *vp = INT_TO_JSVAL(iVal) ;
1220 return JS_TRUE;
1221 } else {
1222 #if TRACK_FIFO_MSG
1223 printf("but the buffer is empty\n") ;
1224 #endif
1225 }
1226 } else if (0 == strcmp(_id_c,"floatInp")) {
1227 #if TRACK_FIFO_MSG
1228 printf("getSFNodeField %d : ptr->handle=%p (which corresponds to realnode in scenegraph/Component_Networking.c,314) node=%p see X3D_NODE(ptr->handle)\n",__LINE__,ptr->handle , node);
1229 #endif
1230 buffer = (RingBuffer *) OSCnode->_floatInpFIFO ;
1231 #if TRACK_FIFO_MSG
1232 printf("getSFNodeField %d : buffer=%p\n",__LINE__,buffer) ;
1233 #endif
1234
1235 if (!RingBuffer_testEmpty(buffer)) {
1236 _id_buffer_c = "_floatInpFIFO" ;
1237 fVal = RingBuffer_pullUnion(buffer)->f ;
1238 #if TRACK_FIFO_MSG
1239 printf("getSFNodeField %d : fVal=%d\n",__LINE__,fVal);
1240 #endif
1241
1242 JS_NewNumberValue(cx,(double)fVal,vp);
1243 return JS_TRUE;
1244 } else {
1245 #if TRACK_FIFO_MSG
1246 printf("but the buffer is empty\n") ;
1247 #endif
1248 }
1249 } else if (0 == strcmp(_id_c,"stringInp")) {
1250 #if TRACK_FIFO_MSG
1251 printf("getSFNodeField %d : ptr->handle=%p (which corresponds to realnode in scenegraph/Component_Networking.c,314) node=%p see X3D_NODE(ptr->handle)\n",__LINE__,ptr->handle , node);
1252 #endif
1253 buffer = (RingBuffer *) OSCnode->_stringInpFIFO ;
1254 #if TRACK_FIFO_MSG
1255 printf("getSFNodeField %d : buffer=%p\n",__LINE__,buffer) ;
1256 #endif
1257
1258 if (!RingBuffer_testEmpty(buffer)) {
1259 _id_buffer_c = "_stringInpFIFO" ;
1260 strPtr = (char *) RingBuffer_pullUnion(buffer)->p ;
1261 #if TRACK_FIFO_MSG
1262 printf("getSFNodeField %d : strPtr=%s\n",__LINE__,strPtr);
1263 #endif
1264
1265 *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,strPtr));
1266 return JS_TRUE;
1267 } else {
1268 #if TRACK_FIFO_MSG
1269 printf("but the buffer is empty\n") ;
1270 #endif
1271 }
1272 } else {
1273 #if TRACK_FIFO_MSG
1274 printf("but this variable itself (%s) is not a ring buffer item\n",_id_c) ;
1275 #endif
1276 }
1277 }
1278#endif
1279
1280 /* get the table entry giving the type, offset, etc. of this field in this node */
1281 fieldOffsetsPtr = getFOP(ptr->handle,_id_c);
1282 if (fieldOffsetsPtr == NULL) {
1283 JS_free(cx,_id_c);
1284 return JS_FALSE;
1285 }
1286 JS_free(cx,_id_c); /* _id_c is not used beyond this point in this fn */
1287
1288 /* now, we have an X3D node, offset, type, etc. Just get the value from memory, and move
1289 it to javascript. Kind of like: void getField_ToJavascript (int num, int fromoffset)
1290 for Routing. */
1291
1292 /* fieldOffsetsPtr points to a 4-number line like:
1293 FIELDNAMES__parentResource, offsetof (struct X3D_Anchor, __parenturl), FIELDTYPE_SFString, KW_initializeOnly */
1294 switch (*(fieldOffsetsPtr+2)) {
1295 case FIELDTYPE_SFBool:
1296 case FIELDTYPE_SFFloat:
1297 case FIELDTYPE_SFTime:
1298 case FIELDTYPE_SFDouble:
1299 case FIELDTYPE_SFInt32:
1300 case FIELDTYPE_SFString:
1301 X3D_ECMA_TO_JS(cx, offsetPointer_deref (void *, node, *(fieldOffsetsPtr+1)),
1302 returnElementLength(*(fieldOffsetsPtr+2)), *(fieldOffsetsPtr+2), vp);
1303 break;
1304 case FIELDTYPE_SFColor:
1305 case FIELDTYPE_SFNode:
1306 case FIELDTYPE_SFVec2f:
1307 case FIELDTYPE_SFVec3f:
1308 case FIELDTYPE_SFVec3d:
1309 case FIELDTYPE_SFRotation:
1310 X3D_SF_TO_JS(cx, obj, offsetPointer_deref (void *, node, *(fieldOffsetsPtr+1)),
1311 returnElementLength(*(fieldOffsetsPtr+2)) * returnElementRowSize(*(fieldOffsetsPtr+2)) , *(fieldOffsetsPtr+2), vp);
1312 break;
1313 case FIELDTYPE_MFColor:
1314 case FIELDTYPE_MFVec3f:
1315 case FIELDTYPE_MFVec2f:
1316 case FIELDTYPE_MFFloat:
1317 case FIELDTYPE_MFTime:
1318 case FIELDTYPE_MFInt32:
1319 case FIELDTYPE_MFString:
1320 case FIELDTYPE_MFNode:
1321 case FIELDTYPE_MFRotation:
1322 case FIELDTYPE_SFImage:
1323 X3D_MF_TO_JS(cx, obj, offsetPointer_deref (void *, node, *(fieldOffsetsPtr+1)), *(fieldOffsetsPtr+2), vp,
1324 (char *)FIELDNAMES[*(fieldOffsetsPtr+0)]);
1325 break;
1326 default: printf ("unhandled type FIELDTYPE_ %d in getSFNodeField\n", *(fieldOffsetsPtr+2)) ;
1327 return JS_FALSE;
1328 }
1329
1330 #ifdef JSVRMLCLASSESVERBOSE
1331 printf ("end of getSFNodeField\n");
1332 #endif
1333
1334 return JS_TRUE;
1335}
1336
1337/* setter for SFNode accesses */
1338JSBool setSFNodeField (JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
1339 JSObject *obj = *hobj.address();
1340 jsid id = *hiid.address();
1341 jsval *vp = hvp.address();
1342
1343 char *_id_c;
1344 SFNodeNative *ptr;
1345 int *fieldOffsetsPtr;
1346 struct X3D_Node *node;
1347
1348 /* get the id field... */
1349
1350 _id_c = JS_EncodeString(cx,JSID_TO_STRING(id));
1351
1352
1353 #ifdef JSVRMLCLASSESVERBOSE
1354 printf ("\nsetSFNodeField called on name %s object %u, jsval %u\n",_id_c, obj, *vp);
1355 #endif
1356
1357 /* get the private data. This will contain a pointer into the FreeWRL scenegraph */
1358 if ((ptr = (SFNodeNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1359 printf( "JS_GetPrivate failed in setSFNodeField.\n");
1360 JS_free(cx,_id_c);
1361 return JS_FALSE;
1362 }
1363
1364 /* get the X3D Scenegraph node pointer from this Javascript SFNode node */
1365 node = (struct X3D_Node *) ptr->handle;
1366
1367 if (node == NULL) {
1368 printf ("setSFNodeField, can not set field \"%s\", NODE is NULL!\n",_id_c);
1369 JS_free(cx,_id_c);
1370 return JS_FALSE;
1371 }
1372
1373
1374 #if USE_OSC
1375 #ifdef JSVRMLCLASSESVERBOSE
1376 /* Is this a ring buffer item? */
1377 fieldOffsetsPtr = getFOP(ptr->handle,"FIFOsize");
1378 #if TRACK_FIFO_MSG
1379 if (fieldOffsetsPtr == NULL) {
1380 printf("setSFNodeField : This is not a ringBuffer type\n");
1381 } else {
1382 printf("setSFNodeField : This is a ringBuffer type\n");
1383 }
1384 #endif
1385 #endif
1386 #endif
1387
1388 /* get the table entry giving the type, offset, etc. of this field in this node */
1389 fieldOffsetsPtr = getFOP(ptr->handle,_id_c);
1390
1391 JS_free(cx,_id_c); /* _id_c is not used beyond this point in this fn */
1392
1393 if (fieldOffsetsPtr == NULL) {
1394 return JS_FALSE;
1395 }
1396
1397 /* now, we have an X3D node, offset, type, etc. Just get the value from Javascript, and move
1398 it to the X3D Scenegraph. */
1399
1400 /* fieldOffsetsPtr points to a 4-number line like:
1401 FIELDNAMES__parentResource, offsetof (struct X3D_Anchor, __parenturl), FIELDTYPE_SFString, KW_initializeOnly */
1402 #ifdef JSVRMLCLASSESVERBOSE
1403 printf ("and a field type of %s\n",FIELDTYPES[*(fieldOffsetsPtr+2)]);
1404 #endif
1405
1406 switch (*(fieldOffsetsPtr+2)) {
1407 case FIELDTYPE_SFBool:
1408 case FIELDTYPE_SFFloat:
1409 case FIELDTYPE_SFTime:
1410 case FIELDTYPE_SFDouble:
1411 case FIELDTYPE_SFInt32:
1412 case FIELDTYPE_SFString:
1413 JS_ECMA_TO_X3D(cx, ((void *)( ((unsigned char *) node) + *(fieldOffsetsPtr+1))),
1414 returnElementLength(*(fieldOffsetsPtr+2)), *(fieldOffsetsPtr+2), vp);
1415 break;
1416 case FIELDTYPE_SFColor:
1417 case FIELDTYPE_SFNode:
1418 case FIELDTYPE_SFVec2f:
1419 case FIELDTYPE_SFVec3f:
1420 case FIELDTYPE_SFVec3d:
1421 case FIELDTYPE_SFRotation:
1422 JS_SF_TO_X3D(cx, ((void *)( ((unsigned char *) node) + *(fieldOffsetsPtr+1))),
1423 returnElementLength(*(fieldOffsetsPtr+2)) * returnElementRowSize(*(fieldOffsetsPtr+2)) , *(fieldOffsetsPtr+2), vp);
1424 break;
1425 case FIELDTYPE_MFColor:
1426 case FIELDTYPE_MFVec3f:
1427 case FIELDTYPE_MFVec2f:
1428 case FIELDTYPE_MFFloat:
1429 case FIELDTYPE_MFTime:
1430 case FIELDTYPE_MFInt32:
1431 case FIELDTYPE_MFString:
1432 case FIELDTYPE_MFNode:
1433 case FIELDTYPE_MFRotation:
1434 case FIELDTYPE_SFImage:
1435 JS_MF_TO_X3D(cx, obj, ((void *)( ((unsigned char *) node) + *(fieldOffsetsPtr+1))), *(fieldOffsetsPtr+2), vp);
1436 break;
1437 default: printf ("unhandled type in setSFNodeField\n");
1438 return JS_FALSE;
1439 }
1440
1441 /* tell the X3D Scenegraph that something has changed in Kansas */
1442 update_node(node);
1443
1444 #ifdef JSVRMLCLASSESVERBOSE
1445 printf ("end of setSFNodeField\n");
1446 #endif
1447
1448
1449 return JS_TRUE;
1450}
1451#define UNHIDE_DEFINE_SFNODESPECIFIC 1
1452#ifdef UNHIDE_DEFINE_SFNODESPECIFIC
1453/* for SFNodes, go through and insure that all properties are defined for the specific node type */
1454int JS_DefineSFNodeSpecificProperties (JSContext *context, JSObject *object, struct X3D_Node * ptr) {
1455 int *fieldOffsetsPtr;
1456 jsval rval = INT_TO_JSVAL(0);
1457 uintN attrs = JSPROP_PERMANENT
1458 | JSPROP_ENUMERATE
1459#ifdef JSPROP_EXPORTED
1460 | JSPROP_EXPORTED
1461#endif
1462 /* | JSPROP_INDEX */
1463 ;
1464 char *name;
1465 SFNodeNative *nodeNative;
1466 #ifdef JSVRMLCLASSESVERBOSE
1467 char *nodeName;
1468 struct X3D_Node *confirmNode;
1469 #endif
1470
1471 /* NOTE - caller of this function is a class constructor, no need to worry about Requests */
1472
1473
1474 #ifdef JSVRMLCLASSESVERBOSE
1475 nodeName = parser_getNameFromNode(ptr) ; /* vi +/dump_scene src/lib/scenegraph/GeneratedCode.c */
1476 if (nodeName == NULL) {
1477 printf ("\nStart of JS_DefineSFNodeSpecificProperties for '---' ... working on node %u object %u (%p,%p)\n",ptr,object,ptr,object);
1478 } else {
1479 printf ("\nStart of JS_DefineSFNodeSpecificProperties for '%s' ... working on node %u object %u (%p,%p)\n",nodeName,ptr,object,ptr,object);
1480 confirmNode = parser_getNodeFromName(nodeName);
1481 if (confirmNode == NULL) {
1482 printf("RoundTrip failed : ptr (%p) -> nodeName (%s) -----\n",ptr,nodeName) ;
1483 } else {
1484 printf("RoundTrip OK : ptr (%p) -> nodeName (%s) -> confirmNode (%p)\n",ptr,nodeName,confirmNode) ;
1485 }
1486 }
1487 #endif
1488
1489 if (ptr != NULL) {
1490 #ifdef JSVRMLCLASSESVERBOSE
1491 printf ("...JS_DefineSFNodeSpecificProperties... it is a %s\n",stringNodeType(ptr->_nodeType));
1492 #endif
1493
1494 /* have we already done this for this node? We really do not want to do this again */
1495 if ((nodeNative = (SFNodeNative *)JS_GetPrivateFw(context,object)) == NULL) {
1496 printf ("JS_DefineSFNodeSpecificProperties, can not get private for a SFNode!\n");
1497 return JS_FALSE;
1498 }
1499 if(SM_method() == 0)
1500 if (nodeNative->fieldsExpanded) {
1501 #ifdef JSVRMLCLASSESVERBOSE
1502 printf ("JS_DefineSFNodeSpecificProperties, already done for node\n");
1503 #endif
1504
1505 return JS_TRUE;
1506 }
1507
1508 fieldOffsetsPtr = (int *) NODE_OFFSETS[ptr->_nodeType];
1509 /*go thru all field*/
1510 /* what we have is a list of 4 numbers, representing:
1511 FIELDNAMES__parentResource, offsetof (struct X3D_Anchor, __parenturl), FIELDTYPE_SFString, KW_initializeOnly,
1512 */
1513
1514 while (*fieldOffsetsPtr != -1) {
1515 /* fieldPtr=(char*)structptr+(*(fieldOffsetsPtr+1)); */
1516 #ifdef JSVRMLCLASSESVERBOSE
1517 printf ("looking at field %s type %s\n",FIELDNAMES[*fieldOffsetsPtr],FIELDTYPES[*(fieldOffsetsPtr+2)]);
1518 #endif
1519
1520#if USE_OSC
1521 if( 0 == strcmp("FreeWRL_PROTOInterfaceNodes",FIELDNAMES[*fieldOffsetsPtr])) {
1522
1523 #ifdef JSVRMLCLASSESVERBOSE
1524 printf ("%s:%d Mangle %s before calling JS_DefineProperty ....\n",__FILE__,__LINE__,FIELDNAMES[*fieldOffsetsPtr]);
1525 #endif
1526 int i ;
1527 char *str1, *token;
1528 char *saveptr1 = NULL;
1529
1530 UNUSED(token); // compiler warning mitigation
1531
1532 for (i=0; i < X3D_GROUP(ptr)->FreeWRL_PROTOInterfaceNodes.n; i++) {
1533 rval = INT_TO_JSVAL(*fieldOffsetsPtr);
1534 name = parser_getNameFromNode(X3D_GROUP(ptr)->FreeWRL_PROTOInterfaceNodes.p[i]);
1535
1536 #ifdef JSVRMLCLASSESVERBOSE
1537 dump_scene(stdout,0,X3D_GROUP(ptr)->FreeWRL_PROTOInterfaceNodes.p[i]);
1538 printf ("%s:%d dummy name=%s\n",__FILE__,__LINE__,name);
1539 #endif
1540
1541 str1 = MALLOC(void *, 1+strlen(name));
1542 strcpy(str1,name) ;
1543 /* discard Proto_0xnnnnn_*/
1544 token = strtok_r(str1, "_", &saveptr1);
1545 str1 = NULL;
1546 token = strtok_r(str1, "_", &saveptr1);
1547 name = saveptr1 ;
1548
1549 #ifdef JSVRMLCLASSESVERBOSE
1550 printf ("%s:%d would call JS_DefineProperty on (context=%p, object=%p, name=%s, rval=%p), setting getSFNodeField, setSFNodeField\n",__FILE__,__LINE__,context,object,name,rval);
1551 #endif
1552 if (!JS_DefineProperty(context, object, name, rval, getSFNodeField, setSFNodeField, attrs)) {
1553 printf("JS_DefineProperty failed for \"%s\" in JS_DefineSFNodeSpecificProperties.\n", name);
1554 return JS_FALSE;
1555 }
1556 }
1557 } else if (FIELDNAMES[*fieldOffsetsPtr][0] != '_') {
1558#else
1559 if (FIELDNAMES[*fieldOffsetsPtr][0] != '_') {
1560#endif
1561 /* skip any fieldNames starting with an underscore, as these are "internal" ones */
1562 name = (char *)FIELDNAMES[*fieldOffsetsPtr];
1563 rval = INT_TO_JSVAL(*fieldOffsetsPtr);
1564
1565 /* is this an initializeOnly property? */
1566 /* lets not do this, ok?
1567 if ((*(fieldOffsetsPtr+3)) == KW_initializeOnly) attrs |= JSPROP_READONLY;
1568 */
1569
1570 #ifdef JSVRMLCLASSESVERBOSE
1571 printf ("calling JS_DefineProperty on (context=%p, object=%p, name=%s, rval=%p), setting getSFNodeField, setSFNodeField\n",context,object,name,rval);
1572 #endif
1573
1574 if (!JS_DefineProperty(context, object, name, rval, getSFNodeField, setSFNodeField, attrs)) {
1575 printf("JS_DefineProperty failed for \"%s\" in JS_DefineSFNodeSpecificProperties.\n", name);
1576 return JS_FALSE;
1577 }
1578 }
1579 fieldOffsetsPtr += FIELDOFFSET_LENGTH;
1580 }
1581
1582 /* set a flag indicating that we have been here already */
1583 if(SM_method() == 0)
1584 nodeNative->fieldsExpanded = TRUE;
1585 }
1586 #ifdef JSVRMLCLASSESVERBOSE
1587 printf ("JS_DefineSFNodeSpecificProperties, returning TRUE\n");
1588 #endif
1589
1590 return TRUE;
1591}
1592#endif // UNHIDE_DEFINE_SFNODESPECIFIC
1593
1594/********************************************************************************************/
1595/* new addition April 2009. It was noted that the following code would not send an event to
1596 FreeWRL:
1597#VRML V2.0 utf8
1598 DEF DisplayScript Script {
1599 eventOut MFString display_string
1600
1601 url [ "javascript:
1602 function eventsProcessed () {
1603 display_string[7] = ' ';
1604 }
1605 "]
1606 }
1607
1608
1609Shape {geometry DEF Display Text {}}
1610 ROUTE DisplayScript.display_string TO Display.set_string
1611
1612(it would if the assignment was display_string = new MFString(...) )
1613
1614But, this property check gets called on the equals. Lets figure out how to indicate that the
1615holding object needs to route to FreeWRL... */
1616
1617
1618#define SET_TOUCHED_TYPE_MF_A(thisMFtype,thisSFtype) \
1619 else if (JS_InstanceOf (cx, obj, &thisMFtype##Class, NULL)) {\
1620 jsval mainElement;\
1621 thisSFtype##Native *ptr; \
1622\
1623 if (!JS_GetElement(cx, obj, num, &mainElement)) { \
1624 printf ("JS_GetElement failed for %d in get_valueChanged_flag\n",num); \
1625 return JS_FALSE; \
1626 } \
1627\
1628 if ((ptr = (thisSFtype##Native *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(mainElement))) == NULL) {\
1629 printf( "JS_GetPrivate failed in assignCheck.\n"); \
1630 return JS_FALSE; \
1631 } else { \
1632 /* printf ("got private for MFVec3f, doing it...\n"); */ \
1633 ptr->valueChanged++; \
1634 } \
1635 return JS_TRUE; \
1636 }
1637
1638
1639
1640
1641JSBool js_SetPropertyCheck(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
1642 JSObject *obj = *hobj.address();
1643 jsid iid = *hiid.address();
1644 jsval *vp = hvp.address();
1645
1646 int num=0;
1647
1648 jsval id;
1649 if (!JS_IdToValue(cx,iid,&id)) {
1650 printf("js_SetPropertyCheck: JS_IdToValue failed.\n");
1651 return JS_FALSE;
1652 }
1653
1654 /* lets worry about the MFs containing ECMAs here - MFFloat MFInt32 MFTime MFString MFBool */
1655
1656 if (JS_InstanceOf (cx, obj, &MFStringClass, NULL)) {
1657 SET_MF_ECMA_HAS_CHANGED;
1658 return JS_TRUE;
1659 }
1660 else if (JS_InstanceOf (cx, obj, &MFFloatClass, NULL)) {
1661 SET_MF_ECMA_HAS_CHANGED;
1662 return JS_TRUE;
1663 }
1664 else if (JS_InstanceOf (cx, obj, &MFInt32Class, NULL)) {
1665 SET_MF_ECMA_HAS_CHANGED;
1666 return JS_TRUE;
1667 }
1668
1669#ifdef NEWCLASSES
1670 else if (JS_InstanceOf (cx, obj, &MFBoolClass, NULL)) {
1671 SET_MF_ECMA_HAS_CHANGED;
1672 return JS_TRUE;
1673 }
1674#endif
1675
1676 SET_TOUCHED_TYPE_MF_A(MFRotation,SFRotation)
1677 SET_TOUCHED_TYPE_MF_A(MFNode,SFNode)
1678 SET_TOUCHED_TYPE_MF_A(MFVec2f,SFVec2f)
1679 SET_TOUCHED_TYPE_MF_A(MFVec3f,SFVec3f)
1680 /* SET_TOUCHED_TYPE_MF_A(MFImage,SFImage) */
1681 SET_TOUCHED_TYPE_MF_A(MFColor,SFColor)
1682 /* SET_TOUCHED_TYPE_MF_A(MFColorRGBA,SFColorRGBA) */
1683
1684
1685 #ifdef JSVRMLCLASSESVERBOSE
1686 printf ("this is a class of "); printJSNodeType (cx,obj);
1687 #endif
1688
1689 return JS_TRUE;
1690
1691}
1692
1693/****************************************************************************/
1694
1695JSBool js_GetPropertyDebug (JSContext *context, JSObject *obj, jsid iid, jsval *vp) {
1696
1697 #ifdef JSVRMLCLASSESVERBOSE
1698 char *_id_c = "(no value in string)";
1699 int num;
1700 /* get the id field... */
1701#if JS_VERSION >= 185
1702 jsval id;
1703 if (!JS_IdToValue(context,iid,&id)) {
1704 printf("js_GetPropertyDebug: JS_IdToValue failed -- NOT returning false\n");
1705 }
1706#endif
1707
1708 if (JSVAL_IS_STRING(id)) {
1709#if JS_VERSION < 185
1710 _id_c = JS_GetStringBytes(JSVAL_TO_STRING(id));
1711#else
1712 _id_c = JS_EncodeString(context,JSVAL_TO_STRING(id));
1713#endif
1714 printf ("\n...js_GetPropertyDebug called on string \"%s\" object %u, jsval %lu\n",_id_c, (unsigned int) obj, *vp);
1715#if JS_VERSION >= 185
1716 JS_free(context,_id_c);
1717#endif
1718 } else if (JSVAL_IS_INT(id)) {
1719 num = JSVAL_TO_INT(id);
1720 printf ("\n...js_GetPropertyDebug called on number %d object %u, jsval %lu\n",num, (unsigned int) obj, *vp);
1721 } else {
1722 printf ("\n...js_GetPropertyDebug called on unknown type of object %u, jsval %lu\n", (unsigned int) obj, *vp);
1723 }
1724 #endif
1725 return JS_TRUE;
1726}
1727
1728#ifdef JSVRMLCLASSESVERBOSE
1729#if JS_VERSION < 185
1730void js_SetPropertyDebugWrapped (JSContext *context, JSObject *obj, jsval id, jsval *vp,char *debugString) {
1731#else
1732void js_SetPropertyDebugWrapped (JSContext *context, JSObject *obj, jsid iid, jsval *vp,char *debugString) {
1733#endif
1734 char *_id_c = "(no value in string)";
1735 int num;
1736#if JS_VERSION >= 185
1737 jsval id;
1738 if (!JS_IdToValue(context,iid,&id)) {
1739 printf("js_GetPropertyDebug: JS_IdToValue failed\n");
1740 }
1741#endif
1742
1743 /* get the id field... */
1744 if (JSVAL_IS_STRING(id)) {
1745#if JS_VERSION < 185
1746 _id_c = JS_GetStringBytes(JSVAL_TO_STRING(id));
1747#else
1748 _id_c = JS_EncodeString(context,JSVAL_TO_STRING(id));
1749#endif
1750 printf ("\n...js_SetPropertyDebug%s called on string \"%s\" object %p, jsval %lu\n",debugString,_id_c, obj, *vp);
1751#if JS_VERSION >= 185
1752 JS_free(context,_id_c);
1753#endif
1754 } else if (JSVAL_IS_INT(id)) {
1755 num = JSVAL_TO_INT(id);
1756 printf ("\n...js_SetPropertyDebug%s called on number %d object %p, jsval %lu\n",debugString,num, obj, *vp);
1757 } else {
1758 printf ("\n...js_SetPropertyDebug%s called on unknown type of object %p, jsval %lu\n",debugString, obj, *vp);
1759 }
1760}
1761#endif
1762
1763JSBool js_SetPropertyDebug (JSContext *context, JSObject *obj, jsid id, JSBool strict, jsval *vp) {
1764
1765 #ifdef JSVRMLCLASSESVERBOSE
1766 js_SetPropertyDebugWrapped(context,obj,id,vp,"");
1767 #endif
1768 return JS_TRUE;
1769}
1770
1771JSBool js_SetPropertyDebug1 (JSContext *context, JSObject *obj, jsid id, JSBool strict, jsval *vp) {
1772
1773 #ifdef JSVRMLCLASSESVERBOSE
1774 js_SetPropertyDebugWrapped(context,obj,id,vp,"1");
1775 #endif
1776 return JS_TRUE;
1777}
1778JSBool js_SetPropertyDebug2 (JSContext *context, JSObject *obj, jsid id, JSBool strict, jsval *vp) {
1779
1780 #ifdef JSVRMLCLASSESVERBOSE
1781 js_SetPropertyDebugWrapped(context,obj,id,vp,"2");
1782 #endif
1783 return JS_TRUE;
1784}
1785
1786
1787JSBool js_SetPropertyDebug3 (JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
1788 JSObject *obj = *hobj.address();
1789 jsid id = *hiid.address();
1790 jsval *vp = hvp.address();
1791
1792 #ifdef JSVRMLCLASSESVERBOSE
1793 js_SetPropertyDebugWrapped(cx,obj,id,vp,"3");
1794 #endif
1795 return JS_TRUE;
1796}
1797JSBool js_SetPropertyDebug4 (JSContext *context, JSObject *obj, jsid id, JSBool strict, jsval *vp) {
1798
1799 #ifdef JSVRMLCLASSESVERBOSE
1800 js_SetPropertyDebugWrapped(context,obj,id,vp,"4");
1801 #endif
1802 return JS_TRUE;
1803}
1804
1805JSBool js_SetPropertyDebug5(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
1806 JSObject *obj = *hobj.address();
1807 jsid id = *hiid.address();
1808 jsval *vp = hvp.address();
1809
1810 #ifdef JSVRMLCLASSESVERBOSE
1811 js_SetPropertyDebugWrapped(context,obj,id,vp,"5");
1812 #endif
1813 return JS_TRUE;
1814}
1815
1816JSBool js_SetPropertyDebug6 (JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
1817 JSObject *obj = *hobj.address();
1818 jsid id = *hiid.address();
1819 jsval *vp = hvp.address();
1820 #ifdef JSVRMLCLASSESVERBOSE
1821 js_SetPropertyDebugWrapped(context,obj,id,vp,"6");
1822 #endif
1823 return JS_TRUE;
1824}
1825JSBool js_SetPropertyDebug7 (JSContext *context, JSObject *obj, jsid id, JSBool strict, jsval *vp) {
1826
1827 #ifdef JSVRMLCLASSESVERBOSE
1828 js_SetPropertyDebugWrapped(context,obj,id,vp,"7");
1829 #endif
1830 return JS_TRUE;
1831}
1832JSBool js_SetPropertyDebug8 (JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
1833 JSObject *obj = *hobj.address();
1834 jsid id = *hiid.address();
1835 jsval *vp = hvp.address();
1836
1837 #ifdef JSVRMLCLASSESVERBOSE
1838 js_SetPropertyDebugWrapped(cx,obj,id,vp,"8");
1839 #endif
1840 return JS_TRUE;
1841}
1842JSBool js_SetPropertyDebug9 (JSContext *context, JSObject *obj, jsid id, JSBool strict, jsval *vp) {
1843
1844 #ifdef JSVRMLCLASSESVERBOSE
1845 js_SetPropertyDebugWrapped(context,obj,id,vp,"9");
1846 #endif
1847 return JS_TRUE;
1848}
1849
1850
1851#endif //defined(JS_SMCPP)
1852#endif //JAVASCRIPT_SM
Definition ringbuf.h:8