ladybird/Libraries/LibJS/Runtime/NativeJavaScriptBackedFunction.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

43 lines
1.4 KiB
C++

/*
* Copyright (c) 2025, Luke Wilde <luke@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/SharedFunctionInstanceData.h>
namespace JS {
class NativeJavaScriptBackedFunction final : public NativeFunction {
JS_OBJECT(NativeJavaScriptBackedFunction, NativeFunction);
GC_DECLARE_ALLOCATOR(NativeJavaScriptBackedFunction);
public:
static GC::Ref<NativeJavaScriptBackedFunction> create(Realm&, FunctionNode const& function_node, PropertyKey const& name, i32 length);
virtual ~NativeJavaScriptBackedFunction() override = default;
virtual void visit_edges(Visitor&) override;
virtual ThrowCompletionOr<void> get_stack_frame_size(size_t& registers_and_locals_count, size_t& constants_count, size_t& argument_count) override;
virtual ThrowCompletionOr<Value> call() override;
Bytecode::Executable& bytecode_executable();
FunctionKind kind() const;
ThisMode this_mode() const;
virtual bool function_environment_needed() const override;
virtual size_t function_environment_bindings_count() const override;
virtual bool is_strict_mode() const override;
private:
explicit NativeJavaScriptBackedFunction(GC::Ref<SharedFunctionInstanceData const> shared_function_instance_data, Object& prototype);
GC::Ref<SharedFunctionInstanceData const> m_shared_function_instance_data;
};
}