1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:47:35 +00:00

LibWeb: Propagate Realm instead of VM more through Fetch

This makes Fetch rely less on using main_thread_vm().current_realm(),
which relies on the dummy execution context if no JavaScript is
currently running.
This commit is contained in:
Luke Wilde 2023-02-28 17:45:49 +00:00 committed by Linus Groh
parent f7ff1fd985
commit 9acc542059
19 changed files with 62 additions and 49 deletions

View file

@ -25,16 +25,12 @@ Body::Body(JS::Handle<Streams::ReadableStream> stream, SourceType source, Option
}
// https://fetch.spec.whatwg.org/#concept-body-clone
WebIDL::ExceptionOr<Body> Body::clone() const
WebIDL::ExceptionOr<Body> Body::clone(JS::Realm& realm) const
{
// To clone a body body, run these steps:
auto& vm = Bindings::main_thread_vm();
auto& realm = *vm.current_realm();
// FIXME: 1. Let « out1, out2 » be the result of teeing bodys stream.
// FIXME: 2. Set bodys stream to out1.
auto out2 = MUST_OR_THROW_OOM(vm.heap().allocate<Streams::ReadableStream>(realm, realm));
auto out2 = MUST_OR_THROW_OOM(realm.heap().allocate<Streams::ReadableStream>(realm, realm));
// 3. Return a body whose stream is out2 and other members are copied from body.
return Body { JS::make_handle(out2), m_source, m_length };

View file

@ -31,7 +31,7 @@ public:
[[nodiscard]] SourceType const& source() const { return m_source; }
[[nodiscard]] Optional<u64> const& length() const { return m_length; }
WebIDL::ExceptionOr<Body> clone() const;
WebIDL::ExceptionOr<Body> clone(JS::Realm&) const;
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> fully_read_as_promise() const;

View file

@ -195,9 +195,10 @@ ErrorOr<ByteBuffer> Request::byte_serialize_origin() const
}
// https://fetch.spec.whatwg.org/#concept-request-clone
WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::clone(JS::VM& vm) const
WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::clone(JS::Realm& realm) const
{
// To clone a request request, run these steps:
auto& vm = realm.vm();
// 1. Let newRequest be a copy of request, except for its body.
auto new_request = Infrastructure::Request::create(vm);
@ -242,7 +243,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::clone(JS::VM& vm) const
// 2. If requests body is non-null, set newRequests body to the result of cloning requests body.
if (auto const* body = m_body.get_pointer<Body>())
new_request->set_body(TRY(body->clone()));
new_request->set_body(TRY(body->clone(realm)));
// 3. Return newRequest.
return new_request;

View file

@ -297,7 +297,7 @@ public:
[[nodiscard]] ErrorOr<String> serialize_origin() const;
[[nodiscard]] ErrorOr<ByteBuffer> byte_serialize_origin() const;
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> clone(JS::VM&) const;
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> clone(JS::Realm&) const;
[[nodiscard]] ErrorOr<void> add_range_header(u64 first, Optional<u64> const& last);
[[nodiscard]] ErrorOr<void> add_origin_header();

View file

@ -127,13 +127,14 @@ ErrorOr<Optional<AK::URL>> Response::location_url(Optional<String> const& reques
}
// https://fetch.spec.whatwg.org/#concept-response-clone
WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::clone(JS::VM& vm) const
WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::clone(JS::Realm& realm) const
{
// To clone a response response, run these steps:
auto& vm = realm.vm();
// 1. If response is a filtered response, then return a new identical filtered response whose internal response is a clone of responses internal response.
if (is<FilteredResponse>(*this)) {
auto internal_response = TRY(static_cast<FilteredResponse const&>(*this).internal_response()->clone(vm));
auto internal_response = TRY(static_cast<FilteredResponse const&>(*this).internal_response()->clone(realm));
if (is<BasicFilteredResponse>(*this))
return TRY_OR_THROW_OOM(vm, BasicFilteredResponse::create(vm, internal_response));
if (is<CORSFilteredResponse>(*this))
@ -164,7 +165,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::clone(JS::VM& vm) cons
// 3. If responses body is non-null, then set newResponses body to the result of cloning responses body.
if (m_body.has_value())
new_response->set_body(TRY(m_body->clone()));
new_response->set_body(TRY(m_body->clone(realm)));
// 4. Return newResponse.
return new_response;

View file

@ -106,7 +106,7 @@ public:
[[nodiscard]] Optional<AK::URL const&> url() const;
[[nodiscard]] ErrorOr<Optional<AK::URL>> location_url(Optional<String> const& request_fragment) const;
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> clone(JS::VM&) const;
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> clone(JS::Realm&) const;
// Non-standard
[[nodiscard]] Optional<StringView> network_error_message() const;