Movatterモバイル変換


[0]ホーム

URL:


  1. WebAssembly
  2. Reference
  3. WebAssembly
  4. WebAssembly.Memory
  5. WebAssembly.Memory.prototype.grow()

WebAssembly.Memory.prototype.grow()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨October 2017⁩.

Thegrow() prototype method of theWebAssembly.Memory object increases the size of the memory instance by a specified number of WebAssembly pages.

Syntax

js
grow(delta)

Parameters

delta

The number of WebAssembly pages you want to grow the memory by (each one is 64KiB in size).

Return value

The previous size of the memory, in units of WebAssembly pages.

Exceptions

  • RangeError: If the current size added withdelta exceeds the Memory instance's maximum size capacity.

Examples

Using grow

The following example creates a new WebAssembly Memory instance with an initial size of 1 page (64KiB), and a maximum size of 10 pages (640KiB).

js
const memory = new WebAssembly.Memory({  initial: 1,  maximum: 10,});

We can then grow the instance by one page like so:

js
const bytesPerPage = 64 * 1024;console.log(memory.buffer.byteLength / bytesPerPage); // "1"console.log(memory.grow(1)); // "1"console.log(memory.buffer.byteLength / bytesPerPage); // "2"

Note the return value ofgrow() here is the previous number of WebAssembly pages.

Detachment upon growing

Every call togrow will detach any references to the oldbuffer, even forgrow(0)!Detachment means that theArrayBuffer'sbyteLength becomes zero, and it no longer has bytes accessible to JavaScript.Accessing thebuffer property after callinggrow, will yield anArrayBuffer with the correct length.

js
const memory = new WebAssembly.Memory({  initial: 1,});const oldMemoryView = new Uint8Array(memory.buffer);memory.grow(1);// the array is empty!console.log(oldMemoryView); // Uint8Array []
js
const memory = new WebAssembly.Memory({  initial: 1,});memory.grow(1);const currentMemoryView = new Uint8Array(memory.buffer);// the array is full of zerosconsole.log(currentMemoryView); // Uint8Array(131072) [ 0, 0, 0, ... ]// 131072 = 64KiB * 2

For a sharedMemory instance, the initialbuffer (which would be aSharedArrayBuffer in such case) will not become detached, but rather its length will not be updated. Accesses to thebuffer property after growing will yield a largerSharedArrayBuffer which may access a larger span of memory than the buffer from before growing theMemory. EverySharedArrayBuffer from thebuffer property will all refer to the start of the same memory address range, and thus manipulate the same data.

Specifications

Specification
WebAssembly JavaScript Interface
# dom-memory-grow

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp