Skip to content

script: Pass &mut JSContext to method of Crypto interface#42809

Merged
TimvdLippe merged 1 commit intoservo:mainfrom
kkoyung:new-cx-crypto
Feb 24, 2026
Merged

script: Pass &mut JSContext to method of Crypto interface#42809
TimvdLippe merged 1 commit intoservo:mainfrom
kkoyung:new-cx-crypto

Conversation

@kkoyung
Copy link
Copy Markdown
Member

@kkoyung kkoyung commented Feb 24, 2026

This patch changes the method of Crypto interface to use the new &mut JSContext.

The method crypto.subtle calls SubtleCrypto::new to create an instance of SubtleCrypto and the new &mut JSContext is passed to SubtleCrypto::new. Therefore, the new reflect_dom_object_with_cx method is also used in SubtleCrypto::new.

Testing: Refactoring. Existing tests suffice.
Fixes: Part of #42638

This patch changes the method of `Crypto` interface to use the new `&mut
JSContext`.

The method `crypto.subtle` calls `SubtleCrypto::new` to create an
instance of `SubtleCrypto` and the new `&mut JSContext` is passed to
`SubtleCrypto::new`. Therefore, the new `reflect_dom_object_with_cx`
method is also used in `SubtleCrypto::new`.

Signed-off-by: Kingsley Yung <kingsley@kkoyung.dev>
@kkoyung kkoyung requested a review from gterzian as a code owner February 24, 2026 09:49
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Feb 24, 2026
impl CryptoMethods<crate::DomTypeHolder> for Crypto {
/// <https://w3c.github.io/webcrypto/#dfn-Crypto-attribute-subtle>
fn Subtle(&self, can_gc: CanGc) -> DomRoot<SubtleCrypto> {
fn Subtle(&self, cx: &mut js::context::JSContext) -> DomRoot<SubtleCrypto> {
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.

I would import js::context::JSContext at the top of the file:

Suggested change
fn Subtle(&self, cx: &mut js::context::JSContext) -> DomRoot<SubtleCrypto> {
fn Subtle(&self, cx: &mut JSContext) -> DomRoot<SubtleCrypto> {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fn GetRandomValues(
&self,
_cx: JSContext,
mut input: CustomAutoRooterGuard<ArrayBufferView>,
) -> Fallible<RootedTraceableBox<HeapArrayBufferView>> {

The function signature of GetRandomValues generated by script_binding still contain the JSContext from crate::script_runtime (even through it is not actually used).

Do we have conventional alias for them if both are imported?

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.

I don't know of any convention, but if you are avoiding a name collision, then using the qualified name is probably fine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Qualified name is used here mainly for avoiding name collision. We may need to clean it up once the crate::script_runtime::JSContext is fully replaced by js::context::JSContext.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

FYI in all other PRs we currently use the fully qualified written out name (275 usages in 74 files and counting). Therefore, for consistency let's keep using that for now. Later, we can also clean it up?

pub(crate) fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<SubtleCrypto> {
reflect_dom_object(Box::new(SubtleCrypto::new_inherited()), global, can_gc)
pub(crate) fn new(
cx: &mut js::context::JSContext,
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.

Ditto.

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Feb 24, 2026
@TimvdLippe TimvdLippe added this pull request to the merge queue Feb 24, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Feb 24, 2026
Merged via the queue into servo:main with commit 5b3684e Feb 24, 2026
33 checks passed
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Feb 24, 2026
@kkoyung kkoyung deleted the new-cx-crypto branch March 7, 2026 03:10
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.

4 participants