test-utils.cc 4.12 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "test/unittests/test-utils.h"
6

7
#include "include/libplatform/libplatform.h"
8
#include "include/v8.h"
9
#include "src/api-inl.h"
10 11
#include "src/base/platform/time.h"
#include "src/flags.h"
12
#include "src/isolate.h"
13
#include "src/objects-inl.h"
14
#include "src/v8.h"
15 16 17

namespace v8 {

18
// static
19
v8::ArrayBuffer::Allocator* TestWithIsolate::array_buffer_allocator_ = nullptr;
20

21
// static
22
Isolate* TestWithIsolate::isolate_ = nullptr;
23 24 25 26

TestWithIsolate::TestWithIsolate()
    : isolate_scope_(isolate()), handle_scope_(isolate()) {}

27
TestWithIsolate::~TestWithIsolate() = default;
28 29 30 31

// static
void TestWithIsolate::SetUpTestCase() {
  Test::SetUpTestCase();
32
  EXPECT_EQ(nullptr, isolate_);
33
  v8::Isolate::CreateParams create_params;
34
  array_buffer_allocator_ = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
35 36
  create_params.array_buffer_allocator = array_buffer_allocator_;
  isolate_ = v8::Isolate::New(create_params);
37
  EXPECT_TRUE(isolate_ != nullptr);
38 39 40 41 42
}


// static
void TestWithIsolate::TearDownTestCase() {
43
  ASSERT_TRUE(isolate_ != nullptr);
44
  v8::Platform* platform = internal::V8::GetCurrentPlatform();
45
  ASSERT_TRUE(platform != nullptr);
46
  while (platform::PumpMessageLoop(platform, isolate_)) continue;
47
  isolate_->Dispose();
48
  isolate_ = nullptr;
49
  delete array_buffer_allocator_;
50 51 52
  Test::TearDownTestCase();
}

53 54 55 56 57 58 59 60 61
Local<Value> TestWithIsolate::RunJS(const char* source) {
  Local<Script> script =
      v8::Script::Compile(
          isolate()->GetCurrentContext(),
          v8::String::NewFromUtf8(isolate(), source, v8::NewStringType::kNormal)
              .ToLocalChecked())
          .ToLocalChecked();
  return script->Run(isolate()->GetCurrentContext()).ToLocalChecked();
}
62

63 64 65 66 67 68 69 70 71 72
Local<Value> TestWithIsolate::RunJS(
    String::ExternalOneByteStringResource* source) {
  Local<Script> script =
      v8::Script::Compile(
          isolate()->GetCurrentContext(),
          v8::String::NewExternalOneByte(isolate(), source).ToLocalChecked())
          .ToLocalChecked();
  return script->Run(isolate()->GetCurrentContext()).ToLocalChecked();
}

73 74 75
TestWithContext::TestWithContext()
    : context_(Context::New(isolate())), context_scope_(context_) {}

76
TestWithContext::~TestWithContext() = default;
77

78 79 80 81 82 83
v8::Local<v8::String> TestWithContext::NewString(const char* string) {
  return v8::String::NewFromUtf8(v8_isolate(), string,
                                 v8::NewStringType::kNormal)
      .ToLocalChecked();
}

84 85 86 87
void TestWithContext::SetGlobalProperty(const char* name,
                                        v8::Local<v8::Value> value) {
  CHECK(v8_context()
            ->Global()
88
            ->Set(v8_context(), NewString(name), value)
89 90 91
            .FromJust());
}

92 93
namespace internal {

94
TestWithIsolate::~TestWithIsolate() = default;
95

96
TestWithIsolateAndZone::~TestWithIsolateAndZone() = default;
97 98 99

Factory* TestWithIsolate::factory() const { return isolate()->factory(); }

Marja Hölttä's avatar
Marja Hölttä committed
100 101 102 103
Handle<Object> TestWithIsolate::RunJSInternal(const char* source) {
  return Utils::OpenHandle(*::v8::TestWithIsolate::RunJS(source));
}

104 105 106 107 108
Handle<Object> TestWithIsolate::RunJSInternal(
    ::v8::String::ExternalOneByteStringResource* source) {
  return Utils::OpenHandle(*::v8::TestWithIsolate::RunJS(source));
}

109 110 111 112
base::RandomNumberGenerator* TestWithIsolate::random_number_generator() const {
  return isolate()->random_number_generator();
}

113
TestWithZone::~TestWithZone() = default;
114

115
TestWithNativeContext::~TestWithNativeContext() = default;
116 117 118 119 120

Handle<Context> TestWithNativeContext::native_context() const {
  return isolate()->native_context();
}

121 122 123 124
SaveFlags::SaveFlags() { non_default_flags_ = FlagList::argv(); }

SaveFlags::~SaveFlags() {
  FlagList::ResetAllFlags();
125
  int argc = static_cast<int>(non_default_flags_->size());
126
  FlagList::SetFlagsFromCommandLine(
127
      &argc, const_cast<char**>(non_default_flags_->data()),
128 129 130 131 132 133 134 135
      false /* remove_flags */);
  for (auto flag = non_default_flags_->begin();
       flag != non_default_flags_->end(); ++flag) {
    delete[] * flag;
  }
  delete non_default_flags_;
}

136 137
}  // namespace internal
}  // namespace v8