Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
colindickson committed Dec 12, 2023
1 parent 2f368d8 commit 34db45e
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 3 deletions.
80 changes: 80 additions & 0 deletions wasm/bench/cmd/barebones/main.go
Original file line number Diff line number Diff line change
@@ -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
}
Empty file added wasm/bench/substreams_ts/env.js
Empty file.
4 changes: 3 additions & 1 deletion wasm/bench/substreams_ts/env.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export declare function bar(): void
declare module "Env" {
export function bar(): void
}
9 changes: 9 additions & 0 deletions wasm/bench/substreams_ts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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"
Expand All @@ -5237,6 +5245,7 @@ var transferTopic = bytesFromHex(
);
function popo() {
console.log("Hello from popo!");
(void 0)();
const out = map_block(readInput());
writeOutput(out);
}
Expand Down
2 changes: 2 additions & 0 deletions wasm/bench/substreams_ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Binary file modified wasm/bench/substreams_ts/index.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion wasm/bench/substreams_ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Binary file modified wasm/bench/substreams_wasi/wasi_hello_world/hello.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions wasm/bench/substreams_wasi/wasi_hello_world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion wasm/wazero/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 34db45e

Please sign in to comment.