Code Monkey home page Code Monkey logo

Comments (4)

ncruces avatar ncruces commented on June 15, 2024 1

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.

dimitropoulos avatar dimitropoulos commented on June 15, 2024

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.

dimitropoulos avatar dimitropoulos commented on June 15, 2024

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.

anuraaga avatar anuraaga commented on June 15, 2024

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).

wasilibs/go-re2@9f8aa89

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.