From 59de73ecbb4559c7ce97d5eb93df0666be43fdfe Mon Sep 17 00:00:00 2001 From: Jaromil Date: Fri, 2 Apr 2010 18:23:43 +0200 Subject: [PATCH] added properties to screen now screen.w and screen.h give back size and screen.initialized says if init() was succesfull plus the array layers[] contains the list of currently added layers --- scripts/javascript/examples/star.js | 2 +- scripts/javascript/examples/vector_test.js | 2 +- src/include/jsparser_data.h | 7 +- src/jsparser.cpp | 2 +- src/screen_js.cpp | 144 ++++++++++++--------- 5 files changed, 92 insertions(+), 65 deletions(-) diff --git a/scripts/javascript/examples/star.js b/scripts/javascript/examples/star.js index ae4a37cf..3631075d 100644 --- a/scripts/javascript/examples/star.js +++ b/scripts/javascript/examples/star.js @@ -14,7 +14,7 @@ c = PI * 2; o = -PI / 2; scr = new Screen(); -if (!scr.is_initialized()) { +if (!scr.initialized) { // screen hasn't been initialized yet, let's do it now scr.init(400,300); add_screen(scr); diff --git a/scripts/javascript/examples/vector_test.js b/scripts/javascript/examples/vector_test.js index 75e143db..9eec6b98 100644 --- a/scripts/javascript/examples/vector_test.js +++ b/scripts/javascript/examples/vector_test.js @@ -1,7 +1,7 @@ // simple test for CairoLayer scr = new Screen(); -if (!scr.is_initialized()) { +if (!scr.initialized) { // screen hasn't been initialized yet, let's do it now scr.init(400,300); add_screen(scr); diff --git a/src/include/jsparser_data.h b/src/include/jsparser_data.h index 9b98a372..6779f377 100644 --- a/src/include/jsparser_data.h +++ b/src/include/jsparser_data.h @@ -119,6 +119,7 @@ extern JSFunctionSpec global_functions[]; // Screen extern JSClass screen_class; extern JSFunctionSpec screen_methods[]; +extern JSPropertySpec screen_properties[]; // Parameter extern JSClass parameter_class; @@ -317,11 +318,13 @@ JS(entry_select); //////////////////////////////// // Screen methods JS(screen_init); -JS(screen_initialized); JS(screen_add_layer); JS(screen_rem_layer); -JS(screen_list_layers); JS(screen_save_frame); +JSP(screen_get_width); +JSP(screen_get_height); +JSP(screen_initialized); +JSP(screen_list_layers); //////////////////////////////// // Parameter methods diff --git a/src/jsparser.cpp b/src/jsparser.cpp index 61886e4d..c289f1d1 100644 --- a/src/jsparser.cpp +++ b/src/jsparser.cpp @@ -140,7 +140,7 @@ void JsParser::init_class(JSContext *cx, JSObject *obj) { REGISTER_CLASS("Screen", screen_class, screen_constructor, - NULL, + screen_properties, screen_methods, NULL); Screen = layer_object; diff --git a/src/screen_js.cpp b/src/screen_js.cpp index 665f3145..3e6a16b4 100644 --- a/src/screen_js.cpp +++ b/src/screen_js.cpp @@ -32,12 +32,19 @@ JSFunctionSpec screen_methods[] = { {"init", screen_init, 2}, {"add_layer", screen_add_layer, 1}, {"rem_layer", screen_rem_layer, 1}, - {"list_layers", screen_list_layers, 0}, - {"is_initialized", screen_initialized, 0}, {"save_frame", screen_save_frame, 1}, {0} }; +JSPropertySpec screen_properties[] = { + // ro + { "w", 0, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY, screen_get_width, NULL }, + { "h", 1, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY, screen_get_height, NULL }, + { "layers", 2, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY, screen_list_layers, NULL }, + { "initialized", 3, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY, screen_initialized, NULL }, + {0} +}; + JS(screen_constructor) { func("%s",__PRETTY_FUNCTION__); char *type = NULL; @@ -94,16 +101,6 @@ JS(screen_init) { return JS_TRUE; } -JS(screen_initialized) { - func("%s",__PRETTY_FUNCTION__); - ViewPort *screen = (ViewPort*)JS_GetPrivate(cx,obj); - if(!screen) { - JS_ERROR("Screen core data is NULL"); - return JS_FALSE; - } - *rval = BOOLEAN_TO_JSVAL(screen->initialized?JS_TRUE:JS_FALSE); - return JS_TRUE; -} JS(screen_save_frame) { func("%s",__PRETTY_FUNCTION__); @@ -128,54 +125,6 @@ JS(screen_save_frame) { } -JS(screen_list_layers) { - func("%s",__PRETTY_FUNCTION__); - JSObject *arr; - JSObject *objtmp; - - Layer *lay; - - jsval val; - int c = 0; - - ViewPort *screen = (ViewPort*)JS_GetPrivate(cx,obj); - if(!screen) { - JS_ERROR("Screen core data is NULL"); - return JS_TRUE; - } - - arr = JS_NewArrayObject(cx, 0, NULL); // create void array - if(!arr) return JS_FALSE; - - // XXX check this carefully - // caedes reports some weird problems after calling list_layers - // looks like here might be the hairy point - lay = screen->layers.begin(); - while(lay) { - if (lay->data) { - func("reusing %p", lay->data); - val = (jsval)lay->data; - } else { - func("new JS Object"); - objtmp = JS_NewObject(cx, lay->jsclass, NULL, obj); - - JS_SetPrivate(cx,objtmp,(void*) lay); - - val = OBJECT_TO_JSVAL(objtmp); - - lay->data = (void*)val; - } - - JS_SetElement(cx, arr, c, &val ); - - c++; - lay = (Layer*)lay->next; - } - - *rval = OBJECT_TO_JSVAL( arr ); - return JS_TRUE; -} - JS(screen_add_layer) { func("%s",__PRETTY_FUNCTION__); @@ -225,6 +174,81 @@ JS(screen_rem_layer) { return JS_TRUE; } + +//////////////////////////////////////////////// +/////// Properties + +JSP(screen_get_width) { + ViewPort *screen = (ViewPort*)JS_GetPrivate(cx,obj); + if(!screen) { JS_ERROR("Screen core data is NULL"); } + else JS_NewNumberValue(cx, (jsint)screen->geo.w, vp); + return JS_TRUE; +} + +JSP(screen_get_height) { + ViewPort *screen = (ViewPort*)JS_GetPrivate(cx,obj); + if(!screen) { JS_ERROR("Screen core data is NULL"); } + else JS_NewNumberValue(cx, (jsint)screen->geo.h, vp); + return JS_TRUE; +} + +JSP(screen_initialized) { + func("%s",__PRETTY_FUNCTION__); + ViewPort *screen = (ViewPort*)JS_GetPrivate(cx,obj); + if(!screen) { + JS_ERROR("Screen core data is NULL"); + return JS_FALSE; + } + *vp = BOOLEAN_TO_JSVAL(screen->initialized?JS_TRUE:JS_FALSE); + return JS_TRUE; +} + + +JSP(screen_list_layers) { + func("%s",__PRETTY_FUNCTION__); + JSObject *arr; + JSObject *objtmp; + + Layer *lay; + + jsval val; + int c = 0; + + ViewPort *screen = (ViewPort*)JS_GetPrivate(cx,obj); + if(!screen) { + JS_ERROR("Screen core data is NULL"); + return JS_TRUE; + } + + arr = JS_NewArrayObject(cx, 0, NULL); // create void array + if(!arr) return JS_FALSE; + + // XXX check this carefully + // caedes reports some weird problems after calling list_layers + // looks like here might be the hairy point + lay = screen->layers.begin(); + while(lay) { + if (lay->jsobj) { + func("TESTING: reusing layer jsobj %p", lay->jsobj); + objtmp = lay->jsobj; + } else { + func("TESTING: creating a jsobj for layer %s", lay->name); + objtmp = JS_NewObject(cx, lay->jsclass, NULL, obj); + JS_SetPrivate(cx,objtmp,(void*) lay); + } + + val = OBJECT_TO_JSVAL(objtmp); + + JS_SetElement(cx, arr, c, &val ); + + c++; + lay = (Layer*)lay->next; + } + + *vp = OBJECT_TO_JSVAL( arr ); + return JS_TRUE; +} + /* void js_screen_gc (JSContext *cx, JSObject *obj) { func("%s",__PRETTY_FUNCTION__);