Browse Source

Adding conversion for function to delegates

Sebastien Ros 11 years ago
parent
commit
e63c06e976
2 changed files with 46 additions and 9 deletions
  1. 35 1
      Jint.Tests/Runtime/InteropTests.cs
  2. 11 8
      Jint/Native/JsValue.cs

+ 35 - 1
Jint.Tests/Runtime/InteropTests.cs

@@ -398,13 +398,32 @@ namespace Jint.Tests.Runtime
         public void ShouldNotTryToConvertDerivedTypes()
         {
             _engine.SetValue("a", new A());
-            _engine.SetValue("p", new Person { Name = "Mickey"});
+            _engine.SetValue("p", new Person { Name = "Mickey" });
 
             RunTest(@"
                 assert(a.Call4(p) === 'Mickey');
             ");
         }
 
+        [Fact]
+        public void ShouldExecuteFunctionCallBackAsDelegate()
+        {
+            _engine.SetValue("a", new A());
+
+            RunTest(@"
+                assert(a.Call5(function(a,b){ return a+b }) === '1foo');
+            ");
+        }
+
+        [Fact]
+        public void ShouldExecuteFunctionCallBackAsFuncAndThisCanBeAssigned()
+        {
+            _engine.SetValue("a", new A());
+
+            RunTest(@"
+                assert(a.Call6(function(a,b){ return this+a+b }) === 'bar1foo');
+            ");
+        }
 
         public interface IPerson
         {
@@ -449,6 +468,21 @@ namespace Jint.Tests.Runtime
                 return x.ToString();
             }
 
+            public string Call5(Delegate callback)
+            {
+                var thisArg = JsValue.Undefined;
+                var arguments = new JsValue[] { 1, "foo" };
+
+                return callback.DynamicInvoke(thisArg, arguments).ToString();
+            }
+
+            public string Call6(Func<JsValue, JsValue[], JsValue> callback)
+            {
+                var thisArg = new JsValue("bar");
+                var arguments = new JsValue[] { 1, "foo" };
+
+                return callback(thisArg, arguments).ToString();
+            }
         }
     }
 }

+ 11 - 8
Jint/Native/JsValue.cs

@@ -6,6 +6,7 @@ using System.Dynamic;
 using Jint.Native.Array;
 using Jint.Native.Boolean;
 using Jint.Native.Date;
+using Jint.Native.Function;
 using Jint.Native.Number;
 using Jint.Native.Object;
 using Jint.Native.RegExp;
@@ -93,7 +94,7 @@ namespace Jint.Native
         [Pure]
         public bool IsArray()
         {
-            return IsObject() && AsObject() is Array.ArrayInstance;
+            return IsObject() && AsObject() is ArrayInstance;
         }
 
         [Pure]
@@ -144,13 +145,13 @@ namespace Jint.Native
         }
 
         [Pure]
-        public Array.ArrayInstance AsArray()
+        public ArrayInstance AsArray()
         {
             if (!IsArray())
             {
                 throw new ArgumentException("The value is not an array");
             }
-            return AsObject() as Array.ArrayInstance;            
+            return AsObject() as ArrayInstance;            
         }
 
         [Pure]
@@ -441,8 +442,13 @@ namespace Jint.Native
                             break;
 
                         case "Function":
-                            // todo
-                            throw new NotSupportedException("Function objects can't be converted yet");
+                            var function = _object as FunctionInstance;
+                            if (function != null)
+                            {
+                                return (Func<JsValue, JsValue[], JsValue>) function.Call;
+                            }
+
+                            break; 
 
                         case "Number":
                             var numberInstance = _object as NumberInstance;
@@ -590,8 +596,5 @@ namespace Jint.Native
                 return hashCode;
             }
         }
-
-
-        public static object function { get; set; }
     }
 }