Skip to content

Commit 7d368c4

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.
1 parent e103d6d commit 7d368c4

File tree

6 files changed

+18
-1
lines changed

6 files changed

+18
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Current Trunk
3232
`BinaryenMemoryOrder` param. The functions formerly implicitly used
3333
`BinaryenMemoryOrderSeqCst()`. In JS this param is optional and thus not
3434
breaking.
35+
- Add `BinaryenHasMemorySegment(<module>, <name>)` to the C API and
36+
`module.hasMemorySegment(name)` to the JS API. Allowing users to check if a segment exists.
3537

3638
v125
3739
----

src/binaryen-c.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5512,6 +5512,9 @@ void BinaryenSetMemory(BinaryenModuleRef module,
55125512
uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module) {
55135513
return ((Module*)module)->dataSegments.size();
55145514
}
5515+
bool BinaryenHasMemorySegment(BinaryenModuleRef module, const char* segmentName) {
5516+
return (Module*)module->getDataSegmentOrNull(Name(segmentName)) != NULL;
5517+
}
55155518
uint32_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
55165519
const char* segmentName) {
55175520
auto* wasm = (Module*)module;

src/binaryen-c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,6 +3017,7 @@ BINARYEN_API bool BinaryenMemoryIs64(BinaryenModuleRef module,
30173017
// Memory segments. Query utilities.
30183018

30193019
BINARYEN_API uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module);
3020+
BINARYEN_API bool BinaryenHasMemorySegment(BinaryenModuleRef module, const char* segmentName);
30203021
BINARYEN_API uint32_t BinaryenGetMemorySegmentByteOffset(
30213022
BinaryenModuleRef module, const char* segmentName);
30223023
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
@@ -2757,6 +2757,14 @@ function wrapModule(module, self = {}) {
27572757
self['getNumMemorySegments'] = function() {
27582758
return Module['_BinaryenGetNumMemorySegments'](module);
27592759
};
2760+
/**
2761+
* Determines wether a memory segment with the given name exists within the given module.
2762+
* @param {string} name - The name of the memory segment to check exists.
2763+
* @returns `true` if the memory segment exists, `false` otherwise
2764+
*/
2765+
self['hasMemorySegment'] = function(name) {
2766+
return Boolean(Module['_BinaryenHasMemorySegment'](module, strToStack(name)));
2767+
};
27602768
self['getMemorySegmentInfo'] = function(name) {
27612769
return preserveStack(() => {
27622770
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
@@ -1132,6 +1132,8 @@ function test_for_each() {
11321132
data: expected_data[2].split('').map(function(x) { return x.charCodeAt(0) })
11331133
}
11341134
], false);
1135+
assert(module.hasMemorySegment(expected_names[0]));
1136+
assert(!module.hasMemorySegment("NonExistantSegment"));
11351137
for (i = 0; i < module.getNumMemorySegments(); i++) {
11361138
var segment = module.getMemorySegmentInfo(expected_names[i]);
11371139
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
@@ -2004,7 +2004,8 @@ void test_for_each() {
20042004
BinaryenTypeInt32(),
20052005
0,
20062006
makeInt32(module, expected_offsets[1]));
2007-
2007+
assert(BinaryenHasMemorySegment(module, segmentNames[0]));
2008+
assert(!BinaryenHasMemorySegment(module, "NonExistantSegment"));
20082009
for (i = 0; i < BinaryenGetNumMemorySegments(module); i++) {
20092010
char out[15] = {};
20102011
assert(BinaryenGetMemorySegmentByteOffset(module, segmentNames[i]) ==

0 commit comments

Comments
 (0)