1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:57:36 +00:00

LibWeb: Remove GC allocations in FetchAlgorithms constructor

We should not GC allocate in the constructors of GC-allocated objects
because a new allocation might trigger garbage collection, which in
turn might access not fully initialized objects.
This commit is contained in:
Aliaksandr Kalenik 2023-09-03 17:32:52 +02:00 committed by Andreas Kling
parent f9f9c38de3
commit 5951a93f51
2 changed files with 46 additions and 15 deletions

View file

@ -12,16 +12,34 @@ namespace Web::Fetch::Infrastructure {
JS::NonnullGCPtr<FetchAlgorithms> FetchAlgorithms::create(JS::VM& vm, Input input) JS::NonnullGCPtr<FetchAlgorithms> FetchAlgorithms::create(JS::VM& vm, Input input)
{ {
return vm.heap().allocate_without_realm<FetchAlgorithms>(vm, move(input)); auto process_request_body_chunk_length = JS::create_heap_function(vm.heap(), move(input.process_request_body_chunk_length));
auto process_request_end_of_body = JS::create_heap_function(vm.heap(), move(input.process_request_end_of_body));
auto process_early_hints_response = JS::create_heap_function(vm.heap(), move(input.process_early_hints_response));
auto process_response = JS::create_heap_function(vm.heap(), move(input.process_response));
auto process_response_end_of_body = JS::create_heap_function(vm.heap(), move(input.process_response_end_of_body));
auto process_response_consume_body = JS::create_heap_function(vm.heap(), move(input.process_response_consume_body));
return vm.heap().allocate_without_realm<FetchAlgorithms>(
process_request_body_chunk_length,
process_request_end_of_body,
process_early_hints_response,
process_response,
process_response_end_of_body,
process_response_consume_body);
} }
FetchAlgorithms::FetchAlgorithms(JS::VM& vm, Input input) FetchAlgorithms::FetchAlgorithms(
: m_process_request_body_chunk_length(JS::create_heap_function(vm.heap(), move(input.process_request_body_chunk_length))) ProcessRequestBodyChunkLengthHeapFunction process_request_body_chunk_length,
, m_process_request_end_of_body(JS::create_heap_function(vm.heap(), move(input.process_request_end_of_body))) ProcessRequestEndOfBodyHeapFunction process_request_end_of_body,
, m_process_early_hints_response(JS::create_heap_function(vm.heap(), move(input.process_early_hints_response))) ProcessEarlyHintsResponseHeapFunction process_early_hints_response,
, m_process_response(JS::create_heap_function(vm.heap(), move(input.process_response))) ProcessResponseHeapFunction process_response,
, m_process_response_end_of_body(JS::create_heap_function(vm.heap(), move(input.process_response_end_of_body))) ProcessResponseEndOfBodyHeapFunction process_response_end_of_body,
, m_process_response_consume_body(JS::create_heap_function(vm.heap(), move(input.process_response_consume_body))) ProcessResponseConsumeBodyHeapFunction process_response_consume_body)
: m_process_request_body_chunk_length(process_request_body_chunk_length)
, m_process_request_end_of_body(process_request_end_of_body)
, m_process_early_hints_response(process_early_hints_response)
, m_process_response(process_response)
, m_process_response_end_of_body(process_response_end_of_body)
, m_process_response_consume_body(process_response_consume_body)
{ {
} }

View file

@ -30,6 +30,13 @@ public:
using ProcessResponseEndOfBodyFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>; using ProcessResponseEndOfBodyFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
using ProcessResponseConsumeBodyFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>, BodyBytes)>; using ProcessResponseConsumeBodyFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>, BodyBytes)>;
using ProcessRequestBodyChunkLengthHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessRequestBodyChunkLengthFunction::FunctionType>>;
using ProcessRequestEndOfBodyHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessRequestEndOfBodyFunction::FunctionType>>;
using ProcessEarlyHintsResponseHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessEarlyHintsResponseFunction::FunctionType>>;
using ProcessResponseHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessResponseFunction::FunctionType>>;
using ProcessResponseEndOfBodyHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessResponseEndOfBodyFunction::FunctionType>>;
using ProcessResponseConsumeBodyHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessResponseConsumeBodyFunction::FunctionType>>;
struct Input { struct Input {
ProcessRequestBodyChunkLengthFunction process_request_body_chunk_length; ProcessRequestBodyChunkLengthFunction process_request_body_chunk_length;
ProcessRequestEndOfBodyFunction process_request_end_of_body; ProcessRequestEndOfBodyFunction process_request_end_of_body;
@ -51,14 +58,20 @@ public:
virtual void visit_edges(JS::Cell::Visitor&) override; virtual void visit_edges(JS::Cell::Visitor&) override;
private: private:
explicit FetchAlgorithms(JS::VM&, Input); explicit FetchAlgorithms(
ProcessRequestBodyChunkLengthHeapFunction process_request_body_chunk_length,
ProcessRequestEndOfBodyHeapFunction process_request_end_of_body,
ProcessEarlyHintsResponseHeapFunction process_early_hints_response,
ProcessResponseHeapFunction process_response,
ProcessResponseEndOfBodyHeapFunction process_response_end_of_body,
ProcessResponseConsumeBodyHeapFunction process_response_consume_body);
JS::NonnullGCPtr<JS::HeapFunction<void(u64)>> m_process_request_body_chunk_length; ProcessRequestBodyChunkLengthHeapFunction m_process_request_body_chunk_length;
JS::NonnullGCPtr<JS::HeapFunction<void()>> m_process_request_end_of_body; ProcessRequestEndOfBodyHeapFunction m_process_request_end_of_body;
JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>> m_process_early_hints_response; ProcessEarlyHintsResponseHeapFunction m_process_early_hints_response;
JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>> m_process_response; ProcessResponseHeapFunction m_process_response;
JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>> m_process_response_end_of_body; ProcessResponseEndOfBodyHeapFunction m_process_response_end_of_body;
JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>, BodyBytes)>> m_process_response_consume_body; ProcessResponseConsumeBodyHeapFunction m_process_response_consume_body;
}; };
} }