Commit Graph

29 Commits

Author SHA1 Message Date
Crypt Keeper
106f96b066 Adds import-go example (#466)
This shows how to define, export and import functions written in Go.

Fixes #464

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-04-15 09:31:52 +08:00
Crypt Keeper
abb3559310 Moves close responsibility to the module context (#438)
This moves the responsibility to close a module from the engine to the
context. The reason for this is that the engine is what defines the
function. When a module is closed, it is often from an imported host
function. If it is on the module engine, it is easy to accidentally
close an imported module.

This refactors the WASI tests also, to ensure they aren't cheating too
much. This allows us to know for example "proc_exit" works without too
much trouble.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-04-04 18:41:42 +08:00
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
81c2414fff wasi: exposes CloseWithExitCode to correct implementation of proc_exit (#410)
Exposes CloseWithExitCode to correct implementation of proc_exit

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-30 17:46:01 +08:00
Crypt Keeper
000cbdeb54 wasi: replaces existing filesystem apis with fs.FS (#394)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-24 15:35:17 +08:00
Takaya Saeki
a723020027 WASI: Implement fd_seek (#348)
Signed-off-by: Takaya Saeki <abc.tkys+pub@gmail.com>
2022-03-09 19:25:48 +08:00
Crypt Keeper
1e4834612a Removes host-specific types (#347)
This converges host-defined modules with Wasm defined modules by
introducing a custom section for host-defined functions. The net result
are far less types and consistent initialization.

* HostModule is removed for Module
* HostFunction is removed for Function
* ModuleContext is removed for Module

Note: One impact of this is that the low-level API no longer accepts a
go context (context.Context), rather a `wasm.Module` which the function
is called in context of. This meant exposing `wasm.Module.WithContext`
to override the default.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-08 16:43:13 +08:00
Crypt Keeper
da65ecf8d1 Stubs WASI functions for GrainLang (#312)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-03 16:47:48 +08:00
Crypt Keeper
3411795ac7 Adds wasm.Store API to get exported module and host functions (#266)
This adds this interface `wasm.Store` which gives access to functions in
a store without leaking an API to change the store. This is primarily to
support configuration use cases where post-initialization, there's no
need or desire to mutate the store. This also backfills codecs needed to
handle float results.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-02-21 13:49:00 +08:00
Takaya Saeki
79a0f010b4 wasi: Implement proc_exit (#255)
Signed-off-by: Takaya Saeki <takaya@tetrate.io>
2022-02-17 20:09:45 +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
Takaya Saeki
2f592e2c2c wasi: Implement environ_get and environ_sizes_get API (#216)
Signed-off-by: Takaya Saeki <takaya@tetrate.io>
2022-02-16 16:33:54 +09:00
Constantine
121e0e595f wasi: Implement random_get (#241) 2022-02-15 20:48:17 +09:00
Crypt Keeper
03de8c43a1 Adds host function interfaces (#246)
This adds two interfaces with only one implementation each, used to
decouple end-user code from internals.

* wasm.HostFunctionCallContext - allows access to go Context and Memory
* wasm.Memory - read/write standard types and arbitrary data

This currently ports existing code to use the interfaces. It is true
that over time we can use structs instead of interfaces internally to
save a pointer reference in WASI call sites. However, I'm choosing not
to do this right now, as it is more important to see the interfaces
work. We should defer optimizing towards structs until after we
repackage implementations under the "internal" directory.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-02-15 19:01:00 +08:00
Crypt Keeper
03a7345aac wasi: refactors existing logic and tests of clock_time_get (#224)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-02-10 17:43:52 +08:00
Crypt Keeper
e6d841c119 wasi: backfills constants and TODOs for remaining functions (#222)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-02-10 15:50:44 +08:00
Crypt Keeper
85370bec2d wasi: adds wasi.RegisterAPI and rewrites documentation to prefer go and text format (#219)
Before, we wrote WASI functions with jargon that is not typically used
in go. As this project is in go and implements a text compiler, we can
relay points more directly and specifically. This is the first example
to move to this.

This centralizes documentation through a new interface `wasi.API`
currently instantiated in `wasi.RegisterAPI(store, opts)`. Later, we
can figure out how to export the context needed to use it properly.

Co-authored-by: Takaya Saeki <takaya@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-02-09 19:52:40 +08:00
Phil Kedy
09ae4017a2 wasi: implements clock_time_get and stubs fd_seek (#188) 2022-02-04 09:47:33 +09:00
Crypt Keeper
ecfe274ba1 text: implements call instruction (#190)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-02-03 12:53:52 +08:00
Takaya Saeki
fee83c0ff6 Implement args_get and args_sizes_get (#166)
Signed-off-by: Takaya Saeki <takaya@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
Co-authored-by: Crypt Keeper <64215+codefromthecrypt@users.noreply.github.com>
2022-02-03 07:33:44 +08:00
Tim Bart
3524953201 wasi: typo fix (#157)
Small typo.
Closes #156
2022-01-22 16:00:14 +09:00
Achille
5c23040a5e wasi: introduce wasi.Errno type and switch error codes to constants (#72) 2021-12-12 15:49:23 +09:00
Crypt Keeper
046a7c2107 Renames to tetratelabs/wazero (#43)
This renames the project to wazero, which emphasizes that this runtime
has no platform depedencies (notably CGO). To avoid having users change
their imports twice, this also changes the org to "tetratelabs" ahead of
transfer.

Exciting times ahead, wazeros!

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2021-11-05 16:13:23 +08:00
Takeshi Yoneda
e982de2f94 Simplify logic around multiple memory instances. (#38) 2021-10-28 23:24:51 +09:00
Takeshi Yoneda
23df8dc412 refactor: introduce the concept of Engine (#34) 2021-10-22 17:16:11 +09:00
Takeshi Yoneda
5d555254ac Refactor / Pass Wasm Specification test suites. (#27) 2021-10-07 21:35:04 +09:00
Gaboose
71556e50c4 wasi: add stdio and file system support (#22) 2021-04-03 08:09:38 +09:00
mathetake
378c1a4dd7 refactor: hostfunc module builder 2020-05-09 14:04:58 +09:00
mathetake
52edbd17a5 initial commit 2020-05-05 21:04:23 +09:00