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 | |
Tom Sepez | 72f520c | 2020-08-24 23:43:46 +0000 | [diff] [blame^] | 7 | #include <memory> |
Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include "core/fxcrt/fx_system.h" |
| 11 | #include "testing/v8_initializer.h" |
| 12 | #include "v8/include/libplatform/libplatform.h" |
| 13 | #include "v8/include/v8-platform.h" |
| 14 | #include "v8/include/v8.h" |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | V8TestEnvironment* g_environment = nullptr; |
| 19 | |
| 20 | } // namespace |
| 21 | |
| 22 | V8TestEnvironment::V8TestEnvironment(const char* exe_name) |
Tom Sepez | 72f520c | 2020-08-24 23:43:46 +0000 | [diff] [blame^] | 23 | : exe_path_(exe_name), |
| 24 | array_buffer_allocator_(std::make_unique<CFX_V8ArrayBufferAllocator>()) { |
Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 25 | ASSERT(!g_environment); |
| 26 | g_environment = this; |
| 27 | } |
| 28 | |
| 29 | V8TestEnvironment::~V8TestEnvironment() { |
| 30 | ASSERT(g_environment); |
Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 31 | |
| 32 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 72f520c | 2020-08-24 23:43:46 +0000 | [diff] [blame^] | 33 | if (startup_data_) |
| 34 | free(const_cast<char*>(startup_data_->data)); |
Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 35 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 72f520c | 2020-08-24 23:43:46 +0000 | [diff] [blame^] | 36 | |
| 37 | g_environment = nullptr; |
Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // static |
| 41 | V8TestEnvironment* V8TestEnvironment::GetInstance() { |
| 42 | return g_environment; |
| 43 | } |
| 44 | |
| 45 | // static |
| 46 | void V8TestEnvironment::PumpPlatformMessageLoop(v8::Isolate* isolate) { |
| 47 | v8::Platform* platform = GetInstance()->platform(); |
| 48 | while (v8::platform::PumpMessageLoop(platform, isolate)) |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | void V8TestEnvironment::SetUp() { |
| 53 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 72f520c | 2020-08-24 23:43:46 +0000 | [diff] [blame^] | 54 | if (startup_data_) { |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 55 | platform_ = InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(), |
| 56 | std::string(), nullptr); |
Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 57 | } else { |
Tom Sepez | 72f520c | 2020-08-24 23:43:46 +0000 | [diff] [blame^] | 58 | startup_data_ = std::make_unique<v8::StartupData>(); |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 59 | platform_ = InitializeV8ForPDFiumWithStartupData( |
Tom Sepez | 72f520c | 2020-08-24 23:43:46 +0000 | [diff] [blame^] | 60 | exe_path_, std::string(), std::string(), startup_data_.get()); |
Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 61 | } |
| 62 | #else |
| 63 | platform_ = InitializeV8ForPDFium(exe_path_); |
| 64 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 72f520c | 2020-08-24 23:43:46 +0000 | [diff] [blame^] | 65 | |
| 66 | v8::Isolate::CreateParams params; |
| 67 | params.array_buffer_allocator = array_buffer_allocator_.get(); |
| 68 | isolate_.reset(v8::Isolate::New(params)); |
Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void V8TestEnvironment::TearDown() { |
Tom Sepez | 72f520c | 2020-08-24 23:43:46 +0000 | [diff] [blame^] | 72 | isolate_.reset(); |
Tom Sepez | eaf9d21 | 2020-07-22 16:24:00 +0000 | [diff] [blame] | 73 | v8::V8::ShutdownPlatform(); |
| 74 | } |