1 // Copyright 2012 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 #include <stdlib.h> 29 #include <wchar.h> 30 31 #include "src/v8.h" 32 33 #include "src/compiler.h" 34 #include "src/disasm.h" 35 #include "src/interpreter/interpreter.h" 36 #include "src/parsing/parser.h" 37 #include "test/cctest/cctest.h" 38 39 using namespace v8::internal; 40 41 static Handle<Object> GetGlobalProperty(const char* name) { 42 Isolate* isolate = CcTest::i_isolate(); 43 return JSReceiver::GetProperty(isolate, isolate->global_object(), name) 44 .ToHandleChecked(); 45 } 46 47 48 static void SetGlobalProperty(const char* name, Object* value) { 49 Isolate* isolate = CcTest::i_isolate(); 50 Handle<Object> object(value, isolate); 51 Handle<String> internalized_name = 52 isolate->factory()->InternalizeUtf8String(name); 53 Handle<JSObject> global(isolate->context()->global_object()); 54 Runtime::SetObjectProperty(isolate, global, internalized_name, object, 55 SLOPPY).Check(); 56 } 57 58 59 static Handle<JSFunction> Compile(const char* source) { 60 Isolate* isolate = CcTest::i_isolate(); 61 Handle<String> source_code = isolate->factory()->NewStringFromUtf8( 62 CStrVector(source)).ToHandleChecked(); 63 Handle<SharedFunctionInfo> shared = Compiler::GetSharedFunctionInfoForScript( 64 source_code, Handle<String>(), 0, 0, v8::ScriptOriginOptions(), 65 Handle<Object>(), Handle<Context>(isolate->native_context()), NULL, NULL, 66 v8::ScriptCompiler::kNoCompileOptions, NOT_NATIVES_CODE, false); 67 return isolate->factory()->NewFunctionFromSharedFunctionInfo( 68 shared, isolate->native_context()); 69 } 70 71 72 static double Inc(Isolate* isolate, int x) { 73 const char* source = "result = %d + 1;"; 74 EmbeddedVector<char, 512> buffer; 75 SNPrintF(buffer, source, x); 76 77 Handle<JSFunction> fun = Compile(buffer.start()); 78 if (fun.is_null()) return -1; 79 80 Handle<JSObject> global(isolate->context()->global_object()); 81 Execution::Call(isolate, fun, global, 0, NULL).Check(); 82 return GetGlobalProperty("result")->Number(); 83 } 84 85 86 TEST(Inc) { 87 CcTest::InitializeVM(); 88 v8::HandleScope scope(CcTest::isolate()); 89 CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3)); 90 } 91 92 93 static double Add(Isolate* isolate, int x, int y) { 94 Handle<JSFunction> fun = Compile("result = x + y;"); 95 if (fun.is_null()) return -1; 96 97 SetGlobalProperty("x", Smi::FromInt(x)); 98 SetGlobalProperty("y", Smi::FromInt(y)); 99 Handle<JSObject> global(isolate->context()->global_object()); 100 Execution::Call(isolate, fun, global, 0, NULL).Check(); 101 return GetGlobalProperty("result")->Number(); 102 } 103 104 105 TEST(Add) { 106 CcTest::InitializeVM(); 107 v8::HandleScope scope(CcTest::isolate()); 108 CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3)); 109 } 110 111 112 static double Abs(Isolate* isolate, int x) { 113 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); 114 if (fun.is_null()) return -1; 115 116 SetGlobalProperty("x", Smi::FromInt(x)); 117 Handle<JSObject> global(isolate->context()->global_object()); 118 Execution::Call(isolate, fun, global, 0, NULL).Check(); 119 return GetGlobalProperty("result")->Number(); 120 } 121 122 123 TEST(Abs) { 124 CcTest::InitializeVM(); 125 v8::HandleScope scope(CcTest::isolate()); 126 CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3)); 127 } 128 129 130 static double Sum(Isolate* isolate, int n) { 131 Handle<JSFunction> fun = 132 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); 133 if (fun.is_null()) return -1; 134 135 SetGlobalProperty("n", Smi::FromInt(n)); 136 Handle<JSObject> global(isolate->context()->global_object()); 137 Execution::Call(isolate, fun, global, 0, NULL).Check(); 138 return GetGlobalProperty("result")->Number(); 139 } 140 141 142 TEST(Sum) { 143 CcTest::InitializeVM(); 144 v8::HandleScope scope(CcTest::isolate()); 145 CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100)); 146 } 147 148 149 TEST(Print) { 150 v8::HandleScope scope(CcTest::isolate()); 151 v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION); 152 v8::Context::Scope context_scope(context); 153 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; 154 Handle<JSFunction> fun = Compile(source); 155 if (fun.is_null()) return; 156 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object()); 157 Execution::Call(CcTest::i_isolate(), fun, global, 0, NULL).Check(); 158 } 159 160 161 // The following test method stems from my coding efforts today. It 162 // tests all the functionality I have added to the compiler today 163 TEST(Stuff) { 164 CcTest::InitializeVM(); 165 v8::HandleScope scope(CcTest::isolate()); 166 const char* source = 167 "r = 0;\n" 168 "a = new Object;\n" 169 "if (a == a) r+=1;\n" // 1 170 "if (a != new Object()) r+=2;\n" // 2 171 "a.x = 42;\n" 172 "if (a.x == 42) r+=4;\n" // 4 173 "function foo() { var x = 87; return x; }\n" 174 "if (foo() == 87) r+=8;\n" // 8 175 "function bar() { var x; x = 99; return x; }\n" 176 "if (bar() == 99) r+=16;\n" // 16 177 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n" 178 "if (baz() == 6) r+=32;\n" // 32 179 "function Cons0() { this.x = 42; this.y = 87; }\n" 180 "if (new Cons0().x == 42) r+=64;\n" // 64 181 "if (new Cons0().y == 87) r+=128;\n" // 128 182 "function Cons2(x, y) { this.sum = x + y; }\n" 183 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 184 185 Handle<JSFunction> fun = Compile(source); 186 CHECK(!fun.is_null()); 187 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object()); 188 Execution::Call( 189 CcTest::i_isolate(), fun, global, 0, NULL).Check(); 190 CHECK_EQ(511.0, GetGlobalProperty("r")->Number()); 191 } 192 193 194 TEST(UncaughtThrow) { 195 CcTest::InitializeVM(); 196 v8::HandleScope scope(CcTest::isolate()); 197 198 const char* source = "throw 42;"; 199 Handle<JSFunction> fun = Compile(source); 200 CHECK(!fun.is_null()); 201 Isolate* isolate = fun->GetIsolate(); 202 Handle<JSObject> global(isolate->context()->global_object()); 203 CHECK(Execution::Call(isolate, fun, global, 0, NULL).is_null()); 204 CHECK_EQ(42.0, isolate->pending_exception()->Number()); 205 } 206 207 208 // Tests calling a builtin function from C/C++ code, and the builtin function 209 // performs GC. It creates a stack frame looks like following: 210 // | C (PerformGC) | 211 // | JS-to-C | 212 // | JS | 213 // | C-to-JS | 214 TEST(C2JSFrames) { 215 FLAG_expose_gc = true; 216 v8::HandleScope scope(CcTest::isolate()); 217 v8::Local<v8::Context> context = 218 CcTest::NewContext(PRINT_EXTENSION | GC_EXTENSION); 219 v8::Context::Scope context_scope(context); 220 221 const char* source = "function foo(a) { gc(), print(a); }"; 222 223 Handle<JSFunction> fun0 = Compile(source); 224 CHECK(!fun0.is_null()); 225 Isolate* isolate = fun0->GetIsolate(); 226 227 // Run the generated code to populate the global object with 'foo'. 228 Handle<JSObject> global(isolate->context()->global_object()); 229 Execution::Call(isolate, fun0, global, 0, NULL).Check(); 230 231 Handle<Object> fun1 = 232 JSReceiver::GetProperty(isolate, isolate->global_object(), "foo") 233 .ToHandleChecked(); 234 CHECK(fun1->IsJSFunction()); 235 236 Handle<Object> argv[] = {isolate->factory()->InternalizeOneByteString( 237 STATIC_CHAR_VECTOR("hello"))}; 238 Execution::Call(isolate, 239 Handle<JSFunction>::cast(fun1), 240 global, 241 arraysize(argv), 242 argv).Check(); 243 } 244 245 246 // Regression 236. Calling InitLineEnds on a Script with undefined 247 // source resulted in crash. 248 TEST(Regression236) { 249 CcTest::InitializeVM(); 250 Isolate* isolate = CcTest::i_isolate(); 251 Factory* factory = isolate->factory(); 252 v8::HandleScope scope(CcTest::isolate()); 253 254 Handle<Script> script = factory->NewScript(factory->empty_string()); 255 script->set_source(CcTest::heap()->undefined_value()); 256 CHECK_EQ(-1, Script::GetLineNumber(script, 0)); 257 CHECK_EQ(-1, Script::GetLineNumber(script, 100)); 258 CHECK_EQ(-1, Script::GetLineNumber(script, -1)); 259 } 260 261 262 TEST(GetScriptLineNumber) { 263 LocalContext context; 264 v8::HandleScope scope(CcTest::isolate()); 265 v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str("test")); 266 const char function_f[] = "function f() {}"; 267 const int max_rows = 1000; 268 const int buffer_size = max_rows + sizeof(function_f); 269 ScopedVector<char> buffer(buffer_size); 270 memset(buffer.start(), '\n', buffer_size - 1); 271 buffer[buffer_size - 1] = '\0'; 272 273 for (int i = 0; i < max_rows; ++i) { 274 if (i > 0) 275 buffer[i - 1] = '\n'; 276 MemCopy(&buffer[i], function_f, sizeof(function_f) - 1); 277 v8::Local<v8::String> script_body = v8_str(buffer.start()); 278 v8::Script::Compile(context.local(), script_body, &origin) 279 .ToLocalChecked() 280 ->Run(context.local()) 281 .ToLocalChecked(); 282 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( 283 context->Global()->Get(context.local(), v8_str("f")).ToLocalChecked()); 284 CHECK_EQ(i, f->GetScriptLineNumber()); 285 } 286 } 287 288 289 TEST(FeedbackVectorPreservedAcrossRecompiles) { 290 if (i::FLAG_always_opt || !i::FLAG_crankshaft) return; 291 i::FLAG_allow_natives_syntax = true; 292 CcTest::InitializeVM(); 293 if (!CcTest::i_isolate()->use_crankshaft()) return; 294 v8::HandleScope scope(CcTest::isolate()); 295 v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); 296 297 // Make sure function f has a call that uses a type feedback slot. 298 CompileRun("function fun() {};" 299 "fun1 = fun;" 300 "function f(a) { a(); } f(fun1);"); 301 302 Handle<JSFunction> f = Handle<JSFunction>::cast( 303 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( 304 CcTest::global()->Get(context, v8_str("f")).ToLocalChecked()))); 305 306 // We shouldn't have deoptimization support. We want to recompile and 307 // verify that our feedback vector preserves information. 308 CHECK(!f->shared()->has_deoptimization_support()); 309 Handle<TypeFeedbackVector> feedback_vector(f->feedback_vector()); 310 311 // Verify that we gathered feedback. 312 CHECK(!feedback_vector->is_empty()); 313 FeedbackVectorSlot slot_for_a(0); 314 Object* object = feedback_vector->Get(slot_for_a); 315 CHECK(object->IsWeakCell() && 316 WeakCell::cast(object)->value()->IsJSFunction()); 317 318 CompileRun("%OptimizeFunctionOnNextCall(f); f(fun1);"); 319 320 // Verify that the feedback is still "gathered" despite a recompilation 321 // of the full code. 322 CHECK(f->IsOptimized()); 323 CHECK(f->shared()->has_deoptimization_support()); 324 object = f->feedback_vector()->Get(slot_for_a); 325 CHECK(object->IsWeakCell() && 326 WeakCell::cast(object)->value()->IsJSFunction()); 327 } 328 329 330 TEST(FeedbackVectorUnaffectedByScopeChanges) { 331 if (i::FLAG_always_opt || !i::FLAG_lazy || 332 (FLAG_ignition && FLAG_ignition_eager)) { 333 return; 334 } 335 CcTest::InitializeVM(); 336 v8::HandleScope scope(CcTest::isolate()); 337 v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); 338 339 CompileRun("function builder() {" 340 " call_target = function() { return 3; };" 341 " return (function() {" 342 " eval('');" 343 " return function() {" 344 " 'use strict';" 345 " call_target();" 346 " }" 347 " })();" 348 "}" 349 "morphing_call = builder();"); 350 351 Handle<JSFunction> f = Handle<JSFunction>::cast(v8::Utils::OpenHandle( 352 *v8::Local<v8::Function>::Cast(CcTest::global() 353 ->Get(context, v8_str("morphing_call")) 354 .ToLocalChecked()))); 355 356 // If we are compiling lazily then it should not be compiled, and so no 357 // feedback vector allocated yet. 358 CHECK(!f->shared()->is_compiled()); 359 CHECK(f->feedback_vector()->is_empty()); 360 361 CompileRun("morphing_call();"); 362 363 // Now a feedback vector is allocated. 364 CHECK(f->shared()->is_compiled()); 365 CHECK(!f->feedback_vector()->is_empty()); 366 } 367 368 // Test that optimized code for different closures is actually shared. 369 TEST(OptimizedCodeSharing1) { 370 FLAG_stress_compaction = false; 371 FLAG_allow_natives_syntax = true; 372 CcTest::InitializeVM(); 373 v8::HandleScope scope(CcTest::isolate()); 374 for (int i = 0; i < 3; i++) { 375 LocalContext env; 376 env->Global() 377 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i)) 378 .FromJust(); 379 CompileRun( 380 "function MakeClosure() {" 381 " return function() { return x; };" 382 "}" 383 "var closure0 = MakeClosure();" 384 "%DebugPrint(closure0());" 385 "%OptimizeFunctionOnNextCall(closure0);" 386 "%DebugPrint(closure0());" 387 "var closure1 = MakeClosure(); closure1();" 388 "var closure2 = MakeClosure(); closure2();"); 389 Handle<JSFunction> fun1 = Handle<JSFunction>::cast( 390 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( 391 env->Global() 392 ->Get(env.local(), v8_str("closure1")) 393 .ToLocalChecked()))); 394 Handle<JSFunction> fun2 = Handle<JSFunction>::cast( 395 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( 396 env->Global() 397 ->Get(env.local(), v8_str("closure2")) 398 .ToLocalChecked()))); 399 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); 400 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); 401 CHECK_EQ(fun1->code(), fun2->code()); 402 } 403 } 404 405 // Test that optimized code for different closures is actually shared. 406 TEST(OptimizedCodeSharing2) { 407 if (FLAG_stress_compaction) return; 408 FLAG_allow_natives_syntax = true; 409 FLAG_native_context_specialization = false; 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() 423 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), 23)) 424 .FromJust(); 425 script->GetUnboundScript() 426 ->BindToCurrentContext() 427 ->Run(env.local()) 428 .ToLocalChecked(); 429 CompileRun( 430 "var closure0 = MakeClosure();" 431 "%DebugPrint(closure0());" 432 "%OptimizeFunctionOnNextCall(closure0);" 433 "%DebugPrint(closure0());"); 434 Handle<JSFunction> fun0 = Handle<JSFunction>::cast( 435 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( 436 env->Global() 437 ->Get(env.local(), v8_str("closure0")) 438 .ToLocalChecked()))); 439 CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); 440 reference_code = handle(fun0->code()); 441 } 442 for (int i = 0; i < 3; i++) { 443 LocalContext env; 444 env->Global() 445 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i)) 446 .FromJust(); 447 script->GetUnboundScript() 448 ->BindToCurrentContext() 449 ->Run(env.local()) 450 .ToLocalChecked(); 451 CompileRun( 452 "var closure0 = MakeClosure();" 453 "%DebugPrint(closure0());" 454 "%OptimizeFunctionOnNextCall(closure0);" 455 "%DebugPrint(closure0());" 456 "var closure1 = MakeClosure(); closure1();" 457 "var closure2 = MakeClosure(); closure2();"); 458 Handle<JSFunction> fun1 = Handle<JSFunction>::cast( 459 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( 460 env->Global() 461 ->Get(env.local(), v8_str("closure1")) 462 .ToLocalChecked()))); 463 Handle<JSFunction> fun2 = Handle<JSFunction>::cast( 464 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( 465 env->Global() 466 ->Get(env.local(), v8_str("closure2")) 467 .ToLocalChecked()))); 468 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); 469 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); 470 CHECK_EQ(*reference_code, fun1->code()); 471 CHECK_EQ(*reference_code, fun2->code()); 472 } 473 } 474 475 // Test that optimized code for different closures is actually shared. 476 TEST(OptimizedCodeSharing3) { 477 if (FLAG_stress_compaction) return; 478 FLAG_allow_natives_syntax = true; 479 FLAG_native_context_specialization = false; 480 FLAG_turbo_cache_shared_code = true; 481 const char* flag = "--turbo-filter=*"; 482 FlagList::SetFlagsFromString(flag, StrLength(flag)); 483 CcTest::InitializeVM(); 484 v8::HandleScope scope(CcTest::isolate()); 485 v8::Local<v8::Script> script = v8_compile( 486 "function MakeClosure() {" 487 " return function() { return x; };" 488 "}"); 489 Handle<Code> reference_code; 490 { 491 LocalContext env; 492 env->Global() 493 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), 23)) 494 .FromJust(); 495 script->GetUnboundScript() 496 ->BindToCurrentContext() 497 ->Run(env.local()) 498 .ToLocalChecked(); 499 CompileRun( 500 "var closure0 = MakeClosure();" 501 "%DebugPrint(closure0());" 502 "%OptimizeFunctionOnNextCall(closure0);" 503 "%DebugPrint(closure0());"); 504 Handle<JSFunction> fun0 = Handle<JSFunction>::cast( 505 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( 506 env->Global() 507 ->Get(env.local(), v8_str("closure0")) 508 .ToLocalChecked()))); 509 CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); 510 reference_code = handle(fun0->code()); 511 // Evict only the context-dependent entry from the optimized code map. This 512 // leaves it in a state where only the context-independent entry exists. 513 fun0->shared()->TrimOptimizedCodeMap(SharedFunctionInfo::kEntryLength); 514 } 515 for (int i = 0; i < 3; i++) { 516 LocalContext env; 517 env->Global() 518 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i)) 519 .FromJust(); 520 script->GetUnboundScript() 521 ->BindToCurrentContext() 522 ->Run(env.local()) 523 .ToLocalChecked(); 524 CompileRun( 525 "var closure0 = MakeClosure();" 526 "%DebugPrint(closure0());" 527 "%OptimizeFunctionOnNextCall(closure0);" 528 "%DebugPrint(closure0());" 529 "var closure1 = MakeClosure(); closure1();" 530 "var closure2 = MakeClosure(); closure2();"); 531 Handle<JSFunction> fun1 = Handle<JSFunction>::cast( 532 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( 533 env->Global() 534 ->Get(env.local(), v8_str("closure1")) 535 .ToLocalChecked()))); 536 Handle<JSFunction> fun2 = Handle<JSFunction>::cast( 537 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( 538 env->Global() 539 ->Get(env.local(), v8_str("closure2")) 540 .ToLocalChecked()))); 541 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); 542 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft()); 543 CHECK_EQ(*reference_code, fun1->code()); 544 CHECK_EQ(*reference_code, fun2->code()); 545 } 546 } 547 548 549 TEST(CompileFunctionInContext) { 550 CcTest::InitializeVM(); 551 v8::HandleScope scope(CcTest::isolate()); 552 LocalContext env; 553 CompileRun("var r = 10;"); 554 v8::Local<v8::Object> math = v8::Local<v8::Object>::Cast( 555 env->Global()->Get(env.local(), v8_str("Math")).ToLocalChecked()); 556 v8::ScriptCompiler::Source script_source(v8_str( 557 "a = PI * r * r;" 558 "x = r * cos(PI);" 559 "y = r * sin(PI / 2);")); 560 v8::Local<v8::Function> fun = 561 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source, 562 0, NULL, 1, &math) 563 .ToLocalChecked(); 564 CHECK(!fun.IsEmpty()); 565 fun->Call(env.local(), env->Global(), 0, NULL).ToLocalChecked(); 566 CHECK(env->Global()->Has(env.local(), v8_str("a")).FromJust()); 567 v8::Local<v8::Value> a = 568 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked(); 569 CHECK(a->IsNumber()); 570 CHECK(env->Global()->Has(env.local(), v8_str("x")).FromJust()); 571 v8::Local<v8::Value> x = 572 env->Global()->Get(env.local(), v8_str("x")).ToLocalChecked(); 573 CHECK(x->IsNumber()); 574 CHECK(env->Global()->Has(env.local(), v8_str("y")).FromJust()); 575 v8::Local<v8::Value> y = 576 env->Global()->Get(env.local(), v8_str("y")).ToLocalChecked(); 577 CHECK(y->IsNumber()); 578 CHECK_EQ(314.1592653589793, a->NumberValue(env.local()).FromJust()); 579 CHECK_EQ(-10.0, x->NumberValue(env.local()).FromJust()); 580 CHECK_EQ(10.0, y->NumberValue(env.local()).FromJust()); 581 } 582 583 584 TEST(CompileFunctionInContextComplex) { 585 CcTest::InitializeVM(); 586 v8::HandleScope scope(CcTest::isolate()); 587 LocalContext env; 588 CompileRun( 589 "var x = 1;" 590 "var y = 2;" 591 "var z = 4;" 592 "var a = {x: 8, y: 16};" 593 "var b = {x: 32};"); 594 v8::Local<v8::Object> ext[2]; 595 ext[0] = v8::Local<v8::Object>::Cast( 596 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked()); 597 ext[1] = v8::Local<v8::Object>::Cast( 598 env->Global()->Get(env.local(), v8_str("b")).ToLocalChecked()); 599 v8::ScriptCompiler::Source script_source(v8_str("result = x + y + z")); 600 v8::Local<v8::Function> fun = 601 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source, 602 0, NULL, 2, ext) 603 .ToLocalChecked(); 604 CHECK(!fun.IsEmpty()); 605 fun->Call(env.local(), env->Global(), 0, NULL).ToLocalChecked(); 606 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust()); 607 v8::Local<v8::Value> result = 608 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked(); 609 CHECK(result->IsNumber()); 610 CHECK_EQ(52.0, result->NumberValue(env.local()).FromJust()); 611 } 612 613 614 TEST(CompileFunctionInContextArgs) { 615 CcTest::InitializeVM(); 616 v8::HandleScope scope(CcTest::isolate()); 617 LocalContext env; 618 CompileRun("var a = {x: 23};"); 619 v8::Local<v8::Object> ext[1]; 620 ext[0] = v8::Local<v8::Object>::Cast( 621 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked()); 622 v8::ScriptCompiler::Source script_source(v8_str("result = x + b")); 623 v8::Local<v8::String> arg = v8_str("b"); 624 v8::Local<v8::Function> fun = 625 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source, 626 1, &arg, 1, ext) 627 .ToLocalChecked(); 628 CHECK(!fun.IsEmpty()); 629 v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0); 630 fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked(); 631 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust()); 632 v8::Local<v8::Value> result = 633 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked(); 634 CHECK(result->IsNumber()); 635 CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust()); 636 } 637 638 639 TEST(CompileFunctionInContextComments) { 640 CcTest::InitializeVM(); 641 v8::HandleScope scope(CcTest::isolate()); 642 LocalContext env; 643 CompileRun("var a = {x: 23, y: 1, z: 2};"); 644 v8::Local<v8::Object> ext[1]; 645 ext[0] = v8::Local<v8::Object>::Cast( 646 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked()); 647 v8::ScriptCompiler::Source script_source( 648 v8_str("result = /* y + */ x + b // + z")); 649 v8::Local<v8::String> arg = v8_str("b"); 650 v8::Local<v8::Function> fun = 651 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source, 652 1, &arg, 1, ext) 653 .ToLocalChecked(); 654 CHECK(!fun.IsEmpty()); 655 v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0); 656 fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked(); 657 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust()); 658 v8::Local<v8::Value> result = 659 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked(); 660 CHECK(result->IsNumber()); 661 CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust()); 662 } 663 664 665 TEST(CompileFunctionInContextNonIdentifierArgs) { 666 CcTest::InitializeVM(); 667 v8::HandleScope scope(CcTest::isolate()); 668 LocalContext env; 669 v8::ScriptCompiler::Source script_source(v8_str("result = 1")); 670 v8::Local<v8::String> arg = v8_str("b }"); 671 CHECK(v8::ScriptCompiler::CompileFunctionInContext( 672 env.local(), &script_source, 1, &arg, 0, NULL) 673 .IsEmpty()); 674 } 675 676 677 TEST(CompileFunctionInContextScriptOrigin) { 678 CcTest::InitializeVM(); 679 v8::HandleScope scope(CcTest::isolate()); 680 LocalContext env; 681 v8::ScriptOrigin origin(v8_str("test"), 682 v8::Integer::New(CcTest::isolate(), 22), 683 v8::Integer::New(CcTest::isolate(), 41)); 684 v8::ScriptCompiler::Source script_source(v8_str("throw new Error()"), origin); 685 v8::Local<v8::Function> fun = 686 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source, 687 0, NULL, 0, NULL) 688 .ToLocalChecked(); 689 CHECK(!fun.IsEmpty()); 690 v8::TryCatch try_catch(CcTest::isolate()); 691 CcTest::isolate()->SetCaptureStackTraceForUncaughtExceptions(true); 692 CHECK(fun->Call(env.local(), env->Global(), 0, NULL).IsEmpty()); 693 CHECK(try_catch.HasCaught()); 694 CHECK(!try_catch.Exception().IsEmpty()); 695 v8::Local<v8::StackTrace> stack = 696 v8::Exception::GetStackTrace(try_catch.Exception()); 697 CHECK(!stack.IsEmpty()); 698 CHECK(stack->GetFrameCount() > 0); 699 v8::Local<v8::StackFrame> frame = stack->GetFrame(0); 700 CHECK_EQ(23, frame->GetLineNumber()); 701 CHECK_EQ(42 + strlen("throw "), static_cast<unsigned>(frame->GetColumn())); 702 } 703 704 705 #ifdef ENABLE_DISASSEMBLER 706 static Handle<JSFunction> GetJSFunction(v8::Local<v8::Object> obj, 707 const char* property_name) { 708 v8::Local<v8::Function> fun = v8::Local<v8::Function>::Cast( 709 obj->Get(CcTest::isolate()->GetCurrentContext(), v8_str(property_name)) 710 .ToLocalChecked()); 711 return Handle<JSFunction>::cast(v8::Utils::OpenHandle(*fun)); 712 } 713 714 715 static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) { 716 // Create a disassembler with default name lookup. 717 disasm::NameConverter name_converter; 718 disasm::Disassembler d(name_converter); 719 720 if (f->code()->kind() == Code::FUNCTION) { 721 Address pc = f->code()->instruction_start(); 722 int decode_size = 723 Min(f->code()->instruction_size(), 724 static_cast<int>(f->code()->back_edge_table_offset())); 725 if (FLAG_enable_embedded_constant_pool) { 726 decode_size = Min(decode_size, f->code()->constant_pool_offset()); 727 } 728 Address end = pc + decode_size; 729 730 v8::internal::EmbeddedVector<char, 128> decode_buffer; 731 v8::internal::EmbeddedVector<char, 128> smi_hex_buffer; 732 Smi* smi = Smi::FromInt(12345678); 733 SNPrintF(smi_hex_buffer, "0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(smi)); 734 while (pc < end) { 735 int num_const = d.ConstantPoolSizeAt(pc); 736 if (num_const >= 0) { 737 pc += (num_const + 1) * kPointerSize; 738 } else { 739 pc += d.InstructionDecode(decode_buffer, pc); 740 CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL); 741 } 742 } 743 } 744 } 745 746 747 TEST(SplitConstantsInFullCompiler) { 748 LocalContext context; 749 v8::HandleScope scope(CcTest::isolate()); 750 751 CompileRun("function f() { a = 12345678 }; f();"); 752 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); 753 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); 754 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); 755 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); 756 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); 757 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); 758 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); 759 } 760 #endif 761 762 static void IsBaselineCompiled( 763 const v8::FunctionCallbackInfo<v8::Value>& args) { 764 Handle<Object> object = v8::Utils::OpenHandle(*args[0]); 765 Handle<JSFunction> function = Handle<JSFunction>::cast(object); 766 bool is_baseline = function->shared()->code()->kind() == Code::FUNCTION; 767 return args.GetReturnValue().Set(is_baseline); 768 } 769 770 static void InstallIsBaselineCompiledHelper(v8::Isolate* isolate) { 771 v8::Local<v8::Context> context = isolate->GetCurrentContext(); 772 v8::Local<v8::FunctionTemplate> t = 773 v8::FunctionTemplate::New(isolate, IsBaselineCompiled); 774 CHECK(context->Global() 775 ->Set(context, v8_str("IsBaselineCompiled"), 776 t->GetFunction(context).ToLocalChecked()) 777 .FromJust()); 778 } 779 780 TEST(IgnitionBaselineOnReturn) { 781 FLAG_allow_natives_syntax = true; 782 FLAG_always_opt = false; 783 CcTest::InitializeVM(); 784 FLAG_ignition = true; 785 reinterpret_cast<i::Isolate*>(CcTest::isolate())->interpreter()->Initialize(); 786 v8::HandleScope scope(CcTest::isolate()); 787 InstallIsBaselineCompiledHelper(CcTest::isolate()); 788 789 CompileRun( 790 "var is_baseline_in_function, is_baseline_after_return;\n" 791 "var return_val;\n" 792 "function f() {\n" 793 " %CompileBaseline(f);\n" 794 " is_baseline_in_function = IsBaselineCompiled(f);\n" 795 " return 1234;\n" 796 "};\n" 797 "return_val = f();\n" 798 "is_baseline_after_return = IsBaselineCompiled(f);\n"); 799 CHECK_EQ(false, GetGlobalProperty("is_baseline_in_function")->BooleanValue()); 800 CHECK_EQ(true, GetGlobalProperty("is_baseline_after_return")->BooleanValue()); 801 CHECK_EQ(1234.0, GetGlobalProperty("return_val")->Number()); 802 } 803