Script.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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/AST.h>
  9. #include <LibJS/Heap/GCPtr.h>
  10. #include <LibJS/Heap/Handle.h>
  11. #include <LibJS/Parser.h>
  12. #include <LibJS/Runtime/Realm.h>
  13. namespace JS {
  14. // 16.1.4 Script Records, https://tc39.es/ecma262/#sec-script-records
  15. class Script final : public Cell {
  16. JS_CELL(Script, Cell);
  17. public:
  18. struct HostDefined {
  19. virtual ~HostDefined() = default;
  20. };
  21. virtual ~Script() override;
  22. static Result<NonnullGCPtr<Script>, Vector<Parser::Error>> 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() { 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. String m_filename;
  34. HostDefined* m_host_defined { nullptr }; // [[HostDefined]]
  35. };
  36. }