| 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 part of js_backend; | 5 part of js_backend; |
| 6 | 6 |
| 7 class NativeEmitter { | 7 class NativeEmitter { |
| 8 | 8 |
| 9 CodeEmitterTask emitter; | 9 CodeEmitterTask emitter; |
| 10 CodeBuffer nativeBuffer; | 10 CodeBuffer nativeBuffer; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 const SourceString('defineNativeMethodsNonleaf')); | 92 const SourceString('defineNativeMethodsNonleaf')); |
| 93 return backend.namer.isolateAccess(element); | 93 return backend.namer.isolateAccess(element); |
| 94 } | 94 } |
| 95 | 95 |
| 96 String get defineNativeMethodsFinishName { | 96 String get defineNativeMethodsFinishName { |
| 97 Element element = compiler.findHelper( | 97 Element element = compiler.findHelper( |
| 98 const SourceString('defineNativeMethodsFinish')); | 98 const SourceString('defineNativeMethodsFinish')); |
| 99 return backend.namer.isolateAccess(element); | 99 return backend.namer.isolateAccess(element); |
| 100 } | 100 } |
| 101 | 101 |
| 102 List<String> nativeTagsOfClass(ClassElement cls) { | 102 bool isNativeGlobal(String quotedName) { |
| 103 return identical(quotedName[1], '@'); |
| 104 } |
| 105 |
| 106 String toNativeTag(ClassElement cls) { |
| 103 String quotedName = cls.nativeTagInfo.slowToString(); | 107 String quotedName = cls.nativeTagInfo.slowToString(); |
| 104 return quotedName.substring(1, quotedName.length - 1).split(','); | 108 if (isNativeGlobal(quotedName)) { |
| 109 // Global object, just be like the other types for now. |
| 110 return quotedName.substring(3, quotedName.length - 1); |
| 111 } else { |
| 112 return quotedName.substring(2, quotedName.length - 1); |
| 113 } |
| 105 } | 114 } |
| 106 | 115 |
| 107 /** | 116 /** |
| 108 * Writes the class definitions for the interceptors to [mainBuffer]. | 117 * Writes the class definitions for the interceptors to [mainBuffer]. |
| 109 * Writes code to associate dispatch tags with interceptors to [nativeBuffer]. | 118 * Writes code to associate dispatch tags with interceptors to [nativeBuffer]. |
| 110 * | 119 * |
| 111 * The interceptors are filtered to avoid emitting trivial interceptors. For | 120 * The interceptors are filtered to avoid emitting trivial interceptors. For |
| 112 * example, if the program contains no code that can distinguish between the | 121 * example, if the program contains no code that can distinguish between the |
| 113 * numerous subclasses of `Element` then we can pretend that `Element` is a | 122 * numerous subclasses of `Element` then we can pretend that `Element` is a |
| 114 * leaf class, and all instances of subclasses of `Element` are instances of | 123 * leaf class, and all instances of subclasses of `Element` are instances of |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 // TODO(9556): We can't remove any unneeded classes until the class | 178 // TODO(9556): We can't remove any unneeded classes until the class |
| 170 // builders contain all the information. [emitRuntimeTypeSupport] must | 179 // builders contain all the information. [emitRuntimeTypeSupport] must |
| 171 // no longer add information to a class definition. | 180 // no longer add information to a class definition. |
| 172 needed = true; | 181 needed = true; |
| 173 } | 182 } |
| 174 | 183 |
| 175 // BUG. There is a missing proto in the picture the DOM gives of the | 184 // BUG. There is a missing proto in the picture the DOM gives of the |
| 176 // proto chain. | 185 // proto chain. |
| 177 // TODO(9907): Fix DOM generation. We might need an annotation. | 186 // TODO(9907): Fix DOM generation. We might need an annotation. |
| 178 if (classElement.isNative()) { | 187 if (classElement.isNative()) { |
| 179 List<String> nativeTags = nativeTagsOfClass(classElement); | 188 String nativeTag = toNativeTag(classElement); |
| 180 if (nativeTags.contains('HTMLElement')) { | 189 if (nativeTag == 'HTMLElement') { |
| 181 nonleafClasses.add(classElement); | 190 nonleafClasses.add(classElement); |
| 182 needed = true; | 191 needed = true; |
| 183 } | 192 } |
| 184 } | 193 } |
| 185 | 194 |
| 186 if (needed || neededClasses.contains(classElement)) { | 195 if (needed || neededClasses.contains(classElement)) { |
| 187 neededClasses.add(classElement); | 196 neededClasses.add(classElement); |
| 188 neededClasses.add(classElement.superclass); | 197 neededClasses.add(classElement.superclass); |
| 189 nonleafClasses.add(classElement.superclass); | 198 nonleafClasses.add(classElement.superclass); |
| 190 } | 199 } |
| 191 } | 200 } |
| 192 | 201 |
| 193 // Collect all the tags that map to each class. | 202 // Collect all the tags that map to each class. |
| 194 | 203 |
| 195 Map<ClassElement, Set<String>> leafTags = | 204 Map<ClassElement, Set<String>> leafTags = |
| 196 new Map<ClassElement, Set<String>>(); | 205 new Map<ClassElement, Set<String>>(); |
| 197 Map<ClassElement, Set<String>> nonleafTags = | 206 Map<ClassElement, Set<String>> nonleafTags = |
| 198 new Map<ClassElement, Set<String>>(); | 207 new Map<ClassElement, Set<String>>(); |
| 199 | 208 |
| 200 for (ClassElement classElement in classes) { | 209 for (ClassElement classElement in classes) { |
| 201 List<String> nativeTags = nativeTagsOfClass(classElement); | 210 String nativeTag = toNativeTag(classElement); |
| 202 | 211 |
| 203 if (nonleafClasses.contains(classElement)) { | 212 if (nonleafClasses.contains(classElement)) { |
| 204 nonleafTags | 213 nonleafTags |
| 205 .putIfAbsent(classElement, () => new Set<String>()) | 214 .putIfAbsent(classElement, () => new Set<String>()) |
| 206 .addAll(nativeTags); | 215 .add(nativeTag); |
| 207 } else { | 216 } else { |
| 208 ClassElement sufficingInterceptor = classElement; | 217 ClassElement sufficingInterceptor = classElement; |
| 209 while (!neededClasses.contains(sufficingInterceptor)) { | 218 while (!neededClasses.contains(sufficingInterceptor)) { |
| 210 sufficingInterceptor = sufficingInterceptor.superclass; | 219 sufficingInterceptor = sufficingInterceptor.superclass; |
| 211 } | 220 } |
| 212 if (sufficingInterceptor == compiler.objectClass) { | 221 if (sufficingInterceptor == compiler.objectClass) { |
| 213 sufficingInterceptor = backend.jsInterceptorClass; | 222 sufficingInterceptor = backend.jsInterceptorClass; |
| 214 } | 223 } |
| 215 leafTags | 224 leafTags |
| 216 .putIfAbsent(sufficingInterceptor, () => new Set<String>()) | 225 .putIfAbsent(sufficingInterceptor, () => new Set<String>()) |
| 217 .addAll(nativeTags); | 226 .add(nativeTag); |
| 218 } | 227 } |
| 219 } | 228 } |
| 220 | 229 |
| 221 // Emit code to set up dispatch data that maps tags to the interceptors. | 230 // Emit code to set up dispatch data that maps tags to the interceptors. |
| 222 | 231 |
| 223 void generateDefines(ClassElement classElement) { | 232 void generateDefines(ClassElement classElement) { |
| 224 generateDefineNativeMethods(leafTags[classElement], classElement, | 233 generateDefineNativeMethods(leafTags[classElement], classElement, |
| 225 defineNativeMethodsName); | 234 defineNativeMethodsName); |
| 226 generateDefineNativeMethods(nonleafTags[classElement], classElement, | 235 generateDefineNativeMethods(nonleafTags[classElement], classElement, |
| 227 defineNativeMethodsNonleafName); | 236 defineNativeMethodsNonleafName); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 // The target JS function may check arguments.length so we need to | 352 // The target JS function may check arguments.length so we need to |
| 344 // make sure not to pass any unspecified optional arguments to it. | 353 // make sure not to pass any unspecified optional arguments to it. |
| 345 // For example, for the following Dart method: | 354 // For example, for the following Dart method: |
| 346 // foo([x, y, z]); | 355 // foo([x, y, z]); |
| 347 // The call: | 356 // The call: |
| 348 // foo(y: 1) | 357 // foo(y: 1) |
| 349 // must be turned into a JS call to: | 358 // must be turned into a JS call to: |
| 350 // foo(null, y). | 359 // foo(null, y). |
| 351 | 360 |
| 352 ClassElement classElement = member.enclosingElement; | 361 ClassElement classElement = member.enclosingElement; |
| 362 String nativeTagInfo = classElement.nativeTagInfo.slowToString(); |
| 353 | 363 |
| 354 List<jsAst.Statement> statements = <jsAst.Statement>[]; | 364 List<jsAst.Statement> statements = <jsAst.Statement>[]; |
| 355 potentiallyConvertDartClosuresToJs(statements, member, stubParameters); | 365 potentiallyConvertDartClosuresToJs(statements, member, stubParameters); |
| 356 | 366 |
| 357 String target; | 367 String target; |
| 358 jsAst.Expression receiver; | 368 jsAst.Expression receiver; |
| 359 List<jsAst.Expression> arguments; | 369 List<jsAst.Expression> arguments; |
| 360 | 370 |
| 361 if (!nativeMethods.contains(member)) { | 371 if (!nativeMethods.contains(member)) { |
| 362 // When calling a method that has a native body, we call it with our | 372 // When calling a method that has a native body, we call it with our |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 if (emitter.compiler.enableMinification) targetBuffer.add(';'); | 458 if (emitter.compiler.enableMinification) targetBuffer.add(';'); |
| 449 targetBuffer.add(jsAst.prettyPrint( | 459 targetBuffer.add(jsAst.prettyPrint( |
| 450 new jsAst.ExpressionStatement(init), compiler)); | 460 new jsAst.ExpressionStatement(init), compiler)); |
| 451 targetBuffer.add('\n'); | 461 targetBuffer.add('\n'); |
| 452 } | 462 } |
| 453 | 463 |
| 454 targetBuffer.add(nativeBuffer); | 464 targetBuffer.add(nativeBuffer); |
| 455 targetBuffer.add('\n'); | 465 targetBuffer.add('\n'); |
| 456 } | 466 } |
| 457 } | 467 } |
| OLD | NEW |