LibJS: Separate raw and capturing native functions

NativeFunction previously stored an AK::Function for every builtin,
even when the callable was just a plain C++ entry point. That mixed
together two different representations, made simple builtins carry
capture storage they did not need, and forced the GC to treat every
native function as if it might contain captured JS values.

Introduce RawNativeFunction for plain NativeFunctionPointer callees
and keep AK::Function-backed callables on a CapturingNativeFunction
subclass. Update the straightforward native registrations in LibJS
and LibWeb to use the raw representation, while leaving exported
Wasm functions on the capturing path because they still capture
state.

Wrap UniversalGlobalScope's byte-length strategy lambda in
Function<...> explicitly so it keeps selecting the capturing
NativeFunction::create overload.
This commit is contained in:
Andreas Kling
2026-04-14 21:45:44 +02:00
committed by Andreas Kling
parent fadea53343
commit 8a9d5ee1a1
Notes: github-actions[bot] 2026-04-15 13:59:08 +00:00
9 changed files with 163 additions and 21 deletions

View File

@@ -575,11 +575,24 @@ GC::Ref<ExportedWasmFunction> ExportedWasmFunction::create(JS::Realm& realm, Utf
}
ExportedWasmFunction::ExportedWasmFunction(Utf16FlyString name, AK::Function<JS::ThrowCompletionOr<JS::Value>(JS::VM&)> behavior, Wasm::FunctionAddress exported_address, JS::Object& prototype)
: NativeFunction(move(name), move(behavior), prototype)
: NativeFunction(move(name), prototype)
, m_behavior(move(behavior))
, m_exported_address(exported_address)
{
}
void ExportedWasmFunction::visit_edges(Cell::Visitor& visitor)
{
NativeFunction::visit_edges(visitor);
visitor.visit_possible_values(m_behavior.raw_capture_range());
}
JS::ThrowCompletionOr<JS::Value> ExportedWasmFunction::call()
{
VERIFY(m_behavior);
return m_behavior(vm());
}
JS::NativeFunction* create_native_function(JS::VM& vm, Wasm::FunctionAddress address, Utf16FlyString name, Instance* instance)
{
auto& realm = *vm.current_realm();