wavecraft-bridge 0.12.40

IPC bridge for WebView ↔ Rust communication
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Plugin parameter loader using libloading for FFI.

use libloading::{Library, Symbol};
use std::ffi::CStr;
use std::os::raw::c_char;
use std::path::Path;
use wavecraft_protocol::{
    DEV_PROCESSOR_VTABLE_VERSION, DevProcessorVTable, ParameterInfo, ProcessorInfo,
};

/// Errors that can occur during plugin loading.
#[derive(Debug)]
pub enum PluginLoaderError {
    /// Failed to load the dynamic library.
    LibraryLoad(libloading::Error),
    /// Failed to find a required FFI symbol.
    SymbolNotFound(String),
    /// FFI function returned a null pointer.
    NullPointer(&'static str),
    /// Failed to parse the parameter JSON.
    JsonParse(serde_json::Error),
    /// The returned string was not valid UTF-8.
    InvalidUtf8(std::str::Utf8Error),
    /// Failed to read a file (e.g., sidecar JSON cache).
    FileRead(std::io::Error),
    /// Vtable ABI version mismatch.
    VtableVersionMismatch { found: u32, expected: u32 },
}

impl std::fmt::Display for PluginLoaderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::LibraryLoad(e) => write!(f, "Failed to load plugin library: {}", e),
            Self::SymbolNotFound(name) => write!(f, "Symbol not found: {}", name),
            Self::NullPointer(func) => write!(f, "FFI function {} returned null", func),
            Self::JsonParse(e) => write!(f, "Failed to parse parameter JSON: {}", e),
            Self::InvalidUtf8(e) => write!(f, "Invalid UTF-8 in FFI response: {}", e),
            Self::FileRead(e) => write!(f, "Failed to read file: {}", e),
            Self::VtableVersionMismatch { found, expected } => write!(
                f,
                "Dev processor vtable version mismatch: found {}, expected {}",
                found, expected
            ),
        }
    }
}

impl std::error::Error for PluginLoaderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::LibraryLoad(e) => Some(e),
            Self::JsonParse(e) => Some(e),
            Self::InvalidUtf8(e) => Some(e),
            Self::FileRead(e) => Some(e),
            _ => None,
        }
    }
}

type GetParamsJsonFn = unsafe extern "C" fn() -> *mut c_char;
type GetProcessorsJsonFn = unsafe extern "C" fn() -> *mut c_char;
type FreeStringFn = unsafe extern "C" fn(*mut c_char);
type DevProcessorVTableFn = unsafe extern "C" fn() -> DevProcessorVTable;

fn parse_json_from_ffi<T>(
    json_ptr: *mut c_char,
    function_name: &'static str,
) -> Result<T, PluginLoaderError>
where
    T: serde::de::DeserializeOwned,
{
    if json_ptr.is_null() {
        return Err(PluginLoaderError::NullPointer(function_name));
    }

    // SAFETY: pointer was returned by FFI symbol using CString::into_raw.
    let c_str = unsafe { CStr::from_ptr(json_ptr) };
    let json_str = c_str.to_str().map_err(PluginLoaderError::InvalidUtf8)?;
    serde_json::from_str(json_str).map_err(PluginLoaderError::JsonParse)
}

/// Plugin loader that extracts parameter metadata and the
/// dev audio processor vtable via FFI.
///
/// # Safety
///
/// This struct manages the lifecycle of a dynamically loaded library.
/// The library must remain loaded while any data from it is in use.
/// The `PluginParamLoader` ensures proper cleanup via Drop.
///
/// # Drop Ordering
///
/// Struct fields are dropped in declaration order (first declared = first
/// dropped). `_library` is the **last** field so it is dropped last,
/// ensuring the dynamic library remains loaded while `parameters` and
/// `dev_processor_vtable` are cleaned up.
///
/// **External invariant:** any `FfiProcessor` created from the vtable
/// must be dropped *before* this loader to keep vtable function pointers
/// valid. The caller must ensure this via local variable declaration
/// order (later declared = first dropped).
pub struct PluginParamLoader {
    parameters: Vec<ParameterInfo>,
    processors: Vec<ProcessorInfo>,
    /// Audio processor vtable for in-process dev audio.
    dev_processor_vtable: DevProcessorVTable,
    /// Dynamic library handle — must be the last field so it is dropped
    /// last, after all data that may reference library symbols.
    _library: Library,
}

