1 // Copyright 2015 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
5 #include "src/v8.h"
6
7 #include "src/factory.h"
8 #include "src/heap/heap.h"
9 #include "src/heap/heap-inl.h"
10 #include "src/runtime/runtime.h"
11 #include "test/unittests/test-utils.h"
12
13 namespace v8 {
14 namespace internal {
15 namespace interpreter {
16
17 class RuntimeInterpreterTest : public TestWithIsolateAndZone {
18 public:
19 typedef Object* (*RuntimeMethod)(int, Object**, Isolate*);
20
RuntimeInterpreterTest()21 RuntimeInterpreterTest() {}
~RuntimeInterpreterTest()22 ~RuntimeInterpreterTest() override {}
23
24 bool TestOperatorWithObjects(RuntimeMethod method, Handle<Object> lhs,
25 Handle<Object> rhs, bool expected);
26 bool TestOperator(RuntimeMethod method, int32_t lhs, int32_t rhs,
27 bool expected);
28 bool TestOperator(RuntimeMethod method, double lhs, double rhs,
29 bool expected);
30 bool TestOperator(RuntimeMethod method, const char* lhs, const char* rhs,
31 bool expected);
32 };
33
34
TestOperatorWithObjects(RuntimeMethod method,Handle<Object> lhs,Handle<Object> rhs,bool expected)35 bool RuntimeInterpreterTest::TestOperatorWithObjects(RuntimeMethod method,
36 Handle<Object> lhs,
37 Handle<Object> rhs,
38 bool expected) {
39 Object* args_object[] = {*rhs, *lhs};
40 Handle<Object> result =
41 handle(method(2, &args_object[1], isolate()), isolate());
42 CHECK(result->IsTrue() || result->IsFalse());
43 return result->IsTrue() == expected;
44 }
45
46
TestOperator(RuntimeMethod method,int32_t lhs,int32_t rhs,bool expected)47 bool RuntimeInterpreterTest::TestOperator(RuntimeMethod method, int32_t lhs,
48 int32_t rhs, bool expected) {
49 Handle<Object> x = isolate()->factory()->NewNumberFromInt(lhs);
50 Handle<Object> y = isolate()->factory()->NewNumberFromInt(rhs);
51 return TestOperatorWithObjects(method, x, y, expected);
52 }
53
54
TestOperator(RuntimeMethod method,double lhs,double rhs,bool expected)55 bool RuntimeInterpreterTest::TestOperator(RuntimeMethod method, double lhs,
56 double rhs, bool expected) {
57 Handle<Object> x = isolate()->factory()->NewNumber(lhs);
58 Handle<Object> y = isolate()->factory()->NewNumber(rhs);
59 CHECK_EQ(HeapNumber::cast(*x)->value(), lhs);
60 CHECK_EQ(HeapNumber::cast(*y)->value(), rhs);
61 return TestOperatorWithObjects(method, x, y, expected);
62 }
63
64
TestOperator(RuntimeMethod method,const char * lhs,const char * rhs,bool expected)65 bool RuntimeInterpreterTest::TestOperator(RuntimeMethod method, const char* lhs,
66 const char* rhs, bool expected) {
67 Handle<Object> x = isolate()->factory()->NewStringFromAsciiChecked(lhs);
68 Handle<Object> y = isolate()->factory()->NewStringFromAsciiChecked(rhs);
69 return TestOperatorWithObjects(method, x, y, expected);
70 }
71
72
TEST_F(RuntimeInterpreterTest,TestOperatorsWithIntegers)73 TEST_F(RuntimeInterpreterTest, TestOperatorsWithIntegers) {
74 int32_t inputs[] = {kMinInt, Smi::kMinValue, -17, -1, 0, 1,
75 991, Smi::kMaxValue, kMaxInt};
76 TRACED_FOREACH(int, lhs, inputs) {
77 TRACED_FOREACH(int, rhs, inputs) {
78 #define INTEGER_OPERATOR_CHECK(r, op, x, y) \
79 CHECK(TestOperator(Runtime_Interpreter##r, x, y, x op y))
80 INTEGER_OPERATOR_CHECK(Equals, ==, lhs, rhs);
81 INTEGER_OPERATOR_CHECK(NotEquals, !=, lhs, rhs);
82 INTEGER_OPERATOR_CHECK(StrictEquals, ==, lhs, rhs);
83 INTEGER_OPERATOR_CHECK(StrictNotEquals, !=, lhs, rhs);
84 INTEGER_OPERATOR_CHECK(LessThan, <, lhs, rhs);
85 INTEGER_OPERATOR_CHECK(GreaterThan, >, lhs, rhs);
86 INTEGER_OPERATOR_CHECK(LessThanOrEqual, <=, lhs, rhs);
87 INTEGER_OPERATOR_CHECK(GreaterThanOrEqual, >=, lhs, rhs);
88 #undef INTEGER_OPERATOR_CHECK
89 }
90 }
91 }
92
93
TEST_F(RuntimeInterpreterTest,TestOperatorsWithDoubles)94 TEST_F(RuntimeInterpreterTest, TestOperatorsWithDoubles) {
95 double inputs[] = {std::numeric_limits<double>::min(),
96 std::numeric_limits<double>::max(),
97 -0.001,
98 0.01,
99 3.14,
100 -6.02214086e23};
101 TRACED_FOREACH(double, lhs, inputs) {
102 TRACED_FOREACH(double, rhs, inputs) {
103 #define DOUBLE_OPERATOR_CHECK(r, op, x, y) \
104 CHECK(TestOperator(Runtime_Interpreter##r, x, y, x op y))
105 DOUBLE_OPERATOR_CHECK(Equals, ==, lhs, rhs);
106 DOUBLE_OPERATOR_CHECK(NotEquals, !=, lhs, rhs);
107 DOUBLE_OPERATOR_CHECK(StrictEquals, ==, lhs, rhs);
108 DOUBLE_OPERATOR_CHECK(StrictNotEquals, !=, lhs, rhs);
109 DOUBLE_OPERATOR_CHECK(LessThan, <, lhs, rhs);
110 DOUBLE_OPERATOR_CHECK(GreaterThan, >, lhs, rhs);
111 DOUBLE_OPERATOR_CHECK(LessThanOrEqual, <=, lhs, rhs);
112 DOUBLE_OPERATOR_CHECK(GreaterThanOrEqual, >=, lhs, rhs);
113 #undef DOUBLE_OPERATOR_CHECK
114 }
115 }
116 }
117
118
TEST_F(RuntimeInterpreterTest,TestOperatorsWithString)119 TEST_F(RuntimeInterpreterTest, TestOperatorsWithString) {
120 const char* inputs[] = {"abc", "a", "def", "0"};
121 TRACED_FOREACH(const char*, lhs, inputs) {
122 TRACED_FOREACH(const char*, rhs, inputs) {
123 #define STRING_OPERATOR_CHECK(r, op, x, y) \
124 CHECK(TestOperator(Runtime_Interpreter##r, x, y, \
125 std::string(x) op std::string(y)))
126 STRING_OPERATOR_CHECK(Equals, ==, lhs, rhs);
127 STRING_OPERATOR_CHECK(NotEquals, !=, lhs, rhs);
128 STRING_OPERATOR_CHECK(StrictEquals, ==, lhs, rhs);
129 STRING_OPERATOR_CHECK(StrictNotEquals, !=, lhs, rhs);
130 STRING_OPERATOR_CHECK(LessThan, <, lhs, rhs);
131 STRING_OPERATOR_CHECK(GreaterThan, >, lhs, rhs);
132 STRING_OPERATOR_CHECK(LessThanOrEqual, <=, lhs, rhs);
133 STRING_OPERATOR_CHECK(GreaterThanOrEqual, >=, lhs, rhs);
134 #undef STRING_OPERATOR_CHECK
135 }
136 }
137 }
138
139
TEST_F(RuntimeInterpreterTest,ToBoolean)140 TEST_F(RuntimeInterpreterTest, ToBoolean) {
141 double quiet_nan = std::numeric_limits<double>::quiet_NaN();
142 std::pair<Handle<Object>, bool> cases[] = {
143 std::make_pair(isolate()->factory()->NewNumberFromInt(0), false),
144 std::make_pair(isolate()->factory()->NewNumberFromInt(1), true),
145 std::make_pair(isolate()->factory()->NewNumberFromInt(100), true),
146 std::make_pair(isolate()->factory()->NewNumberFromInt(-1), true),
147 std::make_pair(isolate()->factory()->NewNumber(7.7), true),
148 std::make_pair(isolate()->factory()->NewNumber(0.00001), true),
149 std::make_pair(isolate()->factory()->NewNumber(quiet_nan), false),
150 std::make_pair(isolate()->factory()->NewHeapNumber(0.0), false),
151 std::make_pair(isolate()->factory()->undefined_value(), false),
152 std::make_pair(isolate()->factory()->null_value(), false),
153 std::make_pair(isolate()->factory()->true_value(), true),
154 std::make_pair(isolate()->factory()->false_value(), false),
155 std::make_pair(isolate()->factory()->NewStringFromStaticChars(""), false),
156 std::make_pair(isolate()->factory()->NewStringFromStaticChars("_"), true),
157 };
158
159 for (size_t i = 0; i < arraysize(cases); i++) {
160 auto& value_expected_tuple = cases[i];
161 Object* args_object[] = {*value_expected_tuple.first};
162 Handle<Object> result = handle(
163 Runtime_InterpreterToBoolean(1, &args_object[0], isolate()), isolate());
164 CHECK(result->IsBoolean());
165 CHECK_EQ(result->IsTrue(), value_expected_tuple.second);
166 }
167 }
168
169
170 } // namespace interpreter
171 } // namespace internal
172 } // namespace v8
173