Comments (4)
I do think the solution you need is the one outlined in #1059 (comment).
For an "env"
module that only creates and exports a memory that your module can import, you can have a look at:
https://github.com/wasilibs/go-re2/blob/f5c90396b5b6b752f7d1fb22563f44aaa052f292/internal/re2_wazero.go#L159-L161
This uses this module built with wat2wasm
and embed with go:embed
:
https://github.com/wasilibs/go-re2/blob/f5c90396b5b6b752f7d1fb22563f44aaa052f292/internal/re2_wazero.go#L31-L34
from wazero.
I wanted to also add that as far as I can tell this is not a duplicate of #1059 since that issue is about exporting memory. Or, at least, I didn't see a way to do this in any file in the linked repo.
from wazero.
ohhh ok wow. thanks! it took me a while to understand that there are (intentionally) two different modules.
For anyone in the future, here's what I used:
(the doom-fizzbuzz.wasm
can be downloaded from the above link)
(the memory.wasm
is just a memory.wat
file with (module (memory (export "memory") 102 65536 shared))
compiled with the command in the comments before the embed)
package main
import (
"context"
_ "embed"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
func js_milliseconds_since_start() uint32 {
return uint32(0)
}
func js_console_log(arg0 uint32, arg1 uint32) {
}
func js_draw_screen(arg0 uint32) {
}
func js_stdout(arg0 uint32, arg1 uint32) {
}
func js_stderr(arg0 uint32, arg1 uint32) {
}
//go:embed doom-fizzbuzz.wasm
var wasmBytes []byte
// wat2wasm memory.wat --output=memory.wasm --enable-threads
//
//go:embed memory.wasm
var memoryWasm []byte
func main() {
ctx := context.Background()
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter().WithCoreFeatures(api.CoreFeaturesV2|experimental.CoreFeaturesThreads))
defer r.Close(ctx)
wasi_snapshot_preview1.MustInstantiate(ctx, r)
_, err := r.NewHostModuleBuilder("js").
NewFunctionBuilder().WithFunc(js_milliseconds_since_start).Export("js_milliseconds_since_start").
NewFunctionBuilder().WithFunc(js_console_log).Export("js_console_log").
NewFunctionBuilder().WithFunc(js_draw_screen).Export("js_draw_screen").
NewFunctionBuilder().WithFunc(js_stdout).Export("js_stdout").
NewFunctionBuilder().WithFunc(js_stderr).Export("js_stderr").
Instantiate(ctx)
if err != nil {
panic(err)
}
_, err = r.InstantiateWithConfig(ctx, memoryWasm, wazero.NewModuleConfig().WithName("env"))
if err != nil {
panic(err)
}
mod, err := r.Instantiate(ctx, wasmBytes)
if err != nil {
panic(err)
}
mainFunc := mod.ExportedFunction("main")
arg0 := uint64(1) // placeholder
arg1 := uint64(1) // placeholder
_, err = mainFunc.Call(ctx, arg0, arg1)
if err != nil {
panic(err)
}
}
thanks so much @ncruces!
from wazero.
Hi @dimitropoulos - for reference, I have had to switch from the static memory module approach to creating the module dynamically (being very simple, it's not too bad).
While I had initially thought virtual memory allocation has no limits, actually it can still be capped on systems with lower memory so had to make max memory dynamic.
from wazero.
Related Issues (20)
- Dynamically load WASM files HOT 1
- ` fd_fdstat_set_flags` truncates file HOT 1
- [WASI] `fd_fdstat_set_flags` with `fdflags::append` resets fd offset HOT 1
- `fd_filestat_set_size` wraps instead of error on large file size
- Inaccurate clock resolution in WASI `clock_res_get` HOT 3
- Feature Request: Rust tokio async interoperability through pollOneOff HOT 20
- Issue with reloading compiled module HOT 3
- flaky test: ExampleCoreFeaturesThreads HOT 2
- flaky test: internal/engine/wazevo.TestE2E on amd64 HOT 2
- flaky test: wasi_snapshot_preview1/wasi_stdlib_unix_test Test_NonblockingFile
- flaky test: TestFileCache_{compiler, wazevo} HOT 12
- Complex Types using WIT Supported HOT 2
- flaky test: TestPoll_Windows/poll_should_wait_for_the_given_duration
- Expose fsConfig.WithSysFSMount for custom writable FS support HOT 4
- How to concurrent call one instance's export function? HOT 2
- dirfs.OpenFile can return EEXIST even if that is not defined FS.OpenFile in the interface documentation HOT 4
- dirfs.OpenFile can return ENAMETOOLONG even if that is not defined FS.OpenFile in the interface documentation HOT 3
- flaky test: Test_pathOpen_Errors/trailing_slash_on_file on Windows HOT 1
- compiler(arm64) overflow in call relocations 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 wazero.