-
Notifications
You must be signed in to change notification settings - Fork 5
/
worker.js
111 lines (99 loc) · 3.23 KB
/
worker.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
"use strict";
function JslinuxWorker(worker_child, is_pseudo) {
var cached_clipboard_text = '';
var clipboard_get = function () {
return cached_clipboard_text;
};
var clibboard_set = function (data) {
worker_child.postMessage({
what: 'clipboard_output',
str: data
});
//should go back again, replace anyway...
cached_clipboard_text = data;
};
var pc;
var load_jslinux_scripts = function (prefix, callback) {
var jslinux_scripts = [
prefix + "/utils.js",
prefix + "/term.js",
prefix + "/cpux86-ta.js",
prefix + "/clipboard.js",
prefix + "/cmos.js",
prefix + "/ide.js",
prefix + "/keyboard.js",
prefix + "/pic.js",
prefix + "/pit.js",
prefix + "/serial.js",
prefix + "/block_reader.js",
prefix + "/block_reader_writer.js",
prefix + "/pcemulator.js",
prefix + "/jslinux.js"
];
if (is_pseudo) {
loadScripts(jslinux_scripts, callback);
} else {
importScripts.apply(self, jslinux_scripts);
callback();
}
};
var post_log_message = function () {
worker_child.postMessage({
what: 'log',
// convert arguments to generic array, to pass over postMessage in worker...
args: Array.prototype.slice.call(arguments, 0)
});
};
var post_com1_message = function (string) {
worker_child.postMessage({
what: 'com1_output',
str: string
});
};
var post_com2_message = function (string) {
worker_child.postMessage({
what: 'com2_output',
str: string
});
};
var render_screenshot = function(data) {
worker_child.postMessage({
what: 'screenshot',
data: data
});
};
worker_child.onmessage = function (evt) {
var data = evt.data;
switch (data.what) {
case 'start':
load_jslinux_scripts(data.prefix, function () {
pc = self.jslinux(clipboard_get, clibboard_set, data.name, data.prefix + '/bin', data.cmdline);
pc.log = post_log_message.bind(worker_child);
pc.com1.write_func = post_com1_message.bind(worker_child);
pc.com2.write_func = post_com2_message.bind(worker_child);
pc.render_screenshot = render_screenshot.bind(worker_child);
//post_log_message(document.getElementsByTagName('script'));
});
break;
case 'com1_input':
if (pc)
pc.com1.send_chars(data.str);
break;
case 'com2_input':
if (pc)
pc.com2.send_chars(data.str);
break;
case 'clipboard_input':
cached_clipboard_text = data.str;
break;
}
};
worker_child.postMessage({
what: 'worker_loaded'
});
}
if (typeof window == 'undefined') {
JslinuxWorker(self, false);
} else {
window.JslinuxWorker = JslinuxWorker;
}