1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//! This module implements the global `RegExp` object.
//!
//! `The `RegExp` object is used for matching text with a pattern.
//!
//! More information:
//!  - [ECMAScript reference][spec]
//!  - [MDN documentation][mdn]
//!
//! [spec]: https://tc39.es/ecma262/#sec-regexp-constructor
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

use regex::Regex;

use super::function::{make_builtin_fn, make_constructor_fn};
use crate::{
    object::ObjectData,
    property::Property,
    value::{RcString, Value},
    BoaProfiler, Context, Result,
};
use gc::{unsafe_empty_trace, Finalize, Trace};

#[cfg(test)]
mod tests;

/// The internal representation on a `RegExp` object.
#[derive(Debug, Clone, Finalize)]
pub struct RegExp {
    /// Regex matcher.
    matcher: Regex,

    /// Update last_index, set if global or sticky flags are set.
    use_last_index: bool,

    /// String of parsed flags.
    flags: String,

    /// Flag 's' - dot matches newline characters.
    dot_all: bool,

    /// Flag 'g'
    global: bool,

    /// Flag 'i' - ignore case.
    ignore_case: bool,

    /// Flag 'm' - '^' and '$' match beginning/end of line.
    multiline: bool,

    /// Flag 'y'
    sticky: bool,

    /// Flag 'u' - Unicode.
    unicode: bool,

    pub(crate) original_source: String,
    original_flags: String,
}

unsafe impl Trace for RegExp {
    unsafe_empty_trace!();
}

impl RegExp {
    /// The name of the object.
    pub(crate) const NAME: &'static str = "RegExp";

    /// The amount of arguments this function object takes.
    pub(crate) const LENGTH: usize = 2;

    /// Create a new `RegExp`
    pub(crate) fn make_regexp(this: &Value, args: &[Value], _: &mut Context) -> Result<Value> {
        let arg = args.get(0).ok_or_else(Value::undefined)?;
        let mut regex_body = String::new();
        let mut regex_flags = String::new();
        match arg {
            Value::String(ref body) => {
                // first argument is a string -> use it as regex pattern
                regex_body = body.to_string();
            }
            Value::Object(ref obj) => {
                let obj = obj.borrow();
                if let Some(regex) = obj.as_regexp() {
                    // first argument is another `RegExp` object, so copy its pattern and flags
                    regex_body = regex.original_source.clone();
                    regex_flags = regex.original_flags.clone();
                }
            }
            _ => return Err(Value::undefined()),
        }
        // if a second argument is given and it's a string, use it as flags
        if let Some(Value::String(flags)) = args.get(1) {
            regex_flags = flags.to_string();
        }

        // parse flags
        let mut sorted_flags = String::new();
        let mut pattern = String::new();
        let mut dot_all = false;
        let mut global = false;
        let mut ignore_case = false;
        let mut multiline = false;
        let mut sticky = false;
        let mut unicode = false;
        if regex_flags.contains('g') {
            global = true;
            sorted_flags.push('g');
        }
        if regex_flags.contains('i') {
            ignore_case = true;
            sorted_flags.push('i');
            pattern.push('i');
        }
        if regex_flags.contains('m') {
            multiline = true;
            sorted_flags.push('m');
            pattern.push('m');
        }
        if regex_flags.contains('s') {
            dot_all = true;
            sorted_flags.push('s');
            pattern.push('s');
        }
        if regex_flags.contains('u') {
            unicode = true;
            sorted_flags.push('u');
            //pattern.push('s'); // rust uses utf-8 anyway
        }
        if regex_flags.contains('y') {
            sticky = true;
            sorted_flags.push('y');
        }
        // the `regex` crate uses '(?{flags})` inside the pattern to enable flags
        if !pattern.is_empty() {
            pattern = format!("(?{})", pattern);
        }
        pattern.push_str(regex_body.as_str());

        let matcher = Regex::new(pattern.as_str()).expect("failed to create matcher");
        let regexp = RegExp {
            matcher,
            use_last_index: global || sticky,
            flags: sorted_flags,
            dot_all,
            global,
            ignore_case,
            multiline,
            sticky,
            unicode,
            original_source: regex_body,
            original_flags: regex_flags,
        };

        this.set_data(ObjectData::RegExp(Box::new(regexp)));

        Ok(this.clone())
    }

    // /// `RegExp.prototype.dotAll`
    // ///
    // /// The `dotAll` property indicates whether or not the "`s`" flag is used with the regular expression.
    // ///
    // /// More information:
    // ///  - [ECMAScript reference][spec]
    // ///  - [MDN documentation][mdn]
    // ///
    // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.dotAll
    // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll
    // fn get_dot_all(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
    //     this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.dot_all)))
    // }

