Browse Source

Format code snippets in docs (#3317)

pull/3318/head
José Julián Espina 1 year ago committed by GitHub
parent
commit
ae414797af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      boa_ast/src/statement/switch.rs
  2. 1
      boa_cli/src/main.rs
  3. 13
      boa_engine/src/context/hooks.rs
  4. 3
      boa_engine/src/context/mod.rs
  5. 6
      boa_engine/src/error.rs
  6. 5
      boa_engine/src/job.rs
  7. 1
      boa_engine/src/lib.rs
  8. 10
      boa_engine/src/module/mod.rs
  9. 3
      boa_engine/src/object/builtins/jsdataview.rs
  10. 17
      boa_engine/src/object/builtins/jsdate.rs
  11. 167
      boa_engine/src/object/builtins/jspromise.rs
  12. 1
      boa_engine/src/object/builtins/jsregexp.rs
  13. 6
      boa_engine/src/object/mod.rs
  14. 2
      boa_engine/src/string/mod.rs
  15. 1
      boa_engine/src/vm/code_block.rs
  16. 1
      boa_engine/src/vm/opcode/mod.rs
  17. 1
      boa_gc/src/lib.rs
  18. 1
      boa_interner/src/lib.rs
  19. 1
      boa_macros/src/lib.rs
  20. 1
      boa_parser/src/lib.rs
  21. 2
      boa_parser/src/parser/cursor/buffered_lexer/tests.rs
  22. 1
      boa_profiler/src/lib.rs
  23. 2
      boa_runtime/src/lib.rs
  24. 1
      boa_tester/src/main.rs
  25. 1
      boa_wasm/src/lib.rs

1
boa_ast/src/statement/switch.rs

@ -1,5 +1,4 @@
//! Switch node.
//!
use crate::{
expression::Expression,
statement::Statement,

1
boa_cli/src/main.rs

@ -1,5 +1,4 @@
//! A ECMAScript REPL implementation based on boa_engine.
//!
#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",

13
boa_engine/src/context/hooks.rs

@ -22,10 +22,8 @@ use super::intrinsics::Intrinsics;
/// ```
/// use boa_engine::{
/// context::{Context, ContextBuilder, HostHooks},
/// JsNativeError,
/// JsResult,
/// realm::Realm,
/// Source
/// JsNativeError, JsResult, Source,
/// };
///
/// struct Hooks;
@ -36,13 +34,18 @@ use super::intrinsics::Intrinsics;
/// _realm: Realm,
/// context: &mut Context<'_>,
/// ) -> JsResult<()> {
/// Err(JsNativeError::typ().with_message("eval calls not available").into())
/// Err(JsNativeError::typ()
/// .with_message("eval calls not available")
/// .into())
/// }
/// }
/// let hooks: &dyn HostHooks = &Hooks; // Can have additional state.
/// 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(), "TypeError: eval calls not available");
/// assert_eq!(
/// result.unwrap_err().to_string(),
/// "TypeError: eval calls not available"
/// );
/// ```
///
/// [`Host Hooks`]: https://tc39.es/ecma262/#sec-host-hooks-summary

3
boa_engine/src/context/mod.rs

@ -50,8 +50,7 @@ use crate::vm::RuntimeLimits;
/// use boa_engine::{
/// object::ObjectInitializer,
/// property::{Attribute, PropertyDescriptor},
/// Context,
/// Source
/// Context, Source,
/// };
///
/// let script = r#"

6
boa_engine/src/error.rs

@ -165,7 +165,8 @@ impl JsError {
/// ```rust
/// # use boa_engine::{Context, JsError, JsNativeError};
/// 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().borrow().is_error());
@ -345,7 +346,8 @@ 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());
///

5
boa_engine/src/job.rs

@ -253,7 +253,10 @@ pub trait JobQueue {
/// can be done by passing a reference to it to the [`ContextBuilder`]:
///
/// ```
/// use boa_engine::{context::ContextBuilder, job::{JobQueue, IdleJobQueue}};
/// use boa_engine::{
/// context::ContextBuilder,
/// job::{IdleJobQueue, JobQueue},
/// };
///
/// let queue: &dyn JobQueue = &IdleJobQueue;
/// let context = ContextBuilder::new().job_queue(queue).build();

1
boa_engine/src/lib.rs

@ -45,7 +45,6 @@
//!
//! [ecma-402]: https://tc39.es/ecma402
//! [examples]: https://github.com/boa-dev/boa/tree/main/boa_examples
//!
#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",

10
boa_engine/src/module/mod.rs

@ -628,7 +628,10 @@ impl Module {
/// # use boa_engine::module::{ModuleLoader, SimpleModuleLoader};
/// let loader = &SimpleModuleLoader::new(Path::new(".")).unwrap();
/// let dyn_loader: &dyn ModuleLoader = loader;
/// let mut context = &mut Context::builder().module_loader(dyn_loader).build().unwrap();
/// let mut context = &mut Context::builder()
/// .module_loader(dyn_loader)
/// .build()
/// .unwrap();
///
/// let source = Source::from_bytes("1 + 3");
///
@ -640,7 +643,10 @@ impl Module {
///
/// context.run_jobs();
///
/// assert_eq!(promise.state().unwrap(), PromiseState::Fulfilled(JsValue::undefined()));
/// assert_eq!(
/// promise.state().unwrap(),
/// PromiseState::Fulfilled(JsValue::undefined())
/// );
/// ```
#[allow(dropping_copy_types)]
#[inline]

3
boa_engine/src/object/builtins/jsdataview.rs

@ -27,7 +27,8 @@ 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(())
/// # }

17
boa_engine/src/object/builtins/jsdate.rs

@ -18,19 +18,22 @@ use crate::{
/// Create a `JsDate` object and set date to December 4 1995
///
/// ```
/// use boa_engine::{object::builtins::JsDate, Context, JsValue, JsResult};
/// use boa_engine::{object::builtins::JsDate, Context, JsResult, JsValue};
///
/// fn main() -> JsResult<()> {
/// // JS mutable Context
/// let context = &mut Context::default();
/// // JS mutable Context
/// let context = &mut Context::default();
///
/// let date = JsDate::new(context);
/// let date = JsDate::new(context);
///
/// date.set_full_year(&[1995.into(), 11.into(), 4.into()], context)?;
/// date.set_full_year(&[1995.into(), 11.into(), 4.into()], context)?;
///
/// assert_eq!(date.to_date_string(context)?, JsValue::from("Mon Dec 04 1995"));
/// assert_eq!(
/// date.to_date_string(context)?,
/// JsValue::from("Mon Dec 04 1995")
/// );
///
/// Ok(())
/// Ok(())
/// }
/// ```
#[derive(Debug, Clone, Trace, Finalize)]

167
boa_engine/src/object/builtins/jspromise.rs

@ -40,9 +40,11 @@ use boa_gc::{Finalize, Gc, GcRefCell, Trace};
/// 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,
@ -54,7 +56,10 @@ use boa_gc::{Finalize, Gc, GcRefCell, Trace};
/// FunctionObjectBuilder::new(
/// context.realm(),
/// 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())
/// }),
/// )
/// .build(),
@ -76,10 +81,12 @@ use boa_gc::{Finalize, Gc, GcRefCell, Trace};
/// FunctionObjectBuilder::new(
/// context.realm(),
/// NativeFunction::from_fn_ptr(|_, _, context| {
/// context
/// .global_object()
/// .clone()
/// .set("finally", JsValue::from(true), true, context)?;
/// context.global_object().clone().set(
/// "finally",
/// JsValue::from(true),
/// true,
/// context,
/// )?;
/// Ok(JsValue::undefined())
/// }),
/// )
@ -135,15 +142,25 @@ impl JsPromise {
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let context = &mut Context::default();
///
/// let promise = JsPromise::new(|resolvers, context| {
/// let result = js_string!("hello world").into();
/// resolvers.resolve.call(&JsValue::undefined(), &[result], context)?;
/// Ok(JsValue::undefined())
/// }, context)?;
/// let promise = JsPromise::new(
/// |resolvers, context| {
/// let result = js_string!("hello world").into();
/// resolvers.resolve.call(
/// &JsValue::undefined(),
/// &[result],
/// context,
/// )?;
/// Ok(JsValue::undefined())
/// },
/// context,
/// )?;
///
/// context.run_jobs();
///
/// assert_eq!(promise.state()?, PromiseState::Fulfilled(js_string!("hello world").into()));
/// assert_eq!(
/// promise.state()?,
/// PromiseState::Fulfilled(js_string!("hello world").into())
/// );
/// # Ok(())
/// # }
/// ```
@ -192,7 +209,9 @@ impl JsPromise {
///
/// assert_eq!(promise.state()?, PromiseState::Pending);
///
/// resolvers.reject.call(&JsValue::undefined(), &[5.into()], context)?;
/// resolvers
/// .reject
/// .call(&JsValue::undefined(), &[5.into()], context)?;
///
/// assert_eq!(promise.state()?, PromiseState::Rejected(5.into()));
///
@ -228,12 +247,17 @@ impl JsPromise {
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let context = &mut Context::default();
///
/// let promise = context.eval(Source::from_bytes("new Promise((resolve, reject) => resolve())"))?;
/// let promise = context.eval(Source::from_bytes(
/// "new Promise((resolve, reject) => resolve())",
/// ))?;
/// let promise = promise.as_object().cloned().unwrap();
///
/// let promise = JsPromise::from_object(promise)?;
///
/// assert_eq!(promise.state()?, PromiseState::Fulfilled(JsValue::undefined()));
/// assert_eq!(
/// promise.state()?,
/// PromiseState::Fulfilled(JsValue::undefined())
/// );
///
/// assert!(JsPromise::from_object(JsObject::with_null_proto()).is_err());
///
@ -327,7 +351,10 @@ impl JsPromise {
///
/// let promise = JsPromise::resolve(js_string!("resolved!"), context)?;
///
/// assert_eq!(promise.state()?, PromiseState::Fulfilled(js_string!("resolved!").into()));
/// assert_eq!(
/// promise.state()?,
/// PromiseState::Fulfilled(js_string!("resolved!").into())
/// );
///
/// # Ok(())
/// # }
@ -363,9 +390,15 @@ impl JsPromise {
/// # fn main() -> Result<(), Box<dyn Error>> {
/// 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()?, PromiseState::Rejected(js_string!("oops!").into()));
/// assert_eq!(
/// promise.state()?,
/// PromiseState::Rejected(js_string!("oops!").into())
/// );
///
/// # Ok(())
/// # }
@ -454,18 +487,23 @@ 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,
/// )?.then(
/// )?
/// .then(
/// Some(
/// FunctionObjectBuilder::new(
/// context.realm(),
/// NativeFunction::from_fn_ptr(|_, args, context| {
/// args.get_or_undefined(0).to_string(context).map(JsValue::from)
/// args.get_or_undefined(0)
/// .to_string(context)
/// .map(JsValue::from)
/// }),
/// )
/// .build(),
@ -521,17 +559,22 @@ 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,
/// )?.catch(
/// )?
/// .catch(
/// FunctionObjectBuilder::new(
/// context.realm(),
/// NativeFunction::from_fn_ptr(|_, args, context| {
/// args.get_or_undefined(0).to_string(context).map(JsValue::from)
/// args.get_or_undefined(0)
/// .to_string(context)
/// .map(JsValue::from)
/// }),
/// )
/// .build(),
@ -585,20 +628,25 @@ 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,
/// )?.finally(
/// )?
/// .finally(
/// FunctionObjectBuilder::new(
/// context.realm(),
/// NativeFunction::from_fn_ptr(|_, _, context| {
/// context
/// .global_object()
/// .clone()
/// .set("finally", JsValue::from(true), true, context)?;
/// context.global_object().clone().set(
/// "finally",
/// JsValue::from(true),
/// true,
/// context,
/// )?;
/// Ok(JsValue::undefined())
/// }),
/// )
@ -663,7 +711,12 @@ impl JsPromise {
///
/// context.run_jobs();
///
/// let array = promise1.state()?.as_fulfilled().and_then(JsValue::as_object).unwrap().clone();
/// let array = promise1
/// .state()?
/// .as_fulfilled()
/// .and_then(JsValue::as_object)
/// .unwrap()
/// .clone();
/// let array = JsArray::from_object(array)?;
/// assert_eq!(array.at(0, context)?, 0.into());
/// assert_eq!(array.at(1, context)?, 2.into());
@ -726,7 +779,12 @@ impl JsPromise {
///
/// context.run_jobs();
///
/// let array = promise.state()?.as_fulfilled().and_then(JsValue::as_object).unwrap().clone();
/// let array = promise
/// .state()?
/// .as_fulfilled()
/// .and_then(JsValue::as_object)
/// .unwrap()
/// .clone();
/// let array = JsArray::from_object(array)?;
///
/// let a = array.at(0, context)?.as_object().unwrap().clone();
@ -735,7 +793,10 @@ impl JsPromise {
///
/// let b = array.at(1, context)?.as_object().unwrap().clone();
/// assert_eq!(b.get("status", context)?, js_string!("rejected").into());
/// assert_eq!(b.get("reason", context)?.to_string(context)?, js_string!("TypeError"));
/// assert_eq!(
/// b.get("reason", context)?.to_string(context)?,
/// js_string!("TypeError")
/// );
///
/// let c = array.at(2, context)?.as_object().unwrap().clone();
/// assert_eq!(c.get("status", context)?, js_string!("fulfilled").into());
@ -788,7 +849,6 @@ impl JsPromise {
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let context = &mut Context::default();
///
///
/// let promise = JsPromise::any(
/// [
/// JsPromise::reject(JsNativeError::syntax(), context)?,
@ -801,7 +861,10 @@ impl JsPromise {
///
/// context.run_jobs();
///
/// assert_eq!(promise.state()?, PromiseState::Fulfilled(js_string!("fulfilled").into()));
/// assert_eq!(
/// promise.state()?,
/// PromiseState::Fulfilled(js_string!("fulfilled").into())
/// );
///
/// # Ok(())
/// # }
@ -856,8 +919,14 @@ impl JsPromise {
/// let promise = JsPromise::race([a, b, c], context)?;
///
/// resolvers_b.reject.call(&JsValue::undefined(), &[], context);
/// resolvers_a.resolve.call(&JsValue::undefined(), &[5.into()], context);
/// resolvers_c.reject.call(&JsValue::undefined(), &[js_string!("c error").into()], context);
/// resolvers_a
/// .resolve
/// .call(&JsValue::undefined(), &[5.into()], context);
/// resolvers_c.reject.call(
/// &JsValue::undefined(),
/// &[js_string!("c error").into()],
/// context,
/// );
///
/// context.run_jobs();
///
@ -913,12 +982,12 @@ impl JsPromise {
/// let (promise, resolvers) = JsPromise::new_pending(context);
/// let promise_future = promise.into_js_future(context)?;
///
/// let future1 = async move {
/// promise_future.await
/// };
/// let future1 = async move { promise_future.await };
///
/// let future2 = async move {
/// resolvers.resolve.call(&JsValue::undefined(), &[10.into()], context)?;
/// resolvers
/// .resolve
/// .call(&JsValue::undefined(), &[10.into()], context)?;
/// context.run_jobs();
/// Ok::<(), JsError>(())
/// };

1
boa_engine/src/object/builtins/jsregexp.rs

@ -35,7 +35,6 @@ use std::ops::Deref;
/// # Ok(())
/// # }
/// ```
///
#[derive(Debug, Clone, Trace, Finalize)]
pub struct JsRegExp {
inner: JsObject,

6
boa_engine/src/object/mod.rs

@ -2152,7 +2152,11 @@ impl<'realm> FunctionObjectBuilder<'realm> {
/// let object = ObjectInitializer::new(&mut context)
/// .property("hello", "world", Attribute::all())
/// .property(1, 1, Attribute::all())
/// .function(NativeFunction::from_fn_ptr(|_, _, _| Ok(JsValue::undefined())), "func", 0)
/// .function(
/// NativeFunction::from_fn_ptr(|_, _, _| Ok(JsValue::undefined())),
/// "func",
/// 0,
/// )
/// .build();
/// ```
///

2
boa_engine/src/string/mod.rs

@ -89,7 +89,7 @@ fn alloc_overflow() -> ! {
/// ```
/// # use boa_engine::js_string;
/// # use boa_engine::string::utf16;
/// const NAME: &[u16] = utf16!("human! ");
/// const NAME: &[u16] = utf16!("human! ");
/// let greeting = js_string!("Hello, ");
/// let msg = js_string!(&greeting, &NAME, utf16!("Nice to meet you!"));
///

1
boa_engine/src/vm/code_block.rs

@ -155,7 +155,6 @@ pub struct CodeBlock {
pub(crate) handlers: ThinVec<Handler>,
/// Compile time environments in this function.
///
// Safety: Nothing in CompileTimeEnvironment needs tracing, so this is safe.
//
// TODO(#3034): Maybe changing this to Gc after garbage collection would be better than Rc.

1
boa_engine/src/vm/opcode/mod.rs

@ -438,7 +438,6 @@ macro_rules! generate_opcodes {
///
/// This trait should be implemented for a struct that corresponds with
/// any arm of the `OpCode` enum.
///
pub(crate) trait Operation {
const NAME: &'static str;
const INSTRUCTION: &'static str;

1
boa_gc/src/lib.rs

@ -3,7 +3,6 @@
//! # Crate Overview
//! **`boa_gc`** is a mark-sweep garbage collector that implements a [`Trace`] and [`Finalize`] trait
//! for garbage collected values.
//!
#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",

1
boa_interner/src/lib.rs

@ -9,7 +9,6 @@
//! to `usize`, and also it's easier to store, since instead of a heap-allocated string, you only
//! need to store a `usize`. This reduces memory consumption and improves performance in the
//! compiler.
//!
#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",

1
boa_macros/src/lib.rs

@ -1,5 +1,4 @@
//! Macros for the Boa JavaScript engine.
//!
#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",

1
boa_parser/src/lib.rs

@ -9,7 +9,6 @@
//! [spec]: https://tc39.es/ecma262
//! [lex]: https://tc39.es/ecma262/#sec-ecmascript-language-lexical-grammar
//! [grammar]: https://tc39.es/ecma262/#sec-ecmascript-language-expressions
//!
#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",

2
boa_parser/src/parser/cursor/buffered_lexer/tests.rs

@ -266,7 +266,7 @@ fn skip_peeked_terminators() {
.unwrap()
.expect("Some value expected")
.kind(),
TokenKind::identifier(interner.get_or_intern_static("B", utf16!("B"))) // This value is after the line terminator
TokenKind::identifier(interner.get_or_intern_static("B", utf16!("B"))) /* This value is after the line terminator */
);
assert_eq!(

1
boa_profiler/src/lib.rs

@ -6,7 +6,6 @@
//! see Boa's page on [profiling][profiler-md].
//!
//! [profiler-md]: https://github.com/boa-dev/boa/blob/main/docs/profiling.md
//!
#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",

2
boa_runtime/src/lib.rs

@ -37,9 +37,7 @@
//! # panic!("An error occured in boa_runtime's js_code");
//! }
//! };
//!
//! ```
//!
#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",

1
boa_tester/src/main.rs

@ -2,7 +2,6 @@
//!
//! This crate will run the full ECMAScript test suite (Test262) and report compliance of the
//! `boa` engine.
//!
#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",

1
boa_wasm/src/lib.rs

@ -1,5 +1,4 @@
//! An ECMAScript WASM implementation based on boa_engine.
//!
#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",

Loading…
Cancel
Save