Skip to content

Commit cae89e0

Browse files
committed
Accept custom library paths in asc, see #19
1 parent 1995bf9 commit cae89e0

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

bin/asc.js

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,22 @@ function checkDiagnostics(parser) {
104104
process.exit(1);
105105
}
106106

107-
// Include standard library
108-
var stdlibDir = path.join(__dirname, "..", "std", "assembly");
109-
if (!args.noLib) {
107+
// Include standard library if --noLib isn't set
108+
var libDirs = args.noLib ? [] : [ path.join(__dirname, "..", "std", "assembly") ];
109+
110+
// Include custom library components (with or without stdlib)
111+
if (args.lib) {
112+
if (Array.isArray(args.lib))
113+
Array.prototype.push.apply(libDirs, args.lib.map(dir));
114+
else
115+
libDirs.push(args.lib);
116+
}
117+
118+
libDirs.forEach(libDir => {
110119
var notIoTime = 0;
111120
readTime += measure(() => {
112-
glob.sync("*.ts", { cwd: stdlibDir }).forEach(file => {
113-
var nextText = fs.readFileSync(path.join(stdlibDir, file), { encoding: "utf8" });
121+
glob.sync("*.ts", { cwd: libDir }).forEach(file => {
122+
var nextText = fs.readFileSync(path.join(libDir, file), { encoding: "utf8" });
114123
++readCount;
115124
var time = measure(() => {
116125
parser = assemblyscript.parseFile(nextText, "std:" + file, parser, false);
@@ -119,7 +128,7 @@ if (!args.noLib) {
119128
notIoTime += time;
120129
});
121130
}) - notIoTime;
122-
}
131+
});
123132

124133
// Include entry files
125134
args._.forEach(filename => {
@@ -154,26 +163,41 @@ args._.forEach(filename => {
154163
});
155164

156165
while ((nextPath = parser.nextFile()) != null) {
157-
try {
166+
var found = false;
167+
if (nextPath.startsWith("std:")) {
168+
for (var i = 0; i < libDirs.length; ++i) {
169+
readTime += measure(() => {
170+
try {
171+
nextText = fs.readFileSync(libDirs[i] + "/" + nextPath.substring(4) + ".ts", { encoding: "utf8" });
172+
found = true;
173+
} catch (e) {}
174+
});
175+
++readCount;
176+
if (found)
177+
break;
178+
}
179+
} else {
158180
readTime += measure(() => {
159-
if (nextPath.startsWith("std:"))
160-
nextText = fs.readFileSync(stdlibDir + "/" + nextPath.substring(4) + ".ts", { encoding: "utf8" });
161-
else
162-
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });
181+
try {
182+
nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" });
183+
found = true;
184+
} catch (e) {}
163185
});
164186
++readCount;
165-
} catch (e) {
166-
try {
187+
if (!found) {
167188
readTime += measure(() => {
168-
nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" });
189+
try {
190+
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });
191+
found = true;
192+
} catch (e) {}
169193
});
170194
++readCount;
171-
nextPath = nextPath + "/index";
172-
} catch (e) {
173-
console.error("Imported file '" + nextPath + ".ts' not found.");
174-
process.exit(1);
175195
}
176196
}
197+
if (!found) {
198+
console.error("Imported file '" + nextPath + ".ts' not found.");
199+
process.exit(1);
200+
}
177201
parseTime += measure(() => {
178202
assemblyscript.parseFile(nextText, nextPath, parser);
179203
});

bin/asc.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,17 @@
7474
"desc": "Performs compilation as usual without emitting code.",
7575
"type": "boolean"
7676
},
77+
"noMemory": {
78+
"desc": "Does not set up a memory.",
79+
"type": "boolean"
80+
},
7781
"noLib": {
7882
"desc": "Does not include the standard library.",
7983
"type": "boolean"
8084
},
81-
"noMemory": {
82-
"desc": "Does not set up a memory.",
83-
"type": "boolean"
85+
"lib": {
86+
"desc": "Adds one or multiple paths to custom library components.",
87+
"type": "string"
8488
},
8589
"trapMode": {
8690
"desc": [

0 commit comments

Comments
 (0)