Skip to content

Commit

Permalink
chore: remove env shims (#36)
Browse files Browse the repository at this point in the history
Now that we have support for the new namespace in every pdk and sdk, we
can safely remove the polyfills for `env`
  • Loading branch information
mhmd-azeez authored Nov 7, 2023
1 parent 5d88f9a commit 9beb289
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 39 deletions.
7 changes: 1 addition & 6 deletions extism.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func NewPlugin(
hostModules[f.Namespace] = append(hostModules[f.Namespace], f)
}

env, err := buildEnvModule(ctx, rt, extism, hostModules["env"])
env, err := buildEnvModule(ctx, rt, extism)
if err != nil {
return nil, err
}
Expand All @@ -270,11 +270,6 @@ func NewPlugin(
}

for name, funcs := range hostModules {
// `env` host functions are handled by `buildEnvModule
if name == "env" {
continue
}

_, err := buildHostModule(c.ctx, c.Wazero, name, funcs)
if err != nil {
return nil, err
Expand Down
34 changes: 1 addition & 33 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,8 @@ func defineCustomHostFunctions(builder wazero.HostModuleBuilder, funcs []HostFun
}
}

func buildEnvModule(ctx context.Context, rt wazero.Runtime, extism api.Module, funcs []HostFunction) (api.Module, error) {
func buildEnvModule(ctx context.Context, rt wazero.Runtime, extism api.Module) (api.Module, error) {
builder := rt.NewHostModuleBuilder("extism:host/env")
envBuilder := rt.NewHostModuleBuilder("env")

wrap := func(name string, params []ValType, results []ValType) {
f := extism.ExportedFunction(name)
Expand All @@ -236,17 +235,6 @@ func buildEnvModule(ctx context.Context, rt wazero.Runtime, extism api.Module, f
}
}), params, results).
Export(name)

// Register the functions in the `env` namespace too for backward compatibility
envBuilder.
NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(func(ctx context.Context, m api.Module, stack []uint64) {
err := f.CallWithStack(ctx, stack)
if err != nil {
panic(err)
}
}), params, results).
Export(fmt.Sprintf("extism_%s", name))
}

wrap("alloc", []ValType{I64}, []ValType{I64})
Expand All @@ -270,29 +258,16 @@ func buildEnvModule(ctx context.Context, rt wazero.Runtime, extism api.Module, f
WithGoModuleFunction(api.GoModuleFunc(api.GoModuleFunc(inputLoad_u64)), []ValType{I64}, []ValType{I64}).
Export("input_load_u64")

envBuilder.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(inputLoad_u64), []ValType{I64}, []ValType{I64}).
Export("extism_input_load_u64")

builder.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(load_u64), []ValType{I64}, []ValType{I64}).
Export("load_u64")

envBuilder.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(load_u64), []ValType{I64}, []ValType{I64}).
Export("extism_load_u64")

builder.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(store_u64), []ValType{I64, I64}, []ValType{}).
Export("store_u64")

envBuilder.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(store_u64), []ValType{I64, I64}, []ValType{}).
Export("extism_store_u64")

hostFunc := func(name string, f interface{}) {
builder.NewFunctionBuilder().WithFunc(f).Export(name)
envBuilder.NewFunctionBuilder().WithFunc(f).Export(fmt.Sprintf("extism_%s", name))
}

hostFunc("config_get", configGet)
Expand All @@ -301,8 +276,6 @@ func buildEnvModule(ctx context.Context, rt wazero.Runtime, extism api.Module, f
hostFunc("http_request", httpRequest)
hostFunc("http_status_code", httpStatusCode)

defineCustomHostFunctions(envBuilder, funcs)

logFunc := func(name string, level LogLevel) {
hostFunc(name, func(ctx context.Context, m api.Module, offset uint64) {
if plugin, ok := ctx.Value("plugin").(*Plugin); ok {
Expand All @@ -325,11 +298,6 @@ func buildEnvModule(ctx context.Context, rt wazero.Runtime, extism api.Module, f
logFunc("log_warn", Warn)
logFunc("log_error", Error)

_, err := envBuilder.Instantiate(ctx)
if err != nil {
return nil, err
}

return builder.Instantiate(ctx)
}

Expand Down
Binary file modified wasm/hello_haskell.wasm
Binary file not shown.

0 comments on commit 9beb289

Please sign in to comment.