Instance.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { V86 } from "./V86Wrapper.mjs";
  2. /**
  3. * Class representing an Instance of an emulator machine.
  4. */
  5. class Instance {
  6. /**
  7. * Create an Instance.
  8. * @param {Object} options - Options for configuring the instance.
  9. * @param {boolean} [options.term=true] - Terminal option.
  10. * @param {boolean} [options.screen=false] - Screen option.
  11. * @param {number} [options.memory=1024] - Memory size for the instance; must be power of two.
  12. * @param {HTMLElement} [options.spawnRoot=undefined] - Html element where instance should be spawned.
  13. * @param {boolean} [options.autoStart=true] - Whether to automatically start the instance.
  14. * @param {string} [options.remote="./"] - Remote URL, defaults to origin.
  15. * @param {string} [options.wsUrl=""] - Websocket URL option for communication with the outside world.
  16. * @throws Will throw an error if remote URL does not end with a slash. @throws Will throw an error if the amount of memory provided is not a power of two.
  17. */
  18. constructor(options) {
  19. const defaultOptions = {
  20. term: false,
  21. screen: false,
  22. memory: 1024,
  23. spawnRoot: undefined,
  24. autoStart: true,
  25. remote: "./",
  26. instanceName: [...Array(10)].map(() => Math.random().toString(36)[2]).join(''),
  27. wsUrl: "",
  28. };
  29. const instanceOptions = { ...defaultOptions, ...options };
  30. if (!instanceOptions.remote.endsWith('/'))
  31. throw new Error("Instance ctor: Remote URL must end with a slash");
  32. if (typeof self !== 'undefined' && self.crypto) {
  33. this.instanceID = self.crypto.randomUUID();
  34. } else {
  35. this.instanceID = "Node";
  36. }
  37. this.terminals = [];
  38. let v86Options = {
  39. wasm_path: instanceOptions.remote + "third-party/v86.wasm",
  40. preserve_mac_from_state_image: true,
  41. memory_size: instanceOptions.memory * 1024 * 1024,
  42. vga_memory_size: 8 * 1024 * 1024,
  43. initial_state: { url: instanceOptions.remote + "static/image.bin" },
  44. filesystem: { baseurl: instanceOptions.remote + "static/9p-rootfs/" },
  45. autostart: instanceOptions.autoStart,
  46. };
  47. if (!(instanceOptions.wsUrl === ""))
  48. v86Options.network_relay_url = instanceOptions.wsUrl;
  49. if (!((Math.log(v86Options.memory_size) / Math.log(2)) % 1 === 0))
  50. throw new Error("Instance ctor: Amount of memory provided isn't a power of two");
  51. if (instanceOptions.screen === true) {
  52. if (instanceOptions.spawnRoot === undefined)
  53. throw new Error("Instance ctor: spawnRoot is undefined, cannot continue")
  54. instanceOptions.spawnRoot.appendChild((() => {
  55. const div = document.createElement("div");
  56. div.setAttribute("id", instanceOptions.instanceName + '-screen');
  57. const child_div = document.createElement("div");
  58. child_div.setAttribute("style", "white-space: pre; font: 14px monospace; line-height: 14px");
  59. const canvas = document.createElement("canvas");
  60. canvas.setAttribute("style", "display: none");
  61. div.appendChild(child_div);
  62. div.appendChild(canvas);
  63. return div;
  64. })());
  65. v86Options.screen_container = document.getElementById(instanceOptions.instanceName + '-screen');
  66. }
  67. this.vm = new V86(v86Options);
  68. if (instanceOptions.term === true) {
  69. if (instanceOptions.spawnRoot === undefined)
  70. throw new Error("Instance ctor: spawnRoot is undefined, cannot continue")
  71. var term = new Terminal({
  72. allowTransparency: true,
  73. });
  74. instanceOptions.spawnRoot.appendChild((() => {
  75. const div = document.createElement("div");
  76. div.setAttribute("id", instanceOptions.instanceName + '-terminal');
  77. return div;
  78. })());
  79. term.open(document.getElementById(instanceOptions.instanceName + '-terminal'));
  80. term.write("Now booting emu, please stand by ...");
  81. this.vm.add_listener("emulator-started", () => {
  82. // emulator.serial0_send("\nsh networking.sh > /dev/null 2>&1 &\n\n");
  83. // emulator.serial0_send("clear\n");
  84. term.write("Welcome to psl!");
  85. this.vm.serial0_send("\n");
  86. });
  87. this.vm.add_listener("serial0-output-byte", (byte) => {
  88. var chr = String.fromCharCode(byte);
  89. if (chr <= "~") {
  90. term.write(chr);
  91. }
  92. });
  93. term.onData(data => {
  94. this.vm.serial0_send(data);
  95. });
  96. }
  97. }
  98. }
  99. export default Instance