EvalFunctionInstance.cs 2.6 KB

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