Comments (9)
Thanks for opening this well prepared issue 💚 I agree in that we should tackle it.
We should pass memory (and maybe even more context) to callback functions. This is already done for DynamicFunc
s for us in Rust-land (we just chose to ignore the vm::Ctx
at the current implementation).
vm::Ctx
offers us the memory
function which we could convert into a memory object and pass down to elixir.
We need to rework how memory resources function, though. Currently it remembers the instance and, for each task, asks it for its exports, finds the WASM-land memory, asks for the specified view (datatype and offset). Instead we need to save the memory object directly in the resource.
It should not be too hard to change the existing memory code. I would suggest to tackle this in three steps:
- finish the import feature without passing memory in
- (could be in parallel) rework the existing memory resource to hold a WASM memory instead of WASM instance
- pass the memory into elixir callbacks for imported functions
About 3: I guess we have to change the method signature of elixir-callbacks here. Maybe they should receive a tuple of two elements:
- context - a map with
%{memory: Wasmex.Memory.t()}
and maybe later, WASM tables or other context-related infos) - and the normal
params
array we pass in now
@myobie what do you think?
from wasmex.
I will add a TODO to #9 to change the signature of elixir callbacks to not only receive params
but {context, params}
(where context will be the empty map for now).
This is hoping that people have an easier time migrating to the bright future of us passing memory and other context into callbacks.
from wasmex.
☝️ that being said, I think you could get around the GenServer process attempting to call itself by not using the Wasmex
GenServer interface, but implement your own - this way you would not need to call the GenServer just to retrieve the memory through a process call (you'd already have the Wasmex.Instance
in your GenServers state and could create the memory directly from it).
It is a workaround I don't like (because I want the higher-level GenServer interface to work), but it may get you a step further in your WASI experiement 🤞
from wasmex.
Your notes and suggested steps make sense and I agree finishing the import feature as is is the top priority.
I did start down the road of a custom GenServer this morning but realized it was too far.
from wasmex.
@myobie could you give the current master
another try? I added support for memory access to elixir callbacks in #16
from wasmex.
@tessi it works for my use case for random_get
. An example that works for me is:
{:ok, instance} =
Wasmex.start_link(%{
bytes: bytes,
imports: %{
wasi_snapshot_preview1: %{
random_get:
{:fn, [:i32, :i32], [:i32],
fn %{memory: memory}, address, size ->
bytes = :crypto.strong_rand_bytes(size)
bytes
|> :binary.bin_to_list()
|> Enum.with_index()
|> Enum.each(fn {byte, index} ->
IO.puts("setting #{byte} at #{address + index}")
Wasmex.Memory.set(memory, address + index, byte)
end)
0
end}
}
}
})
I can't use the function to write a binary because I don't want it to end with a null byte. I'm not sure if it's worth having two functions: one for write binary and one for write null terminated string.
from wasmex.
Good point with the string writing. After some reading and getting more proficient in Rust it became clear to me that we shouldn't rely on null-bytes in our string helpers. We should always use the memory index
and the strings length
to read or write memory (as it is done by Rust in the String
type).
Sooner or later I'll change this API to not use null-bytes, but lengths. Feel free to (but no need to) contribute :)
from wasmex.
In other news: You may have a look at #17 (and a WIP PR at #18) that adds WASI support. I plan to optionally pre-fill the imports object with WASI implementations written in Rust. They should ideally be overwritable from elixir if you want to implement some functions yourself.
Maybe this is helping you in your plans.
from wasmex.
IMO this issue can be closed now that #16 has been merged.
from wasmex.
Related Issues (20)
- Offer precompiled wasmex binaries HOT 3
- Require at least elixir 1.11 HOT 3
- Allow call_function to accept longer GenServer timeouts HOT 1
- Underlying Wasm runtime, wasmer -> wasmtime HOT 17
- Ability to cache compiled modules HOT 2
- How safe is it to run untrusted user wasm modules? HOT 2
- Example using WASI HOT 2
- Calls and execution model HOT 10
- Send and receive WASI std IO as Erlang messages HOT 1
- Switch to an async Rust runtime
- Seems like rust is a prerequisite? HOT 1
- Error during default engine config test and fuel consumption test HOT 4
- call_function example does not behave as expected HOT 2
- consume_fuel function example does not work as expected HOT 1
- unknown import: `wasi_snapshot_preview1::random_get` has not been defined HOT 4
- Compatibility with Lunatic.Solutions HOT 2
- Support for dynamic linking HOT 2
- WebAssembly component support
- Unable to run go code compiled to WASM HOT 1
- Unable to run Go code compiled for WASI HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wasmex.