FreeWRL / FreeX3D 4.3.0
jsVRMLBrowser.c
1/*
2
3
4Javascript C language binding.
5
6*/
7
8/****************************************************************************
9 This file is part of the FreeWRL/FreeX3D Distribution.
10
11 Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
12
13 FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
14 it under the terms of the GNU Lesser Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 FreeWRL/FreeX3D is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
25****************************************************************************/
26
27
28#include <config.h>
29#if !defined(JS_SMCPP)
30#include <system.h>
31//#if !(defined(JAVASCRIPT_STUB) || defined(JAVASCRIPT_DUK))
32#ifdef JAVASCRIPT_SM
33#include <display.h>
34#include <internal.h>
35
36#include <libFreeWRL.h>
37#include <list.h>
38
39#include "../vrml_parser/Structs.h"
40#include "../vrml_parser/CRoutes.h"
41#include "../opengl/OpenGL_Utils.h"
42#include "../main/headers.h"
43#include "../main/ProdCon.h"
44#include "../scenegraph/RenderFuncs.h"
45#include "../vrml_parser/CParseGeneral.h"
46#include "../scenegraph/Vector.h"
47#include "../vrml_parser/CFieldDecls.h"
48#include "../vrml_parser/CParseParser.h"
49#include "../vrml_parser/CParseLexer.h"
50#include "../vrml_parser/CParse.h"
51#include "../main/Snapshot.h"
52#include "../scenegraph/Collision.h"
53#include "../scenegraph/quaternion.h"
54#include "../scenegraph/Viewer.h"
55#include "../x3d_parser/Bindable.h"
56#include "../input/EAIHeaders.h" /* for implicit declarations */
57#include "../ui/common.h"
58
59#include "JScript.h"
60#include "CScripts.h"
61#include "jsUtils.h"
62#include "fieldSet.h"
63#include "jsNative.h"
64#include "jsVRMLClasses.h"
65#include "jsVRMLBrowser.h"
66
67
68#define X3DBROWSER 1
69
70
71
72#ifndef X3DBROWSER
73#if JS_VERSION < 185
74#define SetPropertyStub JS_PropertyStub
75#else
76#define SetPropertyStub JS_StrictPropertyStub
77#endif
78#endif // ndef X3DBROWSER
79
80#ifdef X3DBROWSER
81JSBool
82#if JS_VERSION < 185
83BrowserGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
84#elif JS_VERSION == 185
85BrowserGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp);
86#else
87BrowserGetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSMutableHandleValue hvp);
88#endif
89
90JSBool
91#if JS_VERSION < 185
92BrowserSetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
93#elif JS_VERSION == 185
94BrowserSetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp);
95#else
96BrowserSetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSBool strict, JSMutableHandleValue hvp);
97#endif
98
99#endif
100int jsrrunScript(JSContext *_context, JSObject *_globalObj, char *script, jsval *rval);
101/*
102//js 17 aka 186
103struct JSClass {
104 const char *name;
105 uint32_t flags;
106
107 // Mandatory non-null function pointer members.
108 JSPropertyOp addProperty;
109 JSPropertyOp delProperty;
110 JSPropertyOp getProperty;
111 JSStrictPropertyOp setProperty;
112 JSEnumerateOp enumerate;
113 JSResolveOp resolve;
114 JSConvertOp convert;
115 JSFinalizeOp finalize;
116
117 // Optionally non-null members start here.
118 JSCheckAccessOp checkAccess;
119 JSNative call;
120 JSHasInstanceOp hasInstance; // +
121 JSNative construct;
122 JSTraceOp trace;
123
124 void *reserved[40];
125};
126//js 185
127struct JSClass {
128 const char *name;
129 uint32 flags;
130
131 // Mandatory non-null function pointer members.
132 JSPropertyOp addProperty;
133 JSPropertyOp delProperty;
134 JSPropertyOp getProperty;
135 JSStrictPropertyOp setProperty;
136 JSEnumerateOp enumerate;
137 JSResolveOp resolve;
138 JSConvertOp convert;
139 JSFinalizeOp finalize;
140
141 // Optionally non-null members start here.
142 JSClassInternal reserved0; // -
143 JSCheckAccessOp checkAccess;
144 JSNative call;
145 JSNative construct;
146 JSXDRObjectOp xdrObject; //-
147 JSHasInstanceOp hasInstance; //changed place with construct
148 JSMarkOp mark; //changed from JSTraceOp
149
150 JSClassInternal reserved1;
151 void *reserved[19]; //-
152};
153*/
154
155
156//Q. is this a true sharable static?
157static JSClass Browser = {
158 "Browser",
159 JSCLASS_HAS_PRIVATE,
160 JS_PropertyStub,
161 JS_PropertyStub,
162#ifdef X3DBROWSER
163 JS_PropertyStub, //BrowserGetProperty, //JS_PropertyStub,
164 BrowserSetProperty, //JS_StrictPropertyStub,
165#else
166 JS_PropertyStub,
167 SetPropertyStub,
168#endif
169
170 JS_EnumerateStub,
171 JS_ResolveStub,
172 JS_ConvertStub,
173 JS_FinalizeStub
174};
175
176static JSBool doVRMLRoute(JSContext *context, JSObject *obj, uintN argc, jsval *argv, const char *browserFunc);
177
178static JSFunctionSpec (BrowserFunctions)[] = {
179 {"getName", VrmlBrowserGetName, 0},
180 {"getVersion", VrmlBrowserGetVersion, 0},
181 {"getCurrentSpeed", VrmlBrowserGetCurrentSpeed, 0},
182 {"getCurrentFrameRate", VrmlBrowserGetCurrentFrameRate, 0},
183 {"getWorldURL", VrmlBrowserGetWorldURL, 0},
184 {"replaceWorld", VrmlBrowserReplaceWorld, 0},
185 {"loadURL", VrmlBrowserLoadURL, 0},
186 {"setDescription", VrmlBrowserSetDescription, 0},
187 {"createVrmlFromString", VrmlBrowserCreateVrmlFromString, 0},
188 {"createVrmlFromURL", VrmlBrowserCreateVrmlFromURL, 0},
189 {"createX3DFromString", VrmlBrowserCreateX3DFromString, 0},
190 {"createX3DFromURL", VrmlBrowserCreateVrmlFromURL, 0},
191 {"addRoute", VrmlBrowserAddRoute, 0},
192 {"deleteRoute", VrmlBrowserDeleteRoute, 0},
193 {"print", VrmlBrowserPrint, 0},
194 {"println", VrmlBrowserPrintln, 0},
195#ifdef X3DBROWSER
196 //{"replaceWorld", X3dBrowserReplaceWorld, 0}, //conflicts - X3DScene vs MFNode parameter - could detect?
197 //{"createX3DFromString", X3dBrowserCreateX3DFromString, 0}, //conflicts but above verion shouldn't be above, or could detect?
198 //{"createX3DFromURL", X3dBrowserCreateVrmlFromURL, 0}, //conflicts but above version shouldn't be above, or could detect?
199 //{importDocument, X3dBrowserImportDocument, 0), //not sure we need/want this, what does it do?
200 //{getRenderingProperty, X3dGetRenderingProperty, 0},
201 //{addBrowserListener, X3dAddBrowserListener, 0},
202 //{removeBrowserListener, X3dRemoveBrowserListener, 0},
203#endif
204 {0}
205};
206#ifdef X3DBROWSER
207
208/* ProfileInfo, ProfileInfoArray, ComponentInfo, ComponentInfoArray
209 I decided to do these as thin getter wrappers on the bits and pieces defined
210 in Structs.h, GeneratedCode.c and capabilitiesHandler.c
211 The Array types return the info type wrapper with private native member == index into
212 the native array.
213*/
214//ComonentInfo{
215//String name;
216//Numeric level;
217//String Title;
218//String providerUrl;
219//}
220int capabilitiesHandler_getTableLength(int* table);
221int capabilitiesHandler_getComponentLevel(int *table, int comp);
222int capabilitiesHandler_getProfileLevel(int prof);
223const int *capabilitiesHandler_getProfileComponent(int prof);
224const int *capabilitiesHandler_getCapabilitiesTable();
225typedef struct intTableIndex{
226 int* table;
227 int index;
228} *IntTableIndex;
229
230JSBool
231#if JS_VERSION < 185
232ComponentInfoGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
233#elif JS_VERSION == 185
234ComponentInfoGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp){
235#else
236ComponentInfoGetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSMutableHandleValue hvp){
237 JSObject *obj = *hobj._;
238 jsid iid = *hiid._;
239 jsval *vp = hvp._;
240#endif
241
242 IntTableIndex ptr;
243 int _index, *_table, _nameIndex;
244 jsval rval;
245 jsval id;
246
247 UNUSED(rval); // compiler warning mitigation
248
249#if JS_VERSION >= 185
250 if (!JS_IdToValue(cx,iid,&id)) {
251 printf("JS_IdToValue failed in ComponentInfoGetProperty.\n");
252 return JS_FALSE;
253 }
254#endif
255 if ((ptr = (IntTableIndex)JS_GetPrivateFw(cx, obj)) == NULL) {
256 printf( "JS_GetPrivate failed in ExecutionContextGetProperty.\n");
257 return JS_FALSE;
258 }
259 _index = ptr->index;
260 _table = ptr->table;
261//extern const char *COMPONENTS[];
262//extern const int COMPONENTS_COUNT;
263
264 if (JSVAL_IS_INT(id))
265 {
266 int index = JSVAL_TO_INT(id);
267 switch(index){
268 case 0://name
269 case 1://Title
270 _nameIndex = _table[2*_index];
271#if JS_VERSION < 185
272 *rval = STRING_TO_JSVAL(COMPONENTS[_index]);
273#else
274 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,COMPONENTS[_nameIndex])));
275#endif
276 break;
277 case 2://level
278 {
279 int level = capabilitiesHandler_getComponentLevel(_table,_index);
280#if JS_VERSION < 185
281 *rval = INT_TO_JSVAL(lev);
282#else
283 JS_SET_RVAL(cx,vp,INT_TO_JSVAL(level));
284#endif
285 }
286 break;
287 case 3://providerUrl
288#if JS_VERSION < 185
289 *rval = STRING_TO_JSVAL("freewrl.sourceforge.net");
290#else
291 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,"freewrl.sourceforge.net")));
292#endif
293 break;
294 }
295 }
296 return JS_TRUE;
297}
298JSBool
299#if JS_VERSION < 185
300ComponentInfoSetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
301#elif JS_VERSION == 185
302ComponentInfoSetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp){
303#else
304ComponentInfoSetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSBool strict, JSMutableHandleValue hvp){
305 JSObject *obj = *hobj._;
306 jsid iid = *hiid._;
307 jsval *vp = hvp._;
308#endif
309 //can I, should I force it to read-only this way?
310 return JS_FALSE;
311}
312void
313ComponentInfoFinalize(JSContext *cx, JSObject *obj)
314{
315 IntTableIndex ptr;
316 if ((ptr = (IntTableIndex)JS_GetPrivateFw(cx, obj)) == NULL) {
317 return;
318 } else {
319 FREE_IF_NZ (ptr);
320 }
321}
322
323
324static JSClass ComponentInfoClass = {
325 "ComponentInfo",
326 JSCLASS_HAS_PRIVATE,
327 JS_PropertyStub,
328 JS_PropertyStub,
329 ComponentInfoGetProperty,
330 ComponentInfoSetProperty,
331 JS_EnumerateStub,
332 JS_ResolveStub,
333 JS_ConvertStub,
334 JS_FinalizeStub, //ComponentInfoFinalize //JS_FinalizeStub
335};
336
337static JSPropertySpec (ComponentInfoProperties)[] = {
338 //executionContext
339 {"name", 0, JSPROP_ENUMERATE}, //"Core"
340 {"Title", 1, JSPROP_ENUMERATE}, //"Core"
341 {"level", 2, JSPROP_ENUMERATE}, //4
342 {"providerUrl", 3, JSPROP_ENUMERATE}, //"freewrl.sourceforge.net"
343 {0}
344};
345
346
347//ComponentInfoArray{
348//numeric length;
349//ComponentInfo [integer index];
350//}
351
352JSBool
353#if JS_VERSION < 185
354ComponentInfoArrayGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
355#elif JS_VERSION == 185
356ComponentInfoArrayGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp){
357#else
358ComponentInfoArrayGetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSMutableHandleValue hvp){
359 JSObject *obj = *hobj._;
360 jsid iid = *hiid._;
361 jsval *vp = hvp._;
362#endif
363
364 int *_table;
365 jsval rval;
366 jsval id;
367
368 UNUSED(rval); // compiler warning mitigation
369
370#if JS_VERSION >= 185
371 if (!JS_IdToValue(cx,iid,&id)) {
372 printf("JS_IdToValue failed in ComponentInfoArrayGetProperty.\n");
373 return JS_FALSE;
374 }
375#endif
376 if ((_table = (int *)JS_GetPrivateFw(cx, obj)) == NULL) {
377 printf( "JS_GetPrivate failed in ProfileInfoGetProperty.\n");
378 return JS_FALSE;
379 }
380
381 if (JSVAL_IS_INT(id))
382 {
383 int index = JSVAL_TO_INT(id);
384 if(index == -1){
385//extern const char *COMPONENTS[];
386//extern const int COMPONENTS_COUNT;
387
388 int _length = capabilitiesHandler_getTableLength(_table); //COMPONENTS_COUNT;
389#if JS_VERSION < 185
390 *rval = INT_TO_JSVAL(_length);
391#else
392 JS_SET_RVAL(cx,vp,INT_TO_JSVAL(_length));
393#endif
394 }else if(index > -1 && index < COMPONENTS_COUNT )
395 {
396 JSObject *_obj;
397 IntTableIndex tableindex = MALLOC(void *, sizeof(struct intTableIndex));
398 //int* _index = MALLOC(void *, sizeof(int));
399 _obj = JS_NewObject(cx,&ComponentInfoClass,NULL,obj);
400 tableindex->index = index;
401 tableindex->table = _table;
402 if (!JS_DefineProperties(cx, _obj, ComponentInfoProperties)) {
403 printf( "JS_DefineProperties failed in ComponentInfoProperties.\n");
404 return JS_FALSE;
405 }
406
407 if (!JS_SetPrivateFw(cx, _obj, (void*)tableindex)) {
408 printf( "JS_SetPrivate failed in ComponentInfoArray.\n");
409 return JS_FALSE;
410 }
411
412#if JS_VERSION < 185
413 *rval = OBJECT_TO_JSVAL(_obj);
414#else
415 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
416#endif
417
418 }
419 }
420 return JS_TRUE;
421}
422JSBool
423#if JS_VERSION < 185
424ComponentInfoArraySetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
425#elif JS_VERSION == 185
426ComponentInfoArraySetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp){
427#else
428ComponentInfoArraySetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSBool strict, JSMutableHandleValue hvp){
429 JSObject *obj = *hobj._;
430 jsid iid = *hiid._;
431 jsval *vp = hvp._;
432#endif
433 //can I, should I force it to read-only this way?
434 return JS_FALSE;
435}
436
437
438static JSClass ComponentInfoArrayClass = {
439 "ComponentInfoArray",
440 JSCLASS_HAS_PRIVATE,
441 JS_PropertyStub,
442 JS_PropertyStub,
443 ComponentInfoArrayGetProperty,
444 ComponentInfoArraySetProperty,
445 JS_EnumerateStub,
446 JS_ResolveStub,
447 JS_ConvertStub,
448 JS_FinalizeStub
449};
450
451static JSPropertySpec (ComponentInfoArrayProperties)[] = {
452 {"length", -1, JSPROP_READONLY | JSPROP_SHARED | JSPROP_PERMANENT}, //JSPROP_ENUMERATE},
453 {0}
454};
455
456//ProfileInfo{
457//String name;
458//Numeric level;
459//String Title;
460//String providerUrl;
461//ComonentInfoArray components;
462//}
463
464JSBool
465#if JS_VERSION < 185
466ProfileInfoGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
467#elif JS_VERSION == 185
468ProfileInfoGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp){
469#else
470ProfileInfoGetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSMutableHandleValue hvp){
471 JSObject *obj = *hobj._;
472 jsid iid = *hiid._;
473 jsval *vp = hvp._;
474#endif
475
476 int *ptr;
477 int _index;
478 jsval rval;
479 jsval id;
480
481 UNUSED(rval); // compiler warning mitigation
482
483#if JS_VERSION >= 185
484 if (!JS_IdToValue(cx,iid,&id)) {
485 printf("JS_IdToValue failed in ProfileInfoGetProperty.\n");
486 return JS_FALSE;
487 }
488#endif
489 if ((ptr = (int *)JS_GetPrivateFw(cx, obj)) == NULL) {
490 printf( "JS_GetPrivate failed in ProfileInfoGetProperty.\n");
491 return JS_FALSE;
492 }
493 _index = *ptr;
494//extern const char *PROFILES[];
495//extern const int PROFILES_COUNT;
496
497 if (JSVAL_IS_INT(id))
498 {
499 int index = JSVAL_TO_INT(id);
500 switch(index){
501 case 0://name
502 case 1://Title
503#if JS_VERSION < 185
504 *rval = STRING_TO_JSVAL(COMPONENTS[_index]);
505#else
506 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,PROFILES[_index])));
507#endif
508 break;
509 case 2://level
510 {
511 int level = capabilitiesHandler_getProfileLevel(_index);
512#if JS_VERSION < 185
513 *rval = INT_TO_JSVAL(lev);
514#else
515 JS_SET_RVAL(cx,vp,INT_TO_JSVAL(level));
516#endif
517 }
518 break;
519 case 3://providerUrl
520#if JS_VERSION < 185
521 *rval = STRING_TO_JSVAL("freewrl.sourceforge.net");
522#else
523 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,"freewrl.sourceforge.net")));
524#endif
525 break;
526 case 4://components ComponentInfoArray
527 {
528 const int *_table = capabilitiesHandler_getProfileComponent(_index);
529 JSObject *_obj;
530 //malloc private not needed
531 _obj = JS_NewObject(cx,&ComponentInfoArrayClass,NULL,obj);
532 if (!JS_DefineProperties(cx, _obj, ComponentInfoArrayProperties)) {
533 printf( "JS_DefineProperties failed in ComponentInfoArrayProperties.\n");
534 return JS_FALSE;
535 }
536
537 if (!JS_SetPrivateFw(cx, _obj, (void*)_table)) {
538 printf( "JS_SetPrivate failed in ComponentInfoArray.\n");
539 return JS_FALSE;
540 }
541#if JS_VERSION < 185
542 *rval = OBJECT_TO_JSVAL(_obj);
543#else
544 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
545#endif
546 }
547 break;
548 }
549 }
550 return JS_TRUE;
551}
552JSBool
553#if JS_VERSION < 185
554ProfileInfoSetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
555#elif JS_VERSION == 185
556ProfileInfoSetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp){
557#else
558ProfileInfoSetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSBool strict, JSMutableHandleValue hvp){
559 JSObject *obj = *hobj._;
560 jsid iid = *hiid._;
561 jsval *vp = hvp._;
562#endif
563 //can I, should I force it to read-only this way?
564 return JS_FALSE;
565}
566
567static JSClass ProfileInfoClass = {
568 "ProfileInfo",
569 JSCLASS_HAS_PRIVATE,
570 JS_PropertyStub,
571 JS_PropertyStub,
572 ProfileInfoGetProperty,
573 ProfileInfoSetProperty,
574 JS_EnumerateStub,
575 JS_ResolveStub,
576 JS_ConvertStub,
577 JS_FinalizeStub
578};
579
580static JSPropertySpec (ProfileInfoProperties)[] = {
581 //executionContext
582 {"name", 0, JSPROP_ENUMERATE},
583 {"Title", 1, JSPROP_ENUMERATE},
584 {"level", 2, JSPROP_ENUMERATE},
585 {"providerUrl", 3, JSPROP_ENUMERATE},
586 {"components", 4, JSPROP_ENUMERATE},
587 {0}
588};
589
590//ProfileInfoArray{
591//numeric length;
592//ProfileInfo [integer index];
593//}
594
595JSBool
596#if JS_VERSION < 185
597ProfileInfoArrayGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
598#elif JS_VERSION == 185
599ProfileInfoArrayGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp){
600#else
601ProfileInfoArrayGetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSMutableHandleValue hvp){
602 JSObject *obj = *hobj._;
603 jsid iid = *hiid._;
604 jsval *vp = hvp._;
605#endif
606
607 jsval rval;
608 jsval id;
609
610 UNUSED(rval); // compiler warning mitigation
611
612#if JS_VERSION >= 185
613 if (!JS_IdToValue(cx,iid,&id)) {
614 printf("JS_IdToValue failed in ProfileInfoArrayGetProperty.\n");
615 return JS_FALSE;
616 }
617#endif
618
619 if (JSVAL_IS_INT(id))
620 {
621 int index = JSVAL_TO_INT(id);
622 if(index == -1){
623 int _length = PROFILES_COUNT;
624#if JS_VERSION < 185
625 *rval = INT_TO_JSVAL(_length);
626#else
627 JS_SET_RVAL(cx,vp,INT_TO_JSVAL(_length));
628#endif
629 }else
630 //if(index < getNumberOfProfiles() )
631 {
632 JSObject *_obj;
633 int* _index = MALLOC(void *, sizeof(int));
634 _obj = JS_NewObject(cx,&ProfileInfoClass,NULL,obj);
635 *_index = index;
636 if (!JS_DefineProperties(cx, _obj, ProfileInfoProperties)) {
637 printf( "JS_DefineProperties failed in ProfileInfoProperties.\n");
638 return JS_FALSE;
639 }
640
641 if (!JS_SetPrivateFw(cx, _obj, (void*)_index)) {
642 printf( "JS_SetPrivate failed in ProfileInfoArray.\n");
643 return JS_FALSE;
644 }
645
646#if JS_VERSION < 185
647 *rval = OBJECT_TO_JSVAL(_obj);
648#else
649 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
650#endif
651
652 }
653 }
654 return JS_TRUE;
655}
656JSBool
657#if JS_VERSION < 185
658ProfileInfoArraySetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
659#elif JS_VERSION == 185
660ProfileInfoArraySetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp){
661#else
662ProfileInfoArraySetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSBool strict, JSMutableHandleValue hvp){
663 JSObject *obj = *hobj._;
664 jsid iid = *hiid._;
665 jsval *vp = hvp._;
666#endif
667 //can I, should I force it to read-only this way?
668 return JS_FALSE;
669}
670
671
672static JSClass ProfileInfoArrayClass = {
673 "ProfileInfo",
674 JSCLASS_HAS_PRIVATE,
675 JS_PropertyStub,
676 JS_PropertyStub,
677 ProfileInfoArrayGetProperty,
678 ProfileInfoArraySetProperty,
679 JS_EnumerateStub,
680 JS_ResolveStub,
681 JS_ConvertStub,
682 JS_FinalizeStub
683};
684
685static JSPropertySpec (ProfileInfoArrayProperties)[] = {
686 {"length", -1, JSPROP_READONLY | JSPROP_SHARED | JSPROP_PERMANENT}, //JSPROP_ENUMERATE},
687 {0}
688};
689
690
691
692
693//X3DRoute{
694//SFNode sourceNode;
695//String sourceField;
696//SFNode destinationNode;
697//String destinationField;
698//}
699struct CRStruct *getCRoutes();
700int getCRouteCount();
701
702JSBool
703#if JS_VERSION < 185
704X3DRouteGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
705#elif JS_VERSION == 185
706X3DRouteGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp){
707#else
708X3DRouteGetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSMutableHandleValue hvp){
709 JSObject *obj = *hobj._;
710 jsid iid = *hiid._;
711 jsval *vp = hvp._;
712#endif
713 int *ptr;
714 int _index;
715 JSString *_str;
716 jsval rval;
717 struct X3D_Node *fromNode, *toNode;
718 int fromOffset, toOffset;
719 const char *fieldname;
720 jsval id;
721
722 UNUSED(rval); // compiler warning mitigation
723
724#if JS_VERSION >= 185
725 if (!JS_IdToValue(cx,iid,&id)) {
726 printf("JS_IdToValue failed in ProfileInfoGetProperty.\n");
727 return JS_FALSE;
728 }
729#endif
730 if ((ptr = (int *)JS_GetPrivateFw(cx, obj)) == NULL) {
731 printf( "JS_GetPrivate failed in ProfileInfoGetProperty.\n");
732 return JS_FALSE;
733 }
734 _index = *ptr;
735 //routes = getCRoutes();
736 //route = routes[_index];
737 getSpecificRoute (_index,&fromNode, &fromOffset, &toNode, &toOffset);
738 //fromName = parser_getNameFromNode(fromNode);
739 //toName = parser_getNameFromNode(toNode);
740
741 //fprintf (fp, " %p %s.%s TO %p %s.%s \n",fromNode,fromName,
742 // findFIELDNAMESfromNodeOffset0(fromNode,fromOffset),
743 // toNode,toName,
744 // findFIELDNAMESfromNodeOffset0(toNode,toOffset)
745 // );
746
747 if (JSVAL_IS_INT(id))
748 {
749 int index = JSVAL_TO_INT(id);
750 switch(index){
751 case 0://sourceNode
752 case 2://destinationNode
753 //route.routeFromNode
754 {
755 JSObject *_obj;
756 SFNodeNative *sfnn = MALLOC(void *, sizeof(SFNodeNative));
757 memset(sfnn,0,sizeof(SFNodeNative)); //I don't know if I'm supposed to set something else dug9 aug5,2013
758 if(index==0)
759 sfnn->handle = fromNode;
760 if(index==2)
761 sfnn->handle = toNode;
762
763 _obj = JS_NewObject(cx,&SFNodeClass,NULL,obj);
764 if (!JS_DefineProperties(cx, _obj, SFNodeProperties)) {
765 printf( "JS_DefineProperties failed in Route sourceNode.\n");
766 return JS_FALSE;
767 }
768 if (!JS_DefineFunctions(cx, _obj, SFNodeFunctions)) {
769 printf( "JS_DefineFunctions failed in Route sourceNode.\n");
770 return JS_FALSE;
771 }
772
773 if (!JS_SetPrivateFw(cx, _obj, (void*)sfnn)) {
774 printf( "JS_SetPrivate failed in Route sourceNode.\n");
775 return JS_FALSE;
776 }
777
778#if JS_VERSION < 185
779 *rval = OBJECT_TO_JSVAL(_obj);
780#else
781 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
782#endif
783 }
784 break;
785
786 case 1://sourceField
787 fieldname = findFIELDNAMESfromNodeOffset0(fromNode,fromOffset);
788 _str = JS_NewStringCopyZ(cx,fieldname);
789#if JS_VERSION < 185
790 *rval = STRING_TO_JSVAL(_str);
791#else
792 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
793#endif
794 break;
795 case 3://destinationField
796 fieldname = findFIELDNAMESfromNodeOffset0(toNode,toOffset);
797 _str = JS_NewStringCopyZ(cx,fieldname);
798#if JS_VERSION < 185
799 *rval = STRING_TO_JSVAL(_str);
800#else
801 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
802#endif
803
804 break;
805 }
806 }
807 return JS_TRUE;
808}
809JSBool
810#if JS_VERSION < 185
811X3DRouteSetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
812#elif JS_VERSION == 185
813X3DRouteSetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp){
814#else
815X3DRouteSetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSBool strict, JSMutableHandleValue hvp){
816 JSObject *obj = *hobj._;
817 jsid iid = *hiid._;
818 jsval *vp = hvp._;
819#endif
820 //can I, should I force it to read-only this way?
821 return JS_FALSE;
822}
823
824static JSClass X3DRouteClass = {
825 "X3DRoute",
826 JSCLASS_HAS_PRIVATE,
827 JS_PropertyStub,
828 JS_PropertyStub,
829 X3DRouteGetProperty,
830 X3DRouteSetProperty,
831 JS_EnumerateStub,
832 JS_ResolveStub,
833 JS_ConvertStub,
834 JS_FinalizeStub
835};
836
837static JSPropertySpec (X3DRouteProperties)[] = {
838 //executionContext
839 {"sourceNode", 0, JSPROP_ENUMERATE},
840 {"sourceField", 1, JSPROP_ENUMERATE},
841 {"destinationNode", 2, JSPROP_ENUMERATE},
842 {"destinationField", 3, JSPROP_ENUMERATE},
843 {0}
844};
845
846
847//ProfileInfoArray{
848//numeric length;
849//ProfileInfo [integer index];
850//}
851
852JSBool
853#if JS_VERSION < 185
854RouteArrayGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
855#elif JS_VERSION == 185
856RouteArrayGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp){
857#else
858RouteArrayGetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSMutableHandleValue hvp){
859 JSObject *obj = *hobj._;
860 jsid iid = *hiid._;
861 jsval *vp = hvp._;
862#endif
863
864 jsval rval;
865 jsval id;
866
867 UNUSED(rval); //compiler warning mitigation
868
869#if JS_VERSION >= 185
870 if (!JS_IdToValue(cx,iid,&id)) {
871 printf("JS_IdToValue failed in RouteArrayGetProperty.\n");
872 return JS_FALSE;
873 }
874#endif
875
876 if (JSVAL_IS_INT(id))
877 {
878 int index = JSVAL_TO_INT(id);
879 if(index == -1){
880 int _length = getCRouteCount();
881#if JS_VERSION < 185
882 *rval = INT_TO_JSVAL(_length);
883#else
884 JS_SET_RVAL(cx,vp,INT_TO_JSVAL(_length));
885#endif
886 }else
887 //if(index < getNumberOfProfiles() )
888 {
889 JSObject *_obj;
890 int* _index = MALLOC(void *, sizeof(int));
891 _obj = JS_NewObject(cx,&X3DRouteClass,NULL,obj);
892 *_index = index;
893 if (!JS_DefineProperties(cx, _obj, X3DRouteProperties)) {
894 printf( "JS_DefineProperties failed in RouteArray.\n");
895 return JS_FALSE;
896 }
897
898 if (!JS_SetPrivateFw(cx, _obj, (void*)_index)) {
899 printf( "JS_SetPrivate failed in RouteArray.\n");
900 return JS_FALSE;
901 }
902
903#if JS_VERSION < 185
904 *rval = OBJECT_TO_JSVAL(_obj);
905#else
906 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
907#endif
908
909 }
910 }
911 return JS_TRUE;
912}
913JSBool
914#if JS_VERSION < 185
915RouteArraySetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
916#elif JS_VERSION == 185
917RouteArraySetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp){
918#else
919RouteArraySetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSBool strict, JSMutableHandleValue hvp){
920 JSObject *obj = *hobj._;
921 jsid iid = *hiid._;
922 jsval *vp = hvp._;
923#endif
924 //can I, should I force it to read-only this way?
925 return JS_FALSE;
926}
927
928
929static JSClass RouteArrayClass = {
930 "RouteArray",
931 JSCLASS_HAS_PRIVATE,
932 JS_PropertyStub,
933 JS_PropertyStub,
934 RouteArrayGetProperty,
935 RouteArraySetProperty,
936 JS_EnumerateStub,
937 JS_ResolveStub,
938 JS_ConvertStub,
939 JS_FinalizeStub
940};
941
942static JSPropertySpec (RouteArrayProperties)[] = {
943 {"length", -1, JSPROP_READONLY | JSPROP_SHARED | JSPROP_PERMANENT}, //JSPROP_ENUMERATE},
944 {0}
945};
946
947
948
949
950static JSFunctionSpec (ExecutionContextFunctions)[] = {
951 //executionContext
952 //{"addRoute", X3DExecutionContext_addRoute, 0},
953 //{"deleteRoute", X3DExecutionContext_deleteRoute, 0},
954 //{"createNode", X3DExecutionContext_createNode, 0},
955 //{"createProto", X3DExecutionContext_createProto, 0},
956 //{"getImportedNode", X3DExecutionContext_getImportedNode, 0},
957 //{"updateImportedNode", X3DExecutionContext_updateImportedNode, 0},
958 //{"removeImportedNode", X3DExecutionContext_removeImportedNode, 0},
959 //{"getNamedNode", X3DExecutionContext_getNamedNode, 0},
960 //{"updateNamedNode", X3DExecutionContext_updateNamedNode, 0},
961 //{"removeNamedNode", X3DExecutionContext_removeNamedNode, 0},
963 //{"setMetaData", X3DScene_setMetaData, 0},
964 //{"getMetaData", X3DScene_getMetaData, 0},
965 //{"getExportedNode", X3DScene_getExportedNode, 0},
966 //{"updateExportedNode", X3DScene_updateExportedNode, 0},
967 //{"removeExportedNode", X3DScene_removeExportedNode, 0},
968 {0}
969};
970
971
972static JSPropertySpec (ExecutionContextProperties)[] = {
973 //executionContext
974 {"specificationVersion", 0, JSPROP_ENUMERATE},
975 {"encoding", 1, JSPROP_ENUMERATE},
976 {"profile", 2, JSPROP_ENUMERATE},
977 {"components", 3, JSPROP_ENUMERATE},
978 {"worldURL", 4, JSPROP_ENUMERATE},
979 {"rootNodes", 5, JSPROP_ENUMERATE},
980 {"protos", 6, JSPROP_ENUMERATE},
981 {"externprotos", 7, JSPROP_ENUMERATE},
982 {"routes", 8, JSPROP_ENUMERATE},
983 //scene
984 //{"specificationVersion", 9, JSPROP_ENUMERATE}, //already done for executionContext above
985 {"isScene", 9, JSPROP_ENUMERATE}, //else protoInstance. extra beyond specs - I think flux has it.
986 {0}
987};
988
989//typedef struct _ExecutionContextNative {
990// struct X3D_Node *handle;
991//} ExecutionContextNative;
992typedef struct X3D_Node * ExecutionContextNative;
993
994JSBool
995#if JS_VERSION < 185
996ExecutionContextGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
997#elif JS_VERSION == 185
998ExecutionContextGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp){
999#else
1000ExecutionContextGetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSMutableHandleValue hvp){
1001 JSObject *obj = *hobj._;
1002 jsid iid = *hiid._;
1003 jsval *vp = hvp._;
1004#endif
1005 ExecutionContextNative *ptr;
1006 JSString *_str;
1007 jsval rval;
1008 jsval id;
1009
1010 UNUSED(rval); //compiler warning mitigation
1011
1012#if JS_VERSION >= 185
1013 if (!JS_IdToValue(cx,iid,&id)) {
1014 printf("JS_IdToValue failed in ExecutionContextGetProperty.\n");
1015 return JS_FALSE;
1016 }
1017#endif
1018
1019 if ((ptr = (ExecutionContextNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1020 printf( "JS_GetPrivate failed in ExecutionContextGetProperty.\n");
1021 return JS_FALSE;
1022 }
1023
1024 if (JSVAL_IS_INT(id)) {
1025 switch (JSVAL_TO_INT(id)) {
1026 case 0: //specificationVersion string readonly
1027 {
1028 char cs[100];
1029 sprintf(cs,"{%d,%d,%d}",inputFileVersion[0],inputFileVersion[1],inputFileVersion[2]);
1030 _str = JS_NewStringCopyZ(cx,cs);
1031 }
1032#if JS_VERSION < 185
1033 *rval = STRING_TO_JSVAL(_str);
1034#else
1035 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
1036#endif
1037 break;
1038 case 1: //encoding string readonly
1039 //Valid values are "ASCII", "VRML", "XML", "BINARY", "SCRIPTED", "BIFS", "NONE"
1040 _str = JS_NewStringCopyZ(cx, "not filled in yet sb. VRML or XML or ..");
1041#if JS_VERSION < 185
1042 *rval = STRING_TO_JSVAL(_str);
1043#else
1044 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
1045#endif
1046 break;
1047 case 2: //profile ProfileInfo readonly
1048 {
1049 int index = gglobal()->Mainloop.scene_profile;
1050
1051 JSObject *_obj;
1052 int* _index = MALLOC(void *, sizeof(int));
1053 _obj = JS_NewObject(cx,&ProfileInfoClass,NULL,obj);
1054 *_index = index;
1055 if (!JS_DefineProperties(cx, _obj, ProfileInfoProperties)) {
1056 printf( "JS_DefineProperties failed in ExecutionContextProfileInfoProperties.\n");
1057 return JS_FALSE;
1058 }
1059
1060 if (!JS_SetPrivateFw(cx, _obj, (void*)_index)) {
1061 printf( "JS_SetPrivate failed in ExecutionContextProfileInfoArray.\n");
1062 return JS_FALSE;
1063 }
1064
1065#if JS_VERSION < 185
1066 *rval = OBJECT_TO_JSVAL(_obj);
1067#else
1068 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
1069#endif
1070 }
1071 break;
1072 case 3: //components ComponentInfoArray readonly
1073 {
1074 JSObject *_obj;
1075 //int ncomp;
1076 const int *_table = gglobal()->Mainloop.scene_components; //capabilitiesHandler_getProfileComponent(_index);
1077 //ncomp = capabilitiesHandler_getTableLength(_table);
1078 //malloc private not needed
1079 _obj = JS_NewObject(cx,&ComponentInfoArrayClass,NULL,obj);
1080 if (!JS_DefineProperties(cx, _obj, ComponentInfoArrayProperties)) {
1081 printf( "JS_DefineProperties failed in ExecutionContext_ComponentInfoArrayProperties.\n");
1082 return JS_FALSE;
1083 }
1084
1085 if (!JS_SetPrivateFw(cx, _obj, (void*)_table)) {
1086 printf( "JS_SetPrivate failed in ExecutionContext_ComponentInfoArray.\n");
1087 return JS_FALSE;
1088 }
1089#if JS_VERSION < 185
1090 *rval = OBJECT_TO_JSVAL(_obj);
1091#else
1092 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
1093#endif
1094 }
1095 break;
1096 case 4: //worldURL string readonly
1097 _str = JS_NewStringCopyZ(cx, gglobal()->Mainloop.url);
1098#if JS_VERSION < 185
1099 *rval = STRING_TO_JSVAL(_str);
1100#else
1101 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
1102#endif
1103 break;
1104 case 5: //rootNodes MFNode (readonly if !isScene, else rw)
1105 {
1106 JSObject *_obj;
1107 //MFNodeNative *mfn;
1108
1109 //struct X3D_Group* scene = (struct X3D_Group*)(struct X3D_Node*)ptr; //->handle;
1110 return JS_FALSE;
1111
1112 //scene->children;
1113 //somehow return children as an MFNode
1114 //mfn = MALLOC(void *, sizeof(MFNodeNative));
1115 _obj = JS_NewObject(cx,&MFNodeClass,NULL,obj);
1116
1117 //mfn->handle = (struct X3D_Node*)rootNode(); //change this to (Script)._executionContext when brotos working fully
1118 //if (!JS_DefineProperties(cx, _obj, MFNodeProperties)) {
1119 // printf( "JS_DefineProperties failed in SFRotationConstr.\n");
1120 // return JS_FALSE;
1121 //}
1122 if (!JS_DefineFunctions(cx, _obj, MFNodeFunctions)) {
1123 printf( "JS_DefineProperties failed in SFRotationConstr.\n");
1124 return JS_FALSE;
1125 }
1126//OUCH NEEDS WORK i DON'T KNOW WHAT I'M DOING
1127 //if (!JS_SetPrivateFw(cx, _obj, &scene->children)) {
1128 // printf( "JS_SetPrivate failed in ExecutionContext.\n");
1129 // return JS_FALSE;
1130 //}
1131
1132#if JS_VERSION < 185
1133 *rval = OBJECT_TO_JSVAL(_obj);
1134#else
1135 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
1136#endif
1137 }
1138 break;
1139 case 6: //protos protoDeclarationArray rw
1140 case 7: //externprotos externProtoDeclarationArray rw
1141 return JS_FALSE;
1142 case 8: //routes RouteArray readonly
1143 {
1144 JSObject *_obj;
1145 //malloc private not needed
1146 _obj = JS_NewObject(cx,&RouteArrayClass,NULL,obj);
1147 if (!JS_DefineProperties(cx, _obj, RouteArrayProperties)) {
1148 printf( "JS_DefineProperties failed in ExecutionContext_X3DRouteArrayProperties.\n");
1149 return JS_FALSE;
1150 }
1151 //if (!JS_SetPrivateFw(cx, _obj, (void*)_table)) {
1152 // printf( "JS_SetPrivate failed in ExecutionContext_X3DRouteArray.\n");
1153 // return JS_FALSE;
1154 //}
1155#if JS_VERSION < 185
1156 *rval = OBJECT_TO_JSVAL(_obj);
1157#else
1158 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
1159#endif
1160 }
1161 break;
1162 case 9: //isScene readonly (extra to specs)
1163 //once brotos are working then the main scene broto will need a flag to say it's a scene
1164 JS_SET_RVAL(cx,vp,BOOLEAN_TO_JSVAL(JS_TRUE));
1165 break;
1166 }
1167 }
1168 return JS_TRUE;
1169}
1170
1171
1172JSBool
1173#if JS_VERSION < 185
1174ExecutionContextSetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
1175#elif JS_VERSION == 185
1176ExecutionContextSetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp){
1177#else
1178ExecutionContextSetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSBool strict, JSMutableHandleValue hvp){
1179 JSObject *obj = *hobj._;
1180 jsid iid = *hiid._;
1181 jsval *vp = hvp._;
1182#endif
1183 //can I, should I force it to read-only this way?
1184 return JS_FALSE;
1185}
1186static JSClass ExecutionContextClass = {
1187 "ExecutionContext",
1188 JSCLASS_HAS_PRIVATE,
1189 JS_PropertyStub,
1190 JS_PropertyStub,
1191 ExecutionContextGetProperty,
1192 ExecutionContextSetProperty,
1193 JS_EnumerateStub,
1194 JS_ResolveStub,
1195 JS_ConvertStub,
1196 JS_FinalizeStub
1197};
1198
1199
1200static JSPropertySpec (BrowserProperties)[] = {
1201 {"name", 0, JSPROP_ENUMERATE},
1202 {"version", 1, JSPROP_ENUMERATE},
1203 {"currentSpeed", 2, JSPROP_ENUMERATE},
1204 {"currentFrameRate", 3, JSPROP_ENUMERATE},
1205 {"description", 4, JSPROP_ENUMERATE},
1206 {"supportedComponents", 5, JSPROP_ENUMERATE},
1207 {"supportedProfiles", 6, JSPROP_ENUMERATE},
1208 {"currentScene", 7, JSPROP_ENUMERATE},
1209 {0}
1210};
1211
1212JSBool
1213#if JS_VERSION < 185
1214BrowserGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
1215#elif JS_VERSION == 185
1216BrowserGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp){
1217#else
1218BrowserGetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSMutableHandleValue hvp){
1219 JSObject *obj = *hobj._;
1220 jsid iid = *hiid._;
1221 jsval *vp = hvp._;
1222#endif
1223 BrowserNative *ptr;
1224 jsdouble d;
1225 JSString *_str;
1226 jsval rval;
1227 jsval id;
1228
1229 UNUSED(rval); // compiler warning mitigation
1230
1231#if JS_VERSION >= 185
1232 if (!JS_IdToValue(cx,iid,&id)) {
1233 printf("JS_IdToValue failed in BrowserGetProperty.\n");
1234 return JS_FALSE;
1235 }
1236#endif
1237
1238 //right now we don't need/use the ptr to BrowserNative which is a stub struct,
1239 //because browser is conceptually a global static singleton
1240 //(or more precisely 1:1 with a gglobal[i] 'browser instance' for things like framerate,
1241 // and 1:1 with static for unchanging things like browser version, components and profiles supported),
1242 //and in practice all the bits and pieces are scattered throughout freewrl
1243 //but for fun we'll get it:
1244 if ((ptr = (BrowserNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1245 printf( "JS_GetPrivate failed in BrowserGetProperty.\n");
1246 return JS_FALSE;
1247 }
1248
1249 if (JSVAL_IS_INT(id)) {
1250 switch (JSVAL_TO_INT(id)) {
1251 case 0: //name
1252 _str = JS_NewStringCopyZ(cx,BrowserName);
1253#if JS_VERSION < 185
1254 *rval = STRING_TO_JSVAL(_str);
1255#else
1256 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
1257#endif
1258 break;
1259 case 1: //version
1260 _str = JS_NewStringCopyZ(cx, libFreeWRL_get_version());
1261#if JS_VERSION < 185
1262 *rval = STRING_TO_JSVAL(_str);
1263#else
1264 JS_SET_RVAL(context,vp,STRING_TO_JSVAL(_str));
1265#endif
1266 break;
1267 case 2: //currentSpeed
1268 /* get the variable updated */
1269 getCurrentSpeed();
1270 d = gglobal()->Mainloop.BrowserSpeed;
1271 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
1272 printf("JS_NewDouble failed for %f in BrowserGetProperty.\n",d);
1273 return JS_FALSE;
1274 }
1275 break;
1276 case 3: //currentFrameRate
1277 d = gglobal()->Mainloop.BrowserFPS;
1278 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
1279 printf("JS_NewDouble failed for %f in BrowserGetProperty.\n",d);
1280 return JS_FALSE;
1281 }
1282 break;
1283 case 4: //description
1284 _str = JS_NewStringCopyZ(cx, get_status());
1285#if JS_VERSION < 185
1286 *rval = STRING_TO_JSVAL(_str);
1287#else
1288 JS_SET_RVAL(context,vp,STRING_TO_JSVAL(_str));
1289#endif
1290 break;
1291 case 5: //supportedComponents
1292 {
1293 JSObject *_obj;
1294 //malloc private not needed
1295 _obj = JS_NewObject(cx,&ComponentInfoArrayClass,NULL,obj);
1296 if (!JS_DefineProperties(cx, _obj, ComponentInfoArrayProperties)) {
1297 printf( "JS_DefineProperties failed in ComponentInfoArrayProperties.\n");
1298 return JS_FALSE;
1299 }
1300 //if (!JS_DefineFunctions(cx, _obj, ProfileInfoArrayFunctions)) {
1301 // printf( "JS_DefineProperties failed in ExecutionContextFunctions.\n");
1302 // return JS_FALSE;
1303 //}
1304 if (!JS_SetPrivateFw(cx, _obj, (void*)capabilitiesHandler_getCapabilitiesTable())) {
1305 printf( "JS_SetPrivate failed in ExecutionContext.\n");
1306 return JS_FALSE;
1307 }
1308
1309#if JS_VERSION < 185
1310 *rval = OBJECT_TO_JSVAL(_obj);
1311#else
1312 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
1313#endif
1314
1315 }
1316 break;
1317
1318 case 6: //supportedProfiles
1319 {
1320 JSObject *_obj;
1321 //malloc private not needed
1322 _obj = JS_NewObject(cx,&ProfileInfoArrayClass,NULL,obj);
1323 if (!JS_DefineProperties(cx, _obj, ProfileInfoArrayProperties)) {
1324 printf( "JS_DefineProperties failed in ExecutionContextProperties.\n");
1325 return JS_FALSE;
1326 }
1327 //if (!JS_DefineFunctions(cx, _obj, ProfileInfoArrayFunctions)) {
1328 // printf( "JS_DefineProperties failed in ExecutionContextFunctions.\n");
1329 // return JS_FALSE;
1330 //}
1331 //set private not needed
1332 //if (!JS_SetPrivateFw(cx, _obj, ec)) {
1333 // printf( "JS_SetPrivate failed in ExecutionContext.\n");
1334 // return JS_FALSE;
1335 //}
1336
1337#if JS_VERSION < 185
1338 *rval = OBJECT_TO_JSVAL(_obj);
1339#else
1340 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
1341#endif
1342
1343 }
1344 break;
1345 case 7: //currentScene
1346#ifdef sceneIsBroto
1347 //someday soon I hope, the ScriptNode._executionContext might be working,
1348 //and give you the currentScene native pointer to put as PRIVATE in BrowserNative
1349#else
1350 //in theory it's rootNode() in
1351 //struct X3D_Group *rootNode()
1352 //H: I have to return an ExecutionContextNative here with its guts set to our rootNode or ???
1353 {
1354 JSObject *_obj;
1355 ExecutionContextNative ec = MALLOC(void *, sizeof(ExecutionContextNative));
1356 _obj = JS_NewObject(cx,&ExecutionContextClass,NULL,obj);
1357
1358 //ec->handle = (struct X3D_Node*)rootNode(); //change this to (Script)._executionContext when brotos working fully
1359 ec = (struct X3D_Node*)rootNode(); //change this to (Script)._executionContext when brotos working fully
1360 if (!JS_DefineProperties(cx, _obj, ExecutionContextProperties)) {
1361 printf( "JS_DefineProperties failed in ExecutionContextProperties.\n");
1362 return JS_FALSE;
1363 }
1364 if (!JS_DefineFunctions(cx, _obj, ExecutionContextFunctions)) {
1365 printf( "JS_DefineProperties failed in ExecutionContextFunctions.\n");
1366 return JS_FALSE;
1367 }
1368
1369 if (!JS_SetPrivateFw(cx, _obj, ec)) {
1370 printf( "JS_SetPrivate failed in ExecutionContext.\n");
1371 return JS_FALSE;
1372 }
1373
1374#if JS_VERSION < 185
1375 *rval = OBJECT_TO_JSVAL(_obj);
1376#else
1377 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_obj));
1378#endif
1379
1380 }
1381
1382#endif
1383 }
1384 }
1385 return JS_TRUE;
1386}
1387
1388
1389JSBool
1390#if JS_VERSION < 185
1391BrowserSetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
1392#elif JS_VERSION == 185
1393BrowserSetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp){
1394#else
1395BrowserSetProperty(JSContext *cx, JSHandleObject hobj, JSHandleId hiid, JSBool strict, JSMutableHandleValue hvp){
1396 JSObject *obj = *hobj._;
1397 jsid iid = *hiid._;
1398 jsval *vp = hvp._;
1399#endif
1400
1401 BrowserNative *ptr;
1402 jsval _val;
1403 JSString *ss;
1404 char *cs;
1405#if JS_VERSION >= 185
1406 jsval id;
1407 if (!JS_IdToValue(cx,iid,&id)) {
1408 printf("JS_IdToValue failed in BrowserSetProperty.\n");
1409 return JS_FALSE;
1410 }
1411#endif
1412
1413 if ((ptr = (BrowserNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1414 printf( "JS_GetPrivate failed in BrowserSetProperty.\n");
1415 return JS_FALSE;
1416 }
1417 //ptr->valueChanged++;
1418
1419 if (!JS_ConvertValue(cx, *vp, JSTYPE_STRING, &_val)) {
1420 printf( "JS_ConvertValue failed in BrowserSetProperty.\n");
1421 return JS_FALSE;
1422 }
1423
1424 if (JSVAL_IS_INT(id)) {
1425 switch (JSVAL_TO_INT(id)) {
1426 case 0:
1427 case 1:
1428 case 2:
1429 case 3:
1430 return JS_FALSE;
1431 case 4: //description
1432 ss = JS_ValueToString(cx, _val);
1433 cs = JS_EncodeString(cx, ss);
1434 update_status(cs); //script node setting the statusbar text
1435 break;
1436 case 5:
1437 case 6:
1438 case 7:
1439 return JS_FALSE;
1440
1441 }
1442 }
1443 return JS_TRUE;
1444}
1445
1446
1447#endif
1448
1449
1452//jsval JSCreate_global_return_val;
1453typedef struct pjsVRMLBrowser{
1454 int ijunk;
1455
1456 jsval JSCreate_global_return_val;
1457
1458
1459}* ppjsVRMLBrowser;
1460void *jsVRMLBrowser_constructor(){
1461 void *v = MALLOC(void *, sizeof(struct pjsVRMLBrowser));
1462 memset(v,0,sizeof(struct pjsVRMLBrowser));
1463 return v;
1464}
1465void jsVRMLBrowser_init(struct tjsVRMLBrowser *t){
1466 //public
1467 //private
1468 t->prv = jsVRMLBrowser_constructor();
1469 {
1470 ppjsVRMLBrowser p = (ppjsVRMLBrowser)t->prv;
1471 /* Script name/type table */
1472
1473 t->JSCreate_global_return_val = &p->JSCreate_global_return_val;
1474
1475 }
1476
1477}
1478// ppjsVRMLBrowser p = (ppjsVRMLBrowser)gglobal()->jsVRMLBrowser.prv;
1479/* we add/remove routes with this call */
1480void jsRegisterRoute(
1481 struct X3D_Node* from, int fromOfs,
1482 struct X3D_Node* to, int toOfs,
1483 int len, const char *adrem) {
1484 int ad;
1485
1486 if (strcmp("addRoute",adrem) == 0)
1487 ad = 1;
1488 else ad = 0;
1489
1490 CRoutes_Register(ad, from, fromOfs, to, toOfs , len,
1491 returnInterpolatorPointer(to->_nodeType), 0, 0);
1492}
1493
1494
1495/* used in loadURL*/
1496void conCat (char *out, char *in) {
1497
1498 while (strlen (in) > 0) {
1499 strcat (out," :loadURLStringBreak:");
1500 while (*out != '\0') out++;
1501
1502 if (*in == '[') in++;
1503 while ((*in != '\0') && (*in == ' ')) in++;
1504 if (*in == '"') {
1505 in++;
1506 /* printf ("have the initial quote string here is %s\n",in); */
1507 while (*in != '"') { *out = *in; out++; in++; }
1508 *out = '\0';
1509 /* printf ("found string is :%s:\n",tfilename); */
1510 }
1511
1512 /* skip along to the start of the next name */
1513 if (*in == '"') in++;
1514 if (*in == ',') in++;
1515 if (*in == ']') in++; /* this allows us to leave */
1516 }
1517}
1518
1519
1520
1521void createLoadUrlString(char *out, int outLen, char *url, char *param) {
1522 int commacount1;
1523 int commacount2;
1524 char *tptr;
1525
1526 /* mimic the EAI loadURL, java code is:
1527 // send along sizes of the Strings
1528 SysString = "" + url.length + " " + parameter.length;
1529
1530 for (count=0; count<url.length; count++) {
1531 SysString = SysString + " :loadURLStringBreak:" + url[count];
1532 }
1533
1534 for (count=0; count<parameter.length; count++) {
1535 SysString = SysString + " :loadURLStringBreak:" + parameter[count];
1536 }
1537 */
1538
1539 /* find out how many elements there are */
1540
1541 commacount1 = 0; commacount2 = 0;
1542 tptr = url; while (*tptr != '\0') { if (*tptr == '"') commacount1 ++; tptr++; }
1543 tptr = param; while (*tptr != '\0') { if (*tptr == '"') commacount2 ++; tptr++; }
1544 commacount1 = commacount1 / 2;
1545 commacount2 = commacount2 / 2;
1546
1547 if ((int)(strlen(url) +
1548 strlen(param) +
1549 (commacount1 * strlen (" :loadURLStringBreak:")) +
1550 (commacount2 * strlen (" :loadURLStringBreak:"))) > (outLen - 20)) {
1551 printf ("createLoadUrlString, string too long\n");
1552 return;
1553 }
1554
1555 sprintf (out,"%d %d",commacount1,commacount2);
1556
1557 /* go to the end of this string */
1558 while (*out != '\0') out++;
1559
1560 /* go through the elements and find which (if any) url exists */
1561 conCat (out,url);
1562 while (*out != '\0') out++;
1563 conCat (out,param);
1564}
1565
1566
1567JSBool
1568VrmlBrowserInit(JSContext *context, JSObject *globalObj, BrowserNative *brow)
1569{
1570 JSObject *obj;
1571 ttglobal tg = gglobal();
1572 *(jsval *)tg->jsVRMLBrowser.JSCreate_global_return_val = INT_TO_JSVAL(0);
1573
1574 #ifdef JSVERBOSE
1575 printf("VrmlBrowserInit\n");
1576 #endif
1577
1578 obj = JS_DefineObject(context, globalObj, "Browser", &Browser, NULL,
1579 JSPROP_ENUMERATE | JSPROP_PERMANENT);
1580 if (!JS_DefineFunctions(context, obj, BrowserFunctions)) {
1581 printf( "JS_DefineFunctions failed in VrmlBrowserInit.\n");
1582 return JS_FALSE;
1583 }
1584#ifdef X3DBROWSER
1585
1586 if (!JS_DefineProperties(context, obj, BrowserProperties)) {
1587 printf( "JS_DefineProperties failed in VrmlBrowserInit.\n");
1588 return JS_FALSE;
1589 }
1590#endif
1591 if (!JS_SetPrivateFw(context, obj, brow)) {
1592 printf( "JS_SetPrivate failed in VrmlBrowserInit.\n");
1593 return JS_FALSE;
1594 }
1595 return JS_TRUE;
1596}
1597
1598
1599JSBool
1600#if JS_VERSION < 185
1601VrmlBrowserGetName(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1602#else
1603VrmlBrowserGetName(JSContext *context, uintN argc, jsval *vp) {
1604 JSObject *obj = JS_THIS_OBJECT(context,vp);
1605 jsval *argv = JS_ARGV(context,vp);
1606#endif
1607 JSString *_str;
1608
1609 UNUSED(obj);
1610 UNUSED(argc);
1611 UNUSED(argv);
1612
1613 _str = JS_NewStringCopyZ(context,BrowserName);
1614#if JS_VERSION < 185
1615 *rval = STRING_TO_JSVAL(_str);
1616#else
1617 JS_SET_RVAL(context,vp,STRING_TO_JSVAL(_str));
1618#endif
1619 return JS_TRUE;
1620}
1621
1622
1623/* get the string stored in FWVER into a jsObject */
1624JSBool
1625#if JS_VERSION < 185
1626VrmlBrowserGetVersion(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1627#else
1628VrmlBrowserGetVersion(JSContext *context, uintN argc, jsval *vp) {
1629 JSObject *obj = JS_THIS_OBJECT(context,vp);
1630 jsval *argv = JS_ARGV(context,vp);
1631#endif
1632 JSString *_str;
1633
1634 UNUSED(obj);
1635 UNUSED(argc);
1636 UNUSED(argv);
1637
1638 _str = JS_NewStringCopyZ(context, libFreeWRL_get_version());
1639#if JS_VERSION < 185
1640 *rval = STRING_TO_JSVAL(_str);
1641#else
1642 JS_SET_RVAL(context,vp,STRING_TO_JSVAL(_str));
1643#endif
1644 return JS_TRUE;
1645}
1646
1647
1648JSBool
1649#if JS_VERSION < 185
1650VrmlBrowserGetCurrentSpeed(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1651#else
1652VrmlBrowserGetCurrentSpeed(JSContext *context, uintN argc, jsval *vp) {
1653 JSObject *obj = JS_THIS_OBJECT(context,vp);
1654 jsval *argv = JS_ARGV(context,vp);
1655#endif
1656 JSString *_str;
1657 char string[1000];
1658
1659 UNUSED(obj);
1660 UNUSED(argc);
1661 UNUSED(argv);
1662
1663 /* get the variable updated */
1664 getCurrentSpeed();
1665 sprintf (string,"%f",gglobal()->Mainloop.BrowserSpeed);
1666 _str = JS_NewStringCopyZ(context,string);
1667#if JS_VERSION < 185
1668 *rval = STRING_TO_JSVAL(_str);
1669#else
1670 JS_SET_RVAL(context,vp,STRING_TO_JSVAL(_str));
1671#endif
1672 return JS_TRUE;
1673}
1674
1675
1676JSBool
1677#if JS_VERSION < 185
1678VrmlBrowserGetCurrentFrameRate(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1679#else
1680VrmlBrowserGetCurrentFrameRate(JSContext *context, uintN argc, jsval *vp) {
1681 JSObject *obj = JS_THIS_OBJECT(context,vp);
1682 jsval *argv = JS_ARGV(context,vp);
1683#endif
1684 JSString *_str;
1685 char FPSstring[1000];
1686
1687 UNUSED(obj);
1688 UNUSED(argc);
1689 UNUSED(argv);
1690
1691 sprintf (FPSstring,"%6.2f",gglobal()->Mainloop.BrowserFPS);
1692 _str = JS_NewStringCopyZ(context,FPSstring);
1693#if JS_VERSION < 185
1694 *rval = STRING_TO_JSVAL(_str);
1695#else
1696 JS_SET_RVAL(context,vp,STRING_TO_JSVAL(_str));
1697#endif
1698 return JS_TRUE;
1699}
1700
1701
1702JSBool
1703#if JS_VERSION < 185
1704VrmlBrowserGetWorldURL(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1705#else
1706VrmlBrowserGetWorldURL(JSContext *context, uintN argc, jsval *vp) {
1707 JSObject *obj = JS_THIS_OBJECT(context,vp);
1708 jsval *argv = JS_ARGV(context,vp);
1709#endif
1710 JSString *_str;
1711
1712 UNUSED(obj);
1713 UNUSED(argc);
1714 UNUSED(argv);
1715
1716 _str = JS_NewStringCopyZ(context,BrowserFullPath);
1717#if JS_VERSION < 185
1718 *rval = STRING_TO_JSVAL(_str);
1719#else
1720 JS_SET_RVAL(context,vp,STRING_TO_JSVAL(_str));
1721#endif
1722 return JS_TRUE;
1723}
1724
1725
1726JSBool
1727#if JS_VERSION < 185
1728VrmlBrowserReplaceWorld(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1729#else
1730VrmlBrowserReplaceWorld(JSContext *context, uintN argc, jsval *vp) {
1731 jsval *argv = JS_ARGV(context,vp);
1732#endif
1733 JSObject *_obj;
1734 JSString *_str;
1735 JSClass *_cls;
1736 jsval _rval = INT_TO_JSVAL(0);
1737 char *_c_args = "MFNode nodes",
1738 *_costr,
1739 *_c_format = "o";
1740 char *tptr;
1741
1742 if (JS_ConvertArguments(context, argc, argv, _c_format, &_obj)) {
1743 if ((_cls = JS_GET_CLASS(context, _obj)) == NULL) {
1744 printf("JS_GetClass failed in VrmlBrowserReplaceWorld.\n");
1745 return JS_FALSE;
1746 }
1747
1748 if (memcmp("MFNode", _cls->name, strlen(_cls->name)) != 0) {
1749 printf( "\nIncorrect argument in VrmlBrowserReplaceWorld.\n");
1750 return JS_FALSE;
1751 }
1752 _str = JS_ValueToString(context, argv[0]);
1753#if JS_VERSION < 185
1754 _costr = JS_GetStringBytes(_str);
1755#else
1756 _costr = JS_EncodeString(context,_str);
1757#endif
1758 /* sanitize string, for the EAI_RW call (see EAI_RW code) */
1759 tptr = _costr;
1760 while (*tptr != '\0') {
1761 if(*tptr == '[') *tptr = ' ';
1762 if(*tptr == ']') *tptr = ' ';
1763 if(*tptr == ',') *tptr = ' ';
1764 tptr++;
1765 }
1766 EAI_RW(_costr);
1767#if JS_VERSION >= 185
1768 JS_free(context,_costr);
1769#endif
1770 } else {
1771 printf( "\nIncorrect argument format for replaceWorld(%s).\n", _c_args);
1772 return JS_FALSE;
1773 }
1774#if JS_VERSION < 185
1775 *rval = _rval;
1776#else
1777 JS_SET_RVAL(context,vp,_rval);
1778#endif
1779
1780 return JS_TRUE;
1781}
1782struct X3D_Anchor* get_EAIEventsIn_AnchorNode();
1783JSBool
1784#if JS_VERSION < 185
1785VrmlBrowserLoadURL(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1786#else
1787VrmlBrowserLoadURL(JSContext *context, uintN argc, jsval *vp) {
1788 jsval *argv = JS_ARGV(context,vp);
1789#endif
1790 JSObject *_obj[2];
1791 JSString *_str[2];
1792 JSClass *_cls[2];
1793 char *_c_args = "MFString url, MFString parameter",
1794 *_costr[2],
1795 *_c_format = "o o";
1796 #define myBufSize 2000
1797 char myBuf[myBufSize];
1798
1799 if (JS_ConvertArguments(context, argc, argv, _c_format, &(_obj[0]), &(_obj[1]))) {
1800 if ((_cls[0] = JS_GET_CLASS(context, _obj[0])) == NULL) {
1801 printf( "JS_GetClass failed for arg 0 in VrmlBrowserLoadURL.\n");
1802 return JS_FALSE;
1803 }
1804 if ((_cls[1] = JS_GET_CLASS(context, _obj[1])) == NULL) {
1805 printf( "JS_GetClass failed for arg 1 in VrmlBrowserLoadURL.\n");
1806 return JS_FALSE;
1807 }
1808 if (memcmp("MFString", (_cls[0])->name, strlen((_cls[0])->name)) != 0 &&
1809 memcmp("MFString", (_cls[1])->name, strlen((_cls[1])->name)) != 0) {
1810 printf( "\nIncorrect arguments in VrmlBrowserLoadURL.\n");
1811 return JS_FALSE;
1812 }
1813 _str[0] = JS_ValueToString(context, argv[0]);
1814#if JS_VERSION < 185
1815 _costr[0] = JS_GetStringBytes(_str[0]);
1816#else
1817 _costr[0] = JS_EncodeString(context,_str[0]);
1818#endif
1819
1820 _str[1] = JS_ValueToString(context, argv[1]);
1821#if JS_VERSION < 185
1822 _costr[1] = JS_GetStringBytes(_str[1]);
1823#else
1824 _costr[1] = JS_EncodeString(context,_str[1]);
1825#endif
1826
1827 /* we use the EAI code for this - so reformat this for the EAI format */
1828 {
1829 //extern struct X3D_Anchor EAI_AnchorNode; /* win32 C doesnt like new declarations in the middle of executables - start a new scope {} and put dec at top */
1830
1831 /* make up the URL from what we currently know */
1832 createLoadUrlString(myBuf,myBufSize,_costr[0], _costr[1]);
1833 createLoadURL(myBuf);
1834
1835 /* now tell the fwl_RenderSceneUpdateScene that BrowserAction is requested... */
1836 setAnchorsAnchor( get_EAIEventsIn_AnchorNode()); //&gglobal()->EAIEventsIn.EAI_AnchorNode;
1837 }
1838 gglobal()->RenderFuncs.BrowserAction = TRUE;
1839
1840#if JS_VERSION >= 185
1841 JS_free(context,_costr[0]);
1842 JS_free(context,_costr[1]);
1843#endif
1844 } else {
1845 printf( "\nIncorrect argument format for loadURL(%s).\n", _c_args);
1846 return JS_FALSE;
1847 }
1848#if JS_VERSION < 185
1849 *rval = INT_TO_JSVAL(0);
1850#else
1851 JS_SET_RVAL(context,vp,INT_TO_JSVAL(0)); //JSVAL_ZERO);
1852#endif
1853
1854 return JS_TRUE;
1855}
1856
1857
1858JSBool
1859#if JS_VERSION < 185
1860VrmlBrowserSetDescription(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1861 char *_c_format = "s";
1862#else
1863VrmlBrowserSetDescription(JSContext *context, uintN argc, jsval *vp) {
1864 jsval *argv = JS_ARGV(context,vp);
1865 JSString *js_c;
1866 char *_c_format = "S";
1867#endif
1868 char *_c, *_c_args = "SFString description";
1869
1870 UNUSED(_c); // compiler warning mitigation
1871
1872 if (argc == 1 &&
1873#if JS_VERSION < 185
1874 JS_ConvertArguments(context, argc, argv, _c_format, &_c)) {
1875#else
1876 JS_ConvertArguments(context, argc, argv, _c_format, &js_c)) {
1877 /* _c = JS_EncodeString(context,js_c);
1878 ...why encode the string when we just have to JS_free it later? */
1879#endif
1880
1881 /* we do not do anything with the description. If we ever wanted to, it is in _c */
1882#if JS_VERSION < 185
1883 *rval = INT_TO_JSVAL(0);
1884#else
1885 JS_SET_RVAL(context,vp,INT_TO_JSVAL(0)); //JSVAL_ZERO);
1886#endif
1887 } else {
1888 printf( "\nIncorrect argument format for setDescription(%s).\n", _c_args);
1889 return JS_FALSE;
1890 }
1891 return JS_TRUE;
1892}
1893
1894
1895JSBool
1896#if JS_VERSION < 185
1897VrmlBrowserCreateVrmlFromString(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1898 char *_c_format = "s";
1899#else
1900VrmlBrowserCreateVrmlFromString(JSContext *context, uintN argc, jsval *vp) {
1901 JSObject *obj = JS_THIS_OBJECT(context,vp);
1902 jsval *argv = JS_ARGV(context,vp);
1903 jsval _my_rval;
1904 jsval *rval = &_my_rval;
1905 char *_c_format = "S";
1906 JSString *js_c;
1907#endif
1908 char *_c, *_c_args = "SFString vrmlSyntax";
1909
1910 /* for the return of the nodes */
1911 struct X3D_Group *retGroup;
1912 char *xstr;
1913 char *tmpstr;
1914 char *separator;
1915 int ra;
1916 int count;
1917 int wantedsize;
1918 int MallocdSize;
1919 ttglobal tg = gglobal();
1920 struct VRMLParser *globalParser = (struct VRMLParser *)tg->CParse.globalParser;
1921
1922 UNUSED(ra); //compiler warning mitigation
1923
1924 /* make this a default value */
1925 *rval = INT_TO_JSVAL(0);
1926
1927 if (argc == 1 &&
1928#if JS_VERSION < 185
1929 JS_ConvertArguments(context, argc, argv, _c_format, &_c)) {
1930#else
1931 JS_ConvertArguments(context, argc, argv, _c_format, &js_c)) {
1932 _c = JS_EncodeString(context,js_c);
1933#endif
1934 #ifdef JSVERBOSE
1935 printf("VrmlBrowserCreateVrmlFromString: obj = %u, str = \"%s\"\n",
1936 obj, _c);
1937 #endif
1938
1939 /* do the call to make the VRML code - create a new browser just for this string */
1940 gglobal()->ProdCon.savedParser = (void *)globalParser; globalParser = NULL;
1941 retGroup = createNewX3DNode0(NODE_Group); //don't register
1942 ra = EAI_CreateVrml("String",_c,X3D_NODE(rootNode()),retGroup);
1943 globalParser = (struct VRMLParser*)gglobal()->ProdCon.savedParser; /* restore it */
1944
1945
1946 /* and, make a string that we can use to create the javascript object */
1947 MallocdSize = 200;
1948 xstr = MALLOC (char *, MallocdSize);
1949 strcpy (xstr,"new MFNode(");
1950 separator = " ";
1951 for (count=0; count<retGroup->children.n; count ++) {
1952 tmpstr = MALLOC(char *, strlen(_c) + 100);
1953 sprintf (tmpstr,"%s new SFNode('%s','%p')",separator, _c, (void*) retGroup->children.p[count]);
1954 // +1 to account for the ")" being added later ...
1955 wantedsize = (int) (strlen(tmpstr) + strlen(xstr) + 1);
1956 // sometimes wantedsize is borderline so alloc some more if it's equal
1957 if (wantedsize >= MallocdSize) {
1958 MallocdSize = wantedsize +200;
1959 xstr = REALLOC (xstr,MallocdSize);
1960 }
1961
1962
1963 strncat (xstr,tmpstr,strlen(tmpstr));
1964 FREE_IF_NZ (tmpstr);
1965 separator = ", ";
1966 }
1967 strcat (xstr,")");
1968 //markForDispose(X3D_NODE(retGroup),FALSE); //change in Nov 2014 does FREE_IF_NZ
1969
1970#if JS_VERSION >= 185
1971 JS_free(context,_c);
1972#endif
1973
1974 #ifdef JSVERBOSE
1975 printf ("running runscript on :%s:\n",xstr);
1976 #endif
1977
1978 /* create this value NOTE: rval is set here. */
1979 jsrrunScript(context, obj, xstr, rval);
1980 FREE_IF_NZ (xstr);
1981
1982 } else {
1983 printf("\nIncorrect argument format for createVrmlFromString(%s).\n", _c_args);
1984 return JS_FALSE;
1985 }
1986
1987 /* save this value, in case we need it */
1988#if JS_VERSION < 185
1989 tg->jsVRMLBrowser.JSCreate_global_return_val = *rval;
1990#else
1991 JS_SET_RVAL(context,vp,*rval);
1992#endif
1993 return JS_TRUE;
1994}
1995
1996JSBool
1997#if JS_VERSION < 185
1998VrmlBrowserCreateX3DFromString(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1999 char *_c_format = "s";
2000#else
2001VrmlBrowserCreateX3DFromString(JSContext *context, uintN argc, jsval *vp) {
2002 JSObject *obj = JS_THIS_OBJECT(context,vp);
2003 jsval *argv = JS_ARGV(context,vp);
2004 jsval _my_rval;
2005 jsval *rval = &_my_rval;
2006 char *_c_format = "S";
2007 JSString *js_c;
2008#endif
2009 char *_c, *_c_args = "SFString x3dSyntax"; //x3d
2010
2011 /* for the return of the nodes */
2012 struct X3D_Group *retGroup;
2013 char *xstr;
2014 char *tmpstr;
2015 char *separator;
2016 int ra;
2017 int count;
2018 int wantedsize;
2019 int MallocdSize;
2020 //ttglobal tg = gglobal();
2021 //struct VRMLParser *globalParser = (struct VRMLParser *)tg->CParse.globalParser;
2022
2023 UNUSED(ra); //compiler warning mitigation
2024
2025 /* make this a default value */
2026 *rval = INT_TO_JSVAL(0);
2027
2028 if (argc == 1 &&
2029#if JS_VERSION < 185
2030 JS_ConvertArguments(context, argc, argv, _c_format, &_c)) {
2031#else
2032 JS_ConvertArguments(context, argc, argv, _c_format, &js_c)) {
2033 _c = JS_EncodeString(context,js_c);
2034#endif
2035 #ifdef JSVERBOSE
2036 printf("VrmlBrowserCreateVrmlFromString: obj = %u, str = \"%s\"\n",
2037 obj, _c);
2038 #endif
2039
2040 /* do the call to make the VRML code - create a new browser just for this string */
2041 //gglobal()->ProdCon.savedParser = (void *)globalParser; globalParser = NULL;
2042 retGroup = createNewX3DNode(NODE_Group);
2043 ra = EAI_CreateX3d("String",_c,X3D_NODE(retGroup),retGroup);
2044 //globalParser = (struct VRMLParser*)gglobal()->ProdCon.savedParser; /* restore it */
2045
2046
2047 /* and, make a string that we can use to create the javascript object */
2048 MallocdSize = 200;
2049 xstr = MALLOC (char *, MallocdSize);
2050 strcpy (xstr,"new MFNode(");
2051 separator = " ";
2052 for (count=0; count<retGroup->children.n; count ++) {
2053 tmpstr = MALLOC(char *, strlen(_c) + 100);
2054 sprintf (tmpstr,"%s new SFNode('%s','%p')",separator, _c, (void*) retGroup->children.p[count]);
2055 wantedsize = (int) (strlen(tmpstr) + strlen(xstr));
2056 if (wantedsize > MallocdSize) {
2057 MallocdSize = wantedsize +200;
2058 xstr = REALLOC (xstr,MallocdSize);
2059 }
2060
2061
2062 strncat (xstr,tmpstr,strlen(tmpstr));
2063 FREE_IF_NZ (tmpstr);
2064 separator = ", ";
2065 }
2066 strcat (xstr,")");
2067 markForDispose(X3D_NODE(retGroup),FALSE);
2068
2069#if JS_VERSION >= 185
2070 JS_free(context,_c);
2071#endif
2072
2073 #ifdef JSVERBOSE
2074 printf ("running runscript on :%s:\n",xstr);
2075 #endif
2076
2077 /* create this value NOTE: rval is set here. */
2078 jsrrunScript(context, obj, xstr, rval);
2079 FREE_IF_NZ (xstr);
2080
2081 } else {
2082 printf("\nIncorrect argument format for createVrmlFromString(%s).\n", _c_args);
2083 return JS_FALSE;
2084 }
2085
2086 /* save this value, in case we need it */
2087#if JS_VERSION < 185
2088 *(jsval*)(gglobal()->jsVRMLBrowser.JSCreate_global_return_val) = *rval;
2089#else
2090 JS_SET_RVAL(context,vp,*rval);
2091#endif
2092 return JS_TRUE;
2093}
2094
2095
2096JSBool
2097#if JS_VERSION < 185
2098VrmlBrowserCreateVrmlFromURL(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
2099#else
2100VrmlBrowserCreateVrmlFromURL(JSContext *context, uintN argc, jsval *vp) {
2101 jsval *argv = JS_ARGV(context,vp);
2102 jsval _my_rval;
2103 jsval *rval = &_my_rval;
2104#endif
2105 JSString *_str[2];
2106 JSClass *_cls[2];
2107 SFNodeNative *oldPtr;
2108 char *fieldStr,
2109 *_costr0;
2110 struct X3D_Node *myptr;
2111 #define myFileSizeLimit 4000
2112
2113/* DJ Tue May 4 21:25:15 BST 2010 Old stuff, no longer applicable
2114 int count;
2115 int offset;
2116 int fromtype;
2117 int xxx;
2118 int myField;
2119 char *address;
2120 struct X3D_Group *subtree;
2121*/
2122 resource_item_t *res = NULL;
2123 int fieldInt;
2124 int offs;
2125 int type;
2126 int accessType;
2127 struct Multi_String url;
2128
2129
2130 #ifdef JSVERBOSE
2131 printf ("JS start of createVrmlFromURL\n");
2132 #endif
2133
2134 /* rval is always zero, so lets just set it */
2135#if JS_VERSION < 185
2136 *rval = INT_TO_JSVAL(0);
2137#else
2138 *rval = INT_TO_JSVAL(0); //JSVAL_ZERO;
2139#endif
2140
2141 /* first parameter - expect a MFString Object here */
2142 if (JSVAL_IS_OBJECT(argv[0])) {
2143 if ((_cls[0] = JS_GET_CLASS(context, JSVAL_TO_OBJECT(argv[0]))) == NULL) {
2144 printf( "JS_GetClass failed for arg 0 in VrmlBrowserLoadURL.\n");
2145 return JS_FALSE;
2146 }
2147 } else {
2148 printf ("VrmlBrowserCreateVrmlFromURL - expect first parameter to be an object\n");
2149 return JS_FALSE;
2150 }
2151
2152 /* second parameter - expect a SFNode Object here */
2153 if (JSVAL_IS_OBJECT(argv[1])) {
2154 if ((_cls[1] = JS_GET_CLASS(context, JSVAL_TO_OBJECT(argv[1]))) == NULL) {
2155 printf( "JS_GetClass failed for arg 1 in VrmlBrowserLoadURL.\n");
2156 return JS_FALSE;
2157 }
2158 } else {
2159 printf ("VrmlBrowserCreateVrmlFromURL - expect first parameter to be an object\n");
2160 return JS_FALSE;
2161 }
2162
2163 #ifdef JSVERBOSE
2164 printf ("JS createVrml - step 2\n");
2165 printf ("JS create - we should havve a MFString and SFNode, have :%s: :%s:\n",(_cls[0])->name, (_cls[1])->name);
2166 #endif
2167
2168 /* make sure these 2 objects are really MFString and SFNode */
2169 if (memcmp("MFString", (_cls[0])->name, strlen((_cls[0])->name)) != 0 &&
2170 memcmp("SFNode", (_cls[1])->name, strlen((_cls[1])->name)) != 0) {
2171 printf( "Incorrect arguments in VrmlBrowserLoadURL.\n");
2172 return JS_FALSE;
2173 }
2174
2175 /* third parameter should be a string */
2176 if (JSVAL_IS_STRING(argv[2])) {
2177 _str[1] = JSVAL_TO_STRING(argv[2]);
2178#if JS_VERSION < 185
2179 fieldStr = JS_GetStringBytes(_str[1]);
2180#else
2181 fieldStr = JS_EncodeString(context,_str[1]);
2182#endif
2183 #ifdef JSVERBOSE
2184 printf ("field string is :%s:\n",fieldStr);
2185 #endif
2186 } else {
2187 printf ("Expected a string in createVrmlFromURL\n");
2188 return JS_FALSE;
2189 }
2190
2191 #ifdef JSVERBOSE
2192 printf ("passed object type tests\n");
2193 #endif
2194
2195 /* get the URL listing as a string */
2196 _str[0] = JS_ValueToString(context, argv[0]);
2197#if JS_VERSION < 185
2198 _costr0 = JS_GetStringBytes(_str[0]);
2199#else
2200 _costr0 = JS_EncodeString(context,_str[0]);
2201#endif
2202
2203
2204 #ifdef JSVERBOSE
2205 printf ("URL string is %s\n",_costr0);
2206 #endif
2207
2208
2209 /* get a pointer to the SFNode structure, in order to properly place the new string */
2210 if ((oldPtr = (SFNodeNative *)JS_GetPrivateFw(context, JSVAL_TO_OBJECT(argv[1]))) == NULL) {
2211 printf( "JS_GetPrivate failed in VrmlBrowserLoadURL for SFNode parameter.\n");
2212#if JS_VERSION >= 185
2213 JS_free(context,_costr0);
2214 JS_free(context,fieldStr);
2215#endif
2216 return JS_FALSE;
2217 }
2218 myptr = X3D_NODE(oldPtr->handle);
2219 if (myptr == NULL) {
2220 printf ("CreateVrmlFromURL, internal error - SFNodeNative memory pointer is NULL\n");
2221#if JS_VERSION >= 185
2222 JS_free(context,_costr0);
2223 JS_free(context,fieldStr);
2224#endif
2225 return JS_FALSE;
2226 }
2227
2228
2229 #ifdef JSVERBOSE
2230 printf ("SFNode handle %d, old X3DString %s\n",oldPtr->handle, oldPtr->X3DString);
2231 printf ("myptr %d\n",myptr);
2232 printf ("points to a %s\n",stringNodeType(myptr->_nodeType));
2233 #endif
2234
2235
2236 /* bounds checks */
2237 if (sizeof (_costr0) > (myFileSizeLimit-200)) {
2238 printf ("VrmlBrowserCreateVrmlFromURL, url too long...\n");
2239#if JS_VERSION >= 185
2240 JS_free(context,_costr0);
2241 JS_free(context,fieldStr);
2242#endif
2243 return JS_FALSE;
2244 }
2245
2246 /* ok - here we have:
2247 _costr0 : the url string array; eg: [ "vrml.wrl" ]
2248 opldPtr : pointer to a SFNode, with oldPtr->handle as C memory location.
2249 fielsStr : the field to send this to, eg: addChildren
2250 */
2251
2252 url.n = 0;
2253 url.p = NULL;
2254
2255 /* parse the string, put it into the "url" struct defined here */
2256 Parser_scanStringValueToMem(X3D_NODE(&url),0,FIELDTYPE_MFString, _costr0, FALSE);
2257
2258 /* find a file name that exists. If not, return JS_FALSE */
2259 res = resource_create_multi(&url);
2260 res->whereToPlaceData = myptr;
2261
2262
2263 /* lets see if this node has a routed field fromTo = 0 = from node, anything else = to node */
2264 fieldInt = findRoutedFieldInFIELDNAMES (myptr, fieldStr, TRUE);
2265
2266 if (fieldInt >=0) {
2267 findFieldInOFFSETS(myptr->_nodeType, fieldInt, &offs, &type, &accessType);
2268 } else {
2269 ConsoleMessage ("Can not find field :%s: in nodeType :%s:",fieldStr,stringNodeType(myptr->_nodeType));
2270#if JS_VERSION >= 185
2271 JS_free(context,_costr0);
2272 JS_free(context,fieldStr);
2273#endif
2274 return JS_FALSE;
2275 }
2276
2277 /* printf ("type of field %s, accessType %s\n",stringFieldtypeType(type),stringKeywordType(accessType)); */
2278 res->offsetFromWhereToPlaceData = offs;
2279 parser_process_res_VRML_X3D(res);
2280 //send_resource_to_parser(res);
2281 //resource_wait(res);
2282 //
2283 //if (res->status == ress_parsed) {
2284 // /* Cool :) */
2285 //}
2286
2287 MARK_EVENT(myptr,offs);
2288#if JS_VERSION >= 185
2289 JS_SET_RVAL(context,vp,*rval);
2290 JS_free(context,fieldStr);
2291 JS_free(context,_costr0);
2292#endif
2293 return JS_TRUE;
2294}
2295
2296JSBool
2297#if JS_VERSION < 185
2298VrmlBrowserAddRoute(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
2299 jsval _rval = INT_TO_JSVAL(0);
2300#else
2301VrmlBrowserAddRoute(JSContext *context, uintN argc, jsval *vp) {
2302 JSObject *obj = JS_THIS_OBJECT(context,vp);
2303 jsval *argv = JS_ARGV(context,vp);
2304#endif
2305 if (!doVRMLRoute(context, obj, argc, argv, "addRoute")) {
2306 printf( "doVRMLRoute failed in VrmlBrowserAddRoute.\n");
2307 return JS_FALSE;
2308 }
2309#if JS_VERSION < 185
2310 *rval = _rval;
2311#else
2312 JS_SET_RVAL(context,vp,INT_TO_JSVAL(0)); //JSVAL_ZERO);
2313#endif
2314 return JS_TRUE;
2315}
2316
2317JSBool
2318#if JS_VERSION < 185
2319VrmlBrowserPrint(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
2320 jsval _rval = INT_TO_JSVAL(0);
2321#else
2322VrmlBrowserPrint(JSContext *context, uintN argc, jsval *vp) {
2323 JSObject *obj = JS_THIS_OBJECT(context,vp);
2324 jsval *argv = JS_ARGV(context,vp);
2325#endif
2326 unsigned int count;
2327 JSString *_str;
2328 char *_id_c;
2329
2330 UNUSED (context); UNUSED(obj);
2331 /* printf ("FreeWRL:javascript: "); */
2332 for (count=0; count < argc; count++) {
2333 if (JSVAL_IS_STRING(argv[count])) {
2334 _str = JSVAL_TO_STRING(argv[count]);
2335#if JS_VERSION < 185
2336 _id_c = JS_GetStringBytes(_str);
2337#else
2338 _id_c = JS_EncodeString(context,_str);
2339#endif
2340 // OLD_IPHONE_AQUA #if defined(AQUA) || defined(_MSC_VER)
2341 #if defined(AQUA) || defined(_MSC_VER)
2342 ConsoleMessage(_id_c); /* statusbar hud */
2343 gglobal()->ConsoleMessage.consMsgCount = 0; /* reset the "Maximum" count */
2344 #else
2345 #ifdef HAVE_NOTOOLKIT
2346 printf ("%s", _id_c);
2347 #else
2348 printf ("%s\n", _id_c);
2349 ConsoleMessage(_id_c); /* statusbar hud */
2350 gglobal()->ConsoleMessage.consMsgCount = 0; /* reset the "Maximum" count */
2351 #endif
2352 #endif
2353#if JS_VERSION >= 185
2354 JS_free(context,_id_c);
2355#endif
2356 } else {
2357 /* printf ("unknown arg type %d\n",count); */
2358 }
2359 }
2360 /* the \n should be done with println below, or in javascript print("\n");
2361 except web3d V3 specs don't have Browser.println so print will do \n like the old days*/
2362 // OLD_IPHONE_AQUA #if defined(AQUA) || defined(_MSC_VER)
2363 #if defined(AQUA) || defined(_MSC_VER)
2364 ConsoleMessage("\n"); /* statusbar hud */
2365 gglobal()->ConsoleMessage.consMsgCount = 0; /* reset the "Maximum" count */
2366 #elif !defined(_MSC_VER)
2367 #ifdef HAVE_NOTOOLKIT
2368 printf ("\n");
2369 #endif
2370 #endif
2371#if JS_VERSION < 185
2372 *rval = _rval;
2373#else
2374 JS_SET_RVAL(context,vp,INT_TO_JSVAL(0)); //JSVAL_ZERO);
2375#endif
2376 return JS_TRUE;
2377}
2378
2379JSBool
2380#if JS_VERSION < 185
2381VrmlBrowserPrintln(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
2382 VrmlBrowserPrint(context,obj,argc,argv,rval);
2383#else
2384VrmlBrowserPrintln(JSContext *context, uintN argc, jsval *vp) {
2385 /* note, vp holds rval, since it is set in here we should be good */
2386 VrmlBrowserPrint(context,argc,vp);
2387#endif
2388 // OLD_IPHONE_AQUA #if defined(AQUA) || defined(_MSC_VER)
2389 #if defined(AQUA) || defined(_MSC_VER)
2390 //ConsoleMessage("\n"); /* statusbar hud */
2391 gglobal()->ConsoleMessage.consMsgCount = 0; /* reset the "Maximum" count */
2392 #else
2393 #ifdef HAVE_NOTOOLKIT
2394 printf ("\n");
2395 #endif
2396 #endif
2397 return JS_TRUE;
2398}
2399
2400JSBool
2401#if JS_VERSION < 185
2402VrmlBrowserDeleteRoute(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
2403 jsval _rval = INT_TO_JSVAL(0);
2404#else
2405VrmlBrowserDeleteRoute(JSContext *context, uintN argc, jsval *vp) {
2406 JSObject *obj = JS_THIS_OBJECT(context,vp);
2407 jsval *argv = JS_ARGV(context,vp);
2408#endif
2409 if (!doVRMLRoute(context, obj, argc, argv, "deleteRoute")) {
2410 printf( "doVRMLRoute failed in VrmlBrowserDeleteRoute.\n");
2411 return JS_FALSE;
2412 }
2413#if JS_VERSION < 185
2414 *rval = _rval;
2415#else
2416 JS_SET_RVAL(context,vp,INT_TO_JSVAL(0)); //JSVAL_ZERO);
2417#endif
2418 return JS_TRUE;
2419}
2420
2421/****************************************************************************************/
2422
2423
2424/****************************************************************************************************/
2425
2426/* internal to add/remove a ROUTE */
2427static JSBool doVRMLRoute(JSContext *context, JSObject *obj, uintN argc, jsval *argv, const char *callingFunc) {
2428 JSObject *fromNodeObj, *toNodeObj;
2429 SFNodeNative *fromNative, *toNative;
2430 JSClass *_cls[2];
2431 char
2432 *fromFieldString, *toFieldString,
2433 *_c_args =
2434 "SFNode fromNode, SFString fromEventOut, SFNode toNode, SFString toEventIn",
2435#if JS_VERSION < 185
2436 *_c_format = "o s o s";
2437#else
2438 *_c_format = "oSoS";
2439 JSString *fromFieldStringJS, *toFieldStringJS;
2440#endif
2441 struct X3D_Node *fromNode;
2442 struct X3D_Node *toNode;
2443 int fromOfs, toOfs, len;
2444 int fromtype, totype;
2445 int xxx;
2446 int myField;
2447
2448 /* first, are there 4 arguments? */
2449 if (argc != 4) {
2450 printf ("Problem with script - add/delete route command needs 4 parameters\n");
2451 return JS_FALSE;
2452 }
2453
2454 /* get the arguments, and ensure that they are obj, string, obj, string */
2455 if (JS_ConvertArguments(context, argc, argv, _c_format,
2456#if JS_VERSION < 185
2457 &fromNodeObj, &fromFieldString, &toNodeObj, &toFieldString)) {
2458#else
2459 &fromNodeObj, &fromFieldStringJS, &toNodeObj, &toFieldStringJS)) {
2460 fromFieldString = JS_EncodeString(context,fromFieldStringJS);
2461 toFieldString = JS_EncodeString(context,toFieldStringJS);
2462#endif
2463 if ((_cls[0] = JS_GET_CLASS(context, fromNodeObj)) == NULL) {
2464 printf("JS_GetClass failed for arg 0 in doVRMLRoute called from %s.\n",
2465 callingFunc);
2466 return JS_FALSE;
2467 }
2468 if ((_cls[1] = JS_GET_CLASS(context, toNodeObj)) == NULL) {
2469 printf("JS_GetClass failed for arg 2 in doVRMLRoute called from %s.\n",
2470 callingFunc);
2471 return JS_FALSE;
2472 }
2473
2474 /* make sure these are both SFNodes */
2475 if (memcmp("SFNode", (_cls[0])->name, strlen((_cls[0])->name)) != 0 &&
2476 memcmp("SFNode", (_cls[1])->name, strlen((_cls[1])->name)) != 0) {
2477 printf("\nArguments 0 and 2 must be SFNode in doVRMLRoute called from %s(%s): %s\n",
2478 callingFunc, _c_args, callingFunc);
2479 return JS_FALSE;
2480 }
2481
2482 /* get the "private" data for these nodes. It will consist of a SFNodeNative structure */
2483 if ((fromNative = (SFNodeNative *)JS_GetPrivateFw(context, fromNodeObj)) == NULL) {
2484 printf ("problem getting native props\n");
2485 return JS_FALSE;
2486 }
2487 if ((toNative = (SFNodeNative *)JS_GetPrivateFw(context, toNodeObj)) == NULL) {
2488 printf ("problem getting native props\n");
2489 return JS_FALSE;
2490 }
2491 /* get the "handle" for the actual memory pointer */
2492 fromNode = X3D_NODE(fromNative->handle);
2493 toNode = X3D_NODE(toNative->handle);
2494
2495 #ifdef JSVERBOSE
2496 printf ("routing from a node of type %s to a node of type %s\n",
2497 stringNodeType(fromNode->_nodeType),
2498 stringNodeType(toNode->_nodeType));
2499 #endif
2500
2501 /* From field */
2502 /* try finding it, maybe with a "set_" or "changed" removed */
2503 myField = findRoutedFieldInFIELDNAMES(fromNode,fromFieldString,0);
2504 if (myField == -1)
2505 myField = findRoutedFieldInFIELDNAMES(fromNode,fromFieldString,1);
2506
2507 /* find offsets, etc */
2508 findFieldInOFFSETS(fromNode->_nodeType, myField, &fromOfs, &fromtype, &xxx);
2509
2510 /* To field */
2511 /* try finding it, maybe with a "set_" or "changed" removed */
2512 myField = findRoutedFieldInFIELDNAMES(toNode,toFieldString,0);
2513 if (myField == -1)
2514 myField = findRoutedFieldInFIELDNAMES(toNode,toFieldString,1);
2515
2516 /* find offsets, etc */
2517 findFieldInOFFSETS(toNode->_nodeType, myField, &toOfs, &totype, &xxx);
2518
2519 /* do we have a mismatch here? */
2520 if (fromtype != totype) {
2521 printf ("Javascript routing problem - can not route from %s to %s\n",
2522 stringNodeType(fromNode->_nodeType),
2523 stringNodeType(toNode->_nodeType));
2524 return JS_FALSE;
2525 }
2526
2527 len = returnRoutingElementLength(totype);
2528
2529 jsRegisterRoute(fromNode, fromOfs, toNode, toOfs, len,callingFunc);
2530
2531#if JS_VERSION >= 185
2532 JS_free(context,fromFieldString);
2533 JS_free(context,toFieldString);
2534#endif
2535 } else {
2536 printf( "\nIncorrect argument format for %s(%s).\n",
2537 callingFunc, _c_args);
2538 return JS_FALSE;
2539 }
2540
2541 return JS_TRUE;
2542}
2543
2544//dug9 - first look at x3dbrowser and x3dscene/executionContext
2545#ifdef X3DBROWSER
2546/* The Browser's supportedComponents and supportedProfiles are statically defined
2547 in 'bits and pieces' in generatedCode.c and capabilitiesHandler.c and Structs.h.
2548 The Scene/ExecutionContext Profile and Components should be created during parsing
2549 (as of Aug 3, 2013 the parser calls handleProfile() or handleComponent() which
2550 just complains with printfs if freewrl can't handle the scene, and doesn't save them)
2551
2552 For the browser's supportedComponents and supportedProfiles, we'll have
2553 indexable arrays, and on getting an index, we'll construct a throwaway JS object.
2554*/
2555
2556
2557
2558
2559#endif
2560/*
2561ComonentInfo{
2562String name;
2563Numeric level;
2564String Title;
2565String providerUrl;
2566}
2567
2568ComponentInfoArray{
2569numeric length;
2570ComponentInfo [integer index];
2571}
2572
2573
2574ProfileInfo{
2575String name;
2576Numeric level;
2577String Title;
2578String providerUrl;
2579ComonentInfoArray components;
2580}
2581ProfileInfoArray{
2582numeric length;
2583ProfileInfo [integer index];
2584}
2585
2586X3DFieldDefinition{
2587//properties
2588String name;
2589numeric accessType; //e.g.. inputOnly
2590numeric dataType; //e.g. SFBool
2591}
2592
2593FieldDefinitionArray{
2594numeric length;
2595X3DFieldDefinition [integer index];
2596}
2597
2598
2599ProtoDeclaration{
2600//properties
2601String name;
2602FieldDefinitionArray fields;
2603Boolean isExternProto;
2604//functions
2605SFNode newInstance();
2606}
2607
2608ExternProtoDeclaration : ProtoDeclaration {
2609//properties
2610MFString urls;
2611numeric loadState;
2612//functions
2613void loadNow();
2614}
2615
2616ProtoDeclarationArray{
2617numeric length;
2618X3DProtoDeclaration [integer index];
2619}
2620
2621ExternProtoDeclarationArray{
2622numeric length;
2623X3DExternProtoDeclaration [integer index];
2624}
2625
2626Route{
2627}
2628RouteArray{
2629numeric length;
2630Route [integer index];
2631}
2632
2633
2634ExecutionContext{
2635//properties
2636String specificationVersion;
2637String encoding;
2638ProfileInfo profile;
2639ComponentInfoArray components;
2640String worldURL;
2641MFNode rootNodes; //R + writable except in protoInstance
2642ProtoDeclarationArray protos; //RW
2643ExternProtoDeclarationArray externprotos; //RW
2644RouteArray routes;
2645//functions
2646X3DRoute addRoute(SFNode fromNode, String fromReadableField, SFNode toNode, String toWriteableField);
2647void deleteRoute(X3DRoute route);
2648SFNode createNode(String x3dsyntax);
2649SFNode createProto(String x3dsyntax);
2650SFNode getImportedNode(String defname, String);
2651void updateImportedNode(String defname, String);
2652void removeImportedNode(String defname);
2653SFNode getNamedNode(String defname):
2654void updateNamedNode(String defname, SFNode);
2655void removeNamedNode(String defname);
2656}
2657
2658Scene : ExecutionContext{
2659//properties
2660String specificationVersion;
2661//functions
2662void setMetaData(String name, String value);
2663String getMetaData(String name);
2664SFNode getExportedNode(String defname);
2665void updateExportedNode(String defname, SFNode node);
2666void removeExportedNode(String defname);
2667}
2668
2669//just createX3DFromString, createX3DFromURL and replaceWorld differ in signature between VRML and X3D browser classes
2670X3DBrowser{
2671//properties
2672String name;
2673String version;
2674numeric currentSpeed;
2675numeric currentFrameRate;
2676String description; //R/W
2677CompnentInfoArray supportedComponents;
2678ProfileInfoArray supportedProfiles;
2679//functions
2680X3DScene currentScene; //since X3DScene : X3DExecutionContext, use Scene w/flag
2681void replaceWorld(X3DScene);
2682X3DScene createX3DFromString(String x3dsyntax);
2683X3DScene createX3DFromURL(MFString url, String callbackFunctionName, Object cbContextObject);
2684void loadURL(MFString url, MFString parameter);
2685X3DScene importDocument(DOMNode domNodeObject);
2686void getRenderingProperty(String propertyName);
2687void print(Object or String);
2688void println(Object or String);
2689}
2690*/
2691
2692#endif /* !(defined(JAVASCRIPT_STUB) || defined(JAVASCRIPT_DUK) */
2693#endif