Skip to content

Bind on WebAssembly.Function #46

Open
@SPY

Description

@SPY

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions