From 614e486e0d989d253fde32bbd281c2b0d938adc6 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Thu, 1 Jun 2023 23:20:18 +0200 Subject: [PATCH 1/2] Make `main` optional when compiling to WASM Not sure this is idiomatic. However, I noticed, that when compiling to e.g. a yew app, which has a `main`, it conflicts with the existing `main` from `kord`. The double negation of `not("wasm_no_main")` is because I didn't want to make this a breaking change, which I guess a feature `"wasm_main"` would have been. Let me know if you prefer a feature `"wasm_main"` as part of the `default-features` perhaps, or something else entirely. --- Cargo.toml | 2 ++ src/wasm.rs | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5f133b0..fe3dfdc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,8 @@ ml_gpu = ["ml_train", "burn-tch"] wasm = ["rodio/wasm-bindgen", "wasm-bindgen", "wasm-bindgen-futures", "js-sys", "console_error_panic_hook", "wee_alloc", "gloo-timers"] +wasm_no_main = [] + plot = ["plotters"] [dependencies] diff --git a/src/wasm.rs b/src/wasm.rs index 952e6ce..096537f 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -4,8 +4,6 @@ use std::panic; -use anyhow::Context; - use js_sys::{Array, Object, Reflect}; use wasm_bindgen::{convert::RefFromWasmAbi, prelude::*}; @@ -31,8 +29,15 @@ pub type JsRes = Result; // Entrypoint setup. /// The main entrypoint which sets up global state. +#[cfg(not(feature = "wasm_no_main"))] #[wasm_bindgen(start)] pub fn main() { + set_panic_hook(); +} + +/// Sets the panic hook to print to the console. +#[wasm_bindgen] +pub fn set_panic_hook() { panic::set_hook(Box::new(console_error_panic_hook::hook)); } @@ -335,6 +340,7 @@ impl KordChord { #[cfg(feature = "audio")] pub async fn play(&self, delay: f32, length: f32, fade_in: f32) -> JsRes<()> { use crate::core::base::Playable; + use anyhow::Context; use gloo_timers::future::TimeoutFuture; let _handle = self.inner.play(delay, length, fade_in).context("Could not start the playback.").to_js_error()?; From 9f88548525c838a5298ed56ab4a44b0142283c44 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Fri, 2 Jun 2023 17:42:39 +0200 Subject: [PATCH 2/2] Remove `fn main` from wasm target --- Cargo.toml | 2 -- src/core/note.rs | 4 ++-- src/wasm.rs | 15 --------------- 3 files changed, 2 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fe3dfdc..5f133b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,8 +53,6 @@ ml_gpu = ["ml_train", "burn-tch"] wasm = ["rodio/wasm-bindgen", "wasm-bindgen", "wasm-bindgen-futures", "js-sys", "console_error_panic_hook", "wee_alloc", "gloo-timers"] -wasm_no_main = [] - plot = ["plotters"] [dependencies] diff --git a/src/core/note.rs b/src/core/note.rs index a8392be..9367ba8 100644 --- a/src/core/note.rs +++ b/src/core/note.rs @@ -418,7 +418,7 @@ impl Add for Note { // Basically, if we were already "on" the weird one (this is a perfect unision, or perfect octave, etc.), then we don't // do anything special. Otherwise, if we landed on on of these edge cases, then we need to adjust the octave. let mut special_octave = 0; - + if self.named_pitch != new_pitch { if new_pitch == NamedPitch::CFlat || new_pitch == NamedPitch::CDoubleFlat @@ -459,7 +459,7 @@ impl Sub for Note { // Basically, if we were already "on" the weird one (this is a perfect unision, or perfect octave, etc.), then we don't // do anything special. Otherwise, if we landed on on of these edge cases, then we need to adjust the octave. let mut special_octave = 0; - + if self.named_pitch != new_pitch { if new_pitch == NamedPitch::CFlat || new_pitch == NamedPitch::CDoubleFlat diff --git a/src/wasm.rs b/src/wasm.rs index 096537f..b938826 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -26,21 +26,6 @@ static ALLOC: wee_alloc::WeeAlloc<'_> = wee_alloc::WeeAlloc::INIT; /// The [`Result`] type for the WASM bindings. pub type JsRes = Result; -// Entrypoint setup. - -/// The main entrypoint which sets up global state. -#[cfg(not(feature = "wasm_no_main"))] -#[wasm_bindgen(start)] -pub fn main() { - set_panic_hook(); -} - -/// Sets the panic hook to print to the console. -#[wasm_bindgen] -pub fn set_panic_hook() { - panic::set_hook(Box::new(console_error_panic_hook::hook)); -} - // [`Note`] ABI. /// The [`Note`] wrapper.