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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 * var jsMap = new JsObject.jsify({'a': 1, 'b': 2}); | 82 * var jsMap = new JsObject.jsify({'a': 1, 'b': 2}); |
83 * | 83 * |
84 * This expression creates a JavaScript array: | 84 * This expression creates a JavaScript array: |
85 * | 85 * |
86 * var jsArray = new JsObject.jsify([1, 2, 3]); | 86 * var jsArray = new JsObject.jsify([1, 2, 3]); |
87 */ | 87 */ |
88 library dart.js; | 88 library dart.js; |
89 | 89 |
90 import 'dart:collection' show ListMixin; | 90 import 'dart:collection' show ListMixin; |
91 import 'dart:nativewrappers'; | 91 import 'dart:nativewrappers'; |
92 import 'dart:math' as math; | |
93 import 'dart:mirrors' as mirrors; | |
94 | |
95 // TODO(jacobr): if we care about unchecked mode in Dartium we need to set this | |
96 // to false in unchecked mode. | |
vsm
2015/06/22 22:27:48
That is still the default mode (unchecked).
Jacob
2015/06/24 22:21:58
switched this to an actual ugly test for checked m
| |
97 final bool _CHECK_JS_INVOCATIONS = true; | |
98 | |
99 final _allowedMethods = new Map<Symbol, _DeclarationSet>(); | |
100 final _allowedGetters = new Map<Symbol, _DeclarationSet>(); | |
101 final _allowedSetters = new Map<Symbol, _DeclarationSet>(); | |
102 | |
103 final Set<Type> _jsInterfaceTypes = new Set<Type>(); | |
vsm
2015/06/22 22:27:48
could drop the lhs type here.
Jacob
2015/06/24 22:21:58
Done.
| |
104 Iterable<Type> get jsInterfaceTypes => _jsInterfaceTypes.toList(); | |
vsm
2015/06/22 22:27:48
Do you need toList? I think a Set is an Iterable
Jacob
2015/06/24 22:21:58
Done.
| |
105 | |
106 class _DeclarationSet { | |
107 _DeclarationSet() : _members = <mirrors.DeclarationMirror>[]; | |
108 | |
109 bool _checkType(obj, type) { | |
vsm
2015/06/22 22:27:48
This could be static. Is type a Type or a TypeMir
Jacob
2015/06/24 22:21:58
Done.
| |
110 if (obj == null) return true; | |
111 return mirrors.reflectType(obj.runtimeType).isSubtypeOf(type); | |
112 } | |
113 | |
114 bool _checkReturnType(value) { | |
vsm
2015/06/22 22:27:48
Comment on what this function does?
Jacob
2015/06/24 22:21:58
Done.
| |
115 if (value == null) return true; | |
116 var valueMirror = mirrors.reflectType(value.runtimeType); | |
117 for (var member in _members) { | |
118 if (member is mirrors.VariableMirror || member.isGetter) { | |
119 // TODO(jacobr): actually check return types for getters that return | |
120 // function types. | |
121 return true; | |
122 } else { | |
123 if (valueMirror.isSubtypeOf(member.returnType)) return true; | |
124 } | |
125 } | |
126 return false; | |
127 } | |
128 | |
129 /** | |
130 * Check whether the [invocation] is consistent with the [member] mirror. | |
131 */ | |
132 bool _checkDeclaration( | |
133 Invocation invocation, mirrors.DeclarationMirror member) { | |
134 if (member is mirrors.VariableMirror || member.isGetter) { | |
135 // TODO(jacobr): actually check method types against the function type ret urned | |
vsm
2015/06/22 22:27:48
line len
Jacob
2015/06/24 22:21:58
Done.
| |
136 // by the getter or field. | |
137 return true; | |
138 } | |
139 var parameters = member.parameters; | |
140 var positionalArguments = invocation.positionalArguments; | |
141 // Too many arguments | |
142 if (parameters.length < positionalArguments.length) return false; | |
143 // Too few required arguments. | |
144 if (parameters.length > positionalArguments.length && | |
145 !parameters[positionalArguments.length].isOptional) return false; | |
146 var endPositional = math.min(parameters.length, positionalArguments.length); | |
vsm
2015/06/22 22:27:48
I don't think you need math.min - you already comp
Jacob
2015/06/24 22:21:58
Done.
| |
147 for (var i = 0; i < endPositional; i++) { | |
148 if (parameters[i].isNamed) { | |
149 // Not enough positional arguments. | |
150 return false; | |
151 } | |
152 if (!_checkType( | |
153 invocation.positionalArguments[i], parameters[i].type)) return false; | |
154 } | |
155 if (invocation.namedArguments.isNotEmpty) { | |
156 var startPositional; | |
vsm
2015/06/22 22:27:48
startNamed?
Jacob
2015/06/24 22:21:58
Done.
| |
157 for (startPositional = parameters.length - 1; | |
158 startPositional >= 0; | |
159 startPositional--) { | |
160 if (!parameters[startPositional].isNamed) break; | |
161 } | |
162 startPositional++; | |
163 | |
164 for (var name in invocation.namedArguments.keys) { | |
165 bool match = false; | |
166 for (var j = startPositional; j < parameters.length; j++) { | |
vsm
2015/06/22 22:27:48
Perhaps worth making a HashSet to avoid the n^2?
Jacob
2015/06/24 22:21:58
These are just named parameters in JS implemented
| |
167 var p = parameters[j]; | |
168 if (p.simpleName == name) { | |
169 if (!_checkType(invocation.namedArguments[name], | |
170 parameters[j].type)) return false; | |
171 match = true; | |
172 break; | |
173 } | |
174 } | |
175 if (match == false) return false; | |
176 } | |
177 } | |
178 return true; | |
179 } | |
180 | |
181 bool checkInvocation(Invocation invocation) { | |
182 for (var member in _members) { | |
vsm
2015/06/22 22:27:48
Don't you need a name match? And, if so, _members
Jacob
2015/06/24 22:21:58
This is within a DeclarationSet which is the set o
| |
183 if (_checkDeclaration(invocation, member)) return true; | |
184 } | |
185 return false; | |
186 } | |
187 | |
188 void add(mirrors.DeclarationMirror mirror) { | |
189 _members.add(mirror); | |
190 } | |
191 | |
192 final List<mirrors.DeclarationMirror> _members; | |
193 } | |
194 | |
195 /** | |
196 * Temporary method that we hope to remove at some point. This method should | |
197 * generally only be called by machine generated code. | |
198 */ | |
199 void registerJsInterfaces(List<Type> classes) { | |
200 if (_finalized == true) { | |
201 throw 'JSInterop class registration already finalized'; | |
202 } | |
203 Map<Symbol, List<mirrors.DeclarationMirror>> allMembers; | |
vsm
2015/06/22 22:27:48
Is this used?
Jacob
2015/06/24 22:21:58
Done.
| |
204 for (Type type in classes) { | |
205 if (!_jsInterfaceTypes.add(type)) continue; // Already registered. | |
206 mirrors.ClassMirror typeMirror = mirrors.reflectType(type); | |
207 typeMirror.declarations.forEach((symbol, declaration) { | |
208 if (declaration is mirrors.MethodMirror || | |
209 declaration is mirrors.VariableMirror && !declaration.isStatic) { | |
210 bool treatAsGetter = false; | |
211 bool treatAsSetter = false; | |
212 if (declaration is mirrors.VariableMirror) { | |
213 treatAsGetter = true; | |
214 if (!declaration.isConst && !declaration.isFinal) treatAsSetter = | |
vsm
2015/06/22 22:27:48
braces for multiline if
Jacob
2015/06/24 22:21:58
Done.
| |
215 true; | |
216 } else { | |
217 if (declaration.isGetter) { | |
218 _allowedGetters | |
219 .putIfAbsent(symbol, () => new _DeclarationSet()) | |
220 .add(declaration); | |
221 _allowedMethods | |
vsm
2015/06/22 22:27:48
This looks redundant with the code below also upda
Jacob
2015/06/24 22:21:58
Done.
| |
222 .putIfAbsent(symbol, () => new _DeclarationSet()) | |
223 .add(declaration); | |
224 treatAsGetter = true; | |
225 } else if (declaration.isSetter) { | |
226 treatAsSetter = true; | |
227 } else if (!declaration.isConstructor) { | |
228 _allowedMethods | |
229 .putIfAbsent(symbol, () => new _DeclarationSet()) | |
230 .add(declaration); | |
231 } | |
232 } | |
233 if (treatAsGetter) { | |
234 _allowedGetters | |
235 .putIfAbsent(symbol, () => new _DeclarationSet()) | |
236 .add(declaration); | |
237 _allowedMethods | |
238 .putIfAbsent(symbol, () => new _DeclarationSet()) | |
239 .add(declaration); | |
240 } | |
241 if (treatAsSetter) { | |
242 _allowedSetters | |
243 .putIfAbsent(symbol, () => new _DeclarationSet()) | |
244 .add(declaration); | |
245 } | |
246 } | |
247 }); | |
248 } | |
249 } | |
250 | |
251 _finalizeJsInterfaces() native "Js_finalizeJsInterfaces"; | |
252 | |
253 /** | |
254 * Generates a part file defining source code for JsObjectImpl and related | |
255 * classes. This calass is needed so that type checks for all registered JavaScr ipt | |
256 * interop classes pass. | |
257 */ | |
258 String _generateJsObjectImplPart() { | |
259 Iterable<Type> types = jsInterfaceTypes; | |
260 var libraryPrefixes = new Map<mirrors.LibraryMirror, String>(); | |
261 var prefixNames = new Set<String>(); | |
262 var sb = new StringBuffer(); | |
263 | |
264 var implements = <String>[]; | |
265 for (var type in types) { | |
266 mirrors.ClassMirror typeMirror = mirrors.reflectType(type); | |
267 mirrors.LibraryMirror libraryMirror = typeMirror.owner; | |
268 var prefixName; | |
269 if (libraryPrefixes.containsKey(libraryMirror)) { | |
270 prefixName = libraryPrefixes[libraryMirror]; | |
271 } else { | |
272 var basePrefixName = | |
273 mirrors.MirrorSystem.getName(libraryMirror.simpleName); | |
274 basePrefixName = basePrefixName.replaceAll('.', '_'); | |
275 if (basePrefixName.isEmpty) basePrefixName = "lib"; | |
276 prefixName = basePrefixName; | |
277 var i = 1; | |
278 while (prefixNames.contains(prefixName)) { | |
279 prefixName = '$basePrefixName$i'; | |
280 i++; | |
281 } | |
282 prefixNames.add(prefixName); | |
283 libraryPrefixes[libraryMirror] = prefixName; | |
284 } | |
285 implements.add( | |
286 '${prefixName}.${mirrors.MirrorSystem.getName(typeMirror.simpleName)}'); | |
287 } | |
288 libraryPrefixes.forEach((libraryMirror, prefix) { | |
289 sb.writeln('import "${libraryMirror.uri}" as $prefix;'); | |
290 }); | |
291 var implementsClause = | |
292 implements.isEmpty ? "" : "implements ${implements.join(', ')}"; | |
293 // TODO(jacobr): only certain classes need to be implemented by | |
294 // Function and Array. | |
295 sb.write(''' | |
296 class JsObjectImpl extends JsObject $implementsClause { | |
297 JsObjectImpl.internal() : super.internal(); | |
298 } | |
299 | |
300 class JsFunctionImpl extends JsFunction $implementsClause { | |
301 JsFunctionImpl.internal() : super.internal(); | |
302 } | |
303 | |
304 class JsArrayImpl<E> extends JsArray<E> $implementsClause { | |
305 JsArrayImpl.internal() : super.internal(); | |
306 } | |
307 '''); | |
308 return sb.toString(); | |
309 } | |
310 | |
311 // Start of block of helper methods facilitating emulating JavaScript Array | |
312 // methods on Dart List objects passed to JavaScript via JS interop. | |
313 // TODO(jacobr): match JS more closely. | |
314 String _toStringJs(obj) => '$obj'; | |
315 | |
316 // TODO(jacobr): this might not exactly match JS semantics but should be | |
317 // adequate for now. | |
318 int _toIntJs(obj) { | |
319 if (obj is int) return obj; | |
320 if (obj is num) return obj.toInt(); | |
321 return num.parse('$obj'.trim(), (_) => 0).toInt(); | |
322 } | |
323 | |
324 // TODO(jacobr): this might not exactly match JS semantics but should be | |
325 // adequate for now. | |
326 num _toNumJs(obj) { | |
327 return obj is num ? obj : num.parse('$obj'.trim(), (_) => 0); | |
328 } | |
329 | |
330 /// Match the behavior of setting List length in JavaScript with the exception | |
331 /// that Dart does not distinguish undefined and null. | |
332 _setListLength(List list, rawlen) { | |
333 num len = _toNumJs(rawlen); | |
334 if (len is! int || len < 0) { | |
335 throw new RangeError("Invalid array length"); | |
336 } | |
337 if (len > list.length) { | |
338 _arrayExtend(list, len); | |
339 } else if (len < list.length) { | |
340 list.removeRange(len, list.length); | |
341 } | |
342 return rawlen; | |
343 } | |
344 | |
345 // TODO(jacobr): should we really bother with this method instead of just | |
346 // shallow copying to a JS array and calling the JavaScript join method? | |
347 String _arrayJoin(List list, sep) { | |
348 if (sep == null) { | |
349 sep = ","; | |
350 } | |
351 return list.map((e) => e == null ? "" : e.toString()).join(sep.toString()); | |
352 } | |
353 | |
354 // TODO(jacobr): should we really bother with this method instead of just | |
355 // shallow copying to a JS array and using the toString method? | |
356 String _arrayToString(List list) => _arrayJoin(list, ","); | |
357 | |
358 int _arrayPush(List list, e) { | |
359 list.add(e); | |
360 return list.length; | |
361 } | |
362 | |
363 _arrayPop(List list) { | |
364 if (list.length > 0) return list.removeLast(); | |
365 } | |
366 | |
367 // TODO(jacobr): would it be better to just copy input to a JS List | |
368 // and call Array.concat? | |
369 List _arrayConcat(List input, List args) { | |
370 var ret = new List.from(input); | |
371 for (var e in args) { | |
372 // TODO(jacobr): technically in ES6 we should use | |
373 // Symbol.isConcatSpreadable to determine whether call addAll. Once v8 | |
374 // supports it, we can make all Dart classes implementing Iterable | |
375 // specify isConcatSpreadable and tweak this behavior to allow Iterable. | |
376 if (e is List) { | |
377 ret.addAll(e); | |
378 } else { | |
379 ret.add(e); | |
380 } | |
381 } | |
382 return ret; | |
383 } | |
384 | |
385 List _arraySplice(List input, List args) { | |
386 int start = 0; | |
387 if (args.length > 0) { | |
388 var rawStart = _toIntJs(args[0]); | |
389 if (rawStart < 0) { | |
390 start = math.max(0, input.length - rawStart); | |
391 } else { | |
392 start = math.min(input.length, rawStart); | |
393 } | |
394 } | |
395 var end = start; | |
396 if (args.length > 1) { | |
397 var rawDeleteCount = _toIntJs(args[1]); | |
398 if (rawDeleteCount < 0) rawDeleteCount = 0; | |
399 end = math.min(input.length, start + rawDeleteCount); | |
400 } | |
401 var replacement = []; | |
402 var removedElements = input.getRange(start, end).toList(); | |
403 if (args.length > 2) { | |
404 replacement = args.getRange(2, args.length); | |
405 } | |
406 input.replaceRange(start, end, replacement); | |
407 return removedElements; | |
408 } | |
409 | |
410 List _arrayReverse(List l) { | |
411 for (var i = 0, j = l.length - 1; i < j; i++, j--) { | |
412 var tmp = l[i]; | |
413 l[i] = l[j]; | |
414 l[j] = tmp; | |
415 } | |
416 return l; | |
417 } | |
418 | |
419 _arrayShift(List l) { | |
420 if (l.isEmpty) return null; // Technically we should return undefined. | |
421 return l.removeAt(0); | |
422 } | |
423 | |
424 int _arrayUnshift(List l, List args) { | |
425 l.insertAll(0, args); | |
426 return l.length; | |
427 } | |
428 | |
429 _arrayExtend(List l, int newLength) { | |
430 for (var i = l.length; i < newLength; i++) { | |
431 // TODO(jacobr): we'd really like to add undefined to better match | |
432 // JavaScript semantics. | |
433 l.add(null); | |
434 } | |
435 } | |
436 | |
437 List _arraySort(List l, rawCompare) { | |
438 // TODO(jacobr): alternately we could just copy the Array to JavaScript, | |
439 // invoke the JS sort method and then copy the result back to Dart. | |
440 Comparator compare; | |
441 if (rawCompare == null) { | |
442 compare = (a, b) => _toStringJs(a).compareTo(_toStringJs(b)); | |
443 } else if (rawCompare is JsFunction) { | |
444 compare = (a, b) => rawCompare.apply([a, b]); | |
445 } else { | |
446 compare = rawCompare; | |
447 } | |
448 l.sort(compare); | |
449 return l; | |
450 } | |
451 // End of block of helper methods to emulate JavaScript Array methods on Dart Li st. | |
452 | |
453 /** | |
454 * Can be called to provide a predictable point where no more JS interfaces can | |
455 * be added. Creating an instance of JsObject will also automatically trigger | |
456 * all JsObjects to be finalized. | |
457 */ | |
458 void finalizeJsInterfaces() { | |
459 if (_finalized == true) { | |
460 throw 'JSInterop class registration already finalized'; | |
461 } | |
462 _finalizeJsInterfaces(); | |
463 } | |
92 | 464 |
93 JsObject _cachedContext; | 465 JsObject _cachedContext; |
94 | 466 |
95 JsObject get _context native "Js_context_Callback"; | 467 JsObject get _context native "Js_context_Callback"; |
96 | 468 |
469 bool get _finalized native "Js_interfacesFinalized_Callback"; | |
470 | |
97 JsObject get context { | 471 JsObject get context { |
98 if (_cachedContext == null) { | 472 if (_cachedContext == null) { |
99 _cachedContext = _context; | 473 _cachedContext = _context; |
100 } | 474 } |
101 return _cachedContext; | 475 return _cachedContext; |
102 } | 476 } |
103 | 477 |
104 /** | 478 /** |
105 * Proxies a JavaScript object to Dart. | 479 * Proxies a JavaScript object to Dart. |
106 * | 480 * |
107 * The properties of the JavaScript object are accessible via the `[]` and | 481 * The properties of the JavaScript object are accessible via the `[]` and |
108 * `[]=` operators. Methods are callable via [callMethod]. | 482 * `[]=` operators. Methods are callable via [callMethod]. |
109 */ | 483 */ |
110 class JsObject extends NativeFieldWrapperClass2 { | 484 class JsObject extends NativeFieldWrapperClass2 { |
111 JsObject.internal(); | 485 JsObject.internal(); |
112 | 486 |
113 /** | 487 /** |
114 * Constructs a new JavaScript object from [constructor] and returns a proxy | 488 * Constructs a new JavaScript object from [constructor] and returns a proxy |
115 * to it. | 489 * to it. |
116 */ | 490 */ |
117 factory JsObject(JsFunction constructor, [List arguments]) => _create(construc tor, arguments); | 491 factory JsObject(JsFunction constructor, [List arguments]) => |
492 _create(constructor, arguments); | |
118 | 493 |
119 static JsObject _create(JsFunction constructor, arguments) native "JsObject_co nstructorCallback"; | 494 static JsObject _create( |
495 JsFunction constructor, arguments) native "JsObject_constructorCallback"; | |
496 | |
497 _buildArgs(Invocation invocation) { | |
498 if (invocation.namedArguments.isEmpty) { | |
499 return invocation.positionalArguments; | |
500 } else { | |
501 var varArgs = new Map<String, Object>(); | |
502 invocation.namedArguments.forEach((symbol, val) { | |
503 varArgs[mirrors.MirrorSystem.getName(symbol)] = val; | |
504 }); | |
505 return invocation.positionalArguments.toList() | |
506 ..add(new JsObject.jsify(varArgs)); | |
507 } | |
508 } | |
120 | 509 |
121 /** | 510 /** |
122 * Constructs a [JsObject] that proxies a native Dart object; _for expert use | 511 * Constructs a [JsObject] that proxies a native Dart object; _for expert use |
123 * only_. | 512 * only_. |
124 * | 513 * |
125 * Use this constructor only if you wish to get access to JavaScript | 514 * Use this constructor only if you wish to get access to JavaScript |
126 * properties attached to a browser host object, such as a Node or Blob, that | 515 * properties attached to a browser host object, such as a Node or Blob, that |
127 * is normally automatically converted into a native Dart object. | 516 * is normally automatically converted into a native Dart object. |
128 * | 517 * |
129 * An exception will be thrown if [object] either is `null` or has the type | 518 * An exception will be thrown if [object] either is `null` or has the type |
130 * `bool`, `num`, or `String`. | 519 * `bool`, `num`, or `String`. |
131 */ | 520 */ |
132 factory JsObject.fromBrowserObject(object) { | 521 factory JsObject.fromBrowserObject(object) { |
133 if (object is num || object is String || object is bool || object == null) { | 522 if (object is num || object is String || object is bool || object == null) { |
134 throw new ArgumentError( | 523 throw new ArgumentError("object cannot be a num, string, bool, or null"); |
135 "object cannot be a num, string, bool, or null"); | |
136 } | 524 } |
137 return _fromBrowserObject(object); | 525 return _fromBrowserObject(object); |
138 } | 526 } |
139 | 527 |
140 /** | 528 /** |
141 * Recursively converts a JSON-like collection of Dart objects to a | 529 * Recursively converts a JSON-like collection of Dart objects to a |
142 * collection of JavaScript objects and returns a [JsObject] proxy to it. | 530 * collection of JavaScript objects and returns a [JsObject] proxy to it. |
143 * | 531 * |
144 * [object] must be a [Map] or [Iterable], the contents of which are also | 532 * [object] must be a [Map] or [Iterable], the contents of which are also |
145 * converted. Maps and Iterables are copied to a new JavaScript object. | 533 * converted. Maps and Iterables are copied to a new JavaScript object. |
146 * Primitives and other transferrable values are directly converted to their | 534 * Primitives and other transferrable values are directly converted to their |
147 * JavaScript type, and all other objects are proxied. | 535 * JavaScript type, and all other objects are proxied. |
148 */ | 536 */ |
149 factory JsObject.jsify(object) { | 537 factory JsObject.jsify(object) { |
150 if ((object is! Map) && (object is! Iterable)) { | 538 if ((object is! Map) && (object is! Iterable)) { |
151 throw new ArgumentError("object must be a Map or Iterable"); | 539 throw new ArgumentError("object must be a Map or Iterable"); |
152 } | 540 } |
153 return _jsify(object); | 541 return _jsify(object); |
154 } | 542 } |
155 | 543 |
156 static JsObject _jsify(object) native "JsObject_jsify"; | 544 static JsObject _jsify(object) native "JsObject_jsify"; |
157 | 545 |
158 static JsObject _fromBrowserObject(object) native "JsObject_fromBrowserObject" ; | 546 static JsObject _fromBrowserObject( |
547 object) native "JsObject_fromBrowserObject"; | |
159 | 548 |
160 /** | 549 /** |
161 * Returns the value associated with [property] from the proxied JavaScript | 550 * Returns the value associated with [property] from the proxied JavaScript |
162 * object. | 551 * object. |
163 * | 552 * |
164 * The type of [property] must be either [String] or [num]. | 553 * The type of [property] must be either [String] or [num]. |
165 */ | 554 */ |
166 operator[](property) native "JsObject_[]"; | 555 operator [](property) native "JsObject_[]"; |
167 | 556 |
168 /** | 557 /** |
169 * Sets the value associated with [property] on the proxied JavaScript | 558 * Sets the value associated with [property] on the proxied JavaScript |
170 * object. | 559 * object. |
171 * | 560 * |
172 * The type of [property] must be either [String] or [num]. | 561 * The type of [property] must be either [String] or [num]. |
173 */ | 562 */ |
174 operator[]=(property, value) native "JsObject_[]="; | 563 operator []=(property, value) native "JsObject_[]="; |
175 | 564 |
176 int get hashCode native "JsObject_hashCode"; | 565 int get hashCode native "JsObject_hashCode"; |
177 | 566 |
178 operator==(other) => other is JsObject && _identityEquality(this, other); | 567 operator ==(other) => other is JsObject && _identityEquality(this, other); |
179 | 568 |
180 static bool _identityEquality(JsObject a, JsObject b) native "JsObject_identit yEquality"; | 569 static bool _identityEquality( |
570 JsObject a, JsObject b) native "JsObject_identityEquality"; | |
181 | 571 |
182 /** | 572 /** |
183 * Returns `true` if the JavaScript object contains the specified property | 573 * Returns `true` if the JavaScript object contains the specified property |
184 * either directly or though its prototype chain. | 574 * either directly or though its prototype chain. |
185 * | 575 * |
186 * This is the equivalent of the `in` operator in JavaScript. | 576 * This is the equivalent of the `in` operator in JavaScript. |
187 */ | 577 */ |
188 bool hasProperty(String property) native "JsObject_hasProperty"; | 578 bool hasProperty(String property) native "JsObject_hasProperty"; |
189 | 579 |
190 /** | 580 /** |
191 * Removes [property] from the JavaScript object. | 581 * Removes [property] from the JavaScript object. |
192 * | 582 * |
193 * This is the equivalent of the `delete` operator in JavaScript. | 583 * This is the equivalent of the `delete` operator in JavaScript. |
194 */ | 584 */ |
195 void deleteProperty(String property) native "JsObject_deleteProperty"; | 585 void deleteProperty(String property) native "JsObject_deleteProperty"; |
196 | 586 |
197 /** | 587 /** |
198 * Returns `true` if the JavaScript object has [type] in its prototype chain. | 588 * Returns `true` if the JavaScript object has [type] in its prototype chain. |
199 * | 589 * |
200 * This is the equivalent of the `instanceof` operator in JavaScript. | 590 * This is the equivalent of the `instanceof` operator in JavaScript. |
201 */ | 591 */ |
202 bool instanceof(JsFunction type) native "JsObject_instanceof"; | 592 bool instanceof(JsFunction type) native "JsObject_instanceof"; |
203 | 593 |
204 /** | 594 /** |
205 * Returns the result of the JavaScript objects `toString` method. | 595 * Returns the result of the JavaScript objects `toString` method. |
206 */ | 596 */ |
207 String toString() { | 597 String toString() { |
208 try { | 598 try { |
209 return _toString(); | 599 return _toString(); |
210 } catch(e) { | 600 } catch (e) { |
211 return super.toString(); | 601 return super.toString(); |
212 } | 602 } |
213 } | 603 } |
214 | 604 |
215 String _toString() native "JsObject_toString"; | 605 String _toString() native "JsObject_toString"; |
216 | 606 |
217 /** | 607 /** |
218 * Calls [method] on the JavaScript object with the arguments [args] and | 608 * Calls [method] on the JavaScript object with the arguments [args] and |
219 * returns the result. | 609 * returns the result. |
220 * | 610 * |
221 * The type of [method] must be either [String] or [num]. | 611 * The type of [method] must be either [String] or [num]. |
222 */ | 612 */ |
223 callMethod(String method, [List args]) { | 613 callMethod(String method, [List args]) { |
224 try { | 614 try { |
225 return _callMethod(method, args); | 615 return _callMethod(method, args); |
226 } catch(e) { | 616 } catch (e) { |
227 if (hasProperty(method)) { | 617 if (hasProperty(method)) { |
228 rethrow; | 618 rethrow; |
229 } else { | 619 } else { |
230 throw new NoSuchMethodError(this, new Symbol(method), args, null); | 620 throw new NoSuchMethodError(this, new Symbol(method), args, null); |
231 } | 621 } |
232 } | 622 } |
233 } | 623 } |
234 | 624 |
625 noSuchMethod(Invocation invocation) { | |
626 throwError() { | |
627 throw new NoSuchMethodError(this, invocation.memberName, | |
628 invocation.positionalArguments, invocation.namedArguments); | |
629 } | |
630 | |
631 String name = mirrors.MirrorSystem.getName(invocation.memberName); | |
632 if (invocation.isGetter) { | |
633 if (_CHECK_JS_INVOCATIONS) { | |
634 var matches = _allowedGetters[invocation.memberName]; | |
635 if (matches == null && | |
636 !_allowedMethods.containsKey(invocation.memberName)) { | |
637 throwError(); | |
638 } | |
639 var ret = this[name]; | |
640 if (matches != null && matches._checkReturnType(ret)) return ret; | |
641 if (ret is Function || | |
642 (ret is JsFunction /* shouldn't be needed in the future*/) && | |
643 _allowedMethods.containsKey( | |
644 invocation.memberName)) return ret; // Warning: we have not bound "this"... we could type check on the Function but that is of little value in Dart. | |
645 throwError(); | |
646 } else { | |
647 // TODO(jacobr): should we throw if the JavaScript object doesn't have t he property? | |
648 return this[name]; | |
649 } | |
650 } else if (invocation.isSetter) { | |
651 if (_CHECK_JS_INVOCATIONS) { | |
652 var matches = _allowedSetters[invocation.memberName]; | |
653 if (matches == null || | |
654 !matches.checkInvocation(invocation)) throwError(); | |
655 } | |
656 assert(name.endsWith("=")); | |
657 name = name.substring(0, name.length - 1); | |
658 return this[name] = invocation.positionalArguments.first; | |
659 } else { | |
660 // TODO(jacobr): also allow calling getters that look like functions. | |
661 var matches; | |
662 if (_CHECK_JS_INVOCATIONS) { | |
663 matches = _allowedMethods[invocation.memberName]; | |
664 if (matches == null || | |
665 !matches.checkInvocation(invocation)) throwError(); | |
666 } | |
667 var ret = this.callMethod(name, _buildArgs(invocation)); | |
668 if (_CHECK_JS_INVOCATIONS) { | |
669 if (!matches._checkReturnType(ret)) throwError(); | |
670 } | |
671 return ret; | |
672 } | |
673 } | |
674 | |
235 _callMethod(String name, List args) native "JsObject_callMethod"; | 675 _callMethod(String name, List args) native "JsObject_callMethod"; |
236 } | 676 } |
237 | 677 |
238 /** | 678 /** |
239 * Proxies a JavaScript Function object. | 679 * Proxies a JavaScript Function object. |
240 */ | 680 */ |
241 class JsFunction extends JsObject { | 681 class JsFunction extends JsObject implements Function { |
242 JsFunction.internal() : super.internal(); | 682 JsFunction.internal() : super.internal(); |
243 | 683 |
244 /** | 684 /** |
245 * Returns a [JsFunction] that captures its 'this' binding and calls [f] | 685 * Returns a [JsFunction] that captures its 'this' binding and calls [f] |
246 * with the value of this passed as the first argument. | 686 * with the value of this passed as the first argument. |
247 */ | 687 */ |
248 factory JsFunction.withThis(Function f) => _withThis(f); | 688 factory JsFunction.withThis(Function f) => _withThis(f); |
249 | 689 |
250 /** | 690 /** |
251 * Invokes the JavaScript function with arguments [args]. If [thisArg] is | 691 * Invokes the JavaScript function with arguments [args]. If [thisArg] is |
252 * supplied it is the value of `this` for the invocation. | 692 * supplied it is the value of `this` for the invocation. |
253 */ | 693 */ |
254 dynamic apply(List args, {thisArg}) native "JsFunction_apply"; | 694 dynamic apply(List args, {thisArg}) native "JsFunction_apply"; |
255 | 695 |
696 noSuchMethod(Invocation invocation) { | |
697 if (invocation.isMethod && invocation.memberName == #call) { | |
698 return apply(_buildArgs(invocation)); | |
699 } | |
700 return super.noSuchMethod(invocation); | |
701 } | |
702 | |
256 /** | 703 /** |
257 * Internal only version of apply which uses debugger proxies of Dart objects | 704 * Internal only version of apply which uses debugger proxies of Dart objects |
258 * rather than opaque handles. This method is private because it cannot be | 705 * rather than opaque handles. This method is private because it cannot be |
259 * efficiently implemented in Dart2Js so should only be used by internal | 706 * efficiently implemented in Dart2Js so should only be used by internal |
260 * tools. | 707 * tools. |
261 */ | 708 */ |
262 _applyDebuggerOnly(List args, {thisArg}) native "JsFunction_applyDebuggerOnly" ; | 709 _applyDebuggerOnly(List args, |
710 {thisArg}) native "JsFunction_applyDebuggerOnly"; | |
263 | 711 |
264 static JsFunction _withThis(Function f) native "JsFunction_withThis"; | 712 static JsFunction _withThis(Function f) native "JsFunction_withThis"; |
265 } | 713 } |
266 | 714 |
267 /** | 715 /** |
268 * A [List] proxying a JavaScript Array. | 716 * A [List] proxying a JavaScript Array. |
269 */ | 717 */ |
270 class JsArray<E> extends JsObject with ListMixin<E> { | 718 class JsArray<E> extends JsObject with ListMixin<E> { |
719 JsArray.internal() : super.internal(); | |
271 | 720 |
272 factory JsArray() => _newJsArray(); | 721 factory JsArray() => _newJsArray(); |
273 | 722 |
274 static JsArray _newJsArray() native "JsArray_newJsArray"; | 723 static JsArray _newJsArray() native "JsArray_newJsArray"; |
275 | 724 |
276 factory JsArray.from(Iterable<E> other) => _newJsArrayFromSafeList(new List.fr om(other)); | 725 factory JsArray.from(Iterable<E> other) => |
726 _newJsArrayFromSafeList(new List.from(other)); | |
277 | 727 |
278 static JsArray _newJsArrayFromSafeList(List list) native "JsArray_newJsArrayFr omSafeList"; | 728 static JsArray _newJsArrayFromSafeList( |
729 List list) native "JsArray_newJsArrayFromSafeList"; | |
279 | 730 |
280 _checkIndex(int index, {bool insert: false}) { | 731 _checkIndex(int index, {bool insert: false}) { |
281 int length = insert ? this.length + 1 : this.length; | 732 int length = insert ? this.length + 1 : this.length; |
282 if (index is int && (index < 0 || index >= length)) { | 733 if (index is int && (index < 0 || index >= length)) { |
283 throw new RangeError.range(index, 0, length); | 734 throw new RangeError.range(index, 0, length); |
284 } | 735 } |
285 } | 736 } |
286 | 737 |
287 _checkRange(int start, int end) { | 738 _checkRange(int start, int end) { |
288 int cachedLength = this.length; | 739 int cachedLength = this.length; |
289 if (start < 0 || start > cachedLength) { | 740 if (start < 0 || start > cachedLength) { |
290 throw new RangeError.range(start, 0, cachedLength); | 741 throw new RangeError.range(start, 0, cachedLength); |
291 } | 742 } |
292 if (end < start || end > cachedLength) { | 743 if (end < start || end > cachedLength) { |
293 throw new RangeError.range(end, start, cachedLength); | 744 throw new RangeError.range(end, start, cachedLength); |
294 } | 745 } |
295 } | 746 } |
296 | 747 |
297 // Methods required by ListMixin | 748 // Methods required by ListMixin |
298 | 749 |
299 E operator [](index) { | 750 E operator [](index) { |
300 if (index is int) { | 751 if (index is int) { |
301 _checkIndex(index); | 752 _checkIndex(index); |
302 } | 753 } |
303 return super[index]; | 754 return super[index]; |
304 } | 755 } |
305 | 756 |
306 void operator []=(index, E value) { | 757 void operator []=(index, E value) { |
307 if(index is int) { | 758 if (index is int) { |
308 _checkIndex(index); | 759 _checkIndex(index); |
309 } | 760 } |
310 super[index] = value; | 761 super[index] = value; |
311 } | 762 } |
312 | 763 |
313 int get length native "JsArray_length"; | 764 int get length native "JsArray_length"; |
314 | 765 |
315 void set length(int length) { super['length'] = length; } | 766 void set length(int length) { |
767 super['length'] = length; | |
768 } | |
316 | 769 |
317 // Methods overriden for better performance | 770 // Methods overriden for better performance |
318 | 771 |
319 void add(E value) { | 772 void add(E value) { |
320 callMethod('push', [value]); | 773 callMethod('push', [value]); |
321 } | 774 } |
322 | 775 |
323 void addAll(Iterable<E> iterable) { | 776 void addAll(Iterable<E> iterable) { |
324 // TODO(jacobr): this can be optimized slightly. | 777 // TODO(jacobr): this can be optimized slightly. |
325 callMethod('push', new List.from(iterable)); | 778 callMethod('push', new List.from(iterable)); |
326 } | 779 } |
327 | 780 |
328 void insert(int index, E element) { | 781 void insert(int index, E element) { |
329 _checkIndex(index, insert:true); | 782 _checkIndex(index, insert: true); |
330 callMethod('splice', [index, 0, element]); | 783 callMethod('splice', [index, 0, element]); |
331 } | 784 } |
332 | 785 |
333 E removeAt(int index) { | 786 E removeAt(int index) { |
334 _checkIndex(index); | 787 _checkIndex(index); |
335 return callMethod('splice', [index, 1])[0]; | 788 return callMethod('splice', [index, 1])[0]; |
336 } | 789 } |
337 | 790 |
338 E removeLast() { | 791 E removeLast() { |
339 if (length == 0) throw new RangeError(-1); | 792 if (length == 0) throw new RangeError(-1); |
(...skipping 18 matching lines...) Expand all Loading... | |
358 callMethod('sort', [compare]); | 811 callMethod('sort', [compare]); |
359 } | 812 } |
360 } | 813 } |
361 | 814 |
362 /** | 815 /** |
363 * Placeholder object for cases where we need to determine exactly how many | 816 * Placeholder object for cases where we need to determine exactly how many |
364 * args were passed to a function. | 817 * args were passed to a function. |
365 */ | 818 */ |
366 const _UNDEFINED = const Object(); | 819 const _UNDEFINED = const Object(); |
367 | 820 |
368 // FIXME(jacobr): this method is a hack to work around the lack of proper dart | 821 // TODO(jacobr): this method is a hack to work around the lack of proper dart |
369 // support for varargs methods. | 822 // support for varargs methods. |
370 List _stripUndefinedArgs(List args) => | 823 List _stripUndefinedArgs(List args) => |
371 args.takeWhile((i) => i != _UNDEFINED).toList(); | 824 args.takeWhile((i) => i != _UNDEFINED).toList(); |
372 | 825 |
373 /** | 826 /** |
374 * Returns a method that can be called with an arbitrary number (for n less | 827 * Returns a method that can be called with an arbitrary number (for n less |
375 * than 11) of arguments without violating Dart type checks. | 828 * than 11) of arguments without violating Dart type checks. |
376 */ | 829 */ |
377 Function _wrapAsDebuggerVarArgsFunction(JsFunction jsFunction) => | 830 Function _wrapAsDebuggerVarArgsFunction(JsFunction jsFunction) => |
378 ([a1=_UNDEFINED, a2=_UNDEFINED, a3=_UNDEFINED, a4=_UNDEFINED, | 831 ([a1 = _UNDEFINED, a2 = _UNDEFINED, a3 = _UNDEFINED, a4 = _UNDEFINED, |
379 a5=_UNDEFINED, a6=_UNDEFINED, a7=_UNDEFINED, a8=_UNDEFINED, | 832 a5 = _UNDEFINED, a6 = _UNDEFINED, a7 = _UNDEFINED, a8 = _UNDEFINED, |
380 a9=_UNDEFINED, a10=_UNDEFINED]) => | 833 a9 = _UNDEFINED, a10 = _UNDEFINED]) => jsFunction._applyDebuggerOnly( |
381 jsFunction._applyDebuggerOnly(_stripUndefinedArgs( | 834 _stripUndefinedArgs([a1, a2, a3, a4, a5, a6, a7, a8, a9, a10])); |
382 [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10])); | |
OLD | NEW |