Reference.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. internal JsValue _baseValue;
  14. private Key _name;
  15. internal bool _strict;
  16. public Reference(JsValue baseValue, in Key name, bool strict)
  17. {
  18. _baseValue = baseValue;
  19. _name = name;
  20. }
  21. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  22. public JsValue GetBase()
  23. {
  24. return _baseValue;
  25. }
  26. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  27. public ref readonly Key GetReferencedName()
  28. {
  29. return ref _name;
  30. }
  31. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  32. public bool IsStrict()
  33. {
  34. return _strict;
  35. }
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public bool HasPrimitiveBase()
  38. {
  39. return _baseValue._type != Types.Object && _baseValue._type != Types.None;
  40. }
  41. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  42. public bool IsUnresolvableReference()
  43. {
  44. return _baseValue._type == Types.Undefined;
  45. }
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public bool IsPropertyReference()
  48. {
  49. // http://www.ecma-international.org/ecma-262/5.1/#sec-8.7
  50. return _baseValue._type != Types.Object && _baseValue._type != Types.None
  51. || _baseValue._type == Types.Object && !(_baseValue is EnvironmentRecord);
  52. }
  53. internal Reference Reassign(JsValue baseValue, in Key name, bool strict)
  54. {
  55. _baseValue = baseValue;
  56. _name = name;
  57. _strict = strict;
  58. return this;
  59. }
  60. internal void AssertValid(Engine engine)
  61. {
  62. if (_strict && (_name == KnownKeys.Eval || _name == KnownKeys.Arguments) && _baseValue is EnvironmentRecord)
  63. {
  64. ExceptionHelper.ThrowSyntaxError(engine);
  65. }
  66. }
  67. }
  68. }