Skip to content

Commit ca47ddc

Browse files
Merge pull request RustPython#672 from coolreader18/wasm-vm-exec_single
Use compile::Mode::Single for the demo terminal
2 parents 5b5d3e7 + 913bb14 commit ca47ddc

File tree

3 files changed

+43
-47
lines changed

3 files changed

+43
-47
lines changed

vm/src/frame.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -691,13 +691,10 @@ impl Frame {
691691
let expr = self.pop_value();
692692
if !expr.is(&vm.get_none()) {
693693
let repr = vm.to_repr(&expr)?;
694-
builtins::builtin_print(
695-
vm,
696-
PyFuncArgs {
697-
args: vec![repr],
698-
kwargs: vec![],
699-
},
700-
)?;
694+
// TODO: implement sys.displayhook
695+
if let Some(print) = vm.ctx.get_attr(&vm.builtins, "print") {
696+
vm.invoke(print, vec![repr])?;
697+
}
701698
}
702699
Ok(None)
703700
}

wasm/demo/src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ term.on('data', data => {
113113
} else {
114114
term.write('\r\n');
115115
try {
116-
terminalVM.exec(input);
116+
terminalVM.execSingle(input);
117117
} catch (err) {
118118
if (err instanceof WebAssembly.RuntimeError) {
119119
err = window.__RUSTPYTHON_ERROR || err;

wasm/lib/src/vm_class.rs

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -266,46 +266,40 @@ impl WASMVirtualMachine {
266266

267267
#[wasm_bindgen(js_name = setStdout)]
268268
pub fn set_stdout(&self, stdout: JsValue) -> Result<(), JsValue> {
269-
self.with(
270-
move |StoredVirtualMachine {
271-
ref mut vm,
272-
ref mut scope,
273-
..
274-
}| {
275-
fn error() -> JsValue {
276-
TypeError::new("Unknown stdout option, please pass a function or 'console'")
277-
.into()
278-
}
279-
let print_fn: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult> =
280-
if let Some(s) = stdout.as_string() {
281-
match s.as_str() {
282-
"console" => Box::new(wasm_builtins::builtin_print_console),
283-
_ => return Err(error()),
284-
}
285-
} else if stdout.is_function() {
286-
let func = js_sys::Function::from(stdout);
287-
Box::new(
288-
move |vm: &mut VirtualMachine, args: PyFuncArgs| -> PyResult {
289-
func.call1(
290-
&JsValue::UNDEFINED,
291-
&wasm_builtins::format_print_args(vm, args)?.into(),
292-
)
293-
.map_err(|err| convert::js_to_py(vm, err))?;
294-
Ok(vm.get_none())
295-
},
296-
)
297-
} else if stdout.is_undefined() || stdout.is_null() {
298-
fn noop(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
269+
self.with(move |StoredVirtualMachine { ref mut vm, .. }| {
270+
fn error() -> JsValue {
271+
TypeError::new("Unknown stdout option, please pass a function or 'console'").into()
272+
}
273+
let print_fn: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult> =
274+
if let Some(s) = stdout.as_string() {
275+
match s.as_str() {
276+
"console" => Box::new(wasm_builtins::builtin_print_console),
277+
_ => return Err(error()),
278+
}
279+
} else if stdout.is_function() {
280+
let func = js_sys::Function::from(stdout);
281+
Box::new(
282+
move |vm: &mut VirtualMachine, args: PyFuncArgs| -> PyResult {
283+
func.call1(
284+
&JsValue::UNDEFINED,
285+
&wasm_builtins::format_print_args(vm, args)?.into(),
286+
)
287+
.map_err(|err| convert::js_to_py(vm, err))?;
299288
Ok(vm.get_none())
300-
}
301-
Box::new(noop)
302-
} else {
303-
return Err(error());
304-
};
305-
scope.store_name(&vm, "print", vm.ctx.new_rustfunc(print_fn));
306-
Ok(())
307-
},
308-
)?
289+
},
290+
)
291+
} else if stdout.is_undefined() || stdout.is_null() {
292+
fn noop(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
293+
Ok(vm.get_none())
294+
}
295+
Box::new(noop)
296+
} else {
297+
return Err(error());
298+
};
299+
let rustfunc = vm.ctx.new_rustfunc(print_fn);
300+
vm.ctx.set_attr(&vm.builtins, "print", rustfunc);
301+
Ok(())
302+
})?
309303
}
310304

311305
#[wasm_bindgen(js_name = injectModule)]
@@ -395,4 +389,9 @@ impl WASMVirtualMachine {
395389
pub fn eval(&self, source: String) -> Result<JsValue, JsValue> {
396390
self.run(source, compile::Mode::Eval)
397391
}
392+
393+
#[wasm_bindgen(js_name = execSingle)]
394+
pub fn exec_single(&self, source: String) -> Result<JsValue, JsValue> {
395+
self.run(source, compile::Mode::Single)
396+
}
398397
}

0 commit comments

Comments
 (0)