Chromium Code Reviews| 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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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"))); | |
|
abarth-chromium
2012/11/26 22:53:08
So, records[1].object no longer points to the glob
| |
| 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"))); | |
|
abarth-chromium
2012/11/26 22:53:08
What would happen if you grabbed a reference to re
| |
| 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); | |
|
rossberg
2012/12/03 13:26:18
Is this equivalent to calling Context::New with a
adamk
2012/12/03 19:23:20
Added an explicit test for that codepath as it's a
| |
| 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 } | |
| OLD | NEW |