EvalFunctionInstance.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Esprima;
  2. using Jint.Native.Argument;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Environments;
  6. namespace Jint.Native.Function
  7. {
  8. public class EvalFunctionInstance: FunctionInstance
  9. {
  10. private static readonly ParserOptions ParserOptions = new ParserOptions { AdaptRegexp = true, Tolerant = false };
  11. public EvalFunctionInstance(Engine engine, string[] parameters, LexicalEnvironment scope, bool strict) : base(engine, parameters, scope, strict)
  12. {
  13. Prototype = Engine.Function.PrototypeObject;
  14. SetOwnProperty("length", new PropertyDescriptor(1, PropertyFlag.AllForbidden));
  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. bool argumentInstanceRented = Engine.DeclarationBindingInstantiation(
  48. DeclarationBindingType.EvalCode,
  49. program.HoistingScope.FunctionDeclarations,
  50. program.HoistingScope.VariableDeclarations,
  51. this,
  52. arguments);
  53. var result = _engine.ExecuteStatement(program);
  54. var value = result.GetValueOrDefault();
  55. // we can safely release arguments if they don't escape the scope
  56. if (argumentInstanceRented
  57. && Engine.ExecutionContext.LexicalEnvironment?.Record is DeclarativeEnvironmentRecord der
  58. && !(result.Value is ArgumentsInstance))
  59. {
  60. der.ReleaseArguments();
  61. }
  62. if (result.Type == CompletionType.Throw)
  63. {
  64. var ex = new JavaScriptException(value).SetCallstack(_engine, result.Location);
  65. throw ex;
  66. }
  67. else
  68. {
  69. return value;
  70. }
  71. }
  72. finally
  73. {
  74. if (strictVarEnv != null)
  75. {
  76. Engine.LeaveExecutionContext();
  77. }
  78. if (!directCall)
  79. {
  80. Engine.LeaveExecutionContext();
  81. }
  82. }
  83. }
  84. }
  85. }
  86. catch (ParserException e)
  87. {
  88. if (e.Description == Messages.InvalidLHSInAssignment)
  89. {
  90. throw new JavaScriptException(Engine.ReferenceError);
  91. }
  92. throw new JavaScriptException(Engine.SyntaxError);
  93. }
  94. }
  95. }
  96. }