Reference.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. public Reference(JsValue baseValue, JsValue property, bool strict)
  17. {
  18. _baseValue = baseValue;
  19. _property = property;
  20. }
  21. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  22. public JsValue GetBase() => _baseValue;
  23. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  24. public JsValue GetReferencedName() => _property;
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. public bool IsStrictReference() => _strict;
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. public bool HasPrimitiveBase()
  29. {
  30. return (_baseValue._type & InternalTypes.Primitive) != 0;
  31. }
  32. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. public bool IsUnresolvableReference()
  34. {
  35. return _baseValue._type == InternalTypes.Undefined;
  36. }
  37. public bool IsSuperReference()
  38. {
  39. // TODO super not implemented
  40. return false;
  41. }
  42. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  43. public bool IsPropertyReference()
  44. {
  45. // https://tc39.es/ecma262/#sec-ispropertyreference
  46. return (_baseValue._type & (InternalTypes.Primitive | InternalTypes.Object)) != 0;
  47. }
  48. public JsValue GetThisValue()
  49. {
  50. if (IsSuperReference())
  51. {
  52. return ExceptionHelper.ThrowNotImplementedException<JsValue>();
  53. }
  54. return GetBase();
  55. }
  56. internal Reference Reassign(JsValue baseValue, JsValue name, bool strict)
  57. {
  58. _baseValue = baseValue;
  59. _property = name;
  60. _strict = strict;
  61. return this;
  62. }
  63. internal void AssertValid(Engine engine)
  64. {
  65. if (_strict
  66. && (_baseValue._type & InternalTypes.ObjectEnvironmentRecord) != 0
  67. && (_property == CommonProperties.Eval || _property == CommonProperties.Arguments))
  68. {
  69. ExceptionHelper.ThrowSyntaxError(engine);
  70. }
  71. }
  72. internal void InitializeReferencedBinding(JsValue value)
  73. {
  74. ((EnvironmentRecord) _baseValue).InitializeBinding(TypeConverter.ToString(_property), value);
  75. }
  76. }
  77. }