OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/snapshot.h" | 5 #include "vm/snapshot.h" |
6 | 6 |
7 #include "vm/assert.h" | 7 #include "vm/assert.h" |
8 #include "vm/bigint_operations.h" | 8 #include "vm/bigint_operations.h" |
9 #include "vm/class_finalizer.h" | 9 #include "vm/class_finalizer.h" |
10 #include "vm/unit_test.h" | 10 #include "vm/unit_test.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 TEST_CASE(SerializeNull) { | 44 TEST_CASE(SerializeNull) { |
45 // Write snapshot with object content. | 45 // Write snapshot with object content. |
46 uint8_t* buffer; | 46 uint8_t* buffer; |
47 SnapshotWriter writer(false, &buffer, &allocator); | 47 SnapshotWriter writer(false, &buffer, &allocator); |
48 const Object& null_object = Object::Handle(); | 48 const Object& null_object = Object::Handle(); |
49 writer.WriteObject(null_object.raw()); | 49 writer.WriteObject(null_object.raw()); |
50 writer.FinalizeBuffer(); | 50 writer.FinalizeBuffer(); |
51 | 51 |
52 // Create a snapshot object using the buffer. | 52 // Create a snapshot object using the buffer. |
53 Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 53 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
54 | 54 |
55 // Read object back from the snapshot. | 55 // Read object back from the snapshot. |
56 Isolate* isolate= Isolate::Current(); | 56 Isolate* isolate= Isolate::Current(); |
57 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 57 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
58 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 58 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
59 EXPECT(Equals(null_object, serialized_object)); | 59 EXPECT(Equals(null_object, serialized_object)); |
60 } | 60 } |
61 | 61 |
62 | 62 |
63 TEST_CASE(SerializeSmi1) { | 63 TEST_CASE(SerializeSmi1) { |
64 // Write snapshot with object content. | 64 // Write snapshot with object content. |
65 uint8_t* buffer; | 65 uint8_t* buffer; |
66 SnapshotWriter writer(false, &buffer, &allocator); | 66 SnapshotWriter writer(false, &buffer, &allocator); |
67 const Smi& smi = Smi::Handle(Smi::New(124)); | 67 const Smi& smi = Smi::Handle(Smi::New(124)); |
68 writer.WriteObject(smi.raw()); | 68 writer.WriteObject(smi.raw()); |
69 writer.FinalizeBuffer(); | 69 writer.FinalizeBuffer(); |
70 | 70 |
71 // Create a snapshot object using the buffer. | 71 // Create a snapshot object using the buffer. |
72 Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 72 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
73 | 73 |
74 // Read object back from the snapshot. | 74 // Read object back from the snapshot. |
75 Isolate* isolate= Isolate::Current(); | 75 Isolate* isolate= Isolate::Current(); |
76 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 76 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
77 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 77 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
78 EXPECT(Equals(smi, serialized_object)); | 78 EXPECT(Equals(smi, serialized_object)); |
79 } | 79 } |
80 | 80 |
81 | 81 |
82 TEST_CASE(SerializeSmi2) { | 82 TEST_CASE(SerializeSmi2) { |
83 // Write snapshot with object content. | 83 // Write snapshot with object content. |
84 uint8_t* buffer; | 84 uint8_t* buffer; |
85 SnapshotWriter writer(false, &buffer, &allocator); | 85 SnapshotWriter writer(false, &buffer, &allocator); |
86 const Smi& smi = Smi::Handle(Smi::New(-1)); | 86 const Smi& smi = Smi::Handle(Smi::New(-1)); |
87 writer.WriteObject(smi.raw()); | 87 writer.WriteObject(smi.raw()); |
88 writer.FinalizeBuffer(); | 88 writer.FinalizeBuffer(); |
89 | 89 |
90 // Create a snapshot object using the buffer. | 90 // Create a snapshot object using the buffer. |
91 Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 91 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
92 | 92 |
93 // Read object back from the snapshot. | 93 // Read object back from the snapshot. |
94 Isolate* isolate= Isolate::Current(); | 94 Isolate* isolate= Isolate::Current(); |
95 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 95 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
96 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 96 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
97 EXPECT(Equals(smi, serialized_object)); | 97 EXPECT(Equals(smi, serialized_object)); |
98 } | 98 } |
99 | 99 |
100 | 100 |
101 TEST_CASE(SerializeDouble) { | 101 TEST_CASE(SerializeDouble) { |
102 // Write snapshot with object content. | 102 // Write snapshot with object content. |
103 uint8_t* buffer; | 103 uint8_t* buffer; |
104 SnapshotWriter writer(false, &buffer, &allocator); | 104 SnapshotWriter writer(false, &buffer, &allocator); |
105 const Double& dbl = Double::Handle(Double::New(101.29)); | 105 const Double& dbl = Double::Handle(Double::New(101.29)); |
106 writer.WriteObject(dbl.raw()); | 106 writer.WriteObject(dbl.raw()); |
107 writer.FinalizeBuffer(); | 107 writer.FinalizeBuffer(); |
108 | 108 |
109 // Create a snapshot object using the buffer. | 109 // Create a snapshot object using the buffer. |
110 Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 110 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
111 | 111 |
112 // Read object back from the snapshot. | 112 // Read object back from the snapshot. |
113 Isolate* isolate= Isolate::Current(); | 113 Isolate* isolate= Isolate::Current(); |
114 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 114 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
115 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 115 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
116 EXPECT(Equals(dbl, serialized_object)); | 116 EXPECT(Equals(dbl, serialized_object)); |
117 } | 117 } |
118 | 118 |
119 | 119 |
120 TEST_CASE(SerializeBool) { | 120 TEST_CASE(SerializeBool) { |
121 // Write snapshot with object content. | 121 // Write snapshot with object content. |
122 uint8_t* buffer; | 122 uint8_t* buffer; |
123 SnapshotWriter writer(false, &buffer, &allocator); | 123 SnapshotWriter writer(false, &buffer, &allocator); |
124 const Bool& bool1 = Bool::Handle(Bool::True()); | 124 const Bool& bool1 = Bool::Handle(Bool::True()); |
125 const Bool& bool2 = Bool::Handle(Bool::False()); | 125 const Bool& bool2 = Bool::Handle(Bool::False()); |
126 writer.WriteObject(bool1.raw()); | 126 writer.WriteObject(bool1.raw()); |
127 writer.WriteObject(bool2.raw()); | 127 writer.WriteObject(bool2.raw()); |
128 writer.FinalizeBuffer(); | 128 writer.FinalizeBuffer(); |
129 | 129 |
130 // Create a snapshot object using the buffer. | 130 // Create a snapshot object using the buffer. |
131 Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 131 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
132 | 132 |
133 // Read object back from the snapshot. | 133 // Read object back from the snapshot. |
134 Isolate* isolate= Isolate::Current(); | 134 Isolate* isolate= Isolate::Current(); |
135 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 135 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
136 EXPECT(Bool::True() == reader.ReadObject()); | 136 EXPECT(Bool::True() == reader.ReadObject()); |
137 EXPECT(Bool::False() == reader.ReadObject()); | 137 EXPECT(Bool::False() == reader.ReadObject()); |
138 } | 138 } |
139 | 139 |
140 | 140 |
141 TEST_CASE(SerializeBigint) { | 141 TEST_CASE(SerializeBigint) { |
142 // Write snapshot with object content. | 142 // Write snapshot with object content. |
143 uint8_t* buffer; | 143 uint8_t* buffer; |
144 SnapshotWriter writer(false, &buffer, &allocator); | 144 SnapshotWriter writer(false, &buffer, &allocator); |
145 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); | 145 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); |
146 writer.WriteObject(bigint.raw()); | 146 writer.WriteObject(bigint.raw()); |
147 writer.FinalizeBuffer(); | 147 writer.FinalizeBuffer(); |
148 | 148 |
149 // Create a snapshot object using the buffer. | 149 // Create a snapshot object using the buffer. |
150 Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 150 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
151 | 151 |
152 // Read object back from the snapshot. | 152 // Read object back from the snapshot. |
153 Isolate* isolate= Isolate::Current(); | 153 Isolate* isolate= Isolate::Current(); |
154 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 154 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
155 Bigint& obj = Bigint::Handle(); | 155 Bigint& obj = Bigint::Handle(); |
156 obj ^= reader.ReadObject(); | 156 obj ^= reader.ReadObject(); |
157 OS::Print("%lld", BigintOperations::ToInt64(obj)); | 157 OS::Print("%lld", BigintOperations::ToInt64(obj)); |
158 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); | 158 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); |
159 } | 159 } |
160 | 160 |
(...skipping 18 matching lines...) Expand all Loading... |
179 writer.WriteObject(Object::library_class()); | 179 writer.WriteObject(Object::library_class()); |
180 writer.WriteObject(Object::code_class()); | 180 writer.WriteObject(Object::code_class()); |
181 writer.WriteObject(Object::instructions_class()); | 181 writer.WriteObject(Object::instructions_class()); |
182 writer.WriteObject(Object::pc_descriptors_class()); | 182 writer.WriteObject(Object::pc_descriptors_class()); |
183 writer.WriteObject(Object::exception_handlers_class()); | 183 writer.WriteObject(Object::exception_handlers_class()); |
184 writer.WriteObject(Object::context_class()); | 184 writer.WriteObject(Object::context_class()); |
185 writer.WriteObject(Object::context_scope_class()); | 185 writer.WriteObject(Object::context_scope_class()); |
186 writer.FinalizeBuffer(); | 186 writer.FinalizeBuffer(); |
187 | 187 |
188 // Create a snapshot object using the buffer. | 188 // Create a snapshot object using the buffer. |
189 Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 189 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
190 | 190 |
191 // Read object back from the snapshot. | 191 // Read object back from the snapshot. |
192 Isolate* isolate= Isolate::Current(); | 192 Isolate* isolate= Isolate::Current(); |
193 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 193 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
194 EXPECT(Object::class_class() == reader.ReadObject()); | 194 EXPECT(Object::class_class() == reader.ReadObject()); |
195 EXPECT(Object::null_class() == reader.ReadObject()); | 195 EXPECT(Object::null_class() == reader.ReadObject()); |
196 EXPECT(Object::type_class() == reader.ReadObject()); | 196 EXPECT(Object::type_class() == reader.ReadObject()); |
197 EXPECT(Object::parameterized_type_class() == reader.ReadObject()); | 197 EXPECT(Object::parameterized_type_class() == reader.ReadObject()); |
198 EXPECT(Object::type_parameter_class() == reader.ReadObject()); | 198 EXPECT(Object::type_parameter_class() == reader.ReadObject()); |
199 EXPECT(Object::instantiated_type_class() == reader.ReadObject()); | 199 EXPECT(Object::instantiated_type_class() == reader.ReadObject()); |
(...skipping 16 matching lines...) Expand all Loading... |
216 | 216 |
217 TEST_CASE(SerializeString) { | 217 TEST_CASE(SerializeString) { |
218 // Write snapshot with object content. | 218 // Write snapshot with object content. |
219 uint8_t* buffer; | 219 uint8_t* buffer; |
220 SnapshotWriter writer(false, &buffer, &allocator); | 220 SnapshotWriter writer(false, &buffer, &allocator); |
221 String& str = String::Handle(String::New("This string shall be serialized")); | 221 String& str = String::Handle(String::New("This string shall be serialized")); |
222 writer.WriteObject(str.raw()); | 222 writer.WriteObject(str.raw()); |
223 writer.FinalizeBuffer(); | 223 writer.FinalizeBuffer(); |
224 | 224 |
225 // Create a snapshot object using the buffer. | 225 // Create a snapshot object using the buffer. |
226 Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 226 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
227 | 227 |
228 // Read object back from the snapshot. | 228 // Read object back from the snapshot. |
229 Isolate* isolate= Isolate::Current(); | 229 Isolate* isolate= Isolate::Current(); |
230 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 230 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
231 String& serialized_str = String::Handle(); | 231 String& serialized_str = String::Handle(); |
232 serialized_str ^= reader.ReadObject(); | 232 serialized_str ^= reader.ReadObject(); |
233 EXPECT(str.Equals(serialized_str)); | 233 EXPECT(str.Equals(serialized_str)); |
234 } | 234 } |
235 | 235 |
236 | 236 |
237 TEST_CASE(SerializeArray) { | 237 TEST_CASE(SerializeArray) { |
238 // Write snapshot with object content. | 238 // Write snapshot with object content. |
239 uint8_t* buffer; | 239 uint8_t* buffer; |
240 SnapshotWriter writer(false, &buffer, &allocator); | 240 SnapshotWriter writer(false, &buffer, &allocator); |
241 const int kArrayLength = 10; | 241 const int kArrayLength = 10; |
242 Array& array = Array::Handle(Array::New(kArrayLength)); | 242 Array& array = Array::Handle(Array::New(kArrayLength)); |
243 Smi& smi = Smi::Handle(); | 243 Smi& smi = Smi::Handle(); |
244 for (int i = 0; i < kArrayLength; i++) { | 244 for (int i = 0; i < kArrayLength; i++) { |
245 smi ^= Smi::New(i); | 245 smi ^= Smi::New(i); |
246 array.SetAt(i, smi); | 246 array.SetAt(i, smi); |
247 } | 247 } |
248 writer.WriteObject(array.raw()); | 248 writer.WriteObject(array.raw()); |
249 writer.FinalizeBuffer(); | 249 writer.FinalizeBuffer(); |
250 | 250 |
251 // Create a snapshot object using the buffer. | 251 // Create a snapshot object using the buffer. |
252 Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 252 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
253 | 253 |
254 // Read object back from the snapshot. | 254 // Read object back from the snapshot. |
255 Isolate* isolate= Isolate::Current(); | 255 Isolate* isolate= Isolate::Current(); |
256 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 256 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
257 Array& serialized_array = Array::Handle(); | 257 Array& serialized_array = Array::Handle(); |
258 serialized_array ^= reader.ReadObject(); | 258 serialized_array ^= reader.ReadObject(); |
259 EXPECT(array.Equals(serialized_array)); | 259 EXPECT(array.Equals(serialized_array)); |
260 } | 260 } |
261 | 261 |
262 | 262 |
(...skipping 12 matching lines...) Expand all Loading... |
275 lib.Register(); | 275 lib.Register(); |
276 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 276 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
277 | 277 |
278 // Write snapshot with object content. | 278 // Write snapshot with object content. |
279 uint8_t* buffer; | 279 uint8_t* buffer; |
280 SnapshotWriter writer(false, &buffer, &allocator); | 280 SnapshotWriter writer(false, &buffer, &allocator); |
281 writer.WriteObject(script.raw()); | 281 writer.WriteObject(script.raw()); |
282 writer.FinalizeBuffer(); | 282 writer.FinalizeBuffer(); |
283 | 283 |
284 // Create a snapshot object using the buffer. | 284 // Create a snapshot object using the buffer. |
285 Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 285 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
286 | 286 |
287 // Read object back from the snapshot. | 287 // Read object back from the snapshot. |
288 Isolate* isolate= Isolate::Current(); | 288 Isolate* isolate= Isolate::Current(); |
289 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 289 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
290 Script& serialized_script = Script::Handle(); | 290 Script& serialized_script = Script::Handle(); |
291 serialized_script ^= reader.ReadObject(); | 291 serialized_script ^= reader.ReadObject(); |
292 | 292 |
293 // Check if the serialized script object matches the original script. | 293 // Check if the serialized script object matches the original script. |
294 String& str = String::Handle(); | 294 String& str = String::Handle(); |
295 str ^= serialized_script.source(); | 295 str ^= serialized_script.source(); |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 | 458 |
459 Dart_ExitScope(); // Exit the Dart API scope. | 459 Dart_ExitScope(); // Exit the Dart API scope. |
460 } | 460 } |
461 Dart_ShutdownIsolate(); | 461 Dart_ShutdownIsolate(); |
462 | 462 |
463 free(buffer); | 463 free(buffer); |
464 } | 464 } |
465 #endif // TARGET_ARCH_IA32. | 465 #endif // TARGET_ARCH_IA32. |
466 | 466 |
467 } // namespace dart | 467 } // namespace dart |
OLD | NEW |