Skip to content

Commit dac7501

Browse files
committed
Remove builtin_print_html, move functionality to JS
1 parent 09ab6d1 commit dac7501

4 files changed

Lines changed: 11 additions & 41 deletions

File tree

wasm/demo/src/main.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ function runCodeFromTextarea() {
3636
const code = editor.getValue();
3737
try {
3838
const result = rp.pyEval(code, {
39-
stdout: consoleElement
39+
stdout: output => {
40+
const shouldScroll =
41+
consoleElement.scrollHeight - consoleElement.scrollTop ===
42+
consoleElement.clientHeight;
43+
consoleElement.value += output;
44+
if (shouldScroll) {
45+
consoleElement.scrollTop = consoleElement.scrollHeight;
46+
}
47+
}
4048
});
4149
if (result !== null) {
4250
consoleElement.value += `\n${result}\n`;

wasm/lib/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ features = [
2626
"console",
2727
"Document",
2828
"Element",
29-
"HtmlTextAreaElement",
3029
"Window",
3130
"Headers",
3231
"Request",

wasm/lib/src/vm_class.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ impl WASMVirtualMachine {
273273
}| {
274274
fn error() -> JsValue {
275275
TypeError::new(
276-
"Unknown stdout option, please pass a function, a textarea element, or \
277-
'console'",
276+
"Unknown stdout option, please pass a function or 'console'",
278277
)
279278
.into()
280279
}
@@ -284,13 +283,6 @@ impl WASMVirtualMachine {
284283
"console" => Box::new(wasm_builtins::builtin_print_console),
285284
_ => return Err(error()),
286285
}
287-
} else if let Some(element) = stdout.dyn_ref::<web_sys::HtmlTextAreaElement>() {
288-
let element = element.clone();
289-
Box::new(
290-
move |vm: &mut VirtualMachine, args: PyFuncArgs| -> PyResult {
291-
wasm_builtins::builtin_print_html(vm, args, &element)
292-
},
293-
)
294286
} else if stdout.is_function() {
295287
let func = js_sys::Function::from(stdout);
296288
Box::new(

wasm/lib/src/wasm_builtins.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,16 @@
44
//! desktop.
55
//! Implements functions listed here: https://docs.python.org/3/library/builtins.html.
66
7-
use crate::convert;
87
use js_sys::{self, Array};
98
use rustpython_vm::obj::{objstr, objtype};
109
use rustpython_vm::pyobject::{IdProtocol, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
1110
use rustpython_vm::VirtualMachine;
12-
use wasm_bindgen::prelude::*;
13-
use web_sys::{self, console, HtmlTextAreaElement};
11+
use web_sys::{self, console};
1412

1513
pub(crate) fn window() -> web_sys::Window {
1614
web_sys::window().expect("Window to be available")
1715
}
1816

19-
// The HTML id of the element element that act as our STDOUT
20-
21-
pub fn print_to_html(text: &str, element: &HtmlTextAreaElement) -> Result<(), JsValue> {
22-
let value = element.value();
23-
24-
let scroll_height = element.scroll_height();
25-
let scrolled_to_bottom = scroll_height - element.scroll_top() == element.client_height();
26-
27-
element.set_value(&format!("{}{}", value, text));
28-
29-
if scrolled_to_bottom {
30-
element.scroll_with_x_and_y(0.0, scroll_height.into());
31-
}
32-
33-
Ok(())
34-
}
35-
3617
pub fn format_print_args(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<String, PyObjectRef> {
3718
// Handle 'sep' kwarg:
3819
let sep_arg = args
@@ -85,16 +66,6 @@ pub fn format_print_args(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<St
8566
Ok(output)
8667
}
8768

88-
pub fn builtin_print_html(
89-
vm: &mut VirtualMachine,
90-
args: PyFuncArgs,
91-
element: &HtmlTextAreaElement,
92-
) -> PyResult {
93-
let output = format_print_args(vm, args)?;
94-
print_to_html(&output, element).map_err(|err| convert::js_to_py(vm, err))?;
95-
Ok(vm.get_none())
96-
}
97-
9869
pub fn builtin_print_console(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
9970
let arr = Array::new();
10071
for arg in args.args {

0 commit comments

Comments
 (0)