Browse Source

Implement TypeReference.HasInstance (#422)

This patch introduces support for instanceof operator on engine symbols
 which are created from a TypeReference.  Instances constructed from a
 TypeReference should be an instance of that CLR type.

 Tests included in InteropTests. Fixes sebastienros/jint#421.
Tyler Watson 7 years ago
parent
commit
f04109e400
2 changed files with 23 additions and 1 deletions
  1. 10 0
      Jint.Tests/Runtime/InteropTests.cs
  2. 13 1
      Jint/Runtime/Interop/TypeReference.cs

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

@@ -861,6 +861,16 @@ namespace Jint.Tests.Runtime
             ");
         }
 
+        [Fact]
+        public void ShouldBeInstanceOfTypeReferenceType()
+        {
+            _engine.SetValue("A", typeof(A));
+            RunTest(@"
+                var a = new A();
+                assert(a instanceof A);
+            ");
+        }
+
         [Fact]
         public void ShouldImportNamespace()
         {

+ 13 - 1
Jint/Runtime/Interop/TypeReference.cs

@@ -63,7 +63,7 @@ namespace Jint.Runtime.Interop
                 {
                     for (var i = 0; i < arguments.Length; i++)
                     {
-                        var parameterType =  method.GetParameters()[i].ParameterType;
+                        var parameterType = method.GetParameters()[i].ParameterType;
 
                         if (parameterType == typeof(JsValue))
                         {
@@ -96,6 +96,18 @@ namespace Jint.Runtime.Interop
 
         }
 
+        public override bool HasInstance(JsValue v)
+        {
+            ObjectWrapper wrapper = v.As<ObjectWrapper>();
+
+            if (wrapper == null)
+            {
+                return base.HasInstance(v);
+            }
+
+            return wrapper.Target.GetType() == this.Type;
+        }
+
         public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
         {
             if (throwOnError)