Chromium Code Reviews| 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'; |
| 11 import 'scanner/scannerlib.dart'; | 11 import 'scanner/scannerlib.dart'; |
| 12 import 'ssa/ssa.dart'; | 12 import 'ssa/ssa.dart'; |
| 13 import 'tree/tree.dart'; | 13 import 'tree/tree.dart'; |
| 14 import 'util/util.dart'; | 14 import 'util/util.dart'; |
| 15 | 15 |
| 16 void processNativeClasses(Enqueuer world, | 16 |
| 17 CodeEmitterTask emitter, | 17 // This could be an abstract class but we use it as a stub for the dart_backend. |
| 18 Collection<LibraryElement> libraries) { | 18 class NativeEnqueuer { |
| 19 for (LibraryElement library in libraries) { | 19 void processNativeClasses(Collection<LibraryElement> libraries) {} |
| 20 processNativeClassesInLibrary(world, emitter, library); | 20 |
| 21 } | 21 |
| 22 } | 22 void registerElement(Element element) {} |
| 23 | 23 |
| 24 void addSubtypes(ClassElement cls, | 24 // Method is a member of a native class. |
| 25 NativeEmitter emitter) { | 25 void registerMethod(Element method) {} |
| 26 for (DartType type in cls.allSupertypes) { | 26 |
| 27 List<Element> subtypes = emitter.subtypes.putIfAbsent( | 27 // Field is a member of a native class. |
| 28 type.element, | 28 void registerFieldLoad(Element field) {} |
| 29 void registerFieldStore(Element field) {} | |
| 30 | |
| 31 // JS-form code can be an instantiation point for types. | |
| 32 // | |
| 33 // JS('_DOMWindowImpl', 'window'); | |
| 34 // | |
| 35 // TODO(sra): Who calls this? | |
| 36 // TODO(sra): How do we parse the type? | |
| 37 // void registerJS(type) {} | |
| 38 | |
| 39 void registerJSDartType(DartType type, Node locationNode) {} | |
| 40 } | |
| 41 | |
| 42 | |
| 43 class NativeEnqueuerBase implements NativeEnqueuer { | |
|
ngeoffray
2012/11/08 08:18:01
A couple of high-level comments and alternatives a
sra1
2012/11/15 00:09:10
Enqueuer already needs to be split into a resoluti
| |
| 44 | |
| 45 final Set<ClassElement> nativeClasses = new Set<ClassElement>(); | |
| 46 | |
| 47 // Each class is exactly one of these sets. | |
| 48 final Set<ClassElement> registeredClasses = new Set<ClassElement>(); | |
| 49 final Set<ClassElement> unusedClasses = new Set<ClassElement>(); | |
| 50 final Set<ClassElement> pendingClasses = new Set<ClassElement>(); | |
| 51 | |
| 52 final queue = new Queue(); // Classes in pendingClasse have thunks in queue. | |
| 53 bool flushing = false; | |
| 54 | |
| 55 final Set<DartType> escapedTypes = new Set<DartType>(); | |
| 56 final Set<DartType> capturedTypes = new Set<DartType>(); | |
| 57 | |
| 58 | |
| 59 final Enqueuer world; | |
| 60 final Compiler compiler; | |
| 61 | |
| 62 // constructed by backend. | |
| 63 NativeEnqueuerBase(this.world, this.compiler); | |
| 64 | |
| 65 void processNativeClasses(Collection<LibraryElement> libraries) { | |
| 66 libraries.forEach(processNativeClassesInLibrary); | |
| 67 | |
| 68 // nativeClasses.forEach(enqueueClass); flushQueue(); | |
| 69 } | |
| 70 | |
| 71 void processNativeClassesInLibrary(LibraryElement library) { | |
| 72 // Use implementation to ensure the inclusion of injected members. | |
| 73 library.implementation.forEachLocalMember((Element element) { | |
| 74 if (element.kind == ElementKind.CLASS) { | |
| 75 ClassElement classElement = element; | |
| 76 if (classElement.isNative()) { | |
| 77 nativeClasses.add(classElement); | |
| 78 unusedClasses.add(classElement); | |
| 79 | |
| 80 // Resolve class to ensure the class has valid inheritance info. | |
| 81 classElement.ensureResolved(compiler); | |
| 82 } | |
| 83 } | |
| 84 }); | |
| 85 } | |
| 86 | |
| 87 enqueueClass(ClassElement classElement, cause) { | |
| 88 assert(unusedClasses.contains(classElement)); | |
| 89 compiler.log( | |
| 90 'Adding native class ${classElement.name.slowToString()},' | |
| 91 ' why: $cause'); | |
| 92 unusedClasses.remove(classElement); | |
| 93 pendingClasses.add(classElement); | |
| 94 queue.add(() { processClass(classElement, cause); }); | |
| 95 } | |
| 96 | |
| 97 void flushQueue() { | |
| 98 if (flushing) return; | |
| 99 flushing = true; | |
| 100 while (!queue.isEmpty) { | |
| 101 (queue.removeFirst())(); | |
| 102 } | |
| 103 flushing = false; | |
| 104 } | |
| 105 | |
| 106 processClass(ClassElement classElement, cause) { | |
| 107 assert(!registeredClasses.contains(classElement)); | |
| 108 | |
| 109 bool firstTime = registeredClasses.isEmpty; | |
| 110 pendingClasses.remove(classElement); | |
| 111 registeredClasses.add(classElement); | |
| 112 | |
| 113 // print('processClass $classElement $cause'); | |
| 114 world.registerInstantiatedClass(classElement); | |
| 115 | |
| 116 // Also parse the node to know all its methods because | |
| 117 // otherwise it will only be parsed if there is a call to | |
| 118 // one of its constructors. | |
| 119 classElement.parseNode(compiler); | |
| 120 | |
| 121 if (firstTime) { | |
| 122 queue.add(onFirstNativeClass); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 registerElement(Element element) { | |
| 127 if (element.isFunction()) return registerMethod(element); | |
| 128 } | |
| 129 | |
| 130 registerMethod(Element method) { | |
| 131 if (isNativeMethod(method)) { | |
| 132 // print('Native method call $method'); | |
| 133 captureType(method.computeType(compiler), method); | |
| 134 flushQueue(); | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 bool isNativeMethod(Element element) { | |
| 139 // Native method? | |
| 140 Node node = element.parseNode(compiler); | |
| 141 if (node is! FunctionExpression) return false; | |
| 142 node = node.body; | |
| 143 Token token = node.getBeginToken(); | |
| 144 if (token.stringValue == 'native') return true; | |
| 145 return false; | |
| 146 } | |
| 147 | |
| 148 registerFieldLoad(Element field) { | |
| 149 // print('Native load field $field'); | |
| 150 captureType(field.computeType(compiler), field); | |
| 151 flushQueue(); | |
| 152 } | |
| 153 | |
| 154 registerFieldStore(Element field) { | |
| 155 // print('Native store field $field'); | |
| 156 escapeType(field.computeType(compiler), field); | |
| 157 flushQueue(); | |
| 158 } | |
| 159 | |
| 160 void registerJSDartType(DartType type, Node locationNode) { | |
| 161 captureType(type, locationNode); | |
| 162 flushQueue(); | |
| 163 } | |
| 164 | |
| 165 escapeType(DartType type, cause) { | |
| 166 type = type.unalias(compiler); | |
| 167 if (escapedTypes.contains(type)) return; | |
| 168 escapedTypes.add(type); | |
| 169 // print(' escapeType $type'); | |
| 170 | |
| 171 if (type is FunctionType) { | |
| 172 escapeType(type.returnType, cause); | |
| 173 // TODO: use FunctionSignature. | |
| 174 for (Link<DartType> parameters = type.parameterTypes; | |
| 175 !parameters.isEmpty; | |
| 176 parameters = parameters.tail) { | |
| 177 captureType(parameters.head, cause); | |
| 178 } | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 captureType(DartType type, cause) { | |
| 183 type = type.unalias(compiler); | |
| 184 if (capturedTypes.contains(type)) return; | |
| 185 capturedTypes.add(type); | |
| 186 // print(' capturedType $type'); | |
| 187 | |
| 188 if (type is FunctionType) { | |
| 189 captureType(type.returnType, cause); | |
| 190 for (Link<DartType> parameters = type.parameterTypes; | |
| 191 !parameters.isEmpty; | |
| 192 parameters = parameters.tail) { | |
| 193 escapeType(parameters.head, cause); | |
| 194 } | |
| 195 } else { | |
| 196 bool allUsedBefore = unusedClasses.isEmpty; | |
| 197 enqueueUnusedClassesMatching( | |
| 198 (nativeClass) => compiler.types.isSubtype(nativeClass.type, type), | |
| 199 cause, | |
| 200 'subtypeof($type)'); | |
| 201 if (unusedClasses.isEmpty && !allUsedBefore) { | |
| 202 if (cause is Node) { | |
| 203 throw 'Eugh $cause'; | |
| 204 compiler.log('All native types marked as used due to $cause.'); | |
| 205 } else if (cause.position() != null) { | |
| 206 //compiler.cancel('Eugh!', token: cause.position()); | |
| 207 compiler.log('All native types marked used due to $cause.'); | |
| 208 } else { | |
| 209 compiler.log('All native types marked used due to $type.'); | |
| 210 } | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 // TODO(sra): Do we need to do something for capturing List<Node>? For now, | |
| 215 // hope that NodeListImpl matches List, causing NodeListImpl.operator[] to | |
| 216 // match some invocation, leading to instantiating Node. | |
| 217 | |
| 218 // Required for: | |
| 219 // typedef void MutationCallback(List<MutationRecord> mutations, MutationO bserver observer); | |
| 220 // No other code instantiates MutationRecord. | |
| 221 // We can work-around by annotating the MutationObserver constructor with a list of 'also created' type. | |
| 222 | |
| 223 } | |
| 224 | |
| 225 enqueueUnusedClassesMatching(predicate, cause, [reason]) { | |
| 226 var matches = unusedClasses.filter(predicate); | |
| 227 // print('${matches.length} matches: $matches\n.. $reason $world'); | |
| 228 matches.forEach((c) => enqueueClass(c, cause)); | |
| 229 } | |
| 230 | |
| 231 onFirstNativeClass() { | |
| 232 staticUse(name) => world.registerStaticUse(compiler.findHelper(name)); | |
| 233 | |
| 234 staticUse(const SourceString('dynamicFunction')); | |
| 235 staticUse(const SourceString('dynamicSetMetadata')); | |
| 236 staticUse(const SourceString('defineProperty')); | |
| 237 staticUse(const SourceString('toStringForNativeObject')); | |
| 238 staticUse(const SourceString('hashCodeForNativeObject')); | |
| 239 | |
| 240 addNativeExceptions(); | |
| 241 } | |
| 242 | |
| 243 addNativeExceptions() { | |
| 244 enqueueUnusedClassesMatching((classElement) { | |
| 245 // TODO(sra): Annotate exception classes in dart:html. | |
| 246 var name = classElement.name.slowToString(); | |
| 247 if (name.contains('Exception')) return true; | |
| 248 if (name.contains('Error')) return true; | |
| 249 return false; | |
| 250 }, | |
| 251 'native exception'); | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 | |
| 256 class NativeResolutionEnqueuer extends NativeEnqueuerBase { | |
| 257 | |
| 258 NativeResolutionEnqueuer(Enqueuer world, Compiler compiler) | |
| 259 : super(world, compiler); | |
| 260 } | |
| 261 | |
| 262 | |
| 263 class NativeCodegenEnqueuer extends NativeEnqueuerBase { | |
| 264 | |
| 265 final CodeEmitterTask emitter; | |
| 266 | |
| 267 NativeCodegenEnqueuer(Enqueuer world, Compiler compiler, this.emitter) | |
| 268 : super(world, compiler); | |
| 269 | |
| 270 void processNativeClasses(Collection<LibraryElement> libraries) { | |
| 271 super.processNativeClasses(libraries); | |
| 272 | |
| 273 // HACK HACK - add all the resolved classes. | |
| 274 for (final classElement | |
| 275 in compiler.enqueuer.resolution.nativeEnqueuer.registeredClasses) { | |
| 276 if (unusedClasses.contains(classElement)) { | |
| 277 enqueueClass(classElement, 'was resolved'); | |
| 278 } | |
| 279 } | |
| 280 flushQueue(); | |
| 281 } | |
| 282 | |
| 283 processClass(ClassElement classElement, cause) { | |
| 284 super.processClass(classElement, cause); | |
| 285 // Add the information that this class is a subtype of its supertypes. The | |
| 286 // code emitter and the ssa builder use that information. | |
| 287 addSubtypes(classElement, emitter.nativeEmitter); | |
| 288 } | |
| 289 | |
| 290 void addSubtypes(ClassElement cls, NativeEmitter emitter) { | |
| 291 for (DartType type in cls.allSupertypes) { | |
| 292 List<Element> subtypes = emitter.subtypes.putIfAbsent( | |
| 293 type.element, | |
| 294 () => <ClassElement>[]); | |
| 295 subtypes.add(cls); | |
| 296 } | |
| 297 | |
| 298 List<Element> directSubtypes = emitter.directSubtypes.putIfAbsent( | |
| 299 cls.superclass, | |
| 29 () => <ClassElement>[]); | 300 () => <ClassElement>[]); |
| 30 subtypes.add(cls); | 301 directSubtypes.add(cls); |
| 31 } | 302 } |
| 32 | 303 |
| 33 List<Element> directSubtypes = emitter.directSubtypes.putIfAbsent( | |
| 34 cls.superclass, | |
| 35 () => <ClassElement>[]); | |
| 36 directSubtypes.add(cls); | |
| 37 } | |
| 38 | |
| 39 void processNativeClassesInLibrary(Enqueuer world, | |
| 40 CodeEmitterTask emitter, | |
| 41 LibraryElement library) { | |
| 42 bool hasNativeClass = false; | |
| 43 final compiler = emitter.compiler; | |
| 44 // Use implementation to ensure the inclusion of injected members. | |
| 45 library.implementation.forEachLocalMember((Element element) { | |
| 46 if (element.kind == ElementKind.CLASS) { | |
| 47 ClassElement classElement = element; | |
| 48 if (classElement.isNative()) { | |
| 49 hasNativeClass = true; | |
| 50 world.registerInstantiatedClass(classElement); | |
| 51 // Also parse the node to know all its methods because | |
| 52 // otherwise it will only be parsed if there is a call to | |
| 53 // one of its constructor. | |
| 54 classElement.parseNode(compiler); | |
| 55 // Resolve to setup the inheritance. | |
| 56 classElement.ensureResolved(compiler); | |
| 57 // Add the information that this class is a subtype of | |
| 58 // its supertypes. The code emitter and the ssa builder use that | |
| 59 // information. | |
| 60 addSubtypes(classElement, emitter.nativeEmitter); | |
| 61 } | |
| 62 } | |
| 63 }); | |
| 64 if (hasNativeClass) { | |
| 65 world.registerStaticUse(compiler.findHelper( | |
| 66 const SourceString('dynamicFunction'))); | |
| 67 world.registerStaticUse(compiler.findHelper( | |
| 68 const SourceString('dynamicSetMetadata'))); | |
| 69 world.registerStaticUse(compiler.findHelper( | |
| 70 const SourceString('defineProperty'))); | |
| 71 world.registerStaticUse(compiler.findHelper( | |
| 72 const SourceString('toStringForNativeObject'))); | |
| 73 world.registerStaticUse(compiler.findHelper( | |
| 74 const SourceString('hashCodeForNativeObject'))); | |
| 75 } | |
| 76 } | 304 } |
| 77 | 305 |
| 78 void maybeEnableNative(Compiler compiler, | 306 void maybeEnableNative(Compiler compiler, |
| 79 LibraryElement library, | 307 LibraryElement library, |
| 80 Uri uri) { | 308 Uri uri) { |
| 81 String libraryName = uri.toString(); | 309 String libraryName = uri.toString(); |
| 82 if (library.entryCompilationUnit.script.name.contains( | 310 if (library.entryCompilationUnit.script.name.contains( |
| 83 'dart/tests/compiler/dart2js_native') | 311 'dart/tests/compiler/dart2js_native') |
| 84 || libraryName == 'dart:isolate' | 312 || libraryName == 'dart:isolate' |
| 85 || libraryName == 'dart:html') { | 313 || libraryName == 'dart:html') { |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 String parameters) { | 559 String parameters) { |
| 332 buffer.add(" if (Object.getPrototypeOf(this).hasOwnProperty"); | 560 buffer.add(" if (Object.getPrototypeOf(this).hasOwnProperty"); |
| 333 buffer.add("('$methodName')) {\n"); | 561 buffer.add("('$methodName')) {\n"); |
| 334 buffer.add(" $code"); | 562 buffer.add(" $code"); |
| 335 buffer.add(" } else {\n"); | 563 buffer.add(" } else {\n"); |
| 336 buffer.add(" return Object.prototype.$methodName.call(this"); | 564 buffer.add(" return Object.prototype.$methodName.call(this"); |
| 337 buffer.add(parameters == '' ? '' : ', $parameters'); | 565 buffer.add(parameters == '' ? '' : ', $parameters'); |
| 338 buffer.add(");\n"); | 566 buffer.add(");\n"); |
| 339 buffer.add(" }\n"); | 567 buffer.add(" }\n"); |
| 340 } | 568 } |
| OLD | NEW |