feat: uses new APIs

This commit is contained in:
Sven SAULEAU 2018-05-09 17:40:38 +02:00
parent 85b5ee33e2
commit d491fdc786
No known key found for this signature in database
GPG Key ID: F5464AC83B687AD1
3 changed files with 39 additions and 21 deletions

15
declarations.d.ts vendored
View File

@ -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;
}

View File

@ -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");
@ -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
@ -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();
}
@ -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;
@ -168,7 +178,7 @@ const rewriteImportedGlobals = state => bin => {
});
// Add global declaration instructions
return add(bin, newGlobals);
return addWithAST(state.ast, bin, newGlobals);
};
/**
@ -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 => {
@ -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 {
@ -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

View File

@ -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,