| 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/flags.h" | 12 #include "src/flags.h" |
| 13 #include "src/list.h" | 13 #include "src/list.h" |
| 14 #include "src/snapshot/natives.h" | 14 #include "src/snapshot/natives.h" |
| 15 #include "src/snapshot/partial-serializer.h" | 15 #include "src/snapshot/partial-serializer.h" |
| 16 #include "src/snapshot/startup-serializer.h" | 16 #include "src/snapshot/startup-serializer.h" |
| 17 | 17 |
| 18 using namespace v8; | 18 using namespace v8; |
| 19 | 19 |
| 20 class SnapshotWriter { | 20 class SnapshotWriter { |
| 21 public: | 21 public: |
| 22 SnapshotWriter() : fp_(NULL), startup_blob_file_(NULL) {} | 22 SnapshotWriter() : snapshot_cpp_path_(NULL), snapshot_blob_path_(NULL) {} |
| 23 | 23 |
| 24 ~SnapshotWriter() { | 24 void SetSnapshotFile(const char* snapshot_cpp_file) { |
| 25 if (fp_) fclose(fp_); | 25 snapshot_cpp_path_ = snapshot_cpp_file; |
| 26 if (startup_blob_file_) fclose(startup_blob_file_); | |
| 27 } | 26 } |
| 28 | 27 |
| 29 void SetSnapshotFile(const char* snapshot_file) { | 28 void SetStartupBlobFile(const char* snapshot_blob_file) { |
| 30 if (snapshot_file != NULL) fp_ = GetFileDescriptorOrDie(snapshot_file); | 29 snapshot_blob_path_ = snapshot_blob_file; |
| 31 } | |
| 32 | |
| 33 void SetStartupBlobFile(const char* startup_blob_file) { | |
| 34 if (startup_blob_file != NULL) | |
| 35 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); | |
| 36 } | 30 } |
| 37 | 31 |
| 38 void WriteSnapshot(v8::StartupData blob) const { | 32 void WriteSnapshot(v8::StartupData blob) const { |
| 33 // TODO(crbug/633159): if we crash before the files have been fully created, |
| 34 // we end up with a corrupted snapshot file. The build step would succeed, |
| 35 // but the build target is unusable. Ideally we would write out temporary |
| 36 // files and only move them to the final destination as last step. |
| 39 i::Vector<const i::byte> blob_vector( | 37 i::Vector<const i::byte> blob_vector( |
| 40 reinterpret_cast<const i::byte*>(blob.data), blob.raw_size); | 38 reinterpret_cast<const i::byte*>(blob.data), blob.raw_size); |
| 41 MaybeWriteSnapshotFile(blob_vector); | 39 MaybeWriteSnapshotFile(blob_vector); |
| 42 MaybeWriteStartupBlob(blob_vector); | 40 MaybeWriteStartupBlob(blob_vector); |
| 43 } | 41 } |
| 44 | 42 |
| 45 private: | 43 private: |
| 46 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const { | 44 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const { |
| 47 if (!startup_blob_file_) return; | 45 if (!snapshot_blob_path_) return; |
| 48 | 46 |
| 49 size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_); | 47 FILE* fp = GetFileDescriptorOrDie(snapshot_blob_path_); |
| 48 size_t written = fwrite(blob.begin(), 1, blob.length(), fp); |
| 49 fclose(fp); |
| 50 if (written != static_cast<size_t>(blob.length())) { | 50 if (written != static_cast<size_t>(blob.length())) { |
| 51 i::PrintF("Writing snapshot file failed.. Aborting.\n"); | 51 i::PrintF("Writing snapshot file failed.. Aborting.\n"); |
| 52 remove(snapshot_blob_path_); |
| 52 exit(1); | 53 exit(1); |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 | 56 |
| 56 void MaybeWriteSnapshotFile(const i::Vector<const i::byte>& blob) const { | 57 void MaybeWriteSnapshotFile(const i::Vector<const i::byte>& blob) const { |
| 57 if (!fp_) return; | 58 if (!snapshot_cpp_path_) return; |
| 58 | 59 |
| 59 WriteFilePrefix(); | 60 FILE* fp = GetFileDescriptorOrDie(snapshot_cpp_path_); |
| 60 WriteData(blob); | 61 |
| 61 WriteFileSuffix(); | 62 WriteFilePrefix(fp); |
| 63 WriteData(fp, blob); |
| 64 WriteFileSuffix(fp); |
| 65 |
| 66 fclose(fp); |
| 62 } | 67 } |
| 63 | 68 |
| 64 void WriteFilePrefix() const { | 69 static void WriteFilePrefix(FILE* fp) { |
| 65 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); | 70 fprintf(fp, "// Autogenerated snapshot file. Do not edit.\n\n"); |
| 66 fprintf(fp_, "#include \"src/v8.h\"\n"); | 71 fprintf(fp, "#include \"src/v8.h\"\n"); |
| 67 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); | 72 fprintf(fp, "#include \"src/base/platform/platform.h\"\n\n"); |
| 68 fprintf(fp_, "#include \"src/snapshot/snapshot.h\"\n\n"); | 73 fprintf(fp, "#include \"src/snapshot/snapshot.h\"\n\n"); |
| 69 fprintf(fp_, "namespace v8 {\n"); | 74 fprintf(fp, "namespace v8 {\n"); |
| 70 fprintf(fp_, "namespace internal {\n\n"); | 75 fprintf(fp, "namespace internal {\n\n"); |
| 71 } | 76 } |
| 72 | 77 |
| 73 void WriteFileSuffix() const { | 78 static void WriteFileSuffix(FILE* fp) { |
| 74 fprintf(fp_, "const v8::StartupData* Snapshot::DefaultSnapshotBlob() {\n"); | 79 fprintf(fp, "const v8::StartupData* Snapshot::DefaultSnapshotBlob() {\n"); |
| 75 fprintf(fp_, " return &blob;\n"); | 80 fprintf(fp, " return &blob;\n"); |
| 76 fprintf(fp_, "}\n\n"); | 81 fprintf(fp, "}\n\n"); |
| 77 fprintf(fp_, "} // namespace internal\n"); | 82 fprintf(fp, "} // namespace internal\n"); |
| 78 fprintf(fp_, "} // namespace v8\n"); | 83 fprintf(fp, "} // namespace v8\n"); |
| 79 } | 84 } |
| 80 | 85 |
| 81 void WriteData(const i::Vector<const i::byte>& blob) const { | 86 static void WriteData(FILE* fp, const i::Vector<const i::byte>& blob) { |
| 82 fprintf(fp_, "static const byte blob_data[] = {\n"); | 87 fprintf(fp, "static const byte blob_data[] = {\n"); |
| 83 WriteSnapshotData(blob); | 88 WriteSnapshotData(fp, blob); |
| 84 fprintf(fp_, "};\n"); | 89 fprintf(fp, "};\n"); |
| 85 fprintf(fp_, "static const int blob_size = %d;\n", blob.length()); | 90 fprintf(fp, "static const int blob_size = %d;\n", blob.length()); |
| 86 fprintf(fp_, "static const v8::StartupData blob =\n"); | 91 fprintf(fp, "static const v8::StartupData blob =\n"); |
| 87 fprintf(fp_, "{ (const char*) blob_data, blob_size };\n"); | 92 fprintf(fp, "{ (const char*) blob_data, blob_size };\n"); |
| 88 } | 93 } |
| 89 | 94 |
| 90 void WriteSnapshotData(const i::Vector<const i::byte>& blob) const { | 95 static void WriteSnapshotData(FILE* fp, |
| 96 const i::Vector<const i::byte>& blob) { |
| 91 for (int i = 0; i < blob.length(); i++) { | 97 for (int i = 0; i < blob.length(); i++) { |
| 92 if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n"); | 98 if ((i & 0x1f) == 0x1f) fprintf(fp, "\n"); |
| 93 if (i > 0) fprintf(fp_, ","); | 99 if (i > 0) fprintf(fp, ","); |
| 94 fprintf(fp_, "%u", static_cast<unsigned char>(blob.at(i))); | 100 fprintf(fp, "%u", static_cast<unsigned char>(blob.at(i))); |
| 95 } | 101 } |
| 96 fprintf(fp_, "\n"); | 102 fprintf(fp, "\n"); |
| 97 } | 103 } |
| 98 | 104 |
| 99 FILE* GetFileDescriptorOrDie(const char* filename) { | 105 static FILE* GetFileDescriptorOrDie(const char* filename) { |
| 100 FILE* fp = base::OS::FOpen(filename, "wb"); | 106 FILE* fp = base::OS::FOpen(filename, "wb"); |
| 101 if (fp == NULL) { | 107 if (fp == NULL) { |
| 102 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); | 108 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); |
| 103 exit(1); | 109 exit(1); |
| 104 } | 110 } |
| 105 return fp; | 111 return fp; |
| 106 } | 112 } |
| 107 | 113 |
| 108 FILE* fp_; | 114 const char* snapshot_cpp_path_; |
| 109 FILE* startup_blob_file_; | 115 const char* snapshot_blob_path_; |
| 110 }; | 116 }; |
| 111 | 117 |
| 112 char* GetExtraCode(char* filename, const char* description) { | 118 char* GetExtraCode(char* filename, const char* description) { |
| 113 if (filename == NULL || strlen(filename) == 0) return NULL; | 119 if (filename == NULL || strlen(filename) == 0) return NULL; |
| 114 ::printf("Loading script for %s: %s\n", description, filename); | 120 ::printf("Loading script for %s: %s\n", description, filename); |
| 115 FILE* file = base::OS::FOpen(filename, "rb"); | 121 FILE* file = base::OS::FOpen(filename, "rb"); |
| 116 if (file == NULL) { | 122 if (file == NULL) { |
| 117 fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno); | 123 fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno); |
| 118 exit(1); | 124 exit(1); |
| 119 } | 125 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 CHECK(blob.data); | 181 CHECK(blob.data); |
| 176 writer.WriteSnapshot(blob); | 182 writer.WriteSnapshot(blob); |
| 177 delete[] blob.data; | 183 delete[] blob.data; |
| 178 } | 184 } |
| 179 | 185 |
| 180 V8::Dispose(); | 186 V8::Dispose(); |
| 181 V8::ShutdownPlatform(); | 187 V8::ShutdownPlatform(); |
| 182 delete platform; | 188 delete platform; |
| 183 return 0; | 189 return 0; |
| 184 } | 190 } |
| OLD | NEW |