Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Implement TryFromJs for JsObject wrappers #2809

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions boa_engine/src/object/builtins/jsarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
builtins::Array,
error::JsNativeError,
object::{JsFunction, JsObject, JsObjectType},
value::IntoOrUndefined,
value::{IntoOrUndefined, TryFromJs},
Context, JsResult, JsString, JsValue,
};
use boa_gc::{Finalize, Trace};
Expand Down Expand Up @@ -40,7 +40,7 @@ impl JsArray {
/// This does not clone the fields of the array, it only does a shallow clone of the object.
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
if object.borrow().is_array() {
if object.is_array() {
Ok(Self { inner: object })
} else {
Err(JsNativeError::typ()
Expand Down Expand Up @@ -399,3 +399,14 @@ impl Deref for JsArray {
}

impl JsObjectType for JsArray {}

impl TryFromJs for JsArray {
fn try_from_js(value: &JsValue, _context: &mut Context<'_>) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not an Array object")
.into()),
}
}
}
14 changes: 13 additions & 1 deletion boa_engine/src/object/builtins/jsarraybuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
object::{
internal_methods::get_prototype_from_constructor, JsObject, JsObjectType, ObjectData,
},
value::TryFromJs,
Context, JsResult, JsValue,
};
use boa_gc::{Finalize, Trace};
Expand Down Expand Up @@ -117,7 +118,7 @@ impl JsArrayBuffer {
/// This does not clone the fields of the array buffer, it only does a shallow clone of the object.
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
if object.borrow().is_array_buffer() {
if object.is_array_buffer() {
Ok(Self { inner: object })
} else {
Err(JsNativeError::typ()
Expand Down Expand Up @@ -222,3 +223,14 @@ impl Deref for JsArrayBuffer {
}

impl JsObjectType for JsArrayBuffer {}

impl TryFromJs for JsArrayBuffer {
fn try_from_js(value: &JsValue, _context: &mut Context<'_>) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not an ArrayBuffer object")
.into()),
}
}
}
14 changes: 13 additions & 1 deletion boa_engine/src/object/builtins/jsdataview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
internal_methods::get_prototype_from_constructor, JsArrayBuffer, JsObject, JsObjectType,
ObjectData,
},
value::TryFromJs,
Context, JsNativeError, JsResult, JsValue,
};

Expand Down Expand Up @@ -111,7 +112,7 @@ impl JsDataView {
/// Create a new `JsDataView` object from an existing object.
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
if object.borrow().is_data_view() {
if object.is_data_view() {
Ok(Self { inner: object })
} else {
Err(JsNativeError::typ()
Expand Down Expand Up @@ -485,3 +486,14 @@ impl Deref for JsDataView {
}

impl JsObjectType for JsDataView {}

impl TryFromJs for JsDataView {
fn try_from_js(value: &JsValue, _context: &mut Context<'_>) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not an DataView object")
.into()),
}
}
}
24 changes: 24 additions & 0 deletions boa_engine/src/object/builtins/jsdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use chrono::DateTime;
use crate::{
builtins::Date,
object::{JsObject, JsObjectType, ObjectData},
value::TryFromJs,
Context, JsNativeError, JsResult, JsValue,
};

Expand Down Expand Up @@ -47,6 +48,18 @@ impl JsDate {
Self { inner }
}

/// Create a new `JsDataView` object from an existing object.
Razican marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
if object.is_date() {
Ok(Self { inner: object })
} else {
Err(JsNativeError::typ()
.with_message("object is not a Date")
.into())
}
}

/// Return a `Number` representing the milliseconds elapsed since the UNIX epoch.
///
/// Same as JavaScript's `Date.now()`
Expand Down Expand Up @@ -587,3 +600,14 @@ impl Deref for JsDate {
}

impl JsObjectType for JsDate {}

impl TryFromJs for JsDate {
fn try_from_js(value: &JsValue, _context: &mut Context<'_>) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not a Date object")
.into()),
}
}
}
18 changes: 17 additions & 1 deletion boa_engine/src/object/builtins/jsfunction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! A Rust API wrapper for Boa's `Function` Builtin ECMAScript Object
use crate::{
object::{JsObject, JsObjectType},
JsValue,
value::TryFromJs,
Context, JsNativeError, JsResult, JsValue,
};
use boa_gc::{Finalize, Trace};
use std::ops::Deref;
Expand Down Expand Up @@ -52,3 +53,18 @@ impl Deref for JsFunction {
}

impl JsObjectType for JsFunction {}

impl TryFromJs for JsFunction {
fn try_from_js(value: &JsValue, _context: &mut Context<'_>) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()).ok_or_else(|| {
JsNativeError::typ()
.with_message("object is not a function")
.into()
}),
_ => Err(JsNativeError::typ()
.with_message("value is not a Function object")
.into()),
}
}
}
14 changes: 13 additions & 1 deletion boa_engine/src/object/builtins/jsgenerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::{
builtins::generator::{Generator, GeneratorState},
object::{JsObject, JsObjectType, ObjectData},
value::TryFromJs,
Context, JsNativeError, JsResult, JsValue,
};

