blob: 7d042151435234c3ee501bb079b401daa41fce39 [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// 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 Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "include/libplatform/libplatform.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include "src/base/platform/time.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009#include "src/debug/debug.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010#include "src/flags.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/isolate.h"
12#include "src/v8.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013
14namespace v8 {
15
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016class 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 Bernierd0a1eb72015-03-24 16:35:39 -040021 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022 virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
23 virtual void Free(void* data, size_t) { free(data); }
24};
Emily Bernierd0a1eb72015-03-24 16:35:39 -040025
26
27// static
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028ArrayBufferAllocator* TestWithIsolate::array_buffer_allocator_ = NULL;
29
30// static
Emily Bernierd0a1eb72015-03-24 16:35:39 -040031Isolate* TestWithIsolate::isolate_ = NULL;
32
33
34TestWithIsolate::TestWithIsolate()
35 : isolate_scope_(isolate()), handle_scope_(isolate()) {}
36
37
38TestWithIsolate::~TestWithIsolate() {}
39
40
41// static
42void TestWithIsolate::SetUpTestCase() {
43 Test::SetUpTestCase();
44 EXPECT_EQ(NULL, isolate_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045 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 Bernierd0a1eb72015-03-24 16:35:39 -040049 EXPECT_TRUE(isolate_ != NULL);
50}
51
52
53// static
54void TestWithIsolate::TearDownTestCase() {
55 ASSERT_TRUE(isolate_ != NULL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056 v8::Platform* platform = internal::V8::GetCurrentPlatform();
57 ASSERT_TRUE(platform != NULL);
58 while (platform::PumpMessageLoop(platform, isolate_)) continue;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040059 isolate_->Dispose();
60 isolate_ = NULL;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061 delete array_buffer_allocator_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040062 Test::TearDownTestCase();
63}
64
65
66TestWithContext::TestWithContext()
67 : context_(Context::New(isolate())), context_scope_(context_) {}
68
69
70TestWithContext::~TestWithContext() {}
71
72
73namespace base {
74namespace {
75
76inline int64_t GetRandomSeedFromFlag(int random_seed) {
77 return random_seed ? random_seed : TimeTicks::Now().ToInternalValue();
78}
79
80} // namespace
81
82TestWithRandomNumberGenerator::TestWithRandomNumberGenerator()
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000083 : rng_(GetRandomSeedFromFlag(::v8::internal::FLAG_random_seed)) {}
Emily Bernierd0a1eb72015-03-24 16:35:39 -040084
85
86TestWithRandomNumberGenerator::~TestWithRandomNumberGenerator() {}
87
88} // namespace base
89
90
91namespace internal {
92
93TestWithIsolate::~TestWithIsolate() {}
94
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000095TestWithIsolateAndZone::~TestWithIsolateAndZone() {}
Emily Bernierd0a1eb72015-03-24 16:35:39 -040096
97Factory* TestWithIsolate::factory() const { return isolate()->factory(); }
98
99
100base::RandomNumberGenerator* TestWithIsolate::random_number_generator() const {
101 return isolate()->random_number_generator();
102}
103
104
105TestWithZone::~TestWithZone() {}
106
107} // namespace internal
108} // namespace v8