919 lines
28 KiB
JavaScript
Raw Normal View History

/**
2018-03-19 01:12:18 +01:00
* Compiler frontend for node.js
*
* Uses the low-level API exported from src/index.ts so it works with the compiler compiled to
* JavaScript as well as the compiler compiled to WebAssembly (eventually). Runs the sources
* directly through ts-node if distribution files are not present (indicated by a `-dev` version).
*
* Can also be packaged as a bundle suitable for in-browser use with the standard library injected
* in the build step. See dist/asc.js for the bundle and webpack.config.js for building details.
2018-03-19 01:12:18 +01:00
*
2018-05-28 18:55:51 +02:00
* @module cli/asc
*/
2018-02-12 18:54:17 +01:00
const fs = require("fs");
2018-02-12 18:54:17 +01:00
const path = require("path");
const utf8 = require("@protobufjs/utf8");
2018-06-14 15:57:04 +02:00
const colors = require("./util/colors");
const EOL = process.platform === "win32" ? "\r\n" : "\n";
2017-12-05 15:06:44 +01:00
2018-02-03 02:36:20 +01:00
// Use distribution files if present, otherwise run the sources directly
2018-02-09 15:43:57 +01:00
var assemblyscript, isDev;
(() => {
try {
assemblyscript = require("../dist/assemblyscript.js");
2018-02-09 15:43:57 +01:00
isDev = false;
} catch (e) {
try {
2018-06-28 18:31:19 +02:00
require("ts-node").register({
project: path.join(__dirname, "..", "src", "tsconfig.json"),
files: [ // see: https://github.com/TypeStrong/ts-node/issues/620
path.join(__dirname, "..", "std", "portable.d.ts"),
path.join(__dirname, "..", "src", "glue", "binaryen.d.ts")
]
});
require("../src/glue/js");
assemblyscript = require("../src");
isDev = true;
} catch (e) {
// last resort: same directory CommonJS
assemblyscript = eval("require('./assemblyscript')");
isDev = false;
}
}
})();
2017-12-05 15:06:44 +01:00
2018-02-12 18:54:17 +01:00
/** Whether this is a webpack bundle or not. */
exports.isBundle = typeof BUNDLE_VERSION === "string";
/** Whether asc runs the sources directly or not. */
exports.isDev = isDev;
2018-05-28 18:55:51 +02:00
/** AssemblyScript version. */
exports.version = exports.isBundle ? BUNDLE_VERSION : require("../package.json").version;
2018-02-12 18:54:17 +01:00
2018-05-28 18:55:51 +02:00
/** Available CLI options. */
2018-02-12 18:54:17 +01:00
exports.options = require("./asc.json");
2018-02-09 15:43:57 +01:00
2018-02-12 18:54:17 +01:00
/** Common root used in source maps. */
exports.sourceMapRoot = "assemblyscript:///";
2017-12-05 15:06:44 +01:00
2018-02-12 18:54:17 +01:00
/** Prefix used for library files. */
exports.libraryPrefix = assemblyscript.LIBRARY_PREFIX;
/** Default Binaryen optimization level. */
exports.defaultOptimizeLevel = 2;
/** Default Binaryen shrink level. */
exports.defaultShrinkLevel = 1;
2018-04-04 14:39:40 +02:00
/** Bundled library files. */
exports.libraryFiles = exports.isBundle ? BUNDLE_LIBRARY : (() => { // set up if not a bundle
const libDir = path.join(__dirname, "..", "std", "assembly");
const libFiles = require("glob").sync("**/*.ts", { cwd: libDir });
const bundled = {};
libFiles.forEach(file => bundled[file.replace(/\.ts$/, "")] = fs.readFileSync(path.join(libDir, file), "utf8" ));
return bundled;
})();
2018-02-12 18:54:17 +01:00
2018-04-04 14:39:40 +02:00
/** Bundled definition files. */
exports.definitionFiles = exports.isBundle ? BUNDLE_DEFINITIONS : (() => { // set up if not a bundle
const stdDir = path.join(__dirname, "..", "std");
return {
"assembly": fs.readFileSync(path.join(stdDir, "assembly.d.ts"), "utf8"),
"portable": fs.readFileSync(path.join(stdDir, "portable.d.ts"), "utf8")
};
})();
/** Convenience function that parses and compiles source strings directly. */
2018-04-04 14:39:40 +02:00
exports.compileString = (sources, options) => {
if (typeof sources === "string") sources = { "input.ts": sources };
const output = Object.create({
stdout: createMemoryStream(),
stderr: createMemoryStream(),
2018-04-04 14:39:40 +02:00
binary: null,
text: null
});
exports.main([
"--binaryFile", "binary",
"--textFile", "text",
...Object.keys(options || {}).map(arg => `--${arg}=${options[arg]}`),
...Object.keys(sources),
2018-04-04 14:39:40 +02:00
], {
stdout: output.stdout,
stderr: output.stderr,
readFile: name => sources.hasOwnProperty(name) ? sources[name] : null,
writeFile: (name, contents) => output[name] = contents,
listFiles: () => []
});
2018-04-04 14:39:40 +02:00
return output;
}
2018-02-12 18:54:17 +01:00
/** Runs the command line utility using the specified arguments array. */
exports.main = function main(argv, options, callback) {
if (typeof options === "function") {
callback = options;
options = {};
} else if (!options) {
options = {};
}
const stdout = options.stdout || process.stdout;
const stderr = options.stderr || process.stderr;
2018-02-09 15:43:57 +01:00
const readFile = options.readFile || readFileNode;
const writeFile = options.writeFile || writeFileNode;
const listFiles = options.listFiles || listFilesNode;
const stats = options.stats || createStats();
2018-02-09 15:43:57 +01:00
2018-06-14 15:57:04 +02:00
// Output must be specified if not present in the environment
if (!stdout) throw Error("'options.stdout' must be specified");
if (!stderr) throw Error("'options.stderr' must be specified");
2018-02-03 02:36:20 +01:00
const args = parseArguments(argv);
const indent = 24;
2017-12-05 15:06:44 +01:00
2018-06-14 15:57:04 +02:00
if (args.noColors) {
colors.stdout.supported =
colors.stderr.supported = false;
} else {
colors.stdout = colors.from(stdout);
colors.stderr = colors.from(stderr);
}
// Use default callback if none is provided
if (!callback) callback = function defaultCallback(err) {
var code = 0;
if (err) {
2018-06-14 15:57:04 +02:00
stderr.write(colors.stderr.red("ERROR: ") + err.stack.replace(/^ERROR: /i, "") + EOL);
code = 1;
}
return code;
};
2018-02-03 02:36:20 +01:00
// Just print the version if requested
if (args.version) {
stdout.write("Version " + exports.version + (isDev ? "-dev" : "") + EOL);
return callback(null);
2018-02-03 02:36:20 +01:00
}
// Print the help message if requested or no source files are provided
if (args.help || args._.length < 1) {
const opts = [];
2018-02-12 18:54:17 +01:00
Object.keys(exports.options).forEach(name => {
var option = exports.options[name];
2018-06-14 15:57:04 +02:00
var text = " ";
2018-02-03 02:36:20 +01:00
text += "--" + name;
if (option.aliases && option.aliases[0].length === 1) {
2018-02-03 02:36:20 +01:00
text += ", -" + option.aliases[0];
}
while (text.length < indent) {
2018-02-03 02:36:20 +01:00
text += " ";
}
2018-05-28 18:55:51 +02:00
if (Array.isArray(option.description)) {
opts.push(text + option.description[0] + option.description.slice(1).map(line => {
for (let i = 0; i < indent; ++i) {
2018-02-03 02:36:20 +01:00
line = " " + line;
}
return EOL + line;
2018-02-03 02:36:20 +01:00
}).join(""));
} else {
2018-05-28 18:55:51 +02:00
opts.push(text + option.description);
}
2018-02-03 02:36:20 +01:00
});
2018-06-14 15:57:04 +02:00
var out = args.help ? stdout : stderr;
var color = args.help ? colors.stdout : colors.stderr;
out.write([
color.white("Syntax"),
" " + color.cyan("asc") + " [entryFile ...] [options]",
2018-02-03 02:36:20 +01:00
"",
2018-06-14 15:57:04 +02:00
color.white("Examples"),
" " + color.cyan("asc") + " hello.ts",
" " + color.cyan("asc") + " hello.ts -b hello.wasm -t hello.wat",
" " + color.cyan("asc") + " hello1.ts hello2.ts -b -O > hello.wasm",
2018-02-03 02:36:20 +01:00
"",
2018-06-14 15:57:04 +02:00
color.white("Options"),
].concat(opts).join(EOL) + EOL);
return callback(null);
2018-02-03 02:36:20 +01:00
}
2018-06-14 15:57:04 +02:00
// I/O must be specified if not present in the environment
if (!fs.readFileSync) {
if (readFile === readFileNode) throw Error("'options.readFile' must be specified");
if (writeFile === writeFileNode) throw Error("'options.writeFile' must be specified");
if (listFiles === listFilesNode) throw Error("'options.listFiles' must be specified");
}
// Set up base directory
2018-04-04 14:39:40 +02:00
const baseDir = args.baseDir ? path.resolve(args.baseDir) : ".";
2018-02-03 02:36:20 +01:00
// Set up transforms
const transforms = [];
if (args.transform) {
if (typeof args.transform === "string") args.transform = args.transform.split(",");
args.transform.forEach(transform =>
transforms.push(
require(
path.isAbsolute(transform = transform.trim())
? transform
: path.join(process.cwd(), transform)
)
)
);
}
function applyTransform(name, ...args) {
transforms.forEach(transform => {
if (typeof transform[name] === "function") transform[name](...args);
});
}
// Begin parsing
var parser = null;
// Include library files
if (!args.noLib) { // bundled
Object.keys(exports.libraryFiles).forEach(libPath => {
if (libPath.indexOf("/") >= 0) return; // in sub-directory: imported on demand
stats.parseCount++;
stats.parseTime += measure(() => {
parser = assemblyscript.parseFile(
exports.libraryFiles[libPath],
exports.libraryPrefix + libPath + ".ts",
false,
parser
);
});
});
}
2018-04-07 16:37:39 +02:00
const customLibDirs = [];
2018-02-03 02:36:20 +01:00
if (args.lib) {
2018-04-04 14:39:40 +02:00
if (typeof args.lib === "string") args.lib = args.lib.split(",");
Array.prototype.push.apply(customLibDirs, args.lib.map(lib => lib.trim()));
for (let i = 0, k = customLibDirs.length; i < k; ++i) { // custom
let libDir = customLibDirs[i];
let libFiles;
if (libDir.endsWith(".ts")) {
libFiles = [ path.basename(libDir) ];
libDir = path.dirname(libDir);
} else {
libFiles = listFiles(libDir);
}
for (let j = 0, l = libFiles.length; j < l; ++j) {
let libPath = libFiles[j];
let libText = readFile(path.join(libDir, libPath));
if (libText === null) return callback(Error("Library file '" + libPath + "' not found."));
stats.parseCount++;
stats.parseTime += measure(() => {
parser = assemblyscript.parseFile(
libText,
exports.libraryPrefix + libPath,
false,
parser
);
});
}
}
}
2017-12-05 15:06:44 +01:00
2018-02-03 02:36:20 +01:00
// Include entry files
for (let i = 0, k = args._.length; i < k; ++i) {
const filename = args._[i];
let sourcePath = filename.replace(/\\/g, "/").replace(/(\.ts|\/)$/, "");
2018-02-03 02:36:20 +01:00
// Try entryPath.ts, then entryPath/index.ts
let sourceText = readFile(path.join(baseDir, sourcePath) + ".ts");
if (sourceText === null) {
sourceText = readFile(path.join(baseDir, sourcePath, "index.ts"));
if (sourceText === null) {
return callback(Error("Entry file '" + sourcePath + ".ts' not found."));
} else {
sourcePath += "/index.ts";
}
} else {
sourcePath += ".ts";
}
2017-12-05 15:06:44 +01:00
stats.parseCount++;
2018-02-12 18:54:17 +01:00
stats.parseTime += measure(() => {
parser = assemblyscript.parseFile(sourceText, sourcePath, true, parser);
2018-02-12 18:54:17 +01:00
});
2018-02-03 02:36:20 +01:00
// Process backlog
while ((sourcePath = parser.nextFile()) != null) {
2018-02-03 02:36:20 +01:00
let found = false;
// Load library file if explicitly requested
2018-02-12 18:54:17 +01:00
if (sourcePath.startsWith(exports.libraryPrefix)) {
2018-04-03 23:56:48 +02:00
const plainName = sourcePath.substring(exports.libraryPrefix.length);
const indexName = sourcePath.substring(exports.libraryPrefix.length) + "/index";
if (exports.libraryFiles.hasOwnProperty(plainName)) {
sourceText = exports.libraryFiles[plainName];
sourcePath = exports.libraryPrefix + plainName + ".ts";
} else if (exports.libraryFiles.hasOwnProperty(indexName)) {
sourceText = exports.libraryFiles[indexName];
sourcePath = exports.libraryPrefix + indexName + ".ts";
} else {
2018-04-04 14:39:40 +02:00
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
const dir = customLibDirs[i];
2018-04-03 23:56:48 +02:00
sourceText = readFile(path.join(dir, plainName + ".ts"));
2018-02-09 15:43:57 +01:00
if (sourceText !== null) {
2018-04-03 23:56:48 +02:00
sourcePath = exports.libraryPrefix + plainName + ".ts";
break;
2018-04-03 23:56:48 +02:00
} else {
sourceText = readFile(path.join(dir, indexName + ".ts"));
if (sourceText !== null) {
sourcePath = exports.libraryPrefix + indexName + ".ts";
break;
2018-04-03 23:56:48 +02:00
}
2018-02-09 15:43:57 +01:00
}
}
2018-02-03 02:36:20 +01:00
}
2017-12-16 02:27:39 +01:00
2018-04-03 23:56:48 +02:00
// Otherwise try nextFile.ts, nextFile/index.ts, ~lib/nextFile.ts, ~lib/nextFile/index.ts
2018-02-03 02:36:20 +01:00
} else {
2018-04-03 23:56:48 +02:00
const plainName = sourcePath;
const indexName = sourcePath + "/index";
sourceText = readFile(path.join(baseDir, plainName + ".ts"));
if (sourceText !== null) {
2018-04-03 23:56:48 +02:00
sourcePath = plainName + ".ts";
} else {
2018-04-03 23:56:48 +02:00
sourceText = readFile(path.join(baseDir, indexName + ".ts"));
if (sourceText !== null) {
2018-04-03 23:56:48 +02:00
sourcePath = indexName + ".ts";
} else if (!plainName.startsWith(".")) {
if (exports.libraryFiles.hasOwnProperty(plainName)) {
sourceText = exports.libraryFiles[plainName];
sourcePath = exports.libraryPrefix + plainName + ".ts";
} else if (exports.libraryFiles.hasOwnProperty(indexName)) {
sourceText = exports.libraryFiles[indexName];
sourcePath = exports.libraryPrefix + indexName + ".ts";
} else {
2018-04-04 14:39:40 +02:00
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
const dir = customLibDirs[i];
2018-04-03 23:56:48 +02:00
sourceText = readFile(path.join(dir, plainName + ".ts"));
2018-02-09 15:43:57 +01:00
if (sourceText !== null) {
2018-04-03 23:56:48 +02:00
sourcePath = exports.libraryPrefix + plainName + ".ts";
break;
2018-04-03 23:56:48 +02:00
} else {
sourceText = readFile(path.join(dir, indexName + ".ts"));
if (sourceText !== null) {
sourcePath = exports.libraryPrefix + indexName + ".ts";
break;
2018-04-03 23:56:48 +02:00
}
2018-02-09 15:43:57 +01:00
}
}
}
}
}
}
2018-04-03 23:56:48 +02:00
if (sourceText == null) {
2018-04-04 14:39:40 +02:00
return callback(Error("Import file '" + sourcePath + ".ts' not found."));
2018-04-03 23:56:48 +02:00
}
stats.parseCount++;
2018-02-12 18:54:17 +01:00
stats.parseTime += measure(() => {
assemblyscript.parseFile(sourceText, sourcePath, false, parser);
2018-02-12 18:54:17 +01:00
});
2017-12-09 02:52:20 +01:00
}
if (checkDiagnostics(parser, stderr)) {
return callback(Error("Parse error"));
}
}
applyTransform("afterParse", parser);
// Finish parsing
const program = assemblyscript.finishParsing(parser);
2018-02-03 02:36:20 +01:00
// Begin compilation
const compilerOptions = assemblyscript.createOptions();
assemblyscript.setTarget(compilerOptions, 0);
assemblyscript.setNoTreeShaking(compilerOptions, !!args.noTreeShaking);
assemblyscript.setNoAssert(compilerOptions, !!args.noAssert);
assemblyscript.setImportMemory(compilerOptions, !!args.importMemory);
assemblyscript.setImportTable(compilerOptions, !!args.importTable);
assemblyscript.setMemoryBase(compilerOptions, args.memoryBase >>> 0);
assemblyscript.setSourceMap(compilerOptions, args.sourceMap != null);
// Initialize default aliases
assemblyscript.setGlobalAlias(compilerOptions, "Math", "NativeMath");
assemblyscript.setGlobalAlias(compilerOptions, "Mathf", "NativeMathf");
assemblyscript.setGlobalAlias(compilerOptions, "abort", "~lib/env/abort"); // to disable: --use abort=
// Add or override aliases if specified
var aliases = args.use;
if (aliases != null) {
if (typeof aliases === "string") aliases = aliases.split(",");
for (let i = 0, k = aliases.length; i < k; ++i) {
let part = aliases[i];
let p = part.indexOf("=");
if (p < 0) return callback(Error("Global alias '" + part + "' is invalid."));
let name = part.substring(0, p).trim();
let alias = part.substring(p + 1).trim();
if (!name.length) return callback(Error("Global alias '" + part + "' is invalid."));
assemblyscript.setGlobalAlias(compilerOptions, name, alias);
}
}
2018-02-03 02:36:20 +01:00
// Enable additional features if specified
2018-05-08 14:37:51 +02:00
var features = args.enable;
if (features != null) {
if (typeof features === "string") features = features.split(",");
for (let i = 0, k = features.length; i < k; ++i) {
let name = features[i].trim();
let flag = assemblyscript["FEATURE_" + name.replace(/\-/g, "_").toUpperCase()];
if (!flag) return callback(Error("Feature '" + name + "' is unknown."));
assemblyscript.enableFeature(compilerOptions, flag);
}
}
2018-02-03 02:36:20 +01:00
var module;
stats.compileCount++;
(() => {
try {
stats.compileTime += measure(() => {
module = assemblyscript.compileProgram(program, compilerOptions);
});
} catch (e) {
return callback(e);
}
})();
if (checkDiagnostics(parser, stderr)) {
2018-02-03 02:36:20 +01:00
if (module) module.dispose();
return callback(Error("Compile error"));
2018-02-03 02:36:20 +01:00
}
// Validate the module if requested
if (args.validate) {
stats.validateCount++;
2018-02-03 02:36:20 +01:00
stats.validateTime += measure(() => {
if (!module.validate()) {
module.dispose();
return callback(Error("Validate error"));
2018-02-03 02:36:20 +01:00
}
});
2017-12-09 02:52:20 +01:00
}
2018-02-03 02:36:20 +01:00
// Set Binaryen-specific options
if (args.trapMode === "clamp") {
stats.optimizeCount++;
2018-02-12 18:54:17 +01:00
stats.optimizeTime += measure(() => {
module.runPasses([ "trap-mode-clamp" ]);
});
2018-02-03 02:36:20 +01:00
} else if (args.trapMode === "js") {
stats.optimizeCount++;
2018-02-12 18:54:17 +01:00
stats.optimizeTime += measure(() => {
module.runPasses([ "trap-mode-js" ]);
});
2018-02-03 02:36:20 +01:00
} else if (args.trapMode !== "allow") {
module.dispose();
return callback(Error("Unsupported trap mode"));
2018-02-03 02:36:20 +01:00
}
var optimizeLevel = -1;
var shrinkLevel = 0;
var debugInfo = !args.noDebug;
if (args.optimize !== false) {
if (typeof args.optimize === "number") {
2018-02-03 02:36:20 +01:00
optimizeLevel = args.optimize;
} else if (args["0"]) {
2018-02-03 02:36:20 +01:00
optimizeLevel = 0;
} else if (args["1"]) {
2018-02-03 02:36:20 +01:00
optimizeLevel = 1;
} else if (args["2"]) {
2018-02-03 02:36:20 +01:00
optimizeLevel = 2;
} else if (args["3"]) {
2018-02-03 02:36:20 +01:00
optimizeLevel = 3;
} else if (args.optimize === true) {
2018-02-12 18:54:17 +01:00
optimizeLevel = exports.defaultOptimizeLevel;
shrinkLevel = exports.defaultShrinkLevel;
2018-02-03 02:36:20 +01:00
} else
optimizeLevel = 0;
}
if (args["s"]) {
2018-02-03 02:36:20 +01:00
shrinkLevel = 1;
} else if (args["z"]) {
2018-02-03 02:36:20 +01:00
shrinkLevel = 2;
}
2018-02-03 02:36:20 +01:00
if (typeof args.optimizeLevel === "number") {
2018-02-03 02:36:20 +01:00
optimizeLevel = args.optimizeLevel;
}
if (typeof args.shrinkLevel === "number") {
2018-02-03 02:36:20 +01:00
shrinkLevel = args.shrinkLevel;
} else if (args.shrinkLevel === "s") {
2018-02-03 02:36:20 +01:00
shrinkLevel = 1;
} else if (args.shrinkLevel === "z") {
2018-02-03 02:36:20 +01:00
shrinkLevel = 2;
}
2018-02-03 02:36:20 +01:00
// Implicitly run costly non-LLVM optimizations on -O3 or -Oz
// see: https://github.com/WebAssembly/binaryen/pull/1596
if (optimizeLevel >= 3 || shrinkLevel >= 2) optimizeLevel = 4;
module.setOptimizeLevel(optimizeLevel > 0 ? optimizeLevel : 0);
2018-02-03 02:36:20 +01:00
module.setShrinkLevel(shrinkLevel);
module.setDebugInfo(debugInfo);
var runPasses = [];
if (args.runPasses) {
if (typeof args.runPasses === "string") {
2018-02-03 02:36:20 +01:00
args.runPasses = args.runPasses.split(",");
}
if (args.runPasses.length) {
2018-02-03 02:36:20 +01:00
args.runPasses.forEach(pass => {
if (runPasses.indexOf(pass) < 0)
runPasses.push(pass);
});
}
2018-02-03 02:36:20 +01:00
}
// Optimize the module if requested
if (optimizeLevel >= 0) {
stats.optimizeCount++;
stats.optimizeTime += measure(() => {
module.optimize();
});
}
2018-02-03 02:36:20 +01:00
// Run additional passes if requested
if (runPasses.length) {
stats.optimizeCount++;
2018-02-12 18:54:17 +01:00
stats.optimizeTime += measure(() => {
module.runPasses(runPasses.map(pass => pass.trim()));
});
}
2018-02-03 02:36:20 +01:00
// Prepare output
if (!args.noEmit) {
let hasStdout = false;
let hasOutput = false;
2018-02-03 02:36:20 +01:00
if (args.outFile != null) {
if (/\.was?t$/.test(args.outFile) && args.textFile == null) {
2018-02-03 02:36:20 +01:00
args.textFile = args.outFile;
} else if (/\.js$/.test(args.outFile) && args.asmjsFile == null) {
2018-02-03 02:36:20 +01:00
args.asmjsFile = args.outFile;
} else if (args.binaryFile == null) {
2018-02-03 02:36:20 +01:00
args.binaryFile = args.outFile;
}
}
2017-12-05 15:06:44 +01:00
// Write binary
if (args.binaryFile != null) {
let sourceMapURL = args.sourceMap != null
2018-02-03 02:36:20 +01:00
? args.sourceMap.length
? args.sourceMap
: path.basename(args.binaryFile) + ".map"
: null;
let wasm;
stats.emitCount++;
stats.emitTime += measure(() => {
wasm = module.toBinary(sourceMapURL)
});
if (args.binaryFile.length) {
writeFile(path.join(baseDir, args.binaryFile), wasm.output);
} else {
writeStdout(wasm.output);
hasStdout = true;
}
hasOutput = true;
// Post-process source map
if (wasm.sourceMap != null) {
if (args.binaryFile.length) {
let sourceMap = JSON.parse(wasm.sourceMap);
2018-02-12 18:54:17 +01:00
sourceMap.sourceRoot = exports.sourceMapRoot;
sourceMap.sources.forEach((name, index) => {
let text = null;
2018-02-12 18:54:17 +01:00
if (name.startsWith(exports.libraryPrefix)) {
let stdName = name.substring(exports.libraryPrefix.length).replace(/\.ts$/, "");
if (exports.libraryFiles.hasOwnProperty(stdName)) {
text = exports.libraryFiles[stdName];
} else {
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
text = readFile(path.join(
customLibDirs[i],
name.substring(exports.libraryPrefix.length))
);
if (text !== null) break;
}
}
} else {
text = readFile(path.join(baseDir, name));
}
if (text === null) {
return callback(Error("Source file '" + name + "' not found."));
}
if (!sourceMap.sourceContents) sourceMap.sourceContents = [];
2018-02-12 18:54:17 +01:00
sourceMap.sourceContents[index] = text;
2018-02-03 02:36:20 +01:00
});
2018-02-12 18:54:17 +01:00
writeFile(path.join(
baseDir,
path.dirname(args.binaryFile),
path.basename(sourceMapURL)
), JSON.stringify(sourceMap));
} else {
stderr.write("Skipped source map (stdout already occupied)" + EOL);
}
2018-02-03 02:36:20 +01:00
}
}
// Write asm.js
if (args.asmjsFile != null) {
let asm;
if (args.asmjsFile.length) {
stats.emitCount++;
stats.emitTime += measure(() => {
asm = module.toAsmjs();
});
writeFile(path.join(baseDir, args.asmjsFile), asm);
} else if (!hasStdout) {
stats.emitCount++;
stats.emitTime += measure(() => {
asm = module.toAsmjs();
});
writeStdout(asm);
hasStdout = true;
}
hasOutput = true;
2018-02-03 02:36:20 +01:00
}
// Write WebIDL
if (args.idlFile != null) {
let idl;
if (args.idlFile.length) {
stats.emitCount++;
stats.emitTime += measure(() => {
idl = assemblyscript.buildIDL(program);
});
writeFile(path.join(baseDir, args.idlFile), idl);
} else if (!hasStdout) {
stats.emitCount++;
stats.emitTime += measure(() => {
idl = assemblyscript.buildIDL(program);
});
writeStdout(idl);
hasStdout = true;
2018-02-03 02:36:20 +01:00
}
hasOutput = true;
2018-02-03 02:36:20 +01:00
}
// Write TypeScript definition
if (args.tsdFile != null) {
let tsd;
if (args.tsdFile.length) {
stats.emitCount++;
stats.emitTime += measure(() => {
tsd = assemblyscript.buildTSD(program);
});
writeFile(path.join(baseDir, args.tsdFile), tsd);
} else if (!hasStdout) {
stats.emitCount++;
stats.emitTime += measure(() => {
tsd = assemblyscript.buildTSD(program);
});
writeStdout(tsd);
hasStdout = true;
}
hasOutput = true;
}
// Write text (must be last)
if (args.textFile != null || !hasOutput) {
let wat;
if (args.textFile && args.textFile.length) {
stats.emitCount++;
stats.emitTime += measure(() => {
wat = module.toText();
});
writeFile(path.join(baseDir, args.textFile), wat);
} else if (!hasStdout) {
stats.emitCount++;
stats.emitTime += measure(() => {
wat = module.toText()
});
writeStdout(wat);
}
}
2018-02-03 02:36:20 +01:00
}
module.dispose();
if (args.measure) {
printStats(stats, stderr);
}
return callback(null);
2018-02-09 15:43:57 +01:00
function readFileNode(filename) {
try {
2018-03-13 14:03:57 +01:00
let text;
stats.readCount++;
2018-02-12 18:54:17 +01:00
stats.readTime += measure(() => {
text = fs.readFileSync(filename, { encoding: "utf8" });
});
return text;
} catch (e) {
return null;
}
}
2018-02-09 15:43:57 +01:00
function writeFileNode(filename, contents) {
try {
stats.writeCount++;
2018-02-12 18:54:17 +01:00
stats.writeTime += measure(() => {
if (typeof contents === "string") {
2018-02-12 18:54:17 +01:00
fs.writeFileSync(filename, contents, { encoding: "utf8" } );
} else {
2018-02-12 18:54:17 +01:00
fs.writeFileSync(filename, contents);
}
2018-02-12 18:54:17 +01:00
});
return true;
} catch (e) {
return false;
}
}
2018-02-09 15:43:57 +01:00
function listFilesNode(dirname) {
var files;
try {
2018-02-12 18:54:17 +01:00
stats.readTime += measure(() => {
files = require("glob").sync("*.ts", { cwd: dirname });
});
2018-02-09 15:43:57 +01:00
return files;
} catch (e) {
return [];
}
}
function writeStdout(contents) {
if (!writeStdout.used) {
stats.writeCount++;
writeStdout.used = true;
}
2018-02-12 18:54:17 +01:00
stats.writeTime += measure(() => {
if (typeof contents === "string") {
2018-02-12 18:54:17 +01:00
stdout.write(contents, { encoding: "utf8" });
} else {
2018-02-12 18:54:17 +01:00
stdout.write(contents);
}
2018-02-12 18:54:17 +01:00
});
}
}
2018-02-03 02:36:20 +01:00
2018-02-12 18:54:17 +01:00
/** Parses the specified command line arguments. */
2018-02-03 02:36:20 +01:00
function parseArguments(argv) {
const opts = {};
2018-02-12 18:54:17 +01:00
Object.keys(exports.options).forEach(key => {
const opt = exports.options[key];
if (opt.aliases) {
2018-02-03 02:36:20 +01:00
(opts.alias || (opts.alias = {}))[key] = opt.aliases;
}
if (opt.default !== undefined) {
2018-02-03 02:36:20 +01:00
(opts.default || (opts.default = {}))[key] = opt.default;
}
if (opt.type === "string") {
2018-02-03 02:36:20 +01:00
(opts.string || (opts.string = [])).push(key);
} else if (opt.type === "boolean") {
2018-02-03 02:36:20 +01:00
(opts.boolean || (opts.boolean = [])).push(key);
}
2018-02-03 02:36:20 +01:00
});
return require("minimist")(argv, opts);
2018-01-16 17:52:48 +01:00
}
2018-02-12 18:54:17 +01:00
/** Checks diagnostics emitted so far for errors. */
function checkDiagnostics(emitter, stderr) {
2018-02-03 02:36:20 +01:00
var diagnostic;
var hasErrors = false;
2018-02-12 18:54:17 +01:00
while ((diagnostic = assemblyscript.nextDiagnostic(emitter)) != null) {
2018-05-28 18:55:51 +02:00
if (stderr) {
stderr.write(
assemblyscript.formatDiagnostic(diagnostic, stderr.isTTY, true) +
EOL + EOL
);
}
if (assemblyscript.isError(diagnostic)) hasErrors = true;
2018-02-03 02:36:20 +01:00
}
return hasErrors;
}
exports.checkDiagnostics = checkDiagnostics;
2018-02-12 18:54:17 +01:00
/** Creates an empty set of stats. */
2018-02-03 02:36:20 +01:00
function createStats() {
return {
readTime: 0,
readCount: 0,
writeTime: 0,
writeCount: 0,
parseTime: 0,
parseCount: 0,
2018-02-03 02:36:20 +01:00
compileTime: 0,
compileCount: 0,
emitTime: 0,
emitCount: 0,
2018-02-03 02:36:20 +01:00
validateTime: 0,
validateCount: 0,
optimizeTime: 0,
optimizeCount: 0
2018-02-03 02:36:20 +01:00
};
2017-12-09 00:45:12 +01:00
}
2017-12-05 15:06:44 +01:00
exports.createStats = createStats;
if (!process.hrtime) process.hrtime = require("browser-process-hrtime");
2018-02-12 18:54:17 +01:00
/** Measures the execution time of the specified function. */
2018-02-03 02:36:20 +01:00
function measure(fn) {
const start = process.hrtime();
fn();
const times = process.hrtime(start);
return times[0] * 1e9 + times[1];
}
exports.measure = measure;
2018-05-28 18:55:51 +02:00
/** Formats a high resolution time to a human readable string. */
2018-03-26 03:50:06 +02:00
function formatTime(time) {
return time ? (time / 1e6).toFixed(3) + " ms" : "N/A";
}
exports.formatTime = formatTime;
2018-02-12 18:54:17 +01:00
/** Formats and prints out the contents of a set of stats. */
function printStats(stats, output) {
2018-02-12 18:54:17 +01:00
function format(time, count) {
2018-03-26 03:50:06 +02:00
return formatTime(time);
2018-02-12 18:54:17 +01:00
}
(output || process.stdout).write([
2018-02-12 18:54:17 +01:00
"I/O Read : " + format(stats.readTime, stats.readCount),
"I/O Write : " + format(stats.writeTime, stats.writeCount),
"Parse : " + format(stats.parseTime, stats.parseCount),
"Compile : " + format(stats.compileTime, stats.compileCount),
"Emit : " + format(stats.emitTime, stats.emitCount),
"Validate : " + format(stats.validateTime, stats.validateCount),
"Optimize : " + format(stats.optimizeTime, stats.optimizeCount)
].join(EOL) + EOL);
}
exports.printStats = printStats;
2018-02-09 15:43:57 +01:00
var allocBuffer = typeof global !== "undefined" && global.Buffer
? global.Buffer.allocUnsafe || function(len) { return new global.Buffer(len); }
: function(len) { return new Uint8Array(len) };
2018-02-12 18:54:17 +01:00
/** Creates a memory stream that can be used in place of stdout/stderr. */
2018-02-09 15:43:57 +01:00
function createMemoryStream(fn) {
var stream = [];
stream.write = function(chunk) {
2018-03-31 18:18:55 +02:00
if (fn) fn(chunk);
2018-02-09 15:43:57 +01:00
if (typeof chunk === "string") {
let buffer = allocBuffer(utf8.length(chunk));
utf8.write(chunk, buffer, 0);
chunk = buffer;
2018-02-09 15:43:57 +01:00
}
this.push(chunk);
2018-02-09 15:43:57 +01:00
};
2018-06-14 15:57:04 +02:00
stream.reset = function() {
stream.length = 0;
};
2018-02-09 15:43:57 +01:00
stream.toBuffer = function() {
var offset = 0, i = 0, k = this.length;
while (i < k) offset += this[i++].length;
var buffer = allocBuffer(offset);
offset = i = 0;
while (i < k) {
buffer.set(this[i], offset);
offset += this[i].length;
++i;
}
return buffer;
2018-02-09 15:43:57 +01:00
};
stream.toString = function() {
2018-03-31 00:16:12 +02:00
var buffer = this.toBuffer();
return utf8.read(buffer, 0, buffer.length);
2018-02-09 15:43:57 +01:00
};
return stream;
}
exports.createMemoryStream = createMemoryStream;
2018-05-28 18:55:51 +02:00
/** Compatible TypeScript compiler options for syntax highlighting etc. */
exports.tscOptions = {
alwaysStrict: true,
noImplicitAny: true,
noImplicitReturns: true,
noImplicitThis: true,
noEmitOnError: true,
strictNullChecks: true,
experimentalDecorators: true,
target: "esnext",
module: "commonjs",
noLib: true,
types: [],
allowJs: false
};