Skip to content

Commit 4fe6dfe

Browse files
deps: updates wazero to 1.0.0-pre.3 (#3)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
1 parent a66676c commit 4fe6dfe

File tree

3 files changed

+65
-60
lines changed

3 files changed

+65
-60
lines changed

cmd/host/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.19
44

55
require (
66
github.com/mitchellh/go-homedir v1.1.0
7-
github.com/tetratelabs/wazero v1.0.0-pre.2
7+
github.com/tetratelabs/wazero v1.0.0-pre.3
88
)

cmd/host/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
22
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
3-
github.com/tetratelabs/wazero v1.0.0-pre.2 h1:sHYi8DKUL7s7c4sKz6lw0pNqky5EogYK0Iq4pSIsDog=
4-
github.com/tetratelabs/wazero v1.0.0-pre.2/go.mod h1:M8UDNECGm/HVjOfq0EOe4QfCY9Les1eq54IChMLETbc=
3+
github.com/tetratelabs/wazero v1.0.0-pre.3 h1:Z5fbogMUGcERzaQb9mQU8+yJSy0bVvv2ce3dfR4wcZg=
4+
github.com/tetratelabs/wazero v1.0.0-pre.3/go.mod h1:M8UDNECGm/HVjOfq0EOe4QfCY9Les1eq54IChMLETbc=

cmd/host/main.go

+62-57
Original file line numberDiff line numberDiff line change
@@ -41,67 +41,16 @@ func main() {
4141
if err != nil {
4242
panic(err)
4343
}
44-
definitionsDir := filepath.Join(homeDir, "definitions")
4544

46-
var malloc, free api.Function
47-
48-
returnString := func(m api.Module, value string) uint64 {
49-
size := uint64(len(value))
50-
results, err := malloc.Call(ctx, size)
51-
if err != nil {
52-
panic(err)
53-
}
54-
55-
ptr := uintptr(results[0])
56-
57-
m.Memory().Write(ctx, uint32(ptr), []byte(value))
58-
return (uint64(ptr) << uint64(32)) | uint64(size)
59-
}
45+
definitions := definitions(filepath.Join(homeDir, "definitions"))
6046

61-
resolve := func(ctx context.Context, m api.Module, locationPtr, locationLen, fromPtr, fromLen uint32) uint64 {
62-
locationBuf, ok := m.Memory().Read(ctx, locationPtr, locationLen)
63-
if !ok {
64-
return returnString(m, fmt.Sprintf("error: %v", err))
65-
}
66-
location := string(locationBuf)
67-
68-
loc := filepath.Join(definitionsDir, filepath.Join(strings.Split(location, "/")...))
69-
if filepath.Ext(loc) != ".apex" {
70-
specLoc := loc + ".apex"
71-
found := false
72-
stat, err := os.Stat(specLoc)
73-
if err == nil && !stat.IsDir() {
74-
found = true
75-
loc = specLoc
76-
}
77-
78-
if !found {
79-
stat, err := os.Stat(loc)
80-
if err != nil {
81-
return returnString(m, fmt.Sprintf("error: %v", err))
82-
}
83-
if stat.IsDir() {
84-
loc = filepath.Join(loc, "index.apex")
85-
} else {
86-
loc += ".apex"
87-
}
88-
}
89-
}
90-
91-
data, err := os.ReadFile(loc)
92-
if err != nil {
93-
return returnString(m, fmt.Sprintf("error: %v", err))
94-
}
95-
96-
source := string(data)
97-
return returnString(m, source)
98-
}
47+
var malloc, free api.Function
9948

10049
m, err := r.NewHostModuleBuilder("apex").
101-
ExportFunction("resolve", resolve,
102-
"resolve",
103-
"location_ptr", "location_len",
104-
"from_ptr", "from_len").
50+
NewFunctionBuilder().
51+
WithFunc(definitions.resolve).
52+
WithParameterNames("location_ptr", "location_len", "from_ptr", "from_len").
53+
Export("resolve").
10554
Instantiate(ctx, r)
10655
if err != nil {
10756
panic(err)
@@ -159,6 +108,62 @@ func main() {
159108
fmt.Println(string(docBytes))
160109
}
161110

111+
type definitions string
112+
113+
// resolve is defined as a reflective func because it isn't used frequently.
114+
func (d definitions) resolve(ctx context.Context, m api.Module, locationPtr, locationLen, fromPtr, fromLen uint32) uint64 {
115+
locationBuf, ok := m.Memory().Read(ctx, locationPtr, locationLen)
116+
if !ok {
117+
returnString(ctx, m, "out of memory")
118+
}
119+
location := string(locationBuf)
120+
121+
loc := filepath.Join(string(d), filepath.Join(strings.Split(location, "/")...))
122+
if filepath.Ext(loc) != ".apex" {
123+
specLoc := loc + ".apex"
124+
found := false
125+
stat, err := os.Stat(specLoc)
126+
if err == nil && !stat.IsDir() {
127+
found = true
128+
loc = specLoc
129+
}
130+
131+
if !found {
132+
stat, err := os.Stat(loc)
133+
if err != nil {
134+
return returnString(ctx, m, fmt.Sprintf("error: %v", err))
135+
}
136+
if stat.IsDir() {
137+
loc = filepath.Join(loc, "index.apex")
138+
} else {
139+
loc += ".apex"
140+
}
141+
}
142+
}
143+
144+
data, err := os.ReadFile(loc)
145+
if err != nil {
146+
returnString(ctx, m, fmt.Sprintf("error: %v", err))
147+
}
148+
149+
source := string(data)
150+
return returnString(ctx, m, source)
151+
}
152+
153+
func returnString(ctx context.Context, m api.Module, value string) uint64 {
154+
size := uint64(len(value))
155+
results, err := m.ExportedFunction("_malloc").Call(ctx, size)
156+
if err != nil {
157+
panic(err)
158+
}
159+
160+
ptr := uintptr(results[0])
161+
162+
m.Memory().Write(ctx, uint32(ptr), []byte(value))
163+
ptrSize := (uint64(ptr) << uint64(32)) | uint64(size)
164+
return ptrSize
165+
}
166+
162167
func getHomeDirectory() (string, error) {
163168
home, err := homedir.Dir()
164169
if err != nil {

0 commit comments

Comments
 (0)