Refactor.

This commit is contained in:
Nuno Cruces
2023-04-22 00:15:44 +01:00
parent 7e6d178122
commit bed2ee7674
5 changed files with 44 additions and 44 deletions

View File

@@ -1,4 +1,4 @@
package vfs
package util
import (
"context"
@@ -7,7 +7,10 @@ import (
"github.com/tetratelabs/wazero/api"
)
func registerFunc1[T0, TR ~uint32](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0) TR) {
type i32 interface{ ~int32 | ~uint32 }
type i64 interface{ ~int64 | ~uint64 }
func RegisterFuncII[TR, T0 i32](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0) TR) {
mod.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(
func(ctx context.Context, mod api.Module, stack []uint64) {
@@ -17,7 +20,7 @@ func registerFunc1[T0, TR ~uint32](mod wazero.HostModuleBuilder, name string, fn
Export(name)
}
func registerFunc2[T0, T1, TR ~uint32](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0, _ T1) TR) {
func RegisterFuncIII[TR, T0, T1 i32](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0, _ T1) TR) {
mod.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(
func(ctx context.Context, mod api.Module, stack []uint64) {
@@ -27,7 +30,7 @@ func registerFunc2[T0, T1, TR ~uint32](mod wazero.HostModuleBuilder, name string
Export(name)
}
func registerFunc3[T0, T1, T2, TR ~uint32](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0, _ T1, _ T2) TR) {
func RegisterFuncIIII[TR, T0, T1, T2 i32](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0, _ T1, _ T2) TR) {
mod.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(
func(ctx context.Context, mod api.Module, stack []uint64) {
@@ -37,7 +40,7 @@ func registerFunc3[T0, T1, T2, TR ~uint32](mod wazero.HostModuleBuilder, name st
Export(name)
}
func registerFunc4[T0, T1, T2, T3, TR ~uint32](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0, _ T1, _ T2, _ T3) TR) {
func RegisterFuncIIIII[TR, T0, T1, T2, T3 i32](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0, _ T1, _ T2, _ T3) TR) {
mod.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(
func(ctx context.Context, mod api.Module, stack []uint64) {
@@ -47,7 +50,7 @@ func registerFunc4[T0, T1, T2, T3, TR ~uint32](mod wazero.HostModuleBuilder, nam
Export(name)
}
func registerFunc5[T0, T1, T2, T3, T4, TR ~uint32](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0, _ T1, _ T2, _ T3, _ T4) TR) {
func RegisterFuncIIIIII[TR, T0, T1, T2, T3, T4 i32](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0, _ T1, _ T2, _ T3, _ T4) TR) {
mod.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(
func(ctx context.Context, mod api.Module, stack []uint64) {
@@ -57,21 +60,21 @@ func registerFunc5[T0, T1, T2, T3, T4, TR ~uint32](mod wazero.HostModuleBuilder,
Export(name)
}
func registerFuncRW(mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _, _, _ uint32, _ int64) _ErrorCode) {
func RegisterFuncIIIIJ[TR, T0, T1, T2 i32, T3 i64](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0, _ T1, _ T2, _ T3) TR) {
mod.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(
func(ctx context.Context, mod api.Module, stack []uint64) {
stack[0] = uint64(fn(ctx, mod, uint32(stack[0]), uint32(stack[1]), uint32(stack[2]), int64(stack[3])))
stack[0] = uint64(fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3])))
}),
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI64}, []api.ValueType{api.ValueTypeI32}).
Export(name)
}
func registerFuncT(mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ uint32, _ int64) _ErrorCode) {
func RegisterFuncIIJ[TR, T0 i32, T1 i64](mod wazero.HostModuleBuilder, name string, fn func(ctx context.Context, mod api.Module, _ T0, _ T1) TR) {
mod.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(
func(ctx context.Context, mod api.Module, stack []uint64) {
stack[0] = uint64(fn(ctx, mod, uint32(stack[0]), int64(stack[1])))
stack[0] = uint64(fn(ctx, mod, T0(stack[0]), T1(stack[1])))
}),
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI64}, []api.ValueType{api.ValueTypeI32}).
Export(name)

View File

@@ -40,7 +40,8 @@ func init() {
rt = wazero.NewRuntime(ctx)
wasi_snapshot_preview1.MustInstantiate(ctx, rt)
env := vfs.NewEnvModuleBuilder(rt)
env := vfs.Export(rt.NewHostModuleBuilder("env"))
env.NewFunctionBuilder().WithFunc(system).Export("system")
_, err := env.Instantiate(ctx)
if err != nil {

View File

@@ -35,7 +35,7 @@ func init() {
rt = wazero.NewRuntime(ctx)
wasi_snapshot_preview1.MustInstantiate(ctx, rt)
env := vfs.NewEnvModuleBuilder(rt)
env := vfs.Export(rt.NewHostModuleBuilder("env"))
_, err := env.Instantiate(ctx)
if err != nil {
panic(err)

View File

@@ -17,37 +17,28 @@ import (
"github.com/tetratelabs/wazero/api"
)
func Instantiate(ctx context.Context, r wazero.Runtime) {
env := NewEnvModuleBuilder(r)
_, err := env.Instantiate(ctx)
if err != nil {
panic(err)
}
}
func NewEnvModuleBuilder(r wazero.Runtime) wazero.HostModuleBuilder {
env := r.NewHostModuleBuilder("env")
registerFuncT(env, "os_localtime", vfsLocaltime)
registerFunc3(env, "os_randomness", vfsRandomness)
registerFunc2(env, "os_sleep", vfsSleep)
registerFunc2(env, "os_current_time", vfsCurrentTime)
registerFunc2(env, "os_current_time_64", vfsCurrentTime64)
registerFunc4(env, "os_full_pathname", vfsFullPathname)
registerFunc3(env, "os_delete", vfsDelete)
registerFunc4(env, "os_access", vfsAccess)
registerFunc5(env, "os_open", vfsOpen)
registerFunc1(env, "os_close", vfsClose)
registerFuncRW(env, "os_read", vfsRead)
registerFuncRW(env, "os_write", vfsWrite)
registerFuncT(env, "os_truncate", vfsTruncate)
registerFunc2(env, "os_sync", vfsSync)
registerFunc2(env, "os_file_size", vfsFileSize)
registerFunc3(env, "os_file_control", vfsFileControl)
registerFunc1(env, "os_sector_size", vfsSectorSize)
registerFunc1(env, "os_device_characteristics", vfsDeviceCharacteristics)
registerFunc2(env, "os_lock", vfsLock)
registerFunc2(env, "os_unlock", vfsUnlock)
registerFunc2(env, "os_check_reserved_lock", vfsCheckReservedLock)
func Export(env wazero.HostModuleBuilder) wazero.HostModuleBuilder {
util.RegisterFuncIIJ(env, "os_localtime", vfsLocaltime)
util.RegisterFuncIIII(env, "os_randomness", vfsRandomness)
util.RegisterFuncIII(env, "os_sleep", vfsSleep)
util.RegisterFuncIII(env, "os_current_time", vfsCurrentTime)
util.RegisterFuncIII(env, "os_current_time_64", vfsCurrentTime64)
util.RegisterFuncIIIII(env, "os_full_pathname", vfsFullPathname)
util.RegisterFuncIIII(env, "os_delete", vfsDelete)
util.RegisterFuncIIIII(env, "os_access", vfsAccess)
util.RegisterFuncIIIIII(env, "os_open", vfsOpen)
util.RegisterFuncII(env, "os_close", vfsClose)
util.RegisterFuncIIIIJ(env, "os_read", vfsRead)
util.RegisterFuncIIIIJ(env, "os_write", vfsWrite)
util.RegisterFuncIIJ(env, "os_truncate", vfsTruncate)
util.RegisterFuncIII(env, "os_sync", vfsSync)
util.RegisterFuncIII(env, "os_file_size", vfsFileSize)
util.RegisterFuncIIII(env, "os_file_control", vfsFileControl)
util.RegisterFuncII(env, "os_sector_size", vfsSectorSize)
util.RegisterFuncII(env, "os_device_characteristics", vfsDeviceCharacteristics)
util.RegisterFuncIII(env, "os_lock", vfsLock)
util.RegisterFuncIII(env, "os_unlock", vfsUnlock)
util.RegisterFuncIII(env, "os_check_reserved_lock", vfsCheckReservedLock)
return env
}

View File

@@ -52,7 +52,12 @@ func instantiateModule() (*module, error) {
func compileModule() {
ctx := context.Background()
sqlite3.runtime = wazero.NewRuntime(ctx)
vfs.Instantiate(ctx, sqlite3.runtime)
env := vfs.Export(sqlite3.runtime.NewHostModuleBuilder("env"))
_, sqlite3.err = env.Instantiate(ctx)
if sqlite3.err != nil {
return
}
bin := Binary
if bin == nil && Path != "" {