Changes to module imports for parity between the browser and node, fixed browser and node examples

This commit is contained in:
lsileoni 2024-04-11 20:53:48 +03:00
parent a6b7ec5c66
commit 2b28388fa7
4 changed files with 65 additions and 15 deletions

View File

@ -1,4 +1,4 @@
import * as libv86 from '../third-party/libv86.js';
import { V86 } from "./V86Wrapper.mjs";
/**
* Class representing an Instance of an emulator machine.
*/
@ -17,7 +17,7 @@ class Instance {
*/
constructor(options) {
const defaultOptions = {
term: true,
term: false,
screen: false,
memory: 1024,
spawnRoot: undefined,
@ -29,7 +29,7 @@ class Instance {
const instanceOptions = { ...defaultOptions, ...options };
if (!instanceOptions.remote.endsWith('/'))
throw new Error("Remote URL must end with a slash");
throw new Error("Instance ctor: Remote URL must end with a slash");
if (typeof self !== 'undefined' && self.crypto) {
this.instanceID = self.crypto.randomUUID();
} else {
@ -48,8 +48,10 @@ class Instance {
if (!(instanceOptions.wsUrl === ""))
v86Options.network_relay_url = instanceOptions.wsUrl;
if (!((Math.log(v86Options.memory_size) / Math.log(2)) % 1 === 0))
throw new Error("Amount of memory provided isn't a power of two");
throw new Error("Instance ctor: Amount of memory provided isn't a power of two");
if (instanceOptions.screen === true) {
if (instanceOptions.spawnRoot === undefined)
throw new Error("Instance ctor: spawnRoot is undefined, cannot continue")
instanceOptions.spawnRoot.appendChild((() => {
const div = document.createElement("div");
div.setAttribute("id", instanceOptions.instanceName + '-screen');
@ -66,7 +68,36 @@ class Instance {
})());
v86Options.screen_container = document.getElementById(instanceOptions.instanceName + '-screen');
}
this.vm = new libv86.V86(v86Options);
this.vm = new V86(v86Options);
if (instanceOptions.term === true) {
if (instanceOptions.spawnRoot === undefined)
throw new Error("Instance ctor: spawnRoot is undefined, cannot continue")
var term = new Terminal({
allowTransparency: true,
});
instanceOptions.spawnRoot.appendChild((() => {
const div = document.createElement("div");
div.setAttribute("id", instanceOptions.instanceName + '-terminal');
return div;
})());
term.open(document.getElementById(instanceOptions.instanceName + '-terminal'));
term.write("Now booting emu, please stand by ...");
this.vm.add_listener("emulator-started", () => {
// emulator.serial0_send("\nsh networking.sh > /dev/null 2>&1 &\n\n");
// emulator.serial0_send("clear\n");
term.write("Welcome to psl!");
this.vm.serial0_send("\n");
});
this.vm.add_listener("serial0-output-byte", (byte) => {
var chr = String.fromCharCode(byte);
if (chr <= "~") {
term.write(chr);
}
});
term.onData(data => {
this.vm.serial0_send(data);
});
}
}
}

View File

@ -18,7 +18,7 @@ class InstanceManager {
*/
constructor(options) {
const defaultOptions = {
term: true,
term: false,
screen: false,
instanceName: "Host",
memory: 1024,
@ -32,15 +32,18 @@ class InstanceManager {
this.instanceNames = [];
this.curr_inst = 0;
this.instanceNames.push(instanceOptions.instanceName);
this.instances[instanceOptions.instanceName] = this.initInstance(instanceOptions);
this.instances[instanceOptions.instanceName] = new Instance(instanceOptions);
}
/**
* Initialize an instance with given options.
* Create an instance with given options and adds it to the pool of instances.
* @param {Object} options - Options for configuring the instance.
* @returns {Promise<Object>} - Resolves with the initialized instance.
*/
initInstance(options) {
return new Instance(options);
async createInstance(options) {
const instance = new Instance(options);
this.instanceNames.push(instance.instanceName);
this.instances[instance.instanceName] = instance;
return instance;
}
/**
* Continue running a suspended instance.

View File

@ -0,0 +1,16 @@
let V86;
if (typeof window !== 'undefined') {
V86 = window.V86;
} else {
try {
const { createRequire } = await import('module');
const require = createRequire(import.meta.url);
const NodeV86 = require("../third-party/libv86.js");
V86 = NodeV86.V86;
} catch (error) {
console.error('Failed to load V86 in Node.js environment:', error);
}
}
export { V86 };

View File

@ -14,19 +14,19 @@ import('./js/InstanceManager.mjs').then(module => {
manager.getInstanceByinstName("Host").then(result => {
const hostvm = result.vm;
hostvm.add_listener("emulator-started", function() {
hostvm.add_listener("emulator-started", () => {
process.stdout.write("Welcome to psl!");
hostvm.serial0_send("\n");
});
hostvm.add_listener("serial0-output-byte", function(byte) {
hostvm.add_listener("serial0-output-byte", (byte) => {
var chr = String.fromCharCode(byte);
if (chr <= "~") {
process.stdout.write(chr);
}
});
process.stdin.on("data", function(c) {
// ctrl d
process.stdin.on("data", (c) => {
if (c === "\u0004") {
hostvm.stop();
process.stdin.pause();
@ -36,7 +36,7 @@ import('./js/InstanceManager.mjs').then(module => {
}
});
}).catch(error => {
console.log(error);
console.error(error);
throw Error("Error in getting host inastance, quitting");
});
}).catch(error => {