EvalFunctionInstance.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. Prototype = Engine.Function.PrototypeObject;
  13. FastAddProperty("length", 1, false, false, false);
  14. }
  15. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  16. {
  17. return Call(thisObject, arguments, false);
  18. }
  19. public JsValue Call(JsValue thisObject, JsValue[] arguments, bool directCall)
  20. {
  21. if (arguments.At(0).Type != Types.String)
  22. {
  23. return arguments.At(0);
  24. }
  25. var code = TypeConverter.ToString(arguments.At(0));
  26. try
  27. {
  28. var parser = new JavaScriptParser(StrictModeScope.IsStrictModeCode);
  29. var program = parser.Parse(code);
  30. using (new StrictModeScope(program.Strict))
  31. {
  32. using (new EvalCodeScope())
  33. {
  34. LexicalEnvironment strictVarEnv = null;
  35. try
  36. {
  37. if (!directCall)
  38. {
  39. Engine.EnterExecutionContext(Engine.GlobalEnvironment, Engine.GlobalEnvironment, Engine.Global);
  40. }
  41. if (StrictModeScope.IsStrictModeCode)
  42. {
  43. strictVarEnv = LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment);
  44. Engine.EnterExecutionContext(strictVarEnv, strictVarEnv, Engine.ExecutionContext.ThisBinding);
  45. }
  46. Engine.DeclarationBindingInstantiation(DeclarationBindingType.EvalCode, program.FunctionDeclarations, program.VariableDeclarations, this, arguments);
  47. var result = _engine.ExecuteStatement(program);
  48. if (result.Type == Completion.Throw)
  49. {
  50. throw new JavaScriptException(result.GetValueOrDefault())
  51. .SetCallstack(_engine, result.Location);
  52. }
  53. else
  54. {
  55. return result.GetValueOrDefault();
  56. }
  57. }
  58. finally
  59. {
  60. if (strictVarEnv != null)
  61. {
  62. Engine.LeaveExecutionContext();
  63. }
  64. if (!directCall)
  65. {
  66. Engine.LeaveExecutionContext();
  67. }
  68. }
  69. }
  70. }
  71. }
  72. catch (ParserException)
  73. {
  74. throw new JavaScriptException(Engine.SyntaxError);
  75. }
  76. }
  77. }
  78. }