| OLD | NEW |
| 1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 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 10 matching lines...) Expand all Loading... |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #ifndef CCTEST_H_ | 28 #ifndef CCTEST_H_ |
| 29 #define CCTEST_H_ | 29 #define CCTEST_H_ |
| 30 | 30 |
| 31 #include <memory> |
| 32 |
| 31 #include "include/libplatform/libplatform.h" | 33 #include "include/libplatform/libplatform.h" |
| 32 #include "src/isolate-inl.h" // TODO(everyone): Make cctest IWYU. | 34 #include "include/v8-debug.h" |
| 33 #include "src/objects-inl.h" // TODO(everyone): Make cctest IWYU. | 35 #include "src/base/accounting-allocator.h" |
| 36 #include "src/utils.h" |
| 34 #include "src/v8.h" | 37 #include "src/v8.h" |
| 35 | 38 |
| 39 namespace v8 { |
| 40 namespace base { |
| 41 |
| 42 class RandomNumberGenerator; |
| 43 |
| 44 } // namespace base |
| 45 |
| 46 namespace internal { |
| 47 |
| 48 class HandleScope; |
| 49 class Zone; |
| 50 |
| 51 } // namespace internal |
| 52 |
| 53 } // namespace v8 |
| 54 |
| 36 #ifndef TEST | 55 #ifndef TEST |
| 37 #define TEST(Name) \ | 56 #define TEST(Name) \ |
| 38 static void Test##Name(); \ | 57 static void Test##Name(); \ |
| 39 CcTest register_test_##Name(Test##Name, __FILE__, #Name, true, true); \ | 58 CcTest register_test_##Name(Test##Name, __FILE__, #Name, true, true); \ |
| 40 static void Test##Name() | 59 static void Test##Name() |
| 41 #endif | 60 #endif |
| 42 | 61 |
| 43 #ifndef UNINITIALIZED_TEST | 62 #ifndef UNINITIALIZED_TEST |
| 44 #define UNINITIALIZED_TEST(Name) \ | 63 #define UNINITIALIZED_TEST(Name) \ |
| 45 static void Test##Name(); \ | 64 static void Test##Name(); \ |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 116 |
| 98 static i::Isolate* InitIsolateOnce() { | 117 static i::Isolate* InitIsolateOnce() { |
| 99 if (!initialize_called_) InitializeVM(); | 118 if (!initialize_called_) InitializeVM(); |
| 100 return i_isolate(); | 119 return i_isolate(); |
| 101 } | 120 } |
| 102 | 121 |
| 103 static i::Isolate* i_isolate() { | 122 static i::Isolate* i_isolate() { |
| 104 return reinterpret_cast<i::Isolate*>(isolate()); | 123 return reinterpret_cast<i::Isolate*>(isolate()); |
| 105 } | 124 } |
| 106 | 125 |
| 107 static i::Heap* heap() { | 126 static i::Heap* heap(); |
| 108 return i_isolate()->heap(); | |
| 109 } | |
| 110 | 127 |
| 111 static v8::base::RandomNumberGenerator* random_number_generator() { | 128 static v8::base::RandomNumberGenerator* random_number_generator(); |
| 112 return InitIsolateOnce()->random_number_generator(); | |
| 113 } | |
| 114 | 129 |
| 115 static v8::Local<v8::Object> global() { | 130 static v8::Local<v8::Object> global(); |
| 116 return isolate()->GetCurrentContext()->Global(); | |
| 117 } | |
| 118 | 131 |
| 119 static v8::ArrayBuffer::Allocator* array_buffer_allocator() { | 132 static v8::ArrayBuffer::Allocator* array_buffer_allocator() { |
| 120 return allocator_; | 133 return allocator_; |
| 121 } | 134 } |
| 122 | 135 |
| 123 static void set_array_buffer_allocator( | 136 static void set_array_buffer_allocator( |
| 124 v8::ArrayBuffer::Allocator* allocator) { | 137 v8::ArrayBuffer::Allocator* allocator) { |
| 125 allocator_ = allocator; | 138 allocator_ = allocator; |
| 126 } | 139 } |
| 127 | 140 |
| 128 // TODO(dcarney): Remove. | 141 // TODO(dcarney): Remove. |
| 129 // This must be called first in a test. | 142 // This must be called first in a test. |
| 130 static void InitializeVM() { | 143 static void InitializeVM(); |
| 131 CHECK(!v8::base::NoBarrier_Load(&isolate_used_)); | |
| 132 CHECK(!initialize_called_); | |
| 133 initialize_called_ = true; | |
| 134 v8::HandleScope handle_scope(CcTest::isolate()); | |
| 135 v8::Context::New(CcTest::isolate())->Enter(); | |
| 136 } | |
| 137 | 144 |
| 138 // Only for UNINITIALIZED_TESTs | 145 // Only for UNINITIALIZED_TESTs |
| 139 static void DisableAutomaticDispose(); | 146 static void DisableAutomaticDispose(); |
| 140 | 147 |
| 141 // Helper function to configure a context. | 148 // Helper function to configure a context. |
| 142 // Must be in a HandleScope. | 149 // Must be in a HandleScope. |
| 143 static v8::Local<v8::Context> NewContext( | 150 static v8::Local<v8::Context> NewContext( |
| 144 CcTestExtensionFlags extensions, | 151 CcTestExtensionFlags extensions, |
| 145 v8::Isolate* isolate = CcTest::isolate()); | 152 v8::Isolate* isolate = CcTest::isolate()); |
| 146 | 153 |
| 147 static void TearDown() { | 154 static void TearDown(); |
| 148 if (isolate_ != NULL) isolate_->Dispose(); | |
| 149 } | |
| 150 | 155 |
| 151 private: | 156 private: |
| 152 friend int main(int argc, char** argv); | 157 friend int main(int argc, char** argv); |
| 153 TestFunction* callback_; | 158 TestFunction* callback_; |
| 154 const char* file_; | 159 const char* file_; |
| 155 const char* name_; | 160 const char* name_; |
| 156 bool enabled_; | 161 bool enabled_; |
| 157 bool initialize_; | 162 bool initialize_; |
| 158 CcTest* prev_; | 163 CcTest* prev_; |
| 159 static CcTest* last_; | 164 static CcTest* last_; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 Initialize(isolate, extensions, global_template, global_object); | 267 Initialize(isolate, extensions, global_template, global_object); |
| 263 } | 268 } |
| 264 | 269 |
| 265 LocalContext(v8::ExtensionConfiguration* extensions = 0, | 270 LocalContext(v8::ExtensionConfiguration* extensions = 0, |
| 266 v8::Local<v8::ObjectTemplate> global_template = | 271 v8::Local<v8::ObjectTemplate> global_template = |
| 267 v8::Local<v8::ObjectTemplate>(), | 272 v8::Local<v8::ObjectTemplate>(), |
| 268 v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) { | 273 v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) { |
| 269 Initialize(CcTest::isolate(), extensions, global_template, global_object); | 274 Initialize(CcTest::isolate(), extensions, global_template, global_object); |
| 270 } | 275 } |
| 271 | 276 |
| 272 virtual ~LocalContext() { | 277 virtual ~LocalContext(); |
| 273 v8::HandleScope scope(isolate_); | |
| 274 v8::Local<v8::Context>::New(isolate_, context_)->Exit(); | |
| 275 context_.Reset(); | |
| 276 } | |
| 277 | 278 |
| 278 v8::Context* operator->() { | 279 v8::Context* operator->() { |
| 279 return *reinterpret_cast<v8::Context**>(&context_); | 280 return *reinterpret_cast<v8::Context**>(&context_); |
| 280 } | 281 } |
| 281 v8::Context* operator*() { return operator->(); } | 282 v8::Context* operator*() { return operator->(); } |
| 282 bool IsReady() { return !context_.IsEmpty(); } | 283 bool IsReady() { return !context_.IsEmpty(); } |
| 283 | 284 |
| 284 v8::Local<v8::Context> local() { | 285 v8::Local<v8::Context> local() { |
| 285 return v8::Local<v8::Context>::New(isolate_, context_); | 286 return v8::Local<v8::Context>::New(isolate_, context_); |
| 286 } | 287 } |
| 287 | 288 |
| 288 private: | 289 private: |
| 289 void Initialize(v8::Isolate* isolate, v8::ExtensionConfiguration* extensions, | 290 void Initialize(v8::Isolate* isolate, v8::ExtensionConfiguration* extensions, |
| 290 v8::Local<v8::ObjectTemplate> global_template, | 291 v8::Local<v8::ObjectTemplate> global_template, |
| 291 v8::Local<v8::Value> global_object) { | 292 v8::Local<v8::Value> global_object); |
| 292 v8::HandleScope scope(isolate); | |
| 293 v8::Local<v8::Context> context = v8::Context::New(isolate, | |
| 294 extensions, | |
| 295 global_template, | |
| 296 global_object); | |
| 297 context_.Reset(isolate, context); | |
| 298 context->Enter(); | |
| 299 // We can't do this later perhaps because of a fatal error. | |
| 300 isolate_ = isolate; | |
| 301 } | |
| 302 | 293 |
| 303 v8::Persistent<v8::Context> context_; | 294 v8::Persistent<v8::Context> context_; |
| 304 v8::Isolate* isolate_; | 295 v8::Isolate* isolate_; |
| 305 }; | 296 }; |
| 306 | 297 |
| 307 | 298 |
| 308 static inline uint16_t* AsciiToTwoByteString(const char* source) { | 299 static inline uint16_t* AsciiToTwoByteString(const char* source) { |
| 309 int array_length = i::StrLength(source) + 1; | 300 int array_length = i::StrLength(source) + 1; |
| 310 uint16_t* converted = i::NewArray<uint16_t>(array_length); | 301 uint16_t* converted = i::NewArray<uint16_t>(array_length); |
| 311 for (int i = 0; i < array_length; i++) converted[i] = source[i]; | 302 for (int i = 0; i < array_length; i++) converted[i] = source[i]; |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 v8::Debug::SetDebugEventListener(isolate, nullptr); | 551 v8::Debug::SetDebugEventListener(isolate, nullptr); |
| 561 } | 552 } |
| 562 | 553 |
| 563 | 554 |
| 564 static inline void EmptyMessageQueues(v8::Isolate* isolate) { | 555 static inline void EmptyMessageQueues(v8::Isolate* isolate) { |
| 565 while (v8::platform::PumpMessageLoop(v8::internal::V8::GetCurrentPlatform(), | 556 while (v8::platform::PumpMessageLoop(v8::internal::V8::GetCurrentPlatform(), |
| 566 isolate)) { | 557 isolate)) { |
| 567 } | 558 } |
| 568 } | 559 } |
| 569 | 560 |
| 561 class InitializedHandleScopeImpl; |
| 570 | 562 |
| 571 class InitializedHandleScope { | 563 class InitializedHandleScope { |
| 572 public: | 564 public: |
| 573 InitializedHandleScope() | 565 InitializedHandleScope(); |
| 574 : main_isolate_(CcTest::InitIsolateOnce()), | 566 ~InitializedHandleScope(); |
| 575 handle_scope_(main_isolate_) {} | |
| 576 | 567 |
| 577 // Prefixing the below with main_ reduces a lot of naming clashes. | 568 // Prefixing the below with main_ reduces a lot of naming clashes. |
| 578 i::Isolate* main_isolate() { return main_isolate_; } | 569 i::Isolate* main_isolate() { return main_isolate_; } |
| 579 | 570 |
| 580 private: | 571 private: |
| 581 i::Isolate* main_isolate_; | 572 i::Isolate* main_isolate_; |
| 582 i::HandleScope handle_scope_; | 573 std::unique_ptr<InitializedHandleScopeImpl> initialized_handle_scope_impl_; |
| 583 }; | 574 }; |
| 584 | 575 |
| 585 | 576 |
| 586 class HandleAndZoneScope : public InitializedHandleScope { | 577 class HandleAndZoneScope : public InitializedHandleScope { |
| 587 public: | 578 public: |
| 588 HandleAndZoneScope() : main_zone_(&allocator_) {} | 579 HandleAndZoneScope(); |
| 580 ~HandleAndZoneScope(); |
| 589 | 581 |
| 590 // Prefixing the below with main_ reduces a lot of naming clashes. | 582 // Prefixing the below with main_ reduces a lot of naming clashes. |
| 591 i::Zone* main_zone() { return &main_zone_; } | 583 i::Zone* main_zone() { return main_zone_.get(); } |
| 592 | 584 |
| 593 private: | 585 private: |
| 594 v8::base::AccountingAllocator allocator_; | 586 v8::base::AccountingAllocator allocator_; |
| 595 i::Zone main_zone_; | 587 std::unique_ptr<i::Zone> main_zone_; |
| 596 }; | 588 }; |
| 597 | 589 |
| 598 #endif // ifndef CCTEST_H_ | 590 #endif // ifndef CCTEST_H_ |
| OLD | NEW |