22
33use super :: error:: { exception_already_thrown, ExcResult , ExceptionThrown , ExceptionValue , Throwable , TypeError } ;
44use super :: from_value:: { cast, FromValue } ;
5- use core :: cell :: RefCell ;
5+ use super :: key_cache :: { get_or_create_key_cache , KeyCache } ;
66use core:: fmt;
77use core:: iter:: { repeat_n, RepeatN } ;
88use core:: marker:: PhantomData ;
@@ -11,20 +11,7 @@ use derive_more::From;
1111use spacetimedb_sats:: de:: { self , ArrayVisitor , DeserializeSeed , ProductVisitor , SliceVisitor , SumVisitor } ;
1212use spacetimedb_sats:: { i256, u256} ;
1313use std:: borrow:: { Borrow , Cow } ;
14- use std:: rc:: Rc ;
15- use v8:: { Array , Global , HandleScope , Local , Name , Object , Uint8Array , Value } ;
16-
17- /// Returns a `KeyCache` for the current `scope`.
18- ///
19- /// Creates the cache in the scope if it doesn't exist yet.
20- pub ( super ) fn get_or_create_key_cache ( scope : & mut HandleScope < ' _ > ) -> Rc < RefCell < KeyCache > > {
21- let context = scope. get_current_context ( ) ;
22- context. get_slot :: < RefCell < KeyCache > > ( ) . unwrap_or_else ( || {
23- let cache = Rc :: default ( ) ;
24- context. set_slot ( Rc :: clone ( & cache) ) ;
25- cache
26- } )
27- }
14+ use v8:: { Array , HandleScope , Local , Name , Object , Uint8Array , Value } ;
2815
2916/// Deserializes a `T` from `val` in `scope`, using `seed` for any context needed.
3017pub ( super ) fn deserialize_js_seed < ' de , T : DeserializeSeed < ' de > > (
@@ -109,51 +96,6 @@ fn scratch_buf<const N: usize>() -> [MaybeUninit<u8>; N] {
10996 [ const { MaybeUninit :: uninit ( ) } ; N ]
11097}
11198
112- /// A cache for frequently used strings to avoid re-interning them.
113- #[ derive( Default ) ]
114- pub ( super ) struct KeyCache {
115- /// The `tag` property for sum values in JS.
116- tag : Option < Global < v8:: String > > ,
117- /// The `value` property for sum values in JS.
118- value : Option < Global < v8:: String > > ,
119- }
120-
121- impl KeyCache {
122- /// Returns the `tag` property name.
123- pub ( super ) fn tag < ' scope > ( & mut self , scope : & mut HandleScope < ' scope > ) -> Local < ' scope , v8:: String > {
124- Self :: get_or_create_key ( scope, & mut self . tag , "tag" )
125- }
126-
127- /// Returns the `value` property name.
128- pub ( super ) fn value < ' scope > ( & mut self , scope : & mut HandleScope < ' scope > ) -> Local < ' scope , v8:: String > {
129- Self :: get_or_create_key ( scope, & mut self . value , "value" )
130- }
131-
132- /// Returns an interned string corresponding to `string`
133- /// and memoizes the creation on the v8 side.
134- fn get_or_create_key < ' scope > (
135- scope : & mut HandleScope < ' scope > ,
136- slot : & mut Option < Global < v8:: String > > ,
137- string : & str ,
138- ) -> Local < ' scope , v8:: String > {
139- if let Some ( s) = & * slot {
140- v8:: Local :: new ( scope, s)
141- } else {
142- let s = v8_interned_string ( scope, string) ;
143- * slot = Some ( v8:: Global :: new ( scope, s) ) ;
144- s
145- }
146- }
147- }
148-
149- // Creates an interned [`v8::String`].
150- fn v8_interned_string < ' scope > ( scope : & mut HandleScope < ' scope > , field : & str ) -> Local < ' scope , v8:: String > {
151- // Internalized v8 strings are significantly faster than "normal" v8 strings
152- // since v8 deduplicates re-used strings minimizing new allocations
153- // see: https://github.com/v8/v8/blob/14ac92e02cc3db38131a57e75e2392529f405f2f/include/v8.h#L3165-L3171
154- v8:: String :: new_from_utf8 ( scope, field. as_ref ( ) , v8:: NewStringType :: Internalized ) . unwrap ( )
155- }
156-
15799/// Extracts a reference `&'scope T` from an owned V8 [`Local<'scope, T>`].
158100///
159101/// The lifetime `'scope` is that of the [`HandleScope<'scope>`].
@@ -280,6 +222,14 @@ struct ProductAccess<'this, 'scope> {
280222 index : usize ,
281223}
282224
225+ // Creates an interned [`v8::String`].
226+ pub ( super ) fn v8_interned_string < ' scope > ( scope : & mut HandleScope < ' scope > , field : & str ) -> Local < ' scope , v8:: String > {
227+ // Internalized v8 strings are significantly faster than "normal" v8 strings
228+ // since v8 deduplicates re-used strings minimizing new allocations
229+ // see: https://github.com/v8/v8/blob/14ac92e02cc3db38131a57e75e2392529f405f2f/include/v8.h#L3165-L3171
230+ v8:: String :: new_from_utf8 ( scope, field. as_ref ( ) , v8:: NewStringType :: Internalized ) . unwrap ( )
231+ }
232+
283233/// Normalizes `field` into an interned `v8::String`.
284234pub ( super ) fn intern_field_name < ' scope > (
285235 scope : & mut HandleScope < ' scope > ,
0 commit comments