forked from boa-dev/boa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for regex literals (boa-dev#94)
* Implement regex literal lexing * Add parser support for regex literals * Implement very basic RegExp object * Fix escaping a backslash * Store rust structs as internal state in objects * Remove unnecessary regexp constant * Implement RegExp.test() * Implement properties on RegExp * Implement RegExp.exec() * Implement RegExp.toString() * Rename RegularExpression to RegularExpressionLiteral
- Loading branch information
1 parent
6d81538
commit 5e7df4f
Showing
12 changed files
with
694 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! Implementations for storing normal rust structs inside any object as internal state. | ||
use std::{ | ||
any::Any, | ||
fmt::{self, Debug}, | ||
ops::{Deref, DerefMut}, | ||
rc::Rc, | ||
}; | ||
|
||
use gc::{unsafe_empty_trace, Finalize, Trace}; | ||
|
||
/// Wrapper around `Rc` to implement `Trace` and `Finalize`. | ||
#[derive(Clone)] | ||
pub struct InternalStateCell { | ||
/// The internal state. | ||
state: Rc<dyn Any>, | ||
} | ||
|
||
impl Finalize for InternalStateCell {} | ||
|
||
unsafe impl Trace for InternalStateCell { | ||
unsafe_empty_trace!(); | ||
} | ||
|
||
impl Deref for InternalStateCell { | ||
type Target = dyn Any; | ||
fn deref(&self) -> &Self::Target { | ||
Deref::deref(&self.state) | ||
} | ||
} | ||
|
||
impl DerefMut for InternalStateCell { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
Rc::get_mut(&mut self.state).expect("failed to get mutable") | ||
} | ||
} | ||
|
||
/// The derived version would print 'InternalStateCell { state: ... }', this custom implementation | ||
/// only prints the actual internal state. | ||
impl Debug for InternalStateCell { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
Debug::fmt(&self.state, f) | ||
} | ||
} | ||
|
||
impl InternalStateCell { | ||
/// Create new `InternalStateCell` from a value. | ||
pub fn new<T: Any + InternalState>(value: T) -> Self { | ||
Self { | ||
state: Rc::new(value), | ||
} | ||
} | ||
/// Get a reference to the stored value and cast it to `T`. | ||
pub fn downcast_ref<T: Any + InternalState>(&self) -> Option<&T> { | ||
self.deref().downcast_ref::<T>() | ||
} | ||
/// Get a mutable reference to the stored value and cast it to `T`. | ||
pub fn downcast_mut<T: Any + InternalState>(&mut self) -> Option<&mut T> { | ||
self.deref_mut().downcast_mut::<T>() | ||
} | ||
} | ||
|
||
/// This trait must be implemented by all structs used for internal state. | ||
pub trait InternalState: Debug {} |
Oops, something went wrong.