Skip to content
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

More fetch fixes #4

Closed
wants to merge 7 commits into from
38 changes: 18 additions & 20 deletions builtins/web/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,33 @@ JS::Result<std::string> convertJSValueToByteString(JSContext *cx, JS::Handle<JS:
// Conversion from JavaScript string to ByteString is only valid if all
// characters < 256. This is always the case for Latin1 strings.
size_t length;
UniqueChars result = nullptr;
if (!JS::StringHasLatin1Chars(s)) {
// Creating an exception can GC, so we first scan the string for bad chars
// and report the error outside the AutoCheckCannotGC scope.
bool foundBadChar = false;
{
JS::AutoCheckCannotGC nogc;
const char16_t *chars = JS_GetTwoByteStringCharsAndLength(cx, nogc, s, &length);
if (!chars) {
JS::AutoCheckCannotGC nogc(cx);
const char16_t *chars = JS_GetTwoByteStringCharsAndLength(cx, nogc, s, &length);
if (!chars) {
// Reset the nogc guard, since otherwise we can't throw errors.
nogc.reset();
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_INVALID_CHARACTER_ERROR);
return JS::Result<std::string>(JS::Error());
}

for (size_t i = 0; i < length; i++) {
if (chars[i] > 255) {
// Reset the nogc guard, since otherwise we can't throw errors.
nogc.reset();
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_INVALID_CHARACTER_ERROR);
return JS::Result<std::string>(JS::Error());
}

for (size_t i = 0; i < length; i++) {
if (chars[i] > 255) {
foundBadChar = true;
break;
}
}
}

if (foundBadChar) {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_INVALID_CHARACTER_ERROR);
return JS::Result<std::string>(JS::Error());
}
auto latin1_z =
JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, chars, length);
result = UniqueChars(reinterpret_cast<char*>(latin1_z.get()));
} else {
length = JS::GetStringLength(s);
result = JS_EncodeStringToLatin1(cx, s);
}

