ExecutionContext.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/Heap/MarkedVector.h>
  14. #include <LibJS/Module.h>
  15. #include <LibJS/Runtime/PrivateEnvironment.h>
  16. #include <LibJS/Runtime/Value.h>
  17. #include <LibJS/SourceRange.h>
  18. namespace JS {
  19. using ScriptOrModule = Variant<Empty, NonnullGCPtr<Script>, NonnullGCPtr<Module>>;
  20. // 9.4 Execution Contexts, https://tc39.es/ecma262/#sec-execution-contexts
  21. struct ExecutionContext {
  22. explicit ExecutionContext(Heap& heap);
  23. [[nodiscard]] ExecutionContext copy() const;
  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. explicit ExecutionContext(MarkedVector<Value> existing_arguments, MarkedVector<Value> existing_local_variables);
  30. public:
  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<Bytecode::InstructionStreamIterator> instruction_stream_iterator;
  40. DeprecatedFlyString function_name;
  41. Value this_value;
  42. MarkedVector<Value> arguments;
  43. MarkedVector<Value> local_variables;
  44. bool is_strict_mode { false };
  45. GCPtr<Bytecode::Executable> executable;
  46. // https://html.spec.whatwg.org/multipage/webappapis.html#skip-when-determining-incumbent-counter
  47. // FIXME: Move this out of LibJS (e.g. by using the CustomData concept), as it's used exclusively by LibWeb.
  48. size_t skip_when_determining_incumbent_counter { 0 };
  49. };
  50. struct StackTraceElement {
  51. ExecutionContext* execution_context;
  52. Optional<UnrealizedSourceRange> source_range;
  53. };
  54. }