| 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 final Map<Element, ClassBuilder> cachedBuilders; | 9 final Map<Element, ClassBuilder> cachedBuilders; |
| 10 | 10 |
| 11 final CodeEmitterTask emitterTask; | 11 final CodeEmitterTask emitterTask; |
| 12 | 12 |
| 13 // Native classes found in the application. | 13 // Whether the application contains native classes. |
| 14 Set<ClassElement> nativeClasses = new Set<ClassElement>(); | 14 bool hasNativeClasses = false; |
| 15 | 15 |
| 16 // Caches the native subtypes of a native class. | 16 // Caches the native subtypes of a native class. |
| 17 Map<ClassElement, List<ClassElement>> subtypes; | 17 Map<ClassElement, List<ClassElement>> subtypes; |
| 18 | 18 |
| 19 // Caches the direct native subtypes of a native class. | 19 // Caches the direct native subtypes of a native class. |
| 20 Map<ClassElement, List<ClassElement>> directSubtypes; | 20 Map<ClassElement, List<ClassElement>> directSubtypes; |
| 21 | 21 |
| 22 // Caches the methods that have a native body. | 22 // Caches the methods that have a native body. |
| 23 Set<FunctionElement> nativeMethods; | 23 Set<FunctionElement> nativeMethods; |
| 24 | 24 |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 .add(classElement); | 271 .add(classElement); |
| 272 } | 272 } |
| 273 } | 273 } |
| 274 return map; | 274 return map; |
| 275 } | 275 } |
| 276 | 276 |
| 277 ClassBuilder generateNativeClass(ClassElement classElement) { | 277 ClassBuilder generateNativeClass(ClassElement classElement) { |
| 278 // TODO(sra): Issue #13731- this is commented out as part of custom element | 278 // TODO(sra): Issue #13731- this is commented out as part of custom element |
| 279 // constructor work. | 279 // constructor work. |
| 280 //assert(!classElement.hasBackendMembers); | 280 //assert(!classElement.hasBackendMembers); |
| 281 nativeClasses.add(classElement); | 281 hasNativeClasses = true; |
| 282 | 282 |
| 283 ClassElement superclass = classElement.superclass; | 283 ClassElement superclass = classElement.superclass; |
| 284 assert(superclass != null); | 284 assert(superclass != null); |
| 285 // Fix superclass. TODO(sra): make native classes inherit from Interceptor. | 285 // Fix superclass. TODO(sra): make native classes inherit from Interceptor. |
| 286 assert(superclass != compiler.objectClass); | 286 assert(superclass != compiler.objectClass); |
| 287 if (superclass == compiler.objectClass) { | 287 if (superclass == compiler.objectClass) { |
| 288 superclass = backend.jsInterceptorClass; | 288 superclass = backend.jsInterceptorClass; |
| 289 } | 289 } |
| 290 | 290 |
| 291 String superName = backend.namer.getNameOfClass(superclass); | 291 String superName = backend.namer.getNameOfClass(superclass); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 | 440 |
| 441 void assembleCode(CodeOutput targetOutput) { | 441 void assembleCode(CodeOutput targetOutput) { |
| 442 List<jsAst.Property> objectProperties = <jsAst.Property>[]; | 442 List<jsAst.Property> objectProperties = <jsAst.Property>[]; |
| 443 | 443 |
| 444 jsAst.Property addProperty(String name, jsAst.Expression value) { | 444 jsAst.Property addProperty(String name, jsAst.Expression value) { |
| 445 jsAst.Property prop = new jsAst.Property(js.string(name), value); | 445 jsAst.Property prop = new jsAst.Property(js.string(name), value); |
| 446 objectProperties.add(prop); | 446 objectProperties.add(prop); |
| 447 return prop; | 447 return prop; |
| 448 } | 448 } |
| 449 | 449 |
| 450 if (!nativeClasses.isEmpty) { | 450 if (hasNativeClasses) { |
| 451 // If the native emitter has been asked to take care of the | 451 // If the native emitter has been asked to take care of the |
| 452 // noSuchMethod handlers, we do that now. | 452 // noSuchMethod handlers, we do that now. |
| 453 if (handleNoSuchMethod) { | 453 if (handleNoSuchMethod) { |
| 454 emitterTask.oldEmitter.nsmEmitter.emitNoSuchMethodHandlers(addProperty); | 454 emitterTask.oldEmitter.nsmEmitter.emitNoSuchMethodHandlers(addProperty); |
| 455 } | 455 } |
| 456 } | 456 } |
| 457 | 457 |
| 458 // If we have any properties to add to Object.prototype, we run | 458 // If we have any properties to add to Object.prototype, we run |
| 459 // through them and add them using defineProperty. | 459 // through them and add them using defineProperty. |
| 460 if (!objectProperties.isEmpty) { | 460 if (!objectProperties.isEmpty) { |
| 461 jsAst.Expression init = js(r''' | 461 jsAst.Expression init = js(r''' |
| 462 (function(table) { | 462 (function(table) { |
| 463 for(var key in table) | 463 for(var key in table) |
| 464 #(Object.prototype, key, table[key]); | 464 #(Object.prototype, key, table[key]); |
| 465 })(#)''', | 465 })(#)''', |
| 466 [ defPropFunction, | 466 [ defPropFunction, |
| 467 new jsAst.ObjectInitializer(objectProperties)]); | 467 new jsAst.ObjectInitializer(objectProperties)]); |
| 468 | 468 |
| 469 if (emitterTask.compiler.enableMinification) { | 469 if (emitterTask.compiler.enableMinification) { |
| 470 targetOutput.add(';'); | 470 targetOutput.add(';'); |
| 471 } | 471 } |
| 472 targetOutput.addBuffer(jsAst.prettyPrint( | 472 targetOutput.addBuffer(jsAst.prettyPrint( |
| 473 new jsAst.ExpressionStatement(init), compiler)); | 473 new jsAst.ExpressionStatement(init), compiler)); |
| 474 targetOutput.add('\n'); | 474 targetOutput.add('\n'); |
| 475 } | 475 } |
| 476 | 476 |
| 477 targetOutput.add('\n'); | 477 targetOutput.add('\n'); |
| 478 } | 478 } |
| 479 } | 479 } |
| OLD | NEW |