// Copyright 2012 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following // disclaimer in the documentation and/or other materials provided // with the distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include "src/init/v8.h" #include "src/api/api-inl.h" #include "src/codegen/compilation-cache.h" #include "src/codegen/compiler.h" #include "src/diagnostics/disasm.h" #include "src/heap/factory.h" #include "src/heap/spaces.h" #include "src/interpreter/interpreter.h" #include "src/objects/allocation-site-inl.h" #include "src/objects/objects-inl.h" #include "test/cctest/cctest.h" namespace v8 { namespace internal { static Handle GetGlobalProperty(const char* name) { Isolate* isolate = CcTest::i_isolate(); return JSReceiver::GetProperty(isolate, isolate->global_object(), name) .ToHandleChecked(); } static void SetGlobalProperty(const char* name, Object value) { Isolate* isolate = CcTest::i_isolate(); Handle object(value, isolate); Handle internalized_name = isolate->factory()->InternalizeUtf8String(name); Handle global(isolate->context().global_object(), isolate); Runtime::SetObjectProperty(isolate, global, internalized_name, object, StoreOrigin::kMaybeKeyed, Just(kDontThrow)) .Check(); } static Handle Compile(const char* source) { Isolate* isolate = CcTest::i_isolate(); Handle source_code = isolate->factory()->NewStringFromUtf8( CStrVector(source)).ToHandleChecked(); Handle shared = Compiler::GetSharedFunctionInfoForScript( isolate, source_code, Compiler::ScriptDetails(), v8::ScriptOriginOptions(), nullptr, nullptr, v8::ScriptCompiler::kNoCompileOptions, ScriptCompiler::kNoCacheNoReason, NOT_NATIVES_CODE) .ToHandleChecked(); return isolate->factory()->NewFunctionFromSharedFunctionInfo( shared, isolate->native_context()); } static double Inc(Isolate* isolate, int x) { const char* source = "result = %d + 1;"; EmbeddedVector buffer; SNPrintF(buffer, source, x); Handle fun = Compile(buffer.begin()); if (fun.is_null()) return -1; Handle global(isolate->context().global_object(), isolate); Execution::Call(isolate, fun, global, 0, nullptr).Check(); return GetGlobalProperty("result")->Number(); } TEST(Inc) { CcTest::InitializeVM(); v8::HandleScope scope(CcTest::isolate()); CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3)); } static double Add(Isolate* isolate, int x, int y) { Handle fun = Compile("result = x + y;"); if (fun.is_null()) return -1; SetGlobalProperty("x", Smi::FromInt(x)); SetGlobalProperty("y", Smi::FromInt(y)); Handle global(isolate->context().global_object(), isolate); Execution::Call(isolate, fun, global, 0, nullptr).Check(); return GetGlobalProperty("result")->Number(); } TEST(Add) { CcTest::InitializeVM(); v8::HandleScope scope(CcTest::isolate()); CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3)); } static double Abs(Isolate* isolate, int x) { Handle fun = Compile("if (x < 0) result = -x; else result = x;"); if (fun.is_null()) return -1; SetGlobalProperty("x", Smi::FromInt(x)); Handle global(isolate->context().global_object(), isolate); Execution::Call(isolate, fun, global, 0, nullptr).Check(); return GetGlobalProperty("result")->Number(); } TEST(Abs) { CcTest::InitializeVM(); v8::HandleScope scope(CcTest::isolate()); CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3)); } static double Sum(Isolate* isolate, int n) { Handle fun = Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); if (fun.is_null()) return -1; SetGlobalProperty("n", Smi::FromInt(n)); Handle global(isolate->context().global_object(), isolate); Execution::Call(isolate, fun, global, 0, nullptr).Check(); return GetGlobalProperty("result")->Number(); } TEST(Sum) { CcTest::InitializeVM(); v8::HandleScope scope(CcTest::isolate()); CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100)); } TEST(Print) { v8::HandleScope scope(CcTest::isolate()); v8::Local context = CcTest::NewContext({PRINT_EXTENSION_ID}); v8::Context::Scope context_scope(context); const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; Handle fun = Compile(source); if (fun.is_null()) return; Handle global(CcTest::i_isolate()->context().global_object(), fun->GetIsolate()); Execution::Call(CcTest::i_isolate(), fun, global, 0, nullptr).Check(); } // The following test method stems from my coding efforts today. It // tests all the functionality I have added to the compiler today TEST(Stuff) { CcTest::InitializeVM(); v8::HandleScope scope(CcTest::isolate()); const char* source = "r = 0;\n" "a = new Object;\n" "if (a == a) r+=1;\n" // 1 "if (a != new Object()) r+=2;\n" // 2 "a.x = 42;\n" "if (a.x == 42) r+=4;\n" // 4 "function foo() { var x = 87; return x; }\n" "if (foo() == 87) r+=8;\n" // 8 "function bar() { var x; x = 99; return x; }\n" "if (bar() == 99) r+=16;\n" // 16 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n" "if (baz() == 6) r+=32;\n" // 32 "function Cons0() { this.x = 42; this.y = 87; }\n" "if (new Cons0().x == 42) r+=64;\n" // 64 "if (new Cons0().y == 87) r+=128;\n" // 128 "function Cons2(x, y) { this.sum = x + y; }\n" "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 Handle fun = Compile(source); CHECK(!fun.is_null()); Handle global(CcTest::i_isolate()->context().global_object(), fun->GetIsolate()); Execution::Call(CcTest::i_isolate(), fun, global, 0, nullptr).Check(); CHECK_EQ(511.0, GetGlobalProperty("r")->Number()); } TEST(UncaughtThrow) { CcTest::InitializeVM(); v8::HandleScope scope(CcTest::isolate()); const char* source = "throw 42;"; Handle fun = Compile(source); CHECK(!fun.is_null()); Isolate* isolate = fun->GetIsolate(); Handle global(isolate->context().global_object(), isolate); CHECK(Execution::Call(isolate, fun, global, 0, nullptr).is_null()); CHECK_EQ(42.0, isolate->pending_exception().Number()); } // Tests calling a builtin function from C/C++ code, and the builtin function // performs GC. It creates a stack frame looks like following: // | C (PerformGC) | // | JS-to-C | // | JS | // | C-to-JS | TEST(C2JSFrames) { FLAG_expose_gc = true; v8::HandleScope scope(CcTest::isolate()); v8::Local context = CcTest::NewContext({PRINT_EXTENSION_ID, GC_EXTENSION_ID}); v8::Context::Scope context_scope(context); const char* source = "function foo(a) { gc(), print(a); }"; Handle fun0 = Compile(source); CHECK(!fun0.is_null()); Isolate* isolate = fun0->GetIsolate(); // Run the generated code to populate the global object with 'foo'. Handle global(isolate->context().global_object(), isolate); Execution::Call(isolate, fun0, global, 0, nullptr).Check(); Handle fun1 = JSReceiver::GetProperty(isolate, isolate->global_object(), "foo") .ToHandleChecked(); CHECK(fun1->IsJSFunction()); Handle argv[] = { isolate->factory()->InternalizeString(StaticCharVector("hello"))}; Execution::Call(isolate, Handle::cast(fun1), global, arraysize(argv), argv).Check(); } // Regression 236. Calling InitLineEnds on a Script with undefined // source resulted in crash. TEST(Regression236) { CcTest::InitializeVM(); Isolate* isolate = CcTest::i_isolate(); Factory* factory = isolate->factory(); v8::HandleScope scope(CcTest::isolate()); Handle