cctest.h 22.9 KB
Newer Older
1
// Copyright 2008 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef CCTEST_H_
#define CCTEST_H_

31 32
#include <memory>

33
#include "include/libplatform/libplatform.h"
34
#include "include/v8-platform.h"
35
#include "src/debug/debug-interface.h"
36
#include "src/flags.h"
37
#include "src/heap/factory.h"
38
#include "src/isolate.h"
39
#include "src/objects.h"
40
#include "src/register-configuration.h"
41
#include "src/utils.h"
42
#include "src/v8.h"
43
#include "src/zone/accounting-allocator.h"
44

45 46 47 48 49 50 51 52 53
namespace v8 {
namespace base {

class RandomNumberGenerator;

}  // namespace base

namespace internal {

54 55 56 57 58 59 60
#if defined(V8_TARGET_ARCH_IA32) && defined(V8_EMBEDDED_BUILTINS)
// TODO(v8:6666): Fold into Default config once root is fully supported.
const auto GetRegConfig = RegisterConfiguration::PreserveRootIA32;
#else
const auto GetRegConfig = RegisterConfiguration::Default;
#endif

61 62 63 64 65 66 67
class HandleScope;
class Zone;

}  // namespace internal

}  // namespace v8

68
#ifndef TEST
69 70 71
#define TEST(Name)                                                      \
  static void Test##Name();                                             \
  CcTest register_test_##Name(Test##Name, __FILE__, #Name, true, true); \
72 73 74 75
  static void Test##Name()
#endif

#ifndef UNINITIALIZED_TEST
76 77 78
#define UNINITIALIZED_TEST(Name)                                         \
  static void Test##Name();                                              \
  CcTest register_test_##Name(Test##Name, __FILE__, #Name, true, false); \
79 80 81
  static void Test##Name()
#endif

82
#ifndef DISABLED_TEST
83 84 85
#define DISABLED_TEST(Name)                                              \
  static void Test##Name();                                              \
  CcTest register_test_##Name(Test##Name, __FILE__, #Name, false, true); \
86 87 88
  static void Test##Name()
#endif

89 90 91 92 93
#define EXTENSION_LIST(V)                                                      \
  V(GC_EXTENSION,       "v8/gc")                                               \
  V(PRINT_EXTENSION,    "v8/print")                                            \
  V(PROFILER_EXTENSION, "v8/profiler")                                         \
  V(TRACE_EXTENSION,    "v8/trace")
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

#define DEFINE_EXTENSION_ID(Name, Ident) Name##_ID,
enum CcTestExtensionIds {
  EXTENSION_LIST(DEFINE_EXTENSION_ID)
  kMaxExtensions
};
#undef DEFINE_EXTENSION_ID

