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'; |
(...skipping 791 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
802 builder.add(new HForeign( | 802 builder.add(new HForeign( |
803 new DartString.literal('${parameter.name.slowToString()} = #'), | 803 new DartString.literal('${parameter.name.slowToString()} = #'), |
804 const LiteralDartString('void'), | 804 const LiteralDartString('void'), |
805 <HInstruction>[jsClosure])); | 805 <HInstruction>[jsClosure])); |
806 } | 806 } |
807 }); | 807 }); |
808 LiteralString jsCode = nativeBody.asLiteralString(); | 808 LiteralString jsCode = nativeBody.asLiteralString(); |
809 builder.push(new HForeign.statement(jsCode.dartString, <HInstruction>[])); | 809 builder.push(new HForeign.statement(jsCode.dartString, <HInstruction>[])); |
810 } | 810 } |
811 } | 811 } |
812 | |
813 void generateMethodWithPrototypeCheckForElement(Compiler compiler, | |
814 StringBuffer buffer, | |
815 FunctionElement element, | |
816 String code, | |
817 String parameters) { | |
818 String methodName; | |
819 JavaScriptBackend backend = compiler.backend; | |
820 Namer namer = backend.namer; | |
821 if (element.kind == ElementKind.FUNCTION) { | |
822 methodName = namer.instanceMethodName(element); | |
823 } else if (element.kind == ElementKind.GETTER) { | |
824 methodName = namer.getterName(element.getLibrary(), element.name); | |
825 } else if (element.kind == ElementKind.SETTER) { | |
826 methodName = namer.setterName(element.getLibrary(), element.name); | |
827 } else { | |
828 compiler.internalError('unexpected kind: "${element.kind}"', | |
829 element: element); | |
830 } | |
831 | |
832 generateMethodWithPrototypeCheck( | |
833 compiler, buffer, methodName, code, parameters); | |
834 } | |
835 | |
836 | |
837 // If a method is overridden, we must check if the prototype of | |
838 // 'this' has the method available. Otherwise, we may end up | |
839 // calling the method from the super class. If the method is not | |
840 // available, we make a direct call to Object.prototype.$methodName. | |
841 // This method will patch the prototype of 'this' to the real method. | |
842 void generateMethodWithPrototypeCheck(Compiler compiler, | |
843 StringBuffer buffer, | |
844 String methodName, | |
845 String code, | |
846 String parameters) { | |
847 buffer.add(" if (Object.getPrototypeOf(this).hasOwnProperty"); | |
848 buffer.add("('$methodName')) {\n"); | |
849 buffer.add(" $code"); | |
850 buffer.add(" } else {\n"); | |
851 buffer.add(" return Object.prototype.$methodName.call(this"); | |
852 buffer.add(parameters == '' ? '' : ', $parameters'); | |
853 buffer.add(");\n"); | |
854 buffer.add(" }\n"); | |
855 } | |
OLD | NEW |