A WebAssembly.Global object represents a global variable instance, accessible from both JavaScript and importable/exportable across one or more WebAssembly.Module instances. This allows dynamic linking of multiple modules.

构造函数

WebAssembly.Global()
创建新的 Global 对象。

Global instances

所有 Global instances inherit from the Global() constructor's prototype object — this can be modified to affect all Global 实例。

实例特性

Global.prototype.constructor
Returns the function that created this object's instance. By default this is the WebAssembly.Global() 构造函数。
Global.prototype[@@toStringTag]
The initial value of the @@toStringTag property is the String value "WebAssembly.Global".
Global.prototype.value

The value contained inside the global variable — this can be used to directly set and get the global's value.

实例方法

Global.prototype.valueOf()

Old-style method that returns the value contained inside the global variable.

范例

Creating a new Global instance

The following example shows a new global instance being created using the WebAssembly.Global() constructor. It is being defined as a mutable i32 type, with a value of 0.

The value of the global is then changed, first to 42 使用 Global.value property, and then to 43 using the incGlobal() function exported out of the global.wasm module (this adds 1 to whatever value is given to it and then returns the new value).

const output = document.getElementById('output');
function assertEq(msg, got, expected) {
    output.innerHTML += `Testing ${msg}: `;
    if (got !== expected)
        output.innerHTML += `FAIL!<br>Got: ${got}<br>Expected: ${expected}<br>`;
    else
        output.innerHTML += `SUCCESS! Got: ${got}<br>`;
}
assertEq("WebAssembly.Global exists", typeof WebAssembly.Global, "function");
const global = new WebAssembly.Global({value:'i32', mutable:true}, 0);
WebAssembly.instantiateStreaming(fetch('global.wasm'), { js: { global } })
.then(({instance}) => {
    assertEq("getting initial value from wasm", instance.exports.getGlobal(), 0);
    global.value = 42;
    assertEq("getting JS-updated value from wasm", instance.exports.getGlobal(), 42);
    instance.exports.incGlobal();
    assertEq("getting wasm-updated value from JS", global.value, 43);
});
					

注意 : You can see the example running live on GitHub ; see also the source code .

规范

规范
WebAssembly JavaScript 接口
The definition of 'WebAssembly.Global()' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
Global Chrome 69 Edge No Firefox 62 IE No Opera No Safari No WebView Android 69 Chrome Android 69 Firefox Android 62 Opera Android No Safari iOS No Samsung Internet Android 10.0 nodejs No
Global() 构造函数 Chrome 69 Edge No Firefox 62 IE No Opera No Safari No WebView Android 69 Chrome Android 69 Firefox Android 62 Opera Android No Safari iOS No Samsung Internet Android 10.0 nodejs No
value Chrome 69 Edge No Firefox 62 IE No Opera No Safari No WebView Android 69 Chrome Android 69 Firefox Android 62 Opera Android No Safari iOS No Samsung Internet Android 10.0 nodejs No
valueOf Chrome 69 Edge No Firefox 62 IE No Opera No Safari No WebView Android 69 Chrome Android 69 Firefox Android 62 Opera Android No Safari iOS No Samsung Internet Android 10.0 nodejs No

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: