JintMemberExpression.cs 5.8 KB

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