From d2f86799e9e9045b2d94012290909066cc733704 Mon Sep 17 00:00:00 2001 From: Fabian Date: Mon, 15 Oct 2018 12:33:52 -0300 Subject: [PATCH] Simplify load_from_json: Accept json object directly, don't run asynchronously --- lib/filesystem.js | 37 +++++++++++-------------------------- src/browser/lib.js | 10 +++++++++- src/browser/starter.js | 8 ++++---- tests/devices/virtio_9p.js | 2 +- 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 0d5b0b94..963b31ee 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -165,40 +165,25 @@ FS.prototype.load_from_json = function(fs, done) console.assert(fs, "Invalid fs passed to load_from_json"); } - //console.time("parse"); - var fsdata = JSON.parse(fs); - //console.timeEnd("parse"); - - if(fsdata["version"] !== JSONFS_VERSION) + if(fs["version"] !== JSONFS_VERSION) { throw "The filesystem JSON format has changed. " + "Please update your fs2json (https://github.com/copy/fs2json) and recreate the filesystem JSON."; } - var fsroot = fsdata["fsroot"]; - this.used_size = fsdata["size"]; + var fsroot = fs["fsroot"]; + this.used_size = fs["size"]; - var me = this; + for(var i = 0; i < fsroot.length; i++) { + this.LoadRecursive(fsroot[i], 0); + } - setTimeout(function() - { - //console.time("Load"); - //console.profile("Load"); - for(var i = 0; i < fsroot.length; i++) { - me.LoadRecursive(fsroot[i], 0); - } - //console.profileEnd("Load"); - //console.timeEnd("Load"); + //if(DEBUG) + //{ + // this.Check(); + //} - //if(DEBUG) - //{ - // console.time("Check"); - // me.Check(); - // console.timeEnd("Check"); - //} - - done(); - }, 0); + done && done(); }; FS.prototype.LoadRecursive = function(data, parentid) diff --git a/src/browser/lib.js b/src/browser/lib.js index 80388df7..2132418d 100644 --- a/src/browser/lib.js +++ b/src/browser/lib.js @@ -53,7 +53,15 @@ var ASYNC_SAFE = false; http.open(options.method || "get", filename, true); - if(!options.as_text) + if(options.as_text) + { + // the default + } + else if(options.as_json) + { + http.responseType = "json"; + } + else { http.responseType = "arraybuffer"; } diff --git a/src/browser/starter.js b/src/browser/starter.js index 006d71b3..6c45d1e4 100644 --- a/src/browser/starter.js +++ b/src/browser/starter.js @@ -475,7 +475,7 @@ V86Starter.prototype.continue_init = function(emulator, options) name: "fs9p_json", url: fs_url, size: size, - as_text: true, + as_json: true, }); } } @@ -507,7 +507,7 @@ V86Starter.prototype.continue_init = function(emulator, options) v86util.load_file(f.url, { done: function(result) { - put_on_settings.call(this, f.name, f.as_text ? result : new SyncBuffer(result)); + put_on_settings.call(this, f.name, f.as_json ? result : new SyncBuffer(result)); cont(index + 1); }.bind(this), progress: function progress(e) @@ -534,7 +534,7 @@ V86Starter.prototype.continue_init = function(emulator, options) }); } }, - as_text: f.as_text, + as_json: f.as_json, }); } }.bind(this); @@ -1083,7 +1083,7 @@ V86Starter.prototype.mount_fs = function(path, baseurl, basefs, callback) }; if(baseurl) { - dbg_assert(typeof basefs === "string", "Filesystem: basefs must be a JSON string"); + dbg_assert(typeof basefs === "object", "Filesystem: basefs must be a JSON object"); newfs.load_from_json(basefs, () => mount()); } else diff --git a/tests/devices/virtio_9p.js b/tests/devices/virtio_9p.js index 79a77427..ce3e48c2 100755 --- a/tests/devices/virtio_9p.js +++ b/tests/devices/virtio_9p.js @@ -5,7 +5,7 @@ process.on("unhandledRejection", exn => { throw exn; }); const V86 = require("../../build/libv86-debug.js").V86; const fs = require("fs"); -const testfsjson = JSON.stringify(require('./testfs.json')); +const testfsjson = require("./testfs.json"); const SHOW_LOGS = false; function log_pass(msg, ...args)