-
Notifications
You must be signed in to change notification settings - Fork 141
/
pre.js
115 lines (99 loc) · 3.67 KB
/
pre.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
var Module = {};
var is_browser = (typeof(self) !== "undefined" || typeof(window) !== "undefined");
if(is_browser) {
Module['print'] = function(a) { self['postMessage'](JSON.stringify({'command': 'stdout', 'contents': a})); }
Module['printErr'] = function(a) { self['postMessage'](JSON.stringify({'command': 'stderr', 'contents': a})); }
}
Module['preInit'] = function() {
Module['FS_root'] = function() {
return FS.root.contents;
}
};
var FS_createLazyFilesFromList = function(msg_id, parent, list, parent_url, canRead, canWrite) {
var xhr = new XMLHttpRequest();
xhr.open('GET', list, false);
xhr.responseType = 'text';
xhr.onload = function() {
var lines = this.response.split("\n");
var path, pos, filename;
for(var i in lines) {
pos = lines[i].lastIndexOf("/");
filename = lines[i].slice(pos+1);
path = lines[i].slice(0, pos);
if(filename === '.')
Module['FS_createPath']('/', parent+path, canRead, canWrite);
else
if(filename.length > 0)
Module['FS_createLazyFile'](parent+path, filename, parent_url+path+'/'+filename, canRead, canWrite);
}
self['postMessage'](JSON.stringify({
'command': 'result',
'result': 0,
'msg_id': msg_id,
}));
};
xhr.send();
};
var preparePRNG = function (argument) {
if('egd-pool' in FS.root.contents['dev'].contents) {
var rand_count = 0;
var rand_contents = FS.root.contents['dev'].contents['egd-pool'].contents;
var rand = new Uint8Array(rand_contents);
FS.createDevice('/dev', 'urandom', function() { rand_count++; if(rand_count >= rand.length) { Module.print("Out of entropy!"); throw Error("Out of entropy"); } return rand[rand_count-1]; });
FS.createDevice('/dev', 'random', function() { rand_count++; if(rand_count >= rand.length) { Module.print("Out of entropy!"); throw Error("Out of entropy"); } return rand[rand_count-1]; });
}
}
self['onmessage'] = function(ev) {
var data = JSON.parse(ev['data']);
var args = data['arguments'];
args = [].concat(args);
var res = undefined;
var fn;
var cmd = data['command'];
switch(cmd) {
case 'run':
shouldRunNow = true;
preparePRNG();
try {
res = Module['run'](args);
}
catch(e) {
self['postMessage'](JSON.stringify({'msg_id': data['msg_id'], 'command': 'error', 'message': e.toString()}));
return;
}
self['postMessage'](JSON.stringify({'msg_id': data['msg_id'], 'command': 'success', 'result': res}));
res = undefined;
break;
case 'FS_createLazyFilesFromList':
args.unshift(data['msg_id']);
res = FS_createLazyFilesFromList.apply(this, args);
break;
case 'FS_createDataFile': FS.createDataFile.apply(FS,args);res=true;break;
case 'FS_createLazyFile': FS.createLazyFile.apply(FS,args);res=true;break;
case 'FS_createFolder': FS.createFolder.apply(FS,args);res=true;break;
case 'FS_createPath': FS.createPath.apply(FS,args);res=true;break;
case 'FS_unlink': FS.unlink.apply(FS,args);res=true;break;
case 'FS_readFile':
var tmp=FS.readFile.apply(FS,args);
var res='';
var chunk = 8*1024;
var i;
for (i = 0; i < tmp.length/chunk; i++) {
res += String.fromCharCode.apply(null, tmp.subarray(i*chunk, (i+1)*chunk));
}
res += String.fromCharCode.apply(null, tmp.subarray(i*chunk));
break;
case 'set_TOTAL_MEMORY':
Module.TOTAL_MEMORY = args[0];
res = Module.TOTAL_MEMORY;
break;
case 'test':
break;
}
if(typeof(res) !== 'undefined')
self['postMessage'](JSON.stringify({
'command': 'result',
'result': res,
'msg_id': data['msg_id'],
}));
};