Skip to content

Commit 8f96cbd

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 694f7e8 commit 8f96cbd

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
@@ -59,6 +59,8 @@ v126
5959
`BinaryenMemoryOrder` param. The functions formerly implicitly used
6060
`BinaryenMemoryOrderSeqCst()`. In JS this param is optional and thus not
6161
breaking.
62+
- Add `BinaryenHasMemorySegment(<module>, <name>)` to the C API and
63+
`module.hasMemorySegment(name)` to the JS API. Allowing users to check if a segment exists.
6264

6365
v125
6466
----

src/binaryen-c.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5487,6 +5487,10 @@ void BinaryenSetMemory(BinaryenModuleRef module,
54875487
uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module) {
54885488
return ((Module*)module)->dataSegments.size();
54895489
}
5490+
bool BinaryenHasMemorySegment(BinaryenModuleRef module,
5491+
const char* segmentName) {
5492+
return (Module*)module->getDataSegmentOrNull(Name(segmentName)) != NULL;
5493+
}
54905494
uint32_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
54915495
const char* segmentName) {
54925496
auto* wasm = (Module*)module;

src/binaryen-c.h

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

30063006
BINARYEN_API uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module);
3007+
BINARYEN_API bool BinaryenHasMemorySegment(BinaryenModuleRef module,
3008+
const char* segmentName);
30073009
BINARYEN_API uint32_t BinaryenGetMemorySegmentByteOffset(
30083010
BinaryenModuleRef module, const char* segmentName);
30093011
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
@@ -1142,6 +1142,8 @@ function test_for_each() {
11421142
data: expected_data[2].split('').map(function(x) { return x.charCodeAt(0) })
11431143
}
11441144
], false);
1145+
assert(module.hasMemorySegment(expected_names[0]));
1146+
assert(!module.hasMemorySegment("NonExistantSegment"));
11451147
for (i = 0; i < module.getNumMemorySegments(); i++) {
11461148
var segment = module.getMemorySegmentInfo(expected_names[i]);
11471149
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
@@ -2006,7 +2006,8 @@ void test_for_each() {
20062006
BinaryenTypeInt32(),
20072007
0,
20082008
makeInt32(module, expected_offsets[1]));
2009-
2009+
assert(BinaryenHasMemorySegment(module, segmentNames[0]));
2010+
assert(!BinaryenHasMemorySegment(module, "NonExistantSegment"));
20102011
for (i = 0; i < BinaryenGetNumMemorySegments(module); i++) {
20112012
char out[15] = {};
20122013
assert(BinaryenGetMemorySegmentByteOffset(module, segmentNames[i]) ==

0 commit comments

Comments
 (0)