impl PluginParamLoader {
    /// Load parameters from a sidecar JSON file (bypasses FFI/dlopen).
    ///
    /// Used by `wavecraft start` to read cached parameter metadata without
    /// loading the plugin dylib (which triggers nih-plug static initializers).
    pub fn load_params_from_file<P: AsRef<Path>>(
        json_path: P,
    ) -> Result<Vec<ParameterInfo>, PluginLoaderError> {
        let contents =
            std::fs::read_to_string(json_path.as_ref()).map_err(PluginLoaderError::FileRead)?;
        let params: Vec<ParameterInfo> =
            serde_json::from_str(&contents).map_err(PluginLoaderError::JsonParse)?;
        Ok(params)
    }

    /// Load a plugin from the given path and extract its parameter metadata.
    pub fn load<P: AsRef<Path>>(dylib_path: P) -> Result<Self, PluginLoaderError> {
        // SAFETY: Loading a dynamic library is inherently unsafe. The caller
        // must pass a valid path to a cdylib built with the wavecraft SDK.
        // The library is kept alive for the lifetime of this struct.
        let library =
            unsafe { Library::new(dylib_path.as_ref()) }.map_err(PluginLoaderError::LibraryLoad)?;

        // SAFETY: The symbol `wavecraft_get_params_json` is an `extern "C"`
        // function generated by the `wavecraft_plugin!` macro with the expected
        // signature (`GetParamsJsonFn`). The library is valid and loaded.
        let get_params_json: Symbol<GetParamsJsonFn> = unsafe {
            library.get(b"wavecraft_get_params_json\0").map_err(|e| {
                PluginLoaderError::SymbolNotFound(format!("wavecraft_get_params_json: {}", e))
            })?
        };

        // SAFETY: The symbol `wavecraft_free_string` is an `extern "C"`
        // function generated by the `wavecraft_plugin!` macro with the expected
        // signature (`FreeStringFn`). The library is valid and loaded.
        let free_string: Symbol<FreeStringFn> = unsafe {
            library.get(b"wavecraft_free_string\0").map_err(|e| {
                PluginLoaderError::SymbolNotFound(format!("wavecraft_free_string: {}", e))
            })?
        };

        // SAFETY: Symbol is generated by wavecraft_plugin! and uses GetProcessorsJsonFn ABI.
        let get_processors_json: Symbol<GetProcessorsJsonFn> = unsafe {
            library
                .get(b"wavecraft_get_processors_json\0")
                .map_err(|e| {
                    PluginLoaderError::SymbolNotFound(format!(
                        "wavecraft_get_processors_json: {}",
                        e
                    ))
                })?
        };

        // SAFETY: All FFI calls target `extern "C"` functions generated by
        // the `wavecraft_plugin!` macro:
        // - `get_params_json()` returns a heap-allocated C string (or null).
        //   We check for null before dereferencing.
        // - `CStr::from_ptr()` is safe because the pointer is non-null and
        //   the string is NUL-terminated (allocated by `CString::into_raw`).
        // - `free_string()` deallocates the string originally created by
        //   `CString::into_raw` — matching allocator, called exactly once.
        let params = unsafe {
            let json_ptr = get_params_json();
            let parsed =
                parse_json_from_ffi::<Vec<ParameterInfo>>(json_ptr, "wavecraft_get_params_json")?;
            free_string(json_ptr);
            parsed
        };

        let processors = unsafe {
            let json_ptr = get_processors_json();
            let parsed = parse_json_from_ffi::<Vec<ProcessorInfo>>(
                json_ptr,
                "wavecraft_get_processors_json",
            )?;
            free_string(json_ptr);
            parsed
        };

        // Load and validate audio processor vtable (required for current SDK dev mode)
        let dev_processor_vtable = Self::load_processor_vtable(&library)?;

        Ok(Self {
            parameters: params,
            processors,
            dev_processor_vtable,
            _library: library,
        })
    }

