Fix cctest + unittest to work with an external snapshot.

To do so, extract startup_data_util from d8 and use it those executables.

BUG=

Review URL: https://codereview.chromium.org/913703002

Cr-Commit-Position: refs/heads/master@{#26547}
This commit is contained in:
vogelheim 2015-02-10 07:38:01 -08:00 committed by Commit bot
parent 5d68529be2
commit 2ea8df76ba
9 changed files with 171 additions and 87 deletions

View File

@ -1525,6 +1525,8 @@ if ((current_toolchain == host_toolchain && v8_toolset_for_d8 == "host") ||
sources = [
"src/d8.cc",
"src/d8.h",
"src/startup-data-util.h",
"src/startup-data-util.cc",
]
configs -= [ "//build/config/compiler:chromium_code" ]

View File

@ -49,6 +49,10 @@
#include "src/v8.h"
#endif // !V8_SHARED
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
#include "src/startup-data-util.h"
#endif // V8_USE_EXTERNAL_STARTUP_DATA
#if !defined(_WIN32) && !defined(_WIN64)
#include <unistd.h> // NOLINT
#else
@ -1545,90 +1549,6 @@ class MockArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
};
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
class StartupDataHandler {
public:
StartupDataHandler(const char* exec_path, const char* natives_blob,
const char* snapshot_blob) {
// If we have (at least one) explicitly given blob, use those.
// If not, use the default blob locations next to the d8 binary.
if (natives_blob || snapshot_blob) {
LoadFromFiles(natives_blob, snapshot_blob);
} else {
char* natives;
char* snapshot;
LoadFromFiles(RelativePath(&natives, exec_path, "natives_blob.bin"),
RelativePath(&snapshot, exec_path, "snapshot_blob.bin"));
free(natives);
free(snapshot);
}
}
~StartupDataHandler() {
delete[] natives_.data;
delete[] snapshot_.data;
}
private:
static char* RelativePath(char** buffer, const char* exec_path,
const char* name) {
DCHECK(exec_path);
const char* last_slash = strrchr(exec_path, '/');
if (last_slash) {
int after_slash = last_slash - exec_path + 1;
int name_length = static_cast<int>(strlen(name));
*buffer =
reinterpret_cast<char*>(calloc(after_slash + name_length + 1, 1));
strncpy(*buffer, exec_path, after_slash);
strncat(*buffer, name, name_length);
} else {
*buffer = strdup(name);
}
return *buffer;
}
void LoadFromFiles(const char* natives_blob, const char* snapshot_blob) {
Load(natives_blob, &natives_, v8::V8::SetNativesDataBlob);
Load(snapshot_blob, &snapshot_, v8::V8::SetSnapshotDataBlob);
}
void Load(const char* blob_file,
v8::StartupData* startup_data,
void (*setter_fn)(v8::StartupData*)) {
startup_data->data = NULL;
startup_data->raw_size = 0;
if (!blob_file)
return;
FILE* file = fopen(blob_file, "rb");
if (!file)
return;
fseek(file, 0, SEEK_END);
startup_data->raw_size = ftell(file);
rewind(file);
startup_data->data = new char[startup_data->raw_size];
int read_size =
static_cast<int>(fread(const_cast<char*>(startup_data->data), 1,
startup_data->raw_size, file));
fclose(file);
if (startup_data->raw_size == read_size) (*setter_fn)(startup_data);
}
v8::StartupData natives_;
v8::StartupData snapshot_;
// Disallow copy & assign.
StartupDataHandler(const StartupDataHandler& other);
void operator=(const StartupDataHandler& other);
};
#endif // V8_USE_EXTERNAL_STARTUP_DATA
int Shell::Main(int argc, char* argv[]) {
#if (defined(_WIN32) || defined(_WIN64))
UINT new_flags =
@ -1651,8 +1571,8 @@ int Shell::Main(int argc, char* argv[]) {
v8::V8::InitializePlatform(platform);
v8::V8::Initialize();
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
StartupDataHandler startup_data(argv[0], options.natives_blob,
options.snapshot_blob);
v8::StartupDataHandler startup_data(argv[0], options.natives_blob,
options.snapshot_blob);
#endif
SetFlagsFromString("--trace-hydrogen-file=hydrogen.cfg");
SetFlagsFromString("--trace-turbo-cfg-file=turbo.cfg");

View File

@ -49,6 +49,8 @@
],
'sources': [
'd8.cc',
'startup-data-util.h',
'startup-data-util.cc'
],
'conditions': [
[ 'want_separate_host_toolset==1', {

91
src/startup-data-util.cc Normal file
View File

@ -0,0 +1,91 @@
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/startup-data-util.h"
#include <stdlib.h>
#include <string.h>
#include "src/base/logging.h"
namespace v8 {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
StartupDataHandler::StartupDataHandler(const char* exec_path,
const char* natives_blob,
const char* snapshot_blob) {
// If we have (at least one) explicitly given blob, use those.
// If not, use the default blob locations next to the d8 binary.
if (natives_blob || snapshot_blob) {
LoadFromFiles(natives_blob, snapshot_blob);
} else {
char* natives;
char* snapshot;
LoadFromFiles(RelativePath(&natives, exec_path, "natives_blob.bin"),
RelativePath(&snapshot, exec_path, "snapshot_blob.bin"));
free(natives);
free(snapshot);
}
}
StartupDataHandler::~StartupDataHandler() {
delete[] natives_.data;
delete[] snapshot_.data;
}
char* StartupDataHandler::RelativePath(char** buffer, const char* exec_path,
const char* name) {
DCHECK(exec_path);
const char* last_slash = strrchr(exec_path, '/');
if (last_slash) {
int after_slash = last_slash - exec_path + 1;
int name_length = static_cast<int>(strlen(name));
*buffer = reinterpret_cast<char*>(calloc(after_slash + name_length + 1, 1));
strncpy(*buffer, exec_path, after_slash);
strncat(*buffer, name, name_length);
} else {
*buffer = strdup(name);
}
return *buffer;
}
void StartupDataHandler::LoadFromFiles(const char* natives_blob,
const char* snapshot_blob) {
Load(natives_blob, &natives_, v8::V8::SetNativesDataBlob);
Load(snapshot_blob, &snapshot_, v8::V8::SetSnapshotDataBlob);
}
void StartupDataHandler::Load(const char* blob_file,
v8::StartupData* startup_data,
void (*setter_fn)(v8::StartupData*)) {
startup_data->data = NULL;
startup_data->raw_size = 0;
if (!blob_file) return;
FILE* file = fopen(blob_file, "rb");
if (!file) return;
fseek(file, 0, SEEK_END);
startup_data->raw_size = ftell(file);
rewind(file);
startup_data->data = new char[startup_data->raw_size];
int read_size = static_cast<int>(fread(const_cast<char*>(startup_data->data),
1, startup_data->raw_size, file));
fclose(file);
if (startup_data->raw_size == read_size) (*setter_fn)(startup_data);
}
#endif // V8_USE_EXTERNAL_STARTUP_DATA
} // namespace v8

51
src/startup-data-util.h Normal file
View File

@ -0,0 +1,51 @@
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_STARTUP_DATA_UTIL_H_
#define V8_STARTUP_DATA_UTIL_H_
#include "include/v8.h"
namespace v8 {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
// Helper class to load the startup data files from disk.
//
// This is meant as a convenience for stand-alone binaries like d8, cctest,
// unittest. A V8 embedder would likely either handle startup data on their
// own or just disable the feature if they don't want to handle it at all,
// while tools like cctest need to work in either configuration. Hence this is
// not meant for inclusion in the general v8 library.
class StartupDataHandler {
public:
// Load startup data, and call the v8::V8::Set*DataBlob API functions.
//
// natives_blob and snapshot_blob will be loaded realitive to exec_path,
// which would usually be the equivalent of argv[0].
StartupDataHandler(const char* exec_path, const char* natives_blob,
const char* snapshot_blob);
~StartupDataHandler();
private:
static char* RelativePath(char** buffer, const char* exec_path,
const char* name);
void LoadFromFiles(const char* natives_blob, const char* snapshot_blob);
void Load(const char* blob_file, v8::StartupData* startup_data,
void (*setter_fn)(v8::StartupData*));
v8::StartupData natives_;
v8::StartupData snapshot_;
// Disallow copy & assign.
StartupDataHandler(const StartupDataHandler& other);
void operator=(const StartupDataHandler& other);
};
#endif // V8_USE_EXTERNAL_STARTUP_DATA
} // namespace v8
#endif // V8_STARTUP_DATA_UTIL_H_

View File

@ -34,6 +34,10 @@
#include "test/cctest/profiler-extension.h"
#include "test/cctest/trace-extension.h"
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
#include "src/startup-data-util.h"
#endif // V8_USE_EXTERNAL_STARTUP_DATA
#if V8_OS_WIN
#include <windows.h> // NOLINT
#if V8_CC_MSVC
@ -166,6 +170,9 @@ int main(int argc, char* argv[]) {
v8::V8::InitializePlatform(platform);
v8::internal::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
v8::V8::Initialize();
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
v8::StartupDataHandler startup_data(argv[0], NULL, NULL);
#endif
CcTestArrayBufferAllocator array_buffer_allocator;
v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);

View File

@ -167,7 +167,9 @@
'test-weakmaps.cc',
'test-weaksets.cc',
'test-weaktypedarrays.cc',
'trace-extension.cc'
'trace-extension.cc',
'../../src/startup-data-util.h',
'../../src/startup-data-util.cc'
],
'conditions': [
['v8_target_arch=="ia32"', {

View File

@ -7,6 +7,10 @@
#include "src/base/compiler-specific.h"
#include "testing/gmock/include/gmock/gmock.h"
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
#include "src/startup-data-util.h"
#endif // V8_USE_EXTERNAL_STARTUP_DATA
namespace {
class DefaultPlatformEnvironment FINAL : public ::testing::Environment {
@ -41,5 +45,8 @@ int main(int argc, char** argv) {
testing::InitGoogleMock(&argc, argv);
testing::AddGlobalTestEnvironment(new DefaultPlatformEnvironment);
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
v8::StartupDataHandler startup_data(argv[0], NULL, NULL);
#endif
return RUN_ALL_TESTS();
}

View File

@ -82,6 +82,8 @@
'run-all-unittests.cc',
'test-utils.h',
'test-utils.cc',
'../../src/startup-data-util.h',
'../../src/startup-data-util.cc'
],
'conditions': [
['v8_target_arch=="arm"', {