Commit Graph

12 Commits

Author SHA1 Message Date
Crypt Keeper
2664b1eb62 Simplifies API per feedback (#427)
During #425, @neilalexander gave constructive feedback that the API is
both moving fast, and not good enough yet. This attempts to reduce the
incidental complexity at the cost of a little conflation.

### odd presence of `wasm` and `wasi` packages -> `api` package

We had public API packages in wasm and wasi, which helped us avoid
leaking too many internals as public. That these had names that look
like there should be implementations in them cause unnecessary
confusion. This squashes both into one package "api" which has no
package collission with anything.

We've long struggled with the poorly specified and non-uniformly
implemented WASI specification. Trying to bring visibility to its
constraints knowing they are routinely invalid taints our API for no
good reason. This removes all `WASI` commands for a default to invoke
the function `_start` if it exists. In doing so, there's only one path
to start a module.

Moreover, this puts all wasi code in a top-level package "wasi" as it
isn't re-imported by any internal types.

### Reuse of Module for pre and post instantiation to `Binary` -> `Module`

Module is defined by WebAssembly in many phases, from decoded to
instantiated. However, using the same noun in multiple packages is very
confusing. We at one point tried a name "DecodedModule" or
"InstantiatedModule", but this is a fools errand. By deviating slightly
from the spec we can make it unambiguous what a module is.

This make a result of compilation a `Binary`, retaining `Module` for an
instantiated one. In doing so, there's no longer any name conflicts
whatsoever.

### Confusion about config -> `ModuleConfig`

Also caused by splitting wasm into wasm+wasi is configuration. This
conflates both into the same type `ModuleConfig` as it is simpler than
trying to explain a "will never be finished" api of wasi snapshot-01 in
routine use of WebAssembly. In other words, this further moves WASI out
of the foreground as it has been nothing but burden.

```diff
--- a/README.md
+++ b/README.md
@@ -49,8 +49,8 @@ For example, here's how you can allow WebAssembly modules to read
-wm, err := r.InstantiateModule(wazero.WASISnapshotPreview1())
-defer wm.Close()
+wm, err := wasi.InstantiateSnapshotPreview1(r)
+defer wm.Close()

-sysConfig := wazero.NewSysConfig().WithFS(os.DirFS("/work/home"))
-module, err := wazero.StartWASICommandWithConfig(r, compiled, sysConfig)
+config := wazero.ModuleConfig().WithFS(os.DirFS("/work/home"))
+module, err := r.InstantiateModule(binary, config)
 defer module.Close()
 ...
```
2022-04-02 06:42:36 +08:00
Crypt Keeper
3c3df6b836 Updates to latest wabt and pares back to 1.0 (#430)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-01 15:09:37 +08:00
Crypt Keeper
5b35bd9b66 Makes filesystem test simpler and removes use of panic in examples (#395)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-19 20:01:55 +08:00
Crypt Keeper
a8944f6098 Adds StartWASICommandWithConfig to defer configuration (#367)
StartWASICommandWithConfig is like StartWASICommand, except you can override configuration based on the importing
module. For example, you can use this to define different args depending on the importing module.

```go
// Initialize base configuration:
r := wazero.NewRuntime()
config := wazero.NewWASIConfig().WithStdout(buf)
wasi, _ := r.NewHostModule(wazero.WASISnapshotPreview1WithConfig(config))
decoded, _ := r.CompileModule(source)

// Assign configuration only when ready to instantiate.
module, _ := StartWASICommandWithConfig(r, decoded, config.WithArgs("rotate", "angle=90", "dir=cw"))
```

This also changes the config to be immutable since it may be reused many
times. Instead of assigning fields directly, use `WithXXX`.

Finally, this changes the examples to encourage using `.Close()`

Fixes #364

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-14 14:39:53 +08:00
Takeshi Yoneda
e8406de84a Simplify host_call example (#314)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-03 15:32:48 +09:00
Takeshi Yoneda
490f830096 jit(arm64): restore callee-saved registers on exits from native code (#311)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-03-02 20:51:05 +09:00
Crypt Keeper
5180d2d9c3 Refactors out public API from internals (#238)
This moves to a new end-user API under the root package `wazero`. This
simplifies call sites while hardening function calls to their known
return value. Most importantly, this moves most logic internal, as
noted in the RATIONALE.md.

Ex.

```go
	// Read WebAssembly binary containing an exported "fac" function.
	source, _ := os.ReadFile("./tests/engine/testdata/fac.wasm")

	// Decode the binary as WebAssembly module.
	mod, _ := wazero.DecodeModuleBinary(source)

	// Initialize the execution environment called "store" with Interpreter-based engine.
	store := wazero.NewStore()

	// Instantiate the module, which returns its exported functions
	functions, _ := store.Instantiate(mod)

	// Get the factorial function
	fac, _ := functions.GetFunctionI64Return("fac")

	// Discover 7! is 5040
	fmt.Println(fac(context.Background(), 7))

```

PS I changed the README to factorial because the wat version of
fibonacci is not consistent with the TinyGo one!

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takaya Saeki <takaya@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-02-17 17:39:28 +08:00
Phil Kedy
4819d72876 propagate Go context into HostFunctionCallContext (#203)
Signed-off-by: Phil Kedy <phil.kedy@gmail.com>
2022-02-07 12:53:20 +08:00
Takeshi Yoneda
5f621fc59c Update TinyGo to 0.22.0 (#167) 2022-02-04 14:12:30 +09:00
Takeshi Yoneda
34e48d98fe Move e2e tests in JIT under tests dir (#181)
This PR generalizes e2e tests in jit/engine_test.go and moves them
tests/adhoc directory to run these tests against interpreter as well.

Notably this removes all the testdata directory from wasm/*
and helps isolates the usage of raw binary or texts into examples,
bench and tests directory.

During the course of generalization, I found the inconsistency on the
panic handling between JIT and Interpreter which seems a bug of latter
so I fixed it.
2022-02-02 14:23:35 +09:00
Takeshi Yoneda
9beec85a76 Delete simple.wasm (#112) 2022-01-03 10:58:16 +09:00
Takeshi Yoneda
26ee5eb735 Rename Go source dirs for Wasms to testdata. (#106)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2021-12-31 17:33:03 +09:00