Simplify load_from_json: Accept json object directly, don't run asynchronously

This commit is contained in:
Fabian 2018-10-15 12:33:52 -03:00
parent 44bbeb20e6
commit d2f86799e9
4 changed files with 25 additions and 32 deletions

View file

@ -165,40 +165,25 @@ FS.prototype.load_from_json = function(fs, done)
console.assert(fs, "Invalid fs passed to load_from_json"); console.assert(fs, "Invalid fs passed to load_from_json");
} }
//console.time("parse"); if(fs["version"] !== JSONFS_VERSION)
var fsdata = JSON.parse(fs);
//console.timeEnd("parse");
if(fsdata["version"] !== JSONFS_VERSION)
{ {
throw "The filesystem JSON format has changed. " + throw "The filesystem JSON format has changed. " +
"Please update your fs2json (https://github.com/copy/fs2json) and recreate the filesystem JSON."; "Please update your fs2json (https://github.com/copy/fs2json) and recreate the filesystem JSON.";
} }
var fsroot = fsdata["fsroot"]; var fsroot = fs["fsroot"];
this.used_size = fsdata["size"]; this.used_size = fs["size"];
var me = this; for(var i = 0; i < fsroot.length; i++) {
this.LoadRecursive(fsroot[i], 0);
}
setTimeout(function() //if(DEBUG)
{ //{
//console.time("Load"); // this.Check();
//console.profile("Load"); //}
for(var i = 0; i < fsroot.length; i++) {
me.LoadRecursive(fsroot[i], 0);
}
//console.profileEnd("Load");
//console.timeEnd("Load");
//if(DEBUG) done && done();
//{
// console.time("Check");
// me.Check();
// console.timeEnd("Check");
//}
done();
}, 0);
}; };
FS.prototype.LoadRecursive = function(data, parentid) FS.prototype.LoadRecursive = function(data, parentid)

View file

@ -53,7 +53,15 @@ var ASYNC_SAFE = false;
http.open(options.method || "get", filename, true); 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"; http.responseType = "arraybuffer";
} }

View file

@ -475,7 +475,7 @@ V86Starter.prototype.continue_init = function(emulator, options)
name: "fs9p_json", name: "fs9p_json",
url: fs_url, url: fs_url,
size: size, size: size,
as_text: true, as_json: true,
}); });
} }
} }
@ -507,7 +507,7 @@ V86Starter.prototype.continue_init = function(emulator, options)
v86util.load_file(f.url, { v86util.load_file(f.url, {
done: function(result) 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); cont(index + 1);
}.bind(this), }.bind(this),
progress: function progress(e) 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); }.bind(this);
@ -1083,7 +1083,7 @@ V86Starter.prototype.mount_fs = function(path, baseurl, basefs, callback)
}; };
if(baseurl) 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()); newfs.load_from_json(basefs, () => mount());
} }
else else

View file

@ -5,7 +5,7 @@ process.on("unhandledRejection", exn => { throw exn; });
const V86 = require("../../build/libv86-debug.js").V86; const V86 = require("../../build/libv86-debug.js").V86;
const fs = require("fs"); const fs = require("fs");
const testfsjson = JSON.stringify(require('./testfs.json')); const testfsjson = require("./testfs.json");
const SHOW_LOGS = false; const SHOW_LOGS = false;
function log_pass(msg, ...args) function log_pass(msg, ...args)