Reference.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. enum CallFrameArgumentTag { CallFrameArgument };
  37. Reference(CallFrameArgumentTag, size_t index, FlyString const& name)
  38. : m_base(js_null())
  39. , m_name(name)
  40. , m_call_frame_argument_index(index)
  41. , m_local_variable(true)
  42. {
  43. }
  44. Value base() const { return m_base; }
  45. const PropertyName& name() const { return m_name; }
  46. bool is_strict() const { return m_strict; }
  47. bool is_unresolvable() const { return m_base.is_empty(); }
  48. bool is_property() const
  49. {
  50. return m_base.is_object() || has_primitive_base();
  51. }
  52. bool has_primitive_base() const
  53. {
  54. return m_base.is_boolean() || m_base.is_string() || m_base.is_number();
  55. }
  56. bool is_local_variable() const
  57. {
  58. return m_local_variable;
  59. }
  60. bool is_global_variable() const
  61. {
  62. return m_global_variable;
  63. }
  64. void put(GlobalObject&, Value);
  65. Value get(GlobalObject&);
  66. bool delete_(GlobalObject&);
  67. private:
  68. void throw_reference_error(GlobalObject&);
  69. Value m_base;
  70. PropertyName m_name;
  71. Optional<size_t> m_call_frame_argument_index;
  72. bool m_strict { false };
  73. bool m_local_variable { false };
  74. bool m_global_variable { false };
  75. };
  76. }