Executable.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2021-2024, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DeprecatedFlyString.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/NonnullOwnPtr.h>
  10. #include <AK/OwnPtr.h>
  11. #include <AK/WeakPtr.h>
  12. #include <LibJS/Bytecode/IdentifierTable.h>
  13. #include <LibJS/Bytecode/Label.h>
  14. #include <LibJS/Bytecode/StringTable.h>
  15. #include <LibJS/Forward.h>
  16. #include <LibJS/Heap/Cell.h>
  17. #include <LibJS/Heap/CellAllocator.h>
  18. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  19. #include <LibJS/SourceRange.h>
  20. namespace JS::Bytecode {
  21. struct PropertyLookupCache {
  22. WeakPtr<Shape> shape;
  23. Optional<u32> property_offset;
  24. WeakPtr<Object> prototype;
  25. WeakPtr<PrototypeChainValidity> prototype_chain_validity;
  26. };
  27. struct GlobalVariableCache : public PropertyLookupCache {
  28. u64 environment_serial_number { 0 };
  29. Optional<u32> environment_binding_index;
  30. };
  31. struct SourceRecord {
  32. u32 source_start_offset {};
  33. u32 source_end_offset {};
  34. };
  35. class Executable final : public Cell {
  36. JS_CELL(Executable, Cell);
  37. JS_DECLARE_ALLOCATOR(Executable);
  38. public:
  39. Executable(
  40. Vector<u8> bytecode,
  41. NonnullOwnPtr<IdentifierTable>,
  42. NonnullOwnPtr<StringTable>,
  43. NonnullOwnPtr<RegexTable>,
  44. Vector<Value> constants,
  45. NonnullRefPtr<SourceCode const>,
  46. size_t number_of_property_lookup_caches,
  47. size_t number_of_global_variable_caches,
  48. size_t number_of_registers,
  49. bool is_strict_mode);
  50. virtual ~Executable() override;
  51. DeprecatedFlyString name;
  52. Vector<u8> bytecode;
  53. Vector<PropertyLookupCache> property_lookup_caches;
  54. Vector<GlobalVariableCache> global_variable_caches;
  55. NonnullOwnPtr<StringTable> string_table;
  56. NonnullOwnPtr<IdentifierTable> identifier_table;
  57. NonnullOwnPtr<RegexTable> regex_table;
  58. Vector<Value> constants;
  59. NonnullRefPtr<SourceCode const> source_code;
  60. size_t number_of_registers { 0 };
  61. bool is_strict_mode { false };
  62. struct ExceptionHandlers {
  63. size_t start_offset;
  64. size_t end_offset;
  65. Optional<size_t> handler_offset;
  66. Optional<size_t> finalizer_offset;
  67. };
  68. Vector<ExceptionHandlers> exception_handlers;
  69. Vector<size_t> basic_block_start_offsets;
  70. HashMap<size_t, SourceRecord> source_map;
  71. Vector<DeprecatedFlyString> local_variable_names;
  72. size_t local_index_base { 0 };
  73. Optional<IdentifierTableIndex> length_identifier;
  74. ByteString const& get_string(StringTableIndex index) const { return string_table->get(index); }
  75. DeprecatedFlyString const& get_identifier(IdentifierTableIndex index) const { return identifier_table->get(index); }
  76. Optional<DeprecatedFlyString const&> get_identifier(Optional<IdentifierTableIndex> const& index) const
  77. {
  78. if (!index.has_value())
  79. return {};
  80. return get_identifier(*index);
  81. }
  82. [[nodiscard]] Optional<ExceptionHandlers const&> exception_handlers_for_offset(size_t offset) const;
  83. [[nodiscard]] UnrealizedSourceRange source_range_at(size_t offset) const;
  84. void dump() const;
  85. private:
  86. virtual void visit_edges(Visitor&) override;
  87. };
  88. }