2021-09-14 18:21:07 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
2022-12-11 18:16:15 +00:00
|
|
|
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
|
2021-09-14 18:21:07 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
#include <AK/DeprecatedFlyString.h>
|
2022-02-07 21:48:57 +01:00
|
|
|
#include <AK/WeakPtr.h>
|
2023-09-01 16:53:55 +02:00
|
|
|
#include <LibJS/Bytecode/Instruction.h>
|
2021-09-14 18:21:07 +02:00
|
|
|
#include <LibJS/Forward.h>
|
2021-10-14 16:12:53 +01:00
|
|
|
#include <LibJS/Module.h>
|
2021-10-11 20:29:31 +02:00
|
|
|
#include <LibJS/Runtime/PrivateEnvironment.h>
|
2021-09-14 18:21:07 +02:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2023-07-27 14:40:01 +02:00
|
|
|
#include <LibJS/SourceRange.h>
|
2021-09-14 18:21:07 +02:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2022-09-05 14:31:25 +02:00
|
|
|
using ScriptOrModule = Variant<Empty, NonnullGCPtr<Script>, NonnullGCPtr<Module>>;
|
2022-01-17 14:48:22 +01:00
|
|
|
|
2021-09-14 18:21:07 +02:00
|
|
|
// 9.4 Execution Contexts, https://tc39.es/ecma262/#sec-execution-contexts
|
|
|
|
struct ExecutionContext {
|
2023-11-27 16:45:45 +01:00
|
|
|
static NonnullOwnPtr<ExecutionContext> create(Heap&);
|
|
|
|
[[nodiscard]] NonnullOwnPtr<ExecutionContext> copy() const;
|
2022-12-11 18:16:15 +00:00
|
|
|
|
2023-11-27 16:45:45 +01:00
|
|
|
~ExecutionContext();
|
2021-11-09 20:39:22 +02:00
|
|
|
|
2022-12-11 18:24:33 +00:00
|
|
|
void visit_edges(Cell::Visitor&);
|
|
|
|
|
2021-11-09 20:39:22 +02:00
|
|
|
private:
|
2023-11-27 16:45:45 +01:00
|
|
|
ExecutionContext(Heap&);
|
|
|
|
|
|
|
|
IntrusiveListNode<ExecutionContext> m_list_node;
|
2021-11-09 20:39:22 +02:00
|
|
|
|
|
|
|
public:
|
2023-11-27 16:45:45 +01:00
|
|
|
Heap& m_heap;
|
|
|
|
|
|
|
|
using List = IntrusiveList<&ExecutionContext::m_list_node>;
|
|
|
|
|
2023-02-26 16:09:02 -07:00
|
|
|
GCPtr<FunctionObject> function; // [[Function]]
|
|
|
|
GCPtr<Realm> realm; // [[Realm]]
|
|
|
|
ScriptOrModule script_or_module; // [[ScriptOrModule]]
|
|
|
|
GCPtr<Environment> lexical_environment; // [[LexicalEnvironment]]
|
|
|
|
GCPtr<Environment> variable_environment; // [[VariableEnvironment]]
|
|
|
|
GCPtr<PrivateEnvironment> private_environment; // [[PrivateEnvironment]]
|
2021-09-14 18:21:07 +02:00
|
|
|
|
2022-11-21 11:18:15 +01:00
|
|
|
// Non-standard: This points at something that owns this ExecutionContext, in case it needs to be protected from GC.
|
2023-02-26 16:09:02 -07:00
|
|
|
GCPtr<Cell> context_owner;
|
2022-11-21 11:18:15 +01:00
|
|
|
|
2023-09-02 17:38:17 +02:00
|
|
|
Optional<Bytecode::InstructionStreamIterator> instruction_stream_iterator;
|
2023-11-27 13:38:19 +01:00
|
|
|
GCPtr<PrimitiveString> function_name;
|
2021-09-14 18:21:07 +02:00
|
|
|
Value this_value;
|
|
|
|
bool is_strict_mode { false };
|
2021-10-14 16:12:53 +01:00
|
|
|
|
2023-11-27 13:23:59 +01:00
|
|
|
GCPtr<Bytecode::Executable> executable;
|
2023-11-01 00:39:28 +01:00
|
|
|
|
2021-10-14 16:12:53 +01:00
|
|
|
// https://html.spec.whatwg.org/multipage/webappapis.html#skip-when-determining-incumbent-counter
|
|
|
|
// FIXME: Move this out of LibJS (e.g. by using the CustomData concept), as it's used exclusively by LibWeb.
|
|
|
|
size_t skip_when_determining_incumbent_counter { 0 };
|
2023-11-27 16:45:45 +01:00
|
|
|
|
|
|
|
Value argument(size_t index) const
|
|
|
|
{
|
|
|
|
if (index >= arguments.size()) [[unlikely]]
|
|
|
|
return js_undefined();
|
|
|
|
return arguments[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& local(size_t index)
|
|
|
|
{
|
|
|
|
return locals[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Value> arguments;
|
|
|
|
Vector<Value> locals;
|
2021-09-14 18:21:07 +02:00
|
|
|
};
|
|
|
|
|
2023-11-01 00:39:28 +01:00
|
|
|
struct StackTraceElement {
|
|
|
|
ExecutionContext* execution_context;
|
|
|
|
Optional<UnrealizedSourceRange> source_range;
|
|
|
|
};
|
|
|
|
|
2021-09-14 18:21:07 +02:00
|
|
|
}
|