1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const t = require("@webassemblyjs/ast");
|
8 | const { decode } = require("@webassemblyjs/wasm-parser");
|
9 | const {
|
10 | moduleContextFromModuleAST
|
11 | } = require("@webassemblyjs/helper-module-context");
|
12 |
|
13 | const { Tapable } = require("tapable");
|
14 | const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
|
15 | const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
|
16 |
|
17 |
|
18 |
|
19 | const JS_COMPAT_TYPES = new Set(["i32", "f32", "f64"]);
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | const getJsIncompatibleType = signature => {
|
26 | for (const param of signature.params) {
|
27 | if (!JS_COMPAT_TYPES.has(param.valtype)) {
|
28 | return `${param.valtype} as parameter`;
|
29 | }
|
30 | }
|
31 | for (const type of signature.results) {
|
32 | if (!JS_COMPAT_TYPES.has(type)) return `${type} as result`;
|
33 | }
|
34 | return null;
|
35 | };
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const getJsIncompatibleTypeOfFuncSignature = signature => {
|
43 | for (const param of signature.args) {
|
44 | if (!JS_COMPAT_TYPES.has(param)) {
|
45 | return `${param} as parameter`;
|
46 | }
|
47 | }
|
48 | for (const type of signature.result) {
|
49 | if (!JS_COMPAT_TYPES.has(type)) return `${type} as result`;
|
50 | }
|
51 | return null;
|
52 | };
|
53 |
|
54 | const decoderOpts = {
|
55 | ignoreCodeSection: true,
|
56 | ignoreDataSection: true,
|
57 |
|
58 |
|
59 | ignoreCustomNameSection: true
|
60 | };
|
61 |
|
62 | class WebAssemblyParser extends Tapable {
|
63 | constructor(options) {
|
64 | super();
|
65 | this.hooks = {};
|
66 | this.options = options;
|
67 | }
|
68 |
|
69 | parse(binary, state) {
|
70 |
|
71 | state.module.buildMeta.exportsType = "namespace";
|
72 |
|
73 |
|
74 | const program = decode(binary, decoderOpts);
|
75 | const module = program.body[0];
|
76 |
|
77 | const moduleContext = moduleContextFromModuleAST(module);
|
78 |
|
79 |
|
80 | const exports = (state.module.buildMeta.providedExports = []);
|
81 | const jsIncompatibleExports = (state.module.buildMeta.jsIncompatibleExports = []);
|
82 |
|
83 | const importedGlobals = [];
|
84 | t.traverse(module, {
|
85 | ModuleExport({ node }) {
|
86 | const descriptor = node.descr;
|
87 |
|
88 | if (descriptor.exportType === "Func") {
|
89 | const funcidx = descriptor.id.value;
|
90 |
|
91 |
|
92 | const funcSignature = moduleContext.getFunction(funcidx);
|
93 |
|
94 | const incompatibleType = getJsIncompatibleTypeOfFuncSignature(
|
95 | funcSignature
|
96 | );
|
97 |
|
98 | if (incompatibleType) {
|
99 | jsIncompatibleExports[node.name] = incompatibleType;
|
100 | }
|
101 | }
|
102 |
|
103 | exports.push(node.name);
|
104 |
|
105 | if (node.descr && node.descr.exportType === "Global") {
|
106 | const refNode = importedGlobals[node.descr.id.value];
|
107 | if (refNode) {
|
108 | const dep = new WebAssemblyExportImportedDependency(
|
109 | node.name,
|
110 | refNode.module,
|
111 | refNode.name,
|
112 | refNode.descr.valtype
|
113 | );
|
114 |
|
115 | state.module.addDependency(dep);
|
116 | }
|
117 | }
|
118 | },
|
119 |
|
120 | Global({ node }) {
|
121 | const init = node.init[0];
|
122 |
|
123 | let importNode = null;
|
124 |
|
125 | if (init.id === "get_global") {
|
126 | const globalIdx = init.args[0].value;
|
127 |
|
128 | if (globalIdx < importedGlobals.length) {
|
129 | importNode = importedGlobals[globalIdx];
|
130 | }
|
131 | }
|
132 |
|
133 | importedGlobals.push(importNode);
|
134 | },
|
135 |
|
136 | ModuleImport({ node }) {
|
137 |
|
138 | let onlyDirectImport = false;
|
139 |
|
140 | if (t.isMemory(node.descr) === true) {
|
141 | onlyDirectImport = "Memory";
|
142 | } else if (t.isTable(node.descr) === true) {
|
143 | onlyDirectImport = "Table";
|
144 | } else if (t.isFuncImportDescr(node.descr) === true) {
|
145 | const incompatibleType = getJsIncompatibleType(node.descr.signature);
|
146 | if (incompatibleType) {
|
147 | onlyDirectImport = `Non-JS-compatible Func Sigurature (${incompatibleType})`;
|
148 | }
|
149 | } else if (t.isGlobalType(node.descr) === true) {
|
150 | const type = node.descr.valtype;
|
151 | if (!JS_COMPAT_TYPES.has(type)) {
|
152 | onlyDirectImport = `Non-JS-compatible Global Type (${type})`;
|
153 | }
|
154 | }
|
155 |
|
156 | const dep = new WebAssemblyImportDependency(
|
157 | node.module,
|
158 | node.name,
|
159 | node.descr,
|
160 | onlyDirectImport
|
161 | );
|
162 |
|
163 | state.module.addDependency(dep);
|
164 |
|
165 | if (t.isGlobalType(node.descr)) {
|
166 | importedGlobals.push(node);
|
167 | }
|
168 | }
|
169 | });
|
170 |
|
171 | return state;
|
172 | }
|
173 | }
|
174 |
|
175 | module.exports = WebAssemblyParser;
|