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 '../constants/values.dart'; | 6 import '../constants/values.dart'; |
7 import '../elements/resolution_types.dart'; | 7 import '../elements/resolution_types.dart'; |
8 import '../elements/elements.dart'; | 8 import '../elements/elements.dart'; |
9 import '../js/js.dart' as js; | 9 import '../js/js.dart' as js; |
10 import '../js_backend/js_backend.dart'; | |
11 import '../js_emitter/js_emitter.dart' show NativeEmitter; | 10 import '../js_emitter/js_emitter.dart' show NativeEmitter; |
12 import '../ssa/builder.dart' show SsaBuilder; | 11 import '../ssa/builder.dart' show SsaBuilder; |
13 import '../ssa/nodes.dart' show HInstruction, HForeignCode, HReturn; | 12 import '../ssa/nodes.dart' show HInstruction, HForeignCode, HReturn; |
14 import '../tree/tree.dart'; | 13 import '../tree/tree.dart'; |
15 import '../universe/side_effects.dart' show SideEffects; | 14 import '../universe/side_effects.dart' show SideEffects; |
16 | 15 |
17 final RegExp nativeRedirectionRegExp = new RegExp(r'^[a-zA-Z][a-zA-Z_$0-9]*$'); | 16 final RegExp nativeRedirectionRegExp = new RegExp(r'^[a-zA-Z][a-zA-Z_$0-9]*$'); |
18 | 17 |
19 void handleSsaNative(SsaBuilder builder, Expression nativeBody) { | 18 void handleSsaNative(SsaBuilder builder, Expression nativeBody) { |
20 MethodElement element = builder.target; | 19 MethodElement element = builder.target; |
21 NativeEmitter nativeEmitter = builder.nativeEmitter; | 20 NativeEmitter nativeEmitter = builder.nativeEmitter; |
22 JavaScriptBackend backend = builder.backend; | |
23 | 21 |
24 HInstruction convertDartClosure( | 22 HInstruction convertDartClosure( |
25 ParameterElement parameter, ResolutionFunctionType type) { | 23 ParameterElement parameter, ResolutionFunctionType type) { |
26 HInstruction local = builder.localsHandler.readLocal(parameter); | 24 HInstruction local = builder.localsHandler.readLocal(parameter); |
27 ConstantValue arityConstant = | 25 ConstantValue arityConstant = |
28 builder.constantSystem.createInt(type.parameterTypes.length); | 26 builder.constantSystem.createInt(type.parameterTypes.length); |
29 HInstruction arity = | 27 HInstruction arity = |
30 builder.graph.addConstant(arityConstant, builder.closedWorld); | 28 builder.graph.addConstant(arityConstant, builder.closedWorld); |
31 // TODO(ngeoffray): For static methods, we could pass a method with a | 29 // TODO(ngeoffray): For static methods, we could pass a method with a |
32 // defined arity. | 30 // defined arity. |
33 MethodElement helper = backend.helpers.closureConverter; | 31 MethodElement helper = builder.helpers.closureConverter; |
34 builder.pushInvokeStatic(nativeBody, helper, [local, arity]); | 32 builder.pushInvokeStatic(nativeBody, helper, [local, arity]); |
35 HInstruction closure = builder.pop(); | 33 HInstruction closure = builder.pop(); |
36 return closure; | 34 return closure; |
37 } | 35 } |
38 | 36 |
39 // Check which pattern this native method follows: | 37 // Check which pattern this native method follows: |
40 // 1) foo() native; | 38 // 1) foo() native; |
41 // hasBody = false | 39 // hasBody = false |
42 // 2) foo() native "bar"; | 40 // 2) foo() native "bar"; |
43 // No longer supported, this is now done with @JSName('foo') and case 1. | 41 // No longer supported, this is now done with @JSName('foo') and case 1. |
44 // 3) foo() native "return 42"; | 42 // 3) foo() native "return 42"; |
45 // hasBody = true | 43 // hasBody = true |
46 bool hasBody = false; | 44 bool hasBody = false; |
47 assert(backend.nativeData.isNativeMember(element)); | 45 assert(builder.nativeData.isNativeMember(element)); |
48 String nativeMethodName = backend.nativeData.getFixedBackendName(element); | 46 String nativeMethodName = builder.nativeData.getFixedBackendName(element); |
49 if (nativeBody != null) { | 47 if (nativeBody != null) { |
50 LiteralString jsCode = nativeBody.asLiteralString(); | 48 LiteralString jsCode = nativeBody.asLiteralString(); |
51 String str = jsCode.dartString.slowToString(); | 49 String str = jsCode.dartString.slowToString(); |
52 if (nativeRedirectionRegExp.hasMatch(str)) { | 50 if (nativeRedirectionRegExp.hasMatch(str)) { |
53 throw new SpannableAssertionFailure( | 51 throw new SpannableAssertionFailure( |
54 nativeBody, "Deprecated syntax, use @JSName('name') instead."); | 52 nativeBody, "Deprecated syntax, use @JSName('name') instead."); |
55 } | 53 } |
56 hasBody = true; | 54 hasBody = true; |
57 } | 55 } |
58 | 56 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 LiteralString jsCode = nativeBody.asLiteralString(); | 114 LiteralString jsCode = nativeBody.asLiteralString(); |
117 builder.push(new HForeignCode.statement( | 115 builder.push(new HForeignCode.statement( |
118 js.js.statementTemplateYielding( | 116 js.js.statementTemplateYielding( |
119 new js.LiteralStatement(jsCode.dartString.slowToString())), | 117 new js.LiteralStatement(jsCode.dartString.slowToString())), |
120 <HInstruction>[], | 118 <HInstruction>[], |
121 new SideEffects(), | 119 new SideEffects(), |
122 null, | 120 null, |
123 builder.commonMasks.dynamicType)); | 121 builder.commonMasks.dynamicType)); |
124 } | 122 } |
125 } | 123 } |
OLD | NEW |