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 20 matching lines...) Expand all Loading... |
31 | 31 |
32 #include "api.h" | 32 #include "api.h" |
33 #include "serialize.h" | 33 #include "serialize.h" |
34 #include "snapshot.h" | 34 #include "snapshot.h" |
35 #include "platform.h" | 35 #include "platform.h" |
36 | 36 |
37 namespace v8 { | 37 namespace v8 { |
38 namespace internal { | 38 namespace internal { |
39 | 39 |
40 bool Snapshot::Deserialize(const byte* content, int len) { | 40 bool Snapshot::Deserialize(const byte* content, int len) { |
41 Deserializer des(content, len); | |
42 des.GetFlags(); | |
43 return V8::Initialize(&des); | |
44 } | |
45 | |
46 | |
47 bool Snapshot::Deserialize2(const byte* content, int len) { | |
48 SnapshotByteSource source(content, len); | 41 SnapshotByteSource source(content, len); |
49 Deserializer2 deserializer(&source); | 42 Deserializer deserializer(&source); |
50 return V8::Initialize(&deserializer); | 43 return V8::Initialize(&deserializer); |
51 } | 44 } |
52 | 45 |
53 | 46 |
54 bool Snapshot::Initialize(const char* snapshot_file) { | 47 bool Snapshot::Initialize(const char* snapshot_file) { |
55 if (snapshot_file) { | 48 if (snapshot_file) { |
56 int len; | 49 int len; |
57 byte* str = ReadBytes(snapshot_file, &len); | 50 byte* str = ReadBytes(snapshot_file, &len); |
58 if (!str) return false; | 51 if (!str) return false; |
59 bool result = Deserialize(str, len); | 52 Deserialize(str, len); |
60 DeleteArray(str); | |
61 return result; | |
62 } else if (size_ > 0) { | |
63 return Deserialize(data_, size_); | |
64 } | |
65 return false; | |
66 } | |
67 | |
68 | |
69 bool Snapshot::Initialize2(const char* snapshot_file) { | |
70 if (snapshot_file) { | |
71 int len; | |
72 byte* str = ReadBytes(snapshot_file, &len); | |
73 if (!str) return false; | |
74 Deserialize2(str, len); | |
75 DeleteArray(str); | 53 DeleteArray(str); |
76 return true; | 54 return true; |
77 } else if (size_ > 0) { | 55 } else if (size_ > 0) { |
78 Deserialize2(data_, size_); | 56 Deserialize(data_, size_); |
79 return true; | 57 return true; |
80 } | 58 } |
81 return false; | 59 return false; |
82 } | 60 } |
83 | 61 |
84 | 62 |
85 bool Snapshot::WriteToFile(const char* snapshot_file) { | |
86 Serializer ser; | |
87 ser.Serialize(); | |
88 byte* str; | |
89 int len; | |
90 ser.Finalize(&str, &len); | |
91 | |
92 int written = WriteBytes(snapshot_file, str, len); | |
93 | |
94 DeleteArray(str); | |
95 return written == len; | |
96 } | |
97 | |
98 | |
99 class FileByteSink : public SnapshotByteSink { | 63 class FileByteSink : public SnapshotByteSink { |
100 public: | 64 public: |
101 explicit FileByteSink(const char* snapshot_file) { | 65 explicit FileByteSink(const char* snapshot_file) { |
102 fp_ = OS::FOpen(snapshot_file, "wb"); | 66 fp_ = OS::FOpen(snapshot_file, "wb"); |
103 if (fp_ == NULL) { | 67 if (fp_ == NULL) { |
104 PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file); | 68 PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file); |
105 exit(1); | 69 exit(1); |
106 } | 70 } |
107 } | 71 } |
108 virtual ~FileByteSink() { | 72 virtual ~FileByteSink() { |
109 if (fp_ != NULL) { | 73 if (fp_ != NULL) { |
110 fclose(fp_); | 74 fclose(fp_); |
111 } | 75 } |
112 } | 76 } |
113 virtual void Put(int byte, const char* description) { | 77 virtual void Put(int byte, const char* description) { |
114 if (fp_ != NULL) { | 78 if (fp_ != NULL) { |
115 fputc(byte, fp_); | 79 fputc(byte, fp_); |
116 } | 80 } |
117 } | 81 } |
118 | 82 |
119 private: | 83 private: |
120 FILE* fp_; | 84 FILE* fp_; |
121 }; | 85 }; |
122 | 86 |
123 | 87 |
124 bool Snapshot::WriteToFile2(const char* snapshot_file) { | 88 bool Snapshot::WriteToFile(const char* snapshot_file) { |
125 FileByteSink file(snapshot_file); | 89 FileByteSink file(snapshot_file); |
126 Serializer2 ser(&file); | 90 Serializer ser(&file); |
127 ser.Serialize(); | 91 ser.Serialize(); |
128 return true; | 92 return true; |
129 } | 93 } |
130 | 94 |
131 | 95 |
132 | 96 |
133 } } // namespace v8::internal | 97 } } // namespace v8::internal |
OLD | NEW |