2012-08-28 09:37:41 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2013-03-07 11:12:26 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Check that we can traverse very deep stacks of ConsStrings using
|
2013-01-03 09:18:01 +00:00
|
|
|
// StringCharacterStram. Check that Get(int) works on very deep stacks
|
2008-08-22 13:33:59 +00:00
|
|
|
// of ConsStrings. These operations may not be very fast, but they
|
|
|
|
// should be possible without getting errors due to too deep recursion.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2018-07-23 11:42:37 +00:00
|
|
|
#include "src/api-inl.h"
|
2018-12-18 10:21:08 +00:00
|
|
|
#include "src/base/platform/elapsed-timer.h"
|
2018-04-09 19:11:22 +00:00
|
|
|
#include "src/heap/factory.h"
|
2015-04-16 07:01:20 +00:00
|
|
|
#include "src/messages.h"
|
2017-01-09 13:43:28 +00:00
|
|
|
#include "src/objects-inl.h"
|
2014-10-08 14:55:03 +00:00
|
|
|
#include "src/unicode-decoder.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2016-09-27 13:52:59 +00:00
|
|
|
#include "test/cctest/heap/heap-utils.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2013-08-14 12:40:44 +00:00
|
|
|
// Adapted from http://en.wikipedia.org/wiki/Multiply-with-carry
|
2013-09-10 11:13:55 +00:00
|
|
|
class MyRandomNumberGenerator {
|
2013-08-14 12:40:44 +00:00
|
|
|
public:
|
2013-09-10 11:13:55 +00:00
|
|
|
MyRandomNumberGenerator() {
|
2013-08-14 12:40:44 +00:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2017-12-02 00:30:37 +00:00
|
|
|
void init(uint32_t seed = 0x5688C73E) {
|
|
|
|
static const uint32_t phi = 0x9E3779B9;
|
2013-08-14 12:40:44 +00:00
|
|
|
c = 362436;
|
|
|
|
i = kQSize-1;
|
|
|
|
Q[0] = seed;
|
|
|
|
Q[1] = seed + phi;
|
|
|
|
Q[2] = seed + phi + phi;
|
|
|
|
for (unsigned j = 3; j < kQSize; j++) {
|
|
|
|
Q[j] = Q[j - 3] ^ Q[j - 2] ^ phi ^ j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t next() {
|
|
|
|
uint64_t a = 18782;
|
2017-12-02 00:30:37 +00:00
|
|
|
uint32_t r = 0xFFFFFFFE;
|
2013-08-14 12:40:44 +00:00
|
|
|
i = (i + 1) & (kQSize-1);
|
|
|
|
uint64_t t = a * Q[i] + c;
|
|
|
|
c = (t >> 32);
|
|
|
|
uint32_t x = static_cast<uint32_t>(t + c);
|
|
|
|
if (x < c) {
|
|
|
|
x++;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
return (Q[i] = r - x);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t next(int max) {
|
|
|
|
return next() % max;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool next(double threshold) {
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK(threshold >= 0.0 && threshold <= 1.0);
|
2013-08-14 12:40:44 +00:00
|
|
|
if (threshold == 1.0) return true;
|
|
|
|
if (threshold == 0.0) return false;
|
|
|
|
uint32_t value = next() % 100000;
|
|
|
|
return threshold > static_cast<double>(value)/100000.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const uint32_t kQSize = 4096;
|
|
|
|
uint32_t Q[kQSize];
|
|
|
|
uint32_t c;
|
|
|
|
uint32_t i;
|
|
|
|
};
|
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2017-09-21 03:29:52 +00:00
|
|
|
namespace test_strings {
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
static const int DEEP_DEPTH = 8 * 1024;
|
|
|
|
static const int SUPER_DEEP_DEPTH = 80 * 1024;
|
|
|
|
|
|
|
|
|
2014-02-03 07:29:23 +00:00
|
|
|
class Resource: public v8::String::ExternalStringResource {
|
2009-09-15 11:11:09 +00:00
|
|
|
public:
|
2014-02-03 07:29:23 +00:00
|
|
|
Resource(const uc16* data, size_t length): data_(data), length_(length) {}
|
2018-09-14 15:34:02 +00:00
|
|
|
~Resource() override { i::DeleteArray(data_); }
|
|
|
|
const uint16_t* data() const override { return data_; }
|
|
|
|
size_t length() const override { return length_; }
|
2009-09-15 11:11:09 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const uc16* data_;
|
|
|
|
size_t length_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
class OneByteResource : public v8::String::ExternalOneByteStringResource {
|
2009-12-02 12:58:10 +00:00
|
|
|
public:
|
2014-09-10 12:38:12 +00:00
|
|
|
OneByteResource(const char* data, size_t length)
|
2014-02-03 07:29:23 +00:00
|
|
|
: data_(data), length_(length) {}
|
2018-09-14 15:34:02 +00:00
|
|
|
~OneByteResource() override { i::DeleteArray(data_); }
|
|
|
|
const char* data() const override { return data_; }
|
|
|
|
size_t length() const override { return length_; }
|
2009-12-02 12:58:10 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char* data_;
|
|
|
|
size_t length_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-12-11 10:22:15 +00:00
|
|
|
static void InitializeBuildingBlocks(Handle<String>* building_blocks,
|
|
|
|
int bb_length,
|
|
|
|
bool long_blocks,
|
2014-02-03 07:29:23 +00:00
|
|
|
MyRandomNumberGenerator* rng) {
|
2009-03-27 00:24:49 +00:00
|
|
|
// A list of pointers that we don't have any interest in cleaning up.
|
|
|
|
// If they are reachable from a root then leak detection won't complain.
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-04 10:30:05 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2012-12-11 10:22:15 +00:00
|
|
|
for (int i = 0; i < bb_length; i++) {
|
|
|
|
int len = rng->next(16);
|
|
|
|
int slice_head_chars = 0;
|
|
|
|
int slice_tail_chars = 0;
|
|
|
|
int slice_depth = 0;
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
if (rng->next(0.35)) slice_depth++;
|
|
|
|
}
|
|
|
|
// Must truncate something for a slice string. Loop until
|
|
|
|
// at least one end will be sliced.
|
|
|
|
while (slice_head_chars == 0 && slice_tail_chars == 0) {
|
|
|
|
slice_head_chars = rng->next(15);
|
|
|
|
slice_tail_chars = rng->next(12);
|
|
|
|
}
|
|
|
|
if (long_blocks) {
|
|
|
|
// Generate building blocks which will never be merged
|
|
|
|
len += ConsString::kMinLength + 1;
|
|
|
|
} else if (len > 14) {
|
2008-08-22 13:33:59 +00:00
|
|
|
len += 1234;
|
|
|
|
}
|
2012-12-11 10:22:15 +00:00
|
|
|
// Don't slice 0 length strings.
|
|
|
|
if (len == 0) slice_depth = 0;
|
|
|
|
int slice_length = slice_depth*(slice_head_chars + slice_tail_chars);
|
|
|
|
len += slice_length;
|
|
|
|
switch (rng->next(4)) {
|
2008-08-22 13:33:59 +00:00
|
|
|
case 0: {
|
|
|
|
uc16 buf[2000];
|
|
|
|
for (int j = 0; j < len; j++) {
|
2012-12-11 10:22:15 +00:00
|
|
|
buf[j] = rng->next(0x10000);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2014-04-17 13:27:02 +00:00
|
|
|
building_blocks[i] = factory->NewStringFromTwoByte(
|
|
|
|
Vector<const uc16>(buf, len)).ToHandleChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
for (int j = 0; j < len; j++) {
|
2009-03-17 09:33:06 +00:00
|
|
|
CHECK_EQ(buf[j], building_blocks[i]->Get(j));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: {
|
|
|
|
char buf[2000];
|
|
|
|
for (int j = 0; j < len; j++) {
|
2012-12-11 10:22:15 +00:00
|
|
|
buf[j] = rng->next(0x80);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2017-03-17 17:52:50 +00:00
|
|
|
building_blocks[i] =
|
|
|
|
factory->NewStringFromOneByte(OneByteVector(buf, len))
|
|
|
|
.ToHandleChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
for (int j = 0; j < len; j++) {
|
2009-03-17 09:33:06 +00:00
|
|
|
CHECK_EQ(buf[j], building_blocks[i]->Get(j));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
2014-02-03 07:29:23 +00:00
|
|
|
uc16* buf = NewArray<uc16>(len);
|
2008-08-22 13:33:59 +00:00
|
|
|
for (int j = 0; j < len; j++) {
|
2012-12-11 10:22:15 +00:00
|
|
|
buf[j] = rng->next(0x10000);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2014-02-03 07:29:23 +00:00
|
|
|
Resource* resource = new Resource(buf, len);
|
2015-12-07 15:27:40 +00:00
|
|
|
building_blocks[i] = v8::Utils::OpenHandle(
|
|
|
|
*v8::String::NewExternalTwoByte(CcTest::isolate(), resource)
|
|
|
|
.ToLocalChecked());
|
2008-08-22 13:33:59 +00:00
|
|
|
for (int j = 0; j < len; j++) {
|
2009-03-17 09:33:06 +00:00
|
|
|
CHECK_EQ(buf[j], building_blocks[i]->Get(j));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
2014-02-03 07:29:23 +00:00
|
|
|
char* buf = NewArray<char>(len);
|
2008-08-22 13:33:59 +00:00
|
|
|
for (int j = 0; j < len; j++) {
|
2012-12-13 15:39:01 +00:00
|
|
|
buf[j] = rng->next(0x80);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2014-09-10 12:38:12 +00:00
|
|
|
OneByteResource* resource = new OneByteResource(buf, len);
|
2015-12-07 15:27:40 +00:00
|
|
|
building_blocks[i] = v8::Utils::OpenHandle(
|
|
|
|
*v8::String::NewExternalOneByte(CcTest::isolate(), resource)
|
|
|
|
.ToLocalChecked());
|
2008-08-22 13:33:59 +00:00
|
|
|
for (int j = 0; j < len; j++) {
|
2009-03-17 09:33:06 +00:00
|
|
|
CHECK_EQ(buf[j], building_blocks[i]->Get(j));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-12-11 10:22:15 +00:00
|
|
|
for (int j = slice_depth; j > 0; j--) {
|
2013-06-04 10:30:05 +00:00
|
|
|
building_blocks[i] = factory->NewSubString(
|
2012-12-11 10:22:15 +00:00
|
|
|
building_blocks[i],
|
|
|
|
slice_head_chars,
|
|
|
|
building_blocks[i]->length() - slice_tail_chars);
|
|
|
|
}
|
|
|
|
CHECK(len == building_blocks[i]->length() + slice_length);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-11 10:22:15 +00:00
|
|
|
class ConsStringStats {
|
|
|
|
public:
|
|
|
|
ConsStringStats() {
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
void Reset();
|
|
|
|
void VerifyEqual(const ConsStringStats& that) const;
|
2014-04-29 13:09:31 +00:00
|
|
|
int leaves_;
|
|
|
|
int empty_leaves_;
|
|
|
|
int chars_;
|
|
|
|
int left_traversals_;
|
|
|
|
int right_traversals_;
|
2012-12-11 10:22:15 +00:00
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ConsStringStats);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void ConsStringStats::Reset() {
|
|
|
|
leaves_ = 0;
|
|
|
|
empty_leaves_ = 0;
|
|
|
|
chars_ = 0;
|
|
|
|
left_traversals_ = 0;
|
|
|
|
right_traversals_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConsStringStats::VerifyEqual(const ConsStringStats& that) const {
|
2014-04-29 13:09:31 +00:00
|
|
|
CHECK_EQ(this->leaves_, that.leaves_);
|
|
|
|
CHECK_EQ(this->empty_leaves_, that.empty_leaves_);
|
|
|
|
CHECK_EQ(this->chars_, that.chars_);
|
|
|
|
CHECK_EQ(this->left_traversals_, that.left_traversals_);
|
|
|
|
CHECK_EQ(this->right_traversals_, that.right_traversals_);
|
2012-12-11 10:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ConsStringGenerationData {
|
|
|
|
public:
|
2012-12-13 15:39:01 +00:00
|
|
|
static const int kNumberOfBuildingBlocks = 256;
|
2014-02-03 07:29:23 +00:00
|
|
|
explicit ConsStringGenerationData(bool long_blocks);
|
2012-12-11 10:22:15 +00:00
|
|
|
void Reset();
|
2012-12-13 15:39:01 +00:00
|
|
|
inline Handle<String> block(int offset);
|
|
|
|
inline Handle<String> block(uint32_t offset);
|
2012-12-11 10:22:15 +00:00
|
|
|
// Input variables.
|
|
|
|
double early_termination_threshold_;
|
|
|
|
double leftness_;
|
|
|
|
double rightness_;
|
|
|
|
double empty_leaf_threshold_;
|
2014-04-29 13:09:31 +00:00
|
|
|
int max_leaves_;
|
2012-12-11 10:22:15 +00:00
|
|
|
// Cached data.
|
2012-12-13 15:39:01 +00:00
|
|
|
Handle<String> building_blocks_[kNumberOfBuildingBlocks];
|
2018-11-27 00:48:42 +00:00
|
|
|
String empty_string_;
|
2013-09-10 11:13:55 +00:00
|
|
|
MyRandomNumberGenerator rng_;
|
2012-12-11 10:22:15 +00:00
|
|
|
// Stats.
|
|
|
|
ConsStringStats stats_;
|
2014-04-29 13:09:31 +00:00
|
|
|
int early_terminations_;
|
2012-12-11 10:22:15 +00:00
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ConsStringGenerationData);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-03 07:29:23 +00:00
|
|
|
ConsStringGenerationData::ConsStringGenerationData(bool long_blocks) {
|
2012-12-11 10:22:15 +00:00
|
|
|
rng_.init();
|
|
|
|
InitializeBuildingBlocks(
|
2014-02-03 07:29:23 +00:00
|
|
|
building_blocks_, kNumberOfBuildingBlocks, long_blocks, &rng_);
|
2018-07-04 09:10:05 +00:00
|
|
|
empty_string_ = ReadOnlyRoots(CcTest::heap()).empty_string();
|
2012-12-11 10:22:15 +00:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-13 15:39:01 +00:00
|
|
|
Handle<String> ConsStringGenerationData::block(uint32_t offset) {
|
|
|
|
return building_blocks_[offset % kNumberOfBuildingBlocks ];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<String> ConsStringGenerationData::block(int offset) {
|
|
|
|
CHECK_GE(offset, 0);
|
|
|
|
return building_blocks_[offset % kNumberOfBuildingBlocks];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-11 10:22:15 +00:00
|
|
|
void ConsStringGenerationData::Reset() {
|
|
|
|
early_termination_threshold_ = 0.01;
|
|
|
|
leftness_ = 0.75;
|
|
|
|
rightness_ = 0.75;
|
|
|
|
empty_leaf_threshold_ = 0.02;
|
|
|
|
max_leaves_ = 1000;
|
|
|
|
stats_.Reset();
|
|
|
|
early_terminations_ = 0;
|
2012-12-13 15:39:01 +00:00
|
|
|
rng_.init();
|
2012-12-11 10:22:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 00:48:42 +00:00
|
|
|
void AccumulateStats(ConsString cons_string, ConsStringStats* stats) {
|
2012-12-11 10:22:15 +00:00
|
|
|
int left_length = cons_string->first()->length();
|
|
|
|
int right_length = cons_string->second()->length();
|
|
|
|
CHECK(cons_string->length() == left_length + right_length);
|
|
|
|
// Check left side.
|
2012-12-13 15:39:01 +00:00
|
|
|
bool left_is_cons = cons_string->first()->IsConsString();
|
|
|
|
if (left_is_cons) {
|
2012-12-11 10:22:15 +00:00
|
|
|
stats->left_traversals_++;
|
2012-12-13 15:39:01 +00:00
|
|
|
AccumulateStats(ConsString::cast(cons_string->first()), stats);
|
2012-12-11 10:22:15 +00:00
|
|
|
} else {
|
|
|
|
CHECK_NE(left_length, 0);
|
|
|
|
stats->leaves_++;
|
|
|
|
stats->chars_ += left_length;
|
|
|
|
}
|
|
|
|
// Check right side.
|
|
|
|
if (cons_string->second()->IsConsString()) {
|
|
|
|
stats->right_traversals_++;
|
2012-12-13 15:39:01 +00:00
|
|
|
AccumulateStats(ConsString::cast(cons_string->second()), stats);
|
2012-12-11 10:22:15 +00:00
|
|
|
} else {
|
2012-12-13 15:39:01 +00:00
|
|
|
if (right_length == 0) {
|
|
|
|
stats->empty_leaves_++;
|
|
|
|
CHECK(!left_is_cons);
|
|
|
|
}
|
2012-12-11 10:22:15 +00:00
|
|
|
stats->leaves_++;
|
|
|
|
stats->chars_ += right_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 15:39:01 +00:00
|
|
|
void AccumulateStats(Handle<String> cons_string, ConsStringStats* stats) {
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_allocation;
|
2012-12-13 15:39:01 +00:00
|
|
|
if (cons_string->IsConsString()) {
|
|
|
|
return AccumulateStats(ConsString::cast(*cons_string), stats);
|
|
|
|
}
|
|
|
|
// This string got flattened by gc.
|
|
|
|
stats->chars_ += cons_string->length();
|
|
|
|
}
|
|
|
|
|
2018-11-27 00:48:42 +00:00
|
|
|
void AccumulateStatsWithOperator(ConsString cons_string,
|
|
|
|
ConsStringStats* stats) {
|
2014-10-23 05:57:01 +00:00
|
|
|
ConsStringIterator iter(cons_string);
|
2014-04-29 13:09:31 +00:00
|
|
|
int offset;
|
2018-11-27 00:48:42 +00:00
|
|
|
for (String string = iter.Next(&offset); !string.is_null();
|
|
|
|
string = iter.Next(&offset)) {
|
2012-12-19 13:27:20 +00:00
|
|
|
// Accumulate stats.
|
2014-04-29 13:09:31 +00:00
|
|
|
CHECK_EQ(0, offset);
|
2012-12-19 13:27:20 +00:00
|
|
|
stats->leaves_++;
|
|
|
|
stats->chars_ += string->length();
|
|
|
|
}
|
2012-12-11 10:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VerifyConsString(Handle<String> root, ConsStringGenerationData* data) {
|
|
|
|
// Verify basic data.
|
|
|
|
CHECK(root->IsConsString());
|
2014-04-29 13:09:31 +00:00
|
|
|
CHECK_EQ(root->length(), data->stats_.chars_);
|
2012-12-11 10:22:15 +00:00
|
|
|
// Recursive verify.
|
|
|
|
ConsStringStats stats;
|
2012-12-13 15:39:01 +00:00
|
|
|
AccumulateStats(ConsString::cast(*root), &stats);
|
2012-12-11 10:22:15 +00:00
|
|
|
stats.VerifyEqual(data->stats_);
|
|
|
|
// Iteratively verify.
|
|
|
|
stats.Reset();
|
2012-12-13 15:39:01 +00:00
|
|
|
AccumulateStatsWithOperator(ConsString::cast(*root), &stats);
|
2012-12-11 10:22:15 +00:00
|
|
|
// Don't see these. Must copy over.
|
|
|
|
stats.empty_leaves_ = data->stats_.empty_leaves_;
|
|
|
|
stats.left_traversals_ = data->stats_.left_traversals_;
|
|
|
|
stats.right_traversals_ = data->stats_.right_traversals_;
|
|
|
|
// Adjust total leaves to compensate.
|
|
|
|
stats.leaves_ += stats.empty_leaves_;
|
|
|
|
stats.VerifyEqual(data->stats_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<String> ConstructRandomString(ConsStringGenerationData* data,
|
|
|
|
unsigned max_recursion) {
|
2018-06-20 16:32:59 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
2012-12-11 10:22:15 +00:00
|
|
|
// Compute termination characteristics.
|
|
|
|
bool terminate = false;
|
|
|
|
bool flat = data->rng_.next(data->empty_leaf_threshold_);
|
|
|
|
bool terminate_early = data->rng_.next(data->early_termination_threshold_);
|
|
|
|
if (terminate_early) data->early_terminations_++;
|
|
|
|
// The obvious condition.
|
|
|
|
terminate |= max_recursion == 0;
|
|
|
|
// Flat cons string terminate by definition.
|
|
|
|
terminate |= flat;
|
|
|
|
// Cap for max leaves.
|
|
|
|
terminate |= data->stats_.leaves_ >= data->max_leaves_;
|
|
|
|
// Roll the dice.
|
|
|
|
terminate |= terminate_early;
|
|
|
|
// Compute termination characteristics for each side.
|
|
|
|
bool terminate_left = terminate || !data->rng_.next(data->leftness_);
|
|
|
|
bool terminate_right = terminate || !data->rng_.next(data->rightness_);
|
|
|
|
// Generate left string.
|
|
|
|
Handle<String> left;
|
|
|
|
if (terminate_left) {
|
2012-12-13 15:39:01 +00:00
|
|
|
left = data->block(data->rng_.next());
|
2012-12-11 10:22:15 +00:00
|
|
|
data->stats_.leaves_++;
|
|
|
|
data->stats_.chars_ += left->length();
|
|
|
|
} else {
|
|
|
|
data->stats_.left_traversals_++;
|
|
|
|
}
|
|
|
|
// Generate right string.
|
|
|
|
Handle<String> right;
|
|
|
|
if (terminate_right) {
|
2012-12-13 15:39:01 +00:00
|
|
|
right = data->block(data->rng_.next());
|
2012-12-11 10:22:15 +00:00
|
|
|
data->stats_.leaves_++;
|
|
|
|
data->stats_.chars_ += right->length();
|
|
|
|
} else {
|
|
|
|
data->stats_.right_traversals_++;
|
|
|
|
}
|
2012-12-13 15:39:01 +00:00
|
|
|
// Generate the necessary sub-nodes recursively.
|
|
|
|
if (!terminate_right) {
|
|
|
|
// Need to balance generation fairly.
|
|
|
|
if (!terminate_left && data->rng_.next(0.5)) {
|
|
|
|
left = ConstructRandomString(data, max_recursion - 1);
|
|
|
|
}
|
|
|
|
right = ConstructRandomString(data, max_recursion - 1);
|
|
|
|
}
|
|
|
|
if (!terminate_left && left.is_null()) {
|
|
|
|
left = ConstructRandomString(data, max_recursion - 1);
|
|
|
|
}
|
2012-12-11 10:22:15 +00:00
|
|
|
// Build the cons string.
|
2014-04-03 12:30:37 +00:00
|
|
|
Handle<String> root = factory->NewConsString(left, right).ToHandleChecked();
|
2012-12-11 10:22:15 +00:00
|
|
|
CHECK(root->IsConsString() && !root->IsFlat());
|
|
|
|
// Special work needed for flat string.
|
|
|
|
if (flat) {
|
|
|
|
data->stats_.empty_leaves_++;
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, root);
|
2012-12-11 10:22:15 +00:00
|
|
|
CHECK(root->IsConsString() && root->IsFlat());
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-13 15:39:01 +00:00
|
|
|
static Handle<String> ConstructLeft(
|
|
|
|
ConsStringGenerationData* data,
|
|
|
|
int depth) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Factory* factory = CcTest::i_isolate()->factory();
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> answer = factory->NewStringFromStaticChars("");
|
2012-12-13 15:39:01 +00:00
|
|
|
data->stats_.leaves_++;
|
|
|
|
for (int i = 0; i < depth; i++) {
|
|
|
|
Handle<String> block = data->block(i);
|
2014-04-03 12:30:37 +00:00
|
|
|
Handle<String> next =
|
|
|
|
factory->NewConsString(answer, block).ToHandleChecked();
|
2012-12-13 15:39:01 +00:00
|
|
|
if (next->IsConsString()) data->stats_.leaves_++;
|
|
|
|
data->stats_.chars_ += block->length();
|
|
|
|
answer = next;
|
|
|
|
}
|
|
|
|
data->stats_.left_traversals_ = data->stats_.leaves_ - 2;
|
|
|
|
return answer;
|
|
|
|
}
|
2012-12-11 10:22:15 +00:00
|
|
|
|
|
|
|
|
2012-12-13 15:39:01 +00:00
|
|
|
static Handle<String> ConstructRight(
|
|
|
|
ConsStringGenerationData* data,
|
|
|
|
int depth) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Factory* factory = CcTest::i_isolate()->factory();
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> answer = factory->NewStringFromStaticChars("");
|
2012-12-13 15:39:01 +00:00
|
|
|
data->stats_.leaves_++;
|
|
|
|
for (int i = depth - 1; i >= 0; i--) {
|
|
|
|
Handle<String> block = data->block(i);
|
2014-04-03 12:30:37 +00:00
|
|
|
Handle<String> next =
|
|
|
|
factory->NewConsString(block, answer).ToHandleChecked();
|
2012-12-13 15:39:01 +00:00
|
|
|
if (next->IsConsString()) data->stats_.leaves_++;
|
|
|
|
data->stats_.chars_ += block->length();
|
|
|
|
answer = next;
|
2012-12-11 10:22:15 +00:00
|
|
|
}
|
2012-12-13 15:39:01 +00:00
|
|
|
data->stats_.right_traversals_ = data->stats_.leaves_ - 2;
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<String> ConstructBalancedHelper(
|
|
|
|
ConsStringGenerationData* data,
|
|
|
|
int from,
|
|
|
|
int to) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Factory* factory = CcTest::i_isolate()->factory();
|
2012-12-13 15:39:01 +00:00
|
|
|
CHECK(to > from);
|
|
|
|
if (to - from == 1) {
|
|
|
|
data->stats_.chars_ += data->block(from)->length();
|
|
|
|
return data->block(from);
|
|
|
|
}
|
|
|
|
if (to - from == 2) {
|
|
|
|
data->stats_.chars_ += data->block(from)->length();
|
|
|
|
data->stats_.chars_ += data->block(from+1)->length();
|
2014-04-03 12:30:37 +00:00
|
|
|
return factory->NewConsString(data->block(from), data->block(from+1))
|
|
|
|
.ToHandleChecked();
|
2012-12-13 15:39:01 +00:00
|
|
|
}
|
|
|
|
Handle<String> part1 =
|
|
|
|
ConstructBalancedHelper(data, from, from + ((to - from) / 2));
|
|
|
|
Handle<String> part2 =
|
|
|
|
ConstructBalancedHelper(data, from + ((to - from) / 2), to);
|
|
|
|
if (part1->IsConsString()) data->stats_.left_traversals_++;
|
|
|
|
if (part2->IsConsString()) data->stats_.right_traversals_++;
|
2014-04-03 12:30:37 +00:00
|
|
|
return factory->NewConsString(part1, part2).ToHandleChecked();
|
2012-12-13 15:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<String> ConstructBalanced(
|
|
|
|
ConsStringGenerationData* data, int depth = DEEP_DEPTH) {
|
|
|
|
Handle<String> string = ConstructBalancedHelper(data, 0, depth);
|
|
|
|
data->stats_.leaves_ =
|
|
|
|
data->stats_.left_traversals_ + data->stats_.right_traversals_ + 2;
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Traverse(Handle<String> s1, Handle<String> s2) {
|
|
|
|
int i = 0;
|
2014-10-23 05:57:01 +00:00
|
|
|
StringCharacterStream character_stream_1(*s1);
|
|
|
|
StringCharacterStream character_stream_2(*s2);
|
2013-01-03 09:18:01 +00:00
|
|
|
while (character_stream_1.HasMore()) {
|
2012-12-13 15:39:01 +00:00
|
|
|
CHECK(character_stream_2.HasMore());
|
2013-01-03 09:18:01 +00:00
|
|
|
uint16_t c = character_stream_1.GetNext();
|
2012-12-13 15:39:01 +00:00
|
|
|
CHECK_EQ(c, character_stream_2.GetNext());
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
CHECK(!character_stream_1.HasMore());
|
|
|
|
CHECK(!character_stream_2.HasMore());
|
|
|
|
CHECK_EQ(s1->length(), i);
|
|
|
|
CHECK_EQ(s2->length(), i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void TraverseFirst(Handle<String> s1, Handle<String> s2, int chars) {
|
|
|
|
int i = 0;
|
2014-10-23 05:57:01 +00:00
|
|
|
StringCharacterStream character_stream_1(*s1);
|
|
|
|
StringCharacterStream character_stream_2(*s2);
|
2013-01-03 09:18:01 +00:00
|
|
|
while (character_stream_1.HasMore() && i < chars) {
|
2012-12-13 15:39:01 +00:00
|
|
|
CHECK(character_stream_2.HasMore());
|
2013-01-03 09:18:01 +00:00
|
|
|
uint16_t c = character_stream_1.GetNext();
|
2012-12-13 15:39:01 +00:00
|
|
|
CHECK_EQ(c, character_stream_2.GetNext());
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
s1->Get(s1->length() - 1);
|
|
|
|
s2->Get(s2->length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Traverse) {
|
|
|
|
printf("TestTraverse\n");
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
2018-06-20 16:32:59 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2014-02-03 07:29:23 +00:00
|
|
|
ConsStringGenerationData data(false);
|
2012-12-13 15:39:01 +00:00
|
|
|
Handle<String> flat = ConstructBalanced(&data);
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, flat);
|
2012-12-13 15:39:01 +00:00
|
|
|
Handle<String> left_asymmetric = ConstructLeft(&data, DEEP_DEPTH);
|
|
|
|
Handle<String> right_asymmetric = ConstructRight(&data, DEEP_DEPTH);
|
|
|
|
Handle<String> symmetric = ConstructBalanced(&data);
|
|
|
|
printf("1\n");
|
|
|
|
Traverse(flat, symmetric);
|
|
|
|
printf("2\n");
|
|
|
|
Traverse(flat, left_asymmetric);
|
|
|
|
printf("3\n");
|
|
|
|
Traverse(flat, right_asymmetric);
|
|
|
|
printf("4\n");
|
|
|
|
Handle<String> left_deep_asymmetric =
|
|
|
|
ConstructLeft(&data, SUPER_DEEP_DEPTH);
|
|
|
|
Handle<String> right_deep_asymmetric =
|
|
|
|
ConstructRight(&data, SUPER_DEEP_DEPTH);
|
|
|
|
printf("5\n");
|
|
|
|
TraverseFirst(left_asymmetric, left_deep_asymmetric, 1050);
|
|
|
|
printf("6\n");
|
|
|
|
TraverseFirst(left_asymmetric, right_deep_asymmetric, 65536);
|
|
|
|
printf("7\n");
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, left_asymmetric);
|
2012-12-13 15:39:01 +00:00
|
|
|
printf("10\n");
|
|
|
|
Traverse(flat, left_asymmetric);
|
|
|
|
printf("11\n");
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, right_asymmetric);
|
2012-12-13 15:39:01 +00:00
|
|
|
printf("12\n");
|
|
|
|
Traverse(flat, right_asymmetric);
|
|
|
|
printf("14\n");
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, symmetric);
|
2012-12-13 15:39:01 +00:00
|
|
|
printf("15\n");
|
|
|
|
Traverse(flat, symmetric);
|
|
|
|
printf("16\n");
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, left_deep_asymmetric);
|
2012-12-13 15:39:01 +00:00
|
|
|
printf("18\n");
|
2012-12-11 10:22:15 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 11:23:07 +00:00
|
|
|
TEST(ConsStringWithEmptyFirstFlatten) {
|
|
|
|
printf("ConsStringWithEmptyFirstFlatten\n");
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
|
|
|
i::Handle<i::String> initial_fst =
|
|
|
|
isolate->factory()->NewStringFromAsciiChecked("fst012345");
|
|
|
|
i::Handle<i::String> initial_snd =
|
|
|
|
isolate->factory()->NewStringFromAsciiChecked("snd012345");
|
|
|
|
i::Handle<i::String> str = isolate->factory()
|
|
|
|
->NewConsString(initial_fst, initial_snd)
|
|
|
|
.ToHandleChecked();
|
|
|
|
CHECK(str->IsConsString());
|
|
|
|
auto cons = i::Handle<i::ConsString>::cast(str);
|
|
|
|
|
|
|
|
const int initial_length = cons->length();
|
|
|
|
|
|
|
|
// set_first / set_second does not update the length (which the heap verifier
|
|
|
|
// checks), so we need to ensure the length stays the same.
|
|
|
|
|
|
|
|
i::Handle<i::String> new_fst = isolate->factory()->empty_string();
|
|
|
|
i::Handle<i::String> new_snd =
|
|
|
|
isolate->factory()->NewStringFromAsciiChecked("snd012345012345678");
|
2018-07-11 13:38:20 +00:00
|
|
|
cons->set_first(isolate, *new_fst);
|
|
|
|
cons->set_second(isolate, *new_snd);
|
2016-11-10 11:23:07 +00:00
|
|
|
CHECK(!cons->IsFlat());
|
|
|
|
CHECK_EQ(initial_length, new_fst->length() + new_snd->length());
|
|
|
|
CHECK_EQ(initial_length, cons->length());
|
|
|
|
|
|
|
|
// Make sure Flatten doesn't alloc a new string.
|
|
|
|
DisallowHeapAllocation no_alloc;
|
2018-06-20 16:32:59 +00:00
|
|
|
i::Handle<i::String> flat = i::String::Flatten(isolate, cons);
|
2016-11-10 11:23:07 +00:00
|
|
|
CHECK(flat->IsFlat());
|
|
|
|
CHECK_EQ(initial_length, flat->length());
|
|
|
|
}
|
2012-12-11 10:22:15 +00:00
|
|
|
|
2018-11-27 00:48:42 +00:00
|
|
|
static void VerifyCharacterStream(String flat_string, String cons_string) {
|
2012-12-11 10:22:15 +00:00
|
|
|
// Do not want to test ConString traversal on flat string.
|
2012-12-13 15:39:01 +00:00
|
|
|
CHECK(flat_string->IsFlat() && !flat_string->IsConsString());
|
2012-12-11 10:22:15 +00:00
|
|
|
CHECK(cons_string->IsConsString());
|
|
|
|
// TODO(dcarney) Test stream reset as well.
|
|
|
|
int length = flat_string->length();
|
|
|
|
// Iterate start search in multiple places in the string.
|
|
|
|
int outer_iterations = length > 20 ? 20 : length;
|
|
|
|
for (int j = 0; j <= outer_iterations; j++) {
|
2012-12-11 10:42:10 +00:00
|
|
|
int offset = length * j / outer_iterations;
|
2012-12-11 10:22:15 +00:00
|
|
|
if (offset < 0) offset = 0;
|
|
|
|
// Want to test the offset == length case.
|
|
|
|
if (offset > length) offset = length;
|
2014-10-23 05:57:01 +00:00
|
|
|
StringCharacterStream flat_stream(flat_string, offset);
|
|
|
|
StringCharacterStream cons_stream(cons_string, offset);
|
2012-12-11 10:22:15 +00:00
|
|
|
for (int i = offset; i < length; i++) {
|
|
|
|
uint16_t c = flat_string->Get(i);
|
|
|
|
CHECK(flat_stream.HasMore());
|
|
|
|
CHECK(cons_stream.HasMore());
|
|
|
|
CHECK_EQ(c, flat_stream.GetNext());
|
|
|
|
CHECK_EQ(c, cons_stream.GetNext());
|
|
|
|
}
|
|
|
|
CHECK(!flat_stream.HasMore());
|
|
|
|
CHECK(!cons_stream.HasMore());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 15:39:01 +00:00
|
|
|
static inline void PrintStats(const ConsStringGenerationData& data) {
|
|
|
|
#ifdef DEBUG
|
2014-11-03 19:44:46 +00:00
|
|
|
printf("%s: [%u], %s: [%u], %s: [%u], %s: [%u], %s: [%u], %s: [%u]\n",
|
|
|
|
"leaves", data.stats_.leaves_, "empty", data.stats_.empty_leaves_,
|
|
|
|
"chars", data.stats_.chars_, "lefts", data.stats_.left_traversals_,
|
|
|
|
"rights", data.stats_.right_traversals_, "early_terminations",
|
|
|
|
data.early_terminations_);
|
2012-12-13 15:39:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename BuildString>
|
|
|
|
void TestStringCharacterStream(BuildString build, int test_cases) {
|
2015-09-29 09:08:10 +00:00
|
|
|
FLAG_gc_global = true;
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2012-12-11 10:22:15 +00:00
|
|
|
HandleScope outer_scope(isolate);
|
2014-02-03 07:29:23 +00:00
|
|
|
ConsStringGenerationData data(true);
|
2012-12-13 15:39:01 +00:00
|
|
|
for (int i = 0; i < test_cases; i++) {
|
2012-12-11 10:22:15 +00:00
|
|
|
printf("%d\n", i);
|
|
|
|
HandleScope inner_scope(isolate);
|
2014-03-19 11:31:43 +00:00
|
|
|
AlwaysAllocateScope always_allocate(isolate);
|
2012-12-13 15:39:01 +00:00
|
|
|
// Build flat version of cons string.
|
|
|
|
Handle<String> flat_string = build(i, &data);
|
|
|
|
ConsStringStats flat_string_stats;
|
|
|
|
AccumulateStats(flat_string, &flat_string_stats);
|
|
|
|
// Flatten string.
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, flat_string);
|
2012-12-13 15:39:01 +00:00
|
|
|
// Build unflattened version of cons string to test.
|
|
|
|
Handle<String> cons_string = build(i, &data);
|
|
|
|
ConsStringStats cons_string_stats;
|
|
|
|
AccumulateStats(cons_string, &cons_string_stats);
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_allocation;
|
2012-12-13 15:39:01 +00:00
|
|
|
PrintStats(data);
|
|
|
|
// Full verify of cons string.
|
|
|
|
cons_string_stats.VerifyEqual(flat_string_stats);
|
|
|
|
cons_string_stats.VerifyEqual(data.stats_);
|
|
|
|
VerifyConsString(cons_string, &data);
|
2018-11-27 00:48:42 +00:00
|
|
|
String flat_string_ptr = flat_string->IsConsString()
|
|
|
|
? ConsString::cast(*flat_string)->first()
|
|
|
|
: *flat_string;
|
2012-12-13 15:39:01 +00:00
|
|
|
VerifyCharacterStream(flat_string_ptr, *cons_string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const int kCharacterStreamNonRandomCases = 8;
|
|
|
|
|
2018-06-20 16:32:59 +00:00
|
|
|
static Handle<String> BuildEdgeCaseConsString(int test_case,
|
|
|
|
ConsStringGenerationData* data) {
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
2012-12-13 15:39:01 +00:00
|
|
|
data->Reset();
|
|
|
|
switch (test_case) {
|
|
|
|
case 0:
|
|
|
|
return ConstructBalanced(data, 71);
|
|
|
|
case 1:
|
|
|
|
return ConstructLeft(data, 71);
|
|
|
|
case 2:
|
|
|
|
return ConstructRight(data, 71);
|
|
|
|
case 3:
|
|
|
|
return ConstructLeft(data, 10);
|
|
|
|
case 4:
|
|
|
|
return ConstructRight(data, 10);
|
|
|
|
case 5:
|
|
|
|
// 2 element balanced tree.
|
|
|
|
data->stats_.chars_ += data->block(0)->length();
|
|
|
|
data->stats_.chars_ += data->block(1)->length();
|
|
|
|
data->stats_.leaves_ += 2;
|
2014-04-03 12:30:37 +00:00
|
|
|
return factory->NewConsString(data->block(0), data->block(1))
|
|
|
|
.ToHandleChecked();
|
2012-12-13 15:39:01 +00:00
|
|
|
case 6:
|
|
|
|
// Simple flattened tree.
|
|
|
|
data->stats_.chars_ += data->block(0)->length();
|
|
|
|
data->stats_.chars_ += data->block(1)->length();
|
|
|
|
data->stats_.leaves_ += 2;
|
|
|
|
data->stats_.empty_leaves_ += 1;
|
|
|
|
{
|
|
|
|
Handle<String> string =
|
2014-04-03 12:30:37 +00:00
|
|
|
factory->NewConsString(data->block(0), data->block(1))
|
|
|
|
.ToHandleChecked();
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, string);
|
2012-12-13 15:39:01 +00:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
case 7:
|
|
|
|
// Left node flattened.
|
|
|
|
data->stats_.chars_ += data->block(0)->length();
|
|
|
|
data->stats_.chars_ += data->block(1)->length();
|
|
|
|
data->stats_.chars_ += data->block(2)->length();
|
|
|
|
data->stats_.leaves_ += 3;
|
|
|
|
data->stats_.empty_leaves_ += 1;
|
|
|
|
data->stats_.left_traversals_ += 1;
|
|
|
|
{
|
|
|
|
Handle<String> left =
|
2014-04-03 12:30:37 +00:00
|
|
|
factory->NewConsString(data->block(0), data->block(1))
|
|
|
|
.ToHandleChecked();
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, left);
|
2014-04-03 12:30:37 +00:00
|
|
|
return factory->NewConsString(left, data->block(2)).ToHandleChecked();
|
2012-12-13 15:39:01 +00:00
|
|
|
}
|
|
|
|
case 8:
|
|
|
|
// Left node and right node flattened.
|
|
|
|
data->stats_.chars_ += data->block(0)->length();
|
|
|
|
data->stats_.chars_ += data->block(1)->length();
|
|
|
|
data->stats_.chars_ += data->block(2)->length();
|
|
|
|
data->stats_.chars_ += data->block(3)->length();
|
|
|
|
data->stats_.leaves_ += 4;
|
|
|
|
data->stats_.empty_leaves_ += 2;
|
|
|
|
data->stats_.left_traversals_ += 1;
|
|
|
|
data->stats_.right_traversals_ += 1;
|
|
|
|
{
|
|
|
|
Handle<String> left =
|
2014-04-03 12:30:37 +00:00
|
|
|
factory->NewConsString(data->block(0), data->block(1))
|
|
|
|
.ToHandleChecked();
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, left);
|
2012-12-13 15:39:01 +00:00
|
|
|
Handle<String> right =
|
2014-04-03 12:30:37 +00:00
|
|
|
factory->NewConsString(data->block(2), data->block(2))
|
|
|
|
.ToHandleChecked();
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, right);
|
2014-04-03 12:30:37 +00:00
|
|
|
return factory->NewConsString(left, right).ToHandleChecked();
|
2012-12-13 15:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(StringCharacterStreamEdgeCases) {
|
|
|
|
printf("TestStringCharacterStreamEdgeCases\n");
|
|
|
|
TestStringCharacterStream(
|
|
|
|
BuildEdgeCaseConsString, kCharacterStreamNonRandomCases);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const int kBalances = 3;
|
|
|
|
static const int kTreeLengths = 4;
|
|
|
|
static const int kEmptyLeaves = 4;
|
|
|
|
static const int kUniqueRandomParameters =
|
|
|
|
kBalances*kTreeLengths*kEmptyLeaves;
|
|
|
|
|
|
|
|
|
|
|
|
static void InitializeGenerationData(
|
|
|
|
int test_case, ConsStringGenerationData* data) {
|
|
|
|
// Clear the settings and reinit the rng.
|
|
|
|
data->Reset();
|
|
|
|
// Spin up the rng to a known location that is unique per test.
|
|
|
|
static const int kPerTestJump = 501;
|
|
|
|
for (int j = 0; j < test_case*kPerTestJump; j++) {
|
|
|
|
data->rng_.next();
|
|
|
|
}
|
|
|
|
// Choose balanced, left or right heavy trees.
|
|
|
|
switch (test_case % kBalances) {
|
|
|
|
case 0:
|
|
|
|
// Nothing to do. Already balanced.
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// Left balanced.
|
|
|
|
data->leftness_ = 0.90;
|
|
|
|
data->rightness_ = 0.15;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// Right balanced.
|
|
|
|
data->leftness_ = 0.15;
|
|
|
|
data->rightness_ = 0.90;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Must remove the influence of the above decision.
|
|
|
|
test_case /= kBalances;
|
|
|
|
// Choose tree length.
|
|
|
|
switch (test_case % kTreeLengths) {
|
|
|
|
case 0:
|
|
|
|
data->max_leaves_ = 16;
|
|
|
|
data->early_termination_threshold_ = 0.2;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
data->max_leaves_ = 50;
|
|
|
|
data->early_termination_threshold_ = 0.05;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
data->max_leaves_ = 500;
|
|
|
|
data->early_termination_threshold_ = 0.03;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
data->max_leaves_ = 5000;
|
|
|
|
data->early_termination_threshold_ = 0.001;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
2012-12-11 10:22:15 +00:00
|
|
|
}
|
2012-12-13 15:39:01 +00:00
|
|
|
// Must remove the influence of the above decision.
|
|
|
|
test_case /= kTreeLengths;
|
|
|
|
// Choose how much we allow empty nodes, including not at all.
|
|
|
|
data->empty_leaf_threshold_ =
|
|
|
|
0.03 * static_cast<double>(test_case % kEmptyLeaves);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<String> BuildRandomConsString(
|
|
|
|
int test_case, ConsStringGenerationData* data) {
|
|
|
|
InitializeGenerationData(test_case, data);
|
|
|
|
return ConstructRandomString(data, 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(StringCharacterStreamRandom) {
|
|
|
|
printf("StringCharacterStreamRandom\n");
|
|
|
|
TestStringCharacterStream(BuildRandomConsString, kUniqueRandomParameters*7);
|
2012-12-11 10:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
static const int kDeepOneByteDepth = 100000;
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
TEST(DeepOneByte) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
2018-06-20 16:32:59 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
char* foo = NewArray<char>(kDeepOneByteDepth);
|
|
|
|
for (int i = 0; i < kDeepOneByteDepth; i++) {
|
2008-08-22 13:33:59 +00:00
|
|
|
foo[i] = "foo "[i % 4];
|
|
|
|
}
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> string =
|
|
|
|
factory->NewStringFromOneByte(OneByteVector(foo, kDeepOneByteDepth))
|
|
|
|
.ToHandleChecked();
|
|
|
|
Handle<String> foo_string = factory->NewStringFromStaticChars("foo");
|
|
|
|
for (int i = 0; i < kDeepOneByteDepth; i += 10) {
|
2014-04-03 12:30:37 +00:00
|
|
|
string = factory->NewConsString(string, foo_string).ToHandleChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2014-04-03 12:30:37 +00:00
|
|
|
Handle<String> flat_string =
|
|
|
|
factory->NewConsString(string, foo_string).ToHandleChecked();
|
2018-06-20 16:32:59 +00:00
|
|
|
String::Flatten(isolate, flat_string);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 500; i++) {
|
2014-09-10 12:38:12 +00:00
|
|
|
TraverseFirst(flat_string, string, kDeepOneByteDepth);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2009-03-16 07:53:20 +00:00
|
|
|
DeleteArray<char>(foo);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2008-09-05 13:39:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(Utf8Conversion) {
|
|
|
|
// Smoke test for converting strings to utf-8.
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope handle_scope(CcTest::isolate());
|
2014-09-10 12:38:12 +00:00
|
|
|
// A simple one-byte string
|
|
|
|
const char* one_byte_string = "abcdef12345";
|
|
|
|
int len = v8::String::NewFromUtf8(CcTest::isolate(), one_byte_string,
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::NewStringType::kNormal,
|
|
|
|
StrLength(one_byte_string))
|
|
|
|
.ToLocalChecked()
|
2018-07-16 21:01:31 +00:00
|
|
|
->Utf8Length(CcTest::isolate());
|
2014-09-10 12:38:12 +00:00
|
|
|
CHECK_EQ(StrLength(one_byte_string), len);
|
|
|
|
// A mixed one-byte and two-byte string
|
2008-09-05 13:39:14 +00:00
|
|
|
// U+02E4 -> CB A4
|
|
|
|
// U+0064 -> 64
|
|
|
|
// U+12E4 -> E1 8B A4
|
|
|
|
// U+0030 -> 30
|
|
|
|
// U+3045 -> E3 81 85
|
|
|
|
const uint16_t mixed_string[] = {0x02E4, 0x0064, 0x12E4, 0x0030, 0x3045};
|
|
|
|
// The characters we expect to be output
|
2008-09-05 15:01:10 +00:00
|
|
|
const unsigned char as_utf8[11] = {0xCB, 0xA4, 0x64, 0xE1, 0x8B, 0xA4, 0x30,
|
2008-09-05 13:39:14 +00:00
|
|
|
0xE3, 0x81, 0x85, 0x00};
|
|
|
|
// The number of bytes expected to be written for each length
|
|
|
|
const int lengths[12] = {0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11};
|
2010-04-06 17:58:43 +00:00
|
|
|
const int char_lengths[12] = {0, 0, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5};
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<v8::String> mixed =
|
|
|
|
v8::String::NewFromTwoByte(CcTest::isolate(), mixed_string,
|
|
|
|
v8::NewStringType::kNormal, 5)
|
|
|
|
.ToLocalChecked();
|
2018-07-16 21:01:31 +00:00
|
|
|
CHECK_EQ(10, mixed->Utf8Length(CcTest::isolate()));
|
2008-09-05 13:39:14 +00:00
|
|
|
// Try encoding the string with all capacities
|
|
|
|
char buffer[11];
|
2008-09-06 13:42:50 +00:00
|
|
|
const char kNoChar = static_cast<char>(-1);
|
2008-09-05 13:39:14 +00:00
|
|
|
for (int i = 0; i <= 11; i++) {
|
|
|
|
// Clear the buffer before reusing it
|
|
|
|
for (int j = 0; j < 11; j++)
|
2008-09-06 13:42:50 +00:00
|
|
|
buffer[j] = kNoChar;
|
2010-04-06 17:58:43 +00:00
|
|
|
int chars_written;
|
2018-07-23 02:35:30 +00:00
|
|
|
int written =
|
|
|
|
mixed->WriteUtf8(CcTest::isolate(), buffer, i, &chars_written);
|
2008-09-05 13:39:14 +00:00
|
|
|
CHECK_EQ(lengths[i], written);
|
2010-04-06 17:58:43 +00:00
|
|
|
CHECK_EQ(char_lengths[i], chars_written);
|
2008-09-05 13:39:14 +00:00
|
|
|
// Check that the contents are correct
|
|
|
|
for (int j = 0; j < lengths[i]; j++)
|
2008-09-05 15:01:10 +00:00
|
|
|
CHECK_EQ(as_utf8[j], static_cast<unsigned char>(buffer[j]));
|
2008-09-05 13:39:14 +00:00
|
|
|
// Check that the rest of the buffer hasn't been touched
|
|
|
|
for (int j = lengths[i]; j < 11; j++)
|
2008-09-06 13:42:50 +00:00
|
|
|
CHECK_EQ(kNoChar, buffer[j]);
|
2008-09-05 13:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-09 23:04:00 +00:00
|
|
|
|
2018-12-18 10:21:08 +00:00
|
|
|
TEST(Utf8ConversionPerf) {
|
|
|
|
// Smoke test for converting strings to utf-8.
|
|
|
|
LocalContext context;
|
|
|
|
v8::HandleScope handle_scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::String> ascii_string =
|
|
|
|
CompileRun("'abc'.repeat(1E6)").As<v8::String>();
|
|
|
|
v8::Local<v8::String> one_byte_string =
|
|
|
|
CompileRun("'\\u0255\\u0254\\u0253'.repeat(1E6)").As<v8::String>();
|
|
|
|
v8::Local<v8::String> two_byte_string =
|
|
|
|
CompileRun("'\\u2255\\u2254\\u2253'.repeat(1E6)").As<v8::String>();
|
|
|
|
v8::Local<v8::String> surrogate_string =
|
|
|
|
CompileRun("'\\u{12345}\\u2244'.repeat(1E6)").As<v8::String>();
|
|
|
|
int size = 1E7;
|
|
|
|
char* buffer = new char[4 * size];
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
ascii_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
|
|
|
|
printf("ascii string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
ascii_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
|
|
|
|
printf("ascii string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
ascii_string->WriteUtf8(CcTest::isolate(), buffer, 4 * size, nullptr);
|
|
|
|
printf("ascii string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
one_byte_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
|
|
|
|
printf("one byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
one_byte_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
|
|
|
|
printf("one byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
one_byte_string->WriteUtf8(CcTest::isolate(), buffer, 4 * size, nullptr);
|
|
|
|
printf("one byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
two_byte_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
|
|
|
|
printf("two byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
two_byte_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
|
|
|
|
printf("two byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
two_byte_string->WriteUtf8(CcTest::isolate(), buffer, 4 * size, nullptr);
|
|
|
|
printf("two byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
surrogate_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
|
|
|
|
printf("surrogate string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
surrogate_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
|
|
|
|
printf("surrogate string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::base::ElapsedTimer timer;
|
|
|
|
timer.Start();
|
|
|
|
surrogate_string->WriteUtf8(CcTest::isolate(), buffer, 4 * size, nullptr);
|
|
|
|
printf("surrogate string %0.3f\n", timer.Elapsed().InMillisecondsF());
|
|
|
|
timer.Stop();
|
|
|
|
}
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
2009-04-09 23:04:00 +00:00
|
|
|
|
2009-12-02 12:58:10 +00:00
|
|
|
TEST(ExternalShortStringAdd) {
|
2013-09-19 13:30:47 +00:00
|
|
|
LocalContext context;
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope handle_scope(CcTest::isolate());
|
2009-04-09 23:04:00 +00:00
|
|
|
|
2009-12-02 12:58:10 +00:00
|
|
|
// Make sure we cover all always-flat lengths and at least one above.
|
|
|
|
static const int kMaxLength = 20;
|
2012-01-17 14:29:17 +00:00
|
|
|
CHECK_GT(kMaxLength, i::ConsString::kMinLength);
|
2009-12-02 12:58:10 +00:00
|
|
|
|
|
|
|
// Allocate two JavaScript arrays for holding short strings.
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<v8::Array> one_byte_external_strings =
|
2013-11-28 08:21:26 +00:00
|
|
|
v8::Array::New(CcTest::isolate(), kMaxLength + 1);
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<v8::Array> non_one_byte_external_strings =
|
2013-11-28 08:21:26 +00:00
|
|
|
v8::Array::New(CcTest::isolate(), kMaxLength + 1);
|
2009-12-02 12:58:10 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
// Generate short one-byte and two-byte external strings.
|
2009-12-02 12:58:10 +00:00
|
|
|
for (int i = 0; i <= kMaxLength; i++) {
|
2014-09-10 12:38:12 +00:00
|
|
|
char* one_byte = NewArray<char>(i + 1);
|
2009-12-02 12:58:10 +00:00
|
|
|
for (int j = 0; j < i; j++) {
|
2014-09-10 12:38:12 +00:00
|
|
|
one_byte[j] = 'a';
|
2009-12-02 12:58:10 +00:00
|
|
|
}
|
|
|
|
// Terminating '\0' is left out on purpose. It is not required for external
|
|
|
|
// string data.
|
2014-09-10 12:38:12 +00:00
|
|
|
OneByteResource* one_byte_resource = new OneByteResource(one_byte, i);
|
|
|
|
v8::Local<v8::String> one_byte_external_string =
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::String::NewExternalOneByte(CcTest::isolate(), one_byte_resource)
|
|
|
|
.ToLocalChecked();
|
2009-12-02 12:58:10 +00:00
|
|
|
|
2015-12-07 15:27:40 +00:00
|
|
|
one_byte_external_strings->Set(context.local(),
|
|
|
|
v8::Integer::New(CcTest::isolate(), i),
|
|
|
|
one_byte_external_string)
|
|
|
|
.FromJust();
|
2014-09-10 12:38:12 +00:00
|
|
|
uc16* non_one_byte = NewArray<uc16>(i + 1);
|
2009-12-02 12:58:10 +00:00
|
|
|
for (int j = 0; j < i; j++) {
|
2014-09-10 12:38:12 +00:00
|
|
|
non_one_byte[j] = 0x1234;
|
2009-12-02 12:58:10 +00:00
|
|
|
}
|
|
|
|
// Terminating '\0' is left out on purpose. It is not required for external
|
|
|
|
// string data.
|
2014-09-10 12:38:12 +00:00
|
|
|
Resource* resource = new Resource(non_one_byte, i);
|
|
|
|
v8::Local<v8::String> non_one_byte_external_string =
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::String::NewExternalTwoByte(CcTest::isolate(), resource)
|
|
|
|
.ToLocalChecked();
|
|
|
|
non_one_byte_external_strings->Set(context.local(),
|
|
|
|
v8::Integer::New(CcTest::isolate(), i),
|
|
|
|
non_one_byte_external_string)
|
|
|
|
.FromJust();
|
2009-12-02 12:58:10 +00:00
|
|
|
}
|
2009-04-09 23:04:00 +00:00
|
|
|
|
2009-12-02 12:58:10 +00:00
|
|
|
// Add the arrays with the short external strings in the global object.
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<v8::Object> global = context->Global();
|
|
|
|
global->Set(context.local(), v8_str("external_one_byte"),
|
|
|
|
one_byte_external_strings)
|
|
|
|
.FromJust();
|
|
|
|
global->Set(context.local(), v8_str("external_non_one_byte"),
|
|
|
|
non_one_byte_external_strings)
|
|
|
|
.FromJust();
|
|
|
|
global->Set(context.local(), v8_str("max_length"),
|
|
|
|
v8::Integer::New(CcTest::isolate(), kMaxLength))
|
|
|
|
.FromJust();
|
2009-12-02 12:58:10 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
// Add short external one-byte and two-byte strings checking the result.
|
2009-12-02 12:58:10 +00:00
|
|
|
static const char* source =
|
2014-09-10 12:38:12 +00:00
|
|
|
"function test() {"
|
|
|
|
" var one_byte_chars = 'aaaaaaaaaaaaaaaaaaaa';"
|
|
|
|
" var non_one_byte_chars = "
|
|
|
|
"'\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1"
|
|
|
|
"234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\"
|
|
|
|
"u1234';" // NOLINT
|
|
|
|
" if (one_byte_chars.length != max_length) return 1;"
|
|
|
|
" if (non_one_byte_chars.length != max_length) return 2;"
|
|
|
|
" var one_byte = Array(max_length + 1);"
|
|
|
|
" var non_one_byte = Array(max_length + 1);"
|
|
|
|
" for (var i = 0; i <= max_length; i++) {"
|
|
|
|
" one_byte[i] = one_byte_chars.substring(0, i);"
|
|
|
|
" non_one_byte[i] = non_one_byte_chars.substring(0, i);"
|
|
|
|
" };"
|
|
|
|
" for (var i = 0; i <= max_length; i++) {"
|
|
|
|
" if (one_byte[i] != external_one_byte[i]) return 3;"
|
|
|
|
" if (non_one_byte[i] != external_non_one_byte[i]) return 4;"
|
|
|
|
" for (var j = 0; j < i; j++) {"
|
|
|
|
" if (external_one_byte[i] !="
|
|
|
|
" (external_one_byte[j] + external_one_byte[i - j])) return "
|
|
|
|
"5;"
|
|
|
|
" if (external_non_one_byte[i] !="
|
|
|
|
" (external_non_one_byte[j] + external_non_one_byte[i - "
|
|
|
|
"j])) return 6;"
|
|
|
|
" if (non_one_byte[i] != (non_one_byte[j] + non_one_byte[i - "
|
|
|
|
"j])) return 7;"
|
|
|
|
" if (one_byte[i] != (one_byte[j] + one_byte[i - j])) return 8;"
|
|
|
|
" if (one_byte[i] != (external_one_byte[j] + one_byte[i - j])) "
|
|
|
|
"return 9;"
|
|
|
|
" if (one_byte[i] != (one_byte[j] + external_one_byte[i - j])) "
|
|
|
|
"return 10;"
|
|
|
|
" if (non_one_byte[i] !="
|
|
|
|
" (external_non_one_byte[j] + non_one_byte[i - j])) return "
|
|
|
|
"11;"
|
|
|
|
" if (non_one_byte[i] !="
|
|
|
|
" (non_one_byte[j] + external_non_one_byte[i - j])) return "
|
|
|
|
"12;"
|
|
|
|
" }"
|
|
|
|
" }"
|
|
|
|
" return 0;"
|
|
|
|
"};"
|
|
|
|
"test()";
|
2015-12-07 15:27:40 +00:00
|
|
|
CHECK_EQ(0, CompileRun(source)->Int32Value(context.local()).FromJust());
|
2009-12-02 12:58:10 +00:00
|
|
|
}
|
2010-06-02 09:31:01 +00:00
|
|
|
|
2018-12-18 10:21:08 +00:00
|
|
|
TEST(ReplaceInvalidUtf8) {
|
|
|
|
LocalContext context;
|
|
|
|
v8::HandleScope handle_scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::String> string = CompileRun("'ab\\ud800cd'").As<v8::String>();
|
|
|
|
char buffer[7];
|
|
|
|
memset(buffer, 0, 7);
|
|
|
|
int chars_written = 0;
|
|
|
|
int size = string->WriteUtf8(CcTest::isolate(), buffer, 7, &chars_written,
|
|
|
|
v8::String::REPLACE_INVALID_UTF8);
|
|
|
|
CHECK_EQ(7, size);
|
|
|
|
CHECK_EQ(5, chars_written);
|
|
|
|
CHECK_EQ(0, memcmp("\x61\x62\xef\xbf\xbd\x63\x64", buffer, 7));
|
|
|
|
|
|
|
|
memset(buffer, 0, 7);
|
|
|
|
chars_written = 0;
|
|
|
|
size = string->WriteUtf8(CcTest::isolate(), buffer, 6, &chars_written,
|
|
|
|
v8::String::REPLACE_INVALID_UTF8);
|
|
|
|
CHECK_EQ(6, size);
|
|
|
|
CHECK_EQ(4, chars_written);
|
|
|
|
CHECK_EQ(0, memcmp("\x61\x62\xef\xbf\xbd\x63", buffer, 6));
|
|
|
|
}
|
2010-06-02 09:31:01 +00:00
|
|
|
|
2013-09-09 16:15:40 +00:00
|
|
|
TEST(JSONStringifySliceMadeExternal) {
|
2017-11-02 06:22:13 +00:00
|
|
|
if (!FLAG_string_slices) return;
|
2013-09-19 13:30:47 +00:00
|
|
|
CcTest::InitializeVM();
|
2013-09-09 16:15:40 +00:00
|
|
|
// Create a sliced string from a one-byte string. The latter is turned
|
|
|
|
// into a two-byte external string. Check that JSON.stringify works.
|
|
|
|
v8::HandleScope handle_scope(CcTest::isolate());
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<v8::String> underlying =
|
2014-11-24 10:30:39 +00:00
|
|
|
CompileRun(
|
2018-05-02 15:30:06 +00:00
|
|
|
"var underlying = 'abcdefghijklmnopqrstuvwxyz';"
|
2015-12-07 15:27:40 +00:00
|
|
|
"underlying")
|
|
|
|
->ToString(CcTest::isolate()->GetCurrentContext())
|
|
|
|
.ToLocalChecked();
|
|
|
|
v8::Local<v8::String> slice =
|
|
|
|
CompileRun(
|
|
|
|
"var slice = '';"
|
|
|
|
"slice = underlying.slice(1);"
|
|
|
|
"slice")
|
|
|
|
->ToString(CcTest::isolate()->GetCurrentContext())
|
|
|
|
.ToLocalChecked();
|
2013-09-09 16:15:40 +00:00
|
|
|
CHECK(v8::Utils::OpenHandle(*slice)->IsSlicedString());
|
|
|
|
CHECK(v8::Utils::OpenHandle(*underlying)->IsSeqOneByteString());
|
|
|
|
|
|
|
|
int length = underlying->Length();
|
2014-02-03 07:29:23 +00:00
|
|
|
uc16* two_byte = NewArray<uc16>(length + 1);
|
2018-07-23 02:35:30 +00:00
|
|
|
underlying->Write(CcTest::isolate(), two_byte);
|
2014-02-03 07:29:23 +00:00
|
|
|
Resource* resource = new Resource(two_byte, length);
|
2013-09-09 16:15:40 +00:00
|
|
|
CHECK(underlying->MakeExternal(resource));
|
|
|
|
CHECK(v8::Utils::OpenHandle(*slice)->IsSlicedString());
|
|
|
|
CHECK(v8::Utils::OpenHandle(*underlying)->IsExternalTwoByteString());
|
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK_EQ(0,
|
|
|
|
strcmp("\"bcdefghijklmnopqrstuvwxyz\"",
|
2017-08-24 21:49:48 +00:00
|
|
|
*v8::String::Utf8Value(CcTest::isolate(),
|
|
|
|
CompileRun("JSON.stringify(slice)"))));
|
2013-09-09 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 12:36:17 +00:00
|
|
|
TEST(JSONStringifyWellFormed) {
|
|
|
|
FLAG_harmony_json_stringify = true;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope handle_scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
|
|
|
|
|
|
|
|
// Test some leading surrogates (U+D800 to U+DBFF).
|
|
|
|
{ // U+D800
|
|
|
|
CHECK_EQ(
|
|
|
|
0, strcmp("\"\\ud800\"", *v8::String::Utf8Value(
|
|
|
|
CcTest::isolate(),
|
|
|
|
CompileRun("JSON.stringify('\\uD800')"))));
|
|
|
|
v8::Local<v8::String> json = v8_str("\"\\ud800\"");
|
|
|
|
v8::Local<v8::Value> parsed =
|
|
|
|
v8::JSON::Parse(context, json).ToLocalChecked();
|
|
|
|
CHECK(v8::JSON::Stringify(context, parsed)
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Equals(context, json)
|
|
|
|
.FromJust());
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // U+DAAA
|
|
|
|
CHECK_EQ(
|
|
|
|
0, strcmp("\"\\udaaa\"", *v8::String::Utf8Value(
|
|
|
|
CcTest::isolate(),
|
|
|
|
CompileRun("JSON.stringify('\\uDAAA')"))));
|
|
|
|
v8::Local<v8::String> json = v8_str("\"\\udaaa\"");
|
|
|
|
v8::Local<v8::Value> parsed =
|
|
|
|
v8::JSON::Parse(context, json).ToLocalChecked();
|
|
|
|
CHECK(v8::JSON::Stringify(context, parsed)
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Equals(context, json)
|
|
|
|
.FromJust());
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // U+DBFF
|
|
|
|
CHECK_EQ(
|
|
|
|
0, strcmp("\"\\udbff\"", *v8::String::Utf8Value(
|
|
|
|
CcTest::isolate(),
|
|
|
|
CompileRun("JSON.stringify('\\uDBFF')"))));
|
|
|
|
v8::Local<v8::String> json = v8_str("\"\\udbff\"");
|
|
|
|
v8::Local<v8::Value> parsed =
|
|
|
|
v8::JSON::Parse(context, json).ToLocalChecked();
|
|
|
|
CHECK(v8::JSON::Stringify(context, parsed)
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Equals(context, json)
|
|
|
|
.FromJust());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test some trailing surrogates (U+DC00 to U+DFFF).
|
|
|
|
{ // U+DC00
|
|
|
|
CHECK_EQ(
|
|
|
|
0, strcmp("\"\\udc00\"", *v8::String::Utf8Value(
|
|
|
|
CcTest::isolate(),
|
|
|
|
CompileRun("JSON.stringify('\\uDC00')"))));
|
|
|
|
v8::Local<v8::String> json = v8_str("\"\\udc00\"");
|
|
|
|
v8::Local<v8::Value> parsed =
|
|
|
|
v8::JSON::Parse(context, json).ToLocalChecked();
|
|
|
|
CHECK(v8::JSON::Stringify(context, parsed)
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Equals(context, json)
|
|
|
|
.FromJust());
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // U+DDDD
|
|
|
|
CHECK_EQ(
|
|
|
|
0, strcmp("\"\\udddd\"", *v8::String::Utf8Value(
|
|
|
|
CcTest::isolate(),
|
|
|
|
CompileRun("JSON.stringify('\\uDDDD')"))));
|
|
|
|
v8::Local<v8::String> json = v8_str("\"\\udddd\"");
|
|
|
|
v8::Local<v8::Value> parsed =
|
|
|
|
v8::JSON::Parse(context, json).ToLocalChecked();
|
|
|
|
CHECK(v8::JSON::Stringify(context, parsed)
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Equals(context, json)
|
|
|
|
.FromJust());
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // U+DFFF
|
|
|
|
CHECK_EQ(
|
|
|
|
0, strcmp("\"\\udfff\"", *v8::String::Utf8Value(
|
|
|
|
CcTest::isolate(),
|
|
|
|
CompileRun("JSON.stringify('\\uDFFF')"))));
|
|
|
|
v8::Local<v8::String> json = v8_str("\"\\udfff\"");
|
|
|
|
v8::Local<v8::Value> parsed =
|
|
|
|
v8::JSON::Parse(context, json).ToLocalChecked();
|
|
|
|
CHECK(v8::JSON::Stringify(context, parsed)
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Equals(context, json)
|
|
|
|
.FromJust());
|
|
|
|
}
|
|
|
|
}
|
2013-09-09 16:15:40 +00:00
|
|
|
|
2010-06-02 09:31:01 +00:00
|
|
|
TEST(CachedHashOverflow) {
|
2013-09-19 13:30:47 +00:00
|
|
|
CcTest::InitializeVM();
|
2010-06-02 09:31:01 +00:00
|
|
|
// We incorrectly allowed strings to be tagged as array indices even if their
|
|
|
|
// values didn't fit in the hash field.
|
|
|
|
// See http://code.google.com/p/v8/issues/detail?id=728
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2010-06-02 09:31:01 +00:00
|
|
|
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope handle_scope(CcTest::isolate());
|
2010-06-02 09:31:01 +00:00
|
|
|
// Lines must be executed sequentially. Combining them into one script
|
|
|
|
// makes the bug go away.
|
2017-03-17 15:18:18 +00:00
|
|
|
const char* lines[] = {"var x = [];", "x[4] = 42;", "var s = \"1073741828\";",
|
|
|
|
"x[s];", "x[s] = 37;", "x[4];",
|
|
|
|
"x[s];"};
|
2010-06-02 09:31:01 +00:00
|
|
|
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<Smi> fortytwo(Smi::FromInt(42), isolate);
|
|
|
|
Handle<Smi> thirtyseven(Smi::FromInt(37), isolate);
|
|
|
|
Handle<Object> results[] = { isolate->factory()->undefined_value(),
|
|
|
|
fortytwo,
|
|
|
|
isolate->factory()->undefined_value(),
|
|
|
|
isolate->factory()->undefined_value(),
|
|
|
|
thirtyseven,
|
|
|
|
fortytwo,
|
|
|
|
thirtyseven // Bug yielded 42 here.
|
2010-06-02 09:31:01 +00:00
|
|
|
};
|
|
|
|
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
|
2017-03-17 15:18:18 +00:00
|
|
|
for (size_t i = 0; i < arraysize(lines); i++) {
|
|
|
|
const char* line = lines[i];
|
2010-06-02 09:31:01 +00:00
|
|
|
printf("%s\n", line);
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
v8::Script::Compile(context,
|
|
|
|
v8::String::NewFromUtf8(CcTest::isolate(), line,
|
|
|
|
v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Run(context)
|
|
|
|
.ToLocalChecked();
|
2016-06-14 10:08:44 +00:00
|
|
|
CHECK_EQ(results[i]->IsUndefined(CcTest::i_isolate()),
|
|
|
|
result->IsUndefined());
|
2010-06-02 11:05:06 +00:00
|
|
|
CHECK_EQ(results[i]->IsNumber(), result->IsNumber());
|
2010-06-02 09:31:01 +00:00
|
|
|
if (result->IsNumber()) {
|
2015-07-13 14:41:38 +00:00
|
|
|
int32_t value = 0;
|
|
|
|
CHECK(results[i]->ToInt32(&value));
|
2015-12-07 15:27:40 +00:00
|
|
|
CHECK_EQ(value, result->ToInt32(context).ToLocalChecked()->Value());
|
2010-06-02 09:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 13:03:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(SliceFromCons) {
|
2017-11-02 06:22:13 +00:00
|
|
|
if (!FLAG_string_slices) return;
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
2013-09-19 09:17:13 +00:00
|
|
|
Factory* factory = CcTest::i_isolate()->factory();
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-08-26 13:03:30 +00:00
|
|
|
Handle<String> string =
|
2014-09-10 12:38:12 +00:00
|
|
|
factory->NewStringFromStaticChars("parentparentparent");
|
2014-04-03 12:30:37 +00:00
|
|
|
Handle<String> parent =
|
|
|
|
factory->NewConsString(string, string).ToHandleChecked();
|
2011-08-26 13:03:30 +00:00
|
|
|
CHECK(parent->IsConsString());
|
|
|
|
CHECK(!parent->IsFlat());
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<String> slice = factory->NewSubString(parent, 1, 25);
|
2011-08-26 13:03:30 +00:00
|
|
|
// After slicing, the original string becomes a flat cons.
|
|
|
|
CHECK(parent->IsFlat());
|
|
|
|
CHECK(slice->IsSlicedString());
|
|
|
|
CHECK_EQ(SlicedString::cast(*slice)->parent(),
|
2013-03-20 10:33:26 +00:00
|
|
|
// Parent could have been short-circuited.
|
|
|
|
parent->IsConsString() ? ConsString::cast(*parent)->first()
|
|
|
|
: *parent);
|
2011-08-26 13:03:30 +00:00
|
|
|
CHECK(SlicedString::cast(*slice)->parent()->IsSeqString());
|
|
|
|
CHECK(slice->IsFlat());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
class OneByteVectorResource : public v8::String::ExternalOneByteStringResource {
|
2011-09-15 11:10:01 +00:00
|
|
|
public:
|
2014-09-10 12:38:12 +00:00
|
|
|
explicit OneByteVectorResource(i::Vector<const char> vector)
|
2011-09-15 11:10:01 +00:00
|
|
|
: data_(vector) {}
|
2018-09-14 15:34:02 +00:00
|
|
|
~OneByteVectorResource() override = default;
|
|
|
|
size_t length() const override { return data_.length(); }
|
|
|
|
const char* data() const override { return data_.start(); }
|
2011-09-15 11:10:01 +00:00
|
|
|
private:
|
|
|
|
i::Vector<const char> data_;
|
|
|
|
};
|
|
|
|
|
2017-01-19 13:27:59 +00:00
|
|
|
TEST(InternalizeExternal) {
|
2018-04-09 15:40:52 +00:00
|
|
|
#ifdef ENABLE_MINOR_MC
|
2017-05-03 21:31:06 +00:00
|
|
|
// TODO(mlippautz): Remove once we add support for forwarding ThinStrings in
|
2018-04-09 15:40:52 +00:00
|
|
|
// minor MC
|
2017-05-03 21:31:06 +00:00
|
|
|
if (FLAG_minor_mc) return;
|
2018-04-09 15:40:52 +00:00
|
|
|
#endif // ENABLE_MINOR_MC
|
2017-05-29 11:06:13 +00:00
|
|
|
FLAG_stress_incremental_marking = false;
|
2017-01-31 06:41:14 +00:00
|
|
|
FLAG_thin_strings = true;
|
|
|
|
CcTest::InitializeVM();
|
2017-01-19 13:27:59 +00:00
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
// This won't leak; the external string mechanism will call Dispose() on it.
|
|
|
|
OneByteVectorResource* resource =
|
2018-04-17 16:48:51 +00:00
|
|
|
new OneByteVectorResource(i::Vector<const char>("prop-1234", 9));
|
2017-01-19 13:27:59 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::String> ext_string =
|
|
|
|
v8::String::NewExternalOneByte(CcTest::isolate(), resource)
|
|
|
|
.ToLocalChecked();
|
|
|
|
Handle<String> string = v8::Utils::OpenHandle(*ext_string);
|
|
|
|
CHECK(string->IsExternalString());
|
|
|
|
CHECK(!string->IsInternalizedString());
|
2018-07-10 08:07:58 +00:00
|
|
|
CHECK(!i::Heap::InNewSpace(*string));
|
2018-04-17 16:48:51 +00:00
|
|
|
CHECK_EQ(
|
|
|
|
isolate->factory()->string_table()->LookupStringIfExists_NoAllocate(
|
2018-11-27 00:48:42 +00:00
|
|
|
isolate, string->ptr()),
|
|
|
|
Smi::FromInt(ResultSentinel::kNotFound).ptr());
|
2017-01-19 13:27:59 +00:00
|
|
|
factory->InternalizeName(string);
|
2018-04-17 16:48:51 +00:00
|
|
|
CHECK(string->IsExternalString());
|
2017-01-19 13:27:59 +00:00
|
|
|
CHECK(string->IsInternalizedString());
|
2018-07-10 08:07:58 +00:00
|
|
|
CHECK(!i::Heap::InNewSpace(*string));
|
2017-01-19 13:27:59 +00:00
|
|
|
}
|
|
|
|
CcTest::CollectGarbage(i::OLD_SPACE);
|
|
|
|
CcTest::CollectGarbage(i::OLD_SPACE);
|
|
|
|
}
|
2011-09-15 11:10:01 +00:00
|
|
|
|
|
|
|
TEST(SliceFromExternal) {
|
2017-11-02 06:22:13 +00:00
|
|
|
if (!FLAG_string_slices) return;
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
2013-09-19 09:17:13 +00:00
|
|
|
Factory* factory = CcTest::i_isolate()->factory();
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2014-09-10 12:38:12 +00:00
|
|
|
OneByteVectorResource resource(
|
2011-09-15 11:10:01 +00:00
|
|
|
i::Vector<const char>("abcdefghijklmnopqrstuvwxyz", 26));
|
2014-04-03 12:41:37 +00:00
|
|
|
Handle<String> string =
|
2014-09-10 12:38:12 +00:00
|
|
|
factory->NewExternalStringFromOneByte(&resource).ToHandleChecked();
|
2011-09-15 11:10:01 +00:00
|
|
|
CHECK(string->IsExternalString());
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<String> slice = factory->NewSubString(string, 1, 25);
|
2011-09-15 11:10:01 +00:00
|
|
|
CHECK(slice->IsSlicedString());
|
|
|
|
CHECK(string->IsExternalString());
|
|
|
|
CHECK_EQ(SlicedString::cast(*slice)->parent(), *string);
|
|
|
|
CHECK(SlicedString::cast(*slice)->parent()->IsExternalString());
|
|
|
|
CHECK(slice->IsFlat());
|
2018-07-04 09:31:54 +00:00
|
|
|
// This avoids the GC from trying to free stack allocated resources.
|
2018-07-26 06:42:03 +00:00
|
|
|
i::Handle<i::ExternalOneByteString>::cast(string)->SetResource(
|
|
|
|
CcTest::i_isolate(), nullptr);
|
2011-09-15 11:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-26 13:03:30 +00:00
|
|
|
TEST(TrivialSlice) {
|
|
|
|
// This tests whether a slice that contains the entire parent string
|
|
|
|
// actually creates a new string (it should not).
|
2017-11-02 06:22:13 +00:00
|
|
|
if (!FLAG_string_slices) return;
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
2013-09-19 09:17:13 +00:00
|
|
|
Factory* factory = CcTest::i_isolate()->factory();
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-08-26 13:03:30 +00:00
|
|
|
v8::Local<v8::Value> result;
|
|
|
|
Handle<String> string;
|
2018-05-02 15:30:06 +00:00
|
|
|
const char* init = "var str = 'abcdefghijklmnopqrstuvwxyz';";
|
2011-08-26 13:03:30 +00:00
|
|
|
const char* check = "str.slice(0,26)";
|
|
|
|
const char* crosscheck = "str.slice(1,25)";
|
|
|
|
|
|
|
|
CompileRun(init);
|
|
|
|
|
|
|
|
result = CompileRun(check);
|
|
|
|
CHECK(result->IsString());
|
|
|
|
string = v8::Utils::OpenHandle(v8::String::Cast(*result));
|
|
|
|
CHECK(!string->IsSlicedString());
|
|
|
|
|
2013-06-04 10:30:05 +00:00
|
|
|
string = factory->NewSubString(string, 0, 26);
|
2011-08-26 13:03:30 +00:00
|
|
|
CHECK(!string->IsSlicedString());
|
|
|
|
result = CompileRun(crosscheck);
|
|
|
|
CHECK(result->IsString());
|
|
|
|
string = v8::Utils::OpenHandle(v8::String::Cast(*result));
|
|
|
|
CHECK(string->IsSlicedString());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK_EQ(0, strcmp("bcdefghijklmnopqrstuvwxy", string->ToCString().get()));
|
2011-08-26 13:03:30 +00:00
|
|
|
}
|
2011-09-01 15:24:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(SliceFromSlice) {
|
|
|
|
// This tests whether a slice that contains the entire parent string
|
|
|
|
// actually creates a new string (it should not).
|
2017-11-02 06:22:13 +00:00
|
|
|
if (!FLAG_string_slices) return;
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-09-01 15:24:26 +00:00
|
|
|
v8::Local<v8::Value> result;
|
|
|
|
Handle<String> string;
|
|
|
|
const char* init = "var str = 'abcdefghijklmnopqrstuvwxyz';";
|
2014-12-15 15:46:01 +00:00
|
|
|
const char* slice = "var slice = ''; slice = str.slice(1,-1); slice";
|
2011-09-01 15:24:26 +00:00
|
|
|
const char* slice_from_slice = "slice.slice(1,-1);";
|
|
|
|
|
|
|
|
CompileRun(init);
|
|
|
|
result = CompileRun(slice);
|
|
|
|
CHECK(result->IsString());
|
|
|
|
string = v8::Utils::OpenHandle(v8::String::Cast(*result));
|
|
|
|
CHECK(string->IsSlicedString());
|
|
|
|
CHECK(SlicedString::cast(*string)->parent()->IsSeqString());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK_EQ(0, strcmp("bcdefghijklmnopqrstuvwxy", string->ToCString().get()));
|
2011-09-01 15:24:26 +00:00
|
|
|
|
|
|
|
result = CompileRun(slice_from_slice);
|
|
|
|
CHECK(result->IsString());
|
|
|
|
string = v8::Utils::OpenHandle(v8::String::Cast(*result));
|
|
|
|
CHECK(string->IsSlicedString());
|
|
|
|
CHECK(SlicedString::cast(*string)->parent()->IsSeqString());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK_EQ(0, strcmp("cdefghijklmnopqrstuvwx", string->ToCString().get()));
|
2011-09-01 15:24:26 +00:00
|
|
|
}
|
2012-04-04 14:37:07 +00:00
|
|
|
|
|
|
|
|
2014-09-16 09:15:02 +00:00
|
|
|
UNINITIALIZED_TEST(OneByteArrayJoin) {
|
|
|
|
v8::Isolate::CreateParams create_params;
|
2012-04-04 14:37:07 +00:00
|
|
|
// Set heap limits.
|
2017-07-19 09:53:06 +00:00
|
|
|
create_params.constraints.set_max_semi_space_size_in_kb(1024);
|
2017-10-20 15:18:53 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
create_params.constraints.set_max_old_space_size(20);
|
|
|
|
#else
|
2017-05-29 08:30:50 +00:00
|
|
|
create_params.constraints.set_max_old_space_size(7);
|
2017-10-20 15:18:53 +00:00
|
|
|
#endif
|
2015-04-29 09:54:34 +00:00
|
|
|
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
2014-09-16 09:15:02 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::New(create_params);
|
|
|
|
isolate->Enter();
|
|
|
|
|
|
|
|
{
|
|
|
|
// String s is made of 2^17 = 131072 'c' characters and a is an array
|
|
|
|
// starting with 'bad', followed by 2^14 times the string s. That means the
|
|
|
|
// total length of the concatenated strings is 2^31 + 3. So on 32bit systems
|
|
|
|
// summing the lengths of the strings (as Smis) overflows and wraps.
|
|
|
|
LocalContext context(isolate);
|
|
|
|
v8::HandleScope scope(isolate);
|
2015-05-28 12:49:31 +00:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2014-09-16 09:15:02 +00:00
|
|
|
CHECK(CompileRun(
|
|
|
|
"var two_14 = Math.pow(2, 14);"
|
|
|
|
"var two_17 = Math.pow(2, 17);"
|
|
|
|
"var s = Array(two_17 + 1).join('c');"
|
|
|
|
"var a = ['bad'];"
|
|
|
|
"for (var i = 1; i <= two_14; i++) a.push(s);"
|
|
|
|
"a.join("
|
|
|
|
");").IsEmpty());
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
}
|
|
|
|
isolate->Exit();
|
|
|
|
isolate->Dispose();
|
2012-04-04 14:37:07 +00:00
|
|
|
}
|
2012-04-17 10:49:15 +00:00
|
|
|
|
2014-10-06 15:50:40 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-10-13 16:33:03 +00:00
|
|
|
int* global_use_counts = nullptr;
|
2014-10-06 15:50:40 +00:00
|
|
|
|
|
|
|
void MockUseCounterCallback(v8::Isolate* isolate,
|
|
|
|
v8::Isolate::UseCounterFeature feature) {
|
|
|
|
++global_use_counts[feature];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CountBreakIterator) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
LocalContext context;
|
|
|
|
int use_counts[v8::Isolate::kUseCounterFeatureCount] = {};
|
|
|
|
global_use_counts = use_counts;
|
|
|
|
CcTest::isolate()->SetUseCounterCallback(MockUseCounterCallback);
|
|
|
|
CHECK_EQ(0, use_counts[v8::Isolate::kBreakIterator]);
|
|
|
|
v8::Local<v8::Value> result = CompileRun(
|
|
|
|
"(function() {"
|
|
|
|
" if (!this.Intl) return 0;"
|
|
|
|
" var iterator = Intl.v8BreakIterator(['en']);"
|
|
|
|
" iterator.adoptText('Now is the time');"
|
|
|
|
" iterator.next();"
|
|
|
|
" return iterator.next();"
|
|
|
|
"})();");
|
|
|
|
CHECK(result->IsNumber());
|
2015-12-07 15:27:40 +00:00
|
|
|
int uses =
|
|
|
|
result->ToInt32(context.local()).ToLocalChecked()->Value() == 0 ? 0 : 1;
|
2014-10-06 15:50:40 +00:00
|
|
|
CHECK_EQ(uses, use_counts[v8::Isolate::kBreakIterator]);
|
|
|
|
// Make sure GC cleans up the break iterator, so we don't get a memory leak
|
|
|
|
// reported by ASAN.
|
|
|
|
CcTest::isolate()->LowMemoryNotification();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-28 09:37:41 +00:00
|
|
|
TEST(StringReplaceAtomTwoByteResult) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2012-08-28 09:37:41 +00:00
|
|
|
LocalContext context;
|
|
|
|
v8::Local<v8::Value> result = CompileRun(
|
2014-09-10 12:38:12 +00:00
|
|
|
"var subject = 'one_byte~only~string~'; "
|
2012-08-28 09:37:41 +00:00
|
|
|
"var replace = '\x80'; "
|
|
|
|
"subject.replace(/~/g, replace); ");
|
|
|
|
CHECK(result->IsString());
|
|
|
|
Handle<String> string = v8::Utils::OpenHandle(v8::String::Cast(*result));
|
2017-11-17 14:53:17 +00:00
|
|
|
CHECK(string->IsTwoByteRepresentation());
|
2012-08-28 09:37:41 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
v8::Local<v8::String> expected = v8_str("one_byte\x80only\x80string\x80");
|
2015-12-07 15:27:40 +00:00
|
|
|
CHECK(expected->Equals(context.local(), result).FromJust());
|
2012-08-28 09:37:41 +00:00
|
|
|
}
|
2012-09-11 14:16:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(IsAscii) {
|
2017-10-13 16:33:03 +00:00
|
|
|
CHECK(String::IsAscii(static_cast<char*>(nullptr), 0));
|
|
|
|
CHECK(String::IsOneByte(static_cast<uc16*>(nullptr), 0));
|
2012-09-11 14:16:56 +00:00
|
|
|
}
|
2013-01-16 13:04:07 +00:00
|
|
|
|
|
|
|
|
2013-01-21 16:11:31 +00:00
|
|
|
|
|
|
|
template<typename Op, bool return_first>
|
|
|
|
static uint16_t ConvertLatin1(uint16_t c) {
|
2016-02-02 11:44:01 +00:00
|
|
|
uint32_t result[Op::kMaxWidth];
|
2013-01-16 13:04:07 +00:00
|
|
|
int chars;
|
2017-10-13 16:33:03 +00:00
|
|
|
chars = Op::Convert(c, 0, result, nullptr);
|
2013-01-21 16:11:31 +00:00
|
|
|
if (chars == 0) return 0;
|
|
|
|
CHECK_LE(chars, static_cast<int>(sizeof(result)));
|
|
|
|
if (!return_first && chars > 1) {
|
|
|
|
return 0;
|
2013-01-16 13:04:07 +00:00
|
|
|
}
|
2013-01-21 16:11:31 +00:00
|
|
|
return result[0];
|
2013-01-16 13:04:07 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 03:01:13 +00:00
|
|
|
#ifndef V8_INTL_SUPPORT
|
2013-01-21 16:11:31 +00:00
|
|
|
static void CheckCanonicalEquivalence(uint16_t c, uint16_t test) {
|
|
|
|
uint16_t expect = ConvertLatin1<unibrow::Ecma262UnCanonicalize, true>(c);
|
2018-02-05 14:29:59 +00:00
|
|
|
if (expect > unibrow::Latin1::kMaxChar || expect == 0) expect = c;
|
2013-01-21 16:11:31 +00:00
|
|
|
CHECK_EQ(expect, test);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Latin1IgnoreCase) {
|
2017-09-04 10:47:27 +00:00
|
|
|
for (uint16_t c = unibrow::Latin1::kMaxChar + 1; c != 0; c++) {
|
|
|
|
uint16_t lower = ConvertLatin1<unibrow::ToLowercase, false>(c);
|
|
|
|
uint16_t upper = ConvertLatin1<unibrow::ToUppercase, false>(c);
|
2018-02-05 14:29:59 +00:00
|
|
|
uint16_t test = unibrow::Latin1::TryConvertToLatin1(c);
|
2013-01-21 16:11:31 +00:00
|
|
|
// Filter out all character whose upper is not their lower or vice versa.
|
|
|
|
if (lower == 0 && upper == 0) {
|
|
|
|
CheckCanonicalEquivalence(c, test);
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-04 10:47:27 +00:00
|
|
|
if (lower > unibrow::Latin1::kMaxChar &&
|
|
|
|
upper > unibrow::Latin1::kMaxChar) {
|
2013-01-21 16:11:31 +00:00
|
|
|
CheckCanonicalEquivalence(c, test);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (lower == 0 && upper != 0) {
|
2017-09-04 10:47:27 +00:00
|
|
|
lower = ConvertLatin1<unibrow::ToLowercase, false>(upper);
|
2013-01-21 16:11:31 +00:00
|
|
|
}
|
|
|
|
if (upper == 0 && lower != c) {
|
2017-09-04 10:47:27 +00:00
|
|
|
upper = ConvertLatin1<unibrow::ToUppercase, false>(lower);
|
2013-01-21 16:11:31 +00:00
|
|
|
}
|
2017-09-04 10:47:27 +00:00
|
|
|
if (lower > unibrow::Latin1::kMaxChar &&
|
|
|
|
upper > unibrow::Latin1::kMaxChar) {
|
2013-01-21 16:11:31 +00:00
|
|
|
CheckCanonicalEquivalence(c, test);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (upper != c && lower != c) {
|
|
|
|
CheckCanonicalEquivalence(c, test);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CHECK_EQ(Min(upper, lower), test);
|
2013-01-16 13:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-29 03:01:13 +00:00
|
|
|
#endif
|
2014-03-25 09:09:24 +00:00
|
|
|
|
|
|
|
class DummyResource: public v8::String::ExternalStringResource {
|
|
|
|
public:
|
2018-09-14 15:34:02 +00:00
|
|
|
const uint16_t* data() const override { return nullptr; }
|
|
|
|
size_t length() const override { return 1 << 30; }
|
2014-03-25 09:09:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class DummyOneByteResource: public v8::String::ExternalOneByteStringResource {
|
|
|
|
public:
|
2018-09-14 15:34:02 +00:00
|
|
|
const char* data() const override { return nullptr; }
|
|
|
|
size_t length() const override { return 1 << 30; }
|
2014-03-25 09:09:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(InvalidExternalString) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
{ HandleScope scope(isolate);
|
|
|
|
DummyOneByteResource r;
|
2014-09-10 12:38:12 +00:00
|
|
|
CHECK(isolate->factory()->NewExternalStringFromOneByte(&r).is_null());
|
2014-03-25 09:09:24 +00:00
|
|
|
CHECK(isolate->has_pending_exception());
|
|
|
|
isolate->clear_pending_exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
{ HandleScope scope(isolate);
|
|
|
|
DummyResource r;
|
|
|
|
CHECK(isolate->factory()->NewExternalStringFromTwoByte(&r).is_null());
|
|
|
|
CHECK(isolate->has_pending_exception());
|
|
|
|
isolate->clear_pending_exception();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define INVALID_STRING_TEST(FUN, TYPE) \
|
|
|
|
TEST(StringOOM##FUN) { \
|
|
|
|
CcTest::InitializeVM(); \
|
|
|
|
LocalContext context; \
|
|
|
|
Isolate* isolate = CcTest::i_isolate(); \
|
|
|
|
STATIC_ASSERT(String::kMaxLength < kMaxInt); \
|
|
|
|
static const int invalid = String::kMaxLength + 1; \
|
|
|
|
HandleScope scope(isolate); \
|
|
|
|
Vector<TYPE> dummy = Vector<TYPE>::New(invalid); \
|
2015-01-16 07:42:00 +00:00
|
|
|
memset(dummy.start(), 0x0, dummy.length() * sizeof(TYPE)); \
|
2014-03-25 09:09:24 +00:00
|
|
|
CHECK(isolate->factory()->FUN(Vector<const TYPE>::cast(dummy)).is_null()); \
|
|
|
|
memset(dummy.start(), 0x20, dummy.length() * sizeof(TYPE)); \
|
|
|
|
CHECK(isolate->has_pending_exception()); \
|
|
|
|
isolate->clear_pending_exception(); \
|
|
|
|
dummy.Dispose(); \
|
|
|
|
}
|
|
|
|
|
|
|
|
INVALID_STRING_TEST(NewStringFromUtf8, char)
|
|
|
|
INVALID_STRING_TEST(NewStringFromOneByte, uint8_t)
|
|
|
|
|
|
|
|
#undef INVALID_STRING_TEST
|
2015-04-16 07:01:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(FormatMessage) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
Handle<String> arg0 = isolate->factory()->NewStringFromAsciiChecked("arg0");
|
|
|
|
Handle<String> arg1 = isolate->factory()->NewStringFromAsciiChecked("arg1");
|
|
|
|
Handle<String> arg2 = isolate->factory()->NewStringFromAsciiChecked("arg2");
|
|
|
|
Handle<String> result =
|
2018-10-11 21:35:34 +00:00
|
|
|
MessageFormatter::FormatMessage(
|
2018-07-17 15:08:58 +00:00
|
|
|
isolate, MessageTemplate::kPropertyNotFunction, arg0, arg1, arg2)
|
|
|
|
.ToHandleChecked();
|
2015-04-16 07:01:20 +00:00
|
|
|
Handle<String> expected = isolate->factory()->NewStringFromAsciiChecked(
|
2015-12-02 12:25:02 +00:00
|
|
|
"'arg0' returned for property 'arg1' of object 'arg2' is not a function");
|
2018-06-20 16:32:59 +00:00
|
|
|
CHECK(String::Equals(isolate, result, expected));
|
2015-04-16 07:01:20 +00:00
|
|
|
}
|
2016-05-21 16:58:17 +00:00
|
|
|
|
|
|
|
TEST(Regress609831) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Value> result = CompileRun(
|
|
|
|
"String.fromCharCode(32, 32, 32, 32, 32, "
|
|
|
|
"32, 32, 32, 32, 32, 32, 32, 32, 32, 32, "
|
|
|
|
"32, 32, 32, 32, 32, 32, 32, 32, 32, 32)");
|
|
|
|
CHECK(v8::Utils::OpenHandle(*result)->IsSeqOneByteString());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Value> result = CompileRun(
|
|
|
|
"String.fromCharCode(432, 432, 432, 432, 432, "
|
|
|
|
"432, 432, 432, 432, 432, 432, 432, 432, 432, "
|
|
|
|
"432, 432, 432, 432, 432, 432, 432, 432, 432)");
|
|
|
|
CHECK(v8::Utils::OpenHandle(*result)->IsSeqTwoByteString());
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 13:24:07 +00:00
|
|
|
|
|
|
|
TEST(ExternalStringIndexOf) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext context;
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
const char* raw_string = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
v8::Local<v8::String> string =
|
|
|
|
v8::String::NewExternalOneByte(CcTest::isolate(),
|
|
|
|
new StaticOneByteResource(raw_string))
|
|
|
|
.ToLocalChecked();
|
|
|
|
v8::Local<v8::Object> global = context->Global();
|
|
|
|
global->Set(context.local(), v8_str("external"), string).FromJust();
|
|
|
|
|
|
|
|
char source[] = "external.indexOf('%')";
|
|
|
|
for (size_t i = 0; i < strlen(raw_string); i++) {
|
|
|
|
source[18] = raw_string[i];
|
|
|
|
int result_position = static_cast<int>(i);
|
|
|
|
CHECK_EQ(result_position,
|
|
|
|
CompileRun(source)->Int32Value(context.local()).FromJust());
|
|
|
|
}
|
|
|
|
CHECK_EQ(-1,
|
|
|
|
CompileRun("external.indexOf('abcdefghijklmnopqrstuvwxyz%%%%%%')")
|
|
|
|
->Int32Value(context.local())
|
|
|
|
.FromJust());
|
|
|
|
CHECK_EQ(1, CompileRun("external.indexOf('', 1)")
|
|
|
|
->Int32Value(context.local())
|
|
|
|
.FromJust());
|
|
|
|
CHECK_EQ(-1, CompileRun("external.indexOf('a', 1)")
|
|
|
|
->Int32Value(context.local())
|
|
|
|
.FromJust());
|
|
|
|
CHECK_EQ(-1, CompileRun("external.indexOf('$')")
|
|
|
|
->Int32Value(context.local())
|
|
|
|
.FromJust());
|
|
|
|
}
|
2017-08-11 11:22:28 +00:00
|
|
|
|
2018-02-16 20:22:45 +00:00
|
|
|
#define GC_INSIDE_NEW_STRING_FROM_UTF8_SUB_STRING(NAME, STRING) \
|
|
|
|
TEST(GCInsideNewStringFromUtf8SubStringWith##NAME) { \
|
|
|
|
CcTest::InitializeVM(); \
|
|
|
|
LocalContext context; \
|
|
|
|
v8::HandleScope scope(CcTest::isolate()); \
|
|
|
|
Factory* factory = CcTest::i_isolate()->factory(); \
|
|
|
|
/* Length must be bigger than the buffer size of the Utf8Decoder. */ \
|
|
|
|
const char* buf = STRING; \
|
|
|
|
size_t len = strlen(buf); \
|
|
|
|
Handle<String> main_string = \
|
|
|
|
factory \
|
|
|
|
->NewStringFromOneByte(Vector<const uint8_t>( \
|
|
|
|
reinterpret_cast<const uint8_t*>(buf), len)) \
|
|
|
|
.ToHandleChecked(); \
|
2018-07-10 08:07:58 +00:00
|
|
|
CHECK(Heap::InNewSpace(*main_string)); \
|
2018-02-16 20:22:45 +00:00
|
|
|
/* Next allocation will cause GC. */ \
|
|
|
|
heap::SimulateFullSpace(CcTest::i_isolate()->heap()->new_space()); \
|
|
|
|
/* Offset by two to check substring-ing. */ \
|
|
|
|
Handle<String> s = factory \
|
|
|
|
->NewStringFromUtf8SubString( \
|
|
|
|
Handle<SeqOneByteString>::cast(main_string), 2, \
|
|
|
|
static_cast<int>(len - 2)) \
|
|
|
|
.ToHandleChecked(); \
|
|
|
|
Handle<String> expected_string = \
|
|
|
|
factory->NewStringFromUtf8(Vector<const char>(buf + 2, len - 2)) \
|
|
|
|
.ToHandleChecked(); \
|
|
|
|
CHECK(s->Equals(*expected_string)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
GC_INSIDE_NEW_STRING_FROM_UTF8_SUB_STRING(
|
|
|
|
OneByte,
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ")
|
|
|
|
GC_INSIDE_NEW_STRING_FROM_UTF8_SUB_STRING(
|
|
|
|
TwoByte,
|
|
|
|
"QQ\xF0\x9F\x98\x8D\xF0\x9F\x98\x8D"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"
|
|
|
|
"QQ\xF0\x9F\x98\x8D\xF0\x9F\x98\x8D")
|
|
|
|
|
|
|
|
#undef GC_INSIDE_NEW_STRING_FROM_UTF8_SUB_STRING
|
|
|
|
|
2018-05-08 12:50:28 +00:00
|
|
|
TEST(HashArrayIndexStrings) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext context;
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
|
|
|
CHECK_EQ(StringHasher::MakeArrayIndexHash(0 /* value */, 1 /* length */) >>
|
|
|
|
Name::kHashShift,
|
|
|
|
isolate->factory()->zero_string()->Hash());
|
|
|
|
|
|
|
|
CHECK_EQ(StringHasher::MakeArrayIndexHash(1 /* value */, 1 /* length */) >>
|
|
|
|
Name::kHashShift,
|
|
|
|
isolate->factory()->one_string()->Hash());
|
|
|
|
}
|
|
|
|
|
2018-07-31 10:34:09 +00:00
|
|
|
TEST(StringEquals) {
|
|
|
|
v8::V8::Initialize();
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
|
|
|
|
auto foo_str =
|
|
|
|
v8::String::NewFromUtf8(isolate, "foo", v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
auto bar_str =
|
|
|
|
v8::String::NewFromUtf8(isolate, "bar", v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
auto foo_str2 =
|
|
|
|
v8::String::NewFromUtf8(isolate, "foo", v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
|
|
|
|
uint16_t* two_byte_source = AsciiToTwoByteString("foo");
|
|
|
|
auto foo_two_byte_str =
|
|
|
|
v8::String::NewFromTwoByte(isolate, two_byte_source,
|
|
|
|
v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
i::DeleteArray(two_byte_source);
|
|
|
|
|
|
|
|
CHECK(foo_str->StringEquals(foo_str));
|
|
|
|
CHECK(!foo_str->StringEquals(bar_str));
|
|
|
|
CHECK(foo_str->StringEquals(foo_str2));
|
|
|
|
CHECK(foo_str->StringEquals(foo_two_byte_str));
|
|
|
|
CHECK(!bar_str->StringEquals(foo_str2));
|
|
|
|
}
|
|
|
|
|
2018-10-02 13:17:33 +00:00
|
|
|
class OneByteStringResource : public v8::String::ExternalOneByteStringResource {
|
|
|
|
public:
|
|
|
|
// Takes ownership of |data|.
|
|
|
|
OneByteStringResource(char* data, size_t length)
|
|
|
|
: data_(data), length_(length) {}
|
|
|
|
~OneByteStringResource() override { delete[] data_; }
|
|
|
|
const char* data() const override { return data_; }
|
|
|
|
size_t length() const override { return length_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
char* data_;
|
|
|
|
size_t length_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Regress876759) {
|
|
|
|
v8::V8::Initialize();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
|
|
|
|
const int kLength = 30;
|
|
|
|
uc16 two_byte_buf[kLength];
|
|
|
|
char* external_one_byte_buf = new char[kLength];
|
|
|
|
for (int j = 0; j < kLength; j++) {
|
|
|
|
char c = '0' + (j % 10);
|
|
|
|
two_byte_buf[j] = c;
|
|
|
|
external_one_byte_buf[j] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<String> parent;
|
|
|
|
{
|
|
|
|
Handle<SeqTwoByteString> raw =
|
|
|
|
factory->NewRawTwoByteString(kLength).ToHandleChecked();
|
2018-11-26 09:00:09 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2018-11-30 12:40:36 +00:00
|
|
|
CopyChars(raw->GetChars(no_gc), two_byte_buf, kLength);
|
2018-10-02 13:17:33 +00:00
|
|
|
parent = raw;
|
|
|
|
}
|
|
|
|
CHECK(parent->IsTwoByteRepresentation());
|
|
|
|
Handle<String> sliced = factory->NewSubString(parent, 1, 20);
|
|
|
|
CHECK(sliced->IsSlicedString());
|
|
|
|
factory->InternalizeString(parent);
|
|
|
|
CHECK(parent->IsThinString());
|
|
|
|
Handle<String> grandparent =
|
|
|
|
handle(ThinString::cast(*parent)->actual(), isolate);
|
|
|
|
CHECK_EQ(*parent, SlicedString::cast(*sliced)->parent());
|
|
|
|
OneByteStringResource* resource =
|
|
|
|
new OneByteStringResource(external_one_byte_buf, kLength);
|
|
|
|
grandparent->MakeExternal(resource);
|
|
|
|
// The grandparent string becomes one-byte, but the child strings are still
|
|
|
|
// two-byte.
|
|
|
|
CHECK(grandparent->IsOneByteRepresentation());
|
|
|
|
CHECK(parent->IsTwoByteRepresentation());
|
|
|
|
CHECK(sliced->IsTwoByteRepresentation());
|
2018-12-08 02:43:10 +00:00
|
|
|
// The *Underneath version returns the correct representation.
|
|
|
|
CHECK(String::IsOneByteRepresentationUnderneath(*sliced));
|
2018-10-02 13:17:33 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 03:29:52 +00:00
|
|
|
} // namespace test_strings
|
2017-08-11 11:22:28 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|