Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Export malloc/free by default to match TinyGo 0.28 behavior (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Jun 16, 2023
1 parent cb84f33 commit 0b11eec
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
29 changes: 29 additions & 0 deletions bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@ import (
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)

// TestExportsMalloc is in the bench package since it already provides a fully packaged binary.
// This could be cleaned up by using a different package, but for now we'll stick with being a little
// lazy.
func TestExportsMalloc(t *testing.T) {
wasm, err := os.ReadFile(filepath.Join("..", "build", "bench.wasm"))
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
r := wazero.NewRuntime(ctx)
wasi_snapshot_preview1.MustInstantiate(ctx, r)

mod, err := r.InstantiateModuleFromBinary(ctx, wasm)
if err != nil {
t.Fatal(err)
}

malloc := mod.ExportedFunction("malloc")
free := mod.ExportedFunction("free")

if malloc == nil {
t.Error("malloc is not exported")
}
if free == nil {
t.Error("free is not exported")
}
}

func BenchmarkGC(b *testing.B) {
tests := []string{"bench.wasm", "benchref.wasm"}
for _, tc := range tests {
Expand Down
4 changes: 3 additions & 1 deletion init.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright wasilibs authors
// SPDX-License-Identifier: MIT

//go:build tinygo

package nottinygc

/*
#cgo LDFLAGS: -Lwasm -lgc -lmimalloc -lclang_rt.builtins-wasm32
#cgo LDFLAGS: -Lwasm -lgc -lmimalloc -lclang_rt.builtins-wasm32 --export=malloc --export=free
*/
import "C"
22 changes: 16 additions & 6 deletions magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func Test() error {
return fmt.Errorf("unexpected error message: %s", s)
}

if err := buildBenchExecutable(); err != nil {
return err
}

if err := sh.RunV("go", "test", "./bench"); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -69,8 +77,6 @@ func Format() error {
return nil
}

var errMissingCopyrightHeaders = errors.New("missing copyright headers, use go run mage.go format")

func Lint() error {
if _, err := sh.Exec(map[string]string{}, io.Discard, io.Discard, "go", "run", fmt.Sprintf("github.com/google/addlicense@%s", verAddLicense),
"-check",
Expand All @@ -81,7 +87,7 @@ func Lint() error {
"-ignore", "**/*.yml",
"-ignore", "**/*.yaml",
"."); err != nil {
return errMissingCopyrightHeaders
return fmt.Errorf("missing copyright headers, use go run mage.go format: %w", err)
}

return sh.RunV("go", "run", fmt.Sprintf("github.com/golangci/golangci-lint/cmd/golangci-lint@%s", verGolancCILint), "run")
Expand Down Expand Up @@ -112,19 +118,23 @@ func UpdateLibs() error {

// Bench runs benchmarks.
func Bench() error {
if err := os.MkdirAll("build", 0o755); err != nil {
if err := buildBenchExecutable(); err != nil {
return err
}

if err := sh.RunV("tinygo", "build", "-scheduler=none", "-target=wasi", "-o", "build/benchref.wasm", "./bench"); err != nil {
return err
}

if err := sh.RunV("tinygo", "build", "-gc=custom", "-tags=custommalloc", "-scheduler=none", "-target=wasi", "-o", "build/bench.wasm", "./bench"); err != nil {
return sh.RunV("go", "test", "-bench=.", "-benchtime=10s", "./bench")
}

func buildBenchExecutable() error {
if err := os.MkdirAll("build", 0o755); err != nil {
return err
}

return sh.RunV("go", "test", "-bench=.", "-benchtime=10s", "./bench")
return sh.RunV("tinygo", "build", "-gc=custom", "-tags=custommalloc", "-scheduler=none", "-target=wasi", "-o", "build/bench.wasm", "./bench")
}

var Default = Test

0 comments on commit 0b11eec

Please # to comment.