LexicalEnvironment.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include <LibJS/Interpreter.h>
  27. #include <LibJS/Runtime/Error.h>
  28. #include <LibJS/Runtime/Function.h>
  29. #include <LibJS/Runtime/GlobalObject.h>
  30. #include <LibJS/Runtime/LexicalEnvironment.h>
  31. #include <LibJS/Runtime/Value.h>
  32. namespace JS {
  33. LexicalEnvironment::LexicalEnvironment()
  34. {
  35. }
  36. LexicalEnvironment::LexicalEnvironment(EnvironmentRecordType environment_record_type)
  37. : m_environment_record_type(environment_record_type)
  38. {
  39. }
  40. LexicalEnvironment::LexicalEnvironment(HashMap<FlyString, Variable> variables, LexicalEnvironment* parent)
  41. : m_parent(parent)
  42. , m_variables(move(variables))
  43. {
  44. }
  45. LexicalEnvironment::LexicalEnvironment(HashMap<FlyString, Variable> variables, LexicalEnvironment* parent, EnvironmentRecordType environment_record_type)
  46. : m_environment_record_type(environment_record_type)
  47. , m_parent(parent)
  48. , m_variables(move(variables))
  49. {
  50. }
  51. LexicalEnvironment::~LexicalEnvironment()
  52. {
  53. }
  54. void LexicalEnvironment::visit_edges(Visitor& visitor)
  55. {
  56. Cell::visit_edges(visitor);
  57. visitor.visit(m_parent);
  58. visitor.visit(m_this_value);
  59. visitor.visit(m_home_object);
  60. visitor.visit(m_new_target);
  61. visitor.visit(m_current_function);
  62. for (auto& it : m_variables)
  63. visitor.visit(it.value.value);
  64. }
  65. Optional<Variable> LexicalEnvironment::get(const FlyString& name) const
  66. {
  67. ASSERT(type() != EnvironmentRecordType::Global);
  68. return m_variables.get(name);
  69. }
  70. void LexicalEnvironment::set(GlobalObject& global_object, const FlyString& name, Variable variable)
  71. {
  72. if (type() == EnvironmentRecordType::Global)
  73. global_object.put(name, variable.value);
  74. else
  75. m_variables.set(name, variable);
  76. }
  77. bool LexicalEnvironment::has_super_binding() const
  78. {
  79. return m_environment_record_type == EnvironmentRecordType::Function && this_binding_status() != ThisBindingStatus::Lexical && m_home_object.is_object();
  80. }
  81. Value LexicalEnvironment::get_super_base()
  82. {
  83. ASSERT(has_super_binding());
  84. if (m_home_object.is_object())
  85. return m_home_object.as_object().prototype();
  86. return {};
  87. }
  88. bool LexicalEnvironment::has_this_binding() const
  89. {
  90. // More like "is_capable_of_having_a_this_binding".
  91. switch (m_environment_record_type) {
  92. case EnvironmentRecordType::Declarative:
  93. case EnvironmentRecordType::Object:
  94. return false;
  95. case EnvironmentRecordType::Function:
  96. return this_binding_status() != ThisBindingStatus::Lexical;
  97. case EnvironmentRecordType::Module:
  98. case EnvironmentRecordType::Global:
  99. return true;
  100. }
  101. ASSERT_NOT_REACHED();
  102. }
  103. Value LexicalEnvironment::get_this_binding(GlobalObject& global_object) const
  104. {
  105. ASSERT(has_this_binding());
  106. if (this_binding_status() == ThisBindingStatus::Uninitialized) {
  107. vm().throw_exception<ReferenceError>(global_object, ErrorType::ThisHasNotBeenInitialized);
  108. return {};
  109. }
  110. return m_this_value;
  111. }
  112. void LexicalEnvironment::bind_this_value(GlobalObject& global_object, Value this_value)
  113. {
  114. ASSERT(has_this_binding());
  115. if (m_this_binding_status == ThisBindingStatus::Initialized) {
  116. vm().throw_exception<ReferenceError>(global_object, ErrorType::ThisIsAlreadyInitialized);
  117. return;
  118. }
  119. m_this_value = this_value;
  120. m_this_binding_status = ThisBindingStatus::Initialized;
  121. }
  122. }