From c19993915ea5b4e3a3335607a84460a112bde854 Mon Sep 17 00:00:00 2001 From: Artsiom Trubchyk Date: Mon, 5 Apr 2021 00:12:32 +0300 Subject: [PATCH] Add ArrayBuffer support --- example/engine_template.html | 5 ++++- example/example.script | 6 +++++- example/example_2.script | 6 +++++- jstodef/lib/web/lib_jstodef.js | 21 +++++++++++++++------ jstodef/src/jstodef.cpp | 10 +++++----- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/example/engine_template.html b/example/engine_template.html index 727c176..228d1c9 100644 --- a/example/engine_template.html +++ b/example/engine_template.html @@ -202,10 +202,13 @@ JsToDef.send("ObjectEvent", {foo:"bar", num:16}); JsToDef.send("FloatEvent", 19.2); JsToDef.send("IntEvent", 18); - JsToDef.send("StrintEvent", "str"); + JsToDef.send("StringEvent", "str"); + JsToDef.send("NullStringEvent", "str\0str"); JsToDef.send("EmptyEvent"); JsToDef.send("BooleanEvent", true); JsToDef.send("BooleanEvent", false); + JsToDef.send("Uint8ArrayEvent", new Uint8Array([68, 101, 102, 111, 108, 100])); + JsToDef.send("ArrayBufferEvent", (new Uint8Array([78, 117, 108, 108, 0, 84, 101, 115, 116])).buffer); }, 3000) diff --git a/example/example.script b/example/example.script index 410c392..dd84e8b 100644 --- a/example/example.script +++ b/example/example.script @@ -1,5 +1,9 @@ local function example_1_listener(self, message_id, message) - pprint("example1", message_id, message) + if type(message) == "string" then + print("example1", message_id, message, "(" .. string.len(message) .. " bytes)") + else + pprint("example1", message_id, message) + end end function init(self) diff --git a/example/example_2.script b/example/example_2.script index 3230abe..db5b8a9 100644 --- a/example/example_2.script +++ b/example/example_2.script @@ -1,5 +1,9 @@ local function example_2_listener(self, message_id, message) - pprint("example2", message_id, message) + if type(message) == "string" then + print("example2", message_id, message, "(" .. string.len(message) .. " bytes)") + else + pprint("example2", message_id, message) + end end function init(self) diff --git a/jstodef/lib/web/lib_jstodef.js b/jstodef/lib/web/lib_jstodef.js index e9a6118..8920f8c 100644 --- a/jstodef/lib/web/lib_jstodef.js +++ b/jstodef/lib/web/lib_jstodef.js @@ -23,15 +23,24 @@ var LibJsToDef = { {{{ makeDynCall("vif", "JsToDef._callback_number")}}}(msg_id, message); break; case 'string': - var msg = allocate(intArrayFromString(message), "i8", ALLOC_NORMAL); - {{{ makeDynCall("vii", "JsToDef._callback_string")}}}(msg_id, msg); + var msg_arr = intArrayFromString(message, true); + var msg = allocate(msg_arr, "i8", ALLOC_NORMAL); + {{{ makeDynCall("viii", "JsToDef._callback_string")}}}(msg_id, msg, msg_arr.length); Module._free(msg); break; case 'object': - var msg = JSON.stringify(message); - msg = allocate(intArrayFromString(msg), "i8", ALLOC_NORMAL); - {{{ makeDynCall("vii"," JsToDef._callback_object")}}}(msg_id, msg); - Module._free(msg); + if (message instanceof ArrayBuffer || ArrayBuffer.isView(message)) { + var msg_arr = new Uint8Array(ArrayBuffer.isView(message) ? message.buffer : message); + var msg = allocate(msg_arr, "i8", ALLOC_NORMAL); + {{{ makeDynCall("viii", "JsToDef._callback_string")}}}(msg_id, msg, msg_arr.length); + Module._free(msg); + } else { + var msg = JSON.stringify(message); + var msg_arr = intArrayFromString(msg, true); + msg = allocate(msg_arr, "i8", ALLOC_NORMAL); + {{{ makeDynCall("viii", "JsToDef._callback_object")}}}(msg_id, msg, msg_arr.length); + Module._free(msg); + } break; case 'boolean': var msg = message ? 1 : 0; diff --git a/jstodef/src/jstodef.cpp b/jstodef/src/jstodef.cpp index 2bfa938..af9a697 100644 --- a/jstodef/src/jstodef.cpp +++ b/jstodef/src/jstodef.cpp @@ -7,7 +7,7 @@ #if defined(DM_PLATFORM_HTML5) -typedef void (*ObjectMessage)(const char* message_id, const char* message); +typedef void (*ObjectMessage)(const char* message_id, const char* message, const int length); typedef void (*NoMessage)(const char* message_id); typedef void (*NumberMessage)(const char* message_id, float message); typedef void (*BooleanMessage)(const char* message_id, int message); @@ -63,7 +63,7 @@ static bool check_callback_and_instance(JsToDefListener* cbk) return true; } -static void JsToDef_SendObjectMessage(const char* message_id, const char* message) +static void JsToDef_SendObjectMessage(const char* message_id, const char* message, const int length) { for(int i = m_listeners.Size() - 1; i >= 0; --i) { @@ -79,7 +79,7 @@ static void JsToDef_SendObjectMessage(const char* message_id, const char* messag //[-2] - self //[-3] - callback dmJson::Document doc; - dmJson::Result r = dmJson::Parse(message, &doc); + dmJson::Result r = dmJson::Parse(message, length, &doc); if (r == dmJson::RESULT_OK && doc.m_NodeCount > 0) { char error_str_out[128]; if (dmScript::JsonToLua(L, &doc, 0, error_str_out, sizeof(error_str_out)) < 0) { @@ -110,7 +110,7 @@ static void JsToDef_SendObjectMessage(const char* message_id, const char* messag } } -static void JsToDef_SendStringMessage(const char* message_id, const char* message) +static void JsToDef_SendStringMessage(const char* message_id, const char* message, const int length) { for(int i = m_listeners.Size() - 1; i >= 0; --i) { @@ -122,7 +122,7 @@ static void JsToDef_SendStringMessage(const char* message_id, const char* messag int top = lua_gettop(L); if (check_callback_and_instance(cbk)) { lua_pushstring(L, message_id); - lua_pushstring(L, message); + lua_pushlstring(L, message, length); int ret = lua_pcall(L, 3, 0, 0); if(ret != 0) { dmLogError("Error running callback: %s", lua_tostring(L, -1));