From a932441a0cc85a89822a11a6e8011291979a1ca5 Mon Sep 17 00:00:00 2001 From: "lrn@chromium.org" Date: Tue, 12 Apr 2011 08:27:38 +0000 Subject: [PATCH] Cleanup of ScannerConstants, now named UnicodeCache. The ScannerConstants class was originally static fields on the scanner class. During creation of the stand-alone preparser and later isolates, it has been moved into a separate class with a per-isolate instance. It is used to hold caching unicode Predicate values. This change renames the class to UnicodeCache, and passes a reference to the instance down to methods that doesn't have an easy access to an isolate (to avoid, e.g., having to do an Isolate::Current() for every number parsed). Review URL: http://codereview.chromium.org/6824071 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@7584 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/conversions.cc | 83 ++++++++-------- src/conversions.h | 19 +++- src/dateparser-inl.h | 8 +- src/dateparser.h | 12 +-- src/heap.cc | 4 +- src/isolate.cc | 10 +- src/isolate.h | 10 +- src/objects.cc | 24 ++++- src/parser.cc | 10 +- src/parser.h | 2 +- src/preparser-api.cc | 10 +- src/runtime.cc | 18 ++-- src/scanner-base.cc | 58 ++++------- src/scanner-base.h | 19 ++-- src/scanner.cc | 11 ++- src/scanner.h | 8 +- test/cctest/test-conversions.cc | 207 +++++++++++++++++++++------------------- test/cctest/test-parsing.cc | 20 ++-- 18 files changed, 281 insertions(+), 252 deletions(-) diff --git a/src/conversions.cc b/src/conversions.cc index c3d7bdf..1458584 100644 --- a/src/conversions.cc +++ b/src/conversions.cc @@ -1,4 +1,4 @@ -// Copyright 2006-2008 the V8 project authors. All rights reserved. +// Copyright 2011 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: @@ -109,11 +109,11 @@ static const double JUNK_STRING_VALUE = OS::nan_value(); // Returns true if a nonspace found and false if the end has reached. template -static inline bool AdvanceToNonspace(ScannerConstants* scanner_constants, +static inline bool AdvanceToNonspace(UnicodeCache* unicode_cache, Iterator* current, EndMark end) { while (*current != end) { - if (!scanner_constants->IsWhiteSpace(**current)) return true; + if (!unicode_cache->IsWhiteSpace(**current)) return true; ++*current; } return false; @@ -134,7 +134,7 @@ static double SignedZero(bool negative) { // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end. template -static double InternalStringToIntDouble(ScannerConstants* scanner_constants, +static double InternalStringToIntDouble(UnicodeCache* unicode_cache, Iterator current, EndMark end, bool negative, @@ -161,7 +161,7 @@ static double InternalStringToIntDouble(ScannerConstants* scanner_constants, digit = static_cast(*current) - 'A' + 10; } else { if (allow_trailing_junk || - !AdvanceToNonspace(scanner_constants, ¤t, end)) { + !AdvanceToNonspace(unicode_cache, ¤t, end)) { break; } else { return JUNK_STRING_VALUE; @@ -193,7 +193,7 @@ static double InternalStringToIntDouble(ScannerConstants* scanner_constants, } if (!allow_trailing_junk && - AdvanceToNonspace(scanner_constants, ¤t, end)) { + AdvanceToNonspace(unicode_cache, ¤t, end)) { return JUNK_STRING_VALUE; } @@ -237,14 +237,14 @@ static double InternalStringToIntDouble(ScannerConstants* scanner_constants, template -static double InternalStringToInt(ScannerConstants* scanner_constants, +static double InternalStringToInt(UnicodeCache* unicode_cache, Iterator current, EndMark end, int radix) { const bool allow_trailing_junk = true; const double empty_string_val = JUNK_STRING_VALUE; - if (!AdvanceToNonspace(scanner_constants, ¤t, end)) { + if (!AdvanceToNonspace(unicode_cache, ¤t, end)) { return empty_string_val; } @@ -254,12 +254,12 @@ static double InternalStringToInt(ScannerConstants* scanner_constants, if (*current == '+') { // Ignore leading sign; skip following spaces. ++current; - if (!AdvanceToNonspace(scanner_constants, ¤t, end)) { + if (!AdvanceToNonspace(unicode_cache, ¤t, end)) { return JUNK_STRING_VALUE; } } else if (*current == '-') { ++current; - if (!AdvanceToNonspace(scanner_constants, ¤t, end)) { + if (!AdvanceToNonspace(unicode_cache, ¤t, end)) { return JUNK_STRING_VALUE; } negative = true; @@ -312,21 +312,21 @@ static double InternalStringToInt(ScannerConstants* scanner_constants, switch (radix) { case 2: return InternalStringToIntDouble<1>( - scanner_constants, current, end, negative, allow_trailing_junk); + unicode_cache, current, end, negative, allow_trailing_junk); case 4: return InternalStringToIntDouble<2>( - scanner_constants, current, end, negative, allow_trailing_junk); + unicode_cache, current, end, negative, allow_trailing_junk); case 8: return InternalStringToIntDouble<3>( - scanner_constants, current, end, negative, allow_trailing_junk); + unicode_cache, current, end, negative, allow_trailing_junk); case 16: return InternalStringToIntDouble<4>( - scanner_constants, current, end, negative, allow_trailing_junk); + unicode_cache, current, end, negative, allow_trailing_junk); case 32: return InternalStringToIntDouble<5>( - scanner_constants, current, end, negative, allow_trailing_junk); + unicode_cache, current, end, negative, allow_trailing_junk); default: UNREACHABLE(); } @@ -352,7 +352,7 @@ static double InternalStringToInt(ScannerConstants* scanner_constants, } if (!allow_trailing_junk && - AdvanceToNonspace(scanner_constants, ¤t, end)) { + AdvanceToNonspace(unicode_cache, ¤t, end)) { return JUNK_STRING_VALUE; } @@ -418,7 +418,7 @@ static double InternalStringToInt(ScannerConstants* scanner_constants, } while (!done); if (!allow_trailing_junk && - AdvanceToNonspace(scanner_constants, ¤t, end)) { + AdvanceToNonspace(unicode_cache, ¤t, end)) { return JUNK_STRING_VALUE; } @@ -432,7 +432,7 @@ static double InternalStringToInt(ScannerConstants* scanner_constants, // 2. *current - gets the current character in the sequence. // 3. ++current (advances the position). template -static double InternalStringToDouble(ScannerConstants* scanner_constants, +static double InternalStringToDouble(UnicodeCache* unicode_cache, Iterator current, EndMark end, int flags, @@ -445,7 +445,7 @@ static double InternalStringToDouble(ScannerConstants* scanner_constants, // 'parsing_done'. // 4. 'current' is not dereferenced after the 'parsing_done' label. // 5. Code before 'parsing_done' may rely on 'current != end'. - if (!AdvanceToNonspace(scanner_constants, ¤t, end)) { + if (!AdvanceToNonspace(unicode_cache, ¤t, end)) { return empty_string_val; } @@ -483,7 +483,7 @@ static double InternalStringToDouble(ScannerConstants* scanner_constants, } if (!allow_trailing_junk && - AdvanceToNonspace(scanner_constants, ¤t, end)) { + AdvanceToNonspace(unicode_cache, ¤t, end)) { return JUNK_STRING_VALUE; } @@ -505,7 +505,7 @@ static double InternalStringToDouble(ScannerConstants* scanner_constants, return JUNK_STRING_VALUE; // "0x". } - return InternalStringToIntDouble<4>(scanner_constants, + return InternalStringToIntDouble<4>(unicode_cache, current, end, negative, @@ -643,7 +643,7 @@ static double InternalStringToDouble(ScannerConstants* scanner_constants, } if (!allow_trailing_junk && - AdvanceToNonspace(scanner_constants, ¤t, end)) { + AdvanceToNonspace(unicode_cache, ¤t, end)) { return JUNK_STRING_VALUE; } @@ -651,7 +651,7 @@ static double InternalStringToDouble(ScannerConstants* scanner_constants, exponent += insignificant_digits; if (octal) { - return InternalStringToIntDouble<3>(scanner_constants, + return InternalStringToIntDouble<3>(unicode_cache, buffer, buffer + buffer_pos, negative, @@ -671,23 +671,22 @@ static double InternalStringToDouble(ScannerConstants* scanner_constants, } -double StringToDouble(String* str, int flags, double empty_string_val) { - ScannerConstants* scanner_constants = - Isolate::Current()->scanner_constants(); +double StringToDouble(UnicodeCache* unicode_cache, + String* str, int flags, double empty_string_val) { StringShape shape(str); if (shape.IsSequentialAscii()) { const char* begin = SeqAsciiString::cast(str)->GetChars(); const char* end = begin + str->length(); - return InternalStringToDouble(scanner_constants, begin, end, flags, + return InternalStringToDouble(unicode_cache, begin, end, flags, empty_string_val); } else if (shape.IsSequentialTwoByte()) { const uc16* begin = SeqTwoByteString::cast(str)->GetChars(); const uc16* end = begin + str->length(); - return InternalStringToDouble(scanner_constants, begin, end, flags, + return InternalStringToDouble(unicode_cache, begin, end, flags, empty_string_val); } else { StringInputBuffer buffer(str); - return InternalStringToDouble(scanner_constants, + return InternalStringToDouble(unicode_cache, StringInputBufferIterator(&buffer), StringInputBufferIterator::EndMarker(), flags, @@ -696,21 +695,21 @@ double StringToDouble(String* str, int flags, double empty_string_val) { } -double StringToInt(String* str, int radix) { - ScannerConstants* scanner_constants = - Isolate::Current()->scanner_constants(); +double StringToInt(UnicodeCache* unicode_cache, + String* str, + int radix) { StringShape shape(str); if (shape.IsSequentialAscii()) { const char* begin = SeqAsciiString::cast(str)->GetChars(); const char* end = begin + str->length(); - return InternalStringToInt(scanner_constants, begin, end, radix); + return InternalStringToInt(unicode_cache, begin, end, radix); } else if (shape.IsSequentialTwoByte()) { const uc16* begin = SeqTwoByteString::cast(str)->GetChars(); const uc16* end = begin + str->length(); - return InternalStringToInt(scanner_constants, begin, end, radix); + return InternalStringToInt(unicode_cache, begin, end, radix); } else { StringInputBuffer buffer(str); - return InternalStringToInt(scanner_constants, + return InternalStringToInt(unicode_cache, StringInputBufferIterator(&buffer), StringInputBufferIterator::EndMarker(), radix); @@ -718,22 +717,20 @@ double StringToInt(String* str, int radix) { } -double StringToDouble(const char* str, int flags, double empty_string_val) { - ScannerConstants* scanner_constants = - Isolate::Current()->scanner_constants(); +double StringToDouble(UnicodeCache* unicode_cache, + const char* str, int flags, double empty_string_val) { const char* end = str + StrLength(str); - return InternalStringToDouble(scanner_constants, str, end, flags, + return InternalStringToDouble(unicode_cache, str, end, flags, empty_string_val); } -double StringToDouble(Vector str, +double StringToDouble(UnicodeCache* unicode_cache, + Vector str, int flags, double empty_string_val) { - ScannerConstants* scanner_constants = - Isolate::Current()->scanner_constants(); const char* end = str.start() + str.length(); - return InternalStringToDouble(scanner_constants, str.start(), end, flags, + return InternalStringToDouble(unicode_cache, str.start(), end, flags, empty_string_val); } diff --git a/src/conversions.h b/src/conversions.h index 312e6ae..a14dc9a 100644 --- a/src/conversions.h +++ b/src/conversions.h @@ -1,4 +1,4 @@ -// Copyright 2006-2008 the V8 project authors. All rights reserved. +// Copyright 2011 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: @@ -28,6 +28,8 @@ #ifndef V8_CONVERSIONS_H_ #define V8_CONVERSIONS_H_ +#include "scanner-base.h" + namespace v8 { namespace internal { @@ -91,15 +93,22 @@ static inline uint32_t NumberToUint32(Object* number); // Converts a string into a double value according to ECMA-262 9.3.1 -double StringToDouble(String* str, int flags, double empty_string_val = 0); -double StringToDouble(Vector str, +double StringToDouble(UnicodeCache* unicode_cache, + String* str, + int flags, + double empty_string_val = 0); +double StringToDouble(UnicodeCache* unicode_cache, + Vector str, int flags, double empty_string_val = 0); // This version expects a zero-terminated character array. -double StringToDouble(const char* str, int flags, double empty_string_val = 0); +double StringToDouble(UnicodeCache* unicode_cache, + const char* str, + int flags, + double empty_string_val = 0); // Converts a string into an integer. -double StringToInt(String* str, int radix); +double StringToInt(UnicodeCache* unicode_cache, String* str, int radix); // Converts a double to a string value according to ECMA-262 9.8.1. // The buffer should be large enough for any floating point number. diff --git a/src/dateparser-inl.h b/src/dateparser-inl.h index ac28c62..7f8fac8 100644 --- a/src/dateparser-inl.h +++ b/src/dateparser-inl.h @@ -1,4 +1,4 @@ -// Copyright 2008 the V8 project authors. All rights reserved. +// Copyright 2011 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: @@ -34,9 +34,11 @@ namespace v8 { namespace internal { template -bool DateParser::Parse(Vector str, FixedArray* out) { +bool DateParser::Parse(Vector str, + FixedArray* out, + UnicodeCache* unicode_cache) { ASSERT(out->length() >= OUTPUT_SIZE); - InputReader in(str); + InputReader in(unicode_cache, str); TimeZoneComposer tz; TimeComposer time; DayComposer day; diff --git a/src/dateparser.h b/src/dateparser.h index 51109ee..9d29715 100644 --- a/src/dateparser.h +++ b/src/dateparser.h @@ -1,4 +1,4 @@ -// Copyright 2008 the V8 project authors. All rights reserved. +// Copyright 2011 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: @@ -49,7 +49,7 @@ class DateParser : public AllStatic { // [7]: UTC offset in seconds, or null value if no timezone specified // If parsing fails, return false (content of output array is not defined). template - static bool Parse(Vector str, FixedArray* output); + static bool Parse(Vector str, FixedArray* output, UnicodeCache* cache); enum { YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND, UTC_OFFSET, OUTPUT_SIZE @@ -67,11 +67,11 @@ class DateParser : public AllStatic { template class InputReader BASE_EMBEDDED { public: - explicit InputReader(Vector s) + InputReader(UnicodeCache* unicode_cache, Vector s) : index_(0), buffer_(s), has_read_number_(false), - scanner_constants_(Isolate::Current()->scanner_constants()) { + unicode_cache_(unicode_cache) { Next(); } @@ -122,7 +122,7 @@ class DateParser : public AllStatic { } bool SkipWhiteSpace() { - if (scanner_constants_->IsWhiteSpace(ch_)) { + if (unicode_cache_->IsWhiteSpace(ch_)) { Next(); return true; } @@ -158,7 +158,7 @@ class DateParser : public AllStatic { Vector buffer_; bool has_read_number_; uint32_t ch_; - ScannerConstants* scanner_constants_; + UnicodeCache* unicode_cache_; }; enum KeywordType { INVALID, MONTH_NAME, TIME_ZONE_NAME, AM_PM }; diff --git a/src/heap.cc b/src/heap.cc index c77364b..9a3cfe4 100644 --- a/src/heap.cc +++ b/src/heap.cc @@ -3386,8 +3386,8 @@ MaybeObject* Heap::AllocateStringFromUtf8Slow(Vector string, const uc32 kMaxSupportedChar = 0xFFFF; // Count the number of characters in the UTF-8 string and check if // it is an ASCII string. - Access - decoder(isolate_->scanner_constants()->utf8_decoder()); + Access + decoder(isolate_->unicode_cache()->utf8_decoder()); decoder->Reset(string.start(), string.length()); int chars = 0; while (decoder->has_more()) { diff --git a/src/isolate.cc b/src/isolate.cc index a6e667c..e42d78e 100644 --- a/src/isolate.cc +++ b/src/isolate.cc @@ -1,4 +1,4 @@ -// Copyright 2006-2010 the V8 project authors. All rights reserved. +// Copyright 2011 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: @@ -433,7 +433,7 @@ Isolate::Isolate() context_slot_cache_(NULL), descriptor_lookup_cache_(NULL), handle_scope_implementer_(NULL), - scanner_constants_(NULL), + unicode_cache_(NULL), in_use_list_(0), free_list_(0), preallocated_storage_preallocated_(false), @@ -568,8 +568,8 @@ Isolate::~Isolate() { producer_heap_profile_ = NULL; #endif - delete scanner_constants_; - scanner_constants_ = NULL; + delete unicode_cache_; + unicode_cache_ = NULL; delete regexp_stack_; regexp_stack_ = NULL; @@ -676,7 +676,7 @@ bool Isolate::PreInit() { keyed_lookup_cache_ = new KeyedLookupCache(); context_slot_cache_ = new ContextSlotCache(); descriptor_lookup_cache_ = new DescriptorLookupCache(); - scanner_constants_ = new ScannerConstants(); + unicode_cache_ = new UnicodeCache(); pc_to_code_cache_ = new PcToCodeCache(this); write_input_buffer_ = new StringInputBuffer(); global_handles_ = new GlobalHandles(this); diff --git a/src/isolate.h b/src/isolate.h index a372e54..01b721d 100644 --- a/src/isolate.h +++ b/src/isolate.h @@ -1,4 +1,4 @@ -// Copyright 2010 the V8 project authors. All rights reserved. +// Copyright 2011 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: @@ -72,7 +72,7 @@ class PreallocatedMemoryThread; class ProducerHeapProfile; class RegExpStack; class SaveContext; -class ScannerConstants; +class UnicodeCache; class StringInputBuffer; class StringTracker; class StubCache; @@ -819,8 +819,8 @@ class Isolate { } Zone* zone() { return &zone_; } - ScannerConstants* scanner_constants() { - return scanner_constants_; + UnicodeCache* unicode_cache() { + return unicode_cache_; } PcToCodeCache* pc_to_code_cache() { return pc_to_code_cache_; } @@ -1122,7 +1122,7 @@ class Isolate { DescriptorLookupCache* descriptor_lookup_cache_; v8::ImplementationUtilities::HandleScopeData handle_scope_data_; HandleScopeImplementer* handle_scope_implementer_; - ScannerConstants* scanner_constants_; + UnicodeCache* unicode_cache_; Zone zone_; PreallocatedStorage in_use_list_; PreallocatedStorage free_list_; diff --git a/src/objects.cc b/src/objects.cc index f91b586..6ce4c44 100644 --- a/src/objects.cc +++ b/src/objects.cc @@ -1,4 +1,4 @@ -// Copyright 2010 the V8 project authors. All rights reserved. +// Copyright 2011 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: @@ -1279,6 +1279,22 @@ MaybeObject* JSObject::AddFastPropertyUsingMap(Map* new_map, } +static bool IsIdentifier(UnicodeCache* cache, + unibrow::CharacterStream* buffer) { + // Checks whether the buffer contains an identifier (no escape). + if (!buffer->has_more()) return false; + if (!cache->IsIdentifierStart(buffer->GetNext())) { + return false; + } + while (buffer->has_more()) { + if (!cache->IsIdentifierPart(buffer->GetNext())) { + return false; + } + } + return true; +} + + MaybeObject* JSObject::AddFastProperty(String* name, Object* value, PropertyAttributes attributes) { @@ -1288,7 +1304,7 @@ MaybeObject* JSObject::AddFastProperty(String* name, // hidden symbols) and is not a real identifier. Isolate* isolate = GetHeap()->isolate(); StringInputBuffer buffer(name); - if (!isolate->scanner_constants()->IsIdentifier(&buffer) + if (!IsIdentifier(isolate->unicode_cache(), &buffer) && name != isolate->heap()->hidden_symbol()) { Object* obj; { MaybeObject* maybe_obj = @@ -5423,8 +5439,8 @@ bool String::MarkAsUndetectable() { bool String::IsEqualTo(Vector str) { Isolate* isolate = GetIsolate(); int slen = length(); - Access - decoder(isolate->scanner_constants()->utf8_decoder()); + Access + decoder(isolate->unicode_cache()->utf8_decoder()); decoder->Reset(str.start(), str.length()); int i; for (i = 0; i < slen && decoder->has_more(); i++) { diff --git a/src/parser.cc b/src/parser.cc index 4fad6e4..cf84bfa 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -576,7 +576,7 @@ Parser::Parser(Handle