typedef v8::internal::EnumSet<CcTestExtensionIds> CcTestExtensionFlags;
#define DEFINE_EXTENSION_FLAG(Name, Ident)                               \
  static const CcTestExtensionFlags Name(1 << Name##_ID);
  static const CcTestExtensionFlags NO_EXTENSIONS(0);
  static const CcTestExtensionFlags ALL_EXTENSIONS((1 << kMaxExtensions) - 1);
  EXTENSION_LIST(DEFINE_EXTENSION_FLAG)
#undef DEFINE_EXTENSION_FLAG

110

111 112 113
class CcTest {
 public:
  typedef void (TestFunction)();
114
  CcTest(TestFunction* callback, const char* file, const char* name,
115
         bool enabled, bool initialize);
116
  ~CcTest() { i::DeleteArray(file_); }
117
  void Run();
118
  static CcTest* last() { return last_; }
119 120 121
  CcTest* prev() { return prev_; }
  const char* file() { return file_; }
  const char* name() { return name_; }
122
  bool enabled() { return enabled_; }
123

124
  static v8::Isolate* isolate() {
125
    CHECK_NOT_NULL(isolate_);
126
    v8::base::Relaxed_Store(&isolate_used_, 1);
127
    return isolate_;
128
  }
129

130 131 132 133 134
  static i::Isolate* InitIsolateOnce() {
    if (!initialize_called_) InitializeVM();
    return i_isolate();
  }

135
  static i::Isolate* i_isolate() {
136
    return reinterpret_cast<i::Isolate*>(isolate());
137 138
  }

139
  static i::Heap* heap();
140

141
  static void CollectGarbage(i::AllocationSpace space);
142 143 144
  static void CollectAllGarbage(i::Isolate* isolate = nullptr);
  static void CollectAllAvailableGarbage(i::Isolate* isolate = nullptr);
  static void PreciseCollectAllGarbage(i::Isolate* isolate = nullptr);
145

146
  static v8::base::RandomNumberGenerator* random_number_generator();
147

148
  static v8::Local<v8::Object> global();
149

150 151 152 153 154 155 156 157 158
  static v8::ArrayBuffer::Allocator* array_buffer_allocator() {
    return allocator_;
  }

  static void set_array_buffer_allocator(
      v8::ArrayBuffer::Allocator* allocator) {
    allocator_ = allocator;
  }

159 160
  // TODO(dcarney): Remove.
  // This must be called first in a test.
161
  static void InitializeVM();
162

163 164 165
  // Only for UNINITIALIZED_TESTs
  static void DisableAutomaticDispose();

166 167 168 169 170
  // Helper function to configure a context.
  // Must be in a HandleScope.
  static v8::Local<v8::Context> NewContext(
      CcTestExtensionFlags extensions,
      v8::Isolate* isolate = CcTest::isolate());
171

172
  static void TearDown();
173

174
 private:
175
  friend int main(int argc, char** argv);
176 177 178
  TestFunction* callback_;
  const char* file_;
  const char* name_;
179
  bool enabled_;
180
  bool initialize_;
181
  CcTest* prev_;
182
  static CcTest* last_;
183
  static v8::ArrayBuffer::Allocator* allocator_;
184 185
  static v8::Isolate* isolate_;
  static bool initialize_called_;
186
  static v8::base::Atomic32 isolate_used_;
187 188
};

189 190 191 192 193 194 195 196 197 198
// Switches between all the Api tests using the threading support.
// In order to get a surprising but repeatable pattern of thread
// switching it has extra semaphores to control the order in which
// the tests alternate, not relying solely on the big V8 lock.
//
// A test is augmented with calls to ApiTestFuzzer::Fuzz() in its
// callbacks.  This will have no effect when we are not running the
// thread fuzzing test.  In the thread fuzzing test it will
// pseudorandomly select a successor thread and switch execution
// to that thread, suspending the current test.
199
class ApiTestFuzzer: public v8::base::Thread {
200 201 202 203
 public:
  void CallTest();

  // The ApiTestFuzzer is also a Thread, so it has a Run method.
204
  void Run() override;
205

206 207 208 209 210 211 212 213 214 215 216
  enum PartOfTest {
    FIRST_PART,
    SECOND_PART,
    THIRD_PART,
    FOURTH_PART,
    FIFTH_PART,
    SIXTH_PART,
    SEVENTH_PART,
    EIGHTH_PART,
    LAST_PART = EIGHTH_PART
  };
217

218
  static void SetUp(PartOfTest part);
219 220 221 222 223
  static void RunAllTests();
  static void TearDown();
  // This method switches threads if we are running the Threading test.
  // Otherwise it does nothing.
  static void Fuzz();
224

225
 private:
226
  explicit ApiTestFuzzer(int num)
227
      : Thread(Options("ApiTestFuzzer")),
228
        test_number_(num),
229
        gate_(0),
230
        active_(true) {}
231
  ~ApiTestFuzzer() override = default;
232

233 234 235 236 237 238
  static bool fuzzing_;
  static int tests_being_run_;
  static int current_;
  static int active_tests_;
  static bool NextThread();
  int test_number_;
239
  v8::base::Semaphore gate_;
240 241 242
  bool active_;
  void ContextSwitch();
  static int GetNextTestNumber();
243
  static v8::base::Semaphore all_tests_done_;
244 245 246 247 248 249 250 251 252 253 254 255
};


#define THREADED_TEST(Name)                                          \
  static void Test##Name();                                          \
  RegisterThreadedTest register_##Name(Test##Name, #Name);           \
  /* */ TEST(Name)

class RegisterThreadedTest {
 public:
  explicit RegisterThreadedTest(CcTest::TestFunction* callback,
                                const char* name)
256
      : fuzzer_(nullptr), callback_(callback), name_(name) {
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    prev_ = first_;
    first_ = this;
    count_++;
  }
  static int count() { return count_; }
  static RegisterThreadedTest* nth(int i) {
    CHECK(i < count());
    RegisterThreadedTest* current = first_;
    while (i > 0) {
      i--;
      current = current->prev_;
    }
    return current;
  }
  CcTest::TestFunction* callback() { return callback_; }
  ApiTestFuzzer* fuzzer_;
  const char* name() { return name_; }

 private:
  static RegisterThreadedTest* first_;
  static int count_;
  CcTest::TestFunction* callback_;
  RegisterThreadedTest* prev_;
  const char* name_;
};

// A LocalContext holds a reference to a v8::Context.
class LocalContext {
 public:
286 287
  LocalContext(v8::Isolate* isolate,
               v8::ExtensionConfiguration* extensions = nullptr,
288 289 290
               v8::Local<v8::ObjectTemplate> global_template =
                   v8::Local<v8::ObjectTemplate>(),
               v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) {
291 292 293
    Initialize(isolate, extensions, global_template, global_object);
  }

294
  LocalContext(v8::ExtensionConfiguration* extensions = nullptr,
295 296 297
               v8::Local<v8::ObjectTemplate> global_template =
                   v8::Local<v8::ObjectTemplate>(),
               v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) {
298
    Initialize(CcTest::isolate(), extensions, global_template, global_object);
299 300
  }

301
  virtual ~LocalContext();
302

303 304 305 306
  v8::Context* operator->() {
    return *reinterpret_cast<v8::Context**>(&context_);
  }
  v8::Context* operator*() { return operator->(); }
307 308 309
  bool IsReady() { return !context_.IsEmpty(); }

  v8::Local<v8::Context> local() {
310
    return v8::Local<v8::Context>::New(isolate_, context_);
311 312 313
  }

 private:
314 315
  void Initialize(v8::Isolate* isolate, v8::ExtensionConfiguration* extensions,
                  v8::Local<v8::ObjectTemplate> global_template,
316
                  v8::Local<v8::Value> global_object);
317

318
  v8::Persistent<v8::Context> context_;
319
  v8::Isolate* isolate_;
320 321
};

322 323 324 325 326 327 328 329

static inline uint16_t* AsciiToTwoByteString(const char* source) {
  int array_length = i::StrLength(source) + 1;
  uint16_t* converted = i::NewArray<uint16_t>(array_length);
  for (int i = 0; i < array_length; i++) converted[i] = source[i];
  return converted;
}

330 331 332 333 334 335 336
template <typename T>
static inline i::Handle<T> GetGlobal(const char* name) {
  i::Isolate* isolate = CcTest::i_isolate();
  i::Handle<i::String> str_name =
      isolate->factory()->InternalizeUtf8String(name);

  i::Handle<i::Object> value =
337
      i::Object::GetProperty(isolate, isolate->global_object(), str_name)
338 339 340
          .ToHandleChecked();
  return i::Handle<T>::cast(value);
}
341

342
static inline v8::Local<v8::Value> v8_num(double x) {
343
  return v8::Number::New(v8::Isolate::GetCurrent(), x);
344 345
}

346 347 348
static inline v8::Local<v8::Integer> v8_int(int32_t x) {
  return v8::Integer::New(v8::Isolate::GetCurrent(), x);
}
349 350

static inline v8::Local<v8::String> v8_str(const char* x) {
351 352 353
  return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x,
                                 v8::NewStringType::kNormal)
      .ToLocalChecked();
354 355 356
}


