blob: da8c7a8697333e4e629b49cee9f9cae04e864b1e [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
Tom Sepez72f520c2020-08-24 23:43:46 +00007#include <memory>
Tom Sepezeaf9d212020-07-22 16:24:00 +00008#include <string>
9
10#include "core/fxcrt/fx_system.h"
11#include "testing/v8_initializer.h"
Tom Sepez25f33d02021-01-29 01:58:51 +000012#include "third_party/base/check.h"
Tom Sepezeaf9d212020-07-22 16:24:00 +000013#include "v8/include/libplatform/libplatform.h"
14#include "v8/include/v8-platform.h"
15#include "v8/include/v8.h"
16
17namespace {
18
19V8TestEnvironment* g_environment = nullptr;
20
21} // namespace
22
23V8TestEnvironment::V8TestEnvironment(const char* exe_name)
Tom Sepez72f520c2020-08-24 23:43:46 +000024 : exe_path_(exe_name),
25 array_buffer_allocator_(std::make_unique<CFX_V8ArrayBufferAllocator>()) {
Tom Sepez25f33d02021-01-29 01:58:51 +000026 DCHECK(!g_environment);
Tom Sepezeaf9d212020-07-22 16:24:00 +000027 g_environment = this;
28}
29
30V8TestEnvironment::~V8TestEnvironment() {
Tom Sepez25f33d02021-01-29 01:58:51 +000031 DCHECK(g_environment);
Tom Sepezeaf9d212020-07-22 16:24:00 +000032
33#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000034 if (startup_data_)
35 free(const_cast<char*>(startup_data_->data));
Tom Sepezeaf9d212020-07-22 16:24:00 +000036#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000037
38 g_environment = nullptr;
Tom Sepezeaf9d212020-07-22 16:24:00 +000039}
40
41// static
42V8TestEnvironment* V8TestEnvironment::GetInstance() {
43 return g_environment;
44}
45
46// static
47void V8TestEnvironment::PumpPlatformMessageLoop(v8::Isolate* isolate) {
48 v8::Platform* platform = GetInstance()->platform();
49 while (v8::platform::PumpMessageLoop(platform, isolate))
50 continue;
51}
52
53void V8TestEnvironment::SetUp() {
54#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000055 if (startup_data_) {
Tom Sepeze0d3c502020-08-11 23:51:10 +000056 platform_ = InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(),
57 std::string(), nullptr);
Tom Sepezeaf9d212020-07-22 16:24:00 +000058 } else {
Tom Sepez72f520c2020-08-24 23:43:46 +000059 startup_data_ = std::make_unique<v8::StartupData>();
Tom Sepeze0d3c502020-08-11 23:51:10 +000060 platform_ = InitializeV8ForPDFiumWithStartupData(
Tom Sepez72f520c2020-08-24 23:43:46 +000061 exe_path_, std::string(), std::string(), startup_data_.get());
Tom Sepezeaf9d212020-07-22 16:24:00 +000062 }
63#else
Tom Sepez54a3b3d2021-08-31 18:33:47 +000064 platform_ = InitializeV8ForPDFium(std::string(), exe_path_);
Tom Sepezeaf9d212020-07-22 16:24:00 +000065#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000066
67 v8::Isolate::CreateParams params;
68 params.array_buffer_allocator = array_buffer_allocator_.get();
69 isolate_.reset(v8::Isolate::New(params));
Tom Sepezeaf9d212020-07-22 16:24:00 +000070}
71
72void V8TestEnvironment::TearDown() {
Tom Sepez72f520c2020-08-24 23:43:46 +000073 isolate_.reset();
Tom Sepez30eddfa2021-02-16 19:24:00 +000074 ShutdownV8ForPDFium();
Tom Sepezeaf9d212020-07-22 16:24:00 +000075}