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

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

Issue 11563: Fixing the detection of aliased eval so that it is exact.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 12 years, 1 month 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/v8natives.js ('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-2008 the V8 project authors. All rights reserved. 1 // Copyright 2007-2008 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 4000 matching lines...) Expand 10 before | Expand all | Expand 10 after
4011 " if (str.indexOf('#<a Fun>') == -1) return 3;" 4011 " if (str.indexOf('#<a Fun>') == -1) return 3;"
4012 " return 0;" 4012 " return 0;"
4013 " }" 4013 " }"
4014 " return 4;" 4014 " return 4;"
4015 "}" 4015 "}"
4016 "test();"); 4016 "test();");
4017 CHECK_EQ(0, value->Int32Value()); 4017 CHECK_EQ(0, value->Int32Value());
4018 } 4018 }
4019 4019
4020 4020
4021 THREADED_TEST(Eval) { 4021 THREADED_TEST(Eval) {
Mads Ager (chromium) 2008/11/21 12:41:23 Could you rewrite as many as these tests as possib
4022 v8::HandleScope scope; 4022 v8::HandleScope scope;
4023 LocalContext current; 4023 LocalContext current;
4024 4024
4025 // Test that un-aliased eval uses local context. 4025 // Test that un-aliased eval reads from local context.
4026 Local<Script> script = 4026 Local<Script> script =
4027 Script::Compile(v8_str("foo = 0;" 4027 Script::Compile(v8_str("foo = 0;"
4028 "(function() {" 4028 "(function() {"
4029 " var foo = 2;" 4029 " var foo = 2;"
4030 " return eval('foo');" 4030 " return eval('foo');"
4031 "})();")); 4031 "})();"));
4032 Local<Value> result = script->Run(); 4032 Local<Value> result = script->Run();
4033 CHECK_EQ(2, result->Int32Value()); 4033 CHECK_EQ(2, result->Int32Value());
4034 4034
4035 // Test that un-aliased eval writes to local context.
4036 script =
4037 Script::Compile(v8_str("foo = 0;"
4038 "(function() {"
4039 " var foo = 1;"
4040 " eval('foo = 2;');"
4041 " return foo;"
4042 "})();"));
4043 result = script->Run();
4044 CHECK_EQ(0, current->Global()->Get(v8_str("foo"))->Int32Value());
4045 CHECK(result->IsInt32());
4046 CHECK_EQ(2, result->Int32Value());
4047
4035 // Test that un-aliased eval has right this. 4048 // Test that un-aliased eval has right this.
4036 script = 4049 script =
4037 Script::Compile(v8_str("function MyObject() { this.self = eval('this'); }" 4050 Script::Compile(v8_str("function MyObject() { this.self = eval('this'); }"
4038 "var o = new MyObject();" 4051 "var o = new MyObject();"
4039 "o === o.self")); 4052 "o === o.self"));
4040 result = script->Run(); 4053 result = script->Run();
4041 CHECK(result->IsTrue()); 4054 CHECK(result->IsTrue());
4055
4056 // Test that aliased eval reads from global context.
4057 script =
4058 Script::Compile(v8_str("var e = eval;"
4059 "foo = 0;"
4060 "(function() {"
4061 " var foo = 2;"
4062 " return e('foo');"
4063 "})();"));
4064 result = script->Run();
4065 CHECK_EQ(0, result->Int32Value());
4066
4067 // Test that aliased eval writes to global context.
4068 script =
4069 Script::Compile(v8_str("var e = eval;"
4070 "foo = 0;"
4071 "(function() {"
4072 " e('var foo = 2;');"
4073 "})();"
4074 "foo"));
4075 result = script->Run();
4076 CHECK_EQ(2, result->Int32Value());
4077
4078 // Test that aliased eval has right this.
4079 script =
4080 Script::Compile(v8_str("var e = eval;"
4081 "function MyObject() { this.self = e('this'); }"
4082 "var o = new MyObject();"
4083 "this === o.self"));
4084 result = script->Run();
4085 CHECK(result->IsTrue());
4086
4087 // Try to cheat the 'aliased eval' detection.
4088 script =
4089 Script::Compile(v8_str("var x = this;"
4090 "foo = 0;"
4091 "(function() {"
4092 " var foo = 2;"
4093 " return x.eval('foo');"
4094 "})();"));
4095 result = script->Run();
4096 CHECK_EQ(0, result->Int32Value());
4097
4098 script =
4099 Script::Compile(v8_str("var e = eval;"
4100 "foo = 0;"
4101 "(function() {"
4102 " var eval = function(x) { return x; };"
4103 " var foo = eval(2);"
4104 " return e('foo');"
4105 "})();"));
4106 result = script->Run();
4107 CHECK_EQ(0, result->Int32Value());
4108
4109 script =
4110 Script::Compile(v8_str("(function() {"
4111 " var eval = function(x) { return 2 * x; };"
4112 " return (function() { return eval(2); })();"
4113 "})();"));
4114 result = script->Run();
4115 CHECK_EQ(4, result->Int32Value());
4116
4117 script =
4118 Script::Compile(v8_str("eval = function(x) { return 2 * x; };"
4119 "(function() {"
4120 " return (function() { return eval(2); })();"
4121 "})();"));
4122 result = script->Run();
4123 CHECK_EQ(4, result->Int32Value());
4124 }
4125
4126
4127 THREADED_TEST(EvalAliasedDynamic) {
4128 v8::HandleScope scope;
4129 LocalContext current;
4130
4131 // This sets 'global' to the real global object (as opposed to the
4132 // proxy). It is highly implementation dependent, so take care.
4133 current->Global()->Set(v8_str("global"), current->Global()->GetPrototype());
4134
4135 // Tests where aliased eval can only be resolved dynamically.
4136 Local<Script> script =
4137 Script::Compile(v8_str("function f(x) { "
4138 " var foo = 2;"
4139 " with (x) { return eval('foo'); }"
4140 "}"
4141 "foo = 0;"
4142 "var result = f(new Object()) == 2;"
4143 "result = result && (f(global) == 0);"
4144 "var x = new Object();"
4145 "x.eval = function(x) { return 1; };"
4146 "result = result && (f(x) == 1);"
4147 "result"));
4148 Local<Value> result = script->Run();
4149 CHECK(result->IsTrue());
4150
4151 v8::TryCatch try_catch;
4152 script =
4153 Script::Compile(v8_str("function f(x) { "
4154 " var bar = 2;"
4155 " with (x) { return eval('bar'); }"
4156 "}"
4157 "f(global)"));
4158 result = script->Run();
4159 CHECK(try_catch.HasCaught());
4160 try_catch.Reset();
4161
4162 script =
4163 Script::Compile(v8_str("this.eval = function (x) { return x + x; };"
4164 "foo = 2;"
4165 "eval('foo')"));
4166 result = script->Run();
4167 CHECK(result->IsString());
4168 CHECK_EQ(v8_str("foofoo"), result);
4042 } 4169 }
4043 4170
4044 4171
4045 THREADED_TEST(CrossEval) { 4172 THREADED_TEST(CrossEval) {
4046 v8::HandleScope scope; 4173 v8::HandleScope scope;
4047 LocalContext other; 4174 LocalContext other;
4048 LocalContext current; 4175 LocalContext current;
4049 4176
4050 Local<String> token = v8_str("<security token>"); 4177 Local<String> token = v8_str("<security token>");
4051 other->SetSecurityToken(token); 4178 other->SetSecurityToken(token);
(...skipping 1286 matching lines...) Expand 10 before | Expand all | Expand 10 after
5338 CompileRun("for (var j = 0; j < 10; j++) new RegExp('');"); 5465 CompileRun("for (var j = 0; j < 10; j++) new RegExp('');");
5339 } 5466 }
5340 // Test CallIC. 5467 // Test CallIC.
5341 for (int i = 0; i < 2; i++) { 5468 for (int i = 0; i < 2; i++) {
5342 LocalContext context; 5469 LocalContext context;
5343 context->Global()->Set(v8_str("tmp"), v8::True()); 5470 context->Global()->Set(v8_str("tmp"), v8::True());
5344 context->Global()->Delete(v8_str("tmp")); 5471 context->Global()->Delete(v8_str("tmp"));
5345 CompileRun("for (var j = 0; j < 10; j++) RegExp('')"); 5472 CompileRun("for (var j = 0; j < 10; j++) RegExp('')");
5346 } 5473 }
5347 } 5474 }
OLDNEW
« no previous file with comments | « src/v8natives.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698