| 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 class NativeEmitter { | 5 class NativeEmitter { |
| 6 | 6 |
| 7 Compiler compiler; | 7 Compiler compiler; |
| 8 StringBuffer buffer; | 8 StringBuffer buffer; |
| 9 | 9 |
| 10 // Classes that participate in dynamic dispatch. These are the | 10 // Classes that participate in dynamic dispatch. These are the |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 buffer.add(Strings.join(entries, ',')); | 295 buffer.add(Strings.join(entries, ',')); |
| 296 buffer.add('];\n'); | 296 buffer.add('];\n'); |
| 297 buffer.add('$dynamicSetMetadataName(table);\n'); | 297 buffer.add('$dynamicSetMetadataName(table);\n'); |
| 298 | 298 |
| 299 buffer.add('})();\n'); | 299 buffer.add('})();\n'); |
| 300 } | 300 } |
| 301 } | 301 } |
| 302 | 302 |
| 303 bool isSupertypeOfNativeClass(Element element) { | 303 bool isSupertypeOfNativeClass(Element element) { |
| 304 if (element.isTypeVariable()) { | 304 if (element.isTypeVariable()) { |
| 305 compiler.cancel("Is check for type variable", element: work.element); | 305 compiler.cancel("Is check for type variable", element: element); |
| 306 return false; | 306 return false; |
| 307 } | 307 } |
| 308 if (element.computeType(compiler) is FunctionType) return false; | 308 if (element.computeType(compiler) is FunctionType) return false; |
| 309 | 309 |
| 310 if (!element.isClass()) { | 310 if (!element.isClass()) { |
| 311 compiler.cancel("Is check does not handle element", element: element); | 311 compiler.cancel("Is check does not handle element", element: element); |
| 312 return false; | 312 return false; |
| 313 } | 313 } |
| 314 | 314 |
| 315 return subtypes[element] !== null; | 315 return subtypes[element] !== null; |
| 316 } | 316 } |
| 317 | 317 |
| 318 bool requiresNativeIsCheck(Element element) { | 318 bool requiresNativeIsCheck(Element element) { |
| 319 if (!element.isClass()) return false; | 319 if (!element.isClass()) return false; |
| 320 ClassElement cls = element; | 320 ClassElement cls = element; |
| 321 if (cls.isNative()) return true; | 321 if (cls.isNative()) return true; |
| 322 return isSupertypeOfNativeClass(element); | 322 return isSupertypeOfNativeClass(element); |
| 323 } | 323 } |
| 324 | 324 |
| 325 void emitIsChecks(StringBuffer buffer) { | 325 void emitIsChecks(StringBuffer checkBuffer) { |
| 326 for (Element type in compiler.universe.isChecks) { | 326 for (Element type in compiler.universe.isChecks) { |
| 327 if (!requiresNativeIsCheck(type)) continue; | 327 if (!requiresNativeIsCheck(type)) continue; |
| 328 String name = compiler.namer.operatorIs(type); | 328 String name = compiler.namer.operatorIs(type); |
| 329 buffer.add("$defPropName(Object.prototype, '$name', "); | 329 checkBuffer.add("$defPropName(Object.prototype, '$name', "); |
| 330 buffer.add('function() { return false; });\n'); | 330 checkBuffer.add('function() { return false; });\n'); |
| 331 } | 331 } |
| 332 } | 332 } |
| 333 | 333 |
| 334 void assembleCode(StringBuffer targetBuffer) { | 334 void assembleCode(StringBuffer targetBuffer) { |
| 335 if (nativeClasses.isEmpty()) return; | 335 if (nativeClasses.isEmpty()) return; |
| 336 | 336 |
| 337 // Because of native classes, we have to generate some is checks | 337 // Because of native classes, we have to generate some is checks |
| 338 // by calling a method, instead of accessing a property. So we | 338 // by calling a method, instead of accessing a property. So we |
| 339 // attach to the JS Object prototype these methods that return | 339 // attach to the JS Object prototype these methods that return |
| 340 // false, and will be overridden by subclasses when they have to | 340 // false, and will be overridden by subclasses when they have to |
| 341 // return true. | 341 // return true. |
| 342 StringBuffer objectProperties = new StringBuffer(); | 342 StringBuffer objectProperties = new StringBuffer(); |
| 343 emitIsChecks(objectProperties); | 343 emitIsChecks(objectProperties); |
| 344 | 344 |
| 345 // In order to have the toString method on every native class, | 345 // In order to have the toString method on every native class, |
| 346 // we must patch the JS Object prototype with a helper method. | 346 // we must patch the JS Object prototype with a helper method. |
| 347 String toStringName = compiler.namer.instanceMethodName( | 347 String toStringName = compiler.namer.instanceMethodName( |
| 348 null, const SourceString('toString'), 0); | 348 null, const SourceString('toString'), 0); |
| 349 objectProperties.add("$defPropName(Object.prototype, '$toStringName', "); | 349 objectProperties.add("$defPropName(Object.prototype, '$toStringName', "); |
| 350 objectProperties.add( | 350 objectProperties.add( |
| 351 'function() { return $toStringHelperName(this); });\n'); | 351 'function() { return $toStringHelperName(this); });\n'); |
| 352 | 352 |
| 353 // Finally, emit the code in the main buffer. | 353 // Finally, emit the code in the main buffer. |
| 354 targetBuffer.add('(function() {\n$objectProperties$buffer\n})();\n'); | 354 targetBuffer.add('(function() {\n$objectProperties$buffer\n})();\n'); |
| 355 } | 355 } |
| 356 } | 356 } |
| OLD | NEW |