Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPM and Typescript support #647

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ build/libv86.js: $(CLOSURE) src/*.js lib/*.js src/browser/*.js
--define=DEBUG=false\
$(CLOSURE_FLAGS)\
--compilation_level SIMPLE\
--output_wrapper ';(function(){%output%}).call(this);'\
--output_wrapper 'var v86=(function(){%output% return {v86,V86Starter,CPU,MemoryFileStorage,ServerFileStorageWrapper};}).call(this);if(typeof module !== "undefined" && typeof module.exports !== "undefined"){module.exports = v86;}'\
--js $(CORE_FILES)\
--js $(BROWSER_FILES)\
--js $(LIB_FILES)
Expand All @@ -147,7 +147,7 @@ build/libv86-debug.js: $(CLOSURE) src/*.js lib/*.js src/browser/*.js
$(CLOSURE_FLAGS)\
$(CLOSURE_READABLE)\
--compilation_level SIMPLE\
--output_wrapper ';(function(){%output%}).call(this);'\
--output_wrapper 'var v86=(function(){%output% return {v86,V86Starter,CPU,MemoryFileStorage,ServerFileStorageWrapper};}).call(this);if(typeof module !== "undefined" && typeof module.exports !== "undefined"){module.exports = v86;}'\
--js $(CORE_FILES)\
--js $(BROWSER_FILES)\
--js $(LIB_FILES)
Expand Down
51 changes: 51 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "v86",
"version": "0.0.1",
"description": "x86 virtualization in your browser, recompiling x86 to wasm on the fly",
"main": "build/libv86.js",
"directories": {
"doc": "docs",
"example": "examples",
"lib": "lib",
"test": "tests"
},
"scripts": {
"build": "npm run build:all_debug & npm run build:all",
"build:all_debug": "make all-debug",
"build:all": "make all",
"build:release": "make build/libv86.js build/v86.wasm build/v86-fallback.wasm build/capstone-x86.min.js build/libwabt.js",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are only for debugging, I don't think they should be included in a release build.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bundlers will parse the debug.js requires by defaults and would try to bundle those - if we don’t distribute them the bundlers would complain unless we add explicit externs, my thought here was is to make bundling succeed on defaults, but we can also do something so that the compiled code does not contain require() on those two libraries.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. If you add if(!DEBUG) return; to the beginning of the functions that call require that should cause closure compiler to remove those calls. Alternatively, I'd fine with removing the require call entirely. I haven't used those libraries from node in a long time anyway.

"test:prepare": "wget -nv -P images/ https://k.copy.sh/{linux.iso,linux3.iso,linux4.iso,buildroot-bzimage.bin,TinyCore-11.0.iso,oberon.img,msdos.img,openbsd-floppy.img,kolibri.img,windows101.img,os8.img,freedos722.img,mobius-fd-release5.img}",
"test": "make kvm-unit-test nasmtests nasmtests-force-jit expect-tests jitpagingtests qemutests qemutests-release rust-test tests"
},
"dependencies": {
},
"devDependencies": {
},
"files": [
"bios/*.bin",
"build/libv86.js",
"build/libv86-debug.js",
"build/v86.wasm",
"build/v86-debug.wasm",
"build/v86-fallback.wasm",
"build/capstone-x86.min.js",
"build/libwabt.js",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/copy/v86.git"
},
"keywords": [
"x86",
"virtualization",
"emulator"
],
"author": "Fabian Hemmer",
"license": "BSD-2-Clause",
"bugs": {
"url": "https://github.com/copy/v86/issues"
},
"homepage": "https://copy.sh/v86/"
}
12 changes: 6 additions & 6 deletions src/browser/filestorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ ServerFileStorageWrapper.prototype.uncache = function(sha256sum)
};

// Closure Compiler's way of exporting
if(typeof window !== "undefined")
{
window["MemoryFileStorage"] = MemoryFileStorage;
window["ServerFileStorageWrapper"] = ServerFileStorageWrapper;
}
else if(typeof module !== "undefined" && typeof module.exports !== "undefined")
if(typeof module !== "undefined" && typeof module.exports !== "undefined")
{
module.exports["MemoryFileStorage"] = MemoryFileStorage;
module.exports["ServerFileStorageWrapper"] = ServerFileStorageWrapper;
}
else if(typeof window !== "undefined")
{
window["MemoryFileStorage"] = MemoryFileStorage;
window["ServerFileStorageWrapper"] = ServerFileStorageWrapper;
}
else if(typeof importScripts === "function")
{
// web worker
Expand Down
104 changes: 50 additions & 54 deletions src/browser/starter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,50 +1119,50 @@ V86Starter.prototype.serial_send_bytes = function(serial, data)
* @param {function(Object)=} callback
* @export
*/
V86Starter.prototype.mount_fs = async function(path, baseurl, basefs, callback)
{
let file_storage = new MemoryFileStorage();

if(baseurl)
{
file_storage = new ServerFileStorageWrapper(file_storage, baseurl);
}
const newfs = new FS(file_storage, this.fs9p.qidcounter);
const mount = () =>
{
const idx = this.fs9p.Mount(path, newfs);
if(!callback)
{
return;
}
if(idx === -ENOENT)
{
callback(new FileNotFoundError());
}
else if(idx === -EEXIST)
{
callback(new FileExistsError());
}
else if(idx < 0)
{
dbg_assert(false, "Unexpected error code: " + (-idx));
callback(new Error("Failed to mount. Error number: " + (-idx)));
}
else
{
callback(null);
}
};
if(baseurl)
{
dbg_assert(typeof basefs === "object", "Filesystem: basefs must be a JSON object");
newfs.load_from_json(basefs, () => mount());
}
else
{
mount();
}
};
// V86Starter.prototype.mount_fs = async function(path, baseurl, basefs, callback)
// {
// let file_storage = new MemoryFileStorage();

