mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
script: Support converting JS values to Rc<Promise> with FromJSValConvertible. (#36097)
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
parent
f65b697a5a
commit
f717f6b848
5 changed files with 27 additions and 4 deletions
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -910,6 +910,7 @@ impl TestBindingMethods<crate::DomTypeHolder> for TestBinding {
|
|||
fn FuncControlledMethodDisabled(&self) {}
|
||||
fn FuncControlledMethodEnabled(&self) {}
|
||||
|
||||
fn PassRecordPromise(&self, _: Record<DOMString, Rc<Promise>>) {}
|
||||
fn PassRecord(&self, _: Record<DOMString, i32>) {}
|
||||
fn PassRecordWithUSVStringKey(&self, _: Record<USVString, i32>) {}
|
||||
fn PassRecordWithByteStringKey(&self, _: Record<ByteString, i32>) {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue