Chromium Code Reviews| 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), | 27 raw_file_(NULL), |
| 28 raw_context_file_(NULL), | |
| 29 startup_blob_file_(NULL) {} | 28 startup_blob_file_(NULL) {} |
| 30 | 29 |
| 31 ~SnapshotWriter() { | 30 ~SnapshotWriter() { |
| 32 fclose(fp_); | 31 fclose(fp_); |
| 33 if (raw_file_) fclose(raw_file_); | 32 if (raw_file_) fclose(raw_file_); |
| 34 if (raw_context_file_) fclose(raw_context_file_); | |
| 35 if (startup_blob_file_) fclose(startup_blob_file_); | 33 if (startup_blob_file_) fclose(startup_blob_file_); |
| 36 } | 34 } |
| 37 | 35 |
| 38 void SetRawFiles(const char* raw_file, const char* raw_context_file) { | 36 void SetRawFiles(const char* raw_file) { |
| 39 raw_file_ = GetFileDescriptorOrDie(raw_file); | 37 raw_file_ = GetFileDescriptorOrDie(raw_file); |
| 40 raw_context_file_ = GetFileDescriptorOrDie(raw_context_file); | |
| 41 } | 38 } |
| 42 | 39 |
| 43 void SetStartupBlobFile(const char* startup_blob_file) { | 40 void SetStartupBlobFile(const char* startup_blob_file) { |
| 44 if (startup_blob_file != NULL) | 41 if (startup_blob_file != NULL) |
| 45 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); | 42 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); |
| 46 } | 43 } |
| 47 | 44 |
| 48 void WriteSnapshot(const i::SnapshotData& sd, | 45 void WriteSnapshot(const i::SnapshotData& sd, |
| 49 const i::SnapshotData& csd) const { | 46 const i::SnapshotData& csd) const { |
| 50 WriteSnapshotFile(sd, csd); | 47 i::SnapshotByteSink sink; |
| 51 MaybeWriteStartupBlob(sd, csd); | 48 sink.PutBlob(sd.RawData(), "startup"); |
| 49 sink.PutBlob(csd.RawData(), "context"); | |
| 50 const i::Vector<const i::byte>& blob = sink.data().ToConstVector(); | |
| 51 | |
| 52 WriteSnapshotFile(blob); | |
| 53 MaybeWriteRawFile(blob); | |
| 54 MaybeWriteStartupBlob(blob); | |
|
vogelheim1
2014/12/09 13:26:50
If we have one format for both, why would we need
Yang
2014/12/09 14:40:37
Good point. I removed the --raw-file option altoge
| |
| 52 } | 55 } |
| 53 | 56 |
| 54 private: | 57 private: |
| 55 void MaybeWriteStartupBlob(const i::SnapshotData& sd, | 58 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const { |
| 56 const i::SnapshotData& csd) const { | |
| 57 if (!startup_blob_file_) return; | 59 if (!startup_blob_file_) return; |
| 58 | 60 |
| 59 i::SnapshotByteSink sink; | 61 size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_); |
| 60 | 62 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"); | 63 i::PrintF("Writing snapshot file failed.. Aborting.\n"); |
| 69 exit(1); | 64 exit(1); |
| 70 } | 65 } |
| 71 } | 66 } |
| 72 | 67 |
| 73 void WriteSnapshotFile(const i::SnapshotData& snapshot_data, | 68 void MaybeWriteRawFile(const i::Vector<const i::byte>& blob) const { |
|
vogelheim1
2014/12/09 13:26:50
The two MaybeWrite* methods are identical except f
Yang
2014/12/09 14:40:37
Done.
| |
| 74 const i::SnapshotData& context_snapshot_data) const { | 69 if (!raw_file_) return; |
| 70 | |
| 71 size_t written = fwrite(blob.begin(), 1, blob.length(), raw_file_); | |
| 72 if (written != static_cast<size_t>(blob.length())) { | |
| 73 i::PrintF("Writing raw file failed.. Aborting.\n"); | |
| 74 exit(1); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const { | |
| 75 WriteFilePrefix(); | 79 WriteFilePrefix(); |
| 76 WriteData("", snapshot_data.RawData(), raw_file_); | 80 WriteData(blob); |
| 77 WriteData("context_", context_snapshot_data.RawData(), raw_context_file_); | |
| 78 WriteFileSuffix(); | 81 WriteFileSuffix(); |
| 79 } | 82 } |
| 80 | 83 |
| 81 void WriteFilePrefix() const { | 84 void WriteFilePrefix() const { |
| 82 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); | 85 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); |
| 83 fprintf(fp_, "#include \"src/v8.h\"\n"); | 86 fprintf(fp_, "#include \"src/v8.h\"\n"); |
| 84 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); | 87 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); |
| 85 fprintf(fp_, "#include \"src/snapshot.h\"\n\n"); | 88 fprintf(fp_, "#include \"src/snapshot.h\"\n\n"); |
| 86 fprintf(fp_, "namespace v8 {\n"); | 89 fprintf(fp_, "namespace v8 {\n"); |
| 87 fprintf(fp_, "namespace internal {\n\n"); | 90 fprintf(fp_, "namespace internal {\n\n"); |
| 88 } | 91 } |
| 89 | 92 |
| 90 void WriteFileSuffix() const { | 93 void WriteFileSuffix() const { |
| 94 fprintf(fp_, "const v8::StartupData Snapshot::SnapshotBlob() {\n"); | |
| 95 fprintf(fp_, " v8::StartupData blob;\n"); | |
| 96 fprintf(fp_, " blob.data = reinterpret_cast<const char*>(blob_data);\n"); | |
| 97 fprintf(fp_, " blob.raw_size = blob_size;\n"); | |
| 98 fprintf(fp_, " return blob;\n"); | |
| 99 fprintf(fp_, "}\n\n"); | |
| 91 fprintf(fp_, "} // namespace internal\n"); | 100 fprintf(fp_, "} // namespace internal\n"); |
| 92 fprintf(fp_, "} // namespace v8\n"); | 101 fprintf(fp_, "} // namespace v8\n"); |
| 93 } | 102 } |
| 94 | 103 |
| 95 void WriteData(const char* prefix, | 104 void WriteData(const i::Vector<const i::byte>& blob) const { |
| 96 const i::Vector<const i::byte>& source_data, | 105 fprintf(fp_, "static const byte blob_data[] = {\n"); |
| 97 FILE* raw_file) const { | 106 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"); | 107 fprintf(fp_, "};\n"); |
| 122 fprintf(fp_, "const int Snapshot::%ssize_ = %d;\n", prefix, | 108 fprintf(fp_, "static const int blob_size = %d;\n", blob.length()); |
| 123 source_data.length()); | |
| 124 fprintf(fp_, "\n"); | 109 fprintf(fp_, "\n"); |
| 125 } | 110 } |
| 126 | 111 |
| 127 void WriteSnapshotData(const i::Vector<const i::byte>& data) const { | 112 void WriteSnapshotData(const i::Vector<const i::byte>& blob) const { |
| 128 for (int i = 0; i < data.length(); i++) { | 113 for (int i = 0; i < blob.length(); i++) { |
| 129 if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n"); | 114 if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n"); |
| 130 if (i > 0) fprintf(fp_, ","); | 115 if (i > 0) fprintf(fp_, ","); |
| 131 fprintf(fp_, "%u", static_cast<unsigned char>(data.at(i))); | 116 fprintf(fp_, "%u", static_cast<unsigned char>(blob.at(i))); |
| 132 } | 117 } |
| 133 fprintf(fp_, "\n"); | 118 fprintf(fp_, "\n"); |
| 134 } | 119 } |
| 135 | 120 |
| 136 FILE* GetFileDescriptorOrDie(const char* filename) { | 121 FILE* GetFileDescriptorOrDie(const char* filename) { |
| 137 FILE* fp = base::OS::FOpen(filename, "wb"); | 122 FILE* fp = base::OS::FOpen(filename, "wb"); |
| 138 if (fp == NULL) { | 123 if (fp == NULL) { |
| 139 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); | 124 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); |
| 140 exit(1); | 125 exit(1); |
| 141 } | 126 } |
| 142 return fp; | 127 return fp; |
| 143 } | 128 } |
| 144 | 129 |
| 145 FILE* fp_; | 130 FILE* fp_; |
| 146 FILE* raw_file_; | 131 FILE* raw_file_; |
| 147 FILE* raw_context_file_; | |
| 148 FILE* startup_blob_file_; | 132 FILE* startup_blob_file_; |
| 149 }; | 133 }; |
| 150 | 134 |
| 151 | 135 |
| 152 void DumpException(Handle<Message> message) { | 136 void DumpException(Handle<Message> message) { |
| 153 String::Utf8Value message_string(message->Get()); | 137 String::Utf8Value message_string(message->Get()); |
| 154 String::Utf8Value message_line(message->GetSourceLine()); | 138 String::Utf8Value message_line(message->GetSourceLine()); |
| 155 fprintf(stderr, "%s at line %d\n", *message_string, message->GetLineNumber()); | 139 fprintf(stderr, "%s at line %d\n", *message_string, message->GetLineNumber()); |
| 156 fprintf(stderr, "%s\n", *message_line); | 140 fprintf(stderr, "%s\n", *message_line); |
| 157 for (int i = 0; i <= message->GetEndColumn(); ++i) { | 141 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); | 247 i::StartupSerializer ser(internal_isolate, &snapshot_sink); |
| 264 ser.SerializeStrongReferences(); | 248 ser.SerializeStrongReferences(); |
| 265 | 249 |
| 266 i::SnapshotByteSink context_sink; | 250 i::SnapshotByteSink context_sink; |
| 267 i::PartialSerializer context_ser(internal_isolate, &ser, &context_sink); | 251 i::PartialSerializer context_ser(internal_isolate, &ser, &context_sink); |
| 268 context_ser.Serialize(&raw_context); | 252 context_ser.Serialize(&raw_context); |
| 269 ser.SerializeWeakReferences(); | 253 ser.SerializeWeakReferences(); |
| 270 | 254 |
| 271 { | 255 { |
| 272 SnapshotWriter writer(argv[1]); | 256 SnapshotWriter writer(argv[1]); |
| 273 if (i::FLAG_raw_file && i::FLAG_raw_context_file) | 257 if (i::FLAG_raw_file) writer.SetRawFiles(i::FLAG_raw_file); |
| 274 writer.SetRawFiles(i::FLAG_raw_file, i::FLAG_raw_context_file); | |
| 275 if (i::FLAG_startup_blob) | 258 if (i::FLAG_startup_blob) |
| 276 writer.SetStartupBlobFile(i::FLAG_startup_blob); | 259 writer.SetStartupBlobFile(i::FLAG_startup_blob); |
| 277 i::SnapshotData sd(snapshot_sink, ser); | 260 i::SnapshotData sd(snapshot_sink, ser); |
| 278 i::SnapshotData csd(context_sink, context_ser); | 261 i::SnapshotData csd(context_sink, context_ser); |
| 279 writer.WriteSnapshot(sd, csd); | 262 writer.WriteSnapshot(sd, csd); |
| 280 } | 263 } |
| 281 } | 264 } |
| 282 | 265 |
| 283 isolate->Dispose(); | 266 isolate->Dispose(); |
| 284 V8::Dispose(); | 267 V8::Dispose(); |
| 285 V8::ShutdownPlatform(); | 268 V8::ShutdownPlatform(); |
| 286 delete platform; | 269 delete platform; |
| 287 return 0; | 270 return 0; |
| 288 } | 271 } |
| OLD | NEW |