OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <errno.h> | 5 #include <errno.h> |
6 #include <signal.h> | 6 #include <signal.h> |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "src/v8.h" | 9 #include "src/v8.h" |
10 | 10 |
11 #include "include/libplatform/libplatform.h" | 11 #include "include/libplatform/libplatform.h" |
12 #include "src/assembler.h" | 12 #include "src/assembler.h" |
13 #include "src/base/platform/platform.h" | 13 #include "src/base/platform/platform.h" |
14 #include "src/bootstrapper.h" | 14 #include "src/bootstrapper.h" |
15 #include "src/flags.h" | 15 #include "src/flags.h" |
16 #include "src/list.h" | 16 #include "src/list.h" |
17 #include "src/natives.h" | 17 #include "src/natives.h" |
18 #include "src/serialize.h" | 18 #include "src/serialize.h" |
19 | 19 |
20 | 20 |
21 using namespace v8; | 21 using namespace v8; |
22 | 22 |
23 class SnapshotWriter { | 23 class SnapshotWriter { |
24 public: | 24 public: |
25 explicit SnapshotWriter(const char* snapshot_file) | 25 explicit SnapshotWriter(const char* snapshot_file) |
26 : fp_(GetFileDescriptorOrDie(snapshot_file)), | 26 : fp_(GetFileDescriptorOrDie(snapshot_file)), |
27 raw_file_(NULL), | |
28 raw_context_file_(NULL), | |
29 startup_blob_file_(NULL) {} | 27 startup_blob_file_(NULL) {} |
30 | 28 |
31 ~SnapshotWriter() { | 29 ~SnapshotWriter() { |
32 fclose(fp_); | 30 fclose(fp_); |
33 if (raw_file_) fclose(raw_file_); | |
34 if (raw_context_file_) fclose(raw_context_file_); | |
35 if (startup_blob_file_) fclose(startup_blob_file_); | 31 if (startup_blob_file_) fclose(startup_blob_file_); |
36 } | 32 } |
37 | 33 |
38 void SetRawFiles(const char* raw_file, const char* raw_context_file) { | |
39 raw_file_ = GetFileDescriptorOrDie(raw_file); | |
40 raw_context_file_ = GetFileDescriptorOrDie(raw_context_file); | |
41 } | |
42 | |
43 void SetStartupBlobFile(const char* startup_blob_file) { | 34 void SetStartupBlobFile(const char* startup_blob_file) { |
44 if (startup_blob_file != NULL) | 35 if (startup_blob_file != NULL) |
45 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); | 36 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); |
46 } | 37 } |
47 | 38 |
48 void WriteSnapshot(const i::SnapshotData& sd, | 39 void WriteSnapshot(const i::SnapshotData& sd, |
49 const i::SnapshotData& csd) const { | 40 const i::SnapshotData& csd) const { |
50 WriteSnapshotFile(sd, csd); | 41 i::SnapshotByteSink sink; |
51 MaybeWriteStartupBlob(sd, csd); | 42 sink.PutBlob(sd.RawData(), "startup"); |
| 43 sink.PutBlob(csd.RawData(), "context"); |
| 44 const i::Vector<const i::byte>& blob = sink.data().ToConstVector(); |
| 45 |
| 46 WriteSnapshotFile(blob); |
| 47 MaybeWriteStartupBlob(blob); |
52 } | 48 } |
53 | 49 |
54 private: | 50 private: |
55 void MaybeWriteStartupBlob(const i::SnapshotData& sd, | 51 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const { |
56 const i::SnapshotData& csd) const { | |
57 if (!startup_blob_file_) return; | 52 if (!startup_blob_file_) return; |
58 | 53 |
59 i::SnapshotByteSink sink; | 54 size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_); |
60 | 55 if (written != static_cast<size_t>(blob.length())) { |
61 sink.PutBlob(sd.RawData(), "snapshot"); | |
62 sink.PutBlob(csd.RawData(), "context"); | |
63 | |
64 const i::List<i::byte>& startup_blob = sink.data(); | |
65 size_t written = fwrite(startup_blob.begin(), 1, startup_blob.length(), | |
66 startup_blob_file_); | |
67 if (written != static_cast<size_t>(startup_blob.length())) { | |
68 i::PrintF("Writing snapshot file failed.. Aborting.\n"); | 56 i::PrintF("Writing snapshot file failed.. Aborting.\n"); |
69 exit(1); | 57 exit(1); |
70 } | 58 } |
71 } | 59 } |
72 | 60 |
73 void WriteSnapshotFile(const i::SnapshotData& snapshot_data, | 61 void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const { |
74 const i::SnapshotData& context_snapshot_data) const { | |
75 WriteFilePrefix(); | 62 WriteFilePrefix(); |
76 WriteData("", snapshot_data.RawData(), raw_file_); | 63 WriteData(blob); |
77 WriteData("context_", context_snapshot_data.RawData(), raw_context_file_); | |
78 WriteFileSuffix(); | 64 WriteFileSuffix(); |
79 } | 65 } |
80 | 66 |
81 void WriteFilePrefix() const { | 67 void WriteFilePrefix() const { |
82 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); | 68 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); |
83 fprintf(fp_, "#include \"src/v8.h\"\n"); | 69 fprintf(fp_, "#include \"src/v8.h\"\n"); |
84 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); | 70 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); |
85 fprintf(fp_, "#include \"src/snapshot.h\"\n\n"); | 71 fprintf(fp_, "#include \"src/snapshot.h\"\n\n"); |
86 fprintf(fp_, "namespace v8 {\n"); | 72 fprintf(fp_, "namespace v8 {\n"); |
87 fprintf(fp_, "namespace internal {\n\n"); | 73 fprintf(fp_, "namespace internal {\n\n"); |
88 } | 74 } |
89 | 75 |
90 void WriteFileSuffix() const { | 76 void WriteFileSuffix() const { |
| 77 fprintf(fp_, "const v8::StartupData Snapshot::SnapshotBlob() {\n"); |
| 78 fprintf(fp_, " v8::StartupData blob;\n"); |
| 79 fprintf(fp_, " blob.data = reinterpret_cast<const char*>(blob_data);\n"); |
| 80 fprintf(fp_, " blob.raw_size = blob_size;\n"); |
| 81 fprintf(fp_, " return blob;\n"); |
| 82 fprintf(fp_, "}\n\n"); |
91 fprintf(fp_, "} // namespace internal\n"); | 83 fprintf(fp_, "} // namespace internal\n"); |
92 fprintf(fp_, "} // namespace v8\n"); | 84 fprintf(fp_, "} // namespace v8\n"); |
93 } | 85 } |
94 | 86 |
95 void WriteData(const char* prefix, | 87 void WriteData(const i::Vector<const i::byte>& blob) const { |
96 const i::Vector<const i::byte>& source_data, | 88 fprintf(fp_, "static const byte blob_data[] = {\n"); |
97 FILE* raw_file) const { | 89 WriteSnapshotData(blob); |
98 MaybeWriteRawFile(&source_data, raw_file); | |
99 WriteData(prefix, source_data); | |
100 } | |
101 | |
102 void MaybeWriteRawFile(const i::Vector<const i::byte>* data, | |
103 FILE* raw_file) const { | |
104 if (!data || !raw_file) return; | |
105 | |
106 // Sanity check, whether i::List iterators truly return pointers to an | |
107 // internal array. | |
108 DCHECK(data->end() - data->begin() == data->length()); | |
109 | |
110 size_t written = fwrite(data->begin(), 1, data->length(), raw_file); | |
111 if (written != static_cast<size_t>(data->length())) { | |
112 i::PrintF("Writing raw file failed.. Aborting.\n"); | |
113 exit(1); | |
114 } | |
115 } | |
116 | |
117 void WriteData(const char* prefix, | |
118 const i::Vector<const i::byte>& source_data) const { | |
119 fprintf(fp_, "const byte Snapshot::%sdata_[] = {\n", prefix); | |
120 WriteSnapshotData(source_data); | |
121 fprintf(fp_, "};\n"); | 90 fprintf(fp_, "};\n"); |
122 fprintf(fp_, "const int Snapshot::%ssize_ = %d;\n", prefix, | 91 fprintf(fp_, "static const int blob_size = %d;\n", blob.length()); |
123 source_data.length()); | |
124 fprintf(fp_, "\n"); | 92 fprintf(fp_, "\n"); |
125 } | 93 } |
126 | 94 |
127 void WriteSnapshotData(const i::Vector<const i::byte>& data) const { | 95 void WriteSnapshotData(const i::Vector<const i::byte>& blob) const { |
128 for (int i = 0; i < data.length(); i++) { | 96 for (int i = 0; i < blob.length(); i++) { |
129 if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n"); | 97 if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n"); |
130 if (i > 0) fprintf(fp_, ","); | 98 if (i > 0) fprintf(fp_, ","); |
131 fprintf(fp_, "%u", static_cast<unsigned char>(data.at(i))); | 99 fprintf(fp_, "%u", static_cast<unsigned char>(blob.at(i))); |
132 } | 100 } |
133 fprintf(fp_, "\n"); | 101 fprintf(fp_, "\n"); |
134 } | 102 } |
135 | 103 |
136 FILE* GetFileDescriptorOrDie(const char* filename) { | 104 FILE* GetFileDescriptorOrDie(const char* filename) { |
137 FILE* fp = base::OS::FOpen(filename, "wb"); | 105 FILE* fp = base::OS::FOpen(filename, "wb"); |
138 if (fp == NULL) { | 106 if (fp == NULL) { |
139 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); | 107 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); |
140 exit(1); | 108 exit(1); |
141 } | 109 } |
142 return fp; | 110 return fp; |
143 } | 111 } |
144 | 112 |
145 FILE* fp_; | 113 FILE* fp_; |
146 FILE* raw_file_; | |
147 FILE* raw_context_file_; | |
148 FILE* startup_blob_file_; | 114 FILE* startup_blob_file_; |
149 }; | 115 }; |
150 | 116 |
151 | 117 |
152 void DumpException(Handle<Message> message) { | 118 void DumpException(Handle<Message> message) { |
153 String::Utf8Value message_string(message->Get()); | 119 String::Utf8Value message_string(message->Get()); |
154 String::Utf8Value message_line(message->GetSourceLine()); | 120 String::Utf8Value message_line(message->GetSourceLine()); |
155 fprintf(stderr, "%s at line %d\n", *message_string, message->GetLineNumber()); | 121 fprintf(stderr, "%s at line %d\n", *message_string, message->GetLineNumber()); |
156 fprintf(stderr, "%s\n", *message_line); | 122 fprintf(stderr, "%s\n", *message_line); |
157 for (int i = 0; i <= message->GetEndColumn(); ++i) { | 123 for (int i = 0; i <= message->GetEndColumn(); ++i) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 i::StartupSerializer ser(internal_isolate, &snapshot_sink); | 229 i::StartupSerializer ser(internal_isolate, &snapshot_sink); |
264 ser.SerializeStrongReferences(); | 230 ser.SerializeStrongReferences(); |
265 | 231 |
266 i::SnapshotByteSink context_sink; | 232 i::SnapshotByteSink context_sink; |
267 i::PartialSerializer context_ser(internal_isolate, &ser, &context_sink); | 233 i::PartialSerializer context_ser(internal_isolate, &ser, &context_sink); |
268 context_ser.Serialize(&raw_context); | 234 context_ser.Serialize(&raw_context); |
269 ser.SerializeWeakReferences(); | 235 ser.SerializeWeakReferences(); |
270 | 236 |
271 { | 237 { |
272 SnapshotWriter writer(argv[1]); | 238 SnapshotWriter writer(argv[1]); |
273 if (i::FLAG_raw_file && i::FLAG_raw_context_file) | |
274 writer.SetRawFiles(i::FLAG_raw_file, i::FLAG_raw_context_file); | |
275 if (i::FLAG_startup_blob) | 239 if (i::FLAG_startup_blob) |
276 writer.SetStartupBlobFile(i::FLAG_startup_blob); | 240 writer.SetStartupBlobFile(i::FLAG_startup_blob); |
277 i::SnapshotData sd(snapshot_sink, ser); | 241 i::SnapshotData sd(snapshot_sink, ser); |
278 i::SnapshotData csd(context_sink, context_ser); | 242 i::SnapshotData csd(context_sink, context_ser); |
279 writer.WriteSnapshot(sd, csd); | 243 writer.WriteSnapshot(sd, csd); |
280 } | 244 } |
281 } | 245 } |
282 | 246 |
283 isolate->Dispose(); | 247 isolate->Dispose(); |
284 V8::Dispose(); | 248 V8::Dispose(); |
285 V8::ShutdownPlatform(); | 249 V8::ShutdownPlatform(); |
286 delete platform; | 250 delete platform; |
287 return 0; | 251 return 0; |
288 } | 252 } |
OLD | NEW |