Commit 9f21cab8 authored by Marja Hölttä's avatar Marja Hölttä Committed by Commit Bot

Revert "Reland#2 [parser] Refactor streaming scanner streams."

This reverts commit de9269f3.

Something's still wrong in the encoding handling (see bug).

Bug: chromium:763106
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: Icd19dd42b84b9d090e191375a2942b9941110bcf
Reviewed-on: https://chromium-review.googlesource.com/657386
Commit-Queue: Marja Hölttä <marja@chromium.org>
Reviewed-by: 's avatarDaniel Vogelheim <vogelheim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#47924}
parent 9b218658
...@@ -1358,6 +1358,11 @@ class V8_EXPORT ScriptCompiler { ...@@ -1358,6 +1358,11 @@ class V8_EXPORT ScriptCompiler {
* length of the data returned. When the data ends, GetMoreData should * length of the data returned. When the data ends, GetMoreData should
* return 0. Caller takes ownership of the data. * return 0. Caller takes ownership of the data.
* *
* When streaming UTF-8 data, V8 handles multi-byte characters split between
* two data chunks, but doesn't handle multi-byte characters split between
* more than two data chunks. The embedder can avoid this problem by always
* returning at least 2 bytes of data.
*
* If the embedder wants to cancel the streaming, they should make the next * If the embedder wants to cancel the streaming, they should make the next
* GetMoreData call return 0. V8 will interpret it as end of data (and most * GetMoreData call return 0. V8 will interpret it as end of data (and most
* probably, parsing will fail). The streaming task will return as soon as * probably, parsing will fail). The streaming task will return as soon as
......
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
#include "src/parsing/scanner-character-streams.h" #include "src/parsing/scanner-character-streams.h"
#include <memory>
#include "include/v8.h" #include "include/v8.h"
#include "src/counters.h" #include "src/counters.h"
#include "src/globals.h" #include "src/globals.h"
...@@ -18,8 +16,7 @@ namespace v8 { ...@@ -18,8 +16,7 @@ namespace v8 {
namespace internal { namespace internal {
namespace { namespace {
constexpr unibrow::uchar kUtf8Bom = 0xfeff; const unibrow::uchar kUtf8Bom = 0xfeff;
constexpr uint8_t kUtf8BomBytes[] = {0xef, 0xbb, 0xbf};
} // namespace } // namespace
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
...@@ -33,7 +30,7 @@ class BufferedUtf16CharacterStream : public Utf16CharacterStream { ...@@ -33,7 +30,7 @@ class BufferedUtf16CharacterStream : public Utf16CharacterStream {
BufferedUtf16CharacterStream(); BufferedUtf16CharacterStream();
protected: protected:
static constexpr size_t kBufferCharacterSize = 512; static const size_t kBufferSize = 512;
bool ReadBlock() override; bool ReadBlock() override;
...@@ -43,22 +40,21 @@ class BufferedUtf16CharacterStream : public Utf16CharacterStream { ...@@ -43,22 +40,21 @@ class BufferedUtf16CharacterStream : public Utf16CharacterStream {
// Fixed sized buffer that this class reads from. // Fixed sized buffer that this class reads from.
// The base class' buffer_start_ should always point to buffer_. // The base class' buffer_start_ should always point to buffer_.
uc16 buffer_[kBufferCharacterSize]; uc16 buffer_[kBufferSize];
static constexpr size_t kBufferSizeInBytes =
sizeof(BufferedUtf16CharacterStream::buffer_);
}; };
BufferedUtf16CharacterStream::BufferedUtf16CharacterStream() BufferedUtf16CharacterStream::BufferedUtf16CharacterStream()
: Utf16CharacterStream(buffer_, buffer_, buffer_, 0) {} : Utf16CharacterStream(buffer_, buffer_, buffer_, 0) {}
bool BufferedUtf16CharacterStream::ReadBlock() { bool BufferedUtf16CharacterStream::ReadBlock() {
DCHECK_EQ(buffer_start_, buffer_);
size_t position = pos(); size_t position = pos();
buffer_pos_ = position; buffer_pos_ = position;
buffer_start_ = buffer_cursor_ = buffer_; buffer_cursor_ = buffer_;
buffer_end_ = buffer_ + FillBuffer(position); buffer_end_ = buffer_ + FillBuffer(position);
DCHECK_EQ(pos(), position); DCHECK_EQ(pos(), position);
DCHECK_LE(buffer_end_, buffer_start_ + kBufferCharacterSize); DCHECK_LE(buffer_end_, buffer_start_ + kBufferSize);
return buffer_cursor_ < buffer_end_; return buffer_cursor_ < buffer_end_;
} }
...@@ -93,7 +89,7 @@ GenericStringUtf16CharacterStream::GenericStringUtf16CharacterStream( ...@@ -93,7 +89,7 @@ GenericStringUtf16CharacterStream::GenericStringUtf16CharacterStream(
size_t GenericStringUtf16CharacterStream::FillBuffer(size_t from_pos) { size_t GenericStringUtf16CharacterStream::FillBuffer(size_t from_pos) {
if (from_pos >= length_) return 0; if (from_pos >= length_) return 0;
size_t length = i::Min(kBufferCharacterSize, length_ - from_pos); size_t length = i::Min(kBufferSize, length_ - from_pos);
String::WriteToFlat<uc16>(*string_, buffer_, static_cast<int>(from_pos), String::WriteToFlat<uc16>(*string_, buffer_, static_cast<int>(from_pos),
static_cast<int>(from_pos + length)); static_cast<int>(from_pos + length));
return length; return length;
...@@ -189,492 +185,644 @@ ExternalOneByteStringUtf16CharacterStream:: ...@@ -189,492 +185,644 @@ ExternalOneByteStringUtf16CharacterStream::
size_t ExternalOneByteStringUtf16CharacterStream::FillBuffer(size_t from_pos) { size_t ExternalOneByteStringUtf16CharacterStream::FillBuffer(size_t from_pos) {
if (from_pos >= length_) return 0; if (from_pos >= length_) return 0;
size_t length = Min(kBufferCharacterSize, length_ - from_pos); size_t length = Min(kBufferSize, length_ - from_pos);
i::CopyCharsUnsigned(buffer_, raw_data_ + from_pos, length); i::CopyCharsUnsigned(buffer_, raw_data_ + from_pos, length);
return length; return length;
} }
namespace { // ----------------------------------------------------------------------------
// Utf8ExternalStreamingStream - chunked streaming of Utf-8 data.
//
// This implementation is fairly complex, since data arrives in chunks which
// may 'cut' arbitrarily into utf-8 characters. Also, seeking to a given
// character position is tricky because the byte position cannot be dericed
// from the character position.
// A linked-list container for the chunk data. class Utf8ExternalStreamingStream : public BufferedUtf16CharacterStream {
// Shared by ChunksView and Chunks (which may execute on different threads). public:
struct Chunk { Utf8ExternalStreamingStream(
enum Type : uint8_t { ONE_BYTE, TWO_BYTE }; ScriptCompiler::ExternalSourceStream* source_stream,
Chunk() : Chunk(ONE_BYTE, 0, nullptr, 0, 0, 0) {} RuntimeCallStats* stats)
Chunk(Type type, int first_char_start_offset, const uint8_t* data, : current_({0, {0, 0, unibrow::Utf8::Utf8IncrementalBuffer(0)}}),
size_t byte_length, size_t char_length, size_t char_pos) source_stream_(source_stream),
: type(type), stats_(stats) {}
first_char_start_offset(first_char_start_offset), ~Utf8ExternalStreamingStream() override {
data(data), for (size_t i = 0; i < chunks_.size(); i++) delete[] chunks_[i].data;
byte_length(byte_length),
char_length(char_length),
char_pos(char_pos) {}
~Chunk() { delete next.Value(); }
static Chunk* CreateOneByte(const uint8_t* data, size_t byte_length,
size_t char_pos, size_t start_offset = 0) {
return new Chunk(ONE_BYTE, static_cast<int>(start_offset), data,
byte_length, byte_length - start_offset, char_pos);
}
static Chunk* CreateTwoByte(const uint8_t* data, size_t byte_length,
size_t char_pos, bool odd_start) {
size_t char_length = (odd_start + byte_length) / 2;
return new Chunk(TWO_BYTE, odd_start, data, byte_length, char_length,
char_pos);
}
// Position of the first character which starts in this chunk.
// This is always greater or equal to char_pos, so if no char starts in this
// chunk (i.e. char_length == 0) char_pos+1 is returned.
size_t FirstCharPosition() const {
// There are no split chars for one byte chunks, but there might be some
// start offset
if (type == ONE_BYTE) return char_pos;
return char_pos + (first_char_start_offset != 0);
}
size_t OffsetOf(size_t position) const {
DCHECK_LE(FirstCharPosition(), position);
DCHECK_GE(char_pos + char_length, position);
switch (type) {
case ONE_BYTE:
return position - FirstCharPosition() + first_char_start_offset;
case TWO_BYTE:
return 2 * (position - FirstCharPosition()) + first_char_start_offset;
}
UNREACHABLE();
}
size_t CopyToBuffer(uint8_t* buffer, size_t size, size_t offset) const {
switch (type) {
case ONE_BYTE: {
size_t chars_to_copy = i::Min(size / 2, byte_length - offset);
i::CopyCharsUnsigned(reinterpret_cast<uc16*>(buffer), &data[offset],
chars_to_copy);
return 2 * chars_to_copy;
}
case TWO_BYTE: {
size_t bytes_to_copy = i::Min(size, byte_length - offset);
i::MemCopy(buffer, &data[offset], bytes_to_copy);
return bytes_to_copy;
}
}
UNREACHABLE();
} }
const Type type;
// Offset to the first byte of first character that starts in this chunk.
const int first_char_start_offset;
const std::unique_ptr<const uint8_t[]> data;
const size_t byte_length;
const size_t char_length;
const size_t char_pos;
base::AtomicValue<const Chunk*> next{nullptr};
const Chunk* prev = nullptr;
};
// An interface that delivers a sequence of Chunks. bool can_access_heap() override { return false; }
class ChunkSource {
public: protected:
virtual ~ChunkSource() {} size_t FillBuffer(size_t position) override;
// Fetch and return next chunk.
// Returns nullptr if source is exhausted. private:
// Should not be called after source is exhausted. // A position within the data stream. It stores:
virtual Chunk* GetNextChunk() = 0; // - The 'physical' position (# of bytes in the stream),
// - the 'logical' position (# of ucs-2 characters, also within the stream),
// - a possibly incomplete utf-8 char at the current 'physical' position.
struct StreamPosition {
size_t bytes;
size_t chars;
unibrow::Utf8::Utf8IncrementalBuffer incomplete_char;
};
// Position contains a StreamPosition and the index of the chunk the position
// points into. (The chunk_no could be derived from pos, but that'd be
// an expensive search through all chunks.)
struct Position {
size_t chunk_no;
StreamPosition pos;
};
// A chunk in the list of chunks, containing:
// - The chunk data (data pointer and length), and
// - the position at the first byte of the chunk.
struct Chunk {
const uint8_t* data;
size_t length;
StreamPosition start;
};
// Within the current chunk, skip forward from current_ towards position.
bool SkipToPosition(size_t position);
// Within the current chunk, fill the buffer_ (while it has capacity).
void FillBufferFromCurrentChunk();
// Fetch a new chunk (assuming current_ is at the end of the current data).
bool FetchChunk();
// Search through the chunks and set current_ to point to the given position.
// (This call is potentially expensive.)
void SearchPosition(size_t position);
std::vector<Chunk> chunks_;
Position current_;
ScriptCompiler::ExternalSourceStream* source_stream_;
RuntimeCallStats* stats_;
}; };
// Manages chunks, provides seeking to chunk containing start byte or end byte bool Utf8ExternalStreamingStream::SkipToPosition(size_t position) {
// of a character. It cannot fetch new data, but can read data fetched by viewed DCHECK_LE(current_.pos.chars, position); // We can only skip forward.
// stream (also data fetched after this stream view was created). Multiple
// ChunksViews can operate on same chunks concurrently. // Already there? Then return immediately.
class ChunksView { if (current_.pos.chars == position) return true;
public:
ChunksView(const ChunksView& other) = default; const Chunk& chunk = chunks_[current_.chunk_no];
virtual ~ChunksView() {} DCHECK(current_.pos.bytes >= chunk.start.bytes);
// Return the chunk containing the last byte of the char at position. unibrow::Utf8::Utf8IncrementalBuffer incomplete_char =
// Position is in chars not bytes. chunk.start.incomplete_char;
// If position is behind the end of the stream, nullptr is returned. size_t it = current_.pos.bytes - chunk.start.bytes;
virtual const Chunk* SeekToEndOf(size_t position) { size_t chars = chunk.start.chars;
DCHECK_NOT_NULL(current_); while (it < chunk.length && chars < position) {
DCHECK_NOT_NULL(last_seen_); unibrow::uchar t =
// We almost always 'stream', meaning we want data from the next chunk unibrow::Utf8::ValueOfIncremental(chunk.data[it], &incomplete_char);
if (V8_LIKELY(position >= last_seen_->char_pos)) { if (t == kUtf8Bom && current_.pos.chars == 0) {
// Fast-forward to last seen chunk // BOM detected at beginning of the stream. Don't copy it.
current_ = last_seen_; } else if (t != unibrow::Utf8::kIncomplete) {
// Continue going forward until position found or out of chunks chars++;
while (position >= current_->char_pos + current_->char_length) { if (t > unibrow::Utf16::kMaxNonSurrogateCharCode) chars++;
const Chunk* next = current_->next.Value();
if (!next) return nullptr;
current_ = last_seen_ = next;
}
} else if (position >= current_->char_pos) {
// Go forward until position found or out of chunks
while (position >= current_->char_pos + current_->char_length) {
current_ = current_->next.Value();
if (!current_) return nullptr;
}
} else {
// Shortcut jump to first chunk
DCHECK_NOT_NULL(start_.get());
DCHECK_EQ(start_->char_pos, 0);
DCHECK_EQ(start_->byte_length, 0);
const Chunk* first = start_->next.Value();
DCHECK_NOT_NULL(first);
DCHECK_EQ(first->char_pos, 0);
if (position < first->char_pos + first->char_length) {
current_ = first;
}
// Go backwards until position found
while (position < current_->char_pos) {
current_ = current_->prev;
DCHECK_NOT_NULL(current_);
}
} }
DCHECK_LE(current_->char_pos, position); it++;
DCHECK_LT(position, current_->char_pos + current_->char_length);
return current_;
} }
// Return the chunk containing the first byte of the char at position. current_.pos.bytes += it;
// Position is in chars not bytes. current_.pos.chars = chars;
// If position is behind the end of the stream, nullptr is returned. current_.pos.incomplete_char = incomplete_char;
// This call also guarantees that a whole char is available. current_.chunk_no += (it == chunk.length);
const Chunk* SeekToStartOf(size_t position) {
const Chunk* chunk = SeekToEndOf(position); return current_.pos.chars == position;
if (chunk) { }
while (V8_UNLIKELY(position < chunk->FirstCharPosition())) {
chunk = chunk->prev; void Utf8ExternalStreamingStream::FillBufferFromCurrentChunk() {
DCHECK_NOT_NULL(chunk); DCHECK_LT(current_.chunk_no, chunks_.size());
} DCHECK_EQ(buffer_start_, buffer_cursor_);
current_ = chunk; DCHECK_LT(buffer_end_ + 1, buffer_start_ + kBufferSize);
const Chunk& chunk = chunks_[current_.chunk_no];
// The buffer_ is writable, but buffer_*_ members are const. So we get a
// non-const pointer into buffer that points to the same char as buffer_end_.
uint16_t* cursor = buffer_ + (buffer_end_ - buffer_start_);
DCHECK_EQ(cursor, buffer_end_);
// If the current chunk is the last (empty) chunk we'll have to process
// any left-over, partial characters.
if (chunk.length == 0) {
unibrow::uchar t =
unibrow::Utf8::ValueOfIncrementalFinish(&current_.pos.incomplete_char);
if (t != unibrow::Utf8::kBufferEmpty) {
DCHECK(t < unibrow::Utf16::kMaxNonSurrogateCharCode);
*cursor = static_cast<uc16>(t);
buffer_end_++;
current_.pos.chars++;
} }
return chunk; return;
} }
// Returns next chunk if there is one. unibrow::Utf8::Utf8IncrementalBuffer incomplete_char =
// This function never blocks/fetches new data. current_.pos.incomplete_char;
const Chunk* MoveToNext() { size_t it;
DCHECK_NOT_NULL(current_); for (it = current_.pos.bytes - chunk.start.bytes;
DCHECK_NOT_NULL(last_seen_); it < chunk.length && cursor + 1 < buffer_start_ + kBufferSize; it++) {
const Chunk* next = current_->next.Value(); unibrow::uchar t =
if (next) { unibrow::Utf8::ValueOfIncremental(chunk.data[it], &incomplete_char);
// Stream view may move to a previously unseen chunk if (t == unibrow::Utf8::kIncomplete) continue;
if (current_ == last_seen_) { if (V8_LIKELY(t < kUtf8Bom)) {
last_seen_ = next; *(cursor++) = static_cast<uc16>(t); // The by most frequent case.
} } else if (t == kUtf8Bom && current_.pos.bytes + it == 2) {
current_ = next; // BOM detected at beginning of the stream. Don't copy it.
} else if (t <= unibrow::Utf16::kMaxNonSurrogateCharCode) {
*(cursor++) = static_cast<uc16>(t);
} else {
*(cursor++) = unibrow::Utf16::LeadSurrogate(t);
*(cursor++) = unibrow::Utf16::TrailSurrogate(t);
} }
return next;
} }
protected: current_.pos.bytes = chunk.start.bytes + it;
ChunksView() current_.pos.chars += (cursor - buffer_end_);
: start_(std::make_shared<Chunk>()), current_.pos.incomplete_char = incomplete_char;
current_(start_.get()), current_.chunk_no += (it == chunk.length);
last_seen_(current_) {}
// Start sentinel shared between stream and stream views
// start_->next is first chunk with real data
const std::shared_ptr<Chunk> start_;
// Chunk we're currently at.
// Seek*, MoveToNext alter/work in respect to this.
const Chunk* current_;
// Last chunk this instance of ChunksView has seen so far.
const Chunk* last_seen_;
};
// A ChunksView subclass that does fetch new data from a ChunkSource if needed. buffer_end_ = cursor;
class Chunks : public ChunksView { }
public:
Chunks(std::unique_ptr<ChunkSource> source, RuntimeCallStats* stats) bool Utf8ExternalStreamingStream::FetchChunk() {
: source_(std::move(source)), RuntimeCallTimerScope scope(stats_, &RuntimeCallStats::GetMoreDataCallback);
stats_(stats), DCHECK_EQ(current_.chunk_no, chunks_.size());
last_added_(start_.get()), DCHECK(chunks_.empty() || chunks_.back().length != 0);
source_exhausted_(false) {}
const Chunk* SeekToEndOf(size_t position) override { const uint8_t* chunk = nullptr;
if (!source_exhausted_) { size_t length = source_stream_->GetMoreData(&chunk);
GetNewDataIfNeeded(position); chunks_.push_back({chunk, length, current_.pos});
} return length > 0;
return ChunksView::SeekToEndOf(position); }
void Utf8ExternalStreamingStream::SearchPosition(size_t position) {
// If current_ already points to the right position, we're done.
//
// This is expected to be the common case, since we typically call
// FillBuffer right after the current buffer.
if (current_.pos.chars == position) return;
// No chunks. Fetch at least one, so we can assume !chunks_.empty() below.
if (chunks_.empty()) {
DCHECK_EQ(current_.chunk_no, 0u);
DCHECK_EQ(current_.pos.bytes, 0u);
DCHECK_EQ(current_.pos.chars, 0u);
FetchChunk();
} }
protected: // Search for the last chunk whose start position is less or equal to
void GetNewDataIfNeeded(size_t position) { // position.
DCHECK(!source_exhausted_); size_t chunk_no = chunks_.size() - 1;
DCHECK_NOT_NULL(last_added_); while (chunk_no > 0 && chunks_[chunk_no].start.chars > position) {
size_t char_end_pos = last_added_->char_pos + last_added_->char_length; chunk_no--;
// Get more data if needed.
if (char_end_pos <= position) {
RuntimeCallTimerScope scope(stats_,
&RuntimeCallStats::GetMoreDataCallback);
while (char_end_pos <= position) {
Chunk* chunk = source_->GetNextChunk();
if (chunk) {
chunk->prev = last_added_;
last_added_->next.SetValue(chunk);
last_added_ = chunk;
char_end_pos += chunk->char_length;
DCHECK_EQ(char_end_pos,
last_added_->char_pos + last_added_->char_length);
} else {
source_exhausted_ = true;
break;
}
}
}
DCHECK_NOT_NULL(last_added_);
last_seen_ = last_added_;
// We either got the chunk we were looking for, or source is exhausted.
DCHECK_IMPLIES(!source_exhausted_, position < char_end_pos);
} }
const std::unique_ptr<ChunkSource> source_;
RuntimeCallStats* const stats_;
Chunk* last_added_;
bool source_exhausted_;
};
// ChunkSource for one-byte ExternalSourceStream // Did we find the terminating (zero-length) chunk? Then we're seeking
class OneByteChunkSource : public ChunkSource { // behind the end of the data, and position does not exist.
public: // Set current_ to point to the terminating chunk.
explicit OneByteChunkSource(ScriptCompiler::ExternalSourceStream* source) if (chunks_[chunk_no].length == 0) {
: source_(source) {} current_ = {chunk_no, chunks_[chunk_no].start};
Chunk* GetNextChunk() override { return;
const uint8_t* data = nullptr; }
DCHECK_NOT_NULL(source_);
const size_t byte_len = source_->GetMoreData(&data); // Did we find the non-last chunk? Then our position must be within chunk_no.
if (V8_UNLIKELY(byte_len == 0)) { if (chunk_no + 1 < chunks_.size()) {
delete[] data; // Fancy-pants optimization for ASCII chunks within a utf-8 stream.
return nullptr; // (Many web sites declare utf-8 encoding, but use only (or almost only) the
// ASCII subset for their JavaScript sources. We can exploit this, by
// checking whether the # bytes in a chunk are equal to the # chars, and if
// so avoid the expensive SkipToPosition.)
bool ascii_only_chunk =
chunks_[chunk_no].start.incomplete_char ==
unibrow::Utf8::Utf8IncrementalBuffer(0) &&
(chunks_[chunk_no + 1].start.bytes - chunks_[chunk_no].start.bytes) ==
(chunks_[chunk_no + 1].start.chars - chunks_[chunk_no].start.chars);
if (ascii_only_chunk) {
size_t skip = position - chunks_[chunk_no].start.chars;
current_ = {chunk_no,
{chunks_[chunk_no].start.bytes + skip,
chunks_[chunk_no].start.chars + skip,
unibrow::Utf8::Utf8IncrementalBuffer(0)}};
} else {
current_ = {chunk_no, chunks_[chunk_no].start};
SkipToPosition(position);
} }
size_t char_pos = byte_pos_;
byte_pos_ += byte_len; // Since position was within the chunk, SkipToPosition should have found
return Chunk::CreateOneByte(data, byte_len, char_pos); // something.
DCHECK_EQ(position, current_.pos.chars);
return;
} }
private: // What's left: We're in the last, non-terminating chunk. Our position
ScriptCompiler::ExternalSourceStream* const source_; // may be in the chunk, but it may also be in 'future' chunks, which we'll
size_t byte_pos_ = 0; // have to obtain.
}; DCHECK_EQ(chunk_no, chunks_.size() - 1);
current_ = {chunk_no, chunks_[chunk_no].start};
bool have_more_data = true;
bool found = SkipToPosition(position);
while (have_more_data && !found) {
DCHECK_EQ(current_.chunk_no, chunks_.size());
have_more_data = FetchChunk();
found = have_more_data && SkipToPosition(position);
}
// ChunkSource for two-byte ExternalSourceStream // We'll return with a postion != the desired position only if we're out
class TwoByteChunkSource : public ChunkSource { // of data. In that case, we'll point to the terminating chunk.
public: DCHECK_EQ(found, current_.pos.chars == position);
explicit TwoByteChunkSource(ScriptCompiler::ExternalSourceStream* source) DCHECK_EQ(have_more_data, chunks_.back().length != 0);
: source_(source) {} DCHECK_IMPLIES(!found, !have_more_data);
Chunk* GetNextChunk() override { DCHECK_IMPLIES(!found, current_.chunk_no == chunks_.size() - 1);
const uint8_t* data = nullptr; }
DCHECK_NOT_NULL(source_);
const size_t byte_len = source_->GetMoreData(&data); size_t Utf8ExternalStreamingStream::FillBuffer(size_t position) {
if (V8_UNLIKELY(byte_len == 0)) { buffer_cursor_ = buffer_;
delete[] data; buffer_end_ = buffer_;
return nullptr;
SearchPosition(position);
bool out_of_data = current_.chunk_no != chunks_.size() &&
chunks_[current_.chunk_no].length == 0;
if (out_of_data) return 0;
// Fill the buffer, until we have at least one char (or are out of data).
// (The embedder might give us 1-byte blocks within a utf-8 char, so we
// can't guarantee progress with one chunk. Thus we iterate.)
while (!out_of_data && buffer_cursor_ == buffer_end_) {
// At end of current data, but there might be more? Then fetch it.
if (current_.chunk_no == chunks_.size()) {
out_of_data = !FetchChunk();
} }
bool odd_start = (byte_pos_ % 2) == 1; FillBufferFromCurrentChunk();
size_t char_pos = byte_pos_ / 2;
byte_pos_ += byte_len;
return Chunk::CreateTwoByte(data, byte_len, char_pos, odd_start);
} }
private: DCHECK_EQ(current_.pos.chars - position,
ScriptCompiler::ExternalSourceStream* const source_; static_cast<size_t>(buffer_end_ - buffer_cursor_));
size_t byte_pos_ = 0; return buffer_end_ - buffer_cursor_;
}
// ----------------------------------------------------------------------------
// Chunks - helper for One- + TwoByteExternalStreamingStream
namespace {
struct Chunk {
const uint8_t* data;
size_t byte_length;
size_t byte_pos;
}; };
// ChunkSource that decodes incoming utf8 to two-byte chunks if needed. typedef std::vector<struct Chunk> Chunks;
// Byte Order Mark at the beginning of the stream is skipped.
// ASCII only chunks are kept as one-byte chunks.
// Performance is optimized for source files with mostly ASCII characters.
class Utf8ChunkSource : public ChunkSource {
public:
explicit Utf8ChunkSource(ScriptCompiler::ExternalSourceStream* source)
: source_(source),
incomplete_char_(unibrow::Utf8::Utf8IncrementalBuffer(0)) {}
protected: void DeleteChunks(Chunks& chunks) {
size_t ComputeAsciiPrefixLength(const uint8_t* data, size_t byte_length) { for (size_t i = 0; i < chunks.size(); i++) delete[] chunks[i].data;
if (incomplete_char_ != unibrow::Utf8::Utf8IncrementalBuffer(0)) { }
return 0;
} // Return the chunk index for the chunk containing position.
for (size_t i = 0; i < byte_length; ++i) { // If position is behind the end of the stream, the index of the last,
if (data[i] > unibrow::Utf8::kMaxOneByteChar) { // zero-length chunk is returned.
return i; size_t FindChunk(Chunks& chunks, ScriptCompiler::ExternalSourceStream* source,
} size_t position, RuntimeCallStats* stats) {
} size_t end_pos =
return byte_length; chunks.empty() ? 0 : (chunks.back().byte_pos + chunks.back().byte_length);
}
size_t ConvertToUtf16(const uint8_t* data, size_t byte_length, // Get more data if needed. We usually won't enter the loop body.
size_t ascii_prefix_len, const uint8_t** result) { bool out_of_data = !chunks.empty() && chunks.back().byte_length == 0;
DCHECK_IMPLIES(ascii_prefix_len != 0, {
incomplete_char_ == unibrow::Utf8::Utf8IncrementalBuffer(0)); RuntimeCallTimerScope scope(stats, &RuntimeCallStats::GetMoreDataCallback);
// We get at most 1 character per byte. while (!out_of_data && end_pos <= position + 1) {
// Only exception is the first byte of chunk which may finish an incomplete const uint8_t* chunk = nullptr;
// surrogate pair, hence addition of 1. size_t len = source->GetMoreData(&chunk);
uc16* decoded_data = new uc16[byte_length + 1];
i::CopyCharsUnsigned(decoded_data, data, ascii_prefix_len); chunks.push_back({chunk, len, end_pos});
if (ascii_prefix_len > 0) { end_pos += len;
is_at_first_char_ = false; out_of_data = (len == 0);
}
size_t decoded_len = ascii_prefix_len;
for (size_t i = ascii_prefix_len; i < byte_length; ++i) {
unibrow::uchar t =
unibrow::Utf8::ValueOfIncremental(data[i], &incomplete_char_);
if (t == unibrow::Utf8::kIncomplete) continue;
if (V8_LIKELY(t < kUtf8Bom)) {
decoded_data[decoded_len++] = static_cast<uc16>(t);
} else if (V8_UNLIKELY(is_at_first_char_ && t == kUtf8Bom)) {
// Skip BOM at the beginning of the stream
is_at_first_char_ = false;
} else if (t <= unibrow::Utf16::kMaxNonSurrogateCharCode) {
decoded_data[decoded_len++] = static_cast<uc16>(t);
} else {
decoded_data[decoded_len++] = unibrow::Utf16::LeadSurrogate(t);
decoded_data[decoded_len++] = unibrow::Utf16::TrailSurrogate(t);
}
} }
*result = reinterpret_cast<uint8_t*>(decoded_data);
return 2 * decoded_len;
} }
// Skip possible BOM at the beginning of the stream. // Here, we should always have at least one chunk, and we either have the
// Return number of bytes skipped. // chunk we were looking for, or we're out of data. Also, out_of_data and
size_t SkipBOM(const uint8_t* data, size_t byte_length) { // end_pos are current (and designate whether we have exhausted the stream,
if (V8_UNLIKELY(is_at_first_char_ && byte_length >= 3 && // and the length of data received so far, respectively).
data[0] == kUtf8BomBytes[0] && DCHECK(!chunks.empty());
data[1] == kUtf8BomBytes[1] && DCHECK_EQ(end_pos, chunks.back().byte_pos + chunks.back().byte_length);
data[2] == kUtf8BomBytes[2])) { DCHECK_EQ(out_of_data, chunks.back().byte_length == 0);
return 3; DCHECK(position < end_pos || out_of_data);
}
return 0; // Edge case: position is behind the end of stream: Return the last (length 0)
// chunk to indicate the end of the stream.
if (position >= end_pos) {
DCHECK(out_of_data);
return chunks.size() - 1;
} }
Chunk* GetNextChunk() override { // We almost always 'stream', meaning we want data from the last chunk, so
const uint8_t* data = nullptr; // let's look at chunks back-to-front.
size_t byte_length = 0; size_t chunk_no = chunks.size() - 1;
if (!is_at_end_) { while (chunks[chunk_no].byte_pos > position) {
byte_length = source_->GetMoreData(&data); DCHECK_NE(chunk_no, 0u);
} chunk_no--;
// end of stream
if (V8_UNLIKELY(byte_length == 0)) {
delete[] data;
is_at_end_ = true;
unibrow::uchar t =
unibrow::Utf8::ValueOfIncrementalFinish(&incomplete_char_);
if (t != unibrow::Utf8::kBufferEmpty) {
DCHECK(t < unibrow::Utf16::kMaxNonSurrogateCharCode);
uc16* char_data = new uc16[1];
char_data[0] = static_cast<uc16>(t);
return Chunk::CreateTwoByte(reinterpret_cast<uint8_t*>(char_data), 2,
char_pos_++, false);
}
return nullptr;
}
size_t skipped = SkipBOM(data, byte_length);
if (V8_UNLIKELY(skipped == byte_length)) {
is_at_first_char_ = false;
delete[] data;
return GetNextChunk();
}
size_t ascii_prefix_len =
ComputeAsciiPrefixLength(data + skipped, byte_length - skipped);
bool is_ascii_only = ascii_prefix_len == (byte_length - skipped);
if (V8_LIKELY(is_ascii_only)) {
is_at_first_char_ = false;
Chunk* chunk =
Chunk::CreateOneByte(data, byte_length, char_pos_, skipped);
char_pos_ += chunk->char_length;
return chunk;
}
const uint8_t* decoded_data;
size_t decoded_byte_len = ConvertToUtf16(
data + skipped, byte_length - skipped, ascii_prefix_len, &decoded_data);
delete[] data;
if (V8_UNLIKELY(decoded_byte_len == 0)) {
delete[] decoded_data;
return GetNextChunk();
}
is_at_first_char_ = false;
Chunk* chunk =
Chunk::CreateTwoByte(decoded_data, decoded_byte_len, char_pos_, false);
char_pos_ += chunk->char_length;
return chunk;
} }
DCHECK_LE(chunks[chunk_no].byte_pos, position);
DCHECK_LT(position, chunks[chunk_no].byte_pos + chunks[chunk_no].byte_length);
return chunk_no;
}
} // anonymous namespace
// ----------------------------------------------------------------------------
// OneByteExternalStreamingStream
//
// A stream of latin-1 encoded, chunked data.
class OneByteExternalStreamingStream : public BufferedUtf16CharacterStream {
public:
explicit OneByteExternalStreamingStream(
ScriptCompiler::ExternalSourceStream* source, RuntimeCallStats* stats)
: source_(source), stats_(stats) {}
~OneByteExternalStreamingStream() override { DeleteChunks(chunks_); }
bool can_access_heap() override { return false; }
protected:
size_t FillBuffer(size_t position) override;
private: private:
Chunks chunks_;
ScriptCompiler::ExternalSourceStream* source_; ScriptCompiler::ExternalSourceStream* source_;
unibrow::Utf8::Utf8IncrementalBuffer incomplete_char_; RuntimeCallStats* stats_;
bool is_at_first_char_ = true;
bool is_at_end_ = false;
size_t char_pos_ = 0;
}; };
} // anonymous namespace size_t OneByteExternalStreamingStream::FillBuffer(size_t position) {
const Chunk& chunk = chunks_[FindChunk(chunks_, source_, position, stats_)];
if (chunk.byte_length == 0) return 0;
size_t start_pos = position - chunk.byte_pos;
size_t len = i::Min(kBufferSize, chunk.byte_length - start_pos);
i::CopyCharsUnsigned(buffer_, chunk.data + start_pos, len);
return len;
}
#if !(V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64)
// ----------------------------------------------------------------------------
// TwoByteExternalStreamingStream
//
// A stream of ucs-2 data, delivered in chunks. Chunks may be 'cut' into the
// middle of characters (or even contain only one byte), which adds a bit
// of complexity. This stream avoid all data copying, except for characters
// that cross chunk boundaries.
// A stream of chunked data class TwoByteExternalStreamingStream : public Utf16CharacterStream {
class ExternalStreamingStream : public BufferedUtf16CharacterStream {
public: public:
explicit ExternalStreamingStream(std::unique_ptr<ChunkSource> source, explicit TwoByteExternalStreamingStream(
RuntimeCallStats* stats) ScriptCompiler::ExternalSourceStream* source, RuntimeCallStats* stats);
: chunks_(new Chunks(std::move(source), stats)) {} ~TwoByteExternalStreamingStream() override;
bool can_access_heap() override { return false; } bool can_access_heap() override { return false; }
protected: protected:
bool ReadBlock() override; bool ReadBlock() override;
size_t FillBuffer(size_t position) override;
std::unique_ptr<ChunksView> chunks_; Chunks chunks_;
ScriptCompiler::ExternalSourceStream* source_;
RuntimeCallStats* stats_;
uc16 one_char_buffer_;
}; };
bool ExternalStreamingStream::ReadBlock() { TwoByteExternalStreamingStream::TwoByteExternalStreamingStream(
ScriptCompiler::ExternalSourceStream* source, RuntimeCallStats* stats)
: Utf16CharacterStream(&one_char_buffer_, &one_char_buffer_,
&one_char_buffer_, 0),
source_(source),
stats_(stats),
one_char_buffer_(0) {}
TwoByteExternalStreamingStream::~TwoByteExternalStreamingStream() {
DeleteChunks(chunks_);
}
bool TwoByteExternalStreamingStream::ReadBlock() {
size_t position = pos(); size_t position = pos();
// Find chunk in which the position belongs
const Chunk* chunk = chunks_->SeekToEndOf(position);
// Out of data? Return false. // We'll search for the 2nd byte of our character, to make sure we
if (!chunk) { // have enough data for at least one character.
size_t chunk_no = FindChunk(chunks_, source_, 2 * position + 1, stats_);
// Out of data? Return 0.
if (chunks_[chunk_no].byte_length == 0) {
buffer_pos_ = position; buffer_pos_ = position;
buffer_cursor_ = buffer_end_ = buffer_start_; buffer_cursor_ = buffer_start_;
buffer_end_ = buffer_start_;
return false; return false;
} }
bool odd_start = chunk->first_char_start_offset == 1; Chunk& current = chunks_[chunk_no];
if (odd_start || chunk->type != Chunk::TWO_BYTE) { // Annoying edge case: Chunks may not be 2-byte aligned, meaning that a
return BufferedUtf16CharacterStream::ReadBlock(); // character may be split between the previous and the current chunk.
// If we find such a lonely byte at the beginning of the chunk, we'll use
// one_char_buffer_ to hold the full character.
bool lonely_byte = (chunks_[chunk_no].byte_pos == (2 * position + 1));
if (lonely_byte) {
DCHECK_NE(chunk_no, 0u);
Chunk& previous_chunk = chunks_[chunk_no - 1];
#ifdef V8_TARGET_BIG_ENDIAN
uc16 character = current.data[0] |
previous_chunk.data[previous_chunk.byte_length - 1] << 8;
#else
uc16 character = previous_chunk.data[previous_chunk.byte_length - 1] |
current.data[0] << 8;
#endif
one_char_buffer_ = character;
buffer_pos_ = position;
buffer_start_ = &one_char_buffer_;
buffer_cursor_ = &one_char_buffer_;
buffer_end_ = &one_char_buffer_ + 1;
return true;
} }
// Aligned access is important on MIPS and ARM. // Common case: character is in current chunk.
DCHECK_EQ((reinterpret_cast<uintptr_t>(chunk->data.get()) % 2), 0); DCHECK_LE(current.byte_pos, 2 * position);
DCHECK_LT(2 * position + 1, current.byte_pos + current.byte_length);
buffer_start_ = reinterpret_cast<const uint16_t*>(chunk->data.get());
buffer_end_ = buffer_start_ + chunk->char_length; // Determine # of full ucs-2 chars in stream, and whether we started on an odd
buffer_pos_ = chunk->char_pos; // byte boundary.
bool odd_start = (current.byte_pos % 2) == 1;
size_t number_chars = (current.byte_length - odd_start) / 2;
// Point the buffer_*_ members into the current chunk and set buffer_cursor_
// to point to position. Be careful when converting the byte positions (in
// Chunk) to the ucs-2 character positions (in buffer_*_ members).
buffer_start_ = reinterpret_cast<const uint16_t*>(current.data + odd_start);
buffer_end_ = buffer_start_ + number_chars;
buffer_pos_ = (current.byte_pos + odd_start) / 2;
buffer_cursor_ = buffer_start_ + (position - buffer_pos_); buffer_cursor_ = buffer_start_ + (position - buffer_pos_);
DCHECK_EQ(position, pos()); DCHECK_EQ(position, pos());
return true; return true;
} }
size_t ExternalStreamingStream::FillBuffer(size_t position) { #else
size_t copied_bytes = 0;
const Chunk* chunk = chunks_->SeekToStartOf(position); // ----------------------------------------------------------------------------
// TwoByteExternalBufferedStream
//
// This class is made specifically to address unaligned access to 16-bit data
// in MIPS and ARM architectures. It replaces class
// TwoByteExternalStreamingStream which in some cases does have unaligned
// accesse to 16-bit data
class TwoByteExternalBufferedStream : public Utf16CharacterStream {
public:
explicit TwoByteExternalBufferedStream(
ScriptCompiler::ExternalSourceStream* source, RuntimeCallStats* stats);
~TwoByteExternalBufferedStream();
bool can_access_heap() override { return false; }
protected:
static const size_t kBufferSize = 512;
bool ReadBlock() override;
// FillBuffer should read up to kBufferSize characters at position and store
// them into buffer_[0..]. It returns the number of characters stored.
size_t FillBuffer(size_t position, size_t chunk_no);
// Fixed sized buffer that this class reads from.
// The base class' buffer_start_ should always point to buffer_.
uc16 buffer_[kBufferSize];
Chunks chunks_;
ScriptCompiler::ExternalSourceStream* source_;
RuntimeCallStats* stats_;
};
TwoByteExternalBufferedStream::TwoByteExternalBufferedStream(
ScriptCompiler::ExternalSourceStream* source, RuntimeCallStats* stats)
: Utf16CharacterStream(buffer_, buffer_, buffer_, 0),
source_(source),
stats_(stats) {}
TwoByteExternalBufferedStream::~TwoByteExternalBufferedStream() {
DeleteChunks(chunks_);
}
bool TwoByteExternalBufferedStream::ReadBlock() {
size_t position = pos();
// Find chunk in which the position belongs
size_t chunk_no = FindChunk(chunks_, source_, 2 * position + 1, stats_);
// Out of data? Return 0. // Out of data? Return 0.
if (!chunk) { if (chunks_[chunk_no].byte_length == 0) {
return 0; buffer_pos_ = position;
buffer_cursor_ = buffer_start_;
buffer_end_ = buffer_start_;
return false;
} }
size_t offset = chunk->OffsetOf(position);
while (copied_bytes < kBufferSizeInBytes && chunk) { Chunk& current = chunks_[chunk_no];
// Stop copying if next chunk could be used directly and at least one char
// was copied. bool odd_start = current.byte_pos % 2;
if (chunk->type == Chunk::TWO_BYTE && copied_bytes > 2 && // Common case: character is in current chunk.
chunk->first_char_start_offset == 0) { DCHECK_LE(current.byte_pos, 2 * position + odd_start);
break; DCHECK_LT(2 * position + 1, current.byte_pos + current.byte_length);
// If character starts on odd address copy text in buffer so there is always
// aligned access to characters. This is important on MIPS and ARM
// architectures. Otherwise read characters from memory directly.
if (!odd_start) {
buffer_start_ = reinterpret_cast<const uint16_t*>(current.data);
size_t number_chars = current.byte_length / 2;
buffer_end_ = buffer_start_ + number_chars;
buffer_pos_ = current.byte_pos / 2;
buffer_cursor_ = buffer_start_ + (position - buffer_pos_);
DCHECK_EQ(position, pos());
return true;
} else {
buffer_start_ = buffer_;
buffer_pos_ = position;
buffer_cursor_ = buffer_;
buffer_end_ = buffer_ + FillBuffer(position, chunk_no);
DCHECK_EQ(pos(), position);
DCHECK_LE(buffer_end_, buffer_start_ + kBufferSize);
return buffer_cursor_ < buffer_end_;
}
}
size_t TwoByteExternalBufferedStream::FillBuffer(size_t position,
size_t chunk_no) {
DCHECK_EQ(chunks_[chunk_no].byte_pos % 2, 1u);
bool odd_start = true;
// Align buffer_pos_ to the size of the buffer.
{
size_t new_pos = position / kBufferSize * kBufferSize;
if (new_pos != position) {
chunk_no = FindChunk(chunks_, source_, 2 * new_pos + 1, stats_);
buffer_pos_ = new_pos;
buffer_cursor_ = buffer_start_ + (position - buffer_pos_);
position = new_pos;
odd_start = chunks_[chunk_no].byte_pos % 2;
}
}
Chunk* current = &chunks_[chunk_no];
// Annoying edge case: Chunks may not be 2-byte aligned, meaning that a
// character may be split between the previous and the current chunk.
// If we find such a lonely byte at the beginning of the chunk, we'll copy
// it to the first byte in buffer_.
size_t totalLength = 0;
bool lonely_byte = (current->byte_pos == (2 * position + 1));
if (lonely_byte) {
DCHECK_NE(chunk_no, 0u);
Chunk& previous_chunk = chunks_[chunk_no - 1];
*reinterpret_cast<uint8_t*>(buffer_) =
previous_chunk.data[previous_chunk.byte_length - 1];
totalLength++;
}
// Common case: character is in current chunk.
DCHECK_LE(current->byte_pos, 2 * position + odd_start);
DCHECK_LT(2 * position + 1, current->byte_pos + current->byte_length);
// Copy characters from current chunk starting from chunk_pos to the end of
// buffer or chunk.
size_t chunk_pos = position - current->byte_pos / 2;
size_t start_offset = odd_start && chunk_pos != 0;
size_t bytes_to_move =
i::Min(2 * kBufferSize - lonely_byte,
current->byte_length - 2 * chunk_pos + start_offset);
i::MemMove(reinterpret_cast<uint8_t*>(buffer_) + lonely_byte,
current->data + 2 * chunk_pos - start_offset, bytes_to_move);
// Fill up the rest of the buffer if there is space and data left.
totalLength += bytes_to_move;
position = (current->byte_pos + current->byte_length) / 2;
if (position - buffer_pos_ < kBufferSize) {
chunk_no = FindChunk(chunks_, source_, 2 * position + 1, stats_);
current = &chunks_[chunk_no];
odd_start = current->byte_pos % 2;
bytes_to_move = i::Min(2 * kBufferSize - totalLength, current->byte_length);
while (bytes_to_move) {
// Common case: character is in current chunk.
DCHECK_LE(current->byte_pos, 2 * position + odd_start);
DCHECK_LT(2 * position + 1, current->byte_pos + current->byte_length);
i::MemMove(reinterpret_cast<uint8_t*>(buffer_) + totalLength,
current->data, bytes_to_move);
totalLength += bytes_to_move;
position = (current->byte_pos + current->byte_length) / 2;
chunk_no = FindChunk(chunks_, source_, 2 * position + 1, stats_);
current = &chunks_[chunk_no];
odd_start = current->byte_pos % 2;
bytes_to_move =
i::Min(2 * kBufferSize - totalLength, current->byte_length);
} }
size_t space_left = kBufferSizeInBytes - copied_bytes;
DCHECK_IMPLIES(chunk->type == Chunk::ONE_BYTE, (copied_bytes % 2) == 0);
copied_bytes += chunk->CopyToBuffer(
reinterpret_cast<uint8_t*>(buffer_) + copied_bytes, space_left, offset);
// chunk after the first one are copied from beginning
offset = 0;
chunk = chunks_->MoveToNext();
} }
return copied_bytes / 2; return totalLength / 2;
} }
#endif
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// ScannerStream: Create stream instances. // ScannerStream: Create stream instances.
...@@ -718,20 +866,20 @@ Utf16CharacterStream* ScannerStream::For( ...@@ -718,20 +866,20 @@ Utf16CharacterStream* ScannerStream::For(
ScriptCompiler::ExternalSourceStream* source_stream, ScriptCompiler::ExternalSourceStream* source_stream,
v8::ScriptCompiler::StreamedSource::Encoding encoding, v8::ScriptCompiler::StreamedSource::Encoding encoding,
RuntimeCallStats* stats) { RuntimeCallStats* stats) {
std::unique_ptr<ChunkSource> source;
switch (encoding) { switch (encoding) {
case v8::ScriptCompiler::StreamedSource::TWO_BYTE: case v8::ScriptCompiler::StreamedSource::TWO_BYTE:
source.reset(new TwoByteChunkSource(source_stream)); #if !(V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64)
break; return new TwoByteExternalStreamingStream(source_stream, stats);
#else
return new TwoByteExternalBufferedStream(source_stream, stats);
#endif
case v8::ScriptCompiler::StreamedSource::ONE_BYTE: case v8::ScriptCompiler::StreamedSource::ONE_BYTE:
source.reset(new OneByteChunkSource(source_stream)); return new OneByteExternalStreamingStream(source_stream, stats);
break;
case v8::ScriptCompiler::StreamedSource::UTF8: case v8::ScriptCompiler::StreamedSource::UTF8:
source.reset(new Utf8ChunkSource(source_stream)); return new Utf8ExternalStreamingStream(source_stream, stats);
break;
} }
DCHECK_NOT_NULL(source.get()); UNREACHABLE();
return new ExternalStreamingStream(std::move(source), stats); return nullptr;
} }
} // namespace internal } // namespace internal
......
...@@ -39,13 +39,6 @@ class ChunkSource : public v8::ScriptCompiler::ExternalSourceStream { ...@@ -39,13 +39,6 @@ class ChunkSource : public v8::ScriptCompiler::ExternalSourceStream {
} }
chunks_.push_back({nullptr, 0}); chunks_.push_back({nullptr, 0});
} }
ChunkSource(const uint8_t* data, size_t len, size_t chunk_size)
: current_(0) {
for (size_t i = 0; i < len; i += chunk_size) {
chunks_.push_back({data + i, i::Min(chunk_size, len - i)});
}
chunks_.push_back({nullptr, 0});
}
~ChunkSource() {} ~ChunkSource() {}
bool SetBookmark() override { return false; } bool SetBookmark() override { return false; }
void ResetToBookmark() override {} void ResetToBookmark() override {}
...@@ -445,18 +438,6 @@ TEST(CharacterStreams) { ...@@ -445,18 +438,6 @@ TEST(CharacterStreams) {
TestCharacterStreams(buffer, arraysize(buffer) - 1, 576, 3298); TestCharacterStreams(buffer, arraysize(buffer) - 1, 576, 3298);
} }
TEST(Uft8MultipleBOMChunks) {
const char* chunks = "\xef\xbb\xbf\0\xef\xbb\xbf\0\xef\xbb\xbf\0a\0";
const uint16_t unicode[] = {0xFEFF, 0xFEFF, 97};
ChunkSource chunk_source(chunks);
std::unique_ptr<i::Utf16CharacterStream> stream(i::ScannerStream::For(
&chunk_source, v8::ScriptCompiler::StreamedSource::UTF8, nullptr));
for (size_t i = 0; i < arraysize(unicode); i++) {
CHECK_EQ(unicode[i], stream->Advance());
}
CHECK_EQ(i::Utf16CharacterStream::kEndOfInput, stream->Advance());
}
// Regression test for crbug.com/651333. Read invalid utf-8. // Regression test for crbug.com/651333. Read invalid utf-8.
TEST(Regress651333) { TEST(Regress651333) {
const uint8_t bytes[] = const uint8_t bytes[] =
...@@ -498,17 +479,10 @@ TEST(Regress6377) { ...@@ -498,17 +479,10 @@ TEST(Regress6377) {
"\x80" // second chunk - one-byte end of 4-byte seq "\x80" // second chunk - one-byte end of 4-byte seq
"a\xc3\0" // and an 'a' + start of 2-byte seq "a\xc3\0" // and an 'a' + start of 2-byte seq
"\xbf\0", // third chunk - end of 2-byte seq "\xbf\0", // third chunk - end of 2-byte seq
// Regression test for
// https://bugs.chromium.org/p/chromium/issues/detail?id=758508
"X\xef\xbb\xbfX\0", // first chunk - no BOM but U+feff in the middle
}; };
const std::vector<std::vector<uint16_t>> unicode = { const std::vector<std::vector<uint16_t>> unicode = {
{0xd800, 0xdc00, 97}, {0xd800, 0xdc00, 97}, {0xfff, 97}, {0xff, 97}, {0xd800, 0xdc00, 97, 0xff},
{0xfff, 97}, };
{0xff, 97},
{0xd800, 0xdc00, 97, 0xff},
{88, 0xfeff, 88}};
CHECK_EQ(unicode.size(), sizeof(cases) / sizeof(cases[0])); CHECK_EQ(unicode.size(), sizeof(cases) / sizeof(cases[0]));
for (size_t c = 0; c < unicode.size(); ++c) { for (size_t c = 0; c < unicode.size(); ++c) {
ChunkSource chunk_source(cases[c]); ChunkSource chunk_source(cases[c]);
...@@ -525,36 +499,3 @@ TEST(Regress6377) { ...@@ -525,36 +499,3 @@ TEST(Regress6377) {
CHECK_EQ(i::Utf16CharacterStream::kEndOfInput, stream->Advance()); CHECK_EQ(i::Utf16CharacterStream::kEndOfInput, stream->Advance());
} }
} }
TEST(Regress724166) {
// Chunk size has to be multiple of kBufferCharacterSize
constexpr size_t kBufferCharacterSize = 512;
constexpr size_t kChunkSize = kBufferCharacterSize * 8;
constexpr size_t kChunks = 4;
uint8_t buffer[kChunkSize * kChunks];
for (size_t j = 0; j < kChunks; ++j) {
for (size_t i = 0; i < kChunkSize; ++i) {
buffer[kChunkSize * j + i] = (i % 0x7e) + 1;
}
}
// Add BOM at the beginning
buffer[0] = '\xef';
buffer[1] = '\xbb';
buffer[2] = '\xbf';
ChunkSource chunk_source(buffer, arraysize(buffer), kChunkSize);
std::unique_ptr<i::Utf16CharacterStream> stream(i::ScannerStream::For(
&chunk_source, v8::ScriptCompiler::StreamedSource::UTF8, nullptr));
for (size_t i = 0; i < arraysize(buffer) - 3; ++i) {
CHECK_EQ(static_cast<i::uc32>(buffer[i + 3]), stream->Advance());
}
CHECK_EQ(i::Utf16CharacterStream::kEndOfInput, stream->Advance());
for (int z = -8; z < 8; ++z) {
for (size_t j = kBufferCharacterSize + z; j < arraysize(buffer);
j += kBufferCharacterSize) {
stream->Seek(j);
for (size_t i = j; i < arraysize(buffer) - 3; ++i) {
CHECK_EQ(static_cast<i::uc32>(buffer[i + 3]), stream->Advance());
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment