aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom
diff options
context:
space:
mode:
authorSamson <16504129+sagudev@users.noreply.github.com>2024-08-06 19:12:31 +0200
committerGitHub <noreply@github.com>2024-08-06 17:12:31 +0000
commit68f4b359c53b241e0ef82b640e84d8de70cfb805 (patch)
tree42fcb1df4121a8237ad61dc354e338bda52f6c03 /components/script/dom
parent1d464a576a6506196ff10e2c5bbee1969272fc54 (diff)
downloadservo-68f4b359c53b241e0ef82b640e84d8de70cfb805.tar.gz
servo-68f4b359c53b241e0ef82b640e84d8de70cfb805.zip
Add exception to rejection logic in `generic_call` (#32950)
* exception in JS Promise Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> * EXCEPTION_TO_REJECTION on generic_call Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> * PromiseRejectionEvent should handle promise as object Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> * expectations Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --------- Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
Diffstat (limited to 'components/script/dom')
-rw-r--r--components/script/dom/bindings/codegen/CodegenRust.py14
-rw-r--r--components/script/dom/bindings/utils.rs30
-rw-r--r--components/script/dom/promiserejectionevent.rs35
-rw-r--r--components/script/dom/webidls/PromiseRejectionEvent.webidl4
4 files changed, 54 insertions, 29 deletions
diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py
index fa77c516abb..154ec01c7ae 100644
--- a/components/script/dom/bindings/codegen/CodegenRust.py
+++ b/components/script/dom/bindings/codegen/CodegenRust.py
@@ -1870,13 +1870,17 @@ class MethodDefiner(PropertyDefiner):
else:
selfHostedName = "0 as *const libc::c_char"
if m.get("methodInfo", True):
+ if m.get("returnsPromise", False):
+ exceptionToRejection = "true"
+ else:
+ exceptionToRejection = "false"
identifier = m.get("nativeName", m["name"])
# Go through an intermediate type here, because it's not
# easy to tell whether the methodinfo is a JSJitInfo or
# a JSTypedMethodJitInfo here. The compiler knows, though,
# so let it do the work.
jitinfo = "&%s_methodinfo as *const _ as *const JSJitInfo" % identifier
- accessor = "Some(generic_method)"
+ accessor = f"Some(generic_method::<{exceptionToRejection}>)"
else:
if m.get("returnsPromise", False):
jitinfo = "&%s_methodinfo" % m.get("nativeName", m["name"])
@@ -1977,10 +1981,14 @@ class AttrDefiner(PropertyDefiner):
accessor = 'get_' + self.descriptor.internalNameFor(attr.identifier.name)
jitinfo = "0 as *const JSJitInfo"
else:
+ if attr.type.isPromise():
+ exceptionToRejection = "true"
+ else:
+ exceptionToRejection = "false"
if attr.hasLegacyLenientThis():
- accessor = "generic_lenient_getter"
+ accessor = f"generic_lenient_getter::<{exceptionToRejection}>"
else:
- accessor = "generic_getter"
+ accessor = f"generic_getter::<{exceptionToRejection}>"
jitinfo = "&%s_getterinfo" % self.descriptor.internalNameFor(attr.identifier.name)
return ("JSNativeWrapper { op: Some(%(native)s), info: %(info)s }"
diff --git a/components/script/dom/bindings/utils.rs b/components/script/dom/bindings/utils.rs
index 764e604620c..07424ff6d3f 100644
--- a/components/script/dom/bindings/utils.rs
+++ b/components/script/dom/bindings/utils.rs
@@ -466,7 +466,7 @@ pub unsafe fn delete_property_by_id(
JS_DeletePropertyById(cx, object, id, bp)
}
-unsafe fn generic_call(
+unsafe fn generic_call<const EXCEPTION_TO_REJECTION: bool>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
@@ -488,7 +488,11 @@ unsafe fn generic_call(
let thisobj = args.thisv();
if !thisobj.get().is_null_or_undefined() && !thisobj.get().is_object() {
throw_invalid_this(cx, proto_id);
- return false;
+ return if EXCEPTION_TO_REJECTION {
+ exception_to_promise(cx, args.rval())
+ } else {
+ false
+ };
}
rooted!(in(cx) let obj = if thisobj.get().is_object() {
@@ -507,7 +511,11 @@ unsafe fn generic_call(
return true;
} else {
throw_invalid_this(cx, proto_id);
- return false;
+ return if EXCEPTION_TO_REJECTION {
+ exception_to_promise(cx, args.rval())
+ } else {
+ false
+ };
}
},
};
@@ -522,30 +530,30 @@ unsafe fn generic_call(
}
/// Generic method of IDL interface.
-pub unsafe extern "C" fn generic_method(
+pub unsafe extern "C" fn generic_method<const EXCEPTION_TO_REJECTION: bool>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
- generic_call(cx, argc, vp, false, CallJitMethodOp)
+ generic_call::<EXCEPTION_TO_REJECTION>(cx, argc, vp, false, CallJitMethodOp)
}
/// Generic getter of IDL interface.
-pub unsafe extern "C" fn generic_getter(
+pub unsafe extern "C" fn generic_getter<const EXCEPTION_TO_REJECTION: bool>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
- generic_call(cx, argc, vp, false, CallJitGetterOp)
+ generic_call::<EXCEPTION_TO_REJECTION>(cx, argc, vp, false, CallJitGetterOp)
}
/// Generic lenient getter of IDL interface.
-pub unsafe extern "C" fn generic_lenient_getter(
+pub unsafe extern "C" fn generic_lenient_getter<const EXCEPTION_TO_REJECTION: bool>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
- generic_call(cx, argc, vp, true, CallJitGetterOp)
+ generic_call::<EXCEPTION_TO_REJECTION>(cx, argc, vp, true, CallJitGetterOp)
}
unsafe extern "C" fn call_setter(
@@ -569,7 +577,7 @@ pub unsafe extern "C" fn generic_setter(
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
- generic_call(cx, argc, vp, false, call_setter)
+ generic_call::<false>(cx, argc, vp, false, call_setter)
}
/// Generic lenient setter of IDL interface.
@@ -578,7 +586,7 @@ pub unsafe extern "C" fn generic_lenient_setter(
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
- generic_call(cx, argc, vp, true, call_setter)
+ generic_call::<false>(cx, argc, vp, true, call_setter)
}
unsafe extern "C" fn instance_class_has_proto_at_depth(
diff --git a/components/script/dom/promiserejectionevent.rs b/components/script/dom/promiserejectionevent.rs
index 64e548493f2..fe907b010b4 100644
--- a/components/script/dom/promiserejectionevent.rs
+++ b/components/script/dom/promiserejectionevent.rs
@@ -2,10 +2,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+use std::ptr::NonNull;
use std::rc::Rc;
use dom_struct::dom_struct;
-use js::jsapi::Heap;
+use js::jsapi::{Heap, JSObject};
use js::jsval::JSVal;
use js::rust::{HandleObject, HandleValue};
use servo_atoms::Atom;
@@ -27,18 +28,18 @@ use crate::script_runtime::JSContext;
#[dom_struct]
pub struct PromiseRejectionEvent {
event: Event,
- #[ignore_malloc_size_of = "Rc"]
- promise: Rc<Promise>,
- #[ignore_malloc_size_of = "Defined in rust-mozjs"]
+ #[ignore_malloc_size_of = "Defined in mozjs"]
+ promise: Heap<*mut JSObject>,
+ #[ignore_malloc_size_of = "Defined in mozjs"]
reason: Heap<JSVal>,
}
impl PromiseRejectionEvent {
#[allow(crown::unrooted_must_root)]
- fn new_inherited(promise: Rc<Promise>) -> Self {
+ fn new_inherited() -> Self {
PromiseRejectionEvent {
event: Event::new_inherited(),
- promise,
+ promise: Heap::default(),
reason: Heap::default(),
}
}
@@ -51,7 +52,15 @@ impl PromiseRejectionEvent {
promise: Rc<Promise>,
reason: HandleValue,
) -> DomRoot<Self> {
- Self::new_with_proto(global, None, type_, bubbles, cancelable, promise, reason)
+ Self::new_with_proto(
+ global,
+ None,
+ type_,
+ bubbles,
+ cancelable,
+ promise.promise_obj(),
+ reason,
+ )
}
#[allow(crown::unrooted_must_root)]
@@ -61,14 +70,15 @@ impl PromiseRejectionEvent {
type_: Atom,
bubbles: EventBubbles,
cancelable: EventCancelable,
- promise: Rc<Promise>,
+ promise: HandleObject,
reason: HandleValue,
) -> DomRoot<Self> {
let ev = reflect_dom_object_with_proto(
- Box::new(PromiseRejectionEvent::new_inherited(promise)),
+ Box::new(PromiseRejectionEvent::new_inherited()),
global,
proto,
);
+ ev.promise.set(promise.get());
{
let event = ev.upcast::<Event>();
@@ -87,7 +97,6 @@ impl PromiseRejectionEvent {
init: RootedTraceableBox<PromiseRejectionEventBinding::PromiseRejectionEventInit>,
) -> Fallible<DomRoot<Self>> {
let reason = init.reason.handle();
- let promise = init.promise.clone();
let bubbles = EventBubbles::from(init.parent.bubbles);
let cancelable = EventCancelable::from(init.parent.cancelable);
@@ -97,7 +106,7 @@ impl PromiseRejectionEvent {
Atom::from(type_),
bubbles,
cancelable,
- promise,
+ init.promise.handle(),
reason,
);
Ok(event)
@@ -106,8 +115,8 @@ impl PromiseRejectionEvent {
impl PromiseRejectionEventMethods for PromiseRejectionEvent {
// https://html.spec.whatwg.org/multipage/#dom-promiserejectionevent-promise
- fn Promise(&self) -> Rc<Promise> {
- self.promise.clone()
+ fn Promise(&self, _cx: JSContext) -> NonNull<JSObject> {
+ NonNull::new(self.promise.get()).unwrap()
}
// https://html.spec.whatwg.org/multipage/#dom-promiserejectionevent-reason
diff --git a/components/script/dom/webidls/PromiseRejectionEvent.webidl b/components/script/dom/webidls/PromiseRejectionEvent.webidl
index 70d11b1ff33..46ecbc21718 100644
--- a/components/script/dom/webidls/PromiseRejectionEvent.webidl
+++ b/components/script/dom/webidls/PromiseRejectionEvent.webidl
@@ -7,11 +7,11 @@
[Exposed=(Window,Worker)]
interface PromiseRejectionEvent : Event {
[Throws] constructor(DOMString type, PromiseRejectionEventInit eventInitDict);
- readonly attribute Promise<any> promise;
+ readonly attribute object promise;
readonly attribute any reason;
};
dictionary PromiseRejectionEventInit : EventInit {
- required Promise<any> promise;
+ required object promise;
any reason;
};