8bitworkshop/test/cli/workertestutils.js

72 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-11-22 14:42:07 +00:00
var assert = require('assert');
var fs = require('fs');
var vm = require('vm');
var worker = {};
global.window = global;
global.exports = {};
2017-11-22 14:42:07 +00:00
global.includeInThisContext = function(path) {
var code = fs.readFileSync(path);
vm.runInThisContext(code, path);
2017-11-22 14:42:07 +00:00
};
global.importScripts = function(path) {
includeInThisContext('gen/worker/'+path);
2017-11-22 14:42:07 +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;
}
}
global.XMLHttpRequest = function() {
this.open = function(a,b,c) {
2019-03-17 02:02:35 +00:00
//console.log(':::xml',a,'src/worker/'+b,c,this.responseType);
2017-11-22 14:42:07 +00:00
if (this.responseType == 'json') {
var txt = fs.readFileSync('src/worker/'+b);
this.response = JSON.parse(txt);
} else if (this.responseType == 'blob') {
var data = fs.readFileSync('src/worker/'+b, {encoding:'binary'});
this.response = new Blob(data);
} else if (this.responseType == 'arraybuffer') {
var data = fs.readFileSync('src/worker/'+b, {encoding:'binary'});
this.response = new Blob(data).asArrayBuffer();
2017-11-22 14:42:07 +00:00
}
2019-03-17 02:02:35 +00:00
this.status = this.response ? 200 : 404;
//console.log(':::xml',this.response.length);
2017-11-22 14:42:07 +00:00
}
this.send = function() { }
}
global.FileReaderSync = function() {
this.readAsArrayBuffer = function(blob) {
return blob.asArrayBuffer();
}
}
global.onmessage = null;
global.postMessage = null;
includeInThisContext("gen/worker/workermain.js");
2017-11-22 14:42:07 +00:00
global.ab2str = function(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}