@@ -3,7 +3,7 @@ use std::mem;
3
3
use std:: ptr;
4
4
use std:: slice;
5
5
use std:: vec:: Vec ;
6
-
6
+ use std :: cell :: Cell ;
7
7
use crate :: JsValue ;
8
8
9
9
externs ! {
@@ -121,63 +121,13 @@ fn internal_error(msg: &str) -> ! {
121
121
}
122
122
}
123
123
124
- // Whoa, there's two `tl` modules here! That's currently intention, but for sort
125
- // of a weird reason. The table here is fundamentally thread local, so we want
126
- // to use the `thread_local!` macro. The implementation of thread locals (as of
127
- // the time of this writing) generates a lot of code as it pulls in panic paths
128
- // in libstd (even when using `try_with`). There's a patch to fix that
129
- // (rust-lang/rust#55518), but in the meantime the stable/beta channels produce
130
- // a lot of code.
131
- //
132
- // Matters are made worse here because this code is almost never used (it's only
133
- // here for an unstable feature). If we were to have panics here, though, then
134
- // we couldn't effectively gc away the panic infrastructure, meaning this unused
135
- // infrastructure would show up in binaries! That's a no-no for wasm-bindgen.
136
- //
137
- // In the meantime, if the atomics feature is turned on (which it never is by
138
- // default) then we use `thread_local!`, otherwise we use a home-grown
139
- // implementation that will be replaced once #55518 lands on stable.
140
- #[ cfg( target_feature = "atomics" ) ]
141
- mod tl {
142
- use super :: Slab ;
143
- use std:: cell:: Cell ;
144
- use std:: * ; // hack to get `thread_local!` to work
145
-
146
- thread_local ! ( pub static HEAP_SLAB : Cell <Slab > = Cell :: new( Slab :: new( ) ) ) ;
147
- }
148
-
149
- #[ cfg( not( target_feature = "atomics" ) ) ]
150
- mod tl {
151
- use super :: Slab ;
152
- use std:: alloc:: { self , Layout } ;
153
- use std:: cell:: Cell ;
154
- use std:: ptr;
155
-
156
- pub struct HeapSlab ;
157
- pub static HEAP_SLAB : HeapSlab = HeapSlab ;
158
- static mut SLOT : * mut Cell < Slab > = 0 as * mut Cell < Slab > ;
159
-
160
- impl HeapSlab {
161
- pub fn try_with < R > ( & self , f : impl FnOnce ( & Cell < Slab > ) -> R ) -> Result < R , ( ) > {
162
- unsafe {
163
- if SLOT . is_null ( ) {
164
- let ptr = alloc:: alloc ( Layout :: new :: < Cell < Slab > > ( ) ) ;
165
- if ptr. is_null ( ) {
166
- super :: internal_error ( "allocation failure" ) ;
167
- }
168
- let ptr = ptr as * mut Cell < Slab > ;
169
- ptr:: write ( ptr, Cell :: new ( Slab :: new ( ) ) ) ;
170
- SLOT = ptr;
171
- }
172
- Ok ( f ( & * SLOT ) )
173
- }
174
- }
175
- }
176
- }
124
+ // Management of `anyref` is always thread local since an `anyref` value can't
125
+ // cross threads in wasm. Indices as a result are always thread-local.
126
+ std:: thread_local!( pub static HEAP_SLAB : Cell <Slab > = Cell :: new( Slab :: new( ) ) ) ;
177
127
178
128
#[ no_mangle]
179
129
pub extern "C" fn __wbindgen_anyref_table_alloc ( ) -> usize {
180
- tl :: HEAP_SLAB
130
+ HEAP_SLAB
181
131
. try_with ( |slot| {
182
132
let mut slab = slot. replace ( Slab :: new ( ) ) ;
183
133
let ret = slab. alloc ( ) ;
@@ -197,7 +147,7 @@ pub extern "C" fn __wbindgen_anyref_table_dealloc(idx: usize) {
197
147
unsafe {
198
148
__wbindgen_anyref_table_set_null ( idx) ;
199
149
}
200
- tl :: HEAP_SLAB
150
+ HEAP_SLAB
201
151
. try_with ( |slot| {
202
152
let mut slab = slot. replace ( Slab :: new ( ) ) ;
203
153
slab. dealloc ( idx) ;
@@ -217,7 +167,7 @@ pub unsafe extern "C" fn __wbindgen_drop_anyref_slice(ptr: *mut JsValue, len: us
217
167
// `anyref` instead of the JS `heap`.
218
168
#[ no_mangle]
219
169
pub unsafe extern "C" fn __wbindgen_anyref_heap_live_count_impl ( ) -> u32 {
220
- tl :: HEAP_SLAB
170
+ HEAP_SLAB
221
171
. try_with ( |slot| {
222
172
let slab = slot. replace ( Slab :: new ( ) ) ;
223
173
let count = slab. live_count ( ) ;
0 commit comments