From 4fe6dfe2210fbec9861ae2b3dfbdbb7fc0dfd840 Mon Sep 17 00:00:00 2001 From: Crypt Keeper <64215+codefromthecrypt@users.noreply.github.com> Date: Tue, 1 Nov 2022 06:45:40 +0800 Subject: [PATCH] deps: updates wazero to 1.0.0-pre.3 (#3) Signed-off-by: Adrian Cole --- cmd/host/go.mod | 2 +- cmd/host/go.sum | 4 +- cmd/host/main.go | 119 ++++++++++++++++++++++++----------------------- 3 files changed, 65 insertions(+), 60 deletions(-) diff --git a/cmd/host/go.mod b/cmd/host/go.mod index 7594240..b73b6c4 100644 --- a/cmd/host/go.mod +++ b/cmd/host/go.mod @@ -4,5 +4,5 @@ go 1.19 require ( github.com/mitchellh/go-homedir v1.1.0 - github.com/tetratelabs/wazero v1.0.0-pre.2 + github.com/tetratelabs/wazero v1.0.0-pre.3 ) diff --git a/cmd/host/go.sum b/cmd/host/go.sum index 46c3178..c28bde0 100644 --- a/cmd/host/go.sum +++ b/cmd/host/go.sum @@ -1,4 +1,4 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/tetratelabs/wazero v1.0.0-pre.2 h1:sHYi8DKUL7s7c4sKz6lw0pNqky5EogYK0Iq4pSIsDog= -github.com/tetratelabs/wazero v1.0.0-pre.2/go.mod h1:M8UDNECGm/HVjOfq0EOe4QfCY9Les1eq54IChMLETbc= +github.com/tetratelabs/wazero v1.0.0-pre.3 h1:Z5fbogMUGcERzaQb9mQU8+yJSy0bVvv2ce3dfR4wcZg= +github.com/tetratelabs/wazero v1.0.0-pre.3/go.mod h1:M8UDNECGm/HVjOfq0EOe4QfCY9Les1eq54IChMLETbc= diff --git a/cmd/host/main.go b/cmd/host/main.go index 171dbcd..3f15688 100644 --- a/cmd/host/main.go +++ b/cmd/host/main.go @@ -41,67 +41,16 @@ func main() { if err != nil { panic(err) } - definitionsDir := filepath.Join(homeDir, "definitions") - var malloc, free api.Function - - returnString := func(m api.Module, value string) uint64 { - size := uint64(len(value)) - results, err := malloc.Call(ctx, size) - if err != nil { - panic(err) - } - - ptr := uintptr(results[0]) - - m.Memory().Write(ctx, uint32(ptr), []byte(value)) - return (uint64(ptr) << uint64(32)) | uint64(size) - } + definitions := definitions(filepath.Join(homeDir, "definitions")) - resolve := func(ctx context.Context, m api.Module, locationPtr, locationLen, fromPtr, fromLen uint32) uint64 { - locationBuf, ok := m.Memory().Read(ctx, locationPtr, locationLen) - if !ok { - return returnString(m, fmt.Sprintf("error: %v", err)) - } - location := string(locationBuf) - - loc := filepath.Join(definitionsDir, filepath.Join(strings.Split(location, "/")...)) - if filepath.Ext(loc) != ".apex" { - specLoc := loc + ".apex" - found := false - stat, err := os.Stat(specLoc) - if err == nil && !stat.IsDir() { - found = true - loc = specLoc - } - - if !found { - stat, err := os.Stat(loc) - if err != nil { - return returnString(m, fmt.Sprintf("error: %v", err)) - } - if stat.IsDir() { - loc = filepath.Join(loc, "index.apex") - } else { - loc += ".apex" - } - } - } - - data, err := os.ReadFile(loc) - if err != nil { - return returnString(m, fmt.Sprintf("error: %v", err)) - } - - source := string(data) - return returnString(m, source) - } + var malloc, free api.Function m, err := r.NewHostModuleBuilder("apex"). - ExportFunction("resolve", resolve, - "resolve", - "location_ptr", "location_len", - "from_ptr", "from_len"). + NewFunctionBuilder(). + WithFunc(definitions.resolve). + WithParameterNames("location_ptr", "location_len", "from_ptr", "from_len"). + Export("resolve"). Instantiate(ctx, r) if err != nil { panic(err) @@ -159,6 +108,62 @@ func main() { fmt.Println(string(docBytes)) } +type definitions string + +// resolve is defined as a reflective func because it isn't used frequently. +func (d definitions) resolve(ctx context.Context, m api.Module, locationPtr, locationLen, fromPtr, fromLen uint32) uint64 { + locationBuf, ok := m.Memory().Read(ctx, locationPtr, locationLen) + if !ok { + returnString(ctx, m, "out of memory") + } + location := string(locationBuf) + + loc := filepath.Join(string(d), filepath.Join(strings.Split(location, "/")...)) + if filepath.Ext(loc) != ".apex" { + specLoc := loc + ".apex" + found := false + stat, err := os.Stat(specLoc) + if err == nil && !stat.IsDir() { + found = true + loc = specLoc + } + + if !found { + stat, err := os.Stat(loc) + if err != nil { + return returnString(ctx, m, fmt.Sprintf("error: %v", err)) + } + if stat.IsDir() { + loc = filepath.Join(loc, "index.apex") + } else { + loc += ".apex" + } + } + } + + data, err := os.ReadFile(loc) + if err != nil { + returnString(ctx, m, fmt.Sprintf("error: %v", err)) + } + + source := string(data) + return returnString(ctx, m, source) +} + +func returnString(ctx context.Context, m api.Module, value string) uint64 { + size := uint64(len(value)) + results, err := m.ExportedFunction("_malloc").Call(ctx, size) + if err != nil { + panic(err) + } + + ptr := uintptr(results[0]) + + m.Memory().Write(ctx, uint32(ptr), []byte(value)) + ptrSize := (uint64(ptr) << uint64(32)) | uint64(size) + return ptrSize +} + func getHomeDirectory() (string, error) { home, err := homedir.Dir() if err != nil {