Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: sdk/lib/js/dartium/js_dartium.dart

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

Powered by Google App Engine
This is Rietveld 408576698