v86/src/browser/starter.js

925 lines
22 KiB
JavaScript
Raw Normal View History

"use strict";
2015-09-16 03:25:09 +02:00
/**
2015-01-11 23:31:08 +01:00
* Constructor for emulator instances.
*
* Usage: `var emulator = new V86Starter(options);`
*
* Options can have the following properties (all optional, default in parenthesis):
*
* - `memory_size number` (16 * 1024 * 1024) - The memory size in bytes, should
* be a power of 2.
* - `vga_memory_size number` (8 * 1024 * 1024) - VGA memory size in bytes.
2015-01-20 00:05:17 +01:00
*
2015-01-11 23:31:08 +01:00
* - `autostart boolean` (false) - If emulation should be started when emulator
* is ready.
2015-01-20 00:05:17 +01:00
*
2015-01-11 23:31:08 +01:00
* - `disable_keyboard boolean` (false) - If the keyboard should be disabled.
* - `disable_mouse boolean` (false) - If the mouse should be disabled.
2015-01-20 00:05:17 +01:00
*
2015-01-11 23:31:08 +01:00
* - `network_relay_url string` (No network card) - The url of a server running
2015-03-04 00:58:00 +01:00
* websockproxy. See [networking.md](networking.md). Setting this will
2015-01-20 00:05:17 +01:00
* enable an emulated network card.
*
2015-01-11 23:31:08 +01:00
* - `bios Object` (No bios) - Either a url pointing to a bios or an
* ArrayBuffer, see below.
* - `vga_bios Object` (No VGA bios) - VGA bios, see below.
* - `hda Object` (No hard drive) - First hard disk, see below.
* - `fda Object` (No floppy disk) - First floppy disk, see below.
2015-01-20 00:05:17 +01:00
* - `cdrom Object` (No CD) - See below.
2015-01-11 23:31:08 +01:00
* - `initial_state Object` (Normal boot) - An initial state to load, see
* [`restore_state`](#restore_statearraybuffer-state) and below.
2015-01-20 00:05:17 +01:00
*
* - `filesystem Object` (No 9p filesystem) - A 9p filesystem, see
2015-03-04 00:58:00 +01:00
* [filesystem.md](filesystem.md).
2015-01-20 00:05:17 +01:00
*
2015-01-11 23:31:08 +01:00
* - `serial_container HTMLTextAreaElement` (No serial terminal) - A textarea
* that will receive and send data to the emulated serial terminal.
* Alternatively the serial terminal can also be accessed programatically,
* see [serial.html](../examples/serial.html).
2015-01-20 00:05:17 +01:00
*
2015-01-11 23:31:08 +01:00
* - `screen_container HTMLElement` (No screen) - An HTMLElement. This should
* have a certain structure, see [basic.html](../examples/basic.html).
2015-01-20 00:05:17 +01:00
*
* ***
2015-01-11 23:31:08 +01:00
*
* There are two ways to load images (`bios`, `vga_bios`, `cdrom`, `hda`, ...):
*
2015-01-20 00:05:17 +01:00
* - Pass an object that has a url. Optionally, `async: true` and `size:
* size_in_bytes` can be added to the object, so that sectors of the image
* are loaded on demand instead of being loaded before boot (slower, but
* strongly recommended for big files). In that case, the `Range: bytes=...`
* header must be supported on the server.
*
* ```javascript
* // download file before boot
2015-09-16 03:25:09 +02:00
* options.bios = {
* url: "bios/seabios.bin"
2015-01-20 00:05:17 +01:00
* }
* // download file sectors as requested, size is required
2015-09-16 03:25:09 +02:00
* options.hda = {
2015-01-20 00:05:17 +01:00
* url: "disk/linux.iso",
* async: true,
2015-09-16 03:25:09 +02:00
* size: 16 * 1024 * 1024
2015-01-20 00:05:17 +01:00
* }
* ```
*
* - Pass an `ArrayBuffer` or `File` object as `buffer` property.
*
* ```javascript
* // use <input type=file>
2015-09-16 03:25:09 +02:00
* options.bios = {
2015-01-20 00:05:17 +01:00
* buffer: document.all.hd_image.files[0]
* }
* // start with empty hard drive
2015-09-16 03:25:09 +02:00
* options.hda = {
2015-01-20 00:05:17 +01:00
* buffer: new ArrayBuffer(16 * 1024 * 1024)
* }
* ```
2015-01-11 23:31:08 +01:00
*
2015-01-20 00:05:17 +01:00
* ***
2015-01-11 23:31:08 +01:00
*
* @param {Object} options Options to initialize the emulator with.
2015-09-16 03:25:09 +02:00
* @constructor
2015-01-11 23:31:08 +01:00
*/
function V86Starter(options)
{
//var worker = new Worker("src/browser/worker.js");
//var adapter_bus = this.bus = WorkerBus.init(worker);
2015-09-16 03:25:09 +02:00
this.cpu_is_running = false;
var bus = Bus.create();
var adapter_bus = this.bus = bus[0];
this.emulator_bus = bus[1];
var emulator = this.v86 = new v86(this.emulator_bus);
this.bus.register("emulator-stopped", function()
{
this.cpu_is_running = false;
}, this);
this.bus.register("emulator-started", function()
{
this.cpu_is_running = true;
}, this);
var settings = {};
2015-12-31 00:12:53 +01:00
this.disk_images = {
"fda": undefined,
"fdb": undefined,
"hda": undefined,
"hdb": undefined,
"cdrom": undefined,
};
settings.load_devices = true;
2015-01-11 23:30:43 +01:00
settings.memory_size = options["memory_size"] || 64 * 1024 * 1024;
settings.vga_memory_size = options["vga_memory_size"] || 8 * 1024 * 1024;
settings.boot_order = options["boot_order"] || 0x213;
settings.fda = undefined;
settings.fdb = undefined;
if(options["network_relay_url"])
{
2015-01-11 18:52:34 +01:00
this.network_adapter = new NetworkAdapter(options["network_relay_url"], adapter_bus);
settings.enable_ne2k = true;
}
if(!options["disable_keyboard"])
{
this.keyboard_adapter = new KeyboardAdapter(adapter_bus);
}
if(!options["disable_mouse"])
{
2016-02-15 00:16:22 +01:00
this.mouse_adapter = new MouseAdapter(adapter_bus, options["screen_container"]);
}
if(options["screen_container"])
{
this.screen_adapter = new ScreenAdapter(options["screen_container"], adapter_bus);
}
if(options["serial_container"])
{
this.serial_adapter = new SerialAdapter(options["serial_container"], adapter_bus);
}
2015-01-17 21:59:16 +01:00
// ugly, but required for closure compiler compilation
function put_on_settings(name, buffer)
{
switch(name)
{
case "hda":
2015-12-31 00:12:53 +01:00
settings.hda = this.disk_images["hda"] = buffer;
2015-01-17 21:59:16 +01:00
break;
case "hdb":
2015-12-31 00:12:53 +01:00
settings.hdb = this.disk_images["hdb"] = buffer;
2015-01-17 21:59:16 +01:00
break;
case "cdrom":
2015-12-31 00:12:53 +01:00
settings.cdrom = this.disk_images["cdrom"] = buffer;
2015-01-17 21:59:16 +01:00
break;
case "fda":
2015-12-31 00:12:53 +01:00
settings.fda = this.disk_images["fda"] = buffer;
2015-01-17 21:59:16 +01:00
break;
case "fdb":
2015-12-31 00:12:53 +01:00
settings.fdb = this.disk_images["fdb"] = buffer;
2015-01-17 21:59:16 +01:00
break;
2015-01-24 01:47:05 +01:00
2015-01-17 21:59:16 +01:00
case "bios":
2015-01-24 01:47:05 +01:00
settings.bios = buffer.buffer;
2015-01-17 21:59:16 +01:00
break;
case "vga_bios":
2015-01-24 01:47:05 +01:00
settings.vga_bios = buffer.buffer;
break;
case "initial_state":
settings.initial_state = buffer.buffer;
break;
case "fs9p_json":
settings.fs9p_json = buffer.buffer;
2015-01-17 21:59:16 +01:00
break;
default:
dbg_assert(false, name);
}
}
var files_to_load = [];
2015-01-24 01:47:05 +01:00
function add_file(name, file)
{
if(!file)
{
return;
}
2015-09-16 03:46:13 +02:00
if(file["get"] && file["set"] && file["load"])
{
files_to_load.push({
name: name,
loadable: file,
});
return;
}
2015-01-24 01:47:05 +01:00
// Anything coming from the outside world needs to be quoted for
2015-01-19 22:30:22 +01:00
// Closure Compiler compilation
file = {
buffer: file["buffer"],
async: file["async"],
url: file["url"],
size: file["size"],
};
2015-01-24 01:47:05 +01:00
if(name === "bios" || name === "vga_bios" || name === "initial_state")
{
2015-01-24 01:47:05 +01:00
// Ignore async for these because they must be availabe before boot.
// This should make result.buffer available after the object is loaded
file.async = false;
}
2015-01-24 01:47:05 +01:00
if(file.buffer instanceof ArrayBuffer)
{
2015-01-24 01:47:05 +01:00
var buffer = new SyncBuffer(file.buffer);
files_to_load.push({
name: name,
loadable: buffer,
});
}
else if(typeof File !== "undefined" && file.buffer instanceof File)
{
// SyncFileBuffer:
// - loads the whole disk image into memory, impossible for large files (more than 1GB)
2015-09-16 03:25:09 +02:00
// - can later serve get/set operations fast and synchronously
// - takes some time for first load, neglectable for small files (up to 100Mb)
//
// AsyncFileBuffer:
// - loads slices of the file asynchronously as requested
// - slower get/set
// Heuristics: If file is smaller than 256M, use SyncFileBuffer
2015-01-24 01:47:05 +01:00
if(file.async === undefined)
2015-01-13 01:34:09 +01:00
{
file.async = file.buffer.size < 256 * 1024 * 1024;
2015-01-24 01:47:05 +01:00
}
if(file.async)
{
var buffer = new v86util.SyncFileBuffer(file.buffer);
2015-01-13 01:34:09 +01:00
}
else
{
2015-01-24 01:47:05 +01:00
var buffer = new v86util.AsyncFileBuffer(file.buffer);
2015-01-13 01:34:09 +01:00
}
2015-01-24 01:47:05 +01:00
files_to_load.push({
name: name,
loadable: buffer,
});
}
2015-01-24 01:47:05 +01:00
else if(file.url)
{
2015-01-24 01:47:05 +01:00
if(file.async)
{
var buffer = new v86util.AsyncXHRBuffer(file.url, file.size);
files_to_load.push({
name: name,
loadable: buffer,
});
}
else
{
files_to_load.push({
name: name,
url: file.url,
2015-03-03 20:49:00 +01:00
size: file.size,
2015-01-24 01:47:05 +01:00
});
}
}
else
{
2015-01-24 01:47:05 +01:00
dbg_log("Ignored file: url=" + file.url + " buffer=" + file.buffer);
}
}
2015-01-24 01:47:05 +01:00
var image_names = [
"bios", "vga_bios",
2015-01-24 01:47:05 +01:00
"cdrom", "hda", "hdb", "fda", "fdb",
"initial_state",
];
2015-01-24 01:47:05 +01:00
for(var i = 0; i < image_names.length; i++)
{
add_file(image_names[i], options[image_names[i]]);
}
2015-01-11 23:30:43 +01:00
if(options["filesystem"])
{
2015-01-19 22:56:52 +01:00
var fs_url = options["filesystem"]["basefs"];
var base_url = options["filesystem"]["baseurl"];
2015-01-24 01:47:05 +01:00
this.fs9p = new FS(base_url);
settings.fs9p = this.fs9p;
2015-01-19 22:56:52 +01:00
if(fs_url)
{
2015-01-19 22:56:52 +01:00
console.assert(base_url, "Filesystem: baseurl must be specified");
var size;
if(typeof fs_url === "object")
{
2015-09-16 03:46:13 +02:00
size = fs_url["size"];
fs_url = fs_url["url"];
}
dbg_assert(typeof fs_url === "string");
2015-01-24 01:47:05 +01:00
files_to_load.push({
name: "fs9p_json",
url: fs_url,
size: size,
2015-01-24 01:47:05 +01:00
as_text: true,
2015-01-19 22:56:52 +01:00
});
}
}
var starter = this;
2015-01-24 01:47:05 +01:00
var total = files_to_load.length;
var cont = function(index)
{
2015-01-24 01:47:05 +01:00
if(index === total)
{
setTimeout(done.bind(this), 0);
2015-01-24 01:47:05 +01:00
return;
}
var f = files_to_load[index];
2015-01-24 01:47:05 +01:00
if(f.loadable)
{
f.loadable.onload = function(e)
{
2015-12-31 00:12:53 +01:00
put_on_settings.call(this, f.name, f.loadable);
2015-01-24 01:47:05 +01:00
cont(index + 1);
2015-12-31 00:12:53 +01:00
}.bind(this);
2015-01-24 01:47:05 +01:00
f.loadable.load();
}
else
{
2015-01-13 03:00:15 +01:00
v86util.load_file(f.url, {
done: function(result)
2015-01-13 03:00:15 +01:00
{
2015-12-31 00:12:53 +01:00
put_on_settings.call(this, f.name, new SyncBuffer(result));
2015-01-13 03:00:15 +01:00
cont(index + 1);
2015-12-31 00:12:53 +01:00
}.bind(this),
2015-01-13 03:00:15 +01:00
progress: function progress(e)
{
starter.emulator_bus.send("download-progress", {
file_index: index,
file_count: total,
file_name: f.url,
2015-01-13 03:00:15 +01:00
lengthComputable: e.lengthComputable,
total: e.total || f.size,
2015-01-13 03:00:15 +01:00
loaded: e.loaded,
});
},
as_text: f.as_text,
});
}
}.bind(this);
cont(0);
2015-01-24 01:47:05 +01:00
function done()
2015-01-24 01:47:05 +01:00
{
if(settings.initial_state)
{
// avoid large allocation now, memory will be restored later anyway
settings.memory_size = 0;
}
this.bus.send("cpu-init", settings);
2015-01-24 01:47:05 +01:00
setTimeout(function()
{
if(settings.initial_state)
{
emulator.restore_state(settings.initial_state);
}
setTimeout(function()
{
2015-03-07 01:24:38 +01:00
if(settings.fs9p && settings.fs9p_json)
{
settings.fs9p.OnJSONLoaded(settings.fs9p_json);
}
if(options["autostart"])
{
this.bus.send("cpu-run");
}
}.bind(this), 0)
}.bind(this), 0);
}
}
/**
2015-01-11 23:31:08 +01:00
* Start emulation. Do nothing if emulator is running already. Can be
* asynchronous.
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.run = function()
{
this.bus.send("cpu-run");
};
/**
2015-01-11 23:31:08 +01:00
* Stop emulation. Do nothing if emulator is not running. Can be asynchronous.
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.stop = function()
{
this.bus.send("cpu-stop");
};
2015-09-14 01:57:13 +02:00
/**
* @ignore
2016-09-27 22:11:19 +02:00
* @export
2015-09-14 01:57:13 +02:00
*/
V86Starter.prototype.destroy = function()
{
this.keyboard_adapter.destroy();
};
/**
* Restart (force a reboot).
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.restart = function()
{
this.bus.send("cpu-restart");
};
/**
2015-01-11 23:31:08 +01:00
* Add an event listener (the emulator is an event emitter). A list of events
2015-03-04 00:58:00 +01:00
* can be found at [events.md](events.md).
2015-01-11 23:31:08 +01:00
*
* The callback function gets a single argument which depends on the event.
*
* @param {string} event Name of the event.
2015-09-16 03:25:09 +02:00
* @param {function(*)} listener The callback function.
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.add_listener = function(event, listener)
{
this.bus.register(event, listener, this);
};
/**
2015-09-16 03:25:09 +02:00
* Remove an event listener.
2015-01-11 23:31:08 +01:00
*
* @param {string} event
* @param {function(*)} listener
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.remove_listener = function(event, listener)
{
this.bus.unregister(event, listener);
};
/**
2015-01-11 23:31:08 +01:00
* Restore the emulator state from the given state, which must be an
* ArrayBuffer returned by
2015-09-16 03:25:09 +02:00
* [`save_state`](#save_statefunctionobject-arraybuffer-callback).
2015-01-11 23:31:08 +01:00
*
* Note that the state can only be restored correctly if this constructor has
* been created with the same options as the original instance (e.g., same disk
2015-09-16 03:25:09 +02:00
* images, memory size, etc.).
2015-01-11 23:31:08 +01:00
*
* Different versions of the emulator might use a different format for the
* state buffer.
*
* @param {ArrayBuffer} state
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.restore_state = function(state)
{
this.v86.restore_state(state);
};
/**
2015-01-11 23:31:08 +01:00
* Asynchronously save the current state of the emulator. The first argument to
* the callback is an Error object if something went wrong and is null
* otherwise.
*
* @param {function(Object, ArrayBuffer)} callback
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.save_state = function(callback)
{
// Might become asynchronous at some point
setTimeout(function()
{
2015-01-11 23:31:08 +01:00
try
{
callback(null, this.v86.save_state());
2015-01-11 23:31:08 +01:00
}
catch(e)
{
callback(e, null);
}
}.bind(this), 0);
};
/**
2015-01-11 23:31:08 +01:00
* Return an object with several statistics. Return value looks similar to
* (but can be subject to change in future versions or different
* configurations, so use defensively):
*
2015-01-20 00:05:17 +01:00
* ```javascript
2015-01-11 23:31:08 +01:00
* {
* "cpu": {
* "instruction_counter": 2821610069
* },
* "hda": {
* "sectors_read": 95240,
* "sectors_written": 952,
* "bytes_read": 48762880,
* "bytes_written": 487424,
* "loading": false
* },
* "cdrom": {
* "sectors_read": 0,
* "sectors_written": 0,
* "bytes_read": 0,
* "bytes_written": 0,
* "loading": false
* },
* "mouse": {
* "enabled": true
* },
* "vga": {
* "is_graphical": true,
* "res_x": 800,
* "res_y": 600,
* "bpp": 32
* }
* }
* ```
*
* @deprecated
* @return {Object}
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.get_statistics = function()
{
console.warn("V86Starter.prototype.get_statistics is deprecated. Use events instead.");
var stats = {
cpu: {
2015-01-14 00:28:22 +01:00
instruction_counter: this.get_instruction_counter(),
},
};
if(!this.v86)
{
return stats;
}
var devices = this.v86.cpu.devices;
if(devices.hda)
{
stats.hda = devices.hda.stats;
}
if(devices.cdrom)
{
stats.cdrom = devices.cdrom.stats;
}
if(devices.ps2)
{
2015-09-16 03:46:13 +02:00
stats["mouse"] = {
"enabled": devices.ps2.use_mouse,
};
}
if(devices.vga)
{
2015-09-16 03:46:13 +02:00
stats["vga"] = {
"is_graphical": devices.vga.stats.is_graphical,
};
}
return stats;
};
2015-01-14 00:28:22 +01:00
/**
* @return {number}
* @ignore
2016-09-27 22:11:19 +02:00
* @export
2015-01-14 00:28:22 +01:00
*/
V86Starter.prototype.get_instruction_counter = function()
{
if(this.v86)
{
return this.v86.cpu.timestamp_counter;
}
else
{
// TODO: Should be handled using events
return 0;
}
2015-01-14 00:28:22 +01:00
};
/**
* @return {boolean}
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.is_running = function()
{
return this.cpu_is_running;
};
2015-09-16 03:25:09 +02:00
/**
2015-01-11 23:31:08 +01:00
* Send a sequence of scan codes to the emulated PS2 controller. A list of
* codes can be found at http://stanislavs.org/helppc/make_codes.html.
* Do nothing if there is no keyboard controller.
2015-01-11 23:31:08 +01:00
*
* @param {Array.<number>} codes
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.keyboard_send_scancodes = function(codes)
{
for(var i = 0; i < codes.length; i++)
{
this.bus.send("keyboard-code", codes[i]);
}
};
/**
* Send translated keys
* @ignore
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.keyboard_send_keys = function(codes)
{
for(var i = 0; i < codes.length; i++)
{
this.keyboard_adapter.simulate_press(codes[i]);
}
};
/**
* Send text
* @ignore
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.keyboard_send_text = function(string)
{
for(var i = 0; i < string.length; i++)
{
this.keyboard_adapter.simulate_char(string[i]);
}
};
/**
2015-01-11 23:31:08 +01:00
* Download a screenshot.
*
2015-01-11 23:31:08 +01:00
* @ignore
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.screen_make_screenshot = function()
{
if(this.screen_adapter)
{
this.screen_adapter.make_screenshot();
}
};
/**
2015-01-11 23:31:08 +01:00
* Set the scaling level of the emulated screen.
*
* @param {number} sx
* @param {number} sy
2015-01-11 23:31:08 +01:00
*
* @ignore
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.screen_set_scale = function(sx, sy)
{
if(this.screen_adapter)
{
this.screen_adapter.set_scale(sx, sy);
}
};
/**
2015-01-11 23:31:08 +01:00
* Go fullscreen.
*
* @ignore
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.screen_go_fullscreen = function()
{
if(!this.screen_adapter)
{
return;
}
var elem = document.getElementById("screen_container");
if(!elem)
{
return;
}
// bracket notation because otherwise they get renamed by closure compiler
2015-09-16 03:25:09 +02:00
var fn = elem["requestFullScreen"] ||
elem["webkitRequestFullscreen"] ||
elem["mozRequestFullScreen"] ||
elem["msRequestFullScreen"];
if(fn)
{
fn.call(elem);
// This is necessary, because otherwise chromium keyboard doesn't work anymore.
// Might (but doesn't seem to) break something else
var focus_element = document.getElementsByClassName("phone_keyboard")[0];
focus_element && focus_element.focus();
}
//this.lock_mouse(elem);
this.lock_mouse();
};
/**
2015-01-12 18:54:52 +01:00
* Lock the mouse cursor: It becomes invisble and is not moved out of the
2015-01-11 23:31:08 +01:00
* browser window.
*
* @ignore
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.lock_mouse = function()
{
var elem = document.body;
var fn = elem["requestPointerLock"] ||
elem["mozRequestPointerLock"] ||
elem["webkitRequestPointerLock"];
if(fn)
{
fn.call(elem);
}
};
2015-09-16 03:25:09 +02:00
/**
2015-01-11 23:31:08 +01:00
* Enable or disable sending mouse events to the emulated PS2 controller.
*
* @param {boolean} enabled
*/
V86Starter.prototype.mouse_set_status = function(enabled)
{
if(this.mouse_adapter)
{
this.mouse_adapter.emu_enabled = enabled;
}
};
2015-09-16 03:25:09 +02:00
/**
2015-01-11 23:31:08 +01:00
* Enable or disable sending keyboard events to the emulated PS2 controller.
2015-01-09 15:56:43 +01:00
*
* @param {boolean} enabled
2016-09-27 22:11:19 +02:00
* @export
2015-01-09 15:56:43 +01:00
*/
V86Starter.prototype.keyboard_set_status = function(enabled)
{
if(this.keyboard_adapter)
{
this.keyboard_adapter.emu_enabled = enabled;
}
};
2015-09-16 03:25:09 +02:00
/**
2015-01-11 23:31:08 +01:00
* Send a string to the first emulated serial terminal.
*
* @param {string} data
2016-09-27 22:11:19 +02:00
* @export
*/
V86Starter.prototype.serial0_send = function(data)
{
for(var i = 0; i < data.length; i++)
{
this.bus.send("serial0-input", data.charCodeAt(i));
}
};
2015-01-19 22:45:27 +01:00
/**
2015-01-20 00:05:17 +01:00
* Write to a file in the 9p filesystem. Nothing happens if no filesystem has
* been initialized. First argument to the callback is an error object if
* something went wrong and null otherwise.
*
2015-01-19 22:45:27 +01:00
* @param {string} file
* @param {Uint8Array} data
2015-01-20 00:05:17 +01:00
* @param {function(Object)=} callback
2016-09-27 22:11:19 +02:00
* @export
2015-01-19 22:45:27 +01:00
*/
V86Starter.prototype.create_file = function(file, data, callback)
{
var fs = this.fs9p;
2015-01-20 00:04:26 +01:00
if(!fs)
{
return;
}
2015-01-19 22:45:27 +01:00
var parts = file.split("/");
var filename = parts[parts.length - 1];
var path_infos = fs.SearchPath(file);
var parent_id = path_infos.parentid;
var not_found = filename === "" || parent_id === -1
if(!not_found)
{
fs.CreateBinaryFile(filename, parent_id, data);
}
if(callback)
{
setTimeout(function()
{
if(not_found)
{
callback(new FileNotFoundError());
}
else
{
callback(null);
}
}, 0);
}
};
/**
2015-01-20 00:05:17 +01:00
* Read a file in the 9p filesystem. Nothing happens if no filesystem has been
* initialized.
*
2015-01-19 22:45:27 +01:00
* @param {string} file
2015-01-20 00:05:17 +01:00
* @param {function(Object, Uint8Array)} callback
2016-09-27 22:11:19 +02:00
* @export
2015-01-19 22:45:27 +01:00
*/
V86Starter.prototype.read_file = function(file, callback)
{
var fs = this.fs9p;
2015-01-20 00:04:26 +01:00
if(!fs)
{
return;
}
2015-01-19 22:45:27 +01:00
var path_infos = fs.SearchPath(file);
var id = path_infos.id;
if(id === -1)
{
callback(new FileNotFoundError(), null);
}
else
{
fs.OpenInode(id, undefined);
fs.AddEvent(
id,
function()
2015-01-19 22:45:27 +01:00
{
var data = fs.inodedata[id];
if(data)
{
callback(null, data.subarray(0, fs.inodes[id].size));
}
else
{
callback(new FileNotFoundError(), null);
}
2015-01-19 22:45:27 +01:00
}
);
}
};
2015-09-16 03:25:09 +02:00
/**
2015-01-19 22:45:27 +01:00
* @ignore
* @constructor
*
* @param {string=} message
*/
function FileNotFoundError(message)
{
this.message = message || "File not found";
}
FileNotFoundError.prototype = Error.prototype;
2015-12-31 00:31:08 +01:00
// Closure Compiler's way of exporting
if(typeof window !== "undefined")
{
window["V86Starter"] = V86Starter;
2015-12-31 00:31:22 +01:00
window["V86"] = V86Starter;
}
2015-01-17 20:48:11 +01:00
else if(typeof module !== "undefined" && typeof module.exports !== "undefined")
{
module.exports["V86Starter"] = V86Starter;
2015-12-31 00:31:22 +01:00
module.exports["V86"] = V86Starter;
2015-01-17 20:48:11 +01:00
}
2015-01-31 17:39:32 +01:00
else if(typeof importScripts === "function")
{
// web worker
self["V86Starter"] = V86Starter;
2015-12-31 00:31:22 +01:00
self["V86"] = V86Starter;
2015-01-31 17:39:32 +01:00
}