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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 LocalContext context1; | 57 LocalContext context1; |
58 CompileRun( | 58 CompileRun( |
59 "var count = 0;" | 59 "var count = 0;" |
60 "var calls = 0;" | 60 "var calls = 0;" |
61 "var observer = function(records) { count = records.length; calls++ };" | 61 "var observer = function(records) { count = records.length; calls++ };" |
62 "var obj = {};" | 62 "var obj = {};" |
63 "Object.observe(obj, observer);"); | 63 "Object.observe(obj, observer);"); |
64 Handle<Value> observer = CompileRun("observer"); | 64 Handle<Value> observer = CompileRun("observer"); |
65 Handle<Value> obj = CompileRun("obj"); | 65 Handle<Value> obj = CompileRun("obj"); |
66 Handle<Value> notify_fun1 = CompileRun( | 66 Handle<Value> notify_fun1 = CompileRun( |
67 "(function() { Object.notify(obj, {type: 'a'}); })"); | 67 "(function() { obj.foo = 'bar'; })"); |
68 Handle<Value> notify_fun2; | 68 Handle<Value> notify_fun2; |
69 { | 69 { |
70 LocalContext context2; | 70 LocalContext context2; |
71 context2->Global()->Set(String::New("obj"), obj); | 71 context2->Global()->Set(String::New("obj"), obj); |
72 notify_fun2 = CompileRun( | 72 notify_fun2 = CompileRun( |
73 "(function() { Object.notify(obj, {type: 'b'}); })"); | 73 "(function() { obj.foo = 'baz'; })"); |
74 } | 74 } |
75 Handle<Value> notify_fun3; | 75 Handle<Value> notify_fun3; |
76 { | 76 { |
77 LocalContext context3; | 77 LocalContext context3; |
78 context3->Global()->Set(String::New("obj"), obj); | 78 context3->Global()->Set(String::New("obj"), obj); |
79 notify_fun3 = CompileRun( | 79 notify_fun3 = CompileRun( |
80 "(function() { Object.notify(obj, {type: 'c'}); })"); | 80 "(function() { obj.foo = 'bat'; })"); |
81 } | 81 } |
82 { | 82 { |
83 LocalContext context4; | 83 LocalContext context4; |
84 context4->Global()->Set(String::New("observer"), observer); | 84 context4->Global()->Set(String::New("observer"), observer); |
85 context4->Global()->Set(String::New("fun1"), notify_fun1); | 85 context4->Global()->Set(String::New("fun1"), notify_fun1); |
86 context4->Global()->Set(String::New("fun2"), notify_fun2); | 86 context4->Global()->Set(String::New("fun2"), notify_fun2); |
87 context4->Global()->Set(String::New("fun3"), notify_fun3); | 87 context4->Global()->Set(String::New("fun3"), notify_fun3); |
88 CompileRun("fun1(); fun2(); fun3(); Object.deliverChangeRecords(observer)"); | 88 CompileRun("fun1(); fun2(); fun3(); Object.deliverChangeRecords(observer)"); |
89 } | 89 } |
90 CHECK_EQ(1, CompileRun("calls")->Int32Value()); | 90 CHECK_EQ(1, CompileRun("calls")->Int32Value()); |
91 CHECK_EQ(3, CompileRun("count")->Int32Value()); | 91 CHECK_EQ(3, CompileRun("count")->Int32Value()); |
92 } | 92 } |
93 | 93 |
94 TEST(EndOfMicrotaskDelivery) { | 94 TEST(EndOfMicrotaskDelivery) { |
95 HarmonyIsolate isolate; | 95 HarmonyIsolate isolate; |
96 HandleScope scope; | 96 HandleScope scope; |
97 LocalContext context; | 97 LocalContext context; |
98 CompileRun( | 98 CompileRun( |
99 "var obj = {};" | 99 "var obj = {};" |
100 "var count = 0;" | 100 "var count = 0;" |
101 "var observer = function(records) { count = records.length };" | 101 "var observer = function(records) { count = records.length };" |
102 "Object.observe(obj, observer);" | 102 "Object.observe(obj, observer);" |
103 "Object.notify(obj, {type: 'a'});"); | 103 "obj.foo = 'bar';"); |
104 CHECK_EQ(1, CompileRun("count")->Int32Value()); | 104 CHECK_EQ(1, CompileRun("count")->Int32Value()); |
105 } | 105 } |
106 | 106 |
107 TEST(DeliveryOrdering) { | 107 TEST(DeliveryOrdering) { |
108 HarmonyIsolate isolate; | 108 HarmonyIsolate isolate; |
109 HandleScope scope; | 109 HandleScope scope; |
110 LocalContext context; | 110 LocalContext context; |
111 CompileRun( | 111 CompileRun( |
112 "var obj1 = {};" | 112 "var obj1 = {};" |
113 "var obj2 = {};" | 113 "var obj2 = {};" |
114 "var ordering = [];" | 114 "var ordering = [];" |
115 "function observer2() { ordering.push(2); };" | 115 "function observer2() { ordering.push(2); };" |
116 "function observer1() { ordering.push(1); };" | 116 "function observer1() { ordering.push(1); };" |
117 "function observer3() { ordering.push(3); };" | 117 "function observer3() { ordering.push(3); };" |
118 "Object.observe(obj1, observer1);" | 118 "Object.observe(obj1, observer1);" |
119 "Object.observe(obj1, observer2);" | 119 "Object.observe(obj1, observer2);" |
120 "Object.observe(obj1, observer3);" | 120 "Object.observe(obj1, observer3);" |
121 "Object.notify(obj1, {type: 'a'});"); | 121 "obj1.foo = 'bar';"); |
122 CHECK_EQ(3, CompileRun("ordering.length")->Int32Value()); | 122 CHECK_EQ(3, CompileRun("ordering.length")->Int32Value()); |
123 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value()); | 123 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value()); |
124 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); | 124 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); |
125 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value()); | 125 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value()); |
126 CompileRun( | 126 CompileRun( |
127 "ordering = [];" | 127 "ordering = [];" |
128 "Object.observe(obj2, observer3);" | 128 "Object.observe(obj2, observer3);" |
129 "Object.observe(obj2, observer2);" | 129 "Object.observe(obj2, observer2);" |
130 "Object.observe(obj2, observer1);" | 130 "Object.observe(obj2, observer1);" |
131 "Object.notify(obj2, {type: 'b'});"); | 131 "obj2.foo = 'baz'"); |
132 CHECK_EQ(3, CompileRun("ordering.length")->Int32Value()); | 132 CHECK_EQ(3, CompileRun("ordering.length")->Int32Value()); |
133 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value()); | 133 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value()); |
134 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); | 134 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); |
135 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value()); | 135 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value()); |
136 } | 136 } |
137 | 137 |
138 TEST(DeliveryOrderingReentrant) { | 138 TEST(DeliveryOrderingReentrant) { |
139 HarmonyIsolate isolate; | 139 HarmonyIsolate isolate; |
140 HandleScope scope; | 140 HandleScope scope; |
141 LocalContext context; | 141 LocalContext context; |
142 CompileRun( | 142 CompileRun( |
143 "var obj = {};" | 143 "var obj = {};" |
144 "var reentered = false;" | 144 "var reentered = false;" |
145 "var ordering = [];" | 145 "var ordering = [];" |
146 "function observer1() { ordering.push(1); };" | 146 "function observer1() { ordering.push(1); };" |
147 "function observer2() {" | 147 "function observer2() {" |
148 " if (!reentered) {" | 148 " if (!reentered) {" |
149 " Object.notify(obj, {type: 'b'});" | 149 " obj.foo = 'baz';" |
150 " reentered = true;" | 150 " reentered = true;" |
151 " }" | 151 " }" |
152 " ordering.push(2);" | 152 " ordering.push(2);" |
153 "};" | 153 "};" |
154 "function observer3() { ordering.push(3); };" | 154 "function observer3() { ordering.push(3); };" |
155 "Object.observe(obj, observer1);" | 155 "Object.observe(obj, observer1);" |
156 "Object.observe(obj, observer2);" | 156 "Object.observe(obj, observer2);" |
157 "Object.observe(obj, observer3);" | 157 "Object.observe(obj, observer3);" |
158 "Object.notify(obj, {type: 'a'});"); | 158 "obj.foo = 'bar';"); |
159 CHECK_EQ(5, CompileRun("ordering.length")->Int32Value()); | 159 CHECK_EQ(5, CompileRun("ordering.length")->Int32Value()); |
160 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value()); | 160 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value()); |
161 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); | 161 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); |
162 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value()); | 162 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value()); |
163 // Note that we re-deliver to observers 1 and 2, while observer3 | 163 // Note that we re-deliver to observers 1 and 2, while observer3 |
164 // already received the second record during the first round. | 164 // already received the second record during the first round. |
165 CHECK_EQ(1, CompileRun("ordering[3]")->Int32Value()); | 165 CHECK_EQ(1, CompileRun("ordering[3]")->Int32Value()); |
166 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); | 166 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); |
167 } | 167 } |
168 | 168 |
(...skipping 18 matching lines...) Expand all Loading... |
187 "for (var i = 0; i < 100; ++i) {" | 187 "for (var i = 0; i < 100; ++i) {" |
188 " objArr.push({});" | 188 " objArr.push({});" |
189 " Object.observe(objArr[objArr.length-1], function(){});" | 189 " Object.observe(objArr[objArr.length-1], function(){});" |
190 "}" | 190 "}" |
191 "Object.observe(obj, observer);"); | 191 "Object.observe(obj, observer);"); |
192 } | 192 } |
193 // obj is now marked "is_observed", but our map has moved. | 193 // obj is now marked "is_observed", but our map has moved. |
194 CompileRun("obj.foo = 'bar'"); | 194 CompileRun("obj.foo = 'bar'"); |
195 CHECK(CompileRun("ran")->BooleanValue()); | 195 CHECK(CompileRun("ran")->BooleanValue()); |
196 } | 196 } |
OLD | NEW |