Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

WebAssembly.validate()

BaselineWidely available *

TheWebAssembly.validate() static method validates a giventyped array of WebAssembly binarycode, returning whether the bytes form a valid Wasm module (true) or not(false).

Syntax

js
WebAssembly.validate(bufferSource)WebAssembly.validate(bufferSource, compileOptions)

Parameters

bufferSource

Atyped array orArrayBuffercontaining WebAssembly binary code to be validated.

compileOptionsOptional

An object containing compilation options. This parameter is included on thevalidate() 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:

builtinsOptional

An array of strings that enables the usage ofJavaScript 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.

importedStringConstantsOptional

A string specifying a namespace forimported global 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 whetherbufferSource is valid Wasm code(true) or not (false).

Exceptions

IfbufferSource is not atyped array orArrayBuffer,aTypeError is thrown.

Examples

Using validate

The following example (see the validate.htmlsource code,andsee it live too)fetches a Wasm module and converts it into a typed array.Thevalidate() 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.

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

Specification
WebAssembly JavaScript Interface
# dom-webassembly-validate

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp