Rework startup-data-util.

- Make the API look like v8::V8::InitializeICU.
  (That is: A static method call, not an object to be created on the stack.)
- Fix path separator on Windows, by calling base::OS::isPathSeparator.
- Move into API, so that it can be called by hello-world & friends.
- Actually call it from hello-world and friends.

R=jochen@chromium.org
BUG=

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

Cr-Commit-Position: refs/heads/master@{#30174}
This commit is contained in:
vogelheim 2015-08-14 05:11:29 -07:00 committed by Commit bot
parent f3a4d2c550
commit c69e2eae54
17 changed files with 126 additions and 112 deletions

View File

@ -1143,6 +1143,8 @@ source_set("v8_base") {
"src/splay-tree.h", "src/splay-tree.h",
"src/splay-tree-inl.h", "src/splay-tree-inl.h",
"src/snapshot/snapshot.h", "src/snapshot/snapshot.h",
"src/startup-data-util.h",
"src/startup-data-util.cc",
"src/string-builder.cc", "src/string-builder.cc",
"src/string-builder.h", "src/string-builder.h",
"src/string-search.cc", "src/string-search.cc",
@ -1740,8 +1742,6 @@ if ((current_toolchain == host_toolchain && v8_toolset_for_d8 == "host") ||
sources = [ sources = [
"src/d8.cc", "src/d8.cc",
"src/d8.h", "src/d8.h",
"src/startup-data-util.h",
"src/startup-data-util.cc",
] ]
configs -= [ "//build/config/compiler:chromium_code" ] configs -= [ "//build/config/compiler:chromium_code" ]

View File

@ -6219,6 +6219,25 @@ class V8_EXPORT V8 {
*/ */
static bool InitializeICU(const char* icu_data_file = NULL); static bool InitializeICU(const char* icu_data_file = NULL);
/**
* Initialize the external startup data. The embedder only needs to
* invoke this method when external startup data was enabled in a build.
*
* If V8 was compiled with the startup data in an external file, then
* V8 needs to be given those external files during startup. There are
* three ways to do this:
* - InitializeExternalStartupData(const char*)
* This will look in the given directory for files "natives_blob.bin"
* and "snapshot_blob.bin" - which is what the default build calls them.
* - InitializeExternalStartupData(const char*, const char*)
* As above, but will directly use the two given file names.
* - Call SetNativesDataBlob, SetNativesDataBlob.
* This will read the blobs from the given data structures and will
* not perform any file IO.
*/
static void InitializeExternalStartupData(const char* directory_path);
static void InitializeExternalStartupData(const char* natives_blob,
const char* snapshot_blob);
/** /**
* Sets the v8::Platform to use. This should be invoked before V8 is * Sets the v8::Platform to use. This should be invoked before V8 is
* initialized. * initialized.

View File

@ -25,6 +25,7 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
// Initialize V8. // Initialize V8.
V8::InitializeICU(); V8::InitializeICU();
V8::InitializeExternalStartupData(argv[0]);
Platform* platform = platform::CreateDefaultPlatform(); Platform* platform = platform::CreateDefaultPlatform();
V8::InitializePlatform(platform); V8::InitializePlatform(platform);
V8::Initialize(); V8::Initialize();

View File

@ -688,6 +688,7 @@ void PrintMap(map<string, string>* m) {
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
v8::V8::InitializeICU(); v8::V8::InitializeICU();
v8::V8::InitializeExternalStartupData(argv[0]);
v8::Platform* platform = v8::platform::CreateDefaultPlatform(); v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform); v8::V8::InitializePlatform(platform);
v8::V8::Initialize(); v8::V8::Initialize();

View File

@ -76,6 +76,7 @@ class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
v8::V8::InitializeICU(); v8::V8::InitializeICU();
v8::V8::InitializeExternalStartupData(argv[0]);
v8::Platform* platform = v8::platform::CreateDefaultPlatform(); v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform); v8::V8::InitializePlatform(platform);
v8::V8::Initialize(); v8::V8::Initialize();

View File

@ -50,6 +50,7 @@
#include "src/simulator.h" #include "src/simulator.h"
#include "src/snapshot/natives.h" #include "src/snapshot/natives.h"
#include "src/snapshot/snapshot.h" #include "src/snapshot/snapshot.h"
#include "src/startup-data-util.h"
#include "src/unicode-inl.h" #include "src/unicode-inl.h"
#include "src/v8.h" #include "src/v8.h"
#include "src/v8threads.h" #include "src/v8threads.h"
@ -5393,11 +5394,23 @@ HeapObjectStatistics::HeapObjectStatistics()
object_count_(0), object_count_(0),
object_size_(0) {} object_size_(0) {}
bool v8::V8::InitializeICU(const char* icu_data_file) { bool v8::V8::InitializeICU(const char* icu_data_file) {
return i::InitializeICU(icu_data_file); return i::InitializeICU(icu_data_file);
} }
void v8::V8::InitializeExternalStartupData(const char* directory_path) {
i::InitializeExternalStartupData(directory_path);
}
void v8::V8::InitializeExternalStartupData(const char* natives_blob,
const char* snapshot_blob) {
i::InitializeExternalStartupData(natives_blob, snapshot_blob);
}
const char* v8::V8::GetVersion() { const char* v8::V8::GetVersion() {
return i::Version::GetVersion(); return i::Version::GetVersion();
} }

View File

@ -49,10 +49,6 @@
#include "src/v8.h" #include "src/v8.h"
#endif // !V8_SHARED #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) #if !defined(_WIN32) && !defined(_WIN64)
#include <unistd.h> // NOLINT #include <unistd.h> // NOLINT
#else #else
@ -2357,10 +2353,12 @@ int Shell::Main(int argc, char* argv[]) {
g_platform = v8::platform::CreateDefaultPlatform(); g_platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(g_platform); v8::V8::InitializePlatform(g_platform);
v8::V8::Initialize(); v8::V8::Initialize();
#ifdef V8_USE_EXTERNAL_STARTUP_DATA if (options.natives_blob || options.snapshot_blob) {
v8::StartupDataHandler startup_data(argv[0], options.natives_blob, v8::V8::InitializeExternalStartupData(options.natives_blob,
options.snapshot_blob); options.snapshot_blob);
#endif } else {
v8::V8::InitializeExternalStartupData(argv[0]);
}
SetFlagsFromString("--trace-hydrogen-file=hydrogen.cfg"); SetFlagsFromString("--trace-hydrogen-file=hydrogen.cfg");
SetFlagsFromString("--trace-turbo-cfg-file=turbo.cfg"); SetFlagsFromString("--trace-turbo-cfg-file=turbo.cfg");
SetFlagsFromString("--redirect-code-traces-to=code.asm"); SetFlagsFromString("--redirect-code-traces-to=code.asm");

View File

@ -49,8 +49,6 @@
'sources': [ 'sources': [
'd8.h', 'd8.h',
'd8.cc', 'd8.cc',
'startup-data-util.h',
'startup-data-util.cc'
], ],
'defines': [ 'defines': [
# TODO(jochen): Remove again after this is globally turned on. # TODO(jochen): Remove again after this is globally turned on.

View File

@ -8,66 +8,41 @@
#include <string.h> #include <string.h>
#include "src/base/logging.h" #include "src/base/logging.h"
#include "src/base/platform/platform.h"
namespace v8 { namespace v8 {
namespace internal {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA #ifdef V8_USE_EXTERNAL_STARTUP_DATA
StartupDataHandler::StartupDataHandler(const char* exec_path, namespace {
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); v8::StartupData g_natives;
free(snapshot); v8::StartupData g_snapshot;
}
void ClearStartupData(v8::StartupData* data) {
data->data = nullptr;
data->raw_size = 0;
} }
StartupDataHandler::~StartupDataHandler() { void DeleteStartupData(v8::StartupData* data) {
delete[] natives_.data; delete[] data->data;
delete[] snapshot_.data; ClearStartupData(data);
} }
char* StartupDataHandler::RelativePath(char** buffer, const char* exec_path, void FreeStartupData() {
const char* name) { DeleteStartupData(&g_natives);
DCHECK(exec_path); DeleteStartupData(&g_snapshot);
const char* last_slash = strrchr(exec_path, '/');
if (last_slash) {
int after_slash = static_cast<int>(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, void Load(const char* blob_file, v8::StartupData* startup_data,
const char* snapshot_blob) { void (*setter_fn)(v8::StartupData*)) {
Load(natives_blob, &natives_, v8::V8::SetNativesDataBlob); ClearStartupData(startup_data);
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; if (!blob_file) return;
@ -86,6 +61,57 @@ void StartupDataHandler::Load(const char* blob_file,
if (startup_data->raw_size == read_size) (*setter_fn)(startup_data); if (startup_data->raw_size == read_size) (*setter_fn)(startup_data);
} }
void LoadFromFiles(const char* natives_blob, const char* snapshot_blob) {
Load(natives_blob, &g_natives, v8::V8::SetNativesDataBlob);
Load(snapshot_blob, &g_snapshot, v8::V8::SetSnapshotDataBlob);
atexit(&FreeStartupData);
}
char* RelativePath(char** buffer, const char* exec_path, const char* name) {
DCHECK(exec_path);
int path_separator = static_cast<int>(strlen(exec_path)) - 1;
while (path_separator >= 0 &&
!base::OS::isDirectorySeparator(exec_path[path_separator])) {
path_separator--;
}
if (path_separator >= 0) {
int name_length = static_cast<int>(strlen(name));
*buffer =
reinterpret_cast<char*>(calloc(path_separator + name_length + 2, 1));
*buffer[0] = '\0';
strncat(*buffer, exec_path, path_separator + 1);
strncat(*buffer, name, name_length);
} else {
*buffer = strdup(name);
}
return *buffer;
}
} // namespace
#endif // V8_USE_EXTERNAL_STARTUP_DATA #endif // V8_USE_EXTERNAL_STARTUP_DATA
void InitializeExternalStartupData(const char* directory_path) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
char* natives;
char* snapshot;
LoadFromFiles(RelativePath(&natives, directory_path, "natives_blob.bin"),
RelativePath(&snapshot, directory_path, "snapshot_blob.bin"));
free(natives);
free(snapshot);
#endif // V8_USE_EXTERNAL_STARTUP_DATA
}
void InitializeExternalStartupData(const char* natives_blob,
const char* snapshot_blob) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
LoadFromFiles(natives_blob, snapshot_blob);
#endif // V8_USE_EXTERNAL_STARTUP_DATA
}
} // namespace internal
} // namespace v8 } // namespace v8

View File

@ -9,43 +9,21 @@
#include "include/v8.h" #include "include/v8.h"
namespace v8 { namespace v8 {
namespace internal {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA // Helper functions to load 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, // 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 // 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, // 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 // while tools like cctest need to work in either configuration.
// 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: void InitializeExternalStartupData(const char* directory_path);
static char* RelativePath(char** buffer, const char* exec_path,
const char* name);
void LoadFromFiles(const char* natives_blob, const char* snapshot_blob); void InitializeExternalStartupData(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 internal
} // namespace v8 } // namespace v8
#endif // V8_STARTUP_DATA_UTIL_H_ #endif // V8_STARTUP_DATA_UTIL_H_

View File

@ -34,10 +34,6 @@
#include "test/cctest/profiler-extension.h" #include "test/cctest/profiler-extension.h"
#include "test/cctest/trace-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 #if V8_OS_WIN
#include <windows.h> // NOLINT #include <windows.h> // NOLINT
#if V8_CC_MSVC #if V8_CC_MSVC
@ -182,9 +178,7 @@ int main(int argc, char* argv[]) {
v8::V8::InitializePlatform(platform); v8::V8::InitializePlatform(platform);
v8::internal::FlagList::SetFlagsFromCommandLine(&argc, argv, true); v8::internal::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
v8::V8::Initialize(); v8::V8::Initialize();
#ifdef V8_USE_EXTERNAL_STARTUP_DATA v8::V8::InitializeExternalStartupData(argv[0]);
v8::StartupDataHandler startup_data(argv[0], NULL, NULL);
#endif
CcTestArrayBufferAllocator array_buffer_allocator; CcTestArrayBufferAllocator array_buffer_allocator;
CcTest::set_array_buffer_allocator(&array_buffer_allocator); CcTest::set_array_buffer_allocator(&array_buffer_allocator);

View File

@ -164,8 +164,6 @@
'test-weakmaps.cc', 'test-weakmaps.cc',
'test-weaksets.cc', 'test-weaksets.cc',
'trace-extension.cc', 'trace-extension.cc',
'../../src/startup-data-util.h',
'../../src/startup-data-util.cc'
], ],
'conditions': [ 'conditions': [
['v8_target_arch=="ia32"', { ['v8_target_arch=="ia32"', {

View File

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

View File

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

View File

@ -937,6 +937,8 @@
'../../src/snapshot/snapshot-source-sink.h', '../../src/snapshot/snapshot-source-sink.h',
'../../src/splay-tree.h', '../../src/splay-tree.h',
'../../src/splay-tree-inl.h', '../../src/splay-tree-inl.h',
'../../src/startup-data-util.cc',
'../../src/startup-data-util.h',
'../../src/string-builder.cc', '../../src/string-builder.cc',
'../../src/string-builder.h', '../../src/string-builder.h',
'../../src/string-search.cc', '../../src/string-search.cc',

View File

@ -43,10 +43,6 @@
#include "src/preparse-data.h" #include "src/preparse-data.h"
#include "src/preparser.h" #include "src/preparser.h"
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
#include "src/startup-data-util.h"
#endif // V8_USE_EXTERNAL_STARTUP_DATA
using namespace v8::internal; using namespace v8::internal;
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
@ -150,9 +146,8 @@ int main(int argc, char* argv[]) {
v8::Platform* platform = v8::platform::CreateDefaultPlatform(); v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform); v8::V8::InitializePlatform(platform);
v8::V8::Initialize(); v8::V8::Initialize();
#ifdef V8_USE_EXTERNAL_STARTUP_DATA v8::V8::InitializeExternalStartupData(argv[0]);
v8::StartupDataHandler startup_data(argv[0], NULL, NULL);
#endif // V8_USE_EXTERNAL_STARTUP_DATA
Encoding encoding = LATIN1; Encoding encoding = LATIN1;
std::vector<std::string> fnames; std::vector<std::string> fnames;
std::string benchmark; std::string benchmark;

View File

@ -57,8 +57,6 @@
'sources': [ 'sources': [
'parser-shell.cc', 'parser-shell.cc',
'shell-utils.h', 'shell-utils.h',
'../src/startup-data-util.h',
'../src/startup-data-util.cc',
], ],
}, },
], ],