| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 | 28 |
| 29 // TODO(jochen): Remove this after the setting is turned on globally. |
| 30 #define V8_IMMINENT_DEPRECATION_WARNINGS |
| 31 |
| 29 #include "src/v8.h" | 32 #include "src/v8.h" |
| 30 | 33 |
| 31 #include "src/api.h" | 34 #include "src/api.h" |
| 32 #include "src/debug/debug.h" | 35 #include "src/debug/debug.h" |
| 33 #include "src/string-search.h" | 36 #include "src/string-search.h" |
| 34 #include "test/cctest/cctest.h" | 37 #include "test/cctest/cctest.h" |
| 35 | 38 |
| 36 | 39 |
| 37 using ::v8::base::SmartArrayPointer; | 40 using ::v8::base::SmartArrayPointer; |
| 38 using ::v8::internal::CStrVector; | 41 using ::v8::internal::CStrVector; |
| 39 using ::v8::internal::Factory; | 42 using ::v8::internal::Factory; |
| 40 using ::v8::internal::Handle; | 43 using ::v8::internal::Handle; |
| 41 using ::v8::internal::Heap; | 44 using ::v8::internal::Heap; |
| 42 using ::v8::internal::Isolate; | 45 using ::v8::internal::Isolate; |
| 43 using ::v8::internal::JSFunction; | 46 using ::v8::internal::JSFunction; |
| 44 using ::v8::internal::Object; | 47 using ::v8::internal::Object; |
| 45 using ::v8::internal::Runtime; | 48 using ::v8::internal::Runtime; |
| 46 using ::v8::internal::Script; | 49 using ::v8::internal::Script; |
| 47 using ::v8::internal::SharedFunctionInfo; | 50 using ::v8::internal::SharedFunctionInfo; |
| 48 using ::v8::internal::String; | 51 using ::v8::internal::String; |
| 49 using ::v8::internal::Vector; | 52 using ::v8::internal::Vector; |
| 50 | 53 |
| 51 | 54 |
| 52 static void CheckFunctionName(v8::Handle<v8::Script> script, | 55 static void CheckFunctionName(v8::Local<v8::Script> script, |
| 53 const char* func_pos_src, | 56 const char* func_pos_src, |
| 54 const char* ref_inferred_name) { | 57 const char* ref_inferred_name) { |
| 55 Isolate* isolate = CcTest::i_isolate(); | 58 Isolate* isolate = CcTest::i_isolate(); |
| 56 | 59 |
| 57 // Get script source. | 60 // Get script source. |
| 58 Handle<Object> obj = v8::Utils::OpenHandle(*script); | 61 Handle<Object> obj = v8::Utils::OpenHandle(*script); |
| 59 Handle<SharedFunctionInfo> shared_function; | 62 Handle<SharedFunctionInfo> shared_function; |
| 60 if (obj->IsSharedFunctionInfo()) { | 63 if (obj->IsSharedFunctionInfo()) { |
| 61 shared_function = | 64 shared_function = |
| 62 Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(*obj)); | 65 Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(*obj)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 86 | 89 |
| 87 // Verify inferred function name. | 90 // Verify inferred function name. |
| 88 SmartArrayPointer<char> inferred_name = | 91 SmartArrayPointer<char> inferred_name = |
| 89 shared_func_info->inferred_name()->ToCString(); | 92 shared_func_info->inferred_name()->ToCString(); |
| 90 i::PrintF("expected: %s, found: %s\n", ref_inferred_name, | 93 i::PrintF("expected: %s, found: %s\n", ref_inferred_name, |
| 91 inferred_name.get()); | 94 inferred_name.get()); |
| 92 CHECK_EQ(0, strcmp(ref_inferred_name, inferred_name.get())); | 95 CHECK_EQ(0, strcmp(ref_inferred_name, inferred_name.get())); |
| 93 } | 96 } |
| 94 | 97 |
| 95 | 98 |
| 96 static v8::Handle<v8::Script> Compile(v8::Isolate* isolate, const char* src) { | 99 static v8::Local<v8::Script> Compile(v8::Isolate* isolate, const char* src) { |
| 97 return v8::Script::Compile(v8::String::NewFromUtf8(isolate, src)); | 100 return v8::Script::Compile( |
| 101 isolate->GetCurrentContext(), |
| 102 v8::String::NewFromUtf8(isolate, src, v8::NewStringType::kNormal) |
| 103 .ToLocalChecked()) |
| 104 .ToLocalChecked(); |
| 98 } | 105 } |
| 99 | 106 |
| 100 | 107 |
| 101 TEST(GlobalProperty) { | 108 TEST(GlobalProperty) { |
| 102 CcTest::InitializeVM(); | 109 CcTest::InitializeVM(); |
| 103 v8::HandleScope scope(CcTest::isolate()); | 110 v8::HandleScope scope(CcTest::isolate()); |
| 104 | 111 |
| 105 v8::Handle<v8::Script> script = Compile( | 112 v8::Local<v8::Script> script = Compile(CcTest::isolate(), |
| 106 CcTest::isolate(), | 113 "fun1 = function() { return 1; }\n" |
| 107 "fun1 = function() { return 1; }\n" | 114 "fun2 = function() { return 2; }\n"); |
| 108 "fun2 = function() { return 2; }\n"); | |
| 109 CheckFunctionName(script, "return 1", "fun1"); | 115 CheckFunctionName(script, "return 1", "fun1"); |
| 110 CheckFunctionName(script, "return 2", "fun2"); | 116 CheckFunctionName(script, "return 2", "fun2"); |
| 111 } | 117 } |
| 112 | 118 |
| 113 | 119 |
| 114 TEST(GlobalVar) { | 120 TEST(GlobalVar) { |
| 115 CcTest::InitializeVM(); | 121 CcTest::InitializeVM(); |
| 116 v8::HandleScope scope(CcTest::isolate()); | 122 v8::HandleScope scope(CcTest::isolate()); |
| 117 | 123 |
| 118 v8::Handle<v8::Script> script = Compile( | 124 v8::Local<v8::Script> script = |
| 119 CcTest::isolate(), | 125 Compile(CcTest::isolate(), |
| 120 "var fun1 = function() { return 1; }\n" | 126 "var fun1 = function() { return 1; }\n" |
| 121 "var fun2 = function() { return 2; }\n"); | 127 "var fun2 = function() { return 2; }\n"); |
| 122 CheckFunctionName(script, "return 1", "fun1"); | 128 CheckFunctionName(script, "return 1", "fun1"); |
| 123 CheckFunctionName(script, "return 2", "fun2"); | 129 CheckFunctionName(script, "return 2", "fun2"); |
| 124 } | 130 } |
| 125 | 131 |
| 126 | 132 |
| 127 TEST(LocalVar) { | 133 TEST(LocalVar) { |
| 128 CcTest::InitializeVM(); | 134 CcTest::InitializeVM(); |
| 129 v8::HandleScope scope(CcTest::isolate()); | 135 v8::HandleScope scope(CcTest::isolate()); |
| 130 | 136 |
| 131 v8::Handle<v8::Script> script = Compile( | 137 v8::Local<v8::Script> script = |
| 132 CcTest::isolate(), | 138 Compile(CcTest::isolate(), |
| 133 "function outer() {\n" | 139 "function outer() {\n" |
| 134 " var fun1 = function() { return 1; }\n" | 140 " var fun1 = function() { return 1; }\n" |
| 135 " var fun2 = function() { return 2; }\n" | 141 " var fun2 = function() { return 2; }\n" |
| 136 "}"); | 142 "}"); |
| 137 CheckFunctionName(script, "return 1", "fun1"); | 143 CheckFunctionName(script, "return 1", "fun1"); |
| 138 CheckFunctionName(script, "return 2", "fun2"); | 144 CheckFunctionName(script, "return 2", "fun2"); |
| 139 } | 145 } |
| 140 | 146 |
| 141 | 147 |
| 142 TEST(InConstructor) { | 148 TEST(InConstructor) { |
| 143 CcTest::InitializeVM(); | 149 CcTest::InitializeVM(); |
| 144 v8::HandleScope scope(CcTest::isolate()); | 150 v8::HandleScope scope(CcTest::isolate()); |
| 145 | 151 |
| 146 v8::Handle<v8::Script> script = Compile( | 152 v8::Local<v8::Script> script = |
| 147 CcTest::isolate(), | 153 Compile(CcTest::isolate(), |
| 148 "function MyClass() {\n" | 154 "function MyClass() {\n" |
| 149 " this.method1 = function() { return 1; }\n" | 155 " this.method1 = function() { return 1; }\n" |
| 150 " this.method2 = function() { return 2; }\n" | 156 " this.method2 = function() { return 2; }\n" |
| 151 "}"); | 157 "}"); |
| 152 CheckFunctionName(script, "return 1", "MyClass.method1"); | 158 CheckFunctionName(script, "return 1", "MyClass.method1"); |
| 153 CheckFunctionName(script, "return 2", "MyClass.method2"); | 159 CheckFunctionName(script, "return 2", "MyClass.method2"); |
| 154 } | 160 } |
| 155 | 161 |
| 156 | 162 |
| 157 TEST(Factory) { | 163 TEST(Factory) { |
| 158 CcTest::InitializeVM(); | 164 CcTest::InitializeVM(); |
| 159 v8::HandleScope scope(CcTest::isolate()); | 165 v8::HandleScope scope(CcTest::isolate()); |
| 160 | 166 |
| 161 v8::Handle<v8::Script> script = Compile( | 167 v8::Local<v8::Script> script = |
| 162 CcTest::isolate(), | 168 Compile(CcTest::isolate(), |
| 163 "function createMyObj() {\n" | 169 "function createMyObj() {\n" |
| 164 " var obj = {};\n" | 170 " var obj = {};\n" |
| 165 " obj.method1 = function() { return 1; }\n" | 171 " obj.method1 = function() { return 1; }\n" |
| 166 " obj.method2 = function() { return 2; }\n" | 172 " obj.method2 = function() { return 2; }\n" |
| 167 " return obj;\n" | 173 " return obj;\n" |
| 168 "}"); | 174 "}"); |
| 169 CheckFunctionName(script, "return 1", "obj.method1"); | 175 CheckFunctionName(script, "return 1", "obj.method1"); |
| 170 CheckFunctionName(script, "return 2", "obj.method2"); | 176 CheckFunctionName(script, "return 2", "obj.method2"); |
| 171 } | 177 } |
| 172 | 178 |
| 173 | 179 |
| 174 TEST(Static) { | 180 TEST(Static) { |
| 175 CcTest::InitializeVM(); | 181 CcTest::InitializeVM(); |
| 176 v8::HandleScope scope(CcTest::isolate()); | 182 v8::HandleScope scope(CcTest::isolate()); |
| 177 | 183 |
| 178 v8::Handle<v8::Script> script = Compile( | 184 v8::Local<v8::Script> script = |
| 179 CcTest::isolate(), | 185 Compile(CcTest::isolate(), |
| 180 "function MyClass() {}\n" | 186 "function MyClass() {}\n" |
| 181 "MyClass.static1 = function() { return 1; }\n" | 187 "MyClass.static1 = function() { return 1; }\n" |
| 182 "MyClass.static2 = function() { return 2; }\n" | 188 "MyClass.static2 = function() { return 2; }\n" |
| 183 "MyClass.MyInnerClass = {}\n" | 189 "MyClass.MyInnerClass = {}\n" |
| 184 "MyClass.MyInnerClass.static3 = function() { return 3; }\n" | 190 "MyClass.MyInnerClass.static3 = function() { return 3; }\n" |
| 185 "MyClass.MyInnerClass.static4 = function() { return 4; }"); | 191 "MyClass.MyInnerClass.static4 = function() { return 4; }"); |
| 186 CheckFunctionName(script, "return 1", "MyClass.static1"); | 192 CheckFunctionName(script, "return 1", "MyClass.static1"); |
| 187 CheckFunctionName(script, "return 2", "MyClass.static2"); | 193 CheckFunctionName(script, "return 2", "MyClass.static2"); |
| 188 CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.static3"); | 194 CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.static3"); |
| 189 CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.static4"); | 195 CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.static4"); |
| 190 } | 196 } |
| 191 | 197 |
| 192 | 198 |
| 193 TEST(Prototype) { | 199 TEST(Prototype) { |
| 194 CcTest::InitializeVM(); | 200 CcTest::InitializeVM(); |
| 195 v8::HandleScope scope(CcTest::isolate()); | 201 v8::HandleScope scope(CcTest::isolate()); |
| 196 | 202 |
| 197 v8::Handle<v8::Script> script = Compile( | 203 v8::Local<v8::Script> script = Compile( |
| 198 CcTest::isolate(), | 204 CcTest::isolate(), |
| 199 "function MyClass() {}\n" | 205 "function MyClass() {}\n" |
| 200 "MyClass.prototype.method1 = function() { return 1; }\n" | 206 "MyClass.prototype.method1 = function() { return 1; }\n" |
| 201 "MyClass.prototype.method2 = function() { return 2; }\n" | 207 "MyClass.prototype.method2 = function() { return 2; }\n" |
| 202 "MyClass.MyInnerClass = function() {}\n" | 208 "MyClass.MyInnerClass = function() {}\n" |
| 203 "MyClass.MyInnerClass.prototype.method3 = function() { return 3; }\n" | 209 "MyClass.MyInnerClass.prototype.method3 = function() { return 3; }\n" |
| 204 "MyClass.MyInnerClass.prototype.method4 = function() { return 4; }"); | 210 "MyClass.MyInnerClass.prototype.method4 = function() { return 4; }"); |
| 205 CheckFunctionName(script, "return 1", "MyClass.method1"); | 211 CheckFunctionName(script, "return 1", "MyClass.method1"); |
| 206 CheckFunctionName(script, "return 2", "MyClass.method2"); | 212 CheckFunctionName(script, "return 2", "MyClass.method2"); |
| 207 CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.method3"); | 213 CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.method3"); |
| 208 CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.method4"); | 214 CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.method4"); |
| 209 } | 215 } |
| 210 | 216 |
| 211 | 217 |
| 212 TEST(ObjectLiteral) { | 218 TEST(ObjectLiteral) { |
| 213 CcTest::InitializeVM(); | 219 CcTest::InitializeVM(); |
| 214 v8::HandleScope scope(CcTest::isolate()); | 220 v8::HandleScope scope(CcTest::isolate()); |
| 215 | 221 |
| 216 v8::Handle<v8::Script> script = Compile( | 222 v8::Local<v8::Script> script = |
| 217 CcTest::isolate(), | 223 Compile(CcTest::isolate(), |
| 218 "function MyClass() {}\n" | 224 "function MyClass() {}\n" |
| 219 "MyClass.prototype = {\n" | 225 "MyClass.prototype = {\n" |
| 220 " method1: function() { return 1; },\n" | 226 " method1: function() { return 1; },\n" |
| 221 " method2: function() { return 2; } }"); | 227 " method2: function() { return 2; } }"); |
| 222 CheckFunctionName(script, "return 1", "MyClass.method1"); | 228 CheckFunctionName(script, "return 1", "MyClass.method1"); |
| 223 CheckFunctionName(script, "return 2", "MyClass.method2"); | 229 CheckFunctionName(script, "return 2", "MyClass.method2"); |
| 224 } | 230 } |
| 225 | 231 |
| 226 | 232 |
| 227 TEST(UpperCaseClass) { | 233 TEST(UpperCaseClass) { |
| 228 CcTest::InitializeVM(); | 234 CcTest::InitializeVM(); |
| 229 v8::HandleScope scope(CcTest::isolate()); | 235 v8::HandleScope scope(CcTest::isolate()); |
| 230 | 236 |
| 231 v8::Handle<v8::Script> script = Compile(CcTest::isolate(), | 237 v8::Local<v8::Script> script = Compile(CcTest::isolate(), |
| 232 "'use strict';\n" | 238 "'use strict';\n" |
| 233 "class MyClass {\n" | 239 "class MyClass {\n" |
| 234 " constructor() {\n" | 240 " constructor() {\n" |
| 235 " this.value = 1;\n" | 241 " this.value = 1;\n" |
| 236 " }\n" | 242 " }\n" |
| 237 " method() {\n" | 243 " method() {\n" |
| 238 " this.value = 2;\n" | 244 " this.value = 2;\n" |
| 239 " }\n" | 245 " }\n" |
| 240 "}"); | 246 "}"); |
| 241 CheckFunctionName(script, "this.value = 1", "MyClass"); | 247 CheckFunctionName(script, "this.value = 1", "MyClass"); |
| 242 CheckFunctionName(script, "this.value = 2", "MyClass.method"); | 248 CheckFunctionName(script, "this.value = 2", "MyClass.method"); |
| 243 } | 249 } |
| 244 | 250 |
| 245 | 251 |
| 246 TEST(LowerCaseClass) { | 252 TEST(LowerCaseClass) { |
| 247 CcTest::InitializeVM(); | 253 CcTest::InitializeVM(); |
| 248 v8::HandleScope scope(CcTest::isolate()); | 254 v8::HandleScope scope(CcTest::isolate()); |
| 249 | 255 |
| 250 v8::Handle<v8::Script> script = Compile(CcTest::isolate(), | 256 v8::Local<v8::Script> script = Compile(CcTest::isolate(), |
| 251 "'use strict';\n" | 257 "'use strict';\n" |
| 252 "class myclass {\n" | 258 "class myclass {\n" |
| 253 " constructor() {\n" | 259 " constructor() {\n" |
| 254 " this.value = 1;\n" | 260 " this.value = 1;\n" |
| 255 " }\n" | 261 " }\n" |
| 256 " method() {\n" | 262 " method() {\n" |
| 257 " this.value = 2;\n" | 263 " this.value = 2;\n" |
| 258 " }\n" | 264 " }\n" |
| 259 "}"); | 265 "}"); |
| 260 CheckFunctionName(script, "this.value = 1", "myclass"); | 266 CheckFunctionName(script, "this.value = 1", "myclass"); |
| 261 CheckFunctionName(script, "this.value = 2", "myclass.method"); | 267 CheckFunctionName(script, "this.value = 2", "myclass.method"); |
| 262 } | 268 } |
| 263 | 269 |
| 264 | 270 |
| 265 TEST(AsParameter) { | 271 TEST(AsParameter) { |
| 266 CcTest::InitializeVM(); | 272 CcTest::InitializeVM(); |
| 267 v8::HandleScope scope(CcTest::isolate()); | 273 v8::HandleScope scope(CcTest::isolate()); |
| 268 | 274 |
| 269 v8::Handle<v8::Script> script = Compile( | 275 v8::Local<v8::Script> script = Compile( |
| 270 CcTest::isolate(), | 276 CcTest::isolate(), |
| 271 "function f1(a) { return a(); }\n" | 277 "function f1(a) { return a(); }\n" |
| 272 "function f2(a, b) { return a() + b(); }\n" | 278 "function f2(a, b) { return a() + b(); }\n" |
| 273 "var result1 = f1(function() { return 1; })\n" | 279 "var result1 = f1(function() { return 1; })\n" |
| 274 "var result2 = f2(function() { return 2; }, function() { return 3; })"); | 280 "var result2 = f2(function() { return 2; }, function() { return 3; })"); |
| 275 // Can't infer names here. | 281 // Can't infer names here. |
| 276 CheckFunctionName(script, "return 1", ""); | 282 CheckFunctionName(script, "return 1", ""); |
| 277 CheckFunctionName(script, "return 2", ""); | 283 CheckFunctionName(script, "return 2", ""); |
| 278 CheckFunctionName(script, "return 3", ""); | 284 CheckFunctionName(script, "return 3", ""); |
| 279 } | 285 } |
| 280 | 286 |
| 281 | 287 |
| 282 TEST(MultipleFuncsConditional) { | 288 TEST(MultipleFuncsConditional) { |
| 283 CcTest::InitializeVM(); | 289 CcTest::InitializeVM(); |
| 284 v8::HandleScope scope(CcTest::isolate()); | 290 v8::HandleScope scope(CcTest::isolate()); |
| 285 | 291 |
| 286 v8::Handle<v8::Script> script = Compile( | 292 v8::Local<v8::Script> script = Compile(CcTest::isolate(), |
| 287 CcTest::isolate(), | 293 "fun1 = 0 ?\n" |
| 288 "fun1 = 0 ?\n" | 294 " function() { return 1; } :\n" |
| 289 " function() { return 1; } :\n" | 295 " function() { return 2; }"); |
| 290 " function() { return 2; }"); | |
| 291 CheckFunctionName(script, "return 1", "fun1"); | 296 CheckFunctionName(script, "return 1", "fun1"); |
| 292 CheckFunctionName(script, "return 2", "fun1"); | 297 CheckFunctionName(script, "return 2", "fun1"); |
| 293 } | 298 } |
| 294 | 299 |
| 295 | 300 |
| 296 TEST(MultipleFuncsInLiteral) { | 301 TEST(MultipleFuncsInLiteral) { |
| 297 CcTest::InitializeVM(); | 302 CcTest::InitializeVM(); |
| 298 v8::HandleScope scope(CcTest::isolate()); | 303 v8::HandleScope scope(CcTest::isolate()); |
| 299 | 304 |
| 300 v8::Handle<v8::Script> script = Compile( | 305 v8::Local<v8::Script> script = |
| 301 CcTest::isolate(), | 306 Compile(CcTest::isolate(), |
| 302 "function MyClass() {}\n" | 307 "function MyClass() {}\n" |
| 303 "MyClass.prototype = {\n" | 308 "MyClass.prototype = {\n" |
| 304 " method1: 0 ? function() { return 1; } :\n" | 309 " method1: 0 ? function() { return 1; } :\n" |
| 305 " function() { return 2; } }"); | 310 " function() { return 2; } }"); |
| 306 CheckFunctionName(script, "return 1", "MyClass.method1"); | 311 CheckFunctionName(script, "return 1", "MyClass.method1"); |
| 307 CheckFunctionName(script, "return 2", "MyClass.method1"); | 312 CheckFunctionName(script, "return 2", "MyClass.method1"); |
| 308 } | 313 } |
| 309 | 314 |
| 310 | 315 |
| 311 TEST(AnonymousInAnonymousClosure1) { | 316 TEST(AnonymousInAnonymousClosure1) { |
| 312 CcTest::InitializeVM(); | 317 CcTest::InitializeVM(); |
| 313 v8::HandleScope scope(CcTest::isolate()); | 318 v8::HandleScope scope(CcTest::isolate()); |
| 314 | 319 |
| 315 v8::Handle<v8::Script> script = Compile( | 320 v8::Local<v8::Script> script = Compile(CcTest::isolate(), |
| 316 CcTest::isolate(), | 321 "(function() {\n" |
| 317 "(function() {\n" | 322 " (function() {\n" |
| 318 " (function() {\n" | 323 " var a = 1;\n" |
| 319 " var a = 1;\n" | 324 " return;\n" |
| 320 " return;\n" | 325 " })();\n" |
| 321 " })();\n" | 326 " var b = function() {\n" |
| 322 " var b = function() {\n" | 327 " var c = 1;\n" |
| 323 " var c = 1;\n" | 328 " return;\n" |
| 324 " return;\n" | 329 " };\n" |
| 325 " };\n" | 330 "})();"); |
| 326 "})();"); | |
| 327 CheckFunctionName(script, "return", ""); | 331 CheckFunctionName(script, "return", ""); |
| 328 } | 332 } |
| 329 | 333 |
| 330 | 334 |
| 331 TEST(AnonymousInAnonymousClosure2) { | 335 TEST(AnonymousInAnonymousClosure2) { |
| 332 CcTest::InitializeVM(); | 336 CcTest::InitializeVM(); |
| 333 v8::HandleScope scope(CcTest::isolate()); | 337 v8::HandleScope scope(CcTest::isolate()); |
| 334 | 338 |
| 335 v8::Handle<v8::Script> script = Compile( | 339 v8::Local<v8::Script> script = Compile(CcTest::isolate(), |
| 336 CcTest::isolate(), | 340 "(function() {\n" |
| 337 "(function() {\n" | 341 " (function() {\n" |
| 338 " (function() {\n" | 342 " var a = 1;\n" |
| 339 " var a = 1;\n" | 343 " return;\n" |
| 340 " return;\n" | 344 " })();\n" |
| 341 " })();\n" | 345 " var c = 1;\n" |
| 342 " var c = 1;\n" | 346 "})();"); |
| 343 "})();"); | |
| 344 CheckFunctionName(script, "return", ""); | 347 CheckFunctionName(script, "return", ""); |
| 345 } | 348 } |
| 346 | 349 |
| 347 | 350 |
| 348 TEST(NamedInAnonymousClosure) { | 351 TEST(NamedInAnonymousClosure) { |
| 349 CcTest::InitializeVM(); | 352 CcTest::InitializeVM(); |
| 350 v8::HandleScope scope(CcTest::isolate()); | 353 v8::HandleScope scope(CcTest::isolate()); |
| 351 | 354 |
| 352 v8::Handle<v8::Script> script = Compile( | 355 v8::Local<v8::Script> script = Compile(CcTest::isolate(), |
| 353 CcTest::isolate(), | 356 "var foo = function() {\n" |
| 354 "var foo = function() {\n" | 357 " (function named() {\n" |
| 355 " (function named() {\n" | 358 " var a = 1;\n" |
| 356 " var a = 1;\n" | 359 " })();\n" |
| 357 " })();\n" | 360 " var c = 1;\n" |
| 358 " var c = 1;\n" | 361 " return;\n" |
| 359 " return;\n" | 362 "};"); |
| 360 "};"); | |
| 361 CheckFunctionName(script, "return", "foo"); | 363 CheckFunctionName(script, "return", "foo"); |
| 362 } | 364 } |
| 363 | 365 |
| 364 | 366 |
| 365 // See http://code.google.com/p/v8/issues/detail?id=380 | 367 // See http://code.google.com/p/v8/issues/detail?id=380 |
| 366 TEST(Issue380) { | 368 TEST(Issue380) { |
| 367 CcTest::InitializeVM(); | 369 CcTest::InitializeVM(); |
| 368 v8::HandleScope scope(CcTest::isolate()); | 370 v8::HandleScope scope(CcTest::isolate()); |
| 369 | 371 |
| 370 v8::Handle<v8::Script> script = Compile( | 372 v8::Local<v8::Script> script = |
| 371 CcTest::isolate(), | 373 Compile(CcTest::isolate(), |
| 372 "function a() {\n" | 374 "function a() {\n" |
| 373 "var result = function(p,a,c,k,e,d)" | 375 "var result = function(p,a,c,k,e,d)" |
| 374 "{return p}(\"if blah blah\",62,1976,\'a|b\'.split(\'|\'),0,{})\n" | 376 "{return p}(\"if blah blah\",62,1976,\'a|b\'.split(\'|\'),0,{})\n" |
| 375 "}"); | 377 "}"); |
| 376 CheckFunctionName(script, "return p", ""); | 378 CheckFunctionName(script, "return p", ""); |
| 377 } | 379 } |
| 378 | 380 |
| 379 | 381 |
| 380 TEST(MultipleAssignments) { | 382 TEST(MultipleAssignments) { |
| 381 CcTest::InitializeVM(); | 383 CcTest::InitializeVM(); |
| 382 v8::HandleScope scope(CcTest::isolate()); | 384 v8::HandleScope scope(CcTest::isolate()); |
| 383 | 385 |
| 384 v8::Handle<v8::Script> script = Compile( | 386 v8::Local<v8::Script> script = |
| 385 CcTest::isolate(), | 387 Compile(CcTest::isolate(), |
| 386 "var fun1 = fun2 = function () { return 1; }\n" | 388 "var fun1 = fun2 = function () { return 1; }\n" |
| 387 "var bar1 = bar2 = bar3 = function () { return 2; }\n" | 389 "var bar1 = bar2 = bar3 = function () { return 2; }\n" |
| 388 "foo1 = foo2 = function () { return 3; }\n" | 390 "foo1 = foo2 = function () { return 3; }\n" |
| 389 "baz1 = baz2 = baz3 = function () { return 4; }"); | 391 "baz1 = baz2 = baz3 = function () { return 4; }"); |
| 390 CheckFunctionName(script, "return 1", "fun2"); | 392 CheckFunctionName(script, "return 1", "fun2"); |
| 391 CheckFunctionName(script, "return 2", "bar3"); | 393 CheckFunctionName(script, "return 2", "bar3"); |
| 392 CheckFunctionName(script, "return 3", "foo2"); | 394 CheckFunctionName(script, "return 3", "foo2"); |
| 393 CheckFunctionName(script, "return 4", "baz3"); | 395 CheckFunctionName(script, "return 4", "baz3"); |
| 394 } | 396 } |
| 395 | 397 |
| 396 | 398 |
| 397 TEST(AsConstructorParameter) { | 399 TEST(AsConstructorParameter) { |
| 398 CcTest::InitializeVM(); | 400 CcTest::InitializeVM(); |
| 399 v8::HandleScope scope(CcTest::isolate()); | 401 v8::HandleScope scope(CcTest::isolate()); |
| 400 | 402 |
| 401 v8::Handle<v8::Script> script = Compile( | 403 v8::Local<v8::Script> script = Compile( |
| 402 CcTest::isolate(), | 404 CcTest::isolate(), |
| 403 "function Foo() {}\n" | 405 "function Foo() {}\n" |
| 404 "var foo = new Foo(function() { return 1; })\n" | 406 "var foo = new Foo(function() { return 1; })\n" |
| 405 "var bar = new Foo(function() { return 2; }, function() { return 3; })"); | 407 "var bar = new Foo(function() { return 2; }, function() { return 3; })"); |
| 406 CheckFunctionName(script, "return 1", ""); | 408 CheckFunctionName(script, "return 1", ""); |
| 407 CheckFunctionName(script, "return 2", ""); | 409 CheckFunctionName(script, "return 2", ""); |
| 408 CheckFunctionName(script, "return 3", ""); | 410 CheckFunctionName(script, "return 3", ""); |
| 409 } | 411 } |
| 410 | 412 |
| 411 | 413 |
| 412 TEST(FactoryHashmap) { | 414 TEST(FactoryHashmap) { |
| 413 CcTest::InitializeVM(); | 415 CcTest::InitializeVM(); |
| 414 v8::HandleScope scope(CcTest::isolate()); | 416 v8::HandleScope scope(CcTest::isolate()); |
| 415 | 417 |
| 416 v8::Handle<v8::Script> script = Compile( | 418 v8::Local<v8::Script> script = |
| 417 CcTest::isolate(), | 419 Compile(CcTest::isolate(), |
| 418 "function createMyObj() {\n" | 420 "function createMyObj() {\n" |
| 419 " var obj = {};\n" | 421 " var obj = {};\n" |
| 420 " obj[\"method1\"] = function() { return 1; }\n" | 422 " obj[\"method1\"] = function() { return 1; }\n" |
| 421 " obj[\"method2\"] = function() { return 2; }\n" | 423 " obj[\"method2\"] = function() { return 2; }\n" |
| 422 " return obj;\n" | 424 " return obj;\n" |
| 423 "}"); | 425 "}"); |
| 424 CheckFunctionName(script, "return 1", "obj.method1"); | 426 CheckFunctionName(script, "return 1", "obj.method1"); |
| 425 CheckFunctionName(script, "return 2", "obj.method2"); | 427 CheckFunctionName(script, "return 2", "obj.method2"); |
| 426 } | 428 } |
| 427 | 429 |
| 428 | 430 |
| 429 TEST(FactoryHashmapVariable) { | 431 TEST(FactoryHashmapVariable) { |
| 430 CcTest::InitializeVM(); | 432 CcTest::InitializeVM(); |
| 431 v8::HandleScope scope(CcTest::isolate()); | 433 v8::HandleScope scope(CcTest::isolate()); |
| 432 | 434 |
| 433 v8::Handle<v8::Script> script = Compile( | 435 v8::Local<v8::Script> script = |
| 434 CcTest::isolate(), | 436 Compile(CcTest::isolate(), |
| 435 "function createMyObj() {\n" | 437 "function createMyObj() {\n" |
| 436 " var obj = {};\n" | 438 " var obj = {};\n" |
| 437 " var methodName = \"method1\";\n" | 439 " var methodName = \"method1\";\n" |
| 438 " obj[methodName] = function() { return 1; }\n" | 440 " obj[methodName] = function() { return 1; }\n" |
| 439 " methodName = \"method2\";\n" | 441 " methodName = \"method2\";\n" |
| 440 " obj[methodName] = function() { return 2; }\n" | 442 " obj[methodName] = function() { return 2; }\n" |
| 441 " return obj;\n" | 443 " return obj;\n" |
| 442 "}"); | 444 "}"); |
| 443 // Can't infer function names statically. | 445 // Can't infer function names statically. |
| 444 CheckFunctionName(script, "return 1", "obj.(anonymous function)"); | 446 CheckFunctionName(script, "return 1", "obj.(anonymous function)"); |
| 445 CheckFunctionName(script, "return 2", "obj.(anonymous function)"); | 447 CheckFunctionName(script, "return 2", "obj.(anonymous function)"); |
| 446 } | 448 } |
| 447 | 449 |
| 448 | 450 |
| 449 TEST(FactoryHashmapConditional) { | 451 TEST(FactoryHashmapConditional) { |
| 450 CcTest::InitializeVM(); | 452 CcTest::InitializeVM(); |
| 451 v8::HandleScope scope(CcTest::isolate()); | 453 v8::HandleScope scope(CcTest::isolate()); |
| 452 | 454 |
| 453 v8::Handle<v8::Script> script = Compile( | 455 v8::Local<v8::Script> script = Compile( |
| 454 CcTest::isolate(), | 456 CcTest::isolate(), |
| 455 "function createMyObj() {\n" | 457 "function createMyObj() {\n" |
| 456 " var obj = {};\n" | 458 " var obj = {};\n" |
| 457 " obj[0 ? \"method1\" : \"method2\"] = function() { return 1; }\n" | 459 " obj[0 ? \"method1\" : \"method2\"] = function() { return 1; }\n" |
| 458 " return obj;\n" | 460 " return obj;\n" |
| 459 "}"); | 461 "}"); |
| 460 // Can't infer the function name statically. | 462 // Can't infer the function name statically. |
| 461 CheckFunctionName(script, "return 1", "obj.(anonymous function)"); | 463 CheckFunctionName(script, "return 1", "obj.(anonymous function)"); |
| 462 } | 464 } |
| 463 | 465 |
| 464 | 466 |
| 465 TEST(GlobalAssignmentAndCall) { | 467 TEST(GlobalAssignmentAndCall) { |
| 466 CcTest::InitializeVM(); | 468 CcTest::InitializeVM(); |
| 467 v8::HandleScope scope(CcTest::isolate()); | 469 v8::HandleScope scope(CcTest::isolate()); |
| 468 | 470 |
| 469 v8::Handle<v8::Script> script = Compile( | 471 v8::Local<v8::Script> script = Compile(CcTest::isolate(), |
| 470 CcTest::isolate(), | 472 "var Foo = function() {\n" |
| 471 "var Foo = function() {\n" | 473 " return 1;\n" |
| 472 " return 1;\n" | 474 "}();\n" |
| 473 "}();\n" | 475 "var Baz = Bar = function() {\n" |
| 474 "var Baz = Bar = function() {\n" | 476 " return 2;\n" |
| 475 " return 2;\n" | 477 "}"); |
| 476 "}"); | |
| 477 // The inferred name is empty, because this is an assignment of a result. | 478 // The inferred name is empty, because this is an assignment of a result. |
| 478 CheckFunctionName(script, "return 1", ""); | 479 CheckFunctionName(script, "return 1", ""); |
| 479 // See MultipleAssignments test. | 480 // See MultipleAssignments test. |
| 480 CheckFunctionName(script, "return 2", "Bar"); | 481 CheckFunctionName(script, "return 2", "Bar"); |
| 481 } | 482 } |
| 482 | 483 |
| 483 | 484 |
| 484 TEST(AssignmentAndCall) { | 485 TEST(AssignmentAndCall) { |
| 485 CcTest::InitializeVM(); | 486 CcTest::InitializeVM(); |
| 486 v8::HandleScope scope(CcTest::isolate()); | 487 v8::HandleScope scope(CcTest::isolate()); |
| 487 | 488 |
| 488 v8::Handle<v8::Script> script = Compile( | 489 v8::Local<v8::Script> script = Compile(CcTest::isolate(), |
| 489 CcTest::isolate(), | 490 "(function Enclosing() {\n" |
| 490 "(function Enclosing() {\n" | 491 " var Foo;\n" |
| 491 " var Foo;\n" | 492 " Foo = function() {\n" |
| 492 " Foo = function() {\n" | 493 " return 1;\n" |
| 493 " return 1;\n" | 494 " }();\n" |
| 494 " }();\n" | 495 " var Baz = Bar = function() {\n" |
| 495 " var Baz = Bar = function() {\n" | 496 " return 2;\n" |
| 496 " return 2;\n" | 497 " }\n" |
| 497 " }\n" | 498 "})();"); |
| 498 "})();"); | |
| 499 // The inferred name is empty, because this is an assignment of a result. | 499 // The inferred name is empty, because this is an assignment of a result. |
| 500 CheckFunctionName(script, "return 1", ""); | 500 CheckFunctionName(script, "return 1", ""); |
| 501 // See MultipleAssignments test. | 501 // See MultipleAssignments test. |
| 502 // TODO(2276): Lazy compiling the enclosing outer closure would yield | 502 // TODO(2276): Lazy compiling the enclosing outer closure would yield |
| 503 // in "Enclosing.Bar" being the inferred name here. | 503 // in "Enclosing.Bar" being the inferred name here. |
| 504 CheckFunctionName(script, "return 2", "Bar"); | 504 CheckFunctionName(script, "return 2", "Bar"); |
| 505 } | 505 } |
| 506 | 506 |
| 507 | 507 |
| 508 TEST(MethodAssignmentInAnonymousFunctionCall) { | 508 TEST(MethodAssignmentInAnonymousFunctionCall) { |
| 509 CcTest::InitializeVM(); | 509 CcTest::InitializeVM(); |
| 510 v8::HandleScope scope(CcTest::isolate()); | 510 v8::HandleScope scope(CcTest::isolate()); |
| 511 | 511 |
| 512 v8::Handle<v8::Script> script = Compile( | 512 v8::Local<v8::Script> script = |
| 513 CcTest::isolate(), | 513 Compile(CcTest::isolate(), |
| 514 "(function () {\n" | 514 "(function () {\n" |
| 515 " var EventSource = function () { };\n" | 515 " var EventSource = function () { };\n" |
| 516 " EventSource.prototype.addListener = function () {\n" | 516 " EventSource.prototype.addListener = function () {\n" |
| 517 " return 2012;\n" | 517 " return 2012;\n" |
| 518 " };\n" | 518 " };\n" |
| 519 " this.PublicEventSource = EventSource;\n" | 519 " this.PublicEventSource = EventSource;\n" |
| 520 "})();"); | 520 "})();"); |
| 521 CheckFunctionName(script, "return 2012", "EventSource.addListener"); | 521 CheckFunctionName(script, "return 2012", "EventSource.addListener"); |
| 522 } | 522 } |
| 523 | 523 |
| 524 | 524 |
| 525 TEST(ReturnAnonymousFunction) { | 525 TEST(ReturnAnonymousFunction) { |
| 526 CcTest::InitializeVM(); | 526 CcTest::InitializeVM(); |
| 527 v8::HandleScope scope(CcTest::isolate()); | 527 v8::HandleScope scope(CcTest::isolate()); |
| 528 | 528 |
| 529 v8::Handle<v8::Script> script = Compile( | 529 v8::Local<v8::Script> script = Compile(CcTest::isolate(), |
| 530 CcTest::isolate(), | 530 "(function() {\n" |
| 531 "(function() {\n" | 531 " function wrapCode() {\n" |
| 532 " function wrapCode() {\n" | 532 " return function () {\n" |
| 533 " return function () {\n" | 533 " return 2012;\n" |
| 534 " return 2012;\n" | 534 " };\n" |
| 535 " };\n" | 535 " };\n" |
| 536 " };\n" | 536 " var foo = 10;\n" |
| 537 " var foo = 10;\n" | 537 " function f() {\n" |
| 538 " function f() {\n" | 538 " return wrapCode();\n" |
| 539 " return wrapCode();\n" | 539 " }\n" |
| 540 " }\n" | 540 " this.ref = f;\n" |
| 541 " this.ref = f;\n" | 541 "})()"); |
| 542 "})()"); | 542 script->Run(CcTest::isolate()->GetCurrentContext()).ToLocalChecked(); |
| 543 script->Run(); | |
| 544 CheckFunctionName(script, "return 2012", ""); | 543 CheckFunctionName(script, "return 2012", ""); |
| 545 } | 544 } |
| OLD | NEW |