WebAssembly.Module.customSections() - JavaScript 编辑

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

Syntax

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.

Description

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

Examples

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]);
  };
});

Specifications

Specification
WebAssembly JavaScript Interface
The definition of 'customSections()' in that specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:139 次

字数:5418

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文