2021-10-24 13:30:49 +02:00
|
|
|
/*
|
2025-11-22 12:27:51 +01:00
|
|
|
* Copyright (c) 2021-2025, Andreas Kling <andreas@ladybird.org>
|
2021-10-24 13:30:49 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-03-06 17:39:59 +01:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2023-10-20 12:21:30 +02:00
|
|
|
#include <AK/OwnPtr.h>
|
2026-02-22 14:18:30 +01:00
|
|
|
#include <AK/String.h>
|
2025-08-02 19:27:29 -04:00
|
|
|
#include <AK/Utf16FlyString.h>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/CellAllocator.h>
|
2026-03-07 22:18:15 +01:00
|
|
|
#include <LibGC/Ptr.h>
|
|
|
|
|
#include <LibGC/WeakContainer.h>
|
2026-02-11 00:15:19 +01:00
|
|
|
#include <LibJS/Bytecode/ClassBlueprint.h>
|
2021-10-24 15:34:30 +02:00
|
|
|
#include <LibJS/Bytecode/IdentifierTable.h>
|
2023-10-19 23:18:54 +02:00
|
|
|
#include <LibJS/Bytecode/Label.h>
|
2025-11-22 12:27:51 +01:00
|
|
|
#include <LibJS/Bytecode/Operand.h>
|
2025-12-11 07:57:09 -06:00
|
|
|
#include <LibJS/Bytecode/PropertyKeyTable.h>
|
2021-10-24 13:30:49 +02:00
|
|
|
#include <LibJS/Bytecode/StringTable.h>
|
2025-07-19 13:49:30 -07:00
|
|
|
#include <LibJS/Export.h>
|
2023-10-08 12:25:58 +02:00
|
|
|
#include <LibJS/Forward.h>
|
2023-11-27 13:23:59 +01:00
|
|
|
#include <LibJS/Heap/Cell.h>
|
2025-05-05 21:53:19 +03:00
|
|
|
#include <LibJS/LocalVariable.h>
|
2023-10-26 10:39:40 +02:00
|
|
|
#include <LibJS/Runtime/EnvironmentCoordinate.h>
|
2024-05-06 06:44:08 +02:00
|
|
|
#include <LibJS/SourceRange.h>
|
2021-10-24 13:30:49 +02:00
|
|
|
|
|
|
|
|
namespace JS::Bytecode {
|
|
|
|
|
|
2025-05-06 13:16:44 +02:00
|
|
|
// Represents one polymorphic inline cache used for property lookups.
|
2023-07-08 17:43:26 +02:00
|
|
|
struct PropertyLookupCache {
|
2025-05-06 13:16:44 +02:00
|
|
|
static constexpr size_t max_number_of_shapes_to_remember = 4;
|
|
|
|
|
struct Entry {
|
2025-09-15 17:23:39 +02:00
|
|
|
enum class Type {
|
|
|
|
|
Empty,
|
|
|
|
|
AddOwnProperty,
|
|
|
|
|
ChangeOwnProperty,
|
|
|
|
|
GetOwnProperty,
|
|
|
|
|
ChangePropertyInPrototypeChain,
|
|
|
|
|
GetPropertyInPrototypeChain,
|
|
|
|
|
};
|
2025-12-20 16:16:16 -06:00
|
|
|
u32 property_offset { 0 };
|
|
|
|
|
u32 shape_dictionary_generation { 0 };
|
2026-03-07 22:18:15 +01:00
|
|
|
GC::RawPtr<Shape> from_shape;
|
|
|
|
|
GC::RawPtr<Shape> shape;
|
|
|
|
|
GC::RawPtr<Object> prototype;
|
|
|
|
|
GC::RawPtr<PrototypeChainValidity> prototype_chain_validity;
|
2025-05-06 13:16:44 +02:00
|
|
|
};
|
2025-12-20 17:12:08 -06:00
|
|
|
|
|
|
|
|
void update(Entry::Type type, auto callback)
|
|
|
|
|
{
|
|
|
|
|
// First, move all entries one step back.
|
|
|
|
|
for (size_t i = entries.size() - 1; i >= 1; --i) {
|
2025-12-21 13:00:35 -06:00
|
|
|
types[i] = types[i - 1];
|
2025-12-20 17:12:08 -06:00
|
|
|
entries[i] = entries[i - 1];
|
|
|
|
|
}
|
|
|
|
|
types[0] = type;
|
|
|
|
|
entries[0] = {};
|
|
|
|
|
callback(entries[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AK::Array<Entry::Type, max_number_of_shapes_to_remember> types;
|
2025-05-06 13:16:44 +02:00
|
|
|
AK::Array<Entry, max_number_of_shapes_to_remember> entries;
|
2023-07-08 17:43:26 +02:00
|
|
|
};
|
|
|
|
|
|
2026-03-07 22:18:15 +01:00
|
|
|
// A PropertyLookupCache for use as a static local variable.
|
|
|
|
|
// Registers itself for GC sweep since it's not owned by any Executable.
|
|
|
|
|
struct StaticPropertyLookupCache : public PropertyLookupCache {
|
|
|
|
|
StaticPropertyLookupCache();
|
|
|
|
|
static void sweep_all();
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-12 04:06:59 +02:00
|
|
|
struct GlobalVariableCache : public PropertyLookupCache {
|
|
|
|
|
u64 environment_serial_number { 0 };
|
2025-03-16 18:39:57 -05:00
|
|
|
u32 environment_binding_index { 0 };
|
|
|
|
|
bool has_environment_binding_index { false };
|
|
|
|
|
bool in_module_environment { false };
|
2023-07-12 04:06:59 +02:00
|
|
|
};
|
|
|
|
|
|
2026-01-06 19:49:38 +00:00
|
|
|
// https://tc39.es/ecma262/#sec-gettemplateobject
|
|
|
|
|
// Template objects are cached at the call site.
|
|
|
|
|
struct TemplateObjectCache {
|
|
|
|
|
GC::Ptr<Array> cached_template_object;
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-09 18:55:00 +01:00
|
|
|
// Cache for object literal shapes.
|
|
|
|
|
// When an object literal like {a: 1, b: 2} is instantiated, we cache the final shape
|
|
|
|
|
// so that subsequent instantiations can allocate the object with the correct shape directly,
|
|
|
|
|
// avoiding repeated shape transitions.
|
|
|
|
|
// We also cache the property offsets so that subsequent property writes can bypass
|
|
|
|
|
// shape lookups and write directly to the correct storage slot.
|
|
|
|
|
struct ObjectShapeCache {
|
2026-03-07 22:18:15 +01:00
|
|
|
GC::RawPtr<Shape> shape;
|
2026-01-09 18:55:00 +01:00
|
|
|
Vector<u32> property_offsets;
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-01 16:53:55 +02:00
|
|
|
struct SourceRecord {
|
|
|
|
|
u32 source_start_offset {};
|
|
|
|
|
u32 source_end_offset {};
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-26 17:51:53 +01:00
|
|
|
struct SourceMapEntry {
|
|
|
|
|
u32 bytecode_offset {};
|
|
|
|
|
SourceRecord source_record {};
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-07 22:18:15 +01:00
|
|
|
class JS_API Executable final
|
|
|
|
|
: public Cell
|
|
|
|
|
, public GC::WeakContainer {
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_CELL(Executable, Cell);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(Executable);
|
2023-11-27 13:23:59 +01:00
|
|
|
|
2023-10-03 08:18:10 +02:00
|
|
|
public:
|
|
|
|
|
Executable(
|
2024-05-06 06:44:08 +02:00
|
|
|
Vector<u8> bytecode,
|
2023-10-03 08:18:10 +02:00
|
|
|
NonnullOwnPtr<IdentifierTable>,
|
2025-12-11 07:57:09 -06:00
|
|
|
NonnullOwnPtr<PropertyKeyTable>,
|
2023-10-03 08:18:10 +02:00
|
|
|
NonnullOwnPtr<StringTable>,
|
|
|
|
|
NonnullOwnPtr<RegexTable>,
|
2024-02-02 09:52:25 +01:00
|
|
|
Vector<Value> constants,
|
2023-10-03 08:18:10 +02:00
|
|
|
NonnullRefPtr<SourceCode const>,
|
|
|
|
|
size_t number_of_property_lookup_caches,
|
|
|
|
|
size_t number_of_global_variable_caches,
|
2026-01-06 19:49:38 +00:00
|
|
|
size_t number_of_template_object_caches,
|
2026-01-09 18:55:00 +01:00
|
|
|
size_t number_of_object_shape_caches,
|
2023-10-03 08:18:10 +02:00
|
|
|
size_t number_of_registers,
|
2025-10-28 20:25:12 +01:00
|
|
|
Strict);
|
2023-10-03 08:18:10 +02:00
|
|
|
|
2023-11-27 13:23:59 +01:00
|
|
|
virtual ~Executable() override;
|
2023-10-03 08:18:10 +02:00
|
|
|
|
2025-08-02 19:27:29 -04:00
|
|
|
Utf16FlyString name;
|
2024-05-06 06:44:08 +02:00
|
|
|
Vector<u8> bytecode;
|
2023-07-08 17:43:26 +02:00
|
|
|
Vector<PropertyLookupCache> property_lookup_caches;
|
2023-07-12 04:06:59 +02:00
|
|
|
Vector<GlobalVariableCache> global_variable_caches;
|
2026-01-06 19:49:38 +00:00
|
|
|
Vector<TemplateObjectCache> template_object_caches;
|
2026-01-09 18:55:00 +01:00
|
|
|
Vector<ObjectShapeCache> object_shape_caches;
|
2021-10-24 13:30:49 +02:00
|
|
|
NonnullOwnPtr<StringTable> string_table;
|
2021-10-24 15:34:30 +02:00
|
|
|
NonnullOwnPtr<IdentifierTable> identifier_table;
|
2025-12-11 07:57:09 -06:00
|
|
|
NonnullOwnPtr<PropertyKeyTable> property_key_table;
|
2023-07-13 10:49:07 +02:00
|
|
|
NonnullOwnPtr<RegexTable> regex_table;
|
2024-02-02 09:52:25 +01:00
|
|
|
Vector<Value> constants;
|
2023-10-19 23:18:54 +02:00
|
|
|
|
2026-02-10 23:02:46 +01:00
|
|
|
Vector<GC::Ptr<SharedFunctionInstanceData>> shared_function_data;
|
2026-02-11 00:15:19 +01:00
|
|
|
Vector<ClassBlueprint> class_blueprints;
|
2026-02-10 23:02:46 +01:00
|
|
|
|
2023-09-01 16:53:55 +02:00
|
|
|
NonnullRefPtr<SourceCode const> source_code;
|
2026-01-18 23:17:10 +01:00
|
|
|
u32 number_of_registers { 0 };
|
2022-07-17 18:56:36 +01:00
|
|
|
bool is_strict_mode { false };
|
2021-10-24 13:30:49 +02:00
|
|
|
|
2026-01-18 23:17:10 +01:00
|
|
|
u32 registers_and_locals_count { 0 };
|
|
|
|
|
u32 registers_and_locals_and_constants_count { 0 };
|
2025-10-30 00:09:09 +01:00
|
|
|
|
2024-05-06 06:44:08 +02:00
|
|
|
struct ExceptionHandlers {
|
|
|
|
|
size_t start_offset;
|
|
|
|
|
size_t end_offset;
|
2026-02-09 12:08:49 +01:00
|
|
|
size_t handler_offset;
|
2024-05-06 06:44:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Vector<ExceptionHandlers> exception_handlers;
|
|
|
|
|
Vector<size_t> basic_block_start_offsets;
|
|
|
|
|
|
2026-01-26 17:51:53 +01:00
|
|
|
Vector<SourceMapEntry> source_map;
|
2024-05-06 07:51:14 +02:00
|
|
|
|
2025-05-05 21:53:19 +03:00
|
|
|
Vector<LocalVariable> local_variable_names;
|
2026-01-18 23:17:10 +01:00
|
|
|
u32 local_index_base { 0 };
|
|
|
|
|
u32 argument_index_base { 0 };
|
2024-06-13 21:19:06 +02:00
|
|
|
|
2025-12-11 07:57:09 -06:00
|
|
|
Optional<PropertyKeyTableIndex> length_identifier;
|
2024-07-09 11:37:06 +02:00
|
|
|
|
2025-08-07 19:31:52 -04:00
|
|
|
Utf16String const& get_string(StringTableIndex index) const { return string_table->get(index); }
|
2025-08-02 19:27:29 -04:00
|
|
|
Utf16FlyString const& get_identifier(IdentifierTableIndex index) const { return identifier_table->get(index); }
|
2025-12-11 07:57:09 -06:00
|
|
|
PropertyKey const& get_property_key(PropertyKeyTableIndex index) const { return property_key_table->get(index); }
|
2021-10-24 13:30:49 +02:00
|
|
|
|
2025-08-02 19:27:29 -04:00
|
|
|
Optional<Utf16FlyString const&> get_identifier(Optional<IdentifierTableIndex> const& index) const
|
2024-03-29 11:26:10 -04:00
|
|
|
{
|
|
|
|
|
if (!index.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
return get_identifier(*index);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-04 23:08:21 +01:00
|
|
|
[[nodiscard]] COLD Optional<ExceptionHandlers const&> exception_handlers_for_offset(size_t offset) const;
|
2024-05-06 06:44:08 +02:00
|
|
|
|
|
|
|
|
[[nodiscard]] UnrealizedSourceRange source_range_at(size_t offset) const;
|
|
|
|
|
|
2026-03-07 22:52:25 +01:00
|
|
|
void fixup_cache_pointers();
|
|
|
|
|
|
2021-10-24 13:30:49 +02:00
|
|
|
void dump() const;
|
2026-02-22 14:18:30 +01:00
|
|
|
[[nodiscard]] String dump_to_string() const;
|
2024-03-03 11:34:36 +01:00
|
|
|
|
2025-11-22 12:27:51 +01:00
|
|
|
[[nodiscard]] Operand original_operand_from_raw(u32) const;
|
|
|
|
|
|
2026-03-07 22:18:15 +01:00
|
|
|
virtual void remove_dead_cells(Badge<GC::Heap>) override;
|
|
|
|
|
|
2024-03-03 11:34:36 +01:00
|
|
|
private:
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
2021-10-24 13:30:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|