@@ -41,67 +41,16 @@ func main() {
41
41
if err != nil {
42
42
panic (err )
43
43
}
44
- definitionsDir := filepath .Join (homeDir , "definitions" )
45
44
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" ))
60
46
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
99
48
100
49
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 " ).
105
54
Instantiate (ctx , r )
106
55
if err != nil {
107
56
panic (err )
@@ -159,6 +108,62 @@ func main() {
159
108
fmt .Println (string (docBytes ))
160
109
}
161
110
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
+
162
167
func getHomeDirectory () (string , error ) {
163
168
home , err := homedir .Dir ()
164
169
if err != nil {
0 commit comments