Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 28 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 29 | #include "src/v8.h" |
| 30 | |
| 31 | #include "src/api.h" |
| 32 | #include "src/debug.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 33 | #include "src/string-search.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | #include "test/cctest/cctest.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | using ::v8::internal::CStrVector; |
| 38 | using ::v8::internal::Factory; |
| 39 | using ::v8::internal::Handle; |
| 40 | using ::v8::internal::Heap; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 41 | using ::v8::internal::Isolate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | using ::v8::internal::JSFunction; |
| 43 | using ::v8::internal::Object; |
| 44 | using ::v8::internal::Runtime; |
| 45 | using ::v8::internal::Script; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 46 | using ::v8::internal::SmartArrayPointer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 47 | using ::v8::internal::SharedFunctionInfo; |
| 48 | using ::v8::internal::String; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 49 | using ::v8::internal::Vector; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 50 | |
| 51 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | static void CheckFunctionName(v8::Handle<v8::Script> script, |
| 53 | const char* func_pos_src, |
| 54 | const char* ref_inferred_name) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 55 | Isolate* isolate = CcTest::i_isolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 56 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | // Get script source. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 58 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 68 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 72 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 80 | CHECK_NE(0, func_pos); |
| 81 | |
| 82 | // Obtain SharedFunctionInfo for the function. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 83 | isolate->debug()->PrepareForBreakPoints(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 84 | Object* shared_func_info_ptr = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 85 | isolate->debug()->FindSharedFunctionInfoInScript(i_script, func_pos); |
| 86 | CHECK(shared_func_info_ptr != CcTest::heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 87 | Handle<SharedFunctionInfo> shared_func_info( |
| 88 | SharedFunctionInfo::cast(shared_func_info_ptr)); |
| 89 | |
| 90 | // Verify inferred function name. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 91 | SmartArrayPointer<char> inferred_name = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | shared_func_info->inferred_name()->ToCString(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 93 | CHECK_EQ(ref_inferred_name, inferred_name.get()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | static v8::Handle<v8::Script> Compile(v8::Isolate* isolate, const char* src) { |
| 98 | return v8::Script::Compile(v8::String::NewFromUtf8(isolate, src)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | TEST(GlobalProperty) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | CcTest::InitializeVM(); |
| 104 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | |
| 106 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 107 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | "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 | |
| 115 | TEST(GlobalVar) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 116 | CcTest::InitializeVM(); |
| 117 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 118 | |
| 119 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 120 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | "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 | |
| 128 | TEST(LocalVar) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 129 | CcTest::InitializeVM(); |
| 130 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 131 | |
| 132 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 133 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 134 | "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 | |
| 143 | TEST(InConstructor) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 144 | CcTest::InitializeVM(); |
| 145 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | |
| 147 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 148 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 149 | "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 | |
| 158 | TEST(Factory) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 159 | CcTest::InitializeVM(); |
| 160 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | |
| 162 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 163 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | "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 | |
| 175 | TEST(Static) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | CcTest::InitializeVM(); |
| 177 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | |
| 179 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 180 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 181 | "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 | |
| 194 | TEST(Prototype) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 195 | CcTest::InitializeVM(); |
| 196 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 197 | |
| 198 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 199 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | "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 | |
| 213 | TEST(ObjectLiteral) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 214 | CcTest::InitializeVM(); |
| 215 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | |
| 217 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 218 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 219 | "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 | |
| 228 | TEST(AsParameter) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 229 | CcTest::InitializeVM(); |
| 230 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 231 | |
| 232 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 233 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | "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 | |
| 245 | TEST(MultipleFuncsConditional) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 246 | CcTest::InitializeVM(); |
| 247 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 248 | |
| 249 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 250 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 251 | "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 | |
| 259 | TEST(MultipleFuncsInLiteral) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 260 | CcTest::InitializeVM(); |
| 261 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 262 | |
| 263 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 264 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | "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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 274 | TEST(AnonymousInAnonymousClosure1) { |
| 275 | CcTest::InitializeVM(); |
| 276 | v8::HandleScope scope(CcTest::isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 277 | |
| 278 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 279 | 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 | |
| 294 | TEST(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 | |
| 311 | TEST(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 |
| 329 | TEST(Issue380) { |
| 330 | CcTest::InitializeVM(); |
| 331 | v8::HandleScope scope(CcTest::isolate()); |
| 332 | |
| 333 | v8::Handle<v8::Script> script = Compile( |
| 334 | CcTest::isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | "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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 341 | |
| 342 | |
| 343 | TEST(MultipleAssignments) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 344 | CcTest::InitializeVM(); |
| 345 | v8::HandleScope scope(CcTest::isolate()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 346 | |
| 347 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 348 | CcTest::isolate(), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 349 | "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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 353 | CheckFunctionName(script, "return 1", "fun2"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 354 | CheckFunctionName(script, "return 2", "bar3"); |
| 355 | CheckFunctionName(script, "return 3", "foo2"); |
| 356 | CheckFunctionName(script, "return 4", "baz3"); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 360 | TEST(AsConstructorParameter) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 361 | CcTest::InitializeVM(); |
| 362 | v8::HandleScope scope(CcTest::isolate()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 363 | |
| 364 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 365 | CcTest::isolate(), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 366 | "function Foo() {}\n" |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 367 | "var foo = new Foo(function() { return 1; })\n" |
| 368 | "var bar = new Foo(function() { return 2; }, function() { return 3; })"); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 369 | CheckFunctionName(script, "return 1", ""); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 370 | CheckFunctionName(script, "return 2", ""); |
| 371 | CheckFunctionName(script, "return 3", ""); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
| 375 | TEST(FactoryHashmap) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 376 | CcTest::InitializeVM(); |
| 377 | v8::HandleScope scope(CcTest::isolate()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 378 | |
| 379 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 380 | CcTest::isolate(), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 381 | "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 | |
| 392 | TEST(FactoryHashmapVariable) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 393 | CcTest::InitializeVM(); |
| 394 | v8::HandleScope scope(CcTest::isolate()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 395 | |
| 396 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 397 | CcTest::isolate(), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 398 | "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 | |
| 412 | TEST(FactoryHashmapConditional) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 413 | CcTest::InitializeVM(); |
| 414 | v8::HandleScope scope(CcTest::isolate()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 415 | |
| 416 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 417 | CcTest::isolate(), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 418 | "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 Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 426 | |
| 427 | |
| 428 | TEST(GlobalAssignmentAndCall) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 429 | CcTest::InitializeVM(); |
| 430 | v8::HandleScope scope(CcTest::isolate()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 431 | |
| 432 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 433 | CcTest::isolate(), |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 434 | "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 | |
| 447 | TEST(AssignmentAndCall) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 448 | CcTest::InitializeVM(); |
| 449 | v8::HandleScope scope(CcTest::isolate()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 450 | |
| 451 | v8::Handle<v8::Script> script = Compile( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 452 | CcTest::isolate(), |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 453 | "(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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 465 | // 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 | |
| 471 | TEST(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 | |
| 488 | TEST(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 Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 508 | } |