Skip to content

Commit

Permalink
remove passing AST, redecode AST in Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed May 11, 2018
1 parent 38456ea commit e2c8f3d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
8 changes: 6 additions & 2 deletions lib/wasm/WebAssemblyGenerator.js
Expand Up @@ -6,9 +6,9 @@

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

const { editWithAST, addWithAST } = require("@webassemblyjs/wasm-edit");
const { decode } = require("@webassemblyjs/wasm-parser");
const t = require("@webassemblyjs/ast");

function compose(...fns) {
Expand Down Expand Up @@ -244,9 +244,13 @@ const addInitFunction = ({

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

const ast = decode(bin, {
ignoreDataSection: true,
ignoreCodeSection: true
});

const importedGlobals = getImportedGlobals(ast);
const countImportedFunc = getCountImportedFunc(ast);
const startAtFuncIndex = getStartFuncIndex(ast);
Expand Down
10 changes: 0 additions & 10 deletions lib/wasm/WebAssemblyParser.js
Expand Up @@ -29,30 +29,20 @@ 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

0 comments on commit e2c8f3d

Please sign in to comment.