Skip to content

Commit

Permalink
engine, interp: connect ExternalReferences to Interpreters
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Feb 29, 2024
1 parent ef549c3 commit 38a3d61
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 5 deletions.
8 changes: 5 additions & 3 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package engine

import (
"testing"
)
import "testing"

func TestEngine(t *testing.T) {
t.Run("cannot init without interpreter", func(t *testing.T) {
Expand Down Expand Up @@ -53,3 +51,7 @@ func (i *mockInterpreter) DefineFunc(modulename, funcname string, f interface{})
func (i *mockInterpreter) MemoryData(ptr, sz uint32) ([]byte, error) {
return nil, nil
}

func (i *mockInterpreter) References() *ExternalReferences {
return nil
}
2 changes: 2 additions & 0 deletions engine/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ type Interpreter interface {
DefineFunc(module, name string, f interface{}) error
// MemoryData returns a slice of memory data from the memory managed by the host.
MemoryData(ptr, sz uint32) ([]byte, error)
// References are the external references managed by the host module.
References() *ExternalReferences
}
4 changes: 2 additions & 2 deletions engine/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type ExternalReferences struct {
}

// NewReferences creates a new ExternalReferences store.
func NewReferences() *ExternalReferences {
return &ExternalReferences{
func NewReferences() ExternalReferences {
return ExternalReferences{
refs: make(map[int32]uintptr),
}
}
Expand Down
59 changes: 59 additions & 0 deletions interp/tester/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tester

import (
"testing"
"unsafe"

"github.com/hybridgroup/mechanoid/engine"
)
Expand All @@ -11,6 +12,14 @@ func InitTest(t *testing.T, i engine.Interpreter) {
if err != nil {
t.Errorf("Interpreter.Init() failed: %v", err)
}

if i.Name() == "" {
t.Errorf("Interpreter.Name() failed: %v", i.Name())
}

if i.References() == nil {
t.Errorf("Interpreter.References() failed: %v", i.References())
}
}

func LoadTest(t *testing.T, i engine.Interpreter) {
Expand Down Expand Up @@ -51,3 +60,53 @@ func HaltTest(t *testing.T, i engine.Interpreter) {
t.Errorf("Interpreter.Halt() failed: %v", err)
}
}

func ReferencesTest(t *testing.T, i engine.Interpreter) {
err := i.Init()
if err != nil {
t.Errorf("Interpreter.Init() failed: %v", err)
}
if i.References() == nil {
t.Errorf("Interpreter.References() failed: %v", i.References())
}

var id1, id2 int32
thing1 := &testingType{
val1: "hello",
val2: "world",
}
thing2 := &testingType{
val1: "hola",
val2: "mundo",
}

t.Run("add references", func(t *testing.T) {
id1 = i.References().Add(unsafe.Pointer(&thing1))
id2 = i.References().Add(unsafe.Pointer(&thing2))

if id1 == id2 {
t.Errorf("id1 and id2 should not be the same")
}
})

t.Run("get references", func(t *testing.T) {
if i.References().Get(id1) != uintptr(unsafe.Pointer(&thing1)) {
t.Errorf("refs.Get(id1) failed")
}
if i.References().Get(id2) != uintptr(unsafe.Pointer(&thing2)) {
t.Errorf("refs.Get(id2) failed")
}
})

t.Run("remove references", func(t *testing.T) {
i.References().Remove(id1)
i.References().Remove(id2)

if i.References().Get(id1) != uintptr(0) {
t.Errorf("refs.Get(id1) failed")
}
if i.References().Get(id2) != uintptr(0) {
t.Errorf("refs.Get(id2) failed")
}
})
}
9 changes: 9 additions & 0 deletions interp/wasman/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Interpreter struct {
module *wasmaneng.Module
instance *wasmaneng.Instance
Memory []byte

references engine.ExternalReferences
}

func (i *Interpreter) Name() string {
Expand All @@ -34,6 +36,8 @@ func (i *Interpreter) Init() error {
}
}

i.references = engine.NewReferences()

return nil
}

Expand Down Expand Up @@ -115,3 +119,8 @@ func (i *Interpreter) MemoryData(ptr, sz uint32) ([]byte, error) {

return i.instance.Memory.Value[ptr : ptr+sz], nil
}

// References are the external references managed by the host module.
func (i *Interpreter) References() *engine.ExternalReferences {
return &i.references
}
4 changes: 4 additions & 0 deletions interp/wasman/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func TestHalt(t *testing.T) {
tester.HaltTest(t, &Interpreter{})
}

func TestExternalReferences(t *testing.T) {
tester.ReferencesTest(t, &Interpreter{})
}

func TestDefineFunc(t *testing.T) {
t.Skip("TODO: implement TestDefineFunc")
}
8 changes: 8 additions & 0 deletions interp/wazero/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Interpreter struct {
runtime wazero.Runtime
defs map[string]map[string]any
module api.Module

references engine.ExternalReferences
}

func (i *Interpreter) Name() string {
Expand All @@ -26,6 +28,7 @@ func (i *Interpreter) Init() error {
conf = conf.WithDebugInfoEnabled(false)
conf = conf.WithMemoryLimitPages(1)
i.runtime = wazero.NewRuntimeWithConfig(ctx, conf)
i.references = engine.NewReferences()
return nil
}

Expand Down Expand Up @@ -95,6 +98,11 @@ func (i *Interpreter) MemoryData(ptr, sz uint32) ([]byte, error) {
return data, nil
}

// References are the external references managed by the host module.
func (i *Interpreter) References() *engine.ExternalReferences {
return &i.references
}

// A fake RandSource for having fewer memory allocations.
//
// Should not be used with modules that do need an access to random functions.
Expand Down
4 changes: 4 additions & 0 deletions interp/wazero/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func TestHalt(t *testing.T) {
tester.HaltTest(t, &Interpreter{})
}

func TestExternalReferences(t *testing.T) {
tester.ReferencesTest(t, &Interpreter{})
}

func TestDefineFunc(t *testing.T) {
t.Skip("TODO: implement TestDefineFunc")
}

0 comments on commit 38a3d61

Please sign in to comment.