ソースを参照

Fixing direct call to eval()

Sebastien Ros 12 年 前
コミット
24071e0c9b

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

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

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

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

+ 1 - 1
Jint/Engine.cs

@@ -116,7 +116,7 @@ namespace Jint
                 }
             }
 
-            Eval = new EvalFunctionInstance(this, new ObjectInstance(this), new string[0], LexicalEnvironment.NewDeclarativeEnvironment(this, ExecutionContext.LexicalEnvironment), StrictModeScope.IsStrictModeCode);
+            Eval = new EvalFunctionInstance(this, new string[0], LexicalEnvironment.NewDeclarativeEnvironment(this, ExecutionContext.LexicalEnvironment), StrictModeScope.IsStrictModeCode);
             Global.FastAddProperty("eval", Eval, true, false, true);
 
             _statements = new StatementInterpreter(this);

+ 17 - 4
Jint/Native/Function/EvalFunctionInstance.cs

@@ -1,6 +1,4 @@
-using System.Net.NetworkInformation;
-using Jint.Native.Object;
-using Jint.Parser;
+using Jint.Parser;
 using Jint.Runtime;
 using Jint.Runtime.Environments;
 
@@ -10,12 +8,17 @@ namespace Jint.Native.Function
     {
         private readonly Engine _engine;
 
-        public EvalFunctionInstance(Engine engine, ObjectInstance prototype, string[] parameters, LexicalEnvironment scope, bool strict) : base(engine, parameters, scope, strict)
+        public EvalFunctionInstance(Engine engine, string[] parameters, LexicalEnvironment scope, bool strict) : base(engine, parameters, scope, strict)
         {
             _engine = engine;
         }
 
         public override object Call(object thisObject, object[] arguments)
+        {
+            return Call(thisObject, arguments, false);
+        }
+
+        public object Call(object thisObject, object[] arguments, bool directCall)
         {
             var code = TypeConverter.ToString(arguments.At(0));
 
@@ -31,6 +34,11 @@ namespace Jint.Native.Function
 
                         try
                         {
+                            if (!directCall)
+                            {
+                                Engine.EnterExecutionContext(Engine.GlobalEnvironment, Engine.GlobalEnvironment, Engine.Global);
+                            }
+
                             if (StrictModeScope.IsStrictModeCode)
                             {
                                 strictVarEnv = LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment);
@@ -56,6 +64,11 @@ namespace Jint.Native.Function
                             {
                                 Engine.LeaveExecutionContext();
                             }
+
+                            if (!directCall)
+                            {
+                                Engine.LeaveExecutionContext();
+                            }
                         }
                     }
                 }

+ 6 - 0
Jint/Runtime/ExpressionIntepreter.cs

@@ -825,6 +825,12 @@ namespace Jint.Runtime
                 thisObject = Undefined.Instance;
             }
 
+            // is it a direct call to eval ? http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.1.1
+            if (r != null && r.GetReferencedName() == "eval" && callable is EvalFunctionInstance)
+            {
+                return ((EvalFunctionInstance) callable).Call(thisObject, arguments, true);
+            }
+            
             return callable.Call(thisObject, arguments);
         }