Accept custom library paths in asc, see #19

This commit is contained in:
dcodeIO 2018-01-30 01:20:20 +01:00
parent 1995bf9eb4
commit cae89e0b1f
2 changed files with 52 additions and 24 deletions

View File

@ -104,13 +104,22 @@ function checkDiagnostics(parser) {
process.exit(1);
}
// Include standard library
var stdlibDir = path.join(__dirname, "..", "std", "assembly");
if (!args.noLib) {
// Include standard library if --noLib isn't set
var libDirs = args.noLib ? [] : [ path.join(__dirname, "..", "std", "assembly") ];
// Include custom library components (with or without stdlib)
if (args.lib) {
if (Array.isArray(args.lib))
Array.prototype.push.apply(libDirs, args.lib.map(dir));
else
libDirs.push(args.lib);
}
libDirs.forEach(libDir => {
var notIoTime = 0;
readTime += measure(() => {
glob.sync("*.ts", { cwd: stdlibDir }).forEach(file => {
var nextText = fs.readFileSync(path.join(stdlibDir, file), { encoding: "utf8" });
glob.sync("*.ts", { cwd: libDir }).forEach(file => {
var nextText = fs.readFileSync(path.join(libDir, file), { encoding: "utf8" });
++readCount;
var time = measure(() => {
parser = assemblyscript.parseFile(nextText, "std:" + file, parser, false);
@ -119,7 +128,7 @@ if (!args.noLib) {
notIoTime += time;
});
}) - notIoTime;
}
});
// Include entry files
args._.forEach(filename => {
@ -154,25 +163,40 @@ args._.forEach(filename => {
});
while ((nextPath = parser.nextFile()) != null) {
try {
readTime += measure(() => {
if (nextPath.startsWith("std:"))
nextText = fs.readFileSync(stdlibDir + "/" + nextPath.substring(4) + ".ts", { encoding: "utf8" });
else
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });
});
++readCount;
} catch (e) {
try {
var found = false;
if (nextPath.startsWith("std:")) {
for (var i = 0; i < libDirs.length; ++i) {
readTime += measure(() => {
nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" });
try {
nextText = fs.readFileSync(libDirs[i] + "/" + nextPath.substring(4) + ".ts", { encoding: "utf8" });
found = true;
} catch (e) {}
});
++readCount;
nextPath = nextPath + "/index";
} catch (e) {
console.error("Imported file '" + nextPath + ".ts' not found.");
process.exit(1);
if (found)
break;
}
} else {
readTime += measure(() => {
try {
nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" });
found = true;
} catch (e) {}
});
++readCount;
if (!found) {
readTime += measure(() => {
try {
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });
found = true;
} catch (e) {}
});
++readCount;
}
}
if (!found) {
console.error("Imported file '" + nextPath + ".ts' not found.");
process.exit(1);
}
parseTime += measure(() => {
assemblyscript.parseFile(nextText, nextPath, parser);

View File

@ -74,13 +74,17 @@
"desc": "Performs compilation as usual without emitting code.",
"type": "boolean"
},
"noMemory": {
"desc": "Does not set up a memory.",
"type": "boolean"
},
"noLib": {
"desc": "Does not include the standard library.",
"type": "boolean"
},
"noMemory": {
"desc": "Does not set up a memory.",
"type": "boolean"
"lib": {
"desc": "Adds one or multiple paths to custom library components.",
"type": "string"
},
"trapMode": {
"desc": [