From 651e9bf0b02d70490f6c0a0522997cefd60c811c Mon Sep 17 00:00:00 2001 From: Chris Harvey <1362083+chharvey@users.noreply.github.com> Date: Mon, 30 Nov 2026 11:33:05 -0500 Subject: [PATCH] feat(js-api): support `BinaryenAddFunctionWithHeapType` --- CHANGELOG.md | 2 ++ src/js/binaryen.js-post.js | 26 ++++++++++++++++++++++---- test/binaryen.js/gc.js | 6 ++++++ test/binaryen.js/gc.js.txt | 13 +++++++++++-- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3095102aa3..2a822344b0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ Current Trunk also removes most `ref.as_non_null` instructions when paired with `--traps-never-happen` and `--strip-refining-casts`. The pass currently panics on tuples and can't be used with multivalue. +- [JS API] support `BinaryenAddFunctionWithHeapType` by adding a new overload: + `mod.addFunction(name, heapType, varTypes, body)`. v133 ---- diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index b9a0fc00ad5..054e3d302ae 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -2705,10 +2705,28 @@ function wrapModule(module, self = {}) { }; // 'Module' operations - self['addFunction'] = function(name, params, results, varTypes, body) { - return preserveStack(() => - Module['_BinaryenAddFunction'](module, strToStack(name), params, results, i32sToStack(varTypes), varTypes.length, body) - ); + /** + * This function has two overloads: + * 1. `addFunction(name, params, results, varTypes, body)` - when you want to define your own function type + * 2. `addFunction(name, heapType, varTypes, body)` - when you already have a signature heap type created by a type builder + */ + self['addFunction'] = function (name, arg1, arg2, arg3, arg4) { + if (arguments.length >= 5) { + const params = arg1; + const results = arg2; + const varTypes = arg3; + const body = arg4; + return preserveStack(() => + Module['_BinaryenAddFunction'](module, strToStack(name), params, results, i32sToStack(varTypes), varTypes.length, body) + ); + } else { + const heapType = arg1; + const varTypes = arg2; + const body = arg3; + return preserveStack(() => + Module['_BinaryenAddFunctionWithHeapType'](module, strToStack(name), heapType, i32sToStack(varTypes), varTypes.length, body) + ); + } }; self['getFunction'] = function(name) { return preserveStack(() => Module['_BinaryenGetFunction'](module, strToStack(name))); diff --git a/test/binaryen.js/gc.js b/test/binaryen.js/gc.js index b29f2a1f1ff..4d5eca58be7 100644 --- a/test/binaryen.js/gc.js +++ b/test/binaryen.js/gc.js @@ -27,6 +27,12 @@ module.addFunction("add", binaryen.createType([binaryen.Type.i32, binaryen.Type. module.local.get("1", binaryen.Type.i32) ) ); +module.addFunction("negate", signatureHeapType, [], + module.drop(module.i32.sub( + module.i32.const(0), + module.local.get("0", binaryen.Type.i32), + )) +); module.setMemory(1, -1, null, [ { offset: module.i32.const(0), data: [4, 3, 2, 1] } diff --git a/test/binaryen.js/gc.js.txt b/test/binaryen.js/gc.js.txt index 1fd11ffb097..db14d443942 100644 --- a/test/binaryen.js/gc.js.txt +++ b/test/binaryen.js/gc.js.txt @@ -3,7 +3,8 @@ (type $1 (struct (field (mut i16)) (field (mut f64)))) (type $2 (array (mut funcref))) (type $3 (func (param i32 i32) (result i32))) - (type $4 (func)) + (type $4 (func (param i32))) + (type $5 (func)) (global $struct-global (mut (ref null $1)) (struct.new_default $1)) (global $array-global (mut (ref null $0)) (array.new_default $0 (i32.const 4) @@ -21,7 +22,15 @@ (local.get $1) ) ) - (func $main (type $4) + (func $negate (type $4) (param $0 i32) + (drop + (i32.sub + (i32.const 0) + (local.get $0) + ) + ) + ) + (func $main (type $5) (drop (struct.new $1 (i32.const 1)