fix(fetch): implement Request.text()/.json()/.arrayBuffer() (#1688) - #1690
Merged
Conversation
`new Request(url, { body }).text()` / `.json()` / `.arrayBuffer()` returned
`0` instead of the request body. `req.method` / `req.url` / `req.headers`
already worked (#1649); only the body-consuming methods were unimplemented.
The typed `module=="Request"` codegen arm in `lower_call/options/fetch.rs`
handled the url/method/body/headers property getters but had no body-method
dispatch, so `req.text()` fell through to a generic path that yielded `0`.
- Runtime (perry-stdlib): add `js_request_text` / `js_request_json` /
`js_request_array_buffer`, mirroring the Response `js_fetch_response_text`
/ `js_fetch_response_json` / `js_response_array_buffer` path. The body is
in-memory so the promise resolves synchronously (the LLVM await loop
doesn't drain the deferred pump). A bodiless request reads as "".
- Runtime (perry-ext-fetch): same three FFIs for the well-known-flip path,
matching that crate's conventions + a data-path unit test.
- Codegen: route `text`/`json`/`arrayBuffer` in the Request arm to the new
FFIs (Promise pointer NaN-boxed as POINTER_TAG); declare them in
runtime_decls.
- Extend test_gap_fetch_response.ts with the three body methods (verified
byte-for-byte against `node --experimental-strip-types`).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Implements
Request.text()/.json()/.arrayBuffer()under Perry-native compile. Closes #1688.Before this change,
new Request(url, { body }).text()/.json()/.arrayBuffer()returned0instead of the request body.req.method/req.url/req.headersalready worked (#1649); only the body-consuming methods were unimplemented — the typedmodule=="Request"codegen arm inlower_call/options/fetch.rshandled the property getters but had no body-method dispatch, soreq.text()fell through to a generic path that yielded0.Changes
perry-stdlib): addjs_request_text/js_request_json/js_request_array_buffer, mirroring the existing Responsejs_fetch_response_text/js_fetch_response_json/js_response_array_bufferpath. Body is in-memory, so the promise resolves synchronously (the LLVM await loop doesn't drain the deferred pump). A bodiless request reads as"".perry-ext-fetch): same three FFIs for the well-known-flip path, matching that crate's conventions, plus a data-path unit test.text/json/arrayBufferin the Request arm to the new FFIs (Promise pointer NaN-boxed as POINTER_TAG); declare them inruntime_decls.test_gap_fetch_response.tswith the three body methods.Validation
Byte-for-byte identical to
node --experimental-strip-typeson the fulltest_gap_fetch_response.tsgap test (including the newrequest text/request json data/request arrayBuffer byteLengthlines).cargo test -p perry-ext-fetchgreen (7 tests).cargo fmt --all -- --checkclean.Both code paths were exercised: the well-known flip routed
fetch→perry-ext-fetchand linked cleanly, confirming the ext-fetch additions are also reached.Note (out of scope)
Using
new Request(...)inline (e.g.await new Request(...).text()) doesn't set theuses_fetchfeature flag, so the auto-optimize build omits the fetch module and the link fails — a pre-existing feature-detection limitation that affects all inline-Request method calls (andjs_request_newitself), not just the new ones. Assigning to a variable first (const r = new Request(...), as in the #1688 repro) works. Tracked separately in #1691.