test-func-name-inference.cc 21.1 KB
Newer Older
ager@chromium.org's avatar
ager@chromium.org committed
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
#include <memory>
29

30
#include "src/init/v8.h"
31

32
#include "src/api/api-inl.h"
33
#include "src/debug/debug.h"
34
#include "src/objects/objects-inl.h"
35
#include "src/strings/string-search.h"
36
#include "test/cctest/cctest.h"
37

38 39
using ::v8::base::CStrVector;
using ::v8::base::Vector;
40
using ::v8::internal::Factory;
41
using ::v8::internal::Handle;
42
using ::v8::internal::Heap;
43
using ::v8::internal::JSFunction;
44
using ::v8::internal::Runtime;
45 46 47
using ::v8::internal::SharedFunctionInfo;


48
static void CheckFunctionName(v8::Local<v8::Script> script,
49 50
                              const char* func_pos_src,
                              const char* ref_inferred_name) {
51
  i::Isolate* isolate = CcTest::i_isolate();
52

53
  // Get script source.
54
  Handle<i::Object> obj = v8::Utils::OpenHandle(*script);
55 56 57
  Handle<SharedFunctionInfo> shared_function;
  if (obj->IsSharedFunctionInfo()) {
    shared_function =
58
        Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(*obj), isolate);
59 60
  } else {
    shared_function =
61
        Handle<SharedFunctionInfo>(JSFunction::cast(*obj).shared(), isolate);
62
  }
63 64
  Handle<i::Script> i_script(i::Script::cast(shared_function->script()),
                             isolate);
65
  CHECK(i_script->source().IsString());
66
  Handle<i::String> script_src(i::String::cast(i_script->source()), isolate);
67 68

  // Find the position of a given func source substring in the source.
69 70
  int func_pos;
  {
71
    i::DisallowGarbageCollection no_gc;
72 73
    v8::base::Vector<const uint8_t> func_pos_str =
        v8::base::OneByteVector(func_pos_src);
74
    i::String::FlatContent script_content = script_src->GetFlatContent(no_gc);
75 76 77
    func_pos = SearchString(isolate, script_content.ToOneByteVector(),
                            func_pos_str, 0);
  }
78 79 80
  CHECK_NE(0, func_pos);

  // Obtain SharedFunctionInfo for the function.
81 82
  Handle<SharedFunctionInfo> shared_func_info =
      Handle<SharedFunctionInfo>::cast(
83 84
          isolate->debug()->FindInnermostContainingFunctionInfo(i_script,
                                                                func_pos));
85 86

  // Verify inferred function name.
87
  std::unique_ptr<char[]> inferred_name =
88
      shared_func_info->inferred_name().ToCString();
89 90
  i::PrintF("expected: %s, found: %s\n", ref_inferred_name,
            inferred_name.get());
91
  CHECK_EQ(0, strcmp(ref_inferred_name, inferred_name.get()));
92 93 94
}


95 96 97
static v8::Local<v8::Script> Compile(v8::Isolate* isolate, const char* src) {
  return v8::Script::Compile(
             isolate->GetCurrentContext(),
98
             v8::String::NewFromUtf8(isolate, src).ToLocalChecked())
99
      .ToLocalChecked();
100 101 102 103
}


TEST(GlobalProperty) {
104 105
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
106

107 108 109
  v8::Local<v8::Script> script = Compile(CcTest::isolate(),
                                         "fun1 = function() { return 1; }\n"
                                         "fun2 = function() { return 2; }\n");
110 111 112 113 114 115
  CheckFunctionName(script, "return 1", "fun1");
  CheckFunctionName(script, "return 2", "fun2");
}


TEST(GlobalVar) {
116 117
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
118

119 120 121 122
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "var fun1 = function() { return 1; }\n"
              "var fun2 = function() { return 2; }\n");
123 124 125 126 127 128
  CheckFunctionName(script, "return 1", "fun1");
  CheckFunctionName(script, "return 2", "fun2");
}


TEST(LocalVar) {
129 130
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
131

132 133 134 135 136 137
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "function outer() {\n"
              "  var fun1 = function() { return 1; }\n"
              "  var fun2 = function() { return 2; }\n"
              "}");
138 139 140 141
  CheckFunctionName(script, "return 1", "fun1");
  CheckFunctionName(script, "return 2", "fun2");
}

142 143 144 145 146 147 148 149 150 151 152 153 154
TEST(ObjectProperty) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "var obj = {\n"
              "  fun1: function() { return 1; },\n"
              "  fun2: class { constructor() { return 2; } }\n"
              "}");
  CheckFunctionName(script, "return 1", "obj.fun1");
  CheckFunctionName(script, "return 2", "obj.fun2");
}
155 156

