@@ -50,13 +50,13 @@ internal void CreateImmutableBindingAndInitialize(Key name, bool strict, JsValue
5050 internal sealed override void CreateMutableBinding ( Key name , bool canBeDeleted = false )
5151 {
5252 _dictionary ??= new HybridDictionary < Binding > ( ) ;
53- _dictionary [ name ] = new Binding ( null ! , canBeDeleted , mutable : true , strict : false ) ;
53+ _dictionary . CreateMutableBinding ( name , canBeDeleted ) ;
5454 }
5555
5656 internal sealed override void CreateImmutableBinding ( Key name , bool strict = true )
5757 {
5858 _dictionary ??= new HybridDictionary < Binding > ( ) ;
59- _dictionary [ name ] = new Binding ( null ! , canBeDeleted : false , mutable : false , strict ) ;
59+ _dictionary . CreateImmutableBinding ( name , strict ) ;
6060 }
6161
6262 internal sealed override void InitializeBinding ( Key name , JsValue value )
@@ -69,14 +69,17 @@ internal sealed override void InitializeBinding(Key name, JsValue value)
6969
7070 internal sealed override void SetMutableBinding ( Key name , JsValue value , bool strict )
7171 {
72- if ( _dictionary is null || ! _dictionary . TryGetValue ( name , out var binding ) )
72+ _dictionary ??= new HybridDictionary < Binding > ( ) ;
73+
74+ ref var binding = ref _dictionary . GetValueRefOrNullRef ( name ) ;
75+ if ( Unsafe . IsNullRef ( ref binding ) )
7376 {
7477 if ( strict )
7578 {
7679 ExceptionHelper . ThrowReferenceNameError ( _engine . Realm , name ) ;
7780 }
7881
79- CreateMutableBindingAndInitialize ( name , canBeDeleted : true , value ) ;
82+ _dictionary [ name ] = new Binding ( value , canBeDeleted : true , mutable : true , strict : false ) ;
8083 return ;
8184 }
8285
@@ -93,7 +96,7 @@ internal sealed override void SetMutableBinding(Key name, JsValue value, bool st
9396
9497 if ( binding . Mutable )
9598 {
96- _dictionary [ name ] = binding . ChangeValue ( value ) ;
99+ binding = binding . ChangeValue ( value ) ;
97100 }
98101 else
99102 {
@@ -116,7 +119,7 @@ internal override JsValue GetBindingValue(Key name, bool strict)
116119 }
117120
118121 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
119- private void ThrowUninitializedBindingError ( string name )
122+ private void ThrowUninitializedBindingError ( Key name )
120123 {
121124 ExceptionHelper . ThrowReferenceError ( _engine . Realm , $ "Cannot access '{ name } ' before initialization") ;
122125 }
@@ -151,7 +154,7 @@ internal sealed override string[] GetAllBindingNames()
151154 {
152155 if ( _dictionary is null )
153156 {
154- return Array . Empty < string > ( ) ;
157+ return [ ] ;
155158 }
156159
157160 var keys = new string [ _dictionary . Count ] ;
@@ -183,3 +186,18 @@ internal void TransferTo(List<Key> names, DeclarativeEnvironment env)
183186 }
184187 }
185188}
189+
190+ internal static class DictionaryExtensions
191+ {
192+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
193+ internal static void CreateMutableBinding < T > ( this T dictionary , Key name , bool canBeDeleted = false ) where T : IEngineDictionary < Key , Binding >
194+ {
195+ dictionary [ name ] = new Binding ( null ! , canBeDeleted , mutable : true , strict : false ) ;
196+ }
197+
198+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
199+ internal static void CreateImmutableBinding < T > ( this T dictionary , Key name , bool strict = true ) where T : IEngineDictionary < Key , Binding >
200+ {
201+ dictionary [ name ] = new Binding ( null ! , canBeDeleted : false , mutable : false , strict ) ;
202+ }
203+ }
0 commit comments