Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Unified Diff: src/accessors.cc

Issue 1070093002: Eagerly escape RegExp.source. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/accessors.h ('k') | src/bootstrapper.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/accessors.cc
diff --git a/src/accessors.cc b/src/accessors.cc
index fd770ea16be5bc1befe01daab72b26d041c0010b..5ef8bf15f7e0d00e2724ed0d65748e19a9af2395 100644
--- a/src/accessors.cc
+++ b/src/accessors.cc
@@ -312,98 +312,6 @@ Handle<AccessorInfo> Accessors::StringLengthInfo(
}
-template <typename Char>
-inline int CountRequiredEscapes(Handle<String> source) {
- DisallowHeapAllocation no_gc;
- int escapes = 0;
- Vector<const Char> src = source->GetCharVector<Char>();
- for (int i = 0; i < src.length(); i++) {
- if (src[i] == '/' && (i == 0 || src[i - 1] != '\\')) escapes++;
- }
- return escapes;
-}
-
-
-template <typename Char, typename StringType>
-inline Handle<StringType> WriteEscapedRegExpSource(Handle<String> source,
- Handle<StringType> result) {
- DisallowHeapAllocation no_gc;
- Vector<const Char> src = source->GetCharVector<Char>();
- Vector<Char> dst(result->GetChars(), result->length());
- int s = 0;
- int d = 0;
- while (s < src.length()) {
- if (src[s] == '/' && (s == 0 || src[s - 1] != '\\')) dst[d++] = '\\';
- dst[d++] = src[s++];
- }
- DCHECK_EQ(result->length(), d);
- return result;
-}
-
-
-MaybeHandle<String> EscapeRegExpSource(Isolate* isolate,
- Handle<String> source) {
- String::Flatten(source);
- if (source->length() == 0) return isolate->factory()->query_colon_string();
- bool one_byte = source->IsOneByteRepresentationUnderneath();
- int escapes = one_byte ? CountRequiredEscapes<uint8_t>(source)
- : CountRequiredEscapes<uc16>(source);
- if (escapes == 0) return source;
- int length = source->length() + escapes;
- if (one_byte) {
- Handle<SeqOneByteString> result;
- ASSIGN_RETURN_ON_EXCEPTION(isolate, result,
- isolate->factory()->NewRawOneByteString(length),
- String);
- return WriteEscapedRegExpSource<uint8_t>(source, result);
- } else {
- Handle<SeqTwoByteString> result;
- ASSIGN_RETURN_ON_EXCEPTION(isolate, result,
- isolate->factory()->NewRawTwoByteString(length),
- String);
- return WriteEscapedRegExpSource<uc16>(source, result);
- }
-}
-
-
-// Implements ECMA262 ES6 draft 21.2.5.9
-void Accessors::RegExpSourceGetter(
- v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
- i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
- HandleScope scope(isolate);
-
- Handle<Object> holder =
- Utils::OpenHandle(*v8::Local<v8::Value>(info.Holder()));
- Handle<JSRegExp> regexp = Handle<JSRegExp>::cast(holder);
- Handle<String> result;
- if (regexp->TypeTag() == JSRegExp::NOT_COMPILED) {
- result = isolate->factory()->empty_string();
- } else {
- Handle<String> pattern(regexp->Pattern(), isolate);
- MaybeHandle<String> maybe = EscapeRegExpSource(isolate, pattern);
- if (!maybe.ToHandle(&result)) {
- isolate->OptionalRescheduleException(false);
- return;
- }
- }
- info.GetReturnValue().Set(Utils::ToLocal(result));
-}
-
-
-void Accessors::RegExpSourceSetter(v8::Local<v8::Name> name,
- v8::Local<v8::Value> value,
- const v8::PropertyCallbackInfo<void>& info) {
- UNREACHABLE();
-}
-
-
-Handle<AccessorInfo> Accessors::RegExpSourceInfo(
- Isolate* isolate, PropertyAttributes attributes) {
- return MakeAccessor(isolate, isolate->factory()->source_string(),
- &RegExpSourceGetter, &RegExpSourceSetter, attributes);
-}
-
-
//
// Accessors::ScriptColumnOffset
//
« no previous file with comments | « src/accessors.h ('k') | src/bootstrapper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698