JintMemberExpression.cs 5.8 KB

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