Skip to content

Commit

Permalink
deps: updates wazero and wastime to latest (#46)
Browse files Browse the repository at this point in the history
* deps: updates wazero to 1.0.0-pre.4

This updates [wazero](https://wazero.io/) to [1.0.0-pre.4](https://github.com/tetratelabs/wazero/releases/tag/v1.0.0-pre.4).

Notably, v1.0.0-pre.4:
* improves module initialization speed
* supports listeners in the compiler engine
* supports WASI `fd_pread`, `fd_readdir` and `path_filestat_get`
* breaks GoModuleFunc API

Signed-off-by: Adrian Cole <[email protected]>

* update linter

Signed-off-by: Adrian Cole <[email protected]>

Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
codefromthecrypt authored Dec 1, 2022
1 parent abb4885 commit 02ea3db
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.29
version: v1.50.1

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
75 changes: 28 additions & 47 deletions engines/wazero/wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ import (

const i32 = api.ValueTypeI32

var (
resultZero = []uint64{0}
resultTrue = []uint64{1}
resultFalse = resultZero
)

// functionStart is the name of the nullary function a module exports if it is a WASI Command Module.
//
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/design/application-abi.md#current-unstable-abi
Expand Down Expand Up @@ -216,19 +210,20 @@ func instantiateWapcHost(ctx context.Context, r wazero.Runtime, callHandler wapc

// hostCall is the WebAssembly function export "__host_call", which initiates a host using the callHandler using
// parameters read from linear memory (wasm.Memory).
func (w *wapcHost) hostCall(ctx context.Context, m api.Module, params []uint64) []uint64 {
bindPtr := uint32(params[0])
bindLen := uint32(params[1])
nsPtr := uint32(params[2])
nsLen := uint32(params[3])
cmdPtr := uint32(params[4])
cmdLen := uint32(params[5])
payloadPtr := uint32(params[6])
payloadLen := uint32(params[7])
func (w *wapcHost) hostCall(ctx context.Context, m api.Module, stack []uint64) {
bindPtr := uint32(stack[0])
bindLen := uint32(stack[1])
nsPtr := uint32(stack[2])
nsLen := uint32(stack[3])
cmdPtr := uint32(stack[4])
cmdLen := uint32(stack[5])
payloadPtr := uint32(stack[6])
payloadLen := uint32(stack[7])

ic := fromInvokeContext(ctx)
if ic == nil || w.callHandler == nil {
return resultFalse // there's neither an invocation context, nor a callHandler
stack[0] = 0 // false: neither an invocation context, nor a callHandler
return
}

mem := m.Memory()
Expand All @@ -238,29 +233,27 @@ func (w *wapcHost) hostCall(ctx context.Context, m api.Module, params []uint64)
payload := requireRead(ctx, mem, "payload", payloadPtr, payloadLen)

if ic.hostResp, ic.hostErr = w.callHandler(ctx, binding, namespace, operation, payload); ic.hostErr != nil {
return resultFalse // there was an error (assumed to be logged already?)
stack[0] = 0 // false: error (assumed to be logged already?)
} else {
stack[0] = 1 // true
}

return resultTrue
}

// consoleLog is the WebAssembly function export "__console_log", which logs the message stored by the guest at the
// given offset (ptr) and length (len) in linear memory (wasm.Memory).
func (w *wapcHost) consoleLog(ctx context.Context, m api.Module, params []uint64) (_ []uint64) {
func (w *wapcHost) consoleLog(ctx context.Context, m api.Module, params []uint64) {
ptr := uint32(params[0])
len := uint32(params[1])

if log := w.logger; log != nil {
msg := requireReadString(ctx, m.Memory(), "msg", ptr, len)
w.logger(msg)
}

return
}

// guestRequest is the WebAssembly function export "__guest_request", which writes the invokeContext.operation and
// invokeContext.guestReq to the given offsets (opPtr, ptr) in linear memory (wasm.Memory).
func (w *wapcHost) guestRequest(ctx context.Context, m api.Module, params []uint64) (_ []uint64) {
func (w *wapcHost) guestRequest(ctx context.Context, m api.Module, params []uint64) {
opPtr := uint32(params[0])
ptr := uint32(params[1])

Expand All @@ -276,42 +269,37 @@ func (w *wapcHost) guestRequest(ctx context.Context, m api.Module, params []uint
if guestReq := ic.guestReq; guestReq != nil {
mem.Write(ctx, ptr, guestReq)
}

return
}

// hostResponse is the WebAssembly function export "__host_response", which writes the invokeContext.hostResp to the
// given offset (ptr) in linear memory (wasm.Memory).
func (w *wapcHost) hostResponse(ctx context.Context, m api.Module, params []uint64) (_ []uint64) {
func (w *wapcHost) hostResponse(ctx context.Context, m api.Module, params []uint64) {
ptr := uint32(params[0])

if ic := fromInvokeContext(ctx); ic == nil {
return // no invoke context
} else if hostResp := ic.hostResp; hostResp != nil {
m.Memory().Write(ctx, ptr, hostResp)
}

return
}

// hostResponse is the WebAssembly function export "__host_response_len", which returns the length of the current host
// response from invokeContext.hostResp.
func (w *wapcHost) hostResponseLen(ctx context.Context, _ []uint64) []uint64 {
func (w *wapcHost) hostResponseLen(ctx context.Context, results []uint64) {
var hostResponseLen uint32
if ic := fromInvokeContext(ctx); ic == nil {
return resultZero // no invoke context
results[0] = 0 // no invoke context
} else if hostResp := ic.hostResp; hostResp != nil {
hostResponseLen = uint32(len(hostResp))
results[0] = uint64(hostResponseLen)
} else {
return resultZero // no host response
results[0] = 0 // no host response
}

return []uint64{uint64(hostResponseLen)}
}

// guestResponse is the WebAssembly function export "__guest_response", which reads invokeContext.guestResp from the
// given offset (ptr) and length (len) in linear memory (wasm.Memory).
func (w *wapcHost) guestResponse(ctx context.Context, m api.Module, params []uint64) (_ []uint64) {
func (w *wapcHost) guestResponse(ctx context.Context, m api.Module, params []uint64) {
ptr := uint32(params[0])
len := uint32(params[1])

Expand All @@ -320,13 +308,11 @@ func (w *wapcHost) guestResponse(ctx context.Context, m api.Module, params []uin
} else {
ic.guestResp = requireRead(ctx, m.Memory(), "guestResp", ptr, len)
}

return
}

// guestError is the WebAssembly function export "__guest_error", which reads invokeContext.guestErr from the given
// offset (ptr) and length (len) in linear memory (wasm.Memory).
func (w *wapcHost) guestError(ctx context.Context, m api.Module, params []uint64) (_ []uint64) {
func (w *wapcHost) guestError(ctx context.Context, m api.Module, params []uint64) {
ptr := uint32(params[0])
len := uint32(params[1])

Expand All @@ -335,37 +321,32 @@ func (w *wapcHost) guestError(ctx context.Context, m api.Module, params []uint64
} else {
ic.guestErr = requireReadString(ctx, m.Memory(), "guestErr", ptr, len)
}

return
}

// hostError is the WebAssembly function export "__host_error", which writes the invokeContext.hostErr to the given
// offset (ptr) in linear memory (wasm.Memory).
func (w *wapcHost) hostError(ctx context.Context, m api.Module, params []uint64) (_ []uint64) {
func (w *wapcHost) hostError(ctx context.Context, m api.Module, params []uint64) {
ptr := uint32(params[0])

if ic := fromInvokeContext(ctx); ic == nil {
return // no invoke context
} else if hostErr := ic.hostErr; hostErr != nil {
m.Memory().Write(ctx, ptr, []byte(hostErr.Error()))
}

return
}

// hostError is the WebAssembly function export "__host_error_len", which returns the length of the current host error
// from invokeContext.hostErr.
func (w *wapcHost) hostErrorLen(ctx context.Context, _ []uint64) []uint64 {
func (w *wapcHost) hostErrorLen(ctx context.Context, results []uint64) {
var hostErrorLen uint32
if ic := fromInvokeContext(ctx); ic == nil {
return resultZero // no invoke context
results[0] = 0 // no invoke context
} else if hostErr := ic.hostErr; hostErr != nil {
hostErrorLen = uint32(len(hostErr.Error()))
results[0] = uint64(hostErrorLen)
} else {
return resultZero // no host error
results[0] = 0 // no host error
}

return []uint64{uint64(hostErrorLen)}
}

// Instantiate implements the same method as documented on wapc.Module.
Expand Down
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func host(_ context.Context, binding, namespace, operation string, payload []byt
switch operation {
case "capitalize":
name := string(payload)
name = strings.Title(name)
name = strings.Title(name) // nolint
return []byte(name), nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ go 1.18
require (
github.com/Workiva/go-datastructures v1.0.53
github.com/bytecodealliance/wasmtime-go v1.0.0
github.com/tetratelabs/wazero v1.0.0-pre.3
github.com/tetratelabs/wazero v1.0.0-pre.4
github.com/wasmerio/wasmer-go v1.0.4
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/tetratelabs/wazero v1.0.0-pre.3 h1:Z5fbogMUGcERzaQb9mQU8+yJSy0bVvv2ce3dfR4wcZg=
github.com/tetratelabs/wazero v1.0.0-pre.3/go.mod h1:M8UDNECGm/HVjOfq0EOe4QfCY9Les1eq54IChMLETbc=
github.com/tetratelabs/wazero v1.0.0-pre.4 h1:RBJQT5OzmORkSp6MmZDWoFEr0zXjk4pmvMKAdeUnsaI=
github.com/tetratelabs/wazero v1.0.0-pre.4/go.mod h1:u8wrFmpdrykiFK0DFPiFm5a4+0RzsdmXYVtijBKqUVo=
github.com/tinylib/msgp v1.1.5/go.mod h1:eQsjooMTnV42mHu917E26IogZ2930nFyBQdofk10Udg=
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q=
github.com/wasmerio/wasmer-go v1.0.4 h1:MnqHoOGfiQ8MMq2RF6wyCeebKOe84G88h5yv+vmxJgs=
Expand Down
2 changes: 1 addition & 1 deletion hello/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/wapc/wapc-go/hello

go 1.17

require github.com/wapc/wapc-guest-tinygo v0.3.2
require github.com/wapc/wapc-guest-tinygo v0.3.3
4 changes: 2 additions & 2 deletions hello/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/wapc/wapc-guest-tinygo v0.3.2 h1:b9zkLL+44dxL8MWjZmdG6XAcO6ITK5Xzgok0eKzu/vY=
github.com/wapc/wapc-guest-tinygo v0.3.2/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw=
github.com/wapc/wapc-guest-tinygo v0.3.3 h1:jLebiwjVSHLGnS+BRabQ6+XOV7oihVWAc05Hf1SbeR0=
github.com/wapc/wapc-guest-tinygo v0.3.3/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw=
2 changes: 1 addition & 1 deletion testdata/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/wapc/wapc-go/testdata/go

go 1.17

require github.com/wapc/wapc-guest-tinygo v0.3.2
require github.com/wapc/wapc-guest-tinygo v0.3.3
4 changes: 2 additions & 2 deletions testdata/go/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/wapc/wapc-guest-tinygo v0.3.2 h1:b9zkLL+44dxL8MWjZmdG6XAcO6ITK5Xzgok0eKzu/vY=
github.com/wapc/wapc-guest-tinygo v0.3.2/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw=
github.com/wapc/wapc-guest-tinygo v0.3.3 h1:jLebiwjVSHLGnS+BRabQ6+XOV7oihVWAc05Hf1SbeR0=
github.com/wapc/wapc-guest-tinygo v0.3.3/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw=

0 comments on commit 02ea3db

Please sign in to comment.