ExecutionContext.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/DeprecatedFlyString.h>
  10. #include <AK/WeakPtr.h>
  11. #include <LibJS/Bytecode/BasicBlock.h>
  12. #include <LibJS/Forward.h>
  13. #include <LibJS/Module.h>
  14. #include <LibJS/Runtime/PrivateEnvironment.h>
  15. #include <LibJS/Runtime/Value.h>
  16. #include <LibJS/SourceRange.h>
  17. namespace JS {
  18. using ScriptOrModule = Variant<Empty, GC::Ref<Script>, GC::Ref<Module>>;
  19. struct CachedSourceRange : public RefCounted<CachedSourceRange> {
  20. CachedSourceRange(size_t program_counter, Variant<UnrealizedSourceRange, SourceRange> source_range)
  21. : program_counter(program_counter)
  22. , source_range(move(source_range))
  23. {
  24. }
  25. size_t program_counter { 0 };
  26. Variant<UnrealizedSourceRange, SourceRange> source_range;
  27. };
  28. // 9.4 Execution Contexts, https://tc39.es/ecma262/#sec-execution-contexts
  29. struct ExecutionContext {
  30. static NonnullOwnPtr<ExecutionContext> create();
  31. [[nodiscard]] NonnullOwnPtr<ExecutionContext> copy() const;
  32. ~ExecutionContext();
  33. void visit_edges(Cell::Visitor&);
  34. private:
  35. friend class ExecutionContextAllocator;
  36. ExecutionContext();
  37. public:
  38. void operator delete(void* ptr);
  39. GC::Ptr<FunctionObject> function; // [[Function]]
  40. GC::Ptr<Realm> realm; // [[Realm]]
  41. ScriptOrModule script_or_module; // [[ScriptOrModule]]
  42. GC::Ptr<Environment> lexical_environment; // [[LexicalEnvironment]]
  43. GC::Ptr<Environment> variable_environment; // [[VariableEnvironment]]
  44. GC::Ptr<PrivateEnvironment> private_environment; // [[PrivateEnvironment]]
  45. // Non-standard: This points at something that owns this ExecutionContext, in case it needs to be protected from GC.
  46. GC::Ptr<Cell> context_owner;
  47. Optional<size_t> program_counter;
  48. mutable RefPtr<CachedSourceRange> cached_source_range;
  49. GC::Ptr<PrimitiveString> function_name;
  50. Value this_value;
  51. GC::Ptr<Bytecode::Executable> executable;
  52. // https://html.spec.whatwg.org/multipage/webappapis.html#skip-when-determining-incumbent-counter
  53. // FIXME: Move this out of LibJS (e.g. by using the CustomData concept), as it's used exclusively by LibWeb.
  54. size_t skip_when_determining_incumbent_counter { 0 };
  55. Value argument(size_t index) const
  56. {
  57. if (index >= arguments.size()) [[unlikely]]
  58. return js_undefined();
  59. return arguments[index];
  60. }
  61. Value& local(size_t index)
  62. {
  63. return registers_and_constants_and_locals[index];
  64. }
  65. u32 passed_argument_count { 0 };
  66. bool is_strict_mode { false };
  67. Vector<Value> arguments;
  68. Vector<Value> registers_and_constants_and_locals;
  69. Vector<Bytecode::UnwindInfo> unwind_contexts;
  70. Vector<Optional<size_t>> previously_scheduled_jumps;
  71. Vector<GC::Ptr<Environment>> saved_lexical_environments;
  72. };
  73. struct StackTraceElement {
  74. ExecutionContext* execution_context;
  75. RefPtr<CachedSourceRange> source_range;
  76. };
  77. }