Skip to content

Commit

Permalink
Rename as_promise to as_promise_object and add as_promise -> JsPromise (
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl authored Aug 23, 2024
1 parent 1c1d820 commit 1691802
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions core/engine/src/builtins/promise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,7 @@ impl Promise {
context: &mut Context,
) -> JsResult<JsObject> {
// 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.
Expand Down Expand Up @@ -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")
})?;

Expand Down
15 changes: 12 additions & 3 deletions core/engine/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -205,13 +205,22 @@ impl JsValue {
matches!(self, Self::Object(obj) if obj.is::<Promise>())
}

/// 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::<Promise>())
}

/// Returns the value as a promise if the value is a promise, otherwise `None`.
#[inline]
#[must_use]
pub fn as_promise(&self) -> Option<JsPromise> {
self.as_promise_object()
.cloned()
.and_then(|o| JsPromise::from_object(o).ok())
}

/// Returns true if the value is a symbol.
#[inline]
#[must_use]
Expand Down

0 comments on commit 1691802

Please sign in to comment.