FreeWRL / FreeX3D 4.3.0
jsVRML_SFClasses_sm.cpp
1/*
2
3 A substantial amount of code has been adapted from js/src/js.c,
4 which is the sample application included with the javascript engine.
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#ifdef JAVASCRIPT_SM
30#if defined(JS_SMCPP)
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#define JS_VERSION 187
36//#define JS_THREADSAFE 1 //by default in 186+
37int JS_SetPrivateFw(JSContext *cx, JSObject* obj, void *data);
38JSObject* JS_NewGlobalObjectFw(JSContext *cx, JSClass *clasp); //, JSPrincipals *princ);
39void * JS_GetPrivateFw(JSContext *cx,JSObject*_obj);
40JSObject* JS_GetParentFw(JSContext *cx, JSObject *me);
41JSObject * JS_ConstructObjectWithArgumentsFw(JSContext *cx, JSClass *clasp, JSObject *parent, unsigned argc, jsval *argv);
42JSObject * JS_ConstructObjectFw(JSContext *cx, JSClass *clasp, void *whatever, JSObject *parent);
43JSObject * JS_GetPrototypeFw(JSContext *cx, JSObject * obj);
44JSClass * JS_GetClassFw(JSContext *cx, JSObject * obj);
45#define STRING_SIZE 256
46#define uintN unsigned
47#define intN int
48#define jsint int32_t
49#define jsuint uint32_t
50#define int32 int32_t
51#define jsdouble double
52
53#define JS_FinalizeStub NULL
54#define JS_GET_CLASS JS_GetClassFw
55JSBool JS_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval);
56//#define JSVAL_IS_OBJECT(retval) JSVAL_IS_OBJECT_OR_NULL_IMPL(retval)
57
58#ifndef IBOOL
59typedef int IBOOL;
60#endif
61typedef IBOOL _Bool;
62//typedef _Bool bool;
63
64
65
66extern "C" {
67#include <system.h>
68//#include <system_threads.h>
69//#include <display.h>
70#include <internal.h>
71
72//#include <libFreeWRL.h>
73//#include <list.h>
74#include "scenegraph/LinearAlgebra.h"
75#include "scenegraph/quaternion.h"
76//#include <io_files.h>
77
78//#include "../vrml_parser/Structs.h"
79//#include "../main/headers.h"
80#include "../main/ProdCon.h"
81//#include "../vrml_parser/CParseGeneral.h"
82//#include "../main/Snapshot.h"
83//#include "../scenegraph/LinearAlgebra.h"
84//#include "../scenegraph/Collision.h"
85//#include "../scenegraph/quaternion.h"
86//#include "../scenegraph/Viewer.h"
87//#include "../input/SensInterps.h"
88//#include "../x3d_parser/Bindable.h"
89//#include "../input/InputFunctions.h"
90
91//#include <system_js.h>
92#include "JScript.h"
93#include "CScripts.h"
94#include "jsNative.h"
95
96void Parser_scanStringValueToMem_B(union anyVrml* any, indexT ctype, const char *value, int isXML);
97} //extern "C"
98
99#include "jsUtils_sm.h"
100#include "jsVRMLClasses_sm.h"
101
102//#include "JScript.h"
103
104
105/********************************************************/
106/* */
107/* Second part - SF classes */
108/* */
109/********************************************************/
110
111/* from http://www.cs.rit.edu/~ncs/color/t_convert.html */
112double dMIN3(double a, double b, double c) {
113 double min;
114 if((a<b)&&(a<c))min=a; else if((b<a)&&(b<c))min=b; else min=c; return min;
115}
116
117double dMAX3(double a, double b, double c) {
118 double max;
119 if((a>b)&&(a>c))max=a; else if((b>a)&&(b>c))max=b; else max=c; return max;
120}
121
122void convertRGBtoHSV(double r, double g, double b, double *h, double *s, double *v) {
123 double my_min, my_max, delta;
124
125 my_min = dMIN3( r, g, b );
126 my_max = dMAX3( r, g, b );
127 *v = my_max; /* v */
128 delta = my_max - my_min;
129 if( my_max != 0 )
130 *s = delta / my_max; /* s */
131 else {
132 /* r = g = b = 0 */ /* s = 0, v is undefined */
133 *s = 0;
134 *h = -1;
135 return;
136 }
137 if( r == my_max )
138 *h = ( g - b ) / delta; /* between yellow & magenta */
139 else if( g == my_max )
140 *h = 2 + ( b - r ) / delta; /* between cyan & yellow */
141 else
142 *h = 4 + ( r - g ) / delta; /* between magenta & cyan */
143 *h *= 60; /* degrees */
144 if( *h < 0 )
145 *h += 360;
146}
147void convertHSVtoRGB( double h, double s, double v ,double *r, double *g, double *b)
148{
149 int i;
150 double f, p, q, t;
151 if( s == 0 ) {
152 /* achromatic (grey) */
153 *r = *g = *b = v;
154 return;
155 }
156 h /= 60; /* sector 0 to 5 */
157 i = (int) floor( h );
158 f = h - i; /* factorial part of h */
159 p = v * ( 1 - s );
160 q = v * ( 1 - s * f );
161 t = v * ( 1 - s * ( 1 - f ) );
162 switch( i ) {
163 case 0: *r = v; *g = t; *b = p; break;
164 case 1: *r = q; *g = v; *b = p; break;
165 case 2: *r = p; *g = v; *b = t; break;
166 case 3: *r = p; *g = q; *b = v; break;
167 case 4: *r = t; *g = p; *b = v; break;
168 default: *r = v; *g = p; *b = q; break;
169 }
170}
171
172JSBool
173#if JS_VERSION < 185
174SFColorGetHSV(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
175#else
176SFColorGetHSV(JSContext *cx, uintN argc, jsval *vp) {
177 JSObject *obj = JS_THIS_OBJECT(cx,vp);
178 jsval *argv = JS_ARGV(cx,vp);
179#endif
180 JSObject *result;
181 double xp[3];
182 jsval _v;
183 int i;
184 float *cc;
185
186 UNUSED(argv);
187 if (argc != 0) {
188 printf ("SFColorGetHSV; arguments found but not expected\n");
189 return JS_FALSE;
190 }
191
192 /* get the RGB values */
193 if(SM_method()==2){
194 AnyNative *ptr;
195 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
196 printf( "JS_GetPrivate failed in SFColorGetHSV.\n");
197 return JS_FALSE;
198 }
199 cc = ptr->v->sfcolor.c;
200
201 }else{
202 SFColorNative *ptr;
203 if ((ptr = (SFColorNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
204 printf( "JS_GetPrivate failed in SFColorGetHSV.\n");
205 return JS_FALSE;
206 }
207 cc = ptr->v.c;
208 }
209 /* convert rgb to hsv */
210 convertRGBtoHSV(cc[0], cc[1], cc[2],&xp[0],&xp[1],&xp[2]);
211
212 #ifdef JSVRMLCLASSESVERBOSE
213 printf("hsv code, orig rgb is %.9g %.9g %.9g\n", (ptr->v).c[0], (ptr->v).c[1], (ptr->v).c[2]);
214 printf ("hsv conversion is %lf %lf %lf\n",xp[0],xp[1],xp[2]);
215 #endif
216 // http://www.web3d.org/documents/specifications/19777-1/V3.3/Part1/functions.html#SFColor
217 // - specs want a numeric[3] return val
218 result = JS_NewArrayObject(cx, 3, NULL);
219 ADD_ROOT(cx, result);
220 for(i=0; i<3; i++) {
221 if (JS_NewNumberValue(cx, xp[i],&_v) == JS_FALSE) {
222 printf( "JS_NewDouble failed for %f in SFColorGetHSV.\n", xp[i]);
223 return JS_FALSE;
224 }
225 JS_SetElement(cx, result, (jsint)i, &_v);
226 }
227
228 /* JAS - should we remove this here, or on finalize? JS_RemoveRoot(cx, &result); */
229#if JS_VERSION < 185
230 *rval = OBJECT_TO_JSVAL(result);
231#else
232 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(result));
233#endif
234 return JS_TRUE;
235}
236
237JSBool
238#if JS_VERSION < 185
239SFColorSetHSV(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
240#else
241SFColorSetHSV(JSContext *cx, uintN argc, jsval *vp) {
242 JSObject *obj = JS_THIS_OBJECT(cx,vp);
243 jsval *argv = JS_ARGV(cx,vp);
244#endif
245 double hue, saturation, value;
246 double red,green,blue;
247 float *cc;
248
249 if(SM_method() == 2){
250 AnyNative *ptr;
251 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
252 printf( "JS_GetPrivate failed in SSFColorSetHSV.\n");
253 return JS_FALSE;
254 }
255 if(ptr->valueChanged)
256 (*ptr->valueChanged) ++;
257 cc = ptr->v->sfcolor.c;
258 }else{
259 SFColorNative *ptr;
260 if ((ptr = (SFColorNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
261 printf( "JS_GetPrivate failed in SFColorSetHSV.\n");
262 return JS_FALSE;
263 }
264 ptr->valueChanged ++;
265 cc = ptr->v.c;
266 }
267
268 if (!JS_ConvertArguments(cx, argc, argv, "d d d", &hue, &saturation, &value)) {
269 printf( "JS_ConvertArguments failed in SFColorSetHSV.\n");
270 return JS_FALSE;
271 }
272
273 /* do conversion here!!! */
274 #ifdef JSCLASSESVERBOSE
275 printf("hsv code, orig rgb is %.9g %.9g %.9g\n", (ptr->v).c[0], (ptr->v).c[1], (ptr->v).c[2]);
276 printf ("SFColorSetHSV, have %lf %lf %lf\n",hue,saturation,value);
277 #endif
278
279 convertHSVtoRGB(hue,saturation,value, &red, &green, &blue);
280 cc[0] = (float) red;
281 cc[1] = (float) green;
282 cc[2] = (float) blue;
283 #ifdef JSCLASSESVERBOSE
284 printf("hsv code, now rgb is %.9g %.9g %.9g\n", (ptr->v).c[0], (ptr->v).c[1], (ptr->v).c[2]);
285 #endif
286
287#if JS_VERSION < 185
288 *rval = OBJECT_TO_JSVAL(obj);
289#else
290 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
291#endif
292
293 return JS_TRUE;
294}
295
296JSBool
297#if JS_VERSION < 185
298SFColorToString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
299#else
300SFColorToString(JSContext *cx, uintN argc, jsval *vp) {
301 JSObject *obj = JS_THIS_OBJECT(cx,vp);
302 jsval *argv = JS_ARGV(cx,vp);
303#endif
304 JSString *_str;
305 char _buff[STRING];
306 float *cc;
307
308 UNUSED(argc);
309 UNUSED(argv);
310 if(SM_method()==2){
311 AnyNative *ptr;
312 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
313 printf( "JS_GetPrivate failed in SFColorToString.\n");
314 return JS_FALSE;
315 }
316 cc = ptr->v->sfcolor.c;
317 }else{
318 SFColorNative *ptr;
319 if ((ptr = (SFColorNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
320 printf( "JS_GetPrivate failed in SFColorToString.\n");
321 return JS_FALSE;
322 }
323 cc = ptr->v.c;
324 }
325 memset(_buff, 0, STRING);
326 sprintf(_buff, "%.9g %.9g %.9g",
327 cc[0], cc[1], cc[2]);
328 _str = JS_NewStringCopyZ(cx, _buff);
329#if JS_VERSION < 185
330 *rval = STRING_TO_JSVAL(_str);
331#else
332 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
333#endif
334 return JS_TRUE;
335}
336
337JSBool
338#if JS_VERSION < 185
339SFColorAssign(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
340#else
341SFColorAssign(JSContext *cx, uintN argc, jsval *vp) {
342 JSObject *obj = JS_THIS_OBJECT(cx,vp);
343 jsval *argv = JS_ARGV(cx,vp);
344 JSString *_id_jsstr;
345#endif
346 JSObject *_from_obj;
347 char *_id_str;
348
349 UNUSED(_id_str); // compiler warning mitigation
350
351 if(SM_method() == 2){
352 AnyNative *lhs, *rhs;
353 if ((lhs = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
354 printf( "JS_GetPrivate failed for obj in SFColorAssign.\n");
355 return JS_FALSE;
356 }
357 //if (!JSVAL_IS_OBJECT(*vp))
358 if (!(*vp).isObject())
359 return JS_FALSE;
360 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*vp))) == NULL) {
361 printf("JS_ConvertArguments failed in SFColorAssign. \n");
362 return JS_FALSE;
363 }
364 AnyNativeAssign(lhs,rhs);
365 }else{
366 SFColorNative *ptr, *fptr;
367 if ((ptr = (SFColorNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
368 printf( "JS_GetPrivate failed for obj in SFColorAssign.\n");
369 return JS_FALSE;
370 }
371
372
373 CHECK_CLASS(cx,obj,argv,__FUNCTION__,SFColorClass)
374
375#if JS_VERSION < 185
376 if (!JS_ConvertArguments(cx, argc, argv, "o s", &_from_obj, &_id_str)) {
377#else
378 if (JS_ConvertArguments(cx, argc, argv, "oS", &_from_obj, &_id_jsstr) == JS_TRUE) {
379 _id_str = JS_EncodeString(cx,_id_jsstr);
380 } else {
381#endif
382 printf( "JS_ConvertArguments failed in SFColorAssign.\n");
383 return JS_FALSE;
384 }
385
386 CHECK_CLASS(cx,_from_obj,argv,__FUNCTION__,SFColorClass)
387
388 if ((fptr = (SFColorNative *)JS_GetPrivateFw(cx, _from_obj)) == NULL) {
389 printf( "JS_GetPrivate failed for _from_obj in SFColorAssign.\n");
390 return JS_FALSE;
391 }
392 #ifdef JSVRMLCLASSESVERBOSE
393 printf("SFColorAssign: obj = %p, id = \"%s\", from = %p\n", obj, _id_str, _from_obj);
394 #endif
395
396 SFColorNativeAssign(ptr, fptr);
397 }
398#if JS_VERSION < 185
399 *rval = OBJECT_TO_JSVAL(obj);
400#else
401 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
402#endif
403
404 return JS_TRUE;
405}
406
407JSBool
408#if JS_VERSION < 185
409SFColorConstr(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
410#else
411SFColorConstr(JSContext *cx, uintN argc, jsval *vp) {
412 JSObject *obj = JS_NewObject(cx,&SFColorClass,NULL,NULL);
413 jsval *argv = JS_ARGV(cx,vp);
414#endif
415 jsdouble pars[3];
416 float *cc;
417
418 ADD_ROOT(cx,obj)
419 if(SM_method() == 2){
420 AnyNative *any;
421 if((any = (AnyNative*)AnyNativeNew(FIELDTYPE_SFColor,NULL,NULL)) == NULL){
422 printf( "AnyfNativeNew failed in SFColorConstr.\n");
423 return JS_FALSE;
424 }
425 if (!JS_SetPrivateFw(cx, obj, any)) {
426 printf( "JS_SetPrivate failed in SFColorConstr.\n");
427 return JS_FALSE;
428 }
429 cc = any->v->sfvec3f.c;
430 }else{
431 SFColorNative *ptr;
432
433 if ((ptr = (SFColorNative *) SFColorNativeNew()) == NULL) {
434 printf( "SFColorNativeNew failed in SFColorConstr.\n");
435 return JS_FALSE;
436 }
437
438 //if (!JS_DefineProperties(cx, obj, SFColorProperties)) {
439 // printf( "JS_DefineProperties failed in SFColorConstr.\n");
440 // return JS_FALSE;
441 //}
442
443 if (!JS_SetPrivateFw(cx, obj, ptr)) {
444 printf( "JS_SetPrivate failed in SFColorConstr.\n");
445 return JS_FALSE;
446 }
447 cc = ptr->v.c;
448 ptr->valueChanged = 1;
449 }
450 if (argc == 0) {
451 cc[0] = (float) 0.0;
452 cc[1] = (float) 0.0;
453 cc[2] = (float) 0.0;
454 } else if (JS_ConvertArguments(cx, argc, argv, "d d d", &(pars[0]), &(pars[1]), &(pars[2]))) {
455 cc[0] = (float) pars[0];
456 cc[1] = (float) pars[1];
457 cc[2] = (float) pars[2];
458 } else {
459 printf( "Invalid arguments for SFColorConstr.\n");
460 return JS_FALSE;
461 }
462 #ifdef JSVRMLCLASSESVERBOSE
463 printf("SFColorConstr: obj = %p args = %d, %f %f %f\n",
464 obj, argc,
465 cc[0], cc[1], cc[2]);
466 #endif
467
468
469#if JS_VERSION < 185
470 *rval = OBJECT_TO_JSVAL(obj);
471#else
472 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
473#endif
474
475 return JS_TRUE;
476}
477
478
479JSBool
480#if JS_VERSION < 185
481SFColorGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
482#elif JS_VERSION == 185
483SFColorGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp){
484#else
485SFColorGetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JS::MutableHandle<JS::Value> hvp){
486 JSObject *obj = *hobj.address();
487 jsid iid = *hiid.address();
488 jsval *vp = hvp.address();
489
490#endif
491
492 jsdouble d;
493 float *cc;
494
495#if JS_VERSION >= 185
496 jsval id;
497 if (!JS_IdToValue(cx,iid,&id)) {
498 printf("JS_IdToValue failed in SFColorGetProperty.\n");
499 return JS_FALSE;
500 }
501#endif
502 if(SM_method()==2){
503 AnyNative *any;
504 if ((any = (AnyNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
505 printf( "JS_GetPrivate failed in SFColorGetProperty.\n");
506 return JS_FALSE;
507 }
508 cc = any->v->sfcolor.c;
509 }else{
510 SFColorNative *ptr;
511
512 if ((ptr = (SFColorNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
513 printf( "JS_GetPrivate failed in SFColorGetProperty.\n");
514 return JS_FALSE;
515 }
516 cc = ptr->v.c;
517 }
518 if (JSVAL_IS_INT(id)) {
519 switch (JSVAL_TO_INT(id)) {
520 case 0:
521 d = cc[0];
522 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
523 printf(
524 "JS_NewDouble failed for %f in SFColorGetProperty.\n",
525 d);
526 return JS_FALSE;
527 }
528 break;
529 case 1:
530 d = cc[1];
531 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
532 printf(
533 "JS_NewDouble failed for %f in SFColorGetProperty.\n",
534 d);
535 return JS_FALSE;
536 }
537 break;
538 case 2:
539 d = cc[2];
540 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
541 printf(
542 "JS_NewDouble failed for %f in SFColorGetProperty.\n",
543 d);
544 return JS_FALSE;
545 }
546 break;
547 }
548 }
549 return JS_TRUE;
550}
551
552JSBool
553#if JS_VERSION < 185
554SFColorSetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
555#elif JS_VERSION == 185
556SFColorSetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp){
557#else
558SFColorSetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
559 JSObject *obj = *hobj.address();
560 jsid iid = *hiid.address();
561 jsval *vp = hvp.address();
562#endif
563
564 jsval _val;
565 float *cc;
566
567#if JS_VERSION >= 185
568 jsval id;
569 if (!JS_IdToValue(cx,iid,&id)) {
570 printf("JS_IdToValue failed in SFColorSetProperty.\n");
571 return JS_FALSE;
572 }
573#endif
574 if(SM_method() == 2){
575 AnyNative *any;
576 if ((any = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
577 printf( "JS_GetPrivate failed in SFColorSetProperty.\n");
578 return JS_FALSE;
579 }
580 if(any->valueChanged)
581 (*any->valueChanged)++;
582 cc = any->v->sfcolor.c;
583 }else{
584 SFColorNative *ptr;
585
586 if ((ptr = (SFColorNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
587 printf( "JS_GetPrivate failed in SFColorSetProperty.\n");
588 return JS_FALSE;
589 }
590 ptr->valueChanged++;
591 #ifdef JSVRMLCLASSESVERBOSE
592 printf("SFColorSetProperty: obj = %p, id = %d, valueChanged = %d\n",
593 obj, JSVAL_TO_INT(id), ptr->valueChanged);
594 #endif
595 cc = ptr->v.c;
596 }
597 if (!JS_ConvertValue(cx, *vp, JSTYPE_NUMBER, &_val)) {
598 printf( "JS_ConvertValue failed in SFColorSetProperty.\n");
599 return JS_FALSE;
600 }
601
602 if (JSVAL_IS_INT(id)) {
603 switch (JSVAL_TO_INT(id)) {
604 case 0:
605#if JS_VERSION < 185
606 cc[0] = (float) *JSVAL_TO_DOUBLE(_val);
607#else
608 cc[0] = (float) JSVAL_TO_DOUBLE(_val);
609#endif
610 break;
611 case 1:
612#if JS_VERSION < 185
613 cc[1] = (float) *JSVAL_TO_DOUBLE(_val);
614#else
615 cc[1] = (float) JSVAL_TO_DOUBLE(_val);
616#endif
617 break;
618 case 2:
619#if JS_VERSION < 185
620 cc[2] = (float) *JSVAL_TO_DOUBLE(_val);
621#else
622 cc[2] = (float) JSVAL_TO_DOUBLE(_val);
623#endif
624 break;
625
626 }
627 }
628 return JS_TRUE;
629}
630
631/* copy code from SFColorGetHSV if the spec ever decides to implement this. */
632JSBool
633#if JS_VERSION < 185
634SFColorRGBAGetHSV(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
635#else
636SFColorRGBAGetHSV(JSContext *cx, uintN argc, jsval *vp) {
637 JSObject *obj = JS_THIS_OBJECT(cx,vp);
638 jsval *argv = JS_ARGV(cx,vp);
639#endif
640
641
642 JSObject *_arrayObj;
643 jsdouble hue = 0, saturation = 0, value = 0;
644 jsval _v;
645
646 UNUSED(obj);
647 UNUSED(argc);
648 UNUSED(argv);
649 /* do conversion here!!! */
650
651 if ((_arrayObj = JS_NewArrayObject(cx, 0, NULL)) == NULL) {
652 printf( "JS_NewArrayObject failed in SFColorRGBAGetHSV.\n");
653 return JS_FALSE;
654 }
655
656 /* construct new double before conversion? */
657 JS_NewNumberValue(cx,hue,&_v); /* was: _v = DOUBLE_TO_JSVAL(&hue); */
658 if (!JS_SetElement(cx, _arrayObj, 0, &_v)) {
659 printf( "JS_SetElement failed for hue in SFColorRGBAGetHSV.\n");
660 return JS_FALSE;
661 }
662 JS_NewNumberValue(cx,saturation,&_v); /* was: _v = DOUBLE_TO_JSVAL(&saturation); */
663 if (!JS_SetElement(cx, _arrayObj, 1, &_v)) {
664 printf( "JS_SetElement failed for saturation in SFColorRGBAGetHSV.\n");
665 return JS_FALSE;
666 }
667 JS_NewNumberValue(cx,value,&_v); /* was: _v = DOUBLE_TO_JSVAL(&value); */
668 if (!JS_SetElement(cx, _arrayObj, 2, &_v)) {
669 printf( "JS_SetElement failed for value in SFColorRGBAGetHSV.\n");
670 return JS_FALSE;
671 }
672#if JS_VERSION < 185
673 *rval = OBJECT_TO_JSVAL(_arrayObj);
674#else
675 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_arrayObj));
676#endif
677
678 return JS_TRUE;
679}
680
681/* implement later?? Copy most of code from SFColorSetHSV if we require this */
682JSBool
683#if JS_VERSION < 185
684SFColorRGBASetHSV(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
685#else
686SFColorRGBASetHSV(JSContext *cx, uintN argc, jsval *vp) {
687 JSObject *obj = JS_THIS_OBJECT(cx,vp);
688 jsval *argv = JS_ARGV(cx,vp);
689#endif
690
691 jsdouble hue, saturation, value;
692 float *cc;
693
694 if(SM_method()==2){
695 AnyNative *ptr;
696 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
697 printf( "JS_GetPrivate failed in SFColorRGBAToString.\n");
698 return JS_FALSE;
699 }
700 cc = ptr->v->sfcolorrgba.c;
701 }else{
703 if ((ptr = (SFColorRGBANative *)JS_GetPrivateFw(cx, obj)) == NULL) {
704 printf( "JS_GetPrivate failed in SFColorRGBAToString.\n");
705 return JS_FALSE;
706 }
707 cc = ptr->v.c;
708 }
709 if (!JS_ConvertArguments(cx, argc, argv, "d d d",
710 &hue, &saturation, &value)) {
711 printf( "JS_ConvertArguments failed in SFColorRGBASetHSV.\n");
712 return JS_FALSE;
713 }
714
715 /* do conversion here!!! NOT DOING ANYTHING - BUG */
716
717#if JS_VERSION < 185
718 *rval = OBJECT_TO_JSVAL(obj);
719#else
720 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
721#endif
722
723 return JS_TRUE;
724}
725
726JSBool
727#if JS_VERSION < 185
728SFColorRGBAToString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
729#else
730SFColorRGBAToString(JSContext *cx, uintN argc, jsval *vp) {
731 JSObject *obj = JS_THIS_OBJECT(cx,vp);
732 jsval *argv = JS_ARGV(cx,vp);
733#endif
734
735 JSString *_str;
736 char _buff[STRING];
737 float *cc;
738
739 UNUSED(argc);
740 UNUSED(argv);
741 if(SM_method()==2){
742 AnyNative *ptr;
743 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
744 printf( "JS_GetPrivate failed in SFColorRGBAToString.\n");
745 return JS_FALSE;
746 }
747 cc = ptr->v->sfcolorrgba.c;
748
749 }else{
751 if ((ptr = (SFColorRGBANative *)JS_GetPrivateFw(cx, obj)) == NULL) {
752 printf( "JS_GetPrivate failed in SFColorRGBAToString.\n");
753 return JS_FALSE;
754 }
755 cc = ptr->v.c;
756 }
757 memset(_buff, 0, STRING);
758 sprintf(_buff, "%.9g %.9g %.9g %.9g",
759 cc[0], cc[1], cc[2],cc[3]);
760 _str = JS_NewStringCopyZ(cx, _buff);
761
762#if JS_VERSION < 185
763 *rval = STRING_TO_JSVAL(_str);
764#else
765 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
766#endif
767
768 return JS_TRUE;
769}
770
771JSBool
772#if JS_VERSION < 185
773SFColorRGBAAssign(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
774#else
775SFColorRGBAAssign(JSContext *cx, uintN argc, jsval *vp) {
776 JSObject *obj = JS_THIS_OBJECT(cx,vp);
777 jsval *argv = JS_ARGV(cx,vp);
778 JSString *_id_jsstr;
779#endif
780
781 JSObject *_from_obj;
782 char *_id_str;
783
784 UNUSED(_id_str); // compiler warning mitigation
785 if(SM_method() == 2){
786 AnyNative *lhs, *rhs;
787 if ((lhs = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
788 printf( "JS_GetPrivate failed for obj in SFColorRGBAAssign.\n");
789 return JS_FALSE;
790 }
791 //if (!JSVAL_IS_OBJECT(*vp))
792 if (!(*vp).isObject())
793 return JS_FALSE;
794 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*vp))) == NULL) {
795 printf("JS_ConvertArguments failed in SFColorRGBAAssign. \n");
796 return JS_FALSE;
797 }
798 AnyNativeAssign(lhs,rhs);
799 }else{
800 SFColorRGBANative *ptr, *fptr;
801
802
803 if ((ptr = (SFColorRGBANative *)JS_GetPrivateFw(cx, obj)) == NULL) {
804 printf( "JS_GetPrivate failed for obj in SFColorRGBAAssign.\n");
805 return JS_FALSE;
806 }
807
808 CHECK_CLASS(cx,obj,argv,__FUNCTION__,SFColorRGBAClass)
809
810#if JS_VERSION < 185
811 if (!JS_ConvertArguments(cx, argc, argv, "o s", &_from_obj, &_id_str)) {
812#else
813 if (JS_ConvertArguments(cx, argc, argv, "oS", &_from_obj, &_id_jsstr) == JS_TRUE) {
814 _id_str = JS_EncodeString(cx,_id_jsstr);
815 } else {
816#endif
817 printf( "JS_ConvertArguments failed in SFColorRGBAAssign.\n");
818 return JS_FALSE;
819 }
820
821 CHECK_CLASS(cx,_from_obj,argv,__FUNCTION__,SFColorRGBAClass)
822
823 if ((fptr = (SFColorRGBANative *)JS_GetPrivateFw(cx, _from_obj)) == NULL) {
824 printf( "JS_GetPrivate failed for _from_obj in SFColorRGBAAssign.\n");
825 return JS_FALSE;
826 }
827 #ifdef JSVRMLCLASSESVERBOSE
828 printf("SFColorRGBAAssign: obj = %p, id = \"%s\", from = %p\n",
829 obj, _id_str, _from_obj);
830 #endif
831
832 SFColorRGBANativeAssign(ptr, fptr);
833 }
834#if JS_VERSION < 185
835 *rval = OBJECT_TO_JSVAL(obj);
836#else
837 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
838#endif
839
840 return JS_TRUE;
841}
842
843JSBool
844#if JS_VERSION < 185
845SFColorRGBAConstr(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
846#else
847SFColorRGBAConstr(JSContext *cx, uintN argc, jsval *vp) {
848 JSObject *obj = JS_NewObject(cx,&SFColorRGBAClass,NULL,NULL);
849 jsval *argv = JS_ARGV(cx,vp);
850#endif
851 float *cc;
852 jsdouble pars[4];
853
854 ADD_ROOT(cx,obj)
855 if(SM_method() == 2){
856 AnyNative *any;
857 if((any = (AnyNative*)AnyNativeNew(FIELDTYPE_SFColorRGBA,NULL,NULL)) == NULL){
858 printf( "AnyfNativeNew failed in SFColorRGBAConstr.\n");
859 return JS_FALSE;
860 }
861 if (!JS_SetPrivateFw(cx, obj, any)) {
862 printf( "JS_SetPrivate failed in SFColorRGBAConstr.\n");
863 return JS_FALSE;
864 }
865 cc = any->v->sfvec4f.c;
866 }else{
868
869 if ((ptr = (SFColorRGBANative *) SFColorNativeNew()) == NULL) {
870 printf( "SFColorRGBANativeNew failed in SFColorConstr.\n");
871 return JS_FALSE;
872 }
873
874 //if (!JS_DefineProperties(cx, obj, SFColorRGBAProperties)) {
875 // printf( "JS_DefineProperties failed in SFColorRGBAConstr.\n");
876 // return JS_FALSE;
877 //}
878
879 if (!JS_SetPrivateFw(cx, obj, ptr)) {
880 printf( "JS_SetPrivate failed in SFColorRGBAConstr.\n");
881 return JS_FALSE;
882 }
883 cc = ptr->v.c;
884 ptr->valueChanged = 1;
885
886 }
887 if (argc == 0) {
888 cc[0] = (float) 0.0;
889 cc[1] = (float) 0.0;
890 cc[2] = (float) 0.0;
891 cc[3] = (float) 0.0;
892 } else if (JS_ConvertArguments(cx, argc, argv, "d d d d",
893 &(pars[0]), &(pars[1]), &(pars[2]), &(pars[3]))) {
894 cc[0] = (float) pars[0];
895 cc[1] = (float) pars[1];
896 cc[2] = (float) pars[2];
897 cc[3] = (float) pars[3];
898 } else {
899 printf( "Invalid arguments for SFColorRGBAConstr.\n");
900 return JS_FALSE;
901 }
902
903
904
905 #ifdef JSVRMLCLASSESVERBOSE
906 printf("SFColorRGBAConstr: obj = %p %u args, %f %f %f %f\n",
907 obj, argc,
908 cc[0], cc[1], cc[2],cc[3]);
909 #endif
910#if JS_VERSION < 185
911 *rval = OBJECT_TO_JSVAL(obj);
912#else
913 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
914#endif
915
916 return JS_TRUE;
917}
918
919JSBool
920#if JS_VERSION < 185
921SFColorRGBAGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
922#elif JS_VERSION == 185
923SFColorRGBAGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp){
924 //jsval id;
925#else
926SFColorRGBAGetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JS::MutableHandle<JS::Value> hvp){
927 JSObject *obj = *hobj.address();
928 jsid iid = *hiid.address();
929 jsval *vp = hvp.address();
930 //jsval id = JS_NumberValue(0.0); = INT_TO_JSVAL(0)
931#endif
932
933 jsdouble d;
934 float *cc;
935
936#if JS_VERSION >= 185
937 jsval id;
938 if (!JS_IdToValue(cx,iid,&id)) {
939 printf("JS_IdToValue failed in SFColorRGBAGetProperty.\n");
940 return JS_FALSE;
941 }
942#endif
943
944 if(SM_method()==2){
945 AnyNative *any;
946 if ((any = (AnyNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
947 printf( "JS_GetPrivate failed in SFColorRGBAGetProperty.\n");
948 return JS_FALSE;
949 }
950 cc = any->v->sfcolorrgba.c;
951 }else{
953 if ((ptr = (SFColorRGBANative *)JS_GetPrivateFw(cx, obj)) == NULL) {
954 printf( "JS_GetPrivate failed in SFColorRGBAGetProperty.\n");
955 return JS_FALSE;
956 }
957 cc = ptr->v.c;
958 }
959 if (JSVAL_IS_INT(id)) {
960 int kk = 1;
961 switch (JSVAL_TO_INT(id)) {
962 case 0:
963 d = cc[0];
964 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
965 printf(
966 "JS_NewDouble failed for %f in SFColorRGBAGetProperty.\n",
967 d);
968 return JS_FALSE;
969 }
970 break;
971 case 1:
972 d = cc[1];
973 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
974 printf(
975 "JS_NewDouble failed for %f in SFColorRGBAGetProperty.\n",
976 d);
977 return JS_FALSE;
978 }
979 break;
980 case 2:
981 d = cc[2];
982 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
983 printf(
984 "JS_NewDouble failed for %f in SFColorRGBAGetProperty.\n",
985 d);
986 return JS_FALSE;
987 }
988 break;
989 case 3:
990 d = cc[3];
991 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
992 printf(
993 "JS_NewDouble failed for %f in SFColorRGBAGetProperty.\n",
994 d);
995 return JS_FALSE;
996 }
997 break;
998 }
999 }
1000 return JS_TRUE;
1001}
1002
1003JSBool
1004#if JS_VERSION < 185
1005SFColorRGBASetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
1006#elif JS_VERSION == 185
1007SFColorRGBASetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp){
1008#else
1009SFColorRGBASetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
1010 JSObject *obj = *hobj.address();
1011 jsid iid = *hiid.address();
1012 jsval *vp = hvp.address();
1013#endif
1014
1015 jsval _val;
1016 float *cc;
1017
1018#if JS_VERSION >= 185
1019 jsval id;
1020 if (!JS_IdToValue(cx,iid,&id)) {
1021 printf("JS_IdToValue failed in SFColorRGBASetProperty.\n");
1022 return JS_FALSE;
1023 }
1024#endif
1025 if(SM_method() == 2){
1026 AnyNative *any;
1027 if ((any = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1028 printf( "JS_GetPrivate failed in SFColorRGBASetProperty.\n");
1029 return JS_FALSE;
1030 }
1031 if(any->valueChanged)
1032 (*any->valueChanged)++;
1033 cc = any->v->sfcolorrgba.c;
1034 }else{
1035 SFColorRGBANative *ptr;
1036
1037 if ((ptr = (SFColorRGBANative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1038 printf( "JS_GetPrivate failed in SFColorRGBASetProperty.\n");
1039 return JS_FALSE;
1040 }
1041 ptr->valueChanged++;
1042 #ifdef JSVRMLCLASSESVERBOSE
1043 printf("SFColorRGBASetProperty: obj = %p, id = %d, valueChanged = %d\n",
1044 obj, JSVAL_TO_INT(id), ptr->valueChanged);
1045 #endif
1046 cc = ptr->v.c;
1047 }
1048 if (!JS_ConvertValue(cx, *vp, JSTYPE_NUMBER, &_val)) {
1049 printf( "JS_ConvertValue failed in SFColorRGBASetProperty.\n");
1050 return JS_FALSE;
1051 }
1052
1053 if (JSVAL_IS_INT(id)) {
1054 switch (JSVAL_TO_INT(id)) {
1055 case 0:
1056#if JS_VERSION < 185
1057 cc[0] = (float) *JSVAL_TO_DOUBLE(_val);
1058#else
1059 cc[0] = (float) JSVAL_TO_DOUBLE(_val);
1060#endif
1061 break;
1062 case 1:
1063#if JS_VERSION < 185
1064 cc[1] = (float) *JSVAL_TO_DOUBLE(_val);
1065#else
1066 cc[1] = (float) JSVAL_TO_DOUBLE(_val);
1067#endif
1068 break;
1069 case 2:
1070#if JS_VERSION < 185
1071 cc[2] = (float) *JSVAL_TO_DOUBLE(_val);
1072#else
1073 cc[2] = (float) JSVAL_TO_DOUBLE(_val);
1074#endif
1075 break;
1076 case 3:
1077#if JS_VERSION < 185
1078 cc[3] = (float) *JSVAL_TO_DOUBLE(_val);
1079#else
1080 cc[3] = (float) JSVAL_TO_DOUBLE(_val);
1081#endif
1082 break;
1083
1084 }
1085 }
1086 return JS_TRUE;
1087}
1088
1089JSBool
1090#if JS_VERSION < 185
1091SFImageToString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1092#else
1093SFImageToString(JSContext *cx, uintN argc, jsval *vp) {
1094 JSObject *obj = JS_THIS_OBJECT(cx,vp);
1095 jsval *argv = JS_ARGV(cx,vp);
1096 jsval rval;
1097 JSBool retval;
1098#endif
1099
1100 #ifdef JSVRMLCLASSESVERBOSE
1101 printf("SFImageToString: obj = %p, %u args\n", obj, argc);
1102 #endif
1103
1104 UNUSED(argc);
1105 UNUSED(argv);
1106
1107#if JS_VERSION < 185
1108 return doMFToString(cx, obj, "SFImage", rval);
1109#else
1110 retval = doMFToString(cx, obj, "SFImage", &rval);
1111 JS_SET_RVAL(cx,vp,rval);
1112 return retval;
1113#endif
1114}
1115
1116JSBool
1117#if JS_VERSION < 185
1118SFImageAssign(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1119#else
1120SFImageAssign(JSContext *cx, uintN argc, jsval *vp) {
1121 JSObject *obj = JS_THIS_OBJECT(cx,vp);
1122 jsval *argv = JS_ARGV(cx,vp);
1123 jsval rval;
1124 JSBool retval;
1125#endif
1126
1127 #ifdef JSVRMLCLASSESVERBOSE
1128 printf("SFImageAssign: obj = %p, %u args\n", obj, argc);
1129 #endif
1130
1131#if JS_VERSION < 185
1132 return _standardMFAssign (cx, obj, argc, argv, rval, &SFImageClass,FIELDTYPE_SFImage);
1133#else
1134 retval = _standardMFAssign (cx, obj, argc, argv, &rval, &SFImageClass,FIELDTYPE_SFImage);
1135 JS_SET_RVAL(cx,vp,rval);
1136 return retval;
1137#endif
1138}
1139
1140JSBool
1141#if JS_VERSION < 185
1142SFImageConstr(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1143#else
1144SFImageConstr(JSContext *cx, uintN argc, jsval *vp) {
1145 JSObject *obj = JS_NewObject(cx,&SFImageClass,NULL,NULL);
1146 jsval *argv = JS_ARGV(cx,vp);
1147#endif
1148 unsigned int i;
1149 jsval mv;
1150 int param[3];
1151 int expectedSize;
1152
1153
1154
1155 ADD_ROOT(cx,obj)
1156
1157 #ifdef JSVRMLCLASSESVERBOSE
1158 printf("SFImageConstr: obj = %p, %u args\n", obj, argc);
1159 #endif
1160
1161 /* SFImage really only has the valueChanged flag. */
1162 if(SM_method()==2){
1163 AnyNative *ptr;
1164 if ((ptr = (AnyNative *) AnyNativeNew(FIELDTYPE_SFImage,NULL,NULL)) == NULL) {
1165 printf( "SFImageNativeNew failed in SFImageConstr.\n");
1166 return JS_FALSE;
1167 }
1168
1169 if (!JS_SetPrivateFw(cx, obj, ptr)) {
1170 printf( "JS_SetPrivate failed in SFImageConstr.\n");
1171 return JS_FALSE;
1172 }
1173 //if(ptr->valueChanged)
1174 // (*ptr->valueChanged) = 1;
1175
1176 }else{
1177 SFImageNative *ptr;
1178 if ((ptr = (SFImageNative *) SFImageNativeNew()) == NULL) {
1179 printf( "SFImageNativeNew failed in SFImageConstr.\n");
1180 return JS_FALSE;
1181 }
1182
1183 if (!JS_SetPrivateFw(cx, obj, ptr)) {
1184 printf( "JS_SetPrivate failed in SFImageConstr.\n");
1185 return JS_FALSE;
1186 }
1187
1188 ptr->valueChanged = 1;
1189 }
1190 /* make this so that one can get the ".x", ".y", ".comp" and ".array" */
1191 //if (!JS_DefineProperties(cx, obj, SFImageProperties)) {
1192 // printf( "JS_DefineProperties failed in SFImageConstr.\n");
1193 // return JS_FALSE;
1194 //}
1195
1196 /* null image. Make this [0, 0, 0] NOTE - there are only 3 elements now! */
1197 if (!argc) {
1198 /* expect arguments to be number, number, number, mfint32 */
1199 mv = INT_TO_JSVAL(0);
1200 for (i=0; i<4; i++) {
1201 if (i==3) {
1202#if JS_VERSION < 185
1203 MFInt32Constr(cx, obj, 0, NULL, &mv);
1204#else
1205 /* note - old default constructor call allocates a new obj and assigns to mv,
1206 * but calling fn directly may not actually do that. It seems illegal to call
1207 * the constructor of another object directly on this object, and then feed it
1208 * as a proprety of itself, but since that's what the old code did (and it seems
1209 * to work), am keeping it as-is.
1210 *
1211 * I would -expect- that 'JS_NewObject(cx,&MFInt32Class,NULL,NULL)' should be
1212 * used instead of 'obj' below.... */
1213 MFInt32ConstrInternals(cx, obj, 0, NULL, &mv);
1214#endif
1215 }
1216 if (!JS_DefineElement(cx, obj, (jsuint) i, mv, JS_GET_PROPERTY_STUB, JS_SET_PROPERTY_STUB6, JSPROP_ENUMERATE)) {
1217 printf( "JS_DefineElement failed for arg %d in SFImageConstr.\n", i);
1218 return JS_FALSE;
1219 }
1220 }
1221 DEFINE_LENGTH(cx,obj,4)
1222#if JS_VERSION >= 185
1223 /* returning with success here, so must set rval to return the object we just finished creating */
1224 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
1225#endif
1226 return JS_TRUE;
1227 }
1228
1229 /* ok, ok. There are some parameters here. There had better be 4, or else... */
1230 if ((argc != 4) && (argc != 3)) {
1231 printf ("SFImageConstr, expect 4 parameters, got %d\n",argc);
1232 return JS_FALSE;
1233 }
1234
1235
1236 DEFINE_LENGTH(cx,obj,argc)
1237
1238 /* expect arguments to be number, number, number, mfint32 */
1239 for (i=0; i<3; i++) {
1240 /* printf ("looking at parameter %d\n",i); */
1241 if (JSVAL_IS_INT(argv[i])) {
1242 /* printf ("parameter is a number\n"); */
1243 param[i] = JSVAL_TO_INT(argv[i]);
1244 /* printf ("param is %d\n",param[i]); */
1245 } else {
1246 printf ("SFImageConstr: parameter %d is not a number\n",i);
1247 return JS_FALSE;
1248 }
1249 }
1250 /* now look at the MFInt32 array, and tack it on here */
1251 expectedSize = param[0] * param[1];
1252
1253
1254 /* the third number should be in the range of 0-4 inclusive (number of components in image) */
1255 if ((param[2]<0) || (param[2]>4)) {
1256
1257 printf ("SFImageConstr: with size > 0, comp must be between 1 and 4 inclusive, got %d\n",param[2]);
1258 return JS_FALSE;
1259 }
1260
1261 /* case 1 of null initializer */
1262 if ((expectedSize == 0) && (param[2] != 0)) {
1263 printf ("SFImageConstr: with x and y equal to zero, comp must be zero\n");
1264 return JS_FALSE;
1265 }
1266 /* case 2 of null initializer */
1267 if ((expectedSize != 0) && (param[2] == 0)) {
1268 printf ("SFImageConstr: with x and y not zero, comp must be non-zero\n");
1269 return JS_FALSE;
1270 }
1271
1272 /* worry about the MFInt32 array. Note that we copy the object pointer here. Should
1273 we copy ALL of the elements, or just the object itself?? */
1274
1275 if (argc == 4) {
1276 #ifdef JSVRMLCLASSESVERBOSE
1277 printJSNodeType(cx,JSVAL_TO_OBJECT(argv[3]));
1278 #endif
1279
1280 CHECK_CLASS(cx,JSVAL_TO_OBJECT(argv[3]),NULL,__FUNCTION__,MFInt32Class)
1281 if (!JS_GetProperty(cx, JSVAL_TO_OBJECT(argv[3]), MF_LENGTH_FIELD, &mv)) {
1282 printf( "JS_GetProperty failed for MFInt32 length in SFImageConstr\n");
1283 return JS_FALSE;
1284 }
1285 if (expectedSize != JSVAL_TO_INT(mv)) {
1286 printf ("SFImageConstr: expected %d elements in image data, got %d\n",expectedSize, JSVAL_TO_INT(mv));
1287 return JS_FALSE;
1288 }
1289 }
1290
1291 /* parameters are ok - just save them now in the new object. */
1292 for (i=0; i<argc; i++) {
1293 if (!JS_DefineElement(cx, obj, (jsint) i, argv[i], JS_GET_PROPERTY_STUB, JS_SET_PROPERTY_STUB6, JSPROP_ENUMERATE)) {
1294 printf( "JS_DefineElement failed for arg %d in SFImageConstr.\n", i);
1295 return JS_FALSE;
1296 }
1297 }
1298
1299 /* if we are here, we must have had some success... */
1300#if JS_VERSION < 185
1301 *rval = OBJECT_TO_JSVAL(obj);
1302#else
1303 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
1304#endif
1305 return JS_TRUE;
1306}
1307
1308JSBool
1309SFImageAddProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp) {
1310 return doMFAddProperty(cx, obj, id, vp, "SFImage"); //FIXME: is this ok ??? "SFImageAddProperty");
1311}
1312
1313#if JS_VERSION < 186
1314JSBool SFImageGetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp) {
1315#else
1316JSBool SFImageGetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JS::MutableHandle<JS::Value> hvp){
1317 JSObject *obj = *hobj.address();
1318 jsid id = *hiid.address();
1319 jsval *vp = hvp.address();
1320#endif
1321 return _standardMFGetProperty(cx, obj, id, vp, "_FreeWRL_Internal = 0", FIELDTYPE_SFImage); //FIXME: is this ok ??? "SFImage");
1322}
1323
1324
1325JSBool
1326#if JS_VERSION < 185
1327SFImageSetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp) {
1328#elif JS_VERSION == 185
1329SFImageSetProperty(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp){
1330#else
1331SFImageSetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
1332 JSObject *obj = *hobj.address();
1333 jsid id = *hiid.address();
1334 jsval *vp = hvp.address();
1335#endif
1336 return doMFSetProperty(cx, obj, id, vp, FIELDTYPE_SFImage);
1337}
1338
1339/**********************************************************************************/
1340
1341
1342/* returns a string rep of the pointer to the node in memory */
1343JSBool
1344#if JS_VERSION < 185
1345SFNodeValueOf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1346#else
1347SFNodeValueOf(JSContext *cx, uintN argc, jsval *vp) {
1348 JSObject *obj = JS_THIS_OBJECT(cx,vp);
1349 jsval *argv = JS_ARGV(cx,vp);
1350 jsval rvalinst;
1351 jsval *rval = &rvalinst;
1352#endif
1353 SFNodeNative *ptr;
1354 void* handle;
1355
1356 UNUSED(argc);
1357 UNUSED(argv);
1358 if(SM_method() == 2){
1359 AnyNative *nany;
1360 if ((nany = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1361 printf( "JS_GetPrivate failed in SFNodeToString.\n");
1362 return JS_FALSE;
1363 }
1364 handle = nany->v->sfnode;
1365 }else{
1366 #ifdef JSVRMLCLASSESVERBOSE
1367 printf ("SFNODETOSTRING\n");
1368 #endif
1369 if ((ptr = (SFNodeNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1370 printf( "JS_GetPrivate failed in SFNodeToString.\n");
1371 return JS_FALSE;
1372 }
1373 handle = ptr->handle;
1374 }
1375 /* get the string from creation, and return it. */
1376
1377 /* used to do:
1378 *rval = INT_TO_JSVAL(ptr->handle);
1379
1380 but we have 64 bit pointers in OSX now, and ints are 32 bits. so...
1381 we convert to a double, and hope that it is still correct (seems to be ok
1382 32 and 64 bits - tests/46.wrl will use this path, btw */
1383
1384 {
1385 jsdouble nv;
1386 char tmpline[100];
1387 sprintf (tmpline,"%p",handle);
1388 /* sprintf (tmpline,"%ld",ptr->handle); */
1389
1390 /* printf ("pointer to long int :%s:\n",tmpline); */
1391
1392 nv = strtod(tmpline,NULL);
1393 /* printf ("double is %lf\n",nv); */
1394
1395 /* printf ("nv %lf, handle %lu and %p\n",nv,ptr->handle,ptr->handle); */
1396 if (!JS_NewNumberValue(cx, nv, rval)) {
1397 ConsoleMessage ("Conversion issue in SFNodeToString");
1398 }
1399 }
1400
1401
1402 #ifdef JSVRMLCLASSESVERBOSE
1403 printf ("SFNodeToString, handle %p ",ptr->handle);
1404 printf ("SFNodeToString, handle as unsignned %u ",(unsigned int)ptr->handle);
1405 if (ptr->handle != NULL) {
1406 printf (" (%s) ", stringNodeType (((struct X3D_Node *)ptr->handle)->_nodeType));
1407 }
1408 printf ("string \"%s\"\n",ptr->X3DString);
1409 #endif
1410
1411#if JS_VERSION >= 185
1412 JS_SET_RVAL(cx,vp,*rval);
1413#endif
1414 return JS_TRUE;
1415}
1416
1417JSBool
1418#if JS_VERSION < 185
1419SFNodeToString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1420#else
1421SFNodeToString(JSContext *cx, uintN argc, jsval *vp) {
1422 JSObject *obj = JS_THIS_OBJECT(cx,vp);
1423 jsval *argv = JS_ARGV(cx,vp);
1424 jsval rvalinst;
1425 jsval *rval = &rvalinst;
1426#endif
1427 JSString *_str;
1428 SFNodeNative *ptr;
1429 void *handle;
1430
1431 UNUSED(argc);
1432 UNUSED(argv);
1433 #ifdef JSVRMLCLASSESVERBOSE
1434 printf ("SFNODETOSTRING\n");
1435 #endif
1436 if(SM_method() == 2){
1437 AnyNative *nany;
1438 if ((nany = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1439 printf( "JS_GetPrivate failed in SFNodeToString.\n");
1440 return JS_FALSE;
1441 }
1442 handle = nany->v->sfnode;
1443 }else{
1444
1445 if ((ptr = (SFNodeNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1446 printf( "JS_GetPrivate failed in SFNodeToString.\n");
1447 return JS_FALSE;
1448 }
1449 handle = ptr->handle;
1450 }
1451
1452 /* get the string from creation, and return it. */
1453
1454 /* used to do:
1455 *rval = INT_TO_JSVAL(ptr->handle);
1456
1457 but we have 64 bit pointers in OSX now, and ints are 32 bits. so...
1458 we convert to a double, and hope that it is still correct (seems to be ok
1459 32 and 64 bits - tests/46.wrl will use this path, btw */
1460
1461 {
1462 jsdouble nv;
1463 char buff[STRING];
1464 memset(buff, 0, STRING);
1465 sprintf (buff,"_%p_",handle);
1466 /* sprintf (tmpline,"%ld",ptr->handle); */
1467
1468 /* printf ("pointer to long int :%s:\n",tmpline); */
1469 ADD_ROOT(cx,_str)
1470 _str = JS_NewStringCopyZ(cx, buff);
1471
1472#if JS_VERSION < 185
1473 *rval = STRING_TO_JSVAL(_str);
1474#else
1475 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
1476#endif
1477
1478 REMOVE_ROOT (cx,_str)
1479
1480 }
1481
1482 return JS_TRUE;
1483}
1484
1485
1486
1487JSBool
1488#if JS_VERSION < 185
1489SFNodeAssign(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1490#else
1491SFNodeAssign(JSContext *cx, uintN argc, jsval *vp) {
1492 JSObject *obj = JS_THIS_OBJECT(cx,vp);
1493 jsval *argv = JS_ARGV(cx,vp);
1494 JSString *_id_jsstr;
1495#endif
1496
1497 JSObject *_from_obj;
1498 SFNodeNative *fptr, *ptr;
1499 char *_id_str;
1500
1501 UNUSED(_id_str); // compiler warning mitigation
1502 if(SM_method() == 2){
1503 AnyNative *lhs, *rhs;
1504 //if (JSVAL_IS_OBJECT(*vp)) {
1505 if ((*vp).isObject()){
1506 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*vp))) != NULL) {
1507 if ((lhs = (AnyNative *)JS_GetPrivateFw(cx, obj)) != NULL) {
1508 AnyNativeAssign(lhs,rhs);
1509 }
1510 }
1511 }
1512 }else{
1513
1514
1515 /* unsigned int toptr; */
1516 #ifdef JSVRMLCLASSESVERBOSE
1517 printf ("start of SFNodeAssign argc %d\n",argc);
1518 #endif
1519
1520 /* are we saving to a SFNode? */
1521 CHECK_CLASS(cx,obj,argv,__FUNCTION__,SFNodeClass)
1522
1523 /* get the pointer to the internal stuff */
1524 if ((ptr = (SFNodeNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1525 printf( "JS_GetPrivate failed for obj in SFNodeAssign.\n");
1526 return JS_FALSE;
1527 }
1528
1529 /* get the from, and the string */
1530 #ifdef JSVRMLCLASSESVERBOSE
1531 printf ("SFNodeAssign, we have %d and %d\n",(int)argv[0], (int)argv[1]);
1532 #endif
1533
1534#if JS_VERSION < 185
1535 if (!JS_ConvertArguments(cx, argc, argv, "o s", &_from_obj, &_id_str)) {
1536#else
1537 if (JS_ConvertArguments(cx, argc, argv, "oS", &_from_obj, &_id_jsstr) == JS_TRUE) {
1538 _id_str = JS_EncodeString(cx,_id_jsstr);
1539 } else {
1540#endif
1541 printf( "JS_ConvertArguments failed in SFNodeAssign.\n");
1542 return JS_FALSE;
1543 }
1544 if (_from_obj != NULL) {
1545 CHECK_CLASS(cx,_from_obj,argv,__FUNCTION__,SFNodeClass)
1546
1547 if ((fptr = (SFNodeNative *)JS_GetPrivateFw(cx, _from_obj)) == NULL) {
1548 printf( "JS_GetPrivate failed for _from_obj in SFNodeAssign.\n");
1549 return JS_FALSE;
1550 }
1551 #ifdef JSVRMLCLASSESVERBOSE
1552 printf("SFNodeAssign: obj = %p, id = \"%s\", from = %p\n",
1553 obj, _id_str, _from_obj);
1554 #endif
1555 } else { fptr = NULL; }
1556
1557 /* assign this internally */
1558 if (!SFNodeNativeAssign(ptr, fptr)) {
1559 printf( "SFNodeNativeAssign failed in SFNodeAssign.\n");
1560 return JS_FALSE;
1561 }
1562 } //SMmethod == 2
1563#if JS_VERSION < 185
1564 *rval = OBJECT_TO_JSVAL(obj);
1565#else
1566 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
1567#endif
1568
1569 #ifdef JSVRMLCLASSESVERBOSE
1570 printf ("end of SFNodeAssign\n");
1571 #endif
1572 return JS_TRUE;
1573}
1574
1575
1576// https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Releases/1.8.5
1577// when adding a node.function() don't forget to add a check in SFNodeGetProperty for "function"
1578JSBool
1579#if JS_VERSION < 185
1580SFNodeEquals(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1581 jsval rvalinst;
1582 jsval *rval = &rvalinst;
1583#else
1584SFNodeEquals(JSContext *cx, uintN argc, jsval *vp) {
1585 JSObject *obj = JS_THIS_OBJECT(cx,vp);
1586 jsval *argv = JS_ARGV(cx,vp);
1587#endif
1588 int iret;
1589 JSObject *_from_obj;
1590 SFNodeNative *ptr, *fptr;
1591
1592 if(SM_method() == 2){
1593 AnyNative *lhs, *rhs;
1594 iret = 0;
1595 //if (JSVAL_IS_OBJECT(*vp)) {
1596 if ((*vp).isObject()) {
1597 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*vp))) != NULL) {
1598 if ((lhs = (AnyNative *)JS_GetPrivateFw(cx, obj)) != NULL) {
1599 iret = lhs->v->sfnode == rhs->v->sfnode ? 1 : 0;
1600 }
1601 }
1602 }
1603 }else{
1604
1605 //JS_SET_RVAL(cx,vp,BOOLEAN_TO_JSVAL(1)); //JS_TRUE);
1606 //return JS_TRUE;
1607 if ((ptr = (SFNodeNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1608 printf( "JS_GetPrivate failed in SFNodeNative.\n");
1609 return JS_FALSE;
1610 }
1611 if (!JS_ConvertArguments(cx, argc, argv, "o",
1612 &_from_obj)) {
1613 printf( "JS_ConvertArguments failed in SFNodeNative.\n");
1614 return JS_FALSE;
1615 }
1616
1617
1618 if (_from_obj != NULL) {
1619 CHECK_CLASS(cx,_from_obj,argv,__FUNCTION__,SFNodeClass)
1620
1621 if ((fptr = (SFNodeNative *)JS_GetPrivateFw(cx, _from_obj)) == NULL) {
1622 printf( "JS_GetPrivate failed for _from_obj in SFNodeAssign.\n");
1623 return JS_FALSE;
1624 }
1625 #ifdef JSVRMLCLASSESVERBOSE
1626 printf("SFNodeAssign: obj = %p, id = \"%s\", from = %p\n",
1627 obj, _id_str, _from_obj);
1628 #endif
1629 } else { fptr = NULL; }
1630
1631
1632 /* assign this internally */
1633 iret = SFNodeNativeEquals(ptr, fptr);
1634 } //SM_method == 2
1635#if JS_VERSION < 185
1636 *rval = BOOLEAN_TO_JSVAL(iret);
1637#else
1638 JS_SET_RVAL(cx,vp,BOOLEAN_TO_JSVAL(iret));
1639#endif
1640
1641 #ifdef JSVRMLCLASSESVERBOSE
1642 printf ("end of SFNodeEqual\n");
1643 #endif
1644
1645 return JS_TRUE;
1646}
1647
1648
1649
1650/* define JSVRMLCLASSESVERBOSE */
1651
1652JSBool
1653#if JS_VERSION < 185
1654SFNodeConstr(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
1655#else
1656SFNodeConstr(JSContext *cx, uintN argc, jsval *vp) {
1657 JSObject *obj = JS_NewObject(cx,&SFNodeClass,NULL,NULL);
1658 jsval *argv = JS_ARGV(cx,vp);
1659#endif
1660 /*
1661 X3D ecmascript specs:
1662 http://www.web3d.org/files/specifications/19777-1/V3.0/Part1/functions.html#SFNodeInstanceCreationFunction
1663 "7.6.5.2 Instance creation function
1664 This object cannot be directly instantiated"
1665 That means there should never be myNode = new SFNode(); or SFNode('string') or SFNode('0x12345');
1666 X3DScene Browser.createX3DFromString(String x3dSyntax)
1667
1668 VRML vrmlscript specs:
1669 http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/javascript.html#SFNode
1670 sfNodeObjectName = new SFNode(String vrmlstring);
1671 MFNode Browser.createVrmlFromString( String vrmlSyntax )
1672
1673 The support below is for VRML specs SFNode(vrmlstring),
1674 plus 2 freewrl-specific techniques:
1675 sfn = new SFNode(oldSFNode)
1676 sfn = new SFNode('0x12345'); //ptr to another node
1677
1678 - a char* ptr->X3Dstring holding vrmlstring from a previous new SFNode(string)
1679 is dragged around with an SFNode, and when new SFNode(old) it resends the
1680 string to the parser to create the new node.
1681 - this should fail when oldnode wasn't created with new SFNode(vrmlstring).
1682 (a future broto version would/could possibly deep copy the binary oldnode,
1683 elliminating the need for saving/holding the X3DString, and allowing it to
1684 work even when the oldNode wasn't created with a new SFNode(vrmlstring) constructor call)
1685
1686 */
1687 SFNodeNative *newPtr;
1688 SFNodeNative *oldPtr;
1689
1690 struct X3D_Node *newHandle;
1691 JSString *myStr;
1692 char *cString;
1693
1694 /* unused struct X3D_Group *myGroup; */
1695
1696 ADD_ROOT(cx,obj)
1697 newHandle = NULL;
1698 cString = NULL;
1699
1700 #ifdef JSVRMLCLASSESVERBOSE
1701 printf ("Start of SFNodeConstr argc %d object %p\n",argc,obj);
1702 #endif
1703
1704 /* verify the argc */
1705 if (argc == 0) {
1706 newHandle = NULL;
1707 cString = STRDUP("SFNodeConstr from argc eq 0");
1708 } else if (argc == 1) {
1709 /* is this a string, or a number indicating a node? */
1710 myStr = JS_ValueToString(cx, argv[0]);
1711#if JS_VERSION < 185
1712 cString = JS_GetStringBytes(myStr);
1713#else
1714 cString = JS_EncodeString(cx,myStr);
1715#endif
1716 #ifdef JSVRMLCLASSESVERBOSE
1717 printf ("SFNodeConstr, argc =1l string %s\n",cString);
1718 #endif
1719
1720 /* this is either a memory pointer, or it is actual X3D text, or it is junk */
1721 //if (JSVAL_IS_OBJECT(argv[0])) {
1722 if (argv[0].isObject()) {
1723 /* myNode2 = new SFNode(myNode1); not in specs*/
1724 #ifdef JSVRMLCLASSESVERBOSE
1725 printf ("SFNodeConstr, cstring was an object\n");
1726 #endif
1727 if(SM_method() == 2){
1728 AnyNative *rhs;
1729 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(argv[0]))) == NULL) {
1730 #ifdef JSVRMLCLASSESVERBOSE
1731 printf( "JS_GetPrivate failed in SFNodeConstr.\n");
1732 #endif
1733 return JS_FALSE;
1734 }
1735
1736 newHandle = rhs->v->sfnode;
1737
1738 }else{
1739 if ((oldPtr = (SFNodeNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(argv[0]))) == NULL) {
1740 #ifdef JSVRMLCLASSESVERBOSE
1741 printf( "JS_GetPrivate failed in SFNodeConstr.\n");
1742 #endif
1743 return JS_FALSE;
1744 }
1745
1746 newHandle = oldPtr->handle;
1747 }
1748 if(SM_method() == 0)
1749 cString = STRDUP(oldPtr->X3DString);
1750
1751 } else {
1752 #ifdef JSVRMLCLASSESVERBOSE
1753 printf ("SFNodeConstr, cstring was NOT an object\n");
1754 #endif
1755
1756 /* check if the first character is numeric, if it is then assume a pointer */
1757 if (!((cString[0] >= 'A' && cString [0] <= 'Z')||(cString[0] >= 'a' && cString [0] <= 'z'))) {
1758 /* lets hope this is an initializer, like "new SFNode("0x100675790") */
1759 if (sscanf (cString,"%p",&newHandle) != 1) {
1760 ConsoleMessage ("expected pointer for Javascript SFNode constr, got :%s:",cString);
1761 newHandle = NULL;
1762 #ifdef JSVRMLCLASSESVERBOSE
1763 printf ("SFNodeConstr, expected pointer for Javascript SFNode constr, got :%s:\n",cString);
1764 } else {
1765 printf ("SFNodeConstr, got pointer for Javascript SFNode constr, :%p:\n",newHandle);
1766 #endif
1767 }
1768 } else {
1769 /* cannot be an initializer, must parse the string */
1770 /* myNode = new SFNode('Group{...}'); */
1771 /* try compiling this X3D code... */
1772 struct X3D_Group *myGroup = (struct X3D_Group *) createNewX3DNode(NODE_Group);
1773 resource_item_t *res = resource_create_from_string(cString);
1774 res->whereToPlaceData = myGroup;
1775 res->ectx = myGroup;
1776 res->media_type = resm_vrml;
1777 res->parsed_request = strdup("From the EAI bootcamp of life ");
1778 res->offsetFromWhereToPlaceData = (int) offsetof (struct X3D_Group, children);
1779 #ifdef JSVRMLCLASSESVERBOSE
1780 printf ("SFNodeConstr, sending resource to parser\n");
1781 #endif
1782 //send_resource_to_parser(res);
1783 #ifdef JSVRMLCLASSESVERBOSE
1784 printf ("SFNodeConstr, waiting on resource\n");
1785 #endif
1786 //resource_wait(res);
1787
1788 #ifdef JSVRMLCLASSESVERBOSE
1789 printf ("SFNodeConstr we have created %d nodes\n",myGroup->children.n);
1790 #endif
1791
1792 /* we MUST create 1 node here; if not, there is an error */
1793 //if ((myGroup->children.n) != 1) {
1794 // ConsoleMessage ("SFNativeNew - created %d nodes, expected 1 only\n",myGroup->children.n);
1795 // return JS_FALSE;
1796 //}
1797 parser_process_res_VRML_X3D(res);
1798 newHandle = X3D_NODE(myGroup->children.p[0]);
1799 }
1800
1801 cString = STRDUP("node created in SFNodeConstr");
1802 }
1803
1804 } else if (argc == 2) {
1805 /* eg, createVrmlFromString will send a bunch of SFNodes to a MFNode with text */
1806
1807 #ifdef JSVRMLCLASSESVERBOSE
1808 printf ("SFNodeConstr - have 2 arguments\n");
1809 #endif
1810
1811 if ((JSVAL_IS_STRING(argv[0])) && (JSVAL_IS_STRING(argv[1]))) {
1812 JSString *_idStr;
1813 char *_id_c;
1814
1815 _idStr = JS_ValueToString(cx, argv[0]);
1816#if JS_VERSION < 185
1817 _id_c = JS_GetStringBytes(_idStr);
1818#else
1819 _id_c = JS_EncodeString(cx,_idStr);
1820#endif
1821 /* printf ("first string :%s:\n",_id_c); */
1822
1823 cString = STRDUP(_id_c);
1824
1825 _idStr = JS_ValueToString(cx, argv[1]);
1826#if JS_VERSION < 185
1827 _id_c = JS_GetStringBytes(_idStr);
1828#else
1829 _id_c = JS_EncodeString(cx,_idStr);
1830#endif
1831 /* printf ("second string :%s:\n",_id_c); */
1832
1833 if (sscanf (_id_c,"%p",&newHandle) != 1) {
1834 printf ("SFNodeConstr - can not get handle from %s\n",_id_c);
1835 return JS_FALSE;
1836 }
1837/* nope, need to do this as a pointer string.. newHandle = (struct X3D_Node *) JSVAL_TO_GCTHING(argv[1]); */
1838
1839 #ifdef JSVRMLCLASSESVERBOSE
1840 printf ("string is :%s: new handle is %p\n",cString,newHandle);
1841 #endif
1842
1843 } else {
1844 printf ("SFNodeConstr - 2 args, expected 2 strings\n");
1845 return JS_FALSE;
1846 }
1847
1848
1849 } else {
1850 printf( "SFNodeConstr requires at least 1 string arg.\n");
1851 return JS_FALSE;
1852 }
1853
1854 if(SM_method() == 2){
1855 AnyNative *lhs;
1856 if ((lhs = (AnyNative *) AnyNativeNew(FIELDTYPE_SFNode,NULL,NULL)) == NULL) {
1857 printf( "AnyNativeNew failed in SFNodeConstr.\n");
1858 return JS_FALSE;
1859 }
1860 if (!JS_SetPrivateFw(cx, obj, lhs)) {
1861 printf( "JS_SetPrivate failed in SFNodeConstr.\n");
1862 return JS_FALSE;
1863 }
1864 //lhs->valueChanged = NULL;
1865 lhs->v->sfnode = newHandle;
1866 }else{
1867 /* ok, so far so good... */
1868 if ((newPtr = (SFNodeNative *) SFNodeNativeNew()) == NULL) {
1869 printf( "SFNodeNativeNew failed in SFNodeConstr.\n");
1870 return JS_FALSE;
1871 }
1872
1873 //if (!JS_DefineProperties(cx, obj, SFNodeProperties)) {
1874 // printf( "JS_DefineProperties failed in SFNodeConstr.\n");
1875 // return JS_FALSE;
1876 //}
1877
1878 if (!JS_SetPrivateFw(cx, obj, newPtr)) {
1879 printf( "JS_SetPrivate failed in SFNodeConstr.\n");
1880 return JS_FALSE;
1881 }
1882
1883 newPtr->handle = newHandle;
1884 if(SM_method() == 0){
1885 newPtr->X3DString = (char *)STRDUP(cString);
1886
1887 if (!JS_DefineSFNodeSpecificProperties (cx, obj, newHandle)) {
1888 printf( "JS_DefineSFNodeSpecificProperties failed in SFNodeConstr.\n");
1889 return JS_FALSE;
1890
1891 }
1892 }
1893
1894 newPtr->valueChanged = 1;
1895 } //SM_method == 2
1896
1897 #ifdef JSVRMLCLASSESVERBOSE
1898 {
1899 if (newHandle == NULL)
1900 printf("end of SFNodeConstr: created obj = %p, argc: %u mem ptr: %p (null pointer) text string: %s\n",
1901 obj, argc, newHandle, cString);
1902 else
1903 printf("end of SFNodeConstr: created obj = %p, argc: %u mem ptr: %p (%s) text string: %s\n",
1904 obj, argc, newHandle, stringNodeType(newHandle->_nodeType),cString);
1905 }
1906 #endif
1907
1908#if JS_VERSION < 185
1909 *rval = OBJECT_TO_JSVAL(obj);
1910#else
1911 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
1912#endif
1913
1914 return JS_TRUE;
1915}
1916
1917/* undef JSVRMLCLASSESVERBOSE */
1918
1919void
1920#if JS_VERSION < 186
1921SFNodeFinalize(JSContext *cx, JSObject *obj){
1922#else
1923SFNodeFinalize(JSFreeOp *fop, JSObject *obj){
1924JSContext *cx = NULL;
1925#endif
1926 SFNodeNative *ptr;
1927 AnyNative *any;
1928
1929 #ifdef JSVRMLCLASSESVERBOSE
1930 printf("SFNodeFinalize: obj = %p\n", obj);
1931 #endif
1932
1933 REMOVE_ROOT(cx,obj)
1934
1935 /*so, it appears that recent (2010) versions ofJavascript will give the following error when
1936 the interpreter is shutdown. It appears that sending in the url will cause a SFNode to
1937 be created, even though the normal constructor is not called, eg:
1938
1939 DEF t Script {
1940 url "vrmlscript:
1941 function eventsProcessed () {}
1942 "
1943 }
1944
1945 will cause the following JS_GetPrivate to fail. */
1946 if ((ptr = (SFNodeNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
1947 /* see above printf( "JS_GetPrivate failed in SFNodeFinalize.\n"); */
1948 return;
1949 } else {
1950 if(SM_method() == 0)
1951 FREE_IF_NZ (ptr->X3DString);
1952 if(SM_method() == 2){
1953 any = (AnyNative*)ptr;
1954 if(any->gc) FREE_IF_NZ(any->v);
1955 }
1956
1957 FREE_IF_NZ (ptr);
1958 JS_SetPrivateFw(cx,obj,NULL);
1959 }
1960}
1961
1962
1963void X3D_ECMA_TO_JS(JSContext *cx, void *Data, int datalen, int dataType, jsval *newval);
1964void X3D_MF_TO_JS(JSContext *cx, JSObject *obj, void *Data, int dataType, jsval *newval, char *fieldName);
1965void X3D_SF_TO_JS(JSContext *cx, JSObject *obj, void *Data, unsigned datalen, int dataType, jsval *newval);
1966void X3D_MF_TO_JS_B(JSContext *cx, void *Data, int dataType, int *valueChanged, jsval *newval);
1967void X3D_SF_TO_JS_B(JSContext *cx, void *Data, unsigned datalen, int dataType, int *valueChanged, jsval *newval);
1968int getFieldFromNodeAndName(struct X3D_Node* node,const char *fieldname, int *type, int *kind, int *iifield, union anyVrml **value);
1969JSBool
1970#if JS_VERSION < 185
1971SFNodeGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
1972#elif JS_VERSION == 185
1973SFNodeGetProperty(JSContext *cx, JSObject *obj, jsid iid, jsval *vp){
1974#else
1975SFNodeGetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JS::MutableHandle<JS::Value> hvp){
1976 JSObject *obj = *hobj.address();
1977 jsid iid = *hiid.address();
1978 jsval *vp = hvp.address();
1979#endif
1980
1981
1982 /* We can't really get a property of a SFNode. There are no sub-indexes, etc...
1983 so we don't do anything. Check out SFVec3fGetProperty to see how it handles
1984 properties, should we need to have properties in the future. */
1985
1986 SFNodeNative *ptr;
1987 JSString *_idStr;
1988 char *_id_c;
1989 jsval rval;
1990#if JS_VERSION >= 185
1991 jsval id;
1992 if (!JS_IdToValue(cx,iid,&id)) {
1993 printf("JS_IdToValue failed in SFNodeGetProperty.\n");
1994 return JS_FALSE;
1995 }
1996#endif
1997 if (JSVAL_IS_INT(id)) {
1998 printf("SFNode has no [index] property.\n");
1999 /* would be nice to say node type or node name */
2000 /* note the setter does seem to take [0 or 1] */
2001 return JS_FALSE;
2002 }
2003
2004 _idStr = JS_ValueToString(cx, id);
2005#if JS_VERSION < 185
2006 _id_c = JS_GetStringBytes(_idStr);
2007#else
2008 _id_c = JS_EncodeString(cx,_idStr);
2009#endif
2010 #ifdef JSVRMLCLASSESVERBOSE
2011 printf ("start of SFNodeGetProperty... id is %s\n",_id_c);
2012 #endif
2013
2014 /* is this the string "undefined" ? */
2015 if (strcmp ("undefined",_id_c) == 0) return JS_TRUE;
2016
2017 /* is this one of the SFNode standard functions? see JSFunctionSpec (SFNodeFunctions)[] */
2018 if (strcmp ("toString",_id_c) == 0) return JS_TRUE;
2019 if (strcmp ("valueOf",_id_c) == 0) return JS_TRUE;
2020 if (strcmp ("equals",_id_c) == 0) return JS_TRUE;
2021 if (strcmp ("assign",_id_c) == 0) return JS_TRUE;
2022
2023 /* get the private pointer for this node */
2024 if ((ptr = (SFNodeNative *)JS_GetPrivateFw(cx, obj)) != NULL) {
2025 int ifound, type, kind, iifield, *valueChanged, sfsize, sftype;
2026 union anyVrml *value;
2027 char *fieldname = _id_c;
2028 struct X3D_Node *node;
2029 if(SM_method() == 2){
2030 AnyNative *any = (AnyNative *)ptr;
2031 node = any->v->sfnode;
2032 }else{
2033 node = ptr->handle;
2034 }
2035 #ifdef JSVRMLCLASSESVERBOSE
2036 printf ("SFNodeGetProperty, working on node %p, field %s\n",ptr->handle,_id_c);
2037 #endif
2038 if(SM_method() == 2){
2039 ifound = getFieldFromNodeAndName(node,fieldname,&type,&kind,&iifield,&value);
2040 if(ifound){
2041 valueChanged = &node->_change; //if a regular node field changes, we (re-) compile_Node
2042 // ... (but have no way to detect which field, so routes can't be done later in freewrl system :{
2043 if(node->_nodeType == NODE_Script){
2044 //need one more thing - valueChanged
2045 struct X3D_Script *scriptnode = X3D_SCRIPT(node);
2046 getFieldFromScript((struct Shader_Script*)scriptnode->__scriptObj,fieldname,&type,&kind,&iifield,&value,&valueChanged);
2047 }
2048 sftype = type2SF(type);
2049 sfsize = sizeofSForMF(sftype);
2050
2051 //set up a return value
2052 switch (type) {
2053 case FIELDTYPE_SFBool:
2054 case FIELDTYPE_SFFloat:
2055 case FIELDTYPE_SFTime:
2056 case FIELDTYPE_SFDouble:
2057 case FIELDTYPE_SFInt32:
2058 case FIELDTYPE_SFString:
2059 X3D_ECMA_TO_JS(cx, value,sfsize,type,vp);
2060 break;
2061 case FIELDTYPE_SFColor:
2062 case FIELDTYPE_SFNode:
2063 case FIELDTYPE_SFVec2f:
2064 //case FIELDTYPE_SFVec2d:
2065 case FIELDTYPE_SFVec3f:
2066 case FIELDTYPE_SFVec3d:
2067 case FIELDTYPE_SFVec4f:
2068 case FIELDTYPE_SFVec4d:
2069 //case FIELDTYPE_SFColorRGBA:
2070 case FIELDTYPE_SFRotation:
2071 X3D_SF_TO_JS_B(cx, value,sfsize, type, valueChanged, vp);
2072 break;
2073 case FIELDTYPE_MFColor:
2074 case FIELDTYPE_MFVec3f:
2075 case FIELDTYPE_MFVec2f:
2076 case FIELDTYPE_MFFloat:
2077 case FIELDTYPE_MFTime:
2078 case FIELDTYPE_MFInt32:
2079 case FIELDTYPE_MFString:
2080 case FIELDTYPE_MFNode:
2081 case FIELDTYPE_MFRotation:
2082 case FIELDTYPE_SFImage:
2083 //static void X3D_MF_TO_JS(JSContext *cx, JSObject *obj, void *Data, int dataType, jsval *newval, char *fieldName) {
2084 X3D_MF_TO_JS_B(cx, value, type, valueChanged, vp);
2085 break;
2086 default: printf ("unhandled type FIELDTYPE_ %d in getSFNodeField\n", type) ;
2087 return JS_FALSE;
2088 }
2089
2090
2091 //#if JS_VERSION < 185
2092 // *rval = OBJECT_TO_JSVAL(obj);
2093 //#else
2094 // JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
2095 //#endif
2096
2097 }
2098 } //SM_method > 0
2099 if(SM_method() == 0){
2100 // dug9 attempt to find read the field of another script
2101 //if(!strcmp(stringNodeType(ptr->handle->_nodeType),"Script"))
2102 if( ptr->handle && ptr->handle->_nodeType== NODE_Script )
2103 {
2104 struct Shader_Script *myObj;
2105 JSContext *cx2;
2106 JSObject *obj2;
2107 struct CRscriptStruct *ScriptControl; // = getScriptControl();
2108 myObj = (struct Shader_Script*)X3D_SCRIPT(ptr->handle)->__scriptObj;
2109 // get context and global object for this script
2110 ScriptControl = getScriptControlIndex(myObj->num);
2111 cx2 = (JSContext*)ScriptControl->cx;
2112 obj2 = (JSObject*)ScriptControl->glob;
2113 if (JS_GetProperty (cx2, obj2, _id_c, &rval)) {
2114 if (JSVAL_IS_NULL(rval)) {
2115 ConsoleMessage ("Script - field :%s: does not exist",_id_c);
2116 return JS_FALSE;
2117 }else{
2118 *vp = rval;
2119 return JS_TRUE;
2120 }
2121 }
2122 }
2123
2124 JS_DefineSFNodeSpecificProperties (cx, obj, ptr->handle);
2125
2126 // does the property exist?
2127 if (JS_LookupProperty (cx, obj, _id_c, &rval)) {
2128 if (JSVAL_IS_NULL(rval)) {
2129 // if you mis-spell a builtin node field
2130 // like Cylinder.hight (sb height) you'll end up in here
2131 ConsoleMessage ("SFNode - field :%s: does not exist",_id_c);
2132 return JS_FALSE;
2133 }
2134 }
2135 // if your SFNode is type Script you'll end up here
2136 #ifdef JSVRMLCLASSESVERBOSE
2137 printf ("wondering about rval.. %d. it is a\n",(int)rval);
2138 if (JSVAL_IS_INT(rval)) printf ("IS AN INT\n");
2139 if (JSVAL_IS_OBJECT(rval)) printf ("IS AN OBJECT\n");
2140 if (JSVAL_IS_STRING(rval)) printf ("IS AN STRING\n");
2141 if (rval == JSVAL_FALSE) printf ("FALSE\n");
2142 if (rval == JSVAL_NULL) printf ("NULL\n");
2143 if (rval == JSVAL_ONE) printf ("ONE\n");
2144 if (rval == JSVAL_ZERO) printf ("ZERO\n");
2145 if (rval == JSVAL_VOID) printf ("VOID\n");
2146 if (rval == JSVAL_TRUE) printf ("TRUE\n");
2147 #endif
2148
2149
2150 //dug9 - I find the next line JS_GetProperty recursive
2151 //when the sfnode we're trying to read is a Script node
2152 //if (JS_GetProperty (cx, obj, _id_c, &rval)) {
2153 if(false){
2154 #ifdef JSVRMLCLASSESVERBOSE
2155 printf ("SFNodeGetProperty, found field \"%s\" in node, returning property\n",_id_c);
2156 #endif
2157
2158 *vp = rval;
2159 } else {
2160 #ifdef JSVRMLCLASSESVERBOSE
2161 printf ("SFNodeGetProperty, did not find field \"%s\" in node.\n",_id_c);
2162 #endif
2163 return JS_FALSE;
2164 }
2165 } //SM_method() == 0
2166 } else {
2167 printf ("could not get private for SFNodeGetProperty, field :%s:\n",_id_c);
2168 return JS_FALSE;
2169 }
2170
2171 return JS_TRUE;
2172}
2173
2174void Parser_scanStringValueToMem_B(union anyVrml* any, indexT ctype, const char *value, int isXML);
2175void JS_MF_TO_X3D(JSContext *cx, JSObject * obj, void *Data, int dataType, jsval *newval);
2176JSBool
2177#if JS_VERSION < 185
2178SFNodeSetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){
2179#elif JS_VERSION == 185
2180SFNodeSetProperty(JSContext *cx, JSObject *obj, jsid iid, JSBool strict, jsval *vp){
2181#else
2182SFNodeSetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
2183 JSObject *obj = *hobj.address();
2184 jsid iid = *hiid.address();
2185 jsval *vp = hvp.address();
2186#endif
2187 JSString *_idStr, *_valStr;
2188 char *_id_c, *_val_c;
2189 SFNodeNative *ptr;
2190 size_t val_len;
2191 size_t tmp;
2192#if JS_VERSION >= 185
2193 jsval id;
2194 if (!JS_IdToValue(cx,iid,&id)) {
2195 printf("JS_IdToValue failed in SFNodeSetProperty.\n");
2196 return JS_FALSE;
2197 }
2198#endif
2199 if(SM_method() == 2){
2200 AnyNative *lhs;
2201 int ifound, type, kind, iifield, *valueChanged, sfsize, sftype;
2202 union anyVrml *value;
2203 char *fieldname;
2204 struct X3D_Node *node;
2205
2206 if((lhs = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2207 return JS_FALSE;
2208 }
2209 _idStr = JS_ValueToString(cx, id);
2210#if JS_VERSION < 185
2211 _id_c = JS_GetStringBytes(_idStr);
2212#else
2213 _id_c = JS_EncodeString(cx,_idStr); /* _id_c field name as a string ie "currX" */
2214#endif
2215 fieldname = _id_c;
2216 node = lhs->v->sfnode;
2217 ifound = getFieldFromNodeAndName(node,fieldname,&type,&kind,&iifield,&value);
2218 if(ifound){
2219 valueChanged = &node->_change;
2220 if(node->_nodeType == NODE_Script){
2221 //need one more thing - valueChanged
2222 struct X3D_Script *scriptnode = X3D_SCRIPT(node);
2223 getFieldFromScript((struct Shader_Script*)scriptnode->__scriptObj,fieldname,&type,&kind,&iifield,&value,&valueChanged);
2224 }
2225 sftype = type2SF(type);
2226 sfsize = sizeofSForMF(sftype);
2227 //set up a return value
2228 //in js, null is an object
2229 if( JSVAL_IS_NULL(*vp)){
2230 if(type == FIELDTYPE_SFNode)
2231 value->sfnode = NULL;
2232 if(valueChanged)
2233 (*valueChanged) ++;
2234 //} else if (JSVAL_IS_OBJECT(*vp)) {
2235 } else if ((*vp).isObject()) {
2236 AnyNative *rhs;
2237 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*vp))) == NULL) {
2238 //printf("in setECMANative, RHS was NOT native type \n");
2239 }else{
2240 //printf("in setECMANative, RHS was native type \n");
2241 //can do an assign here
2242 if(type == rhs->type){
2243 if(valueChanged)
2244 (*valueChanged) ++;
2245 //shallow assumes the top has already been malloced (just base part of MF needed)
2246 //use this if you need to malloc anyvrml: int sizeofSForMF(int itype)
2247 shallow_copy_field(rhs->type,rhs->v,value);
2248 }
2249 }
2250
2251 } else {
2252
2253 switch (type) {
2254 case FIELDTYPE_SFBool:
2255 case FIELDTYPE_SFFloat:
2256 case FIELDTYPE_SFTime:
2257 case FIELDTYPE_SFDouble:
2258 case FIELDTYPE_SFInt32:
2259 case FIELDTYPE_SFString:
2260 JS_ECMA_TO_X3D(cx, value,sfsize,type,vp);
2261
2262 if(valueChanged)
2263 (*valueChanged)++;
2264
2265 break;
2266 //case FIELDTYPE_SFColor:
2267 //case FIELDTYPE_SFNode:
2268 //case FIELDTYPE_SFVec2f:
2269 //case FIELDTYPE_SFVec3f:
2270 //case FIELDTYPE_SFVec3d:
2271 //case FIELDTYPE_SFRotation:
2272 // JS_SF_TO_X3D(cx, obj, value,sfsize, type, vp);
2273 // break;
2274 //case FIELDTYPE_MFColor:
2275 //case FIELDTYPE_MFVec3f:
2276 //case FIELDTYPE_MFVec2f:
2277 //case FIELDTYPE_MFFloat:
2278 //case FIELDTYPE_MFTime:
2279 //case FIELDTYPE_MFInt32:
2280 //case FIELDTYPE_MFString:
2281 //case FIELDTYPE_MFNode:
2282 //case FIELDTYPE_MFRotation:
2283 //case FIELDTYPE_SFImage:
2284 // JS_MF_TO_X3D(cx, obj, value, type, vp);
2285 // break;
2286 default:
2287 printf ("unhandled type FIELDTYPE_ %d in setSFNodeField\n", type) ;
2288 return JS_FALSE;
2289 }
2290 }
2291
2292 //#if JS_VERSION < 185
2293 // *rval = OBJECT_TO_JSVAL(obj);
2294 //#else
2295 // JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
2296 //#endif
2297 return JS_TRUE;
2298
2299 }
2300 return JS_FALSE;
2301
2302 }else{ //SM_method == 2
2303 //if (JSVAL_IS_INT(id)) {
2304 // printf("SFNode has no [index] property.\n");
2305 // /* would be nice to say node type or node name */
2306 // return JS_FALSE;
2307 //}
2308
2309 _idStr = JS_ValueToString(cx, id);
2310 _valStr = JS_ValueToString(cx, *vp);
2311#if JS_VERSION < 185
2312 _id_c = JS_GetStringBytes(_idStr);
2313 _val_c = JS_GetStringBytes(_valStr);
2314#else
2315 _id_c = JS_EncodeString(cx,_idStr); /* _id_c field name as a string ie "currX" */
2316 _val_c = JS_EncodeString(cx,_valStr); /* _val_c field value as a string ie "33" */
2317#endif
2318
2319
2320 #ifdef JSVRMLCLASSESVERBOSE
2321 printf("SFNodeSetProperty: obj = %p, id = %s, vp = %s\n",
2322 obj, _id_c, _val_c);
2323 #endif
2324
2325
2326 if ((ptr = (SFNodeNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2327 printf( "JS_GetPrivate failed in SFNodeSetProperty.\n");
2328 return JS_FALSE;
2329 }
2330
2331 if (JSVAL_IS_INT(id)) {
2332 /* dug9 Aug 2013: no Properties are defined on SFNode, so there shouldn't be
2333 any public SFNode[id] = ?. What are we doing here?
2334 Was it an experiment or test method? Or does some other internal
2335 function call this setter[id]? May we drop support for it?*/
2336 if(SM_method() == 0){
2337 ptr->valueChanged++;
2338 val_len = (int) strlen(_val_c) + 1;
2339
2340 #ifdef JSVRMLCLASSESVERBOSE
2341 printf ("switching on %d\n",JSVAL_TO_INT(id));
2342 #endif
2343
2344 switch (JSVAL_TO_INT(id)) {
2345 case 0:
2346 if ((strlen(ptr->X3DString) + 1) > val_len) {
2347 ptr->X3DString =
2348 (char *) REALLOC (ptr->X3DString, val_len * sizeof(char));
2349 }
2350 memset(ptr->X3DString, 0, val_len);
2351 memmove(ptr->X3DString, _val_c, val_len);
2352 break;
2353 case 1:
2354 scanUnsignedIntoValue(_val_c,&tmp);
2355 ptr->handle = X3D_NODE(tmp);
2356 break;
2357 }
2358 } //SM_method == 0
2359 } else {
2360 #ifdef JSVRMLCLASSESVERBOSE
2361 printf ("JS_IS_INT false\n");
2362
2363 printf ("SFNodeSetProperty, setting node %p field %s to value %s\n", ptr->handle,_id_c,_val_c);
2364
2365 {
2366 struct X3D_Node* ptx;
2367 ptx = X3D_NODE(ptr->handle);
2368 printf ("node is of type %s\n",stringNodeType(ptx->_nodeType));
2369 }
2370 #endif
2371
2372 /* dug9 2012 attempt to find and write the field of another script */
2373 if( ptr->handle->_nodeType== NODE_Script )
2374 {
2375 /* code borrowed from fieldGet.c L.138 in set_one_ECMAtype() and reworked
2376 for cx2, obj2 - writes to a script eventIn with timestamp,
2377 and runs the script function, completing the event
2378 cascade (I think)
2379 */
2380 char scriptline[100];
2381 JSScript *eventInFunction;
2382 struct ScriptFieldDecl* myfield;
2383 struct CRjsnameStruct *JSparamnames; // = getJSparamnames();
2384 struct Shader_Script *myObj;
2385 JSContext *cx2;
2386 JSObject *obj2;
2387 jsval newval;
2388 //indexT myfieldType;
2389 //union anyVrml vrmlField;
2390 //bool deepcopy;
2391 struct CRscriptStruct *ScriptControl; // = getScriptControl();
2392 myObj = (struct Shader_Script*)X3D_SCRIPT(ptr->handle)->__scriptObj;
2393 /* is the script ok and initialized? */
2394 ScriptControl = getScriptControlIndex(myObj->num);
2395 if ((!ScriptControl->_initialized) || (!ScriptControl->scriptOK)) {
2396 /* printf ("waiting for initializing script %d at %s:%d\n",(uintptr_t)to_ptr->routeToNode, __FILE__,__LINE__); */
2397 return JS_FALSE;;
2398 }
2399
2400 /* get context and global object for this script */
2401 cx2 = (JSContext*)ScriptControl->cx;
2402 obj2 = (JSObject*)ScriptControl->glob;
2403 //it doesn't seem to matter which cx/obj we use.
2404 cx2 = cx;
2405 obj2 = obj;
2406
2407 { // Scope A for our various stack objects (JSAutoRequest, RootedObject), so they all go
2408 // out of scope before we JS_DestroyContext.
2409 JSAutoRequest ar(cx); // In practice, you would want to exit this any
2410 // time you're spinning the event loop
2411 { // Scope B for JSAutoCompartment
2412 JSAutoCompartment ac(cx, obj2);
2413 /* set the time for this script */
2414 //SET_JS_TICKTIME()
2415 {
2416 jsval zimbo;
2417 JS_NewNumberValue(cx2, TickTime(), &zimbo);
2418 if (!JS_DefineProperty(cx2,obj2, "__eventInTickTime", zimbo, JS_GET_PROPERTY_STUB, JS_SET_PROPERTY_STUB2, JSPROP_PERMANENT)) {
2419 printf( "JS_DefineProperty failed for __eventInTickTime at %s:%d.\n",__FILE__,__LINE__);
2420 return JS_FALSE;
2421 }
2422 }
2423 //X3D_ECMA_TO_JS(cx, Data, datalen, dataType, &newval);
2424 //if( getSFNodeField(cx,obj, id, &newval) == JS_FALSE) //this is for getting fields from builtin node types, not Script nodes
2425 // return JS_FALSE;
2426 myfield = script_getField_viaCharName(myObj, _id_c);
2427
2428 //Q. do I need to deepcopy the vp?
2429 // newval = deepcopy(vp);
2430 //a slight difference: pointer copy: bool=true, deep copy: bool=1
2431 //I'll stick with pointer copy
2432 /*
2433 deepcopy = false;
2434 if(deepcopy)
2435 {
2436 //step 1. get the target/output field's datatype
2437 myfieldType = myfield->fieldDecl->fieldType;
2438
2439 //step 2. try and read the input *vp using that datatype
2440 // borrowed from jsUtils.c L.1247
2441 switch (myfieldType) {
2442 case FIELDTYPE_SFBool:
2443 case FIELDTYPE_SFFloat:
2444 case FIELDTYPE_SFTime:
2445 case FIELDTYPE_SFDouble:
2446 case FIELDTYPE_SFInt32:
2447 case FIELDTYPE_SFString:
2448 JS_ECMA_TO_X3D(cx2, &vrmlField, returnElementLength(myfieldType), myfieldType, vp);
2449 break;
2450 case FIELDTYPE_SFColor:
2451 case FIELDTYPE_SFNode:
2452 case FIELDTYPE_SFVec2f:
2453 case FIELDTYPE_SFVec3f:
2454 case FIELDTYPE_SFVec3d:
2455 case FIELDTYPE_SFRotation:
2456 JS_SF_TO_X3D(cx2,&vrmlField,returnElementLength(myfieldType) * returnElementRowSize(myfieldType) , myfieldType, vp);
2457 break;
2458 case FIELDTYPE_MFColor:
2459 case FIELDTYPE_MFVec3f:
2460 case FIELDTYPE_MFVec2f:
2461 case FIELDTYPE_MFFloat:
2462 case FIELDTYPE_MFTime:
2463 case FIELDTYPE_MFInt32:
2464 case FIELDTYPE_MFString:
2465 case FIELDTYPE_MFNode:
2466 case FIELDTYPE_MFRotation:
2467 case FIELDTYPE_SFImage:
2468 JS_MF_TO_X3D(cx2, obj2, &vrmlField, myfieldType, vp);
2469 break;
2470 default: printf ("unhandled type in setSFNodeField\n");
2471 return JS_FALSE;
2472 }
2473 //step 3. if successful, convert back to a second copy newval
2474 //int setField_FromEAI_ToScript(int tonode, int toname, int datatype, void *data, unsigned rowcount) {
2475 switch (myfieldType) {
2476 case FIELDTYPE_SFBool:
2477 case FIELDTYPE_SFFloat:
2478 case FIELDTYPE_SFTime:
2479 case FIELDTYPE_SFDouble:
2480 case FIELDTYPE_SFInt32:
2481 case FIELDTYPE_SFString:
2482 X3D_ECMA_TO_JS(cx2, &vrmlField, returnElementLength(myfieldType), myfieldType, &newval);
2483 break;
2484 case FIELDTYPE_SFColor:
2485 case FIELDTYPE_SFNode:
2486 case FIELDTYPE_SFVec2f:
2487 case FIELDTYPE_SFVec3f:
2488 case FIELDTYPE_SFVec3d:
2489 case FIELDTYPE_SFRotation:
2490 X3D_SF_TO_JS(cx2, obj2, &vrmlField, returnElementLength(myfieldType) * returnElementRowSize(myfieldType) , myfieldType, &newval);
2491 break;
2492 case FIELDTYPE_MFColor:
2493 case FIELDTYPE_MFVec3f:
2494 case FIELDTYPE_MFVec2f:
2495 case FIELDTYPE_MFFloat:
2496 case FIELDTYPE_MFTime:
2497 case FIELDTYPE_MFInt32:
2498 case FIELDTYPE_MFString:
2499 case FIELDTYPE_MFNode:
2500 case FIELDTYPE_MFRotation:
2501 case FIELDTYPE_SFImage:
2502 X3D_MF_TO_JS(cx2, obj2, &vrmlField, myfieldType, &newval, _id_c);
2503 break;
2504 default: printf ("unhandled type FIELDTYPE_ %d in getSFNodeField\n", myfieldType) ;
2505 return JS_FALSE;
2506 }
2507 }else{ //deepcopy
2508 */
2509 newval = *vp;
2510 //}
2511 /* get the variable name to hold the incoming value */
2512 //sprintf (scriptline,"__eventIn_Value_%s", _id_c);
2513 strcpy(scriptline,_id_c);
2514 #ifdef JSVRMLCLASSESVERBOSE
2515 printf ("set_one_ECMAtype, calling JS_DefineProperty on name %s obj %u, setting setECMANative, 0 \n",scriptline,obj2);
2516 #endif
2517
2518 if (!JS_DefineProperty(cx2,obj2, scriptline, newval, JS_GET_PROPERTY_STUB, JS_SET_PROPERTY_STUB3, JSPROP_PERMANENT)) {
2519 printf( "JS_DefineProperty failed for SFNodeSetProperty at %s:%d.\n",__FILE__,__LINE__);
2520 #if defined(JS_THREADSAFE)
2521 JS_EndRequest(cx);
2522 #endif
2523 return JS_FALSE;
2524 }
2525 /* is the function compiled yet? */
2526 //COMPILE_FUNCTION_IF_NEEDED(toname)
2527 JSparamnames = getJSparamnames();
2528 eventInFunction = (JSScript*) JSparamnames[myfield->fieldDecl->JSparamNameIndex].eventInFunction;
2529 if ( eventInFunction == NULL) {
2530 //sprintf (scriptline,"%s(__eventIn_Value_%s,__eventInTickTime)", _id_c, _id_c);
2531 sprintf (scriptline,"set_%s(%s,__eventInTickTime)", _id_c, _id_c);
2532 /* printf ("compiling function %s\n",scriptline); */
2533 eventInFunction = JS_CompileScript(cx2, obj2, scriptline, strlen(scriptline), "compile eventIn",1);
2534 if(true){
2535 //if (!JS_AddObjectRoot(cx2,&eventInFunction)) {
2536 JSparamnames[myfield->fieldDecl->JSparamNameIndex].eventInFunction = eventInFunction;
2537 #if JS_VERSION >= 185
2538 if (!JS_AddObjectRoot(cx,(JSObject**)(&JSparamnames[myfield->fieldDecl->JSparamNameIndex].eventInFunction))) {
2539 printf( "JS_AddObjectRoot failed for compilation of script \"%s\" at %s:%d.\n",scriptline,__FILE__,__LINE__);
2540 return JS_FALSE;
2541 }
2542 #endif
2543 }
2544 }
2545 /* and run the function */
2546 //RUN_FUNCTION (toname)
2547 {
2548 jsval zimbo;
2549 if (!JS_ExecuteScript(cx2, obj2, eventInFunction, &zimbo))
2550 {
2551 printf ("failed to set parameter for eventIn %s in FreeWRL code %s:%d\n",_id_c,__FILE__,__LINE__); \
2552 /* printf ("myThread is %u\n",pthread_self());*/ \
2553 return JS_FALSE;
2554 }
2555 return JS_TRUE;
2556 }
2557 } //Scope B
2558 } //Scope A
2559 }
2560 //dug9 July 9, 2014 - failed attempt to fix, see runQueuedDirectOutputs()
2561 // problem: 1) it's a lot of work to drill into the JS object for the other script to set the field value and valueChanged flag
2562 // 2) taking a shortcut in the routing loop would require refactoring: JSGlobal_object between get_valueChanged and js_setField_javascriptEventOut
2563 // would need to be generalized to do anyVrml or anyVrml passed directly up and down.
2564 if(0) if( ptr->handle->_nodeType== NODE_Script )
2565 {
2566 int itype, kind;
2567 //step 1. unconditionally write the script->field->value regardless of its kind/PKW
2568 struct ScriptFieldDecl* myfield;
2569 struct Shader_Script *script;
2570 struct CRscriptStruct *ScriptControl; // = getScriptControl();
2571 script = (struct Shader_Script*)X3D_SCRIPT(ptr->handle)->__scriptObj;
2572 /* is the script ok and initialized? */
2573 ScriptControl = getScriptControlIndex(script->num);
2574 if ((!ScriptControl->_initialized) || (!ScriptControl->scriptOK)) {
2575 /* printf ("waiting for initializing script %d at %s:%d\n",(uintptr_t)to_ptr->routeToNode, __FILE__,__LINE__); */
2576 return JS_FALSE;;
2577 }
2578 myfield = script_getField_viaCharName(script, _id_c);
2579 if(!myfield) return JS_FALSE;
2580 itype = ScriptFieldDecl_getType(myfield);
2581 kind = ScriptFieldDecl_getMode(myfield);
2582 Parser_scanStringValueToMem_B(&myfield->value, itype, _val_c, FALSE);
2583 if(kind == PKW_inputOnly || kind == PKW_inputOutput)
2584 myfield->eventInSet = TRUE; //flag for runQueuedDirectOutputs() to run eventIn function on other script, feeding it the value we just set
2585 if(kind == PKW_inputOutput || kind == PKW_outputOnly)
2586 myfield->valueChanged = TRUE; //flag for eventOuts on other script to send what we are writing
2587 return JS_TRUE;
2588 }
2589
2590 setField_fromJavascript (X3D_NODE(ptr->handle), _id_c, _val_c, FALSE);
2591 }
2592 } //SM_method == 2
2593 return JS_TRUE;
2594}
2595
2596
2597/********************************************************************/
2598
2599JSBool
2600SFRotationGetAxis(JSContext *cx, uintN argc, jsval *vp) {
2601 JSObject *obj = JS_THIS_OBJECT(cx,vp);
2602 jsval *argv = JS_ARGV(cx,vp);
2603
2604 JSObject *_retObj;
2605 float *cc, *cclhs;
2606
2607 UNUSED(argc);
2608 UNUSED(argv);
2609 #ifdef JSVRMLCLASSESVERBOSE
2610 printf ("start of SFRotationGetAxis\n");
2611 #endif
2612
2613 if ((_retObj = JS_ConstructObjectFw(cx, &SFVec3fClass, NULL, NULL)) == NULL) {
2614 printf( "JS_ConstructObject failed in SFRotationGetAxis.\n");
2615 return JS_FALSE;
2616 }
2617
2618 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_retObj));
2619
2620 if(SM_method()==2){
2621 AnyNative *_rot;
2622 AnyNative *_retNative;
2623 if ((_rot = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2624 printf( "JS_GetPrivate failed for obj in SFRotationGetAxis.\n");
2625 return JS_FALSE;
2626 }
2627
2628 if ((_retNative = (AnyNative *)JS_GetPrivateFw(cx, _retObj)) == NULL) {
2629 printf( "JS_GetPrivate failed for _retObj in SFRotationGetAxis.\n");
2630 return JS_FALSE;
2631 }
2632 cc = _rot->v->sfrotation.c;
2633 cclhs = _retNative->v->sfvec3f.c;
2634 }else{
2635 SFRotationNative *_rot;
2636 SFVec3fNative *_retNative;
2637 if ((_rot = (SFRotationNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2638 printf( "JS_GetPrivate failed for obj in SFRotationGetAxis.\n");
2639 return JS_FALSE;
2640 }
2641
2642 if ((_retNative = (SFVec3fNative *)JS_GetPrivateFw(cx, _retObj)) == NULL) {
2643 printf( "JS_GetPrivate failed for _retObj in SFRotationGetAxis.\n");
2644 return JS_FALSE;
2645 }
2646 cc = _rot->v.c;
2647 cclhs = _retNative->v.c;
2648
2649 }
2650 veccopy3f(cclhs,cc);
2651
2652 #ifdef JSVRMLCLASSESVERBOSE
2653 printf("SFRotationGetAxis: obj = %p, result = [%.9g, %.9g, %.9g]\n",
2654 obj,
2655 (_retNative->v).c[0],
2656 (_retNative->v).c[1],
2657 (_retNative->v).c[2]);
2658 #endif
2659
2660 return JS_TRUE;
2661}
2662
2663JSBool
2664SFRotationInverse(JSContext *cx, uintN argc, jsval *vp) {
2665 JSObject *obj = JS_THIS_OBJECT(cx,vp);
2666 jsval *argv = JS_ARGV(cx,vp);
2667
2668 JSObject *_retObj, *_proto;
2669 Quaternion q1,qret;
2670 double a,b,c,d;
2671 float *cc, *cclhs;
2672 UNUSED(argc);
2673 UNUSED(argv);
2674 #ifdef JSVRMLCLASSESVERBOSE
2675 printf ("start of SFRotationInverse\n");
2676 #endif
2677
2678 if ((_proto = JS_GetPrototypeFw(cx, obj)) == NULL) {
2679 printf( "JS_GetPrototype failed in SFRotationInverse.\n");
2680 return JS_FALSE;
2681 }
2682 if ((_retObj = JS_ConstructObjectFw(cx, &SFRotationClass, _proto, NULL)) == NULL) {
2683 printf( "JS_ConstructObject failed in SFRotationInverse.\n");
2684 return JS_FALSE;
2685 }
2686 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_retObj));
2687
2688
2689 if(SM_method()==2){
2690 AnyNative *_rot, *_retNative;
2691
2692 if ((_rot = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2693 printf( "JS_GetPrivate failed for obj in SFRotationInverse.\n");
2694 return JS_FALSE;
2695 }
2696
2697 if ((_retNative = (AnyNative *)JS_GetPrivateFw(cx, _retObj)) == NULL) {
2698 printf( "JS_GetPrivate failed for _retObj in SFRotationInverse.\n");
2699 return JS_FALSE;
2700 }
2701 cc = _rot->v->sfrotation.c;
2702 cclhs = _retNative->v->sfrotation.c;
2703
2704 }else{
2705 SFRotationNative *_rot, *_retNative;
2706
2707 if ((_rot = (SFRotationNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2708 printf( "JS_GetPrivate failed for obj in SFRotationInverse.\n");
2709 return JS_FALSE;
2710 }
2711
2712 if ((_retNative = (SFRotationNative *)JS_GetPrivateFw(cx, _retObj)) == NULL) {
2713 printf( "JS_GetPrivate failed for _retObj in SFRotationInverse.\n");
2714 return JS_FALSE;
2715 }
2716 _retNative->valueChanged = 1;
2717 cc = _rot->v.c;
2718 cclhs = _retNative->v.c;
2719 }
2720 /* convert both rotation to quaternion */
2721 vrmlrot_to_quaternion(&q1, (double) cc[0],
2722 (double) cc[1], (double)cc[2], (double) cc[3]);
2723
2724 /* invert it */
2725 quaternion_inverse(&qret,&q1);
2726
2727
2728 /* and return the resultant, as a vrml rotation */
2729 quaternion_to_vrmlrot(&qret, &a, &b, &c, &d);
2730 /* double to floats, can not use pointers... */
2731 cclhs[0] = (float) a;
2732 cclhs[1] = (float) b;
2733 cclhs[2] = (float) c;
2734 cclhs[3] = (float) d;
2735
2736 /* and, we now have a new value */
2737
2738 return JS_TRUE;
2739}
2740
2741JSBool
2742SFRotationMultiply(JSContext *cx, uintN argc, jsval *vp) {
2743 JSObject *obj = JS_THIS_OBJECT(cx,vp);
2744 jsval *argv = JS_ARGV(cx,vp);
2745
2746 Quaternion q1,q2,qret;
2747 double a,b,c,d;
2748
2749 JSObject *_multObj, *_proto, *_retObj;
2750 float *cc, *cc1, *cclhs;
2751 #ifdef JSVRMLCLASSESVERBOSE
2752 printf ("start of SFRotationMultiply\n");
2753 #endif
2754
2755 if (!JS_ConvertArguments(cx, argc, argv, "o", &_multObj)) {
2756 printf( "JS_ConvertArguments failed in SFRotationMultiply.\n");
2757 return JS_FALSE;
2758 }
2759 CHECK_CLASS(cx,_multObj,argv,__FUNCTION__,SFRotationClass)
2760
2761 if ((_proto = JS_GetPrototypeFw(cx, _multObj)) == NULL) {
2762 printf( "JS_GetPrototype failed in SFRotationMultiply.\n");
2763 return JS_FALSE;
2764 }
2765
2766 if ((_retObj = JS_ConstructObjectFw(cx, &SFRotationClass, _proto, NULL)) == NULL) {
2767 printf( "JS_ConstructObject failed in SFRotationMultiply.\n");
2768 return JS_FALSE;
2769 }
2770
2771
2772 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_retObj));
2773
2774 if(SM_method()==2){
2775 AnyNative *_rot1, *_rot2, *_retNative;
2776
2777 if ((_rot1 = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2778 printf( "JS_GetPrivate failed for obj in SFRotationMultiply.\n");
2779 return JS_FALSE;
2780 }
2781
2782 if ((_rot2 = (AnyNative *)JS_GetPrivateFw(cx, _multObj)) == NULL) {
2783 printf( "JS_GetPrivate failed for _multObj in SFRotationMultiply.\n");
2784 return JS_FALSE;
2785 }
2786
2787 if ((_retNative = (AnyNative *)JS_GetPrivateFw(cx, _retObj)) == NULL) {
2788 printf( "JS_GetPrivate failed for _retObj in SFRotationMultiply.\n");
2789 return JS_FALSE;
2790 }
2791 cc = _rot1->v->sfrotation.c;
2792 cc1 = _rot2->v->sfrotation.c;
2793 cclhs = _retNative->v->sfrotation.c;
2794 }else{
2795 SFRotationNative *_rot1, *_rot2, *_retNative;
2796
2797 if ((_rot1 = (SFRotationNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2798 printf( "JS_GetPrivate failed for obj in SFRotationMultiply.\n");
2799 return JS_FALSE;
2800 }
2801
2802 if ((_rot2 = (SFRotationNative *)JS_GetPrivateFw(cx, _multObj)) == NULL) {
2803 printf( "JS_GetPrivate failed for _multObj in SFRotationMultiply.\n");
2804 return JS_FALSE;
2805 }
2806
2807 if ((_retNative = (SFRotationNative *)JS_GetPrivateFw(cx, _retObj)) == NULL) {
2808 printf( "JS_GetPrivate failed for _retObj in SFRotationMultiply.\n");
2809 return JS_FALSE;
2810 }
2811 cc = _rot1->v.c;
2812 cc1 = _rot2->v.c;
2813 cclhs = _retNative->v.c;
2814 _retNative->valueChanged = 1;
2815
2816 }
2817 /* convert both rotations into quaternions */
2818 vrmlrot_to_quaternion(&q1, (double) cc[0],
2819 (double) cc[1], (double) cc[2], (double) cc[3]);
2820 vrmlrot_to_quaternion(&q2, (double) cc1[0],
2821 (double) cc1[1], (double) cc1[2], (double) cc1[3]);
2822
2823 /* multiply them */
2824 quaternion_multiply(&qret,&q1,&q2);
2825
2826
2827 /* and return the resultant, as a vrml rotation */
2828 quaternion_to_vrmlrot(&qret, &a, &b, &c, &d);
2829 /* double to floats, can not use pointers... */
2830 cclhs[0] = (float) a;
2831 cclhs[1] = (float) b;
2832 cclhs[2] = (float) c;
2833 cclhs[3] = (float) d;
2834
2835 /* and, we now have a new value */
2836
2837 return JS_TRUE;
2838}
2839
2840JSBool
2841SFRotationMultVec(JSContext *cx, uintN argc, jsval *vp) {
2842 JSObject *obj = JS_THIS_OBJECT(cx,vp);
2843 jsval *argv = JS_ARGV(cx,vp);
2844
2845 JSObject *_multObj, *_retObj, *_proto;
2846 float rl;
2847 //float vl;
2848 //float rlpt;
2849 float s, c, angle;
2850 //struct point_XYZ r, v, c1, c2;
2851 int i;
2852 float *rot, *vec, *cclhs;
2853 float c1[3], c2[3], r[3]; //temps
2854
2855
2856 #ifdef JSVRMLCLASSESVERBOSE
2857 printf ("start of SFRotationMultiVec\n");
2858 #endif
2859
2860 if (!JS_ConvertArguments(cx, argc, argv, "o", &_multObj)) {
2861 printf( "JS_ConvertArguments failed in SFRotationMultVec.\n");
2862 return JS_FALSE;
2863 }
2864
2865 CHECK_CLASS(cx,_multObj,argv,__FUNCTION__,SFVec3fClass)
2866
2867 if ((_proto = JS_GetPrototypeFw(cx, _multObj)) == NULL) {
2868 printf( "JS_GetPrototype failed in SFRotationMultVec.\n");
2869 return JS_FALSE;
2870 }
2871
2872 if ((_retObj = JS_ConstructObjectFw(cx, &SFVec3fClass, _proto, NULL)) == NULL) {
2873 printf( "JS_ConstructObject failed in SFRotationMultVec.\n");
2874 return JS_FALSE;
2875 }
2876
2877 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_retObj));
2878
2879 if(SM_method()==2){
2880 AnyNative *_rot;
2881 AnyNative *_vec, *_retNative;
2882
2883 if ((_rot = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2884 printf( "JS_GetPrivate failed for obj in SFRotationMultVec.\n");
2885 return JS_FALSE;
2886 }
2887
2888 if ((_vec = (AnyNative *)JS_GetPrivateFw(cx, _multObj)) == NULL) {
2889 printf( "JS_GetPrivate failed for_multObjin SFRotationMultVec.\n");
2890 return JS_FALSE;
2891 }
2892 if ((_retNative = (AnyNative *)JS_GetPrivateFw(cx, _retObj)) == NULL) {
2893 printf( "JS_GetPrivate failed for _retObj in SFRotationMultVec.\n");
2894 return JS_FALSE;
2895 }
2896 rot = _rot->v->sfrotation.c;
2897 vec = _vec->v->sfvec3f.c;
2898 cclhs = _retNative->v->sfvec3f.c;
2899
2900 }else{
2901 SFRotationNative *_rot;
2902 SFVec3fNative *_vec, *_retNative;
2903
2904 if ((_rot = (SFRotationNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2905 printf( "JS_GetPrivate failed for obj in SFRotationMultVec.\n");
2906 return JS_FALSE;
2907 }
2908 //COPY_SFVEC3F_TO_POINT_XYZ(r,_rot->v.c);
2909 //angle = _rot->v.c[3];
2910
2911 if ((_vec = (SFVec3fNative *)JS_GetPrivateFw(cx, _multObj)) == NULL) {
2912 printf( "JS_GetPrivate failed for_multObjin SFRotationMultVec.\n");
2913 return JS_FALSE;
2914 }
2915 //COPY_SFVEC3F_TO_POINT_XYZ(v,_vec->v.c);
2916 if ((_retNative = (SFVec3fNative *)JS_GetPrivateFw(cx, _retObj)) == NULL) {
2917 printf( "JS_GetPrivate failed for _retObj in SFRotationMultVec.\n");
2918 return JS_FALSE;
2919 }
2920 rot = _rot->v.c;
2921 vec = _vec->v.c;
2922 cclhs = _retNative->v.c;
2923 }
2924 rl = veclength3f(rot);
2925 angle = rot[3];
2926 s = (float) sin(angle);
2927 c = (float) cos(angle);
2928 veccross3f(c1,rot,vec);
2929 rl = veclength3f(c1);
2930 vecscale3f(c1,c1,1.0f/rl);
2931 veccross3f(c2,rot,c1);
2932 rl = veclength3f(c2);
2933 vecscale3f(c2,c2,1.0f/rl);
2934 for(i=0;i<3;i++)
2935 cclhs[i] = (float) (vec[i] + s * c1[i] + (1.0-c) * c2[i]);
2936
2937
2938 return JS_TRUE;
2939}
2940
2941JSBool
2942SFRotationSetAxis(JSContext *cx, uintN argc, jsval *vp) {
2943 JSObject *obj = JS_THIS_OBJECT(cx,vp);
2944 jsval *argv = JS_ARGV(cx,vp);
2945
2946 JSObject *_setAxisObj;
2947 float *rot, *vec;
2948
2949 #ifdef JSVRMLCLASSESVERBOSE
2950 printf ("start of SFRotationSetAxis\n");
2951 #endif
2952
2953 if (!JS_ConvertArguments(cx, argc, argv, "o", &_setAxisObj)) {
2954 printf( "JS_ConvertArguments failed in SFRotationSetAxis.\n");
2955 return JS_FALSE;
2956 }
2957
2958 CHECK_CLASS(cx,_setAxisObj,argv,__FUNCTION__,SFVec3fClass)
2959
2960 if(SM_method() == 2){
2961 AnyNative *_rot;
2962 AnyNative *_vec;
2963 if ((_rot = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2964 printf( "JS_GetPrivate failed for obj in SFRotationSetAxis.\n");
2965 return JS_FALSE;
2966 }
2967
2968 if ((_vec = (AnyNative *)JS_GetPrivateFw(cx, _setAxisObj)) == NULL) {
2969 printf( "JS_GetPrivate failed for _retObj in SFRotationSetAxis.\n");
2970 return JS_FALSE;
2971 }
2972 rot = _rot->v->sfrotation.c;
2973 vec = _vec->v->sfvec3f.c;
2974 }else{
2975 SFRotationNative *_rot;
2976 SFVec3fNative *_vec;
2977 if ((_rot = (SFRotationNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
2978 printf( "JS_GetPrivate failed for obj in SFRotationSetAxis.\n");
2979 return JS_FALSE;
2980 }
2981
2982 if ((_vec = (SFVec3fNative *)JS_GetPrivateFw(cx, _setAxisObj)) == NULL) {
2983 printf( "JS_GetPrivate failed for _retObj in SFRotationSetAxis.\n");
2984 return JS_FALSE;
2985 }
2986 rot = _rot->v.c;
2987 vec = _vec->v.c;
2988 }
2989 veccopy3f(rot,vec);
2990
2991 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
2992
2993
2994 #ifdef JSVRMLCLASSESVERBOSE
2995 printf("SFRotationSetAxis: obj = %p, result = [%.9g, %.9g, %.9g, %.9g]\n",
2996 obj,
2997 rot[0],
2998 rot[1],
2999 rot[2],
3000 rot[3]);
3001 #endif
3002
3003 return JS_TRUE;
3004}
3005
3006JSBool
3007SFRotationSlerp(JSContext *cx, uintN argc, jsval *vp) {
3008 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3009 jsval *argv = JS_ARGV(cx,vp);
3010 jsval rvalinst;
3011 jsval *rval = &rvalinst;
3012
3013 JSObject *_destObj, *_retObj, *_proto;
3014 Quaternion _quat, _quat_dest, _quat_ret;
3015 jsdouble t;
3016 float *rot, *dest, *ret;
3017
3018 #ifdef JSVRMLCLASSESVERBOSE
3019 printf ("start of SFRotationSlerp\n");
3020 #endif
3021 if (!JS_ConvertArguments(cx, argc, argv, "o d", &_destObj, &t)) {
3022 printf( "JS_ConvertArguments failed in SFRotationSlerp.\n");
3023 return JS_FALSE;
3024 }
3025
3026 CHECK_CLASS(cx,_destObj,argv,__FUNCTION__,SFRotationClass)
3027
3028
3029 /*
3030 * From Annex C, C.6.7.4:
3031 *
3032 * For t = 0, return object's rotation.
3033 * For t = 1, return 1st argument.
3034 * For 0 < t < 1, compute slerp.
3035 */
3036 if (APPROX(t, 0)) {
3037 *rval = OBJECT_TO_JSVAL(obj);
3038 } else if (APPROX(t, 1)) {
3039 *rval = OBJECT_TO_JSVAL(_destObj);
3040 } else {
3041 if ((_proto = JS_GetPrototypeFw(cx, _destObj)) == NULL) {
3042 printf( "JS_GetPrototype failed in SFRotationSlerp.\n");
3043 return JS_FALSE;
3044 }
3045
3046 if ((_retObj = JS_ConstructObjectFw(cx, &SFRotationClass, _proto, NULL)) == NULL) {
3047 printf( "JS_ConstructObject failed in SFRotationSlerp.\n");
3048 return JS_FALSE;
3049 }
3050 /* root the object */
3051 *rval = OBJECT_TO_JSVAL(_retObj);
3052
3053 if(SM_method() == 2){
3054 AnyNative *_rot, *_dest, *_ret;
3055 if ((_rot = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3056 printf( "JS_GetPrivate failed for obj in SFRotationSlerp.\n");
3057 return JS_FALSE;
3058 }
3059
3060 if ((_dest = (AnyNative *)JS_GetPrivateFw(cx, _destObj)) == NULL) {
3061 printf( "JS_GetPrivate failed for _destObj in SFRotationSlerp.\n");
3062 return JS_FALSE;
3063 }
3064
3065 if ((_ret = (AnyNative *)JS_GetPrivateFw(cx, _retObj)) == NULL) {
3066 printf( "JS_GetPrivate failed for _retObj in SFRotationSlerp.\n");
3067 return JS_FALSE;
3068 }
3069 rot = _rot->v->sfrotation.c;
3070 dest = _dest->v->sfrotation.c;
3071 ret = _ret->v->sfrotation.c;
3072 }else{
3073 SFRotationNative *_rot, *_dest, *_ret;
3074 if ((_rot = (SFRotationNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3075 printf( "JS_GetPrivate failed for obj in SFRotationSlerp.\n");
3076 return JS_FALSE;
3077 }
3078
3079 if ((_dest = (SFRotationNative *)JS_GetPrivateFw(cx, _destObj)) == NULL) {
3080 printf( "JS_GetPrivate failed for _destObj in SFRotationSlerp.\n");
3081 return JS_FALSE;
3082 }
3083
3084 if ((_ret = (SFRotationNative *)JS_GetPrivateFw(cx, _retObj)) == NULL) {
3085 printf( "JS_GetPrivate failed for _retObj in SFRotationSlerp.\n");
3086 return JS_FALSE;
3087 }
3088 rot = _rot->v.c;
3089 dest = _dest->v.c;
3090 ret = _ret->v.c;
3091 }
3092 vrmlrot_to_quaternion(&_quat,
3093 rot[0],
3094 rot[1],
3095 rot[2],
3096 rot[3]);
3097
3098 vrmlrot_to_quaternion(&_quat_dest,
3099 dest[0],
3100 dest[1],
3101 dest[2],
3102 dest[3]);
3103
3104 quaternion_slerp(&_quat_ret, &_quat, &_quat_dest, t);
3105 quaternion_to_vrmlrot4f(&_quat_ret,ret);
3106 }
3107
3108
3109 JS_SET_RVAL(cx,vp,*rval);
3110
3111 return JS_TRUE;
3112}
3113
3114
3115JSBool
3116SFRotationToString(JSContext *cx, uintN argc, jsval *vp) {
3117 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3118 jsval *argv = JS_ARGV(cx,vp);
3119
3120 JSString *_str;
3121 char buff[STRING];
3122 float *cc;
3123
3124 UNUSED(argc);
3125 #ifdef JSVRMLCLASSESVERBOSE
3126 printf ("start of SFRotationToString\n");
3127 #endif
3128
3129 ADD_ROOT (cx,ptr)
3130 ADD_ROOT(cx,_str)
3131 if(SM_method() == 2){
3132 AnyNative *ptr;
3133 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3134 printf( "JS_GetPrivate failed in SFRotationToString.\n");
3135 return JS_FALSE;
3136 }
3137 cc = ptr->v->sfrotation.c;
3138 }else{
3139 SFRotationNative *ptr;
3140 if ((ptr = (SFRotationNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3141 printf( "JS_GetPrivate failed in SFRotationToString.\n");
3142 return JS_FALSE;
3143 }
3144 cc = ptr->v.c;
3145 }
3146 memset(buff, 0, STRING);
3147 sprintf(buff, "%.9g %.9g %.9g %.9g",
3148 cc[0], cc[1], cc[2], cc[3]);
3149 _str = JS_NewStringCopyZ(cx, buff);
3150
3151 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
3152
3153
3154 REMOVE_ROOT (cx,ptr)
3155 REMOVE_ROOT (cx,_str)
3156 return JS_TRUE;
3157}
3158
3159JSBool
3160SFRotationAssign(JSContext *cx, uintN argc, jsval *vp) {
3161 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3162 jsval *argv = JS_ARGV(cx,vp);
3163 JSString *_id_jsstr;
3164
3165 JSObject *_from_obj;
3166 char *_id_str;
3167
3168 UNUSED(_id_str); // compiler warning mitigation
3169
3170
3171 #ifdef JSVRMLCLASSESVERBOSE
3172 printf ("start of SFRotationAssign\n");
3173 #endif
3174 if(SM_method() == 2){
3175 AnyNative *lhs, *rhs;
3176 if ((lhs = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3177 printf( "JS_GetPrivate failed for obj in SFRotationfAssign.\n");
3178 return JS_FALSE;
3179 }
3180 if (!(*vp).isObject())
3181 return JS_FALSE;
3182 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*vp))) == NULL) {
3183 printf("JS_ConvertArguments failed in SFRotationAssign. \n");
3184 return JS_FALSE;
3185 }
3186 if(lhs->type != rhs->type) return JS_FALSE;
3187 AnyNativeAssign(lhs,rhs);
3188 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
3189
3190
3191 }else{
3192 SFRotationNative *fptr, *ptr;
3193
3194 if ((ptr = (SFRotationNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3195 printf( "JS_GetPrivate failed for obj in SFRotationAssign.\n");
3196 return JS_FALSE;
3197 }
3198
3199 CHECK_CLASS(cx,obj,argv,__FUNCTION__,SFRotationClass)
3200
3201 if (JS_ConvertArguments(cx, argc, argv, "oS", &_from_obj, &_id_jsstr) == JS_TRUE) {
3202 _id_str = JS_EncodeString(cx,_id_jsstr);
3203 } else {
3204
3205 printf( "JS_ConvertArguments failed in SFRotationAssign.\n");
3206 return JS_FALSE;
3207 }
3208
3209 /* is this an assignment of NULL? */
3210 if (_from_obj == NULL) {
3211 printf ("we have an assignment to null in SFRotationAssign\n");
3212 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(NULL)); //JSVAL_VOID);
3213
3214 } else {
3215
3216
3217 CHECK_CLASS(cx,_from_obj,argv,__FUNCTION__,SFRotationClass)
3218
3219 if ((fptr = (SFRotationNative *)JS_GetPrivateFw(cx, _from_obj)) == NULL) {
3220 printf( "JS_GetPrivate failed for _from_obj in SFRotationAssign.\n");
3221 return JS_FALSE;
3222 }
3223 #ifdef JSVRMLCLASSESVERBOSE
3224 printf("SFRotationAssign: obj = %p, id = \"%s\", from = %p\n",
3225 obj, _id_str, _from_obj);
3226 #endif
3227
3228 SFRotationNativeAssign(ptr, fptr);
3229 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
3230
3231 }
3232 }
3233 #ifdef JSVRMLCLASSESVERBOSE
3234 printf("SFRotationAssign: returning object as jsval\n");
3235 #endif
3236 return JS_TRUE;
3237}
3238
3239JSBool
3240SFRotationConstr(JSContext *cx, uintN argc, jsval *vp) {
3241 JSObject *obj = JS_NewObject(cx,&SFRotationClass,NULL,NULL);
3242 jsval *argv = JS_ARGV(cx,vp);
3243
3244
3245 JSObject *_ob1, *_ob2;
3246 jsdouble pars[4];
3247 jsdouble doub;
3248 float v1len, v2len;
3249 double v12dp;
3250 struct point_XYZ v1, v2;
3251 int v3fv3f;
3252 float *cc, *vec, *vec2;
3253
3254 #ifdef JSVRMLCLASSESVERBOSE
3255 printf ("start of SFRotationConstr\n");
3256 #endif
3257
3258 ADD_ROOT(cx,obj)
3259 if(SM_method() == 2){
3260 AnyNative *any;
3261 if((any = (AnyNative*)AnyNativeNew(FIELDTYPE_SFRotation,NULL,NULL)) == NULL){
3262 printf( "AnyfNativeNew failed in SFRotationConstr.\n");
3263 return JS_FALSE;
3264 }
3265 if (!JS_SetPrivateFw(cx, obj, any)) {
3266 printf( "JS_SetPrivate failed in SFRotationConstr.\n");
3267 return JS_FALSE;
3268 }
3269 cc = any->v->sfrotation.c;
3270 }else{
3271 SFRotationNative *ptr;
3272
3273 if ((ptr = (SFRotationNative *)SFRotationNativeNew()) == NULL) {
3274 printf( "SFRotationNativeNew failed in SFRotationConstr.\n");
3275 return JS_FALSE;
3276 }
3277
3278 //if (!JS_DefineProperties(cx, obj, SFRotationProperties)) {
3279 // printf( "JS_DefineProperties failed in SFRotationConstr.\n");
3280 // return JS_FALSE;
3281 //}
3282
3283 if (!JS_SetPrivateFw(cx, obj, ptr)) {
3284 printf( "JS_SetPrivate failed in SFRotationConstr.\n");
3285 return JS_FALSE;
3286 }
3287 ptr->valueChanged = 1;
3288 cc = ptr->v.c;
3289
3290 }
3291 if (argc == 0) {
3292 cc[0] = 0.0f; cc[1] = 0.0f; cc[2] = 1.0f; cc[3] = 0.0f;
3293
3294 } else if (argc == 2) {
3295 /* two possibilities - SFVec3f/numeric, or SFVec3f/SFVec3f */
3296 if (argv[0].isObject()) {
3297/* _ob1 = (JSObject *)argv[0]; */
3298 _ob1 = JSVAL_TO_OBJECT(argv[0]);
3299
3300
3301 CHECK_CLASS(cx,_ob1,argv,__FUNCTION__,SFVec3fClass)
3302 if(SM_method()==2){
3303 AnyNative *_vec = NULL;
3304 if ((_vec = (AnyNative *)JS_GetPrivateFw(cx, _ob1)) == NULL) {
3305 printf( "JS_GetPrivate failed for arg format \"o d\" in SFRotationConstr.\n");
3306 return JS_FALSE;
3307 }
3308 vec = _vec->v->sfvec3f.c;
3309 }else{
3310 SFVec3fNative *_vec = NULL;
3311 if ((_vec = (SFVec3fNative *)JS_GetPrivateFw(cx, _ob1)) == NULL) {
3312 printf( "JS_GetPrivate failed for arg format \"o d\" in SFRotationConstr.\n");
3313 return JS_FALSE;
3314 }
3315 vec = _vec->v.c;
3316 }
3317 }
3318 //if (JSVAL_IS_OBJECT(argv[1])) {
3319 if (argv[1].isObject()) {
3320/* _ob2 = (JSObject *)argv[1]; */
3321 _ob2 = JSVAL_TO_OBJECT(argv[1]);
3322
3323 v3fv3f = TRUE;
3324
3325 CHECK_CLASS(cx,_ob2,argv,__FUNCTION__,SFVec3fClass)
3326 if(SM_method()==2){
3327 AnyNative *_vec2 = NULL;
3328 if ((_vec2 = (AnyNative *)JS_GetPrivateFw(cx, _ob2)) == NULL) {
3329 printf( "JS_GetPrivate failed for _ob1 in SFRotationConstr.\n");
3330 return JS_FALSE;
3331 }
3332 vec2 = _vec2->v->sfvec3f.c;
3333 }else{
3334 SFVec3fNative *_vec2 = NULL;
3335 if ((_vec2 = (SFVec3fNative *)JS_GetPrivateFw(cx, _ob2)) == NULL) {
3336 printf( "JS_GetPrivate failed for _ob1 in SFRotationConstr.\n");
3337 return JS_FALSE;
3338 }
3339 vec2 = _vec2->v.c;
3340 }
3341 } else {
3342 v3fv3f = FALSE;
3343 if (!JSVAL_IS_NUMBER(argv[1])) {
3344 printf ("SFRotationConstr param error - number expected\n");
3345 return JS_FALSE;
3346 }
3347 if (!JS_ValueToNumber(cx, argv[1], &doub)) {
3348 printf("JS_ValueToNumber failed in SFRotationConstr.\n");
3349 return JS_FALSE;
3350 }
3351 }
3352
3353
3354 if (!v3fv3f) {
3355 cc[0] = vec[0];
3356 cc[1] = vec[1];
3357 cc[2] = vec[2];
3358 cc[3] = (float) doub;
3359 } else {
3360 v1len = veclength3f(vec);
3361 v2len = veclength3f(vec2);
3362 v12dp = vecdot3f(vec,vec2);
3363 veccross3f(cc,vec,vec2); //I think the following is cross product
3364 //(ptr->v).c[0] = (float) (v1.y * v2.z - v2.y * v1.z);
3365 //(ptr->v).c[1] = (float) (v1.z * v2.x - v2.z * v1.x);
3366 //(ptr->v).c[2] = (float) (v1.x * v2.y - v2.x * v1.y);
3367 v12dp /= v1len * v2len;
3368 cc[3] = (float) atan2(sqrt(1 - v12dp * v12dp), v12dp);
3369 }
3370 } else if (argc == 4 && JS_ConvertArguments(cx, argc, argv, "d d d d",
3371 &(pars[0]), &(pars[1]), &(pars[2]), &(pars[3]))) {
3372 cc[0] = (float) pars[0];
3373 cc[1] = (float) pars[1];
3374 cc[2] = (float) pars[2];
3375 cc[3] = (float) pars[3];
3376 } else {
3377 printf( "Invalid arguments for SFRotationConstr.\n");
3378 return JS_FALSE;
3379 }
3380
3381 #ifdef JSVRMLCLASSESVERBOSE
3382 printf("SFRotationConstr: obj = %p, %u args, %f %f %f %f\n",
3383 obj, argc,
3384 cc[0], cc[1], cc[2], cc[3]);
3385 #endif
3386
3387
3388 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
3389
3390
3391 return JS_TRUE;
3392}
3393
3394JSBool
3395SFRotationGetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JS::MutableHandle<JS::Value> hvp){
3396 JSObject *obj = *hobj.address();
3397 jsid iid = *hiid.address();
3398 jsval *vp = hvp.address();
3399
3400 jsdouble d;
3401 float *cc;
3402
3403 jsval id;
3404 if (!JS_IdToValue(cx,iid,&id)) {
3405 printf("JS_IdToValue failed in SFRotationGetProperty.\n");
3406 return JS_FALSE;
3407 }
3408
3409
3410 #ifdef JSVRMLCLASSESVERBOSE
3411 printf ("start of SFRotationGetProperty\n");
3412 #endif
3413 if(SM_method()== 2){
3414 AnyNative *ptr;
3415 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3416 printf( "JS_GetPrivate failed in SFRotationGetProperty.\n");
3417 return JS_FALSE;
3418 }
3419 cc = ptr->v->sfrotation.c;
3420 }else{
3421 SFRotationNative *ptr;
3422 if ((ptr = (SFRotationNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3423 printf( "JS_GetPrivate failed in SFRotationGetProperty.\n");
3424 return JS_FALSE;
3425 }
3426 cc = ptr->v.c;
3427 }
3428 if (JSVAL_IS_INT(id)) {
3429 switch (JSVAL_TO_INT(id)) {
3430 case 0:
3431 d = cc[0];
3432 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
3433 printf(
3434 "JS_NewDouble failed for %f in SFRotationGetProperty.\n",
3435 d);
3436 return JS_FALSE;
3437 }
3438 break;
3439 case 1:
3440 d = cc[1];
3441 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
3442 printf(
3443 "JS_NewDouble failed for %f in SFRotationGetProperty.\n",
3444 d);
3445 return JS_FALSE;
3446 }
3447 break;
3448 case 2:
3449 d = cc[2];
3450 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
3451 printf(
3452 "JS_NewDouble failed for %f in SFRotationGetProperty.\n",
3453 d);
3454 return JS_FALSE;
3455 }
3456 break;
3457 case 3:
3458 d = cc[3];
3459 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
3460 printf(
3461 "JS_NewDouble failed for %f in SFRotationGetProperty.\n",
3462 d);
3463 return JS_FALSE;
3464 }
3465 break;
3466 }
3467 }
3468 return JS_TRUE;
3469}
3470
3471JSBool
3472SFRotationSetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
3473 JSObject *obj = *hobj.address();
3474 jsid iid = *hiid.address();
3475 jsval *vp = hvp.address();
3476
3477 jsval myv;
3478 float *cc;
3479
3480
3481 jsval id;
3482 if (!JS_IdToValue(cx,iid,&id)) {
3483 printf("JS_IdToValue failed in SFRotationSetProperty.\n");
3484 return JS_FALSE;
3485 }
3486
3487
3488 #ifdef JSVRMLCLASSESVERBOSE
3489 printf ("start of SFRotationSetProperty\n");
3490 #endif
3491 if(SM_method() == 2){
3492 AnyNative *any;
3493 if ((any = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3494 printf( "JS_GetPrivate failed in SFRotationProperty.\n");
3495 return JS_FALSE;
3496 }
3497 if(any->valueChanged)
3498 (*any->valueChanged)++;
3499 cc = any->v->sfrotation.c;
3500 }else{
3501 SFRotationNative *ptr;
3502
3503 if ((ptr = (SFRotationNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3504 printf( "JS_GetPrivate failed in SFRotationSetProperty.\n");
3505 return JS_FALSE;
3506 }
3507 ptr->valueChanged++;
3508 #ifdef JSVRMLCLASSESVERBOSE
3509 printf("SFRotationSetProperty: obj = %p, id = %d, valueChanged = %d\n",
3510 obj, JSVAL_TO_INT(id), ptr->valueChanged);
3511 #endif
3512 cc = ptr->v.c;
3513 }
3514 if (!JS_ConvertValue(cx, *vp, JSTYPE_NUMBER, &myv)) {
3515 printf( "JS_ConvertValue failed in SFRotationSetProperty.\n");
3516 return JS_FALSE;
3517 }
3518
3519 if (JSVAL_IS_INT(id)) {
3520 switch (JSVAL_TO_INT(id)) {
3521 case 0:
3522 cc[0] = (float) JSVAL_TO_DOUBLE(myv);
3523 break;
3524 case 1:
3525 cc[1] = (float) JSVAL_TO_DOUBLE(myv);
3526 break;
3527 case 2:
3528 cc[2] = (float) JSVAL_TO_DOUBLE(myv);
3529 break;
3530 case 3:
3531 cc[3] = (float) JSVAL_TO_DOUBLE(myv);
3532 break;
3533 }
3534 }
3535 return JS_TRUE;
3536}
3537
3538/********************************************************************/
3539
3540/* Generic SFVec2f routines that return a SFVec2f */
3541#define __2FADD 1
3542#define __2FDIVIDE 2
3543#define __2FMULT 3
3544#define __2FSUBT 4
3545#define __2FDOT 5
3546#define __2FLENGTH 6
3547#define __2FNORMALIZE 8
3548JSBool SFVec2fGeneric( JSContext *cx, JSObject *obj,
3549 uintN argc, jsval *argv, jsval *rval, int op) {
3550
3551 JSObject *_paramObj, *_proto, *_retObj;
3552 jsdouble d=0.0;
3553 jsdouble d0=0.0;
3554 jsdouble d1=0.0;
3555 struct point_XYZ v1, v2;
3556 float *cc, *cc2, cc3[2], cclhs[2];
3557
3558
3559 /* parameters */
3560 int SFParam = FALSE;
3561 int numParam = FALSE;
3562
3563 /* return values */
3564 int retSFVec2f = FALSE;
3565 int retNumeric = FALSE;
3566
3567 /* is the "argv" parameter a string? */
3568 int param_isString;
3569 char *charString;
3570 jsdouble pars[3];
3571 JSString *_str;
3572
3573 /* determine what kind of parameter to get */
3574 if ((op==__2FADD)||(op==__2FDOT)||(op==__2FSUBT))SFParam=TRUE;
3575 if ((op==__2FDIVIDE)||(op==__2FMULT))numParam=TRUE;
3576
3577 /* determine the return value, if it is NOT a SFVec2f */
3578 if ((op==__2FDOT)||(op==__2FLENGTH)) retNumeric = TRUE;
3579 retSFVec2f = (!retNumeric);
3580
3581 /* is the parameter a string, possibly gotten from the VRML/X3d
3582 * side of things? */
3583 param_isString = JSVAL_IS_STRING (*argv);
3584
3585 /* get the parameter */
3586 if ((SFParam) || (numParam)) {
3587 if (numParam) {
3588 if (!JSVAL_IS_NUMBER(argv[0])) {
3589 printf ("SFVec2f param error - number expected\n");
3590 return JS_FALSE;
3591 }
3592 if (!JS_ValueToNumber(cx, argv[0], &d)) {
3593 printf("JS_ValueToNumber failed in SFVec2f.\n");
3594 return JS_FALSE;
3595 }
3596 } else {
3597 /* did this come in from VRML as a string, or did
3598 * it get created in javascript? */
3599 if (param_isString) {
3600 _str = JS_ValueToString(cx, *argv);
3601 charString = JS_EncodeString(cx,_str);
3602
3603 if (sscanf(charString, "%lf %lf",
3604 &(pars[0]), &(pars[1])) != 2) {
3605 printf ("conversion problem in SFVec2fGeneric\n");
3606 return JS_FALSE;
3607 }
3608 /* printf ("past scan, %f %f %f\n",pars[0], pars[1]);*/
3609 cc3[0] = pars[0];
3610 cc3[1] = pars[1];
3611 cc2 = cc3;
3612 } else {
3613 if(SM_method()==2){
3614 if (argv[0].isObject()) {
3615 AnyNative *_vec2;
3616 if ((_vec2 = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(argv[0]))) == NULL) {
3617 printf("in SFVec2f, RHS was NOT native type \n");
3618 return JS_FALSE;
3619 }
3620 //if(_vec2->type == FIELDTYPE_SFVec2f) or color. if 3d convert....
3621 cc2 = _vec2->v->sfvec2f.c;
3622 }else{
3623 return JS_FALSE;
3624 }
3625
3626 }else{
3627 SFVec2fNative *_vec2 = NULL;
3628
3629 if (!JS_ConvertArguments(cx, argc, argv, "o", &_paramObj)) {
3630 printf( "JS_ConvertArguments failed in SFVec2f.\n");
3631 return JS_FALSE;
3632 }
3633
3634 CHECK_CLASS(cx,_paramObj,argv,__FUNCTION__,SFVec2fClass)
3635
3636 if ((_vec2 = (SFVec2fNative*)JS_GetPrivateFw(cx, _paramObj)) == NULL) {
3637 printf( "JS_GetPrivate failed for _paramObj in SFVec2f.\n");
3638 return JS_FALSE;
3639 }
3640 cc2 = _vec2->v.c;
3641 }
3642 }
3643 }
3644 }
3645
3646 /* get our values */
3647 if(SM_method() == 2){
3648 AnyNative *_vec1;
3649 if ((_vec1 = (AnyNative*)JS_GetPrivateFw(cx, obj)) == NULL) {
3650 printf( "JS_GetPrivate failed for obj in SFVec3fAdd.\n");
3651 return JS_FALSE;
3652 }
3653 cc = _vec1->v->sfvec2f.c;
3654 }else{
3655 SFVec2fNative *_vec1 = NULL;
3656 if ((_vec1 = (SFVec2fNative*)JS_GetPrivateFw(cx, obj)) == NULL) {
3657 printf( "JS_GetPrivate failed for obj in SFVec2fAdd.\n");
3658 return JS_FALSE;
3659 }
3660 cc = _vec1->v.c;
3661 }
3662 /* do the operation */
3663 switch (op) {
3664 /* returning a SFVec2f */
3665 case __2FADD:
3666 vecadd2f(cclhs,cc,cc2);
3667 break;
3668 case __2FDIVIDE:
3669 vecscale2f(cclhs,cc,1.0/d);
3670 break;
3671 case __2FMULT:
3672 vecscale2f(cclhs,cc,d);
3673 break;
3674 case __2FSUBT:
3675 vecdif2f(cclhs,cc,cc2);
3676 break;
3677 case __2FDOT:
3678 d = vecdot2f(cc,cc2);
3679 break;
3680 case __2FLENGTH:
3681 d = veclength2f(cc);
3682 break;
3683 case __2FNORMALIZE:
3684 vecnormal2f(cclhs,cc);
3685 break;
3686 default:
3687 return JS_FALSE;
3688 }
3689
3690 /* set the return object */
3691 if (retSFVec2f) {
3692 if ((_proto = JS_GetPrototypeFw(cx, obj)) == NULL) {
3693 printf( "JS_GetPrototype failed in SFVec2f.\n");
3694 return JS_FALSE;
3695 }
3696 if ((_retObj =
3697 JS_ConstructObjectFw(cx, &SFVec2fClass, _proto, NULL)) == NULL) {
3698 printf( "JS_ConstructObject failed in SFVec2f.\n");
3699 return JS_FALSE;
3700 }
3701 *rval = OBJECT_TO_JSVAL(_retObj);
3702 if(SM_method() == 2){
3703 AnyNative *any;
3704 if ((any = (AnyNative*)JS_GetPrivateFw(cx, _retObj)) == NULL) {
3705 printf( "JS_GetPrivate failed for _retObj in SFVec3f.\n");
3706 return JS_FALSE;
3707 }
3708 memcpy(any->v->sfvec2f.c,cclhs,2*sizeof(float));
3709 }else{
3710 SFVec3fNative *_retNative;
3711 if ((_retNative = (SFVec3fNative*)JS_GetPrivateFw(cx, _retObj)) == NULL) {
3712 printf( "JS_GetPrivate failed for _retObj in SFVec3f.\n");
3713 return JS_FALSE;
3714 }
3715 memcpy(_retNative->v.c,cclhs,2*sizeof(float));
3716 }
3717
3718 } else if (retNumeric) {
3719 if (JS_NewNumberValue(cx,d,rval) == JS_FALSE) {
3720 printf( "JS_NewDouble failed for %f in SFVec2f.\n",d);
3721 return JS_FALSE;
3722 }
3723 }
3724
3725 #ifdef JSVRMLCLASSESVERBOSE
3726 if (retSFVec2f){
3727 printf("SFVec2fgeneric: obj = %p, result = [%.9g, %.9g]\n",
3728 obj,
3729 (_retNative->v).c[0], (_retNative->v).c[1]);
3730 }
3731 if (retNumeric){
3732 printf("SFVec2fgeneric: obj = %p, result = %.9g\n",
3733 obj, d);
3734 }
3735 #endif
3736
3737 return JS_TRUE;
3738}
3739
3740JSBool
3741SFVec2fAdd(JSContext *cx, uintN argc, jsval *vp) {
3742 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3743 jsval *argv = JS_ARGV(cx,vp);
3744 jsval rval;
3745 JSBool retval = SFVec2fGeneric(cx, obj, argc, argv, &rval, __2FADD);
3746 JS_SET_RVAL(cx,vp,rval);
3747 return retval;
3748
3749}
3750
3751JSBool
3752SFVec2fDivide(JSContext *cx, uintN argc, jsval *vp) {
3753 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3754 jsval *argv = JS_ARGV(cx,vp);
3755 jsval rval;
3756 JSBool retval = SFVec2fGeneric(cx, obj, argc, argv, &rval, __2FDIVIDE);
3757 JS_SET_RVAL(cx,vp,rval);
3758 return retval;
3759}
3760
3761JSBool
3762SFVec2fMultiply(JSContext *cx, uintN argc, jsval *vp) {
3763 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3764 jsval *argv = JS_ARGV(cx,vp);
3765 jsval rval;
3766 JSBool retval = SFVec2fGeneric(cx, obj, argc, argv, &rval, __2FMULT);
3767 JS_SET_RVAL(cx,vp,rval);
3768 return retval;
3769
3770}
3771
3772JSBool
3773SFVec2fSubtract(JSContext *cx, uintN argc, jsval *vp) {
3774 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3775 jsval *argv = JS_ARGV(cx,vp);
3776 jsval rval;
3777 JSBool retval = SFVec2fGeneric(cx, obj, argc, argv, &rval, __2FSUBT);
3778 JS_SET_RVAL(cx,vp,rval);
3779 return retval;
3780
3781}
3782
3783JSBool
3784SFVec2fDot(JSContext *cx, uintN argc, jsval *vp) {
3785 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3786 jsval *argv = JS_ARGV(cx,vp);
3787 jsval rval;
3788 JSBool retval = SFVec2fGeneric(cx, obj, argc, argv, &rval, __2FDOT);
3789 JS_SET_RVAL(cx,vp,rval);
3790 return retval;
3791}
3792
3793JSBool
3794SFVec2fLength(JSContext *cx, uintN argc, jsval *vp) {
3795 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3796 jsval *argv = JS_ARGV(cx,vp);
3797 jsval rval;
3798 JSBool retval = SFVec2fGeneric(cx, obj, argc, argv, &rval, __2FLENGTH);
3799 JS_SET_RVAL(cx,vp,rval);
3800 return retval;
3801
3802}
3803
3804JSBool
3805SFVec2fNormalize(JSContext *cx, uintN argc, jsval *vp) {
3806 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3807 jsval *argv = JS_ARGV(cx,vp);
3808 jsval rval;
3809 JSBool retval = SFVec2fGeneric(cx, obj, argc, argv, &rval, __2FNORMALIZE);
3810 JS_SET_RVAL(cx,vp,rval);
3811 return retval;
3812}
3813
3814JSBool
3815SFVec2fToString(JSContext *cx, uintN argc, jsval *vp) {
3816 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3817 jsval *argv = JS_ARGV(cx,vp);
3818 JSString *_str;
3819 char buff[STRING];
3820 float *cc;
3821
3822 UNUSED(argc);
3823 UNUSED(argv);
3824 if(SM_method()==2){
3825 AnyNative *ptr;
3826 if ((ptr = (AnyNative*)JS_GetPrivateFw(cx, obj)) == NULL) {
3827 printf( "JS_GetPrivate failed in SFVec2fToString.\n");
3828 return JS_FALSE;
3829 }
3830 cc = ptr->v->sfvec2f.c;
3831 }else{
3832 SFVec2fNative *ptr;
3833 if ((ptr = (SFVec2fNative*)JS_GetPrivateFw(cx, obj)) == NULL) {
3834 printf( "JS_GetPrivate failed in SFVec2fToString.\n");
3835 return JS_FALSE;
3836 }
3837 cc = ptr->v.c;
3838 }
3839 memset(buff, 0, STRING);
3840 sprintf(buff, "%.9g %.9g",
3841 cc[0], cc[1]);
3842 _str = JS_NewStringCopyZ(cx, buff);
3843 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
3844
3845 return JS_TRUE;
3846}
3847
3848JSBool
3849SFVec2fAssign(JSContext *cx, uintN argc, jsval *vp) {
3850 JSObject *obj = JS_THIS_OBJECT(cx,vp);
3851 jsval *argv = JS_ARGV(cx,vp);
3852 JSString *_id_jsstr;
3853
3854 JSObject *_from_obj;
3855 char *_id_str;
3856
3857 UNUSED(_id_str); // compiler warning mitigation
3858
3859 if(SM_method() == 2){
3860 AnyNative *lhs, *rhs;
3861 if ((lhs = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3862 printf( "JS_GetPrivate failed for obj in SFVec3dAssign.\n");
3863 return JS_FALSE;
3864 }
3865 if (!(*vp).isObject())
3866 return JS_FALSE;
3867 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*vp))) == NULL) {
3868 printf("JS_ConvertArguments failed in SFVec2fAssign. \n");
3869 return JS_FALSE;
3870 }
3871 AnyNativeAssign(lhs,rhs);
3872 }else{
3873 SFVec2fNative *fptr, *ptr;
3874
3875 if ((ptr = (SFVec2fNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
3876 printf( "JS_GetPrivate failed for obj in SFVec2fAssign.\n");
3877 return JS_FALSE;
3878 }
3879
3880 CHECK_CLASS(cx,obj,argv,__FUNCTION__,SFVec2fClass)
3881
3882 if (JS_ConvertArguments(cx, argc, argv, "oS", &_from_obj, &_id_jsstr) == JS_TRUE) {
3883 _id_str = JS_EncodeString(cx,_id_jsstr);
3884 } else {
3885
3886 printf( "JS_ConvertArguments failed in SFVec2fAssign.\n");
3887 return JS_FALSE;
3888 }
3889
3890 CHECK_CLASS(cx,_from_obj,argv,__FUNCTION__,SFVec2fClass)
3891
3892 if ((fptr = (SFVec2fNative *)JS_GetPrivateFw(cx, _from_obj)) == NULL) {
3893 printf( "JS_GetPrivate failed for _from_obj in SFVec2fAssign.\n");
3894 return JS_FALSE;
3895 }
3896 #ifdef JSVRMLCLASSESVERBOSE
3897 printf("SFVec2fAssign: obj = %p, id = \"%s\", from = %p\n",
3898 obj, _id_str, _from_obj);
3899 #endif
3900
3901 SFVec2fNativeAssign(ptr, fptr);
3902 }
3903 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
3904
3905
3906 return JS_TRUE;
3907}
3908
3909JSBool
3910SFVec2fConstr(JSContext *cx, uintN argc, jsval *vp) {
3911 JSObject *obj = JS_NewObject(cx,&SFVec2fClass,NULL,NULL);
3912 jsval *argv = JS_ARGV(cx,vp);
3913
3914 jsdouble pars[2];
3915 float *cc;
3916
3917 ADD_ROOT(cx,obj)
3918
3919 if(SM_method() == 2){
3920 AnyNative *any;
3921 if((any = (AnyNative*)AnyNativeNew(FIELDTYPE_SFVec2f,NULL,NULL)) == NULL){
3922 printf( "AnyfNativeNew failed in SFVec3fConstr.\n");
3923 return JS_FALSE;
3924 }
3925 if (!JS_SetPrivateFw(cx, obj, any)) {
3926 printf( "JS_SetPrivate failed in SFVec2fConstr.\n");
3927 return JS_FALSE;
3928 }
3929
3930 cc = any->v->sfvec2f.c;
3931 }else{
3932 SFVec2fNative *ptr;
3933
3934 if ((ptr = (SFVec2fNative *) SFVec2fNativeNew()) == NULL) {
3935 printf( "SFVec2fNativeNew failed in SFVec2fConstr.\n");
3936 return JS_FALSE;
3937 }
3938
3939 //if (!JS_DefineProperties(cx, obj, SFVec2fProperties)) {
3940 // printf( "JS_DefineProperties failed in SFVec2fConstr.\n");
3941 // return JS_FALSE;
3942 //}
3943 if (!JS_SetPrivateFw(cx, obj, ptr)) {
3944 printf( "JS_SetPrivate failed in SFVec2fConstr.\n");
3945 return JS_FALSE;
3946 }
3947 cc = ptr->v.c;
3948 ptr->valueChanged = 1;
3949
3950 }
3951 if (argc == 0) {
3952 cc[0] = (float) 0.0;
3953 cc[1] = (float) 0.0;
3954 } else {
3955 if (!JS_ConvertArguments(cx, argc, argv, "d d", &(pars[0]), &(pars[1]))) {
3956 printf( "JS_ConvertArguments failed in SFVec2fConstr.\n");
3957 return JS_FALSE;
3958 }
3959 cc[0] = (float) pars[0];
3960 cc[1] = (float) pars[1];
3961 }
3962 #ifdef JSVRMLCLASSESVERBOSE
3963 printf("SFVec2fConstr: obj = %p, %u args, %f %f\n",
3964 obj, argc,
3965 (ptr->v).c[0], (ptr->v).c[1]);
3966 #endif
3967
3968
3969 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
3970
3971 return JS_TRUE;
3972}
3973
3974
3975JSBool
3976SFVec2fGetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JS::MutableHandle<JS::Value> hvp){
3977 JSObject *obj = *hobj.address();
3978 jsid iid = *hiid.address();
3979 jsval *vp = hvp.address();
3980
3981 jsdouble d;
3982 float *cc;
3983
3984 jsval id;
3985 if (!JS_IdToValue(cx,iid,&id)) {
3986 printf("JS_IdToValue failed in SFVec2fGetProperty.\n");
3987 return JS_FALSE;
3988 }
3989
3990
3991 if(SM_method()==2){
3992 AnyNative *any;
3993 if ((any = (AnyNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
3994 printf( "JS_GetPrivate failed in SFVec2fGetProperty.\n");
3995 return JS_FALSE;
3996 }
3997 cc = any->v->sfvec2f.c;
3998 }else{
3999 SFVec2fNative *ptr;
4000
4001 if ((ptr = (SFVec2fNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
4002 printf( "JS_GetPrivate failed in SFVec2fGetProperty.\n");
4003 return JS_FALSE;
4004 }
4005 cc = ptr->v.c;
4006 }
4007 if (JSVAL_IS_INT(id)) {
4008 switch (JSVAL_TO_INT(id)) {
4009 case 0:
4010 d = cc[0];
4011 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
4012 printf(
4013 "JS_NewDouble failed for %f in SFVec2fGetProperty.\n",
4014 d);
4015 return JS_FALSE;
4016 }
4017 break;
4018 case 1:
4019 d = cc[1];
4020 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
4021 printf(
4022 "JS_NewDouble failed for %f in SFVec2fGetProperty.\n",
4023 d);
4024 return JS_FALSE;
4025 }
4026 break;
4027 }
4028 }
4029 return JS_TRUE;
4030}
4031
4032JSBool
4033SFVec2fSetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
4034 JSObject *obj = *hobj.address();
4035 jsid iid = *hiid.address();
4036 jsval *vp = hvp.address();
4037
4038 jsval myv;
4039 float *cc;
4040
4041
4042 jsval id;
4043 if (!JS_IdToValue(cx,iid,&id)) {
4044 printf("JS_IdToValue failed in SFVec2fSetProperty.\n");
4045 return JS_FALSE;
4046 }
4047
4048
4049 if(SM_method() == 2){
4050 AnyNative *any;
4051 if ((any = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
4052 printf( "JS_GetPrivate failed in SFVec2fSetProperty.\n");
4053 return JS_FALSE;
4054 }
4055 if(any->valueChanged)
4056 (*any->valueChanged)++;
4057 cc = any->v->sfvec2f.c;
4058 }else{
4059 SFVec2fNative *ptr;
4060
4061 if ((ptr = (SFVec2fNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
4062 printf( "JS_GetPrivate failed in SFVec2fSetProperty.\n");
4063 return JS_FALSE;
4064 }
4065 ptr->valueChanged++;
4066 #ifdef JSVRMLCLASSESVERBOSE
4067 printf("SFVec2fSetProperty: obj = %p, id = %d, valueChanged = %d\n",
4068 obj, JSVAL_TO_INT(id), ptr->valueChanged);
4069 #endif
4070 cc = ptr->v.c;
4071 }
4072 if (!JS_ConvertValue(cx, *vp, JSTYPE_NUMBER, &myv)) {
4073 printf( "JS_ConvertValue failed in SFVec2fSetProperty.\n");
4074 return JS_FALSE;
4075 }
4076
4077 if (JSVAL_IS_INT(id)) {
4078 switch (JSVAL_TO_INT(id)) {
4079 case 0:
4080 cc[0] = (float) JSVAL_TO_DOUBLE(myv);
4081 break;
4082 case 1:
4083 cc[1] = (float) JSVAL_TO_DOUBLE(myv);
4084 break;
4085 default: break;
4086 }
4087 }
4088 return JS_TRUE;
4089}
4090
4091
4092/********************************************************************/
4093
4094/* Generic SFVec3f routines that return a SFVec3f */
4095#define __3FADD 1
4096#define __3FDIVIDE 2
4097#define __3FMULT 3
4098#define __3FSUBT 4
4099#define __3FDOT 5
4100#define __3FLENGTH 6
4101#define __3FNORMALIZE 8
4102#define __3FNEGATE 7
4103#define __3FCROSS 9
4104
4105JSBool SFVec3fGeneric( JSContext *cx, JSObject *obj,
4106 uintN argc, jsval *argv, jsval *rval, int op) {
4107 JSObject *_paramObj, *_proto, *_retObj;
4108 //SFVec3fNative *_vec1, *_vec2, *_retNative;
4109 jsdouble d=0.0;
4110 jsdouble d0=0.0;
4111 jsdouble d1=0.0;
4112 jsdouble d2=0.0;
4113 struct point_XYZ v1, v2, ret;
4114 float *cc, *cc2, cc3[3], cclhs[3];
4115
4116
4117 /* parameters */
4118 int SFParam = FALSE;
4119 int numParam = FALSE;
4120
4121 /* return values */
4122 int retSFVec3f = FALSE;
4123 int retNumeric = FALSE;
4124
4125 /* is the "argv" parameter a string? */
4126 int param_isString;
4127 char *charString;
4128 jsdouble pars[3];
4129 JSString *_str;
4130
4131 #ifdef JSVRMLCLASSESVERBOSE
4132 printf ("SFVec3fGeneric\n");
4133 #endif
4134
4135 /* determine what kind of parameter to get */
4136 if ((op==__3FADD)||(op==__3FDOT)||(op==__3FCROSS)||(op==__3FSUBT))SFParam=TRUE;
4137 if ((op==__3FDIVIDE)||(op==__3FMULT))numParam=TRUE;
4138
4139 /* determine the return value, if it is NOT a SFVec3f */
4140 if ((op==__3FDOT)||(op==__3FLENGTH)) retNumeric = TRUE;
4141 retSFVec3f = (!retNumeric);
4142
4143 /* is the parameter a string, possibly gotten from the VRML/X3d
4144 * side of things? */
4145 param_isString = JSVAL_IS_STRING (*argv);
4146
4147 /* get the parameter */
4148 if ((SFParam) || (numParam)) {
4149 if (numParam) {
4150 if (!JSVAL_IS_NUMBER(argv[0])) {
4151 printf ("SFVec3f param error - number expected\n");
4152 return JS_FALSE;
4153 }
4154 if (!JS_ValueToNumber(cx, argv[0], &d)) {
4155 printf("JS_ValueToNumber failed in SFVec3f.\n");
4156 return JS_FALSE;
4157 }
4158 } else {
4159 /* did this come in from VRML as a string, or did
4160 * it get created in javascript? */
4161 if (param_isString) {
4162 _str = JS_ValueToString(cx, *argv);
4163 charString = JS_EncodeString(cx,_str);
4164 if (sscanf(charString, "%lf %lf %lf",
4165 &(pars[0]), &(pars[1]), &(pars[2])) != 3) {
4166 printf ("conversion problem in SFVec3fGeneric\n");
4167 return JS_FALSE;
4168 }
4169 /* printf ("past scan, %f %f %f\n",pars[0], pars[1],pars[2]);*/
4170 cc3[0] = pars[0];
4171 cc3[1] = pars[1];
4172 cc3[2] = pars[2];
4173 cc2 = cc3;
4174 } else {
4175 if(SM_method() == 2){
4176 if (argv[0].isObject()) {
4177 AnyNative *_vec2;
4178 if ((_vec2 = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(argv[0]))) == NULL) {
4179 printf("in SFVec3d, RHS was NOT native type \n");
4180 return JS_FALSE;
4181 }
4182 //if(_vec2->type == FIELDTYPE_SFVec3f) or color. if 3d convert....
4183 cc2 = _vec2->v->sfvec3f.c;
4184 }else{
4185 return JS_FALSE;
4186 }
4187
4188 }else{
4189 SFVec3fNative *_vec2;
4190 if (!JS_ConvertArguments(cx, argc, argv, "o", &_paramObj)) {
4191 printf( "JS_ConvertArguments failed in SFVec3f.\n");
4192 return JS_FALSE;
4193 }
4194
4195 CHECK_CLASS(cx,_paramObj,argv,__FUNCTION__,SFVec3fClass)
4196
4197 /* get the second object's data */
4198 if ((_vec2 = (SFVec3fNative*)JS_GetPrivateFw(cx, _paramObj)) == NULL) {
4199 printf( "JS_GetPrivate failed for _paramObj in SFVec3f.\n");
4200 return JS_FALSE;
4201 }
4202 cc2 = _vec2->v.c;
4203 }
4204 }
4205 }
4206 }
4207
4208 /* get our values */
4209 if(SM_method() == 2){
4210 AnyNative *_vec1;
4211 if ((_vec1 = (AnyNative*)JS_GetPrivateFw(cx, obj)) == NULL) {
4212 printf( "JS_GetPrivate failed for obj in SFVec3fAdd.\n");
4213 return JS_FALSE;
4214 }
4215 cc = _vec1->v->sfvec3f.c;
4216 }else{
4217 SFVec3fNative *_vec1;
4218 if ((_vec1 = (SFVec3fNative*)JS_GetPrivateFw(cx, obj)) == NULL) {
4219 printf( "JS_GetPrivate failed for obj in SFVec3fAdd.\n");
4220 return JS_FALSE;
4221 }
4222 cc = _vec1->v.c;
4223 }
4224 /* do the operation */
4225 #ifdef JSVRMLCLASSESVERBOSE
4226 printf ("SFVec3f generic, vec2 %f %f %f\n",pars[0],pars[1],pars[2]);
4227 #endif
4228
4229 switch (op) {
4230 /* returning a SFVec3f */
4231 case __3FADD:
4232 vecadd3f(cclhs,cc,cc2);
4233 break;
4234 case __3FDIVIDE:
4235 vecscale3f(cclhs,cc,1.0/d);
4236 break;
4237 case __3FMULT:
4238 vecscale3f(cclhs,cc,d);
4239 break;
4240 case __3FSUBT:
4241 vecdif3f(cclhs,cc,cc2);
4242 break;
4243 case __3FDOT:
4244 d = vecdot3f(cc,cc2);
4245 break;
4246 case __3FCROSS:
4247 veccross3f(cclhs,cc,cc2);
4248 break;
4249 case __3FLENGTH:
4250 d = veclength3f(cc);
4251 break;
4252 case __3FNORMALIZE:
4253 vecnormalize3f(cclhs,cc);
4254 break;
4255 case __3FNEGATE:
4256 vecnegate3f(cclhs,cc);
4257 break;
4258 default:
4259 printf ("woops... %d\n",op);
4260 return JS_FALSE;
4261 }
4262
4263 #ifdef JSVRMLCLASSESVERBOSE
4264 printf ("past calcs\n");
4265 #endif
4266
4267 /* set the return object */
4268 if (retSFVec3f) {
4269 #ifdef JSVRMLCLASSESVERBOSE
4270 printf ("returning SFVec3f\n");
4271 #endif
4272 if ((_proto = JS_GetPrototypeFw(cx, obj)) == NULL) {
4273 printf( "JS_GetPrototype failed in SFVec3f.\n");
4274 return JS_FALSE;
4275 }
4276 if ((_retObj =
4277 JS_ConstructObjectFw(cx, &SFVec3fClass, _proto, NULL)) == NULL) {
4278 printf( "JS_ConstructObject failed in SFVec3f.\n");
4279 return JS_FALSE;
4280 }
4281 *rval = OBJECT_TO_JSVAL(_retObj);
4282 if(SM_method() == 2){
4283 AnyNative *any;
4284 if ((any = (AnyNative*)JS_GetPrivateFw(cx, _retObj)) == NULL) {
4285 printf( "JS_GetPrivate failed for _retObj in SFVec3f.\n");
4286 return JS_FALSE;
4287 }
4288 memcpy(any->v->sfvec3f.c,cclhs,3*sizeof(float));
4289 }else{
4290 SFVec3fNative *_retNative;
4291 if ((_retNative = (SFVec3fNative*)JS_GetPrivateFw(cx, _retObj)) == NULL) {
4292 printf( "JS_GetPrivate failed for _retObj in SFVec3f.\n");
4293 return JS_FALSE;
4294 }
4295 memcpy(_retNative->v.c,cclhs,3*sizeof(float));
4296 }
4297 } else if (retNumeric) {
4298 if (JS_NewNumberValue(cx,d,rval) == JS_FALSE) {
4299 printf( "JS_NewDouble failed for %f in SFVec3f.\n",d);
4300 return JS_FALSE;
4301 }
4302 }
4303 #ifdef JSVRMLCLASSESVERBOSE
4304 if (retSFVec3f){
4305 printf("SFVec3fgeneric: obj = %p, result = [%.9g, %.9g, %.9g]\n",
4306 obj,
4307 (_retNative->v).c[0], (_retNative->v).c[1],
4308 (_retNative->v).c[2]);
4309 }
4310 if (retNumeric){
4311 printf("SFVec2fgeneric: obj = %p, result = %.9g\n",
4312 obj, d);
4313 }
4314 #endif
4315return JS_TRUE;
4316}
4317
4318JSBool
4319SFVec3fAdd(JSContext *cx, uintN argc, jsval *vp) {
4320 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4321 jsval *argv = JS_ARGV(cx,vp);
4322 jsval rval;
4323 JSBool retval = SFVec3fGeneric(cx, obj, argc, argv, &rval, __3FADD);
4324 JS_SET_RVAL(cx,vp,rval);
4325 return retval;
4326}
4327
4328JSBool
4329SFVec3fCross(JSContext *cx, uintN argc, jsval *vp) {
4330 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4331 jsval *argv = JS_ARGV(cx,vp);
4332 jsval rval;
4333 JSBool retval = SFVec3fGeneric(cx, obj, argc, argv, &rval, __3FCROSS);
4334 JS_SET_RVAL(cx,vp,rval);
4335 return retval;
4336}
4337
4338JSBool
4339SFVec3fDivide(JSContext *cx, uintN argc, jsval *vp) {
4340 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4341 jsval *argv = JS_ARGV(cx,vp);
4342 jsval rval;
4343 JSBool retval = SFVec3fGeneric(cx, obj, argc, argv, &rval, __3FDIVIDE);
4344 JS_SET_RVAL(cx,vp,rval);
4345 return retval;
4346}
4347
4348JSBool
4349SFVec3fDot(JSContext *cx, uintN argc, jsval *vp) {
4350 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4351 jsval *argv = JS_ARGV(cx,vp);
4352 jsval rval;
4353 JSBool retval = SFVec3fGeneric(cx, obj, argc, argv, &rval, __3FDOT);
4354 JS_SET_RVAL(cx,vp,rval);
4355 return retval;
4356}
4357
4358JSBool
4359SFVec3fLength(JSContext *cx, uintN argc, jsval *vp) {
4360 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4361 jsval *argv = JS_ARGV(cx,vp);
4362 jsval rval;
4363 JSBool retval = SFVec3fGeneric(cx, obj, argc, argv, &rval, __3FLENGTH);
4364 JS_SET_RVAL(cx,vp,rval);
4365 return retval;
4366}
4367
4368
4369JSBool
4370SFVec3fMultiply(JSContext *cx, uintN argc, jsval *vp) {
4371 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4372 jsval *argv = JS_ARGV(cx,vp);
4373 jsval rval;
4374 JSBool retval = SFVec3fGeneric(cx, obj, argc, argv, &rval, __3FMULT);
4375 JS_SET_RVAL(cx,vp,rval);
4376 return retval;
4377}
4378
4379
4380JSBool
4381SFVec3fNegate(JSContext *cx, uintN argc, jsval *vp) {
4382 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4383 jsval *argv = JS_ARGV(cx,vp);
4384 jsval rval;
4385 JSBool retval = SFVec3fGeneric(cx, obj, argc, argv, &rval, __3FNEGATE);
4386 JS_SET_RVAL(cx,vp,rval);
4387 return retval;
4388}
4389
4390JSBool
4391SFVec3fNormalize(JSContext *cx, uintN argc, jsval *vp) {
4392 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4393 jsval *argv = JS_ARGV(cx,vp);
4394 jsval rval;
4395 JSBool retval = SFVec3fGeneric(cx, obj, argc, argv, &rval, __3FNORMALIZE);
4396 JS_SET_RVAL(cx,vp,rval);
4397 return retval;
4398}
4399
4400JSBool
4401SFVec3fSubtract(JSContext *cx, uintN argc, jsval *vp) {
4402 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4403 jsval *argv = JS_ARGV(cx,vp);
4404 jsval rval;
4405 JSBool retval = SFVec3fGeneric(cx, obj, argc, argv, &rval, __3FSUBT);
4406 JS_SET_RVAL(cx,vp,rval);
4407 return retval;
4408}
4409
4410JSBool
4411SFVec3fToString(JSContext *cx, uintN argc, jsval *vp) {
4412 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4413 jsval *argv = JS_ARGV(cx,vp);
4414
4415 JSString *_str;
4416 char buff[STRING];
4417 float *cc;
4418
4419 UNUSED(argc);
4420 UNUSED(argv);
4421 if(SM_method()==2){
4422 AnyNative *ptr;
4423 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
4424 printf( "JS_GetPrivate failed in SFVec3fToString.\n");
4425 return JS_FALSE;
4426 }
4427 cc = ptr->v->sfvec3f.c;
4428 }else{
4429 SFVec3fNative *ptr;
4430 if ((ptr = (SFVec3fNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
4431 printf( "JS_GetPrivate failed in SFVec3fToString.\n");
4432 return JS_FALSE;
4433 }
4434 cc = ptr->v.c;
4435 }
4436 memset(buff, 0, STRING);
4437 sprintf(buff, "%.9g %.9g %.9g",
4438 cc[0], cc[1], cc[2]);
4439 _str = JS_NewStringCopyZ(cx, buff);
4440
4441 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
4442
4443 #ifdef JSVRMLCLASSESVERBOSE
4444 printf ("SFVec3fToString, string is :%s:\n",buff);
4445 #endif
4446
4447 return JS_TRUE;
4448}
4449
4450
4451JSBool
4452SFVec3fAssign(JSContext *cx, uintN argc, jsval *vp) {
4453 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4454 jsval *argv = JS_ARGV(cx,vp);
4455 JSString *_id_jsstr;
4456 JSObject *_from_obj;
4457 char *_id_str;
4458
4459
4460 UNUSED(_id_str); // compiler warning mitigation
4461
4462 #ifdef JSVRMLCLASSESVERBOSE
4463 printf ("start of SFVec3fAssign\n");
4464 #endif
4465
4466 if(SM_method() == 2){
4467 AnyNative *lhs, *rhs;
4468 if ((lhs = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
4469 printf( "JS_GetPrivate failed for obj in SFVec3dAssign.\n");
4470 return JS_FALSE;
4471 }
4472 if (!(*vp).isObject())
4473 return JS_FALSE;
4474 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*vp))) == NULL) {
4475 printf("JS_ConvertArguments failed in SFVec3fAssign. \n");
4476 return JS_FALSE;
4477 }
4478 AnyNativeAssign(lhs,rhs);
4479 }else{
4480 SFVec3fNative *fptr, *ptr;
4481 if ((ptr = (SFVec3fNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
4482 printf( "JS_GetPrivate failed for obj in SFVec3fAssign.\n");
4483 return JS_FALSE;
4484 }
4485
4486 CHECK_CLASS(cx,obj,argv,__FUNCTION__,SFVec3fClass)
4487
4488 if (JS_ConvertArguments(cx, argc, argv, "oS", &_from_obj, &_id_jsstr)) {
4489 _id_str = JS_EncodeString(cx,_id_jsstr);
4490 } else {
4491 printf( "JS_ConvertArguments failed in SFVec3fAssign.\n");
4492 return JS_FALSE;
4493 }
4494
4495 CHECK_CLASS(cx,_from_obj,argv,__FUNCTION__,SFVec3fClass)
4496
4497 if ((fptr = (SFVec3fNative *)JS_GetPrivateFw(cx, _from_obj)) == NULL) {
4498 printf( "JS_GetPrivate failed for _from_obj in SFVec3fAssign.\n");
4499 return JS_FALSE;
4500 }
4501 #ifdef JSVRMLCLASSESVERBOSE
4502 printf("SFVec3fAssign: obj = %p, id = \"%s\", from = %p\n",
4503 obj, _id_str, _from_obj);
4504 #endif
4505
4506 SFVec3fNativeAssign(ptr, fptr);
4507 }
4508 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
4509
4510 #ifdef JSVRMLCLASSESVERBOSE
4511 printf ("end of SFVec3fAssign\n");
4512 #endif
4513
4514 return JS_TRUE;
4515}
4516
4517JSBool
4518SFVec3fConstr(JSContext *cx, uintN argc, jsval *vp) {
4519 JSObject *obj = JS_NewObject(cx,&SFVec3fClass,NULL,NULL);
4520 jsval *argv = JS_ARGV(cx,vp);
4521 jsdouble pars[3];
4522 float *cc;
4523
4524 #ifdef JSVRMLCLASSESVERBOSE
4525 printf ("start of SFVec3fConstr\n");
4526 #endif
4527
4528 ADD_ROOT(cx,obj)
4529 if(SM_method() == 2){
4530 AnyNative *any;
4531 if((any = (AnyNative*)AnyNativeNew(FIELDTYPE_SFVec3f,NULL,NULL)) == NULL){
4532 printf( "AnyfNativeNew failed in SFVec3fConstr.\n");
4533 return JS_FALSE;
4534 }
4535 if (!JS_SetPrivateFw(cx, obj, any)) {
4536 printf( "JS_SetPrivate failed in SFVec3fConstr.\n");
4537 return JS_FALSE;
4538 }
4539 cc = any->v->sfvec3f.c;
4540 }else{
4541 SFVec3fNative *ptr;
4542 if ((ptr = (SFVec3fNative *) SFVec3fNativeNew()) == NULL) {
4543 printf( "SFVec3fNativeNew failed in SFVec3fConstr.\n");
4544 return JS_FALSE;
4545 }
4546
4547 //if (!JS_DefineProperties(cx, obj, SFVec3fProperties)) {
4548 // printf( "JS_DefineProperties failed in SFVec3fConstr.\n");
4549 // return JS_FALSE;
4550 //}
4551 if (!JS_SetPrivateFw(cx, obj, ptr)) {
4552 printf( "JS_SetPrivate failed in SFVec3fConstr.\n");
4553 return JS_FALSE;
4554 }
4555 ptr->valueChanged = 1;
4556 cc = ptr->v.c;
4557 }
4558 if (argc == 0) {
4559 cc[0] = 0.0f;
4560 cc[1] = 0.0f;
4561 cc[2] = 0.0f;
4562 } else if(argc == 1){
4563 if(SM_method() == 2){
4564 int found = 0;
4565 if (argv[0].isObject()) {
4566 AnyNative *rhs;
4567 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(argv[0]))) != NULL) {
4568 union anyVrml *anyv = rhs->v;
4569 int rhstype = rhs->type;
4570 found = 1;
4571 switch(rhstype){
4572 case FIELDTYPE_SFVec3f:
4573 veccopy3f(cc,anyv->sfvec3f.c); break;
4574 case FIELDTYPE_SFVec3d:
4575 double2float(cc,anyv->sfvec3d.c,3); break;
4576 case FIELDTYPE_SFRotation:
4577 veccopy3f(cc,anyv->sfrotation.c); break;
4578 default:
4579 vecset3f(cc,0.0f,0.0f,0.0f);
4580 ConsoleMessage("new SFVec3f( obj ) doesn't handle obj type %d\n",rhstype);
4581 found = 0;
4582 }
4583 }
4584 }
4585 if(!found)
4586 return JS_FALSE;
4587 }
4588 } else {
4589 if (!JS_ConvertArguments(cx, argc, argv, "d d d",
4590 &(pars[0]), &(pars[1]), &(pars[2]))) {
4591 printf( "JS_ConvertArguments failed in SFVec3fConstr.\n");
4592 return JS_FALSE;
4593 }
4594 cc[0] = (float) pars[0];
4595 cc[1] = (float) pars[1];
4596 cc[2] = (float) pars[2];
4597 }
4598 #ifdef JSVRMLCLASSESVERBOSE
4599 printf("SFVec3fConstr: obj = %p, %u args, %f %f %f\n",
4600 obj, argc,
4601 cc[0], cc[1], cc[2]);
4602 #endif
4603
4604
4605 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
4606 return JS_TRUE;
4607}
4608
4609JSBool
4610SFVec3fGetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JS::MutableHandle<JS::Value> hvp){
4611 JSObject *obj = *hobj.address();
4612 jsid iid = *hiid.address();
4613 jsval *vp = hvp.address();
4614
4615 jsdouble d;
4616 float *cc;
4617 #ifdef JSVRMLCLASSESVERBOSE
4618 JSString *_idStr;
4619 char *_id_c;
4620 #endif
4621
4622 jsval id;
4623 if (!JS_IdToValue(cx,iid,&id)) {
4624 printf("JS_IdToValue failed in SFVec3fGetProperty.\n");
4625 return JS_FALSE;
4626 }
4627
4628 #ifdef JSVRMLCLASSESVERBOSE
4629
4630 //JSString *_idStr;
4631 //char *_id_c;
4632
4633/* note, since same variables are used, this first bit gets overwritten -- commenting out
4634 _idStr = JS_ValueToString(cx, id);
4635 _id_c = JS_GetStringBytes(_idStr);*/
4636 _idStr = JS_ValueToString(cx, *vp);
4637#if JS_VERSION < 185
4638 _id_c = JS_GetStringBytes(_idStr);
4639#else
4640 _id_c = JS_EncodeString(cx,_idStr);
4641#endif
4642 #endif
4643 if(SM_method()==2){
4644 AnyNative *any;
4645 if ((any = (AnyNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
4646 printf( "JS_GetPrivate failed in SFVec3fGetProperty.\n");
4647 return JS_FALSE;
4648 }
4649 cc = any->v->sfvec3f.c;
4650 }else{
4651 SFVec3fNative *ptr;
4652 if ((ptr = (SFVec3fNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
4653 printf( "JS_GetPrivate failed in SFVec3fGetProperty.\n");
4654 return JS_FALSE;
4655 }
4656 cc = ptr->v.c;
4657 }
4658 if (JSVAL_IS_INT(id)) {
4659 switch (JSVAL_TO_INT(id)) {
4660 case 0:
4661 d = cc[0];
4662 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
4663 printf(
4664 "JS_NewDouble failed for %f in SFVec3fGetProperty.\n",
4665 d);
4666 return JS_FALSE;
4667 }
4668 break;
4669 case 1:
4670 d = cc[1];
4671 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
4672 printf(
4673 "JS_NewDouble failed for %f in SFVec3fGetProperty.\n",
4674 d);
4675 return JS_FALSE;
4676 }
4677 break;
4678 case 2:
4679 d = cc[2];
4680 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
4681 printf(
4682 "JS_NewDouble failed for %f in SFVec3fGetProperty.\n",
4683 d);
4684 return JS_FALSE;
4685 }
4686 break;
4687 }
4688 } else {
4689 #ifdef JSVRMLCLASSESVERBOSE
4690 printf ("SFVec3fGetProperty, id is NOT an int...\n");
4691 #endif
4692 }
4693
4694 return JS_TRUE;
4695}
4696
4697JSBool
4698SFVec3fSetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
4699 JSObject *obj = *hobj.address();
4700 jsid iid = *hiid.address();
4701 jsval *vp = hvp.address();
4702
4703 jsval myv;
4704 float *cc;
4705
4706 jsval id;
4707 if (!JS_IdToValue(cx,iid,&id)) {
4708 printf("JS_IdToValue failed in SFVec3fSetProperty.\n");
4709 return JS_FALSE;
4710 }
4711
4712
4713 if(SM_method() == 2){
4714 AnyNative *any;
4715 if ((any = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
4716 printf( "JS_GetPrivate failed in SFVec3fSetProperty.\n");
4717 return JS_FALSE;
4718 }
4719 if(any->valueChanged)
4720 (*any->valueChanged)++;
4721 cc = any->v->sfvec3f.c;
4722 }else{
4723 SFVec3fNative *ptr;
4724 if ((ptr = (SFVec3fNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
4725 printf( "JS_GetPrivate failed in SFVec3fSetProperty.\n");
4726 return JS_FALSE;
4727 }
4728 ptr->valueChanged++;
4729 #ifdef JSVRMLCLASSESVERBOSE
4730 printf("SFVec3fSetProperty: obj = %p, id = %d, valueChanged = %d\n",
4731 obj, JSVAL_TO_INT(id), ptr->valueChanged);
4732 #endif
4733 cc = ptr->v.c;
4734 }
4735 if (!JS_ConvertValue(cx, *vp, JSTYPE_NUMBER, &myv)) {
4736 printf( "JS_ConvertValue failed in SFVec3fSetProperty.\n");
4737 return JS_FALSE;
4738 }
4739
4740 if (JSVAL_IS_INT(id)) {
4741 switch (JSVAL_TO_INT(id)) {
4742 case 0:
4743 cc[0] = (float) JSVAL_TO_DOUBLE(myv);
4744 break;
4745 case 1:
4746 cc[1] = (float) JSVAL_TO_DOUBLE(myv);
4747 break;
4748 case 2:
4749 cc[2] = (float) JSVAL_TO_DOUBLE(myv);
4750 break;
4751 }
4752 }
4753 return JS_TRUE;
4754}
4755
4756/********************************************************************/
4757
4758/* Generic SFVec3d routines that return a SFVec3d -- note, already defined above
4759#define __3FADD 1
4760#define __3FDIVIDE 2
4761#define __3FMULT 3
4762#define __3FSUBT 4
4763#define __3FDOT 5
4764#define __3FLENGTH 6
4765#define __3FNORMALIZE 8
4766#define __3FNEGATE 7
4767#define __3FCROSS 9
4768*/
4769
4770JSBool SFVec3dGeneric( JSContext *cx, JSObject *obj,
4771 uintN argc, jsval *argv, jsval *rval, int op) {
4772 JSObject *_paramObj, *_proto, *_retObj;
4773 //SFVec3dNative *_vec1, *_vec2, *_retNative;
4774 jsdouble d=0.0;
4775 jsdouble d0=0.0;
4776 jsdouble d1=0.0;
4777 jsdouble d2=0.0;
4778 double *cc, *cc2, cc3[3], cclhs[3];
4779 struct point_XYZ v1, v2, ret;
4780
4781
4782 /* parameters */
4783 int SFParam = FALSE;
4784 int numParam = FALSE;
4785
4786 /* return values */
4787 int retSFVec3d = FALSE;
4788 int retNumeric = FALSE;
4789
4790 /* is the "argv" parameter a string? */
4791 int param_isString;
4792 char *charString;
4793 jsdouble pars[3];
4794 JSString *_str;
4795
4796 /* determine what kind of parameter to get */
4797 if ((op==__3FADD)||(op==__3FDOT)||(op==__3FCROSS)||(op==__3FSUBT))SFParam=TRUE;
4798 if ((op==__3FDIVIDE)||(op==__3FMULT))numParam=TRUE;
4799
4800 /* determine the return value, if it is NOT a SFVec3d */
4801 if ((op==__3FDOT)||(op==__3FLENGTH)) retNumeric = TRUE;
4802 retSFVec3d = (!retNumeric);
4803
4804 /* is the parameter a string, possibly gotten from the VRML/X3d
4805 * side of things? */
4806 param_isString = JSVAL_IS_STRING (*argv);
4807
4808 /* get the parameter */
4809 if ((SFParam) || (numParam)) {
4810 if (numParam) {
4811 if (!JSVAL_IS_NUMBER(argv[0])) {
4812 printf ("SFVec3d param error - number expected\n");
4813 return JS_FALSE;
4814 }
4815 if (!JS_ValueToNumber(cx, argv[0], &d)) {
4816 printf("JS_ValueToNumber failed in SFVec3d.\n");
4817 return JS_FALSE;
4818 }
4819 } else {
4820 /* did this come in from VRML as a string, or did
4821 * it get created in javascript? */
4822 if (param_isString) {
4823 _str = JS_ValueToString(cx, *argv);
4824 charString = JS_EncodeString(cx,_str);
4825 if (sscanf(charString, "%lf %lf %lf",
4826 &(pars[0]), &(pars[1]), &(pars[2])) != 3) {
4827 printf ("conversion problem in SFVec3dGeneric\n");
4828 return JS_FALSE;
4829 }
4830 /* printf ("past scan, %f %f %f\n",pars[0], pars[1],pars[2]);*/
4831 cc3[0] = pars[0];
4832 cc3[1] = pars[1];
4833 cc3[2] = pars[2];
4834 cc2 = cc3;
4835 } else {
4836 if(SM_method() == 2){
4837 if (argv[0].isObject()) {
4838 AnyNative *_vec2;
4839 if ((_vec2 = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(argv[0]))) == NULL) {
4840 printf("in SFVec3d, RHS was NOT native type \n");
4841 return JS_FALSE;
4842 }
4843 cc2 = _vec2->v->sfvec3d.c;
4844 }else{
4845 return JS_FALSE;
4846 }
4847 }else{
4848 SFVec3dNative *_vec2;
4849 if (!JS_ConvertArguments(cx, argc, argv, "o", &_paramObj)) {
4850 printf( "JS_ConvertArguments failed in SFVec3d.\n");
4851 return JS_FALSE;
4852 }
4853
4854 CHECK_CLASS(cx,_paramObj,argv,__FUNCTION__,SFVec3dClass)
4855
4856 /* get the second object's data */
4857 if ((_vec2 = (SFVec3dNative*)JS_GetPrivateFw(cx, _paramObj)) == NULL) {
4858 printf( "JS_GetPrivate failed for _paramObj in SFVec3d.\n");
4859 return JS_FALSE;
4860 }
4861 cc2 = (_vec2->v).c;
4862 }
4863 }
4864 }
4865 }
4866
4867
4868 /* get our values */
4869 if(SM_method() == 2){
4870 AnyNative *_vec1;
4871 if ((_vec1 = (AnyNative*)JS_GetPrivateFw(cx, obj)) == NULL) {
4872 printf( "JS_GetPrivate failed for obj in SFVec3dAdd.\n");
4873 return JS_FALSE;
4874 }
4875 cc = _vec1->v->sfvec3d.c;
4876 }else{
4877 SFVec3dNative *_vec1;
4878 if ((_vec1 = (SFVec3dNative*)JS_GetPrivateFw(cx, obj)) == NULL) {
4879 printf( "JS_GetPrivate failed for obj in SFVec3dAdd.\n");
4880 return JS_FALSE;
4881 }
4882 cc = _vec1->v.c;
4883 }
4884 /* do the operation */
4885 #ifdef JSVRMLCLASSESVERBOSE
4886 printf ("SFVec3d generic, vec2 %f %f %f\n",pars[0],pars[1],pars[2]);
4887 #endif
4888
4889 switch (op) {
4890 /* returning a SFVec3d */
4891 case __3FADD:
4892 vecaddd(cclhs,cc,cc2);
4893 break;
4894 case __3FDIVIDE:
4895 vecscaled(cclhs,cc,1.0/d);
4896 break;
4897 case __3FMULT:
4898 vecscaled(cclhs,cc,d);
4899 break;
4900 case __3FSUBT:
4901 vecdifd(cclhs,cc,cc2);
4902 break;
4903 case __3FDOT:
4904 d = vecdotd(cc,cc2);
4905 break;
4906 case __3FCROSS:
4907 veccrossd(cclhs,cc,cc2);
4908 break;
4909 case __3FLENGTH:
4910 d = veclengthd(cc);
4911 break;
4912 case __3FNORMALIZE:
4913 vecnormald(cclhs,cc);
4914 break;
4915 case __3FNEGATE:
4916 vecnegated(cclhs,cc);
4917 break;
4918 default:
4919 printf ("woops... %d\n",op);
4920 return JS_FALSE;
4921 }
4922
4923 #ifdef JSVRMLCLASSESVERBOSE
4924 printf ("past calcs\n");
4925 #endif
4926
4927 /* set the return object */
4928 if (retSFVec3d) {
4929 #ifdef JSVRMLCLASSESVERBOSE
4930 printf ("returning SFVec3d\n");
4931 #endif
4932 if ((_proto = JS_GetPrototypeFw(cx, obj)) == NULL) {
4933 printf( "JS_GetPrototype failed in SFVec3d.\n");
4934 return JS_FALSE;
4935 }
4936 if ((_retObj =
4937 JS_ConstructObjectFw(cx, &SFVec3dClass, _proto, NULL)) == NULL) {
4938 printf( "JS_ConstructObject failed in SFVec3d.\n");
4939 return JS_FALSE;
4940 }
4941 *rval = OBJECT_TO_JSVAL(_retObj);
4942 if(SM_method() == 2){
4943 AnyNative *any;
4944 if ((any = (AnyNative*)JS_GetPrivateFw(cx, _retObj)) == NULL) {
4945 printf( "JS_GetPrivate failed for _retObj in SFVec3d.\n");
4946 return JS_FALSE;
4947 }
4948 memcpy(any->v->sfvec3d.c,cclhs,sizeof(double)*3);
4949 }else{
4950 SFVec3dNative *_retNative;
4951 if ((_retNative = (SFVec3dNative*)JS_GetPrivateFw(cx, _retObj)) == NULL) {
4952 printf( "JS_GetPrivate failed for _retObj in SFVec3d.\n");
4953 return JS_FALSE;
4954 }
4955 memcpy(_retNative->v.c,cclhs,3*sizeof(double));
4956 }
4957 } else if (retNumeric) {
4958 if (JS_NewNumberValue(cx,d,rval) == JS_FALSE) {
4959 printf( "JS_NewDouble failed for %f in SFVec3d.\n",d);
4960 return JS_FALSE;
4961 }
4962 }
4963 #ifdef JSVRMLCLASSESVERBOSE
4964 if (retSFVec3d){
4965 printf("SFVec3dgeneric: obj = %p, result = [%.9g, %.9g, %.9g]\n",
4966 obj,
4967 (_retNative->v).c[0], (_retNative->v).c[1],
4968 (_retNative->v).c[2]);
4969 }
4970 if (retNumeric){
4971 printf("SFVec2fgeneric: obj = %p, result = %.9g\n",
4972 obj, d);
4973 }
4974 #endif
4975
4976return JS_TRUE;
4977}
4978
4979JSBool
4980SFVec3dAdd(JSContext *cx, uintN argc, jsval *vp) {
4981 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4982 jsval *argv = JS_ARGV(cx,vp);
4983 jsval rval;
4984 JSBool retval = SFVec3dGeneric(cx, obj, argc, argv, &rval, __3FADD);
4985 JS_SET_RVAL(cx,vp,rval);
4986 return retval;
4987}
4988
4989JSBool
4990SFVec3dCross(JSContext *cx, uintN argc, jsval *vp) {
4991 JSObject *obj = JS_THIS_OBJECT(cx,vp);
4992 jsval *argv = JS_ARGV(cx,vp);
4993 jsval rval;
4994 JSBool retval = SFVec3dGeneric(cx, obj, argc, argv, &rval, __3FCROSS);
4995 JS_SET_RVAL(cx,vp,rval);
4996 return retval;
4997}
4998
4999JSBool
5000SFVec3dDivide(JSContext *cx, uintN argc, jsval *vp) {
5001 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5002 jsval *argv = JS_ARGV(cx,vp);
5003 jsval rval;
5004 JSBool retval = SFVec3dGeneric(cx, obj, argc, argv, &rval, __3FDIVIDE);
5005 JS_SET_RVAL(cx,vp,rval);
5006 return retval;
5007}
5008
5009JSBool
5010SFVec3dDot(JSContext *cx, uintN argc, jsval *vp) {
5011 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5012 jsval *argv = JS_ARGV(cx,vp);
5013 jsval rval;
5014 JSBool retval = SFVec3dGeneric(cx, obj, argc, argv, &rval, __3FDOT);
5015 JS_SET_RVAL(cx,vp,rval);
5016 return retval;
5017}
5018
5019JSBool
5020SFVec3dLength(JSContext *cx, uintN argc, jsval *vp) {
5021 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5022 jsval *argv = JS_ARGV(cx,vp);
5023 jsval rval;
5024 JSBool retval = SFVec3dGeneric(cx, obj, argc, argv, &rval, __3FLENGTH);
5025 JS_SET_RVAL(cx,vp,rval);
5026 return retval;
5027}
5028
5029
5030JSBool
5031SFVec3dMultiply(JSContext *cx, uintN argc, jsval *vp) {
5032 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5033 jsval *argv = JS_ARGV(cx,vp);
5034 jsval rval;
5035 JSBool retval = SFVec3dGeneric(cx, obj, argc, argv, &rval, __3FMULT);
5036 JS_SET_RVAL(cx,vp,rval);
5037 return retval;
5038}
5039
5040
5041JSBool
5042SFVec3dNegate(JSContext *cx, uintN argc, jsval *vp) {
5043 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5044 jsval *argv = JS_ARGV(cx,vp);
5045 jsval rval;
5046 JSBool retval = SFVec3dGeneric(cx, obj, argc, argv, &rval, __3FNEGATE);
5047 JS_SET_RVAL(cx,vp,rval);
5048 return retval;
5049}
5050
5051JSBool
5052SFVec3dNormalize(JSContext *cx, uintN argc, jsval *vp) {
5053 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5054 jsval *argv = JS_ARGV(cx,vp);
5055 jsval rval;
5056 JSBool retval = SFVec3dGeneric(cx, obj, argc, argv, &rval, __3FNORMALIZE);
5057 JS_SET_RVAL(cx,vp,rval);
5058 return retval;
5059}
5060
5061JSBool
5062SFVec3dSubtract(JSContext *cx, uintN argc, jsval *vp) {
5063 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5064 jsval *argv = JS_ARGV(cx,vp);
5065 jsval rval;
5066 JSBool retval = SFVec3dGeneric(cx, obj, argc, argv, &rval, __3FSUBT);
5067 JS_SET_RVAL(cx,vp,rval);
5068 return retval;
5069}
5070
5071JSBool
5072SFVec3dToString(JSContext *cx, uintN argc, jsval *vp) {
5073 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5074 jsval *argv = JS_ARGV(cx,vp);
5075 JSString *_str;
5076 char buff[STRING];
5077 double *cc;
5078
5079 UNUSED(argc);
5080 UNUSED(argv);
5081 if(SM_method()==2){
5082 AnyNative *ptr;
5083 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5084 printf( "JS_GetPrivate failed in SFVec3dToString.\n");
5085 return JS_FALSE;
5086 }
5087 cc = ptr->v->sfvec3d.c;
5088 }else{
5089 SFVec3dNative *ptr;
5090 if ((ptr = (SFVec3dNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5091 printf( "JS_GetPrivate failed in SFVec3dToString.\n");
5092 return JS_FALSE;
5093 }
5094 cc = ptr->v.c;
5095 }
5096 memset(buff, 0, STRING);
5097 sprintf(buff, "%.9g %.9g %.9g",
5098 cc[0], cc[1], cc[2]);
5099 _str = JS_NewStringCopyZ(cx, buff);
5100 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
5101
5102 #ifdef JSVRMLCLASSESVERBOSE
5103 printf ("SFVec3dToString, string is :%s:\n",buff);
5104 #endif
5105
5106 return JS_TRUE;
5107}
5108
5109JSBool
5110SFVec3dAssign(JSContext *cx, uintN argc, jsval *vp) {
5111 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5112 jsval *argv = JS_ARGV(cx,vp);
5113 JSString *_id_jsstr;
5114 JSObject *_from_obj;
5115
5116 char *_id_str;
5117
5118 UNUSED(_id_str); // compiler warning mitigation
5119
5120
5121 #ifdef JSVRMLCLASSESVERBOSE
5122 printf ("start of SFVec3dAssign\n");
5123 #endif
5124 if(SM_method() == 2){
5125 AnyNative *lhs, *rhs;
5126 if ((lhs = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5127 printf( "JS_GetPrivate failed for obj in SFVec3dAssign.\n");
5128 return JS_FALSE;
5129 }
5130 if (!(*vp).isObject())
5131 return JS_FALSE;
5132 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*vp))) == NULL) {
5133 printf("in setECMANative, RHS was NOT native type \n");
5134 return JS_FALSE;
5135 }
5136 AnyNativeAssign(lhs,rhs);
5137
5138 }else{
5139 SFVec3dNative *fptr, *ptr;
5140
5141 if ((ptr = (SFVec3dNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5142 printf( "JS_GetPrivate failed for obj in SFVec3dAssign.\n");
5143 return JS_FALSE;
5144 }
5145
5146 CHECK_CLASS(cx,obj,argv,__FUNCTION__,SFVec3dClass)
5147
5148 if (JS_ConvertArguments(cx, argc, argv, "oS", &_from_obj, &_id_jsstr) == JS_TRUE) {
5149 _id_str = JS_EncodeString(cx,_id_jsstr);
5150 } else {
5151 printf( "JS_ConvertArguments failed in SFVec3dAssign.\n");
5152 return JS_FALSE;
5153 }
5154
5155 CHECK_CLASS(cx,_from_obj,argv,__FUNCTION__,SFVec3dClass)
5156
5157 if ((fptr = (SFVec3dNative *)JS_GetPrivateFw(cx, _from_obj)) == NULL) {
5158 printf( "JS_GetPrivate failed for _from_obj in SFVec3dAssign.\n");
5159 return JS_FALSE;
5160 }
5161 #ifdef JSVRMLCLASSESVERBOSE
5162 printf("SFVec3dAssign: obj = %p, id = \"%s\", from = %p\n",
5163 obj, _id_str, _from_obj);
5164 #endif
5165
5166 SFVec3dNativeAssign(ptr, fptr);
5167 }
5168
5169 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
5170
5171 #ifdef JSVRMLCLASSESVERBOSE
5172 printf ("end of SFVec3dAssign\n");
5173 #endif
5174
5175 return JS_TRUE;
5176}
5177
5178JSBool
5179SFVec3dConstr(JSContext *cx, uintN argc, jsval *vp) {
5180 JSObject *obj = JS_NewObject(cx,&SFVec3dClass,NULL,NULL);
5181 jsval *argv = JS_ARGV(cx,vp);
5182 jsdouble pars[3];
5183 double *cc;
5184
5185 #ifdef JSVRMLCLASSESVERBOSE
5186 printf ("start of SFVec3dConstr\n");
5187 #endif
5188
5189 ADD_ROOT(cx,obj)
5190
5191 if(SM_method() == 2){
5192 AnyNative *any;
5193 if ((any = (AnyNative *) AnyNativeNew(FIELDTYPE_SFVec3d,NULL,NULL)) == NULL) {
5194 printf( "SFVec3dNativeNew failed in SFVec3dConstr.\n");
5195 return JS_FALSE;
5196 }
5197
5198 if (!JS_SetPrivateFw(cx, obj, any)) {
5199 printf( "JS_SetPrivate failed in SFVec3dConstr.\n");
5200 return JS_FALSE;
5201 }
5202 cc = any->v->sfvec3d.c;
5203 }else{
5204 SFVec3dNative *ptr;
5205 if ((ptr = (SFVec3dNative *) SFVec3dNativeNew()) == NULL) {
5206 printf( "SFVec3dNativeNew failed in SFVec3dConstr.\n");
5207 return JS_FALSE;
5208 }
5209
5210 //if (!JS_DefineProperties(cx, obj, SFVec3dProperties)) {
5211 // printf( "JS_DefineProperties failed in SFVec3dConstr.\n");
5212 // return JS_FALSE;
5213 //}
5214 if (!JS_SetPrivateFw(cx, obj, ptr)) {
5215 printf( "JS_SetPrivate failed in SFVec3dConstr.\n");
5216 return JS_FALSE;
5217 }
5218 cc = ptr->v.c;
5219 ptr->valueChanged = 1;
5220 }
5221 if (argc == 0) {
5222 cc[0] = (float) 0.0;
5223 cc[1] = (float) 0.0;
5224 cc[2] = (float) 0.0;
5225 } else {
5226 if (!JS_ConvertArguments(cx, argc, argv, "d d d",
5227 &(pars[0]), &(pars[1]), &(pars[2]))) {
5228 printf( "JS_ConvertArguments failed in SFVec3dConstr.\n");
5229 return JS_FALSE;
5230 }
5231 cc[0] = (float) pars[0];
5232 cc[1] = (float) pars[1];
5233 cc[2] = (float) pars[2];
5234 }
5235 #ifdef JSVRMLCLASSESVERBOSE
5236 printf("SFVec3dConstr: obj = %p, %u args, %f %f %f\n",
5237 obj, argc,
5238 (ptr->v).c[0], (ptr->v).c[1], (ptr->v).c[2]);
5239 #endif
5240
5241
5242 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
5243 return JS_TRUE;
5244}
5245
5246JSBool
5247SFVec3dGetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JS::MutableHandle<JS::Value> hvp){
5248 JSObject *obj = *hobj.address();
5249 jsid iid = *hiid.address();
5250 jsval *vp = hvp.address();
5251 double *cc;
5252 jsdouble d;
5253 #ifdef JSVRMLCLASSESVERBOSE
5254 JSString *_idStr;
5255 char *_id_c;
5256 #endif
5257
5258 jsval id;
5259 if (!JS_IdToValue(cx,iid,&id)) {
5260 printf("JS_IdToValue failed in SFVec3dGetProperty.\n");
5261 return JS_FALSE;
5262 }
5263
5264
5265 #ifdef JSVRMLCLASSESVERBOSE
5266
5267
5268/* same as earlier, these are never used
5269 _idStr = JS_ValueToString(cx, id);
5270 _id_c = JS_GetStringBytes(_idStr); */
5271 _idStr = JS_ValueToString(cx, *vp);
5272#if JS_VERSION < 185
5273 _id_c = JS_GetStringBytes(_idStr);
5274#else
5275 _id_c = JS_EncodeString(cx,_idStr);
5276#endif
5277 #endif
5278
5279 if(SM_method()==2){
5280 AnyNative *ptr;
5281 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
5282 printf( "JS_GetPrivate failed in SFVec3dGetProperty.\n");
5283 return JS_FALSE;
5284 }
5285 cc = ptr->v->sfvec3d.c;
5286 }else{
5287 SFVec3dNative *ptr;
5288 if ((ptr = (SFVec3dNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
5289 printf( "JS_GetPrivate failed in SFVec3dGetProperty.\n");
5290 return JS_FALSE;
5291 }
5292 cc = ptr->v.c;
5293 }
5294 if (JSVAL_IS_INT(id)) {
5295 switch (JSVAL_TO_INT(id)) {
5296 case 0:
5297 d = cc[0];
5298 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
5299 printf(
5300 "JS_NewDouble failed for %f in SFVec3dGetProperty.\n",
5301 d);
5302 return JS_FALSE;
5303 }
5304 break;
5305 case 1:
5306 d = cc[1];
5307 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
5308 printf(
5309 "JS_NewDouble failed for %f in SFVec3dGetProperty.\n",
5310 d);
5311 return JS_FALSE;
5312 }
5313 break;
5314 case 2:
5315 d = cc[2];
5316 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
5317 printf(
5318 "JS_NewDouble failed for %f in SFVec3dGetProperty.\n",
5319 d);
5320 return JS_FALSE;
5321 }
5322 break;
5323 }
5324 } else {
5325 #ifdef JSVRMLCLASSESVERBOSE
5326 printf ("SFVec3dGetProperty, id is NOT an int...\n");
5327 #endif
5328 }
5329
5330 return JS_TRUE;
5331}
5332
5333JSBool
5334SFVec3dSetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
5335 JSObject *obj = *hobj.address();
5336 jsid iid = *hiid.address();
5337 jsval *vp = hvp.address();
5338
5339 double *cc;
5340 jsval myv;
5341
5342 jsval id;
5343 if (!JS_IdToValue(cx,iid,&id)) {
5344 printf("JS_IdToValue failed in SFVec3dSetProperty.\n");
5345 return JS_FALSE;
5346 }
5347
5348
5349 if(SM_method()==2){
5350 AnyNative *ptr;
5351 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5352 printf( "JS_GetPrivate failed in SFVec3dSetProperty.\n");
5353 return JS_FALSE;
5354 }
5355 if(ptr->valueChanged)
5356 (*ptr->valueChanged)++;
5357 #ifdef JSVRMLCLASSESVERBOSE
5358 printf("SFVec3dSetProperty: obj = %p, id = %d, valueChanged = %d\n",
5359 obj, JSVAL_TO_INT(id), ptr->valueChanged);
5360 #endif
5361 cc = ptr->v->sfvec3d.c;
5362 }else{
5363 SFVec3dNative *ptr;
5364 if ((ptr = (SFVec3dNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5365 printf( "JS_GetPrivate failed in SFVec3dSetProperty.\n");
5366 return JS_FALSE;
5367 }
5368 ptr->valueChanged++;
5369 #ifdef JSVRMLCLASSESVERBOSE
5370 printf("SFVec3dSetProperty: obj = %p, id = %d, valueChanged = %d\n",
5371 obj, JSVAL_TO_INT(id), ptr->valueChanged);
5372 #endif
5373 cc = ptr->v.c;
5374 }
5375 if (!JS_ConvertValue(cx, *vp, JSTYPE_NUMBER, &myv)) {
5376 printf( "JS_ConvertValue failed in SFVec3dSetProperty.\n");
5377 return JS_FALSE;
5378 }
5379
5380 if (JSVAL_IS_INT(id)) {
5381 switch (JSVAL_TO_INT(id)) {
5382 case 0:
5383 cc[0] = JSVAL_TO_DOUBLE(myv);
5384 break;
5385 case 1:
5386 cc[1] = JSVAL_TO_DOUBLE(myv);
5387 break;
5388 case 2:
5389 cc[2] = JSVAL_TO_DOUBLE(myv);
5390 break;
5391 }
5392 }
5393 return JS_TRUE;
5394}
5395
5396
5397JSBool
5398SFVec4fToString(JSContext *cx, uintN argc, jsval *vp) {
5399 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5400 jsval *argv = JS_ARGV(cx,vp);
5401 JSString *_str;
5402 char buff[STRING];
5403 float *cc;
5404
5405 UNUSED(argc);
5406 UNUSED(argv);
5407 if(SM_method()==2){
5408 AnyNative *ptr;
5409 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5410 printf( "JS_GetPrivate failed in SFVec4fToString.\n");
5411 return JS_FALSE;
5412 }
5413 cc = ptr->v->sfvec4f.c;
5414
5415 }else{
5416 SFVec4fNative *ptr;
5417 if ((ptr = (SFVec4fNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5418 printf( "JS_GetPrivate failed in SFVec4fToString.\n");
5419 return JS_FALSE;
5420 }
5421 cc = ptr->v.c;
5422 }
5423 memset(buff, 0, STRING);
5424 sprintf(buff, "%.9g %.9g %.9g %.9g",
5425 cc[0], cc[1], cc[2],cc[3]);
5426 _str = JS_NewStringCopyZ(cx, buff);
5427 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
5428
5429 #ifdef JSVRMLCLASSESVERBOSE
5430 printf ("SFVec4fToString, string is :%s:\n",buff);
5431 #endif
5432
5433 return JS_TRUE;
5434}
5435
5436JSBool
5437SFVec4fAssign(JSContext *cx, uintN argc, jsval *vp) {
5438 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5439 jsval *argv = JS_ARGV(cx,vp);
5440 JSString *_id_jsstr;
5441 JSObject *_from_obj;
5442 char *_id_str;
5443
5444 UNUSED(_id_str); // compiler warning mitigation
5445
5446
5447 #ifdef JSVRMLCLASSESVERBOSE
5448 printf ("start of SFVec4fAssign\n");
5449 #endif
5450 if(SM_method() == 2){
5451 AnyNative *lhs, *rhs;
5452 if ((lhs = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5453 printf( "JS_GetPrivate failed for obj in SFVec4fAssign.\n");
5454 return JS_FALSE;
5455 }
5456 if (!(*vp).isObject())
5457 return JS_FALSE;
5458 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*vp))) == NULL) {
5459 printf("JS_ConvertArguments failed in SFVec4fAssign. \n");
5460 return JS_FALSE;
5461 }
5462 AnyNativeAssign(lhs,rhs);
5463 }else{
5464 SFVec4fNative *fptr, *ptr;
5465
5466 if ((ptr = (SFVec4fNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5467 printf( "JS_GetPrivate failed for obj in SFVec4fAssign.\n");
5468 return JS_FALSE;
5469 }
5470
5471 CHECK_CLASS(cx,obj,argv,__FUNCTION__,SFVec4fClass)
5472
5473 if (JS_ConvertArguments(cx, argc, argv, "oS", &_from_obj, &_id_jsstr) == JS_TRUE) {
5474 _id_str = JS_EncodeString(cx,_id_jsstr);
5475 } else {
5476 printf( "JS_ConvertArguments failed in SFVec4fAssign.\n");
5477 return JS_FALSE;
5478 }
5479
5480 CHECK_CLASS(cx,_from_obj,argv,__FUNCTION__,SFVec4fClass)
5481
5482 if ((fptr = (SFVec4fNative *)JS_GetPrivateFw(cx, _from_obj)) == NULL) {
5483 printf( "JS_GetPrivate failed for _from_obj in SFVec4fAssign.\n");
5484 return JS_FALSE;
5485 }
5486 #ifdef JSVRMLCLASSESVERBOSE
5487 printf("SFVec4fAssign: obj = %p, id = \"%s\", from = %p\n",
5488 obj, _id_str, _from_obj);
5489 #endif
5490
5491 SFVec4fNativeAssign(ptr, fptr);
5492 }
5493 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
5494
5495 #ifdef JSVRMLCLASSESVERBOSE
5496 printf ("end of SFVec4fAssign\n");
5497 #endif
5498
5499 return JS_TRUE;
5500}
5501
5502JSBool
5503SFVec4fConstr(JSContext *cx, uintN argc, jsval *vp) {
5504 JSObject *obj = JS_NewObject(cx,&SFVec4fClass,NULL,NULL);
5505 jsval *argv = JS_ARGV(cx,vp);
5506 jsdouble pars[4];
5507 float *cc;
5508
5509 #ifdef JSVRMLCLASSESVERBOSE
5510 printf ("start of SFVec4fConstr\n");
5511 #endif
5512
5513 ADD_ROOT(cx,obj)
5514 if(SM_method() == 2){
5515 AnyNative *any;
5516 if((any = (AnyNative*)AnyNativeNew(FIELDTYPE_SFVec4f,NULL,NULL)) == NULL){
5517 printf( "AnyfNativeNew failed in SFVec4fConstr.\n");
5518 return JS_FALSE;
5519 }
5520 if (!JS_SetPrivateFw(cx, obj, any)) {
5521 printf( "JS_SetPrivate failed in SFVec4fConstr.\n");
5522 return JS_FALSE;
5523 }
5524 cc = any->v->sfvec4f.c;
5525 }else{
5526 SFVec4fNative *ptr;
5527 if ((ptr = (SFVec4fNative *) SFVec4fNativeNew()) == NULL) {
5528 printf( "SFVec4fNativeNew failed in SFVec4fConstr.\n");
5529 return JS_FALSE;
5530 }
5531 ptr->valueChanged = 1;
5532 cc = ptr->v.c;
5533 //if (!JS_DefineProperties(cx, obj, SFVec4fProperties)) {
5534 // printf( "JS_DefineProperties failed in SFVec4fConstr.\n");
5535 // return JS_FALSE;
5536 //}
5537 if (!JS_SetPrivateFw(cx, obj, ptr)) {
5538 printf( "JS_SetPrivate failed in SFVec4fConstr.\n");
5539 return JS_FALSE;
5540 }
5541 }
5542
5543 if (argc == 0) {
5544 cc[0] = (float) 0.0;
5545 cc[1] = (float) 0.0;
5546 cc[2] = (float) 0.0;
5547 cc[3] = (float) 0.0;
5548 } else {
5549 if (!JS_ConvertArguments(cx, argc, argv, "d d d d",
5550 &(pars[0]), &(pars[1]), &(pars[2]), &(pars[3]))) {
5551 printf( "JS_ConvertArguments failed in SFVec4fConstr.\n");
5552 return JS_FALSE;
5553 }
5554 cc[0] = (float) pars[0];
5555 cc[1] = (float) pars[1];
5556 cc[2] = (float) pars[2];
5557 cc[3] = (float) pars[3];
5558 }
5559 #ifdef JSVRMLCLASSESVERBOSE
5560 printf("SFVec4fConstr: obj = %p, %u args, %f %f %f %f\n",
5561 obj, argc,
5562 cc[0], cc[1], cc[2], cc[3]);
5563 #endif
5564
5565 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
5566 return JS_TRUE;
5567}
5568
5569JSBool
5570SFVec4fGetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JS::MutableHandle<JS::Value> hvp){
5571 JSObject *obj = *hobj.address();
5572 jsid iid = *hiid.address();
5573 jsval *vp = hvp.address();
5574
5575 jsdouble d;
5576 float *cc;
5577 #ifdef JSVRMLCLASSESVERBOSE
5578 JSString *_idStr;
5579 char *_id_c;
5580 #endif
5581
5582 jsval id;
5583 if (!JS_IdToValue(cx,iid,&id)) {
5584 printf("JS_IdToValue failed in SFVec4fGetProperty.\n");
5585 return JS_FALSE;
5586 }
5587
5588
5589 #ifdef JSVRMLCLASSESVERBOSE
5590
5591 //JSString *_idStr;
5592 //char *_id_c;
5593
5594/* same as above
5595 _idStr = JS_ValueToString(cx, id);
5596 _id_c = JS_GetStringBytes(_idStr); */
5597 _idStr = JS_ValueToString(cx, *vp);
5598#if JS_VERSION < 185
5599 _id_c = JS_GetStringBytes(_idStr);
5600#else
5601 _id_c = JS_EncodeString(cx,_idStr);
5602#endif
5603 #endif
5604
5605 if(SM_method()==2){
5606 AnyNative *any;
5607 if ((any = (AnyNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
5608 printf( "JS_GetPrivate failed in SFVec4fGetProperty.\n");
5609 return JS_FALSE;
5610 }
5611 cc = any->v->sfvec4f.c;
5612 }else{
5613 SFVec4fNative *ptr;
5614 if ((ptr = (SFVec4fNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
5615 printf( "JS_GetPrivate failed in SFVec4fGetProperty.\n");
5616 return JS_FALSE;
5617 }
5618 cc = ptr->v.c;
5619 }
5620 if (JSVAL_IS_INT(id)) {
5621 switch (JSVAL_TO_INT(id)) {
5622 case 0:
5623 d = cc[0];
5624 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
5625 printf(
5626 "JS_NewDouble failed for %f in SFVec4fGetProperty.\n",
5627 d);
5628 return JS_FALSE;
5629 }
5630 break;
5631 case 1:
5632 d = cc[1];
5633 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
5634 printf(
5635 "JS_NewDouble failed for %f in SFVec4fGetProperty.\n",
5636 d);
5637 return JS_FALSE;
5638 }
5639 break;
5640 case 2:
5641 d = cc[2];
5642 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
5643 printf(
5644 "JS_NewDouble failed for %f in SFVec4fGetProperty.\n",
5645 d);
5646 return JS_FALSE;
5647 }
5648 break;
5649 case 3:
5650 d = cc[3];
5651 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
5652 printf(
5653 "JS_NewDouble failed for %f in SFVec4fGetProperty.\n",
5654 d);
5655 return JS_FALSE;
5656 }
5657 break;
5658 }
5659 } else {
5660 #ifdef JSVRMLCLASSESVERBOSE
5661 printf ("SFVec4fGetProperty, id is NOT an int...\n");
5662 #endif
5663 }
5664
5665 return JS_TRUE;
5666}
5667
5668JSBool
5669SFVec4fSetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
5670 JSObject *obj = *hobj.address();
5671 jsid iid = *hiid.address();
5672 jsval *vp = hvp.address();
5673
5674 jsval myv;
5675 float *cc;
5676
5677
5678 jsval id;
5679 if (!JS_IdToValue(cx,iid,&id)) {
5680 printf("JS_IdToValue failed in SFVec4fSetProperty.\n");
5681 return JS_FALSE;
5682 }
5683
5684 if(SM_method() == 2){
5685 AnyNative *any;
5686 if ((any = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5687 printf( "JS_GetPrivate failed in SFVec4fSetProperty.\n");
5688 return JS_FALSE;
5689 }
5690 if(any->valueChanged)
5691 (*any->valueChanged)++;
5692 cc = any->v->sfvec4f.c;
5693 }else{
5694 SFVec4fNative *ptr;
5695
5696 if ((ptr = (SFVec4fNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5697 printf( "JS_GetPrivate failed in SFVec4fSetProperty.\n");
5698 return JS_FALSE;
5699 }
5700 ptr->valueChanged++;
5701 #ifdef JSVRMLCLASSESVERBOSE
5702 printf("SFVec4fSetProperty: obj = %p, id = %d, valueChanged = %d\n",
5703 obj, JSVAL_TO_INT(id), ptr->valueChanged);
5704 #endif
5705 cc = ptr->v.c;
5706 }
5707 if (!JS_ConvertValue(cx, *vp, JSTYPE_NUMBER, &myv)) {
5708 printf( "JS_ConvertValue failed in SFVec4fSetProperty.\n");
5709 return JS_FALSE;
5710 }
5711
5712 if (JSVAL_IS_INT(id)) {
5713 switch (JSVAL_TO_INT(id)) {
5714 case 0:
5715 cc[0] = (float) JSVAL_TO_DOUBLE(myv);
5716 break;
5717 case 1:
5718 cc[1] = (float) JSVAL_TO_DOUBLE(myv);
5719 break;
5720 case 2:
5721 cc[2] = (float) JSVAL_TO_DOUBLE(myv);
5722 break;
5723 case 3:
5724 cc[3] = (float) JSVAL_TO_DOUBLE(myv);
5725 break;
5726 }
5727 }
5728 return JS_TRUE;
5729}
5730
5731
5732
5733JSBool
5734SFVec4dToString(JSContext *cx, uintN argc, jsval *vp) {
5735 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5736 jsval *argv = JS_ARGV(cx,vp);
5737
5738 JSString *_str;
5739 double *cc;
5740 char buff[STRING];
5741
5742 UNUSED(argc);
5743 UNUSED(argv);
5744 if(SM_method()==2){
5745 AnyNative *ptr;
5746 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5747 printf( "JS_GetPrivate failed in SFVec4dToString.\n");
5748 return JS_FALSE;
5749 }
5750 cc = ptr->v->sfvec4d.c;
5751 }else{
5752 SFVec4dNative *ptr;
5753 if ((ptr = (SFVec4dNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5754 printf( "JS_GetPrivate failed in SFVec4dToString.\n");
5755 return JS_FALSE;
5756 }
5757 cc = ptr->v.c;
5758 }
5759 memset(buff, 0, STRING);
5760 sprintf(buff, "%.9g %.9g %.9g %.9g",
5761 cc[0], cc[1], cc[2],cc[3]);
5762 _str = JS_NewStringCopyZ(cx, buff);
5763
5764 JS_SET_RVAL(cx,vp,STRING_TO_JSVAL(_str));
5765
5766 #ifdef JSVRMLCLASSESVERBOSE
5767 printf ("SFVec4dToString, string is :%s:\n",buff);
5768 #endif
5769
5770 return JS_TRUE;
5771}
5772
5773JSBool
5774SFVec4dAssign(JSContext *cx, uintN argc, jsval *vp) {
5775 JSObject *obj = JS_THIS_OBJECT(cx,vp);
5776 jsval *argv = JS_ARGV(cx,vp);
5777 JSString *_id_jsstr;
5778 JSObject *_from_obj;
5779 char *_id_str;
5780
5781 UNUSED(_id_str); // compiler warning mitigation
5782
5783 #ifdef JSVRMLCLASSESVERBOSE
5784 printf ("start of SFVec4dAssign\n");
5785 #endif
5786
5787 if(SM_method() == 2){
5788 AnyNative *lhs, *rhs;
5789 if ((lhs = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5790 printf( "JS_GetPrivate failed for obj in SFVec4dAssign.\n");
5791 return JS_FALSE;
5792 }
5793 if (!(*vp).isObject())
5794 return JS_FALSE;
5795 if ((rhs = (AnyNative *)JS_GetPrivateFw(cx, JSVAL_TO_OBJECT(*vp))) == NULL) {
5796 printf("in SFVec4dAssign, RHS was NOT native type \n");
5797 return JS_FALSE;
5798 }
5799 AnyNativeAssign(lhs,rhs);
5800
5801 }else{
5802 SFVec4dNative *fptr, *ptr;
5803
5804 if ((ptr = (SFVec4dNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
5805 printf( "JS_GetPrivate failed for obj in SFVec4dAssign.\n");
5806 return JS_FALSE;
5807 }
5808
5809 CHECK_CLASS(cx,obj,argv,__FUNCTION__,SFVec4dClass)
5810
5811 if (JS_ConvertArguments(cx, argc, argv, "oS", &_from_obj, &_id_jsstr) == JS_TRUE) {
5812 _id_str = JS_EncodeString(cx,_id_jsstr);
5813 } else {
5814 printf( "JS_ConvertArguments failed in SFVec4dAssign.\n");
5815 return JS_FALSE;
5816 }
5817
5818 CHECK_CLASS(cx,_from_obj,argv,__FUNCTION__,SFVec4dClass)
5819
5820 if ((fptr = (SFVec4dNative *)JS_GetPrivateFw(cx, _from_obj)) == NULL) {
5821 printf( "JS_GetPrivate failed for _from_obj in SFVec4dAssign.\n");
5822 return JS_FALSE;
5823 }
5824 #ifdef JSVRMLCLASSESVERBOSE
5825 printf("SFVec4dAssign: obj = %p, id = \"%s\", from = %p\n",
5826 obj, _id_str, _from_obj);
5827 #endif
5828
5829 SFVec4dNativeAssign(ptr, fptr);
5830 }
5831 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
5832
5833 #ifdef JSVRMLCLASSESVERBOSE
5834 printf ("end of SFVec4dAssign\n");
5835 #endif
5836
5837 return JS_TRUE;
5838}
5839
5840JSBool
5841SFVec4dConstr(JSContext *cx, uintN argc, jsval *vp) {
5842 JSObject *obj = JS_NewObject(cx,&SFVec4dClass,NULL,NULL);
5843 jsval *argv = JS_ARGV(cx,vp);
5844 jsdouble pars[4];
5845 double *cc;
5846
5847 #ifdef JSVRMLCLASSESVERBOSE
5848 printf ("start of SFVec4dConstr\n");
5849 #endif
5850
5851 ADD_ROOT(cx,obj)
5852 if(SM_method() == 2){
5853 AnyNative *any;
5854 if ((any = (AnyNative *) AnyNativeNew(FIELDTYPE_SFVec4d,NULL,NULL)) == NULL) {
5855 printf( "SFVec3dNativeNew failed in SFVec3dConstr.\n");
5856 return JS_FALSE;
5857 }
5858
5859 if (!JS_SetPrivateFw(cx, obj, any)) {
5860 printf( "JS_SetPrivate failed in SFVec3dConstr.\n");
5861 return JS_FALSE;
5862 }
5863 cc = any->v->sfvec4d.c;
5864 }else{
5865 SFVec4dNative *ptr;
5866
5867 if ((ptr = (SFVec4dNative *) SFVec4dNativeNew()) == NULL) {
5868 printf( "SFVec4dNativeNew failed in SFVec4dConstr.\n");
5869 return JS_FALSE;
5870 }
5871
5872 //if (!JS_DefineProperties(cx, obj, SFVec4dProperties)) {
5873 // printf( "JS_DefineProperties failed in SFVec4dConstr.\n");
5874 // return JS_FALSE;
5875 //}
5876 if (!JS_SetPrivateFw(cx, obj, ptr)) {
5877 printf( "JS_SetPrivate failed in SFVec4dConstr.\n");
5878 return JS_FALSE;
5879 }
5880 ptr->valueChanged = 1;
5881 cc = ptr->v.c;
5882 }
5883 if (argc == 0) {
5884 cc[0] = (float) 0.0;
5885 cc[1] = (float) 0.0;
5886 cc[2] = (float) 0.0;
5887 cc[3] = (float) 0.0;
5888 } else {
5889 if (!JS_ConvertArguments(cx, argc, argv, "d d d d",
5890 &(pars[0]), &(pars[1]), &(pars[2]), &(pars[3]))) {
5891 printf( "JS_ConvertArguments failed in SFVec4dConstr.\n");
5892 return JS_FALSE;
5893 }
5894 cc[0] = (float) pars[0];
5895 cc[1] = (float) pars[1];
5896 cc[2] = (float) pars[2];
5897 cc[3] = (float) pars[3];
5898 }
5899 #ifdef JSVRMLCLASSESVERBOSE
5900 printf("SFVec4dConstr: obj = %p, %u args, %f %f %f %f\n",
5901 obj, argc,
5902 cc[0], cc[1], cc[2], cc[3]);
5903 #endif
5904
5905
5906 JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
5907 return JS_TRUE;
5908}
5909
5910JSBool
5911SFVec4dGetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JS::MutableHandle<JS::Value> hvp){
5912 JSObject *obj = *hobj.address();
5913 jsid iid = *hiid.address();
5914 jsval *vp = hvp.address();
5915
5916 jsdouble d;
5917 double *cc;
5918 #ifdef JSVRMLCLASSESVERBOSE
5919 JSString *_idStr;
5920 char *_id_c;
5921 #endif
5922
5923 jsval id;
5924 if (!JS_IdToValue(cx,iid,&id)) {
5925 printf("JS_IdToValue failed in SFVec4dGetProperty.\n");
5926 return JS_FALSE;
5927 }
5928
5929
5930 #ifdef JSVRMLCLASSESVERBOSE
5931
5932/* _idStr = JS_ValueToString(cx, id);
5933 _id_c = JS_GetStringBytes(_idStr);*/
5934 _idStr = JS_ValueToString(cx, *vp);
5935#if JS_VERSION < 185
5936 _id_c = JS_GetStringBytes(_idStr);
5937#else
5938 _id_c = JS_EncodeString(cx,_idStr);
5939#endif
5940 #endif
5941
5942 if(SM_method()==2){
5943 AnyNative *ptr;
5944 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
5945 printf( "JS_GetPrivate failed in SFVec4dGetProperty.\n");
5946 return JS_FALSE;
5947 }
5948 cc = ptr->v->sfvec4d.c;
5949 }else{
5950 SFVec4dNative *ptr;
5951 if ((ptr = (SFVec4dNative *)JS_GetPrivateFw(cx,obj)) == NULL) {
5952 printf( "JS_GetPrivate failed in SFVec4dGetProperty.\n");
5953 return JS_FALSE;
5954 }
5955 cc = ptr->v.c;
5956 }
5957 if (JSVAL_IS_INT(id)) {
5958 switch (JSVAL_TO_INT(id)) {
5959 case 0:
5960 d = cc[0];
5961 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
5962 printf(
5963 "JS_NewDouble failed for %f in SFVec4dGetProperty.\n",
5964 d);
5965 return JS_FALSE;
5966 }
5967 break;
5968 case 1:
5969 d = cc[1];
5970 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
5971 printf(
5972 "JS_NewDouble failed for %f in SFVec4dGetProperty.\n",
5973 d);
5974 return JS_FALSE;
5975 }
5976 break;
5977 case 2:
5978 d = cc[2];
5979 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
5980 printf(
5981 "JS_NewDouble failed for %f in SFVec4dGetProperty.\n",
5982 d);
5983 return JS_FALSE;
5984 }
5985 break;
5986 case 3:
5987 d = cc[3];
5988 if (JS_NewNumberValue(cx, d, vp) == JS_FALSE) {
5989 printf(
5990 "JS_NewDouble failed for %f in SFVec4dGetProperty.\n",
5991 d);
5992 return JS_FALSE;
5993 }
5994 break;
5995 }
5996 } else {
5997 #ifdef JSVRMLCLASSESVERBOSE
5998 printf ("SFVec4dGetProperty, id is NOT an int...\n");
5999 #endif
6000 }
6001
6002 return JS_TRUE;
6003}
6004
6005JSBool
6006SFVec4dSetProperty(JSContext *cx, JS::Handle<JSObject*> hobj, JS::Handle<jsid> hiid, JSBool strict, JS::MutableHandle<JS::Value> hvp){
6007 JSObject *obj = *hobj.address();
6008 jsid iid = *hiid.address();
6009 jsval *vp = hvp.address();
6010
6011 jsval myv;
6012 double *cc;
6013
6014
6015 jsval id;
6016 if (!JS_IdToValue(cx,iid,&id)) {
6017 printf("JS_IdToValue failed in SFVec4dSetProperty.\n");
6018 return JS_FALSE;
6019 }
6020
6021 if(SM_method()==2){
6022 AnyNative *ptr;
6023 if ((ptr = (AnyNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
6024 printf( "JS_GetPrivate failed in SFVec4dSetProperty.\n");
6025 return JS_FALSE;
6026 }
6027 if(ptr->valueChanged)
6028 (*ptr->valueChanged)++;
6029 #ifdef JSVRMLCLASSESVERBOSE
6030 printf("SFVec4dSetProperty: obj = %p, id = %d, valueChanged = %d\n",
6031 obj, JSVAL_TO_INT(id), ptr->valueChanged);
6032 #endif
6033 cc = ptr->v->sfvec4d.c;
6034 }else{
6035 SFVec4dNative *ptr;
6036
6037 if ((ptr = (SFVec4dNative *)JS_GetPrivateFw(cx, obj)) == NULL) {
6038 printf( "JS_GetPrivate failed in SFVec4dSetProperty.\n");
6039 return JS_FALSE;
6040 }
6041 ptr->valueChanged++;
6042 #ifdef JSVRMLCLASSESVERBOSE
6043 printf("SFVec4dSetProperty: obj = %p, id = %d, valueChanged = %d\n",
6044 obj, JSVAL_TO_INT(id), ptr->valueChanged);
6045 #endif
6046 cc = ptr->v.c;
6047 }
6048 if (!JS_ConvertValue(cx, *vp, JSTYPE_NUMBER, &myv)) {
6049 printf( "JS_ConvertValue failed in SFVec4dSetProperty.\n");
6050 return JS_FALSE;
6051 }
6052
6053 if (JSVAL_IS_INT(id)) {
6054 switch (JSVAL_TO_INT(id)) {
6055 case 0:
6056 cc[0] = (float) JSVAL_TO_DOUBLE(myv);
6057 break;
6058 case 1:
6059 cc[1] = (float) JSVAL_TO_DOUBLE(myv);
6060 break;
6061 case 2:
6062 cc[2] = (float) JSVAL_TO_DOUBLE(myv);
6063 break;
6064 case 3:
6065 cc[3] = (float) JSVAL_TO_DOUBLE(myv);
6066 break;
6067 }
6068 }
6069 return JS_TRUE;
6070}
6071#endif //defined(JS_SMCPP)
6072#endif //JAVASCRIPT_SM