OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** | 5 /** |
6 * Support for interoperating with JavaScript. | 6 * Support for interoperating with JavaScript. |
7 * | 7 * |
8 * This library provides access to JavaScript objects from Dart, allowing | 8 * This library provides access to JavaScript objects from Dart, allowing |
9 * Dart code to get and set properties, and call methods of JavaScript objects | 9 * Dart code to get and set properties, and call methods of JavaScript objects |
10 * and invoke JavaScript functions. The library takes care of converting | 10 * and invoke JavaScript functions. The library takes care of converting |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 if (declaration is mirrors.MethodMirror && declaration.isSetter) { | 303 if (declaration is mirrors.MethodMirror && declaration.isSetter) { |
304 assert(name.endsWith("=")); | 304 assert(name.endsWith("=")); |
305 name = name.substring(0, name.length - 1); | 305 name = name.substring(0, name.length - 1); |
306 } | 306 } |
307 return name; | 307 return name; |
308 } | 308 } |
309 | 309 |
310 final _JS_LIBRARY_PREFIX = "js_library"; | 310 final _JS_LIBRARY_PREFIX = "js_library"; |
311 final _UNDEFINED_VAR = "_UNDEFINED_JS_CONST"; | 311 final _UNDEFINED_VAR = "_UNDEFINED_JS_CONST"; |
312 | 312 |
313 String _accessJsPath(String path) { | 313 String _accessJsPath(String path) => _accessJsPathHelper(path.split(".")); |
314 var parts = path.split("."); | 314 |
| 315 String _accessJsPathHelper(Iterable<String> parts) { |
315 var sb = new StringBuffer(); | 316 var sb = new StringBuffer(); |
316 sb | 317 sb |
317 ..write('${_JS_LIBRARY_PREFIX}.JsNative.getProperty(' * parts.length) | 318 ..write('${_JS_LIBRARY_PREFIX}.JsNative.getProperty(' * parts.length) |
318 ..write("${_JS_LIBRARY_PREFIX}.context"); | 319 ..write("${_JS_LIBRARY_PREFIX}.context"); |
319 for (var p in parts) { | 320 for (var p in parts) { |
320 sb.write(", '$p')"); | 321 sb.write(", '$p')"); |
321 } | 322 } |
322 return sb.toString(); | 323 return sb.toString(); |
323 } | 324 } |
324 | 325 |
325 | |
326 String _accessJsPathSetter(String path) { | 326 String _accessJsPathSetter(String path) { |
327 var parts = path.split("."); | 327 var parts = path.split("."); |
328 return "${_JS_LIBRARY_PREFIX}.JsNative.setProperty(${_accessJsPath(parts.getRa
nge(0, parts.length - 1).join('.')) | 328 return "${_JS_LIBRARY_PREFIX}.JsNative.setProperty(${_accessJsPathHelper(parts
.getRange(0, parts.length - 1)) |
329 }, '{parts.end}', v)"; | 329 }, '${parts.last}', v)"; |
330 } | 330 } |
331 | 331 |
332 @Deprecated("Internal Use Only") | 332 @Deprecated("Internal Use Only") |
333 void addMemberHelper( | 333 void addMemberHelper( |
334 mirrors.MethodMirror declaration, String path, StringBuffer sb, | 334 mirrors.MethodMirror declaration, String path, StringBuffer sb, |
335 {bool isStatic: false, String memberName}) { | 335 {bool isStatic: false, String memberName}) { |
336 if (!declaration.isConstructor) { | 336 if (!declaration.isConstructor) { |
337 var jsName = _getJsMemberName(declaration); | 337 var jsName = _getJsMemberName(declaration); |
338 path = (path != null && path.isNotEmpty) ? "${path}.${jsName}" : jsName; | 338 path = (path != null && path.isNotEmpty) ? "${path}.${jsName}" : jsName; |
339 } | 339 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 ..write("[${args.join(",")}]"); | 388 ..write("[${args.join(",")}]"); |
389 | 389 |
390 if (hasOptional) { | 390 if (hasOptional) { |
391 sb.write(".takeWhile((i) => i != ${_UNDEFINED_VAR}).toList()"); | 391 sb.write(".takeWhile((i) => i != ${_UNDEFINED_VAR}).toList()"); |
392 } | 392 } |
393 sb.write("));"); | 393 sb.write("));"); |
394 } | 394 } |
395 sb.write("\n"); | 395 sb.write("\n"); |
396 } | 396 } |
397 | 397 |
398 // TODO(jacobr): make this check more robust. | 398 bool _isExternal(mirrors.MethodMirror mirror) { |
399 bool _isExternal(mirrors.Mirror mirror) { | 399 // This try-catch block is a workaround for BUG:24834. |
400 /* | 400 try { |
401 var source = mirror.source; | 401 return mirror.isExternal; |
402 return source != null && source.startsWith("external "); | 402 } catch (e) { } |
403 */ | 403 return false; |
404 return mirror.isExternal; | |
405 } | 404 } |
406 | 405 |
407 List<String> _generateExternalMethods() { | 406 List<String> _generateExternalMethods() { |
408 var staticCodegen = <String>[]; | 407 var staticCodegen = <String>[]; |
409 mirrors.currentMirrorSystem().libraries.forEach((uri, library) { | 408 mirrors.currentMirrorSystem().libraries.forEach((uri, library) { |
410 var sb = new StringBuffer(); | 409 var sb = new StringBuffer(); |
411 String jsLibraryName = _getJsName(library); | 410 String jsLibraryName = _getJsName(library); |
412 library.declarations.forEach((name, declaration) { | 411 library.declarations.forEach((name, declaration) { |
413 if (declaration is mirrors.MethodMirror) { | 412 if (declaration is mirrors.MethodMirror) { |
414 if ((_hasJsName(declaration) || jsLibraryName != null) && | 413 if ((_hasJsName(declaration) || jsLibraryName != null) && |
(...skipping 985 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1400 return f; | 1399 return f; |
1401 } else { | 1400 } else { |
1402 var ret = _interopCaptureThisExpando[f]; | 1401 var ret = _interopCaptureThisExpando[f]; |
1403 if (ret == null) { | 1402 if (ret == null) { |
1404 ret = new JsFunction.withThis(f); | 1403 ret = new JsFunction.withThis(f); |
1405 _interopCaptureThisExpando[f] = ret; | 1404 _interopCaptureThisExpando[f] = ret; |
1406 } | 1405 } |
1407 return ret; | 1406 return ret; |
1408 } | 1407 } |
1409 } | 1408 } |
OLD | NEW |