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

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

Issue 12716010: Added a version of the v8::HandleScope constructor with an Isolate and use that consistently. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fixed rest Created 7 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
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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 60
61 v8::Handle<v8::FunctionTemplate> PrintExtension::GetNativeFunction( 61 v8::Handle<v8::FunctionTemplate> PrintExtension::GetNativeFunction(
62 v8::Handle<v8::String> str) { 62 v8::Handle<v8::String> str) {
63 return v8::FunctionTemplate::New(PrintExtension::Print); 63 return v8::FunctionTemplate::New(PrintExtension::Print);
64 } 64 }
65 65
66 66
67 v8::Handle<v8::Value> PrintExtension::Print(const v8::Arguments& args) { 67 v8::Handle<v8::Value> PrintExtension::Print(const v8::Arguments& args) {
68 for (int i = 0; i < args.Length(); i++) { 68 for (int i = 0; i < args.Length(); i++) {
69 if (i != 0) printf(" "); 69 if (i != 0) printf(" ");
70 v8::HandleScope scope; 70 v8::HandleScope scope(args.GetIsolate());
71 v8::String::Utf8Value str(args[i]); 71 v8::String::Utf8Value str(args[i]);
72 if (*str == NULL) return v8::Undefined(); 72 if (*str == NULL) return v8::Undefined();
73 printf("%s", *str); 73 printf("%s", *str);
74 } 74 }
75 printf("\n"); 75 printf("\n");
76 return v8::Undefined(); 76 return v8::Undefined();
77 } 77 }
78 78
79 79
80 static PrintExtension kPrintExtension; 80 static PrintExtension kPrintExtension;
81 v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension); 81 v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension);
82 82
83 83
84 static void InitializeVM() { 84 static void InitializeVM() {
85 if (env.IsEmpty()) { 85 if (env.IsEmpty()) {
86 v8::HandleScope scope;
87 const char* extensions[] = { "v8/print", "v8/gc" }; 86 const char* extensions[] = { "v8/print", "v8/gc" };
88 v8::ExtensionConfiguration config(2, extensions); 87 v8::ExtensionConfiguration config(2, extensions);
89 env = v8::Context::New(&config); 88 env = v8::Context::New(&config);
90 } 89 }
91 v8::HandleScope scope;
92 env->Enter(); 90 env->Enter();
93 } 91 }
94 92
95 93
96 static MaybeObject* GetGlobalProperty(const char* name) { 94 static MaybeObject* GetGlobalProperty(const char* name) {
97 Handle<String> internalized_name = FACTORY->InternalizeUtf8String(name); 95 Handle<String> internalized_name = FACTORY->InternalizeUtf8String(name);
98 return Isolate::Current()->context()->global_object()->GetProperty( 96 return Isolate::Current()->context()->global_object()->GetProperty(
99 *internalized_name); 97 *internalized_name);
100 } 98 }
101 99
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 bool has_pending_exception; 136 bool has_pending_exception;
139 Handle<JSObject> global(Isolate::Current()->context()->global_object()); 137 Handle<JSObject> global(Isolate::Current()->context()->global_object());
140 Execution::Call(fun, global, 0, NULL, &has_pending_exception); 138 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
141 CHECK(!has_pending_exception); 139 CHECK(!has_pending_exception);
142 return GetGlobalProperty("result")->ToObjectChecked()->Number(); 140 return GetGlobalProperty("result")->ToObjectChecked()->Number();
143 } 141 }
144 142
145 143
146 TEST(Inc) { 144 TEST(Inc) {
147 InitializeVM(); 145 InitializeVM();
148 v8::HandleScope scope; 146 v8::HandleScope scope(env->GetIsolate());
149 CHECK_EQ(4.0, Inc(3)); 147 CHECK_EQ(4.0, Inc(3));
150 } 148 }
151 149
152 150
153 static double Add(int x, int y) { 151 static double Add(int x, int y) {
154 Handle<JSFunction> fun = Compile("result = x + y;"); 152 Handle<JSFunction> fun = Compile("result = x + y;");
155 if (fun.is_null()) return -1; 153 if (fun.is_null()) return -1;
156 154
157 SetGlobalProperty("x", Smi::FromInt(x)); 155 SetGlobalProperty("x", Smi::FromInt(x));
158 SetGlobalProperty("y", Smi::FromInt(y)); 156 SetGlobalProperty("y", Smi::FromInt(y));
159 bool has_pending_exception; 157 bool has_pending_exception;
160 Handle<JSObject> global(Isolate::Current()->context()->global_object()); 158 Handle<JSObject> global(Isolate::Current()->context()->global_object());
161 Execution::Call(fun, global, 0, NULL, &has_pending_exception); 159 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
162 CHECK(!has_pending_exception); 160 CHECK(!has_pending_exception);
163 return GetGlobalProperty("result")->ToObjectChecked()->Number(); 161 return GetGlobalProperty("result")->ToObjectChecked()->Number();
164 } 162 }
165 163
166 164
167 TEST(Add) { 165 TEST(Add) {
168 InitializeVM(); 166 InitializeVM();
169 v8::HandleScope scope; 167 v8::HandleScope scope(env->GetIsolate());
170 CHECK_EQ(5.0, Add(2, 3)); 168 CHECK_EQ(5.0, Add(2, 3));
171 } 169 }
172 170
173 171
174 static double Abs(int x) { 172 static double Abs(int x) {
175 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); 173 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
176 if (fun.is_null()) return -1; 174 if (fun.is_null()) return -1;
177 175
178 SetGlobalProperty("x", Smi::FromInt(x)); 176 SetGlobalProperty("x", Smi::FromInt(x));
179 bool has_pending_exception; 177 bool has_pending_exception;
180 Handle<JSObject> global(Isolate::Current()->context()->global_object()); 178 Handle<JSObject> global(Isolate::Current()->context()->global_object());
181 Execution::Call(fun, global, 0, NULL, &has_pending_exception); 179 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
182 CHECK(!has_pending_exception); 180 CHECK(!has_pending_exception);
183 return GetGlobalProperty("result")->ToObjectChecked()->Number(); 181 return GetGlobalProperty("result")->ToObjectChecked()->Number();
184 } 182 }
185 183
186 184
187 TEST(Abs) { 185 TEST(Abs) {
188 InitializeVM(); 186 InitializeVM();
189 v8::HandleScope scope; 187 v8::HandleScope scope(env->GetIsolate());
190 CHECK_EQ(3.0, Abs(-3)); 188 CHECK_EQ(3.0, Abs(-3));
191 } 189 }
192 190
193 191
194 static double Sum(int n) { 192 static double Sum(int n) {
195 Handle<JSFunction> fun = 193 Handle<JSFunction> fun =
196 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); 194 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
197 if (fun.is_null()) return -1; 195 if (fun.is_null()) return -1;
198 196
199 SetGlobalProperty("n", Smi::FromInt(n)); 197 SetGlobalProperty("n", Smi::FromInt(n));
200 bool has_pending_exception; 198 bool has_pending_exception;
201 Handle<JSObject> global(Isolate::Current()->context()->global_object()); 199 Handle<JSObject> global(Isolate::Current()->context()->global_object());
202 Execution::Call(fun, global, 0, NULL, &has_pending_exception); 200 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
203 CHECK(!has_pending_exception); 201 CHECK(!has_pending_exception);
204 return GetGlobalProperty("result")->ToObjectChecked()->Number(); 202 return GetGlobalProperty("result")->ToObjectChecked()->Number();
205 } 203 }
206 204
207 205
208 TEST(Sum) { 206 TEST(Sum) {
209 InitializeVM(); 207 InitializeVM();
210 v8::HandleScope scope; 208 v8::HandleScope scope(env->GetIsolate());
211 CHECK_EQ(5050.0, Sum(100)); 209 CHECK_EQ(5050.0, Sum(100));
212 } 210 }
213 211
214 212
215 TEST(Print) { 213 TEST(Print) {
216 InitializeVM(); 214 InitializeVM();
217 v8::HandleScope scope; 215 v8::HandleScope scope(env->GetIsolate());
218 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; 216 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
219 Handle<JSFunction> fun = Compile(source); 217 Handle<JSFunction> fun = Compile(source);
220 if (fun.is_null()) return; 218 if (fun.is_null()) return;
221 bool has_pending_exception; 219 bool has_pending_exception;
222 Handle<JSObject> global(Isolate::Current()->context()->global_object()); 220 Handle<JSObject> global(Isolate::Current()->context()->global_object());
223 Execution::Call(fun, global, 0, NULL, &has_pending_exception); 221 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
224 CHECK(!has_pending_exception); 222 CHECK(!has_pending_exception);
225 } 223 }
226 224
227 225
228 // The following test method stems from my coding efforts today. It 226 // The following test method stems from my coding efforts today. It
229 // tests all the functionality I have added to the compiler today 227 // tests all the functionality I have added to the compiler today
230 TEST(Stuff) { 228 TEST(Stuff) {
231 InitializeVM(); 229 InitializeVM();
232 v8::HandleScope scope; 230 v8::HandleScope scope(env->GetIsolate());
233 const char* source = 231 const char* source =
234 "r = 0;\n" 232 "r = 0;\n"
235 "a = new Object;\n" 233 "a = new Object;\n"
236 "if (a == a) r+=1;\n" // 1 234 "if (a == a) r+=1;\n" // 1
237 "if (a != new Object()) r+=2;\n" // 2 235 "if (a != new Object()) r+=2;\n" // 2
238 "a.x = 42;\n" 236 "a.x = 42;\n"
239 "if (a.x == 42) r+=4;\n" // 4 237 "if (a.x == 42) r+=4;\n" // 4
240 "function foo() { var x = 87; return x; }\n" 238 "function foo() { var x = 87; return x; }\n"
241 "if (foo() == 87) r+=8;\n" // 8 239 "if (foo() == 87) r+=8;\n" // 8
242 "function bar() { var x; x = 99; return x; }\n" 240 "function bar() { var x; x = 99; return x; }\n"
(...skipping 11 matching lines...) Expand all
254 bool has_pending_exception; 252 bool has_pending_exception;
255 Handle<JSObject> global(Isolate::Current()->context()->global_object()); 253 Handle<JSObject> global(Isolate::Current()->context()->global_object());
256 Execution::Call(fun, global, 0, NULL, &has_pending_exception); 254 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
257 CHECK(!has_pending_exception); 255 CHECK(!has_pending_exception);
258 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); 256 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number());
259 } 257 }
260 258
261 259
262 TEST(UncaughtThrow) { 260 TEST(UncaughtThrow) {
263 InitializeVM(); 261 InitializeVM();
264 v8::HandleScope scope; 262 v8::HandleScope scope(env->GetIsolate());
265 263
266 const char* source = "throw 42;"; 264 const char* source = "throw 42;";
267 Handle<JSFunction> fun = Compile(source); 265 Handle<JSFunction> fun = Compile(source);
268 CHECK(!fun.is_null()); 266 CHECK(!fun.is_null());
269 bool has_pending_exception; 267 bool has_pending_exception;
270 Isolate* isolate = fun->GetIsolate(); 268 Isolate* isolate = fun->GetIsolate();
271 Handle<JSObject> global(isolate->context()->global_object()); 269 Handle<JSObject> global(isolate->context()->global_object());
272 Execution::Call(fun, global, 0, NULL, &has_pending_exception); 270 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
273 CHECK(has_pending_exception); 271 CHECK(has_pending_exception);
274 CHECK_EQ(42.0, isolate->pending_exception()->ToObjectChecked()->Number()); 272 CHECK_EQ(42.0, isolate->pending_exception()->ToObjectChecked()->Number());
275 } 273 }
276 274
277 275
278 // Tests calling a builtin function from C/C++ code, and the builtin function 276 // Tests calling a builtin function from C/C++ code, and the builtin function
279 // performs GC. It creates a stack frame looks like following: 277 // performs GC. It creates a stack frame looks like following:
280 // | C (PerformGC) | 278 // | C (PerformGC) |
281 // | JS-to-C | 279 // | JS-to-C |
282 // | JS | 280 // | JS |
283 // | C-to-JS | 281 // | C-to-JS |
284 TEST(C2JSFrames) { 282 TEST(C2JSFrames) {
285 InitializeVM(); 283 InitializeVM();
286 v8::HandleScope scope; 284 v8::HandleScope scope(env->GetIsolate());
287 285
288 const char* source = "function foo(a) { gc(), print(a); }"; 286 const char* source = "function foo(a) { gc(), print(a); }";
289 287
290 Handle<JSFunction> fun0 = Compile(source); 288 Handle<JSFunction> fun0 = Compile(source);
291 CHECK(!fun0.is_null()); 289 CHECK(!fun0.is_null());
292 Isolate* isolate = fun0->GetIsolate(); 290 Isolate* isolate = fun0->GetIsolate();
293 291
294 // Run the generated code to populate the global object with 'foo'. 292 // Run the generated code to populate the global object with 'foo'.
295 bool has_pending_exception; 293 bool has_pending_exception;
296 Handle<JSObject> global(Isolate::Current()->context()->global_object()); 294 Handle<JSObject> global(Isolate::Current()->context()->global_object());
(...skipping 16 matching lines...) Expand all
313 argv, 311 argv,
314 &has_pending_exception); 312 &has_pending_exception);
315 CHECK(!has_pending_exception); 313 CHECK(!has_pending_exception);
316 } 314 }
317 315
318 316
319 // Regression 236. Calling InitLineEnds on a Script with undefined 317 // Regression 236. Calling InitLineEnds on a Script with undefined
320 // source resulted in crash. 318 // source resulted in crash.
321 TEST(Regression236) { 319 TEST(Regression236) {
322 InitializeVM(); 320 InitializeVM();
323 v8::HandleScope scope; 321 v8::HandleScope scope(env->GetIsolate());
324 322
325 Handle<Script> script = FACTORY->NewScript(FACTORY->empty_string()); 323 Handle<Script> script = FACTORY->NewScript(FACTORY->empty_string());
326 script->set_source(HEAP->undefined_value()); 324 script->set_source(HEAP->undefined_value());
327 CHECK_EQ(-1, GetScriptLineNumber(script, 0)); 325 CHECK_EQ(-1, GetScriptLineNumber(script, 0));
328 CHECK_EQ(-1, GetScriptLineNumber(script, 100)); 326 CHECK_EQ(-1, GetScriptLineNumber(script, 100));
329 CHECK_EQ(-1, GetScriptLineNumber(script, -1)); 327 CHECK_EQ(-1, GetScriptLineNumber(script, -1));
330 } 328 }
331 329
332 330
333 TEST(GetScriptLineNumber) { 331 TEST(GetScriptLineNumber) {
334 LocalContext env; 332 LocalContext env;
335 v8::HandleScope scope; 333 v8::HandleScope scope(env->GetIsolate());
336 v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test")); 334 v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test"));
337 const char function_f[] = "function f() {}"; 335 const char function_f[] = "function f() {}";
338 const int max_rows = 1000; 336 const int max_rows = 1000;
339 const int buffer_size = max_rows + sizeof(function_f); 337 const int buffer_size = max_rows + sizeof(function_f);
340 ScopedVector<char> buffer(buffer_size); 338 ScopedVector<char> buffer(buffer_size);
341 memset(buffer.start(), '\n', buffer_size - 1); 339 memset(buffer.start(), '\n', buffer_size - 1);
342 buffer[buffer_size - 1] = '\0'; 340 buffer[buffer_size - 1] = '\0';
343 341
344 for (int i = 0; i < max_rows; ++i) { 342 for (int i = 0; i < max_rows; ++i) {
345 if (i > 0) 343 if (i > 0)
346 buffer[i - 1] = '\n'; 344 buffer[i - 1] = '\n';
347 memcpy(&buffer[i], function_f, sizeof(function_f) - 1); 345 memcpy(&buffer[i], function_f, sizeof(function_f) - 1);
348 v8::Handle<v8::String> script_body = v8::String::New(buffer.start()); 346 v8::Handle<v8::String> script_body = v8::String::New(buffer.start());
349 v8::Script::Compile(script_body, &origin)->Run(); 347 v8::Script::Compile(script_body, &origin)->Run();
350 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( 348 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
351 env->Global()->Get(v8::String::New("f"))); 349 env->Global()->Get(v8::String::New("f")));
352 CHECK_EQ(i, f->GetScriptLineNumber()); 350 CHECK_EQ(i, f->GetScriptLineNumber());
353 } 351 }
354 } 352 }
355 353
356 354
357 // Test that optimized code for different closures is actually shared 355 // Test that optimized code for different closures is actually shared
358 // immediately by the FastNewClosureStub when run in the same context. 356 // immediately by the FastNewClosureStub when run in the same context.
359 TEST(OptimizedCodeSharing) { 357 TEST(OptimizedCodeSharing) {
360 // Skip test if --cache-optimized-code is not activated by default because 358 // Skip test if --cache-optimized-code is not activated by default because
361 // FastNewClosureStub that is baked into the snapshot is incorrect. 359 // FastNewClosureStub that is baked into the snapshot is incorrect.
362 if (!FLAG_cache_optimized_code) return; 360 if (!FLAG_cache_optimized_code) return;
363 FLAG_allow_natives_syntax = true; 361 FLAG_allow_natives_syntax = true;
364 InitializeVM(); 362 InitializeVM();
365 v8::HandleScope scope; 363 v8::HandleScope scope(env->GetIsolate());
366 for (int i = 0; i < 10; i++) { 364 for (int i = 0; i < 10; i++) {
367 LocalContext env; 365 LocalContext env;
368 env->Global()->Set(v8::String::New("x"), v8::Integer::New(i)); 366 env->Global()->Set(v8::String::New("x"), v8::Integer::New(i));
369 CompileRun("function MakeClosure() {" 367 CompileRun("function MakeClosure() {"
370 " return function() { return x; };" 368 " return function() { return x; };"
371 "}" 369 "}"
372 "var closure0 = MakeClosure();" 370 "var closure0 = MakeClosure();"
373 "%DebugPrint(closure0());" 371 "%DebugPrint(closure0());"
374 "%OptimizeFunctionOnNextCall(closure0);" 372 "%OptimizeFunctionOnNextCall(closure0);"
375 "%DebugPrint(closure0());" 373 "%DebugPrint(closure0());"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 } else { 416 } else {
419 pc += d.InstructionDecode(decode_buffer, pc); 417 pc += d.InstructionDecode(decode_buffer, pc);
420 CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL); 418 CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL);
421 } 419 }
422 } 420 }
423 } 421 }
424 } 422 }
425 423
426 424
427 TEST(SplitConstantsInFullCompiler) { 425 TEST(SplitConstantsInFullCompiler) {
428 v8::HandleScope scope;
429 LocalContext env; 426 LocalContext env;
427 v8::HandleScope scope(env->GetIsolate());
430 428
431 CompileRun("function f() { a = 12345678 }; f();"); 429 CompileRun("function f() { a = 12345678 }; f();");
432 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); 430 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f"));
433 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); 431 CompileRun("function f(x) { a = 12345678 + x}; f(1);");
434 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); 432 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f"));
435 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); 433 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
436 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); 434 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f"));
437 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); 435 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
438 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); 436 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f"));
439 } 437 }
440 #endif 438 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698