123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using Jint.Parser;
- using Jint.Runtime;
- using Jint.Runtime.Environments;
- namespace Jint.Native.Function
- {
- public class EvalFunctionInstance: FunctionInstance
- {
- private readonly Engine _engine;
- public EvalFunctionInstance(Engine engine, string[] parameters, LexicalEnvironment scope, bool strict) : base(engine, parameters, scope, strict)
- {
- _engine = engine;
- Prototype = Engine.Function.PrototypeObject;
- FastAddProperty("length", 1, false, false, false);
- }
- public override JsValue Call(JsValue thisObject, JsValue[] arguments)
- {
- return Call(thisObject, arguments, false);
- }
- public JsValue Call(JsValue thisObject, JsValue[] arguments, bool directCall)
- {
- if (arguments.At(0).Type != Types.String)
- {
- return arguments.At(0);
- }
- var code = TypeConverter.ToString(arguments.At(0));
- try
- {
- var parser = new JavaScriptParser(StrictModeScope.IsStrictModeCode);
- var program = parser.Parse(code);
- using (new StrictModeScope(program.Strict))
- {
- using (new EvalCodeScope())
- {
- LexicalEnvironment strictVarEnv = null;
- try
- {
- if (!directCall)
- {
- Engine.EnterExecutionContext(Engine.GlobalEnvironment, Engine.GlobalEnvironment, Engine.Global);
- }
- if (StrictModeScope.IsStrictModeCode)
- {
- strictVarEnv = LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment);
- Engine.EnterExecutionContext(strictVarEnv, strictVarEnv, Engine.ExecutionContext.ThisBinding);
- }
- Engine.DeclarationBindingInstantiation(DeclarationBindingType.EvalCode, program.FunctionDeclarations, program.VariableDeclarations, this, arguments);
-
- var result = _engine.ExecuteStatement(program);
- if (result.Type == Completion.Throw)
- {
- throw new JavaScriptException(result.GetValueOrDefault())
- .SetCallstack(_engine, result.Location);
- }
- else
- {
- return result.GetValueOrDefault();
- }
- }
- finally
- {
- if (strictVarEnv != null)
- {
- Engine.LeaveExecutionContext();
- }
- if (!directCall)
- {
- Engine.LeaveExecutionContext();
- }
- }
- }
- }
- }
- catch (ParserException)
- {
- throw new JavaScriptException(Engine.SyntaxError);
- }
- }
- }
- }
|