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 14796 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
14807 v8::Isolate* isolate = env->GetIsolate(); | 14807 v8::Isolate* isolate = env->GetIsolate(); |
14808 HandleScope handle_scope(isolate); | 14808 HandleScope handle_scope(isolate); |
14809 | 14809 |
14810 i::FLAG_min_preparse_length = 0; | 14810 i::FLAG_min_preparse_length = 0; |
14811 const char* script = "function foo(a) { return a+1; }"; | 14811 const char* script = "function foo(a) { return a+1; }"; |
14812 v8::ScriptCompiler::Source source(v8_str(script)); | 14812 v8::ScriptCompiler::Source source(v8_str(script)); |
14813 v8::ScriptCompiler::Compile(isolate, &source, | 14813 v8::ScriptCompiler::Compile(isolate, &source, |
14814 v8::ScriptCompiler::kProduceDataToCache); | 14814 v8::ScriptCompiler::kProduceDataToCache); |
14815 // Serialize. | 14815 // Serialize. |
14816 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); | 14816 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); |
14817 char* serialized_data = i::NewArray<char>(cd->length); | 14817 i::byte* serialized_data = i::NewArray<i::byte>(cd->length); |
14818 i::MemCopy(serialized_data, cd->data, cd->length); | 14818 i::MemCopy(serialized_data, cd->data, cd->length); |
14819 | 14819 |
14820 // Deserialize. | 14820 // Deserialize. |
14821 i::ScriptData* deserialized = i::ScriptData::New(serialized_data, cd->length); | 14821 i::ScriptData* deserialized = new i::ScriptData(serialized_data, cd->length); |
14822 | 14822 |
14823 // Verify that the original is the same as the deserialized. | 14823 // Verify that the original is the same as the deserialized. |
14824 CHECK_EQ(cd->length, deserialized->Length()); | 14824 CHECK_EQ(cd->length, deserialized->length()); |
14825 CHECK_EQ(0, memcmp(cd->data, deserialized->Data(), cd->length)); | 14825 CHECK_EQ(0, memcmp(cd->data, deserialized->data(), cd->length)); |
14826 | 14826 |
14827 delete deserialized; | 14827 delete deserialized; |
14828 i::DeleteArray(serialized_data); | 14828 i::DeleteArray(serialized_data); |
14829 } | 14829 } |
14830 | 14830 |
14831 | 14831 |
14832 // Attempts to deserialize bad data. | 14832 // Attempts to deserialize bad data. |
14833 TEST(PreCompileDeserializationError) { | 14833 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.
| |
14834 v8::V8::Initialize(); | 14834 v8::V8::Initialize(); |
14835 const char* data = "DONT CARE"; | 14835 const i::byte data[] = {1, 2, 3, 4}; |
14836 int invalid_size = 3; | 14836 int invalid_size = 3; |
14837 i::ScriptData* sd = i::ScriptData::New(data, invalid_size); | 14837 i::ScriptData* sd = new i::ScriptData(data, invalid_size); |
14838 CHECK_EQ(NULL, sd); | 14838 i::ParseData* pd = new i::ParseData(sd); |
14839 delete sd; | |
14840 delete pd; | |
14839 } | 14841 } |
14840 | 14842 |
14841 | 14843 |
14842 TEST(CompileWithInvalidCachedData) { | 14844 static const char* cached_script_source = |
14845 "function foo(){ return 5;}\n" | |
14846 "function bar(){ return 6 + 7;} foo();"; | |
14847 | |
14848 | |
14849 // ScriptData private implementation details | |
14850 static const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; | |
14851 static const int kFunctionEntrySize = i::FunctionEntry::kSize; | |
14852 static const int kFunctionEntryStartOffset = 0; | |
14853 static const int kFunctionEntryEndOffset = 1; | |
14854 | |
14855 | |
14856 TEST(ExpectFailCompileWithInvalidCachedData1) { | |
14843 v8::V8::Initialize(); | 14857 v8::V8::Initialize(); |
14844 v8::Isolate* isolate = CcTest::isolate(); | 14858 v8::Isolate* isolate = CcTest::isolate(); |
14845 LocalContext context; | 14859 LocalContext context; |
14846 v8::HandleScope scope(context->GetIsolate()); | 14860 v8::HandleScope scope(context->GetIsolate()); |
14847 i::FLAG_min_preparse_length = 0; | 14861 i::FLAG_min_preparse_length = 0; |
14848 | 14862 |
14849 const char* script = "function foo(){ return 5;}\n" | 14863 v8::ScriptCompiler::Source source(v8_str(cached_script_source)); |
14850 "function bar(){ return 6 + 7;} foo();"; | |
14851 v8::ScriptCompiler::Source source(v8_str(script)); | |
14852 v8::ScriptCompiler::Compile(isolate, &source, | 14864 v8::ScriptCompiler::Compile(isolate, &source, |
14853 v8::ScriptCompiler::kProduceDataToCache); | 14865 v8::ScriptCompiler::kProduceDataToCache); |
14854 // source owns its cached data. Create a ScriptData based on it. The user | 14866 // source owns its cached data. Create a ScriptData based on it. The user |
14855 // never needs to create ScriptDatas any more; we only need it here because we | 14867 // never needs to create ScriptDatas any more; we only need it here because we |
14856 // want to modify the data before passing it back. | 14868 // want to modify the data before passing it back. |
14857 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); | 14869 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); |
14858 // ScriptData does not take ownership of the buffers passed to it. | 14870 // ScriptData does not take ownership of the buffers passed to it. |
14859 i::ScriptData* sd = | 14871 i::ScriptData* sd = new i::ScriptData(cd->data, cd->length); |
14860 i::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); | 14872 i::ParseData pd(sd); |
14861 CHECK(!sd->HasError()); | 14873 CHECK(!pd.HasError()); |
14862 // ScriptData private implementation details | 14874 unsigned* pd_data = pd.Data(); |
14863 const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; | |
14864 const int kFunctionEntrySize = i::FunctionEntry::kSize; | |
14865 const int kFunctionEntryStartOffset = 0; | |
14866 const int kFunctionEntryEndOffset = 1; | |
14867 unsigned* sd_data = | |
14868 reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | |
14869 | 14875 |
14870 // Overwrite function bar's end position with 0. | 14876 // Overwrite function bar's end position with 0. |
14871 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; | 14877 pd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; |
14872 v8::TryCatch try_catch; | |
14873 | 14878 |
14874 // Make the script slightly different so that we don't hit the compilation | 14879 // Make the script slightly different so that we don't hit the compilation |
14875 // cache. Don't change the lenghts of tokens. | 14880 // cache. Don't change the lenghts of tokens. |
14876 const char* script2 = "function foo(){ return 6;}\n" | 14881 const char* script2 = "function foo(){ return 6;}\n" |
14877 "function bar(){ return 6 + 7;} foo();"; | 14882 "function bar(){ return 6 + 7;} foo();"; |
14878 v8::ScriptCompiler::Source source2( | 14883 v8::ScriptCompiler::Source source2( |
14879 v8_str(script2), | 14884 v8_str(script2), |
14880 // CachedData doesn't take ownership of the buffers, Source takes | 14885 // CachedData doesn't take ownership of the buffers, Source takes |
14881 // ownership of CachedData. | 14886 // ownership of CachedData. |
14882 new v8::ScriptCompiler::CachedData( | 14887 new v8::ScriptCompiler::CachedData(sd->data(), sd->length())); |
14883 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); | 14888 v8::ScriptCompiler::CompileUnbound(isolate, &source2); |
14884 Local<v8::UnboundScript> compiled_script = | |
14885 v8::ScriptCompiler::CompileUnbound(isolate, &source2); | |
14886 | 14889 |
14887 CHECK(try_catch.HasCaught()); | 14890 delete sd; |
14888 { | 14891 } |
14889 String::Utf8Value exception_value(try_catch.Message()->Get()); | |
14890 CHECK_EQ("Uncaught SyntaxError: Invalid cached data for function bar", | |
14891 *exception_value); | |
14892 } | |
14893 | 14892 |
14894 try_catch.Reset(); | 14893 |
14895 delete sd; | 14894 TEST(ExpectFailCompileWithInvalidCachedData2) { |
14895 v8::V8::Initialize(); | |
14896 v8::Isolate* isolate = CcTest::isolate(); | |
14897 LocalContext context; | |
14898 v8::HandleScope scope(context->GetIsolate()); | |
14899 i::FLAG_min_preparse_length = 0; | |
14900 | |
14901 v8::ScriptCompiler::Source source(v8_str(cached_script_source)); | |
14902 v8::ScriptCompiler::Compile(isolate, &source, | |
14903 v8::ScriptCompiler::kProduceDataToCache); | |
14904 // source owns its cached data. Create a ScriptData based on it. The user | |
14905 // never needs to create ScriptDatas any more; we only need it here because we | |
14906 // want to modify the data before passing it back. | |
14907 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); | |
14908 // ScriptData does not take ownership of the buffers passed to it. | |
14909 i::ScriptData* sd = new i::ScriptData(cd->data, cd->length); | |
14910 i::ParseData pd(sd); | |
14896 | 14911 |
14897 // Overwrite function bar's start position with 200. The function entry will | 14912 // Overwrite function bar's start position with 200. The function entry will |
14898 // not be found when searching for it by position, and the compilation fails. | 14913 // not be found when searching for it by position, and the compilation fails. |
14899 | |
14900 // ScriptData does not take ownership of the buffers passed to it. | 14914 // ScriptData does not take ownership of the buffers passed to it. |
14901 sd = i::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); | 14915 unsigned* pd_data = pd.Data(); |
14902 sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | 14916 pd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = |
14903 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = | |
14904 200; | 14917 200; |
14905 const char* script3 = "function foo(){ return 7;}\n" | 14918 const char* script2 = |
14919 "function foo(){ return 7;}\n" | |
14906 "function bar(){ return 6 + 7;} foo();"; | 14920 "function bar(){ return 6 + 7;} foo();"; |
14907 v8::ScriptCompiler::Source source3( | 14921 v8::ScriptCompiler::Source source2( |
14908 v8_str(script3), | 14922 v8_str(script2), |
14909 new v8::ScriptCompiler::CachedData( | 14923 new v8::ScriptCompiler::CachedData(sd->data(), sd->length())); |
14910 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length())); | 14924 v8::ScriptCompiler::CompileUnbound(isolate, &source2); |
14911 compiled_script = | |
14912 v8::ScriptCompiler::CompileUnbound(isolate, &source3); | |
14913 CHECK(try_catch.HasCaught()); | |
14914 { | |
14915 String::Utf8Value exception_value(try_catch.Message()->Get()); | |
14916 CHECK_EQ("Uncaught SyntaxError: Invalid cached data for function bar", | |
14917 *exception_value); | |
14918 } | |
14919 CHECK(compiled_script.IsEmpty()); | |
14920 try_catch.Reset(); | |
14921 delete sd; | |
14922 | |
14923 // Try passing in cached data which is obviously invalid (wrong length). | |
14924 sd = i::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length); | |
14925 const char* script4 = | |
14926 "function foo(){ return 8;}\n" | |
14927 "function bar(){ return 6 + 7;} foo();"; | |
14928 v8::ScriptCompiler::Source source4( | |
14929 v8_str(script4), | |
14930 new v8::ScriptCompiler::CachedData( | |
14931 reinterpret_cast<const uint8_t*>(sd->Data()), sd->Length() - 1)); | |
14932 compiled_script = | |
14933 v8::ScriptCompiler::CompileUnbound(isolate, &source4); | |
14934 CHECK(try_catch.HasCaught()); | |
14935 { | |
14936 String::Utf8Value exception_value(try_catch.Message()->Get()); | |
14937 CHECK_EQ("Uncaught SyntaxError: Invalid cached data", | |
14938 *exception_value); | |
14939 } | |
14940 CHECK(compiled_script.IsEmpty()); | |
14941 delete sd; | 14925 delete sd; |
14942 } | 14926 } |
14943 | 14927 |
14928 | |
14929 TEST(ExpectFailCompileWithInvalidCachedData3) { | |
14930 v8::V8::Initialize(); | |
14931 v8::Isolate* isolate = CcTest::isolate(); | |
14932 LocalContext context; | |
14933 v8::HandleScope scope(context->GetIsolate()); | |
14934 i::FLAG_min_preparse_length = 0; | |
14935 | |
14936 v8::ScriptCompiler::Source source(v8_str(cached_script_source)); | |
14937 v8::ScriptCompiler::Compile(isolate, &source, | |
14938 v8::ScriptCompiler::kProduceDataToCache); | |
14939 // source owns its cached data. Create a ScriptData based on it. The user | |
14940 // never needs to create ScriptDatas any more; we only need it here because we | |
14941 // want to modify the data before passing it back. | |
14942 const v8::ScriptCompiler::CachedData* cd = source.GetCachedData(); | |
14943 // ScriptData does not take ownership of the buffers passed to it. | |
14944 i::ScriptData* sd = new i::ScriptData(cd->data, cd->length); | |
14945 | |
14946 // Try passing in cached data which is obviously invalid (wrong length). | |
14947 const char* script2 = | |
14948 "function foo(){ return 8;}\n" | |
14949 "function bar(){ return 6 + 7;} foo();"; | |
14950 v8::ScriptCompiler::Source source2( | |
14951 v8_str(script2), | |
14952 new v8::ScriptCompiler::CachedData(sd->data(), sd->length() - 1)); | |
14953 v8::ScriptCompiler::CompileUnbound(isolate, &source2); | |
14954 delete sd; | |
14955 } | |
14956 | |
14944 | 14957 |
14945 // This tests that we do not allow dictionary load/call inline caches | 14958 // This tests that we do not allow dictionary load/call inline caches |
14946 // to use functions that have not yet been compiled. The potential | 14959 // to use functions that have not yet been compiled. The potential |
14947 // problem of loading a function that has not yet been compiled can | 14960 // problem of loading a function that has not yet been compiled can |
14948 // arise because we share code between contexts via the compilation | 14961 // arise because we share code between contexts via the compilation |
14949 // cache. | 14962 // cache. |
14950 THREADED_TEST(DictionaryICLoadedFunction) { | 14963 THREADED_TEST(DictionaryICLoadedFunction) { |
14951 v8::HandleScope scope(CcTest::isolate()); | 14964 v8::HandleScope scope(CcTest::isolate()); |
14952 // Test LoadIC. | 14965 // Test LoadIC. |
14953 for (int i = 0; i < 2; i++) { | 14966 for (int i = 0; i < 2; i++) { |
(...skipping 7935 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
22889 desc = x->GetOwnPropertyDescriptor(v8_str("p1")); | 22902 desc = x->GetOwnPropertyDescriptor(v8_str("p1")); |
22890 Local<Function> set = | 22903 Local<Function> set = |
22891 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("set"))); | 22904 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("set"))); |
22892 Local<Function> get = | 22905 Local<Function> get = |
22893 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("get"))); | 22906 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("get"))); |
22894 CHECK_EQ(v8_num(13), get->Call(x, 0, NULL)); | 22907 CHECK_EQ(v8_num(13), get->Call(x, 0, NULL)); |
22895 Handle<Value> args[] = { v8_num(14) }; | 22908 Handle<Value> args[] = { v8_num(14) }; |
22896 set->Call(x, 1, args); | 22909 set->Call(x, 1, args); |
22897 CHECK_EQ(v8_num(14), get->Call(x, 0, NULL)); | 22910 CHECK_EQ(v8_num(14), get->Call(x, 0, NULL)); |
22898 } | 22911 } |
OLD | NEW |