webpack/lib/wasm-sync/WebAssemblyParser.js

192 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-10-30 20:56:57 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
2017-10-30 20:56:57 +08:00
"use strict";
2018-04-28 00:53:07 +08:00
const t = require("@webassemblyjs/ast");
2021-01-08 06:50:33 +08:00
const { moduleContextFromModuleAST } = require("@webassemblyjs/ast");
2018-07-30 23:08:51 +08:00
const { decode } = require("@webassemblyjs/wasm-parser");
const Parser = require("../Parser");
const StaticExportsDependency = require("../dependencies/StaticExportsDependency");
const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
2018-07-30 23:08:51 +08:00
const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
2017-10-30 20:56:57 +08:00
2018-05-11 02:19:30 +08:00
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Parser").ParserState} ParserState */
/** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
2018-05-11 02:19:30 +08:00
2018-05-11 20:24:08 +08:00
const JS_COMPAT_TYPES = new Set(["i32", "f32", "f64"]);
/**
2018-06-04 22:23:41 +08:00
* @param {t.Signature} signature the func signature
* @returns {null | string} the type incompatible with js types
*/
2018-06-04 22:23:41 +08:00
const getJsIncompatibleType = signature => {
for (const param of signature.params) {
2018-06-04 22:23:41 +08:00
if (!JS_COMPAT_TYPES.has(param.valtype)) {
return `${param.valtype} as parameter`;
2018-06-04 22:23:41 +08:00
}
}
for (const type of signature.results) {
if (!JS_COMPAT_TYPES.has(type)) return `${type} as result`;
}
return null;
};
2018-06-04 22:23:41 +08:00
/**
* TODO why are there two different Signature types?
* @param {t.FuncSignature} signature the func signature
* @returns {null | string} the type incompatible with js types
*/
const getJsIncompatibleTypeOfFuncSignature = signature => {
for (const param of signature.args) {
if (!JS_COMPAT_TYPES.has(param)) {
return `${param} as parameter`;
}
}
for (const type of signature.result) {
if (!JS_COMPAT_TYPES.has(type)) return `${type} as result`;
}
return null;
};
2018-02-28 15:50:29 +08:00
const decoderOpts = {
ignoreCodeSection: true,
ignoreDataSection: true,
// this will avoid having to lookup with identifiers in the ModuleContext
ignoreCustomNameSection: true
2018-02-28 15:50:29 +08:00
};
class WebAssemblyParser extends Parser {
2017-10-30 20:56:57 +08:00
constructor(options) {
super();
2018-07-30 20:25:40 +08:00
this.hooks = Object.freeze({});
2017-10-30 20:56:57 +08:00
this.options = options;
}
/**
* @param {string | Buffer | PreparsedAst} source the source to parse
* @param {ParserState} state the parser state
* @returns {ParserState} the parser state
*/
parse(source, state) {
if (!Buffer.isBuffer(source)) {
throw new Error("WebAssemblyParser input must be a Buffer");
}
// flag it as ESM
state.module.buildInfo.strict = true;
state.module.buildMeta.exportsType = "namespace";
// parse it
const program = decode(source, decoderOpts);
const module = program.body[0];
const moduleContext = moduleContextFromModuleAST(module);
// extract imports and exports
const exports = [];
let jsIncompatibleExports = (state.module.buildMeta.jsIncompatibleExports = undefined);
const importedGlobals = [];
t.traverse(module, {
2018-03-01 22:07:43 +08:00
ModuleExport({ node }) {
const descriptor = node.descr;
if (descriptor.exportType === "Func") {
2020-03-13 00:51:26 +08:00
const funcIdx = descriptor.id.value;
2018-06-04 22:23:41 +08:00
/** @type {t.FuncSignature} */
2020-03-13 00:51:26 +08:00
const funcSignature = moduleContext.getFunction(funcIdx);
2018-06-04 22:23:41 +08:00
const incompatibleType = getJsIncompatibleTypeOfFuncSignature(
funcSignature
);
2018-06-04 22:23:41 +08:00
if (incompatibleType) {
if (jsIncompatibleExports === undefined) {
jsIncompatibleExports = state.module.buildMeta.jsIncompatibleExports = {};
}
2018-06-04 22:23:41 +08:00
jsIncompatibleExports[node.name] = incompatibleType;
}
}
2018-05-09 23:53:25 +08:00
exports.push(node.name);
if (node.descr && node.descr.exportType === "Global") {
const refNode = importedGlobals[node.descr.id.value];
if (refNode) {
const dep = new WebAssemblyExportImportedDependency(
node.name,
refNode.module,
refNode.name,
refNode.descr.valtype
);
state.module.addDependency(dep);
}
}
},
Global({ node }) {
const init = node.init[0];
let importNode = null;
if (init.id === "get_global") {
const globalIdx = init.args[0].value;
if (globalIdx < importedGlobals.length) {
importNode = importedGlobals[globalIdx];
}
}
importedGlobals.push(importNode);
},
2018-03-01 22:07:43 +08:00
ModuleImport({ node }) {
/** @type {false | string} */
let onlyDirectImport = false;
2018-06-06 16:39:06 +08:00
if (t.isMemory(node.descr) === true) {
onlyDirectImport = "Memory";
2018-06-06 16:39:06 +08:00
} else if (t.isTable(node.descr) === true) {
onlyDirectImport = "Table";
2018-06-06 16:39:06 +08:00
} else if (t.isFuncImportDescr(node.descr) === true) {
2018-06-04 22:23:41 +08:00
const incompatibleType = getJsIncompatibleType(node.descr.signature);
if (incompatibleType) {
2020-03-13 00:51:26 +08:00
onlyDirectImport = `Non-JS-compatible Func Signature (${incompatibleType})`;
}
2018-06-06 16:39:06 +08:00
} else if (t.isGlobalType(node.descr) === true) {
2018-06-04 22:23:41 +08:00
const type = node.descr.valtype;
if (!JS_COMPAT_TYPES.has(type)) {
onlyDirectImport = `Non-JS-compatible Global Type (${type})`;
}
}
2018-03-09 00:54:06 +08:00
const dep = new WebAssemblyImportDependency(
2018-05-09 23:53:25 +08:00
node.module,
node.name,
node.descr,
onlyDirectImport
2018-03-09 00:54:06 +08:00
);
state.module.addDependency(dep);
2018-06-06 16:39:06 +08:00
if (t.isGlobalType(node.descr)) {
importedGlobals.push(node);
}
}
});
state.module.addDependency(new StaticExportsDependency(exports, false));
return state;
2017-10-30 20:56:57 +08:00
}
}
module.exports = WebAssemblyParser;