Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2007-2010 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2010 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 883 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 894 Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked(); | 894 Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked(); |
| 895 | 895 |
| 896 CHECK_EQ(6 * 1000000, Handle<String>::cast(copy_result)->length()); | 896 CHECK_EQ(6 * 1000000, Handle<String>::cast(copy_result)->length()); |
| 897 CHECK(isolate->heap()->InSpace(HeapObject::cast(*copy_result), LO_SPACE)); | 897 CHECK(isolate->heap()->InSpace(HeapObject::cast(*copy_result), LO_SPACE)); |
| 898 | 898 |
| 899 delete cache; | 899 delete cache; |
| 900 source.Dispose(); | 900 source.Dispose(); |
| 901 } | 901 } |
| 902 | 902 |
| 903 | 903 |
| 904 class OneByteResource : public v8::String::ExternalOneByteStringResource { | |
| 905 public: | |
| 906 OneByteResource(const char* data, size_t length) | |
| 907 : data_(data), length_(length) {} | |
| 908 virtual const char* data() const { return data_; } | |
| 909 virtual size_t length() const { return length_; } | |
| 910 | |
| 911 private: | |
| 912 const char* data_; | |
| 913 size_t length_; | |
| 914 }; | |
| 915 | |
| 916 | |
| 917 class TwoByteResource : public v8::String::ExternalStringResource { | |
| 918 public: | |
| 919 TwoByteResource(const char* data, size_t length) | |
| 920 : data_(AsciiToTwoByteString(data)), length_(length) {} | |
| 921 ~TwoByteResource() { DeleteArray<const uint16_t>(data_); } | |
| 922 | |
| 923 virtual const uint16_t* data() const { return data_; } | |
| 924 virtual size_t length() const { return length_; } | |
| 925 | |
| 926 private: | |
| 927 const uint16_t* data_; | |
| 928 size_t length_; | |
| 929 }; | |
| 930 | |
| 931 | |
| 932 TEST(SerializeToplevelExternalString) { | |
| 933 FLAG_serialize_toplevel = true; | |
| 934 LocalContext context; | |
| 935 Isolate* isolate = CcTest::i_isolate(); | |
| 936 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. | |
| 937 | |
| 938 v8::HandleScope scope(CcTest::isolate()); | |
| 939 | |
| 940 // Obtain external internalized one-byte string. | |
| 941 OneByteResource one_byte_resource("one_byte", 8); | |
| 942 Handle<String> one_byte_string = | |
| 943 isolate->factory()->NewStringFromAsciiChecked("one_byte"); | |
| 944 one_byte_string = isolate->factory()->InternalizeString(one_byte_string); | |
| 945 one_byte_string->MakeExternal(&one_byte_resource); | |
| 946 CHECK(one_byte_string->IsExternalOneByteString()); | |
| 947 CHECK(one_byte_string->IsInternalizedString()); | |
| 948 | |
| 949 // Obtain external internalized two-byte string. | |
| 950 TwoByteResource two_byte_resource("two_byte", 8); | |
| 951 Handle<String> two_byte_string = | |
| 952 isolate->factory()->NewStringFromAsciiChecked("two_byte"); | |
| 953 two_byte_string = isolate->factory()->InternalizeString(two_byte_string); | |
| 954 two_byte_string->MakeExternal(&two_byte_resource); | |
|
mvstanton
2014/10/01 14:26:21
I like these tests. Nit: Is it possible to check t
| |
| 955 CHECK(two_byte_string->IsExternalTwoByteString()); | |
| 956 CHECK(two_byte_string->IsInternalizedString()); | |
| 957 | |
| 958 const char* source = | |
| 959 "var o = {} \n" | |
| 960 "o.one_byte = 7; \n" | |
| 961 "o.two_byte = 8; \n" | |
| 962 "o.one_byte + o.two_byte; \n"; | |
| 963 Handle<String> source_string = isolate->factory() | |
| 964 ->NewStringFromUtf8(CStrVector(source)) | |
| 965 .ToHandleChecked(); | |
| 966 | |
| 967 Handle<JSObject> global(isolate->context()->global_object()); | |
| 968 ScriptData* cache = NULL; | |
| 969 | |
| 970 Handle<SharedFunctionInfo> orig = Compiler::CompileScript( | |
| 971 source_string, Handle<String>(), 0, 0, false, | |
| 972 Handle<Context>(isolate->native_context()), NULL, &cache, | |
| 973 v8::ScriptCompiler::kProduceCodeCache, NOT_NATIVES_CODE); | |
| 974 | |
| 975 Handle<SharedFunctionInfo> copy; | |
| 976 { | |
| 977 DisallowCompilation no_compile_expected(isolate); | |
| 978 copy = Compiler::CompileScript( | |
| 979 source_string, Handle<String>(), 0, 0, false, | |
| 980 Handle<Context>(isolate->native_context()), NULL, &cache, | |
| 981 v8::ScriptCompiler::kConsumeCodeCache, NOT_NATIVES_CODE); | |
| 982 } | |
| 983 CHECK_NE(*orig, *copy); | |
| 984 | |
| 985 Handle<JSFunction> copy_fun = | |
| 986 isolate->factory()->NewFunctionFromSharedFunctionInfo( | |
| 987 copy, isolate->native_context()); | |
| 988 | |
| 989 Handle<Object> copy_result = | |
| 990 Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked(); | |
| 991 | |
| 992 CHECK_EQ(15.0f, copy_result->Number()); | |
| 993 | |
| 994 delete cache; | |
| 995 } | |
| 996 | |
| 997 | |
| 998 TEST(SerializeToplevelLargeExternalString) { | |
| 999 FLAG_serialize_toplevel = true; | |
| 1000 LocalContext context; | |
| 1001 Isolate* isolate = CcTest::i_isolate(); | |
| 1002 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. | |
| 1003 | |
| 1004 Factory* f = isolate->factory(); | |
| 1005 | |
| 1006 v8::HandleScope scope(CcTest::isolate()); | |
| 1007 | |
| 1008 // Create a huge external internalized string to use as variable name. | |
| 1009 Vector<const uint8_t> string = | |
| 1010 ConstructSource(STATIC_CHAR_VECTOR(""), STATIC_CHAR_VECTOR("abcdef"), | |
| 1011 STATIC_CHAR_VECTOR(""), 1000000); | |
| 1012 Handle<String> name = f->NewStringFromOneByte(string).ToHandleChecked(); | |
| 1013 OneByteResource one_byte_resource( | |
| 1014 reinterpret_cast<const char*>(string.start()), string.length()); | |
| 1015 name = f->InternalizeString(name); | |
| 1016 name->MakeExternal(&one_byte_resource); | |
| 1017 CHECK(name->IsExternalOneByteString()); | |
| 1018 CHECK(name->IsInternalizedString()); | |
| 1019 CHECK(isolate->heap()->InSpace(*name, LO_SPACE)); | |
| 1020 | |
| 1021 // Create the source, which is "var <literal> = 42; <literal>". | |
| 1022 Handle<String> source_str = | |
| 1023 f->NewConsString( | |
| 1024 f->NewConsString(f->NewStringFromAsciiChecked("var "), name) | |
| 1025 .ToHandleChecked(), | |
| 1026 f->NewConsString(f->NewStringFromAsciiChecked(" = 42; "), name) | |
| 1027 .ToHandleChecked()).ToHandleChecked(); | |
| 1028 | |
| 1029 Handle<JSObject> global(isolate->context()->global_object()); | |
| 1030 ScriptData* cache = NULL; | |
| 1031 | |
| 1032 Handle<SharedFunctionInfo> orig = Compiler::CompileScript( | |
| 1033 source_str, Handle<String>(), 0, 0, false, | |
| 1034 Handle<Context>(isolate->native_context()), NULL, &cache, | |
| 1035 v8::ScriptCompiler::kProduceCodeCache, NOT_NATIVES_CODE); | |
| 1036 | |
| 1037 Handle<SharedFunctionInfo> copy; | |
| 1038 { | |
| 1039 DisallowCompilation no_compile_expected(isolate); | |
| 1040 copy = Compiler::CompileScript( | |
| 1041 source_str, Handle<String>(), 0, 0, false, | |
| 1042 Handle<Context>(isolate->native_context()), NULL, &cache, | |
| 1043 v8::ScriptCompiler::kConsumeCodeCache, NOT_NATIVES_CODE); | |
| 1044 } | |
| 1045 CHECK_NE(*orig, *copy); | |
| 1046 | |
| 1047 Handle<JSFunction> copy_fun = | |
| 1048 isolate->factory()->NewFunctionFromSharedFunctionInfo( | |
| 1049 copy, isolate->native_context()); | |
| 1050 | |
| 1051 Handle<Object> copy_result = | |
| 1052 Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked(); | |
| 1053 | |
| 1054 CHECK_EQ(42.0f, copy_result->Number()); | |
| 1055 | |
| 1056 delete cache; | |
| 1057 string.Dispose(); | |
| 1058 } | |
| 1059 | |
| 1060 | |
| 904 TEST(SerializeToplevelIsolates) { | 1061 TEST(SerializeToplevelIsolates) { |
| 905 FLAG_serialize_toplevel = true; | 1062 FLAG_serialize_toplevel = true; |
| 906 | 1063 |
| 907 const char* source = "function f() { return 'abc'; }; f() + 'def'"; | 1064 const char* source = "function f() { return 'abc'; }; f() + 'def'"; |
| 908 v8::ScriptCompiler::CachedData* cache; | 1065 v8::ScriptCompiler::CachedData* cache; |
| 909 | 1066 |
| 910 v8::Isolate* isolate1 = v8::Isolate::New(); | 1067 v8::Isolate* isolate1 = v8::Isolate::New(); |
| 911 { | 1068 { |
| 912 v8::Isolate::Scope iscope(isolate1); | 1069 v8::Isolate::Scope iscope(isolate1); |
| 913 v8::HandleScope scope(isolate1); | 1070 v8::HandleScope scope(isolate1); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 945 { | 1102 { |
| 946 DisallowCompilation no_compile(reinterpret_cast<Isolate*>(isolate2)); | 1103 DisallowCompilation no_compile(reinterpret_cast<Isolate*>(isolate2)); |
| 947 script = v8::ScriptCompiler::CompileUnbound( | 1104 script = v8::ScriptCompiler::CompileUnbound( |
| 948 isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache); | 1105 isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache); |
| 949 } | 1106 } |
| 950 v8::Local<v8::Value> result = script->BindToCurrentContext()->Run(); | 1107 v8::Local<v8::Value> result = script->BindToCurrentContext()->Run(); |
| 951 CHECK(result->ToString()->Equals(v8_str("abcdef"))); | 1108 CHECK(result->ToString()->Equals(v8_str("abcdef"))); |
| 952 } | 1109 } |
| 953 isolate2->Dispose(); | 1110 isolate2->Dispose(); |
| 954 } | 1111 } |
| OLD | NEW |