Repository navigation
Support typed Rust resources for ctx.cache #8544
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright (c) 2026 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| //! `Local<Object>::set_prototype`, the counterpart of `v8::Object::SetPrototype`. | ||
|
|
||
| use jsg::ExceptionType; | ||
| use jsg::v8; | ||
|
|
||
| fn eval_object<'s>( | ||
| lock: &mut jsg::Lock<'s>, | ||
| ctx: &crate::EvalContext, | ||
| code: &str, | ||
| ) -> v8::Local<'s, v8::Object> { | ||
| v8::Local::<v8::Object>::try_from(ctx.eval_raw(lock, code).unwrap()).unwrap() | ||
| } | ||
|
|
||
| #[test] | ||
| fn set_prototype_changes_the_prototype() { | ||
| crate::Harness::new().run_in_context(|lock, ctx| { | ||
| let object = lock.new_object(); | ||
| let parent = lock.new_object(); | ||
| object.set_prototype(lock, Some(parent))?; | ||
| ctx.set_global(lock, "object", object); | ||
| ctx.set_global(lock, "parent", parent); | ||
| assert!( | ||
| ctx.eval::<bool>(lock, "Object.getPrototypeOf(object) === parent") | ||
| .unwrap() | ||
| ); | ||
|
|
||
| object.set_prototype(lock, None)?; | ||
| assert!( | ||
| ctx.eval::<bool>(lock, "Object.getPrototypeOf(object) === null") | ||
| .unwrap() | ||
| ); | ||
| Ok(()) | ||
| }); | ||
| } | ||
|
|
||
| // V8 reports each refusal as `Nothing` without an exception; it must reach Rust as a | ||
| // `TypeError`, not as an error that claims an exception is pending. | ||
| fn assert_refused(result: jsg::Result<()>) { | ||
| let err = result.unwrap_err(); | ||
| assert!(!err.is_pending(), "pending: {err}"); | ||
| assert_eq!(err.name, ExceptionType::TypeError); | ||
| } | ||
|
|
||
| #[test] | ||
| fn set_prototype_refuses_a_cycle() { | ||
| crate::Harness::new().run_in_context(|lock, ctx| { | ||
| let a = lock.new_object(); | ||
| let b = lock.new_object(); | ||
| a.set_prototype(lock, Some(b))?; | ||
| assert_refused(b.set_prototype(lock, Some(a))); | ||
| // Nothing was left pending, so JavaScript still runs. | ||
| assert!(ctx.eval::<bool>(lock, "true").unwrap()); | ||
| Ok(()) | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn set_prototype_refuses_a_non_extensible_object() { | ||
| crate::Harness::new().run_in_context(|lock, ctx| { | ||
| let object = eval_object(lock, ctx, "Object.preventExtensions({})"); | ||
| let parent = lock.new_object(); | ||
| assert_refused(object.set_prototype(lock, Some(parent))); | ||
| Ok(()) | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn set_prototype_refuses_when_a_proxy_trap_throws() { | ||
| crate::Harness::new().run_in_context(|lock, ctx| { | ||
| let proxy = eval_object( | ||
| lock, | ||
| ctx, | ||
| "new Proxy({}, { setPrototypeOf() { throw new Error('trap'); } })", | ||
| ); | ||
| let parent = lock.new_object(); | ||
| assert_refused(proxy.set_prototype(lock, Some(parent))); | ||
| assert!(ctx.eval::<bool>(lock, "true").unwrap()); | ||
| Ok(()) | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright (c) 2026 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <workerd/jsg/jsg.h> | ||
| #include <workerd/rust/jsg-cpp/jsg-cpp.h> | ||
|
|
||
| namespace workerd::api { | ||
|
|
||
| class CacheContext; | ||
|
|
||
| // The embedder identifies its Rust implementation of the ctx.cache resource. The C++ type | ||
| // remains the RTTI delegate until the ExecutionContext itself is ported to Rust. | ||
| struct CacheContextTag { | ||
| static constexpr const char* NAME = "CacheContext"; | ||
| using JsgRttiDelegate = jsg::Ref<CacheContext>; | ||
|
|
||
| static kj::Maybe<bool> isInstance(v8::Isolate* isolate, rust::jsg_cpp::Local value); | ||
| }; | ||
|
|
||
| using RustCacheContext = rust::jsg_cpp::RustRef<CacheContextTag>; | ||
|
|
||
| class CacheContextRef { | ||
| public: | ||
| CacheContextRef(jsg::Ref<CacheContext> value); | ||
| CacheContextRef(RustCacheContext value); | ||
| CacheContextRef(CacheContextRef&&); | ||
| ~CacheContextRef() noexcept(false); | ||
|
|
||
| using JsgRttiDelegate = jsg::Ref<CacheContext>; | ||
|
|
||
| static v8::Local<v8::Value> jsgWrap(auto& typeWrapper, | ||
|
bartlomieju marked this conversation as resolved.
Resolved
|
||
| jsg::Lock& js, | ||
| v8::Local<v8::Context> context, | ||
| kj::Maybe<v8::Local<v8::Object>> creator, | ||
| CacheContextRef cache) { | ||
| KJ_SWITCH_ONEOF(cache.value) { | ||
| KJ_CASE_ONEOF(cpp, jsg::Ref<CacheContext>) { | ||
| return typeWrapper.wrap(js, context, creator, kj::mv(cpp)); | ||
| } | ||
| KJ_CASE_ONEOF(rust, RustCacheContext) { | ||
| return typeWrapper.wrap(js, context, creator, kj::mv(rust)); | ||
| } | ||
| } | ||
| KJ_UNREACHABLE; | ||
| } | ||
|
|
||
| // Only to satisfy SelfConvertible: `ctx.cache` is a getter-only property, so nothing unwraps a | ||
| // CacheContextRef. | ||
| static kj::Maybe<CacheContextRef> jsgTryUnwrap(auto& typeWrapper, | ||
|
bartlomieju marked this conversation as resolved.
Resolved
|
||
| jsg::Lock& js, | ||
| v8::Local<v8::Context> context, | ||
| v8::Local<v8::Value> handle, | ||
| kj::Maybe<v8::Local<v8::Object>> parentObject) { | ||
| KJ_IF_SOME(cpp, | ||
| typeWrapper.tryUnwrap( | ||
| js, context, handle, static_cast<jsg::Ref<CacheContext>*>(nullptr), parentObject)) { | ||
| return CacheContextRef(kj::mv(cpp)); | ||
| } | ||
| KJ_IF_SOME(rust, | ||
| typeWrapper.tryUnwrap( | ||
| js, context, handle, static_cast<RustCacheContext*>(nullptr), parentObject)) { | ||
| return CacheContextRef(kj::mv(rust)); | ||
| } | ||
| return kj::none; | ||
| } | ||
|
|
||
| private: | ||
| kj::OneOf<jsg::Ref<CacheContext>, RustCacheContext> value; | ||
| }; | ||
|
|
||
| // The `ctx.cache` of the current request: the embedder's Rust implementation if it supplies one | ||
| // (Worker::Api::getCtxCacheRustProperty()), otherwise its C++ one (getCtxCacheProperty()), or | ||
| // none outside a request or when the embedder supplies neither. | ||
| jsg::Optional<CacheContextRef> currentCacheContext(jsg::Lock& js); | ||
|
|
||
| } // namespace workerd::api | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.