devalang_wasm/
lib.rs

1#![allow(dead_code)]
2#![allow(clippy::module_inception)]
3
4pub mod engine;
5pub mod language;
6pub mod shared;
7pub mod utils;
8
9// Plugin development SDK (available with "plugin" feature)
10#[cfg(feature = "plugin")]
11pub mod plugin {
12    //! Plugin development SDK for Devalang
13    //!
14    //! This module provides types, macros, and utilities for writing WASM plugins.
15    //! Enable the "plugin" feature to use this module.
16
17    pub use crate::engine::plugin::bindings::*;
18
19    // Re-export macros from crate root (they are exported there due to #[macro_export])
20    pub use crate::{export_plugin, export_plugin_with_state, simple_oscillator_plugin};
21}
22
23// CLI-specific modules (requires terminal, file system, etc.)
24#[cfg(all(feature = "cli", not(target_arch = "wasm32")))]
25pub mod platform;
26
27#[cfg(all(feature = "cli", not(target_arch = "wasm32")))]
28pub mod services;
29
30#[cfg(all(feature = "cli", not(target_arch = "wasm32")))]
31pub mod tools;
32
33#[cfg(all(feature = "cli", not(target_arch = "wasm32")))]
34pub mod workspace;
35
36// WebAssembly bindings (only compiled for wasm32 target with "wasm" feature)
37#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
38pub mod web;
39
40// Stub for web module when compiling for wasm32 without "wasm" feature (e.g., plugins)
41#[cfg(all(target_arch = "wasm32", not(feature = "wasm")))]
42pub mod web {
43    //! Stub module for web functionality when compiling plugins
44    //! This prevents compilation errors when plugin code references web modules
45
46    pub mod registry {
47        pub mod samples {
48            pub fn get_sample(_name: &str) -> Option<Vec<f32>> {
49                None
50            }
51        }
52        pub mod debug {
53            pub fn log(_msg: &str) {}
54            pub fn is_debug_errors_enabled() -> bool {
55                false
56            }
57            pub fn push_parse_error_from_parts(
58                _source: String,
59                _line: usize,
60                _column: usize,
61                _length: usize,
62                _message: String,
63                _severity: String,
64            ) {
65            }
66        }
67        pub mod banks {
68            use std::collections::HashMap;
69            pub static REGISTERED_BANKS: once_cell::sync::Lazy<
70                std::sync::Mutex<HashMap<String, String>>,
71            > = once_cell::sync::Lazy::new(|| std::sync::Mutex::new(HashMap::new()));
72        }
73        pub mod playhead {
74            #[derive(Default)]
75            pub struct PlayheadEvent {
76                pub kind: String,
77                pub event_type: String,
78                pub midi: Vec<u8>,
79                pub time: f32,
80                pub velocity: f32,
81                pub synth_id: String,
82                pub pitch: u8,
83                pub sample: Option<String>,
84            }
85            pub fn push_event(_event: PlayheadEvent) {}
86        }
87    }
88}
89
90// Stub for web_sys when compiling for wasm32 without "wasm" feature
91#[cfg(all(target_arch = "wasm32", not(feature = "wasm")))]
92pub mod web_sys {
93    //! Stub module for web_sys when compiling plugins without wasm-bindgen
94    pub mod console {
95        pub fn log_1(_msg: &str) {}
96        pub fn warn_1(_msg: &str) {}
97    }
98}
99
100// Fallback stub for `web` module when not compiling for wasm32 at all.
101// Some code references `crate::web::registry` even when building for native
102// (e.g., tests or host builds). Provide a minimal stub so those references
103// compile in non-wasm builds.
104#[cfg(not(target_arch = "wasm32"))]
105pub mod web {
106    pub mod registry {
107        pub mod samples {
108            pub fn get_sample(_name: &str) -> Option<Vec<f32>> {
109                None
110            }
111        }
112        pub mod debug {
113            pub fn log(_msg: &str) {}
114            pub fn is_debug_errors_enabled() -> bool {
115                false
116            }
117            pub fn push_parse_error_from_parts(
118                _source: String,
119                _line: usize,
120                _column: usize,
121                _length: usize,
122                _message: String,
123                _severity: String,
124            ) {
125            }
126        }
127        pub mod banks {
128            pub struct BankEntry {
129                pub full_name: String,
130                pub alias: String,
131            }
132            use std::cell::RefCell;
133            thread_local! {
134                pub static REGISTERED_BANKS: RefCell<Vec<BankEntry>> = RefCell::new(Vec::new());
135            }
136        }
137        pub mod playhead {
138            pub fn push_event<T>(_event: T) {}
139        }
140    }
141}
142
143// Stub for midly when not available
144#[cfg(all(target_arch = "wasm32", not(any(feature = "wasm", feature = "cli"))))]
145pub mod midly {
146    //! Stub module for midly when compiling plugins
147    pub struct Header;
148    pub enum Format {
149        SingleTrack,
150    }
151    pub enum Timing {
152        Metrical(u16),
153    }
154
155    #[derive(Debug, Clone)]
156    pub struct TrackEvent {
157        pub delta: u28,
158        pub kind: TrackEventKind,
159    }
160
161    #[derive(Debug, Clone)]
162    pub struct u28(pub u32);
163    impl From<u32> for u28 {
164        fn from(v: u32) -> Self {
165            u28(v)
166        }
167    }
168
169    #[derive(Debug, Clone)]
170    pub struct u24(pub u32);
171    impl From<u32> for u24 {
172        fn from(v: u32) -> Self {
173            u24(v)
174        }
175    }
176
177    #[derive(Debug, Clone)]
178    pub struct u7(pub u8);
179    impl From<u8> for u7 {
180        fn from(v: u8) -> Self {
181            u7(v)
182        }
183    }
184
185    #[derive(Debug, Clone)]
186    pub struct u4(pub u8);
187    impl From<u8> for u4 {
188        fn from(v: u8) -> Self {
189            u4(v)
190        }
191    }
192
193    #[derive(Debug, Clone)]
194    pub enum MetaMessage {
195        Tempo(u24),
196        EndOfTrack,
197    }
198
199    #[derive(Debug, Clone)]
200    pub enum TrackEventKind {
201        Meta(MetaMessage),
202        Midi { channel: u4, message: MidiMessage },
203    }
204
205    #[derive(Debug, Clone)]
206    pub enum MidiMessage {
207        NoteOn { key: u7, vel: u7 },
208        NoteOff { key: u7, vel: u7 },
209    }
210
211    pub struct Track(Vec<TrackEvent>);
212    impl From<Vec<TrackEvent>> for Track {
213        fn from(events: Vec<TrackEvent>) -> Self {
214            Track(events)
215        }
216    }
217
218    pub struct Smf {
219        pub tracks: Vec<Track>,
220    }
221
222    impl Header {
223        pub fn new(_format: Format, _timing: Timing) -> Self {
224            Header
225        }
226    }
227
228    impl Smf {
229        pub fn new(_header: Header) -> Self {
230            Smf { tracks: Vec::new() }
231        }
232        pub fn write<W: std::io::Write>(&self, _writer: &mut W) -> std::io::Result<()> {
233            Ok(())
234        }
235    }
236}