JintMemberExpression.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using Jint.Native;
  2. using Jint.Runtime.Environments;
  3. namespace Jint.Runtime.Interpreter.Expressions;
  4. /// <summary>
  5. /// http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.1
  6. /// </summary>
  7. internal sealed class JintMemberExpression : JintExpression
  8. {
  9. private readonly MemberExpression _memberExpression;
  10. private JintExpression _objectExpression = null!;
  11. private JintExpression? _propertyExpression;
  12. private JsValue? _determinedProperty;
  13. private bool _initialized;
  14. private static readonly JsValue _nullMarker = new JsString("NULL MARKER");
  15. public JintMemberExpression(MemberExpression expression) : base(expression)
  16. {
  17. _memberExpression = (MemberExpression) _expression;
  18. }
  19. internal static JsValue InitializeDeterminedProperty(MemberExpression expression, bool cache)
  20. {
  21. JsValue? property = null;
  22. if (!expression.Computed)
  23. {
  24. if (expression.Property is Identifier identifier)
  25. {
  26. property = cache ? JsString.CachedCreate(identifier.Name) : JsString.Create(identifier.Name);
  27. }
  28. }
  29. else if (expression.Property.Type == NodeType.Literal)
  30. {
  31. property = JintLiteralExpression.ConvertToJsValue((Literal) expression.Property);
  32. }
  33. return property ?? _nullMarker;
  34. }
  35. protected override object EvaluateInternal(EvaluationContext context)
  36. {
  37. if (!_initialized)
  38. {
  39. _objectExpression = Build(_memberExpression.Object);
  40. _determinedProperty ??= _expression.UserData as JsValue ?? InitializeDeterminedProperty(_memberExpression, cache: false);
  41. if (ReferenceEquals(_determinedProperty, _nullMarker))
  42. {
  43. _propertyExpression = Build(_memberExpression.Property);
  44. _determinedProperty = null;
  45. }
  46. _initialized = true;
  47. }
  48. JsValue? actualThis = null;
  49. object? baseReferenceName = null;
  50. JsValue? baseValue = null;
  51. var engine = context.Engine;
  52. if (_objectExpression is JintIdentifierExpression identifierExpression)
  53. {
  54. var identifier = identifierExpression.Identifier;
  55. baseReferenceName = identifier.Key.Name;
  56. var env = engine.ExecutionContext.LexicalEnvironment;
  57. JintEnvironment.TryGetIdentifierEnvironmentWithBindingValue(
  58. env,
  59. identifier,
  60. out _,
  61. out baseValue);
  62. }
  63. else if (_objectExpression is JintThisExpression thisExpression)
  64. {
  65. baseValue = (JsValue?) thisExpression.GetValue(context);
  66. }
  67. else if (_objectExpression is JintSuperExpression)
  68. {
  69. var env = (FunctionEnvironment) engine.ExecutionContext.GetThisEnvironment();
  70. actualThis = env.GetThisBinding();
  71. baseValue = env.GetSuperBase();
  72. }
  73. if (baseValue is null)
  74. {
  75. // fast checks failed
  76. var baseReference = _objectExpression.Evaluate(context);
  77. if (ReferenceEquals(JsValue.Undefined, baseReference))
  78. {
  79. return JsValue.Undefined;
  80. }
  81. if (baseReference is Reference reference)
  82. {
  83. baseReferenceName = reference.ReferencedName;
  84. baseValue = engine.GetValue(reference, returnReferenceToPool: true);
  85. }
  86. else
  87. {
  88. baseValue = engine.GetValue(baseReference, returnReferenceToPool: false);
  89. }
  90. }
  91. if (baseValue.IsNullOrUndefined() && (_memberExpression.Optional || _objectExpression._expression.IsOptional()))
  92. {
  93. return JsValue.Undefined;
  94. }
  95. var property = _determinedProperty ?? _propertyExpression!.GetValue(context);
  96. if (baseValue.IsNullOrUndefined())
  97. {
  98. // we can use base data types securely, object evaluation can mess things up
  99. var referenceName = property.IsPrimitive()
  100. ? TypeConverter.ToString(property)
  101. : _determinedProperty?.ToString() ?? baseReferenceName?.ToString();
  102. TypeConverter.CheckObjectCoercible(engine, baseValue, _memberExpression.Property, referenceName!);
  103. }
  104. if (property.IsPrivateName())
  105. {
  106. return MakePrivateReference(engine, baseValue, property);
  107. }
  108. return context.Engine._referencePool.Rent(baseValue, property, StrictModeScope.IsStrictModeCode, thisValue: actualThis);
  109. }
  110. /// <summary>
  111. /// https://tc39.es/ecma262/#sec-makeprivatereference
  112. /// </summary>
  113. private static Reference MakePrivateReference(Engine engine, JsValue baseValue, JsValue privateIdentifier)
  114. {
  115. var privEnv = engine.ExecutionContext.PrivateEnvironment;
  116. var privateName = privEnv!.ResolvePrivateIdentifier(privateIdentifier.ToString());
  117. return engine._referencePool.Rent(baseValue, privateName!, strict: true, thisValue: null);
  118. }
  119. }