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

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

Issue 111613003: Load the global proxy from the context of the target function. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: hydrogen Created 7 years 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
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 8624 matching lines...) Expand 10 before | Expand all | Expand 10 after
8635 8635
8636 // Create second environment. 8636 // Create second environment.
8637 v8::Handle<Context> env2 = Context::New(env1->GetIsolate()); 8637 v8::Handle<Context> env2 = Context::New(env1->GetIsolate());
8638 8638
8639 Local<Value> foo = v8_str("foo"); 8639 Local<Value> foo = v8_str("foo");
8640 8640
8641 // Set same security token for env1 and env2. 8641 // Set same security token for env1 and env2.
8642 env1->SetSecurityToken(foo); 8642 env1->SetSecurityToken(foo);
8643 env2->SetSecurityToken(foo); 8643 env2->SetSecurityToken(foo);
8644 8644
8645 env1->Global()->Set(v8_str("x"), v8_str("env1_x"));
8646
8645 { 8647 {
8646 v8::Context::Scope scope(env2); 8648 v8::Context::Scope scope(env2);
8649 env2->Global()->Set(v8_str("x"), v8_str("env2_x"));
8647 CompileRun( 8650 CompileRun(
8648 "var x = 'x';" 8651 "function bound_x() { return x; }"
8649 "function get_x() { return this.x; }" 8652 "function get_x() { return this.x; }"
8650 "function get_x_w() { return get_x(); }" 8653 "function get_x_w() { return (function() {return this.x;})(); }"
8654 "function closure() { return [bound_x(), get_x(), get_x_w()]; }"
8651 ""); 8655 "");
8656 env1->Global()->Set(v8_str("bound_x"), CompileRun("bound_x"));
8652 env1->Global()->Set(v8_str("get_x"), CompileRun("get_x")); 8657 env1->Global()->Set(v8_str("get_x"), CompileRun("get_x"));
8653 env1->Global()->Set(v8_str("get_x_w"), CompileRun("get_x_w")); 8658 env1->Global()->Set(v8_str("get_x_w"), CompileRun("get_x_w"));
8654 } 8659 }
8655 8660
8661 CompileRun("function outer_closure() { return closure(); }");
8662
8656 Local<Object> env2_global = env2->Global(); 8663 Local<Object> env2_global = env2->Global();
8657 env2_global->TurnOnAccessCheck(); 8664 env2_global->TurnOnAccessCheck();
8658 env2->DetachGlobal(); 8665 env2->DetachGlobal();
8659 8666
8660 Local<Value> result; 8667 Local<Value> result;
8668 result = CompileRun("bound_x()");
8669 CHECK_EQ(v8_str("env2_x"), result);
8661 result = CompileRun("get_x()"); 8670 result = CompileRun("get_x()");
8662 CHECK(result->IsUndefined()); 8671 CHECK(result->IsUndefined());
8663 result = CompileRun("get_x_w()"); 8672 result = CompileRun("get_x_w()");
8664 CHECK(result->IsUndefined()); 8673 CHECK(result->IsUndefined());
8665 8674
8666 // Reattach env2's proxy 8675 // Reattach env2's proxy
8667 env2 = Context::New(env1->GetIsolate(), 8676 env2 = Context::New(env1->GetIsolate(),
8668 0, 8677 0,
8669 v8::Handle<v8::ObjectTemplate>(), 8678 v8::Handle<v8::ObjectTemplate>(),
8670 env2_global); 8679 env2_global);
8671 env2->SetSecurityToken(foo); 8680 env2->SetSecurityToken(foo);
8672 { 8681 {
8673 v8::Context::Scope scope(env2); 8682 v8::Context::Scope scope(env2);
8674 CompileRun("var x = 'x2';"); 8683 env2->Global()->Set(v8_str("x"), v8_str("env3_x"));
8684 env2->Global()->Set(v8_str("env1"), env1->Global());
8685 result = CompileRun(
8686 "results = [];"
8687 "for (var i = 0; i < 4; i++ ) {"
8688 " results.push(env1.bound_x());"
8689 " results.push(env1.get_x());"
8690 " results.push(env1.get_x_w());"
8691 "}"
8692 "results");
8693 Local<v8::Array> results = Local<v8::Array>::Cast(result);
8694 CHECK_EQ(12, results->Length());
8695 for (int i = 0; i < 12; i += 3) {
8696 CHECK_EQ(v8_str("env2_x"), results->Get(i + 0));
8697 CHECK_EQ(v8_str("env1_x"), results->Get(i + 1));
8698 CHECK_EQ(v8_str("env3_x"), results->Get(i + 2));
8699 }
8675 } 8700 }
8676 8701
8677 result = CompileRun("get_x()"); 8702 result = CompileRun(
8678 CHECK(result->IsUndefined()); 8703 "results = [];"
8679 result = CompileRun("get_x_w()"); 8704 "for (var i = 0; i < 4; i++ ) {"
8680 CHECK_EQ(v8_str("x2"), result); 8705 " results.push(bound_x());"
8706 " results.push(get_x());"
8707 " results.push(get_x_w());"
8708 "}"
8709 "results");
8710 Local<v8::Array> results = Local<v8::Array>::Cast(result);
8711 CHECK_EQ(12, results->Length());
8712 for (int i = 0; i < 12; i += 3) {
8713 CHECK_EQ(v8_str("env2_x"), results->Get(i + 0));
8714 CHECK_EQ(v8_str("env3_x"), results->Get(i + 1));
8715 CHECK_EQ(v8_str("env3_x"), results->Get(i + 2));
8716 }
8717
8718 result = CompileRun(
8719 "results = [];"
8720 "for (var i = 0; i < 4; i++ ) {"
8721 " results.push(this.bound_x());"
8722 " results.push(this.get_x());"
8723 " results.push(this.get_x_w());"
8724 "}"
8725 "results");
8726 results = Local<v8::Array>::Cast(result);
8727 CHECK_EQ(12, results->Length());
8728 for (int i = 0; i < 12; i += 3) {
8729 CHECK_EQ(v8_str("env2_x"), results->Get(i + 0));
8730 CHECK_EQ(v8_str("env1_x"), results->Get(i + 1));
8731 CHECK_EQ(v8_str("env3_x"), results->Get(i + 2));
8732 }
8681 } 8733 }
8682 8734
8683 8735
8684 static bool allowed_access_type[v8::ACCESS_KEYS + 1] = { false }; 8736 static bool allowed_access_type[v8::ACCESS_KEYS + 1] = { false };
8685 static bool NamedAccessBlocker(Local<v8::Object> global, 8737 static bool NamedAccessBlocker(Local<v8::Object> global,
8686 Local<Value> name, 8738 Local<Value> name,
8687 v8::AccessType type, 8739 v8::AccessType type,
8688 Local<Value> data) { 8740 Local<Value> data) {
8689 return CcTest::isolate()->GetCurrentContext()->Global()->Equals(global) || 8741 return CcTest::isolate()->GetCurrentContext()->Global()->Equals(global) ||
8690 allowed_access_type[type]; 8742 allowed_access_type[type];
(...skipping 11146 matching lines...) Expand 10 before | Expand all | Expand 10 after
19837 "Function.prototype.apply.call(func)"); 19889 "Function.prototype.apply.call(func)");
19838 TestReceiver(i, foreign_context->Global(), 19890 TestReceiver(i, foreign_context->Global(),
19839 "Function.prototype.apply.apply(func)"); 19891 "Function.prototype.apply.apply(func)");
19840 // Making calls through built-in functions. 19892 // Making calls through built-in functions.
19841 TestReceiver(i, foreign_context->Global(), "[1].map(func)[0]"); 19893 TestReceiver(i, foreign_context->Global(), "[1].map(func)[0]");
19842 // ToString(func()) is func()[0], i.e., the returned this.id. 19894 // ToString(func()) is func()[0], i.e., the returned this.id.
19843 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/,func)[1]"))); 19895 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/,func)[1]")));
19844 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[1]"))); 19896 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[1]")));
19845 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[3]"))); 19897 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[3]")));
19846 19898
19847 // TODO(1547): Make the following also return "i".
19848 // Calling with environment record as base. 19899 // Calling with environment record as base.
19849 TestReceiver(o, context->Global(), "func()"); 19900 TestReceiver(i, foreign_context->Global(), "func()");
19850 // Calling with no base. 19901 // Calling with no base.
19851 TestReceiver(o, context->Global(), "(1,func)()"); 19902 TestReceiver(i, foreign_context->Global(), "(1,func)()");
19852 } 19903 }
19853 19904
19854 19905
19855 uint8_t callback_fired = 0; 19906 uint8_t callback_fired = 0;
19856 19907
19857 19908
19858 void CallCompletedCallback1() { 19909 void CallCompletedCallback1() {
19859 i::OS::Print("Firing callback 1.\n"); 19910 i::OS::Print("Firing callback 1.\n");
19860 callback_fired ^= 1; // Toggle first bit. 19911 callback_fired ^= 1; // Toggle first bit.
19861 } 19912 }
(...skipping 1088 matching lines...) Expand 10 before | Expand all | Expand 10 after
20950 } 21001 }
20951 for (int i = 0; i < runs; i++) { 21002 for (int i = 0; i < runs; i++) {
20952 Local<String> expected; 21003 Local<String> expected;
20953 if (i != 0) { 21004 if (i != 0) {
20954 CHECK_EQ(v8_str("escape value"), values[i]); 21005 CHECK_EQ(v8_str("escape value"), values[i]);
20955 } else { 21006 } else {
20956 CHECK(values[i].IsEmpty()); 21007 CHECK(values[i].IsEmpty());
20957 } 21008 }
20958 } 21009 }
20959 } 21010 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698