aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/webassembly/memory/index.html
blob: f3f0afebe52843c1dff9a7c4489fc66e0c02950f (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
---
title: WebAssembly.Memory()
slug: Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory
translation_of: Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory
---
<div>{{JSRef}}</div>

<p><code><strong>WebAssembly.Memory()</strong></code> 构造函数创建一个新的 <code>Memory</code> 对象。该对象的 {{jsxref("WebAssembly/Memory/buffer","buffer")}} 属性是一个可调整大小的 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">ArrayBuffer</a> ,其内存储的是 WebAssembly <code>实例</code> 所访问内存的原始字节码。</p>

<p>从 JavaScript 或 WebAssembly 中所创建的内存,可以由 JavaScript 或 WebAssembly 来访问及更改。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">var myMemory = new WebAssembly.Memory(memoryDescriptor);</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><em>memoryDescriptor</em></dt>
 <dd>一个可包含以下成员的对象:
 <dl>
  <dt><em>initial</em></dt>
  <dd>WebAssembly 内存的初始大小,以 WebAssembly 页面为单位。</dd>
  <dt><em>maximum {{optional_inline}}</em></dt>
  <dd>以 WebAssembly 页面为单位,可允许 WebAssembly 内存的 <code>最大值</code>。当存在此属性时,此参数用于提示引擎预先保留内存。但是,引擎可能会忽略或限制此预留请求。通常情况下大多数 WebAssembly 模块不需要设置 <code>最大值</code></dd>
 </dl>
 </dd>
</dl>

<div class="note">
<p><strong>备注:</strong>: A WebAssembly 页面的大小为一个常量 65,536 字节,即64KB。</p>
</div>

<h3 id="异常">异常</h3>

<ul>
 <li>如果 <code>memoryDescriptor</code> 的类型不是对象,则抛出 {{jsxref("TypeError")}} 异常。</li>
 <li>如果指定了 <code>maximum</code> 并且小于 <code>initial</code>,则抛出 {{jsxref("RangeError")}} 异常。</li>
</ul>

<h2 id="Memory_实例"><code>Memory</code> 实例</h2>

<p>所有 <code>Memory</code> 实例都继承自 <code>Memory()</code> 构造函数的 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory/prototype">原型对象</a> — 这个原型可被修改并影响到所有的 <code>Memory</code> 实例。</p>

<h3 id="实例属性">实例属性</h3>

<dl>
 <dt><code>Memory.prototype.constructor</code></dt>
 <dd>返回创建此对象实例的函数。默认情况下,它是 {{jsxref("WebAssembly.Memory()")}} 构造函数。</dd>
 <dt>{{jsxref("WebAssembly/Memory/buffer","Memory.prototype.buffer")}}</dt>
 <dd>一个访问器,用于返回内存中包含的缓冲区。</dd>
</dl>

<h3 id="实例方法">实例方法</h3>

<dl>
 <dt>{{jsxref("WebAssembly/Memory/grow","Memory.prototype.grow()")}}</dt>
 <dd>通过指定 WebAssembly 页面数量来增加内存实例的大小。(每个页面大小为64KB)</dd>
</dl>

<h2 id="示例">示例</h2>

<p>有两种方法可以获得 <code>WebAssembly.Memory</code> 对象。第一种方法是由 JavaScript 来创建。以下示例创建了一个新的 WebAssembly 内存实例,初始大小为 10页(640KB),最大值设置为 100页(6.4MB)。</p>

<pre class="brush: js">var memory = new WebAssembly.Memory({initial:10, maximum:100});</pre>

<p>获取 <code>WebAssembly.Memory</code> 对象的第二种方法是从 WebAssembly 模块中导出。以下示例 (详见GitHub页面 <a href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/memory.html">memory.html</a> ,也可以 <a href="https://mdn.github.io/webassembly-examples/js-api-examples/memory.html">用浏览器运行查看</a>) 使用 {{jsxref("WebAssembly.instantiateStreaming()")}} 方法实例化已加载的 memory.wasm 字节代码,同时导入上面一行中创建的内存。用它来存储一些值,然后导出一个函数并用它来对一些值进行求和操作。</p>

<pre class="brush: js">WebAssembly.instantiateStreaming(fetch('memory.wasm'), { js: { mem: memory } })
.then(obj =&gt; {
  var i32 = new Uint32Array(memory.buffer);
  for (var i = 0; i &lt; 10; i++) {
    i32[i] = i;
  }
  var sum = obj.instance.exports.accumulate(0, 10);
  console.log(sum);
});</pre>

<h2 id="标准规范">标准规范</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', '#webassemblymemory-objects', 'Memory')}}</td>
   <td>{{Spec2('WebAssembly JS')}}</td>
   <td>初步定义草案</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

{{Compat}}

<h2 id="参见">参见</h2>

<ul>
 <li><a href="/en-US/docs/WebAssembly">WebAssembly</a> overview page</li>
 <li><a href="/en-US/docs/WebAssembly/Concepts">WebAssembly concepts</a></li>
 <li><a href="/en-US/docs/WebAssembly/Using_the_JavaScript_API">Using the WebAssembly JavaScript API</a></li>
</ul>