This repository has been archived by the owner on Jun 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
sandbox.js
315 lines (294 loc) · 8.8 KB
/
sandbox.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
(function (global) {
// Window is self in worker. Self is window in iframe.
// Try for Firefox, which doesn't have a setter for these properties
try {
global.window = global.window || global;
} catch (e) { }
try {
global.self = global.self || global;
} catch (e) { }
var Sandboss;
// Messaging.
var msg_handler = function (e) {
var message = JSON.parse(e.data),
current = Sandboss,
parts = message['type'].split('.');
// Route message.
for (var i = 0; i < parts.length; i++) {
current = current[parts[i]];
}
current(message.data)
};
global.addEventListener('message', msg_handler, false);
// Dummy console for some scripts would think there is one.
(function () {
var noop = function () {};
var methods = ['debug', 'error', 'info', 'log',
'warn', 'dir', 'dirxml', 'trace',
'assert', 'count', 'markTimeline',
'profile', 'profileEnd', 'time',
'timeEnd', 'timeStamp', 'group',
'groupCollapsed', 'groupEnd'];
if (typeof console === 'undefined') {
global.console = {};
}
for (var i = 0; i < methods.length; i++) {
if (typeof global.console[methods[i]] !== 'function') {
try{
global.console[methods[i]] = noop;
} catch (e) {}
}
}
})();
// Sandbox controller.
Sandboss = {
outTimeout: 0,
output_buffer: [],
OUT_EVERY_MS: 50,
syncTimeout: Infinity,
isFrame : typeof document !== 'undefined',
// Responsible for posting messages.
post: function (msg) {
var msgStr = JSON.stringify(msg);
if (this.isFrame) {
// Window communication require additional origin argument.
window.parent.postMessage(msgStr, '*');
} else {
self.postMessage(msgStr);
}
},
// Import an array of scripts.
importScripts: function (scriptsArr) {
var reqs = [],
totalSize = 0,
lastLoadedTable = [],
totalUpdated = [],
totalLoaded = 0,
that = this,
XHR = XMLHttpRequest || ActiveXObject('Microsoft.XMLHTTP');
var updateSize = function (req) {
if (totalUpdated.indexOf(req) === -1){
totalUpdated.push(req);
totalSize += parseInt(req.getResponseHeader('X-Raw-Length'), 10);
}
};
var updateProgressCreator = function (index) {
return function (e) {
var loaded = e.loaded || e.position,
lastLoaded = lastLoadedTable[index] || 0;
lastLoadedTable[index] = loaded;
totalLoaded += loaded - lastLoaded;
var percentageDone = (totalLoaded / totalSize) * 100;
if (totalUpdated.length === scriptsArr.length) {
that.progress(percentageDone);
}
};
};
var finished = scriptsArr.length;
var finish = function (e) {
var i;
if (finished === 0) {
for (i = 0; i < reqs.length; i++) {
(self.execScript || function(data) {
self['eval'].call(self, data);
})(reqs[i].responseText);
}
that.engine = new self.JSREPLEngine(that.input, that.out, that.result, that.err, self, that.ready);
that.bindAll(Sandboss.engine);
that.hide('JSREPLEngine');
}
};
for (var i = 0; i < scriptsArr.length; i++){
(function (i) {
reqs[i] = new XHR();
if (reqs[i].addEventListener) {
reqs[i].addEventListener('progress', updateProgressCreator(i), false);
}
reqs[i].onprogress = updateProgressCreator(i);
reqs[i].onreadystatechange = function () {
if (reqs[i].readyState === 2) {
updateSize(reqs[i]);
} else if (reqs[i].readyState === 4) {
finished--;
finish();
}
};
reqs[i].open('GET', scriptsArr[i], true);
reqs[i].send(null);
})(i);
}
},
// Outbound output.
out: function (text) {
var that = this;
this.output_buffer.push(text);
if (this.outTimeout === 0) {
this.outTimeout = setTimeout(this.flush, this.OUT_EVERY_MS);
this.syncTimeout = Date.now();
} else if (Date.now() - this.syncTimeout > this.OUT_EVERY_MS) {
clearTimeout(this.outTimeout);
this.flush();
}
},
flush: function () {
if (!this.output_buffer.length) return;
var message = {
type: 'output',
data: this.output_buffer.join('')
};
this.post(message);
this.outTimeout = 0;
this.output_buffer = [];
},
// Outbound errors.
err: function (e) {
var message = {
type: 'error',
data: e.toString()
};
this.flush();
this.post(message);
},
// Outbound input.
input: function (callback) {
// Incoming input would call "Sandboss.input.write", hence its our continuation callback.
this.input.write = callback;
var message = {
type: 'input'
};
this.flush();
this.post(message);
},
result: function (data) {
var message = {
type: 'result',
data: data
};
this.flush();
this.post(message);
},
// Outbound language ready function.
ready: function (data) {
var message = {
type: 'ready'
};
this.post(message);
},
// Inbound/Outbound getNextLineIndent.
// Gets the nextline indent and sends it in an 'indent' message.
getNextLineIndent: function (data) {
// Get line indent
var indent = this.engine.GetNextLineIndent(data);
var message = {
type: 'indent',
data: indent
};
this.post(message);
},
progress: function (data) {
var message = {
type: 'progress',
data: data
};
this.post(message);
},
dbInput: function () {
var message = {
type: 'db_input'
};
this.flush();
this.post(message);
},
serverInput: function () {
var message = {
type: 'server_input'
};
this.flush();
this.post(message);
},
// Bind all methods to its owner object.
bindAll: function (obj) {
for (var method in obj) {
(function (method) {
var fn = obj[method];
if (typeof fn == "function") {
obj[method] = function () {
var args = [].slice.call(arguments);
return fn.apply(obj, args);
};
}
})(method);
}
},
// Try to hide and secure stuff.
hide: function (prop) {
try {
Object.defineProperty(global, prop, {
writable: false,
enumerable: false,
configurable: false,
value: global[prop]
});
} catch (e) {}
},
set_input_server: function (settings) {
var baseUrl = settings.url || '/emscripten/input/';
function nextUrl() {
// Note: we increment the input_id after each request to avoid race
// conditions on the server. Keep this code in sync with repl.coffee
return baseUrl + settings.input_id++;
}
this.input_server = {
nextUrl: nextUrl,
cors: settings.cors || false
};
}
};
// Bind all the sand minions to the SANDBOSS!! MWAHAHAHA
Sandboss.bindAll(Sandboss);
global.Sandboss = Sandboss;
Sandboss.hide('Sandboss');
var createRequest = function (method, url, isCors){
var xhr = new XMLHttpRequest();
if (isCors) {
if ("withCredentials" in xhr) {
xhr.open(method, url, false);
} else if (typeof XDomainRequest != "undefined"){
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
throw new Error('Your browser doesn\' support CORS');
}
} else {
xhr.open(method, url, false);
}
return xhr;
}
// Synchronous input for emscripted languages.
if (self.openDatabaseSync) {
var DB = self.openDatabaseSync('replit_input', '1.0', 'Emscripted input', 1024);
self.prompt = function () {
Sandboss.dbInput();
var t = null;
DB.transaction(function (tx) {t=tx});
var i, j, res;
while (!(res = t.executeSql('SELECT * FROM input').rows).length) {
for (i = 0; i < 100000000; i++);
}
t.executeSql('DELETE FROM input');
return res.item(0).text;
}
Sandboss.hide('prompt');
} else if (!Sandboss.isFrame) {
self.prompt = function () {
Sandboss.serverInput();
var req = createRequest('GET', Sandboss.input_server.nextUrl(), Sandboss.input_server.cors);
req.send(null);
if (req.status === 200) {
return req.responseText;
} else {
return 'ERROR: ON NON-WEBKIT BROWSERS CONNECTION TO THE SERVER IS NEEDED FOR INPUT';
}
};
}
})(this);