files/en-us/webassembly/reference/javascript_interface/validate_static/index.md
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).
WebAssembly.validate(bufferSource)
WebAssembly.validate(bufferSource, compileOptions)
bufferSource
compileOptions {{optional_inline}}
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 {{optional_inline}}
"js-string", which enables JavaScript string builtins.importedStringConstants {{optional_inline}}
A boolean that specifies whether bufferSource is valid Wasm code
(true) or not (false).
If bufferSource is not a typed array or ArrayBuffer,
a {{jsxref("TypeError")}} is thrown.
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.
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`,
);
});
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.
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}}
{{Compat}}