test-func-name-inference.cc 15 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

29 30 31
#include "v8.h"

#include "api.h"
32
#include "debug.h"
33 34 35 36
#include "runtime.h"
#include "cctest.h"


37 38
using ::v8::internal::CStrVector;
using ::v8::internal::Factory;
39
using ::v8::internal::Handle;
40
using ::v8::internal::Heap;
41
using ::v8::internal::Isolate;
42 43
using ::v8::internal::JSFunction;
using ::v8::internal::Object;
44
using ::v8::internal::Runtime;
45
using ::v8::internal::Script;
46
using ::v8::internal::SmartArrayPointer;
47 48 49 50 51 52 53
using ::v8::internal::SharedFunctionInfo;
using ::v8::internal::String;


static void CheckFunctionName(v8::Handle<v8::Script> script,
                              const char* func_pos_src,
                              const char* ref_inferred_name) {
54 55 56
  Isolate* isolate = Isolate::Current();
  Factory* factory = isolate->factory();

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

  // Find the position of a given func source substring in the source.
  Handle<String> func_pos_str =
73 74
      factory->NewStringFromAscii(CStrVector(func_pos_src));
  int func_pos = Runtime::StringMatch(isolate,
75 76 77
                                      script_src,
                                      func_pos_str,
                                      0);
78 79
  CHECK_NE(0, func_pos);

80
#ifdef ENABLE_DEBUGGER_SUPPORT
81
  // Obtain SharedFunctionInfo for the function.
82
  isolate->debug()->PrepareForBreakPoints();
83
  Object* shared_func_info_ptr =
84
      isolate->debug()->FindSharedFunctionInfoInScript(i_script, func_pos);
85
  CHECK(shared_func_info_ptr != HEAP->undefined_value());
86 87 88 89
  Handle<SharedFunctionInfo> shared_func_info(
      SharedFunctionInfo::cast(shared_func_info_ptr));

  // Verify inferred function name.
90
  SmartArrayPointer<char> inferred_name =
91 92
      shared_func_info->inferred_name()->ToCString();
  CHECK_EQ(ref_inferred_name, *inferred_name);
93
#endif  // ENABLE_DEBUGGER_SUPPORT
94 95 96 97 98 99 100 101 102
}


static v8::Handle<v8::Script> Compile(const char* src) {
  return v8::Script::Compile(v8::String::New(src));
}


TEST(GlobalProperty) {
103 104
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
105 106 107 108 109 110 111 112 113 114

  v8::Handle<v8::Script> script = Compile(
      "fun1 = function() { return 1; }\n"
      "fun2 = function() { return 2; }\n");
  CheckFunctionName(script, "return 1", "fun1");
  CheckFunctionName(script, "return 2", "fun2");
}


TEST(GlobalVar) {
115 116
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
117 118 119 120 121 122 123 124 125 126

  v8::Handle<v8::Script> script = Compile(
      "var fun1 = function() { return 1; }\n"
      "var fun2 = function() { return 2; }\n");
  CheckFunctionName(script, "return 1", "fun1");
  CheckFunctionName(script, "return 2", "fun2");
}


TEST(LocalVar) {
127 128
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
129 130 131 132 133 134 135 136 137 138 139 140

  v8::Handle<v8::Script> script = Compile(
      "function outer() {\n"
      "  var fun1 = function() { return 1; }\n"
      "  var fun2 = function() { return 2; }\n"
      "}");
  CheckFunctionName(script, "return 1", "fun1");
  CheckFunctionName(script, "return 2", "fun2");
}


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

  v8::Handle<v8::Script> script = Compile(
      "function MyClass() {\n"
      "  this.method1 = function() { return 1; }\n"
      "  this.method2 = function() { return 2; }\n"
      "}");
  CheckFunctionName(script, "return 1", "MyClass.method1");
  CheckFunctionName(script, "return 2", "MyClass.method2");
}


