blob: 7f3dafc0637b4c220422251649e27738900eee4e [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Steve Blocka7e24c12009-10-30 11:49:00 +000028
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029#include "src/v8.h"
30
31#include "src/api.h"
32#include "src/debug.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040033#include "src/string-search.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034#include "test/cctest/cctest.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035
36
37using ::v8::internal::CStrVector;
38using ::v8::internal::Factory;
39using ::v8::internal::Handle;
40using ::v8::internal::Heap;
Steve Block44f0eee2011-05-26 01:26:41 +010041using ::v8::internal::Isolate;
Steve Blocka7e24c12009-10-30 11:49:00 +000042using ::v8::internal::JSFunction;
43using ::v8::internal::Object;
44using ::v8::internal::Runtime;
45using ::v8::internal::Script;
Ben Murdoch589d6972011-11-30 16:04:58 +000046using ::v8::internal::SmartArrayPointer;
Steve Blocka7e24c12009-10-30 11:49:00 +000047using ::v8::internal::SharedFunctionInfo;
48using ::v8::internal::String;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049using ::v8::internal::Vector;
Steve Blocka7e24c12009-10-30 11:49:00 +000050
51
Steve Blocka7e24c12009-10-30 11:49:00 +000052static void CheckFunctionName(v8::Handle<v8::Script> script,
53 const char* func_pos_src,
54 const char* ref_inferred_name) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 Isolate* isolate = CcTest::i_isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056
Steve Blocka7e24c12009-10-30 11:49:00 +000057 // Get script source.
Steve Block6ded16b2010-05-10 14:33:55 +010058 Handle<Object> obj = v8::Utils::OpenHandle(*script);
59 Handle<SharedFunctionInfo> shared_function;
60 if (obj->IsSharedFunctionInfo()) {
61 shared_function =
62 Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(*obj));
63 } else {
64 shared_function =
65 Handle<SharedFunctionInfo>(JSFunction::cast(*obj)->shared());
66 }
67 Handle<Script> i_script(Script::cast(shared_function->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +000068 CHECK(i_script->source()->IsString());
69 Handle<String> script_src(String::cast(i_script->source()));
70
71 // Find the position of a given func source substring in the source.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040072 int func_pos;
73 {
74 i::DisallowHeapAllocation no_gc;
75 Vector<const uint8_t> func_pos_str = i::OneByteVector(func_pos_src);
76 String::FlatContent script_content = script_src->GetFlatContent();
77 func_pos = SearchString(isolate, script_content.ToOneByteVector(),
78 func_pos_str, 0);
79 }
Steve Blocka7e24c12009-10-30 11:49:00 +000080 CHECK_NE(0, func_pos);
81
82 // Obtain SharedFunctionInfo for the function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083 isolate->debug()->PrepareForBreakPoints();
Steve Blocka7e24c12009-10-30 11:49:00 +000084 Object* shared_func_info_ptr =
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 isolate->debug()->FindSharedFunctionInfoInScript(i_script, func_pos);
86 CHECK(shared_func_info_ptr != CcTest::heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +000087 Handle<SharedFunctionInfo> shared_func_info(
88 SharedFunctionInfo::cast(shared_func_info_ptr));
89
90 // Verify inferred function name.
Ben Murdoch589d6972011-11-30 16:04:58 +000091 SmartArrayPointer<char> inferred_name =
Steve Blocka7e24c12009-10-30 11:49:00 +000092 shared_func_info->inferred_name()->ToCString();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093 CHECK_EQ(ref_inferred_name, inferred_name.get());
Steve Blocka7e24c12009-10-30 11:49:00 +000094}
95
96
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097static v8::Handle<v8::Script> Compile(v8::Isolate* isolate, const char* src) {
98 return v8::Script::Compile(v8::String::NewFromUtf8(isolate, src));
Steve Blocka7e24c12009-10-30 11:49:00 +000099}
100
101
102TEST(GlobalProperty) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 CcTest::InitializeVM();
104 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000105
106 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000108 "fun1 = function() { return 1; }\n"
109 "fun2 = function() { return 2; }\n");
110 CheckFunctionName(script, "return 1", "fun1");
111 CheckFunctionName(script, "return 2", "fun2");
112}
113
114
115TEST(GlobalVar) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116 CcTest::InitializeVM();
117 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000118
119 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 "var fun1 = function() { return 1; }\n"
122 "var fun2 = function() { return 2; }\n");
123 CheckFunctionName(script, "return 1", "fun1");
124 CheckFunctionName(script, "return 2", "fun2");
125}
126
127
128TEST(LocalVar) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 CcTest::InitializeVM();
130 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000131
132 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000134 "function outer() {\n"
135 " var fun1 = function() { return 1; }\n"
136 " var fun2 = function() { return 2; }\n"
137 "}");
138 CheckFunctionName(script, "return 1", "fun1");
139 CheckFunctionName(script, "return 2", "fun2");
140}
141
142
143TEST(InConstructor) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144 CcTest::InitializeVM();
145 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000146
147 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 "function MyClass() {\n"
150 " this.method1 = function() { return 1; }\n"
151 " this.method2 = function() { return 2; }\n"
152 "}");
153 CheckFunctionName(script, "return 1", "MyClass.method1");
154 CheckFunctionName(script, "return 2", "MyClass.method2");
155}
156
157
158TEST(Factory) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 CcTest::InitializeVM();
160 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000161
162 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000164 "function createMyObj() {\n"
165 " var obj = {};\n"
166 " obj.method1 = function() { return 1; }\n"
167 " obj.method2 = function() { return 2; }\n"
168 " return obj;\n"
169 "}");
170 CheckFunctionName(script, "return 1", "obj.method1");
171 CheckFunctionName(script, "return 2", "obj.method2");
172}
173
174
175TEST(Static) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 CcTest::InitializeVM();
177 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000178
179 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000181 "function MyClass() {}\n"
182 "MyClass.static1 = function() { return 1; }\n"
183 "MyClass.static2 = function() { return 2; }\n"
184 "MyClass.MyInnerClass = {}\n"
185 "MyClass.MyInnerClass.static3 = function() { return 3; }\n"
186 "MyClass.MyInnerClass.static4 = function() { return 4; }");
187 CheckFunctionName(script, "return 1", "MyClass.static1");
188 CheckFunctionName(script, "return 2", "MyClass.static2");
189 CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.static3");
190 CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.static4");
191}
192
193
194TEST(Prototype) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000195 CcTest::InitializeVM();
196 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000197
198 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000200 "function MyClass() {}\n"
201 "MyClass.prototype.method1 = function() { return 1; }\n"
202 "MyClass.prototype.method2 = function() { return 2; }\n"
203 "MyClass.MyInnerClass = function() {}\n"
204 "MyClass.MyInnerClass.prototype.method3 = function() { return 3; }\n"
205 "MyClass.MyInnerClass.prototype.method4 = function() { return 4; }");
206 CheckFunctionName(script, "return 1", "MyClass.method1");
207 CheckFunctionName(script, "return 2", "MyClass.method2");
208 CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.method3");
209 CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.method4");
210}
211
212
213TEST(ObjectLiteral) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 CcTest::InitializeVM();
215 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000216
217 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 "function MyClass() {}\n"
220 "MyClass.prototype = {\n"
221 " method1: function() { return 1; },\n"
222 " method2: function() { return 2; } }");
223 CheckFunctionName(script, "return 1", "MyClass.method1");
224 CheckFunctionName(script, "return 2", "MyClass.method2");
225}
226
227
228TEST(AsParameter) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000229 CcTest::InitializeVM();
230 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000231
232 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 "function f1(a) { return a(); }\n"
235 "function f2(a, b) { return a() + b(); }\n"
236 "var result1 = f1(function() { return 1; })\n"
237 "var result2 = f2(function() { return 2; }, function() { return 3; })");
238 // Can't infer names here.
239 CheckFunctionName(script, "return 1", "");
240 CheckFunctionName(script, "return 2", "");
241 CheckFunctionName(script, "return 3", "");
242}
243
244
245TEST(MultipleFuncsConditional) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000246 CcTest::InitializeVM();
247 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000248
249 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000251 "fun1 = 0 ?\n"
252 " function() { return 1; } :\n"
253 " function() { return 2; }");
254 CheckFunctionName(script, "return 1", "fun1");
255 CheckFunctionName(script, "return 2", "fun1");
256}
257
258
259TEST(MultipleFuncsInLiteral) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 CcTest::InitializeVM();
261 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000262
263 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 "function MyClass() {}\n"
266 "MyClass.prototype = {\n"
267 " method1: 0 ? function() { return 1; } :\n"
268 " function() { return 2; } }");
269 CheckFunctionName(script, "return 1", "MyClass.method1");
270 CheckFunctionName(script, "return 2", "MyClass.method1");
271}
272
273
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000274TEST(AnonymousInAnonymousClosure1) {
275 CcTest::InitializeVM();
276 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000277
278 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279 CcTest::isolate(),
280 "(function() {\n"
281 " (function() {\n"
282 " var a = 1;\n"
283 " return;\n"
284 " })();\n"
285 " var b = function() {\n"
286 " var c = 1;\n"
287 " return;\n"
288 " };\n"
289 "})();");
290 CheckFunctionName(script, "return", "");
291}
292
293
294TEST(AnonymousInAnonymousClosure2) {
295 CcTest::InitializeVM();
296 v8::HandleScope scope(CcTest::isolate());
297
298 v8::Handle<v8::Script> script = Compile(
299 CcTest::isolate(),
300 "(function() {\n"
301 " (function() {\n"
302 " var a = 1;\n"
303 " return;\n"
304 " })();\n"
305 " var c = 1;\n"
306 "})();");
307 CheckFunctionName(script, "return", "");
308}
309
310
311TEST(NamedInAnonymousClosure) {
312 CcTest::InitializeVM();
313 v8::HandleScope scope(CcTest::isolate());
314
315 v8::Handle<v8::Script> script = Compile(
316 CcTest::isolate(),
317 "var foo = function() {\n"
318 " (function named() {\n"
319 " var a = 1;\n"
320 " })();\n"
321 " var c = 1;\n"
322 " return;\n"
323 "};");
324 CheckFunctionName(script, "return", "foo");
325}
326
327
328// See http://code.google.com/p/v8/issues/detail?id=380
329TEST(Issue380) {
330 CcTest::InitializeVM();
331 v8::HandleScope scope(CcTest::isolate());
332
333 v8::Handle<v8::Script> script = Compile(
334 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000335 "function a() {\n"
336 "var result = function(p,a,c,k,e,d)"
337 "{return p}(\"if blah blah\",62,1976,\'a|b\'.split(\'|\'),0,{})\n"
338 "}");
339 CheckFunctionName(script, "return p", "");
340}
Ben Murdoch257744e2011-11-30 15:57:28 +0000341
342
343TEST(MultipleAssignments) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000344 CcTest::InitializeVM();
345 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +0000346
347 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348 CcTest::isolate(),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000349 "var fun1 = fun2 = function () { return 1; }\n"
350 "var bar1 = bar2 = bar3 = function () { return 2; }\n"
351 "foo1 = foo2 = function () { return 3; }\n"
352 "baz1 = baz2 = baz3 = function () { return 4; }");
Ben Murdoch257744e2011-11-30 15:57:28 +0000353 CheckFunctionName(script, "return 1", "fun2");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000354 CheckFunctionName(script, "return 2", "bar3");
355 CheckFunctionName(script, "return 3", "foo2");
356 CheckFunctionName(script, "return 4", "baz3");
Ben Murdoch257744e2011-11-30 15:57:28 +0000357}
358
359
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000360TEST(AsConstructorParameter) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000361 CcTest::InitializeVM();
362 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +0000363
364 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000365 CcTest::isolate(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000366 "function Foo() {}\n"
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000367 "var foo = new Foo(function() { return 1; })\n"
368 "var bar = new Foo(function() { return 2; }, function() { return 3; })");
Ben Murdoch257744e2011-11-30 15:57:28 +0000369 CheckFunctionName(script, "return 1", "");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000370 CheckFunctionName(script, "return 2", "");
371 CheckFunctionName(script, "return 3", "");
Ben Murdoch257744e2011-11-30 15:57:28 +0000372}
373
374
375TEST(FactoryHashmap) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376 CcTest::InitializeVM();
377 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +0000378
379 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 CcTest::isolate(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000381 "function createMyObj() {\n"
382 " var obj = {};\n"
383 " obj[\"method1\"] = function() { return 1; }\n"
384 " obj[\"method2\"] = function() { return 2; }\n"
385 " return obj;\n"
386 "}");
387 CheckFunctionName(script, "return 1", "obj.method1");
388 CheckFunctionName(script, "return 2", "obj.method2");
389}
390
391
392TEST(FactoryHashmapVariable) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000393 CcTest::InitializeVM();
394 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +0000395
396 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000397 CcTest::isolate(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000398 "function createMyObj() {\n"
399 " var obj = {};\n"
400 " var methodName = \"method1\";\n"
401 " obj[methodName] = function() { return 1; }\n"
402 " methodName = \"method2\";\n"
403 " obj[methodName] = function() { return 2; }\n"
404 " return obj;\n"
405 "}");
406 // Can't infer function names statically.
407 CheckFunctionName(script, "return 1", "obj.(anonymous function)");
408 CheckFunctionName(script, "return 2", "obj.(anonymous function)");
409}
410
411
412TEST(FactoryHashmapConditional) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000413 CcTest::InitializeVM();
414 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +0000415
416 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000417 CcTest::isolate(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000418 "function createMyObj() {\n"
419 " var obj = {};\n"
420 " obj[0 ? \"method1\" : \"method2\"] = function() { return 1; }\n"
421 " return obj;\n"
422 "}");
423 // Can't infer the function name statically.
424 CheckFunctionName(script, "return 1", "obj.(anonymous function)");
425}
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000426
427
428TEST(GlobalAssignmentAndCall) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000429 CcTest::InitializeVM();
430 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000431
432 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000433 CcTest::isolate(),
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000434 "var Foo = function() {\n"
435 " return 1;\n"
436 "}();\n"
437 "var Baz = Bar = function() {\n"
438 " return 2;\n"
439 "}");
440 // The inferred name is empty, because this is an assignment of a result.
441 CheckFunctionName(script, "return 1", "");
442 // See MultipleAssignments test.
443 CheckFunctionName(script, "return 2", "Bar");
444}
445
446
447TEST(AssignmentAndCall) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000448 CcTest::InitializeVM();
449 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000450
451 v8::Handle<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 CcTest::isolate(),
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000453 "(function Enclosing() {\n"
454 " var Foo;\n"
455 " Foo = function() {\n"
456 " return 1;\n"
457 " }();\n"
458 " var Baz = Bar = function() {\n"
459 " return 2;\n"
460 " }\n"
461 "})();");
462 // The inferred name is empty, because this is an assignment of a result.
463 CheckFunctionName(script, "return 1", "");
464 // See MultipleAssignments test.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000465 // TODO(2276): Lazy compiling the enclosing outer closure would yield
466 // in "Enclosing.Bar" being the inferred name here.
467 CheckFunctionName(script, "return 2", "Bar");
468}
469
470
471TEST(MethodAssignmentInAnonymousFunctionCall) {
472 CcTest::InitializeVM();
473 v8::HandleScope scope(CcTest::isolate());
474
475 v8::Handle<v8::Script> script = Compile(
476 CcTest::isolate(),
477 "(function () {\n"
478 " var EventSource = function () { };\n"
479 " EventSource.prototype.addListener = function () {\n"
480 " return 2012;\n"
481 " };\n"
482 " this.PublicEventSource = EventSource;\n"
483 "})();");
484 CheckFunctionName(script, "return 2012", "EventSource.addListener");
485}
486
487
488TEST(ReturnAnonymousFunction) {
489 CcTest::InitializeVM();
490 v8::HandleScope scope(CcTest::isolate());
491
492 v8::Handle<v8::Script> script = Compile(
493 CcTest::isolate(),
494 "(function() {\n"
495 " function wrapCode() {\n"
496 " return function () {\n"
497 " return 2012;\n"
498 " };\n"
499 " };\n"
500 " var foo = 10;\n"
501 " function f() {\n"
502 " return wrapCode();\n"
503 " }\n"
504 " this.ref = f;\n"
505 "})()");
506 script->Run();
507 CheckFunctionName(script, "return 2012", "");
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000508}