1 // Copyright 2006-2008 the V8 project 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.
11 #include "include/libplatform/libplatform.h"
12 #include "src/assembler.h"
13 #include "src/base/platform/platform.h"
14 #include "src/bootstrapper.h"
15 #include "src/flags.h"
17 #include "src/snapshot/natives.h"
18 #include "src/snapshot/serialize.h"
23 class SnapshotWriter {
25 explicit SnapshotWriter(const char* snapshot_file)
26 : fp_(GetFileDescriptorOrDie(snapshot_file)),
27 startup_blob_file_(NULL) {}
31 if (startup_blob_file_) fclose(startup_blob_file_);
34 void SetStartupBlobFile(const char* startup_blob_file) {
35 if (startup_blob_file != NULL)
36 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file);
39 void WriteSnapshot(v8::StartupData blob) const {
40 i::Vector<const i::byte> blob_vector(
41 reinterpret_cast<const i::byte*>(blob.data), blob.raw_size);
42 WriteSnapshotFile(blob_vector);
43 MaybeWriteStartupBlob(blob_vector);
47 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const {
48 if (!startup_blob_file_) return;
50 size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_);
51 if (written != static_cast<size_t>(blob.length())) {
52 i::PrintF("Writing snapshot file failed.. Aborting.\n");
57 void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const {
63 void WriteFilePrefix() const {
64 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
65 fprintf(fp_, "#include \"src/v8.h\"\n");
66 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n");
67 fprintf(fp_, "#include \"src/snapshot/snapshot.h\"\n\n");
68 fprintf(fp_, "namespace v8 {\n");
69 fprintf(fp_, "namespace internal {\n\n");
72 void WriteFileSuffix() const {
73 fprintf(fp_, "const v8::StartupData* Snapshot::DefaultSnapshotBlob() {\n");
74 fprintf(fp_, " return &blob;\n");
75 fprintf(fp_, "}\n\n");
76 fprintf(fp_, "} // namespace internal\n");
77 fprintf(fp_, "} // namespace v8\n");
80 void WriteData(const i::Vector<const i::byte>& blob) const {
81 fprintf(fp_, "static const byte blob_data[] = {\n");
82 WriteSnapshotData(blob);
84 fprintf(fp_, "static const int blob_size = %d;\n", blob.length());
85 fprintf(fp_, "static const v8::StartupData blob =\n");
86 fprintf(fp_, "{ (const char*) blob_data, blob_size };\n");
89 void WriteSnapshotData(const i::Vector<const i::byte>& blob) const {
90 for (int i = 0; i < blob.length(); i++) {
91 if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n");
92 if (i > 0) fprintf(fp_, ",");
93 fprintf(fp_, "%u", static_cast<unsigned char>(blob.at(i)));
98 FILE* GetFileDescriptorOrDie(const char* filename) {
99 FILE* fp = base::OS::FOpen(filename, "wb");
101 i::PrintF("Unable to open file \"%s\" for writing.\n", filename);
108 FILE* startup_blob_file_;
112 char* GetExtraCode(char* filename) {
113 if (filename == NULL || strlen(filename) == 0) return NULL;
114 ::printf("Embedding extra script: %s\n", filename);
115 FILE* file = base::OS::FOpen(filename, "rb");
117 fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno);
120 fseek(file, 0, SEEK_END);
121 int size = ftell(file);
123 char* chars = new char[size + 1];
125 for (int i = 0; i < size;) {
126 int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
128 fprintf(stderr, "Failed to read '%s': errno %d\n", filename, errno);
138 int main(int argc, char** argv) {
139 // By default, log code create information in the snapshot.
140 i::FLAG_log_code = true;
141 i::FLAG_logfile_per_isolate = false;
143 // Print the usage if an error occurs when parsing the command line
144 // flags or if the help flag is set.
145 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
146 if (result > 0 || (argc != 2 && argc != 3) || i::FLAG_help) {
147 ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
148 i::FlagList::PrintHelp();
149 return !i::FLAG_help;
152 i::CpuFeatures::Probe(true);
154 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
155 v8::V8::InitializePlatform(platform);
156 v8::V8::Initialize();
159 SnapshotWriter writer(argv[1]);
160 if (i::FLAG_startup_blob) writer.SetStartupBlobFile(i::FLAG_startup_blob);
161 char* extra_code = GetExtraCode(argc == 3 ? argv[2] : NULL);
162 StartupData blob = v8::V8::CreateSnapshotDataBlob(extra_code);
164 writer.WriteSnapshot(blob);
170 V8::ShutdownPlatform();