diff --git a/core/engine/src/builtins/promise/mod.rs b/core/engine/src/builtins/promise/mod.rs index 0533c323a5..e377938055 100644 --- a/core/engine/src/builtins/promise/mod.rs +++ b/core/engine/src/builtins/promise/mod.rs @@ -1559,7 +1559,7 @@ impl Promise { context: &mut Context, ) -> JsResult { // 1. If IsPromise(x) is true, then - if let Some(x) = x.as_promise() { + if let Some(x) = x.as_promise_object() { // a. Let xConstructor be ? Get(x, "constructor"). let x_constructor = x.get(CONSTRUCTOR, context)?; // b. If SameValue(xConstructor, C) is true, return x. @@ -1808,7 +1808,7 @@ impl Promise { let promise = this; // 2. If IsPromise(promise) is false, throw a TypeError exception. - let promise = promise.as_promise().ok_or_else(|| { + let promise = promise.as_promise_object().ok_or_else(|| { JsNativeError::typ().with_message("Promise.prototype.then: this is not a promise") })?; diff --git a/core/engine/src/value/mod.rs b/core/engine/src/value/mod.rs index 56d1d421c0..bc73b60992 100644 --- a/core/engine/src/value/mod.rs +++ b/core/engine/src/value/mod.rs @@ -21,7 +21,7 @@ use boa_profiler::Profiler; #[doc(inline)] pub use conversions::convert::Convert; -use crate::object::JsFunction; +use crate::object::{JsFunction, JsPromise}; use crate::{ builtins::{ number::{f64_to_int32, f64_to_uint32}, @@ -205,13 +205,22 @@ impl JsValue { matches!(self, Self::Object(obj) if obj.is::()) } - /// Returns the promise if the value is a promise, otherwise `None`. + /// Returns the value as an object if the value is a promise, otherwise `None`. #[inline] #[must_use] - pub fn as_promise(&self) -> Option<&JsObject> { + pub(crate) fn as_promise_object(&self) -> Option<&JsObject> { self.as_object().filter(|obj| obj.is::()) } + /// Returns the value as a promise if the value is a promise, otherwise `None`. + #[inline] + #[must_use] + pub fn as_promise(&self) -> Option { + self.as_promise_object() + .cloned() + .and_then(|o| JsPromise::from_object(o).ok()) + } + /// Returns true if the value is a symbol. #[inline] #[must_use]