Chromium Code Reviews| Index: test/cctest/test-api.cc |
| diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc |
| index 0ddf5ba7651470ae1cb6ec6ed9fd0fa04f6b2ba1..1906b6682e0be182358b3d0ab954b978dad2290e 100644 |
| --- a/test/cctest/test-api.cc |
| +++ b/test/cctest/test-api.cc |
| @@ -14814,15 +14814,15 @@ TEST(PreCompileSerialization) { |
| v8::ScriptCompiler::kProduceDataToCache); |
| // Serialize. |
| const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); |
| - char* serialized_data = i::NewArray<char>(cd->length); |
| + i::byte* serialized_data = i::NewArray<i::byte>(cd->length); |
| i::MemCopy(serialized_data, cd->data, cd->length); |
| // Deserialize. |
| - i::ScriptData* deserialized = i::ScriptData::New(serialized_data, cd->length); |
| + i::ScriptData* deserialized = new i::ScriptData(serialized_data, cd->length); |
| // Verify that the original is the same as the deserialized. |
| - CHECK_EQ(cd->length, deserialized->Length()); |
| - CHECK_EQ(0, memcmp(cd->data, deserialized->Data(), cd->length)); |
| + CHECK_EQ(cd->length, deserialized->length()); |
| + CHECK_EQ(0, memcmp(cd->data, deserialized->data(), cd->length)); |
| delete deserialized; |
| i::DeleteArray(serialized_data); |
| @@ -14830,25 +14830,37 @@ TEST(PreCompileSerialization) { |
| // Attempts to deserialize bad data. |
| -TEST(PreCompileDeserializationError) { |
| +TEST(ExpectFailPreCompileDeserializationError) { |
|
marja
2014/07/09 14:42:30
I'd just remove this test, it makes no sense to ke
Yang
2014/07/10 08:28:46
Done.
|
| v8::V8::Initialize(); |
| - const char* data = "DONT CARE"; |
| + const i::byte data[] = {1, 2, 3, 4}; |
| int invalid_size = 3; |
| - i::ScriptData* sd = i::ScriptData::New(data, invalid_size); |
| - CHECK_EQ(NULL, sd); |
| + i::ScriptData* sd = new i::ScriptData(data, invalid_size); |
| + i::ParseData* pd = new i::ParseData(sd); |
| + delete sd; |
| + delete pd; |
| } |
| -TEST(CompileWithInvalidCachedData) { |
| +static const char* cached_script_source = |
| + "function foo(){ return 5;}\n" |
| + "function bar(){ return 6 + 7;} foo();"; |
| + |
| + |
| +// ScriptData private implementation details |
| +static const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; |
| +static const int kFunctionEntrySize = i::FunctionEntry::kSize; |
| +static const int kFunctionEntryStartOffset = 0; |
| +static const int kFunctionEntryEndOffset = 1; |
| + |
| + |
| +TEST(ExpectFailCompileWithInvalidCachedData1) { |
| v8::V8::Initialize(); |
| v8::Isolate* isolate = CcTest::isolate(); |
| LocalContext context; |
| v8::HandleScope scope(context->GetIsolate()); |
| i::FLAG_min_preparse_length = 0; |
| - const char* script = "function foo(){ return 5;}\n" |
| - "function bar(){ return 6 + 7;} foo();"; |
| - v8::ScriptCompiler::Source source(v8_str(script)); |
| + v8::ScriptCompiler::Source source(v8_str(cached_script_source)); |
| v8::ScriptCompiler::Compile(isolate, &source, |
| v8::ScriptCompiler::kProduceDataToCache); |
| // source owns its cached data. Create a ScriptData based on it. The user |
| @@ -14856,20 +14868,13 @@ TEST(CompileWithInvalidCachedData) { |
| // want to modify the data before passing it back. |
| const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); |
| // ScriptData does not take ownership of the buffers passed to it. |
| - i::ScriptData* sd = |
| - i::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); |
| - CHECK(!sd->HasError()); |
| - // ScriptData private implementation details |
| - const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; |
| - const int kFunctionEntrySize = i::FunctionEntry::kSize; |
| - const int kFunctionEntryStartOffset = 0; |
| - const int kFunctionEntryEndOffset = 1; |
| - unsigned* sd_data = |
| - reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); |
| + i::ScriptData* sd = new i::ScriptData(cd->data, cd->length); |
| + i::ParseData pd(sd); |
| + CHECK(!pd.HasError()); |
| + unsigned* pd_data = pd.Data(); |
| // Overwrite function bar's end position with 0. |
| - sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; |
| - v8::TryCatch try_catch; |
| + pd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; |
| // Make the script slightly different so that we don't hit the compilation |
| // cache. Don't change the lenghts of tokens. |
| @@ -14879,65 +14884,73 @@ TEST(CompileWithInvalidCachedData) { |
| v8_str(script2), |
| // CachedData doesn't take ownership of the buffers, Source takes |
| // ownership of CachedData. |
| - new v8::ScriptCompiler::CachedData( |
| - reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); |
| - Local<v8::UnboundScript> compiled_script = |
| - v8::ScriptCompiler::CompileUnbound(isolate, &source2); |
| + new v8::ScriptCompiler::CachedData(sd->data(), sd->length())); |
| + v8::ScriptCompiler::CompileUnbound(isolate, &source2); |
| - CHECK(try_catch.HasCaught()); |
| - { |
| - String::Utf8Value exception_value(try_catch.Message()->Get()); |
| - CHECK_EQ("Uncaught SyntaxError: Invalid cached data for function bar", |
| - *exception_value); |
| - } |
| - |
| - try_catch.Reset(); |
| delete sd; |
| +} |
| + |
| + |
| +TEST(ExpectFailCompileWithInvalidCachedData2) { |
| + v8::V8::Initialize(); |
| + v8::Isolate* isolate = CcTest::isolate(); |
| + LocalContext context; |
| + v8::HandleScope scope(context->GetIsolate()); |
| + i::FLAG_min_preparse_length = 0; |
| + |
| + v8::ScriptCompiler::Source source(v8_str(cached_script_source)); |
| + v8::ScriptCompiler::Compile(isolate, &source, |
| + v8::ScriptCompiler::kProduceDataToCache); |
| + // source owns its cached data. Create a ScriptData based on it. The user |
| + // never needs to create ScriptDatas any more; we only need it here because we |
| + // want to modify the data before passing it back. |
| + const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); |
| + // ScriptData does not take ownership of the buffers passed to it. |
| + i::ScriptData* sd = new i::ScriptData(cd->data, cd->length); |
| + i::ParseData pd(sd); |
| // Overwrite function bar's start position with 200. The function entry will |
| // not be found when searching for it by position, and the compilation fails. |
| - |
| // ScriptData does not take ownership of the buffers passed to it. |
| - sd = i::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); |
| - sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); |
| - sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = |
| + unsigned* pd_data = pd.Data(); |
| + pd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = |
| 200; |
| - const char* script3 = "function foo(){ return 7;}\n" |
| + const char* script2 = |
| + "function foo(){ return 7;}\n" |
| "function bar(){ return 6 + 7;} foo();"; |
| - v8::ScriptCompiler::Source source3( |
| - v8_str(script3), |
| - new v8::ScriptCompiler::CachedData( |
| - reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); |
| - compiled_script = |
| - v8::ScriptCompiler::CompileUnbound(isolate, &source3); |
| - CHECK(try_catch.HasCaught()); |
| - { |
| - String::Utf8Value exception_value(try_catch.Message()->Get()); |
| - CHECK_EQ("Uncaught SyntaxError: Invalid cached data for function bar", |
| - *exception_value); |
| - } |
| - CHECK(compiled_script.IsEmpty()); |
| - try_catch.Reset(); |
| + v8::ScriptCompiler::Source source2( |
| + v8_str(script2), |
| + new v8::ScriptCompiler::CachedData(sd->data(), sd->length())); |
| + v8::ScriptCompiler::CompileUnbound(isolate, &source2); |
| delete sd; |
| +} |
| + |
| + |
| +TEST(ExpectFailCompileWithInvalidCachedData3) { |
| + v8::V8::Initialize(); |
| + v8::Isolate* isolate = CcTest::isolate(); |
| + LocalContext context; |
| + v8::HandleScope scope(context->GetIsolate()); |
| + i::FLAG_min_preparse_length = 0; |
| + |
| + v8::ScriptCompiler::Source source(v8_str(cached_script_source)); |
| + v8::ScriptCompiler::Compile(isolate, &source, |
| + v8::ScriptCompiler::kProduceDataToCache); |
| + // source owns its cached data. Create a ScriptData based on it. The user |
| + // never needs to create ScriptDatas any more; we only need it here because we |
| + // want to modify the data before passing it back. |
| + const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); |
| + // ScriptData does not take ownership of the buffers passed to it. |
| + i::ScriptData* sd = new i::ScriptData(cd->data, cd->length); |
| // Try passing in cached data which is obviously invalid (wrong length). |
| - sd = i::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); |
| - const char* script4 = |
| + const char* script2 = |
| "function foo(){ return 8;}\n" |
| "function bar(){ return 6 + 7;} foo();"; |
| - v8::ScriptCompiler::Source source4( |
| - v8_str(script4), |
| - new v8::ScriptCompiler::CachedData( |
| - reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length() - 1)); |
| - compiled_script = |
| - v8::ScriptCompiler::CompileUnbound(isolate, &source4); |
| - CHECK(try_catch.HasCaught()); |
| - { |
| - String::Utf8Value exception_value(try_catch.Message()->Get()); |
| - CHECK_EQ("Uncaught SyntaxError: Invalid cached data", |
| - *exception_value); |
| - } |
| - CHECK(compiled_script.IsEmpty()); |
| + v8::ScriptCompiler::Source source2( |
| + v8_str(script2), |
| + new v8::ScriptCompiler::CachedData(sd->data(), sd->length() - 1)); |
| + v8::ScriptCompiler::CompileUnbound(isolate, &source2); |
| delete sd; |
| } |