2021-09-14 18:21:07 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
|
2021-09-14 18:21:07 +02:00
|
|
|
* 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>
|
2025-04-23 19:44:54 +02:00
|
|
|
* Copyright (c) 2024-2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
2021-09-14 18:21:07 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-02-07 21:48:57 +01:00
|
|
|
#include <AK/WeakPtr.h>
|
2024-05-01 19:33:49 +02:00
|
|
|
#include <LibJS/Bytecode/BasicBlock.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 {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
using ScriptOrModule = Variant<Empty, GC::Ref<Script>, GC::Ref<Module>>;
|
2022-01-17 14:48:22 +01:00
|
|
|
|
2024-10-14 07:49:12 +02:00
|
|
|
struct CachedSourceRange : public RefCounted<CachedSourceRange> {
|
|
|
|
CachedSourceRange(size_t program_counter, Variant<UnrealizedSourceRange, SourceRange> source_range)
|
|
|
|
: program_counter(program_counter)
|
|
|
|
, source_range(move(source_range))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
size_t program_counter { 0 };
|
|
|
|
Variant<UnrealizedSourceRange, SourceRange> source_range;
|
|
|
|
};
|
|
|
|
|
2021-09-14 18:21:07 +02:00
|
|
|
// 9.4 Execution Contexts, https://tc39.es/ecma262/#sec-execution-contexts
|
2025-06-28 21:39:13 -07:00
|
|
|
struct JS_API ExecutionContext {
|
2025-04-23 00:57:07 +02:00
|
|
|
static NonnullOwnPtr<ExecutionContext> create(u32 registers_and_constants_and_locals_count, u32 arguments_count);
|
2023-11-27 16:45:45 +01:00
|
|
|
[[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:
|
2024-05-23 11:10:23 +01:00
|
|
|
friend class ExecutionContextAllocator;
|
|
|
|
|
2025-04-23 23:44:24 +02:00
|
|
|
public:
|
2025-04-23 19:44:54 +02:00
|
|
|
ExecutionContext(u32 registers_and_constants_and_locals_count, u32 arguments_count);
|
2021-11-09 20:39:22 +02:00
|
|
|
|
2024-05-23 11:10:23 +01:00
|
|
|
void operator delete(void* ptr);
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<FunctionObject> function; // [[Function]]
|
|
|
|
GC::Ptr<Realm> realm; // [[Realm]]
|
|
|
|
ScriptOrModule script_or_module; // [[ScriptOrModule]]
|
|
|
|
GC::Ptr<Environment> lexical_environment; // [[LexicalEnvironment]]
|
|
|
|
GC::Ptr<Environment> variable_environment; // [[VariableEnvironment]]
|
|
|
|
GC::Ptr<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.
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<Cell> context_owner;
|
2022-11-21 11:18:15 +01:00
|
|
|
|
2025-04-28 19:02:56 +02:00
|
|
|
size_t program_counter { 0 };
|
2024-10-14 07:49:12 +02:00
|
|
|
|
|
|
|
mutable RefPtr<CachedSourceRange> cached_source_range;
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<PrimitiveString> function_name;
|
2025-04-04 23:16:34 +02:00
|
|
|
Optional<Value> this_value;
|
2021-10-14 16:12:53 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<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
|
|
|
|
2025-04-23 19:44:54 +02:00
|
|
|
Span<Value> registers_and_constants_and_locals_and_arguments_span()
|
|
|
|
{
|
|
|
|
return { registers_and_constants_and_locals_and_arguments(), registers_and_constants_and_locals_and_arguments_count };
|
|
|
|
}
|
|
|
|
|
|
|
|
Value const* registers_and_constants_and_locals_and_arguments() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<Value*>(reinterpret_cast<uintptr_t>(this) + sizeof(ExecutionContext));
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:45:45 +01:00
|
|
|
Value argument(size_t index) const
|
|
|
|
{
|
2025-04-24 01:37:30 +02:00
|
|
|
if (index >= arguments.size()) [[unlikely]]
|
2023-11-27 16:45:45 +01:00
|
|
|
return js_undefined();
|
2025-04-24 01:37:30 +02:00
|
|
|
return arguments[index];
|
2023-11-27 16:45:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Value& local(size_t index)
|
|
|
|
{
|
2025-04-23 19:44:54 +02:00
|
|
|
return registers_and_constants_and_locals_and_arguments()[index];
|
2023-11-27 16:45:45 +01:00
|
|
|
}
|
|
|
|
|
2025-04-23 00:57:07 +02:00
|
|
|
u32 arguments_offset { 0 };
|
2024-05-05 22:06:55 +02:00
|
|
|
u32 passed_argument_count { 0 };
|
2024-05-31 15:15:49 +02:00
|
|
|
bool is_strict_mode { false };
|
2024-05-05 22:06:55 +02:00
|
|
|
|
2025-04-24 01:37:30 +02:00
|
|
|
Span<Value> arguments;
|
2025-04-22 21:49:41 +02:00
|
|
|
|
2024-05-01 19:33:49 +02:00
|
|
|
Vector<Bytecode::UnwindInfo> unwind_contexts;
|
2024-05-06 06:44:08 +02:00
|
|
|
Vector<Optional<size_t>> previously_scheduled_jumps;
|
2024-11-15 04:01:23 +13:00
|
|
|
Vector<GC::Ptr<Environment>> saved_lexical_environments;
|
2025-04-23 19:44:54 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class Bytecode::Interpreter;
|
|
|
|
|
|
|
|
Value* registers_and_constants_and_locals_and_arguments()
|
|
|
|
{
|
|
|
|
return reinterpret_cast<Value*>(reinterpret_cast<uintptr_t>(this) + sizeof(ExecutionContext));
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 registers_and_constants_and_locals_and_arguments_count { 0 };
|
2021-09-14 18:21:07 +02:00
|
|
|
};
|
|
|
|
|
2025-04-27 11:53:11 +02:00
|
|
|
#define ALLOCATE_EXECUTION_CONTEXT_ON_NATIVE_STACK_WITHOUT_CLEARING_ARGS(execution_context, \
|
2025-04-23 23:44:24 +02:00
|
|
|
registers_and_constants_and_locals_count, \
|
|
|
|
arguments_count) \
|
|
|
|
auto execution_context_size = sizeof(JS::ExecutionContext) \
|
2025-04-27 11:51:56 +02:00
|
|
|
+ (((registers_and_constants_and_locals_count) + (arguments_count)) \
|
|
|
|
* sizeof(JS::Value)); \
|
2025-04-23 23:44:24 +02:00
|
|
|
\
|
|
|
|
void* execution_context_memory = alloca(execution_context_size); \
|
|
|
|
\
|
|
|
|
execution_context = new (execution_context_memory) \
|
|
|
|
JS::ExecutionContext((registers_and_constants_and_locals_count), (arguments_count)); \
|
|
|
|
\
|
|
|
|
ScopeGuard run_execution_context_destructor([execution_context] { \
|
|
|
|
execution_context->~ExecutionContext(); \
|
|
|
|
})
|
|
|
|
|
2025-04-27 11:53:11 +02:00
|
|
|
#define ALLOCATE_EXECUTION_CONTEXT_ON_NATIVE_STACK(execution_context, registers_and_constants_and_locals_count, \
|
|
|
|
arguments_count) \
|
|
|
|
ALLOCATE_EXECUTION_CONTEXT_ON_NATIVE_STACK_WITHOUT_CLEARING_ARGS(execution_context, \
|
|
|
|
registers_and_constants_and_locals_count, arguments_count); \
|
|
|
|
do { \
|
|
|
|
for (size_t i = 0; i < execution_context->arguments.size(); i++) { \
|
|
|
|
execution_context->arguments[i] = JS::js_undefined(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2023-11-01 00:39:28 +01:00
|
|
|
struct StackTraceElement {
|
|
|
|
ExecutionContext* execution_context;
|
2024-10-14 07:49:12 +02:00
|
|
|
RefPtr<CachedSourceRange> source_range;
|
2023-11-01 00:39:28 +01:00
|
|
|
};
|
|
|
|
|
2021-09-14 18:21:07 +02:00
|
|
|
}
|