Skip to content

Support typed Rust resources for ctx.cache - #8544

Merged
bartlomieju merged 1 commit into
mainfrom
biwanczuk/rust-cache-purge
Nov 24, 2026
Merged

bartlomieju merged 1 commit into
mainfrom
biwanczuk/rust-cache-purge

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

We are migrating the ctx.cache API (CacheContext) from C++ to Rust JSG. The implementation lives in an internal project that embeds workerd and supplies it through Worker::Api::getCtxCacheProperty(), but that hook can only return a C++ jsg::Ref<CacheContext>. This change lets the embedder return a typed Rust JSG resource instead, so the Rust implementation can replace the C++ one without changing what Workers see: ctx.cache keeps its CacheContext TypeScript declaration, and standalone workerd still returns undefined.

  • RustRef<Tag> delegates its RTTI to Tag::JsgRttiDelegate when the tag declares one, so a Rust resource returned from a C++ JSG API keeps the TypeScript shape of an existing C++ type.
  • Worker::Api gains getCtxCacheRustProperty() and isCtxCacheRustInstance(). ctx.cache on both ExecutionContext and the cloudflare:workers entrypoints module resolves through currentCacheContext(), which tries the Rust hook first and then the C++ hook. The C++ hook stays until the embedder finishes migrating.
  • Rust JSG gains Local<Object>::set_prototype(), so a flat Rust resource can link its prototype to a parent in each realm without C++ JSG inheritance. A refused change (a cycle, a non-extensible object, or a Proxy trap that refused or threw) is a TypeError.

@bartlomieju
bartlomieju requested review from a team as code owners November 17, 2026 16:50

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of f180c04f.

This review, of 787ba3fc

💬 Approved with comments — The change looks correct and the hook fallback order is the same in both call sites. The only notes are two INFO items: test coverage of the Rust-backed path, and a doc comment that now sits above the wrong declaration.

