summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-03-09 15:37:10 -0500
committerAndreas Kling <kling@serenityos.org>2022-03-14 16:33:15 +0100
commit27904b106003a28ffe4f8a76a1500bd5fe1c2454 (patch)
treebd0ad856ae0bd61cd6e1c63cd60fc3c42709cd20 /Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp
parentf37fbcf516ccb26e56c14ebdd0c248e8416580e9 (diff)
downloadserenity-27904b106003a28ffe4f8a76a1500bd5fe1c2454.zip
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).
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp
index ec61b3ca13..e3255d6e0d 100644
--- a/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp
+++ b/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp
@@ -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()
{
}