diff options
author | dcode <dcode@dcode.io> | 2022-11-03 17:08:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-03 09:08:46 -0700 |
commit | fe78fe116a67ae37d0e6d4597832042d3cbff230 (patch) | |
tree | 2d9e933a13e92402051237ab39b73c5175c8030e /test/example/c-api-kitchen-sink.c | |
parent | 201e5ceae6f092f7a4f34d6e681a9887812f0035 (diff) | |
download | binaryen-fe78fe116a67ae37d0e6d4597832042d3cbff230.tar.gz binaryen-fe78fe116a67ae37d0e6d4597832042d3cbff230.tar.bz2 binaryen-fe78fe116a67ae37d0e6d4597832042d3cbff230.zip |
[C API] Add APIs to inspect compound heap types (#5195)
Adds C APIs to inspect compound struct, array and signature heap types:
Obtain field types, field packed types and field mutabilities of struct types:
BinaryenStructTypeGetNumFields (to iterate)
BinaryenStructTypeGetFieldType
BinaryenStructTypeGetFieldPackedType
BinaryenStructTypeIsFieldMutable
Obtain element type, element packed type and element mutability of array types:
BinaryenArrayTypeGetElementType
BinaryenArrayTypeGetElementPackedType
BinaryenArrayTypeIsElementMutable
Obtain parameter and result types of signature types:
BinaryenSignatureTypeGetParams
BinaryenSignatureTypeGetResults
Diffstat (limited to 'test/example/c-api-kitchen-sink.c')
-rw-r--r-- | test/example/c-api-kitchen-sink.c | 34 |
1 files changed, 32 insertions, 2 deletions
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(); |