Removes api.ImportRenamer for a different approach to "env" (#680)

Before, we introduced a type `api.ImportRenamer` to resolve conflicts
where the "env" module was shared between AssemblyScript and
user-defined functions. This API was never used in GitHub, and is
complicated.

AssemblyScript also isn't the only ABI to share the "env" module, as
other web APIs like Emscripten do also. The less complicated approach is
to have packages that need to share "env" use
`ModuleBuilder.ExportFunctions` instead, and use namespaces as needed if
there is overlap.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-07-11 14:57:26 +08:00
committed by GitHub
parent 09a8aa6030
commit 14d892d310
14 changed files with 322 additions and 493 deletions

View File

@@ -1 +0,0 @@
replace-import

View File

@@ -1,4 +0,0 @@
## Replace import example
This example shows how to override a module name hard-coded in a WebAssembly
module. This is similar to what some tools call "linking".

View File

@@ -1,65 +0,0 @@
package main
import (
"context"
_ "embed"
"fmt"
"log"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
// needsImportWasm was generated by the following:
// cd testdata; wat2wasm --debug-names needs_import.wat
//go:embed testdata/needs_import.wasm
var needsImportWasm []byte
// main shows how to override a module or function name hard-coded in a
// WebAssembly module. This is similar to what some tools call "linking".
func main() {
// Choose the context to use for function calls.
ctx := context.Background()
// Create a new WebAssembly Runtime.
r := wazero.NewRuntime()
defer r.Close(ctx) // This closes everything this Runtime created.
// Instantiate a Go-defined module named "assemblyscript" that exports a
// function to close the module that calls "abort".
host, err := r.NewModuleBuilder("assemblyscript").
ExportFunction("abort", func(ctx context.Context, m api.Module, messageOffset, fileNameOffset, line, col uint32) {
_ = m.CloseWithExitCode(ctx, 255)
}).Instantiate(ctx, r)
if err != nil {
log.Panicln(err)
}
defer host.Close(ctx)
// Compile the WebAssembly module, replacing the import "env.abort" with
// "assemblyscript.abort".
compileConfig := wazero.NewCompileConfig().
WithImportRenamer(func(externType api.ExternType, oldModule, oldName string) (newModule, newName string) {
if oldModule == "env" && oldName == "abort" {
return "assemblyscript", "abort"
}
return oldModule, oldName
})
code, err := r.CompileModule(ctx, needsImportWasm, compileConfig)
if err != nil {
log.Panicln(err)
}
defer code.Close(ctx)
// Instantiate the WebAssembly module.
mod, err := r.InstantiateModule(ctx, code, wazero.NewModuleConfig())
if err != nil {
log.Panicln(err)
}
defer mod.Close(ctx)
// Since the above worked, the exported function closes the module.
_, err = mod.ExportedFunction("abort").Call(ctx, 0, 0, 0, 0)
fmt.Println(err)
}

View File

@@ -1,16 +0,0 @@
package main
import (
"testing"
"github.com/tetratelabs/wazero/internal/testing/maintester"
"github.com/tetratelabs/wazero/internal/testing/require"
)
// Test_main ensures the following will work:
//
// go run replace-import.go
func Test_main(t *testing.T) {
stdout, _ := maintester.TestMain(t, main, "replace-import")
require.Equal(t, "module \"needs-import\" closed with exit_code(255)\n", stdout)
}

View File

@@ -1,3 +0,0 @@
(module $needs-import
(func (export "abort") (import "env" "abort") (param i32 i32 i32 i32))
)