357 358 359 360 361 362 363
static inline v8::Local<v8::String> v8_str(v8::Isolate* isolate,
                                           const char* x) {
  return v8::String::NewFromUtf8(isolate, x, v8::NewStringType::kNormal)
      .ToLocalChecked();
}


364 365 366 367 368
static inline v8::Local<v8::Symbol> v8_symbol(const char* name) {
  return v8::Symbol::New(v8::Isolate::GetCurrent(), v8_str(name));
}


369 370 371 372 373 374 375
static inline v8::Local<v8::Script> v8_compile(v8::Local<v8::String> x) {
  v8::Local<v8::Script> result;
  if (v8::Script::Compile(v8::Isolate::GetCurrent()->GetCurrentContext(), x)
          .ToLocal(&result)) {
    return result;
  }
  return v8::Local<v8::Script>();
376 377 378
}


379 380
static inline v8::Local<v8::Script> v8_compile(const char* x) {
  return v8_compile(v8_str(x));
381 382 383
}


384 385 386 387 388 389
static inline int32_t v8_run_int32value(v8::Local<v8::Script> script) {
  v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
  return script->Run(context).ToLocalChecked()->Int32Value(context).FromJust();
}


390 391 392
static inline v8::Local<v8::Script> CompileWithOrigin(
    v8::Local<v8::String> source, v8::Local<v8::String> origin_url) {
  v8::ScriptOrigin origin(origin_url);
393
  v8::ScriptCompiler::Source script_source(source, origin);
394
  return v8::ScriptCompiler::Compile(
395 396
             v8::Isolate::GetCurrent()->GetCurrentContext(), &script_source)
      .ToLocalChecked();
397 398 399 400 401
}