TEST(Factory) {
155 156
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
157 158 159 160 161 162 163 164 165 166 167 168 169 170

  v8::Handle<v8::Script> script = Compile(
      "function createMyObj() {\n"
      "  var obj = {};\n"
      "  obj.method1 = function() { return 1; }\n"
      "  obj.method2 = function() { return 2; }\n"
      "  return obj;\n"
      "}");
  CheckFunctionName(script, "return 1", "obj.method1");
  CheckFunctionName(script, "return 2", "obj.method2");
}


TEST(Static) {
171 172
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

  v8::Handle<v8::Script> script = Compile(
      "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; }");
  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) {
189 190
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

  v8::Handle<v8::Script> script = Compile(
      "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) {
207 208
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
209 210 211 212 213 214 215 216 217 218 219 220

  v8::Handle<v8::Script> script = Compile(
      "function MyClass() {}\n"
      "MyClass.prototype = {\n"
      "  method1: function() { return 1; },\n"
      "  method2: function() { return 2; } }");
  CheckFunctionName(script, "return 1", "MyClass.method1");
  CheckFunctionName(script, "return 2", "MyClass.method2");
}


TEST(AsParameter) {
221 222
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
223 224 225 226 227 228 229 230 231 232 233

  v8::Handle<v8::Script> script = Compile(
      "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", "");
}
234 235 236


TEST(MultipleFuncsConditional) {
237 238
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
239 240 241 242 243 244 245 246 247 248 249

  v8::Handle<v8::Script> script = Compile(
      "fun1 = 0 ?\n"
      "    function() { return 1; } :\n"
      "    function() { return 2; }");
  CheckFunctionName(script, "return 1", "fun1");
  CheckFunctionName(script, "return 2", "fun1");
}


TEST(MultipleFuncsInLiteral) {
250 251
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
252 253 254 255 256 257 258 259 260

  v8::Handle<v8::Script> script = Compile(
      "function MyClass() {}\n"
      "MyClass.prototype = {\n"
      "  method1: 0 ? function() { return 1; } :\n"
      "               function() { return 2; } }");
  CheckFunctionName(script, "return 1", "MyClass.method1");
  CheckFunctionName(script, "return 2", "MyClass.method1");
}
261 262


263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
TEST(AnonymousInAnonymousClosure1) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  v8::Handle<v8::Script> script = Compile(
      "(function() {\n"
      "  (function() {\n"
      "      var a = 1;\n"
      "      return;\n"
      "  })();\n"
      "  var b = function() {\n"
      "      var c = 1;\n"
      "      return;\n"
      "  };\n"
      "})();");
  CheckFunctionName(script, "return", "");
}


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

  v8::Handle<v8::Script> script = Compile(
      "(function() {\n"
      "  (function() {\n"
      "      var a = 1;\n"
      "      return;\n"
      "  })();\n"
      "  var c = 1;\n"
      "})();");
  CheckFunctionName(script, "return", "");
}


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

  v8::Handle<v8::Script> script = Compile(
      "var foo = function() {\n"
      "  (function named() {\n"
      "      var a = 1;\n"
      "  })();\n"
      "  var c = 1;\n"
      "  return;\n"
      "};");
  CheckFunctionName(script, "return", "foo");
}


314 315
// See http://code.google.com/p/v8/issues/detail?id=380
TEST(Issue380) {
316 317
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
318 319 320 321 322 323 324 325

  v8::Handle<v8::Script> script = Compile(
      "function a() {\n"
      "var result = function(p,a,c,k,e,d)"
      "{return p}(\"if blah blah\",62,1976,\'a|b\'.split(\'|\'),0,{})\n"
      "}");
  CheckFunctionName(script, "return p", "");
}
326 327 328


TEST(MultipleAssignments) {
329 330
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
331 332

  v8::Handle<v8::Script> script = Compile(
333 334 335 336
      "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; }");
337
  CheckFunctionName(script, "return 1", "fun2");
338 339 340
  CheckFunctionName(script, "return 2", "bar3");
  CheckFunctionName(script, "return 3", "foo2");
  CheckFunctionName(script, "return 4", "baz3");
341 342 343
}


