diff --git a/source/qjs_actor.c b/source/qjs_actor.c index 54d592b2..619d6c1a 100644 --- a/source/qjs_actor.c +++ b/source/qjs_actor.c @@ -46,6 +46,36 @@ prosperon_rt *js2actor(JSContext *js, JSValue v) return actor; } +JSValue actor2js(JSContext *js, prosperon_rt *actor) +{ + if (!actor) + return JS_NULL; + + /* Create the actor object */ + JSValue actor_obj = JS_NewObject(js); + if (JS_IsException(actor_obj)) + return actor_obj; + + /* Create the __ACTORDATA__ object */ + JSValue actor_data = JS_NewObject(js); + if (JS_IsException(actor_data)) { + JS_FreeValue(js, actor_obj); + return actor_data; + } + + /* Set the id property in __ACTORDATA__ */ + JS_SetPropertyStr(js, actor_data, "id", JS_NewString(js, actor->id)); + + /* TODO: If the actor has network info, we could add address and port here */ + /* JS_SetPropertyStr(js, actor_data, "address", JS_NewString(js, actor->address)); */ + /* JS_SetPropertyStr(js, actor_data, "port", JS_NewInt32(js, actor->port)); */ + + /* Set __ACTORDATA__ on the actor object */ + JS_SetPropertyStr(js, actor_obj, "__ACTORDATA__", actor_data); + + return actor_obj; +} + JSC_CCALL(os_createactor, int margc = JS_ArrayLength(js, argv[0]); diff --git a/source/qjs_actor.h b/source/qjs_actor.h index 0f94a8db..abab657b 100644 --- a/source/qjs_actor.h +++ b/source/qjs_actor.h @@ -6,5 +6,6 @@ JSValue js_actor_use(JSContext *js); prosperon_rt *js2actor(JSContext *js, JSValue v); +JSValue actor2js(JSContext *js, prosperon_rt *actor); #endif \ No newline at end of file diff --git a/source/qjs_sdl_video.c b/source/qjs_sdl_video.c index 550aa902..f60db021 100644 --- a/source/qjs_sdl_video.c +++ b/source/qjs_sdl_video.c @@ -2,6 +2,7 @@ #include "jsffi.h" #include "qjs_macros.h" #include "qjs_sdl_surface.h" +#include "qjs_actor.h" #include "prosperon.h" #include "sprite.h" #include "transform.h" @@ -1850,6 +1851,6 @@ JSValue js_sdl_video_use(JSContext *js) { int argc = 8; // Create the actor with the hook to set up endowments - create_actor(argc, (char**)argv, video_actor_hook); - return JS_NewString(js,id); + prosperon_rt *actor = create_actor(argc, (char**)argv, video_actor_hook); + return actor2js(js,actor); } diff --git a/tests/draw2d.js b/tests/draw2d.js index ec0004c1..8b01ee66 100644 --- a/tests/draw2d.js +++ b/tests/draw2d.js @@ -6,8 +6,7 @@ var input = use('input') input.watch($_) // Create SDL video actor -var video = use('sdl_video'); -var video_actor = {__ACTORDATA__:{id:video}}; +var video_actor = use('sdl_video'); var window_id = null; var renderer_id = null; diff --git a/tests/sdl_video.js b/tests/sdl_video.js deleted file mode 100644 index ce33803a..00000000 --- a/tests/sdl_video.js +++ /dev/null @@ -1,28 +0,0 @@ -var video = use('sdl_video') - -var window -var renderer - -var act = {__ACTORDATA__:{id:video}} - -function handle_response(data) -{ - if (data.error) - console.log(json.encode(data)) -} - -send(act, {kind:"window", op: "create"}, ({id}) => { - window = id - console.log(`made window id ${id}`) - - send(act, {kind:"window", op:"makeRenderer", id: window}, ({id}) => { - renderer = id - console.log(`made renderer with id ${id}`) - - send(act, {kind:"renderer", id: renderer, op:"set", prop: "drawColor", value:[0.6,0.4,0.5,1]}) - send(act, {kind:"renderer", id: renderer, op:"clear"}, handle_response) - send(act, {kind:"renderer", id: renderer, op: "present"}, handle_response) - }) -}) - -$_.delay($_.stop, 2) \ No newline at end of file