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. 66
      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. 84
      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 =
/// &mut ContextBuilder::new().host_hooks(&Hooks).build().unwrap();
/// let context = &mut ContextBuilder::new().host_hooks(&Hooks).build().unwrap();
/// let result = context.eval(Source::from_bytes(r#"eval("let a = 5")"#));
/// assert_eq!(
/// result.unwrap_err().to_string(),

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

@ -220,11 +220,7 @@ impl Context {
/// let mut context = Context::default();
///
/// context
/// .register_global_property(
/// js_str!("myPrimitiveProperty"),
/// 10,
/// Attribute::all(),
/// )
/// .register_global_property(js_str!("myPrimitiveProperty"), 10, Attribute::all())
/// .expect("property shouldn't exist");
///
/// let object = ObjectInitializer::new(&mut context)
@ -232,11 +228,7 @@ impl Context {
/// .property(js_str!("y"), 1, Attribute::all())
/// .build();
/// context
/// .register_global_property(
/// js_str!("myObjectProperty"),
/// object,
/// Attribute::all(),
/// )
/// .register_global_property(js_str!("myObjectProperty"), object, Attribute::all())
/// .expect("property shouldn't exist");
/// ```
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());
///
/// 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()
/// .with_message("invalid type!")
@ -170,8 +173,7 @@ impl JsError {
/// # use boa_engine::{Context, JsError, JsNativeError};
/// # use boa_engine::builtins::error::ErrorObject;
/// let context = &mut Context::default();
/// let error: JsError =
/// JsNativeError::eval().with_message("invalid script").into();
/// let error: JsError = JsNativeError::eval().with_message("invalid script").into();
/// let error_val = error.to_opaque(context);
///
/// assert!(error_val.as_object().unwrap().is::<ErrorObject>());
@ -346,8 +348,7 @@ impl JsError {
///
/// ```rust
/// # use boa_engine::{JsError, JsNativeError, JsValue};
/// let error: JsError =
/// JsNativeError::error().with_message("Unknown error").into();
/// let error: JsError = JsNativeError::error().with_message("Unknown error").into();
///
/// 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)?;
///
/// // Create a new Dataview from pre-existing ArrayBuffer
/// let data_view =
/// JsDataView::from_js_array_buffer(array_buffer, None, None, context)?;
/// let data_view = JsDataView::from_js_array_buffer(array_buffer, None, None, context)?;
///
/// # 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
///
/// ```
/// use boa_engine::{
/// js_str, object::builtins::JsDate, Context, JsResult, JsValue,
/// };
/// use boa_engine::{js_str, object::builtins::JsDate, Context, JsResult, JsValue};
///
/// fn main() -> JsResult<()> {
/// // JS mutable Context

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

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

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

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

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

@ -33,8 +33,7 @@ use super::internal_methods::{InternalObjectMethods, ORDINARY_INTERNAL_METHODS};
/// counter: usize,
/// }
///
/// let object =
/// JsObject::from_proto_and_data(None, CustomStruct { counter: 5 });
/// let object = JsObject::from_proto_and_data(None, CustomStruct { 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::value::{Convert, TryFromJs};
/// # let mut context = Context::default();
/// let Convert(conv0): Convert<bool> = Convert::try_from_js(&JsValue::Integer(0), &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();
/// let Convert(conv0): Convert<bool> =
/// Convert::try_from_js(&JsValue::Integer(0), &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!(conv5, true);

84
core/interop/src/lib.rs

@ -58,7 +58,13 @@ impl<T: IntoIterator<Item = (JsString, NativeFunction)> + Clone> IntoJsModule fo
/// # let mut context = Context::default();
/// let f = |a: i32, b: i32| a + b;
/// 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));
/// ```
///
@ -79,8 +85,10 @@ impl<T: IntoIterator<Item = (JsString, NativeFunction)> + Clone> IntoJsModule fo
/// move |a: i32| *x.borrow_mut() += a
/// };
/// 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(4)], &mut context).unwrap();
/// f.call(&JsValue::undefined(), &[JsValue::from(1)], &mut context)
/// .unwrap();
/// f.call(&JsValue::undefined(), &[JsValue::from(4)], &mut context)
/// .unwrap();
/// assert_eq!(*x.borrow(), 5);
/// ```
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 f = |a: i32, b: i32| a + b;
/// let f = f.into_js_function_copied(&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));
/// ```
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};
/// # let mut context = Context::default();
/// let sums = (|args: JsRest, context: &mut Context| -> i32 {
/// args.iter().map(|i| i.try_js_into::<i32>(context).unwrap()).sum::<i32>()
/// }).into_js_function_copied(&mut context);
/// args.iter()
/// .map(|i| i.try_js_into::<i32>(context).unwrap())
/// .sum::<i32>()
/// })
/// .into_js_function_copied(&mut context);
///
/// let result = sums.call(
/// &JsValue::undefined(),
/// &[JsValue::from(1), JsValue::from(2), JsValue::from(3)],
/// &mut context
/// ).unwrap();
/// let result = sums
/// .call(
/// &JsValue::undefined(),
/// &[JsValue::from(1), JsValue::from(2), JsValue::from(3)],
/// &mut context,
/// )
/// .unwrap();
/// assert_eq!(result, JsValue::new(6));
/// ```
#[derive(Debug, Clone)]
@ -224,15 +239,22 @@ impl<'a> IntoIterator for JsRest<'a> {
/// # use boa_engine::{Context, JsValue};
/// # use boa_interop::{IntoJsFunctionCopied, JsAll};
/// # let mut context = Context::default();
/// let sums = (|args: JsAll<i32>, context: &mut Context| -> i32 {
/// args.iter().sum()
/// }).into_js_function_copied(&mut context);
/// let sums = (|args: JsAll<i32>, context: &mut Context| -> i32 { args.iter().sum() })
/// .into_js_function_copied(&mut context);
///
/// let result = sums.call(
/// &JsValue::undefined(),
/// &[JsValue::from(1), JsValue::from(2), JsValue::from(3), JsValue::Boolean(true), JsValue::from(4)],
/// &mut context
/// ).unwrap();
/// let result = sums
/// .call(
/// &JsValue::undefined(),
/// &[
/// JsValue::from(1),
/// JsValue::from(2),
/// JsValue::from(3),
/// JsValue::Boolean(true),
/// JsValue::from(4),
/// ],
/// &mut context,
/// )
/// .unwrap();
/// assert_eq!(result, JsValue::new(6));
/// ```
#[derive(Debug, Clone)]
@ -316,20 +338,22 @@ impl<'a, T: TryFromJs> TryFromJsArgument<'a> for JsThis<T> {
/// For example,
/// ```
/// # use boa_engine::{Context, Finalize, JsData, JsValue, Trace};
/// use boa_interop::{IntoJsFunctionCopied, ContextData};
/// use boa_interop::{ContextData, IntoJsFunctionCopied};
///
/// #[derive(Clone, Debug, Finalize, JsData, Trace)]
/// struct CustomHostDefinedStruct {
/// #[unsafe_ignore_trace]
/// pub counter: usize,
/// #[unsafe_ignore_trace]
/// pub counter: usize,
/// }
/// let mut context = Context::default();
/// context.insert_data(CustomHostDefinedStruct { counter: 123 });
/// let f = (|ContextData(host): ContextData<CustomHostDefinedStruct>| {
/// host.counter + 1
/// }).into_js_function_copied(&mut context);
/// let f = (|ContextData(host): ContextData<CustomHostDefinedStruct>| host.counter + 1)
/// .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)]
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**.
//!
//! ```
//! use boa_engine::{ Context, Source, property::Attribute, js_string };
//! use boa_engine::{js_string, property::Attribute, Context, Source};
//! use boa_runtime::Console;
//!
//! // Create the context.

Loading…
Cancel
Save