OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 13985 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13996 } | 13996 } |
13997 | 13997 |
13998 | 13998 |
13999 // This test verifies that pre-compilation (aka preparsing) can be called | 13999 // This test verifies that pre-compilation (aka preparsing) can be called |
14000 // without initializing the whole VM. Thus we cannot run this test in a | 14000 // without initializing the whole VM. Thus we cannot run this test in a |
14001 // multi-threaded setup. | 14001 // multi-threaded setup. |
14002 TEST(PreCompile) { | 14002 TEST(PreCompile) { |
14003 // TODO(155): This test would break without the initialization of V8. This is | 14003 // TODO(155): This test would break without the initialization of V8. This is |
14004 // a workaround for now to make this test not fail. | 14004 // a workaround for now to make this test not fail. |
14005 v8::V8::Initialize(); | 14005 v8::V8::Initialize(); |
| 14006 v8::Isolate* isolate = CcTest::isolate(); |
14006 const char* script = "function foo(a) { return a+1; }"; | 14007 const char* script = "function foo(a) { return a+1; }"; |
14007 v8::ScriptData* sd = | 14008 v8::ScriptData* sd = |
14008 v8::ScriptData::PreCompile(script, i::StrLength(script)); | 14009 v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
14009 CHECK_NE(sd->Length(), 0); | 14010 CHECK_NE(sd->Length(), 0); |
14010 CHECK_NE(sd->Data(), NULL); | 14011 CHECK_NE(sd->Data(), NULL); |
14011 CHECK(!sd->HasError()); | 14012 CHECK(!sd->HasError()); |
14012 delete sd; | 14013 delete sd; |
14013 } | 14014 } |
14014 | 14015 |
14015 | 14016 |
14016 TEST(PreCompileWithError) { | 14017 TEST(PreCompileWithError) { |
14017 v8::V8::Initialize(); | 14018 v8::V8::Initialize(); |
| 14019 v8::Isolate* isolate = CcTest::isolate(); |
14018 const char* script = "function foo(a) { return 1 * * 2; }"; | 14020 const char* script = "function foo(a) { return 1 * * 2; }"; |
14019 v8::ScriptData* sd = | 14021 v8::ScriptData* sd = |
14020 v8::ScriptData::PreCompile(script, i::StrLength(script)); | 14022 v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
14021 CHECK(sd->HasError()); | 14023 CHECK(sd->HasError()); |
14022 delete sd; | 14024 delete sd; |
14023 } | 14025 } |
14024 | 14026 |
14025 | 14027 |
14026 TEST(Regress31661) { | 14028 TEST(Regress31661) { |
14027 v8::V8::Initialize(); | 14029 v8::V8::Initialize(); |
| 14030 v8::Isolate* isolate = CcTest::isolate(); |
14028 const char* script = " The Definintive Guide"; | 14031 const char* script = " The Definintive Guide"; |
14029 v8::ScriptData* sd = | 14032 v8::ScriptData* sd = |
14030 v8::ScriptData::PreCompile(script, i::StrLength(script)); | 14033 v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
14031 CHECK(sd->HasError()); | 14034 CHECK(sd->HasError()); |
14032 delete sd; | 14035 delete sd; |
14033 } | 14036 } |
14034 | 14037 |
14035 | 14038 |
14036 // Tests that ScriptData can be serialized and deserialized. | 14039 // Tests that ScriptData can be serialized and deserialized. |
14037 TEST(PreCompileSerialization) { | 14040 TEST(PreCompileSerialization) { |
14038 v8::V8::Initialize(); | 14041 v8::V8::Initialize(); |
| 14042 v8::Isolate* isolate = CcTest::isolate(); |
14039 const char* script = "function foo(a) { return a+1; }"; | 14043 const char* script = "function foo(a) { return a+1; }"; |
14040 v8::ScriptData* sd = | 14044 v8::ScriptData* sd = |
14041 v8::ScriptData::PreCompile(script, i::StrLength(script)); | 14045 v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
14042 | 14046 |
14043 // Serialize. | 14047 // Serialize. |
14044 int serialized_data_length = sd->Length(); | 14048 int serialized_data_length = sd->Length(); |
14045 char* serialized_data = i::NewArray<char>(serialized_data_length); | 14049 char* serialized_data = i::NewArray<char>(serialized_data_length); |
14046 i::OS::MemCopy(serialized_data, sd->Data(), serialized_data_length); | 14050 i::OS::MemCopy(serialized_data, sd->Data(), serialized_data_length); |
14047 | 14051 |
14048 // Deserialize. | 14052 // Deserialize. |
14049 v8::ScriptData* deserialized_sd = | 14053 v8::ScriptData* deserialized_sd = |
14050 v8::ScriptData::New(serialized_data, serialized_data_length); | 14054 v8::ScriptData::New(serialized_data, serialized_data_length); |
14051 | 14055 |
(...skipping 16 matching lines...) Expand all Loading... |
14068 | 14072 |
14069 CHECK_EQ(0, sd->Length()); | 14073 CHECK_EQ(0, sd->Length()); |
14070 | 14074 |
14071 delete sd; | 14075 delete sd; |
14072 } | 14076 } |
14073 | 14077 |
14074 | 14078 |
14075 // Attempts to deserialize bad data. | 14079 // Attempts to deserialize bad data. |
14076 TEST(PreCompileInvalidPreparseDataError) { | 14080 TEST(PreCompileInvalidPreparseDataError) { |
14077 v8::V8::Initialize(); | 14081 v8::V8::Initialize(); |
| 14082 v8::Isolate* isolate = CcTest::isolate(); |
14078 LocalContext context; | 14083 LocalContext context; |
14079 v8::HandleScope scope(context->GetIsolate()); | 14084 v8::HandleScope scope(context->GetIsolate()); |
14080 | 14085 |
14081 const char* script = "function foo(){ return 5;}\n" | 14086 const char* script = "function foo(){ return 5;}\n" |
14082 "function bar(){ return 6 + 7;} foo();"; | 14087 "function bar(){ return 6 + 7;} foo();"; |
14083 v8::ScriptData* sd = | 14088 v8::ScriptData* sd = |
14084 v8::ScriptData::PreCompile(script, i::StrLength(script)); | 14089 v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
14085 CHECK(!sd->HasError()); | 14090 CHECK(!sd->HasError()); |
14086 // ScriptDataImpl private implementation details | 14091 // ScriptDataImpl private implementation details |
14087 const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; | 14092 const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; |
14088 const int kFunctionEntrySize = i::FunctionEntry::kSize; | 14093 const int kFunctionEntrySize = i::FunctionEntry::kSize; |
14089 const int kFunctionEntryStartOffset = 0; | 14094 const int kFunctionEntryStartOffset = 0; |
14090 const int kFunctionEntryEndOffset = 1; | 14095 const int kFunctionEntryEndOffset = 1; |
14091 unsigned* sd_data = | 14096 unsigned* sd_data = |
14092 reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | 14097 reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); |
14093 | 14098 |
14094 // Overwrite function bar's end position with 0. | 14099 // Overwrite function bar's end position with 0. |
14095 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; | 14100 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; |
14096 v8::TryCatch try_catch; | 14101 v8::TryCatch try_catch; |
14097 | 14102 |
14098 Local<String> source = String::New(script); | 14103 Local<String> source = String::New(script); |
14099 Local<Script> compiled_script = Script::New(source, NULL, sd); | 14104 Local<Script> compiled_script = Script::New(source, NULL, sd); |
14100 CHECK(try_catch.HasCaught()); | 14105 CHECK(try_catch.HasCaught()); |
14101 String::Utf8Value exception_value(try_catch.Message()->Get()); | 14106 String::Utf8Value exception_value(try_catch.Message()->Get()); |
14102 CHECK_EQ("Uncaught SyntaxError: Invalid preparser data for function bar", | 14107 CHECK_EQ("Uncaught SyntaxError: Invalid preparser data for function bar", |
14103 *exception_value); | 14108 *exception_value); |
14104 | 14109 |
14105 try_catch.Reset(); | 14110 try_catch.Reset(); |
14106 | 14111 |
14107 // Overwrite function bar's start position with 200. The function entry | 14112 // Overwrite function bar's start position with 200. The function entry |
14108 // will not be found when searching for it by position and we should fall | 14113 // will not be found when searching for it by position and we should fall |
14109 // back on eager compilation. | 14114 // back on eager compilation. |
14110 sd = v8::ScriptData::PreCompile(script, i::StrLength(script)); | 14115 sd = v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
14111 sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | 14116 sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); |
14112 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = | 14117 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = |
14113 200; | 14118 200; |
14114 compiled_script = Script::New(source, NULL, sd); | 14119 compiled_script = Script::New(source, NULL, sd); |
14115 CHECK(!try_catch.HasCaught()); | 14120 CHECK(!try_catch.HasCaught()); |
14116 | 14121 |
14117 delete sd; | 14122 delete sd; |
14118 } | 14123 } |
14119 | 14124 |
14120 | 14125 |
14121 // Verifies that the Handle<String> and const char* versions of the API produce | 14126 // Verifies that the Handle<String> and const char* versions of the API produce |
14122 // the same results (at least for one trivial case). | 14127 // the same results (at least for one trivial case). |
14123 TEST(PreCompileAPIVariationsAreSame) { | 14128 TEST(PreCompileAPIVariationsAreSame) { |
14124 v8::V8::Initialize(); | 14129 v8::V8::Initialize(); |
14125 v8::HandleScope scope(CcTest::isolate()); | 14130 v8::Isolate* isolate = CcTest::isolate(); |
| 14131 v8::HandleScope scope(isolate); |
14126 | 14132 |
14127 const char* cstring = "function foo(a) { return a+1; }"; | 14133 const char* cstring = "function foo(a) { return a+1; }"; |
14128 | 14134 |
14129 v8::ScriptData* sd_from_cstring = | 14135 v8::ScriptData* sd_from_cstring = |
14130 v8::ScriptData::PreCompile(cstring, i::StrLength(cstring)); | 14136 v8::ScriptData::PreCompile(isolate, cstring, i::StrLength(cstring)); |
14131 | 14137 |
14132 TestAsciiResource* resource = new TestAsciiResource(cstring); | 14138 TestAsciiResource* resource = new TestAsciiResource(cstring); |
14133 v8::ScriptData* sd_from_external_string = v8::ScriptData::PreCompile( | 14139 v8::ScriptData* sd_from_external_string = v8::ScriptData::PreCompile( |
14134 v8::String::NewExternal(resource)); | 14140 v8::String::NewExternal(resource)); |
14135 | 14141 |
14136 v8::ScriptData* sd_from_string = v8::ScriptData::PreCompile( | 14142 v8::ScriptData* sd_from_string = v8::ScriptData::PreCompile( |
14137 v8::String::New(cstring)); | 14143 v8::String::New(cstring)); |
14138 | 14144 |
14139 CHECK_EQ(sd_from_cstring->Length(), sd_from_external_string->Length()); | 14145 CHECK_EQ(sd_from_cstring->Length(), sd_from_external_string->Length()); |
14140 CHECK_EQ(0, memcmp(sd_from_cstring->Data(), | 14146 CHECK_EQ(0, memcmp(sd_from_cstring->Data(), |
(...skipping 6468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
20609 } | 20615 } |
20610 for (int i = 0; i < runs; i++) { | 20616 for (int i = 0; i < runs; i++) { |
20611 Local<String> expected; | 20617 Local<String> expected; |
20612 if (i != 0) { | 20618 if (i != 0) { |
20613 CHECK_EQ(v8_str("escape value"), values[i]); | 20619 CHECK_EQ(v8_str("escape value"), values[i]); |
20614 } else { | 20620 } else { |
20615 CHECK(values[i].IsEmpty()); | 20621 CHECK(values[i].IsEmpty()); |
20616 } | 20622 } |
20617 } | 20623 } |
20618 } | 20624 } |
OLD | NEW |