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

Use wypes for managing refs #22

Merged
merged 3 commits into from
Mar 8, 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
4 changes: 0 additions & 4 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,3 @@ func (i *mockInterpreter) SetModules(wypes.Modules) error {
func (i *mockInterpreter) MemoryData(ptr, sz uint32) ([]byte, error) {
return nil, nil
}

func (i *mockInterpreter) References() *ExternalReferences {
return nil
}
2 changes: 0 additions & 2 deletions engine/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,4 @@ type Interpreter interface {
SetModules(wypes.Modules) 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
}
58 changes: 0 additions & 58 deletions engine/references.go

This file was deleted.

53 changes: 0 additions & 53 deletions engine/references_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ replace github.com/tetratelabs/wazero => github.com/orsinium-forks/wazero v0.0.0

require (
github.com/hybridgroup/wasman v0.0.0-20240304140329-ce1ea6b61834
github.com/orsinium-labs/wypes v0.1.2
github.com/orsinium-labs/wypes v0.1.4
github.com/tetratelabs/wazero v1.6.0
github.com/urfave/cli/v2 v2.27.1
tinygo.org/x/tinyfs v0.3.1-0.20231212053859-32ae3f6bbad9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4 h1:MUh9e2izc
github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A=
github.com/orsinium-labs/tinytest v1.0.0 h1:YiGm/whlGm3mn/ynx9CCFuvEa3Q6yEGrzrKXEqJOkdc=
github.com/orsinium-labs/tinytest v1.0.0/go.mod h1:GwcYBp0aKi6zujzBXFpCnqw6RSLSp9JSedDyu/V1DF4=
github.com/orsinium-labs/wypes v0.1.2 h1:xa+d83u6jkGENM2I63KY1dxTC9S/D2At/rB7EsvU5QY=
github.com/orsinium-labs/wypes v0.1.2/go.mod h1:FSNWGo8I6/D5RYXMkCxyu71TXJAlwJGQUxgs4i6MAwo=
github.com/orsinium-labs/wypes v0.1.4 h1:+7oih2IDlEpz7laiL3sQFlIU8vjd5j/SwcYlPdIPc0Q=
github.com/orsinium-labs/wypes v0.1.4/go.mod h1:FSNWGo8I6/D5RYXMkCxyu71TXJAlwJGQUxgs4i6MAwo=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
Expand Down
18 changes: 0 additions & 18 deletions interp/tester/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tester
import (
"bytes"
"testing"
"unsafe"

"github.com/hybridgroup/mechanoid/engine"
)
Expand Down Expand Up @@ -86,21 +85,4 @@ func InstanceTest(t *testing.T, i engine.Interpreter) {
t.Errorf("Instance.Call() failed: %v", results)
}
})

t.Run("Call externref", func(t *testing.T) {
thing := testingType{val1: "hello", val2: "world"}

// This is a hack to get the pointer value as an int32
// Externelref is an opaque type, so we can't do anything with it
// We just want to make sure that the pointer value is passed through correctly
ptr := uintptr(unsafe.Pointer(&thing)) & 0xFFFFFFFF

results, err := inst.Call("test_externref", ptr)
if err != nil {
t.Errorf("Instance.Call() failed: %v", err)
}
if uintptr(results.(int32)) != ptr {
t.Errorf("Instance.Call() incorrect: %v %v", ptr, results)
}
})
}
55 changes: 0 additions & 55 deletions interp/tester/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ 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 @@ -60,53 +55,3 @@ 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(&thing1)
id2 = i.References().Add(&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).(*testingType).val1 != thing1.val1 {
t.Errorf("refs.Get(id1) %d failed %v %v", id1, i.References().Get(id1).(*testingType).val1, thing1.val1)
}
if i.References().Get(id2).(*testingType).val2 != thing2.val2 {
t.Errorf("refs.Get(id2) %d failed %v %v", id2, i.References().Get(id2).(*testingType).val2, thing1.val2)
}
})

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

if i.References().Get(id1) != nil {
t.Errorf("refs.Get(id1) failed")
}
if i.References().Get(id2) != nil {
t.Errorf("refs.Get(id2) failed")
}
})
}
26 changes: 4 additions & 22 deletions interp/wasman/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import (
)

type Interpreter struct {
linker *wasmaneng.Linker
module *wasmaneng.Module
instance *wasmaneng.Instance
Memory []byte
references engine.ExternalReferences
linker *wasmaneng.Linker
module *wasmaneng.Module
instance *wasmaneng.Instance
Memory []byte
}

func (i *Interpreter) Name() string {
Expand All @@ -28,20 +27,12 @@ func (i *Interpreter) Name() string {

func (i *Interpreter) Init() error {
i.linker = wasmaneng.NewLinker(config.LinkerConfig{})

// use host pre-allocated memory for instances
if i.Memory != nil {
if len(i.Memory)%65536 != 0 {
return engine.ErrInvalidMemorySize
}

if err := i.linker.DefineMemory("env", "memory", i.Memory); err != nil {
return err
}
}

i.references = engine.NewReferences()

return nil
}

Expand Down Expand Up @@ -99,12 +90,8 @@ func (i *Interpreter) Halt() error {
runtime.ReadMemStats(&ms)
mechanoid.Debug("Interpreter Halt - Heap Used: ", ms.HeapInuse, " Free: ", ms.HeapIdle, " Meta: ", ms.GCSys)
}

// clean up extern refs
i.references.Clear()
i.instance = nil
i.module = nil

// force a garbage collection to free memory
runtime.GC()

Expand Down Expand Up @@ -168,11 +155,6 @@ 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
}

func wrapValueTypes(ins []wypes.ValueType) []types.ValueType {
outs := make([]types.ValueType, 0, len(ins))
for _, in := range ins {
Expand Down
4 changes: 0 additions & 4 deletions interp/wasman/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ 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")
}
13 changes: 3 additions & 10 deletions interp/wazero/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import (
)

type Interpreter struct {
runtime wazero.Runtime
module api.Module
references engine.ExternalReferences
modules wypes.Modules
runtime wazero.Runtime
module api.Module
modules wypes.Modules
}

func (i *Interpreter) Name() string {
Expand All @@ -29,7 +28,6 @@ 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 @@ -148,11 +146,6 @@ 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: 0 additions & 4 deletions interp/wazero/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ 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")
}