2018-01-24 06:09:26 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
|
2018-03-09 00:54:06 +08:00
|
|
|
const { RawSource } = require("webpack-sources");
|
|
|
|
|
|
|
|
const { edit, add } = require("@webassemblyjs/wasm-edit");
|
|
|
|
const { decode } = require("@webassemblyjs/wasm-parser");
|
|
|
|
const t = require("@webassemblyjs/ast");
|
|
|
|
|
|
|
|
// FIXME(sven): remove this once we're ready to merge
|
|
|
|
function debug(...msg) {
|
2018-03-12 18:43:20 +08:00
|
|
|
// console.log(...msg);
|
2018-03-09 00:54:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function compose(...fns) {
|
|
|
|
return fns.reverse().reduce((prevFn, nextFn) => {
|
|
|
|
return value => nextFn(prevFn(value));
|
|
|
|
}, value => value);
|
|
|
|
}
|
|
|
|
|
2018-03-12 18:43:20 +08:00
|
|
|
// Utility functions
|
2018-03-09 19:04:27 +08:00
|
|
|
const isGlobalImport = moduleImport => moduleImport.descr.type === "GlobalType";
|
2018-03-12 18:43:20 +08:00
|
|
|
const initFuncId = t.identifier("__init__");
|
2018-03-09 19:04:27 +08:00
|
|
|
|
2018-03-09 00:54:06 +08:00
|
|
|
/**
|
2018-03-12 18:43:20 +08:00
|
|
|
* Removes the start instruction
|
|
|
|
*
|
2018-03-14 21:49:33 +08:00
|
|
|
* @param {Object} state - unused state
|
2018-03-12 18:43:20 +08:00
|
|
|
* @returns {ArrayBuffer} bin'
|
2018-03-09 00:54:06 +08:00
|
|
|
*/
|
2018-03-12 18:43:20 +08:00
|
|
|
const removeStartFunc = state => bin => {
|
|
|
|
debug("removeStartFunc");
|
|
|
|
|
|
|
|
return edit(bin, {
|
|
|
|
Start(path) {
|
|
|
|
path.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-03-09 00:54:06 +08:00
|
|
|
|
2018-03-12 18:43:20 +08:00
|
|
|
function getStartFuncIndex(ast) {
|
2018-03-09 00:54:06 +08:00
|
|
|
let startAtFuncIndex;
|
|
|
|
|
2018-03-12 18:43:20 +08:00
|
|
|
t.traverse(ast, {
|
2018-03-09 00:54:06 +08:00
|
|
|
Start(path) {
|
|
|
|
startAtFuncIndex = path.node.index;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-12 18:43:20 +08:00
|
|
|
return startAtFuncIndex;
|
|
|
|
}
|
2018-03-09 00:54:06 +08:00
|
|
|
|
2018-03-12 18:43:20 +08:00
|
|
|
function getImportedGlobals(ast) {
|
|
|
|
const importedGlobals = [];
|
2018-03-09 00:54:06 +08:00
|
|
|
|
2018-03-12 18:43:20 +08:00
|
|
|
t.traverse(ast, {
|
|
|
|
ModuleImport({ node }) {
|
|
|
|
if (isGlobalImport(node) === true) {
|
|
|
|
importedGlobals.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-03-09 00:54:06 +08:00
|
|
|
|
2018-03-12 18:43:20 +08:00
|
|
|
return importedGlobals;
|
2018-03-09 00:54:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-12 18:43:20 +08:00
|
|
|
* Rewrite the import globals:
|
|
|
|
* - removes the ModuleImport instruction
|
|
|
|
* - injects at the same offset a mutable global of the same time
|
|
|
|
*
|
|
|
|
* Since the imported globals are before the other global declarations, our
|
|
|
|
* indices will be preserved.
|
2018-03-09 19:04:27 +08:00
|
|
|
*
|
2018-03-12 18:43:20 +08:00
|
|
|
* Note that globals will become mutable.
|
|
|
|
*
|
2018-03-14 21:49:33 +08:00
|
|
|
* @param {Object} state - unused state
|
2018-03-09 19:04:27 +08:00
|
|
|
* @returns {ArrayBuffer} bin'
|
|
|
|
*/
|
2018-03-13 00:22:05 +08:00
|
|
|
const rewriteImportedGlobals = state => bin => {
|
2018-03-12 18:43:20 +08:00
|
|
|
debug("rewriteImportedGlobals");
|
|
|
|
|
2018-03-13 00:22:05 +08:00
|
|
|
const newGlobals = [];
|
2018-03-10 02:03:33 +08:00
|
|
|
|
2018-03-13 00:22:05 +08:00
|
|
|
bin = edit(bin, {
|
|
|
|
ModuleImport(path) {
|
|
|
|
if (isGlobalImport(path.node) === true) {
|
|
|
|
const globalType = path.node.descr;
|
2018-03-10 02:03:33 +08:00
|
|
|
|
2018-03-13 00:22:05 +08:00
|
|
|
globalType.mutability = "var";
|
2018-03-10 02:03:33 +08:00
|
|
|
|
2018-03-13 00:22:05 +08:00
|
|
|
newGlobals.push(
|
|
|
|
t.global(globalType, [
|
2018-03-10 02:03:33 +08:00
|
|
|
t.objectInstruction("const", "i32", [t.numberLiteral(0)])
|
2018-03-13 00:22:05 +08:00
|
|
|
])
|
|
|
|
);
|
2018-03-12 18:43:20 +08:00
|
|
|
|
2018-03-13 00:22:05 +08:00
|
|
|
debug("remove import", path.node.module, path.node.name);
|
|
|
|
path.remove();
|
2018-03-09 19:04:27 +08:00
|
|
|
}
|
2018-03-13 00:22:05 +08:00
|
|
|
}
|
|
|
|
});
|
2018-03-12 18:43:20 +08:00
|
|
|
|
2018-03-13 00:22:05 +08:00
|
|
|
// Add global declaration instructions
|
|
|
|
return add(bin, newGlobals);
|
2018-03-12 18:43:20 +08:00
|
|
|
};
|
2018-03-09 19:04:27 +08:00
|
|
|
|
|
|
|
/**
|
2018-03-12 18:43:20 +08:00
|
|
|
* Add an init function.
|
|
|
|
*
|
|
|
|
* The init function fills the globals given input arguments.
|
2018-03-09 19:04:27 +08:00
|
|
|
*
|
2018-03-14 21:49:33 +08:00
|
|
|
* @param {Object} state - transformation state
|
2018-03-09 19:04:27 +08:00
|
|
|
* @returns {ArrayBuffer} bin'
|
|
|
|
*/
|
2018-03-12 18:43:20 +08:00
|
|
|
const addInitFunction = ({
|
|
|
|
startAtFuncIndex,
|
|
|
|
importedGlobals,
|
|
|
|
funcSectionMetadata
|
|
|
|
}) => bin => {
|
|
|
|
debug("addInitFunction");
|
|
|
|
|
|
|
|
const nextTypeIndex = funcSectionMetadata.vectorOfSize;
|
|
|
|
|
|
|
|
const funcParams = importedGlobals.map(importedGlobal => {
|
|
|
|
// used for debugging
|
|
|
|
const id = t.identifier(`${importedGlobal.module}.${importedGlobal.name}`);
|
|
|
|
|
|
|
|
return t.funcParam(importedGlobal.descr.valtype, id);
|
2018-03-09 00:54:06 +08:00
|
|
|
});
|
|
|
|
|
2018-03-12 18:43:20 +08:00
|
|
|
const funcBody = importedGlobals.reduce((acc, importedGlobal, index) => {
|
|
|
|
const args = [t.indexLiteral(index)];
|
|
|
|
const body = [
|
|
|
|
t.instruction("get_local", args),
|
|
|
|
t.instruction("set_global", args)
|
|
|
|
];
|
|
|
|
|
|
|
|
return [...acc, ...body];
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (typeof startAtFuncIndex !== "undefined") {
|
|
|
|
debug("call start func", startAtFuncIndex.value);
|
|
|
|
|
|
|
|
funcBody.push(t.callInstruction(startAtFuncIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
const funcResults = [];
|
|
|
|
|
|
|
|
const func = t.func(initFuncId, funcParams, funcResults, funcBody);
|
2018-03-10 02:03:33 +08:00
|
|
|
|
|
|
|
const functype = t.typeInstructionFunc(func.params, func.result);
|
|
|
|
const funcindex = t.indexInFuncSection(t.indexLiteral(nextTypeIndex));
|
|
|
|
|
|
|
|
const moduleExport = t.moduleExport(
|
2018-03-12 18:43:20 +08:00
|
|
|
initFuncId.value,
|
2018-03-10 02:03:33 +08:00
|
|
|
"Func",
|
2018-03-12 18:43:20 +08:00
|
|
|
t.indexLiteral(nextTypeIndex)
|
2018-03-10 02:03:33 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
return add(bin, [func, moduleExport, funcindex, functype]);
|
2018-03-12 18:43:20 +08:00
|
|
|
};
|
2018-03-09 00:54:06 +08:00
|
|
|
|
2018-01-24 06:09:26 +08:00
|
|
|
class WebAssemblyGenerator {
|
|
|
|
generate(module) {
|
2018-03-09 00:54:06 +08:00
|
|
|
const bin = module.originalSource().source();
|
|
|
|
|
2018-03-12 18:43:20 +08:00
|
|
|
const ast = decode(bin, {
|
|
|
|
ignoreDataSection: true
|
|
|
|
});
|
|
|
|
|
|
|
|
const importedGlobals = getImportedGlobals(ast);
|
|
|
|
const funcSectionMetadata = t.getSectionMetadata(ast, "func");
|
|
|
|
const startAtFuncIndex = getStartFuncIndex(ast);
|
|
|
|
|
|
|
|
const transform = compose(
|
|
|
|
removeStartFunc(),
|
|
|
|
|
2018-03-13 00:22:05 +08:00
|
|
|
rewriteImportedGlobals(),
|
2018-03-12 18:43:20 +08:00
|
|
|
|
|
|
|
addInitFunction({
|
|
|
|
importedGlobals,
|
|
|
|
funcSectionMetadata,
|
|
|
|
startAtFuncIndex
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2018-03-09 00:54:06 +08:00
|
|
|
debug("__________________________________________________________");
|
|
|
|
const newBin = transform(bin);
|
|
|
|
debug("__________________________________________________________");
|
|
|
|
|
2018-03-12 18:43:20 +08:00
|
|
|
// console.log(require("@webassemblyjs/wast-printer").print(decode(newBin)));
|
2018-03-09 00:54:06 +08:00
|
|
|
|
|
|
|
return new RawSource(newBin);
|
2018-01-24 06:09:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = WebAssemblyGenerator;
|