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

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

Issue 11414094: Make Object.observe on the global object functional (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Add test for attaching via Context::New 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 | « src/runtime.cc ('k') | test/mjsunit/harmony/object-observe.js » ('j') | 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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 "for (var i = 0; i < 100; ++i) {" 211 "for (var i = 0; i < 100; ++i) {"
212 " objArr.push({});" 212 " objArr.push({});"
213 " Object.observe(objArr[objArr.length-1], function(){});" 213 " Object.observe(objArr[objArr.length-1], function(){});"
214 "}" 214 "}"
215 "Object.observe(obj, observer);"); 215 "Object.observe(obj, observer);");
216 } 216 }
217 // obj is now marked "is_observed", but our map has moved. 217 // obj is now marked "is_observed", but our map has moved.
218 CompileRun("obj.foo = 'bar'"); 218 CompileRun("obj.foo = 'bar'");
219 CHECK(CompileRun("ran")->BooleanValue()); 219 CHECK(CompileRun("ran")->BooleanValue());
220 } 220 }
221
222 TEST(GlobalObjectObservation) {
223 HarmonyIsolate isolate;
224 HandleScope scope;
225 LocalContext context;
226 Handle<Object> global_proxy = context->Global();
227 Handle<Object> inner_global = global_proxy->GetPrototype().As<Object>();
228 CompileRun(
229 "var records = [];"
230 "var global = this;"
231 "Object.observe(global, function(r) { [].push.apply(records, r) });"
232 "global.foo = 'hello';");
233 CHECK_EQ(1, CompileRun("records.length")->Int32Value());
234 CHECK(global_proxy->StrictEquals(CompileRun("records[0].object")));
235
236 // Detached, mutating the proxy has no effect.
237 context->DetachGlobal();
238 CompileRun("global.bar = 'goodbye';");
239 CHECK_EQ(1, CompileRun("records.length")->Int32Value());
240
241 // Mutating the global object directly still has an effect...
242 CompileRun("this.bar = 'goodbye';");
243 CHECK_EQ(2, CompileRun("records.length")->Int32Value());
244 CHECK(inner_global->StrictEquals(CompileRun("records[1].object")));
245
246 // Reattached, back to global proxy.
247 context->ReattachGlobal(global_proxy);
248 CompileRun("global.baz = 'again';");
249 CHECK_EQ(3, CompileRun("records.length")->Int32Value());
250 CHECK(global_proxy->StrictEquals(CompileRun("records[2].object")));
251
252 // Attached to a different context, should not leak mutations
253 // to the old context.
254 context->DetachGlobal();
255 {
256 LocalContext context2;
257 context2->DetachGlobal();
258 context2->ReattachGlobal(global_proxy);
259 CompileRun(
260 "var records2 = [];"
261 "Object.observe(this, function(r) { [].push.apply(records2, r) });"
262 "this.bat = 'context2';");
263 CHECK_EQ(1, CompileRun("records2.length")->Int32Value());
264 CHECK(global_proxy->StrictEquals(CompileRun("records2[0].object")));
265 }
266 CHECK_EQ(3, CompileRun("records.length")->Int32Value());
267
268 // Attaching by passing to Context::New
269 {
270 // Delegates to Context::New
271 LocalContext context3(NULL, Handle<ObjectTemplate>(), global_proxy);
272 CompileRun(
273 "var records3 = [];"
274 "Object.observe(this, function(r) { [].push.apply(records3, r) });"
275 "this.qux = 'context3';");
276 CHECK_EQ(1, CompileRun("records3.length")->Int32Value());
277 CHECK(global_proxy->StrictEquals(CompileRun("records3[0].object")));
278 }
279 CHECK_EQ(3, CompileRun("records.length")->Int32Value());
280 }
OLDNEW
« no previous file with comments | « src/runtime.cc ('k') | test/mjsunit/harmony/object-observe.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698