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