-
Notifications
You must be signed in to change notification settings - Fork 50
Implement Response.clone (#125) #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
zerotri
wants to merge
6
commits into
bytecodealliance:main
Choose a base branch
from
zerotri:response-clone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+149
−19
Draft
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7b2e0bc
Implement Response.clone (#125)
zerotri b4e1371
Expose URL slot to `Response` to match `Request`.
zerotri 3ba47dd
Clean up `Response.clone`
zerotri a783a52
Rewriting `Response::clone` using `RequestOrResponse::headers_handle_…
zerotri b72a2f0
Modifiying `Request::clone` to match changes to `Response::clone`
zerotri f4d1982
Modify fetch.js tests to check for headers being cloned rather than s…
zerotri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2291,6 +2291,91 @@ bool Response::redirect(JSContext *cx, unsigned argc, Value *vp) { | |||||
| // return true; | ||||||
| // } | ||||||
|
|
||||||
| /// https://fetch.spec.whatwg.org/#dom-response-clone | ||||||
| bool Response::clone(JSContext *cx, unsigned argc, JS::Value *vp) { | ||||||
| // If this is unusable, then throw a TypeError. | ||||||
| METHOD_HEADER(0); | ||||||
|
|
||||||
| // To clone a response response, run these steps: | ||||||
| // 1. If response is a filtered response, then return a new identical filtered response whose internal response is a clone of response’s internal response. | ||||||
| RootedObject new_response(cx, create(cx)); | ||||||
| if (!new_response) { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| init_slots(new_response); | ||||||
|
|
||||||
| // 2. Let newResponse be a copy of response, except for its body. | ||||||
| RootedValue cloned_headers_val(cx, JS::NullValue()); | ||||||
| RootedObject headers(cx, RequestOrResponse::maybe_headers(self)); | ||||||
| if (headers) { | ||||||
| RootedValue headers_val(cx, ObjectValue(*headers)); | ||||||
| JSObject *cloned_headers = Headers::create(cx, headers_val, Headers::guard(headers)); | ||||||
| if (!cloned_headers) { | ||||||
| return false; | ||||||
| } | ||||||
| cloned_headers_val.set(ObjectValue(*cloned_headers)); | ||||||
| } else if (RequestOrResponse::maybe_handle(self)) { | ||||||
| auto handle = RequestOrResponse::headers_handle_clone(cx, self); | ||||||
| JSObject *cloned_headers = | ||||||
| Headers::create(cx, handle.release(), | ||||||
| RequestOrResponse::is_incoming(self) ? Headers::HeadersGuard::Immutable | ||||||
| : Headers::HeadersGuard::Response); | ||||||
| if (!cloned_headers) { | ||||||
| return false; | ||||||
| } | ||||||
| cloned_headers_val.set(ObjectValue(*cloned_headers)); | ||||||
| } | ||||||
|
|
||||||
| SetReservedSlot(new_response, static_cast<uint32_t>(Slots::Headers), cloned_headers_val); | ||||||
|
|
||||||
| Value status_val = GetReservedSlot(self, static_cast<uint32_t>(Slots::Status)); | ||||||
| Value status_message_val = GetReservedSlot(self, static_cast<uint32_t>(Slots::StatusMessage)); | ||||||
|
|
||||||
| // ENGINE->dump_value(status_val, stderr); | ||||||
| // ENGINE->dump_value(status_message_val, stderr); | ||||||
|
|
||||||
|
||||||
| SetReservedSlot(new_response, static_cast<uint32_t>(Slots::Status), status_val); | ||||||
| SetReservedSlot(new_response, static_cast<uint32_t>(Slots::StatusMessage), status_message_val); | ||||||
|
|
||||||
| // 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body. | ||||||
|
||||||
| // 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body. | |
| // 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should be able to use
RequestOrResponse::headers_handle_clonefor this, reducing complexity a bunch. If that's not quite correct to use here, perhaps it can instead be tweaked?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was unable to work out a simple way to use
RequestOrResponse::headers_handle_clonefor this, however the solution I've added in a recent commit that constructs aRootedObjectbased onRequestOrResponse::headersappears to do the trick. I'd love an extra set of eyes on that approach though as I'm still wrapping my head around internal object apis.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this isn't quite right: it will always return an instance of
builtins::web::fetch::Headerstied toself, not a clone of that each time it's called. You should be able to see this if you create a response, clone it usingResponse.clone, and then do something likeresponse.headers.set("foo", "bar"): this should now show up on both responses.Can you say what didn't work when using
headers_handle_clone?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I gave it another look over and was able to get
headers_handle_cloneto work for this. I'll chalk it up to my own ignorance regarding the codebase. I've mirrored the changes toRequest::cloneas well and wrote up some changes to the tests to check that the headers are properly cloned rather than shared.Once any further changes are worked through, do you prefer keeping the individual commits in this PR or would a force-push of squashed commits be preferred?