aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/webassembly/global/index.html
blob: 16817b0777e2543767818b3418693e01f1240e8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
title: WebAssembly.Global
slug: Web/JavaScript/Reference/Global_Objects/WebAssembly/Global
tags:
  - API
  - Constructor
  - JavaScript
  - Reference
  - WebAssembly
  - global
translation_of: Web/JavaScript/Reference/Global_Objects/WebAssembly/Global
---
<div>{{JSRef}}</div>

<p><strong><code>WebAssembly.Global</code></strong> はグローバル変数として存在し、JavaScript または {{jsxref("WebAssembly.Module")}} インスタンスから参照することができます。これにより動的に複数のモジュールをリンクすることができます。</p>

<h2 id="Constructor_Syntax" name="Constructor_Syntax">コンストラクターの文法</h2>

<pre class="syntaxbox">var myGlobal = new WebAssembly.Global(<em>descriptor</em>, <em>value</em>);</pre>

<h3 id="Parameters" name="Parameters">パラメーター</h3>

<dl>
 <dt><em>descriptor</em></dt>
 <dd><code>GlobalDescriptor</code> 辞書オブジェクト、2 つの要素を持っている:
 <ul>
  <li><code>value</code>: {{domxref("USVString")}} はグローバルデータ形式を表し値として <code>i32</code><code>i64</code><code>f32</code><code>f64</code> のうち一つを取ります。</li>
  <li><code>mutable</code>: グローバルがミュータブルかどうかの真偽値です。デフォルトでは <code>false</code> です。</li>
 </ul>
 </dd>
 <dt><em>value</em></dt>
 <dd>変数が保持する値です。変数のデータ型に合う限りどんな値でも取れます。もしも何の値も渡されないと、<a href="https://webassembly.github.io/spec/js-api/#defaultvalue"><code>DefaultValue</code> algorithm</a> で指定した時の様な 型ありの 0 が使われます。</dd>
</dl>

<h2 id="Function_properties_of_the_Global_constructor" name="Function_properties_of_the_Global_constructor">グローバルコンストラクターによる関数プロパティ</h2>

<p>無し</p>

<h2 id="Global_instances" name="Global_instances">グローバルインスタンス</h2>

<p>すべてのグローバルインスタンスは <code>Global()</code> コンストラクターのプロパティオブジェクトを受け継ぐ — これによりすべての <code>Global</code> インスタンスを変更できる</p>

<h3 id="Instance_properties" name="Instance_properties">インスタンスプロパティ</h3>

<p>{{page('/ja/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Global/prototype', 'Properties')}}</p>

<h3 id="Instance_methods" name="Instance_methods">インスタンスメソッド</h3>

<p>{{page('/ja/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Global/prototype', 'Methods')}}</p>

<h2 id="Examples" name="Examples"></h2>

<p>以下の例では新しいグローバルインスタンスは <code>WebAssembly.Global()</code> コンストラクターを用いて初期化され、初期値 0 のミュータブルな <code>i32</code> 型として定義されます。</p>

<p>その後この値は、<code>Global.value</code> プロパティを使うことによって <code>42</code> に、<code>global.wasm</code> モジュールから公開された <code>incGlobal()</code> 関数 (入力に限らず 1 を加算する) を使うことによって <code>43</code> になります。</p>

<pre class="brush: js">const output = document.getElementById('output');

function assertEq(msg, got, expected) {
    output.innerHTML += `Testing ${msg}: `;
    if (got !== expected)
        output.innerHTML += `FAIL!&lt;br&gt;Got: ${got}&lt;br&gt;Expected: ${expected}&lt;br&gt;`;
    else
        output.innerHTML += `SUCCESS! Got: ${got}&lt;br&gt;`;
}

assertEq("WebAssembly.Global exists", typeof WebAssembly.Global, "function");

const global = new WebAssembly.Global({value:'i32', mutable:true}, 0);

WebAssembly.instantiateStreaming(fetch('global.wasm'), { js: { global } })
.then(({instance}) =&gt; {
    assertEq("getting initial value from wasm", instance.exports.getGlobal(), 0);
    global.value = 42;
    assertEq("getting JS-updated value from wasm", instance.exports.getGlobal(), 42);
    instance.exports.incGlobal();
    assertEq("getting wasm-updated value from JS", global.value, 43);
});</pre>

<div class="note">
<p><strong>メモ</strong>: <a href="https://mdn.github.io/webassembly-examples/js-api-examples/global.html">GitHub 上で動くデモ</a>が試せます。<a href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/global.html">ソースコード</a>も確認してみてください。</p>
</div>

<h2 id="Specifications" name="Specifications">仕様</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様</th>
   <th scope="col">策定状況</th>
   <th scope="col">コメント</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('WebAssembly JS', '#globals', 'WebAssembly.Global()')}}</td>
   <td>{{Spec2('WebAssembly JS')}}</td>
   <td>初回ドラフト定義</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザー実装状況</h2>

<div>


<p>{{Compat("javascript.builtins.WebAssembly.Global")}}</p>
</div>

<h2 id="See_also" name="See_also">参考</h2>

<ul>
 <li><a href="/ja/docs/WebAssembly">WebAssembly</a> 概要ページ</li>
 <li><a href="/ja/docs/WebAssembly/Concepts">WebAssembly とは何か</a></li>
 <li><a href="/ja/docs/WebAssembly/Using_the_JavaScript_API">WebAssembly JavaScript API を使用する</a></li>
 <li><a href="https://github.com/WebAssembly/mutable-global/blob/master/proposals/mutable-global/Overview.md">Import/Export mutable globals proposal</a></li>
</ul>