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 14823 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
14834 const char* script = "function foo(a) { return a+1; }"; | 14834 const char* script = "function foo(a) { return a+1; }"; |
14835 v8::ScriptCompiler::Source source(v8_str(script)); | 14835 v8::ScriptCompiler::Source source(v8_str(script)); |
14836 v8::ScriptCompiler::Compile(isolate, &source, | 14836 v8::ScriptCompiler::Compile(isolate, &source, |
14837 v8::ScriptCompiler::kProduceDataToCache); | 14837 v8::ScriptCompiler::kProduceDataToCache); |
14838 // Serialize. | 14838 // Serialize. |
14839 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); | 14839 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); |
14840 char* serialized_data = i::NewArray<char>(cd->length); | 14840 char* serialized_data = i::NewArray<char>(cd->length); |
14841 i::OS::MemCopy(serialized_data, cd->data, cd->length); | 14841 i::OS::MemCopy(serialized_data, cd->data, cd->length); |
14842 | 14842 |
14843 // Deserialize. | 14843 // Deserialize. |
14844 v8::ScriptData* deserialized = | 14844 i::ScriptDataImpl deserialized(serialized_data, cd->length); |
14845 v8::ScriptData::New(serialized_data, cd->length); | |
14846 | 14845 |
14847 // Verify that the original is the same as the deserialized. | 14846 // Verify that the original is the same as the deserialized. |
14848 CHECK_EQ(cd->length, deserialized->Length()); | 14847 CHECK_EQ(cd->length, deserialized.Length()); |
14849 CHECK_EQ(0, memcmp(cd->data, deserialized->Data(), cd->length)); | 14848 CHECK_EQ(0, memcmp(cd->data, deserialized.Data(), cd->length)); |
14850 | 14849 |
14851 delete deserialized; | |
14852 i::DeleteArray(serialized_data); | 14850 i::DeleteArray(serialized_data); |
14853 } | 14851 } |
14854 | 14852 |
14855 | 14853 |
14856 // Attempts to deserialize bad data. | 14854 // Attempts to deserialize bad data. |
14857 TEST(PreCompileDeserializationError) { | 14855 TEST(PreCompileDeserializationError) { |
14858 v8::V8::Initialize(); | 14856 v8::V8::Initialize(); |
14859 const char* data = "DONT CARE"; | 14857 const char* data = "DONT CARE"; |
14860 int invalid_size = 3; | 14858 int invalid_size = 3; |
14861 v8::ScriptData* sd = v8::ScriptData::New(data, invalid_size); | 14859 i::ScriptDataImpl sd(data, invalid_size); |
14862 | 14860 CHECK_EQ(0, sd.Length()); |
14863 CHECK_EQ(0, sd->Length()); | |
14864 | |
14865 delete sd; | |
14866 } | 14861 } |
14867 | 14862 |
14868 | 14863 |
14869 TEST(CompileWithInvalidCachedData) { | 14864 TEST(CompileWithInvalidCachedData) { |
14870 v8::V8::Initialize(); | 14865 v8::V8::Initialize(); |
14871 v8::Isolate* isolate = CcTest::isolate(); | 14866 v8::Isolate* isolate = CcTest::isolate(); |
14872 LocalContext context; | 14867 LocalContext context; |
14873 v8::HandleScope scope(context->GetIsolate()); | 14868 v8::HandleScope scope(context->GetIsolate()); |
14874 i::FLAG_min_preparse_length = 0; | 14869 i::FLAG_min_preparse_length = 0; |
14875 | 14870 |
14876 const char* script = "function foo(){ return 5;}\n" | 14871 const char* script = "function foo(){ return 5;}\n" |
14877 "function bar(){ return 6 + 7;} foo();"; | 14872 "function bar(){ return 6 + 7;} foo();"; |
14878 v8::ScriptCompiler::Source source(v8_str(script)); | 14873 v8::ScriptCompiler::Source source(v8_str(script)); |
14879 v8::ScriptCompiler::Compile(isolate, &source, | 14874 v8::ScriptCompiler::Compile(isolate, &source, |
14880 v8::ScriptCompiler::kProduceDataToCache); | 14875 v8::ScriptCompiler::kProduceDataToCache); |
14881 // source owns its cached data. Create a ScriptData based on it. The user | 14876 // source owns its cached data. Create a ScriptData based on it. The user |
14882 // never needs to create ScriptDatas any more; we only need it here because we | 14877 // never needs to create ScriptDatas any more; we only need it here because we |
14883 // want to modify the data before passing it back. | 14878 // want to modify the data before passing it back. |
14884 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); | 14879 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); |
14885 // ScriptData does not take ownership of the buffers passed to it. | 14880 // ScriptData does not take ownership of the buffers passed to it. |
14886 v8::ScriptData* sd = | 14881 i::ScriptDataImpl sd(reinterpret_cast<const char*>(cd->data), cd->length); |
14887 v8::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); | 14882 CHECK(!sd.HasError()); |
14888 CHECK(!sd->HasError()); | |
14889 // ScriptDataImpl private implementation details | 14883 // ScriptDataImpl private implementation details |
14890 const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; | 14884 const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; |
14891 const int kFunctionEntrySize = i::FunctionEntry::kSize; | 14885 const int kFunctionEntrySize = i::FunctionEntry::kSize; |
14892 const int kFunctionEntryStartOffset = 0; | 14886 const int kFunctionEntryStartOffset = 0; |
14893 const int kFunctionEntryEndOffset = 1; | 14887 const int kFunctionEntryEndOffset = 1; |
14894 unsigned* sd_data = | 14888 unsigned* sd_data = |
14895 reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | 14889 reinterpret_cast<unsigned*>(const_cast<char*>(sd.Data())); |
14896 | 14890 |
14897 // Overwrite function bar's end position with 0. | 14891 // Overwrite function bar's end position with 0. |
14898 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; | 14892 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; |
14899 v8::TryCatch try_catch; | 14893 v8::TryCatch try_catch; |
14900 | 14894 |
14901 // Make the script slightly different so that we don't hit the compilation | 14895 // Make the script slightly different so that we don't hit the compilation |
14902 // cache. Don't change the lenghts of tokens. | 14896 // cache. Don't change the lenghts of tokens. |
14903 const char* script2 = "function foo(){ return 6;}\n" | 14897 const char* script2 = "function foo(){ return 6;}\n" |
14904 "function bar(){ return 6 + 7;} foo();"; | 14898 "function bar(){ return 6 + 7;} foo();"; |
14905 v8::ScriptCompiler::Source source2( | 14899 v8::ScriptCompiler::Source source2( |
14906 v8_str(script2), | 14900 v8_str(script2), |
14907 // CachedData doesn't take ownership of the buffers, Source takes | 14901 // CachedData doesn't take ownership of the buffers, Source takes |
14908 // ownership of CachedData. | 14902 // ownership of CachedData. |
14909 new v8::ScriptCompiler::CachedData( | 14903 new v8::ScriptCompiler::CachedData( |
14910 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); | 14904 reinterpret_cast<const uint8_t*>(sd.Data()), sd.Length())); |
14911 Local<v8::UnboundScript> compiled_script = | 14905 Local<v8::UnboundScript> compiled_script = |
14912 v8::ScriptCompiler::CompileUnbound(isolate, &source2); | 14906 v8::ScriptCompiler::CompileUnbound(isolate, &source2); |
14913 | 14907 |
14914 CHECK(try_catch.HasCaught()); | 14908 CHECK(try_catch.HasCaught()); |
14915 String::Utf8Value exception_value(try_catch.Message()->Get()); | 14909 String::Utf8Value exception_value(try_catch.Message()->Get()); |
14916 CHECK_EQ("Uncaught SyntaxError: Invalid preparser data for function bar", | 14910 CHECK_EQ("Uncaught SyntaxError: Invalid preparser data for function bar", |
14917 *exception_value); | 14911 *exception_value); |
14918 | 14912 |
14919 try_catch.Reset(); | 14913 try_catch.Reset(); |
14920 delete sd; | |
14921 | 14914 |
14922 // Overwrite function bar's start position with 200. The function entry | 14915 // Overwrite function bar's start position with 200. The function entry |
14923 // will not be found when searching for it by position and we should fall | 14916 // will not be found when searching for it by position and we should fall |
14924 // back on eager compilation. | 14917 // back on eager compilation. |
14925 // ScriptData does not take ownership of the buffers passed to it. | 14918 // ScriptData does not take ownership of the buffers passed to it. |
14926 sd = v8::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); | 14919 i::ScriptDataImpl sd2(reinterpret_cast<const char*>(cd->data), cd->length); |
14927 sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | 14920 sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd2.Data())); |
14928 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = | 14921 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = |
14929 200; | 14922 200; |
14930 const char* script3 = "function foo(){ return 7;}\n" | 14923 const char* script3 = "function foo(){ return 7;}\n" |
14931 "function bar(){ return 6 + 7;} foo();"; | 14924 "function bar(){ return 6 + 7;} foo();"; |
14932 v8::ScriptCompiler::Source source3( | 14925 v8::ScriptCompiler::Source source3( |
14933 v8_str(script3), | 14926 v8_str(script3), |
14934 new v8::ScriptCompiler::CachedData( | 14927 new v8::ScriptCompiler::CachedData( |
14935 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); | 14928 reinterpret_cast<const uint8_t*>(sd2.Data()), sd2.Length())); |
14936 compiled_script = | 14929 compiled_script = |
14937 v8::ScriptCompiler::CompileUnbound(isolate, &source3); | 14930 v8::ScriptCompiler::CompileUnbound(isolate, &source3); |
14938 CHECK(!try_catch.HasCaught()); | 14931 CHECK(!try_catch.HasCaught()); |
14939 delete sd; | |
14940 } | 14932 } |
14941 | 14933 |
14942 | 14934 |
14943 // This tests that we do not allow dictionary load/call inline caches | 14935 // This tests that we do not allow dictionary load/call inline caches |
14944 // to use functions that have not yet been compiled. The potential | 14936 // to use functions that have not yet been compiled. The potential |
14945 // problem of loading a function that has not yet been compiled can | 14937 // problem of loading a function that has not yet been compiled can |
14946 // arise because we share code between contexts via the compilation | 14938 // arise because we share code between contexts via the compilation |
14947 // cache. | 14939 // cache. |
14948 THREADED_TEST(DictionaryICLoadedFunction) { | 14940 THREADED_TEST(DictionaryICLoadedFunction) { |
14949 v8::HandleScope scope(CcTest::isolate()); | 14941 v8::HandleScope scope(CcTest::isolate()); |
(...skipping 7440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
22390 "f.call(friend);"); | 22382 "f.call(friend);"); |
22391 CHECK_EQ(2, named_access_count); | 22383 CHECK_EQ(2, named_access_count); |
22392 | 22384 |
22393 // Test access using Object.setPrototypeOf reflective method. | 22385 // Test access using Object.setPrototypeOf reflective method. |
22394 named_access_count = 0; | 22386 named_access_count = 0; |
22395 CompileRun("Object.setPrototypeOf(friend, {});"); | 22387 CompileRun("Object.setPrototypeOf(friend, {});"); |
22396 CHECK_EQ(1, named_access_count); | 22388 CHECK_EQ(1, named_access_count); |
22397 CompileRun("Object.getPrototypeOf(friend);"); | 22389 CompileRun("Object.getPrototypeOf(friend);"); |
22398 CHECK_EQ(2, named_access_count); | 22390 CHECK_EQ(2, named_access_count); |
22399 } | 22391 } |
OLD | NEW |