» www.Giftbox.Az - Bir birindən gözəl hədiyyə satışı
ウィキペディアランダム
毎日カテゴリ
共有: WhatsappFacebookTwitterVK

WebAssembly

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

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

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

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

仕様

WebAssemblyはポータブルなスタックマシン[7]であり、既存のウェブブラウザで広く用いられているJavaScriptと比べ、構文解析と実行が高速になるよう設計されている[8]。WASM specification はWASMの言語仕様および(実行ファイル形式)を定義する。

言語

Wasmは高水準のアセンブリ言語として設計されている。x64等のアセンブリ言語にみられない特徴として以下がある。

  • 制御命令: ifloop。直接のJUMP命令が無い、安全かつ高水準の制御フローを実現(構造化プログラミング[9]
  • 関数: 型・ローカル変数・ボディで定義され[10]call される関数。コードの組織化を実現[11]
    • ローカル変数: 関数スコープの自動変数。get/set /tee 命令でアクセスでき疑似レジスタとして利用可能[12]

値型

値に定義される型は i32 / i64 / f32 / f64 の4種類である[13]。char/stringは型としてサポートされない。また、高級言語のように関数が構造体型を取り回すようなコードが直接的には記述できない。

入出力

デフォルトでは外部と隔離されている(サンドボックス)。計算結果を渡したり外部関数を呼び出したりするために、WASMは imports/exports 機能を提供する。対象となるオブジェクトは関数・テーブル・メモリ・グローバル変数の4種類。exports要素に対象のインデックスを登録することで、WASM外からそのオブジェクトへアクセスできる(関数呼び出しやメモリ読み書き)。またimports要素に対象の名前と型を登録することで、WASM外に存在する対象へのアクセスをWASMランタイムが提供する。

フォーマット

Wasmはバイナリフォーマット: binary format)およびテキストフォーマット: text format)を定義している。フォーマットの設計方針として Compact / Modular / Efficient / Streamable / Parallelizable / Portable を掲げている。

言語のバイナリ/テキスト表現(例: オペコード)に加え、(実行ファイル形式)(コンテナフォーマット)を定義する。module が1つの実行ファイルに相当し、マジックナンバー等のメタ情報が冒頭に記述され、関数やexportsなど11種類のセクションが続く。セクションはサイズ情報を持っており、並行処理可能に設計されている。

以下に、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 

[14]

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

(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)  )  ) ) 

機能

ベクトル演算

WASMは128ビット幅のベクトル型(v128)をもち、ベクトル命令(: Vector instructions)すなわちSIMD命令を定義している[16]。WASM SIMDはChromiumでサポートされている[17]

連携

Wasmはホスト環境に埋め込まれるサンドボックスであり、import/exportを介したホストとの連携によって意味ある結果が得られる。WasmがシンプルなVMである反面として、Wasm-ホスト間の連携にはいくつかのテクニックが必要となる。また各moduleが1つのサンドボックスとして働くため、Wasmモジュール間の連携にも同様のことが言える。

基本的な問題として、Wasm-ホスト間の型システム不一致解消(マーシャリング)を取り扱うことになる。

文字列

char/stringは型としてサポートされない。Wasm上で扱うためにはcharをintとして操作し、線形メモリ上にchar配列を構築する形になる。export関数も数値型しか返せないため、stringの表現として線形メモリのオフセットと長さを返し(intなので返せる)これに基づきランタイム側で該当メモリ区間を文字列型として読む、といった工夫が必要となる。あるいはmalloc等の外部アロケータをimportした上で線形メモリの内容を外部メモリへ割り当て、そのアドレス・参照をintでランタイムへ返す必要がある。

ツール

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

バックエンド

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

ランタイム

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

表. WASMランタイム
実行環境 エンジン/ランタイム コンパイラ
Google Chrome & Node.js V8 Liftoff (baseline)[21] + TurboFan (optimizing)[22]
Mozilla Firefox SpiderMonkey[23] rabaldr (baseline) + BaldrMonkey (optimizing, backed by IonMonkey)
wasmtime[24] - Cranelift[25]
Wasmer[26] - pluggable (LLVM, Cranelift)[27]

ランタイムはウェブブラウザへの組み込み(JavaScript実行環境からの呼び出し)や独立したWASMネイティブランタイム (例: CLIにおける $ wasmtime foo.wasm[28]) として存在する。

かつて存在したランタイムにはCraneliftコンパイラベース[29]のLucetがある[30]

開発環境

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

ライブラリ

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

歴史

WebAssemblyサポートの初期実装は、既存の(asm.js)(英語版)PNaClをベースとしている[32][33]

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

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

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

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

ガベージコレクション (GC) の対応を行い、JavaC#といったGCを持つ言語をサポート対象に加えることが計画されている[44]

WebAssemblyの開発はMozillaマイクロソフトGoogleAppleといった主要ブラウザの開発者により行われている[33]

参考文献

  • V8.dev. WebAssembly compilation pipeline.

脚注

