From 95ef1315c6fea2de25ee4bef9f858d3cd471e46f Mon Sep 17 00:00:00 2001 From: Physcik Date: Tue, 16 Dec 2025 17:25:58 +0500 Subject: Wasm -> go --- Makefile | 3 +-- c_wasm/hello.c | 6 ++++++ hello.c | 3 --- inter/main.go | 16 +++++++++++++++- 4 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 c_wasm/hello.c delete mode 100644 hello.c diff --git a/Makefile b/Makefile index 1f14348..496a2f2 100644 --- a/Makefile +++ b/Makefile @@ -2,5 +2,4 @@ all: build_wasm cd inter; go run . build_wasm: - emcc hello.c -sEXPORTED_FUNCTIONS="_Add" -o hello.wasm --no-entry - + emcc c_wasm/hello.c -sEXPORTED_FUNCTIONS="_Add" -s ERROR_ON_UNDEFINED_SYMBOLS=0 -o hello.wasm --no-entry diff --git a/c_wasm/hello.c b/c_wasm/hello.c new file mode 100644 index 0000000..696b1fb --- /dev/null +++ b/c_wasm/hello.c @@ -0,0 +1,6 @@ +extern void xdx(); + +int Add(int a, int b) { + xdx(); + return a + b; +} diff --git a/hello.c b/hello.c deleted file mode 100644 index 09d79ce..0000000 --- a/hello.c +++ /dev/null @@ -1,3 +0,0 @@ -int Add(int a, int b) { - return a + b; -} diff --git a/inter/main.go b/inter/main.go index e0dd0a7..ee0d463 100644 --- a/inter/main.go +++ b/inter/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "log" "os" "github.com/tetratelabs/wazero" @@ -17,6 +18,15 @@ func main() { var runtime = wazero.NewRuntime(ctx) defer runtime.Close(ctx) + var _, hostErr = runtime.NewHostModuleBuilder("env"). + NewFunctionBuilder(). + WithFunc(xdx). + Export("xdx"). + Instantiate(ctx); + if hostErr != nil { + panic(fmt.Sprintf("Failed to create host module: %s", hostErr.Error())) + } + wasi_snapshot_preview1.MustInstantiate(ctx, runtime) var wasm, wasmErr = LoadWasm() @@ -30,7 +40,7 @@ func main() { } var add = mod.ExportedFunction("Add") - var result, callErr = add.Call(ctx, 25, 25) + var result, callErr = add.Call(ctx, 9, 10) if callErr != nil { panic(fmt.Sprintf("Failed to call WASM finction: %s", callErr.Error())) } @@ -40,3 +50,7 @@ func main() { func LoadWasm() ([]byte, error) { return os.ReadFile(wasmLocation) } + +func xdx() { + log.Printf("xdx (called from WASM)") +} -- cgit v1.3