blob: 94203c2bb6b0a6f43be6cdfee85972355965e362 [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"
12#include "v8/include/libplatform/libplatform.h"
13#include "v8/include/v8-platform.h"
14#include "v8/include/v8.h"
15
16namespace {
17
18V8TestEnvironment* g_environment = nullptr;
19
20} // namespace
21
22V8TestEnvironment::V8TestEnvironment(const char* exe_name)
Tom Sepez72f520c2020-08-24 23:43:46 +000023 : exe_path_(exe_name),
24 array_buffer_allocator_(std::make_unique<CFX_V8ArrayBufferAllocator>()) {
Tom Sepezeaf9d212020-07-22 16:24:00 +000025 ASSERT(!g_environment);
26 g_environment = this;
27}
28
29V8TestEnvironment::~V8TestEnvironment() {
30 ASSERT(g_environment);
Tom Sepezeaf9d212020-07-22 16:24:00 +000031
32#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000033 if (startup_data_)
34 free(const_cast<char*>(startup_data_->data));
Tom Sepezeaf9d212020-07-22 16:24:00 +000035#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000036
37 g_environment = nullptr;
Tom Sepezeaf9d212020-07-22 16:24:00 +000038}
39
40// static
41V8TestEnvironment* V8TestEnvironment::GetInstance() {
42 return g_environment;
43}
44
45// static
46void V8TestEnvironment::PumpPlatformMessageLoop(v8::Isolate* isolate) {
47 v8::Platform* platform = GetInstance()->platform();
48 while (v8::platform::PumpMessageLoop(platform, isolate))
49 continue;
50}
51
52void V8TestEnvironment::SetUp() {
53#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000054 if (startup_data_) {
Tom Sepeze0d3c502020-08-11 23:51:10 +000055 platform_ = InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(),
56 std::string(), nullptr);
Tom Sepezeaf9d212020-07-22 16:24:00 +000057 } else {
Tom Sepez72f520c2020-08-24 23:43:46 +000058 startup_data_ = std::make_unique<v8::StartupData>();
Tom Sepeze0d3c502020-08-11 23:51:10 +000059 platform_ = InitializeV8ForPDFiumWithStartupData(
Tom Sepez72f520c2020-08-24 23:43:46 +000060 exe_path_, std::string(), std::string(), startup_data_.get());
Tom Sepezeaf9d212020-07-22 16:24:00 +000061 }
62#else
63 platform_ = InitializeV8ForPDFium(exe_path_);
64#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez72f520c2020-08-24 23:43:46 +000065
66 v8::Isolate::CreateParams params;
67 params.array_buffer_allocator = array_buffer_allocator_.get();
68 isolate_.reset(v8::Isolate::New(params));
Tom Sepezeaf9d212020-07-22 16:24:00 +000069}
70
71void V8TestEnvironment::TearDown() {
Tom Sepez72f520c2020-08-24 23:43:46 +000072 isolate_.reset();
Tom Sepezeaf9d212020-07-22 16:24:00 +000073 v8::V8::ShutdownPlatform();
74}