Skip to content

Commit

Permalink
LibJS: Ensure capacity of lex/var environments
Browse files Browse the repository at this point in the history
  • Loading branch information
kalenikaliaksandr committed May 9, 2024
1 parent 825a22c commit 0a6d7ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Userland/Libraries/LibJS/Bytecode/Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ CodeGenerationErrorOr<void> Generator::emit_function_declaration_instantiation(E
}
}
} else {
generator.emit<Op::CreateVariableEnvironment>();
generator.emit<Op::CreateVariableEnvironment>(function.m_var_environment_bindings_count);

if (scope_body) {
for (auto const& variable_to_initialize : function.m_var_names_to_initialize_binding) {
Expand Down Expand Up @@ -157,7 +157,7 @@ CodeGenerationErrorOr<void> Generator::emit_function_declaration_instantiation(E
if (!function.m_strict) {
bool can_elide_declarative_environment = !function.m_contains_direct_call_to_eval && (!scope_body || !scope_body->has_lexical_declarations());
if (!can_elide_declarative_environment) {
generator.emit<Op::CreateLexicalEnvironment>();
generator.emit<Op::CreateLexicalEnvironment>(function.m_lex_environment_bindings_count);
}
}

Expand Down
5 changes: 4 additions & 1 deletion Userland/Libraries/LibJS/Bytecode/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,9 @@ ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& inte
ThrowCompletionOr<void> CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
{
auto make_and_swap_envs = [&](auto& old_environment) {
GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
auto declarative_environment = new_declarative_environment(*old_environment).ptr();
declarative_environment->ensure_capacity(m_capacity);
GCPtr<Environment> environment = declarative_environment;
swap(old_environment, environment);
return environment;
};
Expand All @@ -1271,6 +1273,7 @@ ThrowCompletionOr<void> CreateVariableEnvironment::execute_impl(Bytecode::Interp
{
auto& running_execution_context = interpreter.vm().running_execution_context();
auto var_environment = new_declarative_environment(*running_execution_context.lexical_environment);
var_environment->ensure_capacity(m_capacity);
running_execution_context.variable_environment = var_environment;
running_execution_context.lexical_environment = var_environment;
return {};
Expand Down
12 changes: 10 additions & 2 deletions Userland/Libraries/LibJS/Bytecode/Op.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,24 +451,32 @@ enum class EnvironmentMode {

class CreateLexicalEnvironment final : public Instruction {
public:
explicit CreateLexicalEnvironment()
explicit CreateLexicalEnvironment(u32 capacity = 0)
: Instruction(Type::CreateLexicalEnvironment)
, m_capacity(capacity)
{
}

ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
ByteString to_byte_string_impl(Bytecode::Executable const&) const;

private:
u32 m_capacity { 0 };
};

class CreateVariableEnvironment final : public Instruction {
public:
explicit CreateVariableEnvironment()
explicit CreateVariableEnvironment(u32 capacity = 0)
: Instruction(Type::CreateVariableEnvironment)
, m_capacity(capacity)
{
}

ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
ByteString to_byte_string_impl(Bytecode::Executable const&) const;

private:
u32 m_capacity { 0 };
};

class EnterObjectEnvironment final : public Instruction {
Expand Down

0 comments on commit 0a6d7ce

Please sign in to comment.