Reference.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/String.h>
  28. #include <LibJS/Runtime/PropertyName.h>
  29. #include <LibJS/Runtime/Value.h>
  30. namespace JS {
  31. class Reference {
  32. public:
  33. Reference() {}
  34. Reference(Value base, const PropertyName& name, bool strict = false)
  35. : m_base(base)
  36. , m_name(name)
  37. , m_strict(strict)
  38. {
  39. }
  40. enum LocalVariableTag { LocalVariable };
  41. Reference(LocalVariableTag, const String& name, bool strict = false)
  42. : m_base(js_null())
  43. , m_name(name)
  44. , m_strict(strict)
  45. , m_local_variable(true)
  46. {
  47. }
  48. enum GlobalVariableTag { GlobalVariable };
  49. Reference(GlobalVariableTag, const String& name, bool strict = false)
  50. : m_base(js_null())
  51. , m_name(name)
  52. , m_strict(strict)
  53. , m_global_variable(true)
  54. {
  55. }
  56. Value base() const { return m_base; }
  57. const PropertyName& name() const { return m_name; }
  58. bool is_strict() const { return m_strict; }
  59. bool is_unresolvable() const { return m_base.is_undefined(); }
  60. bool is_property() const
  61. {
  62. return m_base.is_object() || has_primitive_base();
  63. }
  64. bool has_primitive_base() const
  65. {
  66. return m_base.is_boolean() || m_base.is_string() || m_base.is_number();
  67. }
  68. bool is_local_variable() const
  69. {
  70. return m_local_variable;
  71. }
  72. bool is_global_variable() const
  73. {
  74. return m_global_variable;
  75. }
  76. void put(Interpreter&, Value);
  77. Value get(Interpreter&);
  78. private:
  79. void throw_reference_error(Interpreter&);
  80. Value m_base { js_undefined() };
  81. PropertyName m_name;
  82. bool m_strict { false };
  83. bool m_local_variable { false };
  84. bool m_global_variable { false };
  85. };
  86. const LogStream& operator<<(const LogStream&, const Value&);
  87. }