OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <errno.h> | |
6 #include <signal.h> | |
7 #include <stdio.h> | |
8 | |
9 #include "src/v8.h" | |
10 | |
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" | |
16 #include "src/list.h" | |
17 #include "src/natives.h" | |
18 #include "src/serialize.h" | |
19 | |
20 | |
21 using namespace v8; | |
22 | |
23 class SnapshotWriter { | |
24 public: | |
25 explicit SnapshotWriter(const char* snapshot_file) | |
26 : fp_(GetFileDescriptorOrDie(snapshot_file)), | |
27 startup_blob_file_(NULL) {} | |
28 | |
29 ~SnapshotWriter() { | |
30 fclose(fp_); | |
31 if (startup_blob_file_) fclose(startup_blob_file_); | |
32 } | |
33 | |
34 void SetStartupBlobFile(const char* startup_blob_file) { | |
35 if (startup_blob_file != NULL) | |
36 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); | |
37 } | |
38 | |
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); | |
44 } | |
45 | |
46 private: | |
47 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const { | |
48 if (!startup_blob_file_) return; | |
49 | |
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"); | |
53 exit(1); | |
54 } | |
55 } | |
56 | |
57 void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const { | |
58 WriteFilePrefix(); | |
59 WriteData(blob); | |
60 WriteFileSuffix(); | |
61 } | |
62 | |
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.h\"\n\n"); | |
68 fprintf(fp_, "namespace v8 {\n"); | |
69 fprintf(fp_, "namespace internal {\n\n"); | |
70 } | |
71 | |
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"); | |
78 } | |
79 | |
80 void WriteData(const i::Vector<const i::byte>& blob) const { | |
81 fprintf(fp_, "static const byte blob_data[] = {\n"); | |
82 WriteSnapshotData(blob); | |
83 fprintf(fp_, "};\n"); | |
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"); | |
87 } | |
88 | |
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))); | |
94 } | |
95 fprintf(fp_, "\n"); | |
96 } | |
97 | |
98 FILE* GetFileDescriptorOrDie(const char* filename) { | |
99 FILE* fp = base::OS::FOpen(filename, "wb"); | |
100 if (fp == NULL) { | |
101 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); | |
102 exit(1); | |
103 } | |
104 return fp; | |
105 } | |
106 | |
107 FILE* fp_; | |
108 FILE* startup_blob_file_; | |
109 }; | |
110 | |
111 | |
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"); | |
116 if (file == NULL) { | |
117 fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno); | |
118 exit(1); | |
119 } | |
120 fseek(file, 0, SEEK_END); | |
121 int size = ftell(file); | |
122 rewind(file); | |
123 char* chars = new char[size + 1]; | |
124 chars[size] = '\0'; | |
125 for (int i = 0; i < size;) { | |
126 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); | |
127 if (read < 0) { | |
128 fprintf(stderr, "Failed to read '%s': errno %d\n", filename, errno); | |
129 exit(1); | |
130 } | |
131 i += read; | |
132 } | |
133 fclose(file); | |
134 return chars; | |
135 } | |
136 | |
137 | |
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; | |
142 | |
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; | |
150 } | |
151 | |
152 i::CpuFeatures::Probe(true); | |
153 V8::InitializeICU(); | |
154 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); | |
155 v8::V8::InitializePlatform(platform); | |
156 v8::V8::Initialize(); | |
157 | |
158 { | |
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); | |
163 CHECK(blob.data); | |
164 writer.WriteSnapshot(blob); | |
165 delete[] extra_code; | |
166 delete[] blob.data; | |
167 } | |
168 | |
169 V8::Dispose(); | |
170 V8::ShutdownPlatform(); | |
171 delete platform; | |
172 return 0; | |
173 } | |
OLD | NEW |