|
| 1 | +mod wasm_builtins; |
| 2 | + |
1 | 3 | extern crate rustpython_vm;
|
2 | 4 | extern crate wasm_bindgen;
|
3 |
| -use rustpython_vm::compile; |
| 5 | +extern crate web_sys; |
| 6 | + |
4 | 7 | use rustpython_vm::VirtualMachine;
|
| 8 | +use rustpython_vm::compile; |
| 9 | +use rustpython_vm::pyobject::AttributeProtocol; |
5 | 10 | use wasm_bindgen::prelude::*;
|
6 |
| - |
7 |
| -#[wasm_bindgen] |
8 |
| -extern "C" { |
9 |
| - // Use `js_namespace` here to bind `console.log(..)` instead of just |
10 |
| - // `log(..)` |
11 |
| - #[wasm_bindgen(js_namespace = console)] |
12 |
| - fn log(s: &str); |
13 |
| -} |
| 11 | +use web_sys::console; |
14 | 12 |
|
15 | 13 | #[wasm_bindgen]
|
16 | 14 | pub fn run_code(source: &str) -> () {
|
17 | 15 | //add hash in here
|
18 |
| - log("Running RustPython"); |
19 |
| - log(&source.to_string()); |
| 16 | + console::log_1(&"Running RustPython".into()); |
| 17 | + console::log_1(&"Running code:".into()); |
| 18 | + console::log_1(&source.to_string().into()); |
| 19 | + console::log_1(&"----- console -----".into()); |
| 20 | + |
20 | 21 | let mut vm = VirtualMachine::new();
|
| 22 | + // We are monkey-patching the builtin print to use console.log |
| 23 | + // TODO: moneky-patch sys.stdout instead, after print actually uses sys.stdout |
| 24 | + vm.builtins.set_attr("print", vm.context().new_rustfunc(wasm_builtins::builtin_print)); |
| 25 | + |
21 | 26 | let code_obj = compile::compile(&mut vm, &source.to_string(), compile::Mode::Exec, None);
|
| 27 | + |
22 | 28 | let builtins = vm.get_builtin_scope();
|
23 | 29 | let vars = vm.context().new_scope(Some(builtins));
|
24 | 30 | match vm.run_code_obj(code_obj.unwrap(), vars) {
|
25 |
| - Ok(_value) => log("Execution successful"), |
26 |
| - Err(_) => log("Execution failed"), |
| 31 | + Ok(_value) => console::log_1(&"Execution successful".into()), |
| 32 | + Err(_) => console::log_1(&"Execution failed".into()), |
27 | 33 | }
|
28 | 34 | }
|
0 commit comments