-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray.py
More file actions
36 lines (29 loc) · 880 Bytes
/
array.py
File metadata and controls
36 lines (29 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import binaryen
from binaryen.type import Int32, NotPacked
# Enable WasmGC and reference types
mod = binaryen.Module()
mod.set_feature(binaryen.Feature.GC | binaryen.Feature.ReferenceTypes)
# Create the Array type
tb = binaryen.TypeBuilder(1)
tb.set_array_type(0, Int32, NotPacked, True)
Int32ArrayHeap = tb.build()[0]
Int32Array = binaryen.type.from_heap_type(Int32ArrayHeap, True)
contents = mod.array_new(Int32ArrayHeap, mod.i32(10), mod.i32(0))
set_array = mod.local_set(0, contents.copy(mod))
array = mod.local_get(0, Int32Array)
mod.add_function(
b"indexArray",
None,
Int32,
[Int32Array],
mod.block(
None,
[set_array, array, mod.Return(mod.array_len(array))],
Int32,
),
)
mod.add_function_export(b"indexArray", b"indexArray")
mod.auto_drop()
if not mod.validate():
raise RuntimeError("Invalid module!")
mod.print()