    // /// `RegExp.prototype.flags`
    // ///
    // /// The `flags` property returns a string consisting of the [`flags`][flags] of the current regular expression object.
    // ///
    // /// More information:
    // ///  - [ECMAScript reference][spec]
    // ///  - [MDN documentation][mdn]
    // ///
    // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.flags
    // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags
    // /// [flags]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Advanced_searching_with_flags_2
    // fn get_flags(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
    //     this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.flags.clone())))
    // }

    // /// `RegExp.prototype.global`
    // ///
    // /// The `global` property indicates whether or not the "`g`" flag is used with the regular expression.
    // ///
    // /// More information:
    // ///  - [ECMAScript reference][spec]
    // ///  - [MDN documentation][mdn]
    // ///
    // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.global
    // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global
    // fn get_global(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
    //     this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.global)))
    // }

    // /// `RegExp.prototype.ignoreCase`
    // ///
    // /// The `ignoreCase` property indicates whether or not the "`i`" flag is used with the regular expression.
    // ///
    // /// More information:
    // ///  - [ECMAScript reference][spec]
    // ///  - [MDN documentation][mdn]
    // ///
    // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.ignorecase
    // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase
    // fn get_ignore_case(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
    //     this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.ignore_case)))
    // }

    // /// `RegExp.prototype.multiline`
    // ///
    // /// The multiline property indicates whether or not the "m" flag is used with the regular expression.
    // ///
    // /// More information:
    // ///  - [ECMAScript reference][spec]
    // ///  - [MDN documentation][mdn]
    // ///
    // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.multiline
    // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline
    // fn get_multiline(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
    //     this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.multiline)))
    // }

    // /// `RegExp.prototype.source`
    // ///
    // /// The `source` property returns a `String` containing the source text of the regexp object,
    // /// and it doesn't contain the two forward slashes on both sides and any flags.
    // ///
    // /// More information:
    // ///  - [ECMAScript reference][spec]
    // ///  - [MDN documentation][mdn]
    // ///
    // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.source
    // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source
    // fn get_source(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
    //     Ok(this.get_internal_slot("OriginalSource"))
    // }

    // /// `RegExp.prototype.sticky`
    // ///
    // /// The `flags` property returns a string consisting of the [`flags`][flags] of the current regular expression object.
    // ///
    // /// More information:
    // ///  - [ECMAScript reference][spec]
    // ///  - [MDN documentation][mdn]
    // ///
    // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.sticky
    // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky
    // fn get_sticky(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
    //     this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.sticky)))
    // }

    // /// `RegExp.prototype.unicode`
    // ///
    // /// The unicode property indicates whether or not the "`u`" flag is used with a regular expression.
    // /// unicode is a read-only property of an individual regular expression instance.
    // ///
    // /// More information:
    // ///  - [ECMAScript reference][spec]
    // ///  - [MDN documentation][mdn]
    // ///
    // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.unicode
    // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode
    // fn get_unicode(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
    //     this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.unicode)))
    // }

