-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnode.js
More file actions
133 lines (113 loc) · 3.61 KB
/
Copy pathnode.js
File metadata and controls
133 lines (113 loc) · 3.61 KB
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
const { TestContext, VerboseReporter } = require("@as-pect/core");
const { instantiateBuffer } = require("assemblyscript/lib/loader");
const glob = require("glob");
const { main } = require("assemblyscript/cli/asc");
const { parse } = require("assemblyscript/cli/util/options");
const path = require("path");
const fs = require("fs");
const Wasi = require("wasi");
const wasi = new Wasi({});
let pass = true;
const options = parse(process.argv.slice(2), {
"help": {
"description": "Prints this message and exits.",
"type": "b",
"alias": "h"
},
"updateFixtures": {
"description": "Update the test fixtures.",
"type": "b",
"alias": "u"
},
});
if (options.unknown.length > 1) {
console.error("Unknown options arguments: " + options.unknown.join(" "));
process.exit(1);
}
const reporter = new VerboseReporter();
function relativeFromCwd(location) {
return path.relative(process.cwd(), location);
}
const ascOptions = [
relativeFromCwd(require.resolve("@as-pect/assembly/assembly/index.ts")),
"--use", "ASC_RTRACE=1",
"--explicitStart",
"--validate",
"--debug",
"--measure",
"--lib", "assembly",
];
const files = glob.sync("tests/**/*.spec.ts")
.map(relativeFromCwd);
const untouchedBinaryMap = new Map();
const optimizedBinaryMap = new Map();
const untouchedWatMap = new Map();
const optimizedWatMap = new Map();
const untouchedAscOptions = {
writeFile(name, contents) {
const ext = path.extname(name);
if (ext === ".wasm") untouchedBinaryMap.set(name, contents);
if (ext === ".wat") untouchedWatMap.set(name, contents);
}
};
const optimizedAscOptions = {
writeFile(name, contents) {
const ext = path.extname(name);
if (ext === ".wasm") optimizedBinaryMap.set(name, contents);
if (ext === ".wat") optimizedWatMap.set(name, contents);
}
};
const errors = [];
for (const file of files) {
const ext = path.extname(file);
const wasmFileName = path.join(path.dirname(file), path.basename(file, ext)) + ".wasm";
const watFileName = path.join(path.dirname(file), path.basename(file, ext)) + ".wat";
const cliOptions = ascOptions.concat([file, "--binaryFile", wasmFileName, "--textFile", watFileName]);
process.stdout.write("Test File : " + file + " (untouched)\n");
main(cliOptions, untouchedAscOptions, (err) => {
if (err) {
console.error(err);
errors.push(err);
} else {
const binary = untouchedBinaryMap.get(wasmFileName);
const wat = untouchedWatMap.get(watFileName);
runTest(file, "untouched", binary, wat);
}
});
process.stdout.write("\n");
process.stdout.write("Test File : " + file + " (optimized)\n");
main(cliOptions.concat(["-O3"]), optimizedAscOptions, (err) => {
if (err) {
console.error(err);
errors.push(err);
} else {
const binary = optimizedBinaryMap.get(wasmFileName);
const wat = optimizedWatMap.get(watFileName);
runTest(file, "optimized", binary, wat);
}
});
process.stdout.write("\n");
}
function runTest(file, type, binary, wat) {
const watPath = path.join(path.dirname(file), path.basename(file, ".ts"))
+ "." + type + ".wat";
// should not block testing
fs.writeFile(watPath, wat, (err) => {
if (err) console.warn(err);
});
const context = new TestContext({
fileName: file,
reporter,
stderr: process.stderr,
stdout: process.stdout,
});
const imports = context.createImports({
wasi_unstable: wasi.exports,
});
const wasm = instantiateBuffer(binary, imports);
wasi.setMemory(wasm.memory);
wasi.view = new DataView(wasm.memory.buffer);
context.run(wasm);
if (!context.pass) pass = false;
}
process.exit(pass ? 0 : 1);