-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ac081a8
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module wasiplaytime | ||
|
||
go 1.21.5 | ||
|
||
require ( | ||
github.com/davidmdm/x/xcontext v0.0.1 | ||
github.com/tetratelabs/wazero v1.6.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davidmdm/x/xcontext v0.0.1 h1:ktCEEBaJvSr7q0/chXxQbrJymkzRgnrxFXCrs/givkk= | ||
github.com/davidmdm/x/xcontext v0.0.1/go.mod h1:lfAacRD741WjdRwwuTrCaKtvEgPsGMSlmfwgib2SpVA= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= | ||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
github.com/tetratelabs/wazero v1.6.0 h1:z0H1iikCdP8t+q341xqepY4EWvHEw8Es7tlqiVzlP3g= | ||
github.com/tetratelabs/wazero v1.6.0/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
fmt.Println("hello world!") | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
_ "embed" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"syscall" | ||
|
||
"github.com/davidmdm/x/xcontext" | ||
"github.com/tetratelabs/wazero" | ||
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" | ||
) | ||
|
||
func main() { | ||
if err := run(); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// main is an example of how to extend a Go application with an addition | ||
// function defined in WebAssembly. | ||
// | ||
// Since addWasm was compiled with TinyGo's `wasi` target, we need to configure | ||
// WASI host imports. | ||
func run() error { | ||
// Parse positional arguments. | ||
flag.Parse() | ||
|
||
wasm, err := os.ReadFile(flag.Arg(0)) | ||
if err != nil { | ||
return fmt.Errorf("failed to read wasm program: %w", err) | ||
} | ||
|
||
// Choose the context to use for function calls. | ||
ctx, done := xcontext.WithSignalCancelation(context.Background(), syscall.SIGINT) | ||
defer done() | ||
|
||
cfg := wazero. | ||
NewRuntimeConfig(). | ||
WithCloseOnContextDone(true) | ||
|
||
// Create a new WebAssembly Runtime. | ||
wasi := wazero.NewRuntimeWithConfig(ctx, cfg) | ||
defer wasi.Close(ctx) // This closes everything this Runtime created. | ||
|
||
wasi_snapshot_preview1.MustInstantiate(ctx, wasi) | ||
|
||
// Because we are running a binary directly rather than embedding in an application, | ||
// we default to wiring up commonly used OS functionality. | ||
mod, err := wasi.CompileModule(ctx, wasm) | ||
if err != nil { | ||
return fmt.Errorf("failed to compile module: %w", err) | ||
} | ||
|
||
moduleCfg := wazero. | ||
NewModuleConfig(). | ||
WithStdout(os.Stdout). | ||
WithStderr(os.Stderr). | ||
WithStdin(os.Stdin). | ||
WithRandSource(rand.Reader). | ||
WithSysNanosleep(). | ||
WithSysNanotime(). | ||
WithSysWalltime(). | ||
WithArgs(append([]string{flag.Arg(0)}, flag.Args()[1:]...)...) | ||
|
||
if _, err := wasi.InstantiateModule(ctx, mod, moduleCfg); err != nil { | ||
return fmt.Errorf("failed to instantiate module: %w", err) | ||
} | ||
|
||
return nil | ||
} |