aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/promise.rs
diff options
context:
space:
mode:
authorJosh Matthews <josh@joshmatthews.net>2025-03-25 10:08:45 -0400
committerGitHub <noreply@github.com>2025-03-25 14:08:45 +0000
commitf717f6b848fe77ee948424b82d4419dcc5666c52 (patch)
treef08336605e3d301b7eac70a1371b8cb71dfa84e4 /components/script/dom/promise.rs
parentf65b697a5aa963c846fc2efd0019fbf983fddfac (diff)
downloadservo-f717f6b848fe77ee948424b82d4419dcc5666c52.tar.gz
servo-f717f6b848fe77ee948424b82d4419dcc5666c52.zip
script: Support converting JS values to Rc<Promise> with FromJSValConvertible. (#36097)
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Diffstat (limited to 'components/script/dom/promise.rs')
-rw-r--r--components/script/dom/promise.rs23
1 files changed, 22 insertions, 1 deletions
diff --git a/components/script/dom/promise.rs b/components/script/dom/promise.rs
index d7c309a7ded..dd09b1df3a1 100644
--- a/components/script/dom/promise.rs
+++ b/components/script/dom/promise.rs
@@ -15,7 +15,7 @@ use std::ptr;
use std::rc::Rc;
use dom_struct::dom_struct;
-use js::conversions::ToJSValConvertible;
+use js::conversions::{ConversionResult, FromJSValConvertibleRc, ToJSValConvertible};
use js::jsapi::{
AddRawValueRoot, CallArgs, GetFunctionNativeReserved, Heap, JS_ClearPendingException,
JS_GetFunctionObject, JS_NewFunction, JSAutoRealm, JSContext, JSObject,
@@ -405,3 +405,24 @@ impl PromiseHelpers<crate::DomTypeHolder> for Promise {
Promise::new_resolved(global, cx, value, CanGc::note())
}
}
+
+impl FromJSValConvertibleRc for Promise {
+ #[allow(unsafe_code)]
+ unsafe fn from_jsval(
+ cx: *mut JSContext,
+ value: HandleValue,
+ ) -> Result<ConversionResult<Rc<Promise>>, ()> {
+ if value.get().is_null() {
+ return Ok(ConversionResult::Failure("null not allowed".into()));
+ }
+ if !value.get().is_object() {
+ return Ok(ConversionResult::Failure("not an object".into()));
+ }
+ rooted!(in(cx) let obj = value.get().to_object());
+ if !IsPromiseObject(obj.handle()) {
+ return Ok(ConversionResult::Failure("not a promise".into()));
+ }
+ let promise = Promise::new_with_js_promise(obj.handle(), SafeJSContext::from_ptr(cx));
+ Ok(ConversionResult::Success(promise))
+ }
+}