2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-06-19 18:38:03 +00:00
|
|
|
#include <errno.h>
|
2008-07-03 15:10:15 +00:00
|
|
|
#include <signal.h>
|
2014-11-21 12:45:10 +00:00
|
|
|
#include <stdio.h>
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-07-03 07:37:27 +00:00
|
|
|
#include "include/libplatform/libplatform.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/assembler.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/platform/platform.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/bootstrapper.h"
|
|
|
|
#include "src/flags.h"
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/list.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/natives.h"
|
|
|
|
#include "src/serialize.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-04-17 14:45:06 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
using namespace v8;
|
|
|
|
|
2014-04-24 12:31:10 +00:00
|
|
|
class SnapshotWriter {
|
|
|
|
public:
|
|
|
|
explicit SnapshotWriter(const char* snapshot_file)
|
2014-12-05 13:03:10 +00:00
|
|
|
: fp_(GetFileDescriptorOrDie(snapshot_file)),
|
|
|
|
startup_blob_file_(NULL) {}
|
2014-04-24 12:31:10 +00:00
|
|
|
|
|
|
|
~SnapshotWriter() {
|
|
|
|
fclose(fp_);
|
2014-06-23 13:52:17 +00:00
|
|
|
if (startup_blob_file_) fclose(startup_blob_file_);
|
2011-04-29 12:08:33 +00:00
|
|
|
}
|
2014-04-24 12:31:10 +00:00
|
|
|
|
2014-06-23 13:52:17 +00:00
|
|
|
void SetStartupBlobFile(const char* startup_blob_file) {
|
|
|
|
if (startup_blob_file != NULL)
|
|
|
|
startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file);
|
|
|
|
}
|
|
|
|
|
2014-12-10 14:20:12 +00:00
|
|
|
void WriteSnapshot(v8::StartupData blob) const {
|
|
|
|
i::Vector<const i::byte> blob_vector(
|
|
|
|
reinterpret_cast<const i::byte*>(blob.data), blob.raw_size);
|
|
|
|
WriteSnapshotFile(blob_vector);
|
|
|
|
MaybeWriteStartupBlob(blob_vector);
|
2014-06-23 13:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-12-10 11:46:27 +00:00
|
|
|
void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const {
|
2014-10-23 11:23:57 +00:00
|
|
|
if (!startup_blob_file_) return;
|
2014-06-23 13:52:17 +00:00
|
|
|
|
2014-12-10 11:46:27 +00:00
|
|
|
size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_);
|
|
|
|
if (written != static_cast<size_t>(blob.length())) {
|
2014-06-23 13:52:17 +00:00
|
|
|
i::PrintF("Writing snapshot file failed.. Aborting.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:46:27 +00:00
|
|
|
void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const {
|
2014-04-24 12:31:10 +00:00
|
|
|
WriteFilePrefix();
|
2014-12-10 11:46:27 +00:00
|
|
|
WriteData(blob);
|
2014-04-24 12:31:10 +00:00
|
|
|
WriteFileSuffix();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteFilePrefix() const {
|
2009-10-30 10:23:12 +00:00
|
|
|
fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
|
2014-06-03 08:12:43 +00:00
|
|
|
fprintf(fp_, "#include \"src/v8.h\"\n");
|
2014-06-30 13:25:46 +00:00
|
|
|
fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n");
|
2014-06-03 08:12:43 +00:00
|
|
|
fprintf(fp_, "#include \"src/snapshot.h\"\n\n");
|
2014-04-24 12:31:10 +00:00
|
|
|
fprintf(fp_, "namespace v8 {\n");
|
|
|
|
fprintf(fp_, "namespace internal {\n\n");
|
2009-10-30 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 12:31:10 +00:00
|
|
|
void WriteFileSuffix() const {
|
2015-02-25 11:14:40 +00:00
|
|
|
fprintf(fp_, "const v8::StartupData* Snapshot::DefaultSnapshotBlob() {\n");
|
|
|
|
fprintf(fp_, " return &blob;\n");
|
2014-12-10 11:46:27 +00:00
|
|
|
fprintf(fp_, "}\n\n");
|
2014-04-24 12:31:10 +00:00
|
|
|
fprintf(fp_, "} // namespace internal\n");
|
|
|
|
fprintf(fp_, "} // namespace v8\n");
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:46:27 +00:00
|
|
|
void WriteData(const i::Vector<const i::byte>& blob) const {
|
|
|
|
fprintf(fp_, "static const byte blob_data[] = {\n");
|
|
|
|
WriteSnapshotData(blob);
|
2014-04-24 12:31:10 +00:00
|
|
|
fprintf(fp_, "};\n");
|
2014-12-10 11:46:27 +00:00
|
|
|
fprintf(fp_, "static const int blob_size = %d;\n", blob.length());
|
2015-02-25 11:14:40 +00:00
|
|
|
fprintf(fp_, "static const v8::StartupData blob =\n");
|
|
|
|
fprintf(fp_, "{ (const char*) blob_data, blob_size };\n");
|
2009-10-30 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 11:46:27 +00:00
|
|
|
void WriteSnapshotData(const i::Vector<const i::byte>& blob) const {
|
|
|
|
for (int i = 0; i < blob.length(); i++) {
|
2014-12-05 13:03:10 +00:00
|
|
|
if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n");
|
|
|
|
if (i > 0) fprintf(fp_, ",");
|
2014-12-10 11:46:27 +00:00
|
|
|
fprintf(fp_, "%u", static_cast<unsigned char>(blob.at(i)));
|
2014-04-24 12:31:10 +00:00
|
|
|
}
|
|
|
|
fprintf(fp_, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* GetFileDescriptorOrDie(const char* filename) {
|
2014-06-30 13:25:46 +00:00
|
|
|
FILE* fp = base::OS::FOpen(filename, "wb");
|
2014-04-24 12:31:10 +00:00
|
|
|
if (fp == NULL) {
|
|
|
|
i::PrintF("Unable to open file \"%s\" for writing.\n", filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return fp;
|
|
|
|
}
|
2011-04-29 12:08:33 +00:00
|
|
|
|
|
|
|
FILE* fp_;
|
2014-06-23 13:52:17 +00:00
|
|
|
FILE* startup_blob_file_;
|
2011-04-29 12:08:33 +00:00
|
|
|
};
|
2010-01-18 16:04:25 +00:00
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2015-01-12 15:26:20 +00:00
|
|
|
char* GetExtraCode(char* filename) {
|
|
|
|
if (filename == NULL || strlen(filename) == 0) return NULL;
|
|
|
|
::printf("Embedding extra script: %s\n", filename);
|
|
|
|
FILE* file = base::OS::FOpen(filename, "rb");
|
|
|
|
if (file == NULL) {
|
|
|
|
fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
int size = ftell(file);
|
|
|
|
rewind(file);
|
|
|
|
char* chars = new char[size + 1];
|
|
|
|
chars[size] = '\0';
|
|
|
|
for (int i = 0; i < size;) {
|
|
|
|
int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
|
|
|
|
if (read < 0) {
|
|
|
|
fprintf(stderr, "Failed to read '%s': errno %d\n", filename, errno);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
i += read;
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
// By default, log code create information in the snapshot.
|
|
|
|
i::FLAG_log_code = true;
|
2014-12-10 14:20:12 +00:00
|
|
|
i::FLAG_logfile_per_isolate = false;
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Print the usage if an error occurs when parsing the command line
|
|
|
|
// flags or if the help flag is set.
|
2014-05-16 15:18:24 +00:00
|
|
|
int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
|
2015-01-12 15:26:20 +00:00
|
|
|
if (result > 0 || (argc != 2 && argc != 3) || i::FLAG_help) {
|
2008-07-03 15:10:15 +00:00
|
|
|
::printf("Usage: %s [flag] ... outfile\n", argv[0]);
|
2008-09-12 10:31:37 +00:00
|
|
|
i::FlagList::PrintHelp();
|
2008-11-06 10:43:15 +00:00
|
|
|
return !i::FLAG_help;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2014-09-19 08:01:35 +00:00
|
|
|
|
|
|
|
i::CpuFeatures::Probe(true);
|
|
|
|
V8::InitializeICU();
|
|
|
|
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
|
|
|
|
v8::V8::InitializePlatform(platform);
|
|
|
|
v8::V8::Initialize();
|
|
|
|
|
2014-12-10 14:20:12 +00:00
|
|
|
{
|
|
|
|
SnapshotWriter writer(argv[1]);
|
|
|
|
if (i::FLAG_startup_blob) writer.SetStartupBlobFile(i::FLAG_startup_blob);
|
2015-01-12 15:26:20 +00:00
|
|
|
char* extra_code = GetExtraCode(argc == 3 ? argv[2] : NULL);
|
|
|
|
StartupData blob = v8::V8::CreateSnapshotDataBlob(extra_code);
|
2014-12-10 14:20:12 +00:00
|
|
|
CHECK(blob.data);
|
|
|
|
writer.WriteSnapshot(blob);
|
2015-01-12 15:26:20 +00:00
|
|
|
delete[] extra_code;
|
2014-12-10 14:20:12 +00:00
|
|
|
delete[] blob.data;
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
2014-04-24 12:31:10 +00:00
|
|
|
|
2014-05-22 09:36:20 +00:00
|
|
|
V8::Dispose();
|
2014-07-03 07:37:27 +00:00
|
|
|
V8::ShutdownPlatform();
|
|
|
|
delete platform;
|
2008-07-03 15:10:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|