TEST(InConstructor) {
157 158
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
159

160 161 162 163 164 165
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "function MyClass() {\n"
              "  this.method1 = function() { return 1; }\n"
              "  this.method2 = function() { return 2; }\n"
              "}");
166 167 168 169 170 171
  CheckFunctionName(script, "return 1", "MyClass.method1");
  CheckFunctionName(script, "return 2", "MyClass.method2");
}


TEST(Factory) {
172 173
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
174

175 176 177 178 179 180 181 182
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "function createMyObj() {\n"
              "  var obj = {};\n"
              "  obj.method1 = function() { return 1; }\n"
              "  obj.method2 = function() { return 2; }\n"
              "  return obj;\n"
              "}");
183 184 185 186 187 188
  CheckFunctionName(script, "return 1", "obj.method1");
  CheckFunctionName(script, "return 2", "obj.method2");
}


TEST(Static) {
189 190
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
191

192 193 194 195 196 197 198 199
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "function MyClass() {}\n"
              "MyClass.static1 = function() { return 1; }\n"
              "MyClass.static2 = function() { return 2; }\n"
              "MyClass.MyInnerClass = {}\n"
              "MyClass.MyInnerClass.static3 = function() { return 3; }\n"
              "MyClass.MyInnerClass.static4 = function() { return 4; }");
200 201 202 203 204 205 206 207
  CheckFunctionName(script, "return 1", "MyClass.static1");
  CheckFunctionName(script, "return 2", "MyClass.static2");
  CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.static3");
  CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.static4");
}


TEST(Prototype) {
208 209
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
210

211
  v8::Local<v8::Script> script = Compile(
212
      CcTest::isolate(),
213 214 215 216 217 218 219 220 221 222 223 224 225 226
      "function MyClass() {}\n"
      "MyClass.prototype.method1 = function() { return 1; }\n"
      "MyClass.prototype.method2 = function() { return 2; }\n"
      "MyClass.MyInnerClass = function() {}\n"
      "MyClass.MyInnerClass.prototype.method3 = function() { return 3; }\n"
      "MyClass.MyInnerClass.prototype.method4 = function() { return 4; }");
  CheckFunctionName(script, "return 1", "MyClass.method1");
  CheckFunctionName(script, "return 2", "MyClass.method2");
  CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.method3");
  CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.method4");
}


TEST(ObjectLiteral) {
227 228
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
229

230 231 232 233 234 235
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "function MyClass() {}\n"
              "MyClass.prototype = {\n"
              "  method1: function() { return 1; },\n"
              "  method2: function() { return 2; } }");
236 237 238 239 240
  CheckFunctionName(script, "return 1", "MyClass.method1");
  CheckFunctionName(script, "return 2", "MyClass.method2");
}


241 242 243 244
TEST(UpperCaseClass) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

245 246 247 248 249 250 251 252 253 254
  v8::Local<v8::Script> script = Compile(CcTest::isolate(),
                                         "'use strict';\n"
                                         "class MyClass {\n"
                                         "  constructor() {\n"
                                         "    this.value = 1;\n"
                                         "  }\n"
                                         "  method() {\n"
                                         "    this.value = 2;\n"
                                         "  }\n"
                                         "}");
255 256 257 258 259 260 261 262 263
  CheckFunctionName(script, "this.value = 1", "MyClass");
  CheckFunctionName(script, "this.value = 2", "MyClass.method");
}


TEST(LowerCaseClass) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

264 265 266 267 268 269 270 271 272 273
  v8::Local<v8::Script> script = Compile(CcTest::isolate(),
                                         "'use strict';\n"
                                         "class myclass {\n"
                                         "  constructor() {\n"
                                         "    this.value = 1;\n"
                                         "  }\n"
                                         "  method() {\n"
                                         "    this.value = 2;\n"
                                         "  }\n"
                                         "}");
274 275 276 277 278
  CheckFunctionName(script, "this.value = 1", "myclass");
  CheckFunctionName(script, "this.value = 2", "myclass.method");
}


279
TEST(AsParameter) {
280 281
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
282

283
  v8::Local<v8::Script> script = Compile(
284
      CcTest::isolate(),
285 286 287 288 289 290 291 292 293
      "function f1(a) { return a(); }\n"
      "function f2(a, b) { return a() + b(); }\n"
      "var result1 = f1(function() { return 1; })\n"
      "var result2 = f2(function() { return 2; }, function() { return 3; })");
  // Can't infer names here.
  CheckFunctionName(script, "return 1", "");
  CheckFunctionName(script, "return 2", "");
  CheckFunctionName(script, "return 3", "");
}
294 295 296


