ExecutionContext.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/Instruction.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. static FlatPtr realm_offset() { return OFFSET_OF(ExecutionContext, realm); }
  26. static FlatPtr lexical_environment_offset() { return OFFSET_OF(ExecutionContext, lexical_environment); }
  27. static FlatPtr variable_environment_offset() { return OFFSET_OF(ExecutionContext, variable_environment); }
  28. private:
  29. ExecutionContext(Heap&);
  30. IntrusiveListNode<ExecutionContext> m_list_node;
  31. public:
  32. Heap& m_heap;
  33. using List = IntrusiveList<&ExecutionContext::m_list_node>;
  34. GCPtr<FunctionObject> function; // [[Function]]
  35. GCPtr<Realm> realm; // [[Realm]]
  36. ScriptOrModule script_or_module; // [[ScriptOrModule]]
  37. GCPtr<Environment> lexical_environment; // [[LexicalEnvironment]]
  38. GCPtr<Environment> variable_environment; // [[VariableEnvironment]]
  39. GCPtr<PrivateEnvironment> private_environment; // [[PrivateEnvironment]]
  40. // Non-standard: This points at something that owns this ExecutionContext, in case it needs to be protected from GC.
  41. GCPtr<Cell> context_owner;
  42. Optional<Bytecode::InstructionStreamIterator> instruction_stream_iterator;
  43. GCPtr<PrimitiveString> function_name;
  44. Value this_value;
  45. bool is_strict_mode { false };
  46. GCPtr<Bytecode::Executable> executable;
  47. // https://html.spec.whatwg.org/multipage/webappapis.html#skip-when-determining-incumbent-counter
  48. // FIXME: Move this out of LibJS (e.g. by using the CustomData concept), as it's used exclusively by LibWeb.
  49. size_t skip_when_determining_incumbent_counter { 0 };
  50. Value argument(size_t index) const
  51. {
  52. if (index >= arguments.size()) [[unlikely]]
  53. return js_undefined();
  54. return arguments[index];
  55. }
  56. Value& local(size_t index)
  57. {
  58. return locals[index];
  59. }
  60. Vector<Value> arguments;
  61. Vector<Value> locals;
  62. };
  63. struct StackTraceElement {
  64. ExecutionContext* execution_context;
  65. Optional<UnrealizedSourceRange> source_range;
  66. };
  67. }