File tree Expand file tree Collapse file tree 4 files changed +21
-15
lines changed
examples/wasm-audio-worklet/src Expand file tree Collapse file tree 4 files changed +21
-15
lines changed Original file line number Diff line number Diff line change @@ -255,6 +255,9 @@ intrinsics! {
255
255
#[ symbol = "__wbindgen_module" ]
256
256
#[ signature = fn ( ) -> Externref ]
257
257
Module ,
258
+ #[ symbol = "__wbindgen_shim_url" ]
259
+ #[ signature = fn ( ) -> Externref ]
260
+ ShimUrl ,
258
261
#[ symbol = "__wbindgen_function_table" ]
259
262
#[ signature = fn ( ) -> Externref ]
260
263
FunctionTable ,
Original file line number Diff line number Diff line change @@ -3503,6 +3503,15 @@ impl<'a> Context<'a> {
3503
3503
format ! ( "wasm.{}" , self . export_name_of( memory) )
3504
3504
}
3505
3505
3506
+ Intrinsic :: ShimUrl => {
3507
+ assert_eq ! ( args. len( ) , 0 ) ;
3508
+ match self . config . mode {
3509
+ OutputMode :: Web => format ! ( "import.meta.url" ) ,
3510
+ OutputMode :: NoModules { .. } => format ! ( "script_src" ) ,
3511
+ _ => format ! ( "null" ) ,
3512
+ }
3513
+ }
3514
+
3506
3515
Intrinsic :: FunctionTable => {
3507
3516
assert_eq ! ( args. len( ) , 0 ) ;
3508
3517
let name = self . export_function_table ( ) ?;
Original file line number Diff line number Diff line change @@ -2,28 +2,14 @@ use js_sys::{Array, JsString};
2
2
use wasm_bindgen:: prelude:: * ;
3
3
use web_sys:: { Blob , BlobPropertyBag , Url } ;
4
4
5
- // This is a not-so-clean approach to get the current bindgen ES module URL
6
- // in Rust. This will fail at run time on bindgen targets not using ES modules.
7
- #[ wasm_bindgen]
8
- extern "C" {
9
- #[ wasm_bindgen]
10
- type ImportMeta ;
11
-
12
- #[ wasm_bindgen( method, getter) ]
13
- fn url ( this : & ImportMeta ) -> JsString ;
14
-
15
- #[ wasm_bindgen( js_namespace = import, js_name = meta) ]
16
- static IMPORT_META : ImportMeta ;
17
- }
18
-
19
5
pub fn on_the_fly ( code : & str ) -> Result < String , JsValue > {
20
6
// Generate the import of the bindgen ES module, assuming `--target web`,
21
7
// preluded by the TextEncoder/TextDecoder polyfill needed inside worklets.
22
8
let header = format ! (
23
9
"import '{}';\n \
24
10
import init, * as bindgen from '{}';\n \n ",
25
11
wasm_bindgen:: link_to!( module = "/src/polyfill.js" ) ,
26
- IMPORT_META . url ( ) ,
12
+ wasm_bindgen :: shim_url ( ) . expect ( "not using `--target web`" ) ,
27
13
) ;
28
14
29
15
Url :: create_object_url_with_blob ( & Blob :: new_with_str_sequence_and_options (
Original file line number Diff line number Diff line change @@ -1084,6 +1084,7 @@ externs! {
1084
1084
1085
1085
fn __wbindgen_memory( ) -> u32 ;
1086
1086
fn __wbindgen_module( ) -> u32 ;
1087
+ fn __wbindgen_shim_url( ) -> u32 ;
1087
1088
fn __wbindgen_function_table( ) -> u32 ;
1088
1089
}
1089
1090
}
@@ -1363,6 +1364,13 @@ pub fn memory() -> JsValue {
1363
1364
unsafe { JsValue :: _new ( __wbindgen_memory ( ) ) }
1364
1365
}
1365
1366
1367
+ /// Returns the URL to the script that instantiated the wasm module.
1368
+ ///
1369
+ /// This will currently return `None` on every target except `web` and `no-modules`.
1370
+ pub fn shim_url ( ) -> Option < String > {
1371
+ unsafe { JsValue :: _new ( __wbindgen_shim_url ( ) ) } . as_string ( )
1372
+ }
1373
+
1366
1374
/// Returns a handle to this wasm instance's `WebAssembly.Table` which is the
1367
1375
/// indirect function table used by Rust
1368
1376
pub fn function_table ( ) -> JsValue {
You can’t perform that action at this time.
0 commit comments