Skip to content

Commit 4e47c5e

Browse files
committed
feat(C & JS API): Add BinaryenHasMemorySegment
This pr adds `BinaryenHasMemorySegment(module, name)` to the c api and `module.hasMemorySegment(name)` to the js api. chore: formatting fix: Ensure to call `preserveStack` before calling `strToStack`
1 parent 6d47684 commit 4e47c5e

6 files changed

Lines changed: 20 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ v126
7777
`BinaryenMemoryOrder` param. The functions formerly implicitly used
7878
`BinaryenMemoryOrderSeqCst()`. In JS this param is optional and thus not
7979
breaking.
80+
- Add `BinaryenHasMemorySegment(<module>, <name>)` to the C API and
81+
`module.hasMemorySegment(name)` to the JS API. Allowing users to check if a segment exists.
8082

8183
v125
8284
----

src/binaryen-c.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5492,6 +5492,10 @@ void BinaryenSetMemory(BinaryenModuleRef module,
54925492
uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module) {
54935493
return ((Module*)module)->dataSegments.size();
54945494
}
5495+
bool BinaryenHasMemorySegment(BinaryenModuleRef module,
5496+
const char* segmentName) {
5497+
return (Module*)module->getDataSegmentOrNull(Name(segmentName)) != NULL;
5498+
}
54955499
uint32_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
54965500
const char* segmentName) {
54975501
auto* wasm = (Module*)module;

src/binaryen-c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,6 +3006,8 @@ BINARYEN_API bool BinaryenMemoryIs64(BinaryenModuleRef module,
30063006
// Memory segments. Query utilities.
30073007

30083008
BINARYEN_API uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module);
3009+
BINARYEN_API bool BinaryenHasMemorySegment(BinaryenModuleRef module,
3010+
const char* segmentName);
30093011
BINARYEN_API uint32_t BinaryenGetMemorySegmentByteOffset(
30103012
BinaryenModuleRef module, const char* segmentName);
30113013
BINARYEN_API size_t BinaryenGetMemorySegmentByteLength(BinaryenModuleRef module,

src/js/binaryen.js-post.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,14 @@ function wrapModule(module, self = {}) {
27722772
self['getNumMemorySegments'] = function() {
27732773
return Module['_BinaryenGetNumMemorySegments'](module);
27742774
};
2775+
/**
2776+
* Determines wether a memory segment with the given name exists within the given module.
2777+
* @param {string} name - The name of the memory segment to check exists.
2778+
* @returns `true` if the memory segment exists, `false` otherwise
2779+
*/
2780+
self['hasMemorySegment'] = function(name) {
2781+
return preserveStack(() => Boolean(Module['_BinaryenHasMemorySegment'](module, strToStack(name))));
2782+
};
27752783
self['getMemorySegmentInfo'] = function(name) {
27762784
return preserveStack(() => {
27772785
const passive = Boolean(Module['_BinaryenGetMemorySegmentPassive'](module, strToStack(name)));

test/binaryen.js/kitchen-sink.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,8 @@ function test_for_each() {
11441144
data: expected_data[2].split('').map(function(x) { return x.charCodeAt(0) })
11451145
}
11461146
], false);
1147+
assert(module.hasMemorySegment(expected_names[0]));
1148+
assert(!module.hasMemorySegment("NonExistantSegment"));
11471149
for (i = 0; i < module.getNumMemorySegments(); i++) {
11481150
var segment = module.getMemorySegmentInfo(expected_names[i]);
11491151
assert(expected_offsets[i] === segment.offset);

test/example/c-api-kitchen-sink.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2019,7 +2019,8 @@ void test_for_each() {
20192019
BinaryenTypeInt32(),
20202020
0,
20212021
makeInt32(module, expected_offsets[1]));
2022-
2022+
assert(BinaryenHasMemorySegment(module, segmentNames[0]));
2023+
assert(!BinaryenHasMemorySegment(module, "NonExistantSegment"));
20232024
for (i = 0; i < BinaryenGetNumMemorySegments(module); i++) {
20242025
char out[15] = {};
20252026
assert(BinaryenGetMemorySegmentByteOffset(module, segmentNames[i]) ==

0 commit comments

Comments
 (0)