v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
v8-regex.cc
Go to the documentation of this file.
1// Copyright 2016 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <limits.h>
8
10#include "include/v8-context.h"
11#include "include/v8-function.h"
14#include "include/v8-regexp.h"
17
18namespace v8_inspector {
19
21 bool caseSensitive, bool multiline)
22 : m_inspector(inspector) {
23 v8::Isolate* isolate = m_inspector->isolate();
24 v8::HandleScope handleScope(isolate);
26 if (!m_inspector->regexContext().ToLocal(&context)) {
27 DCHECK(isolate->IsExecutionTerminating());
28 m_errorMessage = "terminated";
29 return;
30 }
31 v8::Context::Scope contextScope(context);
32 v8::TryCatch tryCatch(isolate);
33
34 unsigned flags = v8::RegExp::kNone;
35 if (!caseSensitive) flags |= v8::RegExp::kIgnoreCase;
36 if (multiline) flags |= v8::RegExp::kMultiline;
37
39 // Protect against reentrant debugger calls via interrupts.
41 if (v8::RegExp::New(context, toV8String(isolate, pattern),
42 static_cast<v8::RegExp::Flags>(flags))
43 .ToLocal(&regex))
44 m_regex.Reset(isolate, regex);
45 else if (tryCatch.HasCaught())
46 m_errorMessage = toProtocolString(isolate, tryCatch.Message()->Get());
47 else
48 m_errorMessage = "Internal error";
49}
50
51int V8Regex::match(const String16& string, int startFrom,
52 int* matchLength) const {
53 if (matchLength) *matchLength = 0;
54
55 if (m_regex.IsEmpty() || string.isEmpty()) return -1;
56
57 // v8 strings are limited to int.
58 if (string.length() > INT_MAX) return -1;
59
60 v8::Isolate* isolate = m_inspector->isolate();
61 v8::HandleScope handleScope(isolate);
63 if (!m_inspector->regexContext().ToLocal(&context)) {
64 DCHECK(isolate->IsExecutionTerminating());
65 return -1;
66 }
67 v8::Context::Scope contextScope(context);
68 v8::MicrotasksScope microtasks(context,
70 // Protect against reentrant debugger calls via interrupts.
72 v8::TryCatch tryCatch(isolate);
73
74 v8::Local<v8::RegExp> regex = m_regex.Get(isolate);
76 if (!regex->Get(context, toV8StringInternalized(isolate, "exec"))
77 .ToLocal(&exec))
78 return -1;
79 v8::Local<v8::Value> argv[] = {
80 toV8String(isolate, string.substring(startFrom))};
81 v8::Local<v8::Value> returnValue;
82 if (!exec.As<v8::Function>()
83 ->Call(context, regex, arraysize(argv), argv)
84 .ToLocal(&returnValue))
85 return -1;
86
87 // RegExp#exec returns null if there's no match, otherwise it returns an
88 // Array of strings with the first being the whole match string and others
89 // being subgroups. The Array also has some random properties tacked on like
90 // "index" which is the offset of the match.
91 //
92 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/exec
93
94 DCHECK(!returnValue.IsEmpty());
95 if (!returnValue->IsArray()) return -1;
96
97 v8::Local<v8::Array> result = returnValue.As<v8::Array>();
98 v8::Local<v8::Value> matchOffset;
99 if (!result->Get(context, toV8StringInternalized(isolate, "index"))
100 .ToLocal(&matchOffset))
101 return -1;
102 if (matchLength) {
104 if (!result->Get(context, 0).ToLocal(&match)) return -1;
105 *matchLength = match.As<v8::String>()->Length();
106 }
107
108 return matchOffset.As<v8::Int32>()->Value() + startFrom;
109}
110
111} // namespace v8_inspector
V8_WARN_UNUSED_RESULT MaybeLocal< Value > Call(v8::Isolate *isolate, Local< Context > context, Local< Value > recv, int argc, Local< Value > argv[])
Definition api.cc:5400
V8_INLINE Local< S > As() const
V8_INLINE Local< T > Get(Isolate *isolate) const
static V8_WARN_UNUSED_RESULT MaybeLocal< RegExp > New(Local< Context > context, Local< String > pattern, Flags flags)
Definition api.cc:8041
Local< v8::Message > Message() const
Definition api.cc:2829
bool HasCaught() const
Definition api.cc:2781
v8::Isolate * isolate() const
v8::MaybeLocal< v8::Context > regexContext()
V8Regex(V8InspectorImpl *, const String16 &, bool caseSensitive, bool multiline=false)
Definition v8-regex.cc:20
v8::Global< v8::RegExp > m_regex
Definition v8-regex.h:35
String16 m_errorMessage
Definition v8-regex.h:36
int match(const String16 &, int startFrom=0, int *matchLength=nullptr) const
Definition v8-regex.cc:51
V8InspectorImpl * m_inspector
Definition v8-regex.h:34
TNode< Context > context
std::string pattern
ZoneVector< RpoNumber > & result
String16 toProtocolString(v8::Isolate *isolate, v8::Local< v8::String > value)
v8::Local< v8::String > toV8String(v8::Isolate *isolate, const String16 &string)
v8::Local< v8::String > toV8StringInternalized(v8::Isolate *isolate, const String16 &string)
uint32_t substring
#define DCHECK(condition)
Definition logging.h:482
#define arraysize(array)
Definition macros.h:67
V8InspectorImpl * m_inspector