bytecodealliance / javy

JS to WebAssembly toolchain
Apache License 2.0
2.2k stars 106 forks source link

Serialize function / callbacks with enclosing context; #648

Closed redoC-A2k closed 4 months ago

redoC-A2k commented 4 months ago

How can I get a function from one quickjs wasm rs vm/runtime and execute that function in another instance of quickjs wasm rs vm/runtime.

Suppose I have a function like below


let x = "hello"
function abc(){
console.log(x)
}

I want to take above abc function from one qjs instance and give it to another instance and run . The output should be same in second instance as it was in first .

redoC-A2k commented 4 months ago

Basically I want to serialize the qjs function along with outer world and then deserialize that function and execute in another qjs instance.

jeffcharles commented 4 months ago

Are you trying to capture state that's been mutated from its initial form? And are you intending to create new instance of your Javy module and execute the function with its captured state in it or execute that function with captured state in an already existing separate instance of your Javy module?

I don't think there's a clean way to serialize a JS closure inside a Wasm instance. But I think you could copy the instance's linear memory in the Wasm host and overwrite the memory of a new instance with the memory you copied.

redoC-A2k commented 4 months ago

My actual use case is this - https://github.com/bytecodealliance/wasmtime/issues/8670#issuecomment-2124871948 Since wasm doesn't allows reentrance , So in the call method of NativeCallbackHandler I was intending to create a new instance of qjs runtime and execute that native function in that new instance . But for that I need to send the independent function from guest wasm to host runtime and for doing so I was expecting to serialize the function along with its outer scope .

jeffcharles commented 4 months ago

I was expecting to serialize the function along with its outer scope

Javy doesn't offer APIs for doing this beyond whatever is built into QuickJS itself. And as far as I know, QuickJS doesn't have an API to do something like this.