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

句法

WebAssembly.Module.customSections(module, sectionName);
					

参数

module
WebAssembly.Module object whose custom sections are being considered.
sectionName

The string name of the desired custom section.

返回值

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

异常

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

描述

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
					

范例

Using customSections

The following example (see the custom-section.html source and live example ) 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.

WebAssembly.compileStreaming(fetch('simple-name-section.wasm'))
.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]);
  };
});
					

规范

规范
WebAssembly JavaScript 接口
The definition of 'customSections()' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
customSections Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: