Merge pull request #7413 from xtuc/feat-wasm-global-in-global-initilizer2
wasm global with global as initializer
This commit is contained in:
commit
e56a4c57d1
|
@ -178,6 +178,7 @@ function getNextFuncIndex(ast, countImportedFunc) {
|
|||
* @returns {ArrayBufferTransform} transform
|
||||
*/
|
||||
const rewriteImportedGlobals = state => bin => {
|
||||
const additionalInitCode = state.additionalInitCode;
|
||||
const newGlobals = [];
|
||||
|
||||
bin = editWithAST(state.ast, bin, {
|
||||
|
@ -212,7 +213,34 @@ const rewriteImportedGlobals = state => bin => {
|
|||
// in order to preserve non-imported global's order we need to re-inject
|
||||
// those as well
|
||||
Global(path) {
|
||||
newGlobals.push(path.node);
|
||||
const { node } = path;
|
||||
const [init] = node.init;
|
||||
|
||||
if (init.id === "get_global") {
|
||||
node.globalType.mutability = "var";
|
||||
|
||||
const initialGlobalidx = init.args[0];
|
||||
|
||||
const valtype = node.globalType.valtype;
|
||||
|
||||
node.init = [
|
||||
// Poisong globals, they are meant to be rewritten
|
||||
t.objectInstruction("const", valtype, [t.numberLiteralFromRaw(666)])
|
||||
];
|
||||
|
||||
additionalInitCode.push(
|
||||
/**
|
||||
* get_global in global initilizer only work for imported globals.
|
||||
* They have the same indices than the init params, so use the
|
||||
* same index.
|
||||
*/
|
||||
t.instruction("get_local", [initialGlobalidx]),
|
||||
t.instruction("set_global", [t.indexLiteral(newGlobals.length)])
|
||||
);
|
||||
}
|
||||
|
||||
newGlobals.push(node);
|
||||
|
||||
path.remove();
|
||||
}
|
||||
});
|
||||
|
@ -274,6 +302,7 @@ const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
|
|||
* @param {t.Identifier} state.initFuncId identifier of the init function
|
||||
* @param {t.IndexLiteral} state.startAtFuncIndex index of the start function
|
||||
* @param {t.ModuleImport[]} state.importedGlobals list of imported globals
|
||||
* @param {t.Instruction[]} state.additionalInitCode list of addition instructions for the init function
|
||||
* @param {t.IndexLiteral} state.nextFuncIndex index of the next function
|
||||
* @param {t.IndexLiteral} state.nextTypeIndex index of the next type
|
||||
* @returns {ArrayBufferTransform} transform
|
||||
|
@ -283,6 +312,7 @@ const addInitFunction = ({
|
|||
initFuncId,
|
||||
startAtFuncIndex,
|
||||
importedGlobals,
|
||||
additionalInitCode,
|
||||
nextFuncIndex,
|
||||
nextTypeIndex
|
||||
}) => bin => {
|
||||
|
@ -307,6 +337,10 @@ const addInitFunction = ({
|
|||
funcBody.push(t.callInstruction(startAtFuncIndex));
|
||||
}
|
||||
|
||||
for (const instr of additionalInitCode) {
|
||||
funcBody.push(instr);
|
||||
}
|
||||
|
||||
const funcResults = [];
|
||||
|
||||
// Code section
|
||||
|
@ -370,6 +404,9 @@ class WebAssemblyGenerator extends Generator {
|
|||
|
||||
const usedDependencyMap = getUsedDependencyMap(module);
|
||||
|
||||
/** @type {t.Instruction[]} */
|
||||
const additionalInitCode = [];
|
||||
|
||||
const transform = compose(
|
||||
rewriteExportNames({
|
||||
ast,
|
||||
|
@ -378,7 +415,7 @@ class WebAssemblyGenerator extends Generator {
|
|||
|
||||
removeStartFunc({ ast }),
|
||||
|
||||
rewriteImportedGlobals({ ast }),
|
||||
rewriteImportedGlobals({ ast, additionalInitCode }),
|
||||
|
||||
rewriteImports({
|
||||
ast,
|
||||
|
@ -389,6 +426,7 @@ class WebAssemblyGenerator extends Generator {
|
|||
ast,
|
||||
initFuncId,
|
||||
importedGlobals,
|
||||
additionalInitCode,
|
||||
startAtFuncIndex,
|
||||
nextFuncIndex,
|
||||
nextTypeIndex
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
export const n = 33;
|
|
@ -0,0 +1,5 @@
|
|||
it("should allow global with imported global as initilizer", function() {
|
||||
return import("./module.wat").then(function({get}) {
|
||||
expect(get()).toEqual(33);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,9 @@
|
|||
(module
|
||||
(import "./env.js" "n" (global i32))
|
||||
(global i32 (get_global 0))
|
||||
|
||||
(func (export "get") (result i32)
|
||||
(get_global 1)
|
||||
)
|
||||
)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
var supportsWebAssembly = require("../../../helpers/supportsWebAssembly");
|
||||
|
||||
module.exports = function(config) {
|
||||
return supportsWebAssembly();
|
||||
};
|
|
@ -6187,8 +6187,8 @@ walker@~1.0.5:
|
|||
makeerror "1.0.x"
|
||||
|
||||
wast-loader@^1.5.5:
|
||||
version "1.5.5"
|
||||
resolved "https://registry.yarnpkg.com/wast-loader/-/wast-loader-1.5.5.tgz#a1d6cdc468b6963a6e479de683dc74755f09b81a"
|
||||
version "1.5.7"
|
||||
resolved "https://registry.yarnpkg.com/wast-loader/-/wast-loader-1.5.7.tgz#1b165cef7225c70a7e82a50e4f8bf0bc1ec9f2fd"
|
||||
dependencies:
|
||||
wabt "^1.0.0"
|
||||
|
||||
|
|
Loading…
Reference in New Issue