diff options
-rw-r--r-- | src/binaryen-c.cpp | 56 | ||||
-rw-r--r-- | src/binaryen-c.h | 17 | ||||
-rw-r--r-- | test/example/c-api-kitchen-sink.c | 34 | ||||
-rw-r--r-- | test/example/c-api-kitchen-sink.txt | 2 |
4 files changed, 106 insertions, 3 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 06c71f5a5..5fd5be5e5 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -336,6 +336,62 @@ BinaryenHeapType BinaryenHeapTypeGetBottom(BinaryenHeapType heapType) { bool BinaryenHeapTypeIsSubType(BinaryenHeapType left, BinaryenHeapType right) { return HeapType::isSubType(HeapType(left), HeapType(right)); } +BinaryenIndex BinaryenStructTypeGetNumFields(BinaryenHeapType heapType) { + auto ht = HeapType(heapType); + assert(ht.isStruct()); + return ht.getStruct().fields.size(); +} +BinaryenType BinaryenStructTypeGetFieldType(BinaryenHeapType heapType, + BinaryenIndex index) { + auto ht = HeapType(heapType); + assert(ht.isStruct()); + auto& fields = ht.getStruct().fields; + assert(index < fields.size()); + return fields[index].type.getID(); +} +BinaryenPackedType +BinaryenStructTypeGetFieldPackedType(BinaryenHeapType heapType, + BinaryenIndex index) { + auto ht = HeapType(heapType); + assert(ht.isStruct()); + auto& fields = ht.getStruct().fields; + assert(index < fields.size()); + return fields[index].packedType; +} +bool BinaryenStructTypeIsFieldMutable(BinaryenHeapType heapType, + BinaryenIndex index) { + auto ht = HeapType(heapType); + assert(ht.isStruct()); + auto& fields = ht.getStruct().fields; + assert(index < fields.size()); + return fields[index].mutable_; +} +BinaryenType BinaryenArrayTypeGetElementType(BinaryenHeapType heapType) { + auto ht = HeapType(heapType); + assert(ht.isArray()); + return ht.getArray().element.type.getID(); +} +BinaryenPackedType +BinaryenArrayTypeGetElementPackedType(BinaryenHeapType heapType) { + auto ht = HeapType(heapType); + assert(ht.isArray()); + return ht.getArray().element.packedType; +} +bool BinaryenArrayTypeIsElementMutable(BinaryenHeapType heapType) { + auto ht = HeapType(heapType); + assert(ht.isArray()); + return ht.getArray().element.mutable_; +} +BinaryenType BinaryenSignatureTypeGetParams(BinaryenHeapType heapType) { + auto ht = HeapType(heapType); + assert(ht.isSignature()); + return ht.getSignature().params.getID(); +} +BinaryenType BinaryenSignatureTypeGetResults(BinaryenHeapType heapType) { + auto ht = HeapType(heapType); + assert(ht.isSignature()); + return ht.getSignature().results.getID(); +} BinaryenHeapType BinaryenTypeGetHeapType(BinaryenType type) { return Type(type).getHeapType().getID(); diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 480016a80..f5347a65b 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -165,6 +165,23 @@ BINARYEN_API BinaryenHeapType BinaryenHeapTypeGetBottom(BinaryenHeapType heapType); BINARYEN_API bool BinaryenHeapTypeIsSubType(BinaryenHeapType left, BinaryenHeapType right); +BINARYEN_API BinaryenIndex +BinaryenStructTypeGetNumFields(BinaryenHeapType heapType); +BINARYEN_API BinaryenType +BinaryenStructTypeGetFieldType(BinaryenHeapType heapType, BinaryenIndex index); +BINARYEN_API BinaryenPackedType BinaryenStructTypeGetFieldPackedType( + BinaryenHeapType heapType, BinaryenIndex index); +BINARYEN_API bool BinaryenStructTypeIsFieldMutable(BinaryenHeapType heapType, + BinaryenIndex index); +BINARYEN_API BinaryenType +BinaryenArrayTypeGetElementType(BinaryenHeapType heapType); +BINARYEN_API BinaryenPackedType +BinaryenArrayTypeGetElementPackedType(BinaryenHeapType heapType); +BINARYEN_API bool BinaryenArrayTypeIsElementMutable(BinaryenHeapType heapType); +BINARYEN_API BinaryenType +BinaryenSignatureTypeGetParams(BinaryenHeapType heapType); +BINARYEN_API BinaryenType +BinaryenSignatureTypeGetResults(BinaryenHeapType heapType); BINARYEN_API BinaryenHeapType BinaryenTypeGetHeapType(BinaryenType type); BINARYEN_API bool BinaryenTypeIsNullable(BinaryenType type); diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index fc3684171..d59c9e0c7 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -2173,7 +2173,7 @@ void test_typebuilder() { assert(!TypeBuilderIsBasic(builder, tempStructIndex)); assert(!TypeBuilderIsBasic(builder, tempSignatureIndex)); - // Create a subtype (with an additional packed field) + // Create a subtype (with an additional immutable packed field) const BinaryenIndex tempSubStructIndex = 4; BinaryenHeapType tempSubStructHeapType = TypeBuilderGetTempHeapType(builder, tempSubStructIndex); @@ -2184,7 +2184,7 @@ void test_typebuilder() { tempStructType, BinaryenTypeInt32()}; // must repeat existing fields BinaryenPackedType fieldPackedTypes[] = {BinaryenPackedTypeNotPacked(), BinaryenPackedTypeInt8()}; - bool fieldMutables[] = {true, true}; + bool fieldMutables[] = {true, false}; TypeBuilderSetStructType(builder, tempSubStructIndex, fieldTypes, @@ -2212,6 +2212,10 @@ void test_typebuilder() { assert(!BinaryenHeapTypeIsBottom(arrayHeapType)); assert(BinaryenHeapTypeIsSubType(arrayHeapType, BinaryenHeapTypeArray())); BinaryenType arrayType = BinaryenTypeFromHeapType(arrayHeapType, true); + assert(BinaryenArrayTypeGetElementType(arrayHeapType) == arrayType); + assert(BinaryenArrayTypeGetElementPackedType(arrayHeapType) == + BinaryenPackedTypeNotPacked()); + assert(BinaryenArrayTypeIsElementMutable(arrayHeapType)); BinaryenHeapType structHeapType = heapTypes[tempStructIndex]; assert(!BinaryenHeapTypeIsBasic(structHeapType)); @@ -2221,6 +2225,11 @@ void test_typebuilder() { assert(!BinaryenHeapTypeIsBottom(structHeapType)); assert(BinaryenHeapTypeIsSubType(structHeapType, BinaryenHeapTypeData())); BinaryenType structType = BinaryenTypeFromHeapType(structHeapType, true); + assert(BinaryenStructTypeGetNumFields(structHeapType) == 1); + assert(BinaryenStructTypeGetFieldType(structHeapType, 0) == structType); + assert(BinaryenStructTypeGetFieldPackedType(structHeapType, 0) == + BinaryenPackedTypeNotPacked()); + assert(BinaryenStructTypeIsFieldMutable(structHeapType, 0)); BinaryenHeapType signatureHeapType = heapTypes[tempSignatureIndex]; assert(!BinaryenHeapTypeIsBasic(signatureHeapType)); @@ -2231,6 +2240,17 @@ void test_typebuilder() { assert(BinaryenHeapTypeIsSubType(signatureHeapType, BinaryenHeapTypeFunc())); BinaryenType signatureType = BinaryenTypeFromHeapType(signatureHeapType, true); + BinaryenType signatureParams = + BinaryenSignatureTypeGetParams(signatureHeapType); + assert(BinaryenTypeArity(signatureParams) == 2); + BinaryenType expandedSignatureParams[2]; + BinaryenTypeExpand(signatureParams, (BinaryenType*)expandedSignatureParams); + assert(expandedSignatureParams[0] == signatureType); + assert(expandedSignatureParams[1] == arrayType); + BinaryenType signatureResults = + BinaryenSignatureTypeGetResults(signatureHeapType); + assert(BinaryenTypeArity(signatureResults) == 1); + assert(signatureResults == signatureType); BinaryenHeapType basicHeapType = heapTypes[tempBasicIndex]; // = eq assert(BinaryenHeapTypeIsBasic(basicHeapType)); @@ -2251,6 +2271,16 @@ void test_typebuilder() { assert(BinaryenHeapTypeIsSubType(subStructHeapType, structHeapType)); BinaryenType subStructType = BinaryenTypeFromHeapType(subStructHeapType, true); + assert(BinaryenStructTypeGetNumFields(subStructHeapType) == 2); + assert(BinaryenStructTypeGetFieldType(subStructHeapType, 0) == structType); + assert(BinaryenStructTypeGetFieldType(subStructHeapType, 1) == + BinaryenTypeInt32()); + assert(BinaryenStructTypeGetFieldPackedType(subStructHeapType, 0) == + BinaryenPackedTypeNotPacked()); + assert(BinaryenStructTypeGetFieldPackedType(subStructHeapType, 1) == + BinaryenPackedTypeInt8()); + assert(BinaryenStructTypeIsFieldMutable(subStructHeapType, 0)); + assert(!BinaryenStructTypeIsFieldMutable(subStructHeapType, 1)); // Build a simple test module, validate and print it BinaryenModuleRef module = BinaryenModuleCreate(); diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt index 0d7ef581b..408c06444 100644 --- a/test/example/c-api-kitchen-sink.txt +++ b/test/example/c-api-kitchen-sink.txt @@ -3041,7 +3041,7 @@ module with recursive GC types: (type $SomeStruct (struct_subtype (field $SomeField (mut (ref null $SomeStruct))) data)) (type $SomeSignature (func_subtype (param (ref null $SomeSignature) (ref null $SomeArray)) (result (ref null $SomeSignature)) func)) (type $none_=>_none (func_subtype func)) - (type $SomeSubStruct (struct_subtype (field $SomeField (mut (ref null $SomeStruct))) (field $SomePackedField (mut i8)) $SomeStruct)) + (type $SomeSubStruct (struct_subtype (field $SomeField (mut (ref null $SomeStruct))) (field $SomePackedField i8) $SomeStruct)) (func $test (type $none_=>_none) (local $0 (ref null $SomeArray)) (local $1 (ref null $SomeStruct)) |