-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Arrays and TypeBuilder
- Loading branch information
1 parent
b9add37
commit 9bee683
Showing
6 changed files
with
211 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from ._binaryen import lib, ffi | ||
from .internals import ( | ||
BinaryenExpressionId, | ||
BinaryenType, | ||
BinaryenPackedType, | ||
BinaryenHeapType, | ||
) | ||
|
||
type_builder_errors = { | ||
lib.TypeBuilderErrorReasonSelfSupertype(): "Self Supertype", | ||
lib.TypeBuilderErrorReasonInvalidSupertype(): "Invalid Supertype", | ||
lib.TypeBuilderErrorReasonForwardSupertypeReference(): "Forward Supertype Reference", | ||
lib.TypeBuilderErrorReasonForwardChildReference(): "Forward Child Reference", | ||
} | ||
|
||
|
||
class TypeBuilder: | ||
def __init__(self, size: int): | ||
self.ref = lib.TypeBuilderCreate(size) | ||
|
||
def grow(self, count: int): | ||
if self.ref is None: | ||
raise RuntimeError("Cannot access TypeBuilder after it has been built.") | ||
lib.TypeBuilderGrow(self.ref, count) | ||
|
||
def get_size(self) -> int: | ||
if self.ref is None: | ||
raise RuntimeError("Cannot access TypeBuilder after it has been built.") | ||
return lib.TypeBuilderGetSize(self.ref) | ||
|
||
def set_signature_type( | ||
self, index: int, param_types: BinaryenType, result_types: BinaryenType | ||
): | ||
if self.ref is None: | ||
raise RuntimeError("Cannot access TypeBuilder after it has been built.") | ||
lib.TypeBuilderSetSignatureType(self.ref, index, param_types, result_types) | ||
|
||
# TODO: SetStructType | ||
|
||
def set_array_type( | ||
self, | ||
index: int, | ||
element_type: BinaryenType, | ||
element_packed_type: BinaryenPackedType, | ||
element_mutable: bool, | ||
): | ||
if self.ref is None: | ||
raise RuntimeError("Cannot access TypeBuilder after it has been built.") | ||
lib.TypeBuilderSetArrayType( | ||
self.ref, index, element_type, element_packed_type, element_mutable | ||
) | ||
|
||
# TODO: GetTempHeapType, GetTempTupleType, TempRefType, SetSubType, SetOpen, CreateRecGroup | ||
|
||
def build(self): | ||
if self.ref is None: | ||
raise RuntimeError("Cannot access TypeBuilder after it has been built.") | ||
|
||
size = self.get_size() | ||
heap_types = ffi.new(f"BinaryenHeapType[{size}]") | ||
error_index = ffi.new("BinaryenIndex[1]") | ||
error_reason = ffi.new("TypeBuilderErrorReason[1]") | ||
successful = lib.TypeBuilderBuildAndDispose( | ||
self.ref, heap_types, error_index, error_reason | ||
) | ||
self.ref = None | ||
|
||
if not successful: | ||
raise RuntimeError( | ||
f"TypeBuilder Error at type index {error_index[0]}: {type_builder_errors[error_reason[0]]}" | ||
) | ||
|
||
result: list[BinaryenHeapType] = [] | ||
for i in range(size): | ||
result.append(heap_types[i]) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import binaryen | ||
from binaryen.type import Int32, NotPacked | ||
|
||
|
||
mod = binaryen.Module() | ||
mod.set_feature(binaryen.Feature.GC | binaryen.Feature.ReferenceTypes) | ||
|
||
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() | ||
|
||
# mod.optimize() | ||
|
||
mod.print() | ||
mod.write_text("array.wat") | ||
mod.write_binary("array.wasm") | ||
|
||
|
||
if not mod.validate(): | ||
raise RuntimeError("Invalid module!") | ||
|
||
# Can either print with `myModule.print()` or write to file with `myModule.write_binary(__file__)` | ||
|
||
# Run the written binary with `wasmtime addTen.wasm --invoke addTen 12` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters