js-types icon indicating copy to clipboard operation
js-types copied to clipboard

Bind on WebAssembly.Function

Open SPY opened this issue 2 years ago • 2 comments

TLDR: bind should be overrided for WebAssembly.Function to return regular JS Function.

According to the proposal WebAssembly.Function is a subclass of regular JS Function and Function.prototype in its prototype chain. It makes possible to call Function.prototype.bind on WebAssembly.Function object. It returns Bound Function exotic object with its [[Prototype]] internal field set to a prototype of target - WebAssembly.Function.prototype in our case. After that we get an object indistinguishable from WebAssembly.Function using instanceof machinery, but it couldn't be used as an argument for Table.set.

let wafn = new WebAssembly.Function({parameters: ['i32'], results:[]}, _ => 0)
let pseudo_wafn = wafn.bind(null)
console.log(pseudo_wafn instanceof WebAssembly.Function) // true in current state of things
console.log(pseudo_wafn.type().parameters) // ['i32']? []? but Function.prototype.bind knows nothing about WebAssembly.Function
let table = new WebAssembly.Table({ initial: 1, element: "anyfunc" })
table.set(0, pseudo_wafn) // throws an exception. pseudo_wafn is not a real WebAssembly.Function

To keep it consistent and predictable I propose to override bind on the level of WebAssembly.Function.prototype to behave exactly like original one, but return BoundFunctionObject with a regular function prototype. It allows to keep ergonomic on JS side without changing ecma262 spec.

SPY avatar Oct 19 '23 12:10 SPY

I think this makes sense. Do you know if there is any precedence for this with other function objects that derive from Function.bind?

eqrion avatar Dec 17 '23 18:12 eqrion

No, I'm not familiar with such precedence. Other function objects(AsyncFunction, AsyncGeneratorFunction) behave the same way as their BoundFunctionObject in any applications. So there is no such problem with them.

SPY avatar Dec 18 '23 17:12 SPY