@@ -1645,15 +1645,42 @@ impl ToTokens for ast::Enum {
1645
1645
impl ToTokens for ast:: ImportStatic {
1646
1646
fn to_tokens ( & self , into : & mut TokenStream ) {
1647
1647
let ty = & self . ty ;
1648
- static_import (
1649
- & self . vis ,
1650
- & self . rust_name ,
1651
- & self . wasm_bindgen ,
1652
- ty,
1653
- ty,
1654
- & self . shim ,
1655
- )
1656
- . to_tokens ( into) ;
1648
+
1649
+ if self . thread_local {
1650
+ thread_local_import (
1651
+ & self . vis ,
1652
+ & self . rust_name ,
1653
+ & self . wasm_bindgen ,
1654
+ ty,
1655
+ ty,
1656
+ & self . shim ,
1657
+ )
1658
+ . to_tokens ( into)
1659
+ } else {
1660
+ let vis = & self . vis ;
1661
+ let name = & self . rust_name ;
1662
+ let wasm_bindgen = & self . wasm_bindgen ;
1663
+ let ty = & self . ty ;
1664
+ let shim_name = & self . shim ;
1665
+ let init = static_init ( wasm_bindgen, ty, shim_name) ;
1666
+
1667
+ into. extend ( quote ! {
1668
+ #[ automatically_derived]
1669
+ #[ deprecated = "use with `#[wasm_bindgen(thread_local)]` instead" ]
1670
+ } ) ;
1671
+ into. extend (
1672
+ quote_spanned ! { name. span( ) => #vis static #name: #wasm_bindgen:: JsStatic <#ty> = {
1673
+ fn init( ) -> #ty {
1674
+ #init
1675
+ }
1676
+ thread_local!( static _VAL: #ty = init( ) ; ) ;
1677
+ #wasm_bindgen:: JsStatic {
1678
+ __inner: & _VAL,
1679
+ }
1680
+ } ;
1681
+ } ,
1682
+ ) ;
1683
+ }
1657
1684
1658
1685
Descriptor {
1659
1686
ident : & self . shim ,
@@ -1672,7 +1699,7 @@ impl ToTokens for ast::ImportString {
1672
1699
let js_sys = & self . js_sys ;
1673
1700
let actual_ty: syn:: Type = parse_quote ! ( #js_sys:: JsString ) ;
1674
1701
1675
- static_import (
1702
+ thread_local_import (
1676
1703
& self . vis ,
1677
1704
& self . rust_name ,
1678
1705
& self . wasm_bindgen ,
@@ -1684,41 +1711,45 @@ impl ToTokens for ast::ImportString {
1684
1711
}
1685
1712
}
1686
1713
1687
- fn static_import (
1714
+ fn thread_local_import (
1688
1715
vis : & syn:: Visibility ,
1689
1716
name : & Ident ,
1690
1717
wasm_bindgen : & syn:: Path ,
1691
1718
actual_ty : & syn:: Type ,
1692
1719
ty : & syn:: Type ,
1693
1720
shim_name : & Ident ,
1694
1721
) -> TokenStream {
1722
+ let init = static_init ( wasm_bindgen, ty, shim_name) ;
1723
+
1724
+ quote ! {
1725
+ thread_local! {
1726
+ #[ automatically_derived]
1727
+ #vis static #name: #actual_ty = {
1728
+ #init
1729
+ } ;
1730
+ }
1731
+ }
1732
+ }
1733
+
1734
+ fn static_init ( wasm_bindgen : & syn:: Path , ty : & syn:: Type , shim_name : & Ident ) -> TokenStream {
1695
1735
let abi_ret = quote ! {
1696
1736
#wasm_bindgen:: convert:: WasmRet <<#ty as #wasm_bindgen:: convert:: FromWasmAbi >:: Abi >
1697
1737
} ;
1698
1738
quote ! {
1699
- #[ automatically_derived]
1700
- #vis static #name: #wasm_bindgen:: JsStatic <#actual_ty> = {
1701
- fn init( ) -> #ty {
1702
- #[ link( wasm_import_module = "__wbindgen_placeholder__" ) ]
1703
- #[ cfg( all( target_arch = "wasm32" , target_os = "unknown" ) ) ]
1704
- extern "C" {
1705
- fn #shim_name( ) -> #abi_ret;
1706
- }
1739
+ #[ link( wasm_import_module = "__wbindgen_placeholder__" ) ]
1740
+ #[ cfg( all( target_arch = "wasm32" , target_os = "unknown" ) ) ]
1741
+ extern "C" {
1742
+ fn #shim_name( ) -> #abi_ret;
1743
+ }
1707
1744
1708
- #[ cfg( not( all( target_arch = "wasm32" , target_os = "unknown" ) ) ) ]
1709
- unsafe fn #shim_name( ) -> #abi_ret {
1710
- panic!( "cannot access imported statics on non-wasm targets" )
1711
- }
1745
+ #[ cfg( not( all( target_arch = "wasm32" , target_os = "unknown" ) ) ) ]
1746
+ unsafe fn #shim_name( ) -> #abi_ret {
1747
+ panic!( "cannot access imported statics on non-wasm targets" )
1748
+ }
1712
1749
1713
- unsafe {
1714
- <#ty as #wasm_bindgen:: convert:: FromWasmAbi >:: from_abi( #shim_name( ) . join( ) )
1715
- }
1716
- }
1717
- thread_local!( static _VAL: #ty = init( ) ; ) ;
1718
- #wasm_bindgen:: JsStatic {
1719
- __inner: & _VAL,
1720
- }
1721
- } ;
1750
+ unsafe {
1751
+ <#ty as #wasm_bindgen:: convert:: FromWasmAbi >:: from_abi( #shim_name( ) . join( ) )
1752
+ }
1722
1753
}
1723
1754
}
1724
1755
0 commit comments