WebAssembly.validate()

The WebAssembly.validate() function validates a given typed array of WebAssembly binary code, returning whether the bytes form a valid Wasm module (true) or not (false).

Syntax

js

WebAssembly.validate(bufferSource)

Parameters

bufferSource

A typed array or ArrayBuffer containing WebAssembly binary code to be validated.

Return value

A boolean that specifies whether bufferSource is valid Wasm code (true) or not (false).

Exceptions

If bufferSource is not a typed array or ArrayBuffer, a TypeError is thrown.

Examples

Using validate

The following example (see the validate.html source code, and see it live too) fetches a Wasm module and converts it into a typed array. The validate() method is then used to check whether the module is valid.

js

fetch("simple.wasm")
  .then((response) => response.arrayBuffer())
  .then((bytes) => {
    const valid = WebAssembly.validate(bytes);
    console.log(
      `The given bytes are ${valid ? "" : "not "}a valid Wasm module`
    );
  });

Specifications

Specification
WebAssembly JavaScript Interface
# dom-webassembly-validate

Browser compatibility

BCD tables only load in the browser

See also