OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 29 matching lines...) Expand all Loading... |
40 i::FLAG_harmony_observation = true; | 40 i::FLAG_harmony_observation = true; |
41 isolate_ = Isolate::New(); | 41 isolate_ = Isolate::New(); |
42 isolate_->Enter(); | 42 isolate_->Enter(); |
43 } | 43 } |
44 | 44 |
45 ~HarmonyIsolate() { | 45 ~HarmonyIsolate() { |
46 isolate_->Exit(); | 46 isolate_->Exit(); |
47 isolate_->Dispose(); | 47 isolate_->Dispose(); |
48 } | 48 } |
49 | 49 |
| 50 Isolate* GetIsolate() const { return isolate_; } |
| 51 |
50 private: | 52 private: |
51 Isolate* isolate_; | 53 Isolate* isolate_; |
52 }; | 54 }; |
53 } | 55 } |
54 | 56 |
55 TEST(PerIsolateState) { | 57 TEST(PerIsolateState) { |
56 HarmonyIsolate isolate; | 58 HarmonyIsolate isolate; |
57 HandleScope scope; | 59 HandleScope scope(isolate.GetIsolate()); |
58 LocalContext context1; | 60 LocalContext context1; |
59 CompileRun( | 61 CompileRun( |
60 "var count = 0;" | 62 "var count = 0;" |
61 "var calls = 0;" | 63 "var calls = 0;" |
62 "var observer = function(records) { count = records.length; calls++ };" | 64 "var observer = function(records) { count = records.length; calls++ };" |
63 "var obj = {};" | 65 "var obj = {};" |
64 "Object.observe(obj, observer);"); | 66 "Object.observe(obj, observer);"); |
65 Handle<Value> observer = CompileRun("observer"); | 67 Handle<Value> observer = CompileRun("observer"); |
66 Handle<Value> obj = CompileRun("obj"); | 68 Handle<Value> obj = CompileRun("obj"); |
67 Handle<Value> notify_fun1 = CompileRun( | 69 Handle<Value> notify_fun1 = CompileRun( |
(...skipping 19 matching lines...) Expand all Loading... |
87 context4->Global()->Set(String::New("fun2"), notify_fun2); | 89 context4->Global()->Set(String::New("fun2"), notify_fun2); |
88 context4->Global()->Set(String::New("fun3"), notify_fun3); | 90 context4->Global()->Set(String::New("fun3"), notify_fun3); |
89 CompileRun("fun1(); fun2(); fun3(); Object.deliverChangeRecords(observer)"); | 91 CompileRun("fun1(); fun2(); fun3(); Object.deliverChangeRecords(observer)"); |
90 } | 92 } |
91 CHECK_EQ(1, CompileRun("calls")->Int32Value()); | 93 CHECK_EQ(1, CompileRun("calls")->Int32Value()); |
92 CHECK_EQ(3, CompileRun("count")->Int32Value()); | 94 CHECK_EQ(3, CompileRun("count")->Int32Value()); |
93 } | 95 } |
94 | 96 |
95 TEST(EndOfMicrotaskDelivery) { | 97 TEST(EndOfMicrotaskDelivery) { |
96 HarmonyIsolate isolate; | 98 HarmonyIsolate isolate; |
97 HandleScope scope; | 99 HandleScope scope(isolate.GetIsolate()); |
98 LocalContext context; | 100 LocalContext context; |
99 CompileRun( | 101 CompileRun( |
100 "var obj = {};" | 102 "var obj = {};" |
101 "var count = 0;" | 103 "var count = 0;" |
102 "var observer = function(records) { count = records.length };" | 104 "var observer = function(records) { count = records.length };" |
103 "Object.observe(obj, observer);" | 105 "Object.observe(obj, observer);" |
104 "obj.foo = 'bar';"); | 106 "obj.foo = 'bar';"); |
105 CHECK_EQ(1, CompileRun("count")->Int32Value()); | 107 CHECK_EQ(1, CompileRun("count")->Int32Value()); |
106 } | 108 } |
107 | 109 |
108 TEST(DeliveryOrdering) { | 110 TEST(DeliveryOrdering) { |
109 HarmonyIsolate isolate; | 111 HarmonyIsolate isolate; |
110 HandleScope scope; | 112 HandleScope scope(isolate.GetIsolate()); |
111 LocalContext context; | 113 LocalContext context; |
112 CompileRun( | 114 CompileRun( |
113 "var obj1 = {};" | 115 "var obj1 = {};" |
114 "var obj2 = {};" | 116 "var obj2 = {};" |
115 "var ordering = [];" | 117 "var ordering = [];" |
116 "function observer2() { ordering.push(2); };" | 118 "function observer2() { ordering.push(2); };" |
117 "function observer1() { ordering.push(1); };" | 119 "function observer1() { ordering.push(1); };" |
118 "function observer3() { ordering.push(3); };" | 120 "function observer3() { ordering.push(3); };" |
119 "Object.observe(obj1, observer1);" | 121 "Object.observe(obj1, observer1);" |
120 "Object.observe(obj1, observer2);" | 122 "Object.observe(obj1, observer2);" |
(...skipping 10 matching lines...) Expand all Loading... |
131 "Object.observe(obj2, observer1);" | 133 "Object.observe(obj2, observer1);" |
132 "obj2.foo = 'baz'"); | 134 "obj2.foo = 'baz'"); |
133 CHECK_EQ(3, CompileRun("ordering.length")->Int32Value()); | 135 CHECK_EQ(3, CompileRun("ordering.length")->Int32Value()); |
134 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value()); | 136 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value()); |
135 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); | 137 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); |
136 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value()); | 138 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value()); |
137 } | 139 } |
138 | 140 |
139 TEST(DeliveryOrderingReentrant) { | 141 TEST(DeliveryOrderingReentrant) { |
140 HarmonyIsolate isolate; | 142 HarmonyIsolate isolate; |
141 HandleScope scope; | 143 HandleScope scope(isolate.GetIsolate()); |
142 LocalContext context; | 144 LocalContext context; |
143 CompileRun( | 145 CompileRun( |
144 "var obj = {};" | 146 "var obj = {};" |
145 "var reentered = false;" | 147 "var reentered = false;" |
146 "var ordering = [];" | 148 "var ordering = [];" |
147 "function observer1() { ordering.push(1); };" | 149 "function observer1() { ordering.push(1); };" |
148 "function observer2() {" | 150 "function observer2() {" |
149 " if (!reentered) {" | 151 " if (!reentered) {" |
150 " obj.foo = 'baz';" | 152 " obj.foo = 'baz';" |
151 " reentered = true;" | 153 " reentered = true;" |
(...skipping 10 matching lines...) Expand all Loading... |
162 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); | 164 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); |
163 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value()); | 165 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value()); |
164 // Note that we re-deliver to observers 1 and 2, while observer3 | 166 // Note that we re-deliver to observers 1 and 2, while observer3 |
165 // already received the second record during the first round. | 167 // already received the second record during the first round. |
166 CHECK_EQ(1, CompileRun("ordering[3]")->Int32Value()); | 168 CHECK_EQ(1, CompileRun("ordering[3]")->Int32Value()); |
167 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); | 169 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); |
168 } | 170 } |
169 | 171 |
170 TEST(DeliveryOrderingDeliverChangeRecords) { | 172 TEST(DeliveryOrderingDeliverChangeRecords) { |
171 HarmonyIsolate isolate; | 173 HarmonyIsolate isolate; |
172 HandleScope scope; | 174 HandleScope scope(isolate.GetIsolate()); |
173 LocalContext context; | 175 LocalContext context; |
174 CompileRun( | 176 CompileRun( |
175 "var obj = {};" | 177 "var obj = {};" |
176 "var ordering = [];" | 178 "var ordering = [];" |
177 "function observer1() { ordering.push(1); if (!obj.b) obj.b = true };" | 179 "function observer1() { ordering.push(1); if (!obj.b) obj.b = true };" |
178 "function observer2() { ordering.push(2); };" | 180 "function observer2() { ordering.push(2); };" |
179 "Object.observe(obj, observer1);" | 181 "Object.observe(obj, observer1);" |
180 "Object.observe(obj, observer2);" | 182 "Object.observe(obj, observer2);" |
181 "obj.a = 1;" | 183 "obj.a = 1;" |
182 "Object.deliverChangeRecords(observer2);"); | 184 "Object.deliverChangeRecords(observer2);"); |
183 CHECK_EQ(4, CompileRun("ordering.length")->Int32Value()); | 185 CHECK_EQ(4, CompileRun("ordering.length")->Int32Value()); |
184 // First, observer2 is called due to deliverChangeRecords | 186 // First, observer2 is called due to deliverChangeRecords |
185 CHECK_EQ(2, CompileRun("ordering[0]")->Int32Value()); | 187 CHECK_EQ(2, CompileRun("ordering[0]")->Int32Value()); |
186 // Then, observer1 is called when the stack unwinds | 188 // Then, observer1 is called when the stack unwinds |
187 CHECK_EQ(1, CompileRun("ordering[1]")->Int32Value()); | 189 CHECK_EQ(1, CompileRun("ordering[1]")->Int32Value()); |
188 // observer1's mutation causes both 1 and 2 to be reactivated, | 190 // observer1's mutation causes both 1 and 2 to be reactivated, |
189 // with 1 having priority. | 191 // with 1 having priority. |
190 CHECK_EQ(1, CompileRun("ordering[2]")->Int32Value()); | 192 CHECK_EQ(1, CompileRun("ordering[2]")->Int32Value()); |
191 CHECK_EQ(2, CompileRun("ordering[3]")->Int32Value()); | 193 CHECK_EQ(2, CompileRun("ordering[3]")->Int32Value()); |
192 } | 194 } |
193 | 195 |
194 TEST(ObjectHashTableGrowth) { | 196 TEST(ObjectHashTableGrowth) { |
195 HarmonyIsolate isolate; | 197 HarmonyIsolate isolate; |
196 HandleScope scope; | 198 HandleScope scope(isolate.GetIsolate()); |
197 // Initializing this context sets up initial hash tables. | 199 // Initializing this context sets up initial hash tables. |
198 LocalContext context; | 200 LocalContext context; |
199 Handle<Value> obj = CompileRun("obj = {};"); | 201 Handle<Value> obj = CompileRun("obj = {};"); |
200 Handle<Value> observer = CompileRun( | 202 Handle<Value> observer = CompileRun( |
201 "var ran = false;" | 203 "var ran = false;" |
202 "(function() { ran = true })"); | 204 "(function() { ran = true })"); |
203 { | 205 { |
204 // As does initializing this context. | 206 // As does initializing this context. |
205 LocalContext context2; | 207 LocalContext context2; |
206 context2->Global()->Set(String::New("obj"), obj); | 208 context2->Global()->Set(String::New("obj"), obj); |
207 context2->Global()->Set(String::New("observer"), observer); | 209 context2->Global()->Set(String::New("observer"), observer); |
208 CompileRun( | 210 CompileRun( |
209 "var objArr = [];" | 211 "var objArr = [];" |
210 // 100 objects should be enough to make the hash table grow | 212 // 100 objects should be enough to make the hash table grow |
211 // (and thus relocate). | 213 // (and thus relocate). |
212 "for (var i = 0; i < 100; ++i) {" | 214 "for (var i = 0; i < 100; ++i) {" |
213 " objArr.push({});" | 215 " objArr.push({});" |
214 " Object.observe(objArr[objArr.length-1], function(){});" | 216 " Object.observe(objArr[objArr.length-1], function(){});" |
215 "}" | 217 "}" |
216 "Object.observe(obj, observer);"); | 218 "Object.observe(obj, observer);"); |
217 } | 219 } |
218 // obj is now marked "is_observed", but our map has moved. | 220 // obj is now marked "is_observed", but our map has moved. |
219 CompileRun("obj.foo = 'bar'"); | 221 CompileRun("obj.foo = 'bar'"); |
220 CHECK(CompileRun("ran")->BooleanValue()); | 222 CHECK(CompileRun("ran")->BooleanValue()); |
221 } | 223 } |
222 | 224 |
223 TEST(GlobalObjectObservation) { | 225 TEST(GlobalObjectObservation) { |
224 HarmonyIsolate isolate; | 226 HarmonyIsolate isolate; |
225 HandleScope scope; | |
226 LocalContext context; | 227 LocalContext context; |
| 228 HandleScope scope(isolate.GetIsolate()); |
227 Handle<Object> global_proxy = context->Global(); | 229 Handle<Object> global_proxy = context->Global(); |
228 Handle<Object> inner_global = global_proxy->GetPrototype().As<Object>(); | 230 Handle<Object> inner_global = global_proxy->GetPrototype().As<Object>(); |
229 CompileRun( | 231 CompileRun( |
230 "var records = [];" | 232 "var records = [];" |
231 "var global = this;" | 233 "var global = this;" |
232 "Object.observe(global, function(r) { [].push.apply(records, r) });" | 234 "Object.observe(global, function(r) { [].push.apply(records, r) });" |
233 "global.foo = 'hello';"); | 235 "global.foo = 'hello';"); |
234 CHECK_EQ(1, CompileRun("records.length")->Int32Value()); | 236 CHECK_EQ(1, CompileRun("records.length")->Int32Value()); |
235 CHECK(global_proxy->StrictEquals(CompileRun("records[0].object"))); | 237 CHECK(global_proxy->StrictEquals(CompileRun("records[0].object"))); |
236 | 238 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 recordObj->Get(String::New("oldValue")))); | 312 recordObj->Get(String::New("oldValue")))); |
311 } | 313 } |
312 } | 314 } |
313 } | 315 } |
314 | 316 |
315 #define EXPECT_RECORDS(records, expectations) \ | 317 #define EXPECT_RECORDS(records, expectations) \ |
316 ExpectRecords(records, expectations, ARRAY_SIZE(expectations)) | 318 ExpectRecords(records, expectations, ARRAY_SIZE(expectations)) |
317 | 319 |
318 TEST(APITestBasicMutation) { | 320 TEST(APITestBasicMutation) { |
319 HarmonyIsolate isolate; | 321 HarmonyIsolate isolate; |
320 HandleScope scope; | 322 HandleScope scope(isolate.GetIsolate()); |
321 LocalContext context; | 323 LocalContext context; |
322 Handle<Object> obj = Handle<Object>::Cast(CompileRun( | 324 Handle<Object> obj = Handle<Object>::Cast(CompileRun( |
323 "var records = [];" | 325 "var records = [];" |
324 "var obj = {};" | 326 "var obj = {};" |
325 "function observer(r) { [].push.apply(records, r); };" | 327 "function observer(r) { [].push.apply(records, r); };" |
326 "Object.observe(obj, observer);" | 328 "Object.observe(obj, observer);" |
327 "obj")); | 329 "obj")); |
328 obj->Set(String::New("foo"), Number::New(7)); | 330 obj->Set(String::New("foo"), Number::New(7)); |
329 obj->Set(1, Number::New(2)); | 331 obj->Set(1, Number::New(2)); |
330 // ForceSet should work just as well as Set | 332 // ForceSet should work just as well as Set |
(...skipping 22 matching lines...) Expand all Loading... |
353 { obj, "new", "1.1", Handle<Value>() }, | 355 { obj, "new", "1.1", Handle<Value>() }, |
354 { obj, "deleted", "foo", Number::New(3) }, | 356 { obj, "deleted", "foo", Number::New(3) }, |
355 { obj, "deleted", "1", Number::New(5) }, | 357 { obj, "deleted", "1", Number::New(5) }, |
356 { obj, "deleted", "1.1", Number::New(6) } | 358 { obj, "deleted", "1.1", Number::New(6) } |
357 }; | 359 }; |
358 EXPECT_RECORDS(CompileRun("records"), expected_records); | 360 EXPECT_RECORDS(CompileRun("records"), expected_records); |
359 } | 361 } |
360 | 362 |
361 TEST(HiddenPrototypeObservation) { | 363 TEST(HiddenPrototypeObservation) { |
362 HarmonyIsolate isolate; | 364 HarmonyIsolate isolate; |
363 HandleScope scope; | 365 HandleScope scope(isolate.GetIsolate()); |
364 LocalContext context; | 366 LocalContext context; |
365 Handle<FunctionTemplate> tmpl = FunctionTemplate::New(); | 367 Handle<FunctionTemplate> tmpl = FunctionTemplate::New(); |
366 tmpl->SetHiddenPrototype(true); | 368 tmpl->SetHiddenPrototype(true); |
367 tmpl->InstanceTemplate()->Set(String::New("foo"), Number::New(75)); | 369 tmpl->InstanceTemplate()->Set(String::New("foo"), Number::New(75)); |
368 Handle<Object> proto = tmpl->GetFunction()->NewInstance(); | 370 Handle<Object> proto = tmpl->GetFunction()->NewInstance(); |
369 Handle<Object> obj = Object::New(); | 371 Handle<Object> obj = Object::New(); |
370 obj->SetPrototype(proto); | 372 obj->SetPrototype(proto); |
371 context->Global()->Set(String::New("obj"), obj); | 373 context->Global()->Set(String::New("obj"), obj); |
372 context->Global()->Set(String::New("proto"), proto); | 374 context->Global()->Set(String::New("proto"), proto); |
373 CompileRun( | 375 CompileRun( |
(...skipping 28 matching lines...) Expand all Loading... |
402 } | 404 } |
403 | 405 |
404 | 406 |
405 static int NumberOfElements(i::Handle<i::JSWeakMap> map) { | 407 static int NumberOfElements(i::Handle<i::JSWeakMap> map) { |
406 return i::ObjectHashTable::cast(map->table())->NumberOfElements(); | 408 return i::ObjectHashTable::cast(map->table())->NumberOfElements(); |
407 } | 409 } |
408 | 410 |
409 | 411 |
410 TEST(ObservationWeakMap) { | 412 TEST(ObservationWeakMap) { |
411 HarmonyIsolate isolate; | 413 HarmonyIsolate isolate; |
412 HandleScope scope; | 414 HandleScope scope(isolate.GetIsolate()); |
413 LocalContext context; | 415 LocalContext context; |
414 CompileRun( | 416 CompileRun( |
415 "var obj = {};" | 417 "var obj = {};" |
416 "Object.observe(obj, function(){});" | 418 "Object.observe(obj, function(){});" |
417 "Object.getNotifier(obj);" | 419 "Object.getNotifier(obj);" |
418 "obj = null;"); | 420 "obj = null;"); |
419 i::Handle<i::JSObject> observation_state = FACTORY->observation_state(); | 421 i::Handle<i::JSObject> observation_state = FACTORY->observation_state(); |
420 i::Handle<i::JSWeakMap> observerInfoMap = | 422 i::Handle<i::JSWeakMap> observerInfoMap = |
421 i::Handle<i::JSWeakMap>::cast( | 423 i::Handle<i::JSWeakMap>::cast( |
422 i::GetProperty(observation_state, "observerInfoMap")); | 424 i::GetProperty(observation_state, "observerInfoMap")); |
423 i::Handle<i::JSWeakMap> objectInfoMap = | 425 i::Handle<i::JSWeakMap> objectInfoMap = |
424 i::Handle<i::JSWeakMap>::cast( | 426 i::Handle<i::JSWeakMap>::cast( |
425 i::GetProperty(observation_state, "objectInfoMap")); | 427 i::GetProperty(observation_state, "objectInfoMap")); |
426 i::Handle<i::JSWeakMap> notifierTargetMap = | 428 i::Handle<i::JSWeakMap> notifierTargetMap = |
427 i::Handle<i::JSWeakMap>::cast( | 429 i::Handle<i::JSWeakMap>::cast( |
428 i::GetProperty(observation_state, "notifierTargetMap")); | 430 i::GetProperty(observation_state, "notifierTargetMap")); |
429 CHECK_EQ(1, NumberOfElements(observerInfoMap)); | 431 CHECK_EQ(1, NumberOfElements(observerInfoMap)); |
430 CHECK_EQ(1, NumberOfElements(objectInfoMap)); | 432 CHECK_EQ(1, NumberOfElements(objectInfoMap)); |
431 CHECK_EQ(1, NumberOfElements(notifierTargetMap)); | 433 CHECK_EQ(1, NumberOfElements(notifierTargetMap)); |
432 HEAP->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); | 434 HEAP->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
433 CHECK_EQ(0, NumberOfElements(observerInfoMap)); | 435 CHECK_EQ(0, NumberOfElements(observerInfoMap)); |
434 CHECK_EQ(0, NumberOfElements(objectInfoMap)); | 436 CHECK_EQ(0, NumberOfElements(objectInfoMap)); |
435 CHECK_EQ(0, NumberOfElements(notifierTargetMap)); | 437 CHECK_EQ(0, NumberOfElements(notifierTargetMap)); |
436 } | 438 } |
OLD | NEW |