Skip to content

Commit

Permalink
fix: implement the finalizer pattern for the Headers builtin (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Oct 1, 2024
1 parent 3c2d7ff commit 8b909f0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
15 changes: 15 additions & 0 deletions builtins/web/fetch/headers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,21 @@ bool Headers::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
return true;
}

void Headers::finalize(JS::GCContext *gcx, JSObject *self) {
HeadersList *list = static_cast<HeadersList *>(
JS::GetReservedSlot(self, static_cast<size_t>(Headers::Slots::HeadersList)).toPrivate());
if (list != nullptr) {
list->clear();
free(list);
}
HeadersSortList *sort_list = static_cast<HeadersSortList *>(
JS::GetReservedSlot(self, static_cast<size_t>(Slots::HeadersSortList)).toPrivate());
if (sort_list != nullptr) {
sort_list->clear();
free(sort_list);
}
}

bool Headers::init_class(JSContext *cx, JS::HandleObject global) {
// get the host forbidden headers for guard checks
forbidden_request_headers = &host_api::HttpHeaders::get_forbidden_request_headers();
Expand Down
4 changes: 3 additions & 1 deletion builtins/web/fetch/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace builtins {
namespace web {
namespace fetch {

class Headers final : public BuiltinImpl<Headers> {
class Headers final : public FinalizableBuiltinImpl<Headers> {
static bool append(JSContext *cx, unsigned argc, JS::Value *vp);
static bool delete_(JSContext *cx, unsigned argc, JS::Value *vp);
static bool entries(JSContext *cx, unsigned argc, JS::Value *vp);
Expand Down Expand Up @@ -127,6 +127,8 @@ class Headers final : public BuiltinImpl<Headers> {
static JSObject *create(JSContext *cx, HandleValue init_headers, HeadersGuard guard);
static JSObject *create(JSContext *cx, host_api::HttpHeadersReadOnly *handle, HeadersGuard guard);

static void finalize(JS::GCContext *gcx, JSObject *obj);

static bool init_entries(JSContext *cx, HandleObject self, HandleValue init_headers);

/// Returns the headers list of entries, constructing it if necessary.
Expand Down
3 changes: 2 additions & 1 deletion builtins/web/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,8 @@ JSObject *URL::create(JSContext *cx, JS::HandleObject self, JS::HandleValue url_
}

void URL::finalize(JS::GCContext *gcx, JSObject *self) {
jsurl::JSUrl* url = static_cast<jsurl::JSUrl *>(JS::GetReservedSlot(self, Slots::Url).toPrivate());
jsurl::JSUrl *url =
static_cast<jsurl::JSUrl *>(JS::GetReservedSlot(self, Slots::Url).toPrivate());
free(url);
}

Expand Down

0 comments on commit 8b909f0

Please sign in to comment.