nodejs-loader.mjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import vm from "node:vm";
  2. import url from "node:url";
  3. import fs from "node:fs";
  4. import path from "node:path";
  5. import crypto from "node:crypto";
  6. import perf_hooks from "node:perf_hooks";
  7. const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
  8. let files = [
  9. "src/const.js",
  10. "src/config.js",
  11. "src/log.js",
  12. "src/cpu.js",
  13. "src/debug.js",
  14. "src/io.js",
  15. "src/main.js",
  16. "src/lib.js",
  17. "src/buffer.js",
  18. "src/ide.js",
  19. "src/pci.js",
  20. "src/floppy.js",
  21. "src/memory.js",
  22. "src/dma.js",
  23. "src/pit.js",
  24. "src/vga.js",
  25. "src/ps2.js",
  26. "src/rtc.js",
  27. "src/uart.js",
  28. "src/acpi.js",
  29. "src/apic.js",
  30. "src/ioapic.js",
  31. "src/state.js",
  32. "src/ne2k.js",
  33. "src/sb16.js",
  34. "src/virtio.js",
  35. "src/virtio_console.js",
  36. //"src/virtio_net.js",
  37. //"src/virtio_balloon.js",
  38. "src/bus.js",
  39. "src/debug.js",
  40. "src/elf.js",
  41. "src/kernel.js",
  42. "lib/9p.js",
  43. "lib/filesystem.js",
  44. "lib/jor1k.js",
  45. "lib/marshall.js",
  46. "lib/utf8.js",
  47. "src/browser/screen.js",
  48. "src/browser/keyboard.js",
  49. "src/browser/mouse.js",
  50. "src/browser/speaker.js",
  51. "src/browser/serial.js",
  52. "src/browser/network.js",
  53. "src/browser/fake_network.js",
  54. "src/browser/fetch_network.js",
  55. "src/browser/starter.js",
  56. "src/browser/worker_bus.js",
  57. "src/browser/dummy_screen.js",
  58. "src/browser/print_stats.js",
  59. "src/browser/filestorage.js"
  60. ];
  61. let globals = Object.create(globalThis);
  62. let v86 = {};
  63. let ctx = vm.createContext(globals);
  64. globals.DEBUG = false;
  65. globals.module = {exports:v86};
  66. Object.defineProperty(globals, "crypto", {value: crypto});
  67. globals.require = (what) => {
  68. return ({
  69. perf_hooks,
  70. fs
  71. })[what];
  72. };
  73. for( let f of files ) {
  74. vm.runInContext(fs.readFileSync(path.join(__dirname, f), "utf8"), ctx, {
  75. filename: f
  76. });
  77. }
  78. export let {
  79. V86,
  80. CPU,
  81. FetchNetworkAdapter,
  82. MemoryFileStorage,
  83. ServerFileStorageWrapper,
  84. } = globals;