static inline v8::Local<v8::Script> CompileWithOrigin(
    v8::Local<v8::String> source, const char* origin_url) {
402 403 404 405 406 407 408
  return CompileWithOrigin(source, v8_str(origin_url));
}


static inline v8::Local<v8::Script> CompileWithOrigin(const char* source,
                                                      const char* origin_url) {
  return CompileWithOrigin(v8_str(source), v8_str(origin_url));
409 410 411 412
}


// Helper functions that compile and run the source.
413 414 415 416 417 418 419 420
static inline v8::MaybeLocal<v8::Value> CompileRun(
    v8::Local<v8::Context> context, const char* source) {
  return v8::Script::Compile(context, v8_str(source))
      .ToLocalChecked()
      ->Run(context);
}


421 422 423 424 425 426 427 428 429 430 431 432
static inline v8::Local<v8::Value> CompileRunChecked(v8::Isolate* isolate,
                                                     const char* source) {
  v8::Local<v8::String> source_string =
      v8::String::NewFromUtf8(isolate, source, v8::NewStringType::kNormal)
          .ToLocalChecked();
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
  v8::Local<v8::Script> script =
      v8::Script::Compile(context, source_string).ToLocalChecked();
  return script->Run(context).ToLocalChecked();
}


433 434 435 436 437 438 439 440
static inline v8::Local<v8::Value> CompileRun(v8::Local<v8::String> source) {
  v8::Local<v8::Value> result;
  if (v8_compile(source)
          ->Run(v8::Isolate::GetCurrent()->GetCurrentContext())
          .ToLocal(&result)) {
    return result;
  }
  return v8::Local<v8::Value>();
441 442 443
}


444
// Helper functions that compile and run the source.
445 446
static inline v8::Local<v8::Value> CompileRun(const char* source) {
  return CompileRun(v8_str(source));
447 448 449
}


450 451 452 453 454 455 456 457 458 459 460
static inline v8::Local<v8::Value> CompileRun(
    v8::Local<v8::Context> context, v8::ScriptCompiler::Source* script_source,
    v8::ScriptCompiler::CompileOptions options) {
  v8::Local<v8::Value> result;
  if (v8::ScriptCompiler::Compile(context, script_source, options)
          .ToLocalChecked()
          ->Run(context)
          .ToLocal(&result)) {
    return result;
  }
  return v8::Local<v8::Value>();
461 462
}

463

464
// Helper functions that compile and run the source with given origin.
465 466 467 468
static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source,
                                                        const char* origin_url,
                                                        int line_number,
                                                        int column_number) {
469
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
470
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
471
  v8::ScriptOrigin origin(v8_str(origin_url),
472 473
                          v8::Integer::New(isolate, line_number),
                          v8::Integer::New(isolate, column_number));
474
  v8::ScriptCompiler::Source script_source(v8_str(source), origin);
475 476
  return CompileRun(context, &script_source,
                    v8::ScriptCompiler::CompileOptions());
477 478 479 480
}


static inline v8::Local<v8::Value> CompileRunWithOrigin(
481
    v8::Local<v8::String> source, const char* origin_url) {
482 483
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
484 485
  v8::ScriptCompiler::Source script_source(
      source, v8::ScriptOrigin(v8_str(origin_url)));
486 487
  return CompileRun(context, &script_source,
                    v8::ScriptCompiler::CompileOptions());
488 489 490 491 492 493
}


static inline v8::Local<v8::Value> CompileRunWithOrigin(
    const char* source, const char* origin_url) {
  return CompileRunWithOrigin(v8_str(source), origin_url);
494 495
}

496

