pub struct JsRegExp { /* private fields */ }Expand description
JsRegExp provides a wrapper for Boa’s implementation of the ECMAScript RegExp builtin object
§Examples
Create a JsRegExp and run RegExp.prototype.test( String )
// Initialize the `Context`
let context = &mut Context::default();
// Create a new RegExp with pattern and flags
let regexp = JsRegExp::new(js_string!("foo"), js_string!("gi"), context)?;
let test_result = regexp.test(js_string!("football"), context)?;
assert!(test_result);
let to_string = regexp.to_string(context)?;
assert_eq!(to_string, String::from("/foo/gi"));Implementations§
Source§impl JsRegExp
impl JsRegExp
Sourcepub fn new<S>(pattern: S, flags: S, context: &mut Context) -> JsResult<Self>
pub fn new<S>(pattern: S, flags: S, context: &mut Context) -> JsResult<Self>
Create a new JsRegExp object
// Initialize the `Context`
let context = &mut Context::default();
// Create a new RegExp with pattern and flags
let regexp = JsRegExp::new(js_string!("foo"), js_string!("gi"), context)?;Sourcepub fn from_object(object: JsObject) -> JsResult<Self>
pub fn from_object(object: JsObject) -> JsResult<Self>
Create a JsRegExp from a regular expression JsObject
Sourcepub fn has_indices(&self, context: &mut Context) -> JsResult<bool>
pub fn has_indices(&self, context: &mut Context) -> JsResult<bool>
Returns a boolean value for whether the d flag is present in JsRegExp flags
Sourcepub fn global(&self, context: &mut Context) -> JsResult<bool>
pub fn global(&self, context: &mut Context) -> JsResult<bool>
Returns a boolean value for whether the g flag is present in JsRegExp flags
Sourcepub fn ignore_case(&self, context: &mut Context) -> JsResult<bool>
pub fn ignore_case(&self, context: &mut Context) -> JsResult<bool>
Returns a boolean value for whether the i flag is present in JsRegExp flags
Sourcepub fn multiline(&self, context: &mut Context) -> JsResult<bool>
pub fn multiline(&self, context: &mut Context) -> JsResult<bool>
Returns a boolean value for whether the m flag is present in JsRegExp flags
Sourcepub fn dot_all(&self, context: &mut Context) -> JsResult<bool>
pub fn dot_all(&self, context: &mut Context) -> JsResult<bool>
Returns a boolean value for whether the s flag is present in JsRegExp flags
Sourcepub fn unicode(&self, context: &mut Context) -> JsResult<bool>
pub fn unicode(&self, context: &mut Context) -> JsResult<bool>
Returns a boolean value for whether the u flag is present in JsRegExp flags
Sourcepub fn sticky(&self, context: &mut Context) -> JsResult<bool>
pub fn sticky(&self, context: &mut Context) -> JsResult<bool>
Returns a boolean value for whether the y flag is present in JsRegExp flags
Sourcepub fn flags(&self, context: &mut Context) -> JsResult<String>
pub fn flags(&self, context: &mut Context) -> JsResult<String>
Returns the flags of JsRegExp as a string
let regexp = JsRegExp::new(js_string!("foo"), js_string!("gi"), context)?;
let flags = regexp.flags(context)?;
assert_eq!(flags, String::from("gi"));Sourcepub fn source(&self, context: &mut Context) -> JsResult<String>
pub fn source(&self, context: &mut Context) -> JsResult<String>
Returns the source pattern of JsRegExp as a string
let regexp = JsRegExp::new(js_string!("foo"), js_string!("gi"), context)?;
let src = regexp.source(context)?;
assert_eq!(src, String::from("foo"));Sourcepub fn test<S>(&self, search_string: S, context: &mut Context) -> JsResult<bool>
pub fn test<S>(&self, search_string: S, context: &mut Context) -> JsResult<bool>
Executes a search for a match between JsRegExp and the provided string
let regexp = JsRegExp::new(js_string!("foo"), js_string!("gi"), context)?;
let test_result = regexp.test(js_string!("football"), context)?;
assert!(test_result);Methods from Deref<Target = JsObject>§
Sourcepub fn downcast_ref<T: NativeObject>(&self) -> Option<Ref<'_, T>>
pub fn downcast_ref<T: NativeObject>(&self) -> Option<Ref<'_, T>>
Downcasts a reference to the object,
if the object is of type T.
§Panics
Panics if the object is currently mutably borrowed.
§Examples
#[derive(Debug, Trace, Finalize, JsData)]
struct CustomStruct;
let obj = JsObject::from_proto_and_data(None, OrdinaryObject);
// Downcast ref succeeds for the correct type.
assert!(obj.downcast_ref::<OrdinaryObject>().is_some());
// Returns `None` for a wrong type.
assert!(obj.downcast_ref::<CustomStruct>().is_none());Sourcepub fn downcast_mut<T: NativeObject>(&self) -> Option<RefMut<'_, T>>
pub fn downcast_mut<T: NativeObject>(&self) -> Option<RefMut<'_, T>>
Downcasts a mutable reference to the object,
if the object is type native object type T.
§Panics
Panics if the object is currently borrowed.
§Examples
#[derive(Debug, Trace, Finalize, JsData)]
struct CustomStruct;
let obj = JsObject::from_proto_and_data(None, OrdinaryObject);
// Downcast mut succeeds for the correct type.
assert!(obj.downcast_mut::<OrdinaryObject>().is_some());
// Returns `None` for a wrong type.
assert!(obj.downcast_mut::<CustomStruct>().is_none());Sourcepub fn is<T: NativeObject>(&self) -> bool
pub fn is<T: NativeObject>(&self) -> bool
Checks if this object is an instance of a certain NativeObject.
§Panics
Panics if the object is currently mutably borrowed.
§Examples
#[derive(Debug, Trace, Finalize, JsData)]
struct CustomStruct;
let obj = JsObject::from_proto_and_data(None, OrdinaryObject);
assert!(obj.is::<OrdinaryObject>());
assert!(!obj.is::<CustomStruct>());Sourcepub fn is_ordinary(&self) -> bool
pub fn is_ordinary(&self) -> bool
Sourcepub fn is_array(&self) -> bool
pub fn is_array(&self) -> bool
Checks if it’s an Array object.
§Examples
let context = &mut Context::default();
let array = JsArray::new(context)?;
// A JsArray's inner JsObject is an array.
assert!(JsObject::from(array).is_array());
// An ordinary object is not an array.
let obj = JsObject::with_object_proto(context.intrinsics());
assert!(!obj.is_array());Sourcepub fn to_primitive(
&self,
context: &mut Context,
preferred_type: PreferredType,
) -> JsResult<JsValue>
pub fn to_primitive( &self, context: &mut Context, preferred_type: PreferredType, ) -> JsResult<JsValue>
The abstract operation ToPrimitive takes an input argument and an optional argument
PreferredType.
Sourcepub fn to_property_descriptor(
&self,
context: &mut Context,
) -> JsResult<PropertyDescriptor>
pub fn to_property_descriptor( &self, context: &mut Context, ) -> JsResult<PropertyDescriptor>
Sourcepub fn downgrade(&self) -> WeakJsObject<T>
pub fn downgrade(&self) -> WeakJsObject<T>
A weak handle to this object.
The counterpart to [WeakGc::upgrade], and the reason it is needed: a
host that caches wrappers by some native key has to be able to ask
whether script can still reach one. Holding the strong handle makes the
cache itself the answer, so the cache pins every object it has ever seen
and nothing keyed by it can ever be released.
A DOM binding is the motivating case. Wrappers are cached per node so
that === and expando properties behave, and with a strong cache the
document can never drop a removed node, because every node script has
touched looks reachable for ever.
Sourcepub fn borrow(&self) -> Ref<'_, Object<T>>
pub fn borrow(&self) -> Ref<'_, Object<T>>
Immutably borrows the Object.
The borrow lasts until the returned Ref exits scope.
Multiple immutable borrows can be taken out at the same time.
§Panics
Panics if the object is currently mutably borrowed.
§Examples
let obj = JsObject::from_proto_and_data(None, OrdinaryObject);
// Multiple immutable borrows are allowed.
let borrowed = obj.borrow();
assert!(borrowed.prototype().is_none());Sourcepub fn borrow_mut(&self) -> RefMut<'_, Object<T>>
pub fn borrow_mut(&self) -> RefMut<'_, Object<T>>
Mutably borrows the Object.
The borrow lasts until the returned RefMut exits scope.
The object cannot be borrowed while this borrow is active.
§Panics
Panics if the object is currently borrowed.
§Examples
let context = &mut Context::default();
let obj = JsObject::from_proto_and_data(
context.intrinsics().constructors().object().prototype(),
OrdinaryObject,
);
// Set the prototype to `None` via a mutable borrow.
obj.borrow_mut().set_prototype(None);
assert!(obj.prototype().is_none());Sourcepub fn try_borrow(&self) -> StdResult<Ref<'_, Object<T>>, BorrowError>
pub fn try_borrow(&self) -> StdResult<Ref<'_, Object<T>>, BorrowError>
Immutably borrows the Object, returning an error if the value is currently mutably borrowed.
The borrow lasts until the returned GcCellRef exits scope.
Multiple immutable borrows can be taken out at the same time.
This is the non-panicking variant of borrow.
§Examples
let obj = JsObject::from_proto_and_data(None, OrdinaryObject);
// Non-panicking immutable borrow.
let result = obj.try_borrow();
assert!(result.is_ok());Sourcepub fn try_borrow_mut(&self) -> StdResult<RefMut<'_, Object<T>>, BorrowMutError>
pub fn try_borrow_mut(&self) -> StdResult<RefMut<'_, Object<T>>, BorrowMutError>
Mutably borrows the object, returning an error if the value is currently borrowed.
The borrow lasts until the returned GcCellRefMut exits scope.
The object be borrowed while this borrow is active.
This is the non-panicking variant of borrow_mut.
§Examples
let obj = JsObject::from_proto_and_data(None, OrdinaryObject);
// Non-panicking mutable borrow.
let result = obj.try_borrow_mut();
assert!(result.is_ok());Sourcepub fn prototype(&self) -> JsPrototype
pub fn prototype(&self) -> JsPrototype
Get the prototype of the object.
§Panics
Panics if the object is currently mutably borrowed.
§Examples
let context = &mut Context::default();
let obj = JsObject::with_object_proto(context.intrinsics());
assert!(obj.prototype().is_some());
let null_obj = JsObject::with_null_proto();
assert!(null_obj.prototype().is_none());Sourcepub fn set_prototype(&self, prototype: JsPrototype) -> bool
pub fn set_prototype(&self, prototype: JsPrototype) -> bool
Set the prototype of the object.
§Panics
Panics if the object is currently mutably borrowed
§Examples
let context = &mut Context::default();
let obj = JsObject::with_object_proto(context.intrinsics());
assert!(obj.prototype().is_some());
// Set the prototype to `None`.
obj.set_prototype(None);
assert!(obj.prototype().is_none());Sourcepub fn insert_property<K, P>(&self, key: K, property: P) -> bool
pub fn insert_property<K, P>(&self, key: K, property: P) -> bool
Inserts a field in the object properties without checking if it’s writable.
If a field was already in the object with the same name, than true is returned
with that field, otherwise false is returned.
Sourcepub fn is_callable(&self) -> bool
pub fn is_callable(&self) -> bool
It determines if Object is a callable function with a [[Call]] internal method.
More information:
Sourcepub fn is_constructor(&self) -> bool
pub fn is_constructor(&self) -> bool
It determines if Object is a function object with a [[Construct]] internal method.
More information:
Sourcepub fn is_extensible(&self, context: &mut Context) -> JsResult<bool>
pub fn is_extensible(&self, context: &mut Context) -> JsResult<bool>
Sourcepub fn get<K>(&self, key: K, context: &mut Context) -> JsResult<JsValue>where
K: Into<PropertyKey>,
pub fn get<K>(&self, key: K, context: &mut Context) -> JsResult<JsValue>where
K: Into<PropertyKey>,
Sourcepub fn set<K, V>(
&self,
key: K,
value: V,
throw: bool,
context: &mut Context,
) -> JsResult<bool>
pub fn set<K, V>( &self, key: K, value: V, throw: bool, context: &mut Context, ) -> JsResult<bool>
Sourcepub fn create_data_property<K, V>(
&self,
key: K,
value: V,
context: &mut Context,
) -> JsResult<bool>
pub fn create_data_property<K, V>( &self, key: K, value: V, context: &mut Context, ) -> JsResult<bool>
Sourcepub fn create_data_property_or_throw<K, V>(
&self,
key: K,
value: V,
context: &mut Context,
) -> JsResult<bool>
pub fn create_data_property_or_throw<K, V>( &self, key: K, value: V, context: &mut Context, ) -> JsResult<bool>
Sourcepub fn define_property_or_throw<K, P>(
&self,
key: K,
desc: P,
context: &mut Context,
) -> JsResult<bool>
pub fn define_property_or_throw<K, P>( &self, key: K, desc: P, context: &mut Context, ) -> JsResult<bool>
Sourcepub fn delete_property_or_throw<K>(
&self,
key: K,
context: &mut Context,
) -> JsResult<bool>where
K: Into<PropertyKey>,
pub fn delete_property_or_throw<K>(
&self,
key: K,
context: &mut Context,
) -> JsResult<bool>where
K: Into<PropertyKey>,
Defines the property or throws a TypeError if the operation fails.
More information:
Sourcepub fn has_property<K>(&self, key: K, context: &mut Context) -> JsResult<bool>where
K: Into<PropertyKey>,
pub fn has_property<K>(&self, key: K, context: &mut Context) -> JsResult<bool>where
K: Into<PropertyKey>,
Sourcepub fn has_own_property<K>(
&self,
key: K,
context: &mut Context,
) -> JsResult<bool>where
K: Into<PropertyKey>,
pub fn has_own_property<K>(
&self,
key: K,
context: &mut Context,
) -> JsResult<bool>where
K: Into<PropertyKey>,
Sourcepub fn own_property_keys(
&self,
context: &mut Context,
) -> JsResult<Vec<PropertyKey>>
pub fn own_property_keys( &self, context: &mut Context, ) -> JsResult<Vec<PropertyKey>>
Sourcepub fn call(
&self,
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue>
pub fn call( &self, this: &JsValue, args: &[JsValue], context: &mut Context, ) -> JsResult<JsValue>
Call ( F, V [ , argumentsList ] )
§Panics
Panics if the object is currently mutably borrowed.
More information:
Sourcepub fn construct(
&self,
args: &[JsValue],
new_target: Option<&Self>,
context: &mut Context,
) -> JsResult<Self>
pub fn construct( &self, args: &[JsValue], new_target: Option<&Self>, context: &mut Context, ) -> JsResult<Self>
Construct ( F [ , argumentsList [ , newTarget ] ] )
Construct an instance of this object with the specified arguments.
§Panics
Panics if the object is currently mutably borrowed.
More information:
Sourcepub fn set_integrity_level(
&self,
level: IntegrityLevel,
context: &mut Context,
) -> JsResult<bool>
pub fn set_integrity_level( &self, level: IntegrityLevel, context: &mut Context, ) -> JsResult<bool>
Sourcepub fn test_integrity_level(
&self,
level: IntegrityLevel,
context: &mut Context,
) -> JsResult<bool>
pub fn test_integrity_level( &self, level: IntegrityLevel, context: &mut Context, ) -> JsResult<bool>
Sourcepub fn copy_data_properties<K>(
&self,
source: &JsValue,
excluded_keys: Vec<K>,
context: &mut Context,
) -> JsResult<()>where
K: Into<PropertyKey>,
pub fn copy_data_properties<K>(
&self,
source: &JsValue,
excluded_keys: Vec<K>,
context: &mut Context,
) -> JsResult<()>where
K: Into<PropertyKey>,
Trait Implementations§
Source§impl Trace for JsRegExp
impl Trace for JsRegExp
Source§unsafe fn trace_non_roots(&self)
unsafe fn trace_non_roots(&self)
Source§fn run_finalizer(&self)
fn run_finalizer(&self)
Finalize::finalize on this object and all
contained subobjects.Auto Trait Implementations§
impl !RefUnwindSafe for JsRegExp
impl !Send for JsRegExp
impl !Sync for JsRegExp
impl !UnwindSafe for JsRegExp
impl Freeze for JsRegExp
impl Unpin for JsRegExp
impl UnsafeUnpin for JsRegExp
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.