LexicalEnvironment.cpp 4.3 KB

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