JintIdentifierExpression.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Esprima.Ast;
  2. using Jint.Native;
  3. using Jint.Native.Argument;
  4. using Jint.Runtime.Environments;
  5. namespace Jint.Runtime.Interpreter.Expressions
  6. {
  7. internal sealed class JintIdentifierExpression : JintExpression
  8. {
  9. internal readonly EnvironmentRecord.BindingName _expressionName;
  10. private readonly JsValue? _calculatedValue;
  11. public JintIdentifierExpression(Identifier expression) : base(expression)
  12. {
  13. _expressionName = new EnvironmentRecord.BindingName(expression.Name);
  14. if (expression.Name == "undefined")
  15. {
  16. _calculatedValue = JsValue.Undefined;
  17. }
  18. }
  19. public bool HasEvalOrArguments
  20. => _expressionName.Key == KnownKeys.Eval || _expressionName.Key == KnownKeys.Arguments;
  21. protected override ExpressionResult EvaluateInternal(EvaluationContext context)
  22. {
  23. var engine = context.Engine;
  24. var env = engine.ExecutionContext.LexicalEnvironment;
  25. var strict = StrictModeScope.IsStrictModeCode;
  26. var identifierEnvironment = JintEnvironment.TryGetIdentifierEnvironmentWithBinding(env, _expressionName, out var temp)
  27. ? temp
  28. : JsValue.Undefined;
  29. return NormalCompletion(engine._referencePool.Rent(identifierEnvironment, _expressionName.StringValue, strict, thisValue: null));
  30. }
  31. public override Completion GetValue(EvaluationContext context)
  32. {
  33. // need to notify correct node when taking shortcut
  34. context.LastSyntaxElement = _expression;
  35. if (_calculatedValue is not null)
  36. {
  37. return new(CompletionType.Normal, _calculatedValue, _expression);
  38. }
  39. var strict = StrictModeScope.IsStrictModeCode;
  40. var engine = context.Engine;
  41. var env = engine.ExecutionContext.LexicalEnvironment;
  42. if (JintEnvironment.TryGetIdentifierEnvironmentWithBindingValue(
  43. env,
  44. _expressionName,
  45. strict,
  46. out _,
  47. out var value))
  48. {
  49. if (value is null)
  50. {
  51. ExceptionHelper.ThrowReferenceError(engine.Realm, _expressionName.Key.Name + " has not been initialized");
  52. }
  53. }
  54. else
  55. {
  56. var reference = engine._referencePool.Rent(JsValue.Undefined, _expressionName.StringValue, strict, thisValue: null);
  57. value = engine.GetValue(reference, true);
  58. }
  59. // make sure arguments access freezes state
  60. if (value is ArgumentsInstance argumentsInstance)
  61. {
  62. argumentsInstance.Materialize();
  63. }
  64. return new(CompletionType.Normal, value, _expression);
  65. }
  66. }
  67. }