説明を見る。00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #define _HAS_EXCEPTIONS 0
00030 #include <signal.h>
00031 #include <string>
00032 #include <map>
00033
00034 #include "v8.h"
00035
00036 #include "bootstrapper.h"
00037 #include "natives.h"
00038 #include "platform.h"
00039 #include "serialize.h"
00040
00041
00042 namespace i = v8::internal;
00043 using namespace v8;
00044
00045 static const unsigned int kMaxCounters = 256;
00046
00047
00048 class Counter {
00049 public:
00050 static const int kMaxNameSize = 64;
00051 int32_t* Bind(const wchar_t* name) {
00052 int i;
00053 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) {
00054 name_[i] = static_cast<char>(name[i]);
00055 }
00056 name_[i] = '\0';
00057 return &counter_;
00058 }
00059 private:
00060 int32_t counter_;
00061 uint8_t name_[kMaxNameSize];
00062 };
00063
00064
00065
00066
00067
00068 class CounterCollection {
00069 public:
00070 CounterCollection() {
00071 magic_number_ = 0xDEADFACE;
00072 max_counters_ = kMaxCounters;
00073 max_name_size_ = Counter::kMaxNameSize;
00074 counters_in_use_ = 0;
00075 }
00076 Counter* GetNextCounter() {
00077 if (counters_in_use_ == kMaxCounters) return NULL;
00078 return &counters_[counters_in_use_++];
00079 }
00080 private:
00081 uint32_t magic_number_;
00082 uint32_t max_counters_;
00083 uint32_t max_name_size_;
00084 uint32_t counters_in_use_;
00085 Counter counters_[kMaxCounters];
00086 };
00087
00088
00089
00090
00091 static CounterCollection local_counters;
00092 static CounterCollection* counters = &local_counters;
00093
00094
00095 typedef std::map<std::wstring, int*> CounterMap;
00096 typedef std::map<std::wstring, int*>::iterator CounterMapIterator;
00097 static CounterMap counter_table_;
00098
00099
00100 static int* counter_callback(const wchar_t* name) {
00101 std::wstring counter = name;
00102
00103 if (counter_table_.find(counter) != counter_table_.end())
00104 return counter_table_[counter];
00105
00106 Counter* ctr = counters->GetNextCounter();
00107 if (ctr == NULL) return NULL;
00108 int* ptr = ctr->Bind(name);
00109 counter_table_[counter] = ptr;
00110 return ptr;
00111 }
00112
00113
00114
00115
00116 static int WriteInternalSnapshotToFile(const char* filename,
00117 const char* str,
00118 int size) {
00119 FILE* f = i::OS::FOpen(filename, "wb");
00120 if (f == NULL) {
00121 i::OS::PrintError("Cannot open file %s for reading.\n", filename);
00122 return 0;
00123 }
00124 fprintf(f, "// Autogenerated snapshot file. Do not edit.\n\n");
00125 fprintf(f, "#include \"v8.h\"\n");
00126 fprintf(f, "#include \"platform.h\"\n\n");
00127 fprintf(f, "#include \"snapshot.h\"\n\n");
00128 fprintf(f, "namespace v8 {\nnamespace internal {\n\n");
00129 fprintf(f, "const char Snapshot::data_[] = {");
00130 int written = 0;
00131 written += fprintf(f, "%i", str[0]);
00132 for (int i = 1; i < size; ++i) {
00133 written += fprintf(f, ",%i", str[i]);
00134
00135 if (i % 512 == 0) fprintf(f, "\n");
00136 }
00137 fprintf(f, "};\n\n");
00138 fprintf(f, "int Snapshot::size_ = %d;\n\n", size);
00139 fprintf(f, "} } // namespace v8::internal\n");
00140 fclose(f);
00141 return written;
00142 }
00143
00144
00145 int main(int argc, char** argv) {
00146 #ifdef ENABLE_LOGGING_AND_PROFILING
00147
00148 i::FLAG_log_code = true;
00149 #endif
00150
00151
00152 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
00153 if (result > 0 || argc != 2 || i::FLAG_h) {
00154 ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
00155 i::FlagList::PrintHelp();
00156 return !i::FLAG_h;
00157 }
00158
00159 v8::V8::SetCounterFunction(counter_callback);
00160 v8::HandleScope scope;
00161
00162 const int kExtensionCount = 1;
00163 const char* extension_list[kExtensionCount] = { "v8/gc" };
00164 v8::ExtensionConfiguration extensions(kExtensionCount, extension_list);
00165 v8::Context::New(&extensions);
00166
00167
00168 { HandleScope scope;
00169 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) {
00170 i::Bootstrapper::NativesSourceLookup(i);
00171 }
00172 }
00173
00174 i::Heap::CollectAllGarbage();
00175 i::Serializer ser;
00176 ser.Serialize();
00177 char* str;
00178 int len;
00179 ser.Finalize(&str, &len);
00180
00181 WriteInternalSnapshotToFile(argv[1], str, len);
00182
00183 i::DeleteArray(str);
00184
00185 return 0;
00186 }