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

feat: support generation integer on headers objects #193

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion builtins/web/fetch/headers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,14 @@ bool prepare_for_entries_modification(JSContext *cx, JS::HandleObject self) {
SetReservedSlot(self, static_cast<size_t>(Headers::Slots::Handle), PrivateValue(new_handle));
}
} else if (mode == Headers::Mode::CachedInContent || mode == Headers::Mode::Uninitialized) {
return switch_mode(cx, self, Headers::Mode::ContentOnly);
if (!switch_mode(cx, self, Headers::Mode::ContentOnly)) {
return false;
}
}
// bump the generation integer
uint32_t gen = JS::GetReservedSlot(self, static_cast<uint32_t>(Headers::Slots::Gen)).toInt32();
if (gen != INT32_MAX) {
JS::SetReservedSlot(self, static_cast<uint32_t>(Headers::Slots::Gen), JS::Int32Value(gen + 1));
}
return true;
}
Expand Down Expand Up @@ -603,6 +610,7 @@ JSObject *Headers::create(JSContext *cx, HeadersGuard guard) {

SetReservedSlot(self, static_cast<uint32_t>(Slots::HeadersList), PrivateValue(nullptr));
SetReservedSlot(self, static_cast<uint32_t>(Slots::HeadersSortList), PrivateValue(nullptr));
SetReservedSlot(self, static_cast<uint32_t>(Slots::Gen), JS::Int32Value(0));
return self;
}

Expand Down Expand Up @@ -651,6 +659,10 @@ bool Headers::init_entries(JSContext *cx, HandleObject self, HandleValue initv)
return true;
}

uint32_t Headers::get_generation(JSObject *self) {
return JS::GetReservedSlot(self, static_cast<uint32_t>(Headers::Slots::Gen)).toInt32();
}

bool Headers::get(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(1)

Expand Down Expand Up @@ -1013,6 +1025,7 @@ bool Headers::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
JS::Int32Value(static_cast<int32_t>(HeadersGuard::None)));
SetReservedSlot(self, static_cast<uint32_t>(Slots::HeadersList), PrivateValue(nullptr));
SetReservedSlot(self, static_cast<uint32_t>(Slots::HeadersSortList), PrivateValue(nullptr));
SetReservedSlot(self, static_cast<uint32_t>(Slots::Gen), JS::Int32Value(0));

// walk the headers list writing in the ordered normalized case headers (distinct from the wire)
if (!init_entries(cx, self, headersInit)) {
Expand Down
7 changes: 7 additions & 0 deletions builtins/web/fetch/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Headers final : public FinalizableBuiltinImpl<Headers> {
HeadersSortList,
Mode,
Guard,
Gen,
Count,
};

Expand Down Expand Up @@ -136,6 +137,12 @@ class Headers final : public FinalizableBuiltinImpl<Headers> {
/// the headers.
static HeadersList *get_list(JSContext *cx, HandleObject self);

/**
* Get the generation integer associated with header mutations.
* Useful for quickly determining if a headers object may have changed since last seen.
*/
static uint32_t get_generation(JSObject *self);

/**
* Returns a cloned handle representing the contents of this Headers object.
*
Expand Down
Loading