Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | namespace { |
| 16 | |
| 17 | V8TestEnvironment* g_environment = nullptr; |
| 18 | |
| 19 | } // namespace |
| 20 | |
| 21 | V8TestEnvironment::V8TestEnvironment(const char* exe_name) |
| 22 | : exe_path_(exe_name) { |
| 23 | ASSERT(!g_environment); |
| 24 | g_environment = this; |
| 25 | } |
| 26 | |
| 27 | V8TestEnvironment::~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 |
| 38 | V8TestEnvironment* V8TestEnvironment::GetInstance() { |
| 39 | return g_environment; |
| 40 | } |
| 41 | |
| 42 | // static |
| 43 | void V8TestEnvironment::PumpPlatformMessageLoop(v8::Isolate* isolate) { |
| 44 | v8::Platform* platform = GetInstance()->platform(); |
| 45 | while (v8::platform::PumpMessageLoop(platform, isolate)) |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | void V8TestEnvironment::SetUp() { |
| 50 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 51 | if (v8_snapshot_) { |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 52 | platform_ = InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(), |
| 53 | std::string(), nullptr); |
Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 54 | } else { |
| 55 | v8_snapshot_ = std::make_unique<v8::StartupData>(); |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 56 | platform_ = InitializeV8ForPDFiumWithStartupData( |
| 57 | exe_path_, std::string(), std::string(), v8_snapshot_.get()); |
Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 58 | } |
| 59 | #else |
| 60 | platform_ = InitializeV8ForPDFium(exe_path_); |
| 61 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 62 | } |
| 63 | |
| 64 | void V8TestEnvironment::TearDown() { |
| 65 | v8::V8::ShutdownPlatform(); |
| 66 | } |