Skip to content

Commit

Permalink
Added Regular Expression Formatting
Browse files Browse the repository at this point in the history
Updated Dependencies
  • Loading branch information
Redfire75369 committed Nov 2, 2023
1 parent f322aa8 commit 6b063a7
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 45 deletions.
75 changes: 40 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ resolver = "2"

[workspace.dependencies]
colored = "2.0.4"
chrono = "0.4.31"
derivative = "2.2.0"
dunce = "1.0.4"
futures = "0.3.29"
Expand All @@ -20,6 +19,11 @@ mozjs_sys = { package = "mozjs_sys", git = "https://github.com/servo/mozjs" }
sourcemap = "6.4.1"
url = "2.4.1"

[workspace.dependencies.chrono]
version = "0.4.31"
default-features = false
features = ["clock", "std"]

[workspace.dependencies.http]
version = "0.2.9"

Expand Down
1 change: 1 addition & 0 deletions ion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ authors = ["Redfire <[email protected]>"]
license = "MPL-2.0"

[dependencies]
arrayvec = "0.7.4"
bitflags = "2.4.1"
byteorder = "1.5.0"
bytemuck = "1.14.0"
Expand Down
3 changes: 2 additions & 1 deletion ion/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::fmt;
use std::fmt::{Display, Formatter};
use std::str;

use arrayvec::ArrayVec;
use bitflags::Flags;
use mozjs::jsapi::{
JSITER_FORAWAITOF, JSITER_HIDDEN, JSITER_OWNONLY, JSITER_PRIVATE, JSITER_SYMBOLS, JSITER_SYMBOLSONLY, JSPROP_ENUMERATE, JSPROP_PERMANENT,
Expand Down Expand Up @@ -84,7 +85,7 @@ bitflags! {

impl Display for RegExpFlags {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut flags = Vec::new();
let mut flags = ArrayVec::<_, 8>::new();
for flag in RegExpFlags::FLAGS {
if self.contains(*flag.value()) {
match *flag.value() {
Expand Down
3 changes: 3 additions & 0 deletions ion/src/format/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct ColourConfig {
pub object: Color,
pub date: Color,
pub promise: Color,
pub regexp: Color,
}

impl Default for ColourConfig {
Expand All @@ -79,6 +80,7 @@ impl Default for ColourConfig {
object: Color::White,
date: Color::White,
promise: Color::Yellow,
regexp: Color::Green,
}
}
}
Expand All @@ -98,6 +100,7 @@ impl ColourConfig {
object: Color::White,
date: Color::White,
promise: Color::White,
regexp: Color::White,
}
}
}
1 change: 1 addition & 0 deletions ion/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod key;
pub mod object;
pub mod primitive;
pub mod promise;
pub mod regexp;
pub mod symbol;

pub const INDENT: &str = " ";
Expand Down
4 changes: 3 additions & 1 deletion ion/src/format/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use colored::Colorize;
use mozjs::conversions::jsstr_to_string;
use mozjs::jsapi::{ESClass, JS_ValueToSource};

use crate::{Array, Context, Date, Exception, Function, Object, Promise};
use crate::{Array, Context, Date, Exception, Function, Object, Promise, RegExp};
use crate::conversions::ToValue;
use crate::format::{format_value, INDENT, NEWLINE};
use crate::format::array::format_array;
Expand All @@ -22,6 +22,7 @@ use crate::format::date::format_date;
use crate::format::function::format_function;
use crate::format::key::format_key;
use crate::format::promise::format_promise;
use crate::format::regexp::format_regexp;

/// Formats a [JavaScript Object](Object), depending on its class, as a string using the given [configuration](Config).
/// The object is passed to more specific formatting functions, such as [format_array] and [format_date].
Expand All @@ -37,6 +38,7 @@ pub fn format_object(cx: &Context, cfg: Config, object: Object) -> String {
ESC::Object => format_plain_object(cx, cfg, &Object::from(object.into_local())),
ESC::Date => format_date(cx, cfg, &Date::from(cx, object.into_local()).unwrap()),
ESC::Promise => format_promise(cx, cfg, &Promise::from(object.into_local()).unwrap()),
ESC::RegExp => format_regexp(cx, cfg, &RegExp::from(cx, object.into_local()).unwrap()),
ESC::Function => format_function(cx, cfg, &Function::from_object(cx, &object).unwrap()),
ESC::Other => format_class_object(cx, cfg, &object),
ESC::Error => {
Expand Down
16 changes: 16 additions & 0 deletions ion/src/format/regexp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

use colored::Colorize;

use crate::Context;
use crate::format::Config;
use crate::objects::RegExp;

/// Formats a [RegExp object](RegExp) as a string using the given [configuration](Config).
pub fn format_regexp(cx: &Context, cfg: Config, regexp: &RegExp) -> String {
regexp.to_string(cx).color(cfg.colours.regexp).to_string()
}
2 changes: 1 addition & 1 deletion ion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use future::PromiseFuture;
#[cfg(feature = "macros")]
pub use ion_proc::*;
pub use local::Local;
pub use objects::{Array, Date, Iterator, JSIterator, Object, OwnedKey, Promise, PropertyKey};
pub use objects::{Array, Date, Iterator, JSIterator, Object, OwnedKey, Promise, PropertyKey, RegExp};
pub use objects::typedarray;
pub use stack::{Stack, StackRecord};
pub use string::String;
Expand Down
16 changes: 12 additions & 4 deletions ion/src/objects/regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use std::ops::{Deref, DerefMut};
use std::ptr::NonNull;

use mozjs::jsapi::{
CheckRegExpSyntax, ExecuteRegExp, ExecuteRegExpNoStatics, GetRegExpFlags, GetRegExpSource, JSObject, NewUCRegExpObject, ObjectIsRegExp,
};
use mozjs::glue::JS_GetRegExpFlags;
use mozjs::jsapi::{CheckRegExpSyntax, ExecuteRegExp, ExecuteRegExpNoStatics, GetRegExpSource, JSObject, NewUCRegExpObject, ObjectIsRegExp};
use mozjs::jsapi::RegExpFlags as REFlags;

use crate::{Context, Local, Object, Value};
use crate::flags::RegExpFlags;
Expand Down Expand Up @@ -50,7 +50,15 @@ impl<'r> RegExp<'r> {
}

pub fn flags(&self, cx: &Context) -> RegExpFlags {
unsafe { GetRegExpFlags(cx.as_ptr(), self.handle().into()).into() }
let mut flags = REFlags { flags_: 0 };
unsafe {
JS_GetRegExpFlags(cx.as_ptr(), self.handle().into(), &mut flags);
}
flags.into()
}

pub fn to_string(&self, cx: &Context) -> String {
format!("/{}/{}", self.source(cx).to_owned(cx), self.flags(cx))
}

pub fn execute_test(&self, cx: &Context, string: &str, index: &mut usize) -> bool {
Expand Down
Loading

0 comments on commit 6b063a7

Please sign in to comment.