1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:28:11 +00:00

LibJS: Add a fast path for creating per-iteration DeclarativeEnvironment

The steps for creating a DeclarativeEnvironment for each iteration of a
for-loop can be done equivalently to the spec without following the spec
directly. For each binding creating in the loop's init expression, we:

    1. Create a new binding in the new environment.
    2. Grab the current value of the binding in the old environment.
    3. Set the value in the new environment to the old value.

This can be replaced by initializing the bindings vector in the new
environment directly with the bindings in the old environment (but only
copying the bindings of the init statement).
This commit is contained in:
Timothy Flynn 2022-03-09 15:37:10 -05:00 committed by Andreas Kling
parent f37fbcf516
commit 27904b1060
3 changed files with 42 additions and 40 deletions

View file

@ -13,6 +13,14 @@
namespace JS {
DeclarativeEnvironment* DeclarativeEnvironment::create_for_per_iteration_bindings(Badge<ForStatement>, DeclarativeEnvironment& other, size_t bindings_size)
{
auto bindings = other.m_bindings.span().slice(0, bindings_size);
auto* parent_scope = other.outer_environment();
return parent_scope->heap().allocate_without_global_object<DeclarativeEnvironment>(parent_scope, bindings);
}
DeclarativeEnvironment::DeclarativeEnvironment()
: Environment(nullptr)
{
@ -23,6 +31,12 @@ DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_scope)
{
}
DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_scope, Span<Binding const> bindings)
: Environment(parent_scope)
, m_bindings(bindings)
{
}
DeclarativeEnvironment::~DeclarativeEnvironment()
{
}