forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebAssemblyParser.js
More file actions
79 lines (62 loc) · 1.66 KB
/
WebAssemblyParser.js
File metadata and controls
79 lines (62 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const t = require("@webassemblyjs/ast");
const { decode } = require("@webassemblyjs/wasm-parser");
const { Tapable } = require("tapable");
const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
/** @typedef {import("../Module")} Module */
/**
* @param {t.ModuleImport} n the import
* @returns {boolean} true, if a memory was imported
*/
const isMemoryImport = n => n.descr.type === "Memory";
/**
* @param {t.ModuleImport} n the import
* @returns {boolean} true, if a table was imported
*/
const isTableImport = n => n.descr.type === "Table";
const decoderOpts = {
ignoreCodeSection: true,
ignoreDataSection: true
};
class WebAssemblyParser extends Tapable {
constructor(options) {
super();
this.hooks = {};
this.options = options;
}
parse(binary, state) {
// flag it as ESM
state.module.buildMeta.exportsType = "namespace";
// parse it
const ast = decode(binary, decoderOpts);
// extract imports and exports
const exports = (state.module.buildMeta.providedExports = []);
t.traverse(ast, {
ModuleExport({ node }) {
exports.push(node.name);
},
ModuleImport({ node }) {
let onlyDirectImport = false;
if (isMemoryImport(node) === true) {
onlyDirectImport = true;
}
if (isTableImport(node) === true) {
onlyDirectImport = true;
}
const dep = new WebAssemblyImportDependency(
node.module,
node.name,
node.descr,
onlyDirectImport
);
state.module.addDependency(dep);
}
});
return state;
}
}
module.exports = WebAssemblyParser;