blob: 08a8840d172fade819131465098592cf3ea7ad7d [file] [log] [blame]
Tom Sepezeaf9d212020-07-22 16:24:00 +00001// Copyright 2020 PDFium 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 "testing/v8_test_environment.h"
6
7#include <string>
8
9#include "core/fxcrt/fx_system.h"
10#include "testing/v8_initializer.h"
11#include "v8/include/libplatform/libplatform.h"
12#include "v8/include/v8-platform.h"
13#include "v8/include/v8.h"
14
15namespace {
16
17V8TestEnvironment* g_environment = nullptr;
18
19} // namespace
20
21V8TestEnvironment::V8TestEnvironment(const char* exe_name)
22 : exe_path_(exe_name) {
23 ASSERT(!g_environment);
24 g_environment = this;
25}
26
27V8TestEnvironment::~V8TestEnvironment() {
28 ASSERT(g_environment);
29 g_environment = nullptr;
30
31#ifdef V8_USE_EXTERNAL_STARTUP_DATA
32 if (v8_snapshot_)
33 free(const_cast<char*>(v8_snapshot_->data));
34#endif // V8_USE_EXTERNAL_STARTUP_DATA
35}
36
37// static
38V8TestEnvironment* V8TestEnvironment::GetInstance() {
39 return g_environment;
40}
41
42// static
43void V8TestEnvironment::PumpPlatformMessageLoop(v8::Isolate* isolate) {
44 v8::Platform* platform = GetInstance()->platform();
45 while (v8::platform::PumpMessageLoop(platform, isolate))
46 continue;
47}
48
49void V8TestEnvironment::SetUp() {
50#ifdef V8_USE_EXTERNAL_STARTUP_DATA
51 if (v8_snapshot_) {
52 platform_ =
53 InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(), nullptr);
54 } else {
55 v8_snapshot_ = std::make_unique<v8::StartupData>();
56 platform_ = InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(),
57 v8_snapshot_.get());
58 }
59#else
60 platform_ = InitializeV8ForPDFium(exe_path_);
61#endif // V8_USE_EXTERNAL_STARTUP_DATA
62}
63
64void V8TestEnvironment::TearDown() {
65 v8::V8::ShutdownPlatform();
66}