2011-05-02 11:08:44 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2009-04-14 00:51:59 +00:00
|
|
|
// 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.
|
|
|
|
|
2016-07-25 10:24:45 +00:00
|
|
|
#include <memory>
|
2013-05-02 20:18:42 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2018-07-23 11:42:37 +00:00
|
|
|
#include "src/api-inl.h"
|
2015-07-31 11:07:50 +00:00
|
|
|
#include "src/debug/debug.h"
|
2017-02-09 08:35:03 +00:00
|
|
|
#include "src/objects-inl.h"
|
2014-10-20 12:07:45 +00:00
|
|
|
#include "src/string-search.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2009-04-14 00:51:59 +00:00
|
|
|
|
|
|
|
|
2009-05-25 08:39:22 +00:00
|
|
|
using ::v8::internal::CStrVector;
|
|
|
|
using ::v8::internal::Factory;
|
2009-04-14 00:51:59 +00:00
|
|
|
using ::v8::internal::Handle;
|
2009-05-25 08:39:22 +00:00
|
|
|
using ::v8::internal::Heap;
|
2009-04-14 00:51:59 +00:00
|
|
|
using ::v8::internal::JSFunction;
|
2009-05-25 08:39:22 +00:00
|
|
|
using ::v8::internal::Runtime;
|
2009-04-14 00:51:59 +00:00
|
|
|
using ::v8::internal::SharedFunctionInfo;
|
2014-10-20 12:07:45 +00:00
|
|
|
using ::v8::internal::Vector;
|
2009-04-14 00:51:59 +00:00
|
|
|
|
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
static void CheckFunctionName(v8::Local<v8::Script> script,
|
2009-04-14 00:51:59 +00:00
|
|
|
const char* func_pos_src,
|
|
|
|
const char* ref_inferred_name) {
|
2018-06-27 19:03:20 +00:00
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
2013-06-04 10:30:05 +00:00
|
|
|
|
2009-04-14 00:51:59 +00:00
|
|
|
// Get script source.
|
2018-06-27 19:03:20 +00:00
|
|
|
Handle<i::Object> obj = v8::Utils::OpenHandle(*script);
|
2010-03-23 06:04:44 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_function;
|
|
|
|
if (obj->IsSharedFunctionInfo()) {
|
|
|
|
shared_function =
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(*obj), isolate);
|
2010-03-23 06:04:44 +00:00
|
|
|
} else {
|
|
|
|
shared_function =
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<SharedFunctionInfo>(JSFunction::cast(*obj)->shared(), isolate);
|
2010-03-23 06:04:44 +00:00
|
|
|
}
|
2018-06-27 19:03:20 +00:00
|
|
|
Handle<i::Script> i_script(i::Script::cast(shared_function->script()),
|
|
|
|
isolate);
|
2009-04-14 00:51:59 +00:00
|
|
|
CHECK(i_script->source()->IsString());
|
2018-06-27 19:03:20 +00:00
|
|
|
Handle<i::String> script_src(i::String::cast(i_script->source()), isolate);
|
2009-04-14 00:51:59 +00:00
|
|
|
|
|
|
|
// Find the position of a given func source substring in the source.
|
2014-10-20 12:07:45 +00:00
|
|
|
int func_pos;
|
|
|
|
{
|
|
|
|
i::DisallowHeapAllocation no_gc;
|
|
|
|
Vector<const uint8_t> func_pos_str = i::OneByteVector(func_pos_src);
|
2018-11-30 12:40:36 +00:00
|
|
|
i::String::FlatContent script_content = script_src->GetFlatContent(no_gc);
|
2014-10-20 12:07:45 +00:00
|
|
|
func_pos = SearchString(isolate, script_content.ToOneByteVector(),
|
|
|
|
func_pos_str, 0);
|
|
|
|
}
|
2009-04-14 00:51:59 +00:00
|
|
|
CHECK_NE(0, func_pos);
|
|
|
|
|
|
|
|
// Obtain SharedFunctionInfo for the function.
|
2015-03-04 13:15:07 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_func_info =
|
|
|
|
Handle<SharedFunctionInfo>::cast(
|
|
|
|
isolate->debug()->FindSharedFunctionInfoInScript(i_script, func_pos));
|
2009-04-14 00:51:59 +00:00
|
|
|
|
|
|
|
// Verify inferred function name.
|
2016-07-25 10:24:45 +00:00
|
|
|
std::unique_ptr<char[]> inferred_name =
|
2009-04-14 00:51:59 +00:00
|
|
|
shared_func_info->inferred_name()->ToCString();
|
2015-07-30 09:21:16 +00:00
|
|
|
i::PrintF("expected: %s, found: %s\n", ref_inferred_name,
|
|
|
|
inferred_name.get());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK_EQ(0, strcmp(ref_inferred_name, inferred_name.get()));
|
2009-04-14 00:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
static v8::Local<v8::Script> Compile(v8::Isolate* isolate, const char* src) {
|
|
|
|
return v8::Script::Compile(
|
|
|
|
isolate->GetCurrentContext(),
|
|
|
|
v8::String::NewFromUtf8(isolate, src, v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked())
|
|
|
|
.ToLocalChecked();
|
2009-04-14 00:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(GlobalProperty) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script = Compile(CcTest::isolate(),
|
|
|
|
"fun1 = function() { return 1; }\n"
|
|
|
|
"fun2 = function() { return 2; }\n");
|
2009-04-14 00:51:59 +00:00
|
|
|
CheckFunctionName(script, "return 1", "fun1");
|
|
|
|
CheckFunctionName(script, "return 2", "fun2");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(GlobalVar) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script =
|
|
|
|
Compile(CcTest::isolate(),
|
|
|
|
"var fun1 = function() { return 1; }\n"
|
|
|
|
"var fun2 = function() { return 2; }\n");
|
2009-04-14 00:51:59 +00:00
|
|
|
CheckFunctionName(script, "return 1", "fun1");
|
|
|
|
CheckFunctionName(script, "return 2", "fun2");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(LocalVar) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script =
|
|
|
|
Compile(CcTest::isolate(),
|
|
|
|
"function outer() {\n"
|
|
|
|
" var fun1 = function() { return 1; }\n"
|
|
|
|
" var fun2 = function() { return 2; }\n"
|
|
|
|
"}");
|
2009-04-14 00:51:59 +00:00
|
|
|
CheckFunctionName(script, "return 1", "fun1");
|
|
|
|
CheckFunctionName(script, "return 2", "fun2");
|
|
|
|
}
|
|
|
|
|
2016-11-15 21:57:25 +00:00
|
|
|
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");
|
|
|
|
}
|
2009-04-14 00:51:59 +00:00
|
|
|
|
|
|
|
TEST(InConstructor) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script =
|
|
|
|
Compile(CcTest::isolate(),
|
|
|
|
"function MyClass() {\n"
|
|
|
|
" this.method1 = function() { return 1; }\n"
|
|
|
|
" this.method2 = function() { return 2; }\n"
|
|
|
|
"}");
|
2009-04-14 00:51:59 +00:00
|
|
|
CheckFunctionName(script, "return 1", "MyClass.method1");
|
|
|
|
CheckFunctionName(script, "return 2", "MyClass.method2");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Factory) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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"
|
|
|
|
"}");
|
2009-04-14 00:51:59 +00:00
|
|
|
CheckFunctionName(script, "return 1", "obj.method1");
|
|
|
|
CheckFunctionName(script, "return 2", "obj.method2");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Static) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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; }");
|
2009-04-14 00:51:59 +00:00
|
|
|
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) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script = Compile(
|
2013-11-22 12:43:17 +00:00
|
|
|
CcTest::isolate(),
|
2009-04-14 00:51:59 +00:00
|
|
|
"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) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script =
|
|
|
|
Compile(CcTest::isolate(),
|
|
|
|
"function MyClass() {}\n"
|
|
|
|
"MyClass.prototype = {\n"
|
|
|
|
" method1: function() { return 1; },\n"
|
|
|
|
" method2: function() { return 2; } }");
|
2009-04-14 00:51:59 +00:00
|
|
|
CheckFunctionName(script, "return 1", "MyClass.method1");
|
|
|
|
CheckFunctionName(script, "return 2", "MyClass.method2");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-30 09:21:16 +00:00
|
|
|
TEST(UpperCaseClass) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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"
|
|
|
|
"}");
|
2015-07-30 09:21:16 +00:00
|
|
|
CheckFunctionName(script, "this.value = 1", "MyClass");
|
|
|
|
CheckFunctionName(script, "this.value = 2", "MyClass.method");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(LowerCaseClass) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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"
|
|
|
|
"}");
|
2015-07-30 09:21:16 +00:00
|
|
|
CheckFunctionName(script, "this.value = 1", "myclass");
|
|
|
|
CheckFunctionName(script, "this.value = 2", "myclass.method");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-14 00:51:59 +00:00
|
|
|
TEST(AsParameter) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script = Compile(
|
2013-11-22 12:43:17 +00:00
|
|
|
CcTest::isolate(),
|
2009-04-14 00:51:59 +00:00
|
|
|
"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", "");
|
|
|
|
}
|
2009-04-16 16:34:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(MultipleFuncsConditional) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-04-16 16:34:24 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script = Compile(CcTest::isolate(),
|
2016-08-15 13:10:41 +00:00
|
|
|
"var x = 0;\n"
|
|
|
|
"fun1 = x ?\n"
|
2015-11-17 12:42:14 +00:00
|
|
|
" function() { return 1; } :\n"
|
|
|
|
" function() { return 2; }");
|
2009-04-16 16:34:24 +00:00
|
|
|
CheckFunctionName(script, "return 1", "fun1");
|
|
|
|
CheckFunctionName(script, "return 2", "fun1");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(MultipleFuncsInLiteral) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-04-16 16:34:24 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script =
|
|
|
|
Compile(CcTest::isolate(),
|
2016-08-15 13:10:41 +00:00
|
|
|
"var x = 0;\n"
|
2015-11-17 12:42:14 +00:00
|
|
|
"function MyClass() {}\n"
|
|
|
|
"MyClass.prototype = {\n"
|
2016-08-15 13:10:41 +00:00
|
|
|
" method1: x ? function() { return 1; } :\n"
|
2015-11-17 12:42:14 +00:00
|
|
|
" function() { return 2; } }");
|
2009-04-16 16:34:24 +00:00
|
|
|
CheckFunctionName(script, "return 1", "MyClass.method1");
|
|
|
|
CheckFunctionName(script, "return 2", "MyClass.method1");
|
|
|
|
}
|
2009-06-16 11:47:00 +00:00
|
|
|
|
|
|
|
|
2013-05-31 12:52:28 +00:00
|
|
|
TEST(AnonymousInAnonymousClosure1) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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"
|
|
|
|
"})();");
|
2013-05-31 12:52:28 +00:00
|
|
|
CheckFunctionName(script, "return", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(AnonymousInAnonymousClosure2) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script = Compile(CcTest::isolate(),
|
|
|
|
"(function() {\n"
|
|
|
|
" (function() {\n"
|
|
|
|
" var a = 1;\n"
|
|
|
|
" return;\n"
|
|
|
|
" })();\n"
|
|
|
|
" var c = 1;\n"
|
|
|
|
"})();");
|
2013-05-31 12:52:28 +00:00
|
|
|
CheckFunctionName(script, "return", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NamedInAnonymousClosure) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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"
|
|
|
|
"};");
|
2013-05-31 12:52:28 +00:00
|
|
|
CheckFunctionName(script, "return", "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-16 11:47:00 +00:00
|
|
|
// See http://code.google.com/p/v8/issues/detail?id=380
|
|
|
|
TEST(Issue380) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-06-16 11:47:00 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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"
|
|
|
|
"}");
|
2009-06-16 11:47:00 +00:00
|
|
|
CheckFunctionName(script, "return p", "");
|
|
|
|
}
|
2011-04-29 20:59:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(MultipleAssignments) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-04-29 20:59:29 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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; }");
|
2011-04-29 20:59:29 +00:00
|
|
|
CheckFunctionName(script, "return 1", "fun2");
|
2011-06-22 20:23:48 +00:00
|
|
|
CheckFunctionName(script, "return 2", "bar3");
|
|
|
|
CheckFunctionName(script, "return 3", "foo2");
|
|
|
|
CheckFunctionName(script, "return 4", "baz3");
|
2011-04-29 20:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-22 20:23:48 +00:00
|
|
|
TEST(AsConstructorParameter) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-05-02 11:08:44 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script = Compile(
|
2013-11-22 12:43:17 +00:00
|
|
|
CcTest::isolate(),
|
2011-04-29 20:59:29 +00:00
|
|
|
"function Foo() {}\n"
|
2011-06-22 20:23:48 +00:00
|
|
|
"var foo = new Foo(function() { return 1; })\n"
|
|
|
|
"var bar = new Foo(function() { return 2; }, function() { return 3; })");
|
2011-04-29 20:59:29 +00:00
|
|
|
CheckFunctionName(script, "return 1", "");
|
2011-06-22 20:23:48 +00:00
|
|
|
CheckFunctionName(script, "return 2", "");
|
|
|
|
CheckFunctionName(script, "return 3", "");
|
2011-04-29 20:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FactoryHashmap) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-05-02 11:08:44 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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"
|
|
|
|
"}");
|
2011-04-29 20:59:29 +00:00
|
|
|
CheckFunctionName(script, "return 1", "obj.method1");
|
|
|
|
CheckFunctionName(script, "return 2", "obj.method2");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FactoryHashmapVariable) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-05-02 11:08:44 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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"
|
|
|
|
"}");
|
2011-04-29 20:59:29 +00:00
|
|
|
// Can't infer function names statically.
|
2019-03-01 20:16:14 +00:00
|
|
|
CheckFunctionName(script, "return 1", "obj.<computed>");
|
|
|
|
CheckFunctionName(script, "return 2", "obj.<computed>");
|
2011-04-29 20:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FactoryHashmapConditional) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-05-02 11:08:44 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script = Compile(
|
2013-11-22 12:43:17 +00:00
|
|
|
CcTest::isolate(),
|
2011-04-29 20:59:29 +00:00
|
|
|
"function createMyObj() {\n"
|
|
|
|
" var obj = {};\n"
|
|
|
|
" obj[0 ? \"method1\" : \"method2\"] = function() { return 1; }\n"
|
|
|
|
" return obj;\n"
|
|
|
|
"}");
|
|
|
|
// Can't infer the function name statically.
|
2019-03-01 20:16:14 +00:00
|
|
|
CheckFunctionName(script, "return 1", "obj.<computed>");
|
2011-04-29 20:59:29 +00:00
|
|
|
}
|
2011-10-03 19:18:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(GlobalAssignmentAndCall) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-10-03 19:18:05 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Script> script = Compile(CcTest::isolate(),
|
|
|
|
"var Foo = function() {\n"
|
|
|
|
" return 1;\n"
|
|
|
|
"}();\n"
|
|
|
|
"var Baz = Bar = function() {\n"
|
|
|
|
" return 2;\n"
|
|
|
|
"}");
|
2011-10-03 19:18:05 +00:00
|
|
|
// 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) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-10-03 19:18:05 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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"
|
|
|
|
"})();");
|
2011-10-03 19:18:05 +00:00
|
|
|
// The inferred name is empty, because this is an assignment of a result.
|
|
|
|
CheckFunctionName(script, "return 1", "");
|
|
|
|
// See MultipleAssignments test.
|
2012-08-07 14:47:36 +00:00
|
|
|
// TODO(2276): Lazy compiling the enclosing outer closure would yield
|
|
|
|
// in "Enclosing.Bar" being the inferred name here.
|
|
|
|
CheckFunctionName(script, "return 2", "Bar");
|
2011-10-03 19:18:05 +00:00
|
|
|
}
|
2012-05-24 12:41:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(MethodAssignmentInAnonymousFunctionCall) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2012-05-24 12:41:55 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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"
|
|
|
|
"})();");
|
2012-05-24 12:41:55 +00:00
|
|
|
CheckFunctionName(script, "return 2012", "EventSource.addListener");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ReturnAnonymousFunction) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2012-05-24 12:41:55 +00:00
|
|
|
|
2015-11-17 12:42:14 +00:00
|
|
|
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();
|
2012-05-24 12:41:55 +00:00
|
|
|
CheckFunctionName(script, "return 2012", "");
|
|
|
|
}
|
2017-12-01 22:35:39 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2017-12-13 21:52:57 +00:00
|
|
|
|
|
|
|
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", "");
|
|
|
|
}
|