Findings: 2 info (2 are inline comments on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 2 info

Two non-blocking notes: no test of the Rust-backed ctx.cache path, and a misplaced RustRef doc comment.

Recommendation: 💬 Optional Nits

  • [INFO] No test exercises the Rust-backed CacheContextRef path: wrap and unwrap of RustCacheContext, Rust hook tried before the C++ hook, and isInstance delegation. (src/workerd/api/cache-context-ref.h:34)
  • [INFO] The RustRef doc comment now sits above RustRefRtti instead of RustRef, and the Tag contract does not mention JsgRttiDelegate. (src/rust/jsg-cpp/jsg-cpp.h:208)
View full review
  • Test coverage: the new tests cover three things: ctx.cache is undefined with no embedder, RTTI delegation, and set_prototype. No test covers the RustCacheContext branch of CacheContextRef::jsgWrap/jsgTryUnwrap, the rule that the Rust hook is tried before the C++ hook, or CacheContextTag::isInstance. The isolate in src/rust/jsg-test already registers RustResourceWrapper and could host such a test.
  • Doc comment placement: RustRefRtti was inserted between the long RustRef<Tag> doc comment and class RustRef, so the comment now reads as documentation for the helper. The Tag contract also does not mention the optional JsgRttiDelegate.

✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 787ba3fc

Comment thread src/workerd/api/cache-context-ref.h Resolved
Comment thread src/rust/jsg-cpp/jsg-cpp.h Resolved

@guybedford guybedford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typed ctx.cache plumbing looks right to me. One real issue in set_prototype's failure handling, plus a few small things inline.

Comment thread src/rust/jsg/v8.rs Resolved
Comment thread src/workerd/api/global-scope.c++ Resolved
Comment thread src/workerd/api/cache-context-ref.h Resolved
Comment thread src/workerd/io/worker.h Resolved

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of a0d6225d.

This review, of f180c04f

✅ Approved — No open findings: the earlier notes are fixed or explained, the set_prototype refusal handling now produces a TypeError, and ctx.cache resolves through a single helper.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The set_prototype shim now tells a refused change from a pending exception. The fallback order lives in currentCacheContext(). The RustRefRtti and Tag contract docs are corrected. RTTI delegation matches the BuildRtti specialization, and the only isolate that uses the global-scope types now registers the needed extension.


⚡ Performance: ✅ LGTM

The ctx.cache lookup adds one virtual call, which returns none when there is no embedder. The set_prototype shim is a single V8 call.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: f180c04f

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 0fb980e8.

This review, of a0d6225d

✅ Approved — No findings: since the last approved review, the only changes are behaviour-preserving clippy and clang-tidy fixes in jsg-cpp.h and v8.rs.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues found. Standalone workerd still resolves ctx.cache to undefined.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The RustRefRtti alias is valid C++23 without typename, and the void_t specialization still forwards JsgRttiDelegate only when the tag declares one. set_prototype taking Option<Self> converts to Local<Value> through the existing cast. Its match arms follow the shim's contract: a successful change returns Ok, a refused change becomes a TypeError, and a pending exception or termination is propagated.


⚡ Performance: ✅ LGTM

Resolving ctx.cache costs one extra virtual call, which returns none when no embedder supplies a Rust resource. set_prototype is a single V8 call.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: a0d6225d

@codecov-commenter

codecov-commenter commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 19.04762% with 68 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.75%. Comparing base (4cd61b1) to head (e86fb61).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/rust/jsg/v8.rs 0.00% 25 Missing ⚠️
src/workerd/api/global-scope.c++ 25.00% 13 Missing and 2 partials ⚠️
src/rust/jsg/ffi.c++ 0.00% 10 Missing ⚠️
src/workerd/api/cache-context-ref.h 0.00% 7 Missing ⚠️
src/workerd/api/api-rtti-test.c++ 57.14% 0 Missing and 6 partials ⚠️
src/workerd/io/worker.c++ 50.00% 3 Missing ⚠️
src/workerd/api/workers-module.c++ 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8544      +/-   ##
==========================================
- Coverage   67.76%   67.75%   -0.02%     
==========================================
  Files         848      849       +1     
  Lines      216079   216156      +77     
  Branches    32540    32553      +13     
==========================================
+ Hits       146426   146448      +22     
- Misses      50441    50487      +46     
- Partials    19212    19221       +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bartlomieju

bartlomieju commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator Author

Waiting on #8617

@bartlomieju
bartlomieju force-pushed the biwanczuk/rust-cache-purge branch from ae7ddac to 0fb980e Compare November 20, 2026 10:25

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of e86fb61c.

This review, of 0fb980e8

✅ Approved — No findings. Since the last approved review, the only changes are a test-module registration and a header include that the new CacheContextRef return type needs.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues. The default Worker::Api hooks return none and false, so standalone workerd still resolves ctx.cache to undefined. Both ctx.cache paths check IoContext::hasCurrent() before they call the embedder hooks.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

mod set_prototype; registers the new set_prototype.rs tests. The new #include "cache-context-ref.h" in global-scope.h is required because ExecutionContext::getCache now returns jsg::Optional<CacheContextRef>, and it matches the definition in global-scope.c++.


⚡ Performance: ✅ LGTM

Resolving ctx.cache adds one virtual call, which returns none when no embedder supplies a Rust resource. set_prototype makes a single V8 call.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 0fb980e8

We are migrating the `ctx.cache` API (`CacheContext`) from C++ to Rust
JSG. The implementation lives in an internal project that embeds workerd
and supplies it through `Worker::Api::getCtxCacheProperty()`, but that
hook can only return a C++ `jsg::Ref<CacheContext>`. This change lets
the embedder return a typed Rust JSG resource instead, so the Rust
implementation can replace the C++ one without changing what Workers
see: `ctx.cache` keeps its `CacheContext` TypeScript declaration, and
standalone workerd still returns `undefined`.

- `RustRef<Tag>` delegates its RTTI to `Tag::JsgRttiDelegate` when the
  tag declares one, so a Rust resource returned from a C++ JSG API keeps
  the TypeScript shape of an existing C++ type.
- `Worker::Api` gains `getCtxCacheRustProperty()` and
  `isCtxCacheRustInstance()`. `ctx.cache` on both `ExecutionContext` and
  the `cloudflare:workers` entrypoints module resolves through
  `currentCacheContext()`, which tries the Rust hook first and then the
  C++ hook. The C++ hook stays until the embedder finishes migrating.
- Rust JSG gains `Local<Object>::set_prototype()`, so a flat Rust
  resource can link its prototype to a parent in each realm without C++
  JSG inheritance. A refused change (a cycle, a non-extensible object,
  or a Proxy trap that refused or threw) is a `TypeError`.
@bartlomieju
bartlomieju force-pushed the biwanczuk/rust-cache-purge branch from 0fb980e to e86fb61 Compare November 24, 2026 09:53

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No findings. Since the last approved review, the only changes are a test-module registration and the includes and wrapper registration that the new CacheContextRef return type needs.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

RustResourceWrapper is registered in JSG_WORKERD_ISOLATE_TYPES, and the jsg-cpp:ffi dependency is added to the api test, io and server targets. The default Worker::Api hooks return none and false, so standalone workerd still resolves ctx.cache to undefined, which global-scope-test.js checks. The set_prototype shim checks exceptionPending both before and after the V8 call.


⚡ Performance: ✅ LGTM

Resolving ctx.cache adds one virtual call to the Rust hook, which returns none in standalone workerd. set_prototype makes a single V8 call.


📝 Documentation: ✅ LGTM

No documentation issues found in the changed files.


Reviewed commit: e86fb61c

@bartlomieju
bartlomieju merged commit 8470626 into main Nov 24, 2026
23 of 25 checks passed
@bartlomieju
bartlomieju deleted the biwanczuk/rust-cache-purge branch November 24, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants