Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: test/cctest/test-object-observe.cc

Issue 11635033: Basic test for interaction of Object.observe and hidden prototypes (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Observe proto Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 CompileRun( 272 CompileRun(
273 "var records3 = [];" 273 "var records3 = [];"
274 "Object.observe(this, function(r) { [].push.apply(records3, r) });" 274 "Object.observe(this, function(r) { [].push.apply(records3, r) });"
275 "this.qux = 'context3';"); 275 "this.qux = 'context3';");
276 CHECK_EQ(1, CompileRun("records3.length")->Int32Value()); 276 CHECK_EQ(1, CompileRun("records3.length")->Int32Value());
277 CHECK(global_proxy->StrictEquals(CompileRun("records3[0].object"))); 277 CHECK(global_proxy->StrictEquals(CompileRun("records3[0].object")));
278 } 278 }
279 CHECK_EQ(3, CompileRun("records.length")->Int32Value()); 279 CHECK_EQ(3, CompileRun("records.length")->Int32Value());
280 } 280 }
281 281
282
282 struct RecordExpectation { 283 struct RecordExpectation {
283 Handle<Value> object; 284 Handle<Value> object;
284 const char* type; 285 const char* type;
285 const char* name; 286 const char* name;
286 Handle<Value> old_value; 287 Handle<Value> old_value;
287 }; 288 };
288 289
289 // TODO(adamk): Use this helper elsewhere in this file. 290 // TODO(adamk): Use this helper elsewhere in this file.
290 static void ExpectRecords(Handle<Value> records, 291 static void ExpectRecords(Handle<Value> records,
291 const RecordExpectation expectations[], 292 const RecordExpectation expectations[],
(...skipping 11 matching lines...) Expand all
303 recordObj->Get(String::New("type")))); 304 recordObj->Get(String::New("type"))));
304 CHECK(String::New(expectations[i].name)->Equals( 305 CHECK(String::New(expectations[i].name)->Equals(
305 recordObj->Get(String::New("name")))); 306 recordObj->Get(String::New("name"))));
306 if (!expectations[i].old_value.IsEmpty()) { 307 if (!expectations[i].old_value.IsEmpty()) {
307 CHECK(expectations[i].old_value->Equals( 308 CHECK(expectations[i].old_value->Equals(
308 recordObj->Get(String::New("oldValue")))); 309 recordObj->Get(String::New("oldValue"))));
309 } 310 }
310 } 311 }
311 } 312 }
312 313
314 #define EXPECT_RECORDS(records, expectations) \
315 ExpectRecords(records, expectations, ARRAY_SIZE(expectations))
316
313 TEST(APITestBasicMutation) { 317 TEST(APITestBasicMutation) {
314 HarmonyIsolate isolate; 318 HarmonyIsolate isolate;
315 HandleScope scope; 319 HandleScope scope;
316 LocalContext context; 320 LocalContext context;
317 Handle<Object> obj = Handle<Object>::Cast(CompileRun( 321 Handle<Object> obj = Handle<Object>::Cast(CompileRun(
318 "var records = [];" 322 "var records = [];"
319 "var obj = {};" 323 "var obj = {};"
320 "function observer(r) { [].push.apply(records, r); };" 324 "function observer(r) { [].push.apply(records, r); };"
321 "Object.observe(obj, observer);" 325 "Object.observe(obj, observer);"
322 "obj")); 326 "obj"));
(...skipping 18 matching lines...) Expand all
341 { obj, "new", "foo", Handle<Value>() }, 345 { obj, "new", "foo", Handle<Value>() },
342 { obj, "new", "1", Handle<Value>() }, 346 { obj, "new", "1", Handle<Value>() },
343 { obj, "updated", "foo", Number::New(1) }, 347 { obj, "updated", "foo", Number::New(1) },
344 { obj, "updated", "1", Number::New(2) }, 348 { obj, "updated", "1", Number::New(2) },
345 { obj, "updated", "1", Number::New(4) }, 349 { obj, "updated", "1", Number::New(4) },
346 { obj, "new", "1.1", Handle<Value>() }, 350 { obj, "new", "1.1", Handle<Value>() },
347 { obj, "deleted", "foo", Number::New(3) }, 351 { obj, "deleted", "foo", Number::New(3) },
348 { obj, "deleted", "1", Number::New(5) }, 352 { obj, "deleted", "1", Number::New(5) },
349 { obj, "deleted", "1.1", Number::New(6) } 353 { obj, "deleted", "1.1", Number::New(6) }
350 }; 354 };
351 ExpectRecords(CompileRun("records"), 355 EXPECT_RECORDS(CompileRun("records"), expected_records);
352 expected_records,
353 ARRAY_SIZE(expected_records));
354 } 356 }
357
358 TEST(HiddenPrototypeObservation) {
359 HarmonyIsolate isolate;
360 HandleScope scope;
361 LocalContext context;
362 Handle<FunctionTemplate> tmpl = FunctionTemplate::New();
363 tmpl->SetHiddenPrototype(true);
364 tmpl->InstanceTemplate()->Set(String::New("foo"), Number::New(75));
365 Handle<Object> proto = tmpl->GetFunction()->NewInstance();
366 Handle<Object> obj = Object::New();
367 obj->SetPrototype(proto);
368 context->Global()->Set(String::New("obj"), obj);
369 context->Global()->Set(String::New("proto"), proto);
370 CompileRun(
371 "var records;"
372 "function observer(r) { records = r; };"
373 "Object.observe(obj, observer);"
374 "obj.foo = 41;" // triggers a notification
375 "proto.foo = 42;" // does not trigger a notification
376 );
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 );
394 const RecordExpectation expected_records3[] = {
395 { proto, "new", "bar", Handle<Value>() }
396 // TODO(adamk): The below record should be emitted since proto is observed
397 // and has been modified. Not clear if this happens in practice.
398 // { proto, "updated", "foo", Number::New(43) }
399 };
400 EXPECT_RECORDS(CompileRun("records"), expected_records3);
401 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698