Reference.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <LibJS/Runtime/PropertyName.h>
  9. #include <LibJS/Runtime/Value.h>
  10. namespace JS {
  11. class Reference {
  12. public:
  13. Reference() { }
  14. Reference(Value base, const PropertyName& name, bool strict = false)
  15. : m_base(base)
  16. , m_name(name)
  17. , m_strict(strict)
  18. {
  19. }
  20. enum LocalVariableTag { LocalVariable };
  21. Reference(LocalVariableTag, const FlyString& name, bool strict = false)
  22. : m_base(js_null())
  23. , m_name(name)
  24. , m_strict(strict)
  25. , m_local_variable(true)
  26. {
  27. }
  28. enum GlobalVariableTag { GlobalVariable };
  29. Reference(GlobalVariableTag, const FlyString& name, bool strict = false)
  30. : m_base(js_null())
  31. , m_name(name)
  32. , m_strict(strict)
  33. , m_global_variable(true)
  34. {
  35. }
  36. Value base() const { return m_base; }
  37. const PropertyName& name() const { return m_name; }
  38. bool is_strict() const { return m_strict; }
  39. bool is_unresolvable() const { return m_base.is_empty(); }
  40. bool is_property() const
  41. {
  42. return m_base.is_object() || has_primitive_base();
  43. }
  44. bool has_primitive_base() const
  45. {
  46. return m_base.is_boolean() || m_base.is_string() || m_base.is_number();
  47. }
  48. bool is_local_variable() const
  49. {
  50. return m_local_variable;
  51. }
  52. bool is_global_variable() const
  53. {
  54. return m_global_variable;
  55. }
  56. void put(GlobalObject&, Value);
  57. Value get(GlobalObject&);
  58. bool delete_(GlobalObject&);
  59. private:
  60. void throw_reference_error(GlobalObject&);
  61. Value m_base;
  62. PropertyName m_name;
  63. bool m_strict { false };
  64. bool m_local_variable { false };
  65. bool m_global_variable { false };
  66. };
  67. }