// 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. #ifndef V8_CCTEST_COMPILER_FUNCTION_TESTER_H_ #define V8_CCTEST_COMPILER_FUNCTION_TESTER_H_ #include "src/ast/ast-numbering.h" #include "src/ast/scopes.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/parser.h" #include "src/parsing/rewriter.h" #include "test/cctest/cctest.h" namespace v8 { namespace internal { namespace compiler { class FunctionTester : public InitializedHandleScope { public: explicit FunctionTester(const char* source, uint32_t flags = 0) : isolate(main_isolate()), function((FLAG_allow_natives_syntax = true, NewFunction(source))), flags_(flags) { Compile(function); const uint32_t supported_flags = CompilationInfo::kFunctionContextSpecializing | CompilationInfo::kInliningEnabled; CHECK_EQ(0u, flags_ & ~supported_flags); } FunctionTester(Graph* graph, int param_count) : isolate(main_isolate()), function(NewFunction(BuildFunction(param_count).c_str())), flags_(0) { CompileGraph(graph); } FunctionTester(const CallInterfaceDescriptor& descriptor, Handle code) : isolate(main_isolate()), function( (FLAG_allow_natives_syntax = true, NewFunction(BuildFunctionFromDescriptor(descriptor).c_str()))), flags_(0) { Compile(function); function->ReplaceCode(*code); } Isolate* isolate; Handle function; MaybeHandle Call() { return Execution::Call(isolate, function, undefined(), 0, nullptr); } MaybeHandle Call(Handle a, Handle b) { Handle args[] = {a, b}; return Execution::Call(isolate, function, undefined(), 2, args); } MaybeHandle Call(Handle a, Handle b, Handle c, Handle d) { Handle args[] = {a, b, c, d}; return Execution::Call(isolate, function, undefined(), 4, args); } void 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 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 CheckCall(Handle expected, Handle a, Handle b) { Handle result = Call(a, b).ToHandleChecked(); CHECK(expected->SameValue(*result)); } void CheckCall(Handle expected, Handle a) { CheckCall(expected, a, undefined()); } void CheckCall(Handle expected) { CheckCall(expected, undefined(), undefined()); } void CheckCall(double expected, double a, double b) { CheckCall(Val(expected), Val(a), Val(b)); } void CheckTrue(Handle a, Handle b) { CheckCall(true_value(), a, b); } void CheckTrue(Handle a) { CheckCall(true_value(), a, undefined()); } void CheckTrue(double a, double b) { CheckCall(true_value(), Val(a), Val(b)); } void CheckFalse(Handle a, Handle b) { CheckCall(false_value(), a, b); } void CheckFalse(Handle a) { CheckCall(false_value(), a, undefined()); } void CheckFalse(double a, double b) { CheckCall(false_value(), Val(a), Val(b)); } Handle NewFunction(const char* source) { return Handle::cast(v8::Utils::OpenHandle( *v8::Local::Cast(CompileRun(source)))); } Handle NewObject(const char* source) { return Handle::cast(v8::Utils::OpenHandle( *v8::Local::Cast(CompileRun(source)))); } Handle Val(const char* string) { return isolate->factory()->InternalizeUtf8String(string); } Handle Val(double value) { return isolate->factory()->NewNumber(value); } Handle infinity() { return isolate->factory()->infinity_value(); } Handle minus_infinity() { return Val(-V8_INFINITY); } Handle nan() { return isolate->factory()->nan_value(); } Handle undefined() { return isolate->factory()->undefined_value(); } Handle null() { return isolate->factory()->null_value(); } Handle true_value() { return isolate->factory()->true_value(); } Handle false_value() { return isolate->factory()->false_value(); } static Handle 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. } private: uint32_t flags_; Handle Compile(Handle function) { Zone zone(function->GetIsolate()->allocator()); ParseInfo parse_info(&zone, function); CompilationInfo info(&parse_info, function); info.MarkAsDeoptimizationEnabled(); CHECK(Parser::ParseStatic(info.parse_info())); info.SetOptimizing(); if (flags_ & CompilationInfo::kFunctionContextSpecializing) { info.MarkAsFunctionContextSpecializing(); } if (flags_ & CompilationInfo::kInliningEnabled) { info.MarkAsInliningEnabled(); } if (FLAG_turbo_from_bytecode && function->shared()->HasBytecodeArray()) { info.MarkAsOptimizeFromBytecode(); } else { CHECK(Compiler::Analyze(info.parse_info())); CHECK(Compiler::EnsureDeoptimizationSupport(&info)); } Handle code = Pipeline::GenerateCodeForTesting(&info); CHECK(!code.is_null()); info.dependencies()->Commit(code); info.context()->native_context()->AddOptimizedCode(*code); function->ReplaceCode(*code); return function; } std::string BuildFunction(int param_count) { std::string function_string = "(function("; if (param_count > 0) { function_string += 'a'; for (int i = 1; i < param_count; i++) { function_string += ','; function_string += static_cast('a' + i); } } function_string += "){})"; return function_string; } std::string BuildFunctionFromDescriptor( const CallInterfaceDescriptor& descriptor) { return BuildFunction(descriptor.GetParameterCount()); } // Compile the given machine graph instead of the source of the function // and replace the JSFunction's code with the result. Handle CompileGraph(Graph* graph) { Zone zone(function->GetIsolate()->allocator()); ParseInfo parse_info(&zone, function); 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 #endif // V8_CCTEST_COMPILER_FUNCTION_TESTER_H_