Skip to content

Commit 618090f

Browse files
committed
Fix bug in getFile() with empty files and add /dev/random
1 parent 1ab6f30 commit 618090f

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

pre.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var console = {log: function() {}};
12
var is_browser = (typeof(self) !== "undefined" || typeof(window) !== "undefined");
23

34
var FS;
@@ -46,17 +47,29 @@ var mkdir = function(id, pseudo_path, pseudo_name) {
4647

4748

4849
var getFile = function(id, pseudo_path, pseudo_name) {
49-
var array = FS.root.contents[pseudo_name].contents;
50-
var binary_data = new Uint8Array(array);
50+
try {
51+
var parentObj = FS.root;
52+
var path = pseudo_path.split('/');
53+
for(var i = 1; i < path.length; i++) {
54+
if(path[i] != '')
55+
parentObj = parentObj.contents[path[i]];
56+
}
57+
58+
var array = parentObj.contents[pseudo_name].contents;
59+
var binary_data = new Uint8Array(array);
5160

52-
var chunk_size = 1000;
53-
var chunk_count = Math.ceil(binary_data.length/chunk_size);
61+
var chunk_size = 1000;
62+
var chunk_count = Math.max(1, Math.ceil(binary_data.length/chunk_size));
5463

55-
for(var i = 0; i < chunk_count; i++) {
56-
var chunk = binary_data.subarray(i*chunk_size, Math.min((i+1)*chunk_size, binary_data.length));
64+
for(var i = 0; i < chunk_count; i++) {
65+
var chunk = binary_data.subarray(i*chunk_size, Math.min((i+1)*chunk_size, binary_data.length));
5766

58-
var str_chunk = String['fromCharCode'].apply(null, chunk);
59-
self.postMessage(JSON.stringify({'id': id, 'chunk_id': i, 'chunk_count': chunk_count, 'contents': str_chunk}));
67+
var str_chunk = String['fromCharCode'].apply(null, chunk);
68+
self.postMessage(JSON.stringify({'id': id, 'chunk_id': i, 'chunk_count': chunk_count, 'contents': str_chunk}));
69+
}
70+
}
71+
catch(e) {
72+
self.postMessage(JSON.stringify({'id': id, 'chunk_id': 0, 'chunk_count': 0, 'error': true}));
6073
}
6174
};
6275

@@ -78,6 +91,16 @@ self['onmessage'] = function(ev) {
7891

7992
if(msg['cmd'] === 'run') {
8093
try {
94+
shouldRunNow = true;
95+
96+
if('egd-pool' in FS.root.contents['dev'].contents) {
97+
var rand_count = 0;
98+
var rand_contents = FS.root.contents['dev'].contents['egd-pool'].contents;
99+
var rand = new Uint8Array(rand_contents);
100+
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]; });
101+
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]; });
102+
}
103+
81104
Module['run'](msg['args']);
82105
}
83106
catch(e) {

0 commit comments

Comments
 (0)