Skip to content

Commit 0989121

Browse files
sagudevGae24
authored andcommitted
Add exception to rejection logic in generic_call (servo#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>
1 parent 08d4ad5 commit 0989121

16 files changed

+61
-75
lines changed

components/script/dom/bindings/codegen/CodegenRust.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,13 +1870,17 @@ def specData(m):
18701870
else:
18711871
selfHostedName = "0 as *const libc::c_char"
18721872
if m.get("methodInfo", True):
1873+
if m.get("returnsPromise", False):
1874+
exceptionToRejection = "true"
1875+
else:
1876+
exceptionToRejection = "false"
18731877
identifier = m.get("nativeName", m["name"])
18741878
# Go through an intermediate type here, because it's not
18751879
# easy to tell whether the methodinfo is a JSJitInfo or
18761880
# a JSTypedMethodJitInfo here. The compiler knows, though,
18771881
# so let it do the work.
18781882
jitinfo = "&%s_methodinfo as *const _ as *const JSJitInfo" % identifier
1879-
accessor = "Some(generic_method)"
1883+
accessor = f"Some(generic_method::<{exceptionToRejection}>)"
18801884
else:
18811885
if m.get("returnsPromise", False):
18821886
jitinfo = "&%s_methodinfo" % m.get("nativeName", m["name"])
@@ -1977,10 +1981,14 @@ def getter(attr):
19771981
accessor = 'get_' + self.descriptor.internalNameFor(attr.identifier.name)
19781982
jitinfo = "0 as *const JSJitInfo"
19791983
else:
1984+
if attr.type.isPromise():
1985+
exceptionToRejection = "true"
1986+
else:
1987+
exceptionToRejection = "false"
19801988
if attr.hasLegacyLenientThis():
1981-
accessor = "generic_lenient_getter"
1989+
accessor = f"generic_lenient_getter::<{exceptionToRejection}>"
19821990
else:
1983-
accessor = "generic_getter"
1991+
accessor = f"generic_getter::<{exceptionToRejection}>"
19841992
jitinfo = "&%s_getterinfo" % self.descriptor.internalNameFor(attr.identifier.name)
19851993

19861994
return ("JSNativeWrapper { op: Some(%(native)s), info: %(info)s }"

components/script/dom/bindings/utils.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ pub unsafe fn delete_property_by_id(
466466
JS_DeletePropertyById(cx, object, id, bp)
467467
}
468468

469-
unsafe fn generic_call(
469+
unsafe fn generic_call<const EXCEPTION_TO_REJECTION: bool>(
470470
cx: *mut JSContext,
471471
argc: libc::c_uint,
472472
vp: *mut JSVal,
@@ -488,7 +488,11 @@ unsafe fn generic_call(
488488
let thisobj = args.thisv();
489489
if !thisobj.get().is_null_or_undefined() && !thisobj.get().is_object() {
490490
throw_invalid_this(cx, proto_id);
491-
return false;
491+
return if EXCEPTION_TO_REJECTION {
492+
exception_to_promise(cx, args.rval())
493+
} else {
494+
false
495+
};
492496
}
493497

494498
rooted!(in(cx) let obj = if thisobj.get().is_object() {
@@ -507,7 +511,11 @@ unsafe fn generic_call(
507511
return true;
508512
} else {
509513
throw_invalid_this(cx, proto_id);
510-
return false;
514+
return if EXCEPTION_TO_REJECTION {
515+
exception_to_promise(cx, args.rval())
516+
} else {
517+
false
518+
};
511519
}
512520
},
513521
};
@@ -522,30 +530,30 @@ unsafe fn generic_call(
522530
}
523531

524532
/// Generic method of IDL interface.
525-
pub unsafe extern "C" fn generic_method(
533+
pub unsafe extern "C" fn generic_method<const EXCEPTION_TO_REJECTION: bool>(
526534
cx: *mut JSContext,
527535
argc: libc::c_uint,
528536
vp: *mut JSVal,
529537
) -> bool {
530-
generic_call(cx, argc, vp, false, CallJitMethodOp)
538+
generic_call::<EXCEPTION_TO_REJECTION>(cx, argc, vp, false, CallJitMethodOp)
531539
}
532540

533541
/// Generic getter of IDL interface.
534-
pub unsafe extern "C" fn generic_getter(
542+
pub unsafe extern "C" fn generic_getter<const EXCEPTION_TO_REJECTION: bool>(
535543
cx: *mut JSContext,
536544
argc: libc::c_uint,
537545
vp: *mut JSVal,
538546
) -> bool {
539-
generic_call(cx, argc, vp, false, CallJitGetterOp)
547+
generic_call::<EXCEPTION_TO_REJECTION>(cx, argc, vp, false, CallJitGetterOp)
540548
}
541549

542550
/// Generic lenient getter of IDL interface.
543-
pub unsafe extern "C" fn generic_lenient_getter(
551+
pub unsafe extern "C" fn generic_lenient_getter<const EXCEPTION_TO_REJECTION: bool>(
544552
cx: *mut JSContext,
545553
argc: libc::c_uint,
546554
vp: *mut JSVal,
547555
) -> bool {
548-
generic_call(cx, argc, vp, true, CallJitGetterOp)
556+
generic_call::<EXCEPTION_TO_REJECTION>(cx, argc, vp, true, CallJitGetterOp)
549557
}
550558

551559
unsafe extern "C" fn call_setter(
@@ -569,7 +577,7 @@ pub unsafe extern "C" fn generic_setter(
569577
argc: libc::c_uint,
570578
vp: *mut JSVal,
571579
) -> bool {
572-
generic_call(cx, argc, vp, false, call_setter)
580+
generic_call::<false>(cx, argc, vp, false, call_setter)
573581
}
574582

575583
/// Generic lenient setter of IDL interface.
@@ -578,7 +586,7 @@ pub unsafe extern "C" fn generic_lenient_setter(
578586
argc: libc::c_uint,
579587
vp: *mut JSVal,
580588
) -> bool {
581-
generic_call(cx, argc, vp, true, call_setter)
589+
generic_call::<false>(cx, argc, vp, true, call_setter)
582590
}
583591

584592
unsafe extern "C" fn instance_class_has_proto_at_depth(

components/script/dom/promiserejectionevent.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44

5+
use std::ptr::NonNull;
56
use std::rc::Rc;
67

78
use dom_struct::dom_struct;
8-
use js::jsapi::Heap;
9+
use js::jsapi::{Heap, JSObject};
910
use js::jsval::JSVal;
1011
use js::rust::{HandleObject, HandleValue};
1112
use servo_atoms::Atom;
@@ -27,18 +28,18 @@ use crate::script_runtime::JSContext;
2728
#[dom_struct]
2829
pub struct PromiseRejectionEvent {
2930
event: Event,
30-
#[ignore_malloc_size_of = "Rc"]
31-
promise: Rc<Promise>,
32-
#[ignore_malloc_size_of = "Defined in rust-mozjs"]
31+
#[ignore_malloc_size_of = "Defined in mozjs"]
32+
promise: Heap<*mut JSObject>,
33+
#[ignore_malloc_size_of = "Defined in mozjs"]
3334
reason: Heap<JSVal>,
3435
}
3536

3637
impl PromiseRejectionEvent {
3738
#[allow(crown::unrooted_must_root)]
38-
fn new_inherited(promise: Rc<Promise>) -> Self {
39+
fn new_inherited() -> Self {
3940
PromiseRejectionEvent {
4041
event: Event::new_inherited(),
41-
promise,
42+
promise: Heap::default(),
4243
reason: Heap::default(),
4344
}
4445
}
@@ -51,7 +52,15 @@ impl PromiseRejectionEvent {
5152
promise: Rc<Promise>,
5253
reason: HandleValue,
5354
) -> DomRoot<Self> {
54-
Self::new_with_proto(global, None, type_, bubbles, cancelable, promise, reason)
55+
Self::new_with_proto(
56+
global,
57+
None,
58+
type_,
59+
bubbles,
60+
cancelable,
61+
promise.promise_obj(),
62+
reason,
63+
)
5564
}
5665

5766
#[allow(crown::unrooted_must_root)]
@@ -61,14 +70,15 @@ impl PromiseRejectionEvent {
6170
type_: Atom,
6271
bubbles: EventBubbles,
6372
cancelable: EventCancelable,
64-
promise: Rc<Promise>,
73+
promise: HandleObject,
6574
reason: HandleValue,
6675
) -> DomRoot<Self> {
6776
let ev = reflect_dom_object_with_proto(
68-
Box::new(PromiseRejectionEvent::new_inherited(promise)),
77+
Box::new(PromiseRejectionEvent::new_inherited()),
6978
global,
7079
proto,
7180
);
81+
ev.promise.set(promise.get());
7282

7383
{
7484
let event = ev.upcast::<Event>();
@@ -87,7 +97,6 @@ impl PromiseRejectionEvent {
8797
init: RootedTraceableBox<PromiseRejectionEventBinding::PromiseRejectionEventInit>,
8898
) -> Fallible<DomRoot<Self>> {
8999
let reason = init.reason.handle();
90-
let promise = init.promise.clone();
91100
let bubbles = EventBubbles::from(init.parent.bubbles);
92101
let cancelable = EventCancelable::from(init.parent.cancelable);
93102

@@ -97,7 +106,7 @@ impl PromiseRejectionEvent {
97106
Atom::from(type_),
98107
bubbles,
99108
cancelable,
100-
promise,
109+
init.promise.handle(),
101110
reason,
102111
);
103112
Ok(event)
@@ -106,8 +115,8 @@ impl PromiseRejectionEvent {
106115

107116
impl PromiseRejectionEventMethods for PromiseRejectionEvent {
108117
// https://html.spec.whatwg.org/multipage/#dom-promiserejectionevent-promise
109-
fn Promise(&self) -> Rc<Promise> {
110-
self.promise.clone()
118+
fn Promise(&self, _cx: JSContext) -> NonNull<JSObject> {
119+
NonNull::new(self.promise.get()).unwrap()
111120
}
112121

113122
// https://html.spec.whatwg.org/multipage/#dom-promiserejectionevent-reason

components/script/dom/webidls/PromiseRejectionEvent.webidl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
[Exposed=(Window,Worker)]
88
interface PromiseRejectionEvent : Event {
99
[Throws] constructor(DOMString type, PromiseRejectionEventInit eventInitDict);
10-
readonly attribute Promise<any> promise;
10+
readonly attribute object promise;
1111
readonly attribute any reason;
1212
};
1313

1414
dictionary PromiseRejectionEventInit : EventInit {
15-
required Promise<any> promise;
15+
required object promise;
1616
any reason;
1717
};

tests/wpt/meta/FileAPI/idlharness.any.js.ini

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
[idlharness.any.html]
22
[Blob interface: operation text()]
3-
expected: FAIL
43

54
[Blob interface: operation arrayBuffer()]
6-
expected: FAIL
75

86
[FileReader interface: operation readAsBinaryString(Blob)]
97
expected: FAIL
@@ -26,10 +24,8 @@
2624

2725
[idlharness.any.worker.html]
2826
[Blob interface: operation text()]
29-
expected: FAIL
3027

3128
[Blob interface: operation arrayBuffer()]
32-
expected: FAIL
3329

3430
[FileReader interface: operation readAsBinaryString(Blob)]
3531
expected: FAIL

tests/wpt/meta/FileAPI/idlharness.html.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
[idlharness.html]
22
[Blob interface: operation text()]
3-
expected: FAIL
43

54
[Blob interface: operation arrayBuffer()]
6-
expected: FAIL
75

86
[FileReader interface: operation readAsBinaryString(Blob)]
97
expected: FAIL

tests/wpt/meta/FileAPI/idlharness.worker.js.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
[idlharness.worker.html]
22
[Blob interface: operation text()]
3-
expected: FAIL
43

54
[Blob interface: operation arrayBuffer()]
6-
expected: FAIL
75

86
[FileReader interface: operation readAsBinaryString(Blob)]
97
expected: FAIL

tests/wpt/meta/dom/idlharness.window.js.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,6 @@
686686
expected: FAIL
687687
688688
[Document interface: operation exitFullscreen()]
689-
expected: FAIL
690689
691690
[Document interface: attribute fullscreenElement]
692691
expected: FAIL
@@ -695,7 +694,6 @@
695694
expected: FAIL
696695
697696
[Element interface: operation requestFullscreen(optional FullscreenOptions)]
698-
expected: FAIL
699697
700698
[Element interface: attribute onfullscreenchange]
701699
expected: FAIL

tests/wpt/meta/fetch/api/idlharness.any.js.ini

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@
1515
expected: FAIL
1616

1717
[Request interface: operation arrayBuffer()]
18-
expected: FAIL
1918

2019
[Request interface: operation blob()]
21-
expected: FAIL
2220

2321
[Request interface: operation formData()]
24-
expected: FAIL
2522

2623
[Request interface: operation json()]
27-
expected: FAIL
2824

2925
[Request interface: operation text()]
30-
expected: FAIL
3126

3227
[Request interface: new Request('about:blank') must inherit property "keepalive" with the proper type]
3328
expected: FAIL
@@ -48,25 +43,19 @@
4843
expected: FAIL
4944

5045
[Response interface: operation arrayBuffer()]
51-
expected: FAIL
5246

5347
[Response interface: operation blob()]
54-
expected: FAIL
5548

5649
[Response interface: operation formData()]
57-
expected: FAIL
5850

5951
[Response interface: operation json()]
60-
expected: FAIL
6152

6253
[Response interface: operation text()]
63-
expected: FAIL
6454

6555
[Response interface: calling json(any, optional ResponseInit) on new Response() with too few arguments must throw TypeError]
6656
expected: FAIL
6757

6858
[WorkerGlobalScope interface: operation fetch(RequestInfo, optional RequestInit)]
69-
expected: FAIL
7059

7160
[WorkerGlobalScope interface: calling fetch(RequestInfo, optional RequestInit) on self with too few arguments must throw TypeError]
7261

@@ -103,19 +92,14 @@
10392
expected: FAIL
10493

10594
[Request interface: operation arrayBuffer()]
106-
expected: FAIL
10795

10896
[Request interface: operation blob()]
109-
expected: FAIL
11097

11198
[Request interface: operation formData()]
112-
expected: FAIL
11399

114100
[Request interface: operation json()]
115-
expected: FAIL
116101

117102
[Request interface: operation text()]
118-
expected: FAIL
119103

120104
[Request interface: new Request('about:blank') must inherit property "keepalive" with the proper type]
121105
expected: FAIL
@@ -136,25 +120,19 @@
136120
expected: FAIL
137121

138122
[Response interface: operation arrayBuffer()]
139-
expected: FAIL
140123

141124
[Response interface: operation blob()]
142-
expected: FAIL
143125

144126
[Response interface: operation formData()]
145-
expected: FAIL
146127

147128
[Response interface: operation json()]
148-
expected: FAIL
149129

150130
[Response interface: operation text()]
151-
expected: FAIL
152131

153132
[Response interface: calling json(any, optional ResponseInit) on new Response() with too few arguments must throw TypeError]
154133
expected: FAIL
155134

156135
[Window interface: operation fetch(RequestInfo, optional RequestInit)]
157-
expected: FAIL
158136

159137
[Window interface: calling fetch(RequestInfo, optional RequestInit) on window with too few arguments must throw TypeError]
160138

0 commit comments

Comments
 (0)