Skip to content

Commit

Permalink
feat: uses new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
xtuc committed May 9, 2018
1 parent 85b5ee3 commit d491fdc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
15 changes: 12 additions & 3 deletions declarations.d.ts
Expand Up @@ -33,19 +33,28 @@ declare module "chrome-trace-event" {
declare module "@webassemblyjs/ast" {
export function traverse(
ast: any,
visitor: { [name: string]: (context: { node: Node }) => void }
visitor: {
ModuleImport?: (p: NodePath<ModuleImport>) => void;
ModuleExport?: (p: NodePath<ModuleExport>) => void;
Start?: (p: NodePath<Start>) => void;
}
);
export class Node {
index: number;
export class NodePath<T> {
node: T;
}
export class Node {}
export class Identifier extends Node {
value: string;
}
export class Start extends Node {
index: Identifier;
}
export class ModuleImport extends Node {
module: string;
descr: {
type: string;
valtype: string;
id: string;
};
name: string;
}
Expand Down
37 changes: 23 additions & 14 deletions lib/wasm/WebAssemblyGenerator.js
Expand Up @@ -7,7 +7,7 @@
const Generator = require("../Generator");
const { RawSource } = require("webpack-sources");

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

Expand All @@ -18,9 +18,19 @@ function compose(...fns) {
}

// Utility functions
const isGlobalImport = moduleImport => moduleImport.descr.type === "GlobalType";
const isFuncImport = moduleImport =>
moduleImport.descr.type === "FuncImportDescr";

/**
* @param {t.ModuleImport} n the import
* @returns {boolean} true, if a global was imported
*/
const isGlobalImport = n => n.descr.type === "GlobalType";

/**
* @param {t.ModuleImport} n the import
* @returns {boolean} true, if a func was imported
*/
const isFuncImport = n => n.descr.type === "FuncImportDescr";

const initFuncId = t.identifier("__webpack_init__");

// TODO replace with @callback
Expand All @@ -35,7 +45,7 @@ const initFuncId = t.identifier("__webpack_init__");
* @returns {ArrayBufferTransform} transform
*/
const removeStartFunc = state => bin => {
return edit(bin, {
return editWithAST(state.ast, bin, {
Start(path) {
path.remove();
}
Expand Down Expand Up @@ -149,7 +159,7 @@ function getNextFuncIndex(ast, countImportedFunc) {
const rewriteImportedGlobals = state => bin => {
const newGlobals = [];

bin = edit(bin, {
bin = editWithAST(state.ast, bin, {
ModuleImport(path) {
if (isGlobalImport(path.node) === true) {
const globalType = path.node.descr;
Expand All @@ -168,7 +178,7 @@ const rewriteImportedGlobals = state => bin => {
});

// Add global declaration instructions
return add(bin, newGlobals);
return addWithAST(state.ast, bin, newGlobals);
};

/**
Expand All @@ -177,17 +187,17 @@ const rewriteImportedGlobals = state => bin => {
* The init function fills the globals given input arguments.
*
* @param {Object} state transformation state
* @param {Object} state.ast - Module's ast
* @param {t.IndexLiteral} state.startAtFuncIndex index of the start function
* @param {t.ModuleImport[]} state.importedGlobals list of imported globals
* @param {TODO} state.funcSectionMetadata ??
* @param {t.IndexLiteral} state.nextFuncIndex index of the next function
* @param {t.IndexLiteral} state.nextTypeIndex index of the next type
* @returns {ArrayBufferTransform} transform
*/
const addInitFunction = ({
ast,
startAtFuncIndex,
importedGlobals,
funcSectionMetadata,
nextFuncIndex,
nextTypeIndex
}) => bin => {
Expand Down Expand Up @@ -229,7 +239,7 @@ const addInitFunction = ({
// Export section
const moduleExport = t.moduleExport(initFuncId.value, "Func", nextFuncIndex);

return add(bin, [func, moduleExport, funcindex, functype]);
return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
};

class WebAssemblyGenerator extends Generator {
Expand All @@ -244,20 +254,19 @@ class WebAssemblyGenerator extends Generator {
});

const importedGlobals = getImportedGlobals(ast);
const funcSectionMetadata = t.getSectionMetadata(ast, "func");
const countImportedFunc = getCountImportedFunc(ast);
const startAtFuncIndex = getStartFuncIndex(ast);
const nextFuncIndex = getNextFuncIndex(ast, countImportedFunc);
const nextTypeIndex = getNextTypeIndex(ast);

const transform = compose(
removeStartFunc({}),
removeStartFunc({ ast }),

rewriteImportedGlobals({}),
rewriteImportedGlobals({ ast }),

addInitFunction({
ast,
importedGlobals,
funcSectionMetadata,
startAtFuncIndex,
nextFuncIndex,
nextTypeIndex
Expand Down
8 changes: 4 additions & 4 deletions lib/wasm/WebAssemblyParser.js
Expand Up @@ -11,16 +11,16 @@ const { Tapable } = require("tapable");
const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");

/**
* @param {t.ModuleImport} moduleImport the import
* @param {t.ModuleImport} n the import
* @returns {boolean} true, if a memory was imported
*/
const isMemoryImport = moduleImport => moduleImport.descr.type === "Memory";
const isMemoryImport = n => n.descr.type === "Memory";

/**
* @param {t.ModuleImport} moduleImport the import
* @param {t.ModuleImport} n the import
* @returns {boolean} true, if a table was imported
*/
const isTableImport = moduleImport => moduleImport.descr.type === "Table";
const isTableImport = n => n.descr.type === "Table";

const decoderOpts = {
ignoreCodeSection: true,
Expand Down

0 comments on commit d491fdc

Please sign in to comment.