ExecutionContext.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.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, NonnullGCPtr<Script>, NonnullGCPtr<Module>>;
  19. // 9.4 Execution Contexts, https://tc39.es/ecma262/#sec-execution-contexts
  20. struct ExecutionContext {
  21. static NonnullOwnPtr<ExecutionContext> create(Heap&);
  22. [[nodiscard]] NonnullOwnPtr<ExecutionContext> copy() const;
  23. ~ExecutionContext();
  24. void visit_edges(Cell::Visitor&);
  25. private:
  26. ExecutionContext(Heap&);
  27. IntrusiveListNode<ExecutionContext> m_list_node;
  28. public:
  29. Heap& m_heap;
  30. using List = IntrusiveList<&ExecutionContext::m_list_node>;
  31. GCPtr<FunctionObject> function; // [[Function]]
  32. GCPtr<Realm> realm; // [[Realm]]
  33. ScriptOrModule script_or_module; // [[ScriptOrModule]]
  34. GCPtr<Environment> lexical_environment; // [[LexicalEnvironment]]
  35. GCPtr<Environment> variable_environment; // [[VariableEnvironment]]
  36. GCPtr<PrivateEnvironment> private_environment; // [[PrivateEnvironment]]
  37. // Non-standard: This points at something that owns this ExecutionContext, in case it needs to be protected from GC.
  38. GCPtr<Cell> context_owner;
  39. Optional<size_t> program_counter;
  40. GCPtr<PrimitiveString> function_name;
  41. Value this_value;
  42. bool is_strict_mode { false };
  43. GCPtr<Bytecode::Executable> executable;
  44. // https://html.spec.whatwg.org/multipage/webappapis.html#skip-when-determining-incumbent-counter
  45. // FIXME: Move this out of LibJS (e.g. by using the CustomData concept), as it's used exclusively by LibWeb.
  46. size_t skip_when_determining_incumbent_counter { 0 };
  47. Value argument(size_t index) const
  48. {
  49. if (index >= arguments.size()) [[unlikely]]
  50. return js_undefined();
  51. return arguments[index];
  52. }
  53. Value& local(size_t index)
  54. {
  55. return registers_and_constants_and_locals[index];
  56. }
  57. u32 passed_argument_count { 0 };
  58. Vector<Value> arguments;
  59. Vector<Value> registers_and_constants_and_locals;
  60. Vector<Bytecode::UnwindInfo> unwind_contexts;
  61. Vector<Optional<size_t>> previously_scheduled_jumps;
  62. Vector<GCPtr<Environment>> saved_lexical_environments;
  63. };
  64. struct StackTraceElement {
  65. ExecutionContext* execution_context;
  66. Optional<UnrealizedSourceRange> source_range;
  67. };
  68. }