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 @@ -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
----
Expand Down
26 changes: 22 additions & 4 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this would be the first place we use arguments.length to disambiguate an API. I'm not necessarily opposed to this, but what do you think about other options like a different name addFunctionWithType - ?

@chharvey chharvey Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arguments.length is non-standard (and also probably not recommended), I just used it to get the job done. I can either change it to if (arg4 !== undefined) or just make it a completely separate function like you suggest. I figured both functions are so similar that multiple overloads under the same name would reduce surface area, but if you think it warrants a separate API point then I can split it.

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)));
Expand Down
6 changes: 6 additions & 0 deletions test/binaryen.js/gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] }
Expand Down
13 changes: 11 additions & 2 deletions test/binaryen.js/gc.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading