Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "test/unittests/test-utils.h" |
| 6 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7 | #include "include/libplatform/libplatform.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8 | #include "src/base/platform/time.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9 | #include "src/debug/debug.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10 | #include "src/flags.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11 | #include "src/isolate.h" |
| 12 | #include "src/v8.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13 | |
| 14 | namespace v8 { |
| 15 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16 | class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
| 17 | public: |
| 18 | virtual void* Allocate(size_t length) { |
| 19 | void* data = AllocateUninitialized(length); |
| 20 | return data == NULL ? data : memset(data, 0, length); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 21 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 22 | virtual void* AllocateUninitialized(size_t length) { return malloc(length); } |
| 23 | virtual void Free(void* data, size_t) { free(data); } |
| 24 | }; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 25 | |
| 26 | |
| 27 | // static |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 28 | ArrayBufferAllocator* TestWithIsolate::array_buffer_allocator_ = NULL; |
| 29 | |
| 30 | // static |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 31 | Isolate* TestWithIsolate::isolate_ = NULL; |
| 32 | |
| 33 | |
| 34 | TestWithIsolate::TestWithIsolate() |
| 35 | : isolate_scope_(isolate()), handle_scope_(isolate()) {} |
| 36 | |
| 37 | |
| 38 | TestWithIsolate::~TestWithIsolate() {} |
| 39 | |
| 40 | |
| 41 | // static |
| 42 | void TestWithIsolate::SetUpTestCase() { |
| 43 | Test::SetUpTestCase(); |
| 44 | EXPECT_EQ(NULL, isolate_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 45 | v8::Isolate::CreateParams create_params; |
| 46 | array_buffer_allocator_ = new ArrayBufferAllocator; |
| 47 | create_params.array_buffer_allocator = array_buffer_allocator_; |
| 48 | isolate_ = v8::Isolate::New(create_params); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 49 | EXPECT_TRUE(isolate_ != NULL); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | // static |
| 54 | void TestWithIsolate::TearDownTestCase() { |
| 55 | ASSERT_TRUE(isolate_ != NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 56 | v8::Platform* platform = internal::V8::GetCurrentPlatform(); |
| 57 | ASSERT_TRUE(platform != NULL); |
| 58 | while (platform::PumpMessageLoop(platform, isolate_)) continue; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 59 | isolate_->Dispose(); |
| 60 | isolate_ = NULL; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 61 | delete array_buffer_allocator_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 62 | Test::TearDownTestCase(); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | TestWithContext::TestWithContext() |
| 67 | : context_(Context::New(isolate())), context_scope_(context_) {} |
| 68 | |
| 69 | |
| 70 | TestWithContext::~TestWithContext() {} |
| 71 | |
| 72 | |
| 73 | namespace base { |
| 74 | namespace { |
| 75 | |
| 76 | inline int64_t GetRandomSeedFromFlag(int random_seed) { |
| 77 | return random_seed ? random_seed : TimeTicks::Now().ToInternalValue(); |
| 78 | } |
| 79 | |
| 80 | } // namespace |
| 81 | |
| 82 | TestWithRandomNumberGenerator::TestWithRandomNumberGenerator() |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 83 | : rng_(GetRandomSeedFromFlag(::v8::internal::FLAG_random_seed)) {} |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 84 | |
| 85 | |
| 86 | TestWithRandomNumberGenerator::~TestWithRandomNumberGenerator() {} |
| 87 | |
| 88 | } // namespace base |
| 89 | |
| 90 | |
| 91 | namespace internal { |
| 92 | |
| 93 | TestWithIsolate::~TestWithIsolate() {} |
| 94 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 95 | TestWithIsolateAndZone::~TestWithIsolateAndZone() {} |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 96 | |
| 97 | Factory* TestWithIsolate::factory() const { return isolate()->factory(); } |
| 98 | |
| 99 | |
| 100 | base::RandomNumberGenerator* TestWithIsolate::random_number_generator() const { |
| 101 | return isolate()->random_number_generator(); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | TestWithZone::~TestWithZone() {} |
| 106 | |
| 107 | } // namespace internal |
| 108 | } // namespace v8 |