LCOV - code coverage report
Current view: top level - test/cctest - test-compiler.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 323 323 100.0 %
Date: 2017-10-20 Functions: 30 30 100.0 %

          Line data    Source code
       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/api.h"
      34             : #include "src/compiler.h"
      35             : #include "src/disasm.h"
      36             : #include "src/factory.h"
      37             : #include "src/interpreter/interpreter.h"
      38             : #include "src/objects-inl.h"
      39             : #include "test/cctest/cctest.h"
      40             : 
      41             : namespace v8 {
      42             : namespace internal {
      43             : 
      44          36 : static Handle<Object> GetGlobalProperty(const char* name) {
      45             :   Isolate* isolate = CcTest::i_isolate();
      46          72 :   return JSReceiver::GetProperty(isolate, isolate->global_object(), name)
      47         108 :       .ToHandleChecked();
      48             : }
      49             : 
      50             : 
      51          24 : static void SetGlobalProperty(const char* name, Object* value) {
      52          24 :   Isolate* isolate = CcTest::i_isolate();
      53             :   Handle<Object> object(value, isolate);
      54             :   Handle<String> internalized_name =
      55          24 :       isolate->factory()->InternalizeUtf8String(name);
      56          24 :   Handle<JSObject> global(isolate->context()->global_object());
      57             :   Runtime::SetObjectProperty(isolate, global, internalized_name, object,
      58          24 :                              LanguageMode::kSloppy)
      59          48 :       .Check();
      60          24 : }
      61             : 
      62             : 
      63          48 : static Handle<JSFunction> Compile(const char* source) {
      64             :   Isolate* isolate = CcTest::i_isolate();
      65             :   Handle<String> source_code = isolate->factory()->NewStringFromUtf8(
      66          96 :       CStrVector(source)).ToHandleChecked();
      67             :   Handle<SharedFunctionInfo> shared =
      68             :       Compiler::GetSharedFunctionInfoForScript(
      69             :           source_code, MaybeHandle<String>(), 0, 0, v8::ScriptOriginOptions(),
      70             :           MaybeHandle<Object>(), Handle<Context>(isolate->native_context()),
      71             :           nullptr, nullptr, v8::ScriptCompiler::kNoCompileOptions,
      72          96 :           NOT_NATIVES_CODE, MaybeHandle<FixedArray>())
      73          96 :           .ToHandleChecked();
      74             :   return isolate->factory()->NewFunctionFromSharedFunctionInfo(
      75          48 :       shared, isolate->native_context());
      76             : }
      77             : 
      78             : 
      79          12 : static double Inc(Isolate* isolate, int x) {
      80             :   const char* source = "result = %d + 1;";
      81             :   EmbeddedVector<char, 512> buffer;
      82           6 :   SNPrintF(buffer, source, x);
      83             : 
      84           6 :   Handle<JSFunction> fun = Compile(buffer.start());
      85           6 :   if (fun.is_null()) return -1;
      86             : 
      87           6 :   Handle<JSObject> global(isolate->context()->global_object());
      88          12 :   Execution::Call(isolate, fun, global, 0, nullptr).Check();
      89          12 :   return GetGlobalProperty("result")->Number();
      90             : }
      91             : 
      92             : 
      93       23724 : TEST(Inc) {
      94           6 :   CcTest::InitializeVM();
      95           6 :   v8::HandleScope scope(CcTest::isolate());
      96           6 :   CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3));
      97           6 : }
      98             : 
      99             : 
     100          12 : static double Add(Isolate* isolate, int x, int y) {
     101           6 :   Handle<JSFunction> fun = Compile("result = x + y;");
     102           6 :   if (fun.is_null()) return -1;
     103             : 
     104           6 :   SetGlobalProperty("x", Smi::FromInt(x));
     105           6 :   SetGlobalProperty("y", Smi::FromInt(y));
     106           6 :   Handle<JSObject> global(isolate->context()->global_object());
     107          12 :   Execution::Call(isolate, fun, global, 0, nullptr).Check();
     108          12 :   return GetGlobalProperty("result")->Number();
     109             : }
     110             : 
     111             : 
     112       23724 : TEST(Add) {
     113           6 :   CcTest::InitializeVM();
     114           6 :   v8::HandleScope scope(CcTest::isolate());
     115           6 :   CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3));
     116           6 : }
     117             : 
     118             : 
     119          12 : static double Abs(Isolate* isolate, int x) {
     120           6 :   Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
     121           6 :   if (fun.is_null()) return -1;
     122             : 
     123           6 :   SetGlobalProperty("x", Smi::FromInt(x));
     124           6 :   Handle<JSObject> global(isolate->context()->global_object());
     125          12 :   Execution::Call(isolate, fun, global, 0, nullptr).Check();
     126          12 :   return GetGlobalProperty("result")->Number();
     127             : }
     128             : 
     129             : 
     130       23724 : TEST(Abs) {
     131           6 :   CcTest::InitializeVM();
     132           6 :   v8::HandleScope scope(CcTest::isolate());
     133           6 :   CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3));
     134           6 : }
     135             : 
     136             : 
     137          12 : static double Sum(Isolate* isolate, int n) {
     138             :   Handle<JSFunction> fun =
     139           6 :       Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
     140           6 :   if (fun.is_null()) return -1;
     141             : 
     142           6 :   SetGlobalProperty("n", Smi::FromInt(n));
     143           6 :   Handle<JSObject> global(isolate->context()->global_object());
     144          12 :   Execution::Call(isolate, fun, global, 0, nullptr).Check();
     145          12 :   return GetGlobalProperty("result")->Number();
     146             : }
     147             : 
     148             : 
     149       23724 : TEST(Sum) {
     150           6 :   CcTest::InitializeVM();
     151           6 :   v8::HandleScope scope(CcTest::isolate());
     152           6 :   CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100));
     153           6 : }
     154             : 
     155             : 
     156       23724 : TEST(Print) {
     157           6 :   v8::HandleScope scope(CcTest::isolate());
     158           6 :   v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION);
     159             :   v8::Context::Scope context_scope(context);
     160             :   const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
     161           6 :   Handle<JSFunction> fun = Compile(source);
     162          12 :   if (fun.is_null()) return;
     163           6 :   Handle<JSObject> global(CcTest::i_isolate()->context()->global_object());
     164          18 :   Execution::Call(CcTest::i_isolate(), fun, global, 0, nullptr).Check();
     165             : }
     166             : 
     167             : 
     168             : // The following test method stems from my coding efforts today. It
     169             : // tests all the functionality I have added to the compiler today
     170       23724 : TEST(Stuff) {
     171           6 :   CcTest::InitializeVM();
     172           6 :   v8::HandleScope scope(CcTest::isolate());
     173             :   const char* source =
     174             :     "r = 0;\n"
     175             :     "a = new Object;\n"
     176             :     "if (a == a) r+=1;\n"  // 1
     177             :     "if (a != new Object()) r+=2;\n"  // 2
     178             :     "a.x = 42;\n"
     179             :     "if (a.x == 42) r+=4;\n"  // 4
     180             :     "function foo() { var x = 87; return x; }\n"
     181             :     "if (foo() == 87) r+=8;\n"  // 8
     182             :     "function bar() { var x; x = 99; return x; }\n"
     183             :     "if (bar() == 99) r+=16;\n"  // 16
     184             :     "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n"
     185             :     "if (baz() == 6) r+=32;\n"  // 32
     186             :     "function Cons0() { this.x = 42; this.y = 87; }\n"
     187             :     "if (new Cons0().x == 42) r+=64;\n"  // 64
     188             :     "if (new Cons0().y == 87) r+=128;\n"  // 128
     189             :     "function Cons2(x, y) { this.sum = x + y; }\n"
     190             :     "if (new Cons2(3,4).sum == 7) r+=256;";  // 256
     191             : 
     192           6 :   Handle<JSFunction> fun = Compile(source);
     193           6 :   CHECK(!fun.is_null());
     194           6 :   Handle<JSObject> global(CcTest::i_isolate()->context()->global_object());
     195          12 :   Execution::Call(CcTest::i_isolate(), fun, global, 0, nullptr).Check();
     196          12 :   CHECK_EQ(511.0, GetGlobalProperty("r")->Number());
     197           6 : }
     198             : 
     199             : 
     200       23724 : TEST(UncaughtThrow) {
     201           6 :   CcTest::InitializeVM();
     202           6 :   v8::HandleScope scope(CcTest::isolate());
     203             : 
     204             :   const char* source = "throw 42;";
     205           6 :   Handle<JSFunction> fun = Compile(source);
     206           6 :   CHECK(!fun.is_null());
     207          12 :   Isolate* isolate = fun->GetIsolate();
     208           6 :   Handle<JSObject> global(isolate->context()->global_object());
     209          12 :   CHECK(Execution::Call(isolate, fun, global, 0, nullptr).is_null());
     210           6 :   CHECK_EQ(42.0, isolate->pending_exception()->Number());
     211           6 : }
     212             : 
     213             : 
     214             : // Tests calling a builtin function from C/C++ code, and the builtin function
     215             : // performs GC. It creates a stack frame looks like following:
     216             : //   | C (PerformGC) |
     217             : //   |   JS-to-C     |
     218             : //   |      JS       |
     219             : //   |   C-to-JS     |
     220       23724 : TEST(C2JSFrames) {
     221           6 :   FLAG_expose_gc = true;
     222           6 :   v8::HandleScope scope(CcTest::isolate());
     223             :   v8::Local<v8::Context> context =
     224          12 :     CcTest::NewContext(PRINT_EXTENSION | GC_EXTENSION);
     225             :   v8::Context::Scope context_scope(context);
     226             : 
     227             :   const char* source = "function foo(a) { gc(), print(a); }";
     228             : 
     229           6 :   Handle<JSFunction> fun0 = Compile(source);
     230           6 :   CHECK(!fun0.is_null());
     231           6 :   Isolate* isolate = fun0->GetIsolate();
     232             : 
     233             :   // Run the generated code to populate the global object with 'foo'.
     234           6 :   Handle<JSObject> global(isolate->context()->global_object());
     235          12 :   Execution::Call(isolate, fun0, global, 0, nullptr).Check();
     236             : 
     237             :   Handle<Object> fun1 =
     238          12 :       JSReceiver::GetProperty(isolate, isolate->global_object(), "foo")
     239          12 :           .ToHandleChecked();
     240           6 :   CHECK(fun1->IsJSFunction());
     241             : 
     242             :   Handle<Object> argv[] = {isolate->factory()->InternalizeOneByteString(
     243          12 :       STATIC_CHAR_VECTOR("hello"))};
     244             :   Execution::Call(isolate,
     245             :                   Handle<JSFunction>::cast(fun1),
     246             :                   global,
     247             :                   arraysize(argv),
     248          18 :                   argv).Check();
     249           6 : }
     250             : 
     251             : 
     252             : // Regression 236. Calling InitLineEnds on a Script with undefined
     253             : // source resulted in crash.
     254       23724 : TEST(Regression236) {
     255           6 :   CcTest::InitializeVM();
     256             :   Isolate* isolate = CcTest::i_isolate();
     257             :   Factory* factory = isolate->factory();
     258           6 :   v8::HandleScope scope(CcTest::isolate());
     259             : 
     260           6 :   Handle<Script> script = factory->NewScript(factory->empty_string());
     261          12 :   script->set_source(CcTest::heap()->undefined_value());
     262           6 :   CHECK_EQ(-1, Script::GetLineNumber(script, 0));
     263           6 :   CHECK_EQ(-1, Script::GetLineNumber(script, 100));
     264           6 :   CHECK_EQ(-1, Script::GetLineNumber(script, -1));
     265           6 : }
     266             : 
     267             : 
     268       23724 : TEST(GetScriptLineNumber) {
     269           6 :   LocalContext context;
     270          12 :   v8::HandleScope scope(CcTest::isolate());
     271           6 :   v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str("test"));
     272           6 :   const char function_f[] = "function f() {}";
     273             :   const int max_rows = 1000;
     274             :   const int buffer_size = max_rows + sizeof(function_f);
     275             :   ScopedVector<char> buffer(buffer_size);
     276             :   memset(buffer.start(), '\n', buffer_size - 1);
     277           6 :   buffer[buffer_size - 1] = '\0';
     278             : 
     279        6006 :   for (int i = 0; i < max_rows; ++i) {
     280        6000 :     if (i > 0)
     281       11988 :       buffer[i - 1] = '\n';
     282        6000 :     MemCopy(&buffer[i], function_f, sizeof(function_f) - 1);
     283        6000 :     v8::Local<v8::String> script_body = v8_str(buffer.start());
     284        6000 :     v8::Script::Compile(context.local(), script_body, &origin)
     285        6000 :         .ToLocalChecked()
     286        6000 :         ->Run(context.local())
     287        6000 :         .ToLocalChecked();
     288             :     v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
     289       30000 :         context->Global()->Get(context.local(), v8_str("f")).ToLocalChecked());
     290        6000 :     CHECK_EQ(i, f->GetScriptLineNumber());
     291           6 :   }
     292           6 : }
     293             : 
     294             : 
     295       23724 : TEST(FeedbackVectorPreservedAcrossRecompiles) {
     296           8 :   if (i::FLAG_always_opt || !i::FLAG_opt) return;
     297           4 :   i::FLAG_allow_natives_syntax = true;
     298           4 :   CcTest::InitializeVM();
     299           4 :   if (!CcTest::i_isolate()->use_optimizer()) return;
     300           4 :   v8::HandleScope scope(CcTest::isolate());
     301           4 :   v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
     302             : 
     303             :   // Make sure function f has a call that uses a type feedback slot.
     304             :   CompileRun("function fun() {};"
     305             :              "fun1 = fun;"
     306             :              "function f(a) { a(); } f(fun1);");
     307             : 
     308             :   Handle<JSFunction> f = Handle<JSFunction>::cast(
     309             :       v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
     310          16 :           CcTest::global()->Get(context, v8_str("f")).ToLocalChecked())));
     311             : 
     312             :   // Verify that we gathered feedback.
     313             :   Handle<FeedbackVector> feedback_vector(f->feedback_vector());
     314           4 :   CHECK(!feedback_vector->is_empty());
     315             :   FeedbackSlot slot_for_a(0);
     316             :   Object* object = feedback_vector->Get(slot_for_a);
     317           8 :   CHECK(object->IsWeakCell() &&
     318             :         WeakCell::cast(object)->value()->IsJSFunction());
     319             : 
     320             :   CompileRun("%OptimizeFunctionOnNextCall(f); f(fun1);");
     321             : 
     322             :   // Verify that the feedback is still "gathered" despite a recompilation
     323             :   // of the full code.
     324           4 :   CHECK(f->IsOptimized());
     325             :   object = f->feedback_vector()->Get(slot_for_a);
     326           8 :   CHECK(object->IsWeakCell() &&
     327           4 :         WeakCell::cast(object)->value()->IsJSFunction());
     328             : }
     329             : 
     330             : 
     331       23724 : TEST(FeedbackVectorUnaffectedByScopeChanges) {
     332           6 :   if (i::FLAG_always_opt || !i::FLAG_lazy) {
     333           1 :     return;
     334             :   }
     335           5 :   CcTest::InitializeVM();
     336           5 :   v8::HandleScope scope(CcTest::isolate());
     337           5 :   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          15 :                                          ->Get(context, v8_str("morphing_call"))
     354           5 :                                          .ToLocalChecked())));
     355             : 
     356             :   // If we are compiling lazily then it should not be compiled, and so no
     357             :   // feedback vector allocated yet.
     358           5 :   CHECK(!f->shared()->is_compiled());
     359             : 
     360             :   CompileRun("morphing_call();");
     361             : 
     362             :   // Now a feedback vector is allocated.
     363           5 :   CHECK(f->shared()->is_compiled());
     364           5 :   CHECK(!f->feedback_vector()->is_empty());
     365             : }
     366             : 
     367             : // Test that optimized code for different closures is actually shared.
     368       23724 : TEST(OptimizedCodeSharing1) {
     369           6 :   FLAG_stress_compaction = false;
     370           6 :   FLAG_allow_natives_syntax = true;
     371           6 :   CcTest::InitializeVM();
     372           6 :   v8::HandleScope scope(CcTest::isolate());
     373          24 :   for (int i = 0; i < 3; i++) {
     374          18 :     LocalContext env;
     375             :     env->Global()
     376          90 :         ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i))
     377          36 :         .FromJust();
     378             :     CompileRun(
     379             :         "function MakeClosure() {"
     380             :         "  return function() { return x; };"
     381             :         "}"
     382             :         "var closure0 = MakeClosure();"
     383             :         "var closure1 = MakeClosure();"  // We only share optimized code
     384             :                                          // if there are at least two closures.
     385             :         "%DebugPrint(closure0());"
     386             :         "%OptimizeFunctionOnNextCall(closure0);"
     387             :         "%DebugPrint(closure0());"
     388             :         "closure1();"
     389             :         "var closure2 = MakeClosure(); closure2();");
     390             :     Handle<JSFunction> fun1 = Handle<JSFunction>::cast(
     391             :         v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
     392             :             env->Global()
     393          72 :                 ->Get(env.local(), v8_str("closure1"))
     394          18 :                 .ToLocalChecked())));
     395             :     Handle<JSFunction> fun2 = Handle<JSFunction>::cast(
     396             :         v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
     397             :             env->Global()
     398          72 :                 ->Get(env.local(), v8_str("closure2"))
     399          18 :                 .ToLocalChecked())));
     400          21 :     CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_optimizer());
     401          21 :     CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_optimizer());
     402          18 :     CHECK_EQ(fun1->code(), fun2->code());
     403          24 :   }
     404           6 : }
     405             : 
     406       23724 : TEST(CompileFunctionInContext) {
     407           6 :   CcTest::InitializeVM();
     408           6 :   v8::HandleScope scope(CcTest::isolate());
     409          12 :   LocalContext env;
     410             :   CompileRun("var r = 10;");
     411             :   v8::Local<v8::Object> math = v8::Local<v8::Object>::Cast(
     412          30 :       env->Global()->Get(env.local(), v8_str("Math")).ToLocalChecked());
     413             :   v8::ScriptCompiler::Source script_source(v8_str(
     414             :       "a = PI * r * r;"
     415             :       "x = r * cos(PI);"
     416           6 :       "y = r * sin(PI / 2);"));
     417             :   v8::Local<v8::Function> fun =
     418             :       v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
     419           6 :                                                    0, nullptr, 1, &math)
     420           6 :           .ToLocalChecked();
     421           6 :   CHECK(!fun.IsEmpty());
     422          18 :   fun->Call(env.local(), env->Global(), 0, nullptr).ToLocalChecked();
     423          30 :   CHECK(env->Global()->Has(env.local(), v8_str("a")).FromJust());
     424             :   v8::Local<v8::Value> a =
     425          30 :       env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked();
     426           6 :   CHECK(a->IsNumber());
     427          30 :   CHECK(env->Global()->Has(env.local(), v8_str("x")).FromJust());
     428             :   v8::Local<v8::Value> x =
     429          30 :       env->Global()->Get(env.local(), v8_str("x")).ToLocalChecked();
     430           6 :   CHECK(x->IsNumber());
     431          30 :   CHECK(env->Global()->Has(env.local(), v8_str("y")).FromJust());
     432             :   v8::Local<v8::Value> y =
     433          30 :       env->Global()->Get(env.local(), v8_str("y")).ToLocalChecked();
     434           6 :   CHECK(y->IsNumber());
     435          12 :   CHECK_EQ(314.1592653589793, a->NumberValue(env.local()).FromJust());
     436          12 :   CHECK_EQ(-10.0, x->NumberValue(env.local()).FromJust());
     437          18 :   CHECK_EQ(10.0, y->NumberValue(env.local()).FromJust());
     438           6 : }
     439             : 
     440             : 
     441       23724 : TEST(CompileFunctionInContextComplex) {
     442           6 :   CcTest::InitializeVM();
     443           6 :   v8::HandleScope scope(CcTest::isolate());
     444          12 :   LocalContext env;
     445             :   CompileRun(
     446             :       "var x = 1;"
     447             :       "var y = 2;"
     448             :       "var z = 4;"
     449             :       "var a = {x: 8, y: 16};"
     450             :       "var b = {x: 32};");
     451          18 :   v8::Local<v8::Object> ext[2];
     452             :   ext[0] = v8::Local<v8::Object>::Cast(
     453          30 :       env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
     454             :   ext[1] = v8::Local<v8::Object>::Cast(
     455          30 :       env->Global()->Get(env.local(), v8_str("b")).ToLocalChecked());
     456           6 :   v8::ScriptCompiler::Source script_source(v8_str("result = x + y + z"));
     457             :   v8::Local<v8::Function> fun =
     458             :       v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
     459           6 :                                                    0, nullptr, 2, ext)
     460           6 :           .ToLocalChecked();
     461           6 :   CHECK(!fun.IsEmpty());
     462          18 :   fun->Call(env.local(), env->Global(), 0, nullptr).ToLocalChecked();
     463          30 :   CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
     464             :   v8::Local<v8::Value> result =
     465          30 :       env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
     466           6 :   CHECK(result->IsNumber());
     467          18 :   CHECK_EQ(52.0, result->NumberValue(env.local()).FromJust());
     468           6 : }
     469             : 
     470             : 
     471       23724 : TEST(CompileFunctionInContextArgs) {
     472           6 :   CcTest::InitializeVM();
     473           6 :   v8::HandleScope scope(CcTest::isolate());
     474          12 :   LocalContext env;
     475             :   CompileRun("var a = {x: 23};");
     476          12 :   v8::Local<v8::Object> ext[1];
     477             :   ext[0] = v8::Local<v8::Object>::Cast(
     478          30 :       env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
     479           6 :   v8::ScriptCompiler::Source script_source(v8_str("result = x + b"));
     480           6 :   v8::Local<v8::String> arg = v8_str("b");
     481             :   v8::Local<v8::Function> fun =
     482             :       v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
     483           6 :                                                    1, &arg, 1, ext)
     484           6 :           .ToLocalChecked();
     485           6 :   CHECK(!fun.IsEmpty());
     486           6 :   v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
     487          18 :   fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
     488          30 :   CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
     489             :   v8::Local<v8::Value> result =
     490          30 :       env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
     491           6 :   CHECK(result->IsNumber());
     492          18 :   CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust());
     493           6 : }
     494             : 
     495             : 
     496       23724 : TEST(CompileFunctionInContextComments) {
     497           6 :   CcTest::InitializeVM();
     498           6 :   v8::HandleScope scope(CcTest::isolate());
     499          12 :   LocalContext env;
     500             :   CompileRun("var a = {x: 23, y: 1, z: 2};");
     501          12 :   v8::Local<v8::Object> ext[1];
     502             :   ext[0] = v8::Local<v8::Object>::Cast(
     503          30 :       env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
     504             :   v8::ScriptCompiler::Source script_source(
     505           6 :       v8_str("result = /* y + */ x + b // + z"));
     506           6 :   v8::Local<v8::String> arg = v8_str("b");
     507             :   v8::Local<v8::Function> fun =
     508             :       v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
     509           6 :                                                    1, &arg, 1, ext)
     510           6 :           .ToLocalChecked();
     511           6 :   CHECK(!fun.IsEmpty());
     512           6 :   v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
     513          18 :   fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
     514          30 :   CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
     515             :   v8::Local<v8::Value> result =
     516          30 :       env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
     517           6 :   CHECK(result->IsNumber());
     518          18 :   CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust());
     519           6 : }
     520             : 
     521             : 
     522       23724 : TEST(CompileFunctionInContextNonIdentifierArgs) {
     523           6 :   CcTest::InitializeVM();
     524           6 :   v8::HandleScope scope(CcTest::isolate());
     525          12 :   LocalContext env;
     526           6 :   v8::ScriptCompiler::Source script_source(v8_str("result = 1"));
     527           6 :   v8::Local<v8::String> arg = v8_str("b }");
     528          12 :   CHECK(v8::ScriptCompiler::CompileFunctionInContext(
     529             :             env.local(), &script_source, 1, &arg, 0, nullptr)
     530           6 :             .IsEmpty());
     531           6 : }
     532             : 
     533             : 
     534       23724 : TEST(CompileFunctionInContextScriptOrigin) {
     535           6 :   CcTest::InitializeVM();
     536           6 :   v8::HandleScope scope(CcTest::isolate());
     537          12 :   LocalContext env;
     538             :   v8::ScriptOrigin origin(v8_str("test"),
     539             :                           v8::Integer::New(CcTest::isolate(), 22),
     540           6 :                           v8::Integer::New(CcTest::isolate(), 41));
     541           6 :   v8::ScriptCompiler::Source script_source(v8_str("throw new Error()"), origin);
     542             :   v8::Local<v8::Function> fun =
     543             :       v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
     544           6 :                                                    0, nullptr, 0, nullptr)
     545           6 :           .ToLocalChecked();
     546           6 :   CHECK(!fun.IsEmpty());
     547          12 :   v8::TryCatch try_catch(CcTest::isolate());
     548           6 :   CcTest::isolate()->SetCaptureStackTraceForUncaughtExceptions(true);
     549          18 :   CHECK(fun->Call(env.local(), env->Global(), 0, nullptr).IsEmpty());
     550           6 :   CHECK(try_catch.HasCaught());
     551          12 :   CHECK(!try_catch.Exception().IsEmpty());
     552             :   v8::Local<v8::StackTrace> stack =
     553           6 :       v8::Exception::GetStackTrace(try_catch.Exception());
     554           6 :   CHECK(!stack.IsEmpty());
     555           6 :   CHECK_GT(stack->GetFrameCount(), 0);
     556           6 :   v8::Local<v8::StackFrame> frame = stack->GetFrame(0);
     557           6 :   CHECK_EQ(23, frame->GetLineNumber());
     558          12 :   CHECK_EQ(42 + strlen("throw "), static_cast<unsigned>(frame->GetColumn()));
     559           6 : }
     560             : 
     561       23724 : TEST(CompileFunctionInContextHarmonyFunctionToString) {
     562             : #define CHECK_NOT_CAUGHT(__local_context__, try_catch, __op__)                \
     563             :   do {                                                                        \
     564             :     const char* op = (__op__);                                                \
     565             :     v8::Local<v8::Context> context = (__local_context__);                     \
     566             :     if (try_catch.HasCaught()) {                                              \
     567             :       v8::String::Utf8Value error(                                            \
     568             :           CcTest::isolate(),                                                  \
     569             :           try_catch.Exception()->ToString(context).ToLocalChecked());         \
     570             :       V8_Fatal(__FILE__, __LINE__,                                            \
     571             :                "Unexpected exception thrown during %s:\n\t%s\n", op, *error); \
     572             :     }                                                                         \
     573             :   } while (0)
     574             : 
     575           6 :   auto previous_flag = v8::internal::FLAG_harmony_function_tostring;
     576           6 :   v8::internal::FLAG_harmony_function_tostring = true;
     577             :   {
     578           6 :     CcTest::InitializeVM();
     579           6 :     v8::HandleScope scope(CcTest::isolate());
     580          12 :     LocalContext env;
     581             : 
     582             :     // Regression test for v8:6190
     583             :     {
     584           6 :       v8::ScriptOrigin origin(v8_str("test"), v8_int(22), v8_int(41));
     585           6 :       v8::ScriptCompiler::Source script_source(v8_str("return event"), origin);
     586             : 
     587           6 :       v8::Local<v8::String> params[] = {v8_str("event")};
     588          12 :       v8::TryCatch try_catch(CcTest::isolate());
     589             :       v8::MaybeLocal<v8::Function> maybe_fun =
     590             :           v8::ScriptCompiler::CompileFunctionInContext(
     591             :               env.local(), &script_source, arraysize(params), params, 0,
     592           6 :               nullptr);
     593             : 
     594           6 :       CHECK_NOT_CAUGHT(env.local(), try_catch,
     595             :                        "v8::ScriptCompiler::CompileFunctionInContext");
     596             : 
     597             :       v8::Local<v8::Function> fun = maybe_fun.ToLocalChecked();
     598           6 :       CHECK(!fun.IsEmpty());
     599           6 :       CHECK(!try_catch.HasCaught());
     600             :       v8::Local<v8::String> result =
     601           6 :           fun->ToString(env.local()).ToLocalChecked();
     602             :       v8::Local<v8::String> expected = v8_str(
     603             :           "function(event){return event\n"
     604           6 :           "}");
     605          18 :       CHECK(expected->Equals(env.local(), result).FromJust());
     606             :     }
     607             : 
     608             :     // With no parameters:
     609             :     {
     610           6 :       v8::ScriptOrigin origin(v8_str("test"), v8_int(17), v8_int(31));
     611           6 :       v8::ScriptCompiler::Source script_source(v8_str("return 0"), origin);
     612             : 
     613          12 :       v8::TryCatch try_catch(CcTest::isolate());
     614             :       v8::MaybeLocal<v8::Function> maybe_fun =
     615             :           v8::ScriptCompiler::CompileFunctionInContext(
     616           6 :               env.local(), &script_source, 0, nullptr, 0, nullptr);
     617             : 
     618           6 :       CHECK_NOT_CAUGHT(env.local(), try_catch,
     619             :                        "v8::ScriptCompiler::CompileFunctionInContext");
     620             : 
     621             :       v8::Local<v8::Function> fun = maybe_fun.ToLocalChecked();
     622           6 :       CHECK(!fun.IsEmpty());
     623           6 :       CHECK(!try_catch.HasCaught());
     624             :       v8::Local<v8::String> result =
     625           6 :           fun->ToString(env.local()).ToLocalChecked();
     626             :       v8::Local<v8::String> expected = v8_str(
     627             :           "function(){return 0\n"
     628           6 :           "}");
     629          18 :       CHECK(expected->Equals(env.local(), result).FromJust());
     630           6 :     }
     631             :   }
     632           6 :   v8::internal::FLAG_harmony_function_tostring = previous_flag;
     633             : 
     634             : #undef CHECK_NOT_CAUGHT
     635           6 : }
     636             : 
     637       23724 : TEST(InvocationCount) {
     638           6 :   FLAG_allow_natives_syntax = true;
     639           6 :   FLAG_always_opt = false;
     640           6 :   CcTest::InitializeVM();
     641           6 :   v8::HandleScope scope(CcTest::isolate());
     642             : 
     643             :   CompileRun(
     644             :       "function bar() {};"
     645             :       "function foo() { return bar(); };"
     646             :       "foo();");
     647           6 :   Handle<JSFunction> foo = Handle<JSFunction>::cast(GetGlobalProperty("foo"));
     648           6 :   CHECK_EQ(1, foo->feedback_vector()->invocation_count());
     649             :   CompileRun("foo()");
     650           6 :   CHECK_EQ(2, foo->feedback_vector()->invocation_count());
     651             :   CompileRun("bar()");
     652           6 :   CHECK_EQ(2, foo->feedback_vector()->invocation_count());
     653             :   CompileRun("foo(); foo()");
     654           6 :   CHECK_EQ(4, foo->feedback_vector()->invocation_count());
     655           6 : }
     656             : 
     657             : }  // namespace internal
     658       71154 : }  // namespace v8

Generated by: LCOV version 1.10