• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 #include "include/libplatform/libplatform.h"
8 #include "src/base/platform/time.h"
9 #include "src/debug/debug.h"
10 #include "src/flags.h"
11 #include "src/isolate.h"
12 #include "src/v8.h"
13 
14 namespace v8 {
15 
16 class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
17  public:
Allocate(size_t length)18   virtual void* Allocate(size_t length) {
19     void* data = AllocateUninitialized(length);
20     return data == NULL ? data : memset(data, 0, length);
21   }
AllocateUninitialized(size_t length)22   virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
Free(void * data,size_t)23   virtual void Free(void* data, size_t) { free(data); }
24 };
25 
26 
27 // static
28 ArrayBufferAllocator* TestWithIsolate::array_buffer_allocator_ = NULL;
29 
30 // static
31 Isolate* TestWithIsolate::isolate_ = NULL;
32 
33 
TestWithIsolate()34 TestWithIsolate::TestWithIsolate()
35     : isolate_scope_(isolate()), handle_scope_(isolate()) {}
36 
37 
~TestWithIsolate()38 TestWithIsolate::~TestWithIsolate() {}
39 
40 
41 // static
SetUpTestCase()42 void TestWithIsolate::SetUpTestCase() {
43   Test::SetUpTestCase();
44   EXPECT_EQ(NULL, isolate_);
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);
49   EXPECT_TRUE(isolate_ != NULL);
50 }
51 
52 
53 // static
TearDownTestCase()54 void TestWithIsolate::TearDownTestCase() {
55   ASSERT_TRUE(isolate_ != NULL);
56   v8::Platform* platform = internal::V8::GetCurrentPlatform();
57   ASSERT_TRUE(platform != NULL);
58   while (platform::PumpMessageLoop(platform, isolate_)) continue;
59   isolate_->Dispose();
60   isolate_ = NULL;
61   delete array_buffer_allocator_;
62   Test::TearDownTestCase();
63 }
64 
65 
TestWithContext()66 TestWithContext::TestWithContext()
67     : context_(Context::New(isolate())), context_scope_(context_) {}
68 
69 
~TestWithContext()70 TestWithContext::~TestWithContext() {}
71 
72 
73 namespace base {
74 namespace {
75 
GetRandomSeedFromFlag(int random_seed)76 inline int64_t GetRandomSeedFromFlag(int random_seed) {
77   return random_seed ? random_seed : TimeTicks::Now().ToInternalValue();
78 }
79 
80 }  // namespace
81 
TestWithRandomNumberGenerator()82 TestWithRandomNumberGenerator::TestWithRandomNumberGenerator()
83     : rng_(GetRandomSeedFromFlag(::v8::internal::FLAG_random_seed)) {}
84 
85 
~TestWithRandomNumberGenerator()86 TestWithRandomNumberGenerator::~TestWithRandomNumberGenerator() {}
87 
88 }  // namespace base
89 
90 
91 namespace internal {
92 
~TestWithIsolate()93 TestWithIsolate::~TestWithIsolate() {}
94 
~TestWithIsolateAndZone()95 TestWithIsolateAndZone::~TestWithIsolateAndZone() {}
96 
factory() const97 Factory* TestWithIsolate::factory() const { return isolate()->factory(); }
98 
99 
random_number_generator() const100 base::RandomNumberGenerator* TestWithIsolate::random_number_generator() const {
101   return isolate()->random_number_generator();
102 }
103 
104 
~TestWithZone()105 TestWithZone::~TestWithZone() {}
106 
107 }  // namespace internal
108 }  // namespace v8
109