LexicalEnvironment.cpp 4.4 KB

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