Browse Source

Prototyping the eval() function

Sebastien Ros 12 years ago
parent
commit
3e7e4e94f2

+ 10 - 1
Jint.Tests/Runtime/EngineTests.cs

@@ -331,7 +331,16 @@ namespace Jint.Tests.Runtime
                 }
                 }
 
 
                 assert(f1() === undefined);
                 assert(f1() === undefined);
-        ");
+            ");
+        }
+
+        [Fact]
+        public void EvalFunctionParseAndExecuteCode()
+        {
+            RunTest(@"
+                var x = 0;
+                eval('assert(x == 0)');
+            ");
         }
         }
 
 
         /*
         /*

+ 4 - 0
Jint/Engine.cs

@@ -79,6 +79,9 @@ namespace Jint
                 }
                 }
             }
             }
 
 
+            Eval = new EvalFunctionInstance(this, new ObjectInstance(null), new Identifier[0], LexicalEnvironment.NewDeclarativeEnvironment(ExecutionContext.LexicalEnvironment), Options.IsStrict());
+            Global.FastDefineDataDescriptor("eval", Eval);
+
             _statements = new StatementInterpreter(this);
             _statements = new StatementInterpreter(this);
             _expressions = new ExpressionInterpreter(this);
             _expressions = new ExpressionInterpreter(this);
         }
         }
@@ -97,6 +100,7 @@ namespace Jint
         public NumberConstructor Number { get; private set; }
         public NumberConstructor Number { get; private set; }
         public DateConstructor Date { get; private set; }
         public DateConstructor Date { get; private set; }
         public MathInstance Math { get; private set; }
         public MathInstance Math { get; private set; }
+        public EvalFunctionInstance Eval { get; private set; }
 
 
         public ExecutionContext ExecutionContext { get { return _executionContexts.Peek(); } }
         public ExecutionContext ExecutionContext { get { return _executionContexts.Peek(); } }
 
 

+ 1 - 0
Jint/Jint.csproj

@@ -48,6 +48,7 @@
     <Compile Include="Native\Errors\ReferenceError.cs" />
     <Compile Include="Native\Errors\ReferenceError.cs" />
     <Compile Include="Native\Errors\TypeError.cs" />
     <Compile Include="Native\Errors\TypeError.cs" />
     <Compile Include="Native\Function\ArgumentsObject.cs" />
     <Compile Include="Native\Function\ArgumentsObject.cs" />
+    <Compile Include="Native\Function\EvalFunctionInstance.cs" />
     <Compile Include="Native\Function\FunctionConstructor.cs" />
     <Compile Include="Native\Function\FunctionConstructor.cs" />
     <Compile Include="Native\Function\FunctionInstance.cs" />
     <Compile Include="Native\Function\FunctionInstance.cs" />
     <Compile Include="Native\Function\FunctionShim.cs" />
     <Compile Include="Native\Function\FunctionShim.cs" />

+ 31 - 0
Jint/Native/Function/EvalFunctionInstance.cs

@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Jint.Native.Object;
+using Jint.Parser;
+using Jint.Parser.Ast;
+using Jint.Runtime;
+using Jint.Runtime.Environments;
+
+namespace Jint.Native.Function
+{
+    public class EvalFunctionInstance: FunctionInstance
+    {
+        private readonly Engine _engine;
+
+        public EvalFunctionInstance(Engine engine, ObjectInstance prototype, Identifier[] parameters, LexicalEnvironment scope, bool strict) : base(engine, prototype, parameters, scope, strict)
+        {
+            _engine = engine;
+        }
+
+        public override object Call(object thisObject, object[] arguments)
+        {
+            var code = TypeConverter.ToString(arguments[0]);
+
+            var parser = new JavascriptParser();
+            var program = parser.Parse(code);
+            return _engine.ExecuteStatement(program);
+        }
+    }
+}