Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

engine: add LoadAndRun method to reduce needed code to support most typical usage #23

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,36 @@ var (
ErrInvalidMemorySize = errors.New("engine: invalid memory size")
)

// Engine is the main struct for the Mechanoid engine.
type Engine struct {
Interpreter Interpreter
FileStore FileStore
Devices []Device
}

// NewEngine returns a new Engine.
func NewEngine() *Engine {
return &Engine{
Devices: []Device{},
}
}

// UseInterpreter sets the Interpreter for the Engine.
func (e *Engine) UseInterpreter(interp Interpreter) {
e.Interpreter = interp
}

// UseFileStore sets the FileStore for the Engine.
func (e *Engine) UseFileStore(fs FileStore) {
e.FileStore = fs
}

// AddDevice adds a Device to the Engine.
func (e *Engine) AddDevice(d Device) {
e.Devices = append(e.Devices, d)
}

// Init initializes the Engine by initializing the Interpreter and all Devices.
func (e *Engine) Init() error {
if e.Interpreter == nil {
return ErrNoInterpreter
Expand Down Expand Up @@ -70,3 +76,25 @@ func (e *Engine) Init() error {
}
return nil
}

// LoadAndRun loads and runs some WASM code using the current Interpreter.
// It returns an error if the Engine has no Interpreter, or if the Interpreter
// fails to load or run the module.
func (e *Engine) LoadAndRun(code Reader) (Instance, error) {
if e.Interpreter == nil {
return nil, ErrNoInterpreter
}

mechanoid.Debug("loading WASM code")
if err := e.Interpreter.Load(code); err != nil {
return nil, err
}

mechanoid.Debug("running WASM code")
ins, err := e.Interpreter.Run()
if err != nil {
return nil, err
}

return ins, nil
}
19 changes: 19 additions & 0 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package engine

import (
"bytes"
"testing"

_ "embed"

"github.com/orsinium-labs/wypes"
)

//go:embed tester.wasm
var wasmCode []byte

func TestEngine(t *testing.T) {
t.Run("cannot init without interpreter", func(t *testing.T) {
e := NewEngine()
Expand All @@ -23,6 +29,19 @@ func TestEngine(t *testing.T) {
t.Errorf("Engine.Init() failed: %v", err)
}
})

t.Run("can LoadAndRun", func(t *testing.T) {
e := NewEngine()
e.UseInterpreter(&mockInterpreter{})
err := e.Init()
if err != nil {
t.Errorf("Engine.Init() failed: %v", err)
}

if _, err := e.LoadAndRun(bytes.NewReader(wasmCode)); err != nil {
t.Errorf("Engine.LoadAndRun() failed: %v", err)
}
})
}

type mockInterpreter struct {
Expand Down
Binary file added engine/tester.wasm
Binary file not shown.