Reference.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Jint.Native;
  4. using Jint.Runtime.Environments;
  5. namespace Jint.Runtime.References
  6. {
  7. /// <summary>
  8. /// Represents the Reference Specification Type
  9. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.7
  10. /// </summary>
  11. public sealed class Reference
  12. {
  13. private JsValue _baseValue;
  14. private JsValue _property;
  15. internal bool _strict;
  16. private JsValue? _thisValue;
  17. public Reference(JsValue baseValue, JsValue property, bool strict, JsValue? thisValue = null)
  18. {
  19. _baseValue = baseValue;
  20. _property = property;
  21. _thisValue = thisValue;
  22. }
  23. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  24. public JsValue GetBase() => _baseValue;
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. public JsValue GetReferencedName() => _property;
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. public bool IsStrictReference() => _strict;
  29. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  30. public bool HasPrimitiveBase()
  31. {
  32. return (_baseValue._type & InternalTypes.Primitive) != 0;
  33. }
  34. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  35. public bool IsUnresolvableReference()
  36. {
  37. return _baseValue._type == InternalTypes.Undefined;
  38. }
  39. public bool IsSuperReference()
  40. {
  41. return _thisValue is not null;
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public bool IsPropertyReference()
  45. {
  46. // https://tc39.es/ecma262/#sec-ispropertyreference
  47. return (_baseValue._type & (InternalTypes.Primitive | InternalTypes.Object)) != 0;
  48. }
  49. public JsValue GetThisValue()
  50. {
  51. if (IsSuperReference())
  52. {
  53. return _thisValue!;
  54. }
  55. return GetBase();
  56. }
  57. internal Reference Reassign(JsValue baseValue, JsValue name, bool strict, JsValue? thisValue)
  58. {
  59. _baseValue = baseValue;
  60. _property = name;
  61. _strict = strict;
  62. _thisValue = thisValue;
  63. return this;
  64. }
  65. internal void AssertValid(Realm realm)
  66. {
  67. if (_strict
  68. && (_baseValue._type & InternalTypes.ObjectEnvironmentRecord) != 0
  69. && (_property == CommonProperties.Eval || _property == CommonProperties.Arguments))
  70. {
  71. ExceptionHelper.ThrowSyntaxError(realm);
  72. }
  73. }
  74. internal void InitializeReferencedBinding(JsValue value)
  75. {
  76. ((EnvironmentRecord) _baseValue).InitializeBinding(TypeConverter.ToString(_property), value);
  77. }
  78. }
  79. }