JintMemberExpression.cs 5.4 KB

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