Reference.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. internal string _name;
  15. internal bool _strict;
  16. public Reference(JsValue baseValue, string 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 string GetReferencedName()
  28. {
  29. return _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. if (_baseValue._type != Types.Object && _baseValue._type != Types.None)
  51. {
  52. // primitive
  53. return true;
  54. }
  55. return _baseValue._type == Types.Object && !(_baseValue is EnvironmentRecord);
  56. }
  57. internal Reference Reassign(JsValue baseValue, string name, bool strict)
  58. {
  59. _baseValue = baseValue;
  60. _name = name;
  61. _strict = strict;
  62. return this;
  63. }
  64. internal void AssertValid(Engine engine)
  65. {
  66. if(_strict && (_name == "eval" || _name == "arguments") && _baseValue is EnvironmentRecord)
  67. {
  68. ExceptionHelper.ThrowSyntaxError(engine);
  69. }
  70. }
  71. }
  72. }