1 // 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
5 #include "test/cctest/compiler/function-tester.h"
6
7 namespace v8 {
8 namespace internal {
9 namespace compiler {
10
11 uint32_t flags = CompilationInfo::kInliningEnabled;
12
13
TEST(Call)14 TEST(Call) {
15 FunctionTester T("(function(a,b) { return %_Call(b, a, 1, 2, 3); })", flags);
16 CompileRun("function f(a,b,c) { return a + b + c + this.d; }");
17
18 T.CheckCall(T.Val(129), T.NewObject("({d:123})"), T.NewObject("f"));
19 T.CheckCall(T.Val("6x"), T.NewObject("({d:'x'})"), T.NewObject("f"));
20 }
21
22
TEST(ClassOf)23 TEST(ClassOf) {
24 FunctionTester T("(function(a) { return %_ClassOf(a); })", flags);
25
26 T.CheckCall(T.Val("Function"), T.NewObject("(function() {})"));
27 T.CheckCall(T.Val("Array"), T.NewObject("([1])"));
28 T.CheckCall(T.Val("Object"), T.NewObject("({})"));
29 T.CheckCall(T.Val("RegExp"), T.NewObject("(/x/)"));
30 T.CheckCall(T.null(), T.undefined());
31 T.CheckCall(T.null(), T.null());
32 T.CheckCall(T.null(), T.Val("x"));
33 T.CheckCall(T.null(), T.Val(1));
34 }
35
36
37 #define COUNTER_NAME "hurz"
38
LookupCounter(const char * name)39 static int* LookupCounter(const char* name) {
40 static int counter = 1234;
41 return strcmp(name, COUNTER_NAME) == 0 ? &counter : nullptr;
42 }
43
44
TEST(IncrementStatsCounter)45 TEST(IncrementStatsCounter) {
46 FLAG_native_code_counters = true;
47 reinterpret_cast<v8::Isolate*>(CcTest::InitIsolateOnce())
48 ->SetCounterFunction(LookupCounter);
49 FunctionTester T(
50 "(function() { %_IncrementStatsCounter('" COUNTER_NAME "'); })", flags);
51 StatsCounter counter(T.main_isolate(), COUNTER_NAME);
52 if (!counter.Enabled()) return;
53
54 int old_value = *counter.GetInternalPointer();
55 T.CheckCall(T.undefined());
56 CHECK_EQ(old_value + 1, *counter.GetInternalPointer());
57 }
58
59 #undef COUNTER_NAME
60
61
TEST(IsArray)62 TEST(IsArray) {
63 FunctionTester T("(function(a) { return %_IsArray(a); })", flags);
64
65 T.CheckFalse(T.NewObject("new Date()"));
66 T.CheckFalse(T.NewObject("(function() {})"));
67 T.CheckTrue(T.NewObject("([1])"));
68 T.CheckFalse(T.NewObject("({})"));
69 T.CheckFalse(T.NewObject("(/x/)"));
70 T.CheckFalse(T.undefined());
71 T.CheckFalse(T.null());
72 T.CheckFalse(T.Val("x"));
73 T.CheckFalse(T.Val(1));
74 }
75
76
TEST(IsDate)77 TEST(IsDate) {
78 FunctionTester T("(function(a) { return %_IsDate(a); })", flags);
79
80 T.CheckTrue(T.NewObject("new Date()"));
81 T.CheckFalse(T.NewObject("(function() {})"));
82 T.CheckFalse(T.NewObject("([1])"));
83 T.CheckFalse(T.NewObject("({})"));
84 T.CheckFalse(T.NewObject("(/x/)"));
85 T.CheckFalse(T.undefined());
86 T.CheckFalse(T.null());
87 T.CheckFalse(T.Val("x"));
88 T.CheckFalse(T.Val(1));
89 }
90
91
TEST(IsFunction)92 TEST(IsFunction) {
93 FunctionTester T("(function(a) { return %_IsFunction(a); })", flags);
94
95 T.CheckFalse(T.NewObject("new Date()"));
96 T.CheckTrue(T.NewObject("(function() {})"));
97 T.CheckFalse(T.NewObject("([1])"));
98 T.CheckFalse(T.NewObject("({})"));
99 T.CheckFalse(T.NewObject("(/x/)"));
100 T.CheckFalse(T.undefined());
101 T.CheckFalse(T.null());
102 T.CheckFalse(T.Val("x"));
103 T.CheckFalse(T.Val(1));
104 }
105
106
TEST(IsMinusZero)107 TEST(IsMinusZero) {
108 FunctionTester T("(function(a) { return %_IsMinusZero(a); })", flags);
109
110 T.CheckFalse(T.Val(1));
111 T.CheckFalse(T.Val(1.1));
112 T.CheckTrue(T.Val(-0.0));
113 T.CheckFalse(T.Val(-2));
114 T.CheckFalse(T.Val(-2.3));
115 T.CheckFalse(T.undefined());
116 }
117
118
TEST(IsRegExp)119 TEST(IsRegExp) {
120 FunctionTester T("(function(a) { return %_IsRegExp(a); })", flags);
121
122 T.CheckFalse(T.NewObject("new Date()"));
123 T.CheckFalse(T.NewObject("(function() {})"));
124 T.CheckFalse(T.NewObject("([1])"));
125 T.CheckFalse(T.NewObject("({})"));
126 T.CheckTrue(T.NewObject("(/x/)"));
127 T.CheckFalse(T.undefined());
128 T.CheckFalse(T.null());
129 T.CheckFalse(T.Val("x"));
130 T.CheckFalse(T.Val(1));
131 }
132
133
TEST(IsSmi)134 TEST(IsSmi) {
135 FunctionTester T("(function(a) { return %_IsSmi(a); })", flags);
136
137 T.CheckFalse(T.NewObject("new Date()"));
138 T.CheckFalse(T.NewObject("(function() {})"));
139 T.CheckFalse(T.NewObject("([1])"));
140 T.CheckFalse(T.NewObject("({})"));
141 T.CheckFalse(T.NewObject("(/x/)"));
142 T.CheckFalse(T.undefined());
143 T.CheckTrue(T.Val(1));
144 T.CheckFalse(T.Val(1.1));
145 T.CheckFalse(T.Val(-0.0));
146 T.CheckTrue(T.Val(-2));
147 T.CheckFalse(T.Val(-2.3));
148 }
149
150
TEST(ObjectEquals)151 TEST(ObjectEquals) {
152 FunctionTester T("(function(a,b) { return %_ObjectEquals(a,b); })", flags);
153 CompileRun("var o = {}");
154
155 T.CheckTrue(T.NewObject("(o)"), T.NewObject("(o)"));
156 T.CheckTrue(T.Val("internal"), T.Val("internal"));
157 T.CheckTrue(T.true_value(), T.true_value());
158 T.CheckFalse(T.true_value(), T.false_value());
159 T.CheckFalse(T.NewObject("({})"), T.NewObject("({})"));
160 T.CheckFalse(T.Val("a"), T.Val("b"));
161 }
162
163
TEST(OneByteSeqStringGetChar)164 TEST(OneByteSeqStringGetChar) {
165 FunctionTester T("(function(a,b) { return %_OneByteSeqStringGetChar(a,b); })",
166 flags);
167
168 Handle<SeqOneByteString> string =
169 T.main_isolate()->factory()->NewRawOneByteString(3).ToHandleChecked();
170 string->SeqOneByteStringSet(0, 'b');
171 string->SeqOneByteStringSet(1, 'a');
172 string->SeqOneByteStringSet(2, 'r');
173 T.CheckCall(T.Val('b'), string, T.Val(0.0));
174 T.CheckCall(T.Val('a'), string, T.Val(1));
175 T.CheckCall(T.Val('r'), string, T.Val(2));
176 }
177
178
TEST(OneByteSeqStringSetChar)179 TEST(OneByteSeqStringSetChar) {
180 FunctionTester T("(function(a,b) { %_OneByteSeqStringSetChar(a,88,b); })",
181 flags);
182
183 Handle<SeqOneByteString> string =
184 T.main_isolate()->factory()->NewRawOneByteString(3).ToHandleChecked();
185 string->SeqOneByteStringSet(0, 'b');
186 string->SeqOneByteStringSet(1, 'a');
187 string->SeqOneByteStringSet(2, 'r');
188 T.Call(T.Val(1), string);
189 CHECK_EQ('b', string->SeqOneByteStringGet(0));
190 CHECK_EQ('X', string->SeqOneByteStringGet(1));
191 CHECK_EQ('r', string->SeqOneByteStringGet(2));
192 }
193
194
TEST(SetValueOf)195 TEST(SetValueOf) {
196 FunctionTester T("(function(a,b) { return %_SetValueOf(a,b); })", flags);
197
198 T.CheckCall(T.Val("a"), T.NewObject("(new String)"), T.Val("a"));
199 T.CheckCall(T.Val(123), T.NewObject("(new Number)"), T.Val(123));
200 T.CheckCall(T.Val("x"), T.undefined(), T.Val("x"));
201 }
202
203
TEST(StringAdd)204 TEST(StringAdd) {
205 FunctionTester T("(function(a,b) { return %_StringAdd(a,b); })", flags);
206
207 T.CheckCall(T.Val("aaabbb"), T.Val("aaa"), T.Val("bbb"));
208 T.CheckCall(T.Val("aaa"), T.Val("aaa"), T.Val(""));
209 T.CheckCall(T.Val("bbb"), T.Val(""), T.Val("bbb"));
210 }
211
212
TEST(StringCharAt)213 TEST(StringCharAt) {
214 FunctionTester T("(function(a,b) { return %_StringCharAt(a,b); })", flags);
215
216 T.CheckCall(T.Val("e"), T.Val("huge fan!"), T.Val(3));
217 T.CheckCall(T.Val("f"), T.Val("\xE2\x9D\x8A fan!"), T.Val(2));
218 T.CheckCall(T.Val(""), T.Val("not a fan!"), T.Val(23));
219 }
220
221
TEST(StringCharCodeAt)222 TEST(StringCharCodeAt) {
223 FunctionTester T("(function(a,b) { return %_StringCharCodeAt(a,b); })",
224 flags);
225
226 T.CheckCall(T.Val('e'), T.Val("huge fan!"), T.Val(3));
227 T.CheckCall(T.Val('f'), T.Val("\xE2\x9D\x8A fan!"), T.Val(2));
228 T.CheckCall(T.nan(), T.Val("not a fan!"), T.Val(23));
229 }
230
231
TEST(StringCharFromCode)232 TEST(StringCharFromCode) {
233 FunctionTester T("(function(a) { return %_StringCharFromCode(a); })", flags);
234
235 T.CheckCall(T.Val("a"), T.Val(97));
236 T.CheckCall(T.Val("\xE2\x9D\x8A"), T.Val(0x274A));
237 T.CheckCall(T.Val(""), T.undefined());
238 }
239
240
TEST(StringCompare)241 TEST(StringCompare) {
242 FunctionTester T("(function(a,b) { return %_StringCompare(a,b); })", flags);
243
244 T.CheckCall(T.Val(-1), T.Val("aaa"), T.Val("bbb"));
245 T.CheckCall(T.Val(0.0), T.Val("bbb"), T.Val("bbb"));
246 T.CheckCall(T.Val(+1), T.Val("ccc"), T.Val("bbb"));
247 }
248
249
TEST(SubString)250 TEST(SubString) {
251 FunctionTester T("(function(a,b) { return %_SubString(a,b,b+3); })", flags);
252
253 T.CheckCall(T.Val("aaa"), T.Val("aaabbb"), T.Val(0.0));
254 T.CheckCall(T.Val("abb"), T.Val("aaabbb"), T.Val(2));
255 T.CheckCall(T.Val("aaa"), T.Val("aaa"), T.Val(0.0));
256 }
257
258
TEST(TwoByteSeqStringGetChar)259 TEST(TwoByteSeqStringGetChar) {
260 FunctionTester T("(function(a,b) { return %_TwoByteSeqStringGetChar(a,b); })",
261 flags);
262
263 Handle<SeqTwoByteString> string =
264 T.main_isolate()->factory()->NewRawTwoByteString(3).ToHandleChecked();
265 string->SeqTwoByteStringSet(0, 'b');
266 string->SeqTwoByteStringSet(1, 'a');
267 string->SeqTwoByteStringSet(2, 'r');
268 T.CheckCall(T.Val('b'), string, T.Val(0.0));
269 T.CheckCall(T.Val('a'), string, T.Val(1));
270 T.CheckCall(T.Val('r'), string, T.Val(2));
271 }
272
273
TEST(TwoByteSeqStringSetChar)274 TEST(TwoByteSeqStringSetChar) {
275 FunctionTester T("(function(a,b) { %_TwoByteSeqStringSetChar(a,88,b); })",
276 flags);
277
278 Handle<SeqTwoByteString> string =
279 T.main_isolate()->factory()->NewRawTwoByteString(3).ToHandleChecked();
280 string->SeqTwoByteStringSet(0, 'b');
281 string->SeqTwoByteStringSet(1, 'a');
282 string->SeqTwoByteStringSet(2, 'r');
283 T.Call(T.Val(1), string);
284 CHECK_EQ('b', string->SeqTwoByteStringGet(0));
285 CHECK_EQ('X', string->SeqTwoByteStringGet(1));
286 CHECK_EQ('r', string->SeqTwoByteStringGet(2));
287 }
288
289
TEST(ValueOf)290 TEST(ValueOf) {
291 FunctionTester T("(function(a) { return %_ValueOf(a); })", flags);
292
293 T.CheckCall(T.Val("a"), T.Val("a"));
294 T.CheckCall(T.Val("b"), T.NewObject("(new String('b'))"));
295 T.CheckCall(T.Val(123), T.Val(123));
296 T.CheckCall(T.Val(456), T.NewObject("(new Number(456))"));
297 }
298
299 } // namespace compiler
300 } // namespace internal
301 } // namespace v8
302