-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow imports to use functions from structs / Multi-threading #23
Comments
So desirable feature. In general, is it ok to use wasm to create plugin system for regular application, that will invoke that wasm like regular function? For example, for each packet from iot device i need to decode it into some struct. That decoders will be supplied as wasm. |
26: feat(import) Support instance context API r=Hywan a=Hywan Partially address #23. Ping @YaronWittenstein This PR brings an `InstanceContext` API: 1. From an imported function, call `IntoInstanceContext(context)` to cast the `unsafe.Pointer` into an `InstanceContext`, 2. Now, call the `Memory` method to get the instance context, 3. Use the `Memory.Data` method to read or write memor data. Co-authored-by: Ivan Enderlin <[email protected]>
Thanks for your proposal. I read multiple requests:
So, about 1, see #27. |
34: feat(module) Support module serialization and deserialization r=Hywan a=Hywan Sequel of #27 and #33. Ping #23. Example: ```go // Compiles the bytes into a WebAssembly module. module1, _ := wasm.Compile(GetBytes()) defer module1.Close() // Serializes the module into a sequence of bytes. serialization, _ := module1.Serialize() // Do something with `serialization`. // Then later… // Deserializes the module. module2, _ := wasm.DeserializeModule(serialization) defer module2.Close() // And enjoy! // Instantiates the WebAssembly module. instance, _ := module2.Instantiate() defer instance.Close() // Gets an exported function. sum, functionExists := instance.Exports["sum"] fmt.Println(functionExists) // Calls the `sum` exported function with Go values. result, _ := sum(1, 2) fmt.Println(result) // Output: // true // 3 ``` The final API is quite easy to use. I hope `[]byte` is a generic enough type to be accepted by Go caching libraries. Co-authored-by: Ivan Enderlin <[email protected]>
I think all the questions have been addressed. I'm closing the issue. Feel free to speak if I missed one point and I'll re-open it. Thanks! |
Motivation
I am testing several WebAssembly runtimes here. In my use case I will be sending and receiving JSON payloads to a function in a multi-threaded host. Currently
Imports.Append
can only use exported function pointers which are global. Unless there is some meaning tocontext unsafe.Pointer
, I cannot encapsulate the input/output JSON in a struct perInstance
.Also, It would also be nice if compilation and execution were separate steps. In a multi-threaded host, this would mean that the cost of compilation is only incurred once.
Proposed solution
This might not be possible with CGO but somehow use the
implementation interface{}
parameter ofImports.Append
as the execution target. Currently it is only used to read the parameters and return type. Another approach could be to pass theInstance
pointer ascontext
and include aUserData
field so that you can cast and delegate to a struct implementation.For separation of compilation and execution, there might be a
func wasm.NewModuleWithImports(code []byte, imports *Imports) *Module
function that returns the compiled module instructions, etc. Thenmodule.NewInstance
would return a single VM with its own memory, etc.Additional context
General question: How does the one use the
context unsafe.Pointer
parameter? I've tried casting it to*Instance
and*Memory
. Both attempts ended poorly (core dump). A sample in the README would be super helpful.The text was updated successfully, but these errors were encountered: