EvalFunctionInstance.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Esprima;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Environments;
  4. namespace Jint.Native.Function
  5. {
  6. public class EvalFunctionInstance: FunctionInstance
  7. {
  8. private static readonly ParserOptions ParserOptions = new ParserOptions { AdaptRegexp = true, Tolerant = false };
  9. private readonly Engine _engine;
  10. public EvalFunctionInstance(Engine engine, string[] parameters, LexicalEnvironment scope, bool strict) : base(engine, parameters, scope, strict)
  11. {
  12. _engine = engine;
  13. Prototype = Engine.Function.PrototypeObject;
  14. FastAddProperty("length", 1, false, false, false);
  15. }
  16. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  17. {
  18. return Call(thisObject, arguments, false);
  19. }
  20. public JsValue Call(JsValue thisObject, JsValue[] arguments, bool directCall)
  21. {
  22. if (arguments.At(0).Type != Types.String)
  23. {
  24. return arguments.At(0);
  25. }
  26. var code = TypeConverter.ToString(arguments.At(0));
  27. try
  28. {
  29. var parser = new JavaScriptParser(code, ParserOptions);
  30. var program = parser.ParseProgram(StrictModeScope.IsStrictModeCode);
  31. using (new StrictModeScope(program.Strict))
  32. {
  33. using (new EvalCodeScope())
  34. {
  35. LexicalEnvironment strictVarEnv = null;
  36. try
  37. {
  38. if (!directCall)
  39. {
  40. Engine.EnterExecutionContext(Engine.GlobalEnvironment, Engine.GlobalEnvironment, Engine.Global);
  41. }
  42. if (StrictModeScope.IsStrictModeCode)
  43. {
  44. strictVarEnv = LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment);
  45. Engine.EnterExecutionContext(strictVarEnv, strictVarEnv, Engine.ExecutionContext.ThisBinding);
  46. }
  47. Engine.DeclarationBindingInstantiation(DeclarationBindingType.EvalCode, program.HoistingScope.FunctionDeclarations, program.HoistingScope.VariableDeclarations, this, arguments);
  48. var result = _engine.ExecuteStatement(program);
  49. if (result.Type == Completion.Throw)
  50. {
  51. throw new JavaScriptException(result.GetValueOrDefault())
  52. .SetCallstack(_engine, result.Location);
  53. }
  54. else
  55. {
  56. return result.GetValueOrDefault();
  57. }
  58. }
  59. finally
  60. {
  61. if (strictVarEnv != null)
  62. {
  63. Engine.LeaveExecutionContext();
  64. }
  65. if (!directCall)
  66. {
  67. Engine.LeaveExecutionContext();
  68. }
  69. }
  70. }
  71. }
  72. }
  73. catch (ParserException e)
  74. {
  75. if (e.Description == Messages.InvalidLHSInAssignment)
  76. {
  77. throw new JavaScriptException(Engine.ReferenceError);
  78. }
  79. throw new JavaScriptException(Engine.SyntaxError);
  80. }
  81. }
  82. }
  83. }