Skip to content

Commit

Permalink
wpt fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jan 31, 2025
1 parent b6b47ce commit 937ab65
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"test:wpt:debug": "tests/wpt-harness/build-wpt-runtime.sh --debug-build && node ./tests/wpt-harness/run-wpt.mjs -vv",
"test:types": "tsd",
"clean": "rm -f starling.wasm fastly.wasm fastly.debug.wasm fastly-weval.wasm fastly-ics.wevalcache fastly-js-compute-*.tgz",
"build": "npm run clean && npm run build:release && npm run build:debug && npm run build:weval",
"build": "npm run clean && npm run build:debug && npm run build:release && npm run build:weval",
"build:release": "./runtime/fastly/build-release.sh",
"build:debug": "./runtime/fastly/build-debug.sh",
"build:weval": "./runtime/fastly/build-release-weval.sh",
Expand Down
2 changes: 1 addition & 1 deletion runtime/fastly/builtins/cache-simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace fastly::cache_simple {
template <RequestOrResponse::BodyReadResult result_type>
bool SimpleCacheEntry::bodyAll(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0);
return RequestOrResponse::bodyAll<result_type>(cx, args, self);
return RequestOrResponse::bodyAll<result_type, false>(cx, args, self);
}

bool SimpleCacheEntry::body_get(JSContext *cx, unsigned argc, JS::Value *vp) {
Expand Down
16 changes: 12 additions & 4 deletions runtime/fastly/builtins/fetch/fetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,20 @@ bool apply_body_transform(JSContext *cx, JS::HandleValue response, host_api::Htt
JS::MutableHandleObject ret_promise) {
JS::RootedObject response_obj(cx, &response.toObject());
// Get the entire body from the response (asynchronously)
JS::RootedValue array_buffer_ret(cx);
if (!JS::Call(cx, response_obj, "arrayBuffer", JS::HandleValueArray::empty(),
&array_buffer_ret)) {
// JS::RootedValue array_buffer_ret(cx);

JS::Value array_buffer_ret;
JS::CallArgs args = JS::CallArgsFromVp(0, &array_buffer_ret);
if (!RequestOrResponse::bodyAll<RequestOrResponse::BodyReadResult::ArrayBuffer, true>(
cx, args, response_obj)) {
return false;
}
JS::RootedObject array_buffer_promise(cx, JS::CallOriginalPromiseResolve(cx, array_buffer_ret));
JS::RootedValue array_buffer(cx, array_buffer_ret);
// if (!JS::Call(cx, response_obj, "arrayBuffer", JS::HandleValueArray::empty(),
// &array_buffer_ret)) {
// return false;
// }
JS::RootedObject array_buffer_promise(cx, JS::CallOriginalPromiseResolve(cx, array_buffer));
if (!array_buffer_promise) {
return false;
}
Expand Down
44 changes: 24 additions & 20 deletions runtime/fastly/builtins/fetch/request-response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,21 @@ struct ReadResult {

// Returns a UniqueChars and the length of that string. The UniqueChars value is not
// null-terminated.
ReadResult read_from_handle_all(JSContext *cx, host_api::HttpBody body) {
template <bool async> ReadResult read_from_handle_all(JSContext *cx, host_api::HttpBody body) {
std::vector<host_api::HostString> chunks;
size_t bytes_read = 0;
bool end_of_stream = true;
while (true) {

auto ready_res = body.is_ready();
if (auto *err = ready_res.to_err()) {
HANDLE_ERROR(cx, *err);
return {nullptr, 0, StreamState::Error};
}
if (!ready_res.unwrap()) {

end_of_stream = false;
break;
if (async) {
auto ready_res = body.is_ready();
if (auto *err = ready_res.to_err()) {
HANDLE_ERROR(cx, *err);
return {nullptr, 0, StreamState::Error};
}
if (!ready_res.unwrap()) {
end_of_stream = false;
break;
}
}
auto res = body.read(HANDLE_READ_CHUNK_SIZE);
if (auto *err = res.to_err()) {
Expand Down Expand Up @@ -1388,7 +1388,7 @@ bool async_process_body_handle_for_bodyAll(JSContext *cx, uint32_t handle, JS::H
JS::HandleValue body_parser) {
auto body = RequestOrResponse::body_handle(self);
auto *parse_body = reinterpret_cast<RequestOrResponse::ParseBodyCB *>(body_parser.toPrivate());
auto [buf, bytes_read, state] = read_from_handle_all(cx, body);
auto [buf, bytes_read, state] = read_from_handle_all<true>(cx, body);
if (state == StreamState::Error) {

JS::RootedObject result_promise(cx);
Expand All @@ -1411,14 +1411,15 @@ bool async_process_body_handle_for_bodyAll(JSContext *cx, uint32_t handle, JS::H
return true;
}

template <bool async>
bool RequestOrResponse::consume_body_handle_for_bodyAll(JSContext *cx, JS::HandleObject self,
JS::HandleValue body_parser,
JS::CallArgs args) {
auto body = body_handle(self);
auto *parse_body = reinterpret_cast<ParseBodyCB *>(body_parser.toPrivate());
auto [buf, bytes_read, state] = read_from_handle_all(cx, body);
auto [buf, bytes_read, state] = read_from_handle_all<async>(cx, body);
MOZ_ASSERT(async || state != StreamState::Wait);
if (state == StreamState::Error) {

JS::RootedObject result_promise(cx);
result_promise =
&JS::GetReservedSlot(self, static_cast<uint32_t>(Slots::BodyAllPromise)).toObject();
Expand All @@ -1427,17 +1428,20 @@ bool RequestOrResponse::consume_body_handle_for_bodyAll(JSContext *cx, JS::Handl
}

if (state == StreamState::Complete) {

return parse_body(cx, self, std::move(buf), bytes_read);
}

// still have to wait for the stream to complete, queue an async task
ENGINE->queue_async_task(new FastlyAsyncTask(body.async_handle(), self, body_parser,
ENGINE->queue_async_task(new FastlyAsyncTask(body.async_handle(), self, JS::UndefinedHandleValue,
async_process_body_handle_for_bodyAll));
return true;
}

template <RequestOrResponse::BodyReadResult result_type>
// Async case only used from fetch.cpp currently
template bool RequestOrResponse::bodyAll<RequestOrResponse::BodyReadResult::ArrayBuffer, true>(
JSContext *, JS::CallArgs, JS::HandleObject);

template <RequestOrResponse::BodyReadResult result_type, bool async>
bool RequestOrResponse::bodyAll(JSContext *cx, JS::CallArgs args, JS::HandleObject self) {
// TODO: mark body as consumed when operating on stream, too.
if (body_used(self)) {
Expand Down Expand Up @@ -1493,7 +1497,7 @@ bool RequestOrResponse::bodyAll(JSContext *cx, JS::CallArgs args, JS::HandleObje
}
} else {

if (!enqueue_internal_method<consume_body_handle_for_bodyAll>(cx, self, body_parser)) {
if (!enqueue_internal_method<consume_body_handle_for_bodyAll<async>>(cx, self, body_parser)) {

return ReturnPromiseRejectedWithPendingError(cx, args);
}
Expand Down Expand Up @@ -2069,7 +2073,7 @@ bool Request::headers_get(JSContext *cx, unsigned argc, JS::Value *vp) {
template <RequestOrResponse::BodyReadResult result_type>
bool Request::bodyAll(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0)
return RequestOrResponse::bodyAll<result_type>(cx, args, self);
return RequestOrResponse::bodyAll<result_type, false>(cx, args, self);
}

bool Request::backend_get(JSContext *cx, unsigned argc, JS::Value *vp) {
Expand Down Expand Up @@ -3214,7 +3218,7 @@ bool Response::headers_get(JSContext *cx, unsigned argc, JS::Value *vp) {
template <RequestOrResponse::BodyReadResult result_type>
bool Response::bodyAll(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0)
return RequestOrResponse::bodyAll<result_type>(cx, args, self);
return RequestOrResponse::bodyAll<result_type, false>(cx, args, self);
}

bool Response::body_get(JSContext *cx, unsigned argc, JS::Value *vp) {
Expand Down
3 changes: 2 additions & 1 deletion runtime/fastly/builtins/fetch/request-response.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ class RequestOrResponse final {
JS::HandleValue extra, JS::CallArgs args);
static bool consume_content_stream_for_bodyAll(JSContext *cx, JS::HandleObject self,
JS::HandleValue stream_val, JS::CallArgs args);
template <bool async>
static bool consume_body_handle_for_bodyAll(JSContext *cx, JS::HandleObject self,
JS::HandleValue body_parser, JS::CallArgs args);
template <RequestOrResponse::BodyReadResult result_type>
template <RequestOrResponse::BodyReadResult result_type, bool async>
static bool bodyAll(JSContext *cx, JS::CallArgs args, JS::HandleObject self);
static bool body_source_cancel_algorithm(JSContext *cx, JS::CallArgs args,
JS::HandleObject stream, JS::HandleObject owner,
Expand Down
2 changes: 1 addition & 1 deletion runtime/fastly/builtins/kv-store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ std::optional<char> find_invalid_character_for_kv_store_key(const char *str) {
template <RequestOrResponse::BodyReadResult result_type>
bool KVStoreEntry::bodyAll(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0)
return RequestOrResponse::bodyAll<result_type>(cx, args, self);
return RequestOrResponse::bodyAll<result_type, false>(cx, args, self);
}

bool KVStoreEntry::body_get(JSContext *cx, unsigned argc, JS::Value *vp) {
Expand Down

0 comments on commit 937ab65

Please sign in to comment.