-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnode.js
More file actions
160 lines (136 loc) · 4.38 KB
/
node.js
File metadata and controls
160 lines (136 loc) · 4.38 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
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
const { TestContext, VerboseReporter } = require("@as-pect/core");
const glob = require("glob");
const { instantiateSync } = require("assemblyscript/lib/loader");
const { main } = require("assemblyscript/cli/asc");
const { parse } = require("assemblyscript/cli/util/options");
const path = require("path");
const fs = require("fs").promises;
const { WASI } = require("wasi");
const promises = [];
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();
reporter.stderr = process.stderr;
reporter.stdout = process.stdout;
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",
"--measure",
"--lib", "assembly",
"--transform", require.resolve("@as-pect/core/lib/transform/index.js"),
];
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.concat(["--debug"]), 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(fileName, type, binary, wat) {
const dirname = path.dirname(fileName);
const basename = path.basename(fileName, ".ts");
const fullName = path.join(dirname, basename)
const watPath = `${fullName}.${type}.wat`;
const fileNamePath = `${fullName}.${type}.ts`;
// should not block testing
promises.push(fs.writeFile(watPath, wat));
const context = new TestContext({
fileName: fileNamePath, // set the fileName
reporter, // use verbose reporter
binary, // pass the binary to get function names
});
const wasi = new WASI({
args: [],
env: {},
preopens: {
// '/sandbox': '/some/real/path/that/wasm/can/access'
}
});
const imports = context.createImports({
wasi_snapshot_preview1: wasi.wasiImport,
});
const instance = instantiateSync(binary, imports);
// TODO: wasi.start(instance);
process.stdout.write("\n");
context.run(instance);
if (!context.pass) pass = false;
}
// await for all the file writes to occur for the wat files
Promise.all(promises)
.then(() => {
// if the file writes were successful, inspect errors and pass
process.exit(pass && errors.length === 0 ? 0 : 1);
})
.catch((error) => {
// report the file write error and exit 1
console.error(error);
process.exit(1);
});