From 53098adc5ca80484cfbaa9105abc17a035cbd07c Mon Sep 17 00:00:00 2001 From: Fabian Date: Mon, 19 Sep 2022 21:41:58 +0800 Subject: [PATCH] Delete src/browser/lib.js, move remaining code to lib.js --- Makefile | 2 +- debug.html | 2 +- src/browser/lib.js | 173 --------------------------------------------- src/lib.js | 170 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+), 175 deletions(-) delete mode 100644 src/browser/lib.js diff --git a/Makefile b/Makefile index 86bc6303..c026eccb 100644 --- a/Makefile +++ b/Makefile @@ -85,7 +85,7 @@ CORE_FILES=const.js config.js io.js main.js lib.js buffer.js ide.js pci.js flopp elf.js kernel.js LIB_FILES=9p.js filesystem.js jor1k.js marshall.js utf8.js BROWSER_FILES=screen.js keyboard.js mouse.js speaker.js serial.js \ - network.js lib.js starter.js worker_bus.js dummy_screen.js \ + network.js starter.js worker_bus.js dummy_screen.js \ print_stats.js filestorage.js RUST_FILES=$(shell find src/rust/ -name '*.rs') \ diff --git a/debug.html b/debug.html index 11f11627..704377cc 100644 --- a/debug.html +++ b/debug.html @@ -13,7 +13,7 @@ var CORE_FILES = "memory.js dma.js pit.js vga.js ps2.js pic.js rtc.js uart.js acpi.js apic.js ioapic.js hpet.js sb16.js " + "ne2k.js state.js virtio.js bus.js elf.js kernel.js"; -var BROWSER_FILES = "main.js screen.js keyboard.js mouse.js speaker.js serial.js lib.js network.js starter.js worker_bus.js print_stats.js filestorage.js"; +var BROWSER_FILES = "main.js screen.js keyboard.js mouse.js speaker.js serial.js network.js starter.js worker_bus.js print_stats.js filestorage.js"; var LIB_FILES = ""; // jor1k stuff diff --git a/src/browser/lib.js b/src/browser/lib.js deleted file mode 100644 index f02092dd..00000000 --- a/src/browser/lib.js +++ /dev/null @@ -1,173 +0,0 @@ -"use strict"; - -(function() -{ - if(typeof XMLHttpRequest === "undefined") - { - v86util.load_file = load_file_nodejs; - } - else - { - v86util.load_file = load_file; - } - - // Reads len characters at offset from Memory object mem as a JS string - v86util.read_sized_string_from_mem = function read_sized_string_from_mem(mem, offset, len) - { - offset >>>= 0; - len >>>= 0; - return String.fromCharCode(...new Uint8Array(mem.buffer, offset, len)); - }; - - /** - * @param {string} filename - * @param {Object} options - * @param {number=} n_tries - */ - function load_file(filename, options, n_tries) - { - var http = new XMLHttpRequest(); - - http.open(options.method || "get", filename, true); - - if(options.as_json) - { - http.responseType = "json"; - } - else - { - http.responseType = "arraybuffer"; - } - - if(options.headers) - { - var header_names = Object.keys(options.headers); - - for(var i = 0; i < header_names.length; i++) - { - var name = header_names[i]; - http.setRequestHeader(name, options.headers[name]); - } - } - - if(options.range) - { - let start = options.range.start; - let end = start + options.range.length - 1; - http.setRequestHeader("Range", "bytes=" + start + "-" + end); - - // Abort if server responds with complete file in response to range - // request, to prevent downloading large files from broken http servers - http.onreadystatechange = function() - { - if(http.status === 200) - { - http.abort(); - } - }; - } - - http.onload = function(e) - { - if(http.readyState === 4) - { - if(http.status !== 200 && http.status !== 206) - { - console.error("Loading the image " + filename + " failed (status %d)", http.status); - if(http.status >= 500 && http.status < 600) - { - retry(); - } - } - else if(http.response) - { - options.done && options.done(http.response, http); - } - } - }; - - http.onerror = function(e) - { - console.error("Loading the image " + filename + " failed", e); - retry(); - }; - - if(options.progress) - { - http.onprogress = function(e) - { - options.progress(e); - }; - } - - http.send(null); - - function retry() - { - const number_of_tries = n_tries || 0; - const timeout = [1, 1, 2, 3, 5, 8, 13, 21][number_of_tries] || 34; - setTimeout(() => { - load_file(filename, options, number_of_tries + 1); - }, 1000 * timeout); - } - } - - function load_file_nodejs(filename, options) - { - let fs = require("fs"); - - if(options.range) - { - dbg_assert(!options.as_json); - - fs["open"](filename, "r", (err, fd) => - { - if(err) throw err; - - let length = options.range.length; - var buffer = Buffer.allocUnsafe(length); - - fs["read"](fd, buffer, 0, length, options.range.start, (err, bytes_read) => - { - if(err) throw err; - - dbg_assert(bytes_read === length); - options.done && options.done(new Uint8Array(buffer)); - - fs["close"](fd, (err) => { - if(err) throw err; - }); - }); - }); - } - else - { - var o = { - encoding: options.as_json ? "utf-8" : null, - }; - - fs["readFile"](filename, o, function(err, data) - { - if(err) - { - console.log("Could not read file:", filename, err); - } - else - { - var result = data; - - if(options.as_json) - { - result = JSON.parse(result); - } - else - { - result = new Uint8Array(result).buffer; - } - - options.done(result); - } - }); - } - } -})(); diff --git a/src/lib.js b/src/lib.js index 9350032c..683bce71 100644 --- a/src/lib.js +++ b/src/lib.js @@ -531,3 +531,173 @@ v86util.Bitmap.prototype.get_buffer = function() { return this.view.buffer; }; + + +if(typeof XMLHttpRequest === "undefined") +{ + v86util.load_file = load_file_nodejs; +} +else +{ + v86util.load_file = load_file; +} + +/** + * @param {string} filename + * @param {Object} options + * @param {number=} n_tries + */ +function load_file(filename, options, n_tries) +{ + var http = new XMLHttpRequest(); + + http.open(options.method || "get", filename, true); + + if(options.as_json) + { + http.responseType = "json"; + } + else + { + http.responseType = "arraybuffer"; + } + + if(options.headers) + { + var header_names = Object.keys(options.headers); + + for(var i = 0; i < header_names.length; i++) + { + var name = header_names[i]; + http.setRequestHeader(name, options.headers[name]); + } + } + + if(options.range) + { + let start = options.range.start; + let end = start + options.range.length - 1; + http.setRequestHeader("Range", "bytes=" + start + "-" + end); + + // Abort if server responds with complete file in response to range + // request, to prevent downloading large files from broken http servers + http.onreadystatechange = function() + { + if(http.status === 200) + { + http.abort(); + } + }; + } + + http.onload = function(e) + { + if(http.readyState === 4) + { + if(http.status !== 200 && http.status !== 206) + { + console.error("Loading the image " + filename + " failed (status %d)", http.status); + if(http.status >= 500 && http.status < 600) + { + retry(); + } + } + else if(http.response) + { + options.done && options.done(http.response, http); + } + } + }; + + http.onerror = function(e) + { + console.error("Loading the image " + filename + " failed", e); + retry(); + }; + + if(options.progress) + { + http.onprogress = function(e) + { + options.progress(e); + }; + } + + http.send(null); + + function retry() + { + const number_of_tries = n_tries || 0; + const timeout = [1, 1, 2, 3, 5, 8, 13, 21][number_of_tries] || 34; + setTimeout(() => { + load_file(filename, options, number_of_tries + 1); + }, 1000 * timeout); + } +} + +function load_file_nodejs(filename, options) +{ + let fs = require("fs"); + + if(options.range) + { + dbg_assert(!options.as_json); + + fs["open"](filename, "r", (err, fd) => + { + if(err) throw err; + + let length = options.range.length; + var buffer = Buffer.allocUnsafe(length); + + fs["read"](fd, buffer, 0, length, options.range.start, (err, bytes_read) => + { + if(err) throw err; + + dbg_assert(bytes_read === length); + options.done && options.done(new Uint8Array(buffer)); + + fs["close"](fd, (err) => { + if(err) throw err; + }); + }); + }); + } + else + { + var o = { + encoding: options.as_json ? "utf-8" : null, + }; + + fs["readFile"](filename, o, function(err, data) + { + if(err) + { + console.log("Could not read file:", filename, err); + } + else + { + var result = data; + + if(options.as_json) + { + result = JSON.parse(result); + } + else + { + result = new Uint8Array(result).buffer; + } + + options.done(result); + } + }); + } +} + +// Reads len characters at offset from Memory object mem as a JS string +v86util.read_sized_string_from_mem = function read_sized_string_from_mem(mem, offset, len) +{ + offset >>>= 0; + len >>>= 0; + return String.fromCharCode(...new Uint8Array(mem.buffer, offset, len)); +};