20 static uint32_t gen() {
25 seed =
static_cast<unsigned int>(z);
26 return static_cast<uint32_t
>(
seed >> 16);
30 using namespace v8::internal;
35 static void InitializeVM() {
38 const char* extensions[] = {
"v8/print" };
47 static const int NUMBER_OF_BUILDING_BLOCKS = 128;
48 static const int DEEP_DEPTH = 8 * 1024;
49 static const int SUPER_DEEP_DEPTH = 80 * 1024;
56 length_ =
string.length();
59 virtual size_t length()
const {
return length_; }
71 length_ =
string.length();
73 virtual const char*
data()
const {
return data_; }
74 virtual size_t length()
const {
return length_; }
82 static void InitializeBuildingBlocks(
86 Zone* zone = Isolate::Current()->runtime_zone();
87 for (
int i = 0; i < NUMBER_OF_BUILDING_BLOCKS; i++) {
95 for (
int j = 0; j < len; j++) {
96 buf[j] = gen() % 65536;
100 for (
int j = 0; j < len; j++) {
101 CHECK_EQ(buf[j], building_blocks[i]->Get(j));
107 for (
int j = 0; j < len; j++) {
108 buf[j] = gen() % 128;
112 for (
int j = 0; j < len; j++) {
113 CHECK_EQ(buf[j], building_blocks[i]->Get(j));
119 for (
int j = 0; j < len; j++) {
120 buf[j] = gen() % 65536;
123 building_blocks[i] =
FACTORY->NewExternalStringFromTwoByte(resource);
124 for (
int j = 0; j < len; j++) {
125 CHECK_EQ(buf[j], building_blocks[i]->Get(j));
130 char* buf = NewArray<char>(len);
131 for (
int j = 0; j < len; j++) {
132 buf[j] = gen() % 128;
136 for (
int j = 0; j < len; j++) {
137 CHECK_EQ(buf[j], building_blocks[i]->Get(j));
139 DeleteArray<char>(buf);
151 for (
int i = 0; i < depth; i++) {
152 answer =
FACTORY->NewConsString(
154 building_blocks[i % NUMBER_OF_BUILDING_BLOCKS]);
164 for (
int i = depth - 1; i >= 0; i--) {
165 answer =
FACTORY->NewConsString(
166 building_blocks[i % NUMBER_OF_BUILDING_BLOCKS],
178 if (to - from == 1) {
179 return building_blocks[from % NUMBER_OF_BUILDING_BLOCKS];
181 if (to - from == 2) {
183 building_blocks[from % NUMBER_OF_BUILDING_BLOCKS],
184 building_blocks[(from+1) % NUMBER_OF_BUILDING_BLOCKS]);
187 ConstructBalancedHelper(building_blocks, from, from + ((to - from) / 2));
189 ConstructBalancedHelper(building_blocks, from + ((to - from) / 2), to);
190 return FACTORY->NewConsString(part1, part2);
196 return ConstructBalancedHelper(building_blocks, 0, DEEP_DEPTH);
208 CHECK(buffer2.has_more());
222 while (buffer.
has_more() && i < chars) {
223 CHECK(buffer2.has_more());
228 s1->Get(s1->length() - 1);
229 s2->Get(s2->length() - 1);
234 printf(
"TestTraverse\n");
238 ZoneScope zone(Isolate::Current()->runtime_zone(),
DELETE_ON_EXIT);
239 InitializeBuildingBlocks(building_blocks);
242 Handle<String> left_asymmetric = ConstructLeft(building_blocks, DEEP_DEPTH);
243 Handle<String> right_asymmetric = ConstructRight(building_blocks, DEEP_DEPTH);
246 Traverse(flat, symmetric);
248 Traverse(flat, left_asymmetric);
250 Traverse(flat, right_asymmetric);
253 ConstructLeft(building_blocks, SUPER_DEEP_DEPTH);
255 ConstructRight(building_blocks, SUPER_DEEP_DEPTH);
257 TraverseFirst(left_asymmetric, left_deep_asymmetric, 1050);
259 TraverseFirst(left_asymmetric, right_deep_asymmetric, 65536);
263 Traverse(flat, left_asymmetric);
267 Traverse(flat, right_asymmetric);
271 Traverse(flat, symmetric);
278 static const int DEEP_ASCII_DEPTH = 100000;
282 printf(
"TestDeepAscii\n");
286 char*
foo = NewArray<char>(DEEP_ASCII_DEPTH);
287 for (
int i = 0; i < DEEP_ASCII_DEPTH; i++) {
288 foo[i] =
"foo "[i % 4];
293 for (
int i = 0; i < DEEP_ASCII_DEPTH; i += 10) {
294 string =
FACTORY->NewConsString(
string, foo_string);
299 for (
int i = 0; i < 500; i++) {
300 TraverseFirst(flat_string,
string, DEEP_ASCII_DEPTH);
302 DeleteArray<char>(
foo);
311 const char* ascii_string =
"abcdef12345";
322 const uint16_t mixed_string[] = {0x02E4, 0x0064, 0x12E4, 0x0030, 0x3045};
324 const unsigned char as_utf8[11] = {0xCB, 0xA4, 0x64, 0xE1, 0x8B, 0xA4, 0x30,
325 0xE3, 0x81, 0x85, 0x00};
327 const int lengths[12] = {0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11};
328 const int char_lengths[12] = {0, 0, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5};
333 const char kNoChar =
static_cast<char>(-1);
334 for (
int i = 0; i <= 11; i++) {
336 for (
int j = 0; j < 11; j++)
339 int written = mixed->
WriteUtf8(buffer, i, &chars_written);
341 CHECK_EQ(char_lengths[i], chars_written);
343 for (
int j = 0; j < lengths[i]; j++)
344 CHECK_EQ(as_utf8[j], static_cast<unsigned char>(buffer[j]));
346 for (
int j = lengths[i]; j < 11; j++)
353 ZoneScope zonescope(Isolate::Current()->runtime_zone(),
DELETE_ON_EXIT);
357 Zone* zone = Isolate::Current()->runtime_zone();
360 static const int kMaxLength = 20;
370 for (
int i = 0; i <= kMaxLength; i++) {
371 char* ascii = zone->
NewArray<
char>(i + 1);
372 for (
int j = 0; j < i; j++) {
384 for (
int j = 0; j < i; j++) {
385 non_ascii[j] = 0x1234;
393 non_ascii_external_string);
398 global->
Set(v8_str(
"external_ascii"), ascii_external_strings);
399 global->
Set(v8_str(
"external_non_ascii"), non_ascii_external_strings);
403 static const char* source =
405 " var ascii_chars = 'aaaaaaaaaaaaaaaaaaaa';"
406 " var non_ascii_chars = '\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234';"
407 " if (ascii_chars.length != max_length) return 1;"
408 " if (non_ascii_chars.length != max_length) return 2;"
409 " var ascii = Array(max_length + 1);"
410 " var non_ascii = Array(max_length + 1);"
411 " for (var i = 0; i <= max_length; i++) {"
412 " ascii[i] = ascii_chars.substring(0, i);"
413 " non_ascii[i] = non_ascii_chars.substring(0, i);"
415 " for (var i = 0; i <= max_length; i++) {"
416 " if (ascii[i] != external_ascii[i]) return 3;"
417 " if (non_ascii[i] != external_non_ascii[i]) return 4;"
418 " for (var j = 0; j < i; j++) {"
419 " if (external_ascii[i] !="
420 " (external_ascii[j] + external_ascii[i - j])) return 5;"
421 " if (external_non_ascii[i] !="
422 " (external_non_ascii[j] + external_non_ascii[i - j])) return 6;"
423 " if (non_ascii[i] != (non_ascii[j] + non_ascii[i - j])) return 7;"
424 " if (ascii[i] != (ascii[j] + ascii[i - j])) return 8;"
425 " if (ascii[i] != (external_ascii[j] + ascii[i - j])) return 9;"
426 " if (ascii[i] != (ascii[j] + external_ascii[i - j])) return 10;"
427 " if (non_ascii[i] !="
428 " (external_non_ascii[j] + non_ascii[i - j])) return 11;"
429 " if (non_ascii[i] !="
430 " (non_ascii[j] + external_non_ascii[i - j])) return 12;"
436 CHECK_EQ(0, CompileRun(source)->Int32Value());
444 ZoneScope zone(Isolate::Current()->runtime_zone(),
DELETE_ON_EXIT);
450 const char* lines[] = {
453 "var s = \"1073741828\";",
474 for (
int i = 0; (line = lines[i]); i++) {
475 printf(
"%s\n", line);
489 FLAG_string_slices =
true;
495 CHECK(parent->IsConsString());
496 CHECK(!parent->IsFlat());
499 CHECK(parent->IsFlat());
500 CHECK(slice->IsSlicedString());
504 CHECK(slice->IsFlat());
513 virtual size_t length()
const {
return data_.length(); }
514 virtual const char*
data()
const {
return data_.start(); }
521 FLAG_string_slices =
true;
527 CHECK(string->IsExternalString());
529 CHECK(slice->IsSlicedString());
530 CHECK(string->IsExternalString());
533 CHECK(slice->IsFlat());
540 FLAG_string_slices =
true;
545 const char* init =
"var str = 'abcdefghijklmnopqrstuvwxyz';";
546 const char*
check =
"str.slice(0,26)";
547 const char* crosscheck =
"str.slice(1,25)";
551 result = CompileRun(check);
554 CHECK(!string->IsSlicedString());
556 string =
FACTORY->NewSubString(
string, 0, 26);
557 CHECK(!string->IsSlicedString());
558 result = CompileRun(crosscheck);
559 CHECK(result->IsString());
561 CHECK(string->IsSlicedString());
562 CHECK_EQ(
"bcdefghijklmnopqrstuvwxy", *(string->ToCString()));
569 FLAG_string_slices =
true;
574 const char* init =
"var str = 'abcdefghijklmnopqrstuvwxyz';";
575 const char* slice =
"var slice = str.slice(1,-1); slice";
576 const char* slice_from_slice =
"slice.slice(1,-1);";
579 result = CompileRun(slice);
582 CHECK(string->IsSlicedString());
584 CHECK_EQ(
"bcdefghijklmnopqrstuvwxy", *(string->ToCString()));
586 result = CompileRun(slice_from_slice);
589 CHECK(string->IsSlicedString());
591 CHECK_EQ(
"cdefghijklmnopqrstuvwx", *(string->ToCString()));
597 static const int K = 1024;
607 static const char* join_causing_out_of_memory =
608 "var two_14 = Math.pow(2, 14);"
609 "var two_17 = Math.pow(2, 17);"
610 "var s = Array(two_17 + 1).join('c');"
612 "for (var i = 1; i <= two_14; i++) a.push(s);"
628 static void CheckException(
const char* source) {
630 CHECK(CompileRun(source).IsEmpty());
637 FLAG_allow_natives_syntax =
true;
642 CompileRun(
"var short = 'abcdef';");
645 CheckException(
"%_SubString(short, 0, 10000);");
646 CheckException(
"%_SubString(short, -1234, 5);");
647 CheckException(
"%_SubString(short, 5, 2);");
649 CheckException(
"%_SubString(short, 1, Infinity);");
650 CheckException(
"%_SubString(short, NaN, 5);");
652 CheckException(
"%_SubString(short, '2', '5');");
654 result = CompileRun(
"%_SubString(short, Math.sqrt(4), 5.1);");
656 CHECK_EQ(
"cde", *(string->ToCString()));
658 CompileRun(
"var long = 'abcdefghijklmnopqrstuvwxyz';");
660 CheckException(
"%_SubString(long, 0, 10000);");
661 CheckException(
"%_SubString(long, -1234, 17);");
662 CheckException(
"%_SubString(long, 17, 2);");
664 CheckException(
"%_SubString(long, 1, Infinity);");
665 CheckException(
"%_SubString(long, NaN, 17);");
667 CheckException(
"%_SubString(long, '2', '17');");
669 result = CompileRun(
"%_SubString(long, Math.sqrt(4), 17.1);");
671 CHECK_EQ(
"cdefghijklmnopq", *(string->ToCString()));
675 CompileRun(
"var slice = long.slice(1, 15);");
676 CheckException(
"%_SubString(slice, 0, 17);");
688 "for (var i = 0; i < 16; i++) { "
691 "a.replace(/a/g, a); ");
697 TEST(StringReplaceAtomTwoByteResult) {
702 "var subject = 'ascii~only~string~'; "
703 "var replace = '\x80'; "
704 "subject.replace(/~/g, replace); ");
707 CHECK(string->IsSeqTwoByteString());
710 CHECK(expected->Equals(result));
static Local< Script > Compile(Handle< String > source, ScriptOrigin *origin=NULL, ScriptData *pre_data=NULL, Handle< String > script_data=Handle< String >())
void FlattenString(Handle< String > string)
V8EXPORT int WriteUtf8(char *buffer, int length=-1, int *nchars_ref=NULL, int options=NO_OPTIONS) const
void set_max_young_space_size(int value)
#define CHECK_EQ(expected, value)
bool HasOutOfMemoryException()
static Smi * FromInt(int value)
static V8EXPORT Local< String > New(const char *data, int length=-1)
V8EXPORT Local< Int32 > ToInt32() const
AsciiResource(Vector< const char > string)
virtual const char * data() const
static Smi * cast(Object *object)
static bool IsAscii(const char *chars, int length)
virtual size_t length() const
static const int kMinLength
void set_max_old_space_size(int value)
static SlicedString * cast(Object *obj)
V8EXPORT int Utf8Length() const
virtual ~AsciiVectorResource()
virtual size_t length() const
static String * Cast(v8::Value *obj)
virtual const uint16_t * data() const
virtual const char * data() const
Vector< const char > CStrVector(const char *data)
int StrLength(const char *string)
V8EXPORT bool IsNumber() const
AsciiVectorResource(i::Vector< const char > vector)
static V8EXPORT Local< Integer > New(int32_t value)
virtual size_t length() const
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
static Persistent< Context > New(ExtensionConfiguration *extensions=NULL, Handle< ObjectTemplate > global_template=Handle< ObjectTemplate >(), Handle< Value > global_object=Handle< Value >())
static V8EXPORT Local< String > NewExternal(ExternalStringResource *resource)
bool V8EXPORT SetResourceConstraints(ResourceConstraints *constraints)
static ConsString * cast(Object *obj)
void check(i::Vector< const char > string)
Resource(Vector< const uc16 > string)
static V8EXPORT Local< Object > New()
V8EXPORT bool Set(Handle< Value > key, Handle< Value > value, PropertyAttribute attribs=None)
static void IgnoreOutOfMemoryException()