diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c94d7dfc0b..0d20f3a4253 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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(, )` to the C API and + `module.hasMemorySegment(name)` to the JS API. Allowing users to check if a segment exists. v125 ---- diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index efbf69d3de3..6f883928c72 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -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; diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 806574f744e..9e552e8b70c 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -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, diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index 0541b171f22..ab760b62508 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -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 preserveStack(() => Boolean(Module['_BinaryenHasMemorySegment'](module, strToStack(name)))); + }; self['getMemorySegmentInfo'] = function(name) { return preserveStack(() => { const passive = Boolean(Module['_BinaryenGetMemorySegmentPassive'](module, strToStack(name))); diff --git a/test/binaryen.js/kitchen-sink.js b/test/binaryen.js/kitchen-sink.js index 673a470607d..1e69d82e34a 100644 --- a/test/binaryen.js/kitchen-sink.js +++ b/test/binaryen.js/kitchen-sink.js @@ -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); diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index 2499ffff646..48c2e0e27c2 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -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]) ==