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 14805 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
14816 // Failing access check to property get results in undefined. | 14816 // Failing access check to property get results in undefined. |
14817 CHECK(f1->Call(global, 0, NULL)->IsUndefined()); | 14817 CHECK(f1->Call(global, 0, NULL)->IsUndefined()); |
14818 CHECK(f2->Call(global, 0, NULL)->IsUndefined()); | 14818 CHECK(f2->Call(global, 0, NULL)->IsUndefined()); |
14819 | 14819 |
14820 // Failing access check to function call results in exception. | 14820 // Failing access check to function call results in exception. |
14821 CHECK(g1->Call(global, 0, NULL).IsEmpty()); | 14821 CHECK(g1->Call(global, 0, NULL).IsEmpty()); |
14822 CHECK(g2->Call(global, 0, NULL).IsEmpty()); | 14822 CHECK(g2->Call(global, 0, NULL).IsEmpty()); |
14823 } | 14823 } |
14824 | 14824 |
14825 | 14825 |
14826 // This test verifies that pre-compilation (aka preparsing) can be called | |
14827 // without initializing the whole VM. Thus we cannot run this test in a | |
14828 // multi-threaded setup. | |
14829 TEST(PreCompile) { | |
14830 // TODO(155): This test would break without the initialization of V8. This is | |
14831 // a workaround for now to make this test not fail. | |
14832 v8::V8::Initialize(); | |
14833 v8::Isolate* isolate = CcTest::isolate(); | |
14834 HandleScope handle_scope(isolate); | |
14835 const char* script = "function foo(a) { return a+1; }"; | |
14836 v8::ScriptData* sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( | |
14837 isolate, script, v8::String::kNormalString, i::StrLength(script))); | |
14838 CHECK_NE(sd->Length(), 0); | |
14839 CHECK_NE(sd->Data(), NULL); | |
14840 CHECK(!sd->HasError()); | |
14841 delete sd; | |
14842 } | |
14843 | |
14844 | |
14845 TEST(PreCompileWithError) { | |
14846 v8::V8::Initialize(); | |
14847 v8::Isolate* isolate = CcTest::isolate(); | |
14848 HandleScope handle_scope(isolate); | |
14849 const char* script = "function foo(a) { return 1 * * 2; }"; | |
14850 v8::ScriptData* sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( | |
14851 isolate, script, v8::String::kNormalString, i::StrLength(script))); | |
14852 CHECK(sd->HasError()); | |
14853 delete sd; | |
14854 } | |
14855 | |
14856 | |
14857 TEST(Regress31661) { | |
14858 v8::V8::Initialize(); | |
14859 v8::Isolate* isolate = CcTest::isolate(); | |
14860 HandleScope handle_scope(isolate); | |
14861 const char* script = " The Definintive Guide"; | |
14862 v8::ScriptData* sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( | |
14863 isolate, script, v8::String::kNormalString, i::StrLength(script))); | |
14864 CHECK(sd->HasError()); | |
14865 delete sd; | |
14866 } | |
14867 | |
14868 | |
14869 // Tests that ScriptData can be serialized and deserialized. | 14826 // Tests that ScriptData can be serialized and deserialized. |
14870 TEST(PreCompileSerialization) { | 14827 TEST(PreCompileSerialization) { |
14871 v8::V8::Initialize(); | 14828 v8::V8::Initialize(); |
14872 v8::Isolate* isolate = CcTest::isolate(); | 14829 LocalContext env; |
| 14830 v8::Isolate* isolate = env->GetIsolate(); |
14873 HandleScope handle_scope(isolate); | 14831 HandleScope handle_scope(isolate); |
| 14832 |
| 14833 i::FLAG_min_preparse_length = 0; |
14874 const char* script = "function foo(a) { return a+1; }"; | 14834 const char* script = "function foo(a) { return a+1; }"; |
14875 v8::ScriptData* sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( | 14835 v8::ScriptCompiler::Source source(v8_str(script)); |
14876 isolate, script, v8::String::kNormalString, i::StrLength(script))); | 14836 v8::ScriptCompiler::Compile(isolate, &source, |
14877 | 14837 v8::ScriptCompiler::kProduceDataToCache); |
14878 // Serialize. | 14838 // Serialize. |
14879 int serialized_data_length = sd->Length(); | 14839 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); |
14880 char* serialized_data = i::NewArray<char>(serialized_data_length); | 14840 char* serialized_data = i::NewArray<char>(cd->length); |
14881 i::OS::MemCopy(serialized_data, sd->Data(), serialized_data_length); | 14841 i::OS::MemCopy(serialized_data, cd->data, cd->length); |
14882 | 14842 |
14883 // Deserialize. | 14843 // Deserialize. |
14884 v8::ScriptData* deserialized_sd = | 14844 v8::ScriptData* deserialized = |
14885 v8::ScriptData::New(serialized_data, serialized_data_length); | 14845 v8::ScriptData::New(serialized_data, cd->length); |
14886 | 14846 |
14887 // Verify that the original is the same as the deserialized. | 14847 // Verify that the original is the same as the deserialized. |
14888 CHECK_EQ(sd->Length(), deserialized_sd->Length()); | 14848 CHECK_EQ(cd->length, deserialized->Length()); |
14889 CHECK_EQ(0, memcmp(sd->Data(), deserialized_sd->Data(), sd->Length())); | 14849 CHECK_EQ(0, memcmp(cd->data, deserialized->Data(), cd->length)); |
14890 CHECK_EQ(sd->HasError(), deserialized_sd->HasError()); | |
14891 | 14850 |
14892 delete sd; | 14851 delete deserialized; |
14893 delete deserialized_sd; | |
14894 i::DeleteArray(serialized_data); | 14852 i::DeleteArray(serialized_data); |
14895 } | 14853 } |
14896 | 14854 |
14897 | 14855 |
14898 // Attempts to deserialize bad data. | 14856 // Attempts to deserialize bad data. |
14899 TEST(PreCompileDeserializationError) { | 14857 TEST(PreCompileDeserializationError) { |
14900 v8::V8::Initialize(); | 14858 v8::V8::Initialize(); |
14901 const char* data = "DONT CARE"; | 14859 const char* data = "DONT CARE"; |
14902 int invalid_size = 3; | 14860 int invalid_size = 3; |
14903 v8::ScriptData* sd = v8::ScriptData::New(data, invalid_size); | 14861 v8::ScriptData* sd = v8::ScriptData::New(data, invalid_size); |
14904 | 14862 |
14905 CHECK_EQ(0, sd->Length()); | 14863 CHECK_EQ(0, sd->Length()); |
14906 | 14864 |
14907 delete sd; | 14865 delete sd; |
14908 } | 14866 } |
14909 | 14867 |
14910 | 14868 |
14911 // Attempts to deserialize bad data. | 14869 TEST(CompileWithInvalidCachedData) { |
14912 TEST(PreCompileInvalidPreparseDataError) { | |
14913 v8::V8::Initialize(); | 14870 v8::V8::Initialize(); |
14914 v8::Isolate* isolate = CcTest::isolate(); | 14871 v8::Isolate* isolate = CcTest::isolate(); |
14915 LocalContext context; | 14872 LocalContext context; |
14916 v8::HandleScope scope(context->GetIsolate()); | 14873 v8::HandleScope scope(context->GetIsolate()); |
| 14874 i::FLAG_min_preparse_length = 0; |
14917 | 14875 |
14918 const char* script = "function foo(){ return 5;}\n" | 14876 const char* script = "function foo(){ return 5;}\n" |
14919 "function bar(){ return 6 + 7;} foo();"; | 14877 "function bar(){ return 6 + 7;} foo();"; |
14920 v8::ScriptData* sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( | 14878 v8::ScriptCompiler::Source source(v8_str(script)); |
14921 isolate, script, v8::String::kNormalString, i::StrLength(script))); | 14879 v8::ScriptCompiler::Compile(isolate, &source, |
| 14880 v8::ScriptCompiler::kProduceDataToCache); |
| 14881 // 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 |
| 14883 // want to modify the data before passing it back. |
| 14884 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); |
| 14885 // ScriptData does not take ownership of the buffers passed to it. |
| 14886 v8::ScriptData* sd = |
| 14887 v8::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); |
14922 CHECK(!sd->HasError()); | 14888 CHECK(!sd->HasError()); |
14923 // ScriptDataImpl private implementation details | 14889 // ScriptDataImpl private implementation details |
14924 const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; | 14890 const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; |
14925 const int kFunctionEntrySize = i::FunctionEntry::kSize; | 14891 const int kFunctionEntrySize = i::FunctionEntry::kSize; |
14926 const int kFunctionEntryStartOffset = 0; | 14892 const int kFunctionEntryStartOffset = 0; |
14927 const int kFunctionEntryEndOffset = 1; | 14893 const int kFunctionEntryEndOffset = 1; |
14928 unsigned* sd_data = | 14894 unsigned* sd_data = |
14929 reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | 14895 reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); |
14930 | 14896 |
14931 // Overwrite function bar's end position with 0. | 14897 // Overwrite function bar's end position with 0. |
14932 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; | 14898 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; |
14933 v8::TryCatch try_catch; | 14899 v8::TryCatch try_catch; |
14934 | 14900 |
14935 v8::ScriptCompiler::Source script_source( | 14901 // Make the script slightly different so that we don't hit the compilation |
14936 String::NewFromUtf8(isolate, script), | 14902 // cache. Don't change the lenghts of tokens. |
| 14903 const char* script2 = "function foo(){ return 6;}\n" |
| 14904 "function bar(){ return 6 + 7;} foo();"; |
| 14905 v8::ScriptCompiler::Source source2( |
| 14906 v8_str(script2), |
| 14907 // CachedData doesn't take ownership of the buffers, Source takes |
| 14908 // ownership of CachedData. |
14937 new v8::ScriptCompiler::CachedData( | 14909 new v8::ScriptCompiler::CachedData( |
14938 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); | 14910 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); |
14939 Local<v8::UnboundScript> compiled_script = | 14911 Local<v8::UnboundScript> compiled_script = |
14940 v8::ScriptCompiler::CompileUnbound(isolate, &script_source); | 14912 v8::ScriptCompiler::CompileUnbound(isolate, &source2); |
14941 | 14913 |
14942 CHECK(try_catch.HasCaught()); | 14914 CHECK(try_catch.HasCaught()); |
14943 String::Utf8Value exception_value(try_catch.Message()->Get()); | 14915 String::Utf8Value exception_value(try_catch.Message()->Get()); |
14944 CHECK_EQ("Uncaught SyntaxError: Invalid preparser data for function bar", | 14916 CHECK_EQ("Uncaught SyntaxError: Invalid preparser data for function bar", |
14945 *exception_value); | 14917 *exception_value); |
14946 | 14918 |
14947 try_catch.Reset(); | 14919 try_catch.Reset(); |
14948 delete sd; | 14920 delete sd; |
14949 | 14921 |
14950 // Overwrite function bar's start position with 200. The function entry | 14922 // Overwrite function bar's start position with 200. The function entry |
14951 // will not be found when searching for it by position and we should fall | 14923 // will not be found when searching for it by position and we should fall |
14952 // back on eager compilation. | 14924 // back on eager compilation. |
14953 sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( | 14925 // ScriptData does not take ownership of the buffers passed to it. |
14954 isolate, script, v8::String::kNormalString, i::StrLength(script))); | 14926 sd = v8::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); |
14955 sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | 14927 sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); |
14956 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = | 14928 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = |
14957 200; | 14929 200; |
14958 v8::ScriptCompiler::Source script_source2( | 14930 const char* script3 = "function foo(){ return 7;}\n" |
14959 String::NewFromUtf8(isolate, script), | 14931 "function bar(){ return 6 + 7;} foo();"; |
| 14932 v8::ScriptCompiler::Source source3( |
| 14933 v8_str(script3), |
14960 new v8::ScriptCompiler::CachedData( | 14934 new v8::ScriptCompiler::CachedData( |
14961 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); | 14935 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); |
14962 compiled_script = | 14936 compiled_script = |
14963 v8::ScriptCompiler::CompileUnbound(isolate, &script_source2); | 14937 v8::ScriptCompiler::CompileUnbound(isolate, &source3); |
14964 CHECK(!try_catch.HasCaught()); | 14938 CHECK(!try_catch.HasCaught()); |
14965 | |
14966 delete sd; | 14939 delete sd; |
14967 } | 14940 } |
14968 | 14941 |
14969 | 14942 |
14970 // This tests that we do not allow dictionary load/call inline caches | 14943 // This tests that we do not allow dictionary load/call inline caches |
14971 // to use functions that have not yet been compiled. The potential | 14944 // to use functions that have not yet been compiled. The potential |
14972 // problem of loading a function that has not yet been compiled can | 14945 // problem of loading a function that has not yet been compiled can |
14973 // arise because we share code between contexts via the compilation | 14946 // arise because we share code between contexts via the compilation |
14974 // cache. | 14947 // cache. |
14975 THREADED_TEST(DictionaryICLoadedFunction) { | 14948 THREADED_TEST(DictionaryICLoadedFunction) { |
(...skipping 7441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
22417 "f.call(friend);"); | 22390 "f.call(friend);"); |
22418 CHECK_EQ(2, named_access_count); | 22391 CHECK_EQ(2, named_access_count); |
22419 | 22392 |
22420 // Test access using Object.setPrototypeOf reflective method. | 22393 // Test access using Object.setPrototypeOf reflective method. |
22421 named_access_count = 0; | 22394 named_access_count = 0; |
22422 CompileRun("Object.setPrototypeOf(friend, {});"); | 22395 CompileRun("Object.setPrototypeOf(friend, {});"); |
22423 CHECK_EQ(1, named_access_count); | 22396 CHECK_EQ(1, named_access_count); |
22424 CompileRun("Object.getPrototypeOf(friend);"); | 22397 CompileRun("Object.getPrototypeOf(friend);"); |
22425 CHECK_EQ(2, named_access_count); | 22398 CHECK_EQ(2, named_access_count); |
22426 } | 22399 } |
OLD | NEW |