2025-11-06 16:20:45 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2025, Andreas Kling <andreas@ladybird.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2026-02-11 01:20:54 +01:00
|
|
|
#include <LibGC/Cell.h>
|
2026-02-11 11:51:48 +01:00
|
|
|
#include <LibJS/Export.h>
|
2025-11-06 16:20:45 +00:00
|
|
|
#include <LibJS/Forward.h>
|
2026-02-11 01:20:54 +01:00
|
|
|
#include <LibJS/LocalVariable.h>
|
2025-11-06 16:20:45 +00:00
|
|
|
#include <LibJS/Runtime/FunctionKind.h>
|
2026-02-11 01:20:54 +01:00
|
|
|
#include <LibJS/Runtime/PrivateEnvironment.h>
|
|
|
|
|
#include <LibJS/Runtime/PropertyKey.h>
|
2025-11-06 16:20:45 +00:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2026-02-11 01:20:54 +01:00
|
|
|
// NB: This mirrors Identifier::Local from AST.h, defined here to avoid
|
|
|
|
|
// including the full AST header in this file.
|
|
|
|
|
struct FunctionLocal {
|
|
|
|
|
enum Type : u8 {
|
|
|
|
|
None,
|
|
|
|
|
Argument,
|
|
|
|
|
Variable,
|
|
|
|
|
};
|
|
|
|
|
Type type { None };
|
|
|
|
|
u32 index { 0 };
|
|
|
|
|
|
|
|
|
|
bool is_argument() const { return type == Argument; }
|
|
|
|
|
bool is_variable() const { return type == Variable; }
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-06 16:20:45 +00:00
|
|
|
enum class ThisMode : u8 {
|
|
|
|
|
Lexical,
|
|
|
|
|
Strict,
|
|
|
|
|
Global,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class ConstructorKind : u8 {
|
|
|
|
|
Base,
|
|
|
|
|
Derived,
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-11 11:51:48 +01:00
|
|
|
class JS_API SharedFunctionInstanceData final : public GC::Cell {
|
2025-11-06 16:20:45 +00:00
|
|
|
GC_CELL(SharedFunctionInstanceData, GC::Cell);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(SharedFunctionInstanceData);
|
2026-02-23 11:50:46 +01:00
|
|
|
static constexpr bool OVERRIDES_FINALIZE = true;
|
2025-11-06 16:20:45 +00:00
|
|
|
|
|
|
|
|
public:
|
2026-04-14 09:56:33 +02:00
|
|
|
static constexpr u64 asm_call_metadata_can_inline_call = 1ull << 32;
|
|
|
|
|
static constexpr u64 asm_call_metadata_function_environment_needed = 1ull << 33;
|
|
|
|
|
static constexpr u64 asm_call_metadata_uses_this = 1ull << 34;
|
|
|
|
|
static constexpr u64 asm_call_metadata_strict = 1ull << 35;
|
|
|
|
|
|
2025-11-06 16:20:45 +00:00
|
|
|
virtual ~SharedFunctionInstanceData() override;
|
2026-02-23 11:50:46 +01:00
|
|
|
virtual void finalize() override;
|
2025-11-06 16:20:45 +00:00
|
|
|
|
2026-02-23 11:50:46 +01:00
|
|
|
SharedFunctionInstanceData(
|
|
|
|
|
VM& vm,
|
|
|
|
|
FunctionKind,
|
|
|
|
|
Utf16FlyString name,
|
|
|
|
|
i32 function_length,
|
|
|
|
|
u32 formal_parameter_count,
|
|
|
|
|
bool strict,
|
|
|
|
|
bool is_arrow_function,
|
|
|
|
|
bool has_simple_parameter_list,
|
|
|
|
|
Vector<Utf16FlyString> parameter_names_for_mapped_arguments,
|
|
|
|
|
void* rust_function_ast);
|
|
|
|
|
|
2026-04-13 13:43:55 +02:00
|
|
|
void set_executable(GC::Ptr<Bytecode::Executable>);
|
|
|
|
|
void set_is_class_constructor();
|
2026-04-14 09:56:33 +02:00
|
|
|
void update_asm_call_metadata();
|
2026-04-13 13:43:55 +02:00
|
|
|
[[nodiscard]] bool can_inline_call() const { return m_can_inline_call; }
|
|
|
|
|
|
2025-11-06 16:20:45 +00:00
|
|
|
mutable GC::Ptr<Bytecode::Executable> m_executable;
|
2026-04-14 09:56:33 +02:00
|
|
|
u64 m_asm_call_metadata { 0 };
|
2025-11-06 16:20:45 +00:00
|
|
|
|
|
|
|
|
Utf16FlyString m_name;
|
2025-12-20 14:23:47 -06:00
|
|
|
|
2026-02-11 11:57:23 +01:00
|
|
|
// NB: m_source_text is normally a view into the underlying JS::SourceCode we parsed the AST from,
|
|
|
|
|
// kept alive by m_source_code. m_source_text_owner is used if the source text needs to be
|
|
|
|
|
// owned by the function data (e.g. for dynamically created functions via Function constructor).
|
|
|
|
|
RefPtr<SourceCode const> m_source_code;
|
2025-12-20 14:23:47 -06:00
|
|
|
Utf16String m_source_text_owner;
|
|
|
|
|
Utf16View m_source_text; // [[SourceText]]
|
2025-11-06 16:20:45 +00:00
|
|
|
|
|
|
|
|
Vector<LocalVariable> m_local_variables_names;
|
|
|
|
|
|
|
|
|
|
i32 m_function_length { 0 };
|
2026-02-11 01:13:06 +01:00
|
|
|
u32 m_formal_parameter_count { 0 };
|
|
|
|
|
Vector<Utf16FlyString> m_parameter_names_for_mapped_arguments;
|
2025-11-06 16:20:45 +00:00
|
|
|
|
|
|
|
|
ThisMode m_this_mode : 2 { ThisMode::Global }; // [[ThisMode]]
|
|
|
|
|
FunctionKind m_kind : 3 { FunctionKind::Normal };
|
|
|
|
|
|
|
|
|
|
bool m_strict { false };
|
|
|
|
|
bool m_might_need_arguments_object { true };
|
|
|
|
|
bool m_contains_direct_call_to_eval { true };
|
|
|
|
|
bool m_is_arrow_function { false };
|
|
|
|
|
bool m_has_simple_parameter_list { false };
|
|
|
|
|
bool m_is_module_wrapper { false };
|
|
|
|
|
|
2026-02-11 01:06:58 +01:00
|
|
|
struct VarBinding {
|
|
|
|
|
Utf16FlyString name;
|
2026-02-11 01:20:54 +01:00
|
|
|
FunctionLocal local {};
|
2025-11-06 16:20:45 +00:00
|
|
|
bool parameter_binding { false };
|
|
|
|
|
bool function_name { false };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool m_has_parameter_expressions { false };
|
|
|
|
|
bool m_has_duplicates { false };
|
|
|
|
|
enum class ParameterIsLocal {
|
|
|
|
|
No,
|
|
|
|
|
Yes,
|
|
|
|
|
};
|
2026-02-23 11:50:46 +01:00
|
|
|
OrderedHashMap<Utf16FlyString, ParameterIsLocal> m_parameter_names;
|
2026-02-11 01:08:21 +01:00
|
|
|
struct FunctionToInitialize {
|
|
|
|
|
GC::Ref<SharedFunctionInstanceData> shared_data;
|
|
|
|
|
Utf16FlyString name;
|
2026-02-11 01:20:54 +01:00
|
|
|
FunctionLocal local {};
|
2026-02-11 01:08:21 +01:00
|
|
|
};
|
|
|
|
|
Vector<FunctionToInitialize> m_functions_to_initialize;
|
2025-11-06 16:20:45 +00:00
|
|
|
bool m_arguments_object_needed { false };
|
|
|
|
|
bool m_function_environment_needed { false };
|
|
|
|
|
bool m_uses_this { false };
|
2026-02-11 01:06:58 +01:00
|
|
|
Vector<VarBinding> m_var_names_to_initialize_binding;
|
2025-11-06 16:20:45 +00:00
|
|
|
Vector<Utf16FlyString> m_function_names_to_initialize_binding;
|
|
|
|
|
|
2026-02-11 01:10:40 +01:00
|
|
|
struct LexicalBinding {
|
|
|
|
|
Utf16FlyString name;
|
|
|
|
|
bool is_constant { false };
|
|
|
|
|
};
|
|
|
|
|
Vector<LexicalBinding> m_lexical_bindings;
|
|
|
|
|
bool m_has_scope_body { false };
|
|
|
|
|
bool m_has_non_local_lexical_declarations { false };
|
|
|
|
|
|
2025-11-06 16:20:45 +00:00
|
|
|
size_t m_function_environment_bindings_count { 0 };
|
|
|
|
|
size_t m_var_environment_bindings_count { 0 };
|
|
|
|
|
size_t m_lex_environment_bindings_count { 0 };
|
|
|
|
|
|
|
|
|
|
Variant<PropertyKey, PrivateName, Empty> m_class_field_initializer_name; // [[ClassFieldInitializerName]]
|
|
|
|
|
ConstructorKind m_constructor_kind : 1 { ConstructorKind::Base }; // [[ConstructorKind]]
|
|
|
|
|
bool m_is_class_constructor : 1 { false }; // [[IsClassConstructor]]
|
|
|
|
|
|
2026-02-23 11:50:46 +01:00
|
|
|
// NB: When non-null, points to a Rust Box<FunctionData> used for
|
|
|
|
|
// lazy compilation through the Rust pipeline.
|
|
|
|
|
void* m_rust_function_ast { nullptr };
|
|
|
|
|
bool m_use_rust_compilation { false };
|
|
|
|
|
|
2026-02-10 22:21:23 +01:00
|
|
|
void clear_compile_inputs();
|
|
|
|
|
|
2025-11-06 16:20:45 +00:00
|
|
|
private:
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
2026-04-13 13:43:55 +02:00
|
|
|
void update_can_inline_call();
|
|
|
|
|
|
|
|
|
|
bool m_can_inline_call { false };
|
2025-11-06 16:20:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|