You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-**malloc/free equivalents**: The WASM backend uses `vcalloc()` and `malloc()` from `vlib/builtin/wasm/builtin.v`
183
+
-`vcalloc(n)` - allocate zeroed memory, already implemented using WASM `memory.fill`
184
+
-`malloc(n)` - allocate uninitialized memory (needs implementation or import)
185
+
-`free(ptr)` - deallocate memory (needs implementation or stub for now)
186
+
- Integration with WASM linear memory model and heap management
187
+
188
+
2.**Array Allocation Helpers**:
132
189
- The WASM backend needs to call builtin functions:
133
190
-`__new_array(len, cap, element_size)` - allocate new array
134
191
-`__new_array_with_default(len, cap, element_size, default_val)` - with default
192
+
-`__new_array_with_multi_default(len, cap, element_size, default_val)` - for complex types
135
193
- These are defined in `vlib/builtin/array.v`
194
+
- Need to ensure these functions are available in WASM context
136
195
137
-
2.**Field Access**:
196
+
3.**Field Access Helpers**:
138
197
- Add helper functions to access array struct fields:
139
-
-`load_array_len()` - get length field
140
-
-`load_array_cap()` - get capacity field
141
-
-`load_array_data()` - get data pointer
142
-
-`store_array_len()` - set length field
143
-
144
-
3.**Array Reallocation**:
145
-
- Implement or call `array_ensure_cap()` for growth operations
146
-
- Handle memory copying during reallocation
198
+
-`load_array_len(arr_ptr)` - get length field (offset +8 bytes)
199
+
-`load_array_cap(arr_ptr)` - get capacity field (offset +12 bytes)
200
+
-`load_array_data(arr_ptr)` - get data pointer (offset +0 bytes)
201
+
-`load_array_offset(arr_ptr)` - get offset field (offset +4 bytes)
202
+
-`store_array_len(arr_ptr, len)` - set length field
203
+
- Field offsets based on struct layout: data(0), offset(4), len(8), cap(12), flags(16), element_size(20)
204
+
205
+
4.**Array Reallocation and Growth**:
206
+
- Implement or call `array_ensure_cap(arr, required_cap)` for growth operations
207
+
- Growth strategy (from `vlib/builtin/array.v`):
208
+
```
209
+
new_cap = if cap < required { required } else { cap * 2 }
210
+
```
211
+
- Handle memory copying during reallocation:
212
+
- Allocate new data block with `vcalloc(new_cap * element_size)`
213
+
- Copy existing data using `vmemcpy(new_data, old_data, len * element_size)`
214
+
- Update array struct fields (data, cap)
215
+
- Free old data block (when memory management is available)
216
+
- Respect `ArrayFlags.nogrow` flag - error if growth attempted when set
147
217
148
218
#### 1.2.4 Runtime Support (`vlib/builtin/wasm/`)
149
219
@@ -157,7 +227,41 @@ ast.Array {
157
227
- WASM-specific array helper functions
158
228
- Optimized versions of common operations
159
229
160
-
### 1.3 Module Layer (`vlib/wasm/`)
230
+
### 1.3 Integration Points in `gen.v`
231
+
232
+
**Multiple locations in `gen.v` need updates to handle dynamic arrays**:
233
+
234
+
1. **Line ~755**: Remove error, add Array handling in IndexExpr
235
+
```v
236
+
ast.Array {
237
+
// Currently: g.w_error('wasm backend does not support dynamic arrays')
238
+
// Change to: Handle array indexing with bounds checking (see section 1.2.2)
239
+
}
240
+
```
241
+
242
+
2.**Line ~723**: ArrayInit expression handling
243
+
- Already partially implemented for fixed arrays
244
+
- Extend to call `__new_array()` for dynamic arrays
245
+
246
+
3.**Function calls involving arrays**:
247
+
- Passing arrays as parameters (pass pointer to array struct)
248
+
- Returning arrays from functions (return pointer)
249
+
- Array assignments (copy pointer, not deep copy unless clone())
250
+
251
+
4.**Array field access in structs**:
252
+
- When struct contains array field, store as array struct
253
+
- Load/store entire array struct (24-28 bytes)
254
+
255
+
5.**Array comparisons**:
256
+
-`arr1 == arr2` should compare elements, not pointers
257
+
- May need to call array comparison helper
258
+
259
+
6.**Array in expressions**:
260
+
- Binary operations involving arrays
261
+
- Array concatenation
262
+
- Array slicing `arr[start..end]`
263
+
264
+
### 1.4 Module Layer (`vlib/wasm/`)
161
265
162
266
**No changes required** - The `wasm` module is for generating WASM bytecode and already supports all necessary instructions (memory operations, function calls, etc.)
0 commit comments