| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 native; | 5 library native; | 
| 6 | 6 | 
| 7 import 'dart:uri'; | 7 import 'dart:uri'; | 
| 8 import 'dart2jslib.dart' hide SourceString; | 8 import 'dart2jslib.dart' hide SourceString; | 
| 9 import 'elements/elements.dart'; | 9 import 'elements/elements.dart'; | 
| 10 import 'js_backend/js_backend.dart'; | 10 import 'js_backend/js_backend.dart'; | 
| (...skipping 15 matching lines...) Expand all  Loading... | 
| 26   /// The specific implementation of List that is JavaScript Array: | 26   /// The specific implementation of List that is JavaScript Array: | 
| 27   static const JsArray = const SpecialType._('=List'); | 27   static const JsArray = const SpecialType._('=List'); | 
| 28 } | 28 } | 
| 29 | 29 | 
| 30 | 30 | 
| 31 /** | 31 /** | 
| 32  * This could be an abstract class but we use it as a stub for the dart_backend. | 32  * This could be an abstract class but we use it as a stub for the dart_backend. | 
| 33  */ | 33  */ | 
| 34 class NativeEnqueuer { | 34 class NativeEnqueuer { | 
| 35   /// Initial entry point to native enqueuer. | 35   /// Initial entry point to native enqueuer. | 
| 36   void processNativeClasses(Collection<LibraryElement> libraries) {} | 36   void processNativeClasses(Iterable<LibraryElement> libraries) {} | 
| 37 | 37 | 
| 38   /// Notification of a main Enqueuer worklist element.  For methods, adds | 38   /// Notification of a main Enqueuer worklist element.  For methods, adds | 
| 39   /// information from metadata attributes, and computes types instantiated due | 39   /// information from metadata attributes, and computes types instantiated due | 
| 40   /// to calling the method. | 40   /// to calling the method. | 
| 41   void registerElement(Element element) {} | 41   void registerElement(Element element) {} | 
| 42 | 42 | 
| 43   /// Notification of native field.  Adds information from metadata attributes. | 43   /// Notification of native field.  Adds information from metadata attributes. | 
| 44   void handleFieldAnnotations(Element field) {} | 44   void handleFieldAnnotations(Element field) {} | 
| 45 | 45 | 
| 46   /// Computes types instantiated due to getting a native field. | 46   /// Computes types instantiated due to getting a native field. | 
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 94   final Compiler compiler; | 94   final Compiler compiler; | 
| 95   final bool enableLiveTypeAnalysis; | 95   final bool enableLiveTypeAnalysis; | 
| 96 | 96 | 
| 97   ClassElement _annotationCreatesClass; | 97   ClassElement _annotationCreatesClass; | 
| 98   ClassElement _annotationReturnsClass; | 98   ClassElement _annotationReturnsClass; | 
| 99   ClassElement _annotationJsNameClass; | 99   ClassElement _annotationJsNameClass; | 
| 100 | 100 | 
| 101   /// Subclasses of [NativeEnqueuerBase] are constructed by the backend. | 101   /// Subclasses of [NativeEnqueuerBase] are constructed by the backend. | 
| 102   NativeEnqueuerBase(this.world, this.compiler, this.enableLiveTypeAnalysis); | 102   NativeEnqueuerBase(this.world, this.compiler, this.enableLiveTypeAnalysis); | 
| 103 | 103 | 
| 104   void processNativeClasses(Collection<LibraryElement> libraries) { | 104   void processNativeClasses(Iterable<LibraryElement> libraries) { | 
| 105     libraries.forEach(processNativeClassesInLibrary); | 105     libraries.forEach(processNativeClassesInLibrary); | 
| 106     processNativeClassesInLibrary(compiler.isolateHelperLibrary); | 106     processNativeClassesInLibrary(compiler.isolateHelperLibrary); | 
| 107     if (!enableLiveTypeAnalysis) { | 107     if (!enableLiveTypeAnalysis) { | 
| 108       nativeClasses.forEach((c) => enqueueClass(c, 'forced')); | 108       nativeClasses.forEach((c) => enqueueClass(c, 'forced')); | 
| 109       flushQueue(); | 109       flushQueue(); | 
| 110     } | 110     } | 
| 111   } | 111   } | 
| 112 | 112 | 
| 113   void processNativeClassesInLibrary(LibraryElement library) { | 113   void processNativeClassesInLibrary(LibraryElement library) { | 
| 114     // Use implementation to ensure the inclusion of injected members. | 114     // Use implementation to ensure the inclusion of injected members. | 
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 343     // Give an info so that library developers can compile with -v to find why | 343     // Give an info so that library developers can compile with -v to find why | 
| 344     // all the native classes are included. | 344     // all the native classes are included. | 
| 345     if (unusedClasses.isEmpty && !allUsedBefore) { | 345     if (unusedClasses.isEmpty && !allUsedBefore) { | 
| 346       compiler.log('All native types marked as used due to $cause.'); | 346       compiler.log('All native types marked as used due to $cause.'); | 
| 347     } | 347     } | 
| 348   } | 348   } | 
| 349 | 349 | 
| 350   enqueueUnusedClassesMatching(bool predicate(classElement), | 350   enqueueUnusedClassesMatching(bool predicate(classElement), | 
| 351                                cause, | 351                                cause, | 
| 352                                [String reason]) { | 352                                [String reason]) { | 
| 353     Collection matches = unusedClasses.filter(predicate); | 353     Iterable matches = unusedClasses.where(predicate); | 
| 354     matches.forEach((c) => enqueueClass(c, cause)); | 354     matches.forEach((c) => enqueueClass(c, cause)); | 
| 355   } | 355   } | 
| 356 | 356 | 
| 357   onFirstNativeClass() { | 357   onFirstNativeClass() { | 
| 358     staticUse(name) => world.registerStaticUse(compiler.findHelper(name)); | 358     staticUse(name) => world.registerStaticUse(compiler.findHelper(name)); | 
| 359 | 359 | 
| 360     staticUse(const SourceString('dynamicFunction')); | 360     staticUse(const SourceString('dynamicFunction')); | 
| 361     staticUse(const SourceString('dynamicSetMetadata')); | 361     staticUse(const SourceString('dynamicSetMetadata')); | 
| 362     staticUse(const SourceString('defineProperty')); | 362     staticUse(const SourceString('defineProperty')); | 
| 363     staticUse(const SourceString('toStringForNativeObject')); | 363     staticUse(const SourceString('toStringForNativeObject')); | 
| (...skipping 29 matching lines...) Expand all  Loading... | 
| 393 | 393 | 
| 394 class NativeCodegenEnqueuer extends NativeEnqueuerBase { | 394 class NativeCodegenEnqueuer extends NativeEnqueuerBase { | 
| 395 | 395 | 
| 396   final CodeEmitterTask emitter; | 396   final CodeEmitterTask emitter; | 
| 397 | 397 | 
| 398   final Set<ClassElement> doneAddSubtypes = new Set<ClassElement>(); | 398   final Set<ClassElement> doneAddSubtypes = new Set<ClassElement>(); | 
| 399 | 399 | 
| 400   NativeCodegenEnqueuer(Enqueuer world, Compiler compiler, this.emitter) | 400   NativeCodegenEnqueuer(Enqueuer world, Compiler compiler, this.emitter) | 
| 401     : super(world, compiler, compiler.enableNativeLiveTypeAnalysis); | 401     : super(world, compiler, compiler.enableNativeLiveTypeAnalysis); | 
| 402 | 402 | 
| 403   void processNativeClasses(Collection<LibraryElement> libraries) { | 403   void processNativeClasses(Iterable<LibraryElement> libraries) { | 
| 404     super.processNativeClasses(libraries); | 404     super.processNativeClasses(libraries); | 
| 405 | 405 | 
| 406     // HACK HACK - add all the resolved classes. | 406     // HACK HACK - add all the resolved classes. | 
| 407     NativeEnqueuerBase enqueuer = compiler.enqueuer.resolution.nativeEnqueuer; | 407     NativeEnqueuerBase enqueuer = compiler.enqueuer.resolution.nativeEnqueuer; | 
| 408     for (final classElement in enqueuer.registeredClasses) { | 408     for (final classElement in enqueuer.registeredClasses) { | 
| 409       if (unusedClasses.contains(classElement)) { | 409       if (unusedClasses.contains(classElement)) { | 
| 410         enqueueClass(classElement, 'was resolved'); | 410         enqueueClass(classElement, 'was resolved'); | 
| 411       } | 411       } | 
| 412     } | 412     } | 
| 413     flushQueue(); | 413     flushQueue(); | 
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 447         '${unusedClasses.length} native classes omitted.'); | 447         '${unusedClasses.length} native classes omitted.'); | 
| 448   } | 448   } | 
| 449 } | 449 } | 
| 450 | 450 | 
| 451 void maybeEnableNative(Compiler compiler, | 451 void maybeEnableNative(Compiler compiler, | 
| 452                        LibraryElement library, | 452                        LibraryElement library, | 
| 453                        Uri uri) { | 453                        Uri uri) { | 
| 454   String libraryName = uri.toString(); | 454   String libraryName = uri.toString(); | 
| 455   if (library.entryCompilationUnit.script.name.contains( | 455   if (library.entryCompilationUnit.script.name.contains( | 
| 456           'dart/tests/compiler/dart2js_native') | 456           'dart/tests/compiler/dart2js_native') | 
|  | 457       || libraryName == 'dart:async' | 
| 457       || libraryName == 'dart:html' | 458       || libraryName == 'dart:html' | 
| 458       || libraryName == 'dart:html_common' | 459       || libraryName == 'dart:html_common' | 
| 459       || libraryName == 'dart:indexed_db' | 460       || libraryName == 'dart:indexed_db' | 
| 460       || libraryName == 'dart:svg' | 461       || libraryName == 'dart:svg' | 
| 461       || libraryName == 'dart:web_audio') { | 462       || libraryName == 'dart:web_audio') { | 
| 462     library.canUseNative = true; | 463     library.canUseNative = true; | 
| 463   } | 464   } | 
| 464 } | 465 } | 
| 465 | 466 | 
| 466 /** | 467 /** | 
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 874   } else { | 875   } else { | 
| 875     if (parameters.parameterCount != 0) { | 876     if (parameters.parameterCount != 0) { | 
| 876       compiler.cancel( | 877       compiler.cancel( | 
| 877           'native "..." syntax is restricted to functions with zero parameters', | 878           'native "..." syntax is restricted to functions with zero parameters', | 
| 878           node: nativeBody); | 879           node: nativeBody); | 
| 879     } | 880     } | 
| 880     LiteralString jsCode = nativeBody.asLiteralString(); | 881     LiteralString jsCode = nativeBody.asLiteralString(); | 
| 881     builder.push(new HForeign.statement(jsCode.dartString, <HInstruction>[])); | 882     builder.push(new HForeign.statement(jsCode.dartString, <HInstruction>[])); | 
| 882   } | 883   } | 
| 883 } | 884 } | 
| OLD | NEW | 
|---|