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 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 CompileRun("obj.foo = 'bar'"); | 235 CompileRun("obj.foo = 'bar'"); |
| 236 CHECK(CompileRun("ran")->BooleanValue()); | 236 CHECK(CompileRun("ran")->BooleanValue()); |
| 237 } | 237 } |
| 238 | 238 |
| 239 | 239 |
| 240 TEST(GlobalObjectObservation) { | 240 TEST(GlobalObjectObservation) { |
| 241 HarmonyIsolate isolate; | 241 HarmonyIsolate isolate; |
| 242 LocalContext context(isolate.GetIsolate()); | 242 LocalContext context(isolate.GetIsolate()); |
| 243 HandleScope scope(isolate.GetIsolate()); | 243 HandleScope scope(isolate.GetIsolate()); |
| 244 Handle<Object> global_proxy = context->Global(); | 244 Handle<Object> global_proxy = context->Global(); |
| 245 Handle<Object> inner_global = global_proxy->GetPrototype().As<Object>(); | |
| 246 CompileRun( | 245 CompileRun( |
| 247 "var records = [];" | 246 "var records = [];" |
| 248 "var global = this;" | 247 "var global = this;" |
| 249 "Object.observe(global, function(r) { [].push.apply(records, r) });" | 248 "Object.observe(global, function(r) { [].push.apply(records, r) });" |
| 250 "global.foo = 'hello';"); | 249 "global.foo = 'hello';"); |
| 251 CHECK_EQ(1, CompileRun("records.length")->Int32Value()); | 250 CHECK_EQ(1, CompileRun("records.length")->Int32Value()); |
| 252 CHECK(global_proxy->StrictEquals(CompileRun("records[0].object"))); | 251 CHECK(global_proxy->StrictEquals(CompileRun("records[0].object"))); |
| 253 | 252 |
| 254 // Detached, mutating the proxy has no effect. | 253 CompileRun("global.baz = 'again';"); |
| 255 context->DetachGlobal(); | |
| 256 CompileRun("global.bar = 'goodbye';"); | |
| 257 CHECK_EQ(1, CompileRun("records.length")->Int32Value()); | |
| 258 | |
| 259 // Mutating the global object directly still has an effect... | |
| 260 CompileRun("this.bar = 'goodbye';"); | |
| 261 CHECK_EQ(2, CompileRun("records.length")->Int32Value()); | 254 CHECK_EQ(2, CompileRun("records.length")->Int32Value()); |
| 262 CHECK(inner_global->StrictEquals(CompileRun("records[1].object"))); | 255 CHECK(global_proxy->StrictEquals(CompileRun("records[1].object"))); |
|
adamk
2013/12/03 21:46:12
Seems like we still want to have a test for what h
| |
| 263 | |
| 264 // Reattached, back to global proxy. | |
| 265 context->ReattachGlobal(global_proxy); | |
| 266 CompileRun("global.baz = 'again';"); | |
| 267 CHECK_EQ(3, CompileRun("records.length")->Int32Value()); | |
| 268 CHECK(global_proxy->StrictEquals(CompileRun("records[2].object"))); | |
| 269 | 256 |
| 270 // Attached to a different context, should not leak mutations | 257 // Attached to a different context, should not leak mutations |
| 271 // to the old context. | 258 // to the old context. |
| 272 context->DetachGlobal(); | 259 context->DetachGlobal(); |
| 273 { | 260 { |
| 274 LocalContext context2(isolate.GetIsolate()); | 261 LocalContext context2(isolate.GetIsolate()); |
| 275 context2->DetachGlobal(); | 262 context2->DetachGlobal(); |
| 276 context2->ReattachGlobal(global_proxy); | |
| 277 CompileRun( | 263 CompileRun( |
| 278 "var records2 = [];" | 264 "var records2 = [];" |
| 279 "Object.observe(this, function(r) { [].push.apply(records2, r) });" | 265 "Object.observe(this, function(r) { [].push.apply(records2, r) });" |
| 280 "this.bat = 'context2';"); | 266 "this.bat = 'context2';"); |
| 281 CHECK_EQ(1, CompileRun("records2.length")->Int32Value()); | 267 CHECK_EQ(0, CompileRun("records2.length")->Int32Value()); |
| 282 CHECK(global_proxy->StrictEquals(CompileRun("records2[0].object"))); | |
| 283 } | 268 } |
| 284 CHECK_EQ(3, CompileRun("records.length")->Int32Value()); | 269 CHECK_EQ(2, CompileRun("records.length")->Int32Value()); |
| 285 | 270 |
| 286 // Attaching by passing to Context::New | 271 // Attaching by passing to Context::New |
| 287 { | 272 { |
| 288 // Delegates to Context::New | 273 // Delegates to Context::New |
| 289 LocalContext context3( | 274 LocalContext context3( |
| 290 isolate.GetIsolate(), NULL, Handle<ObjectTemplate>(), global_proxy); | 275 isolate.GetIsolate(), NULL, Handle<ObjectTemplate>(), global_proxy); |
| 291 CompileRun( | 276 CompileRun( |
| 292 "var records3 = [];" | 277 "var records3 = [];" |
| 293 "Object.observe(this, function(r) { [].push.apply(records3, r) });" | 278 "Object.observe(this, function(r) { [].push.apply(records3, r) });" |
| 294 "this.qux = 'context3';"); | 279 "this.qux = 'context3';"); |
| 295 CHECK_EQ(1, CompileRun("records3.length")->Int32Value()); | 280 CHECK_EQ(1, CompileRun("records3.length")->Int32Value()); |
| 296 CHECK(global_proxy->StrictEquals(CompileRun("records3[0].object"))); | 281 CHECK(global_proxy->StrictEquals(CompileRun("records3[0].object"))); |
| 297 } | 282 } |
| 298 CHECK_EQ(3, CompileRun("records.length")->Int32Value()); | 283 CHECK_EQ(2, CompileRun("records.length")->Int32Value()); |
| 299 } | 284 } |
| 300 | 285 |
| 301 | 286 |
| 302 struct RecordExpectation { | 287 struct RecordExpectation { |
| 303 Handle<Value> object; | 288 Handle<Value> object; |
| 304 const char* type; | 289 const char* type; |
| 305 const char* name; | 290 const char* name; |
| 306 Handle<Value> old_value; | 291 Handle<Value> old_value; |
| 307 }; | 292 }; |
| 308 | 293 |
| (...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 760 "var observer = function(r) { records = r };" | 745 "var observer = function(r) { records = r };" |
| 761 "Object.observe(obj, observer);"); | 746 "Object.observe(obj, observer);"); |
| 762 Handle<Value> obj = | 747 Handle<Value> obj = |
| 763 context->Global()->Get(String::NewFromUtf8(isolate.GetIsolate(), "obj")); | 748 context->Global()->Get(String::NewFromUtf8(isolate.GetIsolate(), "obj")); |
| 764 Handle<Object>::Cast(obj) | 749 Handle<Object>::Cast(obj) |
| 765 ->SetHiddenValue(String::NewFromUtf8(isolate.GetIsolate(), "foo"), | 750 ->SetHiddenValue(String::NewFromUtf8(isolate.GetIsolate(), "foo"), |
| 766 Null(isolate.GetIsolate())); | 751 Null(isolate.GetIsolate())); |
| 767 CompileRun(""); // trigger delivery | 752 CompileRun(""); // trigger delivery |
| 768 CHECK(CompileRun("records")->IsNull()); | 753 CHECK(CompileRun("records")->IsNull()); |
| 769 } | 754 } |
| OLD | NEW |