-
Notifications
You must be signed in to change notification settings - Fork 4
/
driver.js
executable file
·346 lines (302 loc) · 9.1 KB
/
driver.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* Global variables */
// HTML console output
var output = null;
// HTML iframe for tests
var testFrame = null;;
// Timer variable
var timer = null;
// Only required for browsers like Opera
var interval = null;
// Debug variable
var debug = false;
// buffer to keep incomplete lines received from server
var buffer = "";
// queue for our tests
var testQueue = new Array();
// Ready variable
var ready = false;
// If log() should also dump()
var logDump = true;
var logBuffer = new Array();
var isOpera = typeof window.opera !== 'undefined';
var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
/* End of global variables */
if (typeof(dump) == "undefined") {
dump = function(msg) { log(msg); }
// Avoid recursion
logDump = false;
}
function init() {
// Grab console reference
output = document.getElementById("output");
testFrame = document.getElementById("testFrame");
// Add onload event for our iframe
document.getElementById("testFrame").onload = loadedChild;
document.getElementById("testFrame").onerror = loadedChild;
var params = mapParametersSequence(location.search);
ws = new WebSocket("ws://" + params.host + ":" + params.port + "/");
ws.onopen = function(evt) { onOpen(evt) };
ws.onclose = function(evt) { onClose(evt) };
ws.onmessage = function(evt) { onMessage(evt) };
ws.onerror = function(evt) { onError(evt) };
}
function mapParametersSequence(seq)
{
var dict = {};
seq = seq.substring(1).split("&");
for (var i=0; i<seq.length; i++)
{
var pair = seq[i].split('=');
var key = decodeURIComponent(pair[0]);
var value = decodeURIComponent(pair[1]);
dict[key] = value;
}
return dict;
}
function onOpen(evt) {
// Call completion once to send ready event
//completedChild();
//
if (isChrome) {
var body = document.getElementsByTagName('body')[0];
var img = document.createElement('img');
img.width=300;
img.height=300;
img.alt="Here be images";
img.id="img";
body.appendChild(img);
//body.removeChild(testFrame);
//testFrame = this;
testFrameDoc = document;
appendChildScript(createPrologCodeOnly());
} else {
// Initialize to some empty document
testFrame.src = 'data:text/html;charset=utf-8,'
+ escape('<html><head>')
+ createPrologData()
+ escape('</head><body></body></html>');
testFrameDoc = testFrame.contentDocument;
}
}
function onClose(evt) {
}
function onError(evt) {
}
// This function assembles arbitrary data received from the
// server into lines. If the line is a JSON-encoded message,
// that message is passed to processJSONLine() for further
// handling.
function onMessage(evt) {
var data = evt.data;
// TODO: FIXME: Temporary hack to circumvent problems
// with toString and UTF-8 conversion. This will break
// binary data transfer. Ultimately, we must encode all
// content data in base64 before transfer.
//data = data.toString()
/*var dataStr = "";
for (var i = 0; i < data.length; ++i) {
dataStr += String.fromCharCode(data.get(i));
}
data = dataStr;*/
var keepLastChunk = true;
// Data ends exactly with newline, no remainder to keep in buffer
if (data.substr(-1) == "\n") {
keepLastChunk = false;
}
var chunks = data.split("\n");
var lastChunk;
// If the last chunk is incomplete, don't process it
if (keepLastChunk) {
lastChunk = chunks.pop();
}
for (i in chunks) {
chunk = chunks[i];
// First chunk, prepend buffer and reset it
if (i == 0) {
chunk = buffer + chunk;
buffer = "";
}
// Process only JSON encoded messages, ignore everything else
if (chunk.substr(0,1) == '{') {
processJSONLine(chunk);
}
}
// If we have an incomplete chunk, keep it in our buffer
if (keepLastChunk) {
buffer += lastChunk;
}
}
// when we receive a JSON-encoded testcase from the server, write it to a local
// temp file and notify our test-loading worker (via a custom event)
// data is of the form:
// {"type": "html", "content": "<html><body>...</body></html>"} or
// {"type": "js", "content": "var f = function() { return 1 }..."}
function processJSONLine(data) {
var resp = JSON.parse(data);
switch (resp.type) {
case "msg":
var msg = resp.content;
switch(msg) {
case "reset":
/* We don't implement reset. Instead we let the command timeout and will be shutdown */
ready = false;
break;
case "evaluate":
// If we have any tests, we start processing the first test now.
// The completion of this test will automatically trigger the processing
// of the next test in queue, until the queue is entirely empty.
// If we don't have any tests at this point (which does not make sense
// but is perfectly valid), we directly respond to the server again.
if (testQueue.length > 0) {
processTest(testQueue.shift());
} else {
ws.send('{"msg": "Evaluation complete"}\n');
}
break;
case "ping":
ws.send('{"msg": "pong"}\n');
break;
}
break;
case "html":
case "xhtml":
case "js":
case "svg":
case "jpg":
case "template":
// One of our supported test types.
// Store it in our queue for processing later when the
// server sends us an evaluate command.
testQueue.push(resp);
break;
default:
// Malformed data or incomplete JSON object? Warn!
break;
}
}
// Process a single test, provided as JSON object
function processTest(testObj) {
switch (testObj.type) {
// HTML file to load into the test frame
case "html":
case "xhtml":
loadChild(testObj.content);
break;
// JS file to append to the body of the test frame content
case "js":
appendChildScript(testObj.content);
break;
case "svg":
addChildImage(testObj.content);
break;
case "jpg":
addChildImageJPEG(testObj.content);
break;
case "template":
loadTemplate(testObj.content);
break;
}
}
// Load received data as data URL into iframe
function loadChild(data) {
testFrame.src = 'data:text/html;charset=utf-8,' + createPrologData() + escape(data);
timer = setTimeout("abortedChild()", 5000);
}
function loadTemplate(data) {
testFrame.src = 'data:text/html;charset=utf-8,' + data;
timer = setTimeout("loadedChild()", 5000);
}
function createPrologData() {
var prologData = '<script src="data:text/javascript;charset=utf-8,';
//prologData += "netscape.security.PrivilegeManager.enablePrivilege = function() {};\n";
prologData += escape(escape(createPrologCodeOnly()));
prologData += '"></script>';
return prologData;
}
function createPrologCodeOnly() {
var prolog = {
gc: function gc() { },
alert: function alert(msg) { }
};
var prologCode = "";
for (var f in prolog) {
prologCode += prolog[f].toString();
}
return prologCode;
}
function appendChildScript(data) {
timer = setTimeout("abortedChild()", 5000);
var frameBody = testFrameDoc.getElementsByTagName('body')[0];
var frameScript = testFrameDoc.createElement('script');
frameScript.type = 'text/javascript';
frameScript.src = 'data:text/javascript;charset=utf-8,' + escape(data);
if (!isOpera) {
frameScript.onload = loadedChild;
frameBody.appendChild(frameScript);
} else {
frameBody.appendChild(frameScript);
// Opera doesn't support onload for script tags added, use readyState
interval = setInterval(function() {
if (/loaded|complete/.test(testFrameDoc.readyState)) {
clearInterval(interval);
loadedChild();
}
}, 10);
}
}
function addChildImage(data) {
timer = setTimeout("abortedChild()", 5000);
var frameImgTag = testFrame.contentDocument.getElementById('img');
frameImgTag.onload = loadedChild;
frameImgTag.onerror = loadedChild;
frameImgTag.src = "data:image/svg+xml," + escape(data);
}
function addChildImageJPEG(data) {
timer = setTimeout("abortedChild()", 5000);
var frameImgTag = testFrame.contentDocument.getElementById('img');
frameImgTag.onload = loadedChild;
frameImgTag.onerror = loadedChild;
frameImgTag.src = "data:image/jpeg;base64," + data;
}
function completedChild() {
if (testQueue.length > 0) {
// Process the next test. This call will cause this
// function to be called again once that test is complete.
processTest(testQueue.shift());
} else {
if (!ready) {
// request the first testcase
//ws.send('{"msg": "Client ready"}\n');
ws.send('{"msg": "Evaluation complete"}\n');
ready = true;
} else {
ws.send('{"msg": "Evaluation complete"}\n');
}
}
}
// Called on load of the child
function loadedChild() {
clearTimeout(timer);
completedChild();
}
// Called on timeout while waiting for child loading
function abortedChild() {
completedChild();
}
function log(message) {
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);
logBuffer.push(pre);
if (logBuffer.length > 100) {
var oldPre = logBuffer.shift();
output.removeChild(oldPre);
}
if (logDump) {
dump(message + "\n");
}
}
/* End of logging functions */
// Add event listener for starting up
window.addEventListener("load", init, false);