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

Allow adding meta data

Open kenchris opened this issue 5 years ago • 1 comments

In response to https://github.com/w3ctag/design-reviews/issues/532

function print(...args) {
  for (let x of args) console.log(x + "\n")
}

let table = new Table({element: "anyfunc", minimum: 10});

let print_i32 = new WebAssembly.Function({parameters: ["i32"], results: []}, print);
table.set(0, print_i32);
let print_f64 = new WebAssembly.Function({parameters: ["f64"], results: []}, print);
table.set(1, print_f64);
let print_i32_i32 = new WebAssembly.Function({parameters: ["i32", "i32"], results: []}, print);
table.set(2, print_i32_i32);

In the above example where you are exporting a JS function to Wasm, wouldn't it makes sense it I could supply meta data (in the WebAssembly.Function call) that would be given to the print method when run, so that I could change the formatting.

kenchris avatar Jan 27 '21 15:01 kenchris

I believe we don't need a special machinery for this use case. Regular JS bind will be enough here.

let print_i32 = new WebAssembly.Function({parameters: ["i32"], results: []}, print.bind('i32'));
table.set(0, print_i32);
let print_f64 = new WebAssembly.Function({parameters: ["f64"], results: []}, print.bind('i64'));
table.set(1, print_f64);

SPY avatar Oct 19 '23 09:10 SPY