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
12 changes: 8 additions & 4 deletions wasm/demo/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ function runCodeFromTextarea() {
if (result !== null) {
consoleElement.value += `\n${result}\n`;
}
} catch (e) {
errorElement.textContent = e;
console.error(e);
} catch (err) {
if (err instanceof WebAssembly.RuntimeError) {
err = window.__RUSTPYTHON_ERROR || err;
}
errorElement.textContent = err;
console.error(err);
}
}

Expand All @@ -61,4 +64,5 @@ snippets.addEventListener('change', () => {
runCodeFromTextarea();
});

runCodeFromTextarea(); // Run once for demo
// Run once for demo
runCodeFromTextarea();
21 changes: 19 additions & 2 deletions wasm/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,34 @@ extern crate wasm_bindgen_futures;
extern crate web_sys;

use js_sys::{Object, Reflect, TypeError};
use std::panic;
use wasm_bindgen::prelude::*;

pub use crate::vm_class::*;

const PY_EVAL_VM_ID: &str = "__py_eval_vm";

extern crate console_error_panic_hook;
fn panic_hook(info: &panic::PanicInfo) {
// If something errors, just ignore it; we don't want to panic in the panic hook
use js_sys::WebAssembly::RuntimeError;
let window = match web_sys::window() {
Some(win) => win,
None => return,
};
let msg = &info.to_string();
let _ = Reflect::set(&window, &"__RUSTPYTHON_ERROR_MSG".into(), &msg.into());
let error = RuntimeError::new(&msg);
let _ = Reflect::set(&window, &"__RUSTPYTHON_ERROR".into(), &error);
let stack = match Reflect::get(&error, &"stack".into()) {
Ok(stack) => stack,
Err(_) => return,
};
let _ = Reflect::set(&window, &"__RUSTPYTHON_ERROR_STACK".into(), &stack.into());
}

#[wasm_bindgen(start)]
pub fn setup_console_error() {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
std::panic::set_hook(Box::new(panic_hook));
}

// Hack to comment out wasm-bindgen's generated typescript definitons
Expand Down