Skip to content

Commit 83747aa

Browse files
elliotttJakeChampion
authored andcommitted
Return an optional span from value_to_buffer
1 parent a50486e commit 83747aa

File tree

5 files changed

+29
-26
lines changed

5 files changed

+29
-26
lines changed

c-dependencies/js-compute-runtime/builtins/compression-stream.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,24 @@ bool deflate_chunk(JSContext *cx, JS::HandleObject self, JS::HandleValue chunk,
7474
if (!finished) {
7575
// 1. If _chunk_ is not a `BufferSource` type, then throw a `TypeError`.
7676
// Step 2 of transform:
77-
size_t length;
78-
uint8_t *data = value_to_buffer(cx, chunk, "CompressionStream transform: chunks", &length);
79-
if (!data) {
77+
auto data = value_to_buffer(cx, chunk, "CompressionStream transform: chunks");
78+
if (!data.has_value()) {
8079
return false;
8180
}
8281

83-
if (length == 0) {
82+
if (data->size() == 0) {
8483
return true;
8584
}
8685

8786
// 2. Let _buffer_ be the result of compressing _chunk_ with _cs_'s format
8887
// and context. This just sets up step 2. The actual compression happen in
8988
// the `do` loop below.
90-
zstream->avail_in = length;
89+
zstream->avail_in = data->size();
9190

9291
// `data` is a live view into `chunk`. That's ok here because it'll be fully
9392
// used in the `do` loop below before any content can execute again and
9493
// could potentially invalidate the pointer to `data`.
95-
zstream->next_in = data;
94+
zstream->next_in = data->data();
9695
} else {
9796
// Step 1 of flush:
9897
// 1. Let _buffer_ be the result of compressing an empty input with _cs_'s

c-dependencies/js-compute-runtime/builtins/decompression-stream.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,24 @@ bool inflate_chunk(JSContext *cx, JS::HandleObject self, JS::HandleValue chunk,
6969
if (!finished) {
7070
// 1. If _chunk_ is not a `BufferSource` type, then throw a `TypeError`.
7171
// Step 2 of transform:
72-
size_t length;
73-
uint8_t *data = value_to_buffer(cx, chunk, "DecompressionStream transform: chunks", &length);
74-
if (!data) {
72+
auto data = value_to_buffer(cx, chunk, "DecompressionStream transform: chunks");
73+
if (!data.has_value()) {
7574
return false;
7675
}
7776

78-
if (length == 0) {
77+
if (data->size() == 0) {
7978
return true;
8079
}
8180

8281
// 2. Let _buffer_ be the result of decompressing _chunk_ with _ds_'s format
8382
// and context. This just sets up step 2. The actual decompression happen in
8483
// the `do` loop below.
85-
zstream->avail_in = length;
84+
zstream->avail_in = data->size();
8685

8786
// `data` is a live view into `chunk`. That's ok here because it'll be fully
8887
// used in the `do` loop below before any content can execute again and
8988
// could potentially invalidate the pointer to `data`.
90-
zstream->next_in = data;
89+
zstream->next_in = data->data();
9190
} else {
9291
// Step 1 of flush:
9392
// 1. Let _buffer_ be the result of decompressing an empty input with _ds_'s

c-dependencies/js-compute-runtime/builtins/subtle-crypto.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,8 @@ bool SubtleCrypto::digest(JSContext *cx, unsigned argc, JS::Value *vp) {
291291

292292
// 2 . Let data be the result of getting a copy of the bytes held by the data parameter
293293
// passed to the digest() method.
294-
size_t length;
295-
uint8_t *data = value_to_buffer(cx, args.get(1), "SubtleCrypto#digest: data", &length);
296-
if (!data) {
294+
auto data = value_to_buffer(cx, args.get(1), "SubtleCrypto#digest: data");
295+
if (!data.has_value()) {
297296
return false;
298297
}
299298

@@ -367,7 +366,7 @@ bool SubtleCrypto::digest(JSContext *cx, unsigned argc, JS::Value *vp) {
367366
JS_ReportOutOfMemory(cx);
368367
return false;
369368
}
370-
if (!EVP_Digest(data, length, buf, &size, alg, NULL)) {
369+
if (!EVP_Digest(data->data(), data->size(), buf, &size, alg, NULL)) {
371370
JS_ReportErrorUTF8(cx, "SubtleCrypto.digest: failed to create digest");
372371
return RejectPromiseWithPendingError(cx, promise);
373372
}

c-dependencies/js-compute-runtime/js-compute-builtins.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,27 @@ SpecString encode(JSContext *cx, HandleValue val) {
164164
return slice;
165165
}
166166

167-
uint8_t *value_to_buffer(JSContext *cx, HandleValue val, const char *val_desc, size_t *len) {
167+
std::optional<std::span<uint8_t>> value_to_buffer(JSContext *cx, HandleValue val,
168+
const char *val_desc) {
168169
if (!val.isObject() ||
169170
!(JS_IsArrayBufferViewObject(&val.toObject()) || JS::IsArrayBufferObject(&val.toObject()))) {
170171
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_INVALID_BUFFER_ARG, val_desc,
171172
val.type());
172-
return nullptr;
173+
return std::nullopt;
173174
}
174175

175176
RootedObject input(cx, &val.toObject());
176177
uint8_t *data;
177178
bool is_shared;
179+
size_t len = 0;
178180

179181
if (JS_IsArrayBufferViewObject(input)) {
180-
js::GetArrayBufferViewLengthAndData(input, len, &is_shared, &data);
182+
js::GetArrayBufferViewLengthAndData(input, &len, &is_shared, &data);
181183
} else {
182-
JS::GetArrayBufferLengthAndData(input, len, &is_shared, &data);
184+
JS::GetArrayBufferLengthAndData(input, &len, &is_shared, &data);
183185
}
184186

185-
return data;
187+
return std::span(data, len);
186188
}
187189

188190
bool RejectPromiseWithPendingError(JSContext *cx, HandleObject promise) {
@@ -2855,13 +2857,13 @@ bool decode(JSContext *cx, unsigned argc, Value *vp) {
28552857
return true;
28562858
}
28572859

2858-
size_t length;
2859-
uint8_t *data = value_to_buffer(cx, args[0], "TextDecoder#decode: input", &length);
2860-
if (!data) {
2860+
auto data = value_to_buffer(cx, args[0], "TextDecoder#decode: input");
2861+
if (!data.has_value()) {
28612862
return false;
28622863
}
28632864

2864-
RootedString str(cx, JS_NewStringCopyUTF8N(cx, JS::UTF8Chars((char *)data, length)));
2865+
RootedString str(cx,
2866+
JS_NewStringCopyUTF8N(cx, JS::UTF8Chars((char *)data->data(), data->size())));
28652867
if (!str)
28662868
return false;
28672869

c-dependencies/js-compute-runtime/js-compute-builtins.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef fastly_sys_h
22
#define fastly_sys_h
33

4+
#include <optional>
5+
#include <span>
6+
47
// TODO: remove these once the warnings are fixed
58
#pragma clang diagnostic push
69
#pragma clang diagnostic ignored "-Winvalid-offsetof"
@@ -46,7 +49,8 @@ inline bool ReturnPromiseRejectedWithPendingError(JSContext *cx, const JS::CallA
4649
return true;
4750
}
4851

49-
uint8_t *value_to_buffer(JSContext *cx, JS::HandleValue val, const char *val_desc, size_t *len);
52+
std::optional<std::span<uint8_t>> value_to_buffer(JSContext *cx, JS::HandleValue val,
53+
const char *val_desc);
5054

5155
typedef bool InternalMethod(JSContext *cx, JS::HandleObject receiver, JS::HandleValue extra,
5256
JS::CallArgs args);

0 commit comments

Comments
 (0)