source: frontend/node_modules/webpack/lib/wasm-async/AsyncWebAssemblyParser.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const t = require("@webassemblyjs/ast");
9const { decode } = require("@webassemblyjs/wasm-parser");
10const Parser = require("../Parser");
11const StaticExportsDependency = require("../dependencies/StaticExportsDependency");
12const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
13const EnvironmentNotSupportAsyncWarning = require("../errors/EnvironmentNotSupportAsyncWarning");
14
15/** @typedef {import("./AsyncWebAssemblyModulesPlugin").AsyncWasmModuleClass} AsyncWasmModule */
16/** @typedef {import("../Module").BuildInfo} BuildInfo */
17/** @typedef {import("../Module").BuildMeta} BuildMeta */
18/** @typedef {import("../NormalModule")} NormalModule */
19/** @typedef {import("../Parser").ParserState} ParserState */
20/** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
21
22const WASM_HEADER = Buffer.from([0x00, 0x61, 0x73, 0x6d]);
23
24const decoderOpts = {
25 ignoreCodeSection: true,
26 ignoreDataSection: true,
27
28 // this will avoid having to lookup with identifiers in the ModuleContext
29 ignoreCustomNameSection: true
30};
31
32class WebAssemblyParser extends Parser {
33 /**
34 * Parses the provided source and updates the parser state.
35 * @param {string | Buffer | PreparsedAst} source the source to parse
36 * @param {ParserState} state the parser state
37 * @returns {ParserState} the parser state
38 */
39 parse(source, state) {
40 if (!Buffer.isBuffer(source)) {
41 throw new Error("WebAssemblyParser input must be a Buffer");
42 }
43
44 const buildMeta = /** @type {BuildMeta} */ (state.module.buildMeta);
45 buildMeta.exportsType = "namespace";
46 buildMeta.async = true;
47
48 EnvironmentNotSupportAsyncWarning.check(
49 state.module,
50 state.compilation.runtimeTemplate,
51 "asyncWebAssembly"
52 );
53
54 // flag it as async module
55 const buildInfo = /** @type {BuildInfo} */ (state.module.buildInfo);
56
57 buildInfo.strict = true;
58
59 if (/** @type {AsyncWasmModule} */ (state.module).phase === "source") {
60 // For source phase, only validate magic header
61 if (source.length < 4 || !source.subarray(0, 4).equals(WASM_HEADER)) {
62 throw new Error(
63 "Source phase imports require valid WebAssembly modules. Invalid magic header (expected \\0asm)."
64 );
65 }
66
67 // Source phase exports the WebAssembly.Module as default
68 state.module.addDependency(
69 new StaticExportsDependency(["default"], false)
70 );
71
72 // Skip full parsing - no exports/imports needed for source phase
73 return state;
74 }
75
76 // parse it
77 const program = decode(source, decoderOpts);
78 const module = program.body[0];
79 /** @type {string[]} */
80 const exports = [];
81
82 t.traverse(module, {
83 ModuleExport({ node }) {
84 exports.push(node.name);
85 },
86
87 ModuleImport({ node }) {
88 const dep = new WebAssemblyImportDependency(
89 node.module,
90 node.name,
91 node.descr,
92 false
93 );
94
95 state.module.addDependency(dep);
96 }
97 });
98
99 state.module.addDependency(new StaticExportsDependency(exports, false));
100
101 return state;
102 }
103}
104
105module.exports = WebAssemblyParser;
Note: See TracBrowser for help on using the repository browser.