Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,13 +687,10 @@ impl Frame {
let expr = self.pop_value();
if !expr.is(&vm.get_none()) {
let repr = vm.to_repr(&expr)?;
builtins::builtin_print(
vm,
PyFuncArgs {
args: vec![repr],
kwargs: vec![],
},
)?;
// TODO: implement sys.displayhook
if let Some(print) = vm.ctx.get_attr(&vm.builtins, "print") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pro tip: in case of an if-statement, do not forget about the else clause. What happens when we have no print? No worries for now, just a tip.

vm.invoke(print, vec![repr])?;
}
}
Ok(None)
}
Expand Down
2 changes: 1 addition & 1 deletion wasm/demo/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ term.on('data', data => {
} else {
term.write('\r\n');
try {
terminalVM.exec(input);
terminalVM.execSingle(input);
} catch (err) {
if (err instanceof WebAssembly.RuntimeError) {
err = window.__RUSTPYTHON_ERROR || err;
Expand Down
77 changes: 38 additions & 39 deletions wasm/lib/src/vm_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,46 +265,40 @@ impl WASMVirtualMachine {

#[wasm_bindgen(js_name = setStdout)]
pub fn set_stdout(&self, stdout: JsValue) -> Result<(), JsValue> {
self.with(
move |StoredVirtualMachine {
ref mut vm,
ref mut scope,
..
}| {
fn error() -> JsValue {
TypeError::new("Unknown stdout option, please pass a function or 'console'")
.into()
}
let print_fn: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult> =
if let Some(s) = stdout.as_string() {
match s.as_str() {
"console" => Box::new(wasm_builtins::builtin_print_console),
_ => return Err(error()),
}
} else if stdout.is_function() {
let func = js_sys::Function::from(stdout);
Box::new(
move |vm: &mut VirtualMachine, args: PyFuncArgs| -> PyResult {
func.call1(
&JsValue::UNDEFINED,
&wasm_builtins::format_print_args(vm, args)?.into(),
)
.map_err(|err| convert::js_to_py(vm, err))?;
Ok(vm.get_none())
},
)
} else if stdout.is_undefined() || stdout.is_null() {
fn noop(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
self.with(move |StoredVirtualMachine { ref mut vm, .. }| {
fn error() -> JsValue {
TypeError::new("Unknown stdout option, please pass a function or 'console'").into()
}
let print_fn: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult> =
if let Some(s) = stdout.as_string() {
match s.as_str() {
"console" => Box::new(wasm_builtins::builtin_print_console),
_ => return Err(error()),
}
} else if stdout.is_function() {
let func = js_sys::Function::from(stdout);
Box::new(
move |vm: &mut VirtualMachine, args: PyFuncArgs| -> PyResult {
func.call1(
&JsValue::UNDEFINED,
&wasm_builtins::format_print_args(vm, args)?.into(),
)
.map_err(|err| convert::js_to_py(vm, err))?;
Ok(vm.get_none())
}
Box::new(noop)
} else {
return Err(error());
};
scope.store_name(&vm, "print", vm.ctx.new_rustfunc(print_fn));
Ok(())
},
)?
},
)
} else if stdout.is_undefined() || stdout.is_null() {
fn noop(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
Ok(vm.get_none())
}
Box::new(noop)
} else {
return Err(error());
};
let rustfunc = vm.ctx.new_rustfunc(print_fn);
vm.ctx.set_attr(&vm.builtins, "print", rustfunc);
Ok(())
})?
}

#[wasm_bindgen(js_name = injectModule)]
Expand Down Expand Up @@ -394,4 +388,9 @@ impl WASMVirtualMachine {
pub fn eval(&self, source: String) -> Result<JsValue, JsValue> {
self.run(source, compile::Mode::Eval)
}

#[wasm_bindgen(js_name = execSingle)]
pub fn exec_single(&self, source: String) -> Result<JsValue, JsValue> {
self.run(source, compile::Mode::Single)
}
}