2021-10-24 08:30:49 -03:00
|
|
|
/*
|
2025-11-22 08:27:51 -03:00
|
|
|
* Copyright (c) 2021-2025, Andreas Kling <andreas@ladybird.org>
|
2021-10-24 08:30:49 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-03-06 13:39:59 -03:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2023-10-20 07:21:30 -03:00
|
|
|
#include <AK/OwnPtr.h>
|
2026-02-22 10:18:30 -03:00
|
|
|
#include <AK/String.h>
|
2025-08-02 20:27:29 -03:00
|
|
|
#include <AK/Utf16FlyString.h>
|
2024-11-14 12:01:23 -03:00
|
|
|
#include <LibGC/CellAllocator.h>
|
2026-03-07 18:18:15 -03:00
|
|
|
#include <LibGC/Ptr.h>
|
|
|
|
|
#include <LibGC/WeakContainer.h>
|
2026-02-10 20:15:19 -03:00
|
|
|
#include <LibJS/Bytecode/ClassBlueprint.h>
|
2021-10-24 10:34:30 -03:00
|
|
|
#include <LibJS/Bytecode/IdentifierTable.h>
|
2023-10-19 18:18:54 -03:00
|
|
|
#include <LibJS/Bytecode/Label.h>
|
2025-11-22 08:27:51 -03:00
|
|
|
#include <LibJS/Bytecode/Operand.h>
|
2025-12-11 10:57:09 -03:00
|
|
|
#include <LibJS/Bytecode/PropertyKeyTable.h>
|
2021-10-24 08:30:49 -03:00
|
|
|
#include <LibJS/Bytecode/StringTable.h>
|
2025-07-19 17:49:30 -03:00
|
|
|
#include <LibJS/Export.h>
|
2023-10-08 07:25:58 -03:00
|
|
|
#include <LibJS/Forward.h>
|
2023-11-27 09:23:59 -03:00
|
|
|
#include <LibJS/Heap/Cell.h>
|
2025-05-05 15:53:19 -03:00
|
|
|
#include <LibJS/LocalVariable.h>
|
2023-10-26 05:39:40 -03:00
|
|
|
#include <LibJS/Runtime/EnvironmentCoordinate.h>
|
2024-05-06 01:44:08 -03:00
|
|
|
#include <LibJS/SourceRange.h>
|
2021-10-24 08:30:49 -03:00
|
|
|
|
|
|
|
|
namespace JS::Bytecode {
|
|
|
|
|
|
2025-05-06 08:16:44 -03:00
|
|
|
// Represents one polymorphic inline cache used for property lookups.
|
2023-07-08 12:43:26 -03:00
|
|
|
struct PropertyLookupCache {
|
2025-05-06 08:16:44 -03:00
|
|
|
static constexpr size_t max_number_of_shapes_to_remember = 4;
|
|
|
|
|
struct Entry {
|
2025-09-15 12:23:39 -03:00
|
|
|
enum class Type {
|
|
|
|
|
Empty,
|
|
|
|
|
AddOwnProperty,
|
|
|
|
|
ChangeOwnProperty,
|
|
|
|
|
GetOwnProperty,
|
|
|
|
|
ChangePropertyInPrototypeChain,
|
|
|
|
|
GetPropertyInPrototypeChain,
|
|
|
|
|
};
|
2025-12-20 19:16:16 -03:00
|
|
|
u32 property_offset { 0 };
|
|
|
|
|
u32 shape_dictionary_generation { 0 };
|
2026-03-07 18:18:15 -03:00
|
|
|
GC::RawPtr<Shape> from_shape;
|
|
|
|
|
GC::RawPtr<Shape> shape;
|
|
|
|
|
GC::RawPtr<Object> prototype;
|
|
|
|
|
GC::RawPtr<PrototypeChainValidity> prototype_chain_validity;
|
2025-05-06 08:16:44 -03:00
|
|
|
};
|
2025-12-20 20:12:08 -03: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 16:00:35 -03:00
|
|
|
types[i] = types[i - 1];
|
2025-12-20 20:12:08 -03: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 08:16:44 -03:00
|
|
|
AK::Array<Entry, max_number_of_shapes_to_remember> entries;
|
2023-07-08 12:43:26 -03:00
|
|
|
};
|
|
|
|
|
|
2026-03-07 18:18:15 -03: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-11 23:06:59 -03:00
|
|
|
struct GlobalVariableCache : public PropertyLookupCache {
|
|
|
|
|
u64 environment_serial_number { 0 };
|
2025-03-16 20:39:57 -03:00
|
|
|
u32 environment_binding_index { 0 };
|
|
|
|
|
bool has_environment_binding_index { false };
|
|
|
|
|
bool in_module_environment { false };
|
2023-07-11 23:06:59 -03:00
|
|
|
};
|
|
|
|
|
|
2026-01-06 16:49:38 -03: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 14:55:00 -03: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 18:18:15 -03:00
|
|
|
GC::RawPtr<Shape> shape;
|
2026-01-09 14:55:00 -03:00
|
|
|
Vector<u32> property_offsets;
|
|
|
|
|
};
|
|
|
|
|
|
LibJS: Cache stable for-in iteration at bytecode sites
Cache the flattened enumerable key snapshot for each `for..in` site and
reuse a `PropertyNameIterator` when the receiver shape, dictionary
generation, indexed storage kind and length, prototype chain
validity, and magical-length state still match.
Handle packed indexed receivers as well as plain named-property
objects. Teach `ObjectPropertyIteratorNext` in `asmint.asm` to return
cached property values directly and to fall back to the slow iterator
logic when any guard fails.
Treat arrays' hidden non-enumerable `length` property as a visited
name for for-in shadowing, and include the receiver's magical-length
state in the cache key so arrays and plain objects do not share
snapshots.
Add `test-js` and `test-js-bytecode` coverage for mixed numeric and
named keys, packed receiver transitions, re-entry, iterator reuse, GC
retention, array length shadowing, and same-site cache reuse.
2026-04-09 19:56:49 -03:00
|
|
|
enum class ObjectPropertyIteratorFastPath : u8 {
|
|
|
|
|
None,
|
|
|
|
|
PlainNamed,
|
|
|
|
|
PackedIndexed,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class JS_API ObjectPropertyIteratorCacheData final : public Cell {
|
|
|
|
|
GC_CELL(ObjectPropertyIteratorCacheData, Cell);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(ObjectPropertyIteratorCacheData);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ObjectPropertyIteratorCacheData(VM&, Vector<PropertyKey>, ObjectPropertyIteratorFastPath, u32 indexed_property_count, bool receiver_has_magical_length_property, GC::Ref<Shape>, GC::Ptr<PrototypeChainValidity> = nullptr);
|
|
|
|
|
virtual ~ObjectPropertyIteratorCacheData() override = default;
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] ReadonlySpan<PropertyKey> properties() const { return m_properties.span(); }
|
|
|
|
|
[[nodiscard]] ReadonlySpan<Value> property_values() const { return m_property_values.span(); }
|
|
|
|
|
[[nodiscard]] ObjectPropertyIteratorFastPath fast_path() const { return m_fast_path; }
|
|
|
|
|
[[nodiscard]] u32 indexed_property_count() const { return m_indexed_property_count; }
|
|
|
|
|
[[nodiscard]] bool receiver_has_magical_length_property() const { return m_receiver_has_magical_length_property; }
|
|
|
|
|
[[nodiscard]] GC::Ptr<Shape> shape() const { return m_shape; }
|
|
|
|
|
[[nodiscard]] GC::Ptr<PrototypeChainValidity> prototype_chain_validity() const { return m_prototype_chain_validity; }
|
|
|
|
|
[[nodiscard]] u32 shape_dictionary_generation() const { return m_shape_dictionary_generation; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
2026-05-05 19:16:40 -03:00
|
|
|
virtual size_t external_memory_size() const override;
|
LibJS: Cache stable for-in iteration at bytecode sites
Cache the flattened enumerable key snapshot for each `for..in` site and
reuse a `PropertyNameIterator` when the receiver shape, dictionary
generation, indexed storage kind and length, prototype chain
validity, and magical-length state still match.
Handle packed indexed receivers as well as plain named-property
objects. Teach `ObjectPropertyIteratorNext` in `asmint.asm` to return
cached property values directly and to fall back to the slow iterator
logic when any guard fails.
Treat arrays' hidden non-enumerable `length` property as a visited
name for for-in shadowing, and include the receiver's magical-length
state in the cache key so arrays and plain objects do not share
snapshots.
Add `test-js` and `test-js-bytecode` coverage for mixed numeric and
named keys, packed receiver transitions, re-entry, iterator reuse, GC
retention, array length shadowing, and same-site cache reuse.
2026-04-09 19:56:49 -03:00
|
|
|
|
|
|
|
|
Vector<PropertyKey> m_properties;
|
|
|
|
|
Vector<Value> m_property_values;
|
|
|
|
|
GC::Ptr<Shape> m_shape;
|
|
|
|
|
GC::Ptr<PrototypeChainValidity> m_prototype_chain_validity;
|
|
|
|
|
u32 m_indexed_property_count { 0 };
|
|
|
|
|
u32 m_shape_dictionary_generation { 0 };
|
|
|
|
|
bool m_receiver_has_magical_length_property { false };
|
|
|
|
|
ObjectPropertyIteratorFastPath m_fast_path { ObjectPropertyIteratorFastPath::None };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ObjectPropertyIteratorCache {
|
|
|
|
|
GC::Ptr<ObjectPropertyIteratorCacheData> data;
|
|
|
|
|
GC::Ptr<Object> reusable_property_name_iterator;
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-01 11:53:55 -03:00
|
|
|
struct SourceRecord {
|
2026-04-21 12:10:34 -03:00
|
|
|
Position start {};
|
|
|
|
|
Position end {};
|
2023-09-01 11:53:55 -03:00
|
|
|
};
|
|
|
|
|
|
2026-01-26 13:51:53 -03:00
|
|
|
struct SourceMapEntry {
|
|
|
|
|
u32 bytecode_offset {};
|
|
|
|
|
SourceRecord source_record {};
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-07 18:18:15 -03:00
|
|
|
class JS_API Executable final
|
|
|
|
|
: public Cell
|
|
|
|
|
, public GC::WeakContainer {
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_CELL(Executable, Cell);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(Executable);
|
2023-11-27 09:23:59 -03:00
|
|
|
|
2023-10-03 03:18:10 -03:00
|
|
|
public:
|
|
|
|
|
Executable(
|
2024-05-06 01:44:08 -03:00
|
|
|
Vector<u8> bytecode,
|
2023-10-03 03:18:10 -03:00
|
|
|
NonnullOwnPtr<IdentifierTable>,
|
2025-12-11 10:57:09 -03:00
|
|
|
NonnullOwnPtr<PropertyKeyTable>,
|
2023-10-03 03:18:10 -03:00
|
|
|
NonnullOwnPtr<StringTable>,
|
|
|
|
|
NonnullOwnPtr<RegexTable>,
|
2024-02-02 05:52:25 -03:00
|
|
|
Vector<Value> constants,
|
2023-10-03 03:18:10 -03:00
|
|
|
NonnullRefPtr<SourceCode const>,
|
|
|
|
|
size_t number_of_property_lookup_caches,
|
|
|
|
|
size_t number_of_global_variable_caches,
|
2026-01-06 16:49:38 -03:00
|
|
|
size_t number_of_template_object_caches,
|
2026-01-09 14:55:00 -03:00
|
|
|
size_t number_of_object_shape_caches,
|
LibJS: Cache stable for-in iteration at bytecode sites
Cache the flattened enumerable key snapshot for each `for..in` site and
reuse a `PropertyNameIterator` when the receiver shape, dictionary
generation, indexed storage kind and length, prototype chain
validity, and magical-length state still match.
Handle packed indexed receivers as well as plain named-property
objects. Teach `ObjectPropertyIteratorNext` in `asmint.asm` to return
cached property values directly and to fall back to the slow iterator
logic when any guard fails.
Treat arrays' hidden non-enumerable `length` property as a visited
name for for-in shadowing, and include the receiver's magical-length
state in the cache key so arrays and plain objects do not share
snapshots.
Add `test-js` and `test-js-bytecode` coverage for mixed numeric and
named keys, packed receiver transitions, re-entry, iterator reuse, GC
retention, array length shadowing, and same-site cache reuse.
2026-04-09 19:56:49 -03:00
|
|
|
size_t number_of_object_property_iterator_caches,
|
2023-10-03 03:18:10 -03:00
|
|
|
size_t number_of_registers,
|
2025-10-28 16:25:12 -03:00
|
|
|
Strict);
|
2023-10-03 03:18:10 -03:00
|
|
|
|
2023-11-27 09:23:59 -03:00
|
|
|
virtual ~Executable() override;
|
2023-10-03 03:18:10 -03:00
|
|
|
|
2025-08-02 20:27:29 -03:00
|
|
|
Utf16FlyString name;
|
2024-05-06 01:44:08 -03:00
|
|
|
Vector<u8> bytecode;
|
2023-07-08 12:43:26 -03:00
|
|
|
Vector<PropertyLookupCache> property_lookup_caches;
|
2023-07-11 23:06:59 -03:00
|
|
|
Vector<GlobalVariableCache> global_variable_caches;
|
2026-01-06 16:49:38 -03:00
|
|
|
Vector<TemplateObjectCache> template_object_caches;
|
2026-01-09 14:55:00 -03:00
|
|
|
Vector<ObjectShapeCache> object_shape_caches;
|
LibJS: Cache stable for-in iteration at bytecode sites
Cache the flattened enumerable key snapshot for each `for..in` site and
reuse a `PropertyNameIterator` when the receiver shape, dictionary
generation, indexed storage kind and length, prototype chain
validity, and magical-length state still match.
Handle packed indexed receivers as well as plain named-property
objects. Teach `ObjectPropertyIteratorNext` in `asmint.asm` to return
cached property values directly and to fall back to the slow iterator
logic when any guard fails.
Treat arrays' hidden non-enumerable `length` property as a visited
name for for-in shadowing, and include the receiver's magical-length
state in the cache key so arrays and plain objects do not share
snapshots.
Add `test-js` and `test-js-bytecode` coverage for mixed numeric and
named keys, packed receiver transitions, re-entry, iterator reuse, GC
retention, array length shadowing, and same-site cache reuse.
2026-04-09 19:56:49 -03:00
|
|
|
Vector<ObjectPropertyIteratorCache> object_property_iterator_caches;
|
2021-10-24 08:30:49 -03:00
|
|
|
NonnullOwnPtr<StringTable> string_table;
|
2021-10-24 10:34:30 -03:00
|
|
|
NonnullOwnPtr<IdentifierTable> identifier_table;
|
2025-12-11 10:57:09 -03:00
|
|
|
NonnullOwnPtr<PropertyKeyTable> property_key_table;
|
2023-07-13 05:49:07 -03:00
|
|
|
NonnullOwnPtr<RegexTable> regex_table;
|
2024-02-02 05:52:25 -03:00
|
|
|
Vector<Value> constants;
|
2023-10-19 18:18:54 -03:00
|
|
|
|
2026-02-10 19:02:46 -03:00
|
|
|
Vector<GC::Ptr<SharedFunctionInstanceData>> shared_function_data;
|
2026-02-10 20:15:19 -03:00
|
|
|
Vector<ClassBlueprint> class_blueprints;
|
2026-02-10 19:02:46 -03:00
|
|
|
|
2023-09-01 11:53:55 -03:00
|
|
|
NonnullRefPtr<SourceCode const> source_code;
|
2026-01-18 19:17:10 -03:00
|
|
|
u32 number_of_registers { 0 };
|
2026-05-02 05:49:32 -03:00
|
|
|
u32 number_of_arguments { 0 };
|
2022-07-17 14:56:36 -03:00
|
|
|
bool is_strict_mode { false };
|
2021-10-24 08:30:49 -03:00
|
|
|
|
2026-01-18 19:17:10 -03:00
|
|
|
u32 registers_and_locals_count { 0 };
|
|
|
|
|
u32 registers_and_locals_and_constants_count { 0 };
|
2026-04-14 05:16:33 -03:00
|
|
|
size_t asm_constants_size { 0 };
|
|
|
|
|
Value const* asm_constants_data { nullptr };
|
2025-10-29 20:09:09 -03:00
|
|
|
|
2024-05-06 01:44:08 -03:00
|
|
|
struct ExceptionHandlers {
|
|
|
|
|
size_t start_offset;
|
|
|
|
|
size_t end_offset;
|
2026-02-09 08:08:49 -03:00
|
|
|
size_t handler_offset;
|
2024-05-06 01:44:08 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Vector<ExceptionHandlers> exception_handlers;
|
|
|
|
|
Vector<size_t> basic_block_start_offsets;
|
|
|
|
|
|
2026-01-26 13:51:53 -03:00
|
|
|
Vector<SourceMapEntry> source_map;
|
2024-05-06 02:51:14 -03:00
|
|
|
|
2025-05-05 15:53:19 -03:00
|
|
|
Vector<LocalVariable> local_variable_names;
|
2026-01-18 19:17:10 -03:00
|
|
|
u32 local_index_base { 0 };
|
|
|
|
|
u32 argument_index_base { 0 };
|
2024-06-13 16:19:06 -03:00
|
|
|
|
2025-12-11 10:57:09 -03:00
|
|
|
Optional<PropertyKeyTableIndex> length_identifier;
|
2024-07-09 06:37:06 -03:00
|
|
|
|
2025-08-07 20:31:52 -03:00
|
|
|
Utf16String const& get_string(StringTableIndex index) const { return string_table->get(index); }
|
2025-08-02 20:27:29 -03:00
|
|
|
Utf16FlyString const& get_identifier(IdentifierTableIndex index) const { return identifier_table->get(index); }
|
2025-12-11 10:57:09 -03:00
|
|
|
PropertyKey const& get_property_key(PropertyKeyTableIndex index) const { return property_key_table->get(index); }
|
2021-10-24 08:30:49 -03:00
|
|
|
|
2025-08-02 20:27:29 -03:00
|
|
|
Optional<Utf16FlyString const&> get_identifier(Optional<IdentifierTableIndex> const& index) const
|
2024-03-29 12:26:10 -03:00
|
|
|
{
|
|
|
|
|
if (!index.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
return get_identifier(*index);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-04 19:08:21 -03:00
|
|
|
[[nodiscard]] COLD Optional<ExceptionHandlers const&> exception_handlers_for_offset(size_t offset) const;
|
2024-05-06 01:44:08 -03:00
|
|
|
|
2026-04-21 12:10:34 -03:00
|
|
|
[[nodiscard]] Optional<SourceRange> source_range_at(size_t offset) const;
|
2024-05-06 01:44:08 -03:00
|
|
|
|
2026-03-08 07:56:31 -03:00
|
|
|
[[nodiscard]] SourceRange const& get_source_range(u32 program_counter);
|
|
|
|
|
|
2026-03-07 18:52:25 -03:00
|
|
|
void fixup_cache_pointers();
|
|
|
|
|
|
2021-10-24 08:30:49 -03:00
|
|
|
void dump() const;
|
2026-02-22 10:18:30 -03:00
|
|
|
[[nodiscard]] String dump_to_string() const;
|
2024-03-03 07:34:36 -03:00
|
|
|
|
2025-11-22 08:27:51 -03:00
|
|
|
[[nodiscard]] Operand original_operand_from_raw(u32) const;
|
|
|
|
|
|
2026-01-28 21:37:18 -03:00
|
|
|
virtual Cell const& owner_cell(Badge<GC::Heap>) const override { return *this; }
|
2026-03-07 18:18:15 -03:00
|
|
|
virtual void remove_dead_cells(Badge<GC::Heap>) override;
|
|
|
|
|
|
2024-03-03 07:34:36 -03:00
|
|
|
private:
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
2026-05-05 19:16:40 -03:00
|
|
|
virtual size_t external_memory_size() const override;
|
2026-03-08 07:56:31 -03:00
|
|
|
|
|
|
|
|
HashMap<u32, SourceRange> m_source_range_cache;
|
2021-10-24 08:30:49 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|