Browse Source

format code in comments (#3902)

pull/3906/head
Jason Williams 5 months ago committed by GitHub
parent
commit
6a6fa6ae8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      core/engine/src/context/hooks.rs
  2. 12
      core/engine/src/context/mod.rs
  3. 11
      core/engine/src/error.rs
  4. 3
      core/engine/src/object/builtins/jsdataview.rs
  5. 4
      core/engine/src/object/builtins/jsdate.rs
  6. 15
      core/engine/src/object/builtins/jsmap.rs
  7. 64
      core/engine/src/object/builtins/jspromise.rs
  8. 3
      core/engine/src/object/datatypes.rs
  9. 9
      core/engine/src/value/conversions/convert.rs
  10. 70
      core/interop/src/lib.rs
  11. 2
      core/runtime/src/lib.rs

3
core/engine/src/context/hooks.rs

@ -45,8 +45,7 @@ use time::util::local_offset;
/// } /// }
/// } /// }
/// ///
/// let context = /// let context = &mut ContextBuilder::new().host_hooks(&Hooks).build().unwrap();
/// &mut ContextBuilder::new().host_hooks(&Hooks).build().unwrap();
/// let result = context.eval(Source::from_bytes(r#"eval("let a = 5")"#)); /// let result = context.eval(Source::from_bytes(r#"eval("let a = 5")"#));
/// assert_eq!( /// assert_eq!(
/// result.unwrap_err().to_string(), /// result.unwrap_err().to_string(),

12
core/engine/src/context/mod.rs

@ -220,11 +220,7 @@ impl Context {
/// let mut context = Context::default(); /// let mut context = Context::default();
/// ///
/// context /// context
/// .register_global_property( /// .register_global_property(js_str!("myPrimitiveProperty"), 10, Attribute::all())
/// js_str!("myPrimitiveProperty"),
/// 10,
/// Attribute::all(),
/// )
/// .expect("property shouldn't exist"); /// .expect("property shouldn't exist");
/// ///
/// let object = ObjectInitializer::new(&mut context) /// let object = ObjectInitializer::new(&mut context)
@ -232,11 +228,7 @@ impl Context {
/// .property(js_str!("y"), 1, Attribute::all()) /// .property(js_str!("y"), 1, Attribute::all())
/// .build(); /// .build();
/// context /// context
/// .register_global_property( /// .register_global_property(js_str!("myObjectProperty"), object, Attribute::all())
/// js_str!("myObjectProperty"),
/// object,
/// Attribute::all(),
/// )
/// .expect("property shouldn't exist"); /// .expect("property shouldn't exist");
/// ``` /// ```
pub fn register_global_property<K, V>( pub fn register_global_property<K, V>(

11
core/engine/src/error.rs

@ -34,7 +34,10 @@ use thiserror::Error;
/// let cause = JsError::from_opaque(js_str!("error!").into()); /// let cause = JsError::from_opaque(js_str!("error!").into());
/// ///
/// assert!(cause.as_opaque().is_some()); /// assert!(cause.as_opaque().is_some());
/// assert_eq!(cause.as_opaque().unwrap(), &JsValue::from(js_str!("error!"))); /// assert_eq!(
/// cause.as_opaque().unwrap(),
/// &JsValue::from(js_str!("error!"))
/// );
/// ///
/// let native_error: JsError = JsNativeError::typ() /// let native_error: JsError = JsNativeError::typ()
/// .with_message("invalid type!") /// .with_message("invalid type!")
@ -170,8 +173,7 @@ impl JsError {
/// # use boa_engine::{Context, JsError, JsNativeError}; /// # use boa_engine::{Context, JsError, JsNativeError};
/// # use boa_engine::builtins::error::ErrorObject; /// # use boa_engine::builtins::error::ErrorObject;
/// let context = &mut Context::default(); /// let context = &mut Context::default();
/// let error: JsError = /// let error: JsError = JsNativeError::eval().with_message("invalid script").into();
/// JsNativeError::eval().with_message("invalid script").into();
/// let error_val = error.to_opaque(context); /// let error_val = error.to_opaque(context);
/// ///
/// assert!(error_val.as_object().unwrap().is::<ErrorObject>()); /// assert!(error_val.as_object().unwrap().is::<ErrorObject>());
@ -346,8 +348,7 @@ impl JsError {
/// ///
/// ```rust /// ```rust
/// # use boa_engine::{JsError, JsNativeError, JsValue}; /// # use boa_engine::{JsError, JsNativeError, JsValue};
/// let error: JsError = /// let error: JsError = JsNativeError::error().with_message("Unknown error").into();
/// JsNativeError::error().with_message("Unknown error").into();
/// ///
/// assert!(error.as_native().is_some()); /// assert!(error.as_native().is_some());
/// ///

3
core/engine/src/object/builtins/jsdataview.rs

@ -23,8 +23,7 @@ use std::ops::Deref;
/// let array_buffer = JsArrayBuffer::new(4, context)?; /// let array_buffer = JsArrayBuffer::new(4, context)?;
/// ///
/// // Create a new Dataview from pre-existing ArrayBuffer /// // Create a new Dataview from pre-existing ArrayBuffer
/// let data_view = /// let data_view = JsDataView::from_js_array_buffer(array_buffer, None, None, context)?;
/// JsDataView::from_js_array_buffer(array_buffer, None, None, context)?;
/// ///
/// # Ok(()) /// # Ok(())
/// # } /// # }

4
core/engine/src/object/builtins/jsdate.rs

@ -14,9 +14,7 @@ use time::{format_description::well_known::Rfc3339, OffsetDateTime};
/// Create a `JsDate` object and set date to December 4 1995 /// Create a `JsDate` object and set date to December 4 1995
/// ///
/// ``` /// ```
/// use boa_engine::{ /// use boa_engine::{js_str, object::builtins::JsDate, Context, JsResult, JsValue};
/// js_str, object::builtins::JsDate, Context, JsResult, JsValue,
/// };
/// ///
/// fn main() -> JsResult<()> { /// fn main() -> JsResult<()> {
/// // JS mutable Context /// // JS mutable Context

15
core/engine/src/object/builtins/jsmap.rs

@ -111,10 +111,7 @@ impl JsMap {
/// let js_array = JsArray::new(context); /// let js_array = JsArray::new(context);
/// ///
/// // Create a `[key, value]` pair of JsValues and add it to the `JsArray` as a `JsArray` /// // Create a `[key, value]` pair of JsValues and add it to the `JsArray` as a `JsArray`
/// let vec_one: Vec<JsValue> = vec![ /// let vec_one: Vec<JsValue> = vec![js_str!("first-key").into(), js_str!("first-value").into()];
/// js_str!("first-key").into(),
/// js_str!("first-value").into()
/// ];
/// js_array.push(JsArray::from_iter(vec_one, context), context)?; /// js_array.push(JsArray::from_iter(vec_one, context), context)?;
/// ///
/// // Create a `JsMap` from the `JsArray` using it's iterable property. /// // Create a `JsMap` from the `JsArray` using it's iterable property.
@ -232,10 +229,7 @@ impl JsMap {
/// js_map.set(js_str!("foo"), js_str!("bar"), context)?; /// js_map.set(js_str!("foo"), js_str!("bar"), context)?;
/// js_map.set(2, 4, context)?; /// js_map.set(2, 4, context)?;
/// ///
/// assert_eq!( /// assert_eq!(js_map.get(js_str!("foo"), context)?, js_str!("bar").into());
/// js_map.get(js_str!("foo"), context)?,
/// js_str!("bar").into()
/// );
/// assert_eq!(js_map.get(2, context)?, 4.into()); /// assert_eq!(js_map.get(2, context)?, 4.into());
/// # Ok(()) /// # Ok(())
/// # } /// # }
@ -296,10 +290,7 @@ impl JsMap {
/// js_map.delete(js_str!("foo"), context)?; /// js_map.delete(js_str!("foo"), context)?;
/// ///
/// assert_eq!(js_map.get_size(context)?, 1.into()); /// assert_eq!(js_map.get_size(context)?, 1.into());
/// assert_eq!( /// assert_eq!(js_map.get(js_str!("foo"), context)?, JsValue::undefined());
/// js_map.get(js_str!("foo"), context)?,
/// JsValue::undefined()
/// );
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```

64
core/engine/src/object/builtins/jspromise.rs

@ -34,20 +34,14 @@ use boa_gc::{Finalize, Gc, GcRefCell, Trace};
/// # fn main() -> Result<(), Box<dyn Error>> { /// # fn main() -> Result<(), Box<dyn Error>> {
/// let context = &mut Context::default(); /// let context = &mut Context::default();
/// ///
/// context.register_global_property( /// context.register_global_property(js_str!("finally"), false, Attribute::all());
/// js_str!("finally"),
/// false,
/// Attribute::all(),
/// );
/// ///
/// let promise = JsPromise::new( /// let promise = JsPromise::new(
/// |resolvers, context| { /// |resolvers, context| {
/// let result = js_str!("hello world!").into(); /// let result = js_str!("hello world!").into();
/// resolvers.resolve.call( /// resolvers
/// &JsValue::undefined(), /// .resolve
/// &[result], /// .call(&JsValue::undefined(), &[result], context)?;
/// context,
/// )?;
/// Ok(JsValue::undefined()) /// Ok(JsValue::undefined())
/// }, /// },
/// context, /// context,
@ -57,8 +51,7 @@ use boa_gc::{Finalize, Gc, GcRefCell, Trace};
/// .then( /// .then(
/// Some( /// Some(
/// NativeFunction::from_fn_ptr(|_, args, _| { /// NativeFunction::from_fn_ptr(|_, args, _| {
/// Err(JsError::from_opaque(args.get_or_undefined(0).clone()) /// Err(JsError::from_opaque(args.get_or_undefined(0).clone()).into())
/// .into())
/// }) /// })
/// .to_js_function(context.realm()), /// .to_js_function(context.realm()),
/// ), /// ),
@ -66,9 +59,7 @@ use boa_gc::{Finalize, Gc, GcRefCell, Trace};
/// context, /// context,
/// ) /// )
/// .catch( /// .catch(
/// NativeFunction::from_fn_ptr(|_, args, _| { /// NativeFunction::from_fn_ptr(|_, args, _| Ok(args.get_or_undefined(0).clone()))
/// Ok(args.get_or_undefined(0).clone())
/// })
/// .to_js_function(context.realm()), /// .to_js_function(context.realm()),
/// context, /// context,
/// ) /// )
@ -140,11 +131,9 @@ impl JsPromise {
/// let promise = JsPromise::new( /// let promise = JsPromise::new(
/// |resolvers, context| { /// |resolvers, context| {
/// let result = js_string!("hello world").into(); /// let result = js_string!("hello world").into();
/// resolvers.resolve.call( /// resolvers
/// &JsValue::undefined(), /// .resolve
/// &[result], /// .call(&JsValue::undefined(), &[result], context)?;
/// context,
/// )?;
/// Ok(JsValue::undefined()) /// Ok(JsValue::undefined())
/// }, /// },
/// context, /// context,
@ -379,10 +368,7 @@ impl JsPromise {
/// # }; /// # };
/// let context = &mut Context::default(); /// let context = &mut Context::default();
/// ///
/// let promise = JsPromise::reject( /// let promise = JsPromise::reject(JsError::from_opaque(js_string!("oops!").into()), context);
/// JsError::from_opaque(js_string!("oops!").into()),
/// context,
/// );
/// ///
/// assert_eq!( /// assert_eq!(
/// promise.state(), /// promise.state(),
@ -467,11 +453,9 @@ impl JsPromise {
/// ///
/// let promise = JsPromise::new( /// let promise = JsPromise::new(
/// |resolvers, context| { /// |resolvers, context| {
/// resolvers.resolve.call( /// resolvers
/// &JsValue::undefined(), /// .resolve
/// &[255.255.into()], /// .call(&JsValue::undefined(), &[255.255.into()], context)?;
/// context,
/// )?;
/// Ok(JsValue::undefined()) /// Ok(JsValue::undefined())
/// }, /// },
/// context, /// context,
@ -534,11 +518,9 @@ impl JsPromise {
/// |resolvers, context| { /// |resolvers, context| {
/// let error = JsNativeError::typ().with_message("thrown"); /// let error = JsNativeError::typ().with_message("thrown");
/// let error = error.to_opaque(context); /// let error = error.to_opaque(context);
/// resolvers.reject.call( /// resolvers
/// &JsValue::undefined(), /// .reject
/// &[error.into()], /// .call(&JsValue::undefined(), &[error.into()], context)?;
/// context,
/// )?;
/// Ok(JsValue::undefined()) /// Ok(JsValue::undefined())
/// }, /// },
/// context, /// context,
@ -592,21 +574,15 @@ impl JsPromise {
/// # fn main() -> Result<(), Box<dyn Error>> { /// # fn main() -> Result<(), Box<dyn Error>> {
/// let context = &mut Context::default(); /// let context = &mut Context::default();
/// ///
/// context.register_global_property( /// context.register_global_property(js_string!("finally"), false, Attribute::all())?;
/// js_string!("finally"),
/// false,
/// Attribute::all(),
/// )?;
/// ///
/// let promise = JsPromise::new( /// let promise = JsPromise::new(
/// |resolvers, context| { /// |resolvers, context| {
/// let error = JsNativeError::typ().with_message("thrown"); /// let error = JsNativeError::typ().with_message("thrown");
/// let error = error.to_opaque(context); /// let error = error.to_opaque(context);
/// resolvers.reject.call( /// resolvers
/// &JsValue::undefined(), /// .reject
/// &[error.into()], /// .call(&JsValue::undefined(), &[error.into()], context)?;
/// context,
/// )?;
/// Ok(JsValue::undefined()) /// Ok(JsValue::undefined())
/// }, /// },
/// context, /// context,

3
core/engine/src/object/datatypes.rs

@ -33,8 +33,7 @@ use super::internal_methods::{InternalObjectMethods, ORDINARY_INTERNAL_METHODS};
/// counter: usize, /// counter: usize,
/// } /// }
/// ///
/// let object = /// let object = JsObject::from_proto_and_data(None, CustomStruct { counter: 5 });
/// JsObject::from_proto_and_data(None, CustomStruct { counter: 5 });
/// ///
/// assert_eq!(object.downcast_ref::<CustomStruct>().unwrap().counter, 5); /// assert_eq!(object.downcast_ref::<CustomStruct>().unwrap().counter, 5);
/// ``` /// ```

9
core/engine/src/value/conversions/convert.rs

@ -31,9 +31,12 @@ use crate::{Context, JsResult, JsString, JsValue};
/// # use boa_engine::{Context, js_string, JsValue}; /// # use boa_engine::{Context, js_string, JsValue};
/// # use boa_engine::value::{Convert, TryFromJs}; /// # use boa_engine::value::{Convert, TryFromJs};
/// # let mut context = Context::default(); /// # let mut context = Context::default();
/// let Convert(conv0): Convert<bool> = Convert::try_from_js(&JsValue::Integer(0), &mut context).unwrap(); /// let Convert(conv0): Convert<bool> =
/// let Convert(conv5): Convert<bool> = Convert::try_from_js(&JsValue::Integer(5), &mut context).unwrap(); /// Convert::try_from_js(&JsValue::Integer(0), &mut context).unwrap();
/// let Convert(conv_nan): Convert<bool> = Convert::try_from_js(&JsValue::Rational(f64::NAN), &mut context).unwrap(); /// let Convert(conv5): Convert<bool> =
/// Convert::try_from_js(&JsValue::Integer(5), &mut context).unwrap();
/// let Convert(conv_nan): Convert<bool> =
/// Convert::try_from_js(&JsValue::Rational(f64::NAN), &mut context).unwrap();
/// ///
/// assert_eq!(conv0, false); /// assert_eq!(conv0, false);
/// assert_eq!(conv5, true); /// assert_eq!(conv5, true);

70
core/interop/src/lib.rs

@ -58,7 +58,13 @@ impl<T: IntoIterator<Item = (JsString, NativeFunction)> + Clone> IntoJsModule fo
/// # let mut context = Context::default(); /// # let mut context = Context::default();
/// let f = |a: i32, b: i32| a + b; /// let f = |a: i32, b: i32| a + b;
/// let f = unsafe { f.into_js_function_unsafe(&mut context) }; /// let f = unsafe { f.into_js_function_unsafe(&mut context) };
/// let result = f.call(&JsValue::undefined(), &[JsValue::from(1), JsValue::from(2)], &mut context).unwrap(); /// let result = f
/// .call(
/// &JsValue::undefined(),
/// &[JsValue::from(1), JsValue::from(2)],
/// &mut context,
/// )
/// .unwrap();
/// assert_eq!(result, JsValue::new(3)); /// assert_eq!(result, JsValue::new(3));
/// ``` /// ```
/// ///
@ -79,8 +85,10 @@ impl<T: IntoIterator<Item = (JsString, NativeFunction)> + Clone> IntoJsModule fo
/// move |a: i32| *x.borrow_mut() += a /// move |a: i32| *x.borrow_mut() += a
/// }; /// };
/// let f = unsafe { f.into_js_function_unsafe(&mut context) }; /// let f = unsafe { f.into_js_function_unsafe(&mut context) };
/// f.call(&JsValue::undefined(), &[JsValue::from(1)], &mut context).unwrap(); /// f.call(&JsValue::undefined(), &[JsValue::from(1)], &mut context)
/// f.call(&JsValue::undefined(), &[JsValue::from(4)], &mut context).unwrap(); /// .unwrap();
/// f.call(&JsValue::undefined(), &[JsValue::from(4)], &mut context)
/// .unwrap();
/// assert_eq!(*x.borrow(), 5); /// assert_eq!(*x.borrow(), 5);
/// ``` /// ```
pub trait UnsafeIntoJsFunction<Args, Ret>: private::IntoJsFunctionSealed<Args, Ret> { pub trait UnsafeIntoJsFunction<Args, Ret>: private::IntoJsFunctionSealed<Args, Ret> {
@ -103,11 +111,13 @@ pub trait UnsafeIntoJsFunction<Args, Ret>: private::IntoJsFunctionSealed<Args, R
/// # let mut context = Context::default(); /// # let mut context = Context::default();
/// let f = |a: i32, b: i32| a + b; /// let f = |a: i32, b: i32| a + b;
/// let f = f.into_js_function_copied(&mut context); /// let f = f.into_js_function_copied(&mut context);
/// let result = f.call( /// let result = f
/// .call(
/// &JsValue::undefined(), /// &JsValue::undefined(),
/// &[JsValue::from(1), JsValue::from(2)], /// &[JsValue::from(1), JsValue::from(2)],
/// &mut context /// &mut context,
/// ).unwrap(); /// )
/// .unwrap();
/// assert_eq!(result, JsValue::new(3)); /// assert_eq!(result, JsValue::new(3));
/// ``` /// ```
pub trait IntoJsFunctionCopied<Args, Ret>: private::IntoJsFunctionSealed<Args, Ret> + Copy { pub trait IntoJsFunctionCopied<Args, Ret>: private::IntoJsFunctionSealed<Args, Ret> + Copy {
@ -155,14 +165,19 @@ impl<'a, T: TryFromJs> TryFromJsArgument<'a> for T {
/// # use boa_interop::{IntoJsFunctionCopied, JsRest}; /// # use boa_interop::{IntoJsFunctionCopied, JsRest};
/// # let mut context = Context::default(); /// # let mut context = Context::default();
/// let sums = (|args: JsRest, context: &mut Context| -> i32 { /// let sums = (|args: JsRest, context: &mut Context| -> i32 {
/// args.iter().map(|i| i.try_js_into::<i32>(context).unwrap()).sum::<i32>() /// args.iter()
/// }).into_js_function_copied(&mut context); /// .map(|i| i.try_js_into::<i32>(context).unwrap())
/// .sum::<i32>()
/// })
/// .into_js_function_copied(&mut context);
/// ///
/// let result = sums.call( /// let result = sums
/// .call(
/// &JsValue::undefined(), /// &JsValue::undefined(),
/// &[JsValue::from(1), JsValue::from(2), JsValue::from(3)], /// &[JsValue::from(1), JsValue::from(2), JsValue::from(3)],
/// &mut context /// &mut context,
/// ).unwrap(); /// )
/// .unwrap();
/// assert_eq!(result, JsValue::new(6)); /// assert_eq!(result, JsValue::new(6));
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -224,15 +239,22 @@ impl<'a> IntoIterator for JsRest<'a> {
/// # use boa_engine::{Context, JsValue}; /// # use boa_engine::{Context, JsValue};
/// # use boa_interop::{IntoJsFunctionCopied, JsAll}; /// # use boa_interop::{IntoJsFunctionCopied, JsAll};
/// # let mut context = Context::default(); /// # let mut context = Context::default();
/// let sums = (|args: JsAll<i32>, context: &mut Context| -> i32 { /// let sums = (|args: JsAll<i32>, context: &mut Context| -> i32 { args.iter().sum() })
/// args.iter().sum() /// .into_js_function_copied(&mut context);
/// }).into_js_function_copied(&mut context);
/// ///
/// let result = sums.call( /// let result = sums
/// .call(
/// &JsValue::undefined(), /// &JsValue::undefined(),
/// &[JsValue::from(1), JsValue::from(2), JsValue::from(3), JsValue::Boolean(true), JsValue::from(4)], /// &[
/// &mut context /// JsValue::from(1),
/// ).unwrap(); /// JsValue::from(2),
/// JsValue::from(3),
/// JsValue::Boolean(true),
/// JsValue::from(4),
/// ],
/// &mut context,
/// )
/// .unwrap();
/// assert_eq!(result, JsValue::new(6)); /// assert_eq!(result, JsValue::new(6));
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -316,7 +338,7 @@ impl<'a, T: TryFromJs> TryFromJsArgument<'a> for JsThis<T> {
/// For example, /// For example,
/// ``` /// ```
/// # use boa_engine::{Context, Finalize, JsData, JsValue, Trace}; /// # use boa_engine::{Context, Finalize, JsData, JsValue, Trace};
/// use boa_interop::{IntoJsFunctionCopied, ContextData}; /// use boa_interop::{ContextData, IntoJsFunctionCopied};
/// ///
/// #[derive(Clone, Debug, Finalize, JsData, Trace)] /// #[derive(Clone, Debug, Finalize, JsData, Trace)]
/// struct CustomHostDefinedStruct { /// struct CustomHostDefinedStruct {
@ -325,11 +347,13 @@ impl<'a, T: TryFromJs> TryFromJsArgument<'a> for JsThis<T> {
/// } /// }
/// let mut context = Context::default(); /// let mut context = Context::default();
/// context.insert_data(CustomHostDefinedStruct { counter: 123 }); /// context.insert_data(CustomHostDefinedStruct { counter: 123 });
/// let f = (|ContextData(host): ContextData<CustomHostDefinedStruct>| { /// let f = (|ContextData(host): ContextData<CustomHostDefinedStruct>| host.counter + 1)
/// host.counter + 1 /// .into_js_function_copied(&mut context);
/// }).into_js_function_copied(&mut context);
/// ///
/// assert_eq!(f.call(&JsValue::undefined(), &[], &mut context), Ok(JsValue::new(124))); /// assert_eq!(
/// f.call(&JsValue::undefined(), &[], &mut context),
/// Ok(JsValue::new(124))
/// );
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ContextData<T: Clone>(pub T); pub struct ContextData<T: Clone>(pub T);

2
core/runtime/src/lib.rs

@ -6,7 +6,7 @@
//! 1. Add **boa_runtime** as a dependency to your project along with **boa_engine**. //! 1. Add **boa_runtime** as a dependency to your project along with **boa_engine**.
//! //!
//! ``` //! ```
//! use boa_engine::{ Context, Source, property::Attribute, js_string }; //! use boa_engine::{js_string, property::Attribute, Context, Source};
//! use boa_runtime::Console; //! use boa_runtime::Console;
//! //!
//! // Create the context. //! // Create the context.

Loading…
Cancel
Save