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

Side by Side Diff: test/cctest/test-api.cc

Issue 1207003: Implement support for reattaching a global object to a context. This... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 9 months 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/bootstrapper.cc ('k') | 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 2007-2009 the V8 project authors. All rights reserved. 1 // Copyright 2007-2009 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 4447 matching lines...) Expand 10 before | Expand all | Expand 10 after
4458 4458
4459 Local<Value> foo = v8_str("foo"); 4459 Local<Value> foo = v8_str("foo");
4460 4460
4461 // Set to the same domain. 4461 // Set to the same domain.
4462 env1->SetSecurityToken(foo); 4462 env1->SetSecurityToken(foo);
4463 env2->SetSecurityToken(foo); 4463 env2->SetSecurityToken(foo);
4464 4464
4465 // Enter env2 4465 // Enter env2
4466 env2->Enter(); 4466 env2->Enter();
4467 4467
4468 // Create a function in env1 4468 // Create a function in env2 and add a reference to it in env1.
4469 Local<v8::Object> global2 = env2->Global(); 4469 Local<v8::Object> global2 = env2->Global();
4470 global2->Set(v8_str("prop"), v8::Integer::New(1)); 4470 global2->Set(v8_str("prop"), v8::Integer::New(1));
4471 CompileRun("function getProp() {return prop;}"); 4471 CompileRun("function getProp() {return prop;}");
4472 4472
4473 env1->Global()->Set(v8_str("getProp"), 4473 env1->Global()->Set(v8_str("getProp"),
4474 global2->Get(v8_str("getProp"))); 4474 global2->Get(v8_str("getProp")));
4475 4475
4476 // Detach env1's global, and reuse the global object of env1 4476 // Detach env2's global, and reuse the global object of env2
4477 env2->Exit(); 4477 env2->Exit();
4478 env2->DetachGlobal(); 4478 env2->DetachGlobal();
4479 // env2 has a new global object. 4479 // env2 has a new global object.
4480 CHECK(!env2->Global()->Equals(global2)); 4480 CHECK(!env2->Global()->Equals(global2));
4481 4481
4482 v8::Persistent<Context> env3 = 4482 v8::Persistent<Context> env3 =
4483 Context::New(0, v8::Handle<v8::ObjectTemplate>(), global2); 4483 Context::New(0, v8::Handle<v8::ObjectTemplate>(), global2);
4484 env3->SetSecurityToken(v8_str("bar")); 4484 env3->SetSecurityToken(v8_str("bar"));
4485 env3->Enter(); 4485 env3->Enter();
4486 4486
(...skipping 19 matching lines...) Expand all
4506 { 4506 {
4507 Local<Value> r = global3->Get(v8_str("prop2")); 4507 Local<Value> r = global3->Get(v8_str("prop2"));
4508 CHECK(r->IsUndefined()); 4508 CHECK(r->IsUndefined());
4509 } 4509 }
4510 4510
4511 env2.Dispose(); 4511 env2.Dispose();
4512 env3.Dispose(); 4512 env3.Dispose();
4513 } 4513 }
4514 4514
4515 4515
4516 TEST(DetachAndReattachGlobal) {
4517 v8::HandleScope scope;
4518 LocalContext env1;
4519
4520 // Create second environment.
4521 v8::Persistent<Context> env2 = Context::New();
4522
4523 Local<Value> foo = v8_str("foo");
4524
4525 // Set same security token for env1 and env2.
4526 env1->SetSecurityToken(foo);
4527 env2->SetSecurityToken(foo);
4528
4529 // Create a property on the global object in env2.
4530 {
4531 v8::Context::Scope scope(env2);
4532 env2->Global()->Set(v8_str("p"), v8::Integer::New(42));
4533 }
4534
4535 // Create a reference to env2 global from env1 global.
4536 env1->Global()->Set(v8_str("other"), env2->Global());
4537
4538 // Check that we have access to other.p in env2 from env1.
4539 Local<Value> result = CompileRun("other.p");
4540 CHECK(result->IsInt32());
4541 CHECK_EQ(42, result->Int32Value());
4542
4543 // Hold on to global from env2 and detach global from env2.
4544 Local<v8::Object> global2 = env2->Global();
4545 env2->DetachGlobal();
4546
4547 // Check that the global has been detached. No other.p property can
4548 // be found.
4549 result = CompileRun("other.p");
4550 CHECK(result->IsUndefined());
4551
4552 // Reuse global2 for env3.
4553 v8::Persistent<Context> env3 =
4554 Context::New(0, v8::Handle<v8::ObjectTemplate>(), global2);
4555 CHECK_EQ(global2, env3->Global());
4556
4557 // Start by using the same security token for env3 as for env1 and env2.
4558 env3->SetSecurityToken(foo);
4559
4560 // Create a property on the global object in env3.
4561 {
4562 v8::Context::Scope scope(env3);
4563 env3->Global()->Set(v8_str("p"), v8::Integer::New(24));
4564 }
4565
4566 // Check that other.p is now the property in env3 and that we have access.
4567 result = CompileRun("other.p");
4568 CHECK(result->IsInt32());
4569 CHECK_EQ(24, result->Int32Value());
4570
4571 // Change security token for env3 to something different from env1 and env2.
4572 env3->SetSecurityToken(v8_str("bar"));
4573
4574 // Check that we do not have access to other.p in env1. |other| is now
4575 // the global object for env3 which has a different security token,
4576 // so access should be blocked.
4577 result = CompileRun("other.p");
4578 CHECK(result->IsUndefined());
4579
4580 // Detach the global for env3 and reattach it to env2.
4581 env3->DetachGlobal();
4582 env2->ReattachGlobal(global2);
4583
4584 // Check that we have access to other.p again in env1. |other| is now
4585 // the global object for env2 which has the same security token as env1.
4586 result = CompileRun("other.p");
4587 CHECK(result->IsInt32());
4588 CHECK_EQ(42, result->Int32Value());
4589
4590 env2.Dispose();
4591 env3.Dispose();
4592 }
4593
4594
4516 static bool NamedAccessBlocker(Local<v8::Object> global, 4595 static bool NamedAccessBlocker(Local<v8::Object> global,
4517 Local<Value> name, 4596 Local<Value> name,
4518 v8::AccessType type, 4597 v8::AccessType type,
4519 Local<Value> data) { 4598 Local<Value> data) {
4520 return Context::GetCurrent()->Global()->Equals(global); 4599 return Context::GetCurrent()->Global()->Equals(global);
4521 } 4600 }
4522 4601
4523 4602
4524 static bool IndexedAccessBlocker(Local<v8::Object> global, 4603 static bool IndexedAccessBlocker(Local<v8::Object> global,
4525 uint32_t key, 4604 uint32_t key,
(...skipping 5477 matching lines...) Expand 10 before | Expand all | Expand 10 after
10003 CHECK_EQ(2, prologue_call_count_second); 10082 CHECK_EQ(2, prologue_call_count_second);
10004 CHECK_EQ(2, epilogue_call_count_second); 10083 CHECK_EQ(2, epilogue_call_count_second);
10005 v8::V8::RemoveGCPrologueCallback(PrologueCallbackSecond); 10084 v8::V8::RemoveGCPrologueCallback(PrologueCallbackSecond);
10006 v8::V8::RemoveGCEpilogueCallback(EpilogueCallbackSecond); 10085 v8::V8::RemoveGCEpilogueCallback(EpilogueCallbackSecond);
10007 i::Heap::CollectAllGarbage(false); 10086 i::Heap::CollectAllGarbage(false);
10008 CHECK_EQ(2, prologue_call_count); 10087 CHECK_EQ(2, prologue_call_count);
10009 CHECK_EQ(2, epilogue_call_count); 10088 CHECK_EQ(2, epilogue_call_count);
10010 CHECK_EQ(2, prologue_call_count_second); 10089 CHECK_EQ(2, prologue_call_count_second);
10011 CHECK_EQ(2, epilogue_call_count_second); 10090 CHECK_EQ(2, epilogue_call_count_second);
10012 } 10091 }
OLDNEW
« no previous file with comments | « src/bootstrapper.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698