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

Side by Side Diff: src/accessors.cc

Issue 736003002: Correctly escape RegExp source. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@regexpref
Patch Set: Created 6 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « src/accessors.h ('k') | src/bootstrapper.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/accessors.h" 7 #include "src/accessors.h"
8 #include "src/api.h" 8 #include "src/api.h"
9 #include "src/compiler.h" 9 #include "src/compiler.h"
10 #include "src/contexts.h" 10 #include "src/contexts.h"
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 Handle<AccessorInfo> Accessors::StringLengthInfo( 315 Handle<AccessorInfo> Accessors::StringLengthInfo(
316 Isolate* isolate, PropertyAttributes attributes) { 316 Isolate* isolate, PropertyAttributes attributes) {
317 return MakeAccessor(isolate, 317 return MakeAccessor(isolate,
318 isolate->factory()->length_string(), 318 isolate->factory()->length_string(),
319 &StringLengthGetter, 319 &StringLengthGetter,
320 &StringLengthSetter, 320 &StringLengthSetter,
321 attributes); 321 attributes);
322 } 322 }
323 323
324 324
325 template <typename Char>
326 inline int CountRequiredEscapes(Handle<String> source) {
327 DisallowHeapAllocation no_gc;
328 int escapes = 0;
329 Vector<const Char> src = source->GetCharVector<Char>();
330 for (int i = 0; i < src.length(); i++) {
331 if (src[i] == '/' && (i == 0 || src[i - 1] != '\\')) escapes++;
332 }
333 return escapes;
334 }
335
336
337 template <typename Char, typename StringType>
338 inline Handle<StringType> WriteEscapedRegExpSource(Handle<String> source,
339 Handle<StringType> result) {
340 DisallowHeapAllocation no_gc;
341 Vector<const Char> src = source->GetCharVector<Char>();
342 Vector<Char> dst(result->GetChars(), result->length());
343 int s = 0;
344 int d = 0;
345 while (s < src.length()) {
346 if (src[s] == '/' && (s == 0 || src[s - 1] != '\\')) dst[d++] = '\\';
347 dst[d++] = src[s++];
348 }
ulan 2014/11/21 09:19:34 DCHECK_EQ(result.length(), d);
349 return result;
350 }
351
352
353 MaybeHandle<String> EscapeRegExpSource(Isolate* isolate,
354 Handle<String> source) {
355 String::Flatten(source);
356 if (source->length() == 0) return isolate->factory()->query_colon_string();
357 bool one_byte = source->IsOneByteRepresentationUnderneath();
358 int escapes = one_byte ? CountRequiredEscapes<uint8_t>(source)
359 : CountRequiredEscapes<uc16>(source);
360 if (escapes == 0) return source;
361 int length = source->length() + escapes;
362 if (one_byte) {
363 Handle<SeqOneByteString> result;
364 ASSIGN_RETURN_ON_EXCEPTION(isolate, result,
365 isolate->factory()->NewRawOneByteString(length),
366 String);
367 return WriteEscapedRegExpSource<uint8_t>(source, result);
368 } else {
369 Handle<SeqTwoByteString> result;
370 ASSIGN_RETURN_ON_EXCEPTION(isolate, result,
371 isolate->factory()->NewRawTwoByteString(length),
372 String);
373 return WriteEscapedRegExpSource<uc16>(source, result);
374 }
375 }
376
377
378 // Implements ECMA262 ES6 draft 21.2.5.9
379 void Accessors::RegExpSourceGetter(
380 v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
381 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
382 HandleScope scope(isolate);
383
384 Handle<Object> receiver =
385 Utils::OpenHandle(*v8::Local<v8::Value>(info.This()));
386 Handle<JSRegExp> regexp = Handle<JSRegExp>::cast(receiver);
387 Handle<String> pattern(regexp->Pattern(), isolate);
388 MaybeHandle<String> maybe = EscapeRegExpSource(isolate, pattern);
389
390 Handle<String> result;
391 if (!maybe.ToHandle(&result)) {
392 isolate->OptionalRescheduleException(false);
393 return;
394 }
395 info.GetReturnValue().Set(Utils::ToLocal(result));
396 }
397
398
399 void Accessors::RegExpSourceSetter(v8::Local<v8::Name> name,
400 v8::Local<v8::Value> value,
401 const v8::PropertyCallbackInfo<void>& info) {
402 UNREACHABLE();
403 }
404
405
406 Handle<AccessorInfo> Accessors::RegExpSourceInfo(
407 Isolate* isolate, PropertyAttributes attributes) {
408 return MakeAccessor(isolate, isolate->factory()->source_string(),
409 &RegExpSourceGetter, &RegExpSourceSetter, attributes);
410 }
411
412
325 // 413 //
326 // Accessors::ScriptColumnOffset 414 // Accessors::ScriptColumnOffset
327 // 415 //
328 416
329 417
330 void Accessors::ScriptColumnOffsetGetter( 418 void Accessors::ScriptColumnOffsetGetter(
331 v8::Local<v8::Name> name, 419 v8::Local<v8::Name> name,
332 const v8::PropertyCallbackInfo<v8::Value>& info) { 420 const v8::PropertyCallbackInfo<v8::Value>& info) {
333 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); 421 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
334 DisallowHeapAllocation no_allocation; 422 DisallowHeapAllocation no_allocation;
(...skipping 1067 matching lines...) Expand 10 before | Expand all | Expand 10 after
1402 info->set_data(Smi::FromInt(index)); 1490 info->set_data(Smi::FromInt(index));
1403 Handle<Object> getter = v8::FromCData(isolate, &ModuleGetExport); 1491 Handle<Object> getter = v8::FromCData(isolate, &ModuleGetExport);
1404 Handle<Object> setter = v8::FromCData(isolate, &ModuleSetExport); 1492 Handle<Object> setter = v8::FromCData(isolate, &ModuleSetExport);
1405 info->set_getter(*getter); 1493 info->set_getter(*getter);
1406 if (!(attributes & ReadOnly)) info->set_setter(*setter); 1494 if (!(attributes & ReadOnly)) info->set_setter(*setter);
1407 return info; 1495 return info;
1408 } 1496 }
1409 1497
1410 1498
1411 } } // namespace v8::internal 1499 } } // namespace v8::internal
OLDNEW
« 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