Skip to content

Use safe rooted handles in stream callbacks - #48226

Merged
Taym95 merged 2 commits into
servo:mainfrom
Taym95:safe-rooted-stream-callbacks
Sep 20, 2026
Merged

Taym95 merged 2 commits into
servo:mainfrom
Taym95:safe-rooted-stream-callbacks

Conversation

@Taym95

@Taym95 Taym95 commented Sep 20, 2026 •

Copy link
Copy Markdown
Member

replace SafeHandle::from_raw calls with rooted_heap_handle in UnderlyingSource callbacks and add Promise::is_promise_value to centralize the object check.

Testing: No behavior change, existing WPT tests passed locally.

Fixes: Part of #30862

@Taym95
Taym95 requested a review from gterzian as a code owner September 20, 2026 15:09
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Sep 20, 2026
@Taym95
Taym95 force-pushed the safe-rooted-stream-callbacks branch from dc542cd to 26a4ebe Compare September 20, 2026 15:46
Comment on lines +216 to +218
/// Check whether a value is a promise.
#[expect(unsafe_code)]
pub(crate) fn is_promise_value(value: HandleValue, mut object: MutableHandleObject) -> bool {

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 think we should be more descriptive here, as we do not just do the check here. Actually given that every user does something like:

let promise = if is_promise {
                Promise::new_with_js_promise(cx, result_object.handle())
            } else {
                Promise::new_resolved(cx, global, result.get())
            };

maybe we should be also include this here so we will always return promise:

/// returns rust promise if value is promise object
/// otherwise creates new resolved rust promise with value
fn resolve_or_wrap_promise(cx: &mut JSContext, value: HandleValue, global) -> Promise {
 rooted!(&in(cx) let mut result_object = ptr::null_mut::<JSObject>());
let is_promise = unsafe {
                if value.is_object() {
                    result_object.set(value.to_object());
                    IsPromiseObject(result_object.handle().into_handle())
                } else {
                    false
                }
            };
            let promise = if is_promise {
                Promise::new_with_js_promise(cx, result_object.handle())
            } else {
                Promise::new_resolved(cx, global, value.get())
            };
}

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.

this is better indeed, done in 084923c

}
if let Err(error) = start.Call_(
cx,
&rooted_heap_handle(self, |_| this_obj),

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.

NOTE-TO-SELF: while we do project rootedness here, we go from self->underlying_source_type->this_obj

@Taym95
Taym95 added this pull request to the merge queue Sep 20, 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 Sep 20, 2026
Merged via the queue into servo:main with commit 3596f53 Sep 20, 2026
33 checks passed
@Taym95
Taym95 deleted the safe-rooted-stream-callbacks branch September 20, 2026 18:50
@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 Sep 20, 2026
RichardTjokroutomo pushed a commit to RichardTjokroutomo/servo that referenced this pull request Oct 6, 2026
replace `SafeHandle::from_raw` calls with `rooted_heap_handle` in
UnderlyingSource callbacks and add `Promise::is_promise_value` to
centralize the object check.

Testing: No behavior change, existing WPT tests passed locally.

Fixes: Part of [servo#30862](servo#30862)

---------

Signed-off-by: Taym Haddadi <[email protected]>
hhhwsqhhh pushed a commit to hhhwsqhhh/servo that referenced this pull request Oct 12, 2026
replace `SafeHandle::from_raw` calls with `rooted_heap_handle` in
UnderlyingSource callbacks and add `Promise::is_promise_value` to
centralize the object check.

Testing: No behavior change, existing WPT tests passed locally.

Fixes: Part of [servo#30862](servo#30862)

---------

Signed-off-by: Taym Haddadi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-review There is new code that needs to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants