Skip to content

Commit 6e85ac5

Browse files
committed
feat: use function matcher
1 parent 956c0b1 commit 6e85ac5

File tree

3 files changed

+14
-46
lines changed

3 files changed

+14
-46
lines changed

declarations.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ declare module "@webassemblyjs/ast" {
139139
args: string[];
140140
result: string[];
141141
}
142+
143+
// Node matcher
144+
export function isGlobalType(n: Node): boolean;
145+
export function isTable(n: Node): boolean;
146+
export function isMemory(n: Node): boolean;
147+
export function isFuncImportDescr(n: Node): boolean;
142148
}
143149

144150
/**

lib/wasm/WebAssemblyGenerator.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,6 @@ function compose(...fns) {
4545
}, value => value);
4646
}
4747

48-
// Utility functions
49-
50-
/**
51-
* @param {t.ModuleImport} n the import
52-
* @returns {boolean} true, if a global was imported
53-
*/
54-
const isGlobalImport = n => n.descr.type === "GlobalType";
55-
56-
/**
57-
* @param {t.ModuleImport} n the import
58-
* @returns {boolean} true, if a func was imported
59-
*/
60-
const isFuncImport = n => n.descr.type === "FuncImportDescr";
61-
6248
// TODO replace with @callback
6349

6450
/**
@@ -104,7 +90,7 @@ function getImportedGlobals(ast) {
10490

10591
t.traverse(ast, {
10692
ModuleImport({ node }) {
107-
if (isGlobalImport(node) === true) {
93+
if (t.isGlobalType(node.descr) === true) {
10894
importedGlobals.push(node);
10995
}
11096
}
@@ -118,7 +104,7 @@ function getCountImportedFunc(ast) {
118104

119105
t.traverse(ast, {
120106
ModuleImport({ node }) {
121-
if (isFuncImport(node) === true) {
107+
if (t.isFuncImportDescr(node.descr) === true) {
122108
count++;
123109
}
124110
}
@@ -206,7 +192,7 @@ const rewriteImportedGlobals = state => bin => {
206192

207193
bin = editWithAST(state.ast, bin, {
208194
ModuleImport(path) {
209-
if (isGlobalImport(path.node) === true) {
195+
if (t.isGlobalType(path.node.descr) === true) {
210196
const globalType = path.node.descr;
211197

212198
globalType.mutability = "var";

lib/wasm/WebAssemblyParser.js

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,6 @@ const WebAssemblyExportImportedDependency = require("../dependencies/WebAssembly
1616

1717
/** @typedef {import("../Module")} Module */
1818

19-
/**
20-
* @param {t.ModuleImport} n the import
21-
* @returns {boolean} true, if a memory was imported
22-
*/
23-
const isMemoryImport = n => n.descr.type === "Memory";
24-
25-
/**
26-
* @param {t.ModuleImport} n the import
27-
* @returns {boolean} true, if a table was imported
28-
*/
29-
const isTableImport = n => n.descr.type === "Table";
30-
31-
/**
32-
* @param {t.ModuleImport} n the import
33-
* @returns {boolean} true, if a func was imported
34-
*/
35-
const isFuncImport = n => n.descr.type === "FuncImportDescr";
36-
37-
/**
38-
* @param {t.ModuleImport} n the import
39-
* @returns {boolean} true, if a global was imported
40-
*/
41-
const isGlobalImport = n => n.descr.type === "GlobalType";
42-
4319
const JS_COMPAT_TYPES = new Set(["i32", "f32", "f64"]);
4420

4521
/**
@@ -160,16 +136,16 @@ class WebAssemblyParser extends Tapable {
160136
/** @type {false | string} */
161137
let onlyDirectImport = false;
162138

163-
if (isMemoryImport(node) === true) {
139+
if (t.isMemory(node.descr) === true) {
164140
onlyDirectImport = "Memory";
165-
} else if (isTableImport(node) === true) {
141+
} else if (t.isTable(node.descr) === true) {
166142
onlyDirectImport = "Table";
167-
} else if (isFuncImport(node) === true) {
143+
} else if (t.isFuncImportDescr(node.descr) === true) {
168144
const incompatibleType = getJsIncompatibleType(node.descr.signature);
169145
if (incompatibleType) {
170146
onlyDirectImport = `Non-JS-compatible Func Sigurature (${incompatibleType})`;
171147
}
172-
} else if (isGlobalImport(node) === true) {
148+
} else if (t.isGlobalType(node.descr) === true) {
173149
const type = node.descr.valtype;
174150
if (!JS_COMPAT_TYPES.has(type)) {
175151
onlyDirectImport = `Non-JS-compatible Global Type (${type})`;
@@ -185,7 +161,7 @@ class WebAssemblyParser extends Tapable {
185161

186162
state.module.addDependency(dep);
187163

188-
if (isGlobalImport(node)) {
164+
if (t.isGlobalType(node.descr)) {
189165
importedGlobals.push(node);
190166
}
191167
}

0 commit comments

Comments
 (0)