- From: ExE Boss <notifications@github.com>
- Date: Thu, 12 Aug 2021 07:29:59 -0700
- To: heycam/webidl <webidl@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <heycam/webidl/issues/937/897688469@github.com>
> Could you help write some JS code that reifies `WebAssembly.Function`?
My understanding is that `WebAssembly.Function` instances are supposed to have a `[[Call]]` internal method (i.e.: their `typeof` is `"function"`), the following TypeScript‑like pseudocode demonstrates a way that could be accomplished:
```ts
interface WebAssembly.Function {
// Instances of `WasmFunction` are callable
(...args: any): any;
}
class WebAssembly.Function extends globalThis.Function {
/**
* The `[[FunctionAddress]]` internal slot.
*
* @see https://webassembly.github.io/spec/js-api/#exported-function-exotic-objects
*/
#FunctionAddress: number;
constructor(type: FunctionType, func: globalThis.Function) {
const parameters = Array.from(type.parameters, ToString);
const results = Array.from(type.results, ToString);
const F: WebAssembly.Function = Object.setPrototypeOf(
/** @see https://webassembly.github.io/spec/js-api/#call-an-exported-function */
(...args: unknown[]) => call_exported_function(F.#FunctionAddress, args),
new.target.prototype,
);
/** @see https://github.com/littledan/proposal-new-initialize */
new.initialize(F);
/** @see https://webassembly.github.io/spec/js-api/#create-a-host-function */
F.#FunctionAddress = create_host_function(func, {
paramTypes: parameters,
resultTypes: results,
});
return F;
}
type(): FunctionType {
/** @see https://webassembly.github.io/spec/js-api/#associated-store */
const store = surroundingAgent.wasmStore;
/** @see https://webassembly.github.io/spec/core/appendix/embedding.html#embed-func-type */
const functype = func_type(store, this.#FunctionAddress);
return {
parameters: Array.from(functype.paramTypes),
results: Array.from(functype.resultTypes),
};
}
}
```
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/heycam/webidl/issues/937#issuecomment-897688469
Received on Thursday, 12 August 2021 14:30:14 UTC