JintIdentifierExpression.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Jint.Native;
  2. using Jint.Runtime.Environments;
  3. using Jint.Runtime.References;
  4. namespace Jint.Runtime.Interpreter.Expressions
  5. {
  6. internal sealed class JintIdentifierExpression : JintExpression
  7. {
  8. private readonly Key _expressionName;
  9. private readonly JsValue _calculatedValue;
  10. public JintIdentifierExpression(Engine engine, Esprima.Ast.Identifier expression) : base(engine, expression)
  11. {
  12. _expressionName = expression.Name;
  13. if (expression.Name == "undefined")
  14. {
  15. _calculatedValue = JsValue.Undefined;
  16. }
  17. }
  18. public ref readonly Key ExpressionName => ref _expressionName;
  19. protected override object EvaluateInternal()
  20. {
  21. var env = _engine.ExecutionContext.LexicalEnvironment;
  22. var strict = StrictModeScope.IsStrictModeCode;
  23. return LexicalEnvironment.GetIdentifierReference(env, _expressionName, strict);
  24. }
  25. public override JsValue GetValue()
  26. {
  27. // need to notify correct node when taking shortcut
  28. _engine._lastSyntaxNode = _expression;
  29. if (!(_calculatedValue is null))
  30. {
  31. return _calculatedValue;
  32. }
  33. var strict = StrictModeScope.IsStrictModeCode;
  34. return TryGetIdentifierEnvironmentWithBindingValue(strict, _expressionName, out _, out var value)
  35. ? value
  36. : _engine.GetValue(new Reference(JsValue.Undefined, _expressionName, strict), true);
  37. }
  38. }
  39. }