@@ -24,10 +24,11 @@ use shopify_function_wasm_api_core::{
24
24
read:: { ErrorCode , NanBox , Val , ValueRef } ,
25
25
ContextPtr ,
26
26
} ;
27
- use std:: {
28
- ptr:: NonNull ,
29
- sync:: atomic:: { AtomicPtr , AtomicUsize , Ordering } ,
30
- } ;
27
+ use std:: ptr:: NonNull ;
28
+ #[ cfg( target_pointer_width = "32" ) ]
29
+ use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
30
+ #[ cfg( target_pointer_width = "64" ) ]
31
+ use std:: sync:: Mutex ;
31
32
32
33
pub mod read;
33
34
pub mod write;
@@ -242,17 +243,21 @@ impl InternedStringId {
242
243
/// A mechanism for caching interned string IDs.
243
244
pub struct CachedInternedStringId {
244
245
value : & ' static str ,
245
- interned_string_id : AtomicUsize ,
246
- context : AtomicPtr < std:: ffi:: c_void > ,
246
+ #[ cfg( target_pointer_width = "32" ) ]
247
+ interned_string_id_and_context : AtomicU64 ,
248
+ #[ cfg( target_pointer_width = "64" ) ]
249
+ interned_string_id_and_context : Mutex < ( usize , usize ) > ,
247
250
}
248
251
249
252
impl CachedInternedStringId {
250
253
/// Create a new cached interned string ID.
251
254
pub const fn new ( value : & ' static str ) -> Self {
252
255
Self {
253
256
value,
254
- interned_string_id : AtomicUsize :: new ( usize:: MAX ) ,
255
- context : AtomicPtr :: new ( std:: ptr:: null_mut ( ) ) ,
257
+ #[ cfg( target_pointer_width = "32" ) ]
258
+ interned_string_id_and_context : AtomicU64 :: new ( u64:: MAX ) ,
259
+ #[ cfg( target_pointer_width = "64" ) ]
260
+ interned_string_id_and_context : Mutex :: new ( ( usize:: MAX , usize:: MAX ) ) ,
256
261
}
257
262
}
258
263
@@ -266,15 +271,35 @@ impl CachedInternedStringId {
266
271
self . load_from_context_ptr ( value. context . as_ptr ( ) as _ )
267
272
}
268
273
274
+ #[ cfg( target_pointer_width = "32" ) ]
275
+ fn load_from_context_ptr ( & self , context : ContextPtr ) -> InternedStringId {
276
+ let interned_string_id_and_context =
277
+ self . interned_string_id_and_context . load ( Ordering :: Relaxed ) ;
278
+ if interned_string_id_and_context & ( u32:: MAX as u64 ) == context as u64 {
279
+ InternedStringId ( ( interned_string_id_and_context >> 32 ) as usize )
280
+ } else {
281
+ let id = unsafe {
282
+ shopify_function_intern_utf8_str ( context, self . value . as_ptr ( ) , self . value . len ( ) )
283
+ } ;
284
+ let interned_string_id_and_context = ( ( id as u64 ) << 32 ) | ( context as u64 ) ;
285
+ self . interned_string_id_and_context
286
+ . store ( interned_string_id_and_context, Ordering :: Relaxed ) ;
287
+ InternedStringId ( id as usize )
288
+ }
289
+ }
290
+
291
+ #[ cfg( target_pointer_width = "64" ) ]
269
292
fn load_from_context_ptr ( & self , context : ContextPtr ) -> InternedStringId {
270
- if self . context . load ( Ordering :: Relaxed ) != context {
293
+ let mut interned_string_id_and_context =
294
+ self . interned_string_id_and_context . lock ( ) . unwrap ( ) ;
295
+ if interned_string_id_and_context. 1 != context as usize {
271
296
let id = unsafe {
272
297
shopify_function_intern_utf8_str ( context, self . value . as_ptr ( ) , self . value . len ( ) )
273
298
} ;
274
- self . interned_string_id . store ( id , Ordering :: Relaxed ) ;
275
- self . context . store ( context , Ordering :: Relaxed ) ;
299
+ interned_string_id_and_context . 0 = id ;
300
+ interned_string_id_and_context . 1 = context as usize ;
276
301
}
277
- InternedStringId ( self . interned_string_id . load ( Ordering :: Relaxed ) )
302
+ InternedStringId ( interned_string_id_and_context . 0 )
278
303
}
279
304
}
280
305
0 commit comments