blob: 190bbb29ca18636033557264cc72b2705a56d270 [file] [log] [blame]
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "cctest.h"
31
32using namespace v8;
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +000033namespace i = v8::internal;
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +000034
35namespace {
36// Need to create a new isolate when FLAG_harmony_observation is on.
37class HarmonyIsolate {
38 public:
39 HarmonyIsolate() {
40 i::FLAG_harmony_observation = true;
41 isolate_ = Isolate::New();
42 isolate_->Enter();
43 }
44
45 ~HarmonyIsolate() {
46 isolate_->Exit();
47 isolate_->Dispose();
48 }
49
50 private:
51 Isolate* isolate_;
52};
53}
54
55TEST(PerIsolateState) {
56 HarmonyIsolate isolate;
57 HandleScope scope;
58 LocalContext context1;
59 CompileRun(
60 "var count = 0;"
61 "var calls = 0;"
62 "var observer = function(records) { count = records.length; calls++ };"
63 "var obj = {};"
64 "Object.observe(obj, observer);");
65 Handle<Value> observer = CompileRun("observer");
66 Handle<Value> obj = CompileRun("obj");
67 Handle<Value> notify_fun1 = CompileRun(
68 "(function() { obj.foo = 'bar'; })");
69 Handle<Value> notify_fun2;
70 {
71 LocalContext context2;
72 context2->Global()->Set(String::New("obj"), obj);
73 notify_fun2 = CompileRun(
74 "(function() { obj.foo = 'baz'; })");
75 }
76 Handle<Value> notify_fun3;
77 {
78 LocalContext context3;
79 context3->Global()->Set(String::New("obj"), obj);
80 notify_fun3 = CompileRun(
81 "(function() { obj.foo = 'bat'; })");
82 }
83 {
84 LocalContext context4;
85 context4->Global()->Set(String::New("observer"), observer);
86 context4->Global()->Set(String::New("fun1"), notify_fun1);
87 context4->Global()->Set(String::New("fun2"), notify_fun2);
88 context4->Global()->Set(String::New("fun3"), notify_fun3);
89 CompileRun("fun1(); fun2(); fun3(); Object.deliverChangeRecords(observer)");
90 }
91 CHECK_EQ(1, CompileRun("calls")->Int32Value());
92 CHECK_EQ(3, CompileRun("count")->Int32Value());
93}
94
95TEST(EndOfMicrotaskDelivery) {
96 HarmonyIsolate isolate;
97 HandleScope scope;
98 LocalContext context;
99 CompileRun(
100 "var obj = {};"
101 "var count = 0;"
102 "var observer = function(records) { count = records.length };"
103 "Object.observe(obj, observer);"
104 "obj.foo = 'bar';");
105 CHECK_EQ(1, CompileRun("count")->Int32Value());
106}
107
108TEST(DeliveryOrdering) {
109 HarmonyIsolate isolate;
110 HandleScope scope;
111 LocalContext context;
112 CompileRun(
113 "var obj1 = {};"
114 "var obj2 = {};"
115 "var ordering = [];"
116 "function observer2() { ordering.push(2); };"
117 "function observer1() { ordering.push(1); };"
118 "function observer3() { ordering.push(3); };"
119 "Object.observe(obj1, observer1);"
120 "Object.observe(obj1, observer2);"
121 "Object.observe(obj1, observer3);"
122 "obj1.foo = 'bar';");
123 CHECK_EQ(3, CompileRun("ordering.length")->Int32Value());
124 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value());
125 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value());
126 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value());
127 CompileRun(
128 "ordering = [];"
129 "Object.observe(obj2, observer3);"
130 "Object.observe(obj2, observer2);"
131 "Object.observe(obj2, observer1);"
132 "obj2.foo = 'baz'");
133 CHECK_EQ(3, CompileRun("ordering.length")->Int32Value());
134 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value());
135 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value());
136 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value());
137}
138
139TEST(DeliveryOrderingReentrant) {
140 HarmonyIsolate isolate;
141 HandleScope scope;
142 LocalContext context;
143 CompileRun(
144 "var obj = {};"
145 "var reentered = false;"
146 "var ordering = [];"
147 "function observer1() { ordering.push(1); };"
148 "function observer2() {"
149 " if (!reentered) {"
150 " obj.foo = 'baz';"
151 " reentered = true;"
152 " }"
153 " ordering.push(2);"
154 "};"
155 "function observer3() { ordering.push(3); };"
156 "Object.observe(obj, observer1);"
157 "Object.observe(obj, observer2);"
158 "Object.observe(obj, observer3);"
159 "obj.foo = 'bar';");
160 CHECK_EQ(5, CompileRun("ordering.length")->Int32Value());
161 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value());
162 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value());
163 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value());
164 // Note that we re-deliver to observers 1 and 2, while observer3
165 // already received the second record during the first round.
166 CHECK_EQ(1, CompileRun("ordering[3]")->Int32Value());
167 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value());
168}
169
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000170TEST(DeliveryOrderingDeliverChangeRecords) {
171 HarmonyIsolate isolate;
172 HandleScope scope;
173 LocalContext context;
174 CompileRun(
175 "var obj = {};"
176 "var ordering = [];"
177 "function observer1() { ordering.push(1); if (!obj.b) obj.b = true };"
178 "function observer2() { ordering.push(2); };"
179 "Object.observe(obj, observer1);"
180 "Object.observe(obj, observer2);"
181 "obj.a = 1;"
182 "Object.deliverChangeRecords(observer2);");
183 CHECK_EQ(4, CompileRun("ordering.length")->Int32Value());
184 // First, observer2 is called due to deliverChangeRecords
185 CHECK_EQ(2, CompileRun("ordering[0]")->Int32Value());
186 // Then, observer1 is called when the stack unwinds
187 CHECK_EQ(1, CompileRun("ordering[1]")->Int32Value());
188 // observer1's mutation causes both 1 and 2 to be reactivated,
189 // with 1 having priority.
190 CHECK_EQ(1, CompileRun("ordering[2]")->Int32Value());
191 CHECK_EQ(2, CompileRun("ordering[3]")->Int32Value());
192}
193
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000194TEST(ObjectHashTableGrowth) {
195 HarmonyIsolate isolate;
196 HandleScope scope;
197 // Initializing this context sets up initial hash tables.
198 LocalContext context;
199 Handle<Value> obj = CompileRun("obj = {};");
200 Handle<Value> observer = CompileRun(
201 "var ran = false;"
202 "(function() { ran = true })");
203 {
204 // As does initializing this context.
205 LocalContext context2;
206 context2->Global()->Set(String::New("obj"), obj);
207 context2->Global()->Set(String::New("observer"), observer);
208 CompileRun(
209 "var objArr = [];"
210 // 100 objects should be enough to make the hash table grow
211 // (and thus relocate).
212 "for (var i = 0; i < 100; ++i) {"
213 " objArr.push({});"
214 " Object.observe(objArr[objArr.length-1], function(){});"
215 "}"
216 "Object.observe(obj, observer);");
217 }
218 // obj is now marked "is_observed", but our map has moved.
219 CompileRun("obj.foo = 'bar'");
220 CHECK(CompileRun("ran")->BooleanValue());
221}
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000222
223TEST(GlobalObjectObservation) {
224 HarmonyIsolate isolate;
225 HandleScope scope;
226 LocalContext context;
227 Handle<Object> global_proxy = context->Global();
228 Handle<Object> inner_global = global_proxy->GetPrototype().As<Object>();
229 CompileRun(
230 "var records = [];"
231 "var global = this;"
232 "Object.observe(global, function(r) { [].push.apply(records, r) });"
233 "global.foo = 'hello';");
234 CHECK_EQ(1, CompileRun("records.length")->Int32Value());
235 CHECK(global_proxy->StrictEquals(CompileRun("records[0].object")));
236
237 // Detached, mutating the proxy has no effect.
238 context->DetachGlobal();
239 CompileRun("global.bar = 'goodbye';");
240 CHECK_EQ(1, CompileRun("records.length")->Int32Value());
241
242 // Mutating the global object directly still has an effect...
243 CompileRun("this.bar = 'goodbye';");
244 CHECK_EQ(2, CompileRun("records.length")->Int32Value());
245 CHECK(inner_global->StrictEquals(CompileRun("records[1].object")));
246
247 // Reattached, back to global proxy.
248 context->ReattachGlobal(global_proxy);
249 CompileRun("global.baz = 'again';");
250 CHECK_EQ(3, CompileRun("records.length")->Int32Value());
251 CHECK(global_proxy->StrictEquals(CompileRun("records[2].object")));
252
253 // Attached to a different context, should not leak mutations
254 // to the old context.
255 context->DetachGlobal();
256 {
257 LocalContext context2;
258 context2->DetachGlobal();
259 context2->ReattachGlobal(global_proxy);
260 CompileRun(
261 "var records2 = [];"
262 "Object.observe(this, function(r) { [].push.apply(records2, r) });"
263 "this.bat = 'context2';");
264 CHECK_EQ(1, CompileRun("records2.length")->Int32Value());
265 CHECK(global_proxy->StrictEquals(CompileRun("records2[0].object")));
266 }
267 CHECK_EQ(3, CompileRun("records.length")->Int32Value());
268
269 // Attaching by passing to Context::New
270 {
271 // Delegates to Context::New
272 LocalContext context3(NULL, Handle<ObjectTemplate>(), global_proxy);
273 CompileRun(
274 "var records3 = [];"
275 "Object.observe(this, function(r) { [].push.apply(records3, r) });"
276 "this.qux = 'context3';");
277 CHECK_EQ(1, CompileRun("records3.length")->Int32Value());
278 CHECK(global_proxy->StrictEquals(CompileRun("records3[0].object")));
279 }
280 CHECK_EQ(3, CompileRun("records.length")->Int32Value());
281}
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000282
ulan@chromium.org4121f232012-12-27 15:57:11 +0000283
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000284struct RecordExpectation {
285 Handle<Value> object;
286 const char* type;
287 const char* name;
288 Handle<Value> old_value;
289};
290
291// TODO(adamk): Use this helper elsewhere in this file.
292static void ExpectRecords(Handle<Value> records,
293 const RecordExpectation expectations[],
294 int num) {
295 CHECK(records->IsArray());
296 Handle<Array> recordArray = records.As<Array>();
297 CHECK_EQ(num, static_cast<int>(recordArray->Length()));
298 for (int i = 0; i < num; ++i) {
299 Handle<Value> record = recordArray->Get(i);
300 CHECK(record->IsObject());
301 Handle<Object> recordObj = record.As<Object>();
302 CHECK(expectations[i].object->StrictEquals(
303 recordObj->Get(String::New("object"))));
304 CHECK(String::New(expectations[i].type)->Equals(
305 recordObj->Get(String::New("type"))));
306 CHECK(String::New(expectations[i].name)->Equals(
307 recordObj->Get(String::New("name"))));
308 if (!expectations[i].old_value.IsEmpty()) {
309 CHECK(expectations[i].old_value->Equals(
310 recordObj->Get(String::New("oldValue"))));
311 }
312 }
313}
314
ulan@chromium.org4121f232012-12-27 15:57:11 +0000315#define EXPECT_RECORDS(records, expectations) \
316 ExpectRecords(records, expectations, ARRAY_SIZE(expectations))
317
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000318TEST(APITestBasicMutation) {
319 HarmonyIsolate isolate;
320 HandleScope scope;
321 LocalContext context;
322 Handle<Object> obj = Handle<Object>::Cast(CompileRun(
323 "var records = [];"
324 "var obj = {};"
325 "function observer(r) { [].push.apply(records, r); };"
326 "Object.observe(obj, observer);"
327 "obj"));
328 obj->Set(String::New("foo"), Number::New(1));
329 obj->Set(1, Number::New(2));
330 // ForceSet should work just as well as Set
331 obj->ForceSet(String::New("foo"), Number::New(3));
332 obj->ForceSet(Number::New(1), Number::New(4));
333 // Setting an indexed element via the property setting method
334 obj->Set(Number::New(1), Number::New(5));
335 // Setting with a non-String, non-uint32 key
336 obj->Set(Number::New(1.1), Number::New(6), DontDelete);
337 obj->Delete(String::New("foo"));
338 obj->Delete(1);
339 obj->ForceDelete(Number::New(1.1));
340
341 // Force delivery
342 // TODO(adamk): Should the above set methods trigger delivery themselves?
343 CompileRun("void 0");
344 CHECK_EQ(9, CompileRun("records.length")->Int32Value());
345 const RecordExpectation expected_records[] = {
346 { obj, "new", "foo", Handle<Value>() },
347 { obj, "new", "1", Handle<Value>() },
348 { obj, "updated", "foo", Number::New(1) },
349 { obj, "updated", "1", Number::New(2) },
350 { obj, "updated", "1", Number::New(4) },
351 { obj, "new", "1.1", Handle<Value>() },
352 { obj, "deleted", "foo", Number::New(3) },
353 { obj, "deleted", "1", Number::New(5) },
354 { obj, "deleted", "1.1", Number::New(6) }
355 };
ulan@chromium.org4121f232012-12-27 15:57:11 +0000356 EXPECT_RECORDS(CompileRun("records"), expected_records);
357}
358
359TEST(HiddenPrototypeObservation) {
360 HarmonyIsolate isolate;
361 HandleScope scope;
362 LocalContext context;
363 Handle<FunctionTemplate> tmpl = FunctionTemplate::New();
364 tmpl->SetHiddenPrototype(true);
365 tmpl->InstanceTemplate()->Set(String::New("foo"), Number::New(75));
366 Handle<Object> proto = tmpl->GetFunction()->NewInstance();
367 Handle<Object> obj = Object::New();
368 obj->SetPrototype(proto);
369 context->Global()->Set(String::New("obj"), obj);
370 context->Global()->Set(String::New("proto"), proto);
371 CompileRun(
372 "var records;"
373 "function observer(r) { records = r; };"
374 "Object.observe(obj, observer);"
375 "obj.foo = 41;" // triggers a notification
376 "proto.foo = 42;"); // does not trigger a notification
377 const RecordExpectation expected_records[] = {
378 { obj, "updated", "foo", Number::New(75) }
379 };
380 EXPECT_RECORDS(CompileRun("records"), expected_records);
381 obj->SetPrototype(Null());
382 CompileRun("obj.foo = 43");
383 const RecordExpectation expected_records2[] = {
384 { obj, "new", "foo", Handle<Value>() }
385 };
386 EXPECT_RECORDS(CompileRun("records"), expected_records2);
387 obj->SetPrototype(proto);
388 CompileRun(
389 "Object.observe(proto, observer);"
390 "proto.bar = 1;"
391 "Object.unobserve(obj, observer);"
392 "obj.foo = 44;");
393 const RecordExpectation expected_records3[] = {
394 { proto, "new", "bar", Handle<Value>() }
395 // TODO(adamk): The below record should be emitted since proto is observed
396 // and has been modified. Not clear if this happens in practice.
397 // { proto, "updated", "foo", Number::New(43) }
398 };
399 EXPECT_RECORDS(CompileRun("records"), expected_records3);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000400}
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000401
402
403static int NumberOfElements(i::Handle<i::JSWeakMap> map) {
404 return i::ObjectHashTable::cast(map->table())->NumberOfElements();
405}
406
407
408TEST(ObservationWeakMap) {
409 HarmonyIsolate isolate;
410 HandleScope scope;
411 LocalContext context;
412 CompileRun(
413 "var obj = {};"
414 "Object.observe(obj, function(){});"
415 "Object.getNotifier(obj);"
416 "obj = null;");
417 i::Handle<i::JSObject> observation_state = FACTORY->observation_state();
418 i::Handle<i::JSWeakMap> observerInfoMap =
419 i::Handle<i::JSWeakMap>::cast(
420 i::GetProperty(observation_state, "observerInfoMap"));
421 i::Handle<i::JSWeakMap> objectInfoMap =
422 i::Handle<i::JSWeakMap>::cast(
423 i::GetProperty(observation_state, "objectInfoMap"));
424 i::Handle<i::JSWeakMap> notifierTargetMap =
425 i::Handle<i::JSWeakMap>::cast(
426 i::GetProperty(observation_state, "notifierTargetMap"));
427 CHECK_EQ(1, NumberOfElements(observerInfoMap));
428 CHECK_EQ(1, NumberOfElements(objectInfoMap));
429 CHECK_EQ(1, NumberOfElements(notifierTargetMap));
430 HEAP->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask);
431 CHECK_EQ(0, NumberOfElements(observerInfoMap));
432 CHECK_EQ(0, NumberOfElements(objectInfoMap));
433 CHECK_EQ(0, NumberOfElements(notifierTargetMap));
434}