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
2 changes: 2 additions & 0 deletions src/workerd/api/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Headers final: public jsg::Object {
struct IteratorState {
kj::Array<T> copy;
decltype(copy.begin()) cursor = copy.begin();

JSG_HOLDS_NO_GC_HANDLES;
};

public:
Expand Down
9 changes: 9 additions & 0 deletions src/workerd/api/sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,12 @@ struct SqlStorage::IngestResult {
// The list of sql.h types that are added to worker.c++'s JSG_DECLARE_ISOLATE_TYPE

} // namespace workerd::api

namespace workerd::jsg {

// An exception: a Ref is a GC handle. RowIterator and RawIterator hold their Cursor as a strong Ref
// that is never visited, so the iterator keeps the Cursor alive until the iterator is destroyed.
template <>
inline constexpr bool holdsNoGcHandles<Ref<api::SqlStorage::Cursor>> = true;

} // namespace workerd::jsg
8 changes: 8 additions & 0 deletions src/workerd/api/sync-kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,11 @@ class SyncKvStorage: public jsg::Object {
api::SyncKvStorage::ListIterator, api::SyncKvStorage::ListIterator::Next

} // namespace workerd::api

namespace workerd::jsg {

// The cursor is a KJ object; it holds no JS handles.
template <>
inline constexpr bool holdsNoGcHandles<IoOwn<SqliteKv::ListCursor>> = true;

} // namespace workerd::jsg
4 changes: 4 additions & 0 deletions src/workerd/jsg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ This is intentionally weak and does NOT keep its target alive during GC.
Attempting to `visitor.visit()` a weak ref field is a compile error — the correct
signal that weak references should not be traced. Do not include them in `visitForGc()`.

**Iterator State.** The `State` of `JSG_ITERATOR` and `JSG_ASYNC_ITERATOR` must have a
public `visitForGc()`, or declare `JSG_HOLDS_NO_GC_HANDLES` if it holds none; otherwise it
fails to compile. For a type you can't add a member to, specialize `jsg::holdsNoGcHandles<T>`.

**Visit only what this object owns alone.** An object behind a `kj::Rc`, `kj::Arc`, or a
`kj::Own` of a refcounted type can have other owners that visit it too, and a handle visited
from two owners is strong or traced depending on which visited last. The `jsg-shared-visit`
Expand Down
16 changes: 14 additions & 2 deletions src/workerd/jsg/iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,10 @@ class IteratorBase: public Object {
void visitForGc(GcVisitor& visitor) {
if constexpr (hasPublicVisitForGc<State>()) {
visitor.visit(state);
} else {
static_assert(holdsNoGcHandles<State>,
"A JSG_ITERATOR State must have a public visitForGc(), or be marked "
"JSG_HOLDS_NO_GC_HANDLES if it holds no GC handles.");
}
}

Expand Down Expand Up @@ -865,6 +869,10 @@ class AsyncIteratorBase: public Object {
KJ_IF_SOME(inner, state.template tryGet<InnerState>()) {
if constexpr (hasPublicVisitForGc<State>()) {
visitor.visit(inner.state);
} else {
static_assert(holdsNoGcHandles<State>,
"A JSG_ASYNC_ITERATOR State must have a public visitForGc(), or be marked "
"JSG_HOLDS_NO_GC_HANDLES if it holds no GC handles.");
}
visitor.visit(inner.impl);
}
Expand Down Expand Up @@ -1025,7 +1033,9 @@ class AsyncIteratorBase: public Object {
// private:
// struct IteratorState {
// // The iterator's internal state.
// // Implement visitForGc here if the state stores any visitable references.
// // Implement a public visitForGc() here if the state stores any visitable references,
// // otherwise declare:
// JSG_HOLDS_NO_GC_HANDLES;
// };
//
// static kj::Maybe<kj::String> nextFunction(jsg::Lock& js, IteratorState& state) {
Expand Down Expand Up @@ -1119,7 +1129,9 @@ class AsyncIteratorBase: public Object {
// private:
// struct IteratorState {
// // The iterator's internal state.
// // Implement visitForGc here if the state stores any visitable references.
// // Implement a public visitForGc() here if the state stores any visitable references,
// // otherwise declare:
// JSG_HOLDS_NO_GC_HANDLES;
// };
//
// static jsg::Promise<kj::Maybe<kj::String>> nextFunction(
Expand Down
11 changes: 11 additions & 0 deletions src/workerd/jsg/jsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,17 @@ consteval bool hasPublicVisitForGc() {
return hasPublicVisitForGc_(static_cast<T*>(nullptr));
}

// Opt-out for a type with no public `visitForGc()`, required by templates that would otherwise skip
// it silently (such as the State of JSG_ITERATOR). Mark a struct by putting JSG_HOLDS_NO_GC_HANDLES
// in its body. For a type you can't add a member to, specialize the variable in workerd::jsg:
//
// template <>
// inline constexpr bool holdsNoGcHandles<IoOwn<Foo>> = true;
template <typename T>
inline constexpr bool holdsNoGcHandles = requires { typename T::JsgHoldsNoGcHandles; };

#define JSG_HOLDS_NO_GC_HANDLES using JsgHoldsNoGcHandles = void

// Visitor used during garbage collection. Any resource class that holds `Ref`s should
// implement GC visitation by declaring a private method like:
//
Expand Down
1 change: 1 addition & 0 deletions src/workerd/jsg/memory-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct PendingIterable: public Object {
struct SyncIterable: public Object {
struct SyncIteratorState {
kj::String name = kj::str("state");
JSG_HOLDS_NO_GC_HANDLES;
JSG_MEMORY_INFO(SyncIteratorState) {
tracker.trackField("name", name);
}
Expand Down
Loading