// 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/execution.h" #include "src/handles.h" #include "test/cctest/cctest.h" namespace v8 { namespace internal { class CallInterfaceDescriptor; class Isolate; namespace compiler { class Graph; class FunctionTester : public InitializedHandleScope { public: explicit FunctionTester(const char* source, uint32_t flags = 0); FunctionTester(Graph* graph, int param_count); FunctionTester(Handle code, int param_count); // Assumes VoidDescriptor call interface. explicit FunctionTester(Handle code); Isolate* isolate; CanonicalHandleScope canonical; Handle function; MaybeHandle Call() { return Execution::Call(isolate, function, undefined(), 0, nullptr); } template MaybeHandle Call(Arg1 arg1, Args... args) { const int nof_args = sizeof...(Args) + 1; Handle call_args[] = {arg1, args...}; return Execution::Call(isolate, function, undefined(), nof_args, call_args); } template Handle CallChecked(Args... args) { Handle result = Call(args...).ToHandleChecked(); return Handle::cast(result); } void CheckThrows(Handle a); void CheckThrows(Handle a, Handle b); v8::Local CheckThrowsReturnMessage(Handle a, Handle b); void CheckCall(Handle expected, Handle a, Handle b, Handle c, Handle d); void CheckCall(Handle expected, Handle a, Handle b, Handle c) { return CheckCall(expected, a, b, c, undefined()); } void CheckCall(Handle expected, Handle a, Handle b) { return CheckCall(expected, a, b, undefined()); } void CheckCall(Handle expected, Handle a) { CheckCall(expected, a, undefined()); } void CheckCall(Handle expected) { CheckCall(expected, undefined()); } void CheckCall(double expected, double a, double b) { CheckCall(Val(expected), Val(a), Val(b)); } void CheckTrue(Handle a) { CheckCall(true_value(), a); } void CheckTrue(Handle a, Handle b) { CheckCall(true_value(), a, b); } void CheckTrue(Handle a, Handle b, Handle c) { CheckCall(true_value(), a, b, c); } void CheckTrue(Handle a, Handle b, Handle c, Handle d) { CheckCall(true_value(), a, b, c, d); } void CheckTrue(double a, double b) { CheckCall(true_value(), Val(a), Val(b)); } void CheckFalse(Handle a) { CheckCall(false_value(), a); } void CheckFalse(Handle a, Handle b) { CheckCall(false_value(), a, b); } void CheckFalse(double a, double b) { CheckCall(false_value(), Val(a), Val(b)); } Handle NewFunction(const char* source); Handle NewObject(const char* source); Handle Val(const char* string); Handle Val(double value); Handle infinity(); Handle minus_infinity(); Handle nan(); Handle undefined(); Handle null(); Handle true_value(); Handle false_value(); static Handle ForMachineGraph(Graph* graph, int param_count); private: uint32_t flags_; Handle Compile(Handle 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; } // 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); }; } // namespace compiler } // namespace internal } // namespace v8 #endif // V8_CCTEST_COMPILER_FUNCTION_TESTER_H_