blob: 77ba2f2243f0039b32e2da9330d5dbe8324a32ed [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"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032#include "src/debug/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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037using ::v8::base::SmartArrayPointer;
Steve Blocka7e24c12009-10-30 11:49:00 +000038using ::v8::internal::CStrVector;
39using ::v8::internal::Factory;
40using ::v8::internal::Handle;
41using ::v8::internal::Heap;
Steve Block44f0eee2011-05-26 01:26:41 +010042using ::v8::internal::Isolate;
Steve Blocka7e24c12009-10-30 11:49:00 +000043using ::v8::internal::JSFunction;
44using ::v8::internal::Object;
45using ::v8::internal::Runtime;
46using ::v8::internal::Script;
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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052static void CheckFunctionName(v8::Local<v8::Script> script,
Steve Blocka7e24c12009-10-30 11:49:00 +000053 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 Murdoch4a90d5f2016-03-22 12:00:34 +000083 Handle<SharedFunctionInfo> shared_func_info =
84 Handle<SharedFunctionInfo>::cast(
85 isolate->debug()->FindSharedFunctionInfoInScript(i_script, func_pos));
Steve Blocka7e24c12009-10-30 11:49:00 +000086
87 // Verify inferred function name.
Ben Murdoch589d6972011-11-30 16:04:58 +000088 SmartArrayPointer<char> inferred_name =
Steve Blocka7e24c12009-10-30 11:49:00 +000089 shared_func_info->inferred_name()->ToCString();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090 i::PrintF("expected: %s, found: %s\n", ref_inferred_name,
91 inferred_name.get());
92 CHECK_EQ(0, strcmp(ref_inferred_name, inferred_name.get()));
Steve Blocka7e24c12009-10-30 11:49:00 +000093}
94
95
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096static v8::Local<v8::Script> Compile(v8::Isolate* isolate, const char* src) {
97 return v8::Script::Compile(
98 isolate->GetCurrentContext(),
99 v8::String::NewFromUtf8(isolate, src, v8::NewStringType::kNormal)
100 .ToLocalChecked())
101 .ToLocalChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000102}
103
104
105TEST(GlobalProperty) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106 CcTest::InitializeVM();
107 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000108
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109 v8::Local<v8::Script> script = Compile(CcTest::isolate(),
110 "fun1 = function() { return 1; }\n"
111 "fun2 = function() { return 2; }\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 CheckFunctionName(script, "return 1", "fun1");
113 CheckFunctionName(script, "return 2", "fun2");
114}
115
116
117TEST(GlobalVar) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 CcTest::InitializeVM();
119 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000120
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121 v8::Local<v8::Script> script =
122 Compile(CcTest::isolate(),
123 "var fun1 = function() { return 1; }\n"
124 "var fun2 = function() { return 2; }\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000125 CheckFunctionName(script, "return 1", "fun1");
126 CheckFunctionName(script, "return 2", "fun2");
127}
128
129
130TEST(LocalVar) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 CcTest::InitializeVM();
132 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000133
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134 v8::Local<v8::Script> script =
135 Compile(CcTest::isolate(),
136 "function outer() {\n"
137 " var fun1 = function() { return 1; }\n"
138 " var fun2 = function() { return 2; }\n"
139 "}");
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 CheckFunctionName(script, "return 1", "fun1");
141 CheckFunctionName(script, "return 2", "fun2");
142}
143
144
145TEST(InConstructor) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 CcTest::InitializeVM();
147 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000148
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149 v8::Local<v8::Script> script =
150 Compile(CcTest::isolate(),
151 "function MyClass() {\n"
152 " this.method1 = function() { return 1; }\n"
153 " this.method2 = function() { return 2; }\n"
154 "}");
Steve Blocka7e24c12009-10-30 11:49:00 +0000155 CheckFunctionName(script, "return 1", "MyClass.method1");
156 CheckFunctionName(script, "return 2", "MyClass.method2");
157}
158
159
160TEST(Factory) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161 CcTest::InitializeVM();
162 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000163
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000164 v8::Local<v8::Script> script =
165 Compile(CcTest::isolate(),
166 "function createMyObj() {\n"
167 " var obj = {};\n"
168 " obj.method1 = function() { return 1; }\n"
169 " obj.method2 = function() { return 2; }\n"
170 " return obj;\n"
171 "}");
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 CheckFunctionName(script, "return 1", "obj.method1");
173 CheckFunctionName(script, "return 2", "obj.method2");
174}
175
176
177TEST(Static) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178 CcTest::InitializeVM();
179 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000180
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000181 v8::Local<v8::Script> script =
182 Compile(CcTest::isolate(),
183 "function MyClass() {}\n"
184 "MyClass.static1 = function() { return 1; }\n"
185 "MyClass.static2 = function() { return 2; }\n"
186 "MyClass.MyInnerClass = {}\n"
187 "MyClass.MyInnerClass.static3 = function() { return 3; }\n"
188 "MyClass.MyInnerClass.static4 = function() { return 4; }");
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 CheckFunctionName(script, "return 1", "MyClass.static1");
190 CheckFunctionName(script, "return 2", "MyClass.static2");
191 CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.static3");
192 CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.static4");
193}
194
195
196TEST(Prototype) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000197 CcTest::InitializeVM();
198 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000199
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000200 v8::Local<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000201 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000202 "function MyClass() {}\n"
203 "MyClass.prototype.method1 = function() { return 1; }\n"
204 "MyClass.prototype.method2 = function() { return 2; }\n"
205 "MyClass.MyInnerClass = function() {}\n"
206 "MyClass.MyInnerClass.prototype.method3 = function() { return 3; }\n"
207 "MyClass.MyInnerClass.prototype.method4 = function() { return 4; }");
208 CheckFunctionName(script, "return 1", "MyClass.method1");
209 CheckFunctionName(script, "return 2", "MyClass.method2");
210 CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.method3");
211 CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.method4");
212}
213
214
215TEST(ObjectLiteral) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000216 CcTest::InitializeVM();
217 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000218
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000219 v8::Local<v8::Script> script =
220 Compile(CcTest::isolate(),
221 "function MyClass() {}\n"
222 "MyClass.prototype = {\n"
223 " method1: function() { return 1; },\n"
224 " method2: function() { return 2; } }");
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 CheckFunctionName(script, "return 1", "MyClass.method1");
226 CheckFunctionName(script, "return 2", "MyClass.method2");
227}
228
229
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000230TEST(UpperCaseClass) {
231 CcTest::InitializeVM();
232 v8::HandleScope scope(CcTest::isolate());
233
234 v8::Local<v8::Script> script = Compile(CcTest::isolate(),
235 "'use strict';\n"
236 "class MyClass {\n"
237 " constructor() {\n"
238 " this.value = 1;\n"
239 " }\n"
240 " method() {\n"
241 " this.value = 2;\n"
242 " }\n"
243 "}");
244 CheckFunctionName(script, "this.value = 1", "MyClass");
245 CheckFunctionName(script, "this.value = 2", "MyClass.method");
246}
247
248
249TEST(LowerCaseClass) {
250 CcTest::InitializeVM();
251 v8::HandleScope scope(CcTest::isolate());
252
253 v8::Local<v8::Script> script = Compile(CcTest::isolate(),
254 "'use strict';\n"
255 "class myclass {\n"
256 " constructor() {\n"
257 " this.value = 1;\n"
258 " }\n"
259 " method() {\n"
260 " this.value = 2;\n"
261 " }\n"
262 "}");
263 CheckFunctionName(script, "this.value = 1", "myclass");
264 CheckFunctionName(script, "this.value = 2", "myclass.method");
265}
266
267
Steve Blocka7e24c12009-10-30 11:49:00 +0000268TEST(AsParameter) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000269 CcTest::InitializeVM();
270 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000271
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000272 v8::Local<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000273 CcTest::isolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000274 "function f1(a) { return a(); }\n"
275 "function f2(a, b) { return a() + b(); }\n"
276 "var result1 = f1(function() { return 1; })\n"
277 "var result2 = f2(function() { return 2; }, function() { return 3; })");
278 // Can't infer names here.
279 CheckFunctionName(script, "return 1", "");
280 CheckFunctionName(script, "return 2", "");
281 CheckFunctionName(script, "return 3", "");
282}
283
284
285TEST(MultipleFuncsConditional) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286 CcTest::InitializeVM();
287 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000288
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000289 v8::Local<v8::Script> script = Compile(CcTest::isolate(),
290 "fun1 = 0 ?\n"
291 " function() { return 1; } :\n"
292 " function() { return 2; }");
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 CheckFunctionName(script, "return 1", "fun1");
294 CheckFunctionName(script, "return 2", "fun1");
295}
296
297
298TEST(MultipleFuncsInLiteral) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299 CcTest::InitializeVM();
300 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000301
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000302 v8::Local<v8::Script> script =
303 Compile(CcTest::isolate(),
304 "function MyClass() {}\n"
305 "MyClass.prototype = {\n"
306 " method1: 0 ? function() { return 1; } :\n"
307 " function() { return 2; } }");
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 CheckFunctionName(script, "return 1", "MyClass.method1");
309 CheckFunctionName(script, "return 2", "MyClass.method1");
310}
311
312
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000313TEST(AnonymousInAnonymousClosure1) {
314 CcTest::InitializeVM();
315 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000316
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000317 v8::Local<v8::Script> script = Compile(CcTest::isolate(),
318 "(function() {\n"
319 " (function() {\n"
320 " var a = 1;\n"
321 " return;\n"
322 " })();\n"
323 " var b = function() {\n"
324 " var c = 1;\n"
325 " return;\n"
326 " };\n"
327 "})();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 CheckFunctionName(script, "return", "");
329}
330
331
332TEST(AnonymousInAnonymousClosure2) {
333 CcTest::InitializeVM();
334 v8::HandleScope scope(CcTest::isolate());
335
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000336 v8::Local<v8::Script> script = Compile(CcTest::isolate(),
337 "(function() {\n"
338 " (function() {\n"
339 " var a = 1;\n"
340 " return;\n"
341 " })();\n"
342 " var c = 1;\n"
343 "})();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000344 CheckFunctionName(script, "return", "");
345}
346
347
348TEST(NamedInAnonymousClosure) {
349 CcTest::InitializeVM();
350 v8::HandleScope scope(CcTest::isolate());
351
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000352 v8::Local<v8::Script> script = Compile(CcTest::isolate(),
353 "var foo = function() {\n"
354 " (function named() {\n"
355 " var a = 1;\n"
356 " })();\n"
357 " var c = 1;\n"
358 " return;\n"
359 "};");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360 CheckFunctionName(script, "return", "foo");
361}
362
363
364// See http://code.google.com/p/v8/issues/detail?id=380
365TEST(Issue380) {
366 CcTest::InitializeVM();
367 v8::HandleScope scope(CcTest::isolate());
368
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000369 v8::Local<v8::Script> script =
370 Compile(CcTest::isolate(),
371 "function a() {\n"
372 "var result = function(p,a,c,k,e,d)"
373 "{return p}(\"if blah blah\",62,1976,\'a|b\'.split(\'|\'),0,{})\n"
374 "}");
Steve Blocka7e24c12009-10-30 11:49:00 +0000375 CheckFunctionName(script, "return p", "");
376}
Ben Murdoch257744e2011-11-30 15:57:28 +0000377
378
379TEST(MultipleAssignments) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 CcTest::InitializeVM();
381 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +0000382
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000383 v8::Local<v8::Script> script =
384 Compile(CcTest::isolate(),
385 "var fun1 = fun2 = function () { return 1; }\n"
386 "var bar1 = bar2 = bar3 = function () { return 2; }\n"
387 "foo1 = foo2 = function () { return 3; }\n"
388 "baz1 = baz2 = baz3 = function () { return 4; }");
Ben Murdoch257744e2011-11-30 15:57:28 +0000389 CheckFunctionName(script, "return 1", "fun2");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000390 CheckFunctionName(script, "return 2", "bar3");
391 CheckFunctionName(script, "return 3", "foo2");
392 CheckFunctionName(script, "return 4", "baz3");
Ben Murdoch257744e2011-11-30 15:57:28 +0000393}
394
395
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000396TEST(AsConstructorParameter) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000397 CcTest::InitializeVM();
398 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +0000399
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000400 v8::Local<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000401 CcTest::isolate(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000402 "function Foo() {}\n"
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000403 "var foo = new Foo(function() { return 1; })\n"
404 "var bar = new Foo(function() { return 2; }, function() { return 3; })");
Ben Murdoch257744e2011-11-30 15:57:28 +0000405 CheckFunctionName(script, "return 1", "");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000406 CheckFunctionName(script, "return 2", "");
407 CheckFunctionName(script, "return 3", "");
Ben Murdoch257744e2011-11-30 15:57:28 +0000408}
409
410
411TEST(FactoryHashmap) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412 CcTest::InitializeVM();
413 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +0000414
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000415 v8::Local<v8::Script> script =
416 Compile(CcTest::isolate(),
417 "function createMyObj() {\n"
418 " var obj = {};\n"
419 " obj[\"method1\"] = function() { return 1; }\n"
420 " obj[\"method2\"] = function() { return 2; }\n"
421 " return obj;\n"
422 "}");
Ben Murdoch257744e2011-11-30 15:57:28 +0000423 CheckFunctionName(script, "return 1", "obj.method1");
424 CheckFunctionName(script, "return 2", "obj.method2");
425}
426
427
428TEST(FactoryHashmapVariable) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000429 CcTest::InitializeVM();
430 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +0000431
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000432 v8::Local<v8::Script> script =
433 Compile(CcTest::isolate(),
434 "function createMyObj() {\n"
435 " var obj = {};\n"
436 " var methodName = \"method1\";\n"
437 " obj[methodName] = function() { return 1; }\n"
438 " methodName = \"method2\";\n"
439 " obj[methodName] = function() { return 2; }\n"
440 " return obj;\n"
441 "}");
Ben Murdoch257744e2011-11-30 15:57:28 +0000442 // Can't infer function names statically.
443 CheckFunctionName(script, "return 1", "obj.(anonymous function)");
444 CheckFunctionName(script, "return 2", "obj.(anonymous function)");
445}
446
447
448TEST(FactoryHashmapConditional) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000449 CcTest::InitializeVM();
450 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +0000451
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000452 v8::Local<v8::Script> script = Compile(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000453 CcTest::isolate(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000454 "function createMyObj() {\n"
455 " var obj = {};\n"
456 " obj[0 ? \"method1\" : \"method2\"] = function() { return 1; }\n"
457 " return obj;\n"
458 "}");
459 // Can't infer the function name statically.
460 CheckFunctionName(script, "return 1", "obj.(anonymous function)");
461}
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000462
463
464TEST(GlobalAssignmentAndCall) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000465 CcTest::InitializeVM();
466 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000467
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000468 v8::Local<v8::Script> script = Compile(CcTest::isolate(),
469 "var Foo = function() {\n"
470 " return 1;\n"
471 "}();\n"
472 "var Baz = Bar = function() {\n"
473 " return 2;\n"
474 "}");
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000475 // The inferred name is empty, because this is an assignment of a result.
476 CheckFunctionName(script, "return 1", "");
477 // See MultipleAssignments test.
478 CheckFunctionName(script, "return 2", "Bar");
479}
480
481
482TEST(AssignmentAndCall) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483 CcTest::InitializeVM();
484 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000485
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000486 v8::Local<v8::Script> script = Compile(CcTest::isolate(),
487 "(function Enclosing() {\n"
488 " var Foo;\n"
489 " Foo = function() {\n"
490 " return 1;\n"
491 " }();\n"
492 " var Baz = Bar = function() {\n"
493 " return 2;\n"
494 " }\n"
495 "})();");
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000496 // The inferred name is empty, because this is an assignment of a result.
497 CheckFunctionName(script, "return 1", "");
498 // See MultipleAssignments test.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000499 // TODO(2276): Lazy compiling the enclosing outer closure would yield
500 // in "Enclosing.Bar" being the inferred name here.
501 CheckFunctionName(script, "return 2", "Bar");
502}
503
504
505TEST(MethodAssignmentInAnonymousFunctionCall) {
506 CcTest::InitializeVM();
507 v8::HandleScope scope(CcTest::isolate());
508
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000509 v8::Local<v8::Script> script =
510 Compile(CcTest::isolate(),
511 "(function () {\n"
512 " var EventSource = function () { };\n"
513 " EventSource.prototype.addListener = function () {\n"
514 " return 2012;\n"
515 " };\n"
516 " this.PublicEventSource = EventSource;\n"
517 "})();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000518 CheckFunctionName(script, "return 2012", "EventSource.addListener");
519}
520
521
522TEST(ReturnAnonymousFunction) {
523 CcTest::InitializeVM();
524 v8::HandleScope scope(CcTest::isolate());
525
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000526 v8::Local<v8::Script> script = Compile(CcTest::isolate(),
527 "(function() {\n"
528 " function wrapCode() {\n"
529 " return function () {\n"
530 " return 2012;\n"
531 " };\n"
532 " };\n"
533 " var foo = 10;\n"
534 " function f() {\n"
535 " return wrapCode();\n"
536 " }\n"
537 " this.ref = f;\n"
538 "})()");
539 script->Run(CcTest::isolate()->GetCurrentContext()).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000540 CheckFunctionName(script, "return 2012", "");
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000541}