[脚注の使い方]
  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. ^ "Control instructions alter control flow. Control flow is structured ... well-nested constructs ... Branches can only target such constructs." WASM specification 1.0
  10. ^ "func ::= {type typeidx, locals vec(valtype), body expr}" WASM specification 1.0
  11. ^ "Code is organized into separate functions. Each function takes a sequence of values as parameters and returns a sequence of values as results." WASM specification 1.0
  12. ^ "Functions may also declare mutable local variables that are usable as virtual registers." WASM specification 1.0
  13. ^ "valtype ::= i32 | i64 | f32 | f64" WASM specification 1.0
  14. ^ サンプルのソースコードはGitHubのWebAssemblyプロジェクトより https://github.com/WebAssembly/design/blob/master/TextFormat.md
  15. ^ http://cultureofdevelopment.com/blog/build-your-first-thing-with-web-assembly/
  16. ^ "2.4.2. Vector Instructions Vector instructions (also known as SIMD instructions, single data multiple value) provide basic operations over values of vector type." W3C. (2022). WebAssembly Core Specification Editor’s Draft, 2 March 2022.
  17. ^ "Feature: WebAssembly SIMD ... Status in Chromium ... Enabled by default" Feature: WebAssembly SIMD. Chrome Platform Status. 2022-03-06閲覧.
  18. ^ a b c d https://github.com/WebAssembly/binaryen
  19. ^ https://github.com/pipcet/asmjs/blob/everything/asmjs.org
  20. ^ 「LLVM 8.0」登場、WebAssemblyを正式サポート OSDN 2019年3月22日
  21. ^ "V8 first compiles a WebAssembly module with its baseline compiler, Liftoff." V8.dev. WebAssembly compilation pipeline.
  22. ^ "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.
  23. ^ "SpiderMonkey is the JavaScript and WebAssembly engine in Firefox." wasmtime (2018). Cranelift in SpiderMonkey.
  24. ^ "A standalone runtime for WebAssembly" bytecodealliance/wasmtime. RAEDME.md.
  25. ^ "Wasmtime is built on the optimizing Cranelift code generator to quickly generate high-quality machine code at runtime." bytecodealliance/wasmtime. RAEDME.md.
  26. ^ "The leading WebAssembly Runtime supporting WASI and Emscripten" wasmerio/wasmer. About.
  27. ^ "Wasmer supports different compilation frameworks to best suit your needs (LLVM, Cranelift...)." wasmerio/wasmer. About.
  28. ^ " you can execute a WebAssembly file" wastime. Using the wasmtime CLI.
  29. ^ "Lucet uses, and is developed in collaboration with, the Bytecode Alliance's Cranelift code generator." bytecodealliance/lucet. README.md.
  30. ^ "Lucet is a native WebAssembly compiler and runtime." bytecodealliance/lucet. README.md.
  31. ^ Aw, all grown up: Mozilla moves WebAssembly into sparsely furnished Studio apartment The Register 2018年4月11日
  32. ^ “WebAssembly: a binary format for the web”. ②ality – JavaScript and more (2015年6月18日). 2017年3月11日閲覧。
  33. ^ a b Bright, Peter (2015年6月18日). “The Web is getting its bytecode: WebAssembly”. Ars Technica. Condé Nast. 2017年3月11日閲覧。
  34. ^ “Launch bug”. GitHub / WebAssembly / design (2015年6月11日). 2017年3月11日閲覧。
  35. ^ Wagner, Luke (2016年3月14日). “A WebAssembly Milestone: Experimental Support in Multiple Browsers”. Mozilla Hacks. 2017年3月11日閲覧。
  36. ^ Thompson, Seth (2016年3月15日). “Experimental support for WebAssembly in V8”. V8 Blog. 2017年3月11日閲覧。
  37. ^ Zhu, Limin (2016年3月15日). “Previewing WebAssembly experiments in Microsoft Edge”. Microsoft Edge dev blog. 2017年3月11日閲覧。
  38. ^ “Announcing Rust 1.14”. The Rust Programming Language Blog (2016年12月22日). 2017年3月11日閲覧。
  39. ^ “Go 1.11 is released”. The Go Blog (2018年8月24日). 2018年10月4日閲覧。
  40. ^ “Kotlin/Native v0.4 released: Objective-C interop, WebAssembly and more”. KOTLIN BLOG (2017年11月16日). 2019年1月12日閲覧。
  41. ^ “Firefox、ゲームなどをネイティブ並に高速実行する「WebAssembly」を採用”. Impress Watch (2017年3月8日). 2017年3月11日閲覧。
  42. ^ 長岡弥太郎 (2017年11月14日). “WebAssemblyが主要ブラウザでサポート - Mozilla official blog”. マイナビニュース. 2017年11月14日閲覧。
  43. ^ 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.
  44. ^ “WebAssembly/design”. GitHub. 2015年12月28日閲覧。

関連項目

外部リンク

  • 公式ウェブサイト (英語)
  • WebAssembly Community Group (英語)
  • WebAssembly Design (英語)
ウィキペディア、ウィキ、本、library、論文、読んだ、ダウンロード、自由、無料ダウンロード、mp3、video、mp4、3gp、 jpg、jpeg、gif、png、画像、音楽、歌、映画、本、ゲーム、ゲーム。