diff options
author | Abbas Mashayekh <martianboy2005@gmail.com> | 2021-03-24 21:43:45 +0430 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-24 10:13:45 -0700 |
commit | ffac06650507ac413d60d72aadc1e33fb1f91ccf (patch) | |
tree | f581d02da66df8db2a775fc731d39baf15d96c82 /src/binaryen-c.cpp | |
parent | 683c31381f5798016f683a6b42e2a8fad0f871cb (diff) | |
download | binaryen-ffac06650507ac413d60d72aadc1e33fb1f91ccf.tar.gz binaryen-ffac06650507ac413d60d72aadc1e33fb1f91ccf.tar.bz2 binaryen-ffac06650507ac413d60d72aadc1e33fb1f91ccf.zip |
[RT] Support expressions in element segments (#3666)
This PR adds support for `ref.null t` as a valid element segment
item. The abbreviated format of `(elem ... func $f $g...)` is kept in
both printing and binary emitting if all items are `ref.func`s. Public
APIs aren't updated in this PR.
Diffstat (limited to 'src/binaryen-c.cpp')
-rw-r--r-- | src/binaryen-c.cpp | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 1b3f11e22..aff4ed4fc 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -3409,7 +3409,13 @@ BinaryenAddActiveElementSegment(BinaryenModuleRef module, auto segment = std::make_unique<ElementSegment>(table, (Expression*)offset); segment->setExplicitName(name); for (BinaryenIndex i = 0; i < numFuncNames; i++) { - segment->data.push_back(funcNames[i]); + auto* func = ((Module*)module)->getFunctionOrNull(funcNames[i]); + if (func == nullptr) { + Fatal() << "invalid function '" << funcNames[i] << "'."; + } + Type type(HeapType(func->sig), Nullable); + segment->data.push_back( + Builder(*(Module*)module).makeRefFunc(funcNames[i], type)); } return ((Module*)module)->addElementSegment(std::move(segment)); } @@ -3421,7 +3427,13 @@ BinaryenAddPassiveElementSegment(BinaryenModuleRef module, auto segment = std::make_unique<ElementSegment>(); segment->setExplicitName(name); for (BinaryenIndex i = 0; i < numFuncNames; i++) { - segment->data.push_back(funcNames[i]); + auto* func = ((Module*)module)->getFunctionOrNull(funcNames[i]); + if (func == nullptr) { + Fatal() << "invalid function '" << funcNames[i] << "'."; + } + Type type(HeapType(func->sig), Nullable); + segment->data.push_back( + Builder(*(Module*)module).makeRefFunc(funcNames[i], type)); } return ((Module*)module)->addElementSegment(std::move(segment)); } @@ -3460,7 +3472,13 @@ const char* BinaryenElementSegmentGetData(BinaryenElementSegmentRef elem, if (data.size() <= dataId) { Fatal() << "invalid segment data id."; } - return data[dataId].c_str(); + if (data[dataId]->is<RefNull>()) { + return NULL; + } else if (auto* get = data[dataId]->dynCast<RefFunc>()) { + return get->func.c_str(); + } else { + Fatal() << "invalid expression in segment data."; + } } // Memory. One per module |