diff --git a/Cargo.lock b/Cargo.lock index 80373d4c1..5287f6b13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -172,6 +172,15 @@ dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "console_error_panic_hook" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "constant_time_eq" version = "0.1.3" @@ -723,6 +732,7 @@ name = "rustpython_wasm" version = "0.1.0" dependencies = [ "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "js-sys 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "rustpython_parser 0.0.1", "rustpython_vm 0.1.0", @@ -1151,6 +1161,7 @@ dependencies = [ "checksum cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "405216fd8fe65f718daa7102ea808a946b6ce40c742998fbfd3463645552de18" "checksum clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0f16b89cbb9ee36d87483dc939fe9f1e13c05898d56d7b230a0d4dff033a536" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6c5dd2c094474ec60a6acaf31780af270275e3153bafff2db5995b715295762e" "checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e" "checksum diff 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "3c2b69f912779fbb121ceb775d74d51e915af17aaebc38d28a592843a2dd0a3a" "checksum digest 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "00a49051fef47a72c9623101b19bd71924a45cca838826caae3eaa4d00772603" diff --git a/wasm/lib/Cargo.toml b/wasm/lib/Cargo.toml index bc2ee9d05..f7e2d1850 100644 --- a/wasm/lib/Cargo.toml +++ b/wasm/lib/Cargo.toml @@ -15,6 +15,7 @@ rustpython_vm = { path = "../../vm" } cfg-if = "0.1.2" wasm-bindgen = "0.2" js-sys = "0.3" +console_error_panic_hook = "0.1" [dependencies.web-sys] version = "0.3" diff --git a/wasm/lib/src/lib.rs b/wasm/lib/src/lib.rs index 158345798..558cef102 100644 --- a/wasm/lib/src/lib.rs +++ b/wasm/lib/src/lib.rs @@ -15,6 +15,15 @@ use wasm_bindgen::prelude::*; pub use vm_class::*; +#[cfg(debug_assertions)] +extern crate console_error_panic_hook; + +#[cfg(debug_assertions)] +#[wasm_bindgen(start)] +pub fn setup_console_error() { + console_error_panic_hook::set_once(); +} + // Hack to comment out wasm-bindgen's generated typescript definitons #[wasm_bindgen(typescript_custom_section)] const TS_CMT_START: &'static str = "/*"; diff --git a/wasm/lib/src/vm_class.rs b/wasm/lib/src/vm_class.rs index 92998b145..b4a5a2646 100644 --- a/wasm/lib/src/vm_class.rs +++ b/wasm/lib/src/vm_class.rs @@ -1,6 +1,6 @@ use convert; use js_sys::TypeError; -use rustpython_vm::{pyobject::PyObjectRef, VirtualMachine}; +use rustpython_vm::{compile, pyobject::PyObjectRef, VirtualMachine}; use std::cell::RefCell; use std::collections::HashMap; use wasm_bindgen::prelude::*; @@ -50,9 +50,9 @@ impl VMStore { }) } - pub fn destroy(id: &String) { + pub fn destroy(id: String) { STORED_VMS.with(|cell| { - cell.borrow_mut().remove(id); + cell.borrow_mut().remove(&id); }); } @@ -85,7 +85,7 @@ impl WASMVirtualMachine { pub fn destroy(&self) -> Result<(), JsValue> { self.assert_valid()?; - VMStore::destroy(&self.id); + VMStore::destroy(self.id.clone()); Ok(()) } @@ -99,8 +99,26 @@ impl WASMVirtualMachine { ref mut scope, } = vms.get_mut(&self.id).unwrap(); let value = convert::js_to_py(vm, value); - vm.ctx.set_attr(scope, &name, value); - }); - Ok(()) + vm.ctx.set_item(scope, &name, value); + Ok(()) + }) + } + + pub fn run(&self, mut source: String) -> Result { + self.assert_valid()?; + STORED_VMS.with(|cell| { + let mut vms = cell.borrow_mut(); + let StoredVirtualMachine { + ref mut vm, + ref mut scope, + } = vms.get_mut(&self.id).unwrap(); + source.push('\n'); + let code = compile::compile(vm, &source, compile::Mode::Single, None) + .map_err(|err| convert::py_str_err(vm, &err))?; + let result = vm + .run_code_obj(code, scope.clone()) + .map_err(|err| convert::py_str_err(vm, &err))?; + Ok(convert::py_to_js(vm, result)) + }) } }