103
103
#if !defined(__GNUC__)
104
104
# if defined(_WIN32_WCE)
105
105
# pragma comment(lib, "js32")
106
# elif defined (_WIN64)
107
# pragma comment(lib, "js")
106
108
# elif defined (WIN32)
107
# pragma comment(lib, "js32")
109
# pragma comment(lib, "js")
111
113
/*define this macro to force Garbage Collection after each input to JS (script initialize/shutdown and all eventIn)
112
114
on latest SM, GC will crash if called from a different thread than the thread creating the contex, no clue why
116
for iOS don't force GC (better performances according to Ivica)
118
#if !defined(GPAC_IPHONE)
119
# if (JS_VERSION<180)
881
887
/*route to object*/
883
GF_RouteToFunction *r;
890
const char *fun_name;
891
GF_RouteToFunction *r = NULL;
884
892
if (!JSVAL_IS_OBJECT(argv[3]) || !JS_ObjectIsFunction(c, JSVAL_TO_OBJECT(argv[3])) ) return JS_FALSE;
886
GF_SAFEALLOC(r, GF_RouteToFunction)
887
if (!r) return JS_FALSE;
889
r->FromField.fieldIndex = f_id1;
890
gf_node_get_field(r->FromNode, f_id1, &r->FromField);
892
r->ToNode = (GF_Node*)JS_GetScript(c);
893
r->ToField.fieldType = GF_SG_VRML_SCRIPT_FUNCTION;
894
r->ToField.on_event_in = on_route_to_object;
895
r->ToField.eventType = GF_SG_EVENT_IN;
896
r->ToField.far_ptr = NULL;
897
r->ToField.name = JS_GetFunctionName( JS_ValueToFunction(c, argv[3] ) );
899
r->obj = JSVAL_TO_OBJECT( argv[2] ) ;
900
// gf_js_add_root(c, & r->obj);
903
// gf_js_add_root(c, &r->fun);
906
r->graph = n1->sgprivate->scenegraph;
908
if (!n1->sgprivate->interact) GF_SAFEALLOC(n1->sgprivate->interact, struct _node_interactive_ext);
909
if (!n1->sgprivate->interact->routes) n1->sgprivate->interact->routes = gf_list_new();
910
gf_list_add(n1->sgprivate->interact->routes, r);
911
gf_list_add(n1->sgprivate->scenegraph->Routes, r);
894
fun_name = JS_GetFunctionName( JS_ValueToFunction(c, argv[3] ) );
895
if (fun_name && n1->sgprivate->interact && n1->sgprivate->interact->routes ) {
896
while ( (r = (GF_RouteToFunction*)gf_list_enum(n1->sgprivate->interact->routes, &i) )) {
897
if ( (r->FromNode == n1)
898
&& (r->FromField.fieldIndex == f_id1)
899
&& (r->ToNode == (GF_Node*)JS_GetScript(c))
900
&& !stricmp(r->ToField.name, fun_name)
907
GF_SAFEALLOC(r, GF_RouteToFunction)
908
if (!r) return JS_FALSE;
910
r->FromField.fieldIndex = f_id1;
911
gf_node_get_field(r->FromNode, f_id1, &r->FromField);
913
r->ToNode = (GF_Node*)JS_GetScript(c);
914
r->ToField.fieldType = GF_SG_VRML_SCRIPT_FUNCTION;
915
r->ToField.on_event_in = on_route_to_object;
916
r->ToField.eventType = GF_SG_EVENT_IN;
917
r->ToField.far_ptr = NULL;
918
r->ToField.name = fun_name;
920
r->obj = JSVAL_TO_OBJECT( argv[2] ) ;
921
// gf_js_add_root(c, & r->obj);
924
// gf_js_add_root(c, &r->fun);
927
r->graph = n1->sgprivate->scenegraph;
929
if (!n1->sgprivate->interact) GF_SAFEALLOC(n1->sgprivate->interact, struct _node_interactive_ext);
930
if (!n1->sgprivate->interact->routes) n1->sgprivate->interact->routes = gf_list_new();
931
gf_list_add(n1->sgprivate->interact->routes, r);
932
gf_list_add(n1->sgprivate->scenegraph->Routes, r);
1178
1202
/*field has changed, set routes...*/
1179
if (parent->sgprivate->tag == TAG_ProtoNode)
1203
if (parent->sgprivate->tag == TAG_ProtoNode) {
1180
1204
gf_sg_proto_propagate_event(parent, field->fieldIndex, (GF_Node*)JS_GetScript(c));
1205
/* Node exposedField can also be routed to another field */
1206
gf_node_event_out_proto(parent, field->fieldIndex);
1182
1208
gf_node_event_out(parent, field->fieldIndex);
1183
1209
gf_node_changed_internal(parent, field, 0);
1791
1817
pixels = (MFInt32 *) ((GF_JSField *) SMJS_GET_PRIVATE(c, JSVAL_TO_OBJECT(*vp)))->field.far_ptr;
1792
1818
if (sfi->pixels) gf_free(sfi->pixels);
1793
1819
len = sfi->width*sfi->height*sfi->numComponents;
1794
sfi->pixels = (char *) gf_malloc(sizeof(char)*len);
1820
sfi->pixels = (unsigned char *) gf_malloc(sizeof(char)*len);
1795
1821
len = MAX(len, pixels->count);
1796
1822
for (i=0; i<len; i++) sfi->pixels[i] = (u8) pixels->vals[i];
4353
4381
gf_sg_lock_javascript(priv->js_ctx, 1);
4355
4383
//locate function
4356
if (! JS_LookupProperty(priv->js_ctx, priv->js_obj, sf->name, &fval) || JSVAL_IS_VOID(fval)) {
4384
if (!JS_LookupProperty(priv->js_ctx, priv->js_obj, sf->name, &fval) || JSVAL_IS_VOID(fval) ||
4385
!JS_GetPropertyAttributes(priv->js_ctx, priv->js_obj, sf->name, &attr, &found) || found != JS_TRUE ){
4357
4386
gf_sg_lock_javascript(priv->js_ctx, 0);
4415
4446
jsscript[fsize] = 0;
4417
ret = JS_EvaluateScript(priv->js_ctx, priv->js_obj, jsscript, sizeof(char)*(size_t)fsize, 0, 0, &rval);
4448
ret = JS_EvaluateScript(priv->js_ctx, priv->js_obj, jsscript, (u32) (sizeof(char)*fsize), 0, 0, &rval);
4418
4449
if (ret==JS_FALSE) success = 0;
4420
if (success && primary_script && JS_LookupProperty(priv->js_ctx, priv->js_obj, "initialize", &fval)) {
4421
if (! JSVAL_IS_VOID(fval)) {
4451
if (success && primary_script
4452
&& JS_LookupProperty(priv->js_ctx, priv->js_obj, "initialize", &fval) && !JSVAL_IS_VOID(fval)
4453
&& JS_GetPropertyAttributes(priv->js_ctx, priv->js_obj, "initialize", &attr, &found) && found == JS_TRUE) {
4422
4455
JS_CallFunctionValue(priv->js_ctx, priv->js_obj, fval, 0, NULL, &rval);
4423
4456
gf_js_vrml_flush_event_out((GF_Node *)script, priv);
4426
4458
gf_free(jsscript);
4427
4459
return success;
4566
4600
GF_LOG(GF_LOG_DEBUG, GF_LOG_SCRIPT, ("[VRML JS] Evaluating script %s\n", str));
4569
ret = JS_EvaluateScript(priv->js_ctx, priv->js_obj, str, strlen(str), 0, 0, &rval);
4603
ret = JS_EvaluateScript(priv->js_ctx, priv->js_obj, str, (u32) strlen(str), 0, 0, &rval);
4570
4604
if (ret==JS_TRUE) {
4571
4605
/*call initialize if present*/
4572
if (JS_LookupProperty(priv->js_ctx, priv->js_obj, "initialize", &fval) && !JSVAL_IS_VOID(fval))
4606
if (JS_LookupProperty(priv->js_ctx, priv->js_obj, "initialize", &fval) && !JSVAL_IS_VOID(fval)
4607
&& JS_GetPropertyAttributes(priv->js_ctx, priv->js_obj, "initialize", &attr, &found) && found == JS_TRUE)
4573
4609
JS_CallFunctionValue(priv->js_ctx, priv->js_obj, fval, 0, NULL, &rval);
4575
gf_js_vrml_flush_event_out(node, priv);
4610
gf_js_vrml_flush_event_out(node, priv);
4915
4951
#endif /* GPAC_HAS_SPIDERMONKEY */
4953
GF_Err gf_scene_execute_script(GF_SceneGraph *sg, const char *com)
4955
#ifdef GPAC_HAS_SPIDERMONKEY
4958
GF_Node *root = gf_sg_get_root_node(sg);
4960
tag = gf_node_get_tag(root);
4961
if (tag >= GF_NODE_RANGE_FIRST_SVG) {
4962
GF_SVGJS *svg_js = sg->svg_js;
4963
Bool ret = svg_js->script_execute(sg, (char *)com, NULL);
4964
return (ret == GF_TRUE ? GF_OK : GF_BAD_PARAM);
4966
e = GF_NOT_SUPPORTED;
4970
return GF_BAD_PARAM;
4972
return GF_NOT_SUPPORTED;