Skip to content

Commit

Permalink
Add XHR backend and wrap up disk initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
nepx committed May 22, 2020
1 parent a8a39f3 commit e532d51
Showing 1 changed file with 86 additions and 7 deletions.
93 changes: 86 additions & 7 deletions libhalfix.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,29 @@
gc();
};

global["drives"] = [];

window["drive_init"] = function (info_ptr, path, id) {
var p = readstr(path);
if(p.indexOf("!") !== -1){
global["drive_init"] = function (info_ptr, path, id) {
var p = readstr(path), image;
if (p.indexOf("!") !== -1) {
var chunks = p.split("!");
var image = new image_backends[chunks[0]](_cache[parseInt(chunks[1]) | 0]);
}else{
var image = 0;
}
image = new image_backends[chunks[0]](_cache[parseInt(chunks[1]) | 0]);
} else
image = new XHRImage();
requests_in_progress = requests_in_progress + 1 | 0;
image.init(p, function(err, data){
if(err) throw err;

var dataptr = alloc(data.length), strptr = alloc(p.length + 1);
memcpy(dataptr, data);
strcpy(strptr, p);
wrap("drive_emscripten_init")(info_ptr, strptr, dataptr, id);
gc();

global["drives"][id] = image;
requests_in_progress = requests_in_progress - 1 | 0;
if (requests_in_progress === 0) run_wrapper2();
});
};

// ========================================================================
Expand Down Expand Up @@ -559,6 +573,15 @@
throw new Error("implement me");
};

/**
* Initialize hard drive image
* @param {string} arg
* @param {function(Uint8Array)} cb
*/
HardDriveImage.prototype.init = function (arg, cb) {
throw new Error("implement me");
};

/**
* Convert a URL (i.e. os2/blk0000005a.bin) into a number (i.e. 0x5a)
* @param {string} str
Expand All @@ -569,6 +592,19 @@
return parseInt(parts[1], 16) | 0;
}

/**
* Create an "info.dat" file
* @param {number} size
* @param {number} blksize
* @returns {Uint8Array} The data that would have been contained in info.dat
*/
function _construct_info(size, blksize) {
var i32 = new Int32Array(2);
i32[0] = size;
i32[1] = blksize;
return new Uint8Array(i32.buffer);
}

/**
* ArrayBuffer-backed image
* @param {ArrayBuffer} ab
Expand All @@ -594,6 +630,12 @@
cb(null, data);
}, 0);
};
ArrayBufferImage.prototype.init = function (arg, cb) {
var data = _construct_info(this.data.byteLength, 256 << 10);
setTimeout(function () {
cb(null, data);
}, 0);
};

/**
* File API-backed image
Expand Down Expand Up @@ -632,6 +674,30 @@
};
fr.readAsArrayBuffer(this.file);
};
FileImage.prototype.init = function (arg, cb) {
var data = _construct_info(this.file.size, 256 << 10);
setTimeout(function () {
cb(null, data);
}, 0);
};

/**
* XHR-backed image
* @constructor
* @extends HardDriveImage
*/
function XHRImage() {
}
XHRImage.prototype = new HardDriveImage();
XHRImage.prototype.load = function (reqs, cb) {
loadFiles(reqs, cb);
};
XHRImage.prototype.init = function (arg, cb) {
loadFiles([join_path(arg, "info.dat")], function (err, data) {
if (err) throw err;
cb(null, data[0]);
});
};

var image_backends = {
"file": FileImage,
Expand Down Expand Up @@ -713,5 +779,18 @@
return Module["_" + nm];
}

/**
* Join two fragments of a path together
* @param {string} a The first part of the path
* @param {string} b The second part of the path
*/
function join_path(a, b) {
if (b.charAt(0) !== "/")
b = "/" + b;
if (a.charAt(a.length - 1 | 0) === "/")
a = a.substring(0, a.length - 1 | 0);
return a + b; //normalize_path(a + b);
}

global["Halfix"] = Halfix;
})(typeof window !== "undefined" ? window : this);

0 comments on commit e532d51

Please sign in to comment.