OLD | NEW |
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 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 CHECK(f->shared()->is_compiled()); | 363 CHECK(f->shared()->is_compiled()); |
364 int expected_slots = 0; | 364 int expected_slots = 0; |
365 int expected_ic_slots = 2; | 365 int expected_ic_slots = 2; |
366 CHECK_EQ(expected_slots, f->shared()->feedback_vector()->Slots()); | 366 CHECK_EQ(expected_slots, f->shared()->feedback_vector()->Slots()); |
367 CHECK_EQ(expected_ic_slots, f->shared()->feedback_vector()->ICSlots()); | 367 CHECK_EQ(expected_ic_slots, f->shared()->feedback_vector()->ICSlots()); |
368 } | 368 } |
369 | 369 |
370 | 370 |
371 // Test that optimized code for different closures is actually shared | 371 // Test that optimized code for different closures is actually shared |
372 // immediately by the FastNewClosureStub when run in the same context. | 372 // immediately by the FastNewClosureStub when run in the same context. |
373 TEST(OptimizedCodeSharing) { | 373 TEST(OptimizedCodeSharing1) { |
374 FLAG_stress_compaction = false; | 374 FLAG_stress_compaction = false; |
375 FLAG_allow_natives_syntax = true; | 375 FLAG_allow_natives_syntax = true; |
376 FLAG_cache_optimized_code = true; | 376 FLAG_cache_optimized_code = true; |
377 CcTest::InitializeVM(); | 377 CcTest::InitializeVM(); |
378 v8::HandleScope scope(CcTest::isolate()); | 378 v8::HandleScope scope(CcTest::isolate()); |
379 for (int i = 0; i < 10; i++) { | 379 for (int i = 0; i < 10; i++) { |
380 LocalContext env; | 380 LocalContext env; |
381 env->Global()->Set(v8::String::NewFromUtf8(CcTest::isolate(), "x"), | 381 env->Global()->Set(v8::String::NewFromUtf8(CcTest::isolate(), "x"), |
382 v8::Integer::New(CcTest::isolate(), i)); | 382 v8::Integer::New(CcTest::isolate(), i)); |
383 CompileRun("function MakeClosure() {" | 383 CompileRun( |
384 " return function() { return x; };" | 384 "function MakeClosure() {" |
385 "}" | 385 " return function() { return x; };" |
386 "var closure0 = MakeClosure();" | 386 "}" |
387 "%DebugPrint(closure0());" | 387 "var closure0 = MakeClosure();" |
388 "%OptimizeFunctionOnNextCall(closure0);" | 388 "%DebugPrint(closure0());" |
389 "%DebugPrint(closure0());" | 389 "%OptimizeFunctionOnNextCall(closure0);" |
390 "var closure1 = MakeClosure();" | 390 "%DebugPrint(closure0());" |
391 "var closure2 = MakeClosure();"); | 391 "var closure1 = MakeClosure();" |
| 392 "var closure2 = MakeClosure();"); |
392 Handle<JSFunction> fun1 = v8::Utils::OpenHandle( | 393 Handle<JSFunction> fun1 = v8::Utils::OpenHandle( |
393 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure1")))); | 394 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure1")))); |
394 Handle<JSFunction> fun2 = v8::Utils::OpenHandle( | 395 Handle<JSFunction> fun2 = v8::Utils::OpenHandle( |
395 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2")))); | 396 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2")))); |
396 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); | 397 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); |
397 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); | 398 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); |
398 CHECK_EQ(fun1->code(), fun2->code()); | 399 CHECK_EQ(fun1->code(), fun2->code()); |
399 } | 400 } |
400 } | 401 } |
401 | 402 |
402 | 403 |
| 404 // Test that optimized code for different closures is actually shared |
| 405 // immediately by the FastNewClosureStub when run different contexts. |
| 406 TEST(OptimizedCodeSharing2) { |
| 407 if (FLAG_stress_compaction) return; |
| 408 FLAG_allow_natives_syntax = true; |
| 409 FLAG_cache_optimized_code = true; |
| 410 FLAG_turbo_cache_shared_code = true; |
| 411 const char* flag = "--turbo-filter=*"; |
| 412 FlagList::SetFlagsFromString(flag, StrLength(flag)); |
| 413 CcTest::InitializeVM(); |
| 414 v8::HandleScope scope(CcTest::isolate()); |
| 415 v8::Local<v8::Script> script = v8_compile( |
| 416 "function MakeClosure() {" |
| 417 " return function() { return x; };" |
| 418 "}"); |
| 419 Handle<Code> reference_code; |
| 420 { |
| 421 LocalContext env; |
| 422 env->Global()->Set(v8::String::NewFromUtf8(CcTest::isolate(), "x"), |
| 423 v8::Integer::New(CcTest::isolate(), 23)); |
| 424 script->GetUnboundScript()->BindToCurrentContext()->Run(); |
| 425 CompileRun( |
| 426 "var closure0 = MakeClosure();" |
| 427 "%DebugPrint(closure0());" |
| 428 "%OptimizeFunctionOnNextCall(closure0);" |
| 429 "%DebugPrint(closure0());"); |
| 430 Handle<JSFunction> fun0 = v8::Utils::OpenHandle( |
| 431 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure0")))); |
| 432 CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); |
| 433 reference_code = handle(fun0->code()); |
| 434 } |
| 435 for (int i = 0; i < 10; i++) { |
| 436 LocalContext env; |
| 437 env->Global()->Set(v8::String::NewFromUtf8(CcTest::isolate(), "x"), |
| 438 v8::Integer::New(CcTest::isolate(), i)); |
| 439 script->GetUnboundScript()->BindToCurrentContext()->Run(); |
| 440 CompileRun( |
| 441 "var closure0 = MakeClosure();" |
| 442 "%DebugPrint(closure0());" |
| 443 "%OptimizeFunctionOnNextCall(closure0);" |
| 444 "%DebugPrint(closure0());" |
| 445 "var closure1 = MakeClosure();" |
| 446 "var closure2 = MakeClosure();"); |
| 447 Handle<JSFunction> fun1 = v8::Utils::OpenHandle( |
| 448 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure1")))); |
| 449 Handle<JSFunction> fun2 = v8::Utils::OpenHandle( |
| 450 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2")))); |
| 451 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); |
| 452 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); |
| 453 CHECK_EQ(*reference_code, fun1->code()); |
| 454 CHECK_EQ(*reference_code, fun2->code()); |
| 455 } |
| 456 } |
| 457 |
| 458 |
403 TEST(CompileFunctionInContext) { | 459 TEST(CompileFunctionInContext) { |
404 CcTest::InitializeVM(); | 460 CcTest::InitializeVM(); |
405 v8::HandleScope scope(CcTest::isolate()); | 461 v8::HandleScope scope(CcTest::isolate()); |
406 LocalContext env; | 462 LocalContext env; |
407 CompileRun("var r = 10;"); | 463 CompileRun("var r = 10;"); |
408 v8::Local<v8::Object> math = | 464 v8::Local<v8::Object> math = |
409 v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("Math"))); | 465 v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("Math"))); |
410 v8::ScriptCompiler::Source script_source(v8_str( | 466 v8::ScriptCompiler::Source script_source(v8_str( |
411 "a = PI * r * r;" | 467 "a = PI * r * r;" |
412 "x = r * cos(PI);" | 468 "x = r * cos(PI);" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 CompileRun("function f() { a = 12345678 }; f();"); | 614 CompileRun("function f() { a = 12345678 }; f();"); |
559 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 615 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
560 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); | 616 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
561 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 617 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
562 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); | 618 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
563 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 619 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
564 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); | 620 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); |
565 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 621 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
566 } | 622 } |
567 #endif | 623 #endif |
OLD | NEW |