// 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 "include/v8-function.h" #include "src/api/api-inl.h" #include "src/codegen/assembler.h" #include "src/codegen/optimized-compilation-info.h" #include "src/compiler/linkage.h" #include "src/compiler/pipeline.h" #include "src/execution/execution.h" #include "src/handles/handles.h" #include "src/objects/objects-inl.h" #include "src/parsing/parse-info.h" #include "test/cctest/cctest.h" namespace v8 { namespace internal { namespace compiler { FunctionTester::FunctionTester(const char* source, uint32_t flags) : isolate(main_isolate()), function((v8_flags.allow_natives_syntax = true, NewFunction(source))), flags_(flags) { Compile(function); const uint32_t supported_flags = OptimizedCompilationInfo::kInlining; CHECK_EQ(0u, flags_ & ~supported_flags); } FunctionTester::FunctionTester(DirectHandle code, int param_count) : isolate(main_isolate()), function((v8_flags.allow_natives_syntax = true, NewFunction(BuildFunction(param_count).c_str()))), flags_(0) { CHECK(!code.is_null()); CHECK(IsCode(*code)); Compile(function); function->UpdateCode(*code); } FunctionTester::FunctionTester(DirectHandle code) : FunctionTester(code, 0) {} void FunctionTester::CheckThrows(Handle a) { TryCatch try_catch(reinterpret_cast(isolate)); MaybeDirectHandle no_result = Call(a); CHECK(isolate->has_exception()); CHECK(try_catch.HasCaught()); CHECK(no_result.is_null()); } void FunctionTester::CheckThrows(Handle a, Handle b) { TryCatch try_catch(reinterpret_cast(isolate)); MaybeDirectHandle no_result = Call(a, b); CHECK(isolate->has_exception()); CHECK(try_catch.HasCaught()); CHECK(no_result.is_null()); } v8::Local FunctionTester::CheckThrowsReturnMessage( Handle a, Handle b) { TryCatch try_catch(reinterpret_cast(isolate)); MaybeDirectHandle no_result = Call(a, b); CHECK(isolate->has_exception()); CHECK(try_catch.HasCaught()); CHECK(no_result.is_null()); CHECK(!try_catch.Message().IsEmpty()); return try_catch.Message(); } void FunctionTester::CheckCall(DirectHandle expected, Handle a, Handle b, Handle c, Handle d) { DirectHandle result = Call(a, b, c, d).ToHandleChecked(); CHECK(Object::SameValue(*expected, *result)); } Handle FunctionTester::NewFunction(const char* source) { return Cast(v8::Utils::OpenHandle( *v8::Local::Cast(CompileRun(source)))); } DirectHandle FunctionTester::NewObject(const char* source) { return 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); } DirectHandle FunctionTester::infinity() { return isolate->factory()->infinity_value(); } DirectHandle FunctionTester::minus_infinity() { return Val(-V8_INFINITY); } DirectHandle FunctionTester::nan() { return isolate->factory()->nan_value(); } Handle FunctionTester::undefined() { return isolate->factory()->undefined_value(); } DirectHandle 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::Compile(Handle f) { Zone zone(isolate->allocator(), ZONE_NAME); return Optimize(f, &zone, isolate, flags_); } } // namespace compiler } // namespace internal } // namespace v8