2017-01-12 02:02:23 +00:00
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
var fs = require('fs');
|
|
|
|
var vm = require('vm');
|
|
|
|
var worker = {};
|
|
|
|
|
|
|
|
var includeInThisContext = function(path) {
|
|
|
|
var code = fs.readFileSync(path);
|
|
|
|
vm.runInThisContext(code, path);
|
|
|
|
};
|
|
|
|
|
|
|
|
global.importScripts = function(path) {
|
|
|
|
includeInThisContext('src/worker/'+path);
|
|
|
|
}
|
|
|
|
|
2017-01-12 16:22:27 +00:00
|
|
|
function Blob(blob) {
|
|
|
|
this.size = blob.length;
|
|
|
|
this.length = blob.length;
|
|
|
|
this.slice = function(a,b) {
|
|
|
|
var data = blob.slice(a,b);
|
|
|
|
var b = new Blob(data);
|
|
|
|
//console.log(a, b, data.length, data.slice(0,64));
|
|
|
|
//console.log(new Error().stack);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
this.asArrayBuffer = function() {
|
|
|
|
var buf = new ArrayBuffer(blob.length);
|
|
|
|
var arr = new Uint8Array(buf);
|
|
|
|
for (var i=0; i<blob.length; i++)
|
|
|
|
arr[i] = blob[i].charCodeAt(0);
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 02:02:23 +00:00
|
|
|
global.XMLHttpRequest = function() {
|
|
|
|
this.open = function(a,b,c) {
|
|
|
|
if (this.responseType == 'json') {
|
2017-01-12 16:22:27 +00:00
|
|
|
var txt = fs.readFileSync('src/worker/'+b);
|
2017-01-12 02:02:23 +00:00
|
|
|
this.response = JSON.parse(txt);
|
|
|
|
} else if (this.responseType == 'blob') {
|
2017-01-12 16:22:27 +00:00
|
|
|
var data = fs.readFileSync('src/worker/'+b, {encoding:'binary'});
|
|
|
|
//var buf = new ArrayBuffer(data.length);
|
|
|
|
//var blob = new Uint8Array(buf);
|
|
|
|
//blob.set(data);
|
|
|
|
this.response = new Blob(data);
|
2017-01-12 02:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.send = function() { }
|
|
|
|
}
|
|
|
|
|
|
|
|
global.FileReaderSync = function() {
|
|
|
|
this.readAsArrayBuffer = function(blob) {
|
2017-01-12 16:22:27 +00:00
|
|
|
return blob.asArrayBuffer();
|
2017-01-12 02:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global.onmessage = null;
|
|
|
|
global.postMessage = null;
|
|
|
|
|
|
|
|
includeInThisContext("src/worker/workermain.js");
|
|
|
|
|
2017-01-15 03:46:12 +00:00
|
|
|
function compile(tool, code, platform, callback, outlen, nlines, nerrors) {
|
2017-01-12 02:02:23 +00:00
|
|
|
global.postMessage = function(msg) {
|
2017-01-15 03:46:12 +00:00
|
|
|
if (msg.errors && msg.errors.length) {
|
|
|
|
console.log(msg.errors);
|
|
|
|
assert.equal(nerrors, msg.errors.length, "errors");
|
2017-01-14 02:31:04 +00:00
|
|
|
} else {
|
|
|
|
assert.equal(nerrors||0, 0, "errors");
|
|
|
|
assert.equal(msg.output.length, outlen, "output binary");
|
2017-01-15 03:46:12 +00:00
|
|
|
assert.equal(msg.lines.length, nlines, "listing lines");
|
2017-01-14 02:31:04 +00:00
|
|
|
}
|
2017-01-12 02:02:23 +00:00
|
|
|
callback(null, msg);
|
|
|
|
};
|
|
|
|
global.onmessage({
|
2017-01-15 03:46:12 +00:00
|
|
|
data:{code:code, platform:platform, tool:tool}
|
2017-01-12 02:02:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Worker', function() {
|
2017-01-13 02:21:35 +00:00
|
|
|
it('should assemble DASM', function(done) {
|
2017-01-15 03:46:12 +00:00
|
|
|
compile('dasm', '\tprocessor 6502\n\torg $f000\nfoo lda #0\n', 'vcs', done, 2, 1);
|
|
|
|
});
|
2017-01-16 15:35:19 +00:00
|
|
|
it('should NOT assemble DASM', function(done) {
|
|
|
|
compile('dasm', '\tprocessor 6502\n\torg $f000\nfoo xxx #0\n', 'vcs', done, 0, 0, 1);
|
|
|
|
});
|
2017-01-15 03:46:12 +00:00
|
|
|
it('should assemble ACME', function(done) {
|
|
|
|
compile('acme', 'foo: lda #0\n', 'vcs', done, 2, 0); // TODO
|
2017-01-12 02:02:23 +00:00
|
|
|
});
|
2017-01-16 15:35:19 +00:00
|
|
|
it('should NOT assemble ACME', function(done) {
|
|
|
|
compile('acme', 'foo: xxx #0\n', 'vcs', done, 0, 0, 2); // TODO
|
|
|
|
});
|
2017-01-12 02:02:23 +00:00
|
|
|
it('should compile PLASMA', function(done) {
|
2017-01-15 03:46:12 +00:00
|
|
|
compile('plasm', 'word x = 0', 'apple2', done, 5, 0);
|
2017-01-12 02:02:23 +00:00
|
|
|
});
|
2017-01-16 15:35:19 +00:00
|
|
|
it('should NOT compile PLASMA', function(done) {
|
|
|
|
compile('plasm', 'word x = ', 'apple2', done, 0, 0, 1);
|
|
|
|
});
|
2017-01-12 02:02:23 +00:00
|
|
|
it('should compile CC65', function(done) {
|
2017-01-15 03:46:12 +00:00
|
|
|
compile('cc65', '#include <stdio.h>\nint main() {\nint x=1;\nprintf("%d",x);\nreturn x+2;\n}', 'apple2', done, 2947, 4);
|
2017-01-14 02:31:04 +00:00
|
|
|
});
|
2017-01-16 15:35:19 +00:00
|
|
|
it('should NOT compile CC65', function(done) {
|
|
|
|
compile('cc65', 'int main() {\nint x=1;\nprintf("%d",x);\nreturn x+2;\n}', 'apple2', done, 0, 0, 1);
|
2017-01-12 02:02:23 +00:00
|
|
|
});
|
2017-01-13 02:21:35 +00:00
|
|
|
it('should assemble Z80ASM', function(done) {
|
2017-01-15 18:31:52 +00:00
|
|
|
compile('z80asm', '\tMODULE test\n\tXREF _puts\n\tld hl,$0000\n\tret\n', 'spaceinv', done, 4, 2, 0);
|
2017-01-15 03:46:12 +00:00
|
|
|
});
|
2017-01-16 15:35:19 +00:00
|
|
|
it('should NOT assemble Z80ASM', function(done) {
|
|
|
|
compile('z80asm', 'ddwiuweq', 'none', done, 0, 0, 1);
|
2017-01-15 18:31:52 +00:00
|
|
|
});
|
|
|
|
it('should assemble SDASZ80', function(done) {
|
2017-01-16 15:35:19 +00:00
|
|
|
compile('sdasz80', '\tld hl,#0\n\tret\n', 'spaceinv', done, 8192, 2);
|
|
|
|
});
|
|
|
|
it('should NOT assemble SDASZ80', function(done) {
|
|
|
|
compile('sdasz80', '\txxx hl,#0\n\tret\n', 'spaceinv', done, 0, 0, 1);
|
2017-01-13 02:21:35 +00:00
|
|
|
});
|
|
|
|
it('should compile SDCC', function(done) {
|
2017-01-15 18:31:52 +00:00
|
|
|
compile('sdcc', 'int foo=0;\nint main(int argc) {\nint x=1;\nint y=2+argc;\nreturn x+y+argc;\n}', 'spaceinv', done, 8192, 3, 0);
|
2017-01-13 02:21:35 +00:00
|
|
|
});
|
2017-01-16 15:35:19 +00:00
|
|
|
it('should NOT compile SDCC', function(done) {
|
|
|
|
compile('sdcc', 'foobar', 'spaceinv', done, 0, 0, 1);
|
|
|
|
});
|
2017-01-12 02:02:23 +00:00
|
|
|
});
|