@@ -33,7 +33,7 @@ use wasm_bindgen::prelude::*;
3333use wasm_bindgen:: JsCast ;
3434#[ cfg( test) ]
3535use wasm_bindgen_test:: wasm_bindgen_test_configure;
36- use xterm_js_rs:: Terminal ;
36+ use xterm_js_rs:: { OnKeyEvent , Terminal } ;
3737
3838#[ cfg( test) ]
3939wasm_bindgen_test_configure ! ( run_in_browser) ;
@@ -92,31 +92,37 @@ fn setup_storage(storage: &mut endbasic_std::storage::Storage) {
9292/// Connects the EndBASIC interpreter to a web page.
9393#[ wasm_bindgen]
9494pub struct WebTerminal {
95- input : WebInput ,
95+ console : Rc < RefCell < dyn Console > > ,
96+ _on_key_callback : Closure < dyn FnMut ( OnKeyEvent ) > ,
97+ on_screen_keyboard : OnScreenKeyboard ,
9698}
9799
98100#[ wasm_bindgen]
99101impl WebTerminal {
100102 /// Creates a new instance of the `WebTerminal`.
101103 #[ wasm_bindgen( constructor) ]
102- #[ allow( clippy:: new_without_default) ] // Cannot implement Default in wasm-bindgen.
103- pub fn new ( ) -> Self {
104- Self { input : WebInput :: default ( ) }
104+ pub fn new ( terminal : Terminal ) -> Self {
105+ let input = WebInput :: default ( ) ;
106+
107+ let on_key_callback = input. terminal_on_key ( ) ;
108+ terminal. on_key ( on_key_callback. as_ref ( ) . unchecked_ref ( ) ) ;
109+
110+ let on_screen_keyboard = input. on_screen_keyboard ( ) ;
111+
112+ let console = Rc :: from ( RefCell :: from ( XtermJsConsole :: new ( terminal, input) ) ) ;
113+
114+ Self { console, _on_key_callback : on_key_callback, on_screen_keyboard }
105115 }
106116
107117 /// Generates a new `OnScreenKeyboard` that can inject keypresses into this terminal.
108118 pub fn on_screen_keyboard ( & self ) -> OnScreenKeyboard {
109- self . input . on_screen_keyboard ( )
119+ self . on_screen_keyboard . clone ( )
110120 }
111121
112122 /// Starts the EndBASIC interpreter loop on the specified `terminal`.
113- pub async fn run_repl_loop ( self , terminal : Terminal ) {
114- let on_key_callback = self . input . terminal_on_key ( ) ;
115- terminal. on_key ( on_key_callback. as_ref ( ) . unchecked_ref ( ) ) ;
116-
117- let console = Rc :: from ( RefCell :: from ( XtermJsConsole :: new ( terminal, self . input ) ) ) ;
123+ pub async fn run_repl_loop ( self ) {
118124 let mut builder = endbasic_std:: MachineBuilder :: default ( )
119- . with_console ( console. clone ( ) )
125+ . with_console ( self . console . clone ( ) )
120126 . with_sleep_fn ( Box :: from ( js_sleep) )
121127 . make_interactive ( )
122128 . with_program ( Rc :: from ( RefCell :: from ( endbasic_repl:: editor:: Editor :: default ( ) ) ) ) ;
@@ -127,12 +133,15 @@ impl WebTerminal {
127133 setup_storage ( & mut storage. borrow_mut ( ) ) ;
128134
129135 let mut machine = builder. build ( ) . unwrap ( ) ;
130- endbasic_repl:: print_welcome ( console. clone ( ) ) . unwrap ( ) ;
131- endbasic_repl:: try_load_autoexec ( & mut machine, console. clone ( ) , storage) . await . unwrap ( ) ;
136+ endbasic_repl:: print_welcome ( self . console . clone ( ) ) . unwrap ( ) ;
137+ endbasic_repl:: try_load_autoexec ( & mut machine, self . console . clone ( ) , storage)
138+ . await
139+ . unwrap ( ) ;
132140 loop {
133141 let result =
134- endbasic_repl:: run_repl_loop ( & mut machine, console. clone ( ) , program. clone ( ) ) . await ;
135- let mut console = console. borrow_mut ( ) ;
142+ endbasic_repl:: run_repl_loop ( & mut machine, self . console . clone ( ) , program. clone ( ) )
143+ . await ;
144+ let mut console = self . console . borrow_mut ( ) ;
136145 match result {
137146 Ok ( exit_code) => {
138147 console. print ( & format ! ( "Interpreter exited with code {}" , exit_code) )
0 commit comments