Use out parameter for generated methods returning JSVal (#34087)

* Make generated bindings that return a WebIDL `any` value use out parameters.

Returning raw JSVal values makes it easier to create GC hazards in code
that calls these methods. Accepting a MutableHandle argument instead
ensures that the values are rooted by the caller.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Update mozjs.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Fix clippy warnings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2024-11-05 03:29:08 -05:00 committed by GitHub
parent 537958a3cc
commit 25a0764a37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
38 changed files with 763 additions and 515 deletions

View file

@ -33,10 +33,11 @@ use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
use js::conversions::ToJSValConvertible;
use js::jsapi::{GCReason, Heap, JSAutoRealm, JSObject, StackFormat, JSPROP_ENUMERATE, JS_GC};
use js::jsval::{JSVal, NullValue, UndefinedValue};
use js::jsval::{NullValue, UndefinedValue};
use js::rust::wrappers::JS_DefineProperty;
use js::rust::{
CustomAutoRooter, CustomAutoRooterGuard, HandleObject, HandleValue, MutableHandleObject,
MutableHandleValue,
};
use malloc_size_of::MallocSizeOf;
use media::WindowGLContext;
@ -697,22 +698,32 @@ impl WindowMethods for Window {
}
// https://html.spec.whatwg.org/multipage/#dom-opener
fn GetOpener(&self, cx: JSContext, in_realm_proof: InRealm) -> Fallible<JSVal> {
fn GetOpener(
&self,
cx: JSContext,
in_realm_proof: InRealm,
mut retval: MutableHandleValue,
) -> Fallible<()> {
// Step 1, Let current be this Window object's browsing context.
let current = match self.window_proxy.get() {
Some(proxy) => proxy,
// Step 2, If current is null, then return null.
None => return Ok(NullValue()),
None => {
retval.set(NullValue());
return Ok(());
},
};
// Still step 2, since the window's BC is the associated doc's BC,
// see https://html.spec.whatwg.org/multipage/#window-bc
// and a doc's BC is null if it has been discarded.
// see https://html.spec.whatwg.org/multipage/#concept-document-bc
if current.is_browsing_context_discarded() {
return Ok(NullValue());
retval.set(NullValue());
return Ok(());
}
// Step 3 to 5.
Ok(current.opener(*cx, in_realm_proof))
current.opener(*cx, in_realm_proof, retval);
Ok(())
}
#[allow(unsafe_code)]
@ -1388,17 +1399,12 @@ impl WindowMethods for Window {
// https://dom.spec.whatwg.org/#dom-window-event
#[allow(unsafe_code)]
fn Event(&self, cx: JSContext) -> JSVal {
rooted!(in(*cx) let mut rval = UndefinedValue());
fn Event(&self, cx: JSContext, rval: MutableHandleValue) {
if let Some(ref event) = *self.current_event.borrow() {
unsafe {
event
.reflector()
.get_jsobject()
.to_jsval(*cx, rval.handle_mut());
event.reflector().get_jsobject().to_jsval(*cx, rval);
}
}
rval.get()
}
fn IsSecureContext(&self) -> bool {
@ -1571,9 +1577,10 @@ impl WindowMethods for Window {
cx: JSContext,
value: HandleValue,
options: RootedTraceableBox<StructuredSerializeOptions>,
) -> Fallible<js::jsval::JSVal> {
retval: MutableHandleValue,
) -> Fallible<()> {
self.upcast::<GlobalScope>()
.structured_clone(cx, value, options)
.structured_clone(cx, value, options, retval)
}
}