Browse Source

Cache wrapped object returned by WrapObjectHandler (#1327)

Genteure 2 years ago
parent
commit
660f6ad78e
2 changed files with 16 additions and 13 deletions
  1. 1 12
      Jint/Options.cs
  2. 15 1
      Jint/Runtime/Interop/DefaultObjectConverter.cs

+ 1 - 12
Jint/Options.cs

@@ -254,18 +254,7 @@ namespace Jint
         /// </summary>
         /// </summary>
         public WrapObjectDelegate WrapObjectHandler { get; set; } = static (engine, target) =>
         public WrapObjectDelegate WrapObjectHandler { get; set; } = static (engine, target) =>
         {
         {
-            // check global cache, have we already wrapped the value?
-            if (engine._objectWrapperCache.TryGetValue(target, out var wrapped))
-            {
-                return wrapped;
-            }
-
-            wrapped = new ObjectWrapper(engine, target);
-            if (engine.Options.Interop.TrackObjectWrapperIdentity)
-            {
-                engine._objectWrapperCache.Add(target, wrapped);
-            }
-            return wrapped;
+            return new ObjectWrapper(engine, target);
         };
         };
 
 
         /// <summary>
         /// <summary>

+ 15 - 1
Jint/Runtime/Interop/DefaultObjectConverter.cs

@@ -102,7 +102,21 @@ namespace Jint
                     }
                     }
                     else
                     else
                     {
                     {
-                        result = engine.Options.Interop.WrapObjectHandler.Invoke(engine, value);
+                        // check global cache, have we already wrapped the value?
+                        if (engine._objectWrapperCache.TryGetValue(value, out var cached))
+                        {
+                            result = cached;
+                        }
+                        else
+                        {
+                            var wrapped = engine.Options.Interop.WrapObjectHandler.Invoke(engine, value);
+                            result = wrapped;
+
+                            if (engine.Options.Interop.TrackObjectWrapperIdentity && wrapped is not null)
+                            {
+                                engine._objectWrapperCache.Add(value, wrapped);
+                            }
+                        }
                     }
                     }
 
 
                     // if no known type could be guessed, use the default of wrapping using using ObjectWrapper.
                     // if no known type could be guessed, use the default of wrapping using using ObjectWrapper.