// if(baseurl)
// {
// file_storage = new ServerFileStorageWrapper(file_storage, baseurl);
// }
// const newfs = new FS(file_storage, this.fs9p.qidcounter);
// const mount = () =>
// {
// const idx = this.fs9p.Mount(path, newfs);
// if(!callback)
// {
// return;
// }
// if(idx === -ENOENT)
// {
// callback(new FileNotFoundError());
// }
// else if(idx === -EEXIST)
// {
// callback(new FileExistsError());
// }
// else if(idx < 0)
// {
// dbg_assert(false, "Unexpected error code: " + (-idx));
// callback(new Error("Failed to mount. Error number: " + (-idx)));
// }
// else
// {
// callback(null);
// }
// };
// if(baseurl)
// {
// dbg_assert(typeof basefs === "object", "Filesystem: basefs must be a JSON object");
// newfs.load_from_json(basefs, () => mount());
// }
// else
// {
// mount();
// }
// };

/**
* Write to a file in the 9p filesystem. Nothing happens if no filesystem has
Expand Down Expand Up @@ -1349,19 +1349,15 @@ function FileNotFoundError(message)
FileNotFoundError.prototype = Error.prototype;

// Closure Compiler's way of exporting
if(typeof window !== "undefined")
{
window["V86Starter"] = V86Starter;
window["V86"] = V86Starter;
}
else if(typeof module !== "undefined" && typeof module.exports !== "undefined")
if(typeof module !== "undefined" && typeof module.exports !== "undefined")
{
module.exports["V86Starter"] = V86Starter;
module.exports["V86"] = V86Starter;
}
else if(typeof importScripts === "function")
{
} else if(typeof window !== "undefined"){
window["V86Starter"] = V86Starter;
window["V86"] = V86Starter;
} else if(typeof importScripts === "function") {
// web worker
self["V86Starter"] = V86Starter;
self["V86"] = V86Starter;
}
}
13 changes: 0 additions & 13 deletions src/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,6 @@ BusConnector.prototype.send = function(name, value, unused_transfer)
}
};

/**
* Send a message, guaranteeing that it is received asynchronously
*
* @param {string} name
* @param {Object=} value
*/
BusConnector.prototype.send_async = function(name, value)
{
dbg_assert(arguments.length === 1 || arguments.length === 2);

setTimeout(this.send.bind(this, name, value), 0);
};

Bus.create = function()
{
var c0 = new BusConnector();
Expand Down
8 changes: 4 additions & 4 deletions src/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -1508,13 +1508,13 @@ CPU.prototype.device_lower_irq = function(i)
};

// Closure Compiler's way of exporting
if(typeof window !== "undefined")
if(typeof module !== "undefined" && typeof module.exports !== "undefined")
{
window["CPU"] = CPU;
module.exports["CPU"] = CPU;
}
else if(typeof module !== "undefined" && typeof module.exports !== "undefined")
else if(typeof window !== "undefined")
{
module.exports["CPU"] = CPU;
window["CPU"] = CPU;
}
else if(typeof importScripts === "function")
{
Expand Down