Executable.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2021-2024, Andreas Kling <kling@serenityos.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. };
  30. using EnvironmentVariableCache = Optional<EnvironmentCoordinate>;
  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_environment_variable_caches,
  49. size_t number_of_registers,
  50. bool is_strict_mode);
  51. virtual ~Executable() override;
  52. DeprecatedFlyString name;
  53. Vector<u8> bytecode;
  54. Vector<PropertyLookupCache> property_lookup_caches;
  55. Vector<GlobalVariableCache> global_variable_caches;
  56. Vector<EnvironmentVariableCache> environment_variable_caches;
  57. NonnullOwnPtr<StringTable> string_table;
  58. NonnullOwnPtr<IdentifierTable> identifier_table;
  59. NonnullOwnPtr<RegexTable> regex_table;
  60. Vector<Value> constants;
  61. NonnullRefPtr<SourceCode const> source_code;
  62. size_t number_of_registers { 0 };
  63. bool is_strict_mode { false };
  64. struct ExceptionHandlers {
  65. size_t start_offset;
  66. size_t end_offset;
  67. Optional<size_t> handler_offset;
  68. Optional<size_t> finalizer_offset;
  69. };
  70. Vector<ExceptionHandlers> exception_handlers;
  71. Vector<size_t> basic_block_start_offsets;
  72. HashMap<size_t, SourceRecord> source_map;
  73. ByteString const& get_string(StringTableIndex index) const { return string_table->get(index); }
  74. DeprecatedFlyString const& get_identifier(IdentifierTableIndex index) const { return identifier_table->get(index); }
  75. Optional<DeprecatedFlyString const&> get_identifier(Optional<IdentifierTableIndex> const& index) const
  76. {
  77. if (!index.has_value())
  78. return {};
  79. return get_identifier(*index);
  80. }
  81. [[nodiscard]] Optional<ExceptionHandlers const&> exception_handlers_for_offset(size_t offset) const;
  82. [[nodiscard]] UnrealizedSourceRange source_range_at(size_t offset) const;
  83. void dump() const;
  84. private:
  85. virtual void visit_edges(Visitor&) override;
  86. };
  87. }