EvalFunctionInstance.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Jint.Parser;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Environments;
  4. namespace Jint.Native.Function
  5. {
  6. public class EvalFunctionInstance: FunctionInstance
  7. {
  8. private readonly Engine _engine;
  9. public EvalFunctionInstance(Engine engine, string[] parameters, LexicalEnvironment scope, bool strict) : base(engine, parameters, scope, strict)
  10. {
  11. _engine = engine;
  12. }
  13. public override object Call(object thisObject, object[] arguments)
  14. {
  15. return Call(thisObject, arguments, false);
  16. }
  17. public object Call(object thisObject, object[] arguments, bool directCall)
  18. {
  19. var code = TypeConverter.ToString(arguments.At(0));
  20. try
  21. {
  22. var parser = new JavaScriptParser(StrictModeScope.IsStrictModeCode);
  23. var program = parser.Parse(code);
  24. using (new StrictModeScope(program.Strict))
  25. {
  26. using (new EvalCodeScope())
  27. {
  28. LexicalEnvironment strictVarEnv = null;
  29. try
  30. {
  31. if (!directCall)
  32. {
  33. Engine.EnterExecutionContext(Engine.GlobalEnvironment, Engine.GlobalEnvironment, Engine.Global);
  34. }
  35. if (StrictModeScope.IsStrictModeCode)
  36. {
  37. strictVarEnv = LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment);
  38. Engine.EnterExecutionContext(strictVarEnv, strictVarEnv, Engine.ExecutionContext.ThisBinding);
  39. }
  40. Engine.DeclarationBindingInstantiation(DeclarationBindingType.EvalCode, program.FunctionDeclarations, program.VariableDeclarations, this, arguments);
  41. var result = _engine.ExecuteStatement(program);
  42. if (result.Type == Completion.Throw)
  43. {
  44. throw new JavaScriptException(result.Value);
  45. }
  46. else
  47. {
  48. return result.Value ?? Undefined.Instance;
  49. }
  50. }
  51. finally
  52. {
  53. if (strictVarEnv != null)
  54. {
  55. Engine.LeaveExecutionContext();
  56. }
  57. if (!directCall)
  58. {
  59. Engine.LeaveExecutionContext();
  60. }
  61. }
  62. }
  63. }
  64. }
  65. catch (ParserError)
  66. {
  67. throw new JavaScriptException(Engine.SyntaxError);
  68. }
  69. }
  70. }
  71. }