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 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 return counter_table_[counter]; | 102 return counter_table_[counter]; |
103 | 103 |
104 Counter* ctr = counters->GetNextCounter(); | 104 Counter* ctr = counters->GetNextCounter(); |
105 if (ctr == NULL) return NULL; | 105 if (ctr == NULL) return NULL; |
106 int* ptr = ctr->Bind(name); | 106 int* ptr = ctr->Bind(name); |
107 counter_table_[counter] = ptr; | 107 counter_table_[counter] = ptr; |
108 return ptr; | 108 return ptr; |
109 } | 109 } |
110 | 110 |
111 | 111 |
112 class CppByteSink : public i::SnapshotByteSink { | |
113 public: | |
114 explicit CppByteSink(const char* snapshot_file) : bytes_written_(0) { | |
115 fp_ = i::OS::FOpen(snapshot_file, "wb"); | |
116 if (fp_ == NULL) { | |
117 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file); | |
118 exit(1); | |
119 } | |
120 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); | |
121 fprintf(fp_, "#include \"v8.h\"\n"); | |
122 fprintf(fp_, "#include \"platform.h\"\n\n"); | |
123 fprintf(fp_, "#include \"snapshot.h\"\n\n"); | |
124 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n"); | |
125 fprintf(fp_, "const byte Snapshot::data_[] = {"); | |
126 } | |
127 virtual ~CppByteSink() { | |
Mads Ager (chromium)
2009/10/30 10:17:11
Space between the methods?
| |
128 if (fp_ != NULL) { | |
129 fprintf(fp_, "};\n\n"); | |
130 fprintf(fp_, "int Snapshot::size_ = %d;\n\n", bytes_written_); | |
131 fprintf(fp_, "} } // namespace v8::internal\n"); | |
132 fclose(fp_); | |
133 } | |
134 } | |
135 virtual void Put(int byte, const char* description) { | |
136 if (bytes_written_ != 0) { | |
137 fprintf(fp_, ","); | |
138 } | |
139 fprintf(fp_, "%d", byte); | |
140 bytes_written_++; | |
141 if ((bytes_written_ & 0x3f) == 0) { | |
142 fprintf(fp_, "\n"); | |
143 } | |
144 } | |
145 | |
146 private: | |
147 FILE* fp_; | |
148 int bytes_written_; | |
149 }; | |
150 | |
151 | |
112 // Write C++ code that defines Snapshot::snapshot_ to contain the snapshot | 152 // Write C++ code that defines Snapshot::snapshot_ to contain the snapshot |
113 // to the file given by filename. Only the first size chars are written. | 153 // to the file given by filename. Only the first size chars are written. |
114 static int WriteInternalSnapshotToFile(const char* filename, | 154 static int WriteInternalSnapshotToFile(const char* filename, |
115 const v8::internal::byte* bytes, | 155 const v8::internal::byte* bytes, |
116 int size) { | 156 int size) { |
117 FILE* f = i::OS::FOpen(filename, "wb"); | 157 FILE* f = i::OS::FOpen(filename, "wb"); |
118 if (f == NULL) { | 158 if (f == NULL) { |
119 i::OS::PrintError("Cannot open file %s for reading.\n", filename); | 159 i::OS::PrintError("Cannot open file %s for writing.\n", filename); |
120 return 0; | 160 return 0; |
121 } | 161 } |
122 fprintf(f, "// Autogenerated snapshot file. Do not edit.\n\n"); | 162 fprintf(f, "// Autogenerated snapshot file. Do not edit.\n\n"); |
123 fprintf(f, "#include \"v8.h\"\n"); | 163 fprintf(f, "#include \"v8.h\"\n"); |
124 fprintf(f, "#include \"platform.h\"\n\n"); | 164 fprintf(f, "#include \"platform.h\"\n\n"); |
125 fprintf(f, "#include \"snapshot.h\"\n\n"); | 165 fprintf(f, "#include \"snapshot.h\"\n\n"); |
126 fprintf(f, "namespace v8 {\nnamespace internal {\n\n"); | 166 fprintf(f, "namespace v8 {\nnamespace internal {\n\n"); |
127 fprintf(f, "const byte Snapshot::data_[] = {"); | 167 fprintf(f, "const byte Snapshot::data_[] = {"); |
128 int written = 0; | 168 int written = 0; |
129 written += fprintf(f, "0x%x", bytes[0]); | 169 written += fprintf(f, "0x%x", bytes[0]); |
130 for (int i = 1; i < size; ++i) { | 170 for (int i = 1; i < size; ++i) { |
131 written += fprintf(f, ",0x%x", bytes[i]); | 171 written += fprintf(f, ",0x%x", bytes[i]); |
132 // The following is needed to keep the line length low on Visual C++: | 172 // The following is needed to keep the line length low on Visual C++: |
133 if (i % 512 == 0) fprintf(f, "\n"); | 173 if (i % 512 == 0) fprintf(f, "\n"); |
134 } | 174 } |
135 fprintf(f, "};\n\n"); | 175 fprintf(f, "};\n\n"); |
136 fprintf(f, "int Snapshot::size_ = %d;\n\n", size); | 176 fprintf(f, "int Snapshot::size_ = %d;\n\n", size); |
137 fprintf(f, "} } // namespace v8::internal\n"); | 177 fprintf(f, "} } // namespace v8::internal\n"); |
138 fclose(f); | 178 fclose(f); |
139 return written; | 179 return written; |
140 } | 180 } |
141 | 181 |
142 | 182 |
183 int main2(int argc, char** argv) { | |
184 i::Serializer::Enable(); | |
185 Persistent<Context> context = v8::Context::New(); | |
186 // Make sure all builtin scripts are cached. | |
187 { HandleScope scope; | |
188 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { | |
189 i::Bootstrapper::NativesSourceLookup(i); | |
190 } | |
191 } | |
192 context.Dispose(); | |
193 CppByteSink sink(argv[1]); | |
194 i::Serializer2 ser(&sink); | |
195 ser.Serialize(); | |
196 return 0; | |
197 } | |
198 | |
199 | |
143 int main(int argc, char** argv) { | 200 int main(int argc, char** argv) { |
144 #ifdef ENABLE_LOGGING_AND_PROFILING | 201 #ifdef ENABLE_LOGGING_AND_PROFILING |
145 // By default, log code create information in the snapshot. | 202 // By default, log code create information in the snapshot. |
146 i::FLAG_log_code = true; | 203 i::FLAG_log_code = true; |
147 #endif | 204 #endif |
148 // Print the usage if an error occurs when parsing the command line | 205 // Print the usage if an error occurs when parsing the command line |
149 // flags or if the help flag is set. | 206 // flags or if the help flag is set. |
150 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); | 207 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); |
151 if (result > 0 || argc != 2 || i::FLAG_help) { | 208 if (result > 0 || argc != 2 || i::FLAG_help) { |
152 ::printf("Usage: %s [flag] ... outfile\n", argv[0]); | 209 ::printf("Usage: %s [flag] ... outfile\n", argv[0]); |
153 i::FlagList::PrintHelp(); | 210 i::FlagList::PrintHelp(); |
154 return !i::FLAG_help; | 211 return !i::FLAG_help; |
155 } | 212 } |
156 | 213 |
214 if (i::FLAG_new_snapshot) { | |
215 return main2(argc, argv); | |
216 } | |
217 | |
157 v8::V8::SetCounterFunction(counter_callback); | 218 v8::V8::SetCounterFunction(counter_callback); |
158 v8::HandleScope scope; | 219 v8::HandleScope scope; |
159 | 220 |
160 const int kExtensionCount = 1; | 221 const int kExtensionCount = 1; |
161 const char* extension_list[kExtensionCount] = { "v8/gc" }; | 222 const char* extension_list[kExtensionCount] = { "v8/gc" }; |
162 v8::ExtensionConfiguration extensions(kExtensionCount, extension_list); | 223 v8::ExtensionConfiguration extensions(kExtensionCount, extension_list); |
163 | 224 |
164 i::Serializer::Enable(); | 225 i::Serializer::Enable(); |
165 v8::Context::New(&extensions); | 226 v8::Context::New(&extensions); |
166 | 227 |
(...skipping 10 matching lines...) Expand all Loading... | |
177 v8::internal::byte* bytes; | 238 v8::internal::byte* bytes; |
178 int len; | 239 int len; |
179 ser.Finalize(&bytes, &len); | 240 ser.Finalize(&bytes, &len); |
180 | 241 |
181 WriteInternalSnapshotToFile(argv[1], bytes, len); | 242 WriteInternalSnapshotToFile(argv[1], bytes, len); |
182 | 243 |
183 i::DeleteArray(bytes); | 244 i::DeleteArray(bytes); |
184 | 245 |
185 return 0; | 246 return 0; |
186 } | 247 } |
OLD | NEW |