W3cubDocs

/JavaScript

WebAssembly.memory

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The Memory() constructor of the WebAssembly global object creates a new Memory object instance, which represents a WebAssembly memory. These can be thought of as resizeable array buffers, wrappers around WebAssembly memorys.

A memory created by JavaScript or in WebAssembly code will be accessible and mutable from both JavaScript and WebAssembly.

Syntax

var myMemory = new WebAssembly.Memory(memoryDescriptor);

Parameters

memoryDescriptor
An object that can contain the following members:
initial
The initial size of the WebAssembly Memory, in WebAssembly pages.
maximum Optional
The maximum size the WebAssembly Memory is allowed to grow to, in WebAssembly pages.

Note: A WebAssembly page is a fixed unit of memory size, equal to 64KB.

Exceptions

  • If memoryDescriptor is not of type object, a TypeError is thrown.
  • If maximum is specified and is smaller than initial, a RangeError is thrown.

Memory constructor methods

None.

Memory instances

All Memory instances inherit from the Memory() constructor's prototype object — this can be modified to affect all Memory instances.

Instance properties

Memory.prototype.constructor
Returns the function that created this object's instance. By default this is the WebAssembly.Memory() constructor.
Memory.prototype.buffer
An accessor property that returns the buffer contained in the memory.

Instance methods

Memory.prototype.grow()
Increases the size of the memory instance by a specified number of WebAssembly pages (each one is 64KB in size).

Examples

The following example creates a new WebAssembly Memory instance with an initial size of 10 pages (640KB), and a maximum size of 100 pages (6.4MB).

var memory = new WebAssembly.Memory({initial:10, maximum:100});

Note that you can also represent a memory instance in JavaScript by exporting it from a WebAssembly module; the following example (see memory.html on GitHub, and view it live also) fetches and instantiates the loaded memory.wasm byte code using our fetchAndInstantiate() utility function. It then exports a memory from the module, stores some values in it, then exports a function and uses it to sum some values.

fetch('memory.wasm').then(function(response) {
  response.arrayBuffer().then(function(bytes) {
    WebAssembly.instantiate(bytes).then(function(obj) {
      var i32 = new Uint32Array(obj.instance.exports.mem.buffer);
      for (var i = 0; i < 10; i++) {
        i32[i] = i;
      }
      var sum = obj.instance.exports.accumulate(0, 10);
      console.log(sum);
    })
  })
});

Specifications

Specification Status Comment
Web Assembly JavaScript API
The definition of 'Memory' in that specification.
Draft Initial draft definition.

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support No support[1] No support No support[2] No support No support[1] No support
Feature Android Android Webview Edge Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support No support No support[1] No support No support[2] No support No support No support No support[1]

[1] Experimental support can be enabled in Chrome 51+ and Opera 38+ by going to chrome://flags and enabling the Experimental WebAssembly flag.

[2] Experimental support can be enabled in Firefox 47+ by enabling the javascript.options.wasm flag in about:config.

See also

  • Memory instance page
  • WebAssembly landing page.
  • WebAssembly concepts
  • Using the WebAssembly JavaScript API

© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory