[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const t = require("@webassemblyjs/ast");
|
---|
| 9 | const { decode } = require("@webassemblyjs/wasm-parser");
|
---|
| 10 | const EnvironmentNotSupportAsyncWarning = require("../EnvironmentNotSupportAsyncWarning");
|
---|
| 11 | const Parser = require("../Parser");
|
---|
| 12 | const StaticExportsDependency = require("../dependencies/StaticExportsDependency");
|
---|
| 13 | const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
|
---|
| 14 |
|
---|
| 15 | /** @typedef {import("../Module").BuildInfo} BuildInfo */
|
---|
| 16 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
---|
| 17 | /** @typedef {import("../Parser").ParserState} ParserState */
|
---|
| 18 | /** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
|
---|
| 19 |
|
---|
| 20 | const decoderOpts = {
|
---|
| 21 | ignoreCodeSection: true,
|
---|
| 22 | ignoreDataSection: true,
|
---|
| 23 |
|
---|
| 24 | // this will avoid having to lookup with identifiers in the ModuleContext
|
---|
| 25 | ignoreCustomNameSection: true
|
---|
| 26 | };
|
---|
| 27 |
|
---|
| 28 | class WebAssemblyParser extends Parser {
|
---|
| 29 | /**
|
---|
| 30 | * @param {{}=} options parser options
|
---|
| 31 | */
|
---|
| 32 | constructor(options) {
|
---|
| 33 | super();
|
---|
| 34 | this.hooks = Object.freeze({});
|
---|
| 35 | this.options = options;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * @param {string | Buffer | PreparsedAst} source the source to parse
|
---|
| 40 | * @param {ParserState} state the parser state
|
---|
| 41 | * @returns {ParserState} the parser state
|
---|
| 42 | */
|
---|
| 43 | parse(source, state) {
|
---|
| 44 | if (!Buffer.isBuffer(source)) {
|
---|
| 45 | throw new Error("WebAssemblyParser input must be a Buffer");
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | // flag it as async module
|
---|
| 49 | const buildInfo = /** @type {BuildInfo} */ (state.module.buildInfo);
|
---|
| 50 | buildInfo.strict = true;
|
---|
| 51 | const BuildMeta = /** @type {BuildMeta} */ (state.module.buildMeta);
|
---|
| 52 | BuildMeta.exportsType = "namespace";
|
---|
| 53 | BuildMeta.async = true;
|
---|
| 54 | EnvironmentNotSupportAsyncWarning.check(
|
---|
| 55 | state.module,
|
---|
| 56 | state.compilation.runtimeTemplate,
|
---|
| 57 | "asyncWebAssembly"
|
---|
| 58 | );
|
---|
| 59 |
|
---|
| 60 | // parse it
|
---|
| 61 | const program = decode(source, decoderOpts);
|
---|
| 62 | const module = program.body[0];
|
---|
| 63 | /** @type {Array<string>} */
|
---|
| 64 | const exports = [];
|
---|
| 65 | t.traverse(module, {
|
---|
| 66 | ModuleExport({ node }) {
|
---|
| 67 | exports.push(node.name);
|
---|
| 68 | },
|
---|
| 69 |
|
---|
| 70 | ModuleImport({ node }) {
|
---|
| 71 | const dep = new WebAssemblyImportDependency(
|
---|
| 72 | node.module,
|
---|
| 73 | node.name,
|
---|
| 74 | node.descr,
|
---|
| 75 | false
|
---|
| 76 | );
|
---|
| 77 |
|
---|
| 78 | state.module.addDependency(dep);
|
---|
| 79 | }
|
---|
| 80 | });
|
---|
| 81 |
|
---|
| 82 | state.module.addDependency(new StaticExportsDependency(exports, false));
|
---|
| 83 |
|
---|
| 84 | return state;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | module.exports = WebAssemblyParser;
|
---|