TEST(MultipleFuncsConditional) {
297 298
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
299

300
  v8::Local<v8::Script> script = Compile(CcTest::isolate(),
301 302
                                         "var x = 0;\n"
                                         "fun1 = x ?\n"
303 304
                                         "    function() { return 1; } :\n"
                                         "    function() { return 2; }");
305 306 307 308 309 310
  CheckFunctionName(script, "return 1", "fun1");
  CheckFunctionName(script, "return 2", "fun1");
}


TEST(MultipleFuncsInLiteral) {
311 312
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
313

314 315
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
316
              "var x = 0;\n"
317 318
              "function MyClass() {}\n"
              "MyClass.prototype = {\n"
319
              "  method1: x ? function() { return 1; } :\n"
320
              "               function() { return 2; } }");
321 322 323
  CheckFunctionName(script, "return 1", "MyClass.method1");
  CheckFunctionName(script, "return 2", "MyClass.method1");
}
324 325


326 327 328 329
TEST(AnonymousInAnonymousClosure1) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

330 331 332 333 334 335 336 337 338 339 340
  v8::Local<v8::Script> script = Compile(CcTest::isolate(),
                                         "(function() {\n"
                                         "  (function() {\n"
                                         "      var a = 1;\n"
                                         "      return;\n"
                                         "  })();\n"
                                         "  var b = function() {\n"
                                         "      var c = 1;\n"
                                         "      return;\n"
                                         "  };\n"
                                         "})();");
341 342 343 344 345 346 347 348
  CheckFunctionName(script, "return", "");
}


TEST(AnonymousInAnonymousClosure2) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

349 350 351 352 353 354 355 356
  v8::Local<v8::Script> script = Compile(CcTest::isolate(),
                                         "(function() {\n"
                                         "  (function() {\n"
                                         "      var a = 1;\n"
                                         "      return;\n"
                                         "  })();\n"
                                         "  var c = 1;\n"
                                         "})();");
357 358 359 360 361 362 363 364
  CheckFunctionName(script, "return", "");
}


TEST(NamedInAnonymousClosure) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

365 366 367 368 369 370 371 372
  v8::Local<v8::Script> script = Compile(CcTest::isolate(),
                                         "var foo = function() {\n"
                                         "  (function named() {\n"
                                         "      var a = 1;\n"
                                         "  })();\n"
                                         "  var c = 1;\n"
                                         "  return;\n"
                                         "};");
373 374 375 376
  CheckFunctionName(script, "return", "foo");
}


377 378
// See http://code.google.com/p/v8/issues/detail?id=380
TEST(Issue380) {
379 380
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
381

382 383 384 385 386 387
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "function a() {\n"
              "var result = function(p,a,c,k,e,d)"
              "{return p}(\"if blah blah\",62,1976,\'a|b\'.split(\'|\'),0,{})\n"
              "}");
388 389
  CheckFunctionName(script, "return p", "");
}
390 391 392


TEST(MultipleAssignments) {
393 394
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
395

396 397 398 399 400 401
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "var fun1 = fun2 = function () { return 1; }\n"
              "var bar1 = bar2 = bar3 = function () { return 2; }\n"
              "foo1 = foo2 = function () { return 3; }\n"
              "baz1 = baz2 = baz3 = function () { return 4; }");
402
  CheckFunctionName(script, "return 1", "fun2");
403 404 405
  CheckFunctionName(script, "return 2", "bar3");
  CheckFunctionName(script, "return 3", "foo2");
  CheckFunctionName(script, "return 4", "baz3");
406 407 408
}


409
TEST(AsConstructorParameter) {
410 411
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
ager@chromium.org's avatar
ager@chromium.org committed
412

413
  v8::Local<v8::Script> script = Compile(
414
      CcTest::isolate(),
415
      "function Foo() {}\n"
416 417
      "var foo = new Foo(function() { return 1; })\n"
      "var bar = new Foo(function() { return 2; }, function() { return 3; })");
418
  CheckFunctionName(script, "return 1", "");
419 420
  CheckFunctionName(script, "return 2", "");
  CheckFunctionName(script, "return 3", "");
421 422 423 424
}


TEST(FactoryHashmap) {
425 426
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
ager@chromium.org's avatar
ager@chromium.org committed
427

428 429 430 431 432 433 434 435
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "function createMyObj() {\n"
              "  var obj = {};\n"
              "  obj[\"method1\"] = function() { return 1; }\n"
              "  obj[\"method2\"] = function() { return 2; }\n"
              "  return obj;\n"
              "}");
436 437 438 439 440 441
  CheckFunctionName(script, "return 1", "obj.method1");
  CheckFunctionName(script, "return 2", "obj.method2");
}