    /// Load parameters only (skip processor vtable loading).
    ///
    /// Used by hot-reload where only parameter metadata is needed.
    /// Avoids potential side effects from vtable construction.
    pub fn load_params_only<P: AsRef<Path>>(
        dylib_path: P,
    ) -> Result<Vec<ParameterInfo>, PluginLoaderError> {
        // SAFETY: Loading a dynamic library is inherently unsafe. The caller
        // must pass a valid path to a cdylib built with the wavecraft SDK.
        let library =
            unsafe { Library::new(dylib_path.as_ref()) }.map_err(PluginLoaderError::LibraryLoad)?;

        // SAFETY: The symbol `wavecraft_get_params_json` is an `extern "C"`
        // function generated by the `wavecraft_plugin!` macro with the expected
        // signature (`GetParamsJsonFn`). The library is valid and loaded.
        let get_params_json: Symbol<GetParamsJsonFn> = unsafe {
            library.get(b"wavecraft_get_params_json\0").map_err(|e| {
                PluginLoaderError::SymbolNotFound(format!("wavecraft_get_params_json: {}", e))
            })?
        };

        // SAFETY: The symbol `wavecraft_free_string` is an `extern "C"`
        // function generated by the `wavecraft_plugin!` macro with the expected
        // signature (`FreeStringFn`). The library is valid and loaded.
        let free_string: Symbol<FreeStringFn> = unsafe {
            library.get(b"wavecraft_free_string\0").map_err(|e| {
                PluginLoaderError::SymbolNotFound(format!("wavecraft_free_string: {}", e))
            })?
        };

        // SAFETY: See `load()` for detailed rationale on FFI safety.
        let params = unsafe {
            let json_ptr = get_params_json();
            let parsed =
                parse_json_from_ffi::<Vec<ParameterInfo>>(json_ptr, "wavecraft_get_params_json")?;
            free_string(json_ptr);
            parsed
        };

        Ok(params)
    }

    /// Load processor metadata only (skip processor vtable loading).
    pub fn load_processors_only<P: AsRef<Path>>(
        dylib_path: P,
    ) -> Result<Vec<ProcessorInfo>, PluginLoaderError> {
        // SAFETY: See load_params_only() rationale.
        let library =
            unsafe { Library::new(dylib_path.as_ref()) }.map_err(PluginLoaderError::LibraryLoad)?;

        // SAFETY: Symbol is generated by wavecraft_plugin! and uses GetProcessorsJsonFn ABI.
        let get_processors_json: Symbol<GetProcessorsJsonFn> = unsafe {
            library
                .get(b"wavecraft_get_processors_json\0")
                .map_err(|e| {
                    PluginLoaderError::SymbolNotFound(format!(
                        "wavecraft_get_processors_json: {}",
                        e
                    ))
                })?
        };

        // SAFETY: Symbol generated by macro with expected FreeStringFn ABI.
        let free_string: Symbol<FreeStringFn> = unsafe {
            library.get(b"wavecraft_free_string\0").map_err(|e| {
                PluginLoaderError::SymbolNotFound(format!("wavecraft_free_string: {}", e))
            })?
        };

        // SAFETY: See load() for FFI safety rationale.
        let processors = unsafe {
            let json_ptr = get_processors_json();
            let parsed = parse_json_from_ffi::<Vec<ProcessorInfo>>(
                json_ptr,
                "wavecraft_get_processors_json",
            )?;
            free_string(json_ptr);
            parsed
        };

        Ok(processors)
    }

    /// Get the loaded parameter information.
    pub fn parameters(&self) -> &[ParameterInfo] {
        &self.parameters
    }

    /// Get the loaded processor metadata.
    pub fn processors(&self) -> &[ProcessorInfo] {
        &self.processors
    }

    /// Get a parameter by ID.
    #[allow(dead_code)]
    pub fn get_parameter(&self, id: &str) -> Option<&ParameterInfo> {
        self.parameters.iter().find(|p| p.id == id)
    }

    /// Returns the validated dev processor vtable.
    pub fn dev_processor_vtable(&self) -> &DevProcessorVTable {
        &self.dev_processor_vtable
    }

