| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 for (var method in desc) { | 103 for (var method in desc) { |
| 104 if (method) { """/* Short version of: if (method != '') */""" | 104 if (method) { """/* Short version of: if (method != '') */""" |
| 105 if (hasOwnProperty.call(desc, method)) { | 105 if (hasOwnProperty.call(desc, method)) { |
| 106 $dynamicName(method)[cls] = desc[method]; | 106 $dynamicName(method)[cls] = desc[method]; |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 }"""; | 110 }"""; |
| 111 } | 111 } |
| 112 | 112 |
| 113 void generateNativeLiteral(ClassElement classElement) { | |
| 114 String quotedNative = classElement.nativeTagInfo.slowToString(); | |
| 115 String nativeCode = quotedNative.substring(2, quotedNative.length - 1); | |
| 116 String className = backend.namer.getName(classElement); | |
| 117 nativeBuffer.add(className); | |
| 118 nativeBuffer.add('$_=$_'); | |
| 119 nativeBuffer.add(nativeCode); | |
| 120 nativeBuffer.add('$N'); | |
| 121 | |
| 122 void defineInstanceMember(String name, CodeBuffer value) { | |
| 123 nativeBuffer.add("$className.$name$_=$_$value$N"); | |
| 124 } | |
| 125 | |
| 126 classElement.implementation.forEachMember((_, Element member) { | |
| 127 if (member.isInstanceMember()) { | |
| 128 emitter.addInstanceMember(member, defineInstanceMember); | |
| 129 } | |
| 130 }); | |
| 131 } | |
| 132 | |
| 133 bool isNativeLiteral(String quotedName) { | |
| 134 return identical(quotedName[1], '='); | |
| 135 } | |
| 136 | |
| 137 bool isNativeGlobal(String quotedName) { | 113 bool isNativeGlobal(String quotedName) { |
| 138 return identical(quotedName[1], '@'); | 114 return identical(quotedName[1], '@'); |
| 139 } | 115 } |
| 140 | 116 |
| 141 String toNativeTag(ClassElement cls) { | 117 String toNativeTag(ClassElement cls) { |
| 142 String quotedName = cls.nativeTagInfo.slowToString(); | 118 String quotedName = cls.nativeTagInfo.slowToString(); |
| 143 if (isNativeGlobal(quotedName)) { | 119 if (isNativeGlobal(quotedName)) { |
| 144 // Global object, just be like the other types for now. | 120 // Global object, just be like the other types for now. |
| 145 return quotedName.substring(3, quotedName.length - 1); | 121 return quotedName.substring(3, quotedName.length - 1); |
| 146 } else { | 122 } else { |
| 147 return quotedName.substring(2, quotedName.length - 1); | 123 return quotedName.substring(2, quotedName.length - 1); |
| 148 } | 124 } |
| 149 } | 125 } |
| 150 | 126 |
| 151 void generateNativeClass(ClassElement classElement) { | 127 void generateNativeClass(ClassElement classElement) { |
| 152 nativeClasses.add(classElement); | 128 nativeClasses.add(classElement); |
| 153 | 129 |
| 154 assert(classElement.backendMembers.isEmpty); | 130 assert(classElement.backendMembers.isEmpty); |
| 155 String quotedName = classElement.nativeTagInfo.slowToString(); | 131 String quotedName = classElement.nativeTagInfo.slowToString(); |
| 156 if (isNativeLiteral(quotedName)) { | |
| 157 generateNativeLiteral(classElement); | |
| 158 // The native literal kind needs to be dealt with specially when | |
| 159 // generating code for it. | |
| 160 return; | |
| 161 } | |
| 162 | 132 |
| 163 CodeBuffer fieldBuffer = new CodeBuffer(); | 133 CodeBuffer fieldBuffer = new CodeBuffer(); |
| 164 CodeBuffer getterSetterBuffer = new CodeBuffer(); | 134 CodeBuffer getterSetterBuffer = new CodeBuffer(); |
| 165 CodeBuffer methodBuffer = new CodeBuffer(); | 135 CodeBuffer methodBuffer = new CodeBuffer(); |
| 166 | 136 |
| 167 emitter.emitClassFields(classElement, fieldBuffer, false, | 137 emitter.emitClassFields(classElement, fieldBuffer, false, |
| 168 classIsNative: true); | 138 classIsNative: true); |
| 169 emitter.emitClassGettersSetters(classElement, getterSetterBuffer, false); | 139 emitter.emitClassGettersSetters(classElement, getterSetterBuffer, false); |
| 170 emitter.emitInstanceMembers(classElement, methodBuffer, false); | 140 emitter.emitInstanceMembers(classElement, methodBuffer, false); |
| 171 | 141 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 // When calling a JS method, we call it with the native name, and only the | 242 // When calling a JS method, we call it with the native name, and only the |
| 273 // arguments up until the last one provided. | 243 // arguments up until the last one provided. |
| 274 target = member.fixedBackendName(); | 244 target = member.fixedBackendName(); |
| 275 arguments = argumentsBuffer.getRange( | 245 arguments = argumentsBuffer.getRange( |
| 276 0, indexOfLastOptionalArgumentInParameters + 1); | 246 0, indexOfLastOptionalArgumentInParameters + 1); |
| 277 } | 247 } |
| 278 statements.add( | 248 statements.add( |
| 279 new js.Return( | 249 new js.Return( |
| 280 new js.VariableUse('this').dot(target).callWith(arguments))); | 250 new js.VariableUse('this').dot(target).callWith(arguments))); |
| 281 | 251 |
| 282 if (isNativeLiteral(nativeTagInfo) || !overriddenMethods.contains(member)) { | 252 if (!overriddenMethods.contains(member)) { |
| 283 // Call the method directly. | 253 // Call the method directly. |
| 284 return statements; | 254 return statements; |
| 285 } else { | 255 } else { |
| 286 return <js.Statement>[ | 256 return <js.Statement>[ |
| 287 generateMethodBodyWithPrototypeCheck( | 257 generateMethodBodyWithPrototypeCheck( |
| 288 invocationName, new js.Block(statements), stubParameters)]; | 258 invocationName, new js.Block(statements), stubParameters)]; |
| 289 } | 259 } |
| 290 } | 260 } |
| 291 | 261 |
| 292 // If a method is overridden, we must check if the prototype of 'this' has the | 262 // If a method is overridden, we must check if the prototype of 'this' has the |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 if (!first) targetBuffer.add(",\n"); | 549 if (!first) targetBuffer.add(",\n"); |
| 580 targetBuffer.add("$_$name:$_$function"); | 550 targetBuffer.add("$_$name:$_$function"); |
| 581 first = false; | 551 first = false; |
| 582 }); | 552 }); |
| 583 targetBuffer.add("\n})$N$n"); | 553 targetBuffer.add("\n})$N$n"); |
| 584 } | 554 } |
| 585 targetBuffer.add(nativeBuffer); | 555 targetBuffer.add(nativeBuffer); |
| 586 targetBuffer.add('\n'); | 556 targetBuffer.add('\n'); |
| 587 } | 557 } |
| 588 } | 558 } |
| OLD | NEW |