From 3d0d3ef7a3fdba707b27e4fcb6d82b1473350c5b Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 25 Aug 2023 11:07:59 -0400 Subject: [PATCH] progress --- build.sh | 0 push.sh | 0 src/lib.rs | 42 +++++++++++++++++++++++++++++++++++++----- src/math_app.rs | 5 +++-- src/misc.rs | 4 ++-- start-server.sh | 0 tests/misc.rs | 2 +- 7 files changed, 43 insertions(+), 10 deletions(-) mode change 100644 => 100755 build.sh mode change 100644 => 100755 push.sh mode change 100644 => 100755 start-server.sh diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 diff --git a/push.sh b/push.sh old mode 100644 new mode 100755 diff --git a/src/lib.rs b/src/lib.rs index af54204..3ca4403 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,16 +45,48 @@ cfg_if::cfg_if! { #[global_allocator] static ALLOCATOR: LockedAllocator = LockedAllocator::new(FreeListAllocator::new()); + use eframe::WebRunner; + use tracing::metadata::LevelFilter; + #[derive(Clone)] + #[wasm_bindgen] + pub struct WebHandle { + runner: WebRunner, + } + + #[wasm_bindgen] + impl WebHandle { + /// Installs a panic hook, then returns. + #[allow(clippy::new_without_default)] + #[wasm_bindgen(constructor)] + pub fn new() -> Self { + // eframe::WebLogger::init(LevelFilter::Debug).ok(); + tracing_wasm::set_as_global_default(); + + Self { + runner: WebRunner::new(), + } + } + + /// Call this once from JavaScript to start your app. + #[wasm_bindgen] + pub async fn start(&self, canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> { + self.runner + .start( + canvas_id, + eframe::WebOptions::default(), + Box::new(|cc| Box::new(math_app::MathApp::new(cc))), + ) + .await + } + } + #[wasm_bindgen(start)] pub async fn start() { tracing::info!("Starting..."); - // Used in order to hook into `panic!()` to log in the browser's console - tracing_wasm::set_as_global_default(); - - eframe::start_web("canvas", eframe::WebOptions::default(), - Box::new(|cc| Box::new(math_app::MathApp::new(cc)))).await.unwrap(); + let web_handle = WebHandle::new(); + web_handle.start("canvas").await.unwrap() } } } diff --git a/src/math_app.rs b/src/math_app.rs index 8857faa..ea9b52a 100644 --- a/src/math_app.rs +++ b/src/math_app.rs @@ -383,10 +383,11 @@ impl MathApp { #[cfg(target_arch = "wasm32")] { tracing::info!("Saving function data"); - use crate::misc::{HashBytes, HashBytesHelper}; + use crate::misc::{hashed_storage_create, HashBytes}; let hash: HashBytes = unsafe { std::mem::transmute::<&str, HashBytes>(build::SHORT_COMMIT) }; - let saved_data = hash.hashed_storage_create( + let saved_data = hashed_storage_create( + hash, &bincode::serialize(&self.functions) .expect("unable to deserialize functions"), ); diff --git a/src/misc.rs b/src/misc.rs index 12605a3..0fe107a 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -164,8 +164,8 @@ pub const HASH_LENGTH: usize = 8; pub type HashBytes = [u8; HASH_LENGTH]; #[allow(dead_code)] -pub fn hashed_storage_create(hashbytes: &HashBytes, data: &[u8]) -> String { - unsafe { std::mem::transmute::, String>([hashbytes, data].concat()) } +pub fn hashed_storage_create(hashbytes: HashBytes, data: &[u8]) -> String { + unsafe { std::mem::transmute::, String>([hashbytes.to_vec(), data.to_vec()].concat()) } } #[allow(dead_code)] diff --git a/start-server.sh b/start-server.sh old mode 100644 new mode 100755 diff --git a/tests/misc.rs b/tests/misc.rs index 65c0fd1..3c0ca57 100644 --- a/tests/misc.rs +++ b/tests/misc.rs @@ -73,7 +73,7 @@ fn hashed_storage() { .as_slice() .try_into() .expect("cannot turn into [u8; 8]"); - let storage = hashed_storage_create(&storage_tmp, data.as_slice()); + let storage = hashed_storage_create(storage_tmp, data.as_slice()); let read = hashed_storage_read(&storage); assert_eq!(