From abdcbc5934823605d0ddea48ddceb2d483f80e50 Mon Sep 17 00:00:00 2001 From: Angrymouse Date: Sun, 28 Apr 2024 21:46:13 +0200 Subject: [PATCH] fixing calling functions exposed by the vm --- bindings.cpp | 15 +++++++++++++++ index.js | 8 +++++++- package.json | 2 +- test.js | 11 ++++++----- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/bindings.cpp b/bindings.cpp index 740b186..dceb82f 100644 --- a/bindings.cpp +++ b/bindings.cpp @@ -179,6 +179,21 @@ void create_and_associate_thread(duk_context *ctx) emit_event_callback(ctx, json{{"event", "callFinished"}, {"callId", msg["callId"]}, {"result", result}}.dump()); } duk_pop(ctx); + } else if(eventName=="callFunctionByPointer"){ + duk_push_heapptr(ctx, reinterpret_cast(msg["pointer"].get())); + for (const auto arg : msg["args"]) + { + json_to_duk(ctx,arg.dump()); + } + if (duk_pcall(ctx,msg["args"].size()) != 0) + { + emit_event_callback(ctx, json{{"event", "callFinished"}, {"callId", msg["callId"]}, {"error", duk_safe_to_string(ctx, -1)}}.dump()); + }else{ + + emit_event_callback(ctx, json{{"event", "callFinished"}, {"callId", msg["callId"]}, {"result", duk_to_json(ctx,-1)}}.dump()); + duk_pop(ctx); + + } } else if(eventName == "flushContext") { diff --git a/index.js b/index.js index 72250a8..bea0374 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,12 @@ class Glomium { "callFinished": () => { // console.log("Node got:",msg) const prom = this.callbackMap.get(msg.callId) - prom.resolve(msg.result); + if(!msg.result&&msg.error){ + prom.reject(msg.error) + }else{ + prom.resolve(msg.result); + } + this.callbackMap.delete(msg.callId) }, "fatalError": () => { @@ -92,6 +97,7 @@ class Glomium { or = (({ "function": () => { return async (...args) => { + return await engineClass.__passToEngine({ event: "callFunctionByPointer", pointer: o.__engineInternalProperties.heapptr, args:args}) } diff --git a/package.json b/package.json index 8228a2a..c88ff2b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "glomium", - "version": "1.1.2", + "version": "1.1.3", "description": "Duktape bindings for node.js", "main": "index.js", "scripts": { diff --git a/test.js b/test.js index 33cf895..3351c78 100644 --- a/test.js +++ b/test.js @@ -17,10 +17,11 @@ const Glomium = require("./"); await glomium.set("wait", wait); - glomium.run(` - console.log("Hello") - wait(1000) - console.log("World") + glomium.run(`function test(value){ + console.log("calling "+value.name) + throw "test" + } `) - +console.log(await glomium.get("test").then(test=>test({name:"works"})).catch(e=>console.error(e)) +) })()