Skip to content

Commit 2183854

Browse files
committed
Make WebTerminal own the console as a member
This will be necessary to expose certain properties of the generated console back to the UI, before calling into the REPL.
1 parent 1019a5c commit 2183854

3 files changed

Lines changed: 28 additions & 18 deletions

File tree

web/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ term.loadAddon(fitAddon);
3333
term.open(document.getElementById('terminal'));
3434
fitAddon.fit();
3535

36-
var wt = new endbasic_web.WebTerminal();
36+
var wt = new endbasic_web.WebTerminal(term);
3737

3838
var UA = navigator.userAgent;
3939
var isMobile = (
@@ -67,4 +67,4 @@ if (isMobile) {
6767
}
6868

6969
term.focus();
70-
wt.run_repl_loop(term);
70+
wt.run_repl_loop();

web/src/input.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ fn on_key_event_into_key(event: OnKeyEvent) -> Key {
6161
/// Interface to implement an on-screen keyboard to provide keys that may not be available on
6262
/// mobile keyboards.
6363
#[wasm_bindgen]
64+
#[derive(Clone)]
6465
pub struct OnScreenKeyboard {
6566
on_key_tx: Sender<Key>,
6667
}

web/src/lib.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use wasm_bindgen::prelude::*;
3333
use wasm_bindgen::JsCast;
3434
#[cfg(test)]
3535
use wasm_bindgen_test::wasm_bindgen_test_configure;
36-
use xterm_js_rs::Terminal;
36+
use xterm_js_rs::{OnKeyEvent, Terminal};
3737

3838
#[cfg(test)]
3939
wasm_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]
9494
pub 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]
99101
impl 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

Comments
 (0)