From a190f3f4e2c05ec171efd2078533464bc706ced2 Mon Sep 17 00:00:00 2001 From: Jaromil Date: Sat, 16 May 2009 17:00:37 +0200 Subject: [PATCH] refactoring of garbage collection testing that multiple scripts can be included from a main one removed js garbage collection in controllers (was called after script load) included JSON as a standard library (it works!) modified example scripts to avoid namespace clash --- scripts/javascript/examples/cynosure.js | 44 +-- scripts/javascript/examples/json_test.js | 31 ++ scripts/javascript/examples/star.js | 16 +- scripts/javascript/examples/test_rand.js | 48 +-- scripts/javascript/lib/json2.js | 478 +++++++++++++++++++++++ src/controller.cpp | 21 +- src/jsparser.cpp | 2 +- 7 files changed, 578 insertions(+), 62 deletions(-) create mode 100644 scripts/javascript/examples/json_test.js create mode 100644 scripts/javascript/lib/json2.js diff --git a/scripts/javascript/examples/cynosure.js b/scripts/javascript/examples/cynosure.js index 532a3d3a..199b41af 100644 --- a/scripts/javascript/examples/cynosure.js +++ b/scripts/javascript/examples/cynosure.js @@ -43,29 +43,29 @@ width=1024; height=768; width=800; height=800; width=400; height=300; -geo = new GeometryLayer(); -geo.activate(true); -add_layer(geo); +cyno_geo = new GeometryLayer(); +cyno_geo.activate(true); +add_layer(cyno_geo); -trigger = new TriggerController(); -register_controller(trigger); -trigger.frame = function() { Draw(); }; +cyno_trigger = new TriggerController(); +register_controller(cyno_trigger); +cyno_trigger.frame = function() { Draw(); }; -kbd = new KeyboardController(); -register_controller(kbd); -kbd.released_q = function() { quit(); } -kbd.released_x = function() { Draw(); } +cyno_kbd = new KeyboardController(); +register_controller(cyno_kbd); +cyno_kbd.released_q = function() { quit(); } +cyno_kbd.released_x = function() { Draw(); } -kbd.released_p = function() { +cyno_kbd.released_p = function() { echo("ok, I shot myself now"); - try{rem_controller(trigger);}catch(e){echo("tg nope: "+e);} - try{rem_layer(geo);}catch(e){echo("la nope: "+e);} + try{rem_controller(cyno_trigger);}catch(e){echo("tg nope: "+e);} + try{rem_layer(cyno_geo);}catch(e){echo("la nope: "+e);} try{rem_controller(mc);}catch(e){echo("mc nope: "+e);} - try{rem_controller(kbd);}catch(e){echo("kb nope: "+e);} - delete geo; + try{rem_controller(cyno_kbd);}catch(e){echo("kb nope: "+e);} + delete cyno_geo; delete mc; - delete kbd; - delete trigger; + delete cyno_kbd; + delete cyno_trigger; gc(); // kbd-js not cleared within this call! echo("Bullet arrived."); } @@ -132,7 +132,7 @@ function Draw() { for (j=0; j<10; j++) { if ((iterations > 0) && (++i >= iterations)) { i = 0; // clearScreen - geo.rectangle_fill(0,0,width,height,0x000000FF); + cyno_geo.rectangle_fill(0,0,width,height,0x000000FF); } paint(); } @@ -193,22 +193,22 @@ function paint() { /* Draw the shadow */ if (elevation > 0) - geo.rectangle_fill(curX + elevation, curY + elevation, + cyno_geo.rectangle_fill(curX + elevation, curY + elevation, curX + elevation + curWidth, curY + elevation + curHeight, 0x00000060); /* Draw the edge */ if (shadowWidth > 0) - geo.rectangle_fill(curX + shadowWidth, curY + shadowWidth, + cyno_geo.rectangle_fill(curX + shadowWidth, curY + shadowWidth, curX + shadowWidth + curWidth, curY + shadowWidth + curHeight, 0x000000ff); - geo.rectangle_fill(curX, curY, curX+curWidth, curY+curHeight, fg_gc) + cyno_geo.rectangle_fill(curX, curY, curX+curWidth, curY+curHeight, fg_gc) /* Draw a 1-pixel black border around the rectangle */ - geo.rectangle(curX, curY, curX+curWidth, curY+curHeight, 0x00000000); + cyno_geo.rectangle(curX, curY, curX+curWidth, curY+curHeight, 0x00000000); } } } diff --git a/scripts/javascript/examples/json_test.js b/scripts/javascript/examples/json_test.js new file mode 100644 index 00000000..62526d91 --- /dev/null +++ b/scripts/javascript/examples/json_test.js @@ -0,0 +1,31 @@ +include("../lib/json2.js"); + +echo("JSON library included"); + +files = scandir("."); + +echo("files found: " + files.length); + +// stringify the array +json_enc = JSON.stringify(files); + +// and parse it back +json_dec = JSON.parse(json_enc); + +// then go through the array and load the scripts we like +var c; +for(c=0; cname, OBJECT_TO_JSVAL(obj), ctrl->jsobj, ctrl); - notice("JSgc: deleting %s Controller %s", jc->name, ctrl->name); - delete ctrl; - } else { - func("Mh, object(%s) has no private data", jc->name); - } +// if (ctrl) { +// // jc = JS_GET_CLASS(cx,obj); +// func("JSvalcmp(%s): %p / %p ctrl: %p", jc->name, OBJECT_TO_JSVAL(obj), ctrl->jsobj, ctrl); +// notice("JSgc: deleting %s Controller %s", jc->name, ctrl->name); +// delete ctrl; +// } else { +// func("Mh, object(%s) has no private data", jc->name); +// } + } /* JSCall function by name, cvalues will be converted diff --git a/src/jsparser.cpp b/src/jsparser.cpp index f01b24e0..91c4f946 100644 --- a/src/jsparser.cpp +++ b/src/jsparser.cpp @@ -371,7 +371,7 @@ int JsParser::open(JSContext *cx, JSObject *obj, const char* script_file) { // if anything more was wrong, our ErrorReporter was called! free(buf); func("%s evalres: %i", __func__, eval_res); - gc(); + // gc(); return eval_res; }