Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions extism.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,10 @@ type Plugin struct {
close []func(ctx context.Context) error
extism api.Module

//Runtime *Runtime
//Main Module
module api.Module
Timeout time.Duration
Config map[string]string
// NOTE: maybe we can have some nice methods for getting/setting vars
hostFuncError error
module api.Module
Timeout time.Duration
Config map[string]string
Var map[string][]byte
AllowedHosts []string
AllowedPaths map[string]string
Expand Down
22 changes: 20 additions & 2 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ func (p *Plugin) currentPlugin() *CurrentPlugin {
return &CurrentPlugin{p}
}

// SetHostFunctionError allows the host function to set an error that will be
// gracefully returned by extism guest modules.
func (p *CurrentPlugin) SetHostFunctionError(ctx context.Context, err error) {
if err == nil {
return
}

offset, err := p.WriteString(err.Error())
if err != nil {
panic(fmt.Sprintf("failed to write error message to memory: %v", err))
}

_, err = p.plugin.extism.ExportedFunction("error_set").Call(ctx, offset)
if err != nil {
panic(fmt.Sprintf("failed to set error: %v", err))
}
}

func (p *CurrentPlugin) Log(level LogLevel, message string) {
p.plugin.Log(level, message)
}
Expand Down Expand Up @@ -237,7 +255,7 @@ func defineCustomHostFunctions(builder wazero.HostModuleBuilder, funcs []HostFun
// See: https://github.com/extism/go-sdk/issues/5#issuecomment-1666774486
closure := f.stackCallback

builder.NewFunctionBuilder().WithGoFunction(api.GoFunc(func(ctx context.Context, stack []uint64) {
builder.NewFunctionBuilder().WithGoModuleFunction(api.GoModuleFunc(func(ctx context.Context, m api.Module, stack []uint64) {
if plugin, ok := ctx.Value(PluginCtxKey("plugin")).(*Plugin); ok {
closure(ctx, &CurrentPlugin{plugin}, stack)
return
Expand Down Expand Up @@ -306,7 +324,7 @@ func instantiateEnvModule(ctx context.Context, rt wazero.Runtime) (api.Module, e
WithGoModuleFunction(api.GoModuleFunc(store_u64), []ValueType{ValueTypeI64, ValueTypeI64}, []ValueType{}).
Export("store_u64")

hostFunc := func(name string, f interface{}) {
hostFunc := func(name string, f any) {
builder.NewFunctionBuilder().WithFunc(f).Export(name)
}

Expand Down