WebAssembly.Instance

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2017.

Ein WebAssembly.Instance-Objekt ist eine zustandsbehaftete, ausführbare Instanz eines WebAssembly.Module. Instance-Objekte enthalten alle exportierten WebAssembly-Funktionen, die das Aufrufen von WebAssembly-Code aus JavaScript ermöglichen.

Konstruktor

WebAssembly.Instance()

Erstellt ein neues Instance-Objekt.

Instanzeigenschaften

exports

Gibt ein Objekt zurück, das als seine Mitglieder alle Funktionen enthält, die aus der WebAssembly-Modulinstanz exportiert wurden, sodass sie von JavaScript aus erreichbar und nutzbar sind. Read-only.

Beispiele

Synchrones Instanziieren eines WebAssembly-Moduls

Die WebAssembly.Instance()-Konstruktorfunktion kann aufgerufen werden, um ein gegebenes WebAssembly.Module-Objekt synchron zu instanziieren, zum Beispiel:

js
const importObject = {
  my_namespace: {
    imported_func(arg) {
      console.log(arg);
    },
  },
};

fetch("simple.wasm")
  .then((response) => response.arrayBuffer())
  .then((bytes) => {
    const mod = new WebAssembly.Module(bytes);
    const instance = new WebAssembly.Instance(mod, importObject);
    instance.exports.exported_func();
  });

Der bevorzugte Weg, um eine Instance zu erhalten, ist asynchron, z. B. durch Verwendung der WebAssembly.instantiateStreaming()-Funktion wie folgt:

js
const importObject = {
  my_namespace: {
    imported_func(arg) {
      console.log(arg);
    },
  },
};

WebAssembly.instantiateStreaming(fetch("simple.wasm"), importObject).then(
  (obj) => obj.instance.exports.exported_func(),
);

Dies zeigt auch, wie die exports-Eigenschaft genutzt wird, um auf exportierte Funktionen zuzugreifen.

Spezifikationen

Specification
WebAssembly JavaScript Interface
# instances

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
Instance
Instance() constructor
exports

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch