-
Notifications
You must be signed in to change notification settings - Fork 0
/
beebasm-worker.js
145 lines (125 loc) · 3.93 KB
/
beebasm-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
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
var is_initialized = false;
var pending = [];
var stdout = [];
var stderr = [];
var Module = {
locateFile: function(s) {
return 'beebasm/' + s;
},
onRuntimeInitialized: function() {
is_initialized = true;
for (var pendingEvent in pending) {
onmessage(pendingEvent);
}
},
print: function (line) {
stdout.push(line);
},
printErr:function (line) {
stderr.push(line);
}
};
importScripts('./beebasm/beebasm.js');
var compileFn = Module.cwrap('beebide_compile', 'number', ['string', 'string', 'string']);
var dbgGetAddrFn = Module.cwrap('beebide_dbgGetAddr', 'number', ['string', 'number', 'number']);
var dbgGetFileFn = Module.cwrap('beebide_dbgGetFile', 'string', ['number']);
var dbgGetLineFn = Module.cwrap('beebide_dbgGetLine', 'number', ['number']);
var dbgGetColFn = Module.cwrap('beebide_dbgGetCol', 'number', ['number']);
function hexToBytes(hexString) {
var result = new Uint8Array(hexString.length/2);
for (var i = 0; i < hexString.length; i += 2) {
result[i/2] = parseInt(hexString.substr(i, 2), 16);
}
return result;
}
function registerFS(module, files, dir) {
//dir += "/";
for (var fileName in files) {
var fullPath = dir.length ? (dir + '/' + fileName) : fileName;
//var fullPath = dir + "/" + fileName;
//if (Module.FS.stat(fullPath)) {
// Module.FS_unlink(fullPath);
//}
var val = files[fileName];
if (Object.prototype.toString.call(val) === '[object String]') {
var isBinary = fileName.endsWith(".bmp") || fileName.endsWith(".bin");
var rawVal = isBinary ? hexToBytes(val) : val;
//Module.FS_createDataFile(dir, fileName, rawVal, true, false, true);
Module.FS.writeFile(fullPath, rawVal);
}
else {
Module.FS_createPath(dir, fileName);
registerFS(module, val, fullPath);
}
}
}
onmessage = function (event) {
if (!is_initialized) {
pending.push(event);
return;
}
stdout = [];
stderr = [];
var status = -1;
event = event.data;
// Resolve breakpoint
if (event.action === 'bp') {
var bp = event.bp;
if (bp.fileId) {
bp.addr = dbgGetAddrFn(bp.fileId, bp.lineNum, bp.col);
} else {
bp.fileId = dbgGetFileFn(bp.addr);
bp.lineNum = dbgGetLineFn(bp.addr);
bp.col = dbgGetColFn(bp.addr);
}
postMessage({
action: 'bp',
bp: bp
});
return;
}
// Find source location for address
if (event.action === 'dbgsym') {
var addr = event.addr;
postMessage({
file: dbgGetFileFn(addr),
line: dbgGetLineFn(addr),
col: dbgGetColFn(addr),
});
return;
}
//try {
registerFS(Module, event.project.files, "");
var before = Date.now();
status = compileFn(event.project.mainFilename, event.project.bootFilename, 'output.ssd');
var after = Date.now();
var result = null;
if (Module.FS.stat(event.output)) {
result = Module.FS.readFile(event.output);
}
// If build succeeded, resolve any source breakpoints
if (0 === status) {
for (var bp of event.breakpoints) {
if (bp.addr < 0) {
bp.addr = dbgGetAddrFn(bp.fileId, bp.lineNum, bp.col);
}
}
}
postMessage({
id: event.id,
stdout: stdout,
stderr: stderr,
result: result,
status: status,
breakpoints: event.breakpoints,
timeTaken: after - before
});
/*} catch (e) {
console.log(e);
console.log(stderr);
postMessage({
id: event.id,
exception: e.toString()
});
}*/
};