Skip to content

Commit

Permalink
deps: updates wazero to 1.0.0-pre.3 (#3)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
codefromthecrypt committed Oct 31, 2022
1 parent a66676c commit 4fe6dfe
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 60 deletions.
2 changes: 1 addition & 1 deletion cmd/host/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions cmd/host/go.sum
Original file line number Diff line number Diff line change
@@ -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=
119 changes: 62 additions & 57 deletions cmd/host/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4fe6dfe

Please sign in to comment.