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 14768 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
14779 | 14779 |
14780 // This test verifies that pre-compilation (aka preparsing) can be called | 14780 // This test verifies that pre-compilation (aka preparsing) can be called |
14781 // without initializing the whole VM. Thus we cannot run this test in a | 14781 // without initializing the whole VM. Thus we cannot run this test in a |
14782 // multi-threaded setup. | 14782 // multi-threaded setup. |
14783 TEST(PreCompile) { | 14783 TEST(PreCompile) { |
14784 // TODO(155): This test would break without the initialization of V8. This is | 14784 // TODO(155): This test would break without the initialization of V8. This is |
14785 // a workaround for now to make this test not fail. | 14785 // a workaround for now to make this test not fail. |
14786 v8::V8::Initialize(); | 14786 v8::V8::Initialize(); |
14787 v8::Isolate* isolate = CcTest::isolate(); | 14787 v8::Isolate* isolate = CcTest::isolate(); |
14788 const char* script = "function foo(a) { return a+1; }"; | 14788 const char* script = "function foo(a) { return a+1; }"; |
14789 v8::ScriptData* sd = | 14789 v8::ScriptData* sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( |
14790 v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); | 14790 isolate, script, v8::String::kNormalString, i::StrLength(script))); |
14791 CHECK_NE(sd->Length(), 0); | 14791 CHECK_NE(sd->Length(), 0); |
14792 CHECK_NE(sd->Data(), NULL); | 14792 CHECK_NE(sd->Data(), NULL); |
14793 CHECK(!sd->HasError()); | 14793 CHECK(!sd->HasError()); |
14794 delete sd; | 14794 delete sd; |
14795 } | 14795 } |
14796 | 14796 |
14797 | 14797 |
14798 TEST(PreCompileWithError) { | 14798 TEST(PreCompileWithError) { |
14799 v8::V8::Initialize(); | 14799 v8::V8::Initialize(); |
14800 v8::Isolate* isolate = CcTest::isolate(); | 14800 v8::Isolate* isolate = CcTest::isolate(); |
14801 const char* script = "function foo(a) { return 1 * * 2; }"; | 14801 const char* script = "function foo(a) { return 1 * * 2; }"; |
14802 v8::ScriptData* sd = | 14802 v8::ScriptData* sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( |
14803 v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); | 14803 isolate, script, v8::String::kNormalString, i::StrLength(script))); |
14804 CHECK(sd->HasError()); | 14804 CHECK(sd->HasError()); |
14805 delete sd; | 14805 delete sd; |
14806 } | 14806 } |
14807 | 14807 |
14808 | 14808 |
14809 TEST(Regress31661) { | 14809 TEST(Regress31661) { |
14810 v8::V8::Initialize(); | 14810 v8::V8::Initialize(); |
14811 v8::Isolate* isolate = CcTest::isolate(); | 14811 v8::Isolate* isolate = CcTest::isolate(); |
14812 const char* script = " The Definintive Guide"; | 14812 const char* script = " The Definintive Guide"; |
14813 v8::ScriptData* sd = | 14813 v8::ScriptData* sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( |
14814 v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); | 14814 isolate, script, v8::String::kNormalString, i::StrLength(script))); |
14815 CHECK(sd->HasError()); | 14815 CHECK(sd->HasError()); |
14816 delete sd; | 14816 delete sd; |
14817 } | 14817 } |
14818 | 14818 |
14819 | 14819 |
14820 // Tests that ScriptData can be serialized and deserialized. | 14820 // Tests that ScriptData can be serialized and deserialized. |
14821 TEST(PreCompileSerialization) { | 14821 TEST(PreCompileSerialization) { |
14822 v8::V8::Initialize(); | 14822 v8::V8::Initialize(); |
14823 v8::Isolate* isolate = CcTest::isolate(); | 14823 v8::Isolate* isolate = CcTest::isolate(); |
14824 const char* script = "function foo(a) { return a+1; }"; | 14824 const char* script = "function foo(a) { return a+1; }"; |
14825 v8::ScriptData* sd = | 14825 v8::ScriptData* sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( |
14826 v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); | 14826 isolate, script, v8::String::kNormalString, i::StrLength(script))); |
14827 | 14827 |
14828 // Serialize. | 14828 // Serialize. |
14829 int serialized_data_length = sd->Length(); | 14829 int serialized_data_length = sd->Length(); |
14830 char* serialized_data = i::NewArray<char>(serialized_data_length); | 14830 char* serialized_data = i::NewArray<char>(serialized_data_length); |
14831 i::OS::MemCopy(serialized_data, sd->Data(), serialized_data_length); | 14831 i::OS::MemCopy(serialized_data, sd->Data(), serialized_data_length); |
14832 | 14832 |
14833 // Deserialize. | 14833 // Deserialize. |
14834 v8::ScriptData* deserialized_sd = | 14834 v8::ScriptData* deserialized_sd = |
14835 v8::ScriptData::New(serialized_data, serialized_data_length); | 14835 v8::ScriptData::New(serialized_data, serialized_data_length); |
14836 | 14836 |
(...skipping 22 matching lines...) Expand all Loading... |
14859 | 14859 |
14860 // Attempts to deserialize bad data. | 14860 // Attempts to deserialize bad data. |
14861 TEST(PreCompileInvalidPreparseDataError) { | 14861 TEST(PreCompileInvalidPreparseDataError) { |
14862 v8::V8::Initialize(); | 14862 v8::V8::Initialize(); |
14863 v8::Isolate* isolate = CcTest::isolate(); | 14863 v8::Isolate* isolate = CcTest::isolate(); |
14864 LocalContext context; | 14864 LocalContext context; |
14865 v8::HandleScope scope(context->GetIsolate()); | 14865 v8::HandleScope scope(context->GetIsolate()); |
14866 | 14866 |
14867 const char* script = "function foo(){ return 5;}\n" | 14867 const char* script = "function foo(){ return 5;}\n" |
14868 "function bar(){ return 6 + 7;} foo();"; | 14868 "function bar(){ return 6 + 7;} foo();"; |
14869 v8::ScriptData* sd = | 14869 v8::ScriptData* sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( |
14870 v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); | 14870 isolate, script, v8::String::kNormalString, i::StrLength(script))); |
14871 CHECK(!sd->HasError()); | 14871 CHECK(!sd->HasError()); |
14872 // ScriptDataImpl private implementation details | 14872 // ScriptDataImpl private implementation details |
14873 const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; | 14873 const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; |
14874 const int kFunctionEntrySize = i::FunctionEntry::kSize; | 14874 const int kFunctionEntrySize = i::FunctionEntry::kSize; |
14875 const int kFunctionEntryStartOffset = 0; | 14875 const int kFunctionEntryStartOffset = 0; |
14876 const int kFunctionEntryEndOffset = 1; | 14876 const int kFunctionEntryEndOffset = 1; |
14877 unsigned* sd_data = | 14877 unsigned* sd_data = |
14878 reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | 14878 reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); |
14879 | 14879 |
14880 // Overwrite function bar's end position with 0. | 14880 // Overwrite function bar's end position with 0. |
14881 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; | 14881 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; |
14882 v8::TryCatch try_catch; | 14882 v8::TryCatch try_catch; |
14883 | 14883 |
14884 Local<String> source = String::NewFromUtf8(isolate, script); | 14884 Local<String> source = String::NewFromUtf8(isolate, script); |
14885 Local<Script> compiled_script = Script::New(source, NULL, sd); | 14885 Local<Script> compiled_script = Script::New(source, NULL, sd); |
14886 CHECK(try_catch.HasCaught()); | 14886 CHECK(try_catch.HasCaught()); |
14887 String::Utf8Value exception_value(try_catch.Message()->Get()); | 14887 String::Utf8Value exception_value(try_catch.Message()->Get()); |
14888 CHECK_EQ("Uncaught SyntaxError: Invalid preparser data for function bar", | 14888 CHECK_EQ("Uncaught SyntaxError: Invalid preparser data for function bar", |
14889 *exception_value); | 14889 *exception_value); |
14890 | 14890 |
14891 try_catch.Reset(); | 14891 try_catch.Reset(); |
14892 | 14892 |
14893 // Overwrite function bar's start position with 200. The function entry | 14893 // Overwrite function bar's start position with 200. The function entry |
14894 // will not be found when searching for it by position and we should fall | 14894 // will not be found when searching for it by position and we should fall |
14895 // back on eager compilation. | 14895 // back on eager compilation. |
14896 sd = v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); | 14896 sd = v8::ScriptData::PreCompile(v8::String::NewFromUtf8( |
| 14897 isolate, script, v8::String::kNormalString, i::StrLength(script))); |
14897 sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); | 14898 sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); |
14898 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = | 14899 sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = |
14899 200; | 14900 200; |
14900 compiled_script = Script::New(source, NULL, sd); | 14901 compiled_script = Script::New(source, NULL, sd); |
14901 CHECK(!try_catch.HasCaught()); | 14902 CHECK(!try_catch.HasCaught()); |
14902 | 14903 |
14903 delete sd; | 14904 delete sd; |
14904 } | 14905 } |
14905 | 14906 |
14906 | 14907 |
14907 // Verifies that the Handle<String> and const char* versions of the API produce | |
14908 // the same results (at least for one trivial case). | |
14909 TEST(PreCompileAPIVariationsAreSame) { | |
14910 v8::V8::Initialize(); | |
14911 v8::Isolate* isolate = CcTest::isolate(); | |
14912 v8::HandleScope scope(isolate); | |
14913 | |
14914 const char* cstring = "function foo(a) { return a+1; }"; | |
14915 | |
14916 v8::ScriptData* sd_from_cstring = | |
14917 v8::ScriptData::PreCompile(isolate, cstring, i::StrLength(cstring)); | |
14918 | |
14919 TestAsciiResource* resource = new TestAsciiResource(cstring); | |
14920 v8::ScriptData* sd_from_external_string = v8::ScriptData::PreCompile( | |
14921 v8::String::NewExternal(isolate, resource)); | |
14922 | |
14923 v8::ScriptData* sd_from_string = v8::ScriptData::PreCompile( | |
14924 v8::String::NewFromUtf8(isolate, cstring)); | |
14925 | |
14926 CHECK_EQ(sd_from_cstring->Length(), sd_from_external_string->Length()); | |
14927 CHECK_EQ(0, memcmp(sd_from_cstring->Data(), | |
14928 sd_from_external_string->Data(), | |
14929 sd_from_cstring->Length())); | |
14930 | |
14931 CHECK_EQ(sd_from_cstring->Length(), sd_from_string->Length()); | |
14932 CHECK_EQ(0, memcmp(sd_from_cstring->Data(), | |
14933 sd_from_string->Data(), | |
14934 sd_from_cstring->Length())); | |
14935 | |
14936 | |
14937 delete sd_from_cstring; | |
14938 delete sd_from_external_string; | |
14939 delete sd_from_string; | |
14940 } | |
14941 | |
14942 | |
14943 // This tests that we do not allow dictionary load/call inline caches | 14908 // This tests that we do not allow dictionary load/call inline caches |
14944 // to use functions that have not yet been compiled. The potential | 14909 // to use functions that have not yet been compiled. The potential |
14945 // problem of loading a function that has not yet been compiled can | 14910 // problem of loading a function that has not yet been compiled can |
14946 // arise because we share code between contexts via the compilation | 14911 // arise because we share code between contexts via the compilation |
14947 // cache. | 14912 // cache. |
14948 THREADED_TEST(DictionaryICLoadedFunction) { | 14913 THREADED_TEST(DictionaryICLoadedFunction) { |
14949 v8::HandleScope scope(CcTest::isolate()); | 14914 v8::HandleScope scope(CcTest::isolate()); |
14950 // Test LoadIC. | 14915 // Test LoadIC. |
14951 for (int i = 0; i < 2; i++) { | 14916 for (int i = 0; i < 2; i++) { |
14952 LocalContext context; | 14917 LocalContext context; |
(...skipping 6698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
21651 } | 21616 } |
21652 for (int i = 0; i < runs; i++) { | 21617 for (int i = 0; i < runs; i++) { |
21653 Local<String> expected; | 21618 Local<String> expected; |
21654 if (i != 0) { | 21619 if (i != 0) { |
21655 CHECK_EQ(v8_str("escape value"), values[i]); | 21620 CHECK_EQ(v8_str("escape value"), values[i]); |
21656 } else { | 21621 } else { |
21657 CHECK(values[i].IsEmpty()); | 21622 CHECK(values[i].IsEmpty()); |
21658 } | 21623 } |
21659 } | 21624 } |
21660 } | 21625 } |
OLD | NEW |