Browse Source

Should not wrap Type instances as TypeReference by default (#845)

Marko Lahma 4 years ago
parent
commit
f5a2abf615
4 changed files with 24 additions and 12 deletions
  1. 5 1
      Jint.Tests/Runtime/Domain/Person.cs
  2. 14 0
      Jint.Tests/Runtime/InteropTests.cs
  3. 5 1
      Jint/Engine.cs
  4. 0 10
      Jint/Native/JsValue.cs

+ 5 - 1
Jint.Tests/Runtime/Domain/Person.cs

@@ -1,10 +1,14 @@
-namespace Jint.Tests.Runtime.Domain
+using System;
+
+namespace Jint.Tests.Runtime.Domain
 {
     public class Person : IPerson
     {
         public string Name { get; set; }
         public int Age { get; set; }
 
+        public Type TypeProperty { get; set; } = typeof(Person);
+
         public override string ToString()
         {
             return Name;

+ 14 - 0
Jint.Tests/Runtime/InteropTests.cs

@@ -57,6 +57,20 @@ namespace Jint.Tests.Runtime
                 assert(z === 'foo');
             ");
         }
+        
+        [Fact]
+        public void TypePropertyAccess()
+        {
+            var userClass = new Person();
+
+            var result = new Engine()
+                .SetValue("userclass", userClass)
+                .Execute("userclass.TypeProperty.Name;")
+                .GetCompletionValue()
+                .AsString();
+            
+            Assert.Equal("Person", result);
+        }
 
         [Fact]
         public void CanAccessMemberNamedItem()

+ 5 - 1
Jint/Engine.cs

@@ -309,7 +309,11 @@ namespace Jint
 
         public Engine SetValue(JsValue name, object obj)
         {
-            return SetValue(name, JsValue.FromObject(this, obj));
+            var value = obj is Type t
+                ? TypeReference.CreateTypeReference(this, t)
+                : JsValue.FromObject(this, obj);
+
+            return SetValue(name, value);
         }
 
         public void LeaveExecutionContext()

+ 0 - 10
Jint/Native/JsValue.cs

@@ -276,9 +276,6 @@ namespace Jint.Native
         /// <summary>
         /// Creates a valid <see cref="JsValue"/> instance from any <see cref="Object"/> instance
         /// </summary>
-        /// <param name="engine"></param>
-        /// <param name="value"></param>
-        /// <returns></returns>
         public static JsValue FromObject(Engine engine, object value)
         {
             if (value == null)
@@ -311,13 +308,6 @@ namespace Jint.Native
                 return typeMapper(engine, value);
             }
 
-            var type = value as Type;
-            if (type != null)
-            {
-                var typeReference = TypeReference.CreateTypeReference(engine, type);
-                return typeReference;
-            }
-
             if (value is System.Array a)
             {
                 // racy, we don't care, worst case we'll catch up later