11#!/usr/bin/env node
2- "use strict" ;
2+ // "use strict";
33
4- const path = require ( "path" ) ;
4+ import { join } from "path" ;
55
66// TODO:
77// - Timeout
88
99console . log ( "Don't forget to run `make all` before running this script" ) ;
1010
11- var fs = require ( "fs" ) ;
12- var V86 = require ( "./../../../build/libv86.js" ) . V86 ;
11+ import { writeFile } from "fs" ;
12+ import { V86 } from "./../../../build/libv86.js" ;
1313
14- const V86_ROOT = path . join ( __dirname , "../../.." ) ;
14+ const V86_ROOT = join ( import . meta . dirname , "../../.." ) ;
1515
16- var OUTPUT_FILE = path . join ( V86_ROOT , "images/debian-state-base.bin" ) ;
16+ var OUTPUT_FILE = join ( V86_ROOT , "images/debian-state-base.bin" ) ;
1717
1818process . stdin . setRawMode ( true ) ;
1919process . stdin . resume ( ) ;
2020process . stdin . setEncoding ( "utf8" ) ;
2121process . stdin . on ( "data" , handle_key ) ;
2222
2323var emulator = new V86 ( {
24- bios : { url : path . join ( V86_ROOT , "/bios/seabios.bin" ) } ,
25- vga_bios : { url : path . join ( V86_ROOT , "/bios/vgabios.bin" ) } ,
24+ bios : { url : join ( V86_ROOT , "/bios/seabios.bin" ) } ,
25+ vga_bios : { url : join ( V86_ROOT , "/bios/vgabios.bin" ) } ,
2626 autostart : true ,
2727 memory_size : 1024 * 1024 * 1024 ,
2828 vga_memory_size : 8 * 1024 * 1024 ,
@@ -37,9 +37,9 @@ var emulator = new V86({
3737 cmdline : "rw init=/bin/systemd root=host9p console=ttyS0 spectre_v2=off pti=off page_poison=on" ,
3838 filesystem : {
3939 basefs : {
40- url : path . join ( V86_ROOT , "/images/debian-base-fs.json" ) ,
40+ url : join ( V86_ROOT , "/images/debian-base-fs.json" ) ,
4141 } ,
42- baseurl : path . join ( V86_ROOT , "/images/debian-9p-rootfs-flat/" ) ,
42+ baseurl : join ( V86_ROOT , "/images/debian-9p-rootfs-flat/" ) ,
4343 } ,
4444 screen_dummy : true ,
4545} ) ;
@@ -50,51 +50,43 @@ var boot_start = Date.now();
5050var serial_text = "" ;
5151let booted = false ;
5252
53- emulator . add_listener ( "serial0-output-byte" , function ( byte )
54- {
53+ emulator . add_listener ( "serial0-output-byte" , function ( byte ) {
5554 var c = String . fromCharCode ( byte ) ;
5655 process . stdout . write ( c ) ;
5756
5857 serial_text += c ;
5958
60- if ( ! booted && serial_text . endsWith ( "root@localhost:~# " ) )
61- {
59+ if ( ! booted && serial_text . endsWith ( "root@localhost:~# " ) ) {
6260 console . error ( "\nBooted in %d" , ( Date . now ( ) - boot_start ) / 1000 ) ;
6361 booted = true ;
6462
6563 // Sync and drop caches: Makes it safer to change the filesystem as fewer files are rendered
6664 // Disable kernel logging with dmesg (drop_caches outputs things to ttyS0 even if run it on another port)
6765 emulator . serial0_send ( "dmesg -n 1; sync; echo 3 >/proc/sys/vm/drop_caches; cd ~/tutorial; history -c\n" ) ;
6866
69- setTimeout ( async function ( )
70- {
71- const s = await emulator . save_state ( ) ;
72-
73- fs . writeFile ( OUTPUT_FILE , new Uint8Array ( s ) , function ( e )
74- {
75- if ( e ) throw e ;
76- console . error ( "Saved as " + OUTPUT_FILE ) ;
77- stop ( ) ;
78- } ) ;
79- } , 10 * 1000 ) ;
67+ setTimeout ( async function ( ) {
68+ const s = await emulator . save_state ( ) ;
69+
70+ writeFile ( OUTPUT_FILE , new Uint8Array ( s ) , function ( e ) {
71+ if ( e ) throw e ;
72+ console . error ( "Saved as " + OUTPUT_FILE ) ;
73+ stop ( ) ;
74+ } ) ;
75+ } , 10 * 1000 ) ;
8076 }
8177} ) ;
8278
83- function handle_key ( c )
84- {
85- if ( c === "\u0003" )
86- {
79+ function handle_key ( c ) {
80+ if ( c === "\u0003" ) {
8781 // ctrl c
8882 stop ( ) ;
8983 }
90- else
91- {
84+ else {
9285 emulator . serial0_send ( c ) ;
9386 }
9487}
9588
96- function stop ( )
97- {
89+ function stop ( ) {
9890 emulator . stop ( ) ;
9991 process . stdin . pause ( ) ;
10092}
0 commit comments