Skip to content

Commit

Permalink
fixing calling functions exposed by the vm
Browse files Browse the repository at this point in the history
  • Loading branch information
angrymouse committed Apr 28, 2024
1 parent ca92f5a commit abdcbc5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
15 changes: 15 additions & 0 deletions bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void *>(msg["pointer"].get<uintptr_t>()));
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")
{
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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": () => {
Expand Down Expand Up @@ -92,6 +97,7 @@ class Glomium {
or = (({
"function": () => {
return async (...args) => {

return await engineClass.__passToEngine({ event: "callFunctionByPointer", pointer: o.__engineInternalProperties.heapptr, args:args})

}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "glomium",
"version": "1.1.2",
"version": "1.1.3",
"description": "Duktape bindings for node.js",
"main": "index.js",
"scripts": {
Expand Down
11 changes: 6 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
})()

0 comments on commit abdcbc5

Please sign in to comment.