blob: 76dc9acae390c6df9aa0c4f6fd585672a6cc8e11 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/frames-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006#include "test/cctest/cctest.h"
7#include "test/cctest/compiler/function-tester.h"
8
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009namespace v8 {
10namespace internal {
11namespace compiler {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013static void IsOptimized(const v8::FunctionCallbackInfo<v8::Value>& args) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014 JavaScriptFrameIterator it(CcTest::i_isolate());
15 JavaScriptFrame* frame = it.frame();
16 return args.GetReturnValue().Set(frame->is_optimized());
17}
18
19
20static void InstallIsOptimizedHelper(v8::Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021 v8::Local<v8::Context> context = isolate->GetCurrentContext();
22 v8::Local<v8::FunctionTemplate> t =
23 v8::FunctionTemplate::New(isolate, IsOptimized);
24 CHECK(context->Global()
25 ->Set(context, v8_str("IsOptimized"),
26 t->GetFunction(context).ToLocalChecked())
27 .FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028}
29
30
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031TEST(DeoptSimple) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032 FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033
34 FunctionTester T(
35 "(function f(a) {"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036 " var b = 1;"
37 " if (!IsOptimized()) return 0;"
38 " %DeoptimizeFunction(f);"
39 " if (IsOptimized()) return 0;"
40 " return a + b;"
41 "})");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042
43 InstallIsOptimizedHelper(CcTest::isolate());
44 T.CheckCall(T.Val(2), T.Val(1));
45}
46
47
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048TEST(DeoptSimpleInExpr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049 FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050
51 FunctionTester T(
52 "(function f(a) {"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053 " var b = 1;"
54 " var c = 2;"
55 " if (!IsOptimized()) return 0;"
56 " var d = b + (%DeoptimizeFunction(f), c);"
57 " if (IsOptimized()) return 0;"
58 " return d + a;"
59 "})");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060
61 InstallIsOptimizedHelper(CcTest::isolate());
62 T.CheckCall(T.Val(6), T.Val(3));
63}
64
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066TEST(DeoptExceptionHandlerCatch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 FLAG_allow_natives_syntax = true;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068
69 FunctionTester T(
70 "(function f() {"
71 " var is_opt = IsOptimized;"
72 " try {"
73 " DeoptAndThrow(f);"
74 " } catch (e) {"
75 " return is_opt();"
76 " }"
77 "})");
78
79 CompileRun("function DeoptAndThrow(f) { %DeoptimizeFunction(f); throw 0; }");
80 InstallIsOptimizedHelper(CcTest::isolate());
81 T.CheckCall(T.false_value());
82}
83
84
85TEST(DeoptExceptionHandlerFinally) {
86 FLAG_allow_natives_syntax = true;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087
88 FunctionTester T(
89 "(function f() {"
90 " var is_opt = IsOptimized;"
91 " try {"
92 " DeoptAndThrow(f);"
93 " } finally {"
94 " return is_opt();"
95 " }"
96 "})");
97
98 CompileRun("function DeoptAndThrow(f) { %DeoptimizeFunction(f); throw 0; }");
99 InstallIsOptimizedHelper(CcTest::isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100 T.CheckCall(T.false_value());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101}
102
103
104TEST(DeoptTrivial) {
105 FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106
107 FunctionTester T(
108 "(function foo() {"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109 " %DeoptimizeFunction(foo);"
110 " return 1;"
111 "})");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112
113 T.CheckCall(T.Val(1));
114}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115
116} // namespace compiler
117} // namespace internal
118} // namespace v8