Skip to content

Commit

Permalink
remove _ast variable, add WeakMap
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed May 10, 2018
1 parent 8bdc8ad commit 85ef634
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/wasm/WebAssemblyGenerator.js
Expand Up @@ -6,6 +6,7 @@

const Generator = require("../Generator");
const { RawSource } = require("webpack-sources");
const WebAssemblyParser = require("./WebAssemblyParser");

const { editWithAST, addWithAST } = require("@webassemblyjs/wasm-edit");
const t = require("@webassemblyjs/ast");
Expand Down Expand Up @@ -243,7 +244,7 @@ const addInitFunction = ({

class WebAssemblyGenerator extends Generator {
generate(module) {
const ast = module._ast;
const ast = WebAssemblyParser.getAst(module);
const bin = module.originalSource().source();

const importedGlobals = getImportedGlobals(ast);
Expand Down
14 changes: 12 additions & 2 deletions lib/wasm/WebAssemblyParser.js
Expand Up @@ -10,6 +10,8 @@ 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
Expand All @@ -27,20 +29,30 @@ const decoderOpts = {
ignoreDataSection: true
};

/** @type {WeakMap<Module, TODO>} */
const astStore = new WeakMap();

class WebAssemblyParser extends Tapable {
constructor(options) {
super();
this.hooks = {};
this.options = options;
}

static getAst(module) {
return astStore.get(module);
}

parse(binary, state) {
// flag it as ESM
state.module.buildMeta.exportsType = "namespace";

// parse it
const ast = decode(binary, decoderOpts);

// cache it to be available for generators
astStore.set(state.module, ast);

// extract imports and exports
const exports = (state.module.buildMeta.providedExports = []);
t.traverse(ast, {
Expand Down Expand Up @@ -70,8 +82,6 @@ class WebAssemblyParser extends Tapable {
}
});

state.module._ast = ast;

return state;
}
}
Expand Down

0 comments on commit 85ef634

Please sign in to comment.