497 498 499 500

static inline void ExpectString(const char* code, const char* expected) {
  v8::Local<v8::Value> result = CompileRun(code);
  CHECK(result->IsString());
501
  v8::String::Utf8Value utf8(v8::Isolate::GetCurrent(), result);
502
  CHECK_EQ(0, strcmp(expected, *utf8));
503 504 505 506 507 508
}


static inline void ExpectInt32(const char* code, int expected) {
  v8::Local<v8::Value> result = CompileRun(code);
  CHECK(result->IsInt32());
509 510 511
  CHECK_EQ(expected,
           result->Int32Value(v8::Isolate::GetCurrent()->GetCurrentContext())
               .FromJust());
512 513 514 515 516 517
}


static inline void ExpectBoolean(const char* code, bool expected) {
  v8::Local<v8::Value> result = CompileRun(code);
  CHECK(result->IsBoolean());
518
  CHECK_EQ(expected, result->BooleanValue(v8::Isolate::GetCurrent()));
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
}


static inline void ExpectTrue(const char* code) {
  ExpectBoolean(code, true);
}


static inline void ExpectFalse(const char* code) {
  ExpectBoolean(code, false);
}


static inline void ExpectObject(const char* code,
                                v8::Local<v8::Value> expected) {
  v8::Local<v8::Value> result = CompileRun(code);
  CHECK(result->SameValue(expected));
}


static inline void ExpectUndefined(const char* code) {
  v8::Local<v8::Value> result = CompileRun(code);
  CHECK(result->IsUndefined());
}


545 546 547 548 549 550
static inline void ExpectNull(const char* code) {
  v8::Local<v8::Value> result = CompileRun(code);
  CHECK(result->IsNull());
}


551 552 553 554 555 556
static inline void CheckDoubleEquals(double expected, double actual) {
  const double kEpsilon = 1e-10;
  CHECK_LE(expected, actual + kEpsilon);
  CHECK_GE(expected, actual - kEpsilon);
}

557
static v8::debug::DebugDelegate dummy_delegate;
558

559
static inline void EnableDebugger(v8::Isolate* isolate) {
560
  v8::debug::SetDebugDelegate(isolate, &dummy_delegate);
561 562 563
}


564
static inline void DisableDebugger(v8::Isolate* isolate) {
565
  v8::debug::SetDebugDelegate(isolate, nullptr);
566
}
567 568


569 570
static inline void EmptyMessageQueues(v8::Isolate* isolate) {
  while (v8::platform::PumpMessageLoop(v8::internal::V8::GetCurrentPlatform(),
571 572
                                       isolate)) {
  }
573 574
}

575
class InitializedHandleScopeImpl;
576

577 578
class InitializedHandleScope {
 public:
579 580
  InitializedHandleScope();
  ~InitializedHandleScope();
581 582 583 584 585 586

  // Prefixing the below with main_ reduces a lot of naming clashes.
  i::Isolate* main_isolate() { return main_isolate_; }

 private:
  i::Isolate* main_isolate_;
587
  std::unique_ptr<InitializedHandleScopeImpl> initialized_handle_scope_impl_;
588 589 590 591
};

class HandleAndZoneScope : public InitializedHandleScope {
 public:
592 593
  HandleAndZoneScope();
  ~HandleAndZoneScope();
594 595

  // Prefixing the below with main_ reduces a lot of naming clashes.
596
  i::Zone* main_zone() { return main_zone_.get(); }
597 598

 private:
599
  v8::internal::AccountingAllocator allocator_;
600
  std::unique_ptr<i::Zone> main_zone_;
601 602
};

603 604 605 606
class StaticOneByteResource : public v8::String::ExternalOneByteStringResource {
 public:
  explicit StaticOneByteResource(const char* data) : data_(data) {}

607
  ~StaticOneByteResource() override = default;
608

609
  const char* data() const override { return data_; }
610

611
  size_t length() const override { return strlen(data_); }
612 613 614 615 616

 private:
  const char* data_;
};

