WebAssembly.Memory()
构造函数创建一个新的 Memory
对象。它包含一个可调整大小的 ArrayBuffer ,其内存储的是 WebAssembly 实例
所访问内存的原始字节码。
从 JavaScript 或 WebAssembly 中所创建的内存,可以由 JavaScript 或 WebAssembly 来访问及更改。
var myMemory = new WebAssembly.Memory(memoryDescriptor);
最大值
。当存在此属性时,此参数用于提示引擎预先保留内存。但是,引擎可能会忽略或限制此预留请求。通常情况下大多数 WebAssembly 模块不需要设置 最大值
。注意: A WebAssembly 页面的大小为一个常量 65,536 字节,即64KB。
memoryDescriptor
的类型不是对象,则抛出 TypeError
异常。maximum
并且小于 initial
,则抛出 RangeError
异常。Memory
实例所有 Memory
实例都继承自 Memory()
构造函数的 原型对象 — 这个原型可被修改并影响到所有的 Memory
实例。
Memory.prototype.constructor
WebAssembly.Memory()
构造函数。Memory.prototype.buffer
Memory.prototype.grow()
有两种方法可以获得 WebAssembly.Memory
对象。第一种方法是由 JavaScript 来创建。以下示例创建了一个新的 WebAssembly 内存实例,初始大小为 10页(640KB),最大值设置为 100页(6.4MB)。
var memory = new WebAssembly.Memory({initial:10, maximum:100});
获取 WebAssembly.Memory
对象的第二种方法是从 WebAssembly 模块中导出。以下示例 (详见GitHub页面 memory.html ,也可以 用浏览器运行查看) 使用 WebAssembly.instantiateStreaming()
方法实例化已加载的 memory.wasm 字节代码,同时导入上面一行中创建的内存。用它来存储一些值,然后导出一个函数并用它来对一些值进行求和操作。
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);
});
规范 | 状态 | 备注 |
---|---|---|
WebAssembly JavaScript Interface Memory |
Working Draft | 初步定义草案 |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Memory | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Edge Mobile
Full support
Yes
| Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
buffer | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Edge Mobile
Full support
Yes
| Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
grow | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Edge Mobile
Full support
Yes
| Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
prototype | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Edge Mobile
Full support
Yes
| Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |