| 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 "include/libplatform/libplatform.h" | 9 #include "include/libplatform/libplatform.h" |
| 10 #include "src/assembler.h" | 10 #include "src/assembler.h" |
| 11 #include "src/base/platform/platform.h" | 11 #include "src/base/platform/platform.h" |
| 12 #include "src/bootstrapper.h" | 12 #include "src/bootstrapper.h" |
| 13 #include "src/flags.h" | 13 #include "src/flags.h" |
| 14 #include "src/list.h" | 14 #include "src/list.h" |
| 15 #include "src/objects-inl.h" // TODO(mstarzinger): Temporary cycle breaker! | 15 #include "src/objects-inl.h" // TODO(mstarzinger): Temporary cycle breaker! |
| 16 #include "src/snapshot/natives.h" | 16 #include "src/snapshot/natives.h" |
| 17 #include "src/snapshot/serialize.h" | 17 #include "src/snapshot/serialize.h" |
| 18 | 18 |
| 19 | 19 |
| 20 using namespace v8; | 20 using namespace v8; |
| 21 | 21 |
| 22 class SnapshotWriter { | 22 class SnapshotWriter { |
| 23 public: | 23 public: |
| 24 explicit SnapshotWriter(const char* snapshot_file) | 24 SnapshotWriter() : fp_(NULL), startup_blob_file_(NULL) {} |
| 25 : fp_(GetFileDescriptorOrDie(snapshot_file)), | |
| 26 startup_blob_file_(NULL) {} | |
| 27 | 25 |
| 28 ~SnapshotWriter() { | 26 ~SnapshotWriter() { |
| 29 fclose(fp_); | 27 if (fp_) fclose(fp_); |
| 30 if (startup_blob_file_) fclose(startup_blob_file_); | 28 if (startup_blob_file_) fclose(startup_blob_file_); |
| 31 } | 29 } |
| 32 | 30 |
| 31 void SetSnapshotFile(const char* snapshot_file) { |
| 32 if (snapshot_file != NULL) fp_ = GetFileDescriptorOrDie(snapshot_file); |
| 33 } |
| 34 |
| 33 void SetStartupBlobFile(const char* startup_blob_file) { | 35 void SetStartupBlobFile(const char* startup_blob_file) { |
| 34 if (startup_blob_file != NULL) | 36 if (startup_blob_file != NULL) |
| 35 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); | 37 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); |
| 36 } | 38 } |
| 37 | 39 |
| 38 void WriteSnapshot(v8::StartupData blob) const { | 40 void WriteSnapshot(v8::StartupData blob) const { |
| 39 i::Vector<const i::byte> blob_vector( | 41 i::Vector<const i::byte> blob_vector( |
| 40 reinterpret_cast<const i::byte*>(blob.data), blob.raw_size); | 42 reinterpret_cast<const i::byte*>(blob.data), blob.raw_size); |
| 41 WriteSnapshotFile(blob_vector); | 43 MaybeWriteSnapshotFile(blob_vector); |
| 42 MaybeWriteStartupBlob(blob_vector); | 44 MaybeWriteStartupBlob(blob_vector); |
| 43 } | 45 } |
| 44 | 46 |
| 45 private: | 47 private: |
| 46 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const { | 48 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const { |
| 47 if (!startup_blob_file_) return; | 49 if (!startup_blob_file_) return; |
| 48 | 50 |
| 49 size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_); | 51 size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_); |
| 50 if (written != static_cast<size_t>(blob.length())) { | 52 if (written != static_cast<size_t>(blob.length())) { |
| 51 i::PrintF("Writing snapshot file failed.. Aborting.\n"); | 53 i::PrintF("Writing snapshot file failed.. Aborting.\n"); |
| 52 exit(1); | 54 exit(1); |
| 53 } | 55 } |
| 54 } | 56 } |
| 55 | 57 |
| 56 void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const { | 58 void MaybeWriteSnapshotFile(const i::Vector<const i::byte>& blob) const { |
| 59 if (!fp_) return; |
| 60 |
| 57 WriteFilePrefix(); | 61 WriteFilePrefix(); |
| 58 WriteData(blob); | 62 WriteData(blob); |
| 59 WriteFileSuffix(); | 63 WriteFileSuffix(); |
| 60 } | 64 } |
| 61 | 65 |
| 62 void WriteFilePrefix() const { | 66 void WriteFilePrefix() const { |
| 63 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); | 67 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); |
| 64 fprintf(fp_, "#include \"src/v8.h\"\n"); | 68 fprintf(fp_, "#include \"src/v8.h\"\n"); |
| 65 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); | 69 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); |
| 66 fprintf(fp_, "#include \"src/snapshot/snapshot.h\"\n\n"); | 70 fprintf(fp_, "#include \"src/snapshot/snapshot.h\"\n\n"); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 139 |
| 136 | 140 |
| 137 int main(int argc, char** argv) { | 141 int main(int argc, char** argv) { |
| 138 // By default, log code create information in the snapshot. | 142 // By default, log code create information in the snapshot. |
| 139 i::FLAG_log_code = true; | 143 i::FLAG_log_code = true; |
| 140 i::FLAG_logfile_per_isolate = false; | 144 i::FLAG_logfile_per_isolate = false; |
| 141 | 145 |
| 142 // Print the usage if an error occurs when parsing the command line | 146 // Print the usage if an error occurs when parsing the command line |
| 143 // flags or if the help flag is set. | 147 // flags or if the help flag is set. |
| 144 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); | 148 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); |
| 145 if (result > 0 || (argc != 2 && argc != 3) || i::FLAG_help) { | 149 if (result > 0 || (argc != 1 && argc != 2) || i::FLAG_help) { |
| 146 ::printf("Usage: %s [flag] ... outfile\n", argv[0]); | 150 ::printf("Usage: %s --startup_src=... --startup_blob=... [extras]\n", |
| 151 argv[0]); |
| 147 i::FlagList::PrintHelp(); | 152 i::FlagList::PrintHelp(); |
| 148 return !i::FLAG_help; | 153 return !i::FLAG_help; |
| 149 } | 154 } |
| 150 | 155 |
| 151 i::CpuFeatures::Probe(true); | 156 i::CpuFeatures::Probe(true); |
| 152 V8::InitializeICU(); | 157 V8::InitializeICU(); |
| 153 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); | 158 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); |
| 154 v8::V8::InitializePlatform(platform); | 159 v8::V8::InitializePlatform(platform); |
| 155 v8::V8::Initialize(); | 160 v8::V8::Initialize(); |
| 156 | 161 |
| 157 { | 162 { |
| 158 SnapshotWriter writer(argv[1]); | 163 SnapshotWriter writer; |
| 164 if (i::FLAG_startup_src) writer.SetSnapshotFile(i::FLAG_startup_src); |
| 159 if (i::FLAG_startup_blob) writer.SetStartupBlobFile(i::FLAG_startup_blob); | 165 if (i::FLAG_startup_blob) writer.SetStartupBlobFile(i::FLAG_startup_blob); |
| 160 char* extra_code = GetExtraCode(argc == 3 ? argv[2] : NULL); | 166 char* extra_code = GetExtraCode(argc == 2 ? argv[1] : NULL); |
| 161 StartupData blob = v8::V8::CreateSnapshotDataBlob(extra_code); | 167 StartupData blob = v8::V8::CreateSnapshotDataBlob(extra_code); |
| 162 CHECK(blob.data); | 168 CHECK(blob.data); |
| 163 writer.WriteSnapshot(blob); | 169 writer.WriteSnapshot(blob); |
| 164 delete[] extra_code; | 170 delete[] extra_code; |
| 165 delete[] blob.data; | 171 delete[] blob.data; |
| 166 } | 172 } |
| 167 | 173 |
| 168 V8::Dispose(); | 174 V8::Dispose(); |
| 169 V8::ShutdownPlatform(); | 175 V8::ShutdownPlatform(); |
| 170 delete platform; | 176 delete platform; |
| 171 return 0; | 177 return 0; |
| 172 } | 178 } |
| OLD | NEW |