mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-17 08:51:34 +00:00
Update binaryen to latest nightly; Source map support
This commit is contained in:
@ -1,63 +1,74 @@
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var chalk = require("chalk");
|
||||
var glob = require("glob");
|
||||
var diff = require("./util/diff");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const chalk = require("chalk");
|
||||
const glob = require("glob");
|
||||
const diff = require("./util/diff");
|
||||
|
||||
require("ts-node").register({ project: require("path").join(__dirname, "..", "src") });
|
||||
require("../src/glue/js");
|
||||
|
||||
var Compiler = require("../src/compiler").Compiler;
|
||||
var Module = require("../src/module").Module;
|
||||
var Parser = require("../src/parser").Parser;
|
||||
var ElementKind = require("../src/program").ElementKind;
|
||||
const { Compiler, Options } = require("../src/compiler");
|
||||
const { Module } = require("../src/module");
|
||||
const { Parser } = require("../src/parser");
|
||||
const { ElementKind, LIBRARY_PREFIX } = require("../src/program");
|
||||
|
||||
var isCreate = process.argv[2] === "--create";
|
||||
var filter = process.argv.length > 2 && !isCreate ? "**/" + process.argv[2] + ".ts" : "**/*.ts";
|
||||
|
||||
var stdDir = __dirname + "/../std/assembly";
|
||||
var stdDir = path.join(__dirname, "..", "std", "assembly");
|
||||
var stdFiles = glob.sync("*.ts", { cwd: stdDir });
|
||||
|
||||
var success = 0;
|
||||
var failures = 0;
|
||||
|
||||
glob.sync(filter, { cwd: __dirname + "/compiler" }).forEach(filename => {
|
||||
if (filename.charAt(0) == "_")
|
||||
return;
|
||||
|
||||
glob.sync(filter, { cwd: path.join(__dirname, "compiler") }).forEach(filename => {
|
||||
if (filename.charAt(0) == "_") return;
|
||||
console.log(chalk.whiteBright("Testing compiler/" + filename));
|
||||
|
||||
var fixture = path.basename(filename, ".ts");
|
||||
var startTime = process.hrtime();
|
||||
var parser = new Parser();
|
||||
var cwd = path.join(__dirname, "compiler");
|
||||
|
||||
// include stdlib in std/ tests only. doing this to reduce the sheer amount of
|
||||
// program elements listed at the bottom of the basic fixtures.
|
||||
if (filename.startsWith("std/")) {
|
||||
stdFiles.forEach(file => {
|
||||
parser.parseFile(fs.readFileSync(__dirname + "/../std/assembly/" + file, { encoding: "utf8" }), "std:" + path.basename(file), false);
|
||||
});
|
||||
stdFiles.forEach(file => parser.parseFile(fs.readFileSync(path.join(stdDir, file), { encoding: "utf8" }), LIBRARY_PREFIX + path.basename(file), false));
|
||||
fixture = "std/" + fixture;
|
||||
}
|
||||
|
||||
var sourceText = fs.readFileSync(__dirname + "/compiler/" + filename, { encoding: "utf8" });
|
||||
var sourceText = fs.readFileSync(path.join(cwd, filename), { encoding: "utf8" });
|
||||
parser.parseFile(sourceText, filename, true);
|
||||
var nextFile;
|
||||
while ((nextFile = parser.nextFile()) !== null) {
|
||||
var nextSourceText;
|
||||
try {
|
||||
if (nextFile.startsWith("std:"))
|
||||
nextSourceText = fs.readFileSync(path.join(stdDir, nextFile.substring(4) + ".ts"), { encoding: "utf8" });
|
||||
else
|
||||
nextSourceText = fs.readFileSync(path.join(__dirname, "compiler", nextFile + ".ts"), { encoding: "utf8" });
|
||||
} catch (e) {
|
||||
nextSourceText = fs.readFileSync(path.join(__dirname, "compiler", nextFile, "index.ts"), { encoding: "utf8" });
|
||||
if (nextFile.startsWith(LIBRARY_PREFIX)) {
|
||||
sourceText = fs.readFileSync(path.join(stdDir, nextFile.substring(LIBRARY_PREFIX.length) + ".ts"), { encoding: "utf8" });
|
||||
nextFile = nextFile + ".ts";
|
||||
} else {
|
||||
try {
|
||||
sourceText = fs.readFileSync(path.join(cwd, nextFile + ".ts"), { encoding: "utf8" });
|
||||
nextFile += ".ts";
|
||||
} catch (e) {
|
||||
try {
|
||||
sourceText = fs.readFileSync(path.join(cwd, nextFile, "index.ts"), { encoding: "utf8" });
|
||||
nextFile += "/index.ts";
|
||||
} catch (e) {
|
||||
sourceText = fs.readFileSync(path.join(stdDir, nextFile + ".ts"), { encoding: "utf8" });
|
||||
nextFile = LIBRARY_PREFIX + nextFile + ".ts";
|
||||
// FIXME: what exactly is swallowing the error here?
|
||||
}
|
||||
}
|
||||
}
|
||||
parser.parseFile(nextSourceText, nextFile, false);
|
||||
parser.parseFile(sourceText, nextFile, false);
|
||||
}
|
||||
var program = parser.finish();
|
||||
var options = new Options();
|
||||
options.sourceMap = true;
|
||||
var parseTime = process.hrtime(startTime);
|
||||
startTime = process.hrtime();
|
||||
var module;
|
||||
try {
|
||||
module = Compiler.compile(program);
|
||||
module = Compiler.compile(program, options);
|
||||
} catch (e) {
|
||||
failed = true;
|
||||
module = Module.create();
|
||||
@ -75,48 +86,45 @@ glob.sync(filter, { cwd: __dirname + "/compiler" }).forEach(filename => {
|
||||
if (module.validate()) {
|
||||
console.log(chalk.green("validate OK"));
|
||||
try {
|
||||
// already covered by instantiate below, which is also able to use imports, but doesn't
|
||||
// provide as much debugging information. might be necessary to remove this one once imports
|
||||
// are tested more.
|
||||
// module.interpret();
|
||||
// console.log(chalk.green("interpret OK"));
|
||||
try {
|
||||
var binary = module.toBinary();
|
||||
var wasmModule = new WebAssembly.Module(binary);
|
||||
var wasmInstance = new WebAssembly.Instance(wasmModule, {
|
||||
env: {
|
||||
externalFunc: function(arg0, arg1, arg2) {
|
||||
console.log("env.externalFunc called with: " + arg0 + ", " + arg1 + ", " + arg2);
|
||||
},
|
||||
externalConst: 1,
|
||||
allocate_memory: function(size) {
|
||||
console.log("env.allocate_memory called with: " + size);
|
||||
return 0;
|
||||
},
|
||||
free_memory: function(ptr) {
|
||||
console.log("env.free_memory called with: " + ptr);
|
||||
}
|
||||
var binary = module.toBinary();
|
||||
var wasmModule = new WebAssembly.Module(binary.output);
|
||||
var wasmInstance = new WebAssembly.Instance(wasmModule, {
|
||||
env: {
|
||||
abort: function(msg, file, line, column) {
|
||||
throw new Error("Assertion failed");
|
||||
},
|
||||
external: {
|
||||
externalFunc: function(arg0, arg1, arg2) {
|
||||
console.log("external.externalFunc called with: " + arg0 + ", " + arg1 + ", " + arg2);
|
||||
},
|
||||
externalConst: 2
|
||||
externalFunc: function(arg0, arg1, arg2) {
|
||||
console.log("env.externalFunc called with: " + arg0 + ", " + arg1 + ", " + arg2);
|
||||
},
|
||||
externalConst: 1,
|
||||
allocate_memory: function(size) {
|
||||
console.log("env.allocate_memory called with: " + size);
|
||||
return 0;
|
||||
},
|
||||
free_memory: function(ptr) {
|
||||
console.log("env.free_memory called with: " + ptr);
|
||||
}
|
||||
});
|
||||
console.log(chalk.green("instantiate OK"));
|
||||
} catch (e) {
|
||||
failed = true;
|
||||
console.log(chalk.red("instantiate ERROR: ") + e.stack);
|
||||
}
|
||||
},
|
||||
external: {
|
||||
externalFunc: function(arg0, arg1, arg2) {
|
||||
console.log("external.externalFunc called with: " + arg0 + ", " + arg1 + ", " + arg2);
|
||||
},
|
||||
externalConst: 2
|
||||
}
|
||||
});
|
||||
console.log(chalk.green("instantiate OK"));
|
||||
} catch (e) {
|
||||
failed = true;
|
||||
console.log(chalk.red("interpret ERROR:") + e.stack);
|
||||
console.log(chalk.red("instantiate ERROR: ") + e.stack);
|
||||
}
|
||||
module.optimize();
|
||||
actualOptimized = module.toText();
|
||||
if (isCreate)
|
||||
fs.writeFileSync(__dirname + "/compiler/" + fixture + ".optimized.wasm", module.toBinary());
|
||||
if (isCreate) {
|
||||
var binary = module.toBinary(fixture + ".optimized.wasm.map");
|
||||
fs.writeFileSync(__dirname + "/compiler/" + fixture + ".optimized.wasm", binary.output);
|
||||
if (binary.sourceMap != null)
|
||||
fs.writeFileSync(__dirname + "/compiler/" + fixture + ".optimized.wasm.map", binary.sourceMap, { encoding: "utf8" });
|
||||
}
|
||||
} else {
|
||||
failed = true;
|
||||
console.log(chalk.red("validate ERROR"));
|
||||
|
Reference in New Issue
Block a user