WebAssembly
https://scrapbox.io/files/668132ea16a1bf001d0f5025.svg
アセンブリでないバイトコード。今のところ 32bit のみ。モダンブラウザで扱うことが出来、Node.js v8 以降で使うことが出来るため AWS Lambda や BigQuery でも扱える。
https://webassembly.org/
拡張の提案
TC39 による ECMAScript の仕様策定と似ており Stage の概念を持つ(Phase と呼ばれている)。
https://github.com/WebAssembly/proposals
各環境で使える機能については Feature Extensions で確認できる。
https://webassembly.org/features/
バイナリ仕様
http://webassembly.github.io/spec/core/
JavaScript インターフェース
code: (webidl)
dictionary WebAssemblyInstantiatedSource {
required Module module;
required Instance instance;
};
Exposed=(Window,Worker,Worklet)
namespace WebAssembly {
boolean validate(BufferSource bytes);
Promise<Module> compile(BufferSource bytes);
Promise<WebAssemblyInstantiatedSource> instantiate(
BufferSource bytes, optional object importObject);
Promise<Instance> instantiate(
Module moduleObject, optional object importObject);
};
https://webassembly.github.io/spec/js-api/
code: (webidl)
partial namespace WebAssembly {
Promise<Module> compileStreaming(Promise<Response> source);
Promise<WebAssemblyInstantiatedSource> instantiateStreaming(
Promise<Response> source, optional object importObject);
};
https://webassembly.github.io/spec/web-api/
Promise<Response> を受け取る。過去の提案だと Response でも良かったように見えるけど、今の仕様だと Promise でラップしないと受け取れなそう? もしかしたら実装で最適化できるとかそういった理由があるのかもしれない。
バイナリをコンパイルして得られるインターフェース
Module
code: (webidl)
enum ImportExportKind {
"function",
"table",
"memory",
"global"
};
dictionary ModuleExportDescriptor {
required USVString name;
required ImportExportKind kind;
// Note: Other fields such as signature may be added in the future.
};
dictionary ModuleImportDescriptor {
required USVString module;
required USVString name;
required ImportExportKind kind;
};
LegacyNamespace=WebAssembly, Constructor(BufferSource bytes), Exposed=(Window,Worker,Worklet)
interface Module {
static sequence<ModuleExportDescriptor> exports(Module moduleObject);
static sequence<ModuleImportDescriptor> imports(Module moduleObject);
static sequence<ArrayBuffer> customSections(Module moduleObject, DOMString sectionName);
};
このままでは函数を扱うことができないが Transferable なため、Worker, Worklet に送りつける用途として想定されている。かつては Serializable でもあったが仕様から消されてしまった。
Instance
code: (webidl)
LegacyNamespace=WebAssembly, Constructor(Module module, optional object importObject), Exposed=(Window,Worker,Worklet)
interface Instance {
readonly attribute object exports;
};
これを使って JavaScript から WebAssembly の函数を実行する。
インポート、エクスポートするインターフェース
Memory
code: (webidl)
dictionary MemoryDescriptor {
required EnforceRange unsigned long initial;
EnforceRange unsigned long maximum;
};
LegacyNamespace=WebAssembly, Constructor(MemoryDescriptor descriptor), Exposed=(Window,Worker,Worklet)
interface Memory {
unsigned long grow(EnforceRange unsigned long delta);
readonly attribute ArrayBuffer buffer;
};
WebAssembly のヒープ。JavaScript との間でバッファのやり取りをするときは基本的にこれを使う。
WebAssembly 側で作ることも出来るし、JavaScript 側で作って Instance 生成時に渡すことも出来る。
Table
code: (webidl)
enum TableKind {
"anyfunc",
// Note: More values may be added in future iterations,
// e.g., typed function references, typed GC references
};
dictionary TableDescriptor {
required TableKind element;
required EnforceRange unsigned long initial;
EnforceRange unsigned long maximum;
};
LegacyNamespace=WebAssembly, Constructor(TableDescriptor descriptor), Exposed=(Window,Worker,Worklet)
interface Table {
unsigned long grow(EnforceRange unsigned long delta);
Function? get(EnforceRange unsigned long index);
void set(EnforceRange unsigned long index, Function? value);
readonly attribute unsigned long length;
};
今のところは函数をマッピングしているだけのあまり役に立たないものだが、将来的に拡張することが決まっている。これを使って WebAssembly から DOM API にアクセス出来るようにするらしい。
Global
code: (webidl)
dictionary GlobalDescriptor {
required USVString value;
boolean mutable = false;
};
LegacyNamespace=WebAssembly, Constructor(GlobalDescriptor descriptor, optional any v), Exposed=(Window,Worker,Worklet)
interface Global {
any valueOf();
attribute any value;
};
WebAssembly と JavaScript でやり取りするグローバル変数。Instance 初期化時の環境変数を与える用途などで使える。ミュータブルでも受け入れるようになった。