2018-03-03 18:38:38 +01:00
|
|
|
/**
|
2018-03-19 01:12:18 +01:00
|
|
|
* Compiler frontend for node.js
|
2018-03-03 18:38:38 +01:00
|
|
|
*
|
|
|
|
* 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-03-03 18:38:38 +01:00
|
|
|
*/
|
2018-02-12 18:54:17 +01:00
|
|
|
|
2018-07-03 03:06:01 +02:00
|
|
|
// Use "." instead of "/" as cwd in browsers
|
|
|
|
if (process.browser) process.cwd = function() { return "."; };
|
|
|
|
|
2018-02-05 17:10:14 +01:00
|
|
|
const fs = require("fs");
|
2018-02-12 18:54:17 +01:00
|
|
|
const path = require("path");
|
2018-04-01 23:46:41 +02:00
|
|
|
const utf8 = require("@protobufjs/utf8");
|
2018-07-03 03:06:01 +02:00
|
|
|
const colorsUtil = require("./util/colors");
|
|
|
|
const optionsUtil = require("./util/options");
|
2018-08-04 01:02:56 +02:00
|
|
|
const mkdirp = require("./util/mkdirp");
|
2018-03-31 00:03:02 +02:00
|
|
|
const EOL = process.platform === "win32" ? "\r\n" : "\n";
|
2017-12-05 15:06:44 +01:00
|
|
|
|
2018-07-10 17:38:03 +02:00
|
|
|
// Emscripten adds an `uncaughtException` listener to Binaryen that results in an additional
|
|
|
|
// useless code fragment on top of an actual error. suppress this:
|
|
|
|
if (process.removeAllListeners) process.removeAllListeners("uncaughtException");
|
|
|
|
|
2018-02-03 02:36:20 +01:00
|
|
|
// Use distribution files if present, otherwise run the sources directly
|
2018-07-03 03:06:01 +02:00
|
|
|
var assemblyscript, isDev = false;
|
2018-03-03 18:38:38 +01:00
|
|
|
(() => {
|
2018-07-04 21:47:58 +02:00
|
|
|
try { // `asc` on the command line
|
2018-03-03 18:38:38 +01:00
|
|
|
assemblyscript = require("../dist/assemblyscript.js");
|
|
|
|
} catch (e) {
|
2018-07-04 21:47:58 +02:00
|
|
|
try { // `asc` on the command line without dist files
|
2018-07-03 03:06:01 +02:00
|
|
|
require("ts-node").register({ project: path.join(__dirname, "..", "src", "tsconfig.json") });
|
2018-03-03 18:38:38 +01:00
|
|
|
require("../src/glue/js");
|
|
|
|
assemblyscript = require("../src");
|
|
|
|
isDev = true;
|
2018-07-04 21:47:58 +02:00
|
|
|
} catch (e_ts) {
|
|
|
|
try { // `require("dist/asc.js")` in explicit browser tests
|
|
|
|
assemblyscript = eval("require('./assemblyscript')");
|
|
|
|
} catch (e) {
|
|
|
|
// combine both errors that lead us here
|
|
|
|
e.stack = e_ts.stack + "\n---\n" + e.stack;
|
|
|
|
throw e;
|
|
|
|
}
|
2018-03-03 18:38:38 +01:00
|
|
|
}
|
2018-02-05 17:10:14 +01:00
|
|
|
}
|
2018-03-03 18:38:38 +01:00
|
|
|
})();
|
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. */
|
2018-03-03 18:38:38 +01:00
|
|
|
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");
|
2018-07-03 03:06:01 +02:00
|
|
|
const libFiles = require("glob").sync("**/!(*.d).ts", { cwd: libDir });
|
2018-04-04 14:39:40 +02:00
|
|
|
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 {
|
2018-07-03 03:06:01 +02:00
|
|
|
"assembly": fs.readFileSync(path.join(stdDir, "assembly", "index.d.ts"), "utf8"),
|
|
|
|
"portable": fs.readFileSync(path.join(stdDir, "portable", "index.d.ts"), "utf8")
|
2018-04-04 14:39:40 +02:00
|
|
|
};
|
|
|
|
})();
|
2018-03-20 21:56:24 -04:00
|
|
|
|
|
|
|
/** 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({
|
2018-03-20 21:56:24 -04:00
|
|
|
stdout: createMemoryStream(),
|
2018-12-03 19:29:49 +01:00
|
|
|
stderr: createMemoryStream()
|
2018-04-04 14:39:40 +02:00
|
|
|
});
|
2018-07-03 03:06:01 +02:00
|
|
|
var argv = [
|
2018-04-04 14:39:40 +02:00
|
|
|
"--binaryFile", "binary",
|
|
|
|
"--textFile", "text",
|
2018-07-03 03:06:01 +02:00
|
|
|
];
|
|
|
|
Object.keys(options || {}).forEach(key => {
|
|
|
|
var val = options[key];
|
|
|
|
if (Array.isArray(val)) val.forEach(val => argv.push("--" + key, String(val)));
|
|
|
|
else argv.push("--" + key, String(val));
|
|
|
|
});
|
|
|
|
exports.main(argv.concat(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-03-20 21:56:24 -04:00
|
|
|
});
|
2018-04-04 14:39:40 +02:00
|
|
|
return output;
|
|
|
|
}
|
2018-03-20 21:56:24 -04:00
|
|
|
|
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) {
|
2018-02-04 07:51:40 +01:00
|
|
|
if (typeof options === "function") {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
2018-03-03 18:38:38 +01:00
|
|
|
} else if (!options) {
|
2018-02-04 07:51:40 +01:00
|
|
|
options = {};
|
2018-03-03 18:38:38 +01:00
|
|
|
}
|
2018-02-04 07:51:40 +01:00
|
|
|
|
|
|
|
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;
|
2018-02-09 16:40:53 +01:00
|
|
|
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
|
2018-03-03 18:38:38 +01:00
|
|
|
if (!stdout) throw Error("'options.stdout' must be specified");
|
|
|
|
if (!stderr) throw Error("'options.stderr' must be specified");
|
2018-02-04 07:51:40 +01:00
|
|
|
|
2018-07-03 03:06:01 +02:00
|
|
|
const opts = optionsUtil.parse(argv, exports.options);
|
|
|
|
const args = opts.options;
|
|
|
|
argv = opts.arguments;
|
2018-06-14 15:57:04 +02:00
|
|
|
if (args.noColors) {
|
2018-07-03 03:06:01 +02:00
|
|
|
colorsUtil.stdout.supported =
|
|
|
|
colorsUtil.stderr.supported = false;
|
2018-06-14 15:57:04 +02:00
|
|
|
} else {
|
2018-07-03 03:06:01 +02:00
|
|
|
colorsUtil.stdout = colorsUtil.from(stdout);
|
|
|
|
colorsUtil.stderr = colorsUtil.from(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for unknown arguments
|
|
|
|
if (opts.unknown.length) {
|
|
|
|
opts.unknown.forEach(arg => {
|
|
|
|
stderr.write(colorsUtil.stderr.yellow("WARN: ") + "Unknown option '" + arg + "'" + EOL);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for trailing arguments
|
|
|
|
if (opts.trailing.length) {
|
|
|
|
stderr.write(colorsUtil.stderr.yellow("WARN: ") + "Unsupported trailing arguments: " + opts.trailing.join(" ") + EOL);
|
2018-06-14 15:57:04 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 17:10:14 +01:00
|
|
|
// Use default callback if none is provided
|
2018-02-04 07:51:40 +01:00
|
|
|
if (!callback) callback = function defaultCallback(err) {
|
|
|
|
var code = 0;
|
|
|
|
if (err) {
|
2018-07-03 03:06:01 +02:00
|
|
|
stderr.write(colorsUtil.stderr.red("ERROR: ") + err.stack.replace(/^ERROR: /i, "") + EOL);
|
2018-02-04 07:51:40 +01:00
|
|
|
code = 1;
|
2018-02-05 17:10:14 +01:00
|
|
|
}
|
2018-02-04 07:51:40 +01:00
|
|
|
return code;
|
|
|
|
};
|
|
|
|
|
2018-02-03 02:36:20 +01:00
|
|
|
// Just print the version if requested
|
|
|
|
if (args.version) {
|
2018-03-31 00:03:02 +02:00
|
|
|
stdout.write("Version " + exports.version + (isDev ? "-dev" : "") + EOL);
|
2018-02-04 07:51:40 +01:00
|
|
|
return callback(null);
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
|
|
|
// Print the help message if requested or no source files are provided
|
2018-07-03 03:06:01 +02:00
|
|
|
if (args.help || !argv.length) {
|
2018-06-14 15:57:04 +02:00
|
|
|
var out = args.help ? stdout : stderr;
|
2018-07-03 03:06:01 +02:00
|
|
|
var color = args.help ? colorsUtil.stdout : colorsUtil.stderr;
|
2018-06-14 15:57:04 +02:00
|
|
|
out.write([
|
2018-07-03 03:06:01 +02:00
|
|
|
color.white("SYNTAX"),
|
2018-06-14 15:57:04 +02:00
|
|
|
" " + color.cyan("asc") + " [entryFile ...] [options]",
|
2018-02-03 02:36:20 +01:00
|
|
|
"",
|
2018-07-03 03:06:01 +02:00
|
|
|
color.white("EXAMPLES"),
|
2018-06-14 15:57:04 +02:00
|
|
|
" " + 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-07-03 03:06:01 +02:00
|
|
|
color.white("OPTIONS"),
|
|
|
|
].concat(
|
|
|
|
optionsUtil.help(exports.options, 24, EOL)
|
|
|
|
).join(EOL) + EOL);
|
2018-02-04 07:51:40 +01:00
|
|
|
return callback(null);
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
2018-01-18 15:45:34 +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");
|
|
|
|
}
|
|
|
|
|
2018-02-04 07:51:40 +01:00
|
|
|
// 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
|
|
|
|
2018-05-26 13:13:39 +02:00
|
|
|
// Set up transforms
|
|
|
|
const transforms = [];
|
|
|
|
if (args.transform) {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-07 03:27:22 +02:00
|
|
|
// Begin parsing
|
|
|
|
var parser = null;
|
|
|
|
|
|
|
|
// Include library files
|
2018-09-09 04:05:00 +02:00
|
|
|
if (!args.noLib) {
|
2018-04-07 03:27:22 +02:00
|
|
|
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-09-09 04:05:00 +02:00
|
|
|
} else { // always include builtins
|
|
|
|
stats.parseCount++;
|
|
|
|
stats.parseTime += measure(() => {
|
|
|
|
parser = assemblyscript.parseFile(
|
|
|
|
exports.libraryFiles["builtins"],
|
|
|
|
exports.libraryPrefix + "builtins.ts",
|
|
|
|
false,
|
|
|
|
parser
|
|
|
|
);
|
|
|
|
});
|
2018-04-07 03:27:22 +02:00
|
|
|
}
|
2018-04-07 16:37:39 +02:00
|
|
|
const customLibDirs = [];
|
2018-02-03 02:36:20 +01:00
|
|
|
if (args.lib) {
|
2018-07-03 03:06:01 +02:00
|
|
|
let lib = args.lib;
|
|
|
|
if (typeof lib === "string") lib = lib.split(",");
|
|
|
|
Array.prototype.push.apply(customLibDirs, lib.map(lib => lib.trim()));
|
2018-04-07 03:27:22 +02:00
|
|
|
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];
|
2018-12-03 19:29:49 +01:00
|
|
|
let libText = readFile(libPath, libDir);
|
2018-04-07 03:27:22 +02:00
|
|
|
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-06 17:47:48 +01:00
|
|
|
}
|
2017-12-05 15:06:44 +01:00
|
|
|
|
2018-12-03 17:22:01 +01:00
|
|
|
// Parses the backlog of imported files after including entry files
|
|
|
|
function parseBacklog() {
|
|
|
|
var sourcePath, sourceText;
|
2018-02-05 17:10:14 +01:00
|
|
|
while ((sourcePath = parser.nextFile()) != null) {
|
2018-02-03 02:36:20 +01:00
|
|
|
|
|
|
|
// 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) {
|
2018-12-03 19:29:49 +01:00
|
|
|
sourceText = readFile(plainName + ".ts", customLibDirs[i]);
|
2018-02-09 15:43:57 +01:00
|
|
|
if (sourceText !== null) {
|
2018-04-03 23:56:48 +02:00
|
|
|
sourcePath = exports.libraryPrefix + plainName + ".ts";
|
2018-04-08 00:43:38 +02:00
|
|
|
break;
|
2018-04-03 23:56:48 +02:00
|
|
|
} else {
|
2018-12-03 19:29:49 +01:00
|
|
|
sourceText = readFile(indexName + ".ts", customLibDirs[i]);
|
2018-04-03 23:56:48 +02:00
|
|
|
if (sourceText !== null) {
|
|
|
|
sourcePath = exports.libraryPrefix + indexName + ".ts";
|
2018-04-08 00:43:38 +02:00
|
|
|
break;
|
2018-04-03 23:56:48 +02:00
|
|
|
}
|
2018-02-09 15:43:57 +01:00
|
|
|
}
|
2018-02-05 17:10:14 +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";
|
2018-12-03 19:29:49 +01:00
|
|
|
sourceText = readFile(plainName + ".ts", baseDir);
|
2018-03-21 23:27:53 +01:00
|
|
|
if (sourceText !== null) {
|
2018-04-03 23:56:48 +02:00
|
|
|
sourcePath = plainName + ".ts";
|
2018-03-21 23:27:53 +01:00
|
|
|
} else {
|
2018-12-03 19:29:49 +01:00
|
|
|
sourceText = readFile(indexName + ".ts", baseDir);
|
2018-03-21 23:27:53 +01:00
|
|
|
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";
|
2018-03-21 23:27:53 +01:00
|
|
|
} else {
|
2018-04-04 14:39:40 +02:00
|
|
|
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
|
|
|
|
const dir = customLibDirs[i];
|
2018-12-03 19:29:49 +01:00
|
|
|
sourceText = readFile(plainName + ".ts", customLibDirs[i]);
|
2018-02-09 15:43:57 +01:00
|
|
|
if (sourceText !== null) {
|
2018-04-03 23:56:48 +02:00
|
|
|
sourcePath = exports.libraryPrefix + plainName + ".ts";
|
2018-04-08 00:43:38 +02:00
|
|
|
break;
|
2018-04-03 23:56:48 +02:00
|
|
|
} else {
|
2018-12-03 19:29:49 +01:00
|
|
|
sourceText = readFile(indexName + ".ts", customLibDirs[i]);
|
2018-04-03 23:56:48 +02:00
|
|
|
if (sourceText !== null) {
|
|
|
|
sourcePath = exports.libraryPrefix + indexName + ".ts";
|
2018-04-08 00:43:38 +02:00
|
|
|
break;
|
2018-04-03 23:56:48 +02:00
|
|
|
}
|
2018-02-09 15:43:57 +01:00
|
|
|
}
|
2018-02-05 17:10:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-03 18:38:38 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-30 01:20:20 +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
|
|
|
}
|
2018-02-05 17:10:14 +01:00
|
|
|
stats.parseCount++;
|
2018-02-12 18:54:17 +01:00
|
|
|
stats.parseTime += measure(() => {
|
2018-02-14 19:21:31 +01:00
|
|
|
assemblyscript.parseFile(sourceText, sourcePath, false, parser);
|
2018-02-12 18:54:17 +01:00
|
|
|
});
|
2017-12-09 02:52:20 +01:00
|
|
|
}
|
2018-03-03 18:38:38 +01:00
|
|
|
if (checkDiagnostics(parser, stderr)) {
|
2018-02-04 07:51:40 +01:00
|
|
|
return callback(Error("Parse error"));
|
2018-03-03 18:38:38 +01:00
|
|
|
}
|
2018-02-04 07:51:40 +01:00
|
|
|
}
|
|
|
|
|
2018-12-03 17:22:01 +01:00
|
|
|
// Include entry files
|
|
|
|
for (let i = 0, k = argv.length; i < k; ++i) {
|
|
|
|
const filename = argv[i];
|
|
|
|
|
|
|
|
let sourcePath = String(filename).replace(/\\/g, "/").replace(/(\.ts|\/)$/, "");
|
|
|
|
|
|
|
|
// Try entryPath.ts, then entryPath/index.ts
|
2018-12-03 19:29:49 +01:00
|
|
|
let sourceText = readFile(sourcePath + ".ts", baseDir);
|
2018-12-03 17:22:01 +01:00
|
|
|
if (sourceText === null) {
|
2018-12-03 19:29:49 +01:00
|
|
|
sourceText = readFile(sourcePath + "/index.ts", baseDir);
|
2018-12-03 17:22:01 +01:00
|
|
|
if (sourceText === null) {
|
|
|
|
return callback(Error("Entry file '" + sourcePath + ".ts' not found."));
|
|
|
|
} else {
|
|
|
|
sourcePath += "/index.ts";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sourcePath += ".ts";
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.parseCount++;
|
|
|
|
stats.parseTime += measure(() => {
|
|
|
|
parser = assemblyscript.parseFile(sourceText, sourcePath, true, parser);
|
|
|
|
});
|
|
|
|
let code = parseBacklog();
|
|
|
|
if (code) return code;
|
|
|
|
}
|
|
|
|
|
2018-05-26 13:13:39 +02:00
|
|
|
applyTransform("afterParse", parser);
|
2018-12-03 17:22:01 +01:00
|
|
|
{
|
|
|
|
let code = parseBacklog();
|
|
|
|
if (code) return code;
|
|
|
|
}
|
2018-05-26 13:13:39 +02:00
|
|
|
|
2018-03-17 01:37:05 +01:00
|
|
|
// Finish parsing
|
|
|
|
const program = assemblyscript.finishParsing(parser);
|
|
|
|
|
2018-06-29 00:14:42 +02:00
|
|
|
// Set up optimization levels
|
2018-07-03 03:06:01 +02:00
|
|
|
var optimizeLevel = 0;
|
2018-06-29 00:14:42 +02:00
|
|
|
var shrinkLevel = 0;
|
2018-07-03 03:06:01 +02:00
|
|
|
if (args.optimize) {
|
|
|
|
optimizeLevel = exports.defaultOptimizeLevel;
|
|
|
|
shrinkLevel = exports.defaultShrinkLevel;
|
2018-06-29 00:14:42 +02:00
|
|
|
}
|
|
|
|
if (typeof args.optimizeLevel === "number") {
|
|
|
|
optimizeLevel = args.optimizeLevel;
|
|
|
|
}
|
|
|
|
if (typeof args.shrinkLevel === "number") {
|
|
|
|
shrinkLevel = args.shrinkLevel;
|
|
|
|
}
|
2018-07-03 03:06:01 +02:00
|
|
|
optimizeLevel = Math.min(Math.max(optimizeLevel, 0), 3);
|
|
|
|
shrinkLevel = Math.min(Math.max(shrinkLevel, 0), 2);
|
2018-06-29 00:14:42 +02:00
|
|
|
|
2018-02-03 02:36:20 +01:00
|
|
|
// Begin compilation
|
2018-02-04 07:51:40 +01:00
|
|
|
const compilerOptions = assemblyscript.createOptions();
|
|
|
|
assemblyscript.setTarget(compilerOptions, 0);
|
2018-07-03 03:06:01 +02:00
|
|
|
assemblyscript.setNoTreeShaking(compilerOptions, args.noTreeShaking);
|
|
|
|
assemblyscript.setNoAssert(compilerOptions, args.noAssert);
|
|
|
|
assemblyscript.setImportMemory(compilerOptions, args.importMemory);
|
|
|
|
assemblyscript.setImportTable(compilerOptions, args.importTable);
|
2018-02-17 00:16:08 +01:00
|
|
|
assemblyscript.setMemoryBase(compilerOptions, args.memoryBase >>> 0);
|
2018-02-04 07:51:40 +01:00
|
|
|
assemblyscript.setSourceMap(compilerOptions, args.sourceMap != null);
|
2018-06-29 00:14:42 +02:00
|
|
|
assemblyscript.setOptimizeLevelHints(compilerOptions, optimizeLevel, shrinkLevel);
|
2018-03-30 17:25:54 +02:00
|
|
|
|
2018-07-22 05:13:39 -07:00
|
|
|
if (!args.noLib) {
|
|
|
|
// Initialize default aliases
|
|
|
|
assemblyscript.setGlobalAlias(compilerOptions, "Math", "NativeMath");
|
|
|
|
assemblyscript.setGlobalAlias(compilerOptions, "Mathf", "NativeMathf");
|
|
|
|
assemblyscript.setGlobalAlias(compilerOptions, "abort", "~lib/env/abort");
|
|
|
|
assemblyscript.setGlobalAlias(compilerOptions, "trace", "~lib/env/trace");
|
|
|
|
}
|
2018-03-26 16:54:25 +02:00
|
|
|
|
2018-03-30 17:25:54 +02:00
|
|
|
// Add or override aliases if specified
|
2018-07-04 21:47:58 +02:00
|
|
|
if (args.use) {
|
|
|
|
let aliases = args.use;
|
2018-03-26 16:54:25 +02:00
|
|
|
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();
|
2018-05-21 23:14:47 +02:00
|
|
|
if (!name.length) return callback(Error("Global alias '" + part + "' is invalid."));
|
2018-03-26 16:54:25 +02:00
|
|
|
assemblyscript.setGlobalAlias(compilerOptions, name, alias);
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 02:36:20 +01:00
|
|
|
|
2018-05-08 00:36:19 +02:00
|
|
|
// Enable additional features if specified
|
2018-05-08 14:37:51 +02:00
|
|
|
var features = args.enable;
|
2018-05-08 00:36:19 +02:00
|
|
|
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();
|
2018-05-08 09:27:56 +02:00
|
|
|
let flag = assemblyscript["FEATURE_" + name.replace(/\-/g, "_").toUpperCase()];
|
|
|
|
if (!flag) return callback(Error("Feature '" + name + "' is unknown."));
|
2018-05-08 00:36:19 +02:00
|
|
|
assemblyscript.enableFeature(compilerOptions, flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-03 02:36:20 +01:00
|
|
|
var module;
|
2018-02-05 17:10:14 +01:00
|
|
|
stats.compileCount++;
|
2018-03-03 18:38:38 +01:00
|
|
|
(() => {
|
|
|
|
try {
|
|
|
|
stats.compileTime += measure(() => {
|
2018-03-17 01:37:05 +01:00
|
|
|
module = assemblyscript.compileProgram(program, compilerOptions);
|
2018-03-03 18:38:38 +01:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return callback(e);
|
|
|
|
}
|
|
|
|
})();
|
2018-02-04 07:51:40 +01:00
|
|
|
if (checkDiagnostics(parser, stderr)) {
|
2018-02-03 02:36:20 +01:00
|
|
|
if (module) module.dispose();
|
2018-02-04 07:51:40 +01:00
|
|
|
return callback(Error("Compile error"));
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the module if requested
|
|
|
|
if (args.validate) {
|
2018-02-05 17:10:14 +01:00
|
|
|
stats.validateCount++;
|
2018-02-03 02:36:20 +01:00
|
|
|
stats.validateTime += measure(() => {
|
|
|
|
if (!module.validate()) {
|
|
|
|
module.dispose();
|
2018-02-04 07:51:40 +01:00
|
|
|
return callback(Error("Validate error"));
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
2018-01-18 15:45:34 +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") {
|
2018-02-05 17:10:14 +01:00
|
|
|
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") {
|
2018-02-05 17:10:14 +01:00
|
|
|
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();
|
2018-02-04 07:51:40 +01:00
|
|
|
return callback(Error("Unsupported trap mode"));
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
|
|
|
|
2018-06-09 14:05:33 +02: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;
|
|
|
|
|
2018-06-29 00:14:42 +02:00
|
|
|
module.setOptimizeLevel(optimizeLevel);
|
2018-02-03 02:36:20 +01:00
|
|
|
module.setShrinkLevel(shrinkLevel);
|
2018-11-09 12:28:10 +01:00
|
|
|
module.setDebugInfo(args.debug);
|
2018-02-03 02:36:20 +01:00
|
|
|
|
|
|
|
var runPasses = [];
|
|
|
|
if (args.runPasses) {
|
2018-03-03 18:38:38 +01:00
|
|
|
if (typeof args.runPasses === "string") {
|
2018-02-03 02:36:20 +01:00
|
|
|
args.runPasses = args.runPasses.split(",");
|
2018-03-03 18:38:38 +01:00
|
|
|
}
|
|
|
|
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-03-03 18:38:38 +01:00
|
|
|
}
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Optimize the module if requested
|
2018-06-29 00:14:42 +02:00
|
|
|
if (optimizeLevel > 0 || shrinkLevel > 0) {
|
2018-02-05 17:10:14 +01:00
|
|
|
stats.optimizeCount++;
|
2018-02-14 09:18:43 +01:00
|
|
|
stats.optimizeTime += measure(() => {
|
|
|
|
module.optimize();
|
|
|
|
});
|
2018-02-05 17:10:14 +01:00
|
|
|
}
|
2018-02-03 02:36:20 +01:00
|
|
|
|
|
|
|
// Run additional passes if requested
|
2018-02-05 17:10:14 +01:00
|
|
|
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-05 17:10:14 +01:00
|
|
|
}
|
2018-02-03 02:36:20 +01:00
|
|
|
|
|
|
|
// Prepare output
|
|
|
|
if (!args.noEmit) {
|
2018-02-04 07:51:40 +01:00
|
|
|
let hasStdout = false;
|
2018-03-21 23:34:40 +01:00
|
|
|
let hasOutput = false;
|
2018-02-03 02:36:20 +01:00
|
|
|
|
|
|
|
if (args.outFile != null) {
|
2018-03-03 18:38:38 +01:00
|
|
|
if (/\.was?t$/.test(args.outFile) && args.textFile == null) {
|
2018-02-03 02:36:20 +01:00
|
|
|
args.textFile = args.outFile;
|
2018-03-03 18:38:38 +01:00
|
|
|
} else if (/\.js$/.test(args.outFile) && args.asmjsFile == null) {
|
2018-02-03 02:36:20 +01:00
|
|
|
args.asmjsFile = args.outFile;
|
2018-03-03 18:38:38 +01:00
|
|
|
} else if (args.binaryFile == null) {
|
2018-02-03 02:36:20 +01:00
|
|
|
args.binaryFile = args.outFile;
|
2018-03-03 18:38:38 +01:00
|
|
|
}
|
2018-01-18 15:45:34 +01:00
|
|
|
}
|
2017-12-05 15:06:44 +01:00
|
|
|
|
2018-02-04 07:51:40 +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;
|
2018-02-04 07:51:40 +01:00
|
|
|
|
2018-03-17 01:37:05 +01:00
|
|
|
let wasm;
|
2018-02-05 17:10:14 +01:00
|
|
|
stats.emitCount++;
|
2018-02-14 09:18:43 +01:00
|
|
|
stats.emitTime += measure(() => {
|
2018-03-17 01:37:05 +01:00
|
|
|
wasm = module.toBinary(sourceMapURL)
|
2018-02-14 09:18:43 +01:00
|
|
|
});
|
2018-02-04 07:51:40 +01:00
|
|
|
|
|
|
|
if (args.binaryFile.length) {
|
2018-12-03 19:29:49 +01:00
|
|
|
writeFile(args.binaryFile, wasm.output, baseDir);
|
2018-02-04 07:51:40 +01:00
|
|
|
} else {
|
2018-03-17 01:37:05 +01:00
|
|
|
writeStdout(wasm.output);
|
2018-02-04 07:51:40 +01:00
|
|
|
hasStdout = true;
|
|
|
|
}
|
2018-03-21 23:34:40 +01:00
|
|
|
hasOutput = true;
|
2018-02-04 07:51:40 +01:00
|
|
|
|
|
|
|
// Post-process source map
|
2018-03-17 01:37:05 +01:00
|
|
|
if (wasm.sourceMap != null) {
|
2018-02-04 07:51:40 +01:00
|
|
|
if (args.binaryFile.length) {
|
2018-03-17 01:37:05 +01:00
|
|
|
let sourceMap = JSON.parse(wasm.sourceMap);
|
2018-02-12 18:54:17 +01:00
|
|
|
sourceMap.sourceRoot = exports.sourceMapRoot;
|
2018-02-04 07:51:40 +01:00
|
|
|
sourceMap.sources.forEach((name, index) => {
|
2018-02-05 17:10:14 +01:00
|
|
|
let text = null;
|
2018-02-12 18:54:17 +01:00
|
|
|
if (name.startsWith(exports.libraryPrefix)) {
|
2018-04-08 00:43:38 +02:00
|
|
|
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) {
|
2018-12-03 19:29:49 +01:00
|
|
|
text = readFile(name.substring(exports.libraryPrefix.length), customLibDirs[i]);
|
2018-04-08 00:43:38 +02:00
|
|
|
if (text !== null) break;
|
|
|
|
}
|
2018-02-04 07:51:40 +01:00
|
|
|
}
|
2018-03-03 18:38:38 +01:00
|
|
|
} else {
|
2018-12-03 19:29:49 +01:00
|
|
|
text = readFile(name, baseDir);
|
2018-03-03 18:38:38 +01:00
|
|
|
}
|
|
|
|
if (text === null) {
|
2018-02-04 07:51:40 +01:00
|
|
|
return callback(Error("Source file '" + name + "' not found."));
|
2018-03-03 18:38:38 +01:00
|
|
|
}
|
|
|
|
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(
|
|
|
|
path.dirname(args.binaryFile),
|
|
|
|
path.basename(sourceMapURL)
|
2018-12-03 19:29:49 +01:00
|
|
|
).replace(/^\.\//, ""), JSON.stringify(sourceMap), baseDir);
|
2018-02-04 07:51:40 +01:00
|
|
|
} else {
|
2018-03-31 00:03:02 +02:00
|
|
|
stderr.write("Skipped source map (stdout already occupied)" + EOL);
|
2018-02-04 07:51:40 +01:00
|
|
|
}
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 01:37:05 +01:00
|
|
|
// Write asm.js
|
|
|
|
if (args.asmjsFile != null) {
|
|
|
|
let asm;
|
|
|
|
if (args.asmjsFile.length) {
|
2018-02-05 17:10:14 +01:00
|
|
|
stats.emitCount++;
|
2018-02-14 09:18:43 +01:00
|
|
|
stats.emitTime += measure(() => {
|
2018-03-17 01:37:05 +01:00
|
|
|
asm = module.toAsmjs();
|
2018-02-14 09:18:43 +01:00
|
|
|
});
|
2018-12-03 19:29:49 +01:00
|
|
|
writeFile(args.asmjsFile, asm, baseDir);
|
2018-02-04 07:51:40 +01:00
|
|
|
} else if (!hasStdout) {
|
2018-02-05 17:10:14 +01:00
|
|
|
stats.emitCount++;
|
2018-02-14 09:18:43 +01:00
|
|
|
stats.emitTime += measure(() => {
|
2018-03-17 01:37:05 +01:00
|
|
|
asm = module.toAsmjs();
|
2018-02-14 09:18:43 +01:00
|
|
|
});
|
2018-03-17 01:37:05 +01:00
|
|
|
writeStdout(asm);
|
2018-02-04 07:51:40 +01:00
|
|
|
hasStdout = true;
|
|
|
|
}
|
2018-03-21 23:34:40 +01:00
|
|
|
hasOutput = true;
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
2017-12-09 02:38:17 +01:00
|
|
|
|
2018-03-17 01:37:05 +01:00
|
|
|
// Write WebIDL
|
|
|
|
if (args.idlFile != null) {
|
|
|
|
let idl;
|
|
|
|
if (args.idlFile.length) {
|
2018-02-05 17:10:14 +01:00
|
|
|
stats.emitCount++;
|
2018-02-14 09:18:43 +01:00
|
|
|
stats.emitTime += measure(() => {
|
2018-03-17 01:37:05 +01:00
|
|
|
idl = assemblyscript.buildIDL(program);
|
2018-02-14 09:18:43 +01:00
|
|
|
});
|
2018-12-03 19:29:49 +01:00
|
|
|
writeFile(args.idlFile, idl, baseDir);
|
2018-02-04 07:51:40 +01:00
|
|
|
} else if (!hasStdout) {
|
2018-02-05 17:10:14 +01:00
|
|
|
stats.emitCount++;
|
2018-02-14 09:18:43 +01:00
|
|
|
stats.emitTime += measure(() => {
|
2018-03-17 01:37:05 +01:00
|
|
|
idl = assemblyscript.buildIDL(program);
|
2018-02-14 09:18:43 +01:00
|
|
|
});
|
2018-03-17 01:37:05 +01:00
|
|
|
writeStdout(idl);
|
2018-02-04 07:51:40 +01:00
|
|
|
hasStdout = true;
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
2018-03-21 23:34:40 +01:00
|
|
|
hasOutput = true;
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
2018-03-17 01:37:05 +01:00
|
|
|
|
|
|
|
// Write TypeScript definition
|
|
|
|
if (args.tsdFile != null) {
|
|
|
|
let tsd;
|
|
|
|
if (args.tsdFile.length) {
|
|
|
|
stats.emitCount++;
|
|
|
|
stats.emitTime += measure(() => {
|
|
|
|
tsd = assemblyscript.buildTSD(program);
|
|
|
|
});
|
2018-12-03 19:29:49 +01:00
|
|
|
writeFile(args.tsdFile, tsd, baseDir);
|
2018-03-17 01:37:05 +01:00
|
|
|
} else if (!hasStdout) {
|
|
|
|
stats.emitCount++;
|
|
|
|
stats.emitTime += measure(() => {
|
|
|
|
tsd = assemblyscript.buildTSD(program);
|
|
|
|
});
|
|
|
|
writeStdout(tsd);
|
|
|
|
hasStdout = true;
|
|
|
|
}
|
2018-03-21 23:34:40 +01:00
|
|
|
hasOutput = true;
|
2018-03-17 01:37:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write text (must be last)
|
2018-03-21 23:34:40 +01:00
|
|
|
if (args.textFile != null || !hasOutput) {
|
2018-03-17 01:37:05 +01:00
|
|
|
let wat;
|
|
|
|
if (args.textFile && args.textFile.length) {
|
|
|
|
stats.emitCount++;
|
|
|
|
stats.emitTime += measure(() => {
|
|
|
|
wat = module.toText();
|
|
|
|
});
|
2018-12-03 19:29:49 +01:00
|
|
|
writeFile(args.textFile, wat, baseDir);
|
2018-03-17 01:37:05 +01:00
|
|
|
} else if (!hasStdout) {
|
|
|
|
stats.emitCount++;
|
|
|
|
stats.emitTime += measure(() => {
|
|
|
|
wat = module.toText()
|
|
|
|
});
|
|
|
|
writeStdout(wat);
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.dispose();
|
2018-03-03 18:38:38 +01:00
|
|
|
if (args.measure) {
|
2018-02-05 17:10:14 +01:00
|
|
|
printStats(stats, stderr);
|
2018-03-03 18:38:38 +01:00
|
|
|
}
|
2018-02-04 07:51:40 +01:00
|
|
|
return callback(null);
|
2018-02-05 17:10:14 +01:00
|
|
|
|
2018-12-03 19:29:49 +01:00
|
|
|
function readFileNode(filename, baseDir) {
|
2018-02-05 17:10:14 +01:00
|
|
|
try {
|
2018-03-13 14:03:57 +01:00
|
|
|
let text;
|
2018-02-05 17:10:14 +01:00
|
|
|
stats.readCount++;
|
2018-02-12 18:54:17 +01:00
|
|
|
stats.readTime += measure(() => {
|
2018-12-03 19:29:49 +01:00
|
|
|
text = fs.readFileSync(path.join(baseDir, filename), { encoding: "utf8" });
|
2018-02-12 18:54:17 +01:00
|
|
|
});
|
2018-02-05 17:10:14 +01:00
|
|
|
return text;
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 19:29:49 +01:00
|
|
|
function writeFileNode(filename, contents, baseDir) {
|
2018-02-05 17:10:14 +01:00
|
|
|
try {
|
|
|
|
stats.writeCount++;
|
2018-02-12 18:54:17 +01:00
|
|
|
stats.writeTime += measure(() => {
|
2018-12-03 19:29:49 +01:00
|
|
|
mkdirp(path.join(baseDir, path.dirname(filename)));
|
2018-03-03 18:38:38 +01:00
|
|
|
if (typeof contents === "string") {
|
2018-12-03 19:29:49 +01:00
|
|
|
fs.writeFileSync(path.join(baseDir, filename), contents, { encoding: "utf8" } );
|
2018-03-03 18:38:38 +01:00
|
|
|
} else {
|
2018-12-03 19:29:49 +01:00
|
|
|
fs.writeFileSync(path.join(baseDir, filename), contents);
|
2018-03-03 18:38:38 +01:00
|
|
|
}
|
2018-02-12 18:54:17 +01:00
|
|
|
});
|
2018-02-05 17:10:14 +01:00
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 19:29:49 +01:00
|
|
|
function listFilesNode(dirname, baseDir) {
|
2018-02-09 15:43:57 +01:00
|
|
|
var files;
|
|
|
|
try {
|
2018-02-12 18:54:17 +01:00
|
|
|
stats.readTime += measure(() => {
|
2018-12-03 19:29:49 +01:00
|
|
|
files = fs.readdirSync(path.join(baseDir, dirname)).filter(file => /^(?!.*\.d\.ts$).*\.ts$/.test(file));
|
2018-02-12 18:54:17 +01:00
|
|
|
});
|
2018-02-09 15:43:57 +01:00
|
|
|
return files;
|
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 17:10:14 +01:00
|
|
|
function writeStdout(contents) {
|
|
|
|
if (!writeStdout.used) {
|
|
|
|
stats.writeCount++;
|
|
|
|
writeStdout.used = true;
|
|
|
|
}
|
2018-02-12 18:54:17 +01:00
|
|
|
stats.writeTime += measure(() => {
|
2018-03-03 18:38:38 +01:00
|
|
|
if (typeof contents === "string") {
|
2018-02-12 18:54:17 +01:00
|
|
|
stdout.write(contents, { encoding: "utf8" });
|
2018-03-03 18:38:38 +01:00
|
|
|
} else {
|
2018-02-12 18:54:17 +01:00
|
|
|
stdout.write(contents);
|
2018-03-03 18:38:38 +01:00
|
|
|
}
|
2018-02-12 18:54:17 +01:00
|
|
|
});
|
2018-02-05 17:10:14 +01:00
|
|
|
}
|
2018-01-18 05:56:45 +01:00
|
|
|
}
|
2018-02-03 02:36:20 +01:00
|
|
|
|
2018-07-03 03:06:01 +02:00
|
|
|
var argumentSubstitutions = {
|
|
|
|
"-O" : [ "--optimize" ],
|
|
|
|
"-Os" : [ "--optimize", "--shrinkLevel", "1" ],
|
|
|
|
"-Oz" : [ "--optimize", "--shrinkLevel", "2" ],
|
|
|
|
"-O0" : [ "--optimizeLevel", "0", "--shrinkLevel", "0" ],
|
|
|
|
"-O0s": [ "--optimizeLevel", "0", "--shrinkLevel", "1" ],
|
|
|
|
"-O0z": [ "--optimizeLevel", "0", "--shrinkLevel", "2" ],
|
|
|
|
"-O1" : [ "--optimizeLevel", "1", "--shrinkLevel", "0" ],
|
|
|
|
"-O1s": [ "--optimizeLevel", "1", "--shrinkLevel", "1" ],
|
|
|
|
"-O1z": [ "--optimizeLevel", "1", "--shrinkLevel", "2" ],
|
|
|
|
"-O2" : [ "--optimizeLevel", "2", "--shrinkLevel", "0" ],
|
|
|
|
"-O2s": [ "--optimizeLevel", "2", "--shrinkLevel", "1" ],
|
|
|
|
"-O2z": [ "--optimizeLevel", "2", "--shrinkLevel", "2" ],
|
|
|
|
"-O3" : [ "--optimizeLevel", "3", "--shrinkLevel", "0" ],
|
|
|
|
"-O3s": [ "--optimizeLevel", "3", "--shrinkLevel", "1" ],
|
|
|
|
"-O3z": [ "--optimizeLevel", "3", "--shrinkLevel", "2" ],
|
|
|
|
};
|
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
|
|
|
|
);
|
|
|
|
}
|
2018-03-03 18:38:38 +01:00
|
|
|
if (assemblyscript.isError(diagnostic)) hasErrors = true;
|
2018-02-03 02:36:20 +01:00
|
|
|
}
|
|
|
|
return hasErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.checkDiagnostics = checkDiagnostics;
|
2018-01-18 05:56:45 +01:00
|
|
|
|
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,
|
2018-02-05 17:10:14 +01:00
|
|
|
parseCount: 0,
|
2018-02-03 02:36:20 +01:00
|
|
|
compileTime: 0,
|
2018-02-05 17:10:14 +01:00
|
|
|
compileCount: 0,
|
|
|
|
emitTime: 0,
|
|
|
|
emitCount: 0,
|
2018-02-03 02:36:20 +01:00
|
|
|
validateTime: 0,
|
2018-02-05 17:10:14 +01:00
|
|
|
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
|
|
|
|
2018-02-09 16:40:53 +01:00
|
|
|
exports.createStats = createStats;
|
|
|
|
|
2018-03-03 18:38:38 +01:00
|
|
|
if (!process.hrtime) process.hrtime = require("browser-process-hrtime");
|
2018-02-04 07:51:40 +01:00
|
|
|
|
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];
|
|
|
|
}
|
2018-02-04 07:51:40 +01:00
|
|
|
|
|
|
|
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. */
|
2018-02-04 07:51:40 +01:00
|
|
|
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
|
|
|
}
|
2018-02-04 07:51:40 +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)
|
2018-03-31 00:03:02 +02:00
|
|
|
].join(EOL) + EOL);
|
2018-02-04 07:51:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.printStats = printStats;
|
2018-02-09 15:43:57 +01:00
|
|
|
|
2018-04-27 00:08:41 +02: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-03-31 00:03:02 +02:00
|
|
|
|
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") {
|
2018-04-26 03:18:39 +03:00
|
|
|
let buffer = allocBuffer(utf8.length(chunk));
|
2018-03-31 00:03:02 +02:00
|
|
|
utf8.write(chunk, buffer, 0);
|
|
|
|
chunk = buffer;
|
2018-02-09 15:43:57 +01:00
|
|
|
}
|
2018-03-31 00:03:02 +02: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() {
|
2018-03-31 00:03:02 +02:00
|
|
|
var offset = 0, i = 0, k = this.length;
|
|
|
|
while (i < k) offset += this[i++].length;
|
2018-04-26 03:18:39 +03:00
|
|
|
var buffer = allocBuffer(offset);
|
2018-03-31 00:03:02 +02:00
|
|
|
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-02-10 18:34:29 +01:00
|
|
|
|
2018-05-28 18:55:51 +02:00
|
|
|
/** Compatible TypeScript compiler options for syntax highlighting etc. */
|
2018-02-10 18:34:29 +01:00
|
|
|
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
|
|
|
|
};
|