Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Current Trunk
`BinaryenMemoryOrder` param. The functions formerly implicitly used
`BinaryenMemoryOrderSeqCst()`. In JS this param is optional and thus not
breaking.
- Add `BinaryenHasMemorySegment(<module>, <name>)` to the C API and
`module.hasMemorySegment(name)` to the JS API. Allowing users to check if a segment exists.

v125
----
Expand Down
4 changes: 4 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5512,6 +5512,10 @@ void BinaryenSetMemory(BinaryenModuleRef module,
uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module) {
return ((Module*)module)->dataSegments.size();
}
bool BinaryenHasMemorySegment(BinaryenModuleRef module,
const char* segmentName) {
return (Module*)module->getDataSegmentOrNull(Name(segmentName)) != NULL;
}
uint32_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
const char* segmentName) {
auto* wasm = (Module*)module;
Expand Down
2 changes: 2 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -3017,6 +3017,8 @@ BINARYEN_API bool BinaryenMemoryIs64(BinaryenModuleRef module,
// Memory segments. Query utilities.

BINARYEN_API uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module);
BINARYEN_API bool BinaryenHasMemorySegment(BinaryenModuleRef module,
const char* segmentName);
BINARYEN_API uint32_t BinaryenGetMemorySegmentByteOffset(
BinaryenModuleRef module, const char* segmentName);
BINARYEN_API size_t BinaryenGetMemorySegmentByteLength(BinaryenModuleRef module,
Expand Down
8 changes: 8 additions & 0 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -2757,6 +2757,14 @@ function wrapModule(module, self = {}) {
self['getNumMemorySegments'] = function() {
return Module['_BinaryenGetNumMemorySegments'](module);
};
/**
* Determines wether a memory segment with the given name exists within the given module.
* @param {string} name - The name of the memory segment to check exists.
* @returns `true` if the memory segment exists, `false` otherwise
*/
self['hasMemorySegment'] = function(name) {
return Boolean(Module['_BinaryenHasMemorySegment'](module, strToStack(name)));
};
self['getMemorySegmentInfo'] = function(name) {
return preserveStack(() => {
const passive = Boolean(Module['_BinaryenGetMemorySegmentPassive'](module, strToStack(name)));
Expand Down
2 changes: 2 additions & 0 deletions test/binaryen.js/kitchen-sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,8 @@ function test_for_each() {
data: expected_data[2].split('').map(function(x) { return x.charCodeAt(0) })
}
], false);
assert(module.hasMemorySegment(expected_names[0]));
assert(!module.hasMemorySegment("NonExistantSegment"));
for (i = 0; i < module.getNumMemorySegments(); i++) {
var segment = module.getMemorySegmentInfo(expected_names[i]);
assert(expected_offsets[i] === segment.offset);
Expand Down
3 changes: 2 additions & 1 deletion test/example/c-api-kitchen-sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,8 @@ void test_for_each() {
BinaryenTypeInt32(),
0,
makeInt32(module, expected_offsets[1]));

assert(BinaryenHasMemorySegment(module, segmentNames[0]));
assert(!BinaryenHasMemorySegment(module, "NonExistantSegment"));
for (i = 0; i < BinaryenGetNumMemorySegments(module); i++) {
char out[15] = {};
assert(BinaryenGetMemorySegmentByteOffset(module, segmentNames[i]) ==
Expand Down
Loading