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.
7 #include "test/cctest/compiler/function-tester.h"
9 using namespace v8::internal;
10 using namespace v8::internal::compiler;
11 uint32_t flags = CompilationInfo::kInliningEnabled;
15 FunctionTester T("(function(a,b) { return %_CallFunction(a, 1, 2, 3, b); })",
17 CompileRun("function f(a,b,c) { return a + b + c + this.d; }");
19 T.CheckCall(T.Val(129), T.NewObject("({d:123})"), T.NewObject("f"));
20 T.CheckCall(T.Val("6x"), T.NewObject("({d:'x'})"), T.NewObject("f"));
25 FunctionTester T("(function(a) { return %_ClassOf(a); })", flags);
27 T.CheckCall(T.Val("Function"), T.NewObject("(function() {})"));
28 T.CheckCall(T.Val("Array"), T.NewObject("([1])"));
29 T.CheckCall(T.Val("Object"), T.NewObject("({})"));
30 T.CheckCall(T.Val("RegExp"), T.NewObject("(/x/)"));
31 T.CheckCall(T.null(), T.undefined());
32 T.CheckCall(T.null(), T.null());
33 T.CheckCall(T.null(), T.Val("x"));
34 T.CheckCall(T.null(), T.Val(1));
38 TEST(HeapObjectGetMap) {
39 FunctionTester T("(function(a) { return %_HeapObjectGetMap(a); })", flags);
41 Factory* factory = T.main_isolate()->factory();
42 T.CheckCall(factory->null_map(), T.null());
43 T.CheckCall(factory->undefined_map(), T.undefined());
44 T.CheckCall(factory->heap_number_map(), T.Val(3.1415));
45 T.CheckCall(factory->symbol_map(), factory->NewSymbol());
49 #define COUNTER_NAME "hurz"
51 static int* LookupCounter(const char* name) {
52 static int counter = 1234;
53 return strcmp(name, COUNTER_NAME) == 0 ? &counter : nullptr;
57 TEST(IncrementStatsCounter) {
58 FLAG_native_code_counters = true;
59 reinterpret_cast<v8::Isolate*>(CcTest::InitIsolateOnce())
60 ->SetCounterFunction(LookupCounter);
62 "(function() { %_IncrementStatsCounter('" COUNTER_NAME "'); })", flags);
63 StatsCounter counter(T.main_isolate(), COUNTER_NAME);
64 if (!counter.Enabled()) return;
66 int old_value = *counter.GetInternalPointer();
67 T.CheckCall(T.undefined());
68 CHECK_EQ(old_value + 1, *counter.GetInternalPointer());
75 FunctionTester T("(function(a) { return %_IsArray(a); })", flags);
77 T.CheckFalse(T.NewObject("new Date()"));
78 T.CheckFalse(T.NewObject("(function() {})"));
79 T.CheckTrue(T.NewObject("([1])"));
80 T.CheckFalse(T.NewObject("({})"));
81 T.CheckFalse(T.NewObject("(/x/)"));
82 T.CheckFalse(T.undefined());
83 T.CheckFalse(T.null());
84 T.CheckFalse(T.Val("x"));
85 T.CheckFalse(T.Val(1));
90 FunctionTester T("(function(a) { return %_IsDate(a); })", flags);
92 T.CheckTrue(T.NewObject("new Date()"));
93 T.CheckFalse(T.NewObject("(function() {})"));
94 T.CheckFalse(T.NewObject("([1])"));
95 T.CheckFalse(T.NewObject("({})"));
96 T.CheckFalse(T.NewObject("(/x/)"));
97 T.CheckFalse(T.undefined());
98 T.CheckFalse(T.null());
99 T.CheckFalse(T.Val("x"));
100 T.CheckFalse(T.Val(1));
105 FunctionTester T("(function(a) { return %_IsFunction(a); })", flags);
107 T.CheckFalse(T.NewObject("new Date()"));
108 T.CheckTrue(T.NewObject("(function() {})"));
109 T.CheckFalse(T.NewObject("([1])"));
110 T.CheckFalse(T.NewObject("({})"));
111 T.CheckFalse(T.NewObject("(/x/)"));
112 T.CheckFalse(T.undefined());
113 T.CheckFalse(T.null());
114 T.CheckFalse(T.Val("x"));
115 T.CheckFalse(T.Val(1));
120 FunctionTester T("(function(a) { return %_IsMinusZero(a); })", flags);
122 T.CheckFalse(T.Val(1));
123 T.CheckFalse(T.Val(1.1));
124 T.CheckTrue(T.Val(-0.0));
125 T.CheckFalse(T.Val(-2));
126 T.CheckFalse(T.Val(-2.3));
127 T.CheckFalse(T.undefined());
131 TEST(IsNonNegativeSmi) {
132 FunctionTester T("(function(a) { return %_IsNonNegativeSmi(a); })", flags);
134 T.CheckTrue(T.Val(1));
135 T.CheckFalse(T.Val(1.1));
136 T.CheckFalse(T.Val(-0.0));
137 T.CheckFalse(T.Val(-2));
138 T.CheckFalse(T.Val(-2.3));
139 T.CheckFalse(T.undefined());
144 FunctionTester T("(function(a) { return %_IsRegExp(a); })", flags);
146 T.CheckFalse(T.NewObject("new Date()"));
147 T.CheckFalse(T.NewObject("(function() {})"));
148 T.CheckFalse(T.NewObject("([1])"));
149 T.CheckFalse(T.NewObject("({})"));
150 T.CheckTrue(T.NewObject("(/x/)"));
151 T.CheckFalse(T.undefined());
152 T.CheckFalse(T.null());
153 T.CheckFalse(T.Val("x"));
154 T.CheckFalse(T.Val(1));
159 FunctionTester T("(function(a) { return %_IsSmi(a); })", flags);
161 T.CheckFalse(T.NewObject("new Date()"));
162 T.CheckFalse(T.NewObject("(function() {})"));
163 T.CheckFalse(T.NewObject("([1])"));
164 T.CheckFalse(T.NewObject("({})"));
165 T.CheckFalse(T.NewObject("(/x/)"));
166 T.CheckFalse(T.undefined());
167 T.CheckTrue(T.Val(1));
168 T.CheckFalse(T.Val(1.1));
169 T.CheckFalse(T.Val(-0.0));
170 T.CheckTrue(T.Val(-2));
171 T.CheckFalse(T.Val(-2.3));
175 TEST(MapGetInstanceType) {
177 "(function(a) { return %_MapGetInstanceType(%_HeapObjectGetMap(a)); })",
180 Factory* factory = T.main_isolate()->factory();
181 T.CheckCall(T.Val(ODDBALL_TYPE), T.null());
182 T.CheckCall(T.Val(ODDBALL_TYPE), T.undefined());
183 T.CheckCall(T.Val(HEAP_NUMBER_TYPE), T.Val(3.1415));
184 T.CheckCall(T.Val(SYMBOL_TYPE), factory->NewSymbol());
189 FunctionTester T("(function(a,b) { return %_ObjectEquals(a,b); })", flags);
190 CompileRun("var o = {}");
192 T.CheckTrue(T.NewObject("(o)"), T.NewObject("(o)"));
193 T.CheckTrue(T.Val("internal"), T.Val("internal"));
194 T.CheckTrue(T.true_value(), T.true_value());
195 T.CheckFalse(T.true_value(), T.false_value());
196 T.CheckFalse(T.NewObject("({})"), T.NewObject("({})"));
197 T.CheckFalse(T.Val("a"), T.Val("b"));
201 TEST(OneByteSeqStringGetChar) {
202 FunctionTester T("(function(a,b) { return %_OneByteSeqStringGetChar(a,b); })",
205 Handle<SeqOneByteString> string =
206 T.main_isolate()->factory()->NewRawOneByteString(3).ToHandleChecked();
207 string->SeqOneByteStringSet(0, 'b');
208 string->SeqOneByteStringSet(1, 'a');
209 string->SeqOneByteStringSet(2, 'r');
210 T.CheckCall(T.Val('b'), string, T.Val(0.0));
211 T.CheckCall(T.Val('a'), string, T.Val(1));
212 T.CheckCall(T.Val('r'), string, T.Val(2));
216 TEST(OneByteSeqStringSetChar) {
217 FunctionTester T("(function(a,b) { %_OneByteSeqStringSetChar(a,88,b); })",
220 Handle<SeqOneByteString> string =
221 T.main_isolate()->factory()->NewRawOneByteString(3).ToHandleChecked();
222 string->SeqOneByteStringSet(0, 'b');
223 string->SeqOneByteStringSet(1, 'a');
224 string->SeqOneByteStringSet(2, 'r');
225 T.Call(T.Val(1), string);
226 CHECK_EQ('b', string->SeqOneByteStringGet(0));
227 CHECK_EQ('X', string->SeqOneByteStringGet(1));
228 CHECK_EQ('r', string->SeqOneByteStringGet(2));
232 TEST(NewConsString) {
235 " return %_NewConsString(14, true, 'abcdefghi', 'jklmn');"
239 T.CheckCall(T.Val("abcdefghijklmn"));
244 FunctionTester T("(function(a,b) { return %_SetValueOf(a,b); })", flags);
246 T.CheckCall(T.Val("a"), T.NewObject("(new String)"), T.Val("a"));
247 T.CheckCall(T.Val(123), T.NewObject("(new Number)"), T.Val(123));
248 T.CheckCall(T.Val("x"), T.undefined(), T.Val("x"));
253 FunctionTester T("(function(a,b) { return %_StringAdd(a,b); })", flags);
255 T.CheckCall(T.Val("aaabbb"), T.Val("aaa"), T.Val("bbb"));
256 T.CheckCall(T.Val("aaa"), T.Val("aaa"), T.Val(""));
257 T.CheckCall(T.Val("bbb"), T.Val(""), T.Val("bbb"));
262 FunctionTester T("(function(a,b) { return %_StringCharAt(a,b); })", flags);
264 T.CheckCall(T.Val("e"), T.Val("huge fan!"), T.Val(3));
265 T.CheckCall(T.Val("f"), T.Val("\xE2\x9D\x8A fan!"), T.Val(2));
266 T.CheckCall(T.Val(""), T.Val("not a fan!"), T.Val(23));
270 TEST(StringCharCodeAt) {
271 FunctionTester T("(function(a,b) { return %_StringCharCodeAt(a,b); })",
274 T.CheckCall(T.Val('e'), T.Val("huge fan!"), T.Val(3));
275 T.CheckCall(T.Val('f'), T.Val("\xE2\x9D\x8A fan!"), T.Val(2));
276 T.CheckCall(T.nan(), T.Val("not a fan!"), T.Val(23));
280 TEST(StringCharFromCode) {
281 FunctionTester T("(function(a) { return %_StringCharFromCode(a); })", flags);
283 T.CheckCall(T.Val("a"), T.Val(97));
284 T.CheckCall(T.Val("\xE2\x9D\x8A"), T.Val(0x274A));
285 T.CheckCall(T.Val(""), T.undefined());
289 TEST(StringCompare) {
290 FunctionTester T("(function(a,b) { return %_StringCompare(a,b); })", flags);
292 T.CheckCall(T.Val(-1), T.Val("aaa"), T.Val("bbb"));
293 T.CheckCall(T.Val(0.0), T.Val("bbb"), T.Val("bbb"));
294 T.CheckCall(T.Val(+1), T.Val("ccc"), T.Val("bbb"));
299 FunctionTester T("(function(a,b) { return %_SubString(a,b,b+3); })", flags);
301 T.CheckCall(T.Val("aaa"), T.Val("aaabbb"), T.Val(0.0));
302 T.CheckCall(T.Val("abb"), T.Val("aaabbb"), T.Val(2));
303 T.CheckCall(T.Val("aaa"), T.Val("aaa"), T.Val(0.0));
307 TEST(TwoByteSeqStringGetChar) {
308 FunctionTester T("(function(a,b) { return %_TwoByteSeqStringGetChar(a,b); })",
311 Handle<SeqTwoByteString> string =
312 T.main_isolate()->factory()->NewRawTwoByteString(3).ToHandleChecked();
313 string->SeqTwoByteStringSet(0, 'b');
314 string->SeqTwoByteStringSet(1, 'a');
315 string->SeqTwoByteStringSet(2, 'r');
316 T.CheckCall(T.Val('b'), string, T.Val(0.0));
317 T.CheckCall(T.Val('a'), string, T.Val(1));
318 T.CheckCall(T.Val('r'), string, T.Val(2));
322 TEST(TwoByteSeqStringSetChar) {
323 FunctionTester T("(function(a,b) { %_TwoByteSeqStringSetChar(a,88,b); })",
326 Handle<SeqTwoByteString> string =
327 T.main_isolate()->factory()->NewRawTwoByteString(3).ToHandleChecked();
328 string->SeqTwoByteStringSet(0, 'b');
329 string->SeqTwoByteStringSet(1, 'a');
330 string->SeqTwoByteStringSet(2, 'r');
331 T.Call(T.Val(1), string);
332 CHECK_EQ('b', string->SeqTwoByteStringGet(0));
333 CHECK_EQ('X', string->SeqTwoByteStringGet(1));
334 CHECK_EQ('r', string->SeqTwoByteStringGet(2));
339 FunctionTester T("(function(a) { return %_ValueOf(a); })", flags);
341 T.CheckCall(T.Val("a"), T.Val("a"));
342 T.CheckCall(T.Val("b"), T.NewObject("(new String('b'))"));
343 T.CheckCall(T.Val(123), T.Val(123));
344 T.CheckCall(T.Val(456), T.NewObject("(new Number(456))"));