2025-11-06 16:20:45 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2025, Andreas Kling <andreas@ladybird.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/SharedFunctionInstanceData.h>
|
2026-02-23 11:50:46 +01:00
|
|
|
#include <LibJS/RustIntegration.h>
|
2025-11-06 16:20:45 +00:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
|
|
GC_DEFINE_ALLOCATOR(SharedFunctionInstanceData);
|
|
|
|
|
|
|
|
|
|
SharedFunctionInstanceData::SharedFunctionInstanceData(
|
2026-03-19 13:15:04 -05:00
|
|
|
VM&,
|
2025-11-06 16:20:45 +00:00
|
|
|
FunctionKind kind,
|
|
|
|
|
Utf16FlyString name,
|
|
|
|
|
i32 function_length,
|
2026-03-19 13:15:04 -05:00
|
|
|
u32 formal_parameter_count,
|
2025-11-06 16:20:45 +00:00
|
|
|
bool strict,
|
|
|
|
|
bool is_arrow_function,
|
2026-03-19 13:15:04 -05:00
|
|
|
bool has_simple_parameter_list,
|
|
|
|
|
Vector<Utf16FlyString> parameter_names_for_mapped_arguments,
|
|
|
|
|
void* rust_function_ast)
|
|
|
|
|
: m_name(move(name))
|
2025-11-06 16:20:45 +00:00
|
|
|
, m_function_length(function_length)
|
2026-03-19 13:15:04 -05:00
|
|
|
, m_formal_parameter_count(formal_parameter_count)
|
|
|
|
|
, m_parameter_names_for_mapped_arguments(move(parameter_names_for_mapped_arguments))
|
2025-11-06 16:20:45 +00:00
|
|
|
, m_kind(kind)
|
|
|
|
|
, m_strict(strict)
|
|
|
|
|
, m_is_arrow_function(is_arrow_function)
|
2026-03-19 13:15:04 -05:00
|
|
|
, m_has_simple_parameter_list(has_simple_parameter_list)
|
|
|
|
|
, m_rust_function_ast(rust_function_ast)
|
|
|
|
|
, m_use_rust_compilation(true)
|
2025-11-06 16:20:45 +00:00
|
|
|
{
|
|
|
|
|
if (m_is_arrow_function)
|
|
|
|
|
m_this_mode = ThisMode::Lexical;
|
|
|
|
|
else if (m_strict)
|
|
|
|
|
m_this_mode = ThisMode::Strict;
|
|
|
|
|
else
|
|
|
|
|
m_this_mode = ThisMode::Global;
|
2026-04-13 13:43:55 +02:00
|
|
|
|
|
|
|
|
update_can_inline_call();
|
2025-11-06 16:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SharedFunctionInstanceData::visit_edges(Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_executable);
|
2026-02-11 01:08:21 +01:00
|
|
|
for (auto& function : m_functions_to_initialize)
|
|
|
|
|
visitor.visit(function.shared_data);
|
2026-01-06 00:36:34 +01:00
|
|
|
m_class_field_initializer_name.visit([&](PropertyKey const& key) { key.visit_edges(visitor); }, [](auto&) {});
|
2025-11-06 16:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SharedFunctionInstanceData::~SharedFunctionInstanceData() = default;
|
|
|
|
|
|
2026-04-13 13:43:55 +02:00
|
|
|
void SharedFunctionInstanceData::set_executable(GC::Ptr<Bytecode::Executable> executable)
|
|
|
|
|
{
|
|
|
|
|
m_executable = executable;
|
|
|
|
|
update_can_inline_call();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SharedFunctionInstanceData::set_is_class_constructor()
|
|
|
|
|
{
|
|
|
|
|
m_is_class_constructor = true;
|
|
|
|
|
update_can_inline_call();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 09:56:33 +02:00
|
|
|
void SharedFunctionInstanceData::update_asm_call_metadata()
|
|
|
|
|
{
|
|
|
|
|
m_asm_call_metadata = m_formal_parameter_count;
|
|
|
|
|
if (m_can_inline_call)
|
|
|
|
|
m_asm_call_metadata |= asm_call_metadata_can_inline_call;
|
|
|
|
|
if (m_function_environment_needed)
|
|
|
|
|
m_asm_call_metadata |= asm_call_metadata_function_environment_needed;
|
|
|
|
|
if (m_uses_this)
|
|
|
|
|
m_asm_call_metadata |= asm_call_metadata_uses_this;
|
|
|
|
|
if (m_strict)
|
|
|
|
|
m_asm_call_metadata |= asm_call_metadata_strict;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 11:50:46 +01:00
|
|
|
void SharedFunctionInstanceData::finalize()
|
|
|
|
|
{
|
|
|
|
|
Base::finalize();
|
|
|
|
|
RustIntegration::free_function_ast(m_rust_function_ast);
|
|
|
|
|
m_rust_function_ast = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 22:21:23 +01:00
|
|
|
void SharedFunctionInstanceData::clear_compile_inputs()
|
|
|
|
|
{
|
|
|
|
|
VERIFY(m_executable);
|
|
|
|
|
m_functions_to_initialize.clear();
|
|
|
|
|
m_var_names_to_initialize_binding.clear();
|
2026-02-11 01:10:40 +01:00
|
|
|
m_lexical_bindings.clear();
|
2026-02-23 11:50:46 +01:00
|
|
|
RustIntegration::free_function_ast(m_rust_function_ast);
|
|
|
|
|
m_rust_function_ast = nullptr;
|
2026-02-10 22:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-13 13:43:55 +02:00
|
|
|
void SharedFunctionInstanceData::update_can_inline_call()
|
|
|
|
|
{
|
|
|
|
|
m_can_inline_call = m_executable && m_kind == FunctionKind::Normal && !m_is_class_constructor;
|
2026-04-14 09:56:33 +02:00
|
|
|
update_asm_call_metadata();
|
2026-04-13 13:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
2025-11-06 16:20:45 +00:00
|
|
|
}
|