v8/
regexp.rs

1use crate::Context;
2use crate::Local;
3use crate::Object;
4use crate::RegExp;
5use crate::String;
6use crate::scope::PinScope;
7use crate::support::int;
8
9bitflags! {
10  #[derive(Debug, Clone, Copy, PartialEq, Eq)]
11  #[repr(transparent)]
12  pub struct RegExpCreationFlags: int {
13    const GLOBAL = 1 << 0;
14    const IGNORE_CASE = 1 << 1;
15    const MULTILINE = 1 << 2;
16    const STICKY = 1 << 3;
17    const UNICODE = 1 << 4;
18    const DOT_ALL = 1 << 5;
19    const LINEAR = 1 << 6;
20    const HAS_INDICES = 1 << 7;
21    const UNICODE_SETS = 1 << 8;
22  }
23}
24
25unsafe extern "C" {
26  fn v8__RegExp__New(
27    context: *const Context,
28    pattern: *const String,
29    flags: RegExpCreationFlags,
30  ) -> *const RegExp;
31  fn v8__RegExp__Exec(
32    this: *const RegExp,
33    context: *const Context,
34    subject: *const String,
35  ) -> *const Object;
36  fn v8__RegExp__GetSource(this: *const RegExp) -> *const String;
37}
38
39impl RegExp {
40  #[inline(always)]
41  pub fn new<'s>(
42    scope: &PinScope<'s, '_>,
43    pattern: Local<String>,
44    flags: RegExpCreationFlags,
45  ) -> Option<Local<'s, RegExp>> {
46    unsafe {
47      scope.cast_local(|sd| {
48        v8__RegExp__New(sd.get_current_context(), &*pattern, flags)
49      })
50    }
51  }
52
53  #[inline(always)]
54  pub fn exec<'s>(
55    &self,
56    scope: &PinScope<'s, '_>,
57    subject: Local<String>,
58  ) -> Option<Local<'s, Object>> {
59    unsafe {
60      scope.cast_local(|sd| {
61        v8__RegExp__Exec(self, sd.get_current_context(), &*subject)
62      })
63    }
64  }
65
66  #[inline(always)]
67  pub fn get_source<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, String> {
68    unsafe { scope.cast_local(|_| v8__RegExp__GetSource(self)) }.unwrap()
69  }
70}