From 4e0c425fa05f9eb90366e6e8a95799624fd11198 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 28 Feb 2022 11:05:11 -0500 Subject: [PATCH] forgot to enable something + better debug --- src/function.rs | 15 ++++++++++----- src/lib.rs | 25 +++++++++---------------- src/misc.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/function.rs b/src/function.rs index 8dd9db5..b2ace40 100644 --- a/src/function.rs +++ b/src/function.rs @@ -1,3 +1,4 @@ +use crate::misc::debug_log; use eframe::egui::{plot::Value, widgets::plot::Bar}; use meval::Expr; @@ -159,10 +160,8 @@ impl Function { self.min_x = min_x; self.max_x = max_x; self.pixel_width = pixel_width; - } else if ((min_x != self.min_x) | (max_x != self.max_x)) - && self.back_cache.is_some() - && false - { + } else if ((min_x != self.min_x) | (max_x != self.max_x)) && self.back_cache.is_some() { + debug_log("back_cache: partial regen"); let range_new: f64 = max_x.abs() + min_x.abs(); let resolution: f64 = (self.pixel_width as f64 / range_new) as f64; @@ -207,8 +206,12 @@ impl Function { pub fn run(&mut self) -> FunctionOutput { let front_values: Vec = match self.back_cache.is_some() { - true => self.back_cache.as_ref().expect("").clone(), + true => { + debug_log("back_cache: using"); + self.back_cache.as_ref().expect("").clone() + } false => { + debug_log("back_cache: regen"); let absrange = (self.max_x - self.min_x).abs(); let resolution: f64 = (self.pixel_width as f64 / absrange) as f64; let front_data: Vec = (1..=self.pixel_width) @@ -226,11 +229,13 @@ impl Function { if self.integral { let back_bars: (Vec, f64) = match self.front_cache.is_some() { true => { + debug_log("front_cache: using"); let cache = self.front_cache.as_ref().expect(""); let vec_bars: Vec = cache.0.to_vec(); (vec_bars, cache.1) } false => { + debug_log("front_cache: regen"); let (data, area) = self.integral_rectangles(); let bars: Vec = data.iter().map(|(x, y)| Bar::new(*x, *y)).collect(); diff --git a/src/lib.rs b/src/lib.rs index 7f3cf3a..8045494 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,8 @@ mod egui_app; mod function; mod misc; +#[cfg(target_arch = "wasm32")] +use misc::log_helper; #[cfg(target_arch = "wasm32")] use wasm_bindgen::prelude::*; @@ -12,21 +14,12 @@ use wasm_bindgen::prelude::*; #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; -#[cfg(target_arch = "wasm32")] -#[wasm_bindgen] -extern "C" { - // Use `js_namespace` here to bind `console.log(..)` instead of just - // `log(..)` - #[wasm_bindgen(js_namespace = console)] - fn log(s: &str); -} - #[cfg(target_arch = "wasm32")] #[cfg(debug_assertions)] fn init_tracing_wasm() { - log("Initializing tracing_wasm..."); + log_helper("Initializing tracing_wasm..."); tracing_wasm::set_as_global_default(); - log("Initialized tracing_wasm!"); + log_helper("Initialized tracing_wasm!"); } #[cfg(target_arch = "wasm32")] @@ -36,19 +29,19 @@ fn init_tracing_wasm() {} #[cfg(target_arch = "wasm32")] #[wasm_bindgen] pub fn start(canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> { - log("Initializing..."); + log_helper("Initializing..."); // See performance in browser profiler! init_tracing_wasm(); // Used in order to hook into `panic!()` to log in the browser's console - log("Initializing console_error_panic_hook..."); + log_helper("Initializing console_error_panic_hook..."); std::panic::set_hook(Box::new(console_error_panic_hook::hook)); - log("Initialized console_error_panic_hook!"); + log_helper("Initialized console_error_panic_hook!"); - log("Finished initializing!"); + log_helper("Finished initializing!"); - log("Starting App..."); + log_helper("Starting App..."); let app = egui_app::MathApp::default(); eframe::start_web(canvas_id, Box::new(app)) } diff --git a/src/misc.rs b/src/misc.rs index d0b0ef5..00094b8 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -1,5 +1,37 @@ use meval::Expr; +#[cfg(target_arch = "wasm32")] +use wasm_bindgen::prelude::*; +#[cfg(target_arch = "wasm32")] +#[wasm_bindgen] +extern "C" { + // Use `js_namespace` here to bind `console.log(..)` instead of just + // `log(..)` + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +#[allow(dead_code)] +pub fn log_helper(s: &str) { + #[cfg(target_arch = "wasm32")] + log(s); + + #[cfg(not(target_arch = "wasm32"))] + println!("{}", s); +} + +#[cfg(debug_assertions)] +pub fn debug_log(s: &str) { + #[cfg(target_arch = "wasm32")] + log(s); + + #[cfg(not(target_arch = "wasm32"))] + println!("{}", s); +} + +#[cfg(not(debug_assertions))] +pub fn debug_log(_s: &str) {} + /* EXTREMELY Janky function that tries to put asterisks in the proper places to be parsed. This is so cursed. But it works, and I hopefully won't ever have to touch it again. One limitation though, variables with multiple characters like `pi` cannot be multiplied (like `pipipipi` won't result in `pi*pi*pi*pi`). But that's such a niche use case (and that same thing could be done by using exponents) that it doesn't really matter.