diff options
author | Abbas Mashayekh <martianboy2005@gmail.com> | 2021-03-06 03:08:51 +0330 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-05 15:38:51 -0800 |
commit | 89b8af006bc56cb4bf68f12a80b1cfe8e7a353d4 (patch) | |
tree | 602bd8e24753b5e45c859a2c3672062ce07133fe /test/example | |
parent | 57619b508d38677844cb482a4034dc985d2cecc6 (diff) | |
download | binaryen-89b8af006bc56cb4bf68f12a80b1cfe8e7a353d4.tar.gz binaryen-89b8af006bc56cb4bf68f12a80b1cfe8e7a353d4.tar.bz2 binaryen-89b8af006bc56cb4bf68f12a80b1cfe8e7a353d4.zip |
[reference-types] Support passive elem segments (#3572)
Passive element segments do not belong to any table, so the link between
Table and elem needs to be weaker; i.e. an elem may have a table in case
of active segments, or simply be a collection of function references in
case of passive/declarative segments.
This PR takes Table::Segment out and turns it into a first class module
element just like tables and functions. It also implements early support
for parsing, printing, encoding and decoding passive/declarative elem
segments.
Diffstat (limited to 'test/example')
-rw-r--r-- | test/example/c-api-kitchen-sink.c | 31 | ||||
-rw-r--r-- | test/example/c-api-kitchen-sink.txt | 8 | ||||
-rw-r--r-- | test/example/c-api-multiple-tables.c | 44 | ||||
-rw-r--r-- | test/example/c-api-multiple-tables.txt | 5 |
4 files changed, 58 insertions, 30 deletions
diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index 04f552069..fa23a47ce 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -320,7 +320,7 @@ void test_core() { BinaryenAddEvent( module, "a-event", 0, BinaryenTypeInt32(), BinaryenTypeNone()); - BinaryenAddTable(module, "tab", 0, 100, NULL, 0, makeInt32(module, 0)); + BinaryenAddTable(module, "tab", 0, 100); // Exception handling @@ -855,7 +855,17 @@ void test_core() { // Function table. One per module const char* funcNames[] = { BinaryenFunctionGetName(sinker) }; - BinaryenSetFunctionTable(module, 1, 1, funcNames, 1, BinaryenConst(module, BinaryenLiteralInt32(0))); + BinaryenAddTable(module, "0", 1, 1); + BinaryenAddActiveElementSegment( + module, + "0", + "0", + funcNames, + 1, + BinaryenConst(module, BinaryenLiteralInt32(0))); + BinaryenAddPassiveElementSegment(module, "passive", funcNames, 1); + BinaryenAddPassiveElementSegment(module, "p2", funcNames, 1); + BinaryenRemoveElementSegment(module, "p2"); // Memory. One per module @@ -1382,14 +1392,15 @@ void test_for_each() { BinaryenFunctionGetName(fns[2]) }; BinaryenExpressionRef constExprRef = BinaryenConst(module, BinaryenLiteralInt32(0)); - BinaryenSetFunctionTable(module, 1, 1, funcNames, 3, constExprRef); - assert(0 == BinaryenIsFunctionTableImported(module)); - assert(1 == BinaryenGetNumFunctionTableSegments(module)); - assert(constExprRef == BinaryenGetFunctionTableSegmentOffset(module, 0)); - assert(3 == BinaryenGetFunctionTableSegmentLength(module, 0)); - for (i = 0; i != BinaryenGetFunctionTableSegmentLength(module, 0); ++i) - { - const char * str = BinaryenGetFunctionTableSegmentData(module, 0, i); + BinaryenAddTable(module, "0", 1, 1); + BinaryenAddActiveElementSegment( + module, "0", "0", funcNames, 3, constExprRef); + assert(1 == BinaryenGetNumElementSegments(module)); + BinaryenElementSegmentRef segment = + BinaryenGetElementSegmentByIndex(module, 0); + assert(constExprRef == BinaryenElementSegmentGetOffset(segment)); + for (i = 0; i != BinaryenElementSegmentGetLength(segment); ++i) { + const char* str = BinaryenElementSegmentGetData(segment, i); assert(0 == strcmp(funcNames[i], str)); } } diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt index 40b7da26e..761b08c85 100644 --- a/test/example/c-api-kitchen-sink.txt +++ b/test/example/c-api-kitchen-sink.txt @@ -39,8 +39,10 @@ BinaryenFeatureAll: 8191 (memory $0 (shared 1 256)) (data (i32.const 10) "hello, world") (data passive "I am passive") - (table $tab 1 1 funcref) - (elem (i32.const 0) "$kitchen()sinker") + (table $tab 0 100 funcref) + (table $0 1 1 funcref) + (elem $0 (table $0) (i32.const 0) func "$kitchen()sinker") + (elem $passive func "$kitchen()sinker") (global $a-global i32 (i32.const 7)) (global $a-mutable-global (mut f32) (f32.const 7.5)) (event $a-event (attr 0) (param i32)) @@ -2461,7 +2463,7 @@ validation: 0 (data (i32.const 10) "hello, world") (data (global.get $a-global) "segment data 2") (table $0 1 1 funcref) - (elem (i32.const 0) $fn0 $fn1 $fn2) + (elem $0 (i32.const 0) $fn0 $fn1 $fn2) (global $a-global i32 (i32.const 125)) (export "export0" (func $fn0)) (export "export1" (func $fn1)) diff --git a/test/example/c-api-multiple-tables.c b/test/example/c-api-multiple-tables.c index 625af444c..c19dcdb6f 100644 --- a/test/example/c-api-multiple-tables.c +++ b/test/example/c-api-multiple-tables.c @@ -31,28 +31,42 @@ int main() { BinaryenAddFunction(module, "adder", params, results, NULL, 0, add); const char* funcNames[] = {"adder"}; - BinaryenAddTable(module, - "tab", - 1, - 1, - funcNames, - 1, - BinaryenConst(module, BinaryenLiteralInt32(0))); + BinaryenAddTable(module, "tab", 1, 1); assert(BinaryenGetTable(module, "tab") != NULL); - - BinaryenAddTable(module, - "t2", - 1, - 1, - funcNames, - 1, - BinaryenConst(module, BinaryenLiteralInt32(0))); + BinaryenAddActiveElementSegment( + module, + "tab", + "0", + funcNames, + 1, + BinaryenConst(module, BinaryenLiteralInt32(0))); + + BinaryenAddTable(module, "t2", 1, 1); + BinaryenAddActiveElementSegment( + module, + "t2", + "1", + funcNames, + 1, + BinaryenConst(module, BinaryenLiteralInt32(0))); + BinaryenAddPassiveElementSegment(module, "passive", funcNames, 1); + assert(NULL != BinaryenGetElementSegmentByIndex(module, 2)); + assert(1 == BinayenElementSegmentIsPassive( + BinaryenGetElementSegment(module, "passive"))); BinaryenTableRef t2 = BinaryenGetTableByIndex(module, 1); assert(t2 != NULL); + BinaryenElementSegmentRef elem1 = BinaryenGetElementSegment(module, "1"); + assert(elem1 != NULL); + assert(strcmp(BinaryenElementSegmentGetName(elem1), "1") == 0); + assert(strcmp(BinaryenElementSegmentGetTable(elem1), "t2") == 0); + assert(BinaryenElementSegmentGetLength(elem1) == 1); + assert(strcmp(BinaryenElementSegmentGetData(elem1, 0), funcNames[0]) == 0); assert(strcmp(BinaryenTableGetName(t2), "t2") == 0); BinaryenTableSetName(t2, "table2"); assert(strcmp(BinaryenTableGetName(t2), "table2") == 0); + BinaryenElementSegmentSetTable(elem1, "table2"); + assert(strcmp(BinaryenElementSegmentGetTable(elem1), "table2") == 0); assert(BinaryenTableGetInitial(t2) == 1); BinaryenTableSetInitial(t2, 2); assert(BinaryenTableGetInitial(t2) == 2); diff --git a/test/example/c-api-multiple-tables.txt b/test/example/c-api-multiple-tables.txt index 5b2858e91..8a2963729 100644 --- a/test/example/c-api-multiple-tables.txt +++ b/test/example/c-api-multiple-tables.txt @@ -1,9 +1,10 @@ (module (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) (table $tab 1 1 funcref) - (elem (table $tab) (i32.const 0) func $adder) + (elem $0 (table $tab) (i32.const 0) func $adder) (table $table2 2 2 funcref) - (elem (table $table2) (i32.const 0) func $adder) + (elem $1 (table $table2) (i32.const 0) func $adder) + (elem $passive func $adder) (func $adder (param $0 i32) (param $1 i32) (result i32) (i32.add (local.get $0) |