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 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 Handle<JSFunction> fun2 = v8::Utils::OpenHandle( | 449 Handle<JSFunction> fun2 = v8::Utils::OpenHandle( |
450 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2")))); | 450 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2")))); |
451 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); | 451 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); |
452 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); | 452 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); |
453 CHECK_EQ(*reference_code, fun1->code()); | 453 CHECK_EQ(*reference_code, fun1->code()); |
454 CHECK_EQ(*reference_code, fun2->code()); | 454 CHECK_EQ(*reference_code, fun2->code()); |
455 } | 455 } |
456 } | 456 } |
457 | 457 |
458 | 458 |
| 459 // Test that optimized code for different closures is actually shared |
| 460 // immediately by the FastNewClosureStub without context-dependent entries. |
| 461 TEST(OptimizedCodeSharing3) { |
| 462 if (FLAG_stress_compaction) return; |
| 463 FLAG_allow_natives_syntax = true; |
| 464 FLAG_cache_optimized_code = true; |
| 465 FLAG_turbo_cache_shared_code = true; |
| 466 const char* flag = "--turbo-filter=*"; |
| 467 FlagList::SetFlagsFromString(flag, StrLength(flag)); |
| 468 CcTest::InitializeVM(); |
| 469 v8::HandleScope scope(CcTest::isolate()); |
| 470 v8::Local<v8::Script> script = v8_compile( |
| 471 "function MakeClosure() {" |
| 472 " return function() { return x; };" |
| 473 "}"); |
| 474 Handle<Code> reference_code; |
| 475 { |
| 476 LocalContext env; |
| 477 env->Global()->Set(v8::String::NewFromUtf8(CcTest::isolate(), "x"), |
| 478 v8::Integer::New(CcTest::isolate(), 23)); |
| 479 script->GetUnboundScript()->BindToCurrentContext()->Run(); |
| 480 CompileRun( |
| 481 "var closure0 = MakeClosure();" |
| 482 "%DebugPrint(closure0());" |
| 483 "%OptimizeFunctionOnNextCall(closure0);" |
| 484 "%DebugPrint(closure0());"); |
| 485 Handle<JSFunction> fun0 = v8::Utils::OpenHandle( |
| 486 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure0")))); |
| 487 CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); |
| 488 reference_code = handle(fun0->code()); |
| 489 // Evict only the context-dependent entry from the optimized code map. This |
| 490 // leaves it in a state where only the context-independent entry exists. |
| 491 fun0->shared()->TrimOptimizedCodeMap(SharedFunctionInfo::kEntryLength); |
| 492 } |
| 493 for (int i = 0; i < 10; i++) { |
| 494 LocalContext env; |
| 495 env->Global()->Set(v8::String::NewFromUtf8(CcTest::isolate(), "x"), |
| 496 v8::Integer::New(CcTest::isolate(), i)); |
| 497 script->GetUnboundScript()->BindToCurrentContext()->Run(); |
| 498 CompileRun( |
| 499 "var closure0 = MakeClosure();" |
| 500 "%DebugPrint(closure0());" |
| 501 "%OptimizeFunctionOnNextCall(closure0);" |
| 502 "%DebugPrint(closure0());" |
| 503 "var closure1 = MakeClosure();" |
| 504 "var closure2 = MakeClosure();"); |
| 505 Handle<JSFunction> fun1 = v8::Utils::OpenHandle( |
| 506 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure1")))); |
| 507 Handle<JSFunction> fun2 = v8::Utils::OpenHandle( |
| 508 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2")))); |
| 509 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); |
| 510 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); |
| 511 CHECK_EQ(*reference_code, fun1->code()); |
| 512 CHECK_EQ(*reference_code, fun2->code()); |
| 513 } |
| 514 } |
| 515 |
| 516 |
459 TEST(CompileFunctionInContext) { | 517 TEST(CompileFunctionInContext) { |
460 CcTest::InitializeVM(); | 518 CcTest::InitializeVM(); |
461 v8::HandleScope scope(CcTest::isolate()); | 519 v8::HandleScope scope(CcTest::isolate()); |
462 LocalContext env; | 520 LocalContext env; |
463 CompileRun("var r = 10;"); | 521 CompileRun("var r = 10;"); |
464 v8::Local<v8::Object> math = | 522 v8::Local<v8::Object> math = |
465 v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("Math"))); | 523 v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("Math"))); |
466 v8::ScriptCompiler::Source script_source(v8_str( | 524 v8::ScriptCompiler::Source script_source(v8_str( |
467 "a = PI * r * r;" | 525 "a = PI * r * r;" |
468 "x = r * cos(PI);" | 526 "x = r * cos(PI);" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 CompileRun("function f() { a = 12345678 }; f();"); | 698 CompileRun("function f() { a = 12345678 }; f();"); |
641 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 699 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
642 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); | 700 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
643 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 701 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
644 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); | 702 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
645 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 703 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
646 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); | 704 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); |
647 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 705 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
648 } | 706 } |
649 #endif | 707 #endif |
OLD | NEW |