Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function createCompiledModule(ast: Program): CompiledModule {
const exports: Array = [];
const imports = [];
let start;
// Do compile-time ast manipulation in order to remove WAST
// semantics during execution
denormalizeTypeReferences(ast);
wastIdentifierToIndex(ast);
validateAST(ast);
t.traverse(ast, {
ModuleExport({ node }: NodePath) {
if (node.descr.exportType === "Func") {
exports.push({
name: node.name,
kind: "function"
});
}
},
Start({ node }: NodePath) {
if (typeof start !== "undefined") {
throw new CompileError("Multiple start functions is not allowed");
}
export function createInstance(
allocator: Allocator,
node: Global
): GlobalInstance {
let value;
const { valtype, mutability } = node.globalType;
// None or multiple constant expressions in the initializer seems not possible
// TODO(sven): find a specification reference for that
// FIXME(sven): +1 because of the implicit end, change the order of validations
if (node.init.length > 2 || node.init.length === 1) {
throw new CompileError("type mismatch");
}
// Validate the type
const resultInferedType = getType(node.init);
if (
resultInferedType != null &&
typeEq([node.globalType.valtype], resultInferedType) === false
) {
throw new CompileError("type mismatch");
}
const res = evaluate(allocator, node.init);
if (res != null) {
value = res.value;
}
return {
type: valtype,
let value;
const { valtype, mutability } = node.globalType;
// None or multiple constant expressions in the initializer seems not possible
// TODO(sven): find a specification reference for that
// FIXME(sven): +1 because of the implicit end, change the order of validations
if (node.init.length > 2 || node.init.length === 1) {
throw new CompileError("type mismatch");
}
// Validate the type
const resultInferedType = getType(node.init);
if (
resultInferedType != null &&
typeEq([node.globalType.valtype], resultInferedType) === false
) {
throw new CompileError("type mismatch");
}
const res = evaluate(allocator, node.init);
if (res != null) {
value = res.value;
}
return {
type: valtype,
mutability,
value
};
}
function createModuleInstanceFromAst(moduleNode, enableTypeChecking = false) {
const internalInstanceOptions = {
checkForI64InSignature: false,
returnStackLocal: true
};
const importObject = {
_internalInstanceOptions: internalInstanceOptions
};
if (enableTypeChecking === true) {
denormalizeTypeReferences(moduleNode);
const typeErrors = typeCheck(t.program([moduleNode]));
if (typeErrors.length > 0) {
const containsImmutableGlobalViolation = typeErrors.some(
x => x === "global is immutable"
);
if (containsImmutableGlobalViolation) {
throw new Error("global is immutable");
}
throw new Error("type mismatch");
}
}
const compiledModule = createCompiledModule(moduleNode);