Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/HttpResponseWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ struct HttpResponseWrapper {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args);
if (res) {
/* This thing perfectly fits in with unique_function, and will Reset on destructor */
UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[0]));
/* Noexcept move keeps the 16 byte capture in MoveOnlyFunction's inline storage */
NoexceptPersistent<Function> p(isolate, Local<Function>::Cast(args[0]));

res->onData([p = std::move(p), isolate](std::string_view data, bool last) {
HandleScope hs(isolate);

Local<ArrayBuffer> dataArrayBuffer = ArrayBuffer_New(isolate, (void *) data.data(), data.length());

Local<Value> argv[] = {dataArrayBuffer, Boolean::New(isolate, last)};
CallJS(isolate, Local<Function>::New(isolate, p), 2, argv);
CallJS(isolate, Local<Function>::New(isolate, p.p), 2, argv);

dataArrayBuffer->Detach();
});
Expand Down Expand Up @@ -197,8 +197,8 @@ struct HttpResponseWrapper {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args);
if (res) {
/* This thing perfectly fits in with unique_function, and will Reset on destructor */
UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[0]));
/* Noexcept move keeps the 16 byte capture in MoveOnlyFunction's inline storage */
NoexceptPersistent<Function> p(isolate, Local<Function>::Cast(args[0]));

res->onDataV2([p = std::move(p), isolate](std::string_view data, uint64_t maxRemainingBodyLength) {
HandleScope hs(isolate);
Expand All @@ -207,7 +207,7 @@ struct HttpResponseWrapper {

/* Pass maxRemainingBodyLength so user can preallocate; 0 signals the last chunk */
Local<Value> argv[] = {dataArrayBuffer, BigInt::NewFromUnsigned(isolate, maxRemainingBodyLength)};
CallJS(isolate, Local<Function>::New(isolate, p), 2, argv);
CallJS(isolate, Local<Function>::New(isolate, p.p), 2, argv);

dataArrayBuffer->Detach();
});
Expand Down Expand Up @@ -341,16 +341,16 @@ struct HttpResponseWrapper {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args);
if (res) {
/* This thing perfectly fits in with unique_function, and will Reset on destructor */
UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[0]));
/* Noexcept move keeps the 16 byte capture in MoveOnlyFunction's inline storage */
NoexceptPersistent<Function> p(isolate, Local<Function>::Cast(args[0]));

res->onWritable([p = std::move(p), isolate](size_t offset) -> bool {
HandleScope hs(isolate);

Local<Value> argv[] = {Number::New(isolate, offset)};

/* We should check if this is really here! */
MaybeLocal<Value> maybeBoolean = CallJS(isolate, Local<Function>::New(isolate, p), 1, argv);
MaybeLocal<Value> maybeBoolean = CallJS(isolate, Local<Function>::New(isolate, p.p), 1, argv);
if (maybeBoolean.IsEmpty()) {
std::cerr << "Warning: uWS.HttpResponse.onWritable callback should return Boolean. See documentation for uWS.HttpResponse.onWritable and consult the user manual." << std::endl;
/* The default should be true, as it only adds a potential extra send, rather than erroneously avoid it */
Expand Down
11 changes: 11 additions & 0 deletions src/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ static inline bool missingArguments(int length, const FunctionCallbackInfo<Value
return false;
}

/* v8::Global's move constructor is not noexcept, so a lambda capturing one fails
* MoveOnlyFunction's small-object test and heap-allocates. The underlying move is
* a pointer swap and cannot throw, this wrapper only restores the noexcept */
template <class T>
struct NoexceptPersistent {
UniquePersistent<T> p;
NoexceptPersistent(Isolate *isolate, const Local<T> &v) : p(isolate, v) {}
NoexceptPersistent(NoexceptPersistent &&other) noexcept : p(std::move(other.p)) {}
};
static_assert(std::is_nothrow_move_constructible<NoexceptPersistent<Function>>::value, "NoexceptPersistent must be nothrow movable");

struct Callback {
bool invalid = false;
UniquePersistent<Function> f;
Expand Down