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 JsGenerator and wrapper docs clean up #2380

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion boa_engine/src/object/builtins/jsarray.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! This module implements a wrapper for the `Array` Builtin JavaScript Object
use crate::{
builtins::Array,
error::JsNativeError,
Expand All @@ -8,7 +9,7 @@ use crate::{
use boa_gc::{Finalize, Trace};
use std::ops::Deref;

/// JavaScript `Array` rust object.
/// `JsArray` provides a wrapper for Boa's implementation of the JavaScript `Array` object.
#[derive(Debug, Clone, Trace, Finalize)]
pub struct JsArray {
inner: JsObject,
Expand Down
3 changes: 2 additions & 1 deletion boa_engine/src/object/builtins/jsarraybuffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! This module implements a wrapper for the `ArrayBuffer` Builtin JavaScript Object
use crate::{
builtins::array_buffer::ArrayBuffer,
context::intrinsics::StandardConstructors,
Expand All @@ -10,7 +11,7 @@ use crate::{
use boa_gc::{Finalize, Trace};
use std::ops::Deref;

/// JavaScript `ArrayBuffer` rust object.
/// `JsArrayBuffer` provides a wrapper for Boa's implementation of the JavaScript `ArrayBuffer` object
#[derive(Debug, Clone, Trace, Finalize)]
pub struct JsArrayBuffer {
inner: JsObject,
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/object/builtins/jsdataview.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This module implements a wrapper for the `DataView` Builtin Javascript Object
//! This module implements a wrapper for the `DataView` Builtin JavaScript Object
use crate::{
builtins::DataView,
context::intrinsics::StandardConstructors,
Expand All @@ -12,7 +12,7 @@ use crate::{
use boa_gc::{Finalize, Trace};
use std::ops::Deref;

/// `JsDataView` Provides a wrapper for Boa's implementation of the Javascript `DataView` object
/// `JsDataView` Provides a wrapper for Boa's implementation of the JavaScript `DataView` object
///
/// # Examples
/// ```
Expand Down
1 change: 1 addition & 0 deletions boa_engine/src/object/builtins/jsfunction.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! This module implements a wrapper for the `Function` Builtin JavaScript Object
use crate::{
object::{JsObject, JsObjectType},
JsValue,
Expand Down
104 changes: 104 additions & 0 deletions boa_engine/src/object/builtins/jsgenerator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//! This module implements a wrapper for the `Generator` Builtin JavaScript Object
use crate::{
builtins::generator::{Generator, GeneratorState},
object::{JsObject, JsObjectType, ObjectData},
Context, JsNativeError, JsResult, JsValue,
};

use boa_gc::{Finalize, Trace};
use std::ops::Deref;

/// `JsGenerator` provides a wrapper for Boa's implementation of the JavaScript `Generator` builtin object
#[derive(Debug, Clone, Trace, Finalize)]
pub struct JsGenerator {
inner: JsObject,
}

impl JsGenerator {
/// Create a new `JsGenerator` object
#[inline]
pub fn new(context: &mut Context) -> Self {
let prototype = context.intrinsics().constructors().generator().prototype();

let generator = JsObject::from_proto_and_data(
prototype,
ObjectData::generator(Generator {
state: GeneratorState::Undefined,
context: None,
}),
);

Self { inner: generator }
}

/// Create a `JsGenerator` from a regular expression `JsObject`
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
if object.borrow().is_generator() {
Ok(Self { inner: object })
} else {
Err(JsNativeError::typ()
.with_message("object is not a Generator")
.into())
}
}

/// Calls `Generator.prototype.next()`
///
/// This method returns an object with the properties `done` and `value`
#[inline]
pub fn next<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue>
where
T: Into<JsValue>,
{
Generator::next(&self.inner.clone().into(), &[value.into()], context)
}

/// Calls `Generator.prototype.return()`
///
/// This method returns the given value and finishes the generator
#[inline]
pub fn r#return<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue>
where
T: Into<JsValue>,
{
Generator::r#return(&self.inner.clone().into(), &[value.into()], context)
}

/// Calls `Generator.prototype.throw()`
///
/// This method resumes the execution of a generator by throwing an error and returning an
/// an object with the properties `done` and `value`
#[inline]
pub fn throw<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue>
where
T: Into<JsValue>,
{
Generator::throw(&self.inner.clone().into(), &[value.into()], context)
}
}

impl From<JsGenerator> for JsObject {
#[inline]
fn from(o: JsGenerator) -> Self {
o.inner.clone()
}
}

impl From<JsGenerator> for JsValue {
#[inline]
fn from(o: JsGenerator) -> Self {
o.inner.clone().into()
}
}

impl Deref for JsGenerator {
type Target = JsObject;

#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl JsObjectType for JsGenerator {}
4 changes: 2 additions & 2 deletions boa_engine/src/object/builtins/jsmap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This module implements a wrapper for the Map Builtin Javascript Object
//! This module implements a wrapper for the `Map` Builtin JavaScript Object
use crate::{
builtins::map::{add_entries_from_iterable, ordered_map::OrderedMap},
builtins::Map,
Expand All @@ -10,7 +10,7 @@ use crate::{
use boa_gc::{Finalize, Trace};
use std::ops::Deref;

/// `JsMap` provides a wrapper for Boa's implementation of the Javascript `Map` object.
/// `JsMap` provides a wrapper for Boa's implementation of the JavaScript `Map` object.
///
/// # Examples
///
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/object/builtins/jsmap_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This module implements a wrapper for the `MapIterator` object
//! This module implements a wrapper for the `MapIterator` Builtin JavaScript Object
use crate::{
builtins::map::map_iterator::MapIterator,
error::JsNativeError,
Expand Down
1 change: 1 addition & 0 deletions boa_engine/src/object/builtins/jsproxy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! This module implements a wrapper for the `Proxy` Builtin JavaScript Object
use boa_gc::{Finalize, Trace};

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/object/builtins/jsregexp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This module implements a wrapper for the `RegExp` Builtin Javascript Object
//! This module implements a wrapper for the `RegExp` Builtin JavaScript Object
use crate::{
builtins::RegExp,
object::{JsArray, JsObject, JsObjectType},
Expand Down
3 changes: 2 additions & 1 deletion boa_engine/src/object/builtins/jsset.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! This module implements a wrapper for the `Set` Builtin JavaScript Object
use std::ops::Deref;

use boa_gc::{Finalize, Trace};
Expand All @@ -9,7 +10,7 @@ use crate::{
Context, JsResult, JsValue,
};

/// JavaScript `Set` rust object.
/// `JsSet` provides a wrapper for Boa's implementation of the JavaScript `Set` object.
#[derive(Debug, Clone, Trace, Finalize)]
pub struct JsSet {
inner: JsObject,
Expand Down
3 changes: 2 additions & 1 deletion boa_engine/src/object/builtins/jsset_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! This module implements a wrapper for the `SetIterator` Builtin JavaScript Object
use std::ops::Deref;

use boa_gc::{Finalize, Trace};
Expand All @@ -9,7 +10,7 @@ use crate::{
Context, JsResult, JsValue,
};

/// JavaScript `SetIterator` rust object
/// `JsSetIterator` provides a wrapper for Boa's implementation of the JavaScript `SetIterator` object
#[derive(Debug, Clone, Finalize, Trace)]
pub struct JsSetIterator {
inner: JsObject,
Expand Down
1 change: 1 addition & 0 deletions boa_engine/src/object/builtins/jstypedarray.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! This module implements a wrapper for the `TypedArray` Builtin JavaScript Object
use crate::{
builtins::typed_array::TypedArray,
error::JsNativeError,
Expand Down
2 changes: 2 additions & 0 deletions boa_engine/src/object/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod jsarray;
mod jsarraybuffer;
mod jsdataview;
mod jsfunction;
mod jsgenerator;
mod jsmap;
mod jsmap_iterator;
pub(crate) mod jsproxy;
Expand All @@ -16,6 +17,7 @@ pub use jsarray::*;
pub use jsarraybuffer::*;
pub use jsdataview::*;
pub use jsfunction::*;
pub use jsgenerator::*;
pub use jsmap::*;
pub use jsmap_iterator::*;
pub use jsproxy::{JsProxy, JsRevocableProxy};
Expand Down