Browse Source

Fixing Function.prototype.apply

Sebastien Ros 12 years ago
parent
commit
f9119c6279

+ 1 - 0
Jint.Tests.Ecma/Ecma/10.4.2.1.cs

@@ -2,6 +2,7 @@ using Xunit;
 
 namespace Jint.Tests.Ecma
 {
+    [Trait("Category", "Pass")]
     public class Test_10_4_2_1 : EcmaTest
     {
         [Fact]

+ 2 - 6
Jint/Native/Function/FunctionConstructor.cs

@@ -5,14 +5,10 @@ using Jint.Native.Object;
 using Jint.Parser;
 using Jint.Parser.Ast;
 using Jint.Runtime;
-using Jint.Runtime.Descriptors;
 using Jint.Runtime.Environments;
 
 namespace Jint.Native.Function
 {
-    /// <summary>
-    /// 
-    /// </summary>
     public sealed class FunctionConstructor : FunctionInstance, IConstructor
     {
         private FunctionConstructor(Engine engine):base(engine, null, null, false)
@@ -82,7 +78,7 @@ namespace Jint.Native.Function
                 var functionExpression = "function(" + p + ") { " + body + "}";
                 function = parser.ParseFunctionExpression(functionExpression); 
             }
-            catch (ParserError e)
+            catch (ParserError)
             {
                 throw new JavaScriptException(Engine.SyntaxError);
             }
@@ -96,7 +92,7 @@ namespace Jint.Native.Function
                         Body = new BlockStatement
                             {
                                 Type = SyntaxNodes.BlockStatement,
-                                Body = new Statement[] { function.Body }
+                                Body = new [] { function.Body }
                             },
                         Parameters = parameters.Select(x => new Identifier
                             {

+ 2 - 7
Jint/Native/Function/FunctionPrototype.cs

@@ -57,14 +57,9 @@ namespace Jint.Native.Function
 
         public object Apply(object thisObject, object[] arguments)
         {
-            if (arguments.Length != 2)
-            {
-                throw new ArgumentException("Apply has to be called with two arguments.");
-            }
-
             var func = thisObject as ICallable;
-            object thisArg = arguments[0];
-            object argArray = arguments[1];
+            object thisArg = arguments.At(0);
+            object argArray = arguments.At(1);
 
             if (func == null)
             {

+ 22 - 14
Jint/Native/Function/ScriptFunctionInstance.cs

@@ -61,8 +61,6 @@ namespace Jint.Native.Function
 
             using (new StrictModeScope(Strict, true))
             {
-                var strict = StrictModeScope.IsStrictModeCode;
-
                 // setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
                 if (StrictModeScope.IsStrictModeCode)
                 {
@@ -85,22 +83,32 @@ namespace Jint.Native.Function
 
                 Engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
 
-                Engine.DeclarationBindingInstantiation(DeclarationBindingType.FunctionCode, _functionDeclaration.FunctionDeclarations, _functionDeclaration.VariableDeclarations, this, arguments);
-
-                var result = Engine.ExecuteStatement(_functionDeclaration.Body);
-
-                Engine.LeaveExecutionContext();
-
-                if (result.Type == Completion.Throw)
+                try
                 {
-                    throw new JavaScriptException(result.Value);
+                    Engine.DeclarationBindingInstantiation(
+                        DeclarationBindingType.FunctionCode,
+                        _functionDeclaration.FunctionDeclarations, 
+                        _functionDeclaration.VariableDeclarations, 
+                        this,
+                        arguments);
+
+                    var result = Engine.ExecuteStatement(_functionDeclaration.Body);
+
+                    if (result.Type == Completion.Throw)
+                    {
+                        throw new JavaScriptException(result.Value);
+                    }
+
+                    if (result.Type == Completion.Return)
+                    {
+                        return result.Value;
+                    }
                 }
-
-                if (result.Type == Completion.Return)
+                finally
                 {
-                    return result.Value;
+                    Engine.LeaveExecutionContext();
                 }
-                
+
                 return Undefined.Instance;
             }
         }