1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:37:35 +00:00

LibJS: Cache access to bindings in the global environment

This patch adds a special EnvironmentCoordinate::global_marker value
that signifies that a binding lookup ended up searching the global
environment. It doesn't matter if we find it there or not, the global
marker is always returned. This allows us to bypass other environments
on subsequent access, going directly to the global environment.
This commit is contained in:
Andreas Kling 2022-11-10 20:55:03 +01:00 committed by Linus Groh
parent 7b30df0840
commit d7e5a2058d
4 changed files with 18 additions and 8 deletions

View file

@ -68,7 +68,7 @@ ThrowCompletionOr<void> Reference::put_value(VM& vm, Value value)
VERIFY(m_base_environment);
// c. Return ? base.SetMutableBinding(V.[[ReferencedName]], W, V.[[Strict]]) (see 9.1).
if (m_environment_coordinate.has_value())
if (m_environment_coordinate.has_value() && m_environment_coordinate->index != EnvironmentCoordinate::global_marker)
return static_cast<DeclarativeEnvironment*>(m_base_environment)->set_mutable_binding_direct(vm, m_environment_coordinate->index, value, m_strict);
else
return m_base_environment->set_mutable_binding(vm, m_name.as_string(), value, m_strict);
@ -138,7 +138,7 @@ ThrowCompletionOr<Value> Reference::get_value(VM& vm) const
VERIFY(m_base_environment);
// c. Return ? base.GetBindingValue(V.[[ReferencedName]], V.[[Strict]]) (see 9.1).
if (m_environment_coordinate.has_value())
if (m_environment_coordinate.has_value() && m_environment_coordinate->index != EnvironmentCoordinate::global_marker)
return static_cast<DeclarativeEnvironment*>(m_base_environment)->get_binding_value_direct(vm, m_environment_coordinate->index, m_strict);
return m_base_environment->get_binding_value(vm, m_name.as_string(), m_strict);
}