diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
| commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
| tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/webassembly/memory | |
| parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
| download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip | |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/webassembly/memory')
| -rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/webassembly/memory/index.html | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/webassembly/memory/index.html b/files/zh-cn/web/javascript/reference/global_objects/webassembly/memory/index.html new file mode 100644 index 0000000000..1d559210a4 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/webassembly/memory/index.html @@ -0,0 +1,112 @@ +--- +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 => { + var i32 = new Uint32Array(memory.buffer); + for (var i = 0; i < 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="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("javascript.builtins.WebAssembly.Memory")}}</p> +</div> + +<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> |
