ladybird/Libraries/LibJS/Runtime/WrappedFunction.h
Andreas Kling 4d92c4d71a LibJS: Skip initializing constant slots in ExecutionContext
Every function call allocates an ExecutionContext with a trailing array
of Values for registers, locals, constants, and arguments. Previously,
the constructor would initialize all slots to js_special_empty_value(),
but constant slots were then immediately overwritten by the interpreter
copying in values from the Executable before execution began.

To eliminate this redundant initialization, we rearrange the layout from
[registers | constants | locals] to [registers | locals | constants].
This groups registers and locals together at the front, allowing us to
initialize only those slots while leaving constant slots uninitialized
until they're populated with their actual values.

This reduces the per-call initialization cost from O(registers + locals
+ constants) to O(registers + locals).

Also tightens up the types involved (size_t -> u32) and adds VERIFYs to
guard against overflow when computing the combined slot counts, and to
ensure the total fits within the 29-bit operand index field.
2026-01-19 10:48:12 +01:00

47 lines
1.7 KiB
C++

/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/Realm.h>
namespace JS {
class WrappedFunction final : public FunctionObject {
JS_OBJECT(WrappedFunction, FunctionObject);
GC_DECLARE_ALLOCATOR(WrappedFunction);
public:
static ThrowCompletionOr<GC::Ref<WrappedFunction>> create(Realm&, Realm& caller_realm, FunctionObject& target_function);
virtual ~WrappedFunction() = default;
virtual ThrowCompletionOr<Value> internal_call(ExecutionContext&, Value this_argument) override;
virtual Realm* realm() const override { return m_realm; }
FunctionObject const& wrapped_target_function() const { return m_wrapped_target_function; }
FunctionObject& wrapped_target_function() { return m_wrapped_target_function; }
virtual ThrowCompletionOr<void> get_stack_frame_size(size_t& registers_and_locals_count, size_t& constants_count, size_t& argument_count) override;
virtual Utf16String name_for_call_stack() const override;
private:
WrappedFunction(Realm&, FunctionObject&, Object& prototype);
virtual void visit_edges(Visitor&) override;
// Internal Slots of Wrapped Function Exotic Objects, https://tc39.es/proposal-shadowrealm/#table-internal-slots-of-wrapped-function-exotic-objects
GC::Ref<FunctionObject> m_wrapped_target_function; // [[WrappedTargetFunction]]
GC::Ref<Realm> m_realm; // [[Realm]]
};
ThrowCompletionOr<Value> ordinary_wrapped_function_call(WrappedFunction&, Value this_argument, Span<Value> arguments_list);
void prepare_for_wrapped_function_call(WrappedFunction&, ExecutionContext& callee_context);
}