e01.org

WebAssembly.validate()

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

The **`WebAssembly.validate()`** static method validates a given [typed array](/docs/Web/JavaScript/Guide/Typed_arrays) of WebAssembly binary
code, returning whether the bytes form a valid Wasm module (`true`) or not
(`false`).

## Syntax

```js-nolint
WebAssembly.validate(bufferSource)
WebAssembly.validate(bufferSource, compileOptions)
```

### Parameters

- `bufferSource`
- : A [typed array](/docs/Web/JavaScript/Guide/Typed_arrays) or [ArrayBuffer](/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
containing WebAssembly binary code to be validated.
- `compileOptions`
- : An object containing compilation options. This parameter is included on the `validate()` method so that it can be used to validate modules when the compilation options are present (for example, to implement feature detection). Properties can include:
- `builtins`
- : An array of strings that enables the usage of [JavaScript builtins](/docs/WebAssembly/Guides/JavaScript_builtins) in the compiled Wasm module. The strings define the builtins you want to enable. Currently the only available value is `"js-string"`, which enables JavaScript string builtins.
- `importedStringConstants`
- : A string specifying a namespace for [imported global string constants](/docs/WebAssembly/Guides/Imported_string_constants). This property needs to be specified if you wish to use imported global string constants in the Wasm module.

### Return value

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

### Exceptions

If `bufferSource` is not a [typed array](/docs/Web/JavaScript/Guide/Typed_arrays) or [ArrayBuffer](/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer),
a TypeError is thrown.

## Examples

### Using validate

The following example (see the validate.html [source code](https://github.com/mdn/webassembly-examples/blob/main/js-api-examples/validate.html),
and [see it live too](https://mdn.github.io/webassembly-examples/js-api-examples/validate.html))
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`,
);
});
```

### Validating a module with JavaScript builtins and global string imports enabled

This example validates a Wasm module with JavaScript string builtins and imported global string constants enabled, logging `"Wasm module valid: true"` to the console if it is valid, and `"Wasm module valid: false"` if it isn't. [See it running live](https://mdn.github.io/webassembly-examples/js-builtin-examples/validate/).

```js
const compileOptions = {
builtins: ["js-string"], // Enable JavaScript string builtins
importedStringConstants: "string_constants", // Enable imported global string constants
};

fetch("log-concat.wasm")
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.validate(bytes, compileOptions))
.then((result) => console.log(`Wasm module valid: ${result}`));
```

## Specifications

## Browser compatibility

## See also

- [WebAssembly](/docs/WebAssembly) overview
- [WebAssembly concepts](/docs/WebAssembly/Guides/Concepts)
- [Using the WebAssembly JavaScript API](/docs/WebAssembly/Guides/Using_the_JavaScript_API)

source: MDN Web Docs mirror · page

Cite this

"WebAssembly.validate()". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface/validate_static

JSON record · API collection · citation policy