-
-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix regular expression construction (#3338)
* Fix regular expression construction The previous implementation regular expression (e.g. /abc/) used the global `RegExp`, which caused errors when the `RegExp` was overwritten. * Apply Review
- Loading branch information
Showing
11 changed files
with
217 additions
and
39 deletions.
There are no files selected for viewing
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,94 @@ | ||
//! This module contains the ECMAScript representation regular expressions. | ||
//! | ||
//! More information: | ||
//! - [ECMAScript reference][spec] | ||
//! - [MDN documentation][mdn] | ||
//! | ||
//! [spec]: https://tc39.es/ecma262/#sec-literals-regular-expression-literals | ||
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions | ||
use std::ops::ControlFlow; | ||
|
||
use boa_interner::{Interner, Sym, ToInternedString}; | ||
|
||
use crate::{ | ||
try_break, | ||
visitor::{VisitWith, Visitor, VisitorMut}, | ||
}; | ||
|
||
use super::Expression; | ||
|
||
/// Regular expressions in ECMAScript. | ||
/// | ||
/// More information: | ||
/// - [ECMAScript reference][spec] | ||
/// - [MDN documentation][mdn] | ||
/// | ||
/// [spec]: https://tc39.es/ecma262/#sec-literals-regular-expression-literals | ||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct RegExpLiteral { | ||
pattern: Sym, | ||
flags: Sym, | ||
} | ||
|
||
impl RegExpLiteral { | ||
/// Create a new [`RegExp`]. | ||
#[inline] | ||
#[must_use] | ||
pub const fn new(pattern: Sym, flags: Sym) -> Self { | ||
Self { pattern, flags } | ||
} | ||
|
||
/// Get the pattern part of the [`RegExp`]. | ||
#[inline] | ||
#[must_use] | ||
pub const fn pattern(&self) -> Sym { | ||
self.pattern | ||
} | ||
|
||
/// Get the flags part of the [`RegExp`]. | ||
#[inline] | ||
#[must_use] | ||
pub const fn flags(&self) -> Sym { | ||
self.flags | ||
} | ||
} | ||
|
||
impl ToInternedString for RegExpLiteral { | ||
#[inline] | ||
fn to_interned_string(&self, interner: &Interner) -> String { | ||
let pattern = interner.resolve_expect(self.pattern); | ||
let flags = interner.resolve_expect(self.flags); | ||
format!("/{pattern}/{flags}") | ||
} | ||
} | ||
|
||
impl From<RegExpLiteral> for Expression { | ||
#[inline] | ||
fn from(value: RegExpLiteral) -> Self { | ||
Self::RegExpLiteral(value) | ||
} | ||
} | ||
|
||
impl VisitWith for RegExpLiteral { | ||
#[inline] | ||
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy> | ||
where | ||
V: Visitor<'a>, | ||
{ | ||
try_break!(visitor.visit_sym(&self.pattern)); | ||
visitor.visit_sym(&self.flags) | ||
} | ||
|
||
#[inline] | ||
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy> | ||
where | ||
V: VisitorMut<'a>, | ||
{ | ||
try_break!(visitor.visit_sym_mut(&mut self.pattern)); | ||
visitor.visit_sym_mut(&mut self.flags) | ||
} | ||
} |
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
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
Oops, something went wrong.