1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-06-16 12:29:28 +00:00
8bitworkshop/test/worker.js

86 lines
2.4 KiB
JavaScript
Raw Normal View History

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");
function compile(tool, code, callback, outlen) {
global.postMessage = function(msg) {
2017-01-12 16:22:27 +00:00
if (msg.listing.errors.length) console.log(msg.listing.errors);
2017-01-12 02:02:23 +00:00
assert.ok(msg.listing.lines);
assert.equal(msg.listing.errors.length, msg.listing.errors);
assert.equal(msg.output.length, outlen);
callback(null, msg);
};
global.onmessage({
data:{code:code, tool:tool}
});
}
describe('Worker', function() {
it('should compile DASM', function(done) {
compile('dasm', '\tprocessor 6502\n\torg $f000\nfoo lda #0\n', done, 2);
});
it('should compile PLASMA', function(done) {
compile('plasm', 'word x = 0', done, 5);
});
it('should compile CC65', function(done) {
2017-01-12 16:22:27 +00:00
compile('cc65', '#include <stdio.h>\nint main() {\nint x=1;\nprintf("%d",x);\nreturn x+2;\n}', done, 2947);
2017-01-12 02:02:23 +00:00
});
});