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;
13 FunctionTester T("(function(foo,a) { return foo(a); })");
14 Handle<JSFunction> foo = T.NewFunction("(function(a) { return a; })");
16 T.CheckCall(T.Val(3), foo, T.Val(3));
17 T.CheckCall(T.Val(3.1), foo, T.Val(3.1));
18 T.CheckCall(foo, foo, foo);
19 T.CheckCall(T.Val("Abba"), foo, T.Val("Abba"));
24 FunctionTester T("(function(foo,a) { return foo(a); })");
25 Handle<JSFunction> foo = T.NewFunction("(function(a) { return a; })");
28 T.CheckCall(T.Val(3), foo, T.Val(3));
29 T.CheckCall(T.Val(3.1), foo, T.Val(3.1));
30 T.CheckCall(foo, foo, foo);
31 T.CheckCall(T.Val("Abba"), foo, T.Val("Abba"));
36 FunctionTester T("(function(foo,a) { return foo(a,3); })");
37 Handle<JSFunction> foo = T.NewFunction("(function(a,b) { return a + b; })");
40 T.CheckCall(T.Val(6), foo, T.Val(3));
41 T.CheckCall(T.Val(6.1), foo, T.Val(3.1));
42 T.CheckCall(T.Val("function (a,b) { return a + b; }3"), foo, foo);
43 T.CheckCall(T.Val("Abba3"), foo, T.Val("Abba"));
48 FunctionTester T("(function(foo,a) { return foo(a,\"3\"); })");
49 Handle<JSFunction> foo = T.NewFunction("(function(a,b) { return a + b; })");
52 T.CheckCall(T.Val("33"), foo, T.Val(3));
53 T.CheckCall(T.Val("3.13"), foo, T.Val(3.1));
54 T.CheckCall(T.Val("function (a,b) { return a + b; }3"), foo, foo);
55 T.CheckCall(T.Val("Abba3"), foo, T.Val("Abba"));
59 TEST(PropertyNamedCall) {
60 FunctionTester T("(function(a,b) { return a.foo(b,23); })");
61 CompileRun("function foo(y,z) { return this.x + y + z; }");
63 T.CheckCall(T.Val(32), T.NewObject("({ foo:foo, x:4 })"), T.Val(5));
64 T.CheckCall(T.Val("xy23"), T.NewObject("({ foo:foo, x:'x' })"), T.Val("y"));
65 T.CheckCall(T.nan(), T.NewObject("({ foo:foo, y:0 })"), T.Val(3));
69 TEST(PropertyKeyedCall) {
70 FunctionTester T("(function(a,b) { var f = 'foo'; return a[f](b,23); })");
71 CompileRun("function foo(y,z) { return this.x + y + z; }");
73 T.CheckCall(T.Val(32), T.NewObject("({ foo:foo, x:4 })"), T.Val(5));
74 T.CheckCall(T.Val("xy23"), T.NewObject("({ foo:foo, x:'x' })"), T.Val("y"));
75 T.CheckCall(T.nan(), T.NewObject("({ foo:foo, y:0 })"), T.Val(3));
80 FunctionTester T("(function(a,b) { return foo(a,b); })");
81 CompileRun("function foo(a,b) { return a + b + this.c; }");
82 CompileRun("var c = 23;");
84 T.CheckCall(T.Val(32), T.Val(4), T.Val(5));
85 T.CheckCall(T.Val("xy23"), T.Val("x"), T.Val("y"));
86 T.CheckCall(T.nan(), T.undefined(), T.Val(3));
91 FunctionTester T("(function(a,b) { with (a) { return foo(a,b); } })");
93 CompileRun("function f1(a,b) { return a.val + b; }");
94 T.CheckCall(T.Val(5), T.NewObject("({ foo:f1, val:2 })"), T.Val(3));
95 T.CheckCall(T.Val("xy"), T.NewObject("({ foo:f1, val:'x' })"), T.Val("y"));
97 CompileRun("function f2(a,b) { return this.val + b; }");
98 T.CheckCall(T.Val(9), T.NewObject("({ foo:f2, val:4 })"), T.Val(5));
99 T.CheckCall(T.Val("xy"), T.NewObject("({ foo:f2, val:'x' })"), T.Val("y"));
103 TEST(MismatchCallTooFew) {
104 FunctionTester T("(function(a,b) { return foo(a,b); })");
105 CompileRun("function foo(a,b,c) { return a + b + c; }");
107 T.CheckCall(T.nan(), T.Val(23), T.Val(42));
108 T.CheckCall(T.nan(), T.Val(4.2), T.Val(2.3));
109 T.CheckCall(T.Val("abundefined"), T.Val("a"), T.Val("b"));
113 TEST(MismatchCallTooMany) {
114 FunctionTester T("(function(a,b) { return foo(a,b); })");
115 CompileRun("function foo(a) { return a; }");
117 T.CheckCall(T.Val(23), T.Val(23), T.Val(42));
118 T.CheckCall(T.Val(4.2), T.Val(4.2), T.Val(2.3));
119 T.CheckCall(T.Val("a"), T.Val("a"), T.Val("b"));
123 TEST(ConstructorCall) {
124 FunctionTester T("(function(a,b) { return new foo(a,b).value; })");
125 CompileRun("function foo(a,b) { return { value: a + b + this.c }; }");
126 CompileRun("foo.prototype.c = 23;");
128 T.CheckCall(T.Val(32), T.Val(4), T.Val(5));
129 T.CheckCall(T.Val("xy23"), T.Val("x"), T.Val("y"));
130 T.CheckCall(T.nan(), T.undefined(), T.Val(3));
134 // TODO(titzer): factor these out into test-runtime-calls.cc
135 TEST(RuntimeCallCPP2) {
136 FLAG_allow_natives_syntax = true;
137 FunctionTester T("(function(a,b) { return %NumberImul(a, b); })");
139 T.CheckCall(T.Val(2730), T.Val(42), T.Val(65));
140 T.CheckCall(T.Val(798), T.Val(42), T.Val(19));
144 TEST(RuntimeCallJS) {
145 FLAG_allow_natives_syntax = true;
146 FunctionTester T("(function(a) { return %to_string_fun(a); })");
148 T.CheckCall(T.Val("23"), T.Val(23), T.undefined());
149 T.CheckCall(T.Val("4.2"), T.Val(4.2), T.undefined());
150 T.CheckCall(T.Val("str"), T.Val("str"), T.undefined());
151 T.CheckCall(T.Val("true"), T.true_value(), T.undefined());
152 T.CheckCall(T.Val("false"), T.false_value(), T.undefined());
153 T.CheckCall(T.Val("undefined"), T.undefined(), T.undefined());
157 TEST(RuntimeCallInline) {
158 FLAG_allow_natives_syntax = true;
159 FunctionTester T("(function(a) { return %_IsSpecObject(a); })");
161 T.CheckCall(T.false_value(), T.Val(23), T.undefined());
162 T.CheckCall(T.false_value(), T.Val(4.2), T.undefined());
163 T.CheckCall(T.false_value(), T.Val("str"), T.undefined());
164 T.CheckCall(T.false_value(), T.true_value(), T.undefined());
165 T.CheckCall(T.false_value(), T.false_value(), T.undefined());
166 T.CheckCall(T.false_value(), T.undefined(), T.undefined());
167 T.CheckCall(T.true_value(), T.NewObject("({})"), T.undefined());
168 T.CheckCall(T.true_value(), T.NewObject("([])"), T.undefined());
173 FunctionTester T("(function(a,b) { return eval(a); })");
174 Handle<JSObject> g(T.function->context()->global_object()->global_proxy());
176 T.CheckCall(T.Val(23), T.Val("17 + 6"), T.undefined());
177 T.CheckCall(T.Val("'Y'; a"), T.Val("'Y'; a"), T.Val("b-val"));
178 T.CheckCall(T.Val("b-val"), T.Val("'Y'; b"), T.Val("b-val"));
179 T.CheckCall(g, T.Val("this"), T.undefined());
180 T.CheckCall(g, T.Val("'use strict'; this"), T.undefined());
182 CompileRun("eval = function(x) { return x; }");
183 T.CheckCall(T.Val("17 + 6"), T.Val("17 + 6"), T.undefined());
185 CompileRun("eval = function(x) { return this; }");
186 T.CheckCall(g, T.Val("17 + 6"), T.undefined());
188 CompileRun("eval = function(x) { 'use strict'; return this; }");
189 T.CheckCall(T.undefined(), T.Val("17 + 6"), T.undefined());
193 TEST(ReceiverPatching) {
194 // TODO(turbofan): Note that this test only checks that the function prologue
195 // patches an undefined receiver to the global receiver. If this starts to
196 // fail once we fix the calling protocol, just remove this test.
197 FunctionTester T("(function(a) { return this; })");
198 Handle<JSObject> g(T.function->context()->global_object()->global_proxy());
199 T.CheckCall(g, T.undefined());
207 "function bar() { return eval('x') };"
211 T.CheckCall(T.Val(42), T.Val("x"), T.undefined());
215 TEST(ContextLoadedFromActivation) {
219 " return function () { return x };"
222 // Disable context specialization.
223 FunctionTester T(script);
224 v8::Local<v8::Context> context = v8::Context::New(CcTest::isolate());
225 v8::Context::Scope scope(context);
226 v8::Local<v8::Value> value = CompileRun(script);
227 i::Handle<i::Object> ofun = v8::Utils::OpenHandle(*value);
228 i::Handle<i::JSFunction> jsfun = Handle<JSFunction>::cast(ofun);
229 jsfun->set_code(T.function->code());
230 jsfun->set_shared(T.function->shared());
231 context->Global()->Set(v8_str("foo"), v8::Utils::ToLocal(jsfun));
232 CompileRun("var x = 24;");
233 ExpectInt32("foo();", 24);
237 TEST(BuiltinLoadedFromActivation) {
241 " return function () { return this; };"
244 // Disable context specialization.
245 FunctionTester T(script);
246 v8::Local<v8::Context> context = v8::Context::New(CcTest::isolate());
247 v8::Context::Scope scope(context);
248 v8::Local<v8::Value> value = CompileRun(script);
249 i::Handle<i::Object> ofun = v8::Utils::OpenHandle(*value);
250 i::Handle<i::JSFunction> jsfun = Handle<JSFunction>::cast(ofun);
251 jsfun->set_code(T.function->code());
252 jsfun->set_shared(T.function->shared());
253 context->Global()->Set(v8_str("foo"), v8::Utils::ToLocal(jsfun));
254 CompileRun("var x = 24;");
255 ExpectObject("foo()", context->Global());