2021-10-24 08:30:49 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2021-2024, Andreas Kling <andreas@ladybird.org>
|
2021-10-24 08:30:49 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-10-19 18:18:54 -03:00
|
|
|
#include <AK/HashMap.h>
|
2023-03-06 13:39:59 -03:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2023-10-20 07:21:30 -03:00
|
|
|
#include <AK/OwnPtr.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>
|
2025-10-16 06:13:54 -03:00
|
|
|
#include <LibGC/Weak.h>
|
|
|
|
|
#include <LibGC/WeakInlines.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>
|
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,
|
|
|
|
|
};
|
|
|
|
|
Type type { Type::Empty };
|
2025-10-16 06:13:54 -03:00
|
|
|
GC::Weak<Shape> from_shape;
|
|
|
|
|
GC::Weak<Shape> shape;
|
2025-05-06 08:16:44 -03:00
|
|
|
Optional<u32> property_offset;
|
2025-10-16 06:13:54 -03:00
|
|
|
GC::Weak<Object> prototype;
|
|
|
|
|
GC::Weak<PrototypeChainValidity> prototype_chain_validity;
|
2025-09-07 11:27:16 -03:00
|
|
|
Optional<u32> shape_dictionary_generation;
|
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
|
|
|
};
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2023-09-01 11:53:55 -03:00
|
|
|
struct SourceRecord {
|
|
|
|
|
u32 source_start_offset {};
|
|
|
|
|
u32 source_end_offset {};
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-19 17:49:30 -03:00
|
|
|
class JS_API Executable final : public Cell {
|
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>,
|
|
|
|
|
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,
|
|
|
|
|
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;
|
2021-10-24 08:30:49 -03:00
|
|
|
NonnullOwnPtr<StringTable> string_table;
|
2021-10-24 10:34:30 -03:00
|
|
|
NonnullOwnPtr<IdentifierTable> identifier_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
|
|
|
|
2023-09-01 11:53:55 -03:00
|
|
|
NonnullRefPtr<SourceCode const> source_code;
|
2021-10-24 08:30:49 -03:00
|
|
|
size_t number_of_registers { 0 };
|
2022-07-17 14:56:36 -03:00
|
|
|
bool is_strict_mode { false };
|
2021-10-24 08:30:49 -03:00
|
|
|
|
2024-05-06 01:44:08 -03:00
|
|
|
struct ExceptionHandlers {
|
|
|
|
|
size_t start_offset;
|
|
|
|
|
size_t end_offset;
|
|
|
|
|
Optional<size_t> handler_offset;
|
|
|
|
|
Optional<size_t> finalizer_offset;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Vector<ExceptionHandlers> exception_handlers;
|
|
|
|
|
Vector<size_t> basic_block_start_offsets;
|
|
|
|
|
|
2024-05-06 02:51:14 -03:00
|
|
|
HashMap<size_t, SourceRecord> source_map;
|
|
|
|
|
|
2025-05-05 15:53:19 -03:00
|
|
|
Vector<LocalVariable> local_variable_names;
|
2024-06-13 16:19:06 -03:00
|
|
|
size_t local_index_base { 0 };
|
2025-04-24 17:10:41 -03:00
|
|
|
size_t argument_index_base { 0 };
|
2024-06-13 16:19:06 -03:00
|
|
|
|
2024-07-09 06:37:06 -03:00
|
|
|
Optional<IdentifierTableIndex> length_identifier;
|
|
|
|
|
|
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); }
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 01:44:08 -03:00
|
|
|
[[nodiscard]] Optional<ExceptionHandlers const&> exception_handlers_for_offset(size_t offset) const;
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] UnrealizedSourceRange source_range_at(size_t offset) const;
|
|
|
|
|
|
2021-10-24 08:30:49 -03:00
|
|
|
void dump() const;
|
2024-03-03 07:34:36 -03:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
2021-10-24 08:30:49 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|