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