JintMemberExpression.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. var strict = StrictModeScope.IsStrictModeCode;
  53. if (_objectExpression is JintIdentifierExpression identifierExpression)
  54. {
  55. var identifier = identifierExpression.Identifier;
  56. baseReferenceName = identifier.Key.Name;
  57. var env = engine.ExecutionContext.LexicalEnvironment;
  58. JintEnvironment.TryGetIdentifierEnvironmentWithBindingValue(
  59. env,
  60. identifier,
  61. strict,
  62. out _,
  63. out baseValue);
  64. }
  65. else if (_objectExpression is JintThisExpression thisExpression)
  66. {
  67. baseValue = (JsValue?) thisExpression.GetValue(context);
  68. }
  69. else if (_objectExpression is JintSuperExpression)
  70. {
  71. var env = (FunctionEnvironment) engine.ExecutionContext.GetThisEnvironment();
  72. actualThis = env.GetThisBinding();
  73. baseValue = env.GetSuperBase();
  74. }
  75. if (baseValue is null)
  76. {
  77. // fast checks failed
  78. var baseReference = _objectExpression.Evaluate(context);
  79. if (ReferenceEquals(JsValue.Undefined, baseReference))
  80. {
  81. return JsValue.Undefined;
  82. }
  83. if (baseReference is Reference reference)
  84. {
  85. baseReferenceName = reference.ReferencedName;
  86. baseValue = engine.GetValue(reference, returnReferenceToPool: true);
  87. }
  88. else
  89. {
  90. baseValue = engine.GetValue(baseReference, returnReferenceToPool: false);
  91. }
  92. }
  93. if (baseValue.IsNullOrUndefined() && (_memberExpression.Optional || _objectExpression._expression.IsOptional()))
  94. {
  95. return JsValue.Undefined;
  96. }
  97. var property = _determinedProperty ?? _propertyExpression!.GetValue(context);
  98. if (baseValue.IsNullOrUndefined())
  99. {
  100. // we can use base data types securely, object evaluation can mess things up
  101. var referenceName = property.IsPrimitive()
  102. ? TypeConverter.ToString(property)
  103. : _determinedProperty?.ToString() ?? baseReferenceName?.ToString();
  104. TypeConverter.CheckObjectCoercible(engine, baseValue, _memberExpression.Property, referenceName!);
  105. }
  106. if (property.IsPrivateName())
  107. {
  108. return MakePrivateReference(engine, baseValue, property);
  109. }
  110. return context.Engine._referencePool.Rent(baseValue, property, StrictModeScope.IsStrictModeCode, thisValue: actualThis);
  111. }
  112. /// <summary>
  113. /// https://tc39.es/ecma262/#sec-makeprivatereference
  114. /// </summary>
  115. private static Reference MakePrivateReference(Engine engine, JsValue baseValue, JsValue privateIdentifier)
  116. {
  117. var privEnv = engine.ExecutionContext.PrivateEnvironment;
  118. var privateName = privEnv!.ResolvePrivateIdentifier(privateIdentifier.ToString());
  119. return engine._referencePool.Rent(baseValue, privateName!, strict: true, thisValue: null);
  120. }
  121. }