MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/d6d7ff2e2f9c

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The WebAssembly.customSections() function returns a copy of the contents of all custom sections in the given module with the given string name.

Syntax

var custSec = WebAssembly.Module.customSections(module, sectionName);

Parameters

module
The WebAssembly.Module object whose custom sections are being considered.
sectionName
The string name of the desired custom section.

Return value

A (possibly empty) array containing ArrayBuffer copies of the contents of all custom sections matching sectionName.

Exceptions

If module is not a WebAssembly.Module object instance, a TypeError is thrown.

Custom sections

A wasm module is comprised of a sequence of sections. Most of these sections are fully specified and validated by the wasm spec, but modules can also contain custom sections that are ignored and skipped over during validation. (Read High level structure for information on section structures, and how normal sections ("known sections") and custom sections are distinguished.)

This provides developers with a way to include custom data inside wasm modules for other purposes, for example the name custom section, which allows developers to provide names for all the functions and locals in the module (like "symbols" in a native build).

Note that the WebAssembly text format currently doesn't have a syntax specified for adding new custom sections; you can however add a name section to your wasm during conversion from text format over to .wasm. The wast2wasm command available as part of the wabt tool has a --debug-names option — specify this during conversion to get a .wasm with a names custom section, for example:

wast2wasm simple-name-section.was -o simple-name-section.wasm --debug-names

Example

The following example (see the custom-section.html source and live example) compiles and compiles the loaded simple-name-section.wasm byte code.

We then do a check using WebAssembly.Module.customSections, looking to see whether the module instance contains a "name" custom section by checking that its length is more than 0. Since there is a "name" section in the example, an ArrayBuffer object is returned.

fetch('simple-name-section.wasm').then(response =>
  response.arrayBuffer()
).then(bytes =>
  WebAssembly.compile(bytes)
).then(function(mod) {
  var nameSections = WebAssembly.Module.customSections(mod, "name");
  if (nameSections.length != 0) {
    console.log("Module contains a name section");
    console.log(nameSections[0]);
  };
});

Specifications

Specification Status Comment
Web Assembly JavaScript API
The definition of 'customSections()' in that specification.
Draft Initial draft definition.

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic Support5716522 No4411
FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic Support5757 (Yes)1522 No ?11

1. , this feature is behind the Experimental JavaScript Features preference.

2. Disabled in the Firefox 52 Extended Support Release (ESR).

See also

Document Tags and Contributors

 Contributors to this page: fscholz, chrisdavidmills, lukewagner, svodnik
 Last updated by: fscholz,