Bläddra i källkod

Supporting enum types

#34
Sebastien Ros 11 år sedan
förälder
incheckning
bec47d08d3

+ 1 - 0
Jint.Tests/Jint.Tests.csproj

@@ -57,6 +57,7 @@
     <Compile Include="Runtime\Converters\NegateBoolConverter.cs" />
     <Compile Include="Runtime\Domain\A.cs" />
     <Compile Include="Runtime\Domain\ClassWithField.cs" />
+    <Compile Include="Runtime\Domain\Colors.cs" />
     <Compile Include="Runtime\Domain\IPerson.cs" />
     <Compile Include="Runtime\Domain\Person.cs" />
     <Compile Include="Runtime\Domain\Shape.cs" />

+ 12 - 0
Jint.Tests/Runtime/Domain/Colors.cs

@@ -0,0 +1,12 @@
+using System;
+
+namespace Jint.Tests.Runtime.Domain
+{
+    [Flags]
+    public enum Colors
+    {
+        Red,
+        Green,
+        Blue = 10
+    }
+}

+ 3 - 0
Jint.Tests/Runtime/Domain/Shape.cs

@@ -3,12 +3,15 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using Jint.Tests.Runtime.Domain;
 
 namespace Shapes
 {
     public abstract class Shape
     {
         public abstract double Perimeter();
+        public Colors Color { get; set; }
+
     }
 
     public class Circle : Shape

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

@@ -602,5 +602,53 @@ namespace Jint.Tests.Runtime
                 assert(2 === list.Count);
             ");
         }
+
+
+        [Fact]
+        public void EnumIsConvertedToNumber()
+        {
+            var o = new
+            {
+                r = Colors.Red,
+                b = Colors.Blue,
+                g = Colors.Green
+            };
+
+            _engine.SetValue("o", o);
+
+            RunTest(@"
+                assert(o.r === 0);
+                assert(o.g === 1);
+                assert(o.b === 10);
+            ");
+        }
+
+
+        [Fact]
+        public void ShouldConvertToEnum()
+        {
+            var s = new Circle
+            {
+                Color = Colors.Red,
+            };
+
+            _engine.SetValue("s", s);
+
+            RunTest(@"
+                assert(s.Color === 0);
+                s.Color = 10;
+                assert(s.Color === 10);
+            ");
+
+            _engine.SetValue("s", s);
+
+            RunTest(@"
+                s.Color = 11;
+                assert(s.Color === 11);
+            ");
+
+            Assert.Equal(Colors.Blue | Colors.Green, s.Color);
+        }
+
     }
 }

+ 5 - 0
Jint/Native/JsValue.cs

@@ -374,6 +374,11 @@ namespace Jint.Native
                 return new DelegateWrapper(engine, d);
             }
 
+            if (value.GetType().IsEnum)
+            {
+                return new JsValue((Int32)value);
+            }
+
             // if no known type could be guessed, wrap it as an ObjectInstance
             return new ObjectWrapper(engine, value);
         }

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

@@ -12,6 +12,17 @@ namespace Jint.Runtime.Interop
                 return value;
             }
 
+            if (type.IsEnum)
+            {
+                var integer = System.Convert.ChangeType(value, typeof (int), formatProvider);
+                if (integer == null)
+                {
+                    throw new ArgumentOutOfRangeException();
+                }
+
+                return Enum.ToObject(type, integer);
+            }
+
             return System.Convert.ChangeType(value, type, formatProvider);
         }
     }