From 8da7397e4f96dea6704c07aeafbdf16c9c84bdf4 Mon Sep 17 00:00:00 2001 From: Chris Harvey <1362083+chharvey@users.noreply.github.com> Date: Tue, 28 Jul 2026 23:30:41 -0400 Subject: [PATCH] feat: add `Module#string.const` --- src/js/binaryen.js-post.js | 9 +++++++++ test/binaryen.js/gc.js | 7 +++++-- test/binaryen.js/gc.js.txt | 3 +++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index c97a08642b9..c9f78b153f3 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -2612,6 +2612,15 @@ function wrapModule(module, self = {}) { }; // TODO: string.* + self['string'] = { + /** + * Creates a new string from the literal string contents. + * This instruction is constant and can be used in global variable initializers. + */ + 'const'(value) { + return preserveStack(() => Module['_BinaryenStringConst'](module, strToStack(value))); + } + }; // 'Module' operations self['addFunction'] = function(name, params, results, varTypes, body) { diff --git a/test/binaryen.js/gc.js b/test/binaryen.js/gc.js index 0f12184b4c4..cf3c8ab4009 100644 --- a/test/binaryen.js/gc.js +++ b/test/binaryen.js/gc.js @@ -19,7 +19,7 @@ var arrayType = binaryen.getTypeFromHeapType(arrayHeapType, true); var funcArrayType = binaryen.getTypeFromHeapType(funcArrayHeapType, true); var module = new binaryen.Module(); -module.setFeatures(binaryen.Features.ReferenceTypes | binaryen.Features.BulkMemory | binaryen.Features.GC); +module.setFeatures(binaryen.Features.ReferenceTypes | binaryen.Features.BulkMemory | binaryen.Features.GC | binaryen.Features.Strings); module.addFunction("add", binaryen.createType([binaryen.i32, binaryen.i32]), binaryen.i32, [], module.i32.add( @@ -157,7 +157,10 @@ var valueList = [ module.i32.const(0), module.i32.const(1), module.i32.const(2) - ) + ), + + // string + module.string.const("hello 🌎"), ]; module.addFunction("main", binaryen.none, binaryen.none, [], module.block( diff --git a/test/binaryen.js/gc.js.txt b/test/binaryen.js/gc.js.txt index 7b557c7c743..83476caff6c 100644 --- a/test/binaryen.js/gc.js.txt +++ b/test/binaryen.js/gc.js.txt @@ -111,6 +111,9 @@ (i32.const 1) (i32.const 2) ) + (drop + (string.const "hello \f0\9f\8c\8e") + ) ) )