diff --git a/wasm/bench/cmd/barebones/main.go b/wasm/bench/cmd/barebones/main.go index 06ab7d0f9..584d6fb8d 100644 --- a/wasm/bench/cmd/barebones/main.go +++ b/wasm/bench/cmd/barebones/main.go @@ -1 +1,81 @@ package main + +import ( + "context" + "fmt" + "github.com/tetratelabs/wazero/api" + "log" + "os" + + "github.com/tetratelabs/wazero" + "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" + "github.com/tetratelabs/wazero/sys" +) + +// main writes an input file to stdout, just like `cat`. +// +// This is a basic introduction to the WebAssembly System Interface (WASI). +// See https://github.com/WebAssembly/WASI +func main() { + // Choose the context to use for function calls. + ctx := context.Background() + + // Create a new WebAssembly Runtime. + runtime := wazero.NewRuntime(ctx) + defer runtime.Close(ctx) // This closes everything this Runtime created. + + reader, err := os.Open("/Users/colindickson/code/dfuse/substreams/wasm/bench/cmd/barebones/testdata/block.binpb") + if err != nil { + panic(err) + } + defer reader.Close() + + // Combine the above into our baseline config, overriding defaults. + config := wazero.NewModuleConfig(). + // By default, I/O streams are discarded and there's no file system. + WithStdout(os.Stdout).WithStderr(os.Stderr).WithStdin(reader) + config = config.WithStartFunctions("popo") + + // Instantiate WASI, which implements system I/O such as console output. + wasi_snapshot_preview1.MustInstantiate(ctx, runtime) + + build := runtime.NewHostModuleBuilder("env") + build.NewFunctionBuilder(). + WithGoModuleFunction(api.GoModuleFunc(func(ctx context.Context, mod api.Module, stack []uint64) { + fmt.Println("hello world") + }), nil, nil). + WithName("bar"). + Export("bar") + patateModule, err := build.Compile(ctx) + if err != nil { + panic(err) + } + apiMod, err := runtime.InstantiateModule(ctx, patateModule, config.WithName("env")) + if err != nil { + panic(err) + } + _ = apiMod + + catWasm := readCode("/Users/colindickson/code/dfuse/substreams/wasm/bench/substreams_ts/index.wasm") + + // InstantiateModule runs the "_start" function, WASI's "main". + // * Set the program name (arg[0]) to "wasi"; arg[1] should be "/test.txt". + if _, err := runtime.InstantiateWithConfig(ctx, catWasm, config); err != nil { + // Note: Most compilers do not exit the module after running "_start", + // unless there was an error. This allows you to call exported functions. + if exitErr, ok := err.(*sys.ExitError); ok && exitErr.ExitCode() != 0 { + fmt.Fprintf(os.Stderr, "exit_code: %d\n", exitErr.ExitCode()) + } else if !ok { + log.Panicln(err) + } + } +} + +func readCode(filename string) []byte { + content, err := os.ReadFile(filename) + if err != nil { + panic(err) + } + + return content +} diff --git a/wasm/bench/substreams_ts/env.js b/wasm/bench/substreams_ts/env.js new file mode 100644 index 000000000..e69de29bb diff --git a/wasm/bench/substreams_ts/env.ts b/wasm/bench/substreams_ts/env.ts index 190586946..df2a4070b 100644 --- a/wasm/bench/substreams_ts/env.ts +++ b/wasm/bench/substreams_ts/env.ts @@ -1 +1,3 @@ -export declare function bar(): void \ No newline at end of file +declare module "Env" { + export function bar(): void +} diff --git a/wasm/bench/substreams_ts/index.js b/wasm/bench/substreams_ts/index.js index e1557e60f..6d3d5bad1 100644 --- a/wasm/bench/substreams_ts/index.js +++ b/wasm/bench/substreams_ts/index.js @@ -754,6 +754,13 @@ var require_bigInt = __commonJS({ } }); +// env.ts +var require_env = __commonJS({ + "env.ts"() { + "use strict"; + } +}); + // index.ts var import_bigInt = __toESM(require_bigInt()); @@ -5228,6 +5235,7 @@ _Field.fields = proto3.util.newFieldList(() => [ var Field = _Field; // index.ts +var Env = __toESM(require_env()); var rocketAddress = bytesFromHex("0xae78736Cd615f374D3085123A210448E74Fc6393"); var approvalTopic = bytesFromHex( "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925" @@ -5237,6 +5245,7 @@ var transferTopic = bytesFromHex( ); function popo() { console.log("Hello from popo!"); + (void 0)(); const out = map_block(readInput()); writeOutput(out); } diff --git a/wasm/bench/substreams_ts/index.ts b/wasm/bench/substreams_ts/index.ts index b81f1a3bd..4b3453938 100644 --- a/wasm/bench/substreams_ts/index.ts +++ b/wasm/bench/substreams_ts/index.ts @@ -17,10 +17,12 @@ const transferTopic = bytesFromHex( ) // @ts-ignore +import * as Env from "./env"; export function popo() { console.log("Hello from popo!") + Env.bar() const out = map_block(readInput()) writeOutput(out) diff --git a/wasm/bench/substreams_ts/index.wasm b/wasm/bench/substreams_ts/index.wasm index 1ec4fa301..bd59fde5e 100644 Binary files a/wasm/bench/substreams_ts/index.wasm and b/wasm/bench/substreams_ts/index.wasm differ diff --git a/wasm/bench/substreams_ts/package.json b/wasm/bench/substreams_ts/package.json index ddfdb67fe..d1ef44897 100644 --- a/wasm/bench/substreams_ts/package.json +++ b/wasm/bench/substreams_ts/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "", "scripts": { - "build": "esbuild --bundle index.ts --format=esm --outdir=. --target=es2020 && javy compile index.js --wit index.wit -n index-world -o ./index.wasm" + "build": "esbuild --bundle env.ts index.ts --format=esm --outdir=. --target=es2020 && javy compile index.js --wit index.wit -n index-world -o ./index.wasm" }, "repository": { "type": "git", diff --git a/wasm/bench/substreams_wasi/wasi_hello_world/hello.wasm b/wasm/bench/substreams_wasi/wasi_hello_world/hello.wasm index f97f2613f..eaafa5234 100755 Binary files a/wasm/bench/substreams_wasi/wasi_hello_world/hello.wasm and b/wasm/bench/substreams_wasi/wasi_hello_world/hello.wasm differ diff --git a/wasm/bench/substreams_wasi/wasi_hello_world/src/main.rs b/wasm/bench/substreams_wasi/wasi_hello_world/src/main.rs index 8c36cd7e4..c2f888b9e 100644 --- a/wasm/bench/substreams_wasi/wasi_hello_world/src/main.rs +++ b/wasm/bench/substreams_wasi/wasi_hello_world/src/main.rs @@ -25,6 +25,10 @@ fn main() { #[no_mangle] pub extern "C" fn map_block(blk_ptr: *mut u8, blk_len: usize) { println!("Hello world! {}", blk_len); + + // let mut file = fs::File::create("/helloworld/helloworld.txt").unwrap(); + // write!(file, "Hello world!\n").unwrap(); + let mut buf = Vec::with_capacity(50000); let ptr = buf.as_mut_ptr(); unsafe { diff --git a/wasm/wazero/module.go b/wasm/wazero/module.go index 998bd9451..1981982c4 100644 --- a/wasm/wazero/module.go +++ b/wasm/wazero/module.go @@ -78,8 +78,10 @@ func newModule(ctx context.Context, wasmCode []byte, wasmCodeType string, regist // return nil, fmt.Errorf("missing required functions: dealloc") //} + wazConfig := wazero.NewModuleConfig() + return &Module{ - wazModuleConfig: wazero.NewModuleConfig(), + wazModuleConfig: wazConfig, wazRuntime: runtime, userModule: mod, hostModules: hostModules,