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 <AK/RefCounted.h>
  9. #include <LibJS/AST.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
  16. : public RefCounted<Script>
  17. , public Weakable<Script> {
  18. public:
  19. struct HostDefined {
  20. virtual ~HostDefined() = default;
  21. };
  22. ~Script();
  23. static Result<NonnullRefPtr<Script>, Vector<Parser::Error>> parse(StringView source_text, Realm&, StringView filename = {}, HostDefined* = nullptr);
  24. Realm& realm() { return *m_realm.cell(); }
  25. Program const& parse_node() const { return *m_parse_node; }
  26. HostDefined* host_defined() { return m_host_defined; }
  27. StringView filename() const { return m_filename; }
  28. private:
  29. Script(Realm&, StringView filename, NonnullRefPtr<Program>, HostDefined* = nullptr);
  30. // Handles are not safe unless we keep the VM alive.
  31. NonnullRefPtr<VM> m_vm;
  32. Handle<Realm> m_realm; // [[Realm]]
  33. NonnullRefPtr<Program> m_parse_node; // [[ECMAScriptCode]]
  34. // Needed for potential lookups of modules.
  35. String m_filename;
  36. HostDefined* m_host_defined { nullptr }; // [[HostDefined]]
  37. };
  38. }