コンテンツにスキップ

「WebAssembly」の版間の差分

出典: フリー百科事典『ウィキペディア(Wikipedia)』
削除された内容 追加された内容
整理: 歴史的内容を個別の節に移動、追加: 2021年現在の定義と特徴
Cewbot (会話 | 投稿記録)
50行目: 50行目:


== 開発 ==
== 開発 ==
WebAssemblyサポートの初期実装は、既存の{{仮リンク|asm.js|en|asm.js}}と[[Google Native Client|PNaCl]]をベースとしている<ref>{{Cite web|url=http://www.2ality.com/2015/06/web-assembly.html|title=WebAssembly: a binary format for the web|work=②ality&nbsp;– JavaScript and more|date=18 June 2015|accessdate=2017-03-11}}</ref><ref name="ars">{{Cite web |first=Peter |last=Bright |url=http://arstechnica.com/information-technology/2015/06/the-web-is-getting-its-bytecode-webassembly/|title=The Web is getting its bytecode: WebAssembly|work=Ars Technica |publisher=[[コンデナスト・パブリケーションズ|Condé Nast]] |date=18 June 2015|accessdate=2017-03-11}}</ref>。必要最低限の機能のみを持つ製品 (MVP) がリリースされた後、次いで[[ガベージコレクション]] (GC) の対応を行い、[[Java]]や[[C Sharp|C#]]といったGCを持つ言語をサポート対象に加えることが計画されている<ref>{{Cite web|title = WebAssembly/design|url = https://github.com/WebAssembly/design/blob/master/GC.md|website = GitHub|accessdate = 28 December 2015}}</ref>。WebAssemblyの開発は[[Mozilla]]、[[マイクロソフト]]、[[Google]]、[[アップル (企業)|アップル]]といった主要ブラウザの開発者により行われている<ref name="ars" />。
WebAssemblyサポートの初期実装は、既存の{{仮リンク|asm.js|en|asm.js}}と[[Google Native Client|PNaCl]]をベースとしている<ref>{{Cite web|url=http://www.2ality.com/2015/06/web-assembly.html|title=WebAssembly: a binary format for the web|work=②ality&nbsp;– JavaScript and more|date=18 June 2015|accessdate=2017-03-11}}</ref><ref name="ars">{{Cite web |first=Peter |last=Bright |url=http://arstechnica.com/information-technology/2015/06/the-web-is-getting-its-bytecode-webassembly/|title=The Web is getting its bytecode: WebAssembly|work=Ars Technica |publisher=[[コンデナスト・パブリケーションズ|Condé Nast]] |date=18 June 2015|accessdate=2017-03-11}}</ref>。必要最低限の機能のみを持つ製品 (MVP) がリリースされた後、次いで[[ガベージコレクション]] (GC) の対応を行い、[[Java]]や[[C Sharp|C#]]といったGCを持つ言語をサポート対象に加えることが計画されている<ref>{{Cite web|title = WebAssembly/design|url = https://github.com/WebAssembly/design/blob/master/GC.md|website = GitHub|accessdate = 28 December 2015}}</ref>。WebAssemblyの開発は[[Mozilla]]、[[マイクロソフト]]、[[Google]]、[[Apple]]といった主要ブラウザの開発者により行われている<ref name="ars" />。


== フォーマット ==
== フォーマット ==

2021年5月20日 (木) 12:55時点における版

パラダイム 式指向英語版
最新リリース 2.0 / 2022年6月1日[1]
型付け 静的
影響を受けた言語 asm.js英語版, PNaCl
ライセンス Apache License
ウェブサイト webassembly.org
拡張子 .wast, .wasm
テンプレートを表示

WebAssemblyは仮想命令セットアーキテクチャあるいはプログラミング言語の一種である[2]。略称はWasm[3]CRustなど様々なプログラミング言語のコンパイルターゲットとしてWasmバイナリは生成され、ウェブブラウザを含む様々な環境内のスタックベース仮想マシンにより実行される。

ネイティブコード相当の高速性・隔離環境でのメモリ安全な実行による安全性・仮想マシンによるハードウェア/プラットフォーム可搬性・ソースプログラミング言語中立性などを特徴とする[4]。この命令セットはバイナリ形式で定義されており、またアセンブリ言語ライクなテキスト形式も定義されている(その意味で低水準プログラミング言語といえる)。

Wasm自体は命令セットアーキテクチャであり、Linuxカーネルが提供するようなシステムコール(例: ファイルI/O)、Webブラウザが提供するようなDOMアクセスなどを提供していない[5]。上記の安全性や可搬性はこの特徴に由来している。それと同時に、WasmエコシステムとしてはシステムコールやDOMアクセスがAPIとして個別に定義されており、Wasmランタイムが実装することでそれらの機能を提供している(例: システムコールを提供するWASI[6]。シンプルでオープンなISAとランタイムごとのAPIを組み合わせることでWasmエコシステムは高い拡張性を有している。例えばWasmをHTTPプロキシでのフィルタスクリプトとして利用するプロジェクトが存在する。

設計

WebAssemblyはポータブルなスタックマシン[7]であり、既存のウェブブラウザで広く用いられているJavaScriptと比べ、構文解析と実行が高速になるよう設計されている[8]

歴史

WebAssemblyの最初のアナウンスは2015年6月17日に行われ[9]2016年3月15日には主要ブラウザ (Firefox, Chromium, Google Chrome, Microsoft Edge) 上でUnityによるAngry Botsというデモが行われた[10][11][12]

最初の目標としてCC++からのコンパイルをサポートすることを目指し[8]Rustがバージョン1.14以降で[13]Goがバージョン1.11以降で[14]Kotlin/Nativeがバージョン0.4以降で[15]で対応するなど、他のプログラミング言語のサポートも進められた。

2017年3月7日には、WebAssemblyに標準対応した初のブラウザとなるFirefox 52.0がリリースされた[16]。2017年11月、MozillaはSafariとEdgeがWebAssemblyに対応したと発表し、すでに対応しているChromeとFirefoxを含め、主要なブラウザすべてでサポートされることになった[17]

2019-12-05にはW3C勧告WebAssembly Core Specification」が策定され、WebAssemblyは正式なウェブ標準に認定された[18]

開発

WebAssemblyサポートの初期実装は、既存のasm.js英語版PNaClをベースとしている[19][20]。必要最低限の機能のみを持つ製品 (MVP) がリリースされた後、次いでガベージコレクション (GC) の対応を行い、JavaC#といったGCを持つ言語をサポート対象に加えることが計画されている[21]。WebAssemblyの開発はMozillaマイクロソフトGoogleAppleといった主要ブラウザの開発者により行われている[20]

フォーマット

WebAssemblyコミュニティグループは、複数のブラウザにおいて最初 (MVP) のバイナリフォーマットのリリース候補とJavaScript APIを実装している[22]

WebAssemblyのバイナリフォーマットはそのまま人間が読めるようには設計されていない。その代り、人間が読めるリニアアセンブリバイトコード (Linear Assembly Bytecode) と呼ばれる既存のアセンブリ言語に相当するようなフォーマットが用意されている。

以下に、C言語のソースコードが、wasmのリニアアセンブリバイトコードとバイナリにそれぞれ変換された例を示す。

C(変換元) リニアアセンブリバイトコード
(中間表現)
WASMバイナリフォーマット
(16進数で表記)
int factorial(int n) {
  if (n == 0)
    return 1;
  else
    return n * factorial(n-1);
}
get_local 0
i64.const 0
i64.eq
if i64
    i64.const 1
else
    get_local 0
    get_local 0
    i64.const 1
    i64.sub
    call 0
    i64.mul
end
20 00
42 00
51
04 7e
42 01
05
20 00
20 00
42 01
7d
10 00
7e
0b

[23]

内部的には、wasmコンパイラシステムは中間コードを扱うためにS式を使用している。サンプルを以下に示す。[24]

(module
  (memory 256 256)
  (export "memory" memory)
  (type $FUNCSIG$dd (func (param f64) (result f64)))
  (import $exp "global.Math" "exp" (param f64) (result f64))
  (export "doubleExp" $doubleExp)
  (func $doubleExp (param $0 f64) (result f64)
    (f64.mul
      (call_import $exp
        (get_local $0)
      )
      (f64.const 2)
    )
  )
)

ツール

  • Emscripten - 元々asm.js向けであったが、その後、WebAssemblyにも対応した。C言語/C++からWebAssemblyへのコンパイルでは、フロントエンドにclangもしくはそのforkであるfastcomp-clangを、中間層にLLVMもしくはそのforkであるfastcompを、バックエンドにbinaryen (後述)を使用する[25]。なお、LLVMのWebAssembly実装とFastcompのWebAssembly実装は別物である。
  • GCC asm.js backend - asm.js及びWebAssemblyに対応している[26]
  • LLVM - WebAssemblyバックエンドを持ち、WebAssemblyバイナリを直接出力できる。また、LLDによるWebAssemblyバイナリのリンクも可能。LLVM 8.0で正式に対応した[27]

バックエンド

  • Binaryen
    • asm2wasm - asm.jsからWebAssemblyテキストへのコンバータ[25]
    • s2wasm - LLVMのWebAssembly用テキストアセンブリ (*.s)からWebAssemblyテキストへのコンバータ[25]
    • mir2wasm - Rust言語の中間レベルIR (MIR)からWebAssemblyテキストへのコンバータ[25]
    • wasm-as - WebAssemblyテキストからWebAssemblyバイナリへのコンバータ。
  • WABT
    • wat2wasm - WebAssemblyテキストからWebAssemblyバイナリへのコンバータ。
    • wasm-link - WebAssemblyバイナリのリンカー

ランタイム

仮想マシン命令セットであるWASMはコンピュータの実行ファイルではない(そのまま機械語として実行はできない)。そのためWASMファイルはランタイムを介し解釈・実行される。ランタイムにはインタプリタ/JITコンパイラ/AOTコンパイラがあり用途に合わせて選択される。

表. WASMランタイム
実行環境 エンジン/ランタイム コンパイラ
Google Chrome & Node.js V8 Liftoff (baseline)[28] + TurboFan (optimizing)[29]
Mozilla Firefox SpiderMonkey[30] rabaldr (baseline) + BaldrMonkey (optimizing, backed by IonMonkey)
wasmtime[31] - Cranelift[32]
Lucet[33] - Cranelift[34]
Wasmer[35] - pluggable (LLVM, Cranelift)[36]

ランタイムは当初JavaScript実行環境からの呼び出しにより機能するよう作られていたが、Lucet等のWASMネイティブランタイム登場により様々な環境から実行できるようになった (例: CLIにおける $ wasmtime foo.wasm[37]) 。

統合開発環境

  • WebAssembly Studio - WebベースのWebAssembly向け開発環境。C言語及びRustに対応している。オープンソース[38]

アプリケーションフレームワーク

  • Qt for WebAssembly - アプリケーションフレームワークのQtをWebAssemblyに移植したもの。2018年4月現在、テクノロジープレビュー。

参考文献

脚注

  1. ^ 出典URL: https://github.com/WebAssembly/spec/releases/tag/opam-2.0.0, 閲覧日: 2023年2月11日, 題名: Release 2.0, 出版日: 2022年6月1日
  2. ^ "At its core, WebAssembly is a virtual instruction set architecture (virtual ISA)." WebAssembly Specification Release 1.1 (Draft, Mar 12, 2021) 2021-03-26閲覧
  3. ^ "abbreviated Wasm ... A contraction of “WebAssembly”, not an acronym, hence not using all-caps." WebAssembly Specification Release 1.1 (Draft, Mar 12, 2021) 2021-03-26閲覧
  4. ^ "The design goals of WebAssembly are the following ... Fast: executes with near native code performance ... Safe: code is validated and executes in a memory-safe, sandboxed environment ... Hardware-independent ... Language-independent ... Platform-independent: can be embedded in browsers, run as a stand-alone VM, or integrated in other environments" WebAssembly Specification Release 1.1 (Draft, Mar 12, 2021) 2021-03-26閲覧
  5. ^ "This document is concerned with the core ISA layer of WebAssembly. ... It does not, however, define how WebAssembly programs can interact with a specific environment they execute in" WebAssembly Specification Release 1.1 (Draft, Mar 12, 2021) 2021-03-26閲覧
  6. ^ "Instead, this specification is complemented by additional documents defining interfaces to specific embedding environments such as the Web. These will each define a WebAssembly application programming interface (API) suitable for a given environment." WebAssembly Specification Release 1.1 (Draft, Mar 12, 2021) 2021-03-26閲覧
  7. ^ Design Rationale”. GitHub / WebAssembly / design (2016年10月1日). 2017年3月11日閲覧。
  8. ^ a b WebAssembly High-Level Goals”. GitHub / WebAssembly / design (2015年12月11日). 2017年3月11日閲覧。
  9. ^ Launch bug”. GitHub / WebAssembly / design (2015年6月11日). 2017年3月11日閲覧。
  10. ^ Wagner, Luke (2016年3月14日). “A WebAssembly Milestone: Experimental Support in Multiple Browsers”. Mozilla Hacks. 2017年3月11日閲覧。
  11. ^ Thompson, Seth (2016年3月15日). “Experimental support for WebAssembly in V8”. V8 Blog. 2017年3月11日閲覧。
  12. ^ Zhu, Limin (2016年3月15日). “Previewing WebAssembly experiments in Microsoft Edge”. Microsoft Edge dev blog. 2017年3月11日閲覧。
  13. ^ Announcing Rust 1.14”. The Rust Programming Language Blog (2016年12月22日). 2017年3月11日閲覧。
  14. ^ Go 1.11 is released”. The Go Blog (2018年8月24日). 2018年10月4日閲覧。
  15. ^ Kotlin/Native v0.4 released: Objective-C interop, WebAssembly and more”. KOTLIN BLOG (2017年11月16日). 2019年1月12日閲覧。
  16. ^ Firefox、ゲームなどをネイティブ並に高速実行する「WebAssembly」を採用”. Impress Watch (2017年3月8日). 2017年3月11日閲覧。
  17. ^ 長岡弥太郎 (2017年11月14日). “WebAssemblyが主要ブラウザでサポート - Mozilla official blog”. マイナビニュース. 2017年11月14日閲覧。
  18. ^ 5 December 2019 — The World Wide Web Consortium (W3C) announced today that the WebAssembly Core Specification is now an official web standard, launching a powerful new language for the Web. W3C (2019). World Wide Web Consortium (W3C) brings a new language to the Web as WebAssembly becomes a W3C Recommendation.
  19. ^ WebAssembly: a binary format for the web”. ②ality – JavaScript and more (2015年6月18日). 2017年3月11日閲覧。
  20. ^ a b Bright, Peter (2015年6月18日). “The Web is getting its bytecode: WebAssembly”. Ars Technica. Condé Nast. 2017年3月11日閲覧。
  21. ^ WebAssembly/design”. GitHub. 2015年12月28日閲覧。
  22. ^ Roadmap - WebAssembly”. webassembly.org. 2017年1月27日閲覧。
  23. ^ サンプルのソースコードはGitHubのWebAssemblyプロジェクトより https://github.com/WebAssembly/design/blob/master/TextFormat.md
  24. ^ http://cultureofdevelopment.com/blog/build-your-first-thing-with-web-assembly/
  25. ^ a b c d https://github.com/WebAssembly/binaryen
  26. ^ https://github.com/pipcet/asmjs/blob/everything/asmjs.org
  27. ^ 「LLVM 8.0」登場、WebAssemblyを正式サポート OSDN 2019年3月22日
  28. ^ "V8 first compiles a WebAssembly module with its baseline compiler, Liftoff." V8.dev. WebAssembly compilation pipeline.
  29. ^ "This is why, as soon as Liftoff compilation is finished, V8 immediately starts to "tier up" the module by recompiling all functions with TurboFan, the optimizing compiler in V8 for both WebAssembly and JavaScript." V8.dev. WebAssembly compilation pipeline.
  30. ^ "SpiderMonkey is the JavaScript and WebAssembly engine in Firefox." wasmtime (2018). Cranelift in SpiderMonkey.
  31. ^ "A standalone runtime for WebAssembly" bytecodealliance/wasmtime. RAEDME.md.
  32. ^ "Wasmtime is built on the optimizing Cranelift code generator to quickly generate high-quality machine code at runtime." bytecodealliance/wasmtime. RAEDME.md.
  33. ^ "Lucet is a native WebAssembly compiler and runtime." bytecodealliance/lucet. README.md.
  34. ^ "Lucet uses, and is developed in collaboration with, the Bytecode Alliance's Cranelift code generator." bytecodealliance/lucet. README.md.
  35. ^ "The leading WebAssembly Runtime supporting WASI and Emscripten" wasmerio/wasmer. About.
  36. ^ "Wasmer supports different compilation frameworks to best suit your needs (LLVM, Cranelift...)." wasmerio/wasmer. About.
  37. ^ " you can execute a WebAssembly file" wastime. Using the wasmtime CLI.
  38. ^ Aw, all grown up: Mozilla moves WebAssembly into sparsely furnished Studio apartment The Register 2018年4月11日

関連項目

外部リンク