Line data Source code
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 :
5 : #include "src/inspector/v8-regex.h"
6 :
7 : #include <limits.h>
8 :
9 : #include "src/inspector/string-util.h"
10 : #include "src/inspector/v8-inspector-impl.h"
11 :
12 : #include "include/v8-inspector.h"
13 :
14 : namespace v8_inspector {
15 :
16 125 : V8Regex::V8Regex(V8InspectorImpl* inspector, const String16& pattern,
17 : bool caseSensitive, bool multiline)
18 125 : : m_inspector(inspector) {
19 125 : v8::Isolate* isolate = m_inspector->isolate();
20 125 : v8::HandleScope handleScope(isolate);
21 125 : v8::Local<v8::Context> context = m_inspector->regexContext();
22 : v8::Context::Scope contextScope(context);
23 250 : v8::TryCatch tryCatch(isolate);
24 :
25 : unsigned flags = v8::RegExp::kNone;
26 125 : if (!caseSensitive) flags |= v8::RegExp::kIgnoreCase;
27 125 : if (multiline) flags |= v8::RegExp::kMultiline;
28 :
29 : v8::Local<v8::RegExp> regex;
30 125 : if (v8::RegExp::New(context, toV8String(isolate, pattern),
31 125 : static_cast<v8::RegExp::Flags>(flags))
32 125 : .ToLocal(®ex))
33 : m_regex.Reset(isolate, regex);
34 5 : else if (tryCatch.HasCaught())
35 15 : m_errorMessage = toProtocolString(isolate, tryCatch.Message()->Get());
36 : else
37 125 : m_errorMessage = "Internal error";
38 125 : }
39 :
40 505 : int V8Regex::match(const String16& string, int startFrom,
41 : int* matchLength) const {
42 505 : if (matchLength) *matchLength = 0;
43 :
44 1010 : if (m_regex.IsEmpty() || string.isEmpty()) return -1;
45 :
46 : // v8 strings are limited to int.
47 495 : if (string.length() > INT_MAX) return -1;
48 :
49 495 : v8::Isolate* isolate = m_inspector->isolate();
50 495 : v8::HandleScope handleScope(isolate);
51 495 : v8::Local<v8::Context> context = m_inspector->regexContext();
52 : v8::Context::Scope contextScope(context);
53 : v8::MicrotasksScope microtasks(isolate,
54 990 : v8::MicrotasksScope::kDoNotRunMicrotasks);
55 990 : v8::TryCatch tryCatch(isolate);
56 :
57 : v8::Local<v8::RegExp> regex = m_regex.Get(isolate);
58 : v8::Local<v8::Value> exec;
59 990 : if (!regex->Get(context, toV8StringInternalized(isolate, "exec"))
60 495 : .ToLocal(&exec))
61 : return -1;
62 : v8::Local<v8::Value> argv[] = {
63 990 : toV8String(isolate, string.substring(startFrom))};
64 : v8::Local<v8::Value> returnValue;
65 495 : if (!exec.As<v8::Function>()
66 495 : ->Call(context, regex, arraysize(argv), argv)
67 495 : .ToLocal(&returnValue))
68 : return -1;
69 :
70 : // RegExp#exec returns null if there's no match, otherwise it returns an
71 : // Array of strings with the first being the whole match string and others
72 : // being subgroups. The Array also has some random properties tacked on like
73 : // "index" which is the offset of the match.
74 : //
75 : // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/exec
76 :
77 : DCHECK(!returnValue.IsEmpty());
78 495 : if (!returnValue->IsArray()) return -1;
79 :
80 : v8::Local<v8::Array> result = returnValue.As<v8::Array>();
81 : v8::Local<v8::Value> matchOffset;
82 510 : if (!result->Get(context, toV8StringInternalized(isolate, "index"))
83 255 : .ToLocal(&matchOffset))
84 : return -1;
85 255 : if (matchLength) {
86 : v8::Local<v8::Value> match;
87 0 : if (!result->Get(context, 0).ToLocal(&match)) return -1;
88 0 : *matchLength = match.As<v8::String>()->Length();
89 : }
90 :
91 750 : return matchOffset.As<v8::Int32>()->Value() + startFrom;
92 : }
93 :
94 : } // namespace v8_inspector
|