-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
243 lines (172 loc) · 6.13 KB
/
utils.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
242
243
// function for pretty printing values
function display(obj) {
try {
// custom string conversion of object
return recursiveDisplay(obj);
} catch(e) {
if (
e.name === "RangeError" &&
e.message === "Maximum call stack size exceeded"
) {
// native string conversion if object has circular references
return obj.toString();
} else {
// re-throw error if not due to circular references
throw e;
}
}
}
// helper function for pretty printing values
function recursiveDisplay(obj) {
if (obj === undefined) {
// represent undefined as undefined
return "undefined";
} else if (obj === null) {
// represent null as null
return "null";
} else if (Array.isArray(obj)) {
// recursively convert array element to string
return `[${obj.map(element => display(element)).join(", ")}]`;
} else if (typeof obj === "object") {
let keys = [];
// capture all object keys in an array
if (
obj.hasOwnProperty !== undefined &&
typeof obj.hasOwnProperty === "function"
) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
keys.push(key);
}
}
} else {
for (let key in obj) {
keys.push(key);
}
}
// recursively convert object key/value pairs to string
// NOTE: keys are sorted lexicographically
return `{${keys
.sort()
.map(element => `${display(element)}: ${display(obj[element])}`)
.join(", ")
}}`;
} else if (typeof obj === "string") {
let s = JSON.stringify(obj);
// simplify string representation: if string contains only double quotes
// and not single quote, then use single quotes as delimiter such that
// no escaping is neeeded
if (s.includes("\\\"") && !s.includes("'")) {
s = `'${s.slice(1, -1).replace(/\\"/g, "\"")}'`;
}
return s;
} else {
// native string conversion if not one of the above types
return obj.toString();
}
}
// helper function for converting Error objects to string
function displayError(e, cleanup) {
// cleanup error message by default
if (cleanup === undefined) {
cleanup = true;
}
try {
if (typeof e === "string") {
// error message was already converted to string representation
return e;
} else if (e.stack !== undefined) {
// initialize array to capture lines of the stack trace
let message = [];
// filter lines of the stack trace
for (let line of e.stack.split("\n")) {
if (cleanup && line.trim() === "") {
// remove part above stack trace that describes where the
// error occurs in the code (not necessarily user code)
message = [];
}
else if (
// include all lines if no cleanup is needed
!cleanup ||
// always include non at-lines
// - indicate errors themselves
// - indicate where error occurs
!line.startsWith(" at ") ||
// always include lines that report errors in submitted code
line.includes("<code>:") ||
// always include lines that report errors in tests
line.includes("<test>:")
) {
if (line.length > 0 && line[0] !== ' ') {
while (line.includes('[')) {
let start = line.indexOf('[');
while (start > 0 && line[start - 1] === ' ') {
start -= 1;
}
let stop = line.indexOf(']', start);
if (stop === -1) {
stop = line.length - 1;
}
line = line.slice(0, start) + line.slice(stop + 1);
}
}
message.push(line);
}
}
// reconstruct stack trace based on filtered lines
return message.join("\n");
} else {
let message;
// format message
if (e.name !== undefined && e.message !== undefined) {
message = e.name;
while (message.includes('[')) {
let start = message.indexOf('[');
while (start > 0 && message[start - 1] === ' ') {
start -= 1;
}
let stop = message.indexOf(']');
message = message.slice(0, start) + message.slice(stop + 1);
}
// add line number if available
if (e.lineNumber !== undefined) {
message += ` (line ${e.lineNumber})`;
}
message += `: ${ e.message}`;
} else {
message = "JudgeError: ill-formed Error";
if (display(e) !== "") {
message += `: ${display(e)}`;
}
}
return message;
}
} catch (e) {
// for converting Error objects to string
return e.toString();
}
}
function lineError(e) {
let last = "";
if (typeof e !== "string") {
e = displayError(e);
}
for (let line of e.split("\n")) {
if (!line.startsWith(" ")) {
last = line;
}
}
return last;
}
function statusError(e) {
if (lineError(e) === "Error: Script execution timed out.") {
return "time limit exceeded";
}
return "runtime error";
}
module.exports = {
display: display,
displayError: displayError,
lineError: lineError,
statusError: statusError,
};