LexicalEnvironment.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/FlyString.h>
  28. #include <AK/HashMap.h>
  29. #include <LibJS/Runtime/Cell.h>
  30. #include <LibJS/Runtime/Value.h>
  31. namespace JS {
  32. struct Variable {
  33. Value value;
  34. DeclarationKind declaration_kind;
  35. };
  36. class LexicalEnvironment final : public Cell {
  37. public:
  38. enum class ThisBindingStatus {
  39. Lexical,
  40. Initialized,
  41. Uninitialized,
  42. };
  43. enum class EnvironmentRecordType {
  44. Declarative,
  45. Function,
  46. Global,
  47. Object,
  48. Module,
  49. };
  50. LexicalEnvironment();
  51. LexicalEnvironment(EnvironmentRecordType);
  52. LexicalEnvironment(HashMap<FlyString, Variable> variables, LexicalEnvironment* parent);
  53. LexicalEnvironment(HashMap<FlyString, Variable> variables, LexicalEnvironment* parent, EnvironmentRecordType);
  54. virtual ~LexicalEnvironment() override;
  55. LexicalEnvironment* parent() const { return m_parent; }
  56. Optional<Variable> get(const FlyString&) const;
  57. void set(const FlyString&, Variable);
  58. void clear();
  59. const HashMap<FlyString, Variable>& variables() const { return m_variables; }
  60. void set_home_object(Value object) { m_home_object = object; }
  61. bool has_super_binding() const;
  62. Value get_super_base();
  63. bool has_this_binding() const;
  64. ThisBindingStatus this_binding_status() const { return m_this_binding_status; }
  65. Value get_this_binding() const;
  66. void bind_this_value(Value this_value);
  67. // Not a standard operation.
  68. void replace_this_binding(Value this_value) { m_this_value = this_value; }
  69. Value new_target() const { return m_new_target; };
  70. void set_new_target(Value new_target) { m_new_target = new_target; }
  71. Function* current_function() const { return m_current_function; }
  72. void set_current_function(Function& function) { m_current_function = &function; }
  73. private:
  74. virtual const char* class_name() const override { return "LexicalEnvironment"; }
  75. virtual void visit_children(Visitor&) override;
  76. LexicalEnvironment* m_parent { nullptr };
  77. HashMap<FlyString, Variable> m_variables;
  78. EnvironmentRecordType m_environment_record_type = EnvironmentRecordType::Declarative;
  79. ThisBindingStatus m_this_binding_status = ThisBindingStatus::Uninitialized;
  80. Value m_home_object;
  81. Value m_this_value;
  82. Value m_new_target;
  83. // Corresponds to [[FunctionObject]]
  84. Function* m_current_function { nullptr };
  85. };
  86. }