Skip to content

Commit

Permalink
Addressed feedback on API breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmcc authored and zshipko committed Nov 8, 2024
1 parent 5209719 commit a3a0c61
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 126 deletions.
36 changes: 18 additions & 18 deletions extism.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func (l LogLevel) String() string {
return s
}

// InstantiatedPlugin is used to call WASM functions
type InstantiatedPlugin struct {
// Plugin is used to call WASM functions
type Plugin struct {
close []func(ctx context.Context) error
extism api.Module

Expand Down Expand Up @@ -134,16 +134,16 @@ func logStd(level LogLevel, message string) {
log.Print(message)
}

func (p *InstantiatedPlugin) Module() *Module {
func (p *Plugin) Module() *Module {
return &Module{inner: p.module}
}

// SetLogger sets a custom logging callback
func (p *InstantiatedPlugin) SetLogger(logger func(LogLevel, string)) {
func (p *Plugin) SetLogger(logger func(LogLevel, string)) {
p.log = logger
}

func (p *InstantiatedPlugin) Log(level LogLevel, message string) {
func (p *Plugin) Log(level LogLevel, message string) {
minimumLevel := LogLevel(pluginLogLevel.Load())

// If the global log level hasn't been set, use LogLevelOff as default
Expand All @@ -156,7 +156,7 @@ func (p *InstantiatedPlugin) Log(level LogLevel, message string) {
}
}

func (p *InstantiatedPlugin) Logf(level LogLevel, format string, args ...any) {
func (p *Plugin) Logf(level LogLevel, format string, args ...any) {
message := fmt.Sprintf(format, args...)
p.Log(level, message)
}
Expand Down Expand Up @@ -339,12 +339,12 @@ func (m *Manifest) UnmarshalJSON(data []byte) error {
}

// Close closes the plugin by freeing the underlying resources.
func (p *InstantiatedPlugin) Close(ctx context.Context) error {
func (p *Plugin) Close(ctx context.Context) error {
return p.CloseWithContext(ctx)
}

// CloseWithContext closes the plugin by freeing the underlying resources.
func (p *InstantiatedPlugin) CloseWithContext(ctx context.Context) error {
func (p *Plugin) CloseWithContext(ctx context.Context) error {
for _, fn := range p.close {
if err := fn(ctx); err != nil {
return err
Expand All @@ -362,12 +362,12 @@ func SetLogLevel(level LogLevel) {
}

// SetInput sets the input data for the plugin to be used in the next WebAssembly function call.
func (p *InstantiatedPlugin) SetInput(data []byte) (uint64, error) {
func (p *Plugin) SetInput(data []byte) (uint64, error) {
return p.SetInputWithContext(context.Background(), data)
}

// SetInputWithContext sets the input data for the plugin to be used in the next WebAssembly function call.
func (p *InstantiatedPlugin) SetInputWithContext(ctx context.Context, data []byte) (uint64, error) {
func (p *Plugin) SetInputWithContext(ctx context.Context, data []byte) (uint64, error) {
_, err := p.extism.ExportedFunction("reset").Call(ctx)
if err != nil {
fmt.Println(err)
Expand All @@ -384,12 +384,12 @@ func (p *InstantiatedPlugin) SetInputWithContext(ctx context.Context, data []byt
}

// GetOutput retrieves the output data from the last WebAssembly function call.
func (p *InstantiatedPlugin) GetOutput() ([]byte, error) {
func (p *Plugin) GetOutput() ([]byte, error) {
return p.GetOutputWithContext(context.Background())
}

// GetOutputWithContext retrieves the output data from the last WebAssembly function call.
func (p *InstantiatedPlugin) GetOutputWithContext(ctx context.Context) ([]byte, error) {
func (p *Plugin) GetOutputWithContext(ctx context.Context) ([]byte, error) {
outputOffs, err := p.extism.ExportedFunction("output_offset").Call(ctx)
if err != nil {
return []byte{}, err
Expand All @@ -409,17 +409,17 @@ func (p *InstantiatedPlugin) GetOutputWithContext(ctx context.Context) ([]byte,
}

// Memory returns the plugin's WebAssembly memory interface.
func (p *InstantiatedPlugin) Memory() api.Memory {
func (p *Plugin) Memory() api.Memory {
return p.extism.ExportedMemory("memory")
}

// GetError retrieves the error message from the last WebAssembly function call, if any.
func (p *InstantiatedPlugin) GetError() string {
func (p *Plugin) GetError() string {
return p.GetErrorWithContext(context.Background())
}

// GetErrorWithContext retrieves the error message from the last WebAssembly function call.
func (p *InstantiatedPlugin) GetErrorWithContext(ctx context.Context) string {
func (p *Plugin) GetErrorWithContext(ctx context.Context) string {
errOffs, err := p.extism.ExportedFunction("error_get").Call(ctx)
if err != nil {
return ""
Expand All @@ -439,17 +439,17 @@ func (p *InstantiatedPlugin) GetErrorWithContext(ctx context.Context) string {
}

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

// Call a function by name with the given input, returning the output
func (p *InstantiatedPlugin) Call(name string, data []byte) (uint32, []byte, error) {
func (p *Plugin) Call(name string, data []byte) (uint32, []byte, error) {
return p.CallWithContext(context.Background(), name, data)
}

// Call a function by name with the given input and context, returning the output
func (p *InstantiatedPlugin) CallWithContext(ctx context.Context, name string, data []byte) (uint32, []byte, error) {
func (p *Plugin) CallWithContext(ctx context.Context, name string, data []byte) (uint32, []byte, error) {
ctx = context.WithValue(ctx, PluginCtxKey("extism"), p.extism)
if p.Timeout > 0 {
var cancel context.CancelFunc
Expand Down
Loading

0 comments on commit a3a0c61

Please sign in to comment.