    /// Load and validate the dev audio processor vtable from the library.
    fn load_processor_vtable(library: &Library) -> Result<DevProcessorVTable, PluginLoaderError> {
        // SAFETY: `library` is a valid loaded Library handle. The symbol name
        // matches the `extern "C"` function generated by the `wavecraft_plugin!`
        // macro with identical signature (`DevProcessorVTableFn`).
        let symbol: Symbol<DevProcessorVTableFn> = unsafe {
            library
                .get(b"wavecraft_dev_create_processor\0")
                .map_err(|e| {
                    PluginLoaderError::SymbolNotFound(format!(
                        "wavecraft_dev_create_processor: {}",
                        e
                    ))
                })?
        };

        // SAFETY: The symbol points to a valid `extern "C"` function generated
        // by the `wavecraft_plugin!` macro with matching ABI and return type.
        // The function constructs and returns a `DevProcessorVTable` by value
        // (no heap allocation, no side effects beyond initialization).
        // The Library remains loaded for the duration of this call.
        let vtable = unsafe { symbol() };

        // Version check — catch ABI mismatches early
        if vtable.version != DEV_PROCESSOR_VTABLE_VERSION {
            return Err(PluginLoaderError::VtableVersionMismatch {
                found: vtable.version,
                expected: DEV_PROCESSOR_VTABLE_VERSION,
            });
        }

        Ok(vtable)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_error_display() {
        let err = PluginLoaderError::SymbolNotFound("test_symbol".to_string());
        assert!(err.to_string().contains("test_symbol"));
    }

    #[test]
    fn test_null_pointer_error() {
        let err = PluginLoaderError::NullPointer("wavecraft_get_params_json");
        assert!(err.to_string().contains("null"));
    }

    #[test]
    fn test_file_read_error() {
        let err = PluginLoaderError::FileRead(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "file not found",
        ));
        assert!(err.to_string().contains("Failed to read file"));
    }

    #[test]
    fn test_vtable_version_mismatch_error_display() {
        let err = PluginLoaderError::VtableVersionMismatch {
            found: 1,
            expected: 2,
        };
        assert!(err.to_string().contains("version mismatch"));
        assert!(err.to_string().contains("found 1"));
        assert!(err.to_string().contains("expected 2"));
    }

    #[test]
    fn test_load_params_from_file() {
        use wavecraft_protocol::ParameterType;

        let dir = std::env::temp_dir().join("wavecraft_test_sidecar");
        let _ = std::fs::create_dir_all(&dir);
        let json_path = dir.join("wavecraft-params.json");

        let params = vec![ParameterInfo {
            id: "gain".to_string(),
            name: "Gain".to_string(),
            param_type: ParameterType::Float,
            value: 0.5,
            default: 0.5,
            min: 0.0,
            max: 1.0,
            unit: Some("dB".to_string()),
            group: Some("Main".to_string()),
            variants: None,
        }];

        let json = serde_json::to_string_pretty(&params).unwrap();
        std::fs::write(&json_path, &json).unwrap();

        let loaded = PluginParamLoader::load_params_from_file(&json_path).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].id, "gain");
        assert_eq!(loaded[0].name, "Gain");
        assert!((loaded[0].default - 0.5).abs() < f32::EPSILON);

        // Cleanup
        let _ = std::fs::remove_file(&json_path);
        let _ = std::fs::remove_dir(&dir);
    }

    #[test]
    fn test_load_params_from_file_not_found() {
        let result = PluginParamLoader::load_params_from_file("/nonexistent/path.json");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, PluginLoaderError::FileRead(_)));
    }

    #[test]
    fn test_load_params_from_file_invalid_json() {
        let dir = std::env::temp_dir().join("wavecraft_test_bad_json");
        let _ = std::fs::create_dir_all(&dir);
        let json_path = dir.join("bad-params.json");

        std::fs::write(&json_path, "not valid json").unwrap();

        let result = PluginParamLoader::load_params_from_file(&json_path);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            PluginLoaderError::JsonParse(_)
        ));

        // Cleanup
        let _ = std::fs::remove_file(&json_path);
        let _ = std::fs::remove_dir(&dir);
    }

    #[test]
    fn test_parse_processors_json() {
        let json = r#"[{"id":"oscillator"},{"id":"output_gain"}]"#;
        let processors: Vec<ProcessorInfo> =
            serde_json::from_str(json).expect("json should deserialize");

        assert_eq!(processors.len(), 2);
        assert_eq!(processors[0].id, "oscillator");
        assert_eq!(processors[1].id, "output_gain");
    }

    #[test]
    fn test_parse_processors_json_invalid() {
        let json = "not valid json";
        let parsed: Result<Vec<ProcessorInfo>, _> = serde_json::from_str(json);
        assert!(parsed.is_err());
    }
}