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

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

Issue 104013008: Reland v8:18458 "Load the global proxy from the context of the target function." (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 11 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/x64/stub-cache-x64.cc ('k') | test/mjsunit/contextual-calls.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 8686 matching lines...) Expand 10 before | Expand all | Expand 10 after
8697 env3->SetSecurityToken(v8_str("bar")); 8697 env3->SetSecurityToken(v8_str("bar"));
8698 8698
8699 // Check that we do not have access to other.p in env1. |other| is now 8699 // Check that we do not have access to other.p in env1. |other| is now
8700 // the global object for env3 which has a different security token, 8700 // the global object for env3 which has a different security token,
8701 // so access should be blocked. 8701 // so access should be blocked.
8702 result = CompileRun("other.p"); 8702 result = CompileRun("other.p");
8703 CHECK(result->IsUndefined()); 8703 CHECK(result->IsUndefined());
8704 } 8704 }
8705 8705
8706 8706
8707 void GetThisX(const v8::FunctionCallbackInfo<v8::Value>& info) {
8708 info.GetReturnValue().Set(
8709 info.GetIsolate()->GetCurrentContext()->Global()->Get(v8_str("x")));
8710 }
8711
8712
8707 TEST(DetachedAccesses) { 8713 TEST(DetachedAccesses) {
8708 LocalContext env1; 8714 LocalContext env1;
8709 v8::HandleScope scope(env1->GetIsolate()); 8715 v8::HandleScope scope(env1->GetIsolate());
8710 8716
8711 // Create second environment. 8717 // Create second environment.
8712 v8::Handle<Context> env2 = Context::New(env1->GetIsolate()); 8718 Local<ObjectTemplate> inner_global_template =
8719 FunctionTemplate::New(env1->GetIsolate())->InstanceTemplate();
8720 inner_global_template ->SetAccessorProperty(
8721 v8_str("this_x"), FunctionTemplate::New(env1->GetIsolate(), GetThisX));
8722 v8::Local<Context> env2 =
8723 Context::New(env1->GetIsolate(), NULL, inner_global_template);
8713 8724
8714 Local<Value> foo = v8_str("foo"); 8725 Local<Value> foo = v8_str("foo");
8715 8726
8716 // Set same security token for env1 and env2. 8727 // Set same security token for env1 and env2.
8717 env1->SetSecurityToken(foo); 8728 env1->SetSecurityToken(foo);
8718 env2->SetSecurityToken(foo); 8729 env2->SetSecurityToken(foo);
8719 8730
8731 env1->Global()->Set(v8_str("x"), v8_str("env1_x"));
8732
8720 { 8733 {
8721 v8::Context::Scope scope(env2); 8734 v8::Context::Scope scope(env2);
8735 env2->Global()->Set(v8_str("x"), v8_str("env2_x"));
8722 CompileRun( 8736 CompileRun(
8723 "var x = 'x';" 8737 "function bound_x() { return x; }"
8724 "function get_x() { return this.x; }" 8738 "function get_x() { return this.x; }"
8725 "function get_x_w() { return get_x(); }" 8739 "function get_x_w() { return (function() {return this.x;})(); }");
8726 ""); 8740 env1->Global()->Set(v8_str("bound_x"), CompileRun("bound_x"));
8727 env1->Global()->Set(v8_str("get_x"), CompileRun("get_x")); 8741 env1->Global()->Set(v8_str("get_x"), CompileRun("get_x"));
8728 env1->Global()->Set(v8_str("get_x_w"), CompileRun("get_x_w")); 8742 env1->Global()->Set(v8_str("get_x_w"), CompileRun("get_x_w"));
8743 env1->Global()->Set(
8744 v8_str("this_x"),
8745 CompileRun("Object.getOwnPropertyDescriptor(this, 'this_x').get"));
8729 } 8746 }
8730 8747
8731 Local<Object> env2_global = env2->Global(); 8748 Local<Object> env2_global = env2->Global();
8732 env2_global->TurnOnAccessCheck(); 8749 env2_global->TurnOnAccessCheck();
8733 env2->DetachGlobal(); 8750 env2->DetachGlobal();
8734 8751
8735 Local<Value> result; 8752 Local<Value> result;
8753 result = CompileRun("bound_x()");
8754 CHECK_EQ(v8_str("env2_x"), result);
8736 result = CompileRun("get_x()"); 8755 result = CompileRun("get_x()");
8737 CHECK(result->IsUndefined()); 8756 CHECK(result->IsUndefined());
8738 result = CompileRun("get_x_w()"); 8757 result = CompileRun("get_x_w()");
8739 CHECK(result->IsUndefined()); 8758 CHECK(result->IsUndefined());
8759 result = CompileRun("this_x()");
8760 CHECK_EQ(v8_str("env2_x"), result);
8740 8761
8741 // Reattach env2's proxy 8762 // Reattach env2's proxy
8742 env2 = Context::New(env1->GetIsolate(), 8763 env2 = Context::New(env1->GetIsolate(),
8743 0, 8764 0,
8744 v8::Handle<v8::ObjectTemplate>(), 8765 v8::Handle<v8::ObjectTemplate>(),
8745 env2_global); 8766 env2_global);
8746 env2->SetSecurityToken(foo); 8767 env2->SetSecurityToken(foo);
8747 { 8768 {
8748 v8::Context::Scope scope(env2); 8769 v8::Context::Scope scope(env2);
8749 CompileRun("var x = 'x2';"); 8770 env2->Global()->Set(v8_str("x"), v8_str("env3_x"));
8771 env2->Global()->Set(v8_str("env1"), env1->Global());
8772 result = CompileRun(
8773 "results = [];"
8774 "for (var i = 0; i < 4; i++ ) {"
8775 " results.push(env1.bound_x());"
8776 " results.push(env1.get_x());"
8777 " results.push(env1.get_x_w());"
8778 " results.push(env1.this_x());"
8779 "}"
8780 "results");
8781 Local<v8::Array> results = Local<v8::Array>::Cast(result);
8782 CHECK_EQ(16, results->Length());
8783 for (int i = 0; i < 16; i += 4) {
8784 CHECK_EQ(v8_str("env2_x"), results->Get(i + 0));
8785 CHECK_EQ(v8_str("env1_x"), results->Get(i + 1));
8786 CHECK_EQ(v8_str("env3_x"), results->Get(i + 2));
8787 CHECK_EQ(v8_str("env2_x"), results->Get(i + 3));
8788 }
8750 } 8789 }
8751 8790
8752 result = CompileRun("get_x()"); 8791 result = CompileRun(
8753 CHECK(result->IsUndefined()); 8792 "results = [];"
8754 result = CompileRun("get_x_w()"); 8793 "for (var i = 0; i < 4; i++ ) {"
8755 CHECK_EQ(v8_str("x2"), result); 8794 " results.push(bound_x());"
8795 " results.push(get_x());"
8796 " results.push(get_x_w());"
8797 " results.push(this_x());"
8798 "}"
8799 "results");
8800 Local<v8::Array> results = Local<v8::Array>::Cast(result);
8801 CHECK_EQ(16, results->Length());
8802 for (int i = 0; i < 16; i += 4) {
8803 CHECK_EQ(v8_str("env2_x"), results->Get(i + 0));
8804 CHECK_EQ(v8_str("env3_x"), results->Get(i + 1));
8805 CHECK_EQ(v8_str("env3_x"), results->Get(i + 2));
8806 CHECK_EQ(v8_str("env2_x"), results->Get(i + 3));
8807 }
8808
8809 result = CompileRun(
8810 "results = [];"
8811 "for (var i = 0; i < 4; i++ ) {"
8812 " results.push(this.bound_x());"
8813 " results.push(this.get_x());"
8814 " results.push(this.get_x_w());"
8815 " results.push(this.this_x());"
8816 "}"
8817 "results");
8818 results = Local<v8::Array>::Cast(result);
8819 CHECK_EQ(16, results->Length());
8820 for (int i = 0; i < 16; i += 4) {
8821 CHECK_EQ(v8_str("env2_x"), results->Get(i + 0));
8822 CHECK_EQ(v8_str("env1_x"), results->Get(i + 1));
8823 CHECK_EQ(v8_str("env3_x"), results->Get(i + 2));
8824 CHECK_EQ(v8_str("env2_x"), results->Get(i + 3));
8825 }
8756 } 8826 }
8757 8827
8758 8828
8759 static bool allowed_access_type[v8::ACCESS_KEYS + 1] = { false }; 8829 static bool allowed_access_type[v8::ACCESS_KEYS + 1] = { false };
8760 static bool NamedAccessBlocker(Local<v8::Object> global, 8830 static bool NamedAccessBlocker(Local<v8::Object> global,
8761 Local<Value> name, 8831 Local<Value> name,
8762 v8::AccessType type, 8832 v8::AccessType type,
8763 Local<Value> data) { 8833 Local<Value> data) {
8764 return CcTest::isolate()->GetCurrentContext()->Global()->Equals(global) || 8834 return CcTest::isolate()->GetCurrentContext()->Global()->Equals(global) ||
8765 allowed_access_type[type]; 8835 allowed_access_type[type];
(...skipping 11337 matching lines...) Expand 10 before | Expand all | Expand 10 after
20103 "Function.prototype.apply.call(func)"); 20173 "Function.prototype.apply.call(func)");
20104 TestReceiver(i, foreign_context->Global(), 20174 TestReceiver(i, foreign_context->Global(),
20105 "Function.prototype.apply.apply(func)"); 20175 "Function.prototype.apply.apply(func)");
20106 // Making calls through built-in functions. 20176 // Making calls through built-in functions.
20107 TestReceiver(i, foreign_context->Global(), "[1].map(func)[0]"); 20177 TestReceiver(i, foreign_context->Global(), "[1].map(func)[0]");
20108 // ToString(func()) is func()[0], i.e., the returned this.id. 20178 // ToString(func()) is func()[0], i.e., the returned this.id.
20109 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/,func)[1]"))); 20179 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/,func)[1]")));
20110 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[1]"))); 20180 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[1]")));
20111 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[3]"))); 20181 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[3]")));
20112 20182
20113 // TODO(1547): Make the following also return "i".
20114 // Calling with environment record as base. 20183 // Calling with environment record as base.
20115 TestReceiver(o, context->Global(), "func()"); 20184 TestReceiver(i, foreign_context->Global(), "func()");
20116 // Calling with no base. 20185 // Calling with no base.
20117 TestReceiver(o, context->Global(), "(1,func)()"); 20186 TestReceiver(i, foreign_context->Global(), "(1,func)()");
20118 } 20187 }
20119 20188
20120 20189
20121 uint8_t callback_fired = 0; 20190 uint8_t callback_fired = 0;
20122 20191
20123 20192
20124 void CallCompletedCallback1() { 20193 void CallCompletedCallback1() {
20125 i::OS::Print("Firing callback 1.\n"); 20194 i::OS::Print("Firing callback 1.\n");
20126 callback_fired ^= 1; // Toggle first bit. 20195 callback_fired ^= 1; // Toggle first bit.
20127 } 20196 }
(...skipping 1344 matching lines...) Expand 10 before | Expand all | Expand 10 after
21472 } 21541 }
21473 for (int i = 0; i < runs; i++) { 21542 for (int i = 0; i < runs; i++) {
21474 Local<String> expected; 21543 Local<String> expected;
21475 if (i != 0) { 21544 if (i != 0) {
21476 CHECK_EQ(v8_str("escape value"), values[i]); 21545 CHECK_EQ(v8_str("escape value"), values[i]);
21477 } else { 21546 } else {
21478 CHECK(values[i].IsEmpty()); 21547 CHECK(values[i].IsEmpty());
21479 } 21548 }
21480 } 21549 }
21481 } 21550 }
OLDNEW
« no previous file with comments | « src/x64/stub-cache-x64.cc ('k') | test/mjsunit/contextual-calls.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698