// Copyright 2014 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "test/cctest/compiler/function-tester.h" #include "src/ast/ast-numbering.h" #include "src/compilation-info.h" #include "src/compiler.h" #include "src/compiler/linkage.h" #include "src/compiler/pipeline.h" #include "src/execution.h" #include "src/full-codegen/full-codegen.h" #include "src/handles.h" #include "src/objects-inl.h" #include "src/parsing/parse-info.h" #include "src/parsing/parser.h" #include "test/cctest/cctest.h" namespace v8 { namespace internal { namespace compiler { FunctionTester::FunctionTester(const char* source, uint32_t flags) : isolate(main_isolate()), function((FLAG_allow_natives_syntax = true, NewFunction(source))), flags_(flags) { Compile(function); const uint32_t supported_flags = CompilationInfo::kInliningEnabled; CHECK_EQ(0u, flags_ & ~supported_flags); } FunctionTester::FunctionTester(Graph* graph, int param_count) : isolate(main_isolate()), function(NewFunction(BuildFunction(param_count).c_str())), flags_(0) { CompileGraph(graph); } FunctionTester::FunctionTester(Handle code, int param_count) : isolate(main_isolate()), function((FLAG_allow_natives_syntax = true, NewFunction(BuildFunction(param_count).c_str()))), flags_(0) { Compile(function); function->ReplaceCode(*code); } FunctionTester::FunctionTester(const CallInterfaceDescriptor& descriptor, Handle code) : FunctionTester(code, descriptor.GetParameterCount()) {} MaybeHandle FunctionTester::Call() { return Execution::Call(isolate, function, undefined(), 0, nullptr); } MaybeHandle FunctionTester::Call(Handle a) { Handle args[] = {a}; return Execution::Call(isolate, function, undefined(), 1, args); } MaybeHandle FunctionTester::Call(Handle a, Handle b) { Handle args[] = {a, b}; return Execution::Call(isolate, function, undefined(), 2, args); } MaybeHandle FunctionTester::Call(Handle a, Handle b, Handle c) { Handle args[] = {a, b, c}; return Execution::Call(isolate, function, undefined(), 3, args); } MaybeHandle FunctionTester::Call(Handle a, Handle b, Handle c, Handle d) { Handle args[] = {a, b, c, d}; return Execution::Call(isolate, function, undefined(), 4, args); } void FunctionTester::CheckThrows(Handle a, Handle b) { TryCatch try_catch(reinterpret_cast(isolate)); MaybeHandle no_result = Call(a, b); CHECK(isolate->has_pending_exception()); CHECK(try_catch.HasCaught()); CHECK(no_result.is_null()); isolate->OptionalRescheduleException(true); } v8::Local FunctionTester::CheckThrowsReturnMessage( Handle a, Handle b) { TryCatch try_catch(reinterpret_cast(isolate)); MaybeHandle no_result = Call(a, b); CHECK(isolate->has_pending_exception()); CHECK(try_catch.HasCaught()); CHECK(no_result.is_null()); isolate->OptionalRescheduleException(true); CHECK(!try_catch.Message().IsEmpty()); return try_catch.Message(); } void FunctionTester::CheckCall(Handle expected, Handle a, Handle b, Handle c, Handle d) { Handle result = Call(a, b, c, d).ToHandleChecked(); CHECK(expected->SameValue(*result)); } Handle FunctionTester::NewFunction(const char* source) { return Handle::cast(v8::Utils::OpenHandle( *v8::Local::Cast(CompileRun(source)))); } Handle FunctionTester::NewObject(const char* source) { return Handle::cast( v8::Utils::OpenHandle(*v8::Local::Cast(CompileRun(source)))); } Handle FunctionTester::Val(const char* string) { return isolate->factory()->InternalizeUtf8String(string); } Handle FunctionTester::Val(double value) { return isolate->factory()->NewNumber(value); } Handle FunctionTester::infinity() { return isolate->factory()->infinity_value(); } Handle FunctionTester::minus_infinity() { return Val(-V8_INFINITY); } Handle FunctionTester::nan() { return isolate->factory()->nan_value(); } Handle FunctionTester::undefined() { return isolate->factory()->undefined_value(); } Handle FunctionTester::null() { return isolate->factory()->null_value(); } Handle FunctionTester::true_value() { return isolate->factory()->true_value(); } Handle FunctionTester::false_value() { return isolate->factory()->false_value(); } Handle FunctionTester::ForMachineGraph(Graph* graph, int param_count) { JSFunction* p = NULL; { // because of the implicit handle scope of FunctionTester. FunctionTester f(graph, param_count); p = *f.function; } return Handle(p); // allocated in outer handle scope. } Handle FunctionTester::Compile(Handle function) { Zone zone(function->GetIsolate()->allocator(), ZONE_NAME); ParseInfo parse_info(&zone, handle(function->shared())); CompilationInfo info(&parse_info, function); info.SetOptimizing(); info.MarkAsDeoptimizationEnabled(); if (flags_ & CompilationInfo::kInliningEnabled) { info.MarkAsInliningEnabled(); } if (Compiler::EnsureBytecode(&info)) { info.MarkAsOptimizeFromBytecode(); } else { CHECK(Compiler::ParseAndAnalyze(info.parse_info())); CHECK(Compiler::EnsureDeoptimizationSupport(&info)); } JSFunction::EnsureLiterals(function); Handle code = Pipeline::GenerateCodeForTesting(&info); CHECK(!code.is_null()); info.dependencies()->Commit(code); info.context()->native_context()->AddOptimizedCode(*code); function->ReplaceCode(*code); return function; } // Compile the given machine graph instead of the source of the function // and replace the JSFunction's code with the result. Handle FunctionTester::CompileGraph(Graph* graph) { Zone zone(function->GetIsolate()->allocator(), ZONE_NAME); ParseInfo parse_info(&zone, handle(function->shared())); CompilationInfo info(&parse_info, function); CHECK(Parser::ParseStatic(info.parse_info())); info.SetOptimizing(); Handle code = Pipeline::GenerateCodeForTesting(&info, graph); CHECK(!code.is_null()); function->ReplaceCode(*code); return function; } } // namespace compiler } // namespace internal } // namespace v8