-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathcompiler.js
241 lines (216 loc) · 7.6 KB
/
compiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const fs = require("fs");
const path = require("path");
const os = require("os");
const glob = require("glob");
const colorsUtil = require("../cli/util/colors");
const optionsUtil = require("../cli/util/options");
const diff = require("./util/diff");
const asc = require("../cli/asc.js");
const config = {
"create": {
"description": [
"Recreates the fixture for the specified test(s)",
"or all the fixtures if no specific test is given."
],
"type": "b"
},
"help": {
"description": "Prints this message and exits.",
"type": "b",
"alias": "h"
}
};
const opts = optionsUtil.parse(process.argv.slice(2),config);
const args = opts.options;
const argv = opts.arguments;
if (args.help) {
console.log([
colorsUtil.white("SYNTAX"),
" " + colorsUtil.cyan("npm run test:compiler --") + " [test1, test2 ...] [options]",
"",
colorsUtil.white("OPTIONS"),
optionsUtil.help(config)
].join(os.EOL) + os.EOL);
process.exit(0);
}
var successes = 0;
var failedTests = [];
const basedir = path.join(__dirname, "compiler");
// Get a list of all tests
var tests = glob.sync("**/!(_*).ts", { cwd: basedir });
// Run specific tests only if arguments are provided
if (argv.length) {
tests = tests.filter(filename => argv.indexOf(filename.replace(/\.ts$/, "")) >= 0);
if (!tests.length) {
console.error("No matching tests: " + argv.join(" "));
process.exit(1);
}
}
const EXPECT_ERROR_PREFIX = '// Expect error:';
// Returns an array of error strings to expect, or null if compilation should succeed.
function getExpectedErrors(filePath) {
const lines = fs.readFileSync(filePath).toString().split('\n');
const expectErrorLines = lines.filter(line => line.startsWith(EXPECT_ERROR_PREFIX));
if (expectErrorLines.length === 0) {
return null;
}
return expectErrorLines.map(line => line.slice(EXPECT_ERROR_PREFIX.length).trim());
}
// TODO: asc's callback is synchronous here. This might change.
tests.forEach(filename => {
console.log(colorsUtil.white("Testing compiler/" + filename) + "\n");
const expectedErrors = getExpectedErrors(path.join(basedir, filename));
const basename = filename.replace(/\.ts$/, "");
const stdout = asc.createMemoryStream();
const stderr = asc.createMemoryStream(chunk => process.stderr.write(chunk.toString().replace(/^(?!$)/mg, " ")));
stderr.isTTY = true;
var failed = false;
// TODO: also save stdout/stderr and diff it (-> expected failures)
// Build unoptimized
asc.main([
filename,
"--baseDir", basedir,
"--validate",
"--measure",
"--debug",
"--textFile" // -> stdout
], {
stdout: stdout,
stderr: stderr
}, err => {
console.log();
if (expectedErrors) {
const stderrString = stderr.toString();
for (const expectedError of expectedErrors) {
if (!stderrString.includes(expectedError)) {
console.log(`Expected error "${expectedError}" was not in the error output.`);
console.log("- " + colorsUtil.red("error check ERROR"));
failedTests.push(basename);
console.log();
return;
}
}
console.log("- " + colorsUtil.green("error check OK"));
++successes;
console.log();
return;
}
if (err)
stderr.write(err + os.EOL);
var actual = stdout.toString().replace(/\r\n/g, "\n");
if (args.create) {
fs.writeFileSync(path.join(basedir, basename + ".untouched.wat"), actual, { encoding: "utf8" });
console.log("- " + colorsUtil.yellow("Created fixture"));
} else {
let expected = fs.readFileSync(path.join(basedir, basename + ".untouched.wat"), { encoding: "utf8" }).replace(/\r\n/g, "\n");
let diffs = diff(basename + ".untouched.wat", expected, actual);
if (diffs !== null) {
console.log(diffs);
console.log("- " + colorsUtil.red("diff ERROR"));
failed = true;
} else
console.log("- " + colorsUtil.green("diff OK"));
}
console.log();
stdout.length = 0;
stderr.length = 0;
// Build optimized
var cmd = [
filename,
"--baseDir", basedir,
"--validate",
"--measure",
"--binaryFile", // -> stdout
"-O3"
];
if (args.create) cmd.push(
"--textFile", basename + ".optimized.wat"
);
asc.main(cmd, {
stdout: stdout,
stderr: stderr
}, err => {
console.log();
if (err)
stderr.write(err.stack + os.EOL);
// Instantiate
try {
let memory = new WebAssembly.Memory({ initial: 10 });
let exports = {};
function getString(ptr) {
if (!ptr) return "null";
var U32 = new Uint32Array(exports.memory ? exports.memory.buffer : memory.buffer);
var U16 = new Uint16Array(exports.memory ? exports.memory.buffer : memory.buffer);
var dataLength = U32[ptr >>> 2];
var dataOffset = (ptr + 4) >>> 1;
var dataRemain = dataLength;
var parts = [];
const chunkSize = 1024;
while (dataRemain > chunkSize) {
let last = U16[dataOffset + chunkSize - 1];
let size = last >= 0xD800 && last < 0xDC00 ? chunkSize - 1 : chunkSize;
let part = U16.subarray(dataOffset, dataOffset += size);
parts.push(String.fromCharCode.apply(String, part));
dataRemain -= size;
}
return parts.join("") + String.fromCharCode.apply(String, U16.subarray(dataOffset, dataOffset + dataRemain));
}
let runTime = asc.measure(() => {
exports = new WebAssembly.Instance(new WebAssembly.Module(stdout.toBuffer()), {
env: {
memory,
abort: function(msg, file, line, column) {
console.log(colorsUtil.red(" abort: " + getString(msg) + " at " + getString(file) + ":" + line + ":" + column));
},
trace: function(msg, n) {
console.log(" trace: " + getString(msg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", "));
},
externalFunction: function() { },
externalConstant: 1
},
math: {
mod: function(a, b) { return a % b; }
},
Math,
Date,
// tests/declare
declare: {
externalFunction: function() { },
externalConstant: 1,
"my.externalFunction": function() { },
"my.externalConstant": 2
},
// tests/external
external: {
foo: function() {},
"foo.bar": function() {},
bar: function() {}
},
foo: {
baz: function() {},
"var": 3
}
}).exports;
if (exports.main) {
console.log(colorsUtil.white(" [main]"));
var code = exports.main();
console.log(colorsUtil.white(" [exit " + code + "]\n"));
}
});
console.log("- " + colorsUtil.green("instantiate OK") + " (" + asc.formatTime(runTime) + ")");
console.log("\n " + Object.keys(exports).map(key => "[" + (typeof exports[key]).substring(0, 3) + "] " + key).join("\n "));
} catch (e) {
console.log("- " + colorsUtil.red("instantiate ERROR: ") + e.stack);
failed = true;
}
if (failed) failedTests.push(basename);
else ++successes;
console.log();
});
});
});
if (failedTests.length) {
process.exitCode = 1;
console.log(colorsUtil.red("ERROR: ") + failedTests.length + " compiler tests failed: " + failedTests.join(", "));
} else
console.log("[ " + colorsUtil.white("SUCCESS") + " ]");