| Lift freeq's AV media plane out of sleek 90f8b89 nandi 19d ago | 1 | // This file is based on code from: |
| 2 | // https://github.com/rustwasm/wasm-bindgen/blob/main/examples/wasm-audio-worklet/src/dependent_module.rs |
| 3 | // |
| 4 | // The original code is licensed under either of: |
| 5 | // - MIT license (https://opensource.org/licenses/MIT) |
| 6 | // - Apache License, Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0) |
| 7 | // at your option. |
| 8 | // |
| 9 | // Copyright (c) 2017-2024 The wasm-bindgen Developers |
| 10 | // |
| 11 | // This file incorporates code from the above source under the Apache License, Version 2.0 license. |
| 12 | // Please see the original repository for more details. |
| 13 | // |
| 14 | // See this issue for a further explanation of what this file does: https://github.com/rustwasm/wasm-bindgen/issues/3019 |
| 15 | |
| 16 | use js_sys::{wasm_bindgen, Array, JsString}; |
| 17 | use wasm_bindgen::prelude::*; |
| 18 | use web_sys::{Blob, BlobPropertyBag, Url}; |
| 19 | |
| 20 | // This is a not-so-clean approach to get the current bindgen ES module URL |
| 21 | // in Rust. This will fail at run time on bindgen targets not using ES modules. |
| 22 | #[wasm_bindgen] |
| 23 | extern "C" { |
| 24 | #[wasm_bindgen] |
| 25 | type ImportMeta; |
| 26 | |
| 27 | #[wasm_bindgen(method, getter)] |
| 28 | fn url(this: &ImportMeta) -> JsString; |
| 29 | |
| 30 | #[wasm_bindgen(thread_local_v2, js_namespace = import, js_name = meta)] |
| 31 | static IMPORT_META: ImportMeta; |
| 32 | } |
| 33 | |
| 34 | pub fn on_the_fly(code: &str) -> Result<String, JsValue> { |
| 35 | // Generate the import of the bindgen ES module, assuming `--target web`. |
| 36 | let header = format!( |
| 37 | "import init, * as bindgen from '{}';\n\n", |
| 38 | IMPORT_META.with(ImportMeta::url), |
| 39 | ); |
| 40 | |
| 41 | let options = BlobPropertyBag::new(); |
| 42 | options.set_type("text/javascript"); |
| 43 | Url::create_object_url_with_blob(&Blob::new_with_str_sequence_and_options( |
| 44 | &Array::of2(&JsValue::from(header.as_str()), &JsValue::from(code)), |
| 45 | &options, |
| 46 | )?) |
| 47 | } |
| 48 | |
| 49 | // dependent_module! takes a local file name to a JS module as input and |
| 50 | // returns a URL to a slightly modified module in run time. This modified module |
| 51 | // has an additional import statement in the header that imports the current |
| 52 | // bindgen JS module under the `bindgen` alias, and the separate init function. |
| 53 | // How this URL is produced does not matter for the macro user. on_the_fly |
| 54 | // creates a blob URL in run time. A better, more sophisticated solution |
| 55 | // would add wasm_bindgen support to put such a module in pkg/ during build time |
| 56 | // and return a URL to this file instead (described in #3019). |
| 57 | #[macro_export] |
| 58 | macro_rules! dependent_module { |
| 59 | ($file_name:expr) => { |
| 60 | $crate::host::audioworklet::dependent_module::on_the_fly(include_str!($file_name)) |
| 61 | }; |
| 62 | } |