Skip to content

Commit bde86d3

Browse files
committed
Use compile::Mode::Single for the demo terminal
1 parent 83788b9 commit bde86d3

File tree

3 files changed

+77
-79
lines changed

3 files changed

+77
-79
lines changed

vm/src/frame.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,13 +687,10 @@ impl Frame {
687687
let expr = self.pop_value();
688688
if !expr.is(&vm.get_none()) {
689689
let repr = vm.to_repr(&expr)?;
690-
builtins::builtin_print(
691-
vm,
692-
PyFuncArgs {
693-
args: vec![repr],
694-
kwargs: vec![],
695-
},
696-
)?;
690+
// TODO: implement sys.displayhook
691+
if let Some(print) = vm.ctx.get_attr(&vm.builtins, "print") {
692+
vm.invoke(print, vec![repr])?;
693+
}
697694
}
698695
Ok(None)
699696
}

wasm/demo/src/main.js

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -74,57 +74,58 @@ snippets.addEventListener('change', updateSnippet);
7474
// option selected for the `select`, but the textarea won't be updated)
7575
updateSnippet();
7676

77-
const prompt = ">>>>> ";
77+
const prompt = '>>>>> ';
7878

7979
const term = new Terminal();
8080
term.open(document.getElementById('terminal'));
8181
term.write(prompt);
8282

8383
function removeNonAscii(str) {
84-
if ((str===null) || (str===''))
85-
return false;
86-
else
87-
str = str.toString();
84+
if (str === null || str === '') return false;
85+
else str = str.toString();
8886

8987
return str.replace(/[^\x20-\x7E]/g, '');
9088
}
9189

9290
function printToConsole(data) {
93-
term.write(removeNonAscii(data) + "\r\n");
91+
term.write(removeNonAscii(data) + '\r\n');
9492
}
9593

96-
const terminalVM = rp.vmStore.init("term_vm");
94+
const terminalVM = rp.vmStore.init('term_vm');
9795
terminalVM.setStdout(printToConsole);
9896

99-
var input = "";
100-
term.on("data", (data) => {
101-
const code = data.charCodeAt(0);
102-
if (code == 13) { // CR
103-
if (input[input.length - 1] == ':') {
104-
input += data
105-
term.write("\r\n.....");
106-
} else {
107-
term.write("\r\n");
108-
try {
109-
terminalVM.exec(input);
110-
} catch (err) {
111-
if (err instanceof WebAssembly.RuntimeError) {
112-
err = window.__RUSTPYTHON_ERROR || err;
97+
var input = '';
98+
term.on('data', data => {
99+
const code = data.charCodeAt(0);
100+
if (code == 13) {
101+
// CR
102+
if (input[input.length - 1] == ':') {
103+
input += data;
104+
term.write('\r\n.....');
105+
} else {
106+
term.write('\r\n');
107+
try {
108+
terminalVM.execSingle(input);
109+
} catch (err) {
110+
if (err instanceof WebAssembly.RuntimeError) {
111+
err = window.__RUSTPYTHON_ERROR || err;
112+
}
113+
printToConsole(err);
113114
}
114-
printToConsole(err);
115+
term.write(prompt);
116+
input = '';
115117
}
116-
term.write(prompt);
117-
input = "";
118-
}
119-
} else if (code == 127) {
120-
if (input.length > 0) {
121-
term.write("\b \b");
122-
input = input.slice(0, -1);
118+
} else if (code == 127) {
119+
if (input.length > 0) {
120+
term.write('\b \b');
121+
input = input.slice(0, -1);
122+
}
123+
} else if (code < 32 || code == 127) {
124+
// Control
125+
return;
126+
} else {
127+
// Visible
128+
term.write(data);
129+
input += data;
123130
}
124-
} else if (code < 32 || code == 127) { // Control
125-
return;
126-
} else { // Visible
127-
term.write(data);
128-
input += data;
129-
}
130131
});

wasm/lib/src/vm_class.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -265,43 +265,38 @@ impl WASMVirtualMachine {
265265

266266
#[wasm_bindgen(js_name = setStdout)]
267267
pub fn set_stdout(&self, stdout: JsValue) -> Result<(), JsValue> {
268-
self.with(
269-
move |StoredVirtualMachine {
270-
ref mut vm,
271-
ref mut scope,
272-
..
273-
}| {
274-
let print_fn: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult> =
275-
if let Some(selector) = stdout.as_string() {
276-
Box::new(
277-
move |vm: &mut VirtualMachine, args: PyFuncArgs| -> PyResult {
278-
wasm_builtins::builtin_print_html(vm, args, &selector)
279-
},
280-
)
281-
} else if stdout.is_function() {
282-
let func = js_sys::Function::from(stdout);
283-
Box::new(
284-
move |vm: &mut VirtualMachine, args: PyFuncArgs| -> PyResult {
285-
func.call1(
286-
&JsValue::UNDEFINED,
287-
&wasm_builtins::format_print_args(vm, args)?.into(),
288-
)
289-
.map_err(|err| convert::js_to_py(vm, err))?;
290-
Ok(vm.get_none())
291-
},
292-
)
293-
} else if stdout.is_undefined() || stdout.is_null() {
294-
Box::new(wasm_builtins::builtin_print_console)
295-
} else {
296-
return Err(TypeError::new(
297-
"stdout must be null, a function or a css selector",
298-
)
299-
.into());
300-
};
301-
scope.store_name(&vm, "print", vm.ctx.new_rustfunc(print_fn));
302-
Ok(())
303-
},
304-
)?
268+
self.with(move |StoredVirtualMachine { ref mut vm, .. }| {
269+
let print_fn: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult> =
270+
if let Some(selector) = stdout.as_string() {
271+
Box::new(
272+
move |vm: &mut VirtualMachine, args: PyFuncArgs| -> PyResult {
273+
wasm_builtins::builtin_print_html(vm, args, &selector)
274+
},
275+
)
276+
} else if stdout.is_function() {
277+
let func = js_sys::Function::from(stdout);
278+
Box::new(
279+
move |vm: &mut VirtualMachine, args: PyFuncArgs| -> PyResult {
280+
func.call1(
281+
&JsValue::UNDEFINED,
282+
&wasm_builtins::format_print_args(vm, args)?.into(),
283+
)
284+
.map_err(|err| convert::js_to_py(vm, err))?;
285+
Ok(vm.get_none())
286+
},
287+
)
288+
} else if stdout.is_undefined() || stdout.is_null() {
289+
Box::new(wasm_builtins::builtin_print_console)
290+
} else {
291+
return Err(TypeError::new(
292+
"stdout must be null, a function or a css selector",
293+
)
294+
.into());
295+
};
296+
let rustfunc = vm.ctx.new_rustfunc(print_fn);
297+
vm.ctx.set_attr(&vm.builtins, "print", rustfunc);
298+
Ok(())
299+
})?
305300
}
306301

307302
#[wasm_bindgen(js_name = injectModule)]
@@ -391,4 +386,9 @@ impl WASMVirtualMachine {
391386
pub fn eval(&self, source: String) -> Result<JsValue, JsValue> {
392387
self.run(source, compile::Mode::Eval)
393388
}
389+
390+
#[wasm_bindgen(js_name = execSingle)]
391+
pub fn exec_single(&self, source: String) -> Result<JsValue, JsValue> {
392+
self.run(source, compile::Mode::Single)
393+
}
394394
}

0 commit comments

Comments
 (0)