-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathRunnerWorker.js
More file actions
184 lines (148 loc) · 4.23 KB
/
RunnerWorker.js
File metadata and controls
184 lines (148 loc) · 4.23 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
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
function serializeError(error) {
if (!error) {
return "Unknown error";
}
if (typeof error === "string") {
return error;
}
var parts = [];
if (error.name) {
parts.push(error.name);
}
if (error.message) {
parts.push(error.message);
}
var summary = parts.join(": ");
if (error.stack) {
summary += "\n" + error.stack;
}
return summary || String(error);
}
function readTextFile(path) {
var data = NSData.dataWithContentsOfFile(path);
if (!data) {
throw new Error("Unable to read file: " + path);
}
var text = NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding);
if (!text) {
throw new Error("Unable to decode UTF-8 file: " + path);
}
return text.toString();
}
function withSourceURL(source, path) {
return source + "\n//# sourceURL=" + path.replace(/\\/g, "/");
}
function evalGlobal(source, path) {
var globalEval = (0, eval);
return globalEval(withSourceURL(source, path));
}
function compileScript(source, path) {
return Function(withSourceURL(source, path));
}
function expectedErrorMatches(error, negative) {
if (!negative || !negative.type) {
return true;
}
return error && (error.name === negative.type || (error.constructor && error.constructor.name === negative.type));
}
function makePrelude() {
globalThis.$262 = {
global: globalThis
};
globalThis.print = function () {
};
globalThis.reportCompare = function (expected, actual, message) {
if (expected !== actual) {
throw new Error(message || ("Expected " + expected + " but received " + actual));
}
};
}
function loadHarness(test262Root, includes) {
includes.forEach(function (includeName) {
var harnessPath = test262Root + "/harness/" + includeName;
evalGlobal(readTextFile(harnessPath), harnessPath);
});
}
function postPass() {
postMessage({ status: "passed" });
}
function postFailure(error) {
postMessage({
status: "failed",
error: serializeError(error)
});
}
function runCase(payload) {
var settled = false;
var test262Root = NSBundle.mainBundle.bundlePath + "/app/tests/" + payload.bundleSubmodulePath;
var testPath = test262Root + "/test/" + payload.relativePath;
var source = readTextFile(testPath);
var wrappedSource = payload.mode === "strict" ? "\"use strict\";\n" + source : source;
function completeWithPass() {
if (settled) {
return;
}
settled = true;
postPass();
}
function completeWithFailure(error) {
if (settled) {
return;
}
settled = true;
postFailure(error);
}
makePrelude();
if (payload.async) {
globalThis.$DONE = function (error) {
if (error === undefined || error === null) {
completeWithPass();
return;
}
completeWithFailure(error);
};
}
loadHarness(test262Root, payload.includes || []);
if (payload.negative && payload.negative.phase === "parse") {
try {
compileScript(wrappedSource, testPath);
} catch (error) {
if (expectedErrorMatches(error, payload.negative)) {
completeWithPass();
return;
}
completeWithFailure(error);
return;
}
completeWithFailure(new Error("Expected parse failure but compilation succeeded"));
return;
}
try {
evalGlobal(wrappedSource, testPath);
} catch (error) {
if (payload.negative) {
if (expectedErrorMatches(error, payload.negative)) {
completeWithPass();
return;
}
completeWithFailure(error);
return;
}
completeWithFailure(error);
return;
}
if (payload.negative) {
completeWithFailure(new Error("Expected runtime failure but test completed successfully"));
return;
}
if (!payload.async) {
completeWithPass();
}
}
onmessage = function (message) {
try {
runCase(message.data);
} catch (error) {
postFailure(error);
}
};