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 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 ClassElement cls, | 169 ClassElement cls, |
170 NativeEmitter nativeEmitter) { | 170 NativeEmitter nativeEmitter) { |
171 List<ClassElement> subtypes = nativeEmitter.subtypes[cls]; | 171 List<ClassElement> subtypes = nativeEmitter.subtypes[cls]; |
172 if (subtypes == null) return false; | 172 if (subtypes == null) return false; |
173 for (ClassElement subtype in subtypes) { | 173 for (ClassElement subtype in subtypes) { |
174 if (subtype.lookupLocalMember(element.name) != null) return true; | 174 if (subtype.lookupLocalMember(element.name) != null) return true; |
175 } | 175 } |
176 return false; | 176 return false; |
177 } | 177 } |
178 | 178 |
| 179 final RegExp nativeRedirectionRegExp = new RegExp(r'^[a-zA-Z][a-zA-Z_$0-9]*$'); |
| 180 |
179 void handleSsaNative(SsaBuilder builder, Expression nativeBody) { | 181 void handleSsaNative(SsaBuilder builder, Expression nativeBody) { |
180 Compiler compiler = builder.compiler; | 182 Compiler compiler = builder.compiler; |
181 FunctionElement element = builder.work.element; | 183 FunctionElement element = builder.work.element; |
182 element.setNative(); | 184 element.setNative(); |
183 NativeEmitter nativeEmitter = builder.emitter.nativeEmitter; | 185 NativeEmitter nativeEmitter = builder.emitter.nativeEmitter; |
184 // If what we're compiling is a getter named 'typeName' and the native | 186 // If what we're compiling is a getter named 'typeName' and the native |
185 // class is named 'DOMType', we generate a call to the typeNameOf | 187 // class is named 'DOMType', we generate a call to the typeNameOf |
186 // function attached on the isolate. | 188 // function attached on the isolate. |
187 // The DOM classes assume that their 'typeName' property, which is | 189 // The DOM classes assume that their 'typeName' property, which is |
188 // not a JS property on the DOM types, returns the type name. | 190 // not a JS property on the DOM types, returns the type name. |
(...skipping 16 matching lines...) Expand all Loading... |
205 Element helper = builder.interceptors.getClosureConverter(); | 207 Element helper = builder.interceptors.getClosureConverter(); |
206 builder.pushInvokeHelper2(helper, local, arity); | 208 builder.pushInvokeHelper2(helper, local, arity); |
207 HInstruction closure = builder.pop(); | 209 HInstruction closure = builder.pop(); |
208 return closure; | 210 return closure; |
209 } | 211 } |
210 | 212 |
211 // Check which pattern this native method follows: | 213 // Check which pattern this native method follows: |
212 // 1) foo() native; hasBody = false, isRedirecting = false | 214 // 1) foo() native; hasBody = false, isRedirecting = false |
213 // 2) foo() native "bar"; hasBody = false, isRedirecting = true | 215 // 2) foo() native "bar"; hasBody = false, isRedirecting = true |
214 // 3) foo() native "return 42"; hasBody = true, isRedirecting = false | 216 // 3) foo() native "return 42"; hasBody = true, isRedirecting = false |
215 RegExp nativeRedirectionRegExp = const RegExp(r'^[a-zA-Z][a-zA-Z_$0-9]*$'); | |
216 bool hasBody = false; | 217 bool hasBody = false; |
217 bool isRedirecting = false; | 218 bool isRedirecting = false; |
218 String nativeMethodName = element.name.slowToString(); | 219 String nativeMethodName = element.name.slowToString(); |
219 if (nativeBody != null) { | 220 if (nativeBody != null) { |
220 LiteralString jsCode = nativeBody.asLiteralString(); | 221 LiteralString jsCode = nativeBody.asLiteralString(); |
221 String str = jsCode.dartString.slowToString(); | 222 String str = jsCode.dartString.slowToString(); |
222 if (nativeRedirectionRegExp.hasMatch(str)) { | 223 if (nativeRedirectionRegExp.hasMatch(str)) { |
223 nativeMethodName = str; | 224 nativeMethodName = str; |
224 isRedirecting = true; | 225 isRedirecting = true; |
225 nativeEmitter.addRedirectingMethod(element, nativeMethodName); | 226 nativeEmitter.addRedirectingMethod(element, nativeMethodName); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 String parameters) { | 333 String parameters) { |
333 buffer.add(" if (Object.getPrototypeOf(this).hasOwnProperty"); | 334 buffer.add(" if (Object.getPrototypeOf(this).hasOwnProperty"); |
334 buffer.add("('$methodName')) {\n"); | 335 buffer.add("('$methodName')) {\n"); |
335 buffer.add(" $code"); | 336 buffer.add(" $code"); |
336 buffer.add(" } else {\n"); | 337 buffer.add(" } else {\n"); |
337 buffer.add(" return Object.prototype.$methodName.call(this"); | 338 buffer.add(" return Object.prototype.$methodName.call(this"); |
338 buffer.add(parameters == '' ? '' : ', $parameters'); | 339 buffer.add(parameters == '' ? '' : ', $parameters'); |
339 buffer.add(");\n"); | 340 buffer.add(");\n"); |
340 buffer.add(" }\n"); | 341 buffer.add(" }\n"); |
341 } | 342 } |
OLD | NEW |