EvalFunctionInstance.cs 3.7 KB

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