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 object.All Global instances inherit from the Global() constructor's prototype object — this can be modified to affect all Global instances.
Global.prototype.constructorWebAssembly.Global() constructor.Global.prototype[@@toStringTag]Global.prototype.valueGlobal.prototype.valueOf()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 using the 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);
}); Note: You can see the example running live on GitHub; see also the source code.
| Specification |
|---|
| WebAssembly JavaScript Interface The definition of 'WebAssembly.Global()' in that specification. |
| Desktop | ||||||
|---|---|---|---|---|---|---|
Global |
69 | No | 62 | No | No | 13.1 |
Global() constructor |
69 | No | 62 | No | No | 13.1 |
value |
69 | No | 62 | No | No | 13.1 |
valueOf |
69 | No | 62 | No | No | 13.1 |
| Mobile | ||||||
|---|---|---|---|---|---|---|
Global |
69 | 69 | 62 | No | 13.4 | 10.0 |
Global() constructor |
69 | 69 | 62 | No | 13.4 | 10.0 |
value |
69 | 69 | 62 | No | 13.4 | 10.0 |
valueOf |
69 | 69 | 62 | No | 13.4 | 10.0 |
| Server | |
|---|---|
Global |
No |
Global() constructor |
No |
value |
No |
valueOf |
No |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Global