344
TEST(AsConstructorParameter) {
345 346
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
ager@chromium.org's avatar
ager@chromium.org committed
347

348 349
  v8::Handle<v8::Script> script = Compile(
      "function Foo() {}\n"
350 351
      "var foo = new Foo(function() { return 1; })\n"
      "var bar = new Foo(function() { return 2; }, function() { return 3; })");
352
  CheckFunctionName(script, "return 1", "");
353 354
  CheckFunctionName(script, "return 2", "");
  CheckFunctionName(script, "return 3", "");
355 356 357 358
}


TEST(FactoryHashmap) {
359 360
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
ager@chromium.org's avatar
ager@chromium.org committed
361

362 363 364 365 366 367 368 369 370 371 372 373 374
  v8::Handle<v8::Script> script = Compile(
      "function createMyObj() {\n"
      "  var obj = {};\n"
      "  obj[\"method1\"] = function() { return 1; }\n"
      "  obj[\"method2\"] = function() { return 2; }\n"
      "  return obj;\n"
      "}");
  CheckFunctionName(script, "return 1", "obj.method1");
  CheckFunctionName(script, "return 2", "obj.method2");
}


TEST(FactoryHashmapVariable) {
375 376
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
ager@chromium.org's avatar
ager@chromium.org committed
377

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
  v8::Handle<v8::Script> script = Compile(
      "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"
      "}");
  // Can't infer function names statically.
  CheckFunctionName(script, "return 1", "obj.(anonymous function)");
  CheckFunctionName(script, "return 2", "obj.(anonymous function)");
}


TEST(FactoryHashmapConditional) {
394 395
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
ager@chromium.org's avatar
ager@chromium.org committed
396

397 398 399 400 401 402 403 404 405
  v8::Handle<v8::Script> script = Compile(
      "function createMyObj() {\n"
      "  var obj = {};\n"
      "  obj[0 ? \"method1\" : \"method2\"] = function() { return 1; }\n"
      "  return obj;\n"
      "}");
  // Can't infer the function name statically.
  CheckFunctionName(script, "return 1", "obj.(anonymous function)");
}
406 407 408


TEST(GlobalAssignmentAndCall) {
409 410
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

  v8::Handle<v8::Script> script = Compile(
      "var Foo = function() {\n"
      "  return 1;\n"
      "}();\n"
      "var Baz = Bar = function() {\n"
      "  return 2;\n"
      "}");
  // 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) {
427 428
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
429 430 431 432 433 434 435 436 437 438 439 440 441 442

  v8::Handle<v8::Script> script = Compile(
      "(function Enclosing() {\n"
      "  var Foo;\n"
      "  Foo = function() {\n"
      "    return 1;\n"
      "  }();\n"
      "  var Baz = Bar = function() {\n"
      "    return 2;\n"
      "  }\n"
      "})();");
  // The inferred name is empty, because this is an assignment of a result.
  CheckFunctionName(script, "return 1", "");
  // See MultipleAssignments test.
443 444 445
  // TODO(2276): Lazy compiling the enclosing outer closure would yield
  // in "Enclosing.Bar" being the inferred name here.
  CheckFunctionName(script, "return 2", "Bar");
446
}
447 448 449


TEST(MethodAssignmentInAnonymousFunctionCall) {
450 451
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
452 453 454 455 456 457 458 459 460 461 462 463 464 465

  v8::Handle<v8::Script> script = Compile(
      "(function () {\n"
      "    var EventSource = function () { };\n"
      "    EventSource.prototype.addListener = function () {\n"
      "        return 2012;\n"
      "    };\n"
      "    this.PublicEventSource = EventSource;\n"
      "})();");
  CheckFunctionName(script, "return 2012", "EventSource.addListener");
}


TEST(ReturnAnonymousFunction) {
466 467
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

  v8::Handle<v8::Script> script = Compile(
      "(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();
  CheckFunctionName(script, "return 2012", "");
}