    /// `RegExp.prototype.test( string )`
    ///
    /// The `test()` method executes a search for a match between a regular expression and a specified string.
    ///
    /// Returns `true` or `false`.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype.test
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
    pub(crate) fn test(this: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> {
        let arg_str = args
            .get(0)
            .expect("could not get argument")
            .to_string(ctx)?;
        let mut last_index = this.get_field("lastIndex").to_index(ctx)?;
        let result = if let Some(object) = this.as_object() {
            let regex = object.as_regexp().unwrap();
            let result = if let Some(m) = regex.matcher.find_at(arg_str.as_str(), last_index) {
                if regex.use_last_index {
                    last_index = m.end();
                }
                true
            } else {
                if regex.use_last_index {
                    last_index = 0;
                }
                false
            };
            Ok(Value::boolean(result))
        } else {
            panic!("object is not a regexp")
        };
        this.set_field("lastIndex", Value::from(last_index));
        result
    }

    /// `RegExp.prototype.exec( string )`
    ///
    /// The exec() method executes a search for a match in a specified string.
    ///
    /// Returns a result array, or `null`.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype.exec
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
    pub(crate) fn exec(this: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> {
        let arg_str = args
            .get(0)
            .expect("could not get argument")
            .to_string(ctx)?;
        let mut last_index = this.get_field("lastIndex").to_index(ctx)?;
        let result = if let Some(object) = this.as_object() {
            let regex = object.as_regexp().unwrap();
            let mut locations = regex.matcher.capture_locations();
            let result = if let Some(m) =
                regex
                    .matcher
                    .captures_read_at(&mut locations, arg_str.as_str(), last_index)
            {
                if regex.use_last_index {
                    last_index = m.end();
                }
                let mut result = Vec::with_capacity(locations.len());
                for i in 0..locations.len() {
                    if let Some((start, end)) = locations.get(i) {
                        result.push(Value::from(
                            arg_str.get(start..end).expect("Could not get slice"),
                        ));
                    } else {
                        result.push(Value::undefined());
                    }
                }

                let result = Value::from(result);
                result.set_property("index", Property::default().value(Value::from(m.start())));
                result.set_property("input", Property::default().value(Value::from(arg_str)));
                result
            } else {
                if regex.use_last_index {
                    last_index = 0;
                }
                Value::null()
            };
            Ok(result)
        } else {
            panic!("object is not a regexp")
        };
        this.set_field("lastIndex", Value::from(last_index));
        result
    }

    /// `RegExp.prototype[ @@match ]( string )`
    ///
    /// This method retrieves the matches when matching a string against a regular expression.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype-@@match
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@match
    pub(crate) fn r#match(this: &Value, arg: RcString, ctx: &mut Context) -> Result<Value> {
        let (matcher, flags) = if let Some(object) = this.as_object() {
            let regex = object.as_regexp().unwrap();
            (regex.matcher.clone(), regex.flags.clone())
        } else {
            panic!("object is not a regexp")
        };
        if flags.contains('g') {
            let mut matches = Vec::new();
            for mat in matcher.find_iter(&arg) {
                matches.push(Value::from(mat.as_str()));
            }
            if matches.is_empty() {
                return Ok(Value::null());
            }
            Ok(Value::from(matches))
        } else {
            Self::exec(this, &[Value::from(arg)], ctx)
        }
    }

    /// `RegExp.prototype.toString()`
    ///
    /// Return a string representing the regular expression.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype.tostring
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/toString
    #[allow(clippy::wrong_self_convention)]
    pub(crate) fn to_string(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
        let (body, flags) = if let Some(object) = this.as_object() {
            let regex = object.as_regexp().unwrap();
            (regex.original_source.clone(), regex.flags.clone())
        } else {
            panic!("object is not an object")
        };
        Ok(Value::from(format!("/{}/{}", body, flags)))
    }

    /// `RegExp.prototype[ @@matchAll ]( string )`
    ///
    /// The `[@@matchAll]` method returns all matches of the regular expression against a string.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-regexp-prototype-matchall
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll
    // TODO: it's returning an array, it should return an iterator
    pub(crate) fn match_all(this: &Value, arg_str: String) -> Result<Value> {
        let matches = if let Some(object) = this.as_object() {
            let regex = object.as_regexp().unwrap();
            let mut matches = Vec::new();

            for m in regex.matcher.find_iter(&arg_str) {
                if let Some(caps) = regex.matcher.captures(&m.as_str()) {
                    let match_vec = caps
                        .iter()
                        .map(|group| match group {
                            Some(g) => Value::from(g.as_str()),
                            None => Value::undefined(),
                        })
                        .collect::<Vec<Value>>();

                    let match_val = Value::from(match_vec);

                    match_val
                        .set_property("index", Property::default().value(Value::from(m.start())));
                    match_val.set_property(
                        "input",
                        Property::default().value(Value::from(arg_str.clone())),
                    );
                    matches.push(match_val);

                    if !regex.flags.contains('g') {
                        break;
                    }
                }
            }

            matches
        } else {
            panic!("object is not a regexp")
        };

        let length = matches.len();
        let result = Value::from(matches);
        result.set_field("length", Value::from(length));
        result.set_data(ObjectData::Array);

        Ok(result)
    }

    /// Initialise the `RegExp` object on the global object.
    #[inline]
    pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) {
        let _timer = BoaProfiler::global().start_event(Self::NAME, "init");
        let global = interpreter.global_object();

        // Create prototype
        let prototype = Value::new_object(Some(global));
        prototype
            .as_object_mut()
            .unwrap()
            .insert_field("lastIndex", Value::from(0));

        make_builtin_fn(Self::test, "test", &prototype, 1, interpreter);
        make_builtin_fn(Self::exec, "exec", &prototype, 1, interpreter);
        make_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter);

        // TODO: make them accessor properties, not methods.
        // make_builtin_fn(Self::get_dot_all, "dotAll", &prototype, 0);
        // make_builtin_fn(Self::get_flags, "flags", &prototype, 0);
        // make_builtin_fn(Self::get_global, "global", &prototype, 0);
        // make_builtin_fn(Self::get_ignore_case, "ignoreCase", &prototype, 0);
        // make_builtin_fn(Self::get_multiline, "multiline", &prototype, 0);
        // make_builtin_fn(Self::get_source, "source", &prototype, 0);
        // make_builtin_fn(Self::get_sticky, "sticky", &prototype, 0);
        // make_builtin_fn(Self::get_unicode, "unicode", &prototype, 0);

        let regexp = make_constructor_fn(
            Self::NAME,
            Self::LENGTH,
            Self::make_regexp,
            global,
            prototype,
            true,
            true,
        );

        (Self::NAME, regexp)
    }
}