TEST(FactoryHashmapVariable) {
442 443
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
ager@chromium.org's avatar
ager@chromium.org committed
444

445 446 447 448 449 450 451 452 453 454
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "function createMyObj() {\n"
              "  var obj = {};\n"
              "  var methodName = \"method1\";\n"
              "  obj[methodName] = function() { return 1; }\n"
              "  methodName = \"method2\";\n"
              "  obj[methodName] = function() { return 2; }\n"
              "  return obj;\n"
              "}");
455
  // Can't infer function names statically.
456 457
  CheckFunctionName(script, "return 1", "obj.<computed>");
  CheckFunctionName(script, "return 2", "obj.<computed>");
458 459 460 461
}


TEST(FactoryHashmapConditional) {
462 463
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
ager@chromium.org's avatar
ager@chromium.org committed
464

465
  v8::Local<v8::Script> script = Compile(
466
      CcTest::isolate(),
467 468 469 470 471 472
      "function createMyObj() {\n"
      "  var obj = {};\n"
      "  obj[0 ? \"method1\" : \"method2\"] = function() { return 1; }\n"
      "  return obj;\n"
      "}");
  // Can't infer the function name statically.
473
  CheckFunctionName(script, "return 1", "obj.<computed>");
474
}
475 476 477


TEST(GlobalAssignmentAndCall) {
478 479
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
480

481 482 483 484 485 486 487
  v8::Local<v8::Script> script = Compile(CcTest::isolate(),
                                         "var Foo = function() {\n"
                                         "  return 1;\n"
                                         "}();\n"
                                         "var Baz = Bar = function() {\n"
                                         "  return 2;\n"
                                         "}");
488 489 490 491 492 493 494 495
  // The inferred name is empty, because this is an assignment of a result.
  CheckFunctionName(script, "return 1", "");
  // See MultipleAssignments test.
  CheckFunctionName(script, "return 2", "Bar");
}


TEST(AssignmentAndCall) {
496 497
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
498

499 500 501 502 503 504 505 506 507 508
  v8::Local<v8::Script> script = Compile(CcTest::isolate(),
                                         "(function Enclosing() {\n"
                                         "  var Foo;\n"
                                         "  Foo = function() {\n"
                                         "    return 1;\n"
                                         "  }();\n"
                                         "  var Baz = Bar = function() {\n"
                                         "    return 2;\n"
                                         "  }\n"
                                         "})();");
509 510 511
  // The inferred name is empty, because this is an assignment of a result.
  CheckFunctionName(script, "return 1", "");
  // See MultipleAssignments test.
512 513 514
  // TODO(2276): Lazy compiling the enclosing outer closure would yield
  // in "Enclosing.Bar" being the inferred name here.
  CheckFunctionName(script, "return 2", "Bar");
515
}
516 517 518


TEST(MethodAssignmentInAnonymousFunctionCall) {
519 520
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
521

522 523 524 525 526 527 528 529 530
  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "(function () {\n"
              "    var EventSource = function () { };\n"
              "    EventSource.prototype.addListener = function () {\n"
              "        return 2012;\n"
              "    };\n"
              "    this.PublicEventSource = EventSource;\n"
              "})();");
531 532 533 534 535
  CheckFunctionName(script, "return 2012", "EventSource.addListener");
}


TEST(ReturnAnonymousFunction) {
536 537
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
538

539 540 541 542 543 544 545 546 547 548 549 550 551 552
  v8::Local<v8::Script> script = Compile(CcTest::isolate(),
                                         "(function() {\n"
                                         "  function wrapCode() {\n"
                                         "    return function () {\n"
                                         "      return 2012;\n"
                                         "    };\n"
                                         "  };\n"
                                         "  var foo = 10;\n"
                                         "  function f() {\n"
                                         "    return wrapCode();\n"
                                         "  }\n"
                                         "  this.ref = f;\n"
                                         "})()");
  script->Run(CcTest::isolate()->GetCurrentContext()).ToLocalChecked();
553 554
  CheckFunctionName(script, "return 2012", "");
}
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

TEST(IgnoreExtendsClause) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  v8::Local<v8::Script> script =
      Compile(CcTest::isolate(),
              "(function() {\n"
              "  var foo = {};\n"
              "  foo.C = class {}\n"
              "  class D extends foo.C {}\n"
              "  foo.bar = function() { return 1; };\n"
              "})()");
  script->Run(CcTest::isolate()->GetCurrentContext()).ToLocalChecked();
  CheckFunctionName(script, "return 1", "foo.bar");
}
571 572 573 574 575 576 577 578 579 580 581 582

TEST(ParameterAndArrow) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  v8::Local<v8::Script> script = Compile(CcTest::isolate(),
                                         "(function(param) {\n"
                                         "  (() => { return 2017 })();\n"
                                         "})()");
  script->Run(CcTest::isolate()->GetCurrentContext()).ToLocalChecked();
  CheckFunctionName(script, "return 2017", "");
}