UniqueChars result = JS_EncodeStringToLatin1(cx, s);
if (!result) {
return JS::Result<std::string>(JS::Error());
}
Expand Down
1 change: 1 addition & 0 deletions builtins/web/crypto/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ bool Crypto::get_random_values(JSContext *cx, unsigned argc, JS::Value *vp) {

auto res = host_api::Random::get_bytes(byte_length);
if (auto *err = res.to_err()) {
noGC.reset();
HANDLE_ERROR(cx, *err);
return false;
}
Expand Down
100 changes: 32 additions & 68 deletions builtins/web/fetch/request-response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,71 +624,44 @@ bool RequestOrResponse::content_stream_read_then_handler(JSContext *cx, JS::Hand
if (!JS::GetArrayLength(cx, contents, &contentsLength)) {
return false;
}
// TODO(performance): investigate whether we can infer the size directly from `contents`
size_t buf_size = HANDLE_READ_CHUNK_SIZE;
// TODO(performance): make use of malloc slack.
// https://github.com/fastly/js-compute-runtime/issues/217
size_t offset = 0;
// In this loop we are finding the length of each entry in `contents` and resizing the `buf`
// until it is large enough to fit all the entries in `contents`
for (uint32_t index = 0; index < contentsLength; index++) {
JS::RootedValue val(cx);

size_t total_length = 0;
RootedValue val(cx);

for (size_t index = 0; index < contentsLength; index++) {
if (!JS_GetElement(cx, contents, index, &val)) {
return false;
}
{
JS::AutoCheckCannotGC nogc;
MOZ_ASSERT(val.isObject());
JSObject *array = &val.toObject();
MOZ_ASSERT(JS_IsUint8Array(array));
size_t length = JS_GetTypedArrayByteLength(array);
if (length) {
offset += length;
// if buf is not big enough to fit the next uint8array's bytes then resize
if (offset > buf_size) {
buf_size =
buf_size + (HANDLE_READ_CHUNK_SIZE * ((length / HANDLE_READ_CHUNK_SIZE) + 1));
}
}
}
JSObject *array = &val.toObject();
size_t length = JS_GetTypedArrayByteLength(array);
total_length += length;
}

JS::UniqueChars buf{static_cast<char *>(JS_malloc(cx, buf_size + 1))};
JS::UniqueChars buf{static_cast<char *>(JS_malloc(cx, total_length))};
if (!buf) {
JS_ReportOutOfMemory(cx);
return false;
}
// reset the offset for the next loop
offset = 0;

size_t offset = 0;
// In this loop we are inserting each entry in `contents` into `buf`
for (uint32_t index = 0; index < contentsLength; index++) {
JS::RootedValue val(cx);
if (!JS_GetElement(cx, contents, index, &val)) {
return false;
}
{
JS::AutoCheckCannotGC nogc;
MOZ_ASSERT(val.isObject());
JSObject *array = &val.toObject();
MOZ_ASSERT(JS_IsUint8Array(array));
bool is_shared;
size_t length = JS_GetTypedArrayByteLength(array);
if (length) {
static_assert(CHAR_BIT == 8, "Strange char");
auto bytes = reinterpret_cast<char *>(JS_GetUint8ArrayData(array, &is_shared, nogc));
memcpy(buf.get() + offset, bytes, length);
offset += length;
}
}
}
buf[offset] = '\0';
#ifdef DEBUG
bool foundBodyParser;
if (!JS_HasElement(cx, catch_handler, 2, &foundBodyParser)) {
return false;
JSObject *array = &val.toObject();
bool is_shared;
size_t length = JS_GetTypedArrayByteLength(array);
JS::AutoCheckCannotGC nogc(cx);
auto bytes = reinterpret_cast<char *>(JS_GetUint8ArrayData(array, &is_shared, nogc));
memcpy(buf.get() + offset, bytes, length);
offset += length;
}

mozilla::DebugOnly foundBodyParser = false;
MOZ_ASSERT(JS_HasElement(cx, catch_handler, 2, &foundBodyParser));
MOZ_ASSERT(foundBodyParser);
#endif

// Now we can call parse_body on the result
JS::RootedValue body_parser(cx);
if (!JS_GetElement(cx, catch_handler, 2, &body_parser)) {
Expand Down Expand Up @@ -983,17 +956,16 @@ bool reader_for_outgoing_body_then_handler(JSContext *cx, JS::HandleObject body_
return false;
}

host_api::Result<host_api::Void> res;
{
JS::AutoCheckCannotGC nogc;
JSObject *array = &val.toObject();
bool is_shared;
uint8_t *bytes = JS_GetUint8ArrayData(array, &is_shared, nogc);
size_t length = JS_GetTypedArrayByteLength(array);
// TODO: change this to write in chunks, respecting backpressure.
auto body = RequestOrResponse::outgoing_body_handle(body_owner);
res = body->write_all(bytes, length);
}
RootedObject array(cx, &val.toObject());
size_t length = JS_GetTypedArrayByteLength(array);
bool is_shared;
RootedObject buffer(cx, JS_GetArrayBufferViewBuffer(cx, array, &is_shared));
MOZ_ASSERT(!is_shared);
auto bytes = static_cast<uint8_t *>(StealArrayBufferContents(cx, buffer));
// TODO: change this to write in chunks, respecting backpressure.
auto body = RequestOrResponse::outgoing_body_handle(body_owner);
auto res = body->write_all(bytes, length);
js_free(bytes);

// Needs to be outside the nogc block in case we need to create an exception.
if (auto *err = res.to_err()) {
Expand Down Expand Up @@ -1055,14 +1027,6 @@ bool RequestOrResponse::maybe_stream_body(JSContext *cx, JS::HandleObject body_o
return false;
}

if (streams::TransformStream::is_ts_readable(cx, stream)) {
JSObject *ts = streams::TransformStream::ts_from_readable(cx, stream);
if (streams::TransformStream::readable_used_as_body(ts)) {
*requires_streaming = true;
return true;
}
}

// If the body stream is backed by an HTTP body handle, we can directly pipe
// that handle into the body we're about to send.
if (streams::NativeStreamSource::stream_is_body(cx, stream)) {
Expand Down
2 changes: 1 addition & 1 deletion builtins/web/streams/compression-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ bool deflate_chunk(JSContext *cx, JS::HandleObject self, JS::HandleValue chunk,

{
bool is_shared;
JS::AutoCheckCannotGC nogc;
JS::AutoCheckCannotGC nogc(cx);
uint8_t *out_buffer = JS_GetUint8ArrayData(out_obj, &is_shared, nogc);
memcpy(out_buffer, buffer, bytes);
}
Expand Down
4 changes: 4 additions & 0 deletions builtins/web/timers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class TimerTask final : public api::AsyncTask {
return true;
}

[[nodiscard]] uint64_t deadline() override {
return deadline_;
}

void trace(JSTracer *trc) override {
TraceEdge(trc, &callback_, "Timer callback");
for (auto &arg : arguments_) {
Expand Down
4 changes: 4 additions & 0 deletions include/extension-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ class AsyncTask {
return handle_;
}

[[nodiscard]] virtual uint64_t deadline() {
return 0;
}

virtual void trace(JSTracer *trc) = 0;

/**
Expand Down
Loading