OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 import '../common.dart'; | 5 import '../common.dart'; |
6 import '../compiler.dart' show Compiler; | |
7 import '../constants/values.dart'; | 6 import '../constants/values.dart'; |
8 import '../elements/resolution_types.dart'; | 7 import '../elements/resolution_types.dart'; |
9 import '../elements/elements.dart'; | 8 import '../elements/elements.dart'; |
10 import '../js/js.dart' as js; | 9 import '../js/js.dart' as js; |
11 import '../js_backend/js_backend.dart'; | 10 import '../js_backend/js_backend.dart'; |
12 import '../js_emitter/js_emitter.dart' show NativeEmitter; | 11 import '../js_emitter/js_emitter.dart' show NativeEmitter; |
13 import '../ssa/builder.dart' show SsaBuilder; | 12 import '../ssa/builder.dart' show SsaBuilder; |
14 import '../ssa/nodes.dart' show HInstruction, HForeignCode, HReturn; | 13 import '../ssa/nodes.dart' show HInstruction, HForeignCode, HReturn; |
15 import '../tree/tree.dart'; | 14 import '../tree/tree.dart'; |
16 import '../universe/side_effects.dart' show SideEffects; | 15 import '../universe/side_effects.dart' show SideEffects; |
17 | 16 |
18 final RegExp nativeRedirectionRegExp = new RegExp(r'^[a-zA-Z][a-zA-Z_$0-9]*$'); | 17 final RegExp nativeRedirectionRegExp = new RegExp(r'^[a-zA-Z][a-zA-Z_$0-9]*$'); |
19 | 18 |
20 void handleSsaNative(SsaBuilder builder, Expression nativeBody) { | 19 void handleSsaNative(SsaBuilder builder, Expression nativeBody) { |
21 Compiler compiler = builder.compiler; | |
22 MethodElement element = builder.target; | 20 MethodElement element = builder.target; |
23 NativeEmitter nativeEmitter = builder.nativeEmitter; | 21 NativeEmitter nativeEmitter = builder.nativeEmitter; |
24 JavaScriptBackend backend = builder.backend; | 22 JavaScriptBackend backend = builder.backend; |
25 DiagnosticReporter reporter = compiler.reporter; | |
26 | 23 |
27 HInstruction convertDartClosure( | 24 HInstruction convertDartClosure( |
28 ParameterElement parameter, ResolutionFunctionType type) { | 25 ParameterElement parameter, ResolutionFunctionType type) { |
29 HInstruction local = builder.localsHandler.readLocal(parameter); | 26 HInstruction local = builder.localsHandler.readLocal(parameter); |
30 ConstantValue arityConstant = | 27 ConstantValue arityConstant = |
31 builder.constantSystem.createInt(type.parameterTypes.length); | 28 builder.constantSystem.createInt(type.parameterTypes.length); |
32 HInstruction arity = | 29 HInstruction arity = |
33 builder.graph.addConstant(arityConstant, builder.closedWorld); | 30 builder.graph.addConstant(arityConstant, builder.closedWorld); |
34 // TODO(ngeoffray): For static methods, we could pass a method with a | 31 // TODO(ngeoffray): For static methods, we could pass a method with a |
35 // defined arity. | 32 // defined arity. |
(...skipping 10 matching lines...) Expand all Loading... |
46 // No longer supported, this is now done with @JSName('foo') and case 1. | 43 // No longer supported, this is now done with @JSName('foo') and case 1. |
47 // 3) foo() native "return 42"; | 44 // 3) foo() native "return 42"; |
48 // hasBody = true | 45 // hasBody = true |
49 bool hasBody = false; | 46 bool hasBody = false; |
50 assert(backend.nativeData.isNativeMember(element)); | 47 assert(backend.nativeData.isNativeMember(element)); |
51 String nativeMethodName = backend.nativeData.getFixedBackendName(element); | 48 String nativeMethodName = backend.nativeData.getFixedBackendName(element); |
52 if (nativeBody != null) { | 49 if (nativeBody != null) { |
53 LiteralString jsCode = nativeBody.asLiteralString(); | 50 LiteralString jsCode = nativeBody.asLiteralString(); |
54 String str = jsCode.dartString.slowToString(); | 51 String str = jsCode.dartString.slowToString(); |
55 if (nativeRedirectionRegExp.hasMatch(str)) { | 52 if (nativeRedirectionRegExp.hasMatch(str)) { |
56 reporter.internalError( | 53 throw new SpannableAssertionFailure( |
57 nativeBody, "Deprecated syntax, use @JSName('name') instead."); | 54 nativeBody, "Deprecated syntax, use @JSName('name') instead."); |
58 } | 55 } |
59 hasBody = true; | 56 hasBody = true; |
60 } | 57 } |
61 | 58 |
62 if (!hasBody) { | 59 if (!hasBody) { |
63 nativeEmitter.nativeMethods.add(element); | 60 nativeEmitter.nativeMethods.add(element); |
64 } | 61 } |
65 | 62 |
66 FunctionSignature parameters = element.functionSignature; | 63 FunctionSignature parameters = element.functionSignature; |
(...skipping 19 matching lines...) Expand all Loading... |
86 | 83 |
87 String foreignParameters = arguments.join(','); | 84 String foreignParameters = arguments.join(','); |
88 String nativeMethodCall; | 85 String nativeMethodCall; |
89 if (element.kind == ElementKind.FUNCTION) { | 86 if (element.kind == ElementKind.FUNCTION) { |
90 nativeMethodCall = '$receiver$nativeMethodName($foreignParameters)'; | 87 nativeMethodCall = '$receiver$nativeMethodName($foreignParameters)'; |
91 } else if (element.kind == ElementKind.GETTER) { | 88 } else if (element.kind == ElementKind.GETTER) { |
92 nativeMethodCall = '$receiver$nativeMethodName'; | 89 nativeMethodCall = '$receiver$nativeMethodName'; |
93 } else if (element.kind == ElementKind.SETTER) { | 90 } else if (element.kind == ElementKind.SETTER) { |
94 nativeMethodCall = '$receiver$nativeMethodName = $foreignParameters'; | 91 nativeMethodCall = '$receiver$nativeMethodName = $foreignParameters'; |
95 } else { | 92 } else { |
96 builder.reporter | 93 throw new SpannableAssertionFailure( |
97 .internalError(element, 'Unexpected kind: "${element.kind}".'); | 94 element, 'Unexpected kind: "${element.kind}".'); |
98 } | 95 } |
99 | 96 |
100 builder.push(new HForeignCode( | 97 builder.push(new HForeignCode( |
101 // TODO(sra): This could be cached. The number of templates should | 98 // TODO(sra): This could be cached. The number of templates should |
102 // be proportional to the number of native methods, which is bounded | 99 // be proportional to the number of native methods, which is bounded |
103 // by the dart: libraries. | 100 // by the dart: libraries. |
104 js.js.uncachedExpressionTemplate(nativeMethodCall), | 101 js.js.uncachedExpressionTemplate(nativeMethodCall), |
105 builder.commonMasks.dynamicType, | 102 builder.commonMasks.dynamicType, |
106 inputs, | 103 inputs, |
107 effects: new SideEffects())); | 104 effects: new SideEffects())); |
108 // TODO(johnniwinther): Provide source information. | 105 // TODO(johnniwinther): Provide source information. |
109 builder | 106 builder |
110 .close(new HReturn(builder.pop(), null)) | 107 .close(new HReturn(builder.pop(), null)) |
111 .addSuccessor(builder.graph.exit); | 108 .addSuccessor(builder.graph.exit); |
112 } else { | 109 } else { |
113 if (parameters.parameterCount != 0) { | 110 if (parameters.parameterCount != 0) { |
114 reporter.internalError( | 111 throw new SpannableAssertionFailure( |
115 nativeBody, | 112 nativeBody, |
116 'native "..." syntax is restricted to ' | 113 'native "..." syntax is restricted to ' |
117 'functions with zero parameters.'); | 114 'functions with zero parameters.'); |
118 } | 115 } |
119 LiteralString jsCode = nativeBody.asLiteralString(); | 116 LiteralString jsCode = nativeBody.asLiteralString(); |
120 builder.push(new HForeignCode.statement( | 117 builder.push(new HForeignCode.statement( |
121 js.js.statementTemplateYielding( | 118 js.js.statementTemplateYielding( |
122 new js.LiteralStatement(jsCode.dartString.slowToString())), | 119 new js.LiteralStatement(jsCode.dartString.slowToString())), |
123 <HInstruction>[], | 120 <HInstruction>[], |
124 new SideEffects(), | 121 new SideEffects(), |
125 null, | 122 null, |
126 builder.commonMasks.dynamicType)); | 123 builder.commonMasks.dynamicType)); |
127 } | 124 } |
128 } | 125 } |
OLD | NEW |