Expand Down Expand Up @@ -34,7 +35,7 @@ impl JsGenerator {
/// Create a `JsGenerator` from a regular expression `JsObject`
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
if object.borrow().is_generator() {
if object.is_generator() {
Ok(Self { inner: object })
} else {
Err(JsNativeError::typ()
Expand Down Expand Up @@ -99,3 +100,14 @@ impl Deref for JsGenerator {
}

impl JsObjectType for JsGenerator {}

impl TryFromJs for JsGenerator {
fn try_from_js(value: &JsValue, _context: &mut Context<'_>) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not a Generator object")
.into()),
}
}
}
14 changes: 13 additions & 1 deletion boa_engine/src/object/builtins/jsmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
error::JsNativeError,
object::{JsFunction, JsMapIterator, JsObject, JsObjectType, ObjectData},
string::utf16,
value::TryFromJs,
Context, JsResult, JsValue,
};

Expand Down Expand Up @@ -169,7 +170,7 @@ impl JsMap {
/// ```
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
if object.borrow().is_map() {
if object.is_map() {
Ok(Self { inner: object })
} else {
Err(JsNativeError::typ()
Expand Down Expand Up @@ -422,3 +423,14 @@ impl Deref for JsMap {
}

impl JsObjectType for JsMap {}

impl TryFromJs for JsMap {
fn try_from_js(value: &JsValue, _context: &mut Context<'_>) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not a Map object")
.into()),
}
}
}
14 changes: 13 additions & 1 deletion boa_engine/src/object/builtins/jsmap_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
builtins::map::MapIterator,
error::JsNativeError,
object::{JsObject, JsObjectType},
value::TryFromJs,
Context, JsResult, JsValue,
};

Expand All @@ -19,7 +20,7 @@ impl JsMapIterator {
/// Create a [`JsMapIterator`] from a [`JsObject`]. If object is not a `MapIterator`, throw `TypeError`
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
if object.borrow().is_map_iterator() {
if object.is_map_iterator() {
Ok(Self { inner: object })
} else {
Err(JsNativeError::typ()
Expand Down Expand Up @@ -58,3 +59,14 @@ impl Deref for JsMapIterator {
}

impl JsObjectType for JsMapIterator {}

impl TryFromJs for JsMapIterator {
fn try_from_js(value: &JsValue, _context: &mut Context<'_>) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not a MapIterator object")
.into()),
}
}
}
16 changes: 14 additions & 2 deletions boa_engine/src/object/builtins/jspromise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
},
context::intrinsics::StandardConstructors,
object::{JsObject, JsObjectType, ObjectData},
value::TryFromJs,
Context, JsError, JsNativeError, JsResult, JsValue,
};

Expand Down Expand Up @@ -243,13 +244,13 @@ impl JsPromise {
/// # }
/// ```
#[inline]
pub fn from_object(object: JsObject) -> JsResult<JsPromise> {
pub fn from_object(object: JsObject) -> JsResult<Self> {
if !object.is_promise() {
return Err(JsNativeError::typ()
.with_message("`object` is not a Promise")
.into());
}
Ok(JsPromise { inner: object })
Ok(Self { inner: object })
}

/// Resolves a `JsValue` into a `JsPromise`.
Expand Down Expand Up @@ -883,3 +884,14 @@ impl std::ops::Deref for JsPromise {
}

impl JsObjectType for JsPromise {}

impl TryFromJs for JsPromise {
fn try_from_js(value: &JsValue, _context: &mut Context<'_>) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not a Promise object")
.into()),
}
}
}
27 changes: 26 additions & 1 deletion boa_engine/src/object/builtins/jsproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{
native_function::{NativeFunction, NativeFunctionPointer},
object::{FunctionObjectBuilder, JsObject, JsObjectType, ObjectData},
string::utf16,
Context, JsResult, JsValue,
value::TryFromJs,
Context, JsNativeError, JsResult, JsValue,
};

use super::JsFunction;
Expand All @@ -33,6 +34,19 @@ impl JsProxy {
pub fn builder(target: JsObject) -> JsProxyBuilder {
JsProxyBuilder::new(target)
}

/// Create a [`JsProxy`] from a [`JsObject`], if the object is not a `Proxy` throw a
/// `TypeError`.
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
if object.borrow().is_proxy() {
Ok(Self { inner: object })
} else {
Err(JsNativeError::typ()
.with_message("object is not a Proxy")
.into())
}
}
}

impl From<JsProxy> for JsObject {
Expand Down Expand Up @@ -60,6 +74,17 @@ impl std::ops::Deref for JsProxy {

impl JsObjectType for JsProxy {}

impl TryFromJs for JsProxy {
fn try_from_js(value: &JsValue, _context: &mut Context<'_>) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not a Proxy object")
.into()),
}
}
}

/// `JsRevocableProxy` provides a wrapper for `JsProxy` that can be disabled.
///
/// Safe interface for the [`Proxy.revocable`][revocable] method that creates a
Expand Down
14 changes: 13 additions & 1 deletion boa_engine/src/object/builtins/jsregexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::{
builtins::RegExp,
object::{JsArray, JsObject, JsObjectType},
value::TryFromJs,
Context, JsNativeError, JsResult, JsValue,
};

Expand Down Expand Up @@ -79,7 +80,7 @@ impl JsRegExp {
/// Create a `JsRegExp` from a regular expression `JsObject`
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
if object.borrow().is_regexp() {
if object.is_regexp() {
Ok(Self { inner: object })
} else {
Err(JsNativeError::typ()
Expand Down Expand Up @@ -279,3 +280,14 @@ impl Deref for JsRegExp {
}

impl JsObjectType for JsRegExp {}

impl TryFromJs for JsRegExp {
fn try_from_js(value: &JsValue, _context: &mut Context<'_>) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not a RegExp object")
.into()),
}
}
}
Loading