From 8da9dbbb54b4e5afaf58924121f7308b4a0517c2 Mon Sep 17 00:00:00 2001 From: Ross McIlroy Date: Fri, 7 Sep 2018 10:17:06 +0100 Subject: [PATCH] [Parser] Add support for Zone allocated ConsumedPreParsingScopeData. Adds support for zone allocated (off-heap) ConsumedPreParsingScopeData to enable worker-thread access to PreParsingScopeData during parallel IIFE compile tasks. In order to avoid code-duplication, a templated BaseConsumedPreParsingScopeData is added which implements the logic for decoding the bytestream into scope data. Two implementations of this base class are instantiated for each of the underlying serialized scope date: - ZoneConsumedPreParsedScopeData for exposing ZonePreParsedScopeData - OnHeapConsumedPreParsedScopeData for exposing on-heap PreParsedScopeData The interface for each of these classes is the ConsumedPreParsingScopeData, which exposes the methods required by the parser to deserialize the required data. As a side-cleanup, moved Ucs2CharLength and Utf8LengthHelper implementations to cc file so that we don't get a linker error if one of them are unused by the cc file including the header. BUG=v8:8041 Change-Id: Id502312d32fe4a9ddb6f5d2d9d3e3a9d30b9b27d Reviewed-on: https://chromium-review.googlesource.com/1199462 Commit-Queue: Ross McIlroy Reviewed-by: Adam Klein Cr-Commit-Position: refs/heads/master@{#55711} --- BUILD.gn | 1 + src/compiler.cc | 12 +- src/parsing/parse-info.h | 10 +- src/parsing/parser.cc | 3 +- src/parsing/preparsed-scope-data-impl.h | 259 +++++++++++++++++++ src/parsing/preparsed-scope-data.cc | 318 ++++++++++++++---------- src/parsing/preparsed-scope-data.h | 126 +++------- src/zone/zone-chunk-list.h | 4 +- test/cctest/BUILD.gn | 1 + test/cctest/parsing/test-preparser.cc | 88 +++++-- test/cctest/unicode-helpers.cc | 31 +++ test/cctest/unicode-helpers.h | 27 +- 12 files changed, 601 insertions(+), 279 deletions(-) create mode 100644 src/parsing/preparsed-scope-data-impl.h create mode 100644 test/cctest/unicode-helpers.cc diff --git a/BUILD.gn b/BUILD.gn index c463ea8280..367a4ab9e3 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -2278,6 +2278,7 @@ v8_source_set("v8_base") { "src/parsing/parsing.cc", "src/parsing/parsing.h", "src/parsing/pattern-rewriter.cc", + "src/parsing/preparsed-scope-data-impl.h", "src/parsing/preparsed-scope-data.cc", "src/parsing/preparsed-scope-data.h", "src/parsing/preparser-logger.h", diff --git a/src/compiler.cc b/src/compiler.cc index efe58dfe03..6aa3648aad 100644 --- a/src/compiler.cc +++ b/src/compiler.cc @@ -933,7 +933,7 @@ std::unique_ptr CompileTopLevelOnBackgroundThread( stricter_language_mode(parse_info->language_mode(), language_mode)); // Can't access scope info data off-main-thread. - DCHECK(!parse_info->consumed_preparsed_scope_data()->HasData()); + DCHECK(!parse_info->consumed_preparsed_scope_data()); // Generate the unoptimized bytecode or asm-js data. std::unique_ptr outer_function_job( @@ -1086,10 +1086,12 @@ bool Compiler::Compile(Handle shared_info, if (FLAG_preparser_scope_analysis) { if (shared_info->HasUncompiledDataWithPreParsedScope()) { - parse_info.consumed_preparsed_scope_data()->SetData( - isolate, handle(shared_info->uncompiled_data_with_pre_parsed_scope() - ->pre_parsed_scope_data(), - isolate)); + parse_info.set_consumed_preparsed_scope_data( + ConsumedPreParsedScopeData::For( + isolate, + handle(shared_info->uncompiled_data_with_pre_parsed_scope() + ->pre_parsed_scope_data(), + isolate))); } } diff --git a/src/parsing/parse-info.h b/src/parsing/parse-info.h index dbe6ebf738..0d7c7370a2 100644 --- a/src/parsing/parse-info.h +++ b/src/parsing/parse-info.h @@ -12,6 +12,7 @@ #include "include/v8.h" #include "src/globals.h" #include "src/handles.h" +#include "src/objects/script.h" #include "src/parsing/preparsed-scope-data.h" #include "src/pending-compilation-error-handler.h" @@ -105,9 +106,12 @@ class V8_EXPORT_PRIVATE ParseInfo { v8::Extension* extension() const { return extension_; } void set_extension(v8::Extension* extension) { extension_ = extension; } - + void set_consumed_preparsed_scope_data( + std::unique_ptr data) { + consumed_preparsed_scope_data_.swap(data); + } ConsumedPreParsedScopeData* consumed_preparsed_scope_data() { - return &consumed_preparsed_scope_data_; + return consumed_preparsed_scope_data_.get(); } DeclarationScope* script_scope() const { return script_scope_; } @@ -266,7 +270,7 @@ class V8_EXPORT_PRIVATE ParseInfo { //----------- Inputs+Outputs of parsing and scope analysis ----------------- std::unique_ptr character_stream_; - ConsumedPreParsedScopeData consumed_preparsed_scope_data_; + std::unique_ptr consumed_preparsed_scope_data_; std::unique_ptr ast_value_factory_; const class AstStringConstants* ast_string_constants_; const AstRawString* function_name_; diff --git a/src/parsing/parser.cc b/src/parsing/parser.cc index 16339b1c2a..b7321b0f20 100644 --- a/src/parsing/parser.cc +++ b/src/parsing/parser.cc @@ -2731,8 +2731,7 @@ Parser::LazyParsingResult Parser::SkipFunction( scanner()->current_token() == Token::ARROW); // FIXME(marja): There are 2 ways to skip functions now. Unify them. - DCHECK_NOT_NULL(consumed_preparsed_scope_data_); - if (consumed_preparsed_scope_data_->HasData()) { + if (consumed_preparsed_scope_data_) { DCHECK(FLAG_preparser_scope_analysis); int end_position; LanguageMode language_mode; diff --git a/src/parsing/preparsed-scope-data-impl.h b/src/parsing/preparsed-scope-data-impl.h new file mode 100644 index 0000000000..e11a49fdfb --- /dev/null +++ b/src/parsing/preparsed-scope-data-impl.h @@ -0,0 +1,259 @@ +// Copyright 2018 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_PARSING_PREPARSED_SCOPE_DATA_IMPL_H_ +#define V8_PARSING_PREPARSED_SCOPE_DATA_IMPL_H_ + +#include "src/parsing/preparsed-scope-data.h" + +#include "src/assert-scope.h" + +namespace v8 { +namespace internal { + +// Classes which are internal to prepared-scope-data.cc, but are exposed in +// a header for tests. + +struct PreParsedScopeByteDataConstants { +#ifdef DEBUG + static constexpr int kMagicValue = 0xC0DE0DE; + + static constexpr size_t kUint32Size = 5; + static constexpr size_t kUint8Size = 2; + static constexpr size_t kQuarterMarker = 0; + static constexpr size_t kPlaceholderSize = kUint32Size; +#else + static constexpr size_t kUint32Size = 4; + static constexpr size_t kUint8Size = 1; + static constexpr size_t kPlaceholderSize = 0; +#endif + + static const size_t kSkippableFunctionDataSize = + 4 * kUint32Size + 1 * kUint8Size; +}; + +class ProducedPreParsedScopeData::ByteData + : public ZoneObject, + public PreParsedScopeByteDataConstants { + public: + explicit ByteData(Zone* zone) + : backing_store_(zone), free_quarters_in_last_byte_(0) {} + + void WriteUint32(uint32_t data); + void WriteUint8(uint8_t data); + void WriteQuarter(uint8_t data); + +#ifdef DEBUG + // For overwriting previously written data at position 0. + void OverwriteFirstUint32(uint32_t data); +#endif + + Handle> Serialize(Isolate* isolate); + + size_t size() const { return backing_store_.size(); } + + ZoneChunkList::iterator begin() { return backing_store_.begin(); } + + ZoneChunkList::iterator end() { return backing_store_.end(); } + + private: + ZoneChunkList backing_store_; + uint8_t free_quarters_in_last_byte_; +}; + +template +class BaseConsumedPreParsedScopeData : public ConsumedPreParsedScopeData { + public: + class ByteData : public PreParsedScopeByteDataConstants { + public: + ByteData() + : data_(nullptr), index_(0), stored_quarters_(0), stored_byte_(0) {} + + // Reading from the ByteData is only allowed when a ReadingScope is on the + // stack. This ensures that we have a DisallowHeapAllocation in place + // whenever ByteData holds a raw pointer into the heap. + class ReadingScope { + public: + ReadingScope(ByteData* consumed_data, Data* data) + : consumed_data_(consumed_data) { + consumed_data->data_ = data; + } + explicit ReadingScope(BaseConsumedPreParsedScopeData* parent) + : ReadingScope(parent->scope_data_.get(), parent->GetScopeData()) {} + ~ReadingScope() { consumed_data_->data_ = nullptr; } + + private: + ByteData* consumed_data_; + DisallowHeapAllocation no_gc; + }; + + void SetPosition(int position) { index_ = position; } + + size_t RemainingBytes() const { + DCHECK_NOT_NULL(data_); + return data_->length() - index_; + } + + int32_t ReadUint32() { + DCHECK_NOT_NULL(data_); + DCHECK_GE(RemainingBytes(), kUint32Size); +#ifdef DEBUG + // Check that there indeed is an integer following. + DCHECK_EQ(data_->get(index_++), kUint32Size); +#endif + int32_t result = 0; + byte* p = reinterpret_cast(&result); + for (int i = 0; i < 4; ++i) { + *p++ = data_->get(index_++); + } + stored_quarters_ = 0; + return result; + } + + uint8_t ReadUint8() { + DCHECK_NOT_NULL(data_); + DCHECK_GE(RemainingBytes(), kUint8Size); +#ifdef DEBUG + // Check that there indeed is a byte following. + DCHECK_EQ(data_->get(index_++), kUint8Size); +#endif + stored_quarters_ = 0; + return data_->get(index_++); + } + + uint8_t ReadQuarter() { + DCHECK_NOT_NULL(data_); + if (stored_quarters_ == 0) { + DCHECK_GE(RemainingBytes(), kUint8Size); +#ifdef DEBUG + // Check that there indeed are quarters following. + DCHECK_EQ(data_->get(index_++), kQuarterMarker); +#endif + stored_byte_ = data_->get(index_++); + stored_quarters_ = 4; + } + // Read the first 2 bits from stored_byte_. + uint8_t result = (stored_byte_ >> 6) & 3; + DCHECK_LE(result, 3); + --stored_quarters_; + stored_byte_ <<= 2; + return result; + } + + private: + Data* data_; + int index_; + uint8_t stored_quarters_; + uint8_t stored_byte_; + }; + + BaseConsumedPreParsedScopeData() + : scope_data_(new ByteData()), child_index_(0) {} + + virtual Data* GetScopeData() = 0; + + virtual ProducedPreParsedScopeData* GetChildData(Zone* zone, + int child_index) = 0; + + ProducedPreParsedScopeData* GetDataForSkippableFunction( + Zone* zone, int start_position, int* end_position, int* num_parameters, + int* num_inner_functions, bool* uses_super_property, + LanguageMode* language_mode) final; + + void RestoreScopeAllocationData(DeclarationScope* scope) final; + +#ifdef DEBUG + void VerifyDataStart(); +#endif + + private: + void RestoreData(Scope* scope); + void RestoreDataForVariable(Variable* var); + void RestoreDataForInnerScopes(Scope* scope); + + std::unique_ptr scope_data_; + // When consuming the data, these indexes point to the data we're going to + // consume next. + int child_index_; + + DISALLOW_COPY_AND_ASSIGN(BaseConsumedPreParsedScopeData); +}; + +// Implementation of ConsumedPreParsedScopeData for on-heap data. +class OnHeapConsumedPreParsedScopeData final + : public BaseConsumedPreParsedScopeData> { + public: + OnHeapConsumedPreParsedScopeData(Isolate* isolate, + Handle data); + + PodArray* GetScopeData() final; + ProducedPreParsedScopeData* GetChildData(Zone* zone, int child_index) final; + + private: + Isolate* isolate_; + Handle data_; +}; + +// Wraps a ZoneVector to have with functions named the same as +// PodArray. +class ZoneVectorWrapper { + public: + explicit ZoneVectorWrapper(ZoneVector* data) : data_(data) {} + + int length() const { return static_cast(data_->size()); } + + uint8_t get(int index) const { return data_->at(index); } + + private: + ZoneVector* data_; + + DISALLOW_COPY_AND_ASSIGN(ZoneVectorWrapper); +}; + +// A serialized PreParsedScopeData in zone memory (as apposed to being on-heap). +class ZonePreParsedScopeData : public ZoneObject { + public: + ZonePreParsedScopeData(Zone* zone, + ZoneChunkList::iterator byte_data_begin, + ZoneChunkList::iterator byte_data_end, + int child_length); + + Handle Serialize(Isolate* isolate); + + int child_length() const { return static_cast(children_.size()); } + + ZonePreParsedScopeData* get_child(int index) { return children_[index]; } + + void set_child(int index, ZonePreParsedScopeData* child) { + children_[index] = child; + } + + ZoneVector* byte_data() { return &byte_data_; } + + private: + ZoneVector byte_data_; + ZoneVector children_; + + DISALLOW_COPY_AND_ASSIGN(ZonePreParsedScopeData); +}; + +// Implementation of ConsumedPreParsedScopeData for PreParsedScopeData +// serialized into zone memory. +class ZoneConsumedPreParsedScopeData final + : public BaseConsumedPreParsedScopeData { + public: + ZoneConsumedPreParsedScopeData(Zone* zone, ZonePreParsedScopeData* data); + + ZoneVectorWrapper* GetScopeData() final; + ProducedPreParsedScopeData* GetChildData(Zone* zone, int child_index) final; + + private: + ZonePreParsedScopeData* data_; + ZoneVectorWrapper scope_data_wrapper_; +}; + +} // namespace internal +} // namespace v8 + +#endif // V8_PARSING_PREPARSED_SCOPE_DATA_IMPL_H_ diff --git a/src/parsing/preparsed-scope-data.cc b/src/parsing/preparsed-scope-data.cc index 90e8819e32..4d7cbf2de4 100644 --- a/src/parsing/preparsed-scope-data.cc +++ b/src/parsing/preparsed-scope-data.cc @@ -4,11 +4,14 @@ #include "src/parsing/preparsed-scope-data.h" +#include + #include "src/ast/scopes.h" #include "src/ast/variables.h" #include "src/handles.h" #include "src/objects-inl.h" #include "src/objects/shared-function-info.h" +#include "src/parsing/preparsed-scope-data-impl.h" #include "src/parsing/preparser.h" namespace v8 { @@ -24,22 +27,6 @@ class VariableMaybeAssignedField : public BitField8 {}; class VariableContextAllocatedField : public BitField8 {}; - -#ifdef DEBUG -const int kMagicValue = 0xC0DE0DE; - -const size_t kUint32Size = 5; -const size_t kUint8Size = 2; -const size_t kQuarterMarker = 0; -const size_t kPlaceholderSize = kUint32Size; -#else -const size_t kUint32Size = 4; -const size_t kUint8Size = 1; -const size_t kPlaceholderSize = 0; -#endif - -const size_t kSkippableFunctionDataSize = 4 * kUint32Size + 1 * kUint8Size; - class LanguageField : public BitField8 {}; class UsesSuperField : public BitField8 {}; STATIC_ASSERT(LanguageModeSize <= LanguageField::kNumValues); @@ -164,7 +151,8 @@ ProducedPreParsedScopeData::ProducedPreParsedScopeData( : parent_(parent), byte_data_(new (zone) ByteData(zone)), data_for_inner_functions_(zone), - bailed_out_(false) { + bailed_out_(false), + previously_produced_zone_preparsed_scope_data_(nullptr) { if (parent != nullptr) { parent->data_for_inner_functions_.push_back(this); } @@ -182,7 +170,18 @@ ProducedPreParsedScopeData::ProducedPreParsedScopeData( byte_data_(nullptr), data_for_inner_functions_(zone), bailed_out_(false), - previously_produced_preparsed_scope_data_(data) {} + previously_produced_on_heap_preparsed_scope_data_(data), + previously_produced_zone_preparsed_scope_data_(nullptr) {} + +// Create a ProducedPreParsedScopeData which is just a proxy for a previous +// produced PreParsedScopeData. +ProducedPreParsedScopeData::ProducedPreParsedScopeData( + ZonePreParsedScopeData* data, Zone* zone) + : parent_(nullptr), + byte_data_(nullptr), + data_for_inner_functions_(zone), + bailed_out_(false), + previously_produced_zone_preparsed_scope_data_(data) {} ProducedPreParsedScopeData::DataGatheringScope::DataGatheringScope( DeclarationScope* function_scope, PreParser* preparser) @@ -225,7 +224,8 @@ void ProducedPreParsedScopeData::AddSkippableFunction( int num_inner_functions, LanguageMode language_mode, bool uses_super_property) { DCHECK(FLAG_preparser_scope_analysis); - DCHECK(previously_produced_preparsed_scope_data_.is_null()); + DCHECK(previously_produced_on_heap_preparsed_scope_data_.is_null()); + DCHECK_NULL(previously_produced_zone_preparsed_scope_data_); if (bailed_out_) { return; @@ -248,12 +248,14 @@ void ProducedPreParsedScopeData::AddSkippableFunction( void ProducedPreParsedScopeData::SaveScopeAllocationData( DeclarationScope* scope) { DCHECK(FLAG_preparser_scope_analysis); - DCHECK(previously_produced_preparsed_scope_data_.is_null()); + DCHECK(previously_produced_on_heap_preparsed_scope_data_.is_null()); + DCHECK_NULL(previously_produced_zone_preparsed_scope_data_); // The data contains a uint32 (reserved space for scope_data_start) and // function data items, kSkippableFunctionDataSize each. - DCHECK_GE(byte_data_->size(), kPlaceholderSize); + DCHECK_GE(byte_data_->size(), ByteData::kPlaceholderSize); DCHECK_LE(byte_data_->size(), std::numeric_limits::max()); - DCHECK_EQ(byte_data_->size() % kSkippableFunctionDataSize, kPlaceholderSize); + DCHECK_EQ(byte_data_->size() % ByteData::kSkippableFunctionDataSize, + ByteData::kPlaceholderSize); if (bailed_out_) { return; @@ -262,7 +264,7 @@ void ProducedPreParsedScopeData::SaveScopeAllocationData( uint32_t scope_data_start = static_cast(byte_data_->size()); // If there are no skippable inner functions, we don't need to save anything. - if (scope_data_start == kPlaceholderSize) { + if (scope_data_start == ByteData::kPlaceholderSize) { return; } @@ -271,7 +273,7 @@ void ProducedPreParsedScopeData::SaveScopeAllocationData( // For a data integrity check, write a value between data about skipped inner // funcs and data about variables. - byte_data_->WriteUint32(kMagicValue); + byte_data_->WriteUint32(ByteData::kMagicValue); byte_data_->WriteUint32(scope->start_position()); byte_data_->WriteUint32(scope->end_position()); #endif @@ -280,23 +282,30 @@ void ProducedPreParsedScopeData::SaveScopeAllocationData( } bool ProducedPreParsedScopeData::ContainsInnerFunctions() const { - return byte_data_->size() > kPlaceholderSize; + return byte_data_->size() > ByteData::kPlaceholderSize; } MaybeHandle ProducedPreParsedScopeData::Serialize( Isolate* isolate) { - if (!previously_produced_preparsed_scope_data_.is_null()) { + if (!previously_produced_on_heap_preparsed_scope_data_.is_null()) { DCHECK(!bailed_out_); DCHECK_EQ(data_for_inner_functions_.size(), 0); - return previously_produced_preparsed_scope_data_; + return previously_produced_on_heap_preparsed_scope_data_; + } else if (previously_produced_zone_preparsed_scope_data_ != nullptr) { + DCHECK(!bailed_out_); + DCHECK_EQ(data_for_inner_functions_.size(), 0); + previously_produced_on_heap_preparsed_scope_data_ = + previously_produced_zone_preparsed_scope_data_->Serialize(isolate); + return previously_produced_on_heap_preparsed_scope_data_; } + if (bailed_out_) { return MaybeHandle(); } DCHECK(!ThisOrParentBailedOut()); - if (byte_data_->size() <= kPlaceholderSize) { + if (byte_data_->size() <= ByteData::kPlaceholderSize) { // The data contains only the placeholder. return MaybeHandle(); } @@ -322,6 +331,40 @@ MaybeHandle ProducedPreParsedScopeData::Serialize( return data; } +ZonePreParsedScopeData* ProducedPreParsedScopeData::Serialize(Zone* zone) { + DCHECK(previously_produced_on_heap_preparsed_scope_data_.is_null()); + + if (previously_produced_zone_preparsed_scope_data_ != nullptr) { + DCHECK(!bailed_out_); + DCHECK_EQ(data_for_inner_functions_.size(), 0); + return previously_produced_zone_preparsed_scope_data_; + } + + if (bailed_out_) { + return nullptr; + } + + DCHECK(!ThisOrParentBailedOut()); + + if (byte_data_->size() <= ByteData::kPlaceholderSize) { + // The data contains only the placeholder. + return nullptr; + } + + int child_length = static_cast(data_for_inner_functions_.size()); + ZonePreParsedScopeData* result = new (zone) ZonePreParsedScopeData( + zone, byte_data_->begin(), byte_data_->end(), child_length); + + int i = 0; + for (const auto& item : data_for_inner_functions_) { + ZonePreParsedScopeData* child = item->Serialize(zone); + result->set_child(i, child); + i++; + } + + return result; +} + bool ProducedPreParsedScopeData::ScopeNeedsData(Scope* scope) { if (scope->scope_type() == ScopeType::FUNCTION_SCOPE) { // Default constructors don't need data (they cannot contain inner functions @@ -431,91 +474,16 @@ void ProducedPreParsedScopeData::SaveDataForInnerScopes(Scope* scope) { } } -ConsumedPreParsedScopeData::ByteData::ReadingScope::ReadingScope( - ConsumedPreParsedScopeData* parent) - : ReadingScope(parent->scope_data_.get(), parent->data_->scope_data()) {} - -int32_t ConsumedPreParsedScopeData::ByteData::ReadUint32() { - DCHECK_NOT_NULL(data_); - DCHECK_GE(RemainingBytes(), kUint32Size); -#ifdef DEBUG - // Check that there indeed is an integer following. - DCHECK_EQ(data_->get(index_++), kUint32Size); -#endif - int32_t result = 0; - byte* p = reinterpret_cast(&result); - for (int i = 0; i < 4; ++i) { - *p++ = data_->get(index_++); - } - stored_quarters_ = 0; - return result; -} - -uint8_t ConsumedPreParsedScopeData::ByteData::ReadUint8() { - DCHECK_NOT_NULL(data_); - DCHECK_GE(RemainingBytes(), kUint8Size); -#ifdef DEBUG - // Check that there indeed is a byte following. - DCHECK_EQ(data_->get(index_++), kUint8Size); -#endif - stored_quarters_ = 0; - return data_->get(index_++); -} - -uint8_t ConsumedPreParsedScopeData::ByteData::ReadQuarter() { - DCHECK_NOT_NULL(data_); - if (stored_quarters_ == 0) { - DCHECK_GE(RemainingBytes(), kUint8Size); -#ifdef DEBUG - // Check that there indeed are quarters following. - DCHECK_EQ(data_->get(index_++), kQuarterMarker); -#endif - stored_byte_ = data_->get(index_++); - stored_quarters_ = 4; - } - // Read the first 2 bits from stored_byte_. - uint8_t result = (stored_byte_ >> 6) & 3; - DCHECK_LE(result, 3); - --stored_quarters_; - stored_byte_ <<= 2; - return result; -} - -size_t ConsumedPreParsedScopeData::ByteData::RemainingBytes() const { - DCHECK_NOT_NULL(data_); - return data_->length() - index_; -} - -ConsumedPreParsedScopeData::ConsumedPreParsedScopeData() - : isolate_(nullptr), scope_data_(new ByteData()), child_index_(0) {} - -ConsumedPreParsedScopeData::~ConsumedPreParsedScopeData() {} - -void ConsumedPreParsedScopeData::SetData(Isolate* isolate, - Handle data) { - DCHECK_NOT_NULL(isolate); - DCHECK(data->IsPreParsedScopeData()); - isolate_ = isolate; - data_ = data; -#ifdef DEBUG - ByteData::ReadingScope reading_scope(this); - int scope_data_start = scope_data_->ReadUint32(); - scope_data_->SetPosition(scope_data_start); - DCHECK_EQ(scope_data_->ReadUint32(), kMagicValue); - // The first data item is scope_data_start. Skip over it. - scope_data_->SetPosition(kPlaceholderSize); -#endif -} - +template ProducedPreParsedScopeData* -ConsumedPreParsedScopeData::GetDataForSkippableFunction( +BaseConsumedPreParsedScopeData::GetDataForSkippableFunction( Zone* zone, int start_position, int* end_position, int* num_parameters, int* num_inner_functions, bool* uses_super_property, LanguageMode* language_mode) { // The skippable function *must* be the next function in the data. Use the // start position as a sanity check. - ByteData::ReadingScope reading_scope(this); - CHECK_GE(scope_data_->RemainingBytes(), kSkippableFunctionDataSize); + typename ByteData::ReadingScope reading_scope(this); + CHECK_GE(scope_data_->RemainingBytes(), ByteData::kSkippableFunctionDataSize); int start_position_from_data = scope_data_->ReadUint32(); CHECK_EQ(start_position, start_position_from_data); @@ -531,28 +499,20 @@ ConsumedPreParsedScopeData::GetDataForSkippableFunction( // Retrieve the corresponding PreParsedScopeData and associate it to the // skipped function. If the skipped functions contains inner functions, those // can be skipped when the skipped function is eagerly parsed. - CHECK_GT(data_->length(), child_index_); - Object* child_data = data_->child_data(child_index_++); - if (!child_data->IsPreParsedScopeData()) { - return nullptr; - } - Handle child_data_handle( - PreParsedScopeData::cast(child_data), isolate_); - return new (zone) ProducedPreParsedScopeData(child_data_handle, zone); + return GetChildData(zone, child_index_++); } -void ConsumedPreParsedScopeData::RestoreScopeAllocationData( +template +void BaseConsumedPreParsedScopeData::RestoreScopeAllocationData( DeclarationScope* scope) { DCHECK(FLAG_preparser_scope_analysis); DCHECK_EQ(scope->scope_type(), ScopeType::FUNCTION_SCOPE); - DCHECK(!data_.is_null()); - - ByteData::ReadingScope reading_scope(this); + typename ByteData::ReadingScope reading_scope(this); #ifdef DEBUG int magic_value_from_data = scope_data_->ReadUint32(); // Check that we've consumed all inner function data. - DCHECK_EQ(magic_value_from_data, kMagicValue); + DCHECK_EQ(magic_value_from_data, ByteData::kMagicValue); int start_position_from_data = scope_data_->ReadUint32(); int end_position_from_data = scope_data_->ReadUint32(); @@ -566,7 +526,8 @@ void ConsumedPreParsedScopeData::RestoreScopeAllocationData( DCHECK_EQ(scope_data_->RemainingBytes(), 0); } -void ConsumedPreParsedScopeData::RestoreData(Scope* scope) { +template +void BaseConsumedPreParsedScopeData::RestoreData(Scope* scope) { if (scope->is_declaration_scope() && scope->AsDeclarationScope()->is_skipped_function()) { return; @@ -579,14 +540,8 @@ void ConsumedPreParsedScopeData::RestoreData(Scope* scope) { return; } - if (scope_data_->RemainingBytes() < kUint8Size) { - // Temporary debugging code for detecting inconsistent data. Write debug - // information on the stack, then crash. - isolate_->PushStackTraceAndDie(); - } - // scope_type is stored only in debug mode. - CHECK_GE(scope_data_->RemainingBytes(), kUint8Size); + CHECK_GE(scope_data_->RemainingBytes(), ByteData::kUint8Size); DCHECK_EQ(scope_data_->ReadUint8(), scope->scope_type()); uint32_t eval = scope_data_->ReadUint8(); @@ -613,7 +568,9 @@ void ConsumedPreParsedScopeData::RestoreData(Scope* scope) { RestoreDataForInnerScopes(scope); } -void ConsumedPreParsedScopeData::RestoreDataForVariable(Variable* var) { +template +void BaseConsumedPreParsedScopeData::RestoreDataForVariable( + Variable* var) { #ifdef DEBUG const AstRawString* name = var->raw_name(); bool data_one_byte = scope_data_->ReadUint8(); @@ -647,7 +604,9 @@ void ConsumedPreParsedScopeData::RestoreDataForVariable(Variable* var) { } } -void ConsumedPreParsedScopeData::RestoreDataForInnerScopes(Scope* scope) { +template +void BaseConsumedPreParsedScopeData::RestoreDataForInnerScopes( + Scope* scope) { std::vector scopes; for (Scope* inner = scope->inner_scope(); inner != nullptr; inner = inner->sibling()) { @@ -658,5 +617,104 @@ void ConsumedPreParsedScopeData::RestoreDataForInnerScopes(Scope* scope) { } } +#ifdef DEBUG +template +void BaseConsumedPreParsedScopeData::VerifyDataStart() { + typename ByteData::ReadingScope reading_scope(this); + int scope_data_start = scope_data_->ReadUint32(); + scope_data_->SetPosition(scope_data_start); + DCHECK_EQ(scope_data_->ReadUint32(), ByteData::kMagicValue); + // The first data item is scope_data_start. Skip over it. + scope_data_->SetPosition(ByteData::kPlaceholderSize); +} +#endif + +PodArray* OnHeapConsumedPreParsedScopeData::GetScopeData() { + return data_->scope_data(); +} + +ProducedPreParsedScopeData* OnHeapConsumedPreParsedScopeData::GetChildData( + Zone* zone, int child_index) { + CHECK_GT(data_->length(), child_index); + Object* child_data = data_->child_data(child_index); + if (!child_data->IsPreParsedScopeData()) { + return nullptr; + } + Handle child_data_handle( + PreParsedScopeData::cast(child_data), isolate_); + return new (zone) ProducedPreParsedScopeData(child_data_handle, zone); +} + +OnHeapConsumedPreParsedScopeData::OnHeapConsumedPreParsedScopeData( + Isolate* isolate, Handle data) + : BaseConsumedPreParsedScopeData>(), + isolate_(isolate), + data_(data) { + DCHECK_NOT_NULL(isolate); + DCHECK(data->IsPreParsedScopeData()); +#ifdef DEBUG + VerifyDataStart(); +#endif +} + +ZonePreParsedScopeData::ZonePreParsedScopeData( + Zone* zone, ZoneChunkList::iterator byte_data_begin, + ZoneChunkList::iterator byte_data_end, int child_length) + : byte_data_(byte_data_begin, byte_data_end, zone), + children_(child_length, zone) {} + +Handle ZonePreParsedScopeData::Serialize(Isolate* isolate) { + int child_data_length = child_length(); + Handle result = + isolate->factory()->NewPreParsedScopeData(child_data_length); + + Handle> scope_data_array = PodArray::New( + isolate, static_cast(byte_data()->size()), TENURED); + scope_data_array->copy_in(0, byte_data()->data(), + static_cast(byte_data()->size())); + result->set_scope_data(*scope_data_array); + + for (int i = 0; i < child_data_length; i++) { + ZonePreParsedScopeData* child = get_child(i); + if (child) { + Handle child_data = child->Serialize(isolate); + result->set_child_data(i, *child_data); + } + } + return result; +} + +ZoneConsumedPreParsedScopeData::ZoneConsumedPreParsedScopeData( + Zone* zone, ZonePreParsedScopeData* data) + : data_(data), scope_data_wrapper_(data_->byte_data()) { +#ifdef DEBUG + VerifyDataStart(); +#endif +} + +ZoneVectorWrapper* ZoneConsumedPreParsedScopeData::GetScopeData() { + return &scope_data_wrapper_; +} + +ProducedPreParsedScopeData* ZoneConsumedPreParsedScopeData::GetChildData( + Zone* zone, int child_index) { + CHECK_GT(data_->child_length(), child_index); + ZonePreParsedScopeData* child_data = data_->get_child(child_index); + if (child_data == nullptr) { + return nullptr; + } + return new (zone) ProducedPreParsedScopeData(child_data, zone); +} + +std::unique_ptr ConsumedPreParsedScopeData::For( + Isolate* isolate, Handle data) { + return base::make_unique(isolate, data); +} + +std::unique_ptr ConsumedPreParsedScopeData::For( + Zone* zone, ZonePreParsedScopeData* data) { + return base::make_unique(zone, data); +} + } // namespace internal } // namespace v8 diff --git a/src/parsing/preparsed-scope-data.h b/src/parsing/preparsed-scope-data.h index 61d67291a4..012dfdcfd2 100644 --- a/src/parsing/preparsed-scope-data.h +++ b/src/parsing/preparsed-scope-data.h @@ -5,23 +5,21 @@ #ifndef V8_PARSING_PREPARSED_SCOPE_DATA_H_ #define V8_PARSING_PREPARSED_SCOPE_DATA_H_ -#include -#include -#include - #include "src/globals.h" #include "src/handles.h" -#include "src/objects/shared-function-info.h" +#include "src/maybe-handles.h" #include "src/zone/zone-chunk-list.h" +#include "src/zone/zone-containers.h" namespace v8 { namespace internal { template -class Handle; +class PodArray; class PreParser; class PreParsedScopeData; +class ZonePreParsedScopeData; /* @@ -66,37 +64,20 @@ class PreParsedScopeData; class ProducedPreParsedScopeData : public ZoneObject { public: - class ByteData : public ZoneObject { - public: - explicit ByteData(Zone* zone) - : backing_store_(zone), free_quarters_in_last_byte_(0) {} - - void WriteUint32(uint32_t data); - void WriteUint8(uint8_t data); - void WriteQuarter(uint8_t data); - -#ifdef DEBUG - // For overwriting previously written data at position 0. - void OverwriteFirstUint32(uint32_t data); -#endif - - Handle> Serialize(Isolate* isolate); - - size_t size() const { return backing_store_.size(); } - - private: - ZoneChunkList backing_store_; - uint8_t free_quarters_in_last_byte_; - }; + class ByteData; // Create a ProducedPreParsedScopeData object which will collect data as we // parse. ProducedPreParsedScopeData(Zone* zone, ProducedPreParsedScopeData* parent); // Create a ProducedPreParsedScopeData which is just a proxy for a previous - // produced PreParsedScopeData. + // produced PreParsedScopeData on the heap. ProducedPreParsedScopeData(Handle data, Zone* zone); + // Create a ProducedPreParsedScopeData which is just a proxy for a previous + // produced PreParsedScopeData in zone. + ProducedPreParsedScopeData(ZonePreParsedScopeData* data, Zone* zone); + ProducedPreParsedScopeData* parent() const { return parent_; } // For gathering the inner function data and splitting it up according to the @@ -153,6 +134,11 @@ class ProducedPreParsedScopeData : public ZoneObject { // MaybeHandle. MaybeHandle Serialize(Isolate* isolate); + // If there is data (if the Scope contains skippable inner functions), return + // an off-heap ZonePreParsedScopeData representing the data; otherwise + // return nullptr. + ZonePreParsedScopeData* Serialize(Zone* zone); + static bool ScopeNeedsData(Scope* scope); static bool ScopeIsSkippableFunctionScope(Scope* scope); @@ -174,80 +160,48 @@ class ProducedPreParsedScopeData : public ZoneObject { // Whether we've given up producing the data for this function. bool bailed_out_; - // ProducedPreParsedScopeData can also mask a Handle + // ProducedPreParsedScopeData can also hold a Handle // which was produced already earlier. This happens for deeper lazy functions. - Handle previously_produced_preparsed_scope_data_; + // TODO(rmcilroy): Split apart ProducedPreParsedScopeData into different + // classes for situations where it has already been produced. + Handle previously_produced_on_heap_preparsed_scope_data_; + + // ProducedPreParsedScopeData can also hold a ZonePreParsedScopeData* + // which was produced already earlier. This happens for deeper lazy functions. + // TODO(rmcilroy): Split apart ProducedPreParsedScopeData into different + // classes for situations where it has already been produced. + ZonePreParsedScopeData* previously_produced_zone_preparsed_scope_data_; DISALLOW_COPY_AND_ASSIGN(ProducedPreParsedScopeData); }; class ConsumedPreParsedScopeData { public: - class ByteData { - public: - ByteData() - : data_(nullptr), index_(0), stored_quarters_(0), stored_byte_(0) {} + // Creates a ConsumedPreParsedScopeData representing the data of an on-heap + // PreParsedScopeData |data|. + static std::unique_ptr For( + Isolate* isolate, Handle data); - // Reading from the ByteData is only allowed when a ReadingScope is on the - // stack. This ensures that we have a DisallowHeapAllocation in place - // whenever ByteData holds a raw pointer into the heap. - class ReadingScope { - public: - ReadingScope(ByteData* consumed_data, PodArray* data) - : consumed_data_(consumed_data) { - consumed_data->data_ = data; - } - explicit ReadingScope(ConsumedPreParsedScopeData* parent); - ~ReadingScope() { consumed_data_->data_ = nullptr; } + // Creates a ConsumedPreParsedScopeData representing the data of an off-heap + // ZonePreParsedScopeData |data|. + static std::unique_ptr For( + Zone* zone, ZonePreParsedScopeData* data); - private: - ByteData* consumed_data_; - DisallowHeapAllocation no_gc; - }; + virtual ~ConsumedPreParsedScopeData() {} - void SetPosition(int position) { index_ = position; } - - int32_t ReadUint32(); - uint8_t ReadUint8(); - uint8_t ReadQuarter(); - - size_t RemainingBytes() const; - - // private: - PodArray* data_; - int index_; - uint8_t stored_quarters_; - uint8_t stored_byte_; - }; - - ConsumedPreParsedScopeData(); - ~ConsumedPreParsedScopeData(); - - void SetData(Isolate* isolate, Handle data); - - bool HasData() const { return !data_.is_null(); } - - ProducedPreParsedScopeData* GetDataForSkippableFunction( + virtual ProducedPreParsedScopeData* GetDataForSkippableFunction( Zone* zone, int start_position, int* end_position, int* num_parameters, int* num_inner_functions, bool* uses_super_property, - LanguageMode* language_mode); + LanguageMode* language_mode) = 0; // Restores the information needed for allocating the Scope's (and its // subscopes') variables. - void RestoreScopeAllocationData(DeclarationScope* scope); + virtual void RestoreScopeAllocationData(DeclarationScope* scope) = 0; + + protected: + ConsumedPreParsedScopeData() {} private: - void RestoreData(Scope* scope); - void RestoreDataForVariable(Variable* var); - void RestoreDataForInnerScopes(Scope* scope); - - Isolate* isolate_; - Handle data_; - std::unique_ptr scope_data_; - // When consuming the data, these indexes point to the data we're going to - // consume next. - int child_index_; - DISALLOW_COPY_AND_ASSIGN(ConsumedPreParsedScopeData); }; diff --git a/src/zone/zone-chunk-list.h b/src/zone/zone-chunk-list.h index 9c0c073a81..049e8f52a9 100644 --- a/src/zone/zone-chunk-list.h +++ b/src/zone/zone-chunk-list.h @@ -155,8 +155,8 @@ class ZoneChunkListIterator using ChunkList = maybe_const>; public: - maybe_const& operator*() { return current_->items()[position_]; } - maybe_const* operator->() { return ¤t_->items()[position_]; } + maybe_const& operator*() const { return current_->items()[position_]; } + maybe_const* operator->() const { return ¤t_->items()[position_]; } bool operator==(const ZoneChunkListIterator& other) const { return other.current_ == current_ && other.position_ == position_; } diff --git a/test/cctest/BUILD.gn b/test/cctest/BUILD.gn index 6071628cb7..31a4acbebf 100644 --- a/test/cctest/BUILD.gn +++ b/test/cctest/BUILD.gn @@ -239,6 +239,7 @@ v8_source_set("cctest_sources") { "trace-extension.cc", "trace-extension.h", "types-fuzz.h", + "unicode-helpers.cc", "unicode-helpers.h", "wasm/test-c-wasm-entry.cc", "wasm/test-jump-table-assembler.cc", diff --git a/test/cctest/parsing/test-preparser.cc b/test/cctest/parsing/test-preparser.cc index 473debec40..1ff038ba06 100644 --- a/test/cctest/parsing/test-preparser.cc +++ b/test/cctest/parsing/test-preparser.cc @@ -8,6 +8,7 @@ #include "src/objects-inl.h" #include "src/parsing/parse-info.h" #include "src/parsing/parsing.h" +#include "src/parsing/preparsed-scope-data-impl.h" #include "src/parsing/preparsed-scope-data.h" #include "test/cctest/cctest.h" @@ -747,8 +748,8 @@ TEST(PreParserScopeAnalysis) { // Parse the lazy function using the scope data. i::ParseInfo using_scope_data(isolate, shared); using_scope_data.set_lazy_compile(); - using_scope_data.consumed_preparsed_scope_data()->SetData( - isolate, produced_data_on_heap); + using_scope_data.set_consumed_preparsed_scope_data( + i::ConsumedPreParsedScopeData::For(isolate, produced_data_on_heap)); CHECK(i::parsing::ParseFunction(&using_scope_data, shared, isolate)); // Verify that we skipped at least one function inside that scope. @@ -841,32 +842,67 @@ TEST(ProducingAndConsumingByteData) { // End with a lonely quarter. bytes.WriteQuarter(2); - i::Handle> data_on_heap = bytes.Serialize(isolate); - i::ConsumedPreParsedScopeData::ByteData bytes_for_reading; - i::ConsumedPreParsedScopeData::ByteData::ReadingScope reading_scope( - &bytes_for_reading, *data_on_heap); + { + // Serialize as a ZoneConsumedPreParsedScopeData, and read back data. + i::ZonePreParsedScopeData zone_serialized(&zone, bytes.begin(), bytes.end(), + 0); + i::ZoneConsumedPreParsedScopeData::ByteData bytes_for_reading; + i::ZoneVectorWrapper wrapper(zone_serialized.byte_data()); + i::ZoneConsumedPreParsedScopeData::ByteData::ReadingScope reading_scope( + &bytes_for_reading, &wrapper); - // Read the data back. #ifdef DEBUG - CHECK_EQ(bytes_for_reading.ReadUint32(), 2017); + CHECK_EQ(bytes_for_reading.ReadUint32(), 2017); #else - CHECK_EQ(bytes_for_reading.ReadUint32(), 1983); + CHECK_EQ(bytes_for_reading.ReadUint32(), 1983); #endif - CHECK_EQ(bytes_for_reading.ReadUint32(), 2147483647); - CHECK_EQ(bytes_for_reading.ReadUint8(), 4); - CHECK_EQ(bytes_for_reading.ReadUint8(), 255); - CHECK_EQ(bytes_for_reading.ReadUint32(), 0); - CHECK_EQ(bytes_for_reading.ReadUint8(), 0); - CHECK_EQ(bytes_for_reading.ReadUint8(), 100); - CHECK_EQ(bytes_for_reading.ReadQuarter(), 3); - CHECK_EQ(bytes_for_reading.ReadQuarter(), 0); - CHECK_EQ(bytes_for_reading.ReadQuarter(), 2); - CHECK_EQ(bytes_for_reading.ReadQuarter(), 1); - CHECK_EQ(bytes_for_reading.ReadQuarter(), 0); - CHECK_EQ(bytes_for_reading.ReadUint8(), 50); - CHECK_EQ(bytes_for_reading.ReadQuarter(), 0); - CHECK_EQ(bytes_for_reading.ReadQuarter(), 1); - CHECK_EQ(bytes_for_reading.ReadQuarter(), 2); - CHECK_EQ(bytes_for_reading.ReadUint32(), 50); - CHECK_EQ(bytes_for_reading.ReadQuarter(), 2); + CHECK_EQ(bytes_for_reading.ReadUint32(), 2147483647); + CHECK_EQ(bytes_for_reading.ReadUint8(), 4); + CHECK_EQ(bytes_for_reading.ReadUint8(), 255); + CHECK_EQ(bytes_for_reading.ReadUint32(), 0); + CHECK_EQ(bytes_for_reading.ReadUint8(), 0); + CHECK_EQ(bytes_for_reading.ReadUint8(), 100); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 3); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 0); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 2); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 1); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 0); + CHECK_EQ(bytes_for_reading.ReadUint8(), 50); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 0); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 1); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 2); + CHECK_EQ(bytes_for_reading.ReadUint32(), 50); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 2); + } + + { + // Serialize as an OnHeapConsumedPreParsedScopeData, and read back data. + i::Handle> data_on_heap = bytes.Serialize(isolate); + i::OnHeapConsumedPreParsedScopeData::ByteData bytes_for_reading; + i::OnHeapConsumedPreParsedScopeData::ByteData::ReadingScope reading_scope( + &bytes_for_reading, *data_on_heap); + +#ifdef DEBUG + CHECK_EQ(bytes_for_reading.ReadUint32(), 2017); +#else + CHECK_EQ(bytes_for_reading.ReadUint32(), 1983); +#endif + CHECK_EQ(bytes_for_reading.ReadUint32(), 2147483647); + CHECK_EQ(bytes_for_reading.ReadUint8(), 4); + CHECK_EQ(bytes_for_reading.ReadUint8(), 255); + CHECK_EQ(bytes_for_reading.ReadUint32(), 0); + CHECK_EQ(bytes_for_reading.ReadUint8(), 0); + CHECK_EQ(bytes_for_reading.ReadUint8(), 100); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 3); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 0); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 2); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 1); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 0); + CHECK_EQ(bytes_for_reading.ReadUint8(), 50); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 0); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 1); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 2); + CHECK_EQ(bytes_for_reading.ReadUint32(), 50); + CHECK_EQ(bytes_for_reading.ReadQuarter(), 2); + } } diff --git a/test/cctest/unicode-helpers.cc b/test/cctest/unicode-helpers.cc new file mode 100644 index 0000000000..524e5936fc --- /dev/null +++ b/test/cctest/unicode-helpers.cc @@ -0,0 +1,31 @@ +// Copyright 2018 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "test/cctest/unicode-helpers.h" + +int Ucs2CharLength(unibrow::uchar c) { + if (c == unibrow::Utf8::kIncomplete || c == unibrow::Utf8::kBufferEmpty) { + return 0; + } else if (c < 0xFFFF) { + return 1; + } else { + return 2; + } +} + +int Utf8LengthHelper(const char* s) { + unibrow::Utf8::Utf8IncrementalBuffer buffer(unibrow::Utf8::kBufferEmpty); + unibrow::Utf8::State state = unibrow::Utf8::State::kAccept; + + int length = 0; + size_t i = 0; + while (s[i] != '\0') { + unibrow::uchar tmp = + unibrow::Utf8::ValueOfIncremental(s[i], &i, &state, &buffer); + length += Ucs2CharLength(tmp); + } + unibrow::uchar tmp = unibrow::Utf8::ValueOfIncrementalFinish(&state); + length += Ucs2CharLength(tmp); + return length; +} diff --git a/test/cctest/unicode-helpers.h b/test/cctest/unicode-helpers.h index ca75fb65d7..06c3fcd8ea 100644 --- a/test/cctest/unicode-helpers.h +++ b/test/cctest/unicode-helpers.h @@ -7,30 +7,7 @@ #include "src/unicode.h" -static int Ucs2CharLength(unibrow::uchar c) { - if (c == unibrow::Utf8::kIncomplete || c == unibrow::Utf8::kBufferEmpty) { - return 0; - } else if (c < 0xFFFF) { - return 1; - } else { - return 2; - } -} - -static int Utf8LengthHelper(const char* s) { - unibrow::Utf8::Utf8IncrementalBuffer buffer(unibrow::Utf8::kBufferEmpty); - unibrow::Utf8::State state = unibrow::Utf8::State::kAccept; - - int length = 0; - size_t i = 0; - while (s[i] != '\0') { - unibrow::uchar tmp = - unibrow::Utf8::ValueOfIncremental(s[i], &i, &state, &buffer); - length += Ucs2CharLength(tmp); - } - unibrow::uchar tmp = unibrow::Utf8::ValueOfIncrementalFinish(&state); - length += Ucs2CharLength(tmp); - return length; -} +int Ucs2CharLength(unibrow::uchar c); +int Utf8LengthHelper(const char* s); #endif // V8_CCTEST_UNICODE_HELPERS_H_