Skip to content

Commit

Permalink
Exposed extism module and added methods to get exported functions (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmcc authored Oct 7, 2024
1 parent 34ceed6 commit bdb398e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
21 changes: 8 additions & 13 deletions extism.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ import (
"github.com/tetratelabs/wazero/sys"
)

type module struct {
module api.Module
wasm []byte
}

type PluginCtxKey string
type InputOffsetKey string

Expand Down Expand Up @@ -119,8 +114,8 @@ func (l LogLevel) String() string {
// Plugin is used to call WASM functions
type Plugin struct {
Runtime *Runtime
Modules map[string]module
Main module
Modules map[string]Module
Main Module
Timeout time.Duration
Config map[string]string
// NOTE: maybe we can have some nice methods for getting/setting vars
Expand Down Expand Up @@ -424,7 +419,7 @@ func NewPlugin(
return nil, fmt.Errorf("manifest can't be empty")
}

modules := map[string]module{}
modules := map[string]Module{}

// NOTE: this is only necessary for guest modules because
// host modules have the same access privileges as the host itself
Expand Down Expand Up @@ -500,7 +495,7 @@ func NewPlugin(
return nil, err
}

modules[data.Name] = module{module: m, wasm: data.Data}
modules[data.Name] = Module{inner: m}
}

i := 0
Expand All @@ -514,7 +509,7 @@ func NewPlugin(
varMax = int64(manifest.Memory.MaxVarBytes)
}
for _, m := range modules {
if m.module.Name() == "main" {
if m.inner.Name() == "main" {
p := &Plugin{
Runtime: &c,
Modules: modules,
Expand Down Expand Up @@ -619,9 +614,9 @@ func (plugin *Plugin) GetErrorWithContext(ctx context.Context) string {
return string(mem)
}

// FunctionExists returns true when the named function is present in the plugin's main module
// FunctionExists returns true when the named function is present in the plugin's main Module
func (plugin *Plugin) FunctionExists(name string) bool {
return plugin.Main.module.ExportedFunction(name) != nil
return plugin.Main.inner.ExportedFunction(name) != nil
}

// Call a function by name with the given input, returning the output
Expand All @@ -646,7 +641,7 @@ func (plugin *Plugin) CallWithContext(ctx context.Context, name string, data []b

ctx = context.WithValue(ctx, InputOffsetKey("inputOffset"), intputOffset)

var f = plugin.Main.module.ExportedFunction(name)
var f = plugin.Main.inner.ExportedFunction(name)

if f == nil {
return 1, []byte{}, fmt.Errorf("unknown function: %s", name)
Expand Down
30 changes: 30 additions & 0 deletions module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package extism

import "github.com/tetratelabs/wazero/api"

// Module is a wrapper around a wazero module. It allows us to provide
// our own API and stability guarantees despite any changes that wazero
// may choose to make.
type Module struct {
inner api.Module
}

// ExportedFunctions returns a map of functions exported from the module
// keyed by the function name.
func (m *Module) ExportedFunctions() map[string]FunctionDefinition {
v := make(map[string]FunctionDefinition)
for name, def := range m.inner.ExportedFunctionDefinitions() {
v[name] = FunctionDefinition{inner: def}
}
return v
}

// FunctionDefinition represents a function defined in a module. It provides
// a wrapper around the underlying wazero function definition.
type FunctionDefinition struct {
inner api.FunctionDefinition
}

func (f *FunctionDefinition) Name() string {
return f.inner.Name()
}
4 changes: 2 additions & 2 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ type guestRuntime struct {
func detectGuestRuntime(ctx context.Context, p *Plugin) guestRuntime {
m := p.Main

runtime, ok := haskellRuntime(ctx, p, m.module)
runtime, ok := haskellRuntime(ctx, p, m.inner)
if ok {
return runtime
}

runtime, ok = wasiRuntime(ctx, p, m.module)
runtime, ok = wasiRuntime(ctx, p, m.inner)
if ok {
return runtime
}
Expand Down

0 comments on commit bdb398e

Please sign in to comment.