| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library dart2js.js_emitter.program_builder; | 5 library dart2js.js_emitter.program_builder; |
| 6 | 6 |
| 7 import '../../closure.dart' show ClosureFieldElement; | 7 import '../../closure.dart' show ClosureFieldElement; |
| 8 import '../../common.dart'; | 8 import '../../common.dart'; |
| 9 import '../../common/names.dart' show Names, Selectors; | 9 import '../../common/names.dart' show Names, Selectors; |
| 10 import '../../compiler.dart' show Compiler; | 10 import '../../compiler.dart' show Compiler; |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 List<Library> _buildLibraries(LibrariesMap librariesMap) { | 316 List<Library> _buildLibraries(LibrariesMap librariesMap) { |
| 317 List<Library> libraries = new List<Library>(librariesMap.length); | 317 List<Library> libraries = new List<Library>(librariesMap.length); |
| 318 int count = 0; | 318 int count = 0; |
| 319 librariesMap.forEach((LibraryElement library, List<Element> elements) { | 319 librariesMap.forEach((LibraryElement library, List<Element> elements) { |
| 320 libraries[count++] = _buildLibrary(library, elements); | 320 libraries[count++] = _buildLibrary(library, elements); |
| 321 }); | 321 }); |
| 322 return libraries; | 322 return libraries; |
| 323 } | 323 } |
| 324 | 324 |
| 325 void _addJsInteropStubs(LibrariesMap librariesMap) { | 325 void _addJsInteropStubs(LibrariesMap librariesMap) { |
| 326 // TODO(26456): we should be using codegenWorld instead of resolverWorld |
| 327 // to determine what invocations could be live but codegenWorld has a bug |
| 328 // resulting in it missing some invocations. |
| 326 if (_classes.containsKey(_compiler.coreClasses.objectClass)) { | 329 if (_classes.containsKey(_compiler.coreClasses.objectClass)) { |
| 327 var toStringInvocation = namer.invocationName(Selectors.toString_); | 330 var toStringInvocation = namer.invocationName(Selectors.toString_); |
| 328 // TODO(jacobr): register toString as used so that it is always accessible | 331 // TODO(jacobr): register toString as used so that it is always accessible |
| 329 // from JavaScript. | 332 // from JavaScript. |
| 330 _classes[_compiler.coreClasses.objectClass].callStubs.add( | 333 _classes[_compiler.coreClasses.objectClass].callStubs.add( |
| 331 _buildStubMethod(new StringBackedName("toString"), | 334 _buildStubMethod(new StringBackedName("toString"), |
| 332 js.js('function() { return this.#(this) }', toStringInvocation))); | 335 js.js('function() { return this.#(this) }', toStringInvocation))); |
| 333 } | 336 } |
| 334 | 337 |
| 335 // We add all members from classes marked with isJsInterop to the base | 338 // We add all members from classes marked with isJsInterop to the base |
| 336 // Interceptor class with implementations that directly call the | 339 // Interceptor class with implementations that directly call the |
| 337 // corresponding JavaScript member. We do not attempt to bind this when | 340 // corresponding JavaScript member. We do not attempt to bind this when |
| 338 // tearing off JavaScript methods as we cannot distinguish between calling | 341 // tearing off JavaScript methods as we cannot distinguish between calling |
| 339 // a regular getter that returns a JavaScript function and tearing off | 342 // a regular getter that returns a JavaScript function and tearing off |
| 340 // a method in the case where there exist multiple JavaScript classes | 343 // a method in the case where there exist multiple JavaScript classes |
| 341 // that conflict on whether the member is a getter or a method. | 344 // that conflict on whether the member is a getter or a method. |
| 342 var interceptorClass = _classes[helpers.jsJavaScriptObjectClass]; | 345 var interceptorClass = _classes[helpers.jsJavaScriptObjectClass]; |
| 343 var stubNames = new Set<String>(); | 346 var stubNames = new Set<String>(); |
| 344 librariesMap.forEach((LibraryElement library, List<Element> elements) { | 347 librariesMap.forEach((LibraryElement library, List<Element> elements) { |
| 345 for (Element e in elements) { | 348 for (Element e in elements) { |
| 346 if (e is ClassElement && backend.isJsInterop(e)) { | 349 if (e is ClassElement && backend.isJsInterop(e)) { |
| 347 e.declaration.forEachMember((_, Element member) { | 350 e.declaration.forEachMember((_, Element member) { |
| 348 if (!member.isInstanceMember) return; | 351 if (!member.isInstanceMember) return; |
| 349 if (member.isGetter || member.isField || member.isFunction) { | 352 if (member.isGetter || member.isField || member.isFunction) { |
| 350 var selectors = | 353 var selectors = |
| 351 _compiler.codegenWorld.getterInvocationsByName(member.name); | 354 _compiler.resolverWorld.getterInvocationsByName(member.name); |
| 352 if (selectors != null && !selectors.isEmpty) { | 355 if (selectors != null && !selectors.isEmpty) { |
| 353 for (var selector in selectors.keys) { | 356 for (var selector in selectors.keys) { |
| 354 var stubName = namer.invocationName(selector); | 357 var stubName = namer.invocationName(selector); |
| 355 if (stubNames.add(stubName.key)) { | 358 if (stubNames.add(stubName.key)) { |
| 356 interceptorClass.callStubs.add(_buildStubMethod(stubName, | 359 interceptorClass.callStubs.add(_buildStubMethod(stubName, |
| 357 js.js('function(obj) { return obj.# }', [member.name]), | 360 js.js('function(obj) { return obj.# }', [member.name]), |
| 358 element: member)); | 361 element: member)); |
| 359 } | 362 } |
| 360 } | 363 } |
| 361 } | 364 } |
| 362 } | 365 } |
| 363 | 366 |
| 364 if (member.isSetter || (member.isField && !member.isConst)) { | 367 if (member.isSetter || (member.isField && !member.isConst)) { |
| 365 var selectors = | 368 var selectors = |
| 366 _compiler.codegenWorld.setterInvocationsByName(member.name); | 369 _compiler.resolverWorld.setterInvocationsByName(member.name); |
| 367 if (selectors != null && !selectors.isEmpty) { | 370 if (selectors != null && !selectors.isEmpty) { |
| 368 var stubName = namer.setterForElement(member); | 371 var stubName = namer.setterForElement(member); |
| 369 if (stubNames.add(stubName.key)) { | 372 if (stubNames.add(stubName.key)) { |
| 370 interceptorClass.callStubs.add(_buildStubMethod( | 373 interceptorClass.callStubs.add(_buildStubMethod( |
| 371 stubName, | 374 stubName, |
| 372 js.js('function(obj, v) { return obj.# = v }', | 375 js.js('function(obj, v) { return obj.# = v }', |
| 373 [member.name]), | 376 [member.name]), |
| 374 element: member)); | 377 element: member)); |
| 375 } | 378 } |
| 376 } | 379 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 int minArgs; | 415 int minArgs; |
| 413 int maxArgs; | 416 int maxArgs; |
| 414 if (functionType != null) { | 417 if (functionType != null) { |
| 415 minArgs = functionType.parameterTypes.length; | 418 minArgs = functionType.parameterTypes.length; |
| 416 maxArgs = minArgs + functionType.optionalParameterTypes.length; | 419 maxArgs = minArgs + functionType.optionalParameterTypes.length; |
| 417 } else { | 420 } else { |
| 418 minArgs = 0; | 421 minArgs = 0; |
| 419 maxArgs = 32767; | 422 maxArgs = 32767; |
| 420 } | 423 } |
| 421 var selectors = | 424 var selectors = |
| 422 _compiler.codegenWorld.invocationsByName(member.name); | 425 _compiler.resolverWorld.invocationsByName(member.name); |
| 426 |
| 423 // Named arguments are not yet supported. In the future we | 427 // Named arguments are not yet supported. In the future we |
| 424 // may want to map named arguments to an object literal containing | 428 // may want to map named arguments to an object literal containing |
| 425 // all named arguments. | 429 // all named arguments. |
| 426 if (selectors != null && !selectors.isEmpty) { | 430 if (selectors != null && !selectors.isEmpty) { |
| 427 for (var selector in selectors.keys) { | 431 for (var selector in selectors.keys) { |
| 432 |
| 428 // Check whether the arity matches this member. | 433 // Check whether the arity matches this member. |
| 429 var argumentCount = selector.argumentCount; | 434 var argumentCount = selector.argumentCount; |
| 430 // JS interop does not support named arguments. | 435 // JS interop does not support named arguments. |
| 431 if (selector.namedArgumentCount > 0) break; | 436 if (selector.namedArgumentCount > 0) break; |
| 432 if (argumentCount < minArgs) break; | 437 if (argumentCount < minArgs) break; |
| 433 if (argumentCount > maxArgs) break; | 438 if (argumentCount > maxArgs) break; |
| 434 var stubName = namer.invocationName(selector); | 439 var stubName = namer.invocationName(selector); |
| 435 if (!stubNames.add(stubName.key)) break; | 440 if (!stubNames.add(stubName.key)) break; |
| 436 var parameters = | 441 var parameters = |
| 437 new List<String>.generate(argumentCount, (i) => 'p$i'); | 442 new List<String>.generate(argumentCount, (i) => 'p$i'); |
| (...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 975 Constant constant = new Constant(name, holder, constantValue); | 980 Constant constant = new Constant(name, holder, constantValue); |
| 976 _constants[constantValue] = constant; | 981 _constants[constantValue] = constant; |
| 977 } | 982 } |
| 978 } | 983 } |
| 979 | 984 |
| 980 Holder _registerStaticStateHolder() { | 985 Holder _registerStaticStateHolder() { |
| 981 return _registry.registerHolder(namer.staticStateHolder, | 986 return _registry.registerHolder(namer.staticStateHolder, |
| 982 isStaticStateHolder: true); | 987 isStaticStateHolder: true); |
| 983 } | 988 } |
| 984 } | 989 } |
| OLD | NEW |