blob: ad91d3db7b3d1053a783c776170299b326e9ca3b [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"
Lei Zhangbfcf1562022-07-07 01:49:26 +000014#include "v8/include/v8-isolate.h"
Tom Sepezeaf9d212020-07-22 16:24:00 +000015#include "v8/include/v8-platform.h"
Lei Zhangbfcf1562022-07-07 01:49:26 +000016#include "v8/include/v8-snapshot.h"
Tom Sepezeaf9d212020-07-22 16:24:00 +000017
18namespace {
19
20V8TestEnvironment* g_environment = nullptr;
21
22} // namespace
23
24V8TestEnvironment::V8TestEnvironment(const char* exe_name)
Tom Sepez72f520c2020-08-24 23:43:46 +000025 : exe_path_(exe_name),
26 array_buffer_allocator_(std::make_unique<CFX_V8ArrayBufferAllocator>()) {
Tom Sepez25f33d02021-01-29 01:58:51 +000027 DCHECK(!g_environment);
Tom Sepezeaf9d212020-07-22 16:24:00 +000028 g_environment = this;
29}
30
31V8TestEnvironment::~V8TestEnvironment() {
Tom Sepez25f33d02021-01-29 01:58:51 +000032 DCHECK(g_environment);
Tom Sepezeaf9d212020-07-22 16:24:00 +000033
34#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000035 if (startup_data_)
36 free(const_cast<char*>(startup_data_->data));
Tom Sepezeaf9d212020-07-22 16:24:00 +000037#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000038
39 g_environment = nullptr;
Tom Sepezeaf9d212020-07-22 16:24:00 +000040}
41
42// static
43V8TestEnvironment* V8TestEnvironment::GetInstance() {
44 return g_environment;
45}
46
47// static
48void V8TestEnvironment::PumpPlatformMessageLoop(v8::Isolate* isolate) {
49 v8::Platform* platform = GetInstance()->platform();
50 while (v8::platform::PumpMessageLoop(platform, isolate))
51 continue;
52}
53
54void V8TestEnvironment::SetUp() {
55#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000056 if (startup_data_) {
Tom Sepeze0d3c502020-08-11 23:51:10 +000057 platform_ = InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(),
58 std::string(), nullptr);
Tom Sepezeaf9d212020-07-22 16:24:00 +000059 } else {
Tom Sepez72f520c2020-08-24 23:43:46 +000060 startup_data_ = std::make_unique<v8::StartupData>();
Tom Sepeze0d3c502020-08-11 23:51:10 +000061 platform_ = InitializeV8ForPDFiumWithStartupData(
Tom Sepez72f520c2020-08-24 23:43:46 +000062 exe_path_, std::string(), std::string(), startup_data_.get());
Tom Sepezeaf9d212020-07-22 16:24:00 +000063 }
64#else
Tom Sepez54a3b3d2021-08-31 18:33:47 +000065 platform_ = InitializeV8ForPDFium(std::string(), exe_path_);
Tom Sepezeaf9d212020-07-22 16:24:00 +000066#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000067
68 v8::Isolate::CreateParams params;
69 params.array_buffer_allocator = array_buffer_allocator_.get();
70 isolate_.reset(v8::Isolate::New(params));
Tom Sepezeaf9d212020-07-22 16:24:00 +000071}
72
73void V8TestEnvironment::TearDown() {
Tom Sepez72f520c2020-08-24 23:43:46 +000074 isolate_.reset();
Tom Sepez30eddfa2021-02-16 19:24:00 +000075 ShutdownV8ForPDFium();
Tom Sepezeaf9d212020-07-22 16:24:00 +000076}