JintMemberExpression.cs 5.8 KB

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