Open DrSensor opened 2 years ago
V8 now support calling Promise in WebAssembly (behind feature flag)
TBD
import { Attribute, Bind, ColonFor } from "../query.ts";
import bind from "./common.ts";
const cache: Record<string, Module> = {};
let count = 0;
export default (attrs: Attribute[]) => async (path: string) => {
let linkCount = count;
(await source).url;
const module = await WebAssembly.instantiateStreaming(source, {
env: { lc: () => linkCount },
});
count++;
};
/** WebAssembly module **instance** */
interface Module {
/** instantiate module */
["constructor"](): void;
/** instance accessor for accessing instance value */
[key: `get ${string}`]: () => unknown;
/** instance accessor for mutating instance value */
[key: `set ${string}`]: (value: unknown) => unknown;
/** instance method that might access/mutate instance and/or global value */
[key: string]: (...args: unknown[]) => unknown;
/** static block which run only once */
// ["static"](): void; // wasm $startFunction serve the same purpose
/** static accessor for accessing global value */
[key: `static get ${string}`]: () => unknown;
/** static accessor for mutating global value */
[key: `static set ${string}`]: (value: unknown) => unknown;
/** static method that can't access/mutate instance value */
[key: `static ${string}`]: (...args: unknown[]) => unknown;
}
Some references
I'm thinking to use Apache Arrow format to solve this. Also, ecmascript data type still fully supported and may automatically inferred when the module is in JavaScript, but only partially (and no infer value) when in WebAssembly. By using this columnar format, it's possible for the module to run on multiple threads whether it's main, worker, or even gpu.
In javascript it might look like
import { Map, Set, Date, ... } from "nusa/std/type"
import { Utf8, Float16, Date64, ... } from "nusa/arrow/type"
Another approach is to use objectbuffer by Bnaya Peretz
I'm in favor of Apache Arrow because the interoperability between languages are more consistent. For example, objectbuffer use ES string which is UTF-16 while most programming languages use UTF-8.
15x Faster TypedArrays: Vector Addition in WebAssembly @ 154GB/s (see wasmblr benchmark code)
Arquero, a JavaScript library for query processing and transformation of Apache Arrow columns
Based on wasm component model and the output of wit-bindgen, the size of <link href=module.wasm>
can be reduced by splitting arrow data type into different components/chunks and import it when <render-scope>
is visible based on the declared imports in module.wasm
.
So which language should be used for implementing that?
I've written some of [component].wasm
in frankenstein Rust π
It's Rust without Cargo that use voodoo technique like manually linking to hand-knitted assembly code and using bunch compiler flags or tools other than rustc
Seems I will continue to go on this path π§ββοΈ
Since not all wasm language can import a function with multi-value return, I've added a fallback function with cABI
prefix which pack all return values into 64bit single value. For example,
(import "nusa" "accessor" (func $a (result i32 i32)))
the fallback would be
(import "nusa" "cABIaccessor" (func $a (result i64)))
The language binding must use cABI*
function instead of normal one only if they can't produce wasm binary with multivalue return.
prototype (stalled)
constructor
,extern instance
)Unlike Javascript module that export class, setter in WebAssembly doesn't automatically update html attribute. You need to specify which bounded property to update.