| Index: src/accessors.cc
|
| diff --git a/src/accessors.cc b/src/accessors.cc
|
| index b6a3a27c7fd72c55a838c135ac69468f15cde6ea..c108236b855703b6b3e0398d52fd862520c72eae 100644
|
| --- a/src/accessors.cc
|
| +++ b/src/accessors.cc
|
| @@ -322,6 +322,95 @@ 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> receiver =
|
| + Utils::OpenHandle(*v8::Local<v8::Value>(info.This()));
|
| + Handle<JSRegExp> regexp = Handle<JSRegExp>::cast(receiver);
|
| + Handle<String> pattern(regexp->Pattern(), isolate);
|
| + MaybeHandle<String> maybe = EscapeRegExpSource(isolate, pattern);
|
| +
|
| + Handle<String> result;
|
| + 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
|
| //
|
|
|