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), |
27 startup_blob_file_(NULL) {} | 29 startup_blob_file_(NULL) {} |
28 | 30 |
29 ~SnapshotWriter() { | 31 ~SnapshotWriter() { |
30 fclose(fp_); | 32 fclose(fp_); |
| 33 if (raw_file_) fclose(raw_file_); |
| 34 if (raw_context_file_) fclose(raw_context_file_); |
31 if (startup_blob_file_) fclose(startup_blob_file_); | 35 if (startup_blob_file_) fclose(startup_blob_file_); |
32 } | 36 } |
33 | 37 |
| 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 |
34 void SetStartupBlobFile(const char* startup_blob_file) { | 43 void SetStartupBlobFile(const char* startup_blob_file) { |
35 if (startup_blob_file != NULL) | 44 if (startup_blob_file != NULL) |
36 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); | 45 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); |
37 } | 46 } |
38 | 47 |
39 void WriteSnapshot(const i::SnapshotData& sd, | 48 void WriteSnapshot(const i::SnapshotData& sd, |
40 const i::SnapshotData& csd) const { | 49 const i::SnapshotData& csd) const { |
41 i::SnapshotByteSink sink; | 50 WriteSnapshotFile(sd, csd); |
42 sink.PutBlob(sd.RawData(), "startup"); | 51 MaybeWriteStartupBlob(sd, csd); |
43 sink.PutBlob(csd.RawData(), "context"); | |
44 const i::Vector<const i::byte>& blob = sink.data().ToConstVector(); | |
45 | |
46 WriteSnapshotFile(blob); | |
47 MaybeWriteStartupBlob(blob); | |
48 } | 52 } |
49 | 53 |
50 private: | 54 private: |
51 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const { | 55 void MaybeWriteStartupBlob(const i::SnapshotData& sd, |
| 56 const i::SnapshotData& csd) const { |
52 if (!startup_blob_file_) return; | 57 if (!startup_blob_file_) return; |
53 | 58 |
54 size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_); | 59 i::SnapshotByteSink sink; |
55 if (written != static_cast<size_t>(blob.length())) { | 60 |
| 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())) { |
56 i::PrintF("Writing snapshot file failed.. Aborting.\n"); | 68 i::PrintF("Writing snapshot file failed.. Aborting.\n"); |
57 exit(1); | 69 exit(1); |
58 } | 70 } |
59 } | 71 } |
60 | 72 |
61 void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const { | 73 void WriteSnapshotFile(const i::SnapshotData& snapshot_data, |
| 74 const i::SnapshotData& context_snapshot_data) const { |
62 WriteFilePrefix(); | 75 WriteFilePrefix(); |
63 WriteData(blob); | 76 WriteData("", snapshot_data.RawData(), raw_file_); |
| 77 WriteData("context_", context_snapshot_data.RawData(), raw_context_file_); |
64 WriteFileSuffix(); | 78 WriteFileSuffix(); |
65 } | 79 } |
66 | 80 |
67 void WriteFilePrefix() const { | 81 void WriteFilePrefix() const { |
68 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); | 82 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); |
69 fprintf(fp_, "#include \"src/v8.h\"\n"); | 83 fprintf(fp_, "#include \"src/v8.h\"\n"); |
70 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); | 84 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); |
71 fprintf(fp_, "#include \"src/snapshot.h\"\n\n"); | 85 fprintf(fp_, "#include \"src/snapshot.h\"\n\n"); |
72 fprintf(fp_, "namespace v8 {\n"); | 86 fprintf(fp_, "namespace v8 {\n"); |
73 fprintf(fp_, "namespace internal {\n\n"); | 87 fprintf(fp_, "namespace internal {\n\n"); |
74 } | 88 } |
75 | 89 |
76 void WriteFileSuffix() const { | 90 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"); | |
83 fprintf(fp_, "} // namespace internal\n"); | 91 fprintf(fp_, "} // namespace internal\n"); |
84 fprintf(fp_, "} // namespace v8\n"); | 92 fprintf(fp_, "} // namespace v8\n"); |
85 } | 93 } |
86 | 94 |
87 void WriteData(const i::Vector<const i::byte>& blob) const { | 95 void WriteData(const char* prefix, |
88 fprintf(fp_, "static const byte blob_data[] = {\n"); | 96 const i::Vector<const i::byte>& source_data, |
89 WriteSnapshotData(blob); | 97 FILE* raw_file) const { |
| 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); |
90 fprintf(fp_, "};\n"); | 121 fprintf(fp_, "};\n"); |
91 fprintf(fp_, "static const int blob_size = %d;\n", blob.length()); | 122 fprintf(fp_, "const int Snapshot::%ssize_ = %d;\n", prefix, |
| 123 source_data.length()); |
92 fprintf(fp_, "\n"); | 124 fprintf(fp_, "\n"); |
93 } | 125 } |
94 | 126 |
95 void WriteSnapshotData(const i::Vector<const i::byte>& blob) const { | 127 void WriteSnapshotData(const i::Vector<const i::byte>& data) const { |
96 for (int i = 0; i < blob.length(); i++) { | 128 for (int i = 0; i < data.length(); i++) { |
97 if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n"); | 129 if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n"); |
98 if (i > 0) fprintf(fp_, ","); | 130 if (i > 0) fprintf(fp_, ","); |
99 fprintf(fp_, "%u", static_cast<unsigned char>(blob.at(i))); | 131 fprintf(fp_, "%u", static_cast<unsigned char>(data.at(i))); |
100 } | 132 } |
101 fprintf(fp_, "\n"); | 133 fprintf(fp_, "\n"); |
102 } | 134 } |
103 | 135 |
104 FILE* GetFileDescriptorOrDie(const char* filename) { | 136 FILE* GetFileDescriptorOrDie(const char* filename) { |
105 FILE* fp = base::OS::FOpen(filename, "wb"); | 137 FILE* fp = base::OS::FOpen(filename, "wb"); |
106 if (fp == NULL) { | 138 if (fp == NULL) { |
107 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); | 139 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); |
108 exit(1); | 140 exit(1); |
109 } | 141 } |
110 return fp; | 142 return fp; |
111 } | 143 } |
112 | 144 |
113 FILE* fp_; | 145 FILE* fp_; |
| 146 FILE* raw_file_; |
| 147 FILE* raw_context_file_; |
114 FILE* startup_blob_file_; | 148 FILE* startup_blob_file_; |
115 }; | 149 }; |
116 | 150 |
117 | 151 |
118 void DumpException(Handle<Message> message) { | 152 void DumpException(Handle<Message> message) { |
119 String::Utf8Value message_string(message->Get()); | 153 String::Utf8Value message_string(message->Get()); |
120 String::Utf8Value message_line(message->GetSourceLine()); | 154 String::Utf8Value message_line(message->GetSourceLine()); |
121 fprintf(stderr, "%s at line %d\n", *message_string, message->GetLineNumber()); | 155 fprintf(stderr, "%s at line %d\n", *message_string, message->GetLineNumber()); |
122 fprintf(stderr, "%s\n", *message_line); | 156 fprintf(stderr, "%s\n", *message_line); |
123 for (int i = 0; i <= message->GetEndColumn(); ++i) { | 157 for (int i = 0; i <= message->GetEndColumn(); ++i) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 i::StartupSerializer ser(internal_isolate, &snapshot_sink); | 263 i::StartupSerializer ser(internal_isolate, &snapshot_sink); |
230 ser.SerializeStrongReferences(); | 264 ser.SerializeStrongReferences(); |
231 | 265 |
232 i::SnapshotByteSink context_sink; | 266 i::SnapshotByteSink context_sink; |
233 i::PartialSerializer context_ser(internal_isolate, &ser, &context_sink); | 267 i::PartialSerializer context_ser(internal_isolate, &ser, &context_sink); |
234 context_ser.Serialize(&raw_context); | 268 context_ser.Serialize(&raw_context); |
235 ser.SerializeWeakReferences(); | 269 ser.SerializeWeakReferences(); |
236 | 270 |
237 { | 271 { |
238 SnapshotWriter writer(argv[1]); | 272 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); |
239 if (i::FLAG_startup_blob) | 275 if (i::FLAG_startup_blob) |
240 writer.SetStartupBlobFile(i::FLAG_startup_blob); | 276 writer.SetStartupBlobFile(i::FLAG_startup_blob); |
241 i::SnapshotData sd(snapshot_sink, ser); | 277 i::SnapshotData sd(snapshot_sink, ser); |
242 i::SnapshotData csd(context_sink, context_ser); | 278 i::SnapshotData csd(context_sink, context_ser); |
243 writer.WriteSnapshot(sd, csd); | 279 writer.WriteSnapshot(sd, csd); |
244 } | 280 } |
245 } | 281 } |
246 | 282 |
247 isolate->Dispose(); | 283 isolate->Dispose(); |
248 V8::Dispose(); | 284 V8::Dispose(); |
249 V8::ShutdownPlatform(); | 285 V8::ShutdownPlatform(); |
250 delete platform; | 286 delete platform; |
251 return 0; | 287 return 0; |
252 } | 288 } |
OLD | NEW |