Script.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtr.h>
  8. #include <LibJS/Heap/GCPtr.h>
  9. #include <LibJS/Heap/Handle.h>
  10. #include <LibJS/ParserError.h>
  11. #include <LibJS/Runtime/Realm.h>
  12. namespace JS {
  13. // 16.1.4 Script Records, https://tc39.es/ecma262/#sec-script-records
  14. class Script final : public Cell {
  15. JS_CELL(Script, Cell);
  16. public:
  17. struct HostDefined {
  18. virtual ~HostDefined() = default;
  19. virtual void visit_host_defined_self(Cell::Visitor&) = 0;
  20. };
  21. virtual ~Script() override;
  22. static Result<NonnullGCPtr<Script>, Vector<ParserError>> parse(StringView source_text, Realm&, StringView filename = {}, HostDefined* = nullptr, size_t line_number_offset = 1);
  23. Realm& realm() { return *m_realm; }
  24. Program const& parse_node() const { return *m_parse_node; }
  25. HostDefined* host_defined() const { return m_host_defined; }
  26. StringView filename() const { return m_filename; }
  27. private:
  28. Script(Realm&, StringView filename, NonnullRefPtr<Program>, HostDefined* = nullptr);
  29. virtual void visit_edges(Cell::Visitor&) override;
  30. GCPtr<Realm> m_realm; // [[Realm]]
  31. NonnullRefPtr<Program> m_parse_node; // [[ECMAScriptCode]]
  32. // Needed for potential lookups of modules.
  33. DeprecatedString m_filename;
  34. HostDefined* m_host_defined { nullptr }; // [[HostDefined]]
  35. };
  36. }