617 618 619 620 621
class ManualGCScope {
 public:
  ManualGCScope()
      : flag_concurrent_marking_(i::FLAG_concurrent_marking),
        flag_concurrent_sweeping_(i::FLAG_concurrent_sweeping),
622
        flag_stress_incremental_marking_(i::FLAG_stress_incremental_marking),
623 624 625
        flag_parallel_marking_(i::FLAG_parallel_marking),
        flag_detect_ineffective_gcs_near_heap_limit_(
            i::FLAG_detect_ineffective_gcs_near_heap_limit) {
626 627 628
    i::FLAG_concurrent_marking = false;
    i::FLAG_concurrent_sweeping = false;
    i::FLAG_stress_incremental_marking = false;
629 630
    // Parallel marking has a dependency on concurrent marking.
    i::FLAG_parallel_marking = false;
631
    i::FLAG_detect_ineffective_gcs_near_heap_limit = false;
632 633 634 635 636
  }
  ~ManualGCScope() {
    i::FLAG_concurrent_marking = flag_concurrent_marking_;
    i::FLAG_concurrent_sweeping = flag_concurrent_sweeping_;
    i::FLAG_stress_incremental_marking = flag_stress_incremental_marking_;
637
    i::FLAG_parallel_marking = flag_parallel_marking_;
638 639
    i::FLAG_detect_ineffective_gcs_near_heap_limit =
        flag_detect_ineffective_gcs_near_heap_limit_;
640 641 642 643 644 645
  }

 private:
  bool flag_concurrent_marking_;
  bool flag_concurrent_sweeping_;
  bool flag_stress_incremental_marking_;
646
  bool flag_parallel_marking_;
647
  bool flag_detect_ineffective_gcs_near_heap_limit_;
648 649
};

650 651 652 653 654 655
// This is an abstract base class that can be overridden to implement a test
// platform. It delegates all operations to a given platform at the time
// of construction.
class TestPlatform : public v8::Platform {
 public:
  // v8::Platform implementation.
656 657 658 659
  v8::PageAllocator* GetPageAllocator() override {
    return old_platform_->GetPageAllocator();
  }

660 661 662 663
  void OnCriticalMemoryPressure() override {
    old_platform_->OnCriticalMemoryPressure();
  }

664 665 666 667
  bool OnCriticalMemoryPressure(size_t length) override {
    return old_platform_->OnCriticalMemoryPressure(length);
  }

668 669 670 671
  int NumberOfWorkerThreads() override {
    return old_platform_->NumberOfWorkerThreads();
  }

672 673 674 675 676
  std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
      v8::Isolate* isolate) override {
    return old_platform_->GetForegroundTaskRunner(isolate);
  }

677 678
  void CallOnWorkerThread(std::unique_ptr<v8::Task> task) override {
    old_platform_->CallOnWorkerThread(std::move(task));
679 680
  }

681 682 683 684 685
  void CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task,
                                 double delay_in_seconds) override {
    old_platform_->CallDelayedOnWorkerThread(std::move(task), delay_in_seconds);
  }

686
  void CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) override {
687 688
    // This is a deprecated function and should not be called anymore.
    UNREACHABLE();
689 690 691 692
  }

  void CallDelayedOnForegroundThread(v8::Isolate* isolate, v8::Task* task,
                                     double delay_in_seconds) override {
693 694
    // This is a deprecated function and should not be called anymore.
    UNREACHABLE();
695 696 697 698 699 700
  }

  double MonotonicallyIncreasingTime() override {
    return old_platform_->MonotonicallyIncreasingTime();
  }

701 702 703 704
  double CurrentClockTimeMillis() override {
    return old_platform_->CurrentClockTimeMillis();
  }

705 706
  void CallIdleOnForegroundThread(v8::Isolate* isolate,
                                  v8::IdleTask* task) override {
707 708
    // This is a deprecated function and should not be called anymore.
    UNREACHABLE();
709 710 711 712 713 714 715 716 717 718 719 720
  }

  bool IdleTasksEnabled(v8::Isolate* isolate) override {
    return old_platform_->IdleTasksEnabled(isolate);
  }

  v8::TracingController* GetTracingController() override {
    return old_platform_->GetTracingController();
  }

 protected:
  TestPlatform() : old_platform_(i::V8::GetCurrentPlatform()) {}
721
  ~TestPlatform() override { i::V8::SetPlatformForTesting(old_platform_); }
722 723 724 725 726 727 728 729 730

  v8::Platform* old_platform() const { return old_platform_; }

 private:
  v8::Platform* old_platform_;

  DISALLOW_COPY_AND_ASSIGN(TestPlatform);
};

731
#endif  // ifndef CCTEST_H_