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.
var myMemory = new WebAssembly.Memory(memoryDescriptor);
Note: A WebAssembly page is a fixed unit of memory size, equal to 64KB.
memoryDescriptor is not of type object, a TypeError is thrown.maximum is specified and is smaller than initial, a RangeError is thrown.Memory constructor methodsNone.
Memory instancesAll Memory instances inherit from the Memory() constructor's prototype object — this can be modified to affect all Memory instances.
Memory.prototype.constructorWebAssembly.Memory() constructor.Memory.prototype.bufferMemory.prototype.grow()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);
})
})
}); | Specification | Status | Comment |
|---|---|---|
| Web Assembly JavaScript API The definition of 'Memory' in that specification. | Draft | Initial draft definition. |
| 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.
© 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