2
0
Эх сурвалжийг харах

Always set ObjectInstance prototype by default (#982)

Marko Lahma 3 жил өмнө
parent
commit
412922c9ea

+ 9 - 0
Jint.Tests/Runtime/ObjectInstanceTests.cs

@@ -41,5 +41,14 @@ namespace Jint.Tests.Runtime
             var engine = new Engine();
             Assert.True(engine.Evaluate(code).AsBoolean());
         }
+
+        [Fact]
+        public void ShouldHavePrototypeInPlaceByDefault()
+        {
+            var engine = new Engine();
+            var instance = new ObjectInstance(engine);
+            Assert.NotNull(instance.GetPrototypeOf());
+            Assert.Equal("[object Object]", instance.ToString());
+        }
     }
 }

+ 1 - 1
Jint/Native/Array/ArrayIteratorPrototype.cs

@@ -94,7 +94,7 @@ namespace Jint.Native.Array
                 }
 
                 _closed = true;
-                nextItem = KeyValueIteratorPosition.Done;
+                nextItem = KeyValueIteratorPosition.Done(_engine);
                 return false;
             }
         }

+ 8 - 9
Jint/Native/Iterator/IteratorInstance.cs

@@ -43,7 +43,7 @@ namespace Jint.Native.Iterator
                 return true;
             }
 
-            nextItem = ValueIteratorPosition.Done;
+            nextItem = ValueIteratorPosition.Done(_engine);
             return false;
         }
 
@@ -61,7 +61,7 @@ namespace Jint.Native.Iterator
 
         internal sealed class KeyValueIteratorPosition : ObjectInstance
         {
-            internal static readonly ObjectInstance Done = new KeyValueIteratorPosition(null, null, null);
+            internal static ObjectInstance Done(Engine engine) => new KeyValueIteratorPosition(engine, null, null);
 
             public KeyValueIteratorPosition(Engine engine, JsValue key, JsValue value) : base(engine)
             {
@@ -79,16 +79,15 @@ namespace Jint.Native.Iterator
 
         internal sealed class ValueIteratorPosition : ObjectInstance
         {
-            internal static readonly ObjectInstance Done = new KeyValueIteratorPosition(null, null, null);
+            internal static ObjectInstance Done(Engine engine) => new ValueIteratorPosition(engine, Undefined, true);
 
-            public ValueIteratorPosition(Engine engine, JsValue value) : base(engine)
+            public ValueIteratorPosition(Engine engine, JsValue value, bool? done = null) : base(engine)
             {
-                var done = ReferenceEquals(null, value);
-                if (!done)
+                if (value is not null)
                 {
                     SetProperty("value", new PropertyDescriptor(value, PropertyFlag.AllForbidden));
                 }
-                SetProperty("done", new PropertyDescriptor(done, PropertyFlag.AllForbidden));
+                SetProperty("done", new PropertyDescriptor(done ?? value is null, PropertyFlag.AllForbidden));
             }
         }
 
@@ -115,7 +114,7 @@ namespace Jint.Native.Iterator
                 }
 
                 _closed = true;
-                nextItem = KeyValueIteratorPosition.Done;
+                nextItem = KeyValueIteratorPosition.Done(_engine);
                 return false;
             }
         }
@@ -210,7 +209,7 @@ namespace Jint.Native.Iterator
                     return true;
                 }
 
-                nextItem = KeyValueIteratorPosition.Done;
+                nextItem = KeyValueIteratorPosition.Done(_engine);
                 return false;
             }
         }

+ 1 - 1
Jint/Native/Map/MapIteratorPrototype.cs

@@ -70,7 +70,7 @@ namespace Jint.Native.Map
                     return true;
                 }
 
-                nextItem = KeyValueIteratorPosition.Done;
+                nextItem = KeyValueIteratorPosition.Done(_engine);
                 return false;
             }
         }

+ 2 - 12
Jint/Native/Object/ObjectConstructor.cs

@@ -171,23 +171,13 @@ namespace Jint.Native.Object
             }
 
 
-            var obj = new ObjectInstance(_engine)
-            {
-                _prototype = _engine.ExecutionContext.Realm.Intrinsics.Object.PrototypeObject
-            };
-
-            return obj;
+            return new ObjectInstance(_engine);
         }
 
         internal ObjectInstance Construct(int propertyCount)
         {
-            var obj = new ObjectInstance(_engine)
-            {
-                _prototype = Engine.Realm.Intrinsics.Object.PrototypeObject,
-            };
-
+            var obj = new ObjectInstance(_engine);
             obj.SetProperties(propertyCount > 0  ? new PropertyDictionary(propertyCount, checkExistingKeys: true) : null);
-
             return obj;
         }
 

+ 3 - 2
Jint/Native/Object/ObjectInstance.cs

@@ -40,6 +40,8 @@ namespace Jint.Native.Object
         {
             _engine = engine;
             _class = objectClass;
+            // if engine is ready, we can take default prototype for object
+            _prototype = engine.Realm.Intrinsics?.Object?.PrototypeObject;
             Extensible = true;
         }
 
@@ -329,8 +331,7 @@ namespace Jint.Native.Object
                 : desc._value;
 
             // IsDataDescriptor inlined
-            if ((desc._flags & (PropertyFlag.WritableSet | PropertyFlag.Writable)) != 0
-                || !ReferenceEquals(value, null))
+            if ((desc._flags & (PropertyFlag.WritableSet | PropertyFlag.Writable)) != 0 || value is not null)
             {
                 return value ?? Undefined;
             }

+ 1 - 1
Jint/Native/Set/SetIteratorPrototype.cs

@@ -57,7 +57,7 @@ namespace Jint.Native.Set
                     return true;
                 }
 
-                nextItem = KeyValueIteratorPosition.Done;
+                nextItem = KeyValueIteratorPosition.Done(_engine);
                 return false;
             }
         }

+ 0 - 1
Jint/Runtime/Interop/DefaultTypeConverter.cs

@@ -8,7 +8,6 @@ using System.Linq.Expressions;
 using System.Reflection;
 using Jint.Extensions;
 using Jint.Native;
-using Jint.Runtime.Interop.Reflection;
 
 namespace Jint.Runtime.Interop
 {

+ 2 - 2
Jint/Runtime/Interop/ObjectWrapper.cs

@@ -320,7 +320,7 @@ namespace Jint.Runtime.Interop
                     return true;
                 }
 
-                nextItem = KeyValueIteratorPosition.Done;
+                nextItem = KeyValueIteratorPosition.Done(_engine);
                 return false;
             }
         }
@@ -343,7 +343,7 @@ namespace Jint.Runtime.Interop
                     return true;
                 }
 
-                nextItem = KeyValueIteratorPosition.Done;
+                nextItem = KeyValueIteratorPosition.Done(_engine);
                 return false;
             }
         }