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 // Detached, mutating the proxy has no effect. |
255 context->DetachGlobal(); | 254 context->DetachGlobal(); |
256 CompileRun("global.bar = 'goodbye';"); | 255 CompileRun("global.bar = 'goodbye';"); |
257 CHECK_EQ(1, CompileRun("records.length")->Int32Value()); | 256 CHECK_EQ(1, CompileRun("records.length")->Int32Value()); |
258 | 257 CompileRun("this.baz = 'goodbye';"); |
259 // Mutating the global object directly still has an effect... | 258 CHECK_EQ(1, CompileRun("records.length")->Int32Value()); |
260 CompileRun("this.bar = 'goodbye';"); | |
261 CHECK_EQ(2, CompileRun("records.length")->Int32Value()); | |
262 CHECK(inner_global->StrictEquals(CompileRun("records[1].object"))); | |
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 | 259 |
270 // Attached to a different context, should not leak mutations | 260 // Attached to a different context, should not leak mutations |
271 // to the old context. | 261 // to the old context. |
272 context->DetachGlobal(); | 262 context->DetachGlobal(); |
273 { | 263 { |
274 LocalContext context2(isolate.GetIsolate()); | 264 LocalContext context2(isolate.GetIsolate()); |
275 context2->DetachGlobal(); | |
276 context2->ReattachGlobal(global_proxy); | |
277 CompileRun( | 265 CompileRun( |
278 "var records2 = [];" | 266 "var records2 = [];" |
| 267 "var global = this;" |
279 "Object.observe(this, function(r) { [].push.apply(records2, r) });" | 268 "Object.observe(this, function(r) { [].push.apply(records2, r) });" |
280 "this.bat = 'context2';"); | 269 "this.v1 = 'context2';"); |
| 270 context2->DetachGlobal(); |
| 271 CompileRun( |
| 272 "global.v2 = 'context2';" |
| 273 "this.v3 = 'context2';"); |
281 CHECK_EQ(1, CompileRun("records2.length")->Int32Value()); | 274 CHECK_EQ(1, CompileRun("records2.length")->Int32Value()); |
282 CHECK(global_proxy->StrictEquals(CompileRun("records2[0].object"))); | |
283 } | 275 } |
284 CHECK_EQ(3, CompileRun("records.length")->Int32Value()); | 276 CHECK_EQ(1, CompileRun("records.length")->Int32Value()); |
285 | 277 |
286 // Attaching by passing to Context::New | 278 // Attaching by passing to Context::New |
287 { | 279 { |
288 // Delegates to Context::New | 280 // Delegates to Context::New |
289 LocalContext context3( | 281 LocalContext context3( |
290 isolate.GetIsolate(), NULL, Handle<ObjectTemplate>(), global_proxy); | 282 isolate.GetIsolate(), NULL, Handle<ObjectTemplate>(), global_proxy); |
291 CompileRun( | 283 CompileRun( |
292 "var records3 = [];" | 284 "var records3 = [];" |
293 "Object.observe(this, function(r) { [].push.apply(records3, r) });" | 285 "Object.observe(this, function(r) { [].push.apply(records3, r) });" |
294 "this.qux = 'context3';"); | 286 "this.qux = 'context3';"); |
295 CHECK_EQ(1, CompileRun("records3.length")->Int32Value()); | 287 CHECK_EQ(1, CompileRun("records3.length")->Int32Value()); |
296 CHECK(global_proxy->StrictEquals(CompileRun("records3[0].object"))); | 288 CHECK(global_proxy->StrictEquals(CompileRun("records3[0].object"))); |
297 } | 289 } |
298 CHECK_EQ(3, CompileRun("records.length")->Int32Value()); | 290 CHECK_EQ(1, CompileRun("records.length")->Int32Value()); |
299 } | 291 } |
300 | 292 |
301 | 293 |
302 struct RecordExpectation { | 294 struct RecordExpectation { |
303 Handle<Value> object; | 295 Handle<Value> object; |
304 const char* type; | 296 const char* type; |
305 const char* name; | 297 const char* name; |
306 Handle<Value> old_value; | 298 Handle<Value> old_value; |
307 }; | 299 }; |
308 | 300 |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
760 "var observer = function(r) { records = r };" | 752 "var observer = function(r) { records = r };" |
761 "Object.observe(obj, observer);"); | 753 "Object.observe(obj, observer);"); |
762 Handle<Value> obj = | 754 Handle<Value> obj = |
763 context->Global()->Get(String::NewFromUtf8(isolate.GetIsolate(), "obj")); | 755 context->Global()->Get(String::NewFromUtf8(isolate.GetIsolate(), "obj")); |
764 Handle<Object>::Cast(obj) | 756 Handle<Object>::Cast(obj) |
765 ->SetHiddenValue(String::NewFromUtf8(isolate.GetIsolate(), "foo"), | 757 ->SetHiddenValue(String::NewFromUtf8(isolate.GetIsolate(), "foo"), |
766 Null(isolate.GetIsolate())); | 758 Null(isolate.GetIsolate())); |
767 CompileRun(""); // trigger delivery | 759 CompileRun(""); // trigger delivery |
768 CHECK(CompileRun("records")->IsNull()); | 760 CHECK(CompileRun("records")->IsNull()); |
769 } | 761 } |
OLD | NEW |