Movatterモバイル変換


[0]ホーム

URL:


  1. WebAssembly
  2. Reference
  3. WebAssembly
  4. WebAssembly.Global

WebAssembly.Global

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨March 2020⁩.

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

Constructor

WebAssembly.Global()

Creates a newGlobal object.

Global instances

AllGlobal instances inherit from theGlobal() constructor's prototype object — this can be modified to affect allGlobal instances.

Instance properties

Global.prototype.constructor

Returns the function that created this object's instance. By default this is theWebAssembly.Global() constructor.

Global.prototype[Symbol.toStringTag]

The initial value of the[Symbol.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.

Instance methods

Global.prototype.valueOf()

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

Examples

Creating a new Global instance

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

The value of the global is then changed, first to42 using theGlobal.value property, and then to 43 using theincGlobal() function exported out of theglobal.wasm module (this adds 1 to whatever value is given to it and then returns the new value).

js
const output = document.getElementById("output");function assertEq(msg, got, expected) {  const result =    got === expected      ? `SUCCESS! Got: ${got}\n`      : `FAIL!\nGot: ${got}\nExpected: ${expected}\n`;  output.innerText += `Testing ${msg}: ${result}`;}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 examplerunning live on GitHub; see also thesource code.

Specifications

Specification
WebAssembly JavaScript Interface
# globals

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp