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

Side by Side Diff: sdk/lib/js/dart2js/js_dart2js.dart

Issue 1318043005: Support user generated custom native JS classes. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: ptal Created 5 years, 3 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
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:html' show Blob, Event, ImageData, Node, Window; 90 import 'dart:html' show Blob, Event, ImageData, Node, Window;
91 import 'dart:collection' show HashMap, ListMixin; 91 import 'dart:collection' show HashMap, ListMixin;
92 import 'dart:indexed_db' show KeyRange; 92 import 'dart:indexed_db' show KeyRange;
93 import 'dart:typed_data' show TypedData; 93 import 'dart:typed_data' show TypedData;
94 94
95 import 'dart:_foreign_helper' show JS, DART_CLOSURE_TO_JS; 95 import 'dart:_foreign_helper' show JS, JS_CONST, DART_CLOSURE_TO_JS;
96 import 'dart:_interceptors' show JavaScriptObject, UnknownJavaScriptObject; 96 import 'dart:_interceptors'
97 import 'dart:_js_helper' show Primitives, convertDartClosureToJS, 97 show JavaScriptObject, UnknownJavaScriptObject, DART_CLOSURE_PROPERTY_NAME;
98 getIsolateAffinityTag; 98 import 'dart:_js_helper'
99 show Primitives, convertDartClosureToJS, getIsolateAffinityTag;
100
101 export 'dart:_interceptors' show Interceptor, JavaScriptObject, JSArray;
99 102
100 final JsObject context = _wrapToDart(JS('', 'self')); 103 final JsObject context = _wrapToDart(JS('', 'self'));
101 104
102 _convertDartFunction(Function f, {bool captureThis: false}) { 105 _convertDartFunction(Function f, {bool captureThis: false}) {
103 return JS('', 106 return JS(
104 'function(_call, f, captureThis) {' 107 '',
108 'function(_call, f, captureThis) {'
Siggi Cherem (dart-lang) 2015/09/18 20:34:10 +1 on using ''' to restore indentation
Jacob 2015/10/01 00:47:33 this file is now automatically formatted so please
sra1 2015/10/01 17:34:30 When this was originally written JS didn't parse f
Siggi Cherem (dart-lang) 2015/10/01 18:21:28 to clarify, what I meant was to use the triple-' g
Jacob 2015/10/02 20:08:15 fine. I switched to ''' and did a bunch of white s
105 'return function() {' 109 'return function() {'
106 'return _call(f, captureThis, this, ' 110 'return _call(f, captureThis, this, '
107 'Array.prototype.slice.apply(arguments));' 111 'Array.prototype.slice.apply(arguments));'
108 '}' 112 '}'
109 '}(#, #, #)', DART_CLOSURE_TO_JS(_callDartFunction), f, captureThis); 113 '}(#, #, #)',
114 DART_CLOSURE_TO_JS(_callDartFunction),
115 f,
116 captureThis);
110 } 117 }
111 118
112 _callDartFunction(callback, bool captureThis, self, List arguments) { 119 _callDartFunction(callback, bool captureThis, self, List arguments) {
113 if (captureThis) { 120 if (captureThis) {
114 arguments = [self]..addAll(arguments); 121 arguments = [self]..addAll(arguments);
115 } 122 }
116 var dartArgs = new List.from(arguments.map(_convertToDart)); 123 var dartArgs = new List.from(arguments.map(_convertToDart));
117 return _convertToJS(Function.apply(callback, dartArgs)); 124 return _convertToJS(Function.apply(callback, dartArgs));
118 } 125 }
119 126
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 * 212 *
206 * Use this constructor only if you wish to get access to JavaScript 213 * Use this constructor only if you wish to get access to JavaScript
207 * properties attached to a browser host object, such as a Node or Blob, that 214 * properties attached to a browser host object, such as a Node or Blob, that
208 * is normally automatically converted into a native Dart object. 215 * is normally automatically converted into a native Dart object.
209 * 216 *
210 * An exception will be thrown if [object] either is `null` or has the type 217 * An exception will be thrown if [object] either is `null` or has the type
211 * `bool`, `num`, or `String`. 218 * `bool`, `num`, or `String`.
212 */ 219 */
213 factory JsObject.fromBrowserObject(object) { 220 factory JsObject.fromBrowserObject(object) {
214 if (object is num || object is String || object is bool || object == null) { 221 if (object is num || object is String || object is bool || object == null) {
215 throw new ArgumentError( 222 throw new ArgumentError("object cannot be a num, string, bool, or null");
216 "object cannot be a num, string, bool, or null");
217 } 223 }
218 return _wrapToDart(_convertToJS(object)); 224 return _wrapToDart(_convertToJS(object));
219 } 225 }
220 226
221 /** 227 /**
222 * Recursively converts a JSON-like collection of Dart objects to a 228 * Recursively converts a JSON-like collection of Dart objects to a
223 * collection of JavaScript objects and returns a [JsObject] proxy to it. 229 * collection of JavaScript objects and returns a [JsObject] proxy to it.
224 * 230 *
225 * [object] must be a [Map] or [Iterable], the contents of which are also 231 * [object] must be a [Map] or [Iterable], the contents of which are also
226 * converted. Maps and Iterables are copied to a new JavaScript object. 232 * converted. Maps and Iterables are copied to a new JavaScript object.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 266
261 return _convert(data); 267 return _convert(data);
262 } 268 }
263 269
264 /** 270 /**
265 * Returns the value associated with [property] from the proxied JavaScript 271 * Returns the value associated with [property] from the proxied JavaScript
266 * object. 272 * object.
267 * 273 *
268 * The type of [property] must be either [String] or [num]. 274 * The type of [property] must be either [String] or [num].
269 */ 275 */
270 dynamic operator[](property) { 276 dynamic operator [](property) {
271 if (property is! String && property is! num) { 277 if (property is! String && property is! num) {
272 throw new ArgumentError("property is not a String or num"); 278 throw new ArgumentError("property is not a String or num");
273 } 279 }
274 return _convertToDart(JS('', '#[#]', _jsObject, property)); 280 return _convertToDart(JS('', '#[#]', _jsObject, property));
275 } 281 }
276 282
277 /** 283 /**
278 * Sets the value associated with [property] on the proxied JavaScript 284 * Sets the value associated with [property] on the proxied JavaScript
279 * object. 285 * object.
280 * 286 *
281 * The type of [property] must be either [String] or [num]. 287 * The type of [property] must be either [String] or [num].
282 */ 288 */
283 operator[]=(property, value) { 289 operator []=(property, value) {
284 if (property is! String && property is! num) { 290 if (property is! String && property is! num) {
285 throw new ArgumentError("property is not a String or num"); 291 throw new ArgumentError("property is not a String or num");
286 } 292 }
287 JS('', '#[#]=#', _jsObject, property, _convertToJS(value)); 293 JS('', '#[#]=#', _jsObject, property, _convertToJS(value));
288 } 294 }
289 295
290 int get hashCode => 0; 296 int get hashCode => 0;
291 297
292 bool operator==(other) => other is JsObject && 298 bool operator ==(other) =>
293 JS('bool', '# === #', _jsObject, other._jsObject); 299 other is JsObject && JS('bool', '# === #', _jsObject, other._jsObject);
294 300
295 /** 301 /**
296 * Returns `true` if the JavaScript object contains the specified property 302 * Returns `true` if the JavaScript object contains the specified property
297 * either directly or though its prototype chain. 303 * either directly or though its prototype chain.
298 * 304 *
299 * This is the equivalent of the `in` operator in JavaScript. 305 * This is the equivalent of the `in` operator in JavaScript.
300 */ 306 */
301 bool hasProperty(property) { 307 bool hasProperty(property) {
302 if (property is! String && property is! num) { 308 if (property is! String && property is! num) {
303 throw new ArgumentError("property is not a String or num"); 309 throw new ArgumentError("property is not a String or num");
(...skipping 21 matching lines...) Expand all
325 bool instanceof(JsFunction type) { 331 bool instanceof(JsFunction type) {
326 return JS('bool', '# instanceof #', _jsObject, _convertToJS(type)); 332 return JS('bool', '# instanceof #', _jsObject, _convertToJS(type));
327 } 333 }
328 334
329 /** 335 /**
330 * Returns the result of the JavaScript objects `toString` method. 336 * Returns the result of the JavaScript objects `toString` method.
331 */ 337 */
332 String toString() { 338 String toString() {
333 try { 339 try {
334 return JS('String', 'String(#)', _jsObject); 340 return JS('String', 'String(#)', _jsObject);
335 } catch(e) { 341 } catch (e) {
336 return super.toString(); 342 return super.toString();
337 } 343 }
338 } 344 }
339 345
340 /** 346 /**
341 * Calls [method] on the JavaScript object with the arguments [args] and 347 * Calls [method] on the JavaScript object with the arguments [args] and
342 * returns the result. 348 * returns the result.
343 * 349 *
344 * The type of [method] must be either [String] or [num]. 350 * The type of [method] must be either [String] or [num].
345 */ 351 */
346 dynamic callMethod(method, [List args]) { 352 dynamic callMethod(method, [List args]) {
347 if (method is! String && method is! num) { 353 if (method is! String && method is! num) {
348 throw new ArgumentError("method is not a String or num"); 354 throw new ArgumentError("method is not a String or num");
349 } 355 }
350 return _convertToDart(JS('', '#[#].apply(#, #)', _jsObject, method, 356 return _convertToDart(JS(
357 '',
358 '#[#].apply(#, #)',
359 _jsObject,
360 method,
351 _jsObject, 361 _jsObject,
352 args == null ? null : new List.from(args.map(_convertToJS)))); 362 args == null ? null : new List.from(args.map(_convertToJS))));
353 } 363 }
354 } 364 }
355 365
356 /** 366 /**
357 * Proxies a JavaScript Function object. 367 * Proxies a JavaScript Function object.
358 */ 368 */
359 class JsFunction extends JsObject { 369 class JsFunction extends JsObject {
360
361 /** 370 /**
362 * Returns a [JsFunction] that captures its 'this' binding and calls [f] 371 * Returns a [JsFunction] that captures its 'this' binding and calls [f]
363 * with the value of this passed as the first argument. 372 * with the value of this passed as the first argument.
364 */ 373 */
365 factory JsFunction.withThis(Function f) { 374 factory JsFunction.withThis(Function f) {
366 var jsFunc = _convertDartFunction(f, captureThis: true); 375 var jsFunc = _convertDartFunction(f, captureThis: true);
367 return new JsFunction._fromJs(jsFunc); 376 return new JsFunction._fromJs(jsFunc);
368 } 377 }
369 378
370 JsFunction._fromJs(jsObject) : super._fromJs(jsObject); 379 JsFunction._fromJs(jsObject) : super._fromJs(jsObject);
371 380
372 /** 381 /**
373 * Invokes the JavaScript function with arguments [args]. If [thisArg] is 382 * Invokes the JavaScript function with arguments [args]. If [thisArg] is
374 * supplied it is the value of `this` for the invocation. 383 * supplied it is the value of `this` for the invocation.
375 */ 384 */
376 dynamic apply(List args, { thisArg }) => 385 dynamic apply(List args, {thisArg}) => _convertToDart(JS(
377 _convertToDart(JS('', '#.apply(#, #)', _jsObject, 386 '',
378 _convertToJS(thisArg), 387 '#.apply(#, #)',
379 args == null ? null : new List.from(args.map(_convertToJS)))); 388 _jsObject,
389 _convertToJS(thisArg),
390 args == null ? null : new List.from(args.map(_convertToJS))));
380 } 391 }
381 392
382 /** 393 /**
383 * A [List] that proxies a JavaScript array. 394 * A [List] that proxies a JavaScript array.
384 */ 395 */
385 class JsArray<E> extends JsObject with ListMixin<E> { 396 class JsArray<E> extends JsObject with ListMixin<E> {
386
387 /** 397 /**
388 * Creates a new JavaScript array. 398 * Creates a new JavaScript array.
389 */ 399 */
390 JsArray() : super._fromJs([]); 400 JsArray() : super._fromJs([]);
391 401
392 /** 402 /**
393 * Creates a new JavaScript array and initializes it to the contents of 403 * Creates a new JavaScript array and initializes it to the contents of
394 * [other]. 404 * [other].
395 */ 405 */
396 JsArray.from(Iterable<E> other) 406 JsArray.from(Iterable<E> other)
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 int get length { 452 int get length {
443 // Check the length honours the List contract. 453 // Check the length honours the List contract.
444 var len = JS('', '#.length', _jsObject); 454 var len = JS('', '#.length', _jsObject);
445 // JavaScript arrays have lengths which are unsigned 32-bit integers. 455 // JavaScript arrays have lengths which are unsigned 32-bit integers.
446 if (JS('bool', 'typeof # === "number" && (# >>> 0) === #', len, len, len)) { 456 if (JS('bool', 'typeof # === "number" && (# >>> 0) === #', len, len, len)) {
447 return JS('int', '#', len); 457 return JS('int', '#', len);
448 } 458 }
449 throw new StateError('Bad JsArray length'); 459 throw new StateError('Bad JsArray length');
450 } 460 }
451 461
452 void set length(int length) { super['length'] = length; } 462 void set length(int length) {
453 463 super['length'] = length;
464 }
454 465
455 // Methods overriden for better performance 466 // Methods overriden for better performance
456 467
457 void add(E value) { 468 void add(E value) {
458 callMethod('push', [value]); 469 callMethod('push', [value]);
459 } 470 }
460 471
461 void addAll(Iterable<E> iterable) { 472 void addAll(Iterable<E> iterable) {
462 var list = (JS('bool', '# instanceof Array', iterable)) 473 var list = (JS('bool', '# instanceof Array', iterable))
463 ? iterable 474 ? iterable
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 507
497 void sort([int compare(E a, E b)]) { 508 void sort([int compare(E a, E b)]) {
498 // Note: arr.sort(null) is a type error in FF 509 // Note: arr.sort(null) is a type error in FF
499 callMethod('sort', compare == null ? [] : [compare]); 510 callMethod('sort', compare == null ? [] : [compare]);
500 } 511 }
501 } 512 }
502 513
503 // property added to a Dart object referencing its JS-side DartObject proxy 514 // property added to a Dart object referencing its JS-side DartObject proxy
504 final String _DART_OBJECT_PROPERTY_NAME = 515 final String _DART_OBJECT_PROPERTY_NAME =
505 getIsolateAffinityTag(r'_$dart_dartObject'); 516 getIsolateAffinityTag(r'_$dart_dartObject');
506 final String _DART_CLOSURE_PROPERTY_NAME =
507 getIsolateAffinityTag(r'_$dart_dartClosure');
508 517
509 // property added to a JS object referencing its Dart-side JsObject proxy 518 // property added to a JS object referencing its Dart-side JsObject proxy
510 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; 519 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject';
511 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; 520 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction';
521 const _JS_FUNCTION_PROPERTY_NAME_CAPTURE_THIS = r'_$dart_jsFunctionCaptureThis';
512 522
513 bool _defineProperty(o, String name, value) { 523 bool _defineProperty(o, String name, value) {
514 if (_isExtensible(o) && 524 if (_isExtensible(o) &&
515 // TODO(ahe): Calling _hasOwnProperty to work around 525 // TODO(ahe): Calling _hasOwnProperty to work around
516 // https://code.google.com/p/dart/issues/detail?id=21331. 526 // https://code.google.com/p/dart/issues/detail?id=21331.
517 !_hasOwnProperty(o, name)) { 527 !_hasOwnProperty(o, name)) {
518 try { 528 try {
519 JS('void', 'Object.defineProperty(#, #, { value: #})', o, name, value); 529 JS('void', 'Object.defineProperty(#, #, { value: #})', o, name, value);
520 return true; 530 return true;
521 } catch (e) { 531 } catch (e) {
(...skipping 25 matching lines...) Expand all
547 dynamic _convertToJS(dynamic o) { 557 dynamic _convertToJS(dynamic o) {
548 // Note: we don't write `if (o == null) return null;` to make sure dart2js 558 // Note: we don't write `if (o == null) return null;` to make sure dart2js
549 // doesn't convert `return null;` into `return;` (which would make `null` be 559 // doesn't convert `return null;` into `return;` (which would make `null` be
550 // `undefined` in Javascprit). See dartbug.com/20305 for details. 560 // `undefined` in Javascprit). See dartbug.com/20305 for details.
551 if (o == null || o is String || o is num || o is bool) { 561 if (o == null || o is String || o is num || o is bool) {
552 return o; 562 return o;
553 } 563 }
554 if (o is JsObject) { 564 if (o is JsObject) {
555 return o._jsObject; 565 return o._jsObject;
556 } 566 }
557 if (o is Blob || o is Event || o is KeyRange || o is ImageData || o is Node || 567 if (o is Blob ||
558 o is TypedData || o is Window) { 568 o is Event ||
569 o is KeyRange ||
570 o is ImageData ||
571 o is Node ||
572 o is TypedData ||
573 o is Window) {
559 return o; 574 return o;
560 } 575 }
561 if (o is DateTime) { 576 if (o is DateTime) {
562 return Primitives.lazyAsJsDate(o); 577 return Primitives.lazyAsJsDate(o);
563 } 578 }
564 if (o is Function) { 579 if (o is Function) {
565 return _getJsProxy(o, _JS_FUNCTION_PROPERTY_NAME, (o) { 580 return _getJsProxy(o, _JS_FUNCTION_PROPERTY_NAME, (o) {
566 var jsFunction = _convertDartFunction(o); 581 var jsFunction = _convertDartFunction(o);
567 // set a property on the JS closure referencing the Dart closure 582 // set a property on the JS closure referencing the Dart closure
568 _defineProperty(jsFunction, _DART_CLOSURE_PROPERTY_NAME, o); 583 _defineProperty(jsFunction, DART_CLOSURE_PROPERTY_NAME, o);
569 return jsFunction; 584 return jsFunction;
570 }); 585 });
571 } 586 }
572 var ctor = _dartProxyCtor; 587 var ctor = _dartProxyCtor;
573 return _getJsProxy(o, _JS_OBJECT_PROPERTY_NAME, 588 return _getJsProxy(
574 (o) => JS('', 'new #(#)', ctor, o)); 589 o, _JS_OBJECT_PROPERTY_NAME, (o) => JS('', 'new #(#)', ctor, o));
575 } 590 }
576 591
577 Object _getJsProxy(o, String propertyName, createProxy(o)) { 592 Object _getJsProxy(o, String propertyName, createProxy(o)) {
578 var jsProxy = _getOwnProperty(o, propertyName); 593 var jsProxy = _getOwnProperty(o, propertyName);
579 if (jsProxy == null) { 594 if (jsProxy == null) {
580 jsProxy = createProxy(o); 595 jsProxy = createProxy(o);
581 _defineProperty(o, propertyName, jsProxy); 596 _defineProperty(o, propertyName, jsProxy);
582 } 597 }
583 return jsProxy; 598 return jsProxy;
584 } 599 }
585 600
586 // converts a Dart object to a reference to a native JS object 601 // converts a Dart object to a reference to a native JS object
587 // which might be a DartObject JS->Dart proxy 602 // which might be a DartObject JS->Dart proxy
588 Object _convertToDart(o) { 603 Object _convertToDart(o) {
589 if (JS('bool', '# == null', o) || 604 if (JS('bool', '# == null', o) ||
590 JS('bool', 'typeof # == "string"', o) || 605 JS('bool', 'typeof # == "string"', o) ||
591 JS('bool', 'typeof # == "number"', o) || 606 JS('bool', 'typeof # == "number"', o) ||
592 JS('bool', 'typeof # == "boolean"', o)) { 607 JS('bool', 'typeof # == "boolean"', o)) {
593 return o; 608 return o;
594 } else if (_isLocalObject(o) 609 } else if (_isLocalObject(o) &&
595 && (o is Blob || o is Event || o is KeyRange || o is ImageData 610 (o is Blob ||
596 || o is Node || o is TypedData || o is Window)) { 611 o is Event ||
612 o is KeyRange ||
613 o is ImageData ||
614 o is Node ||
615 o is TypedData ||
616 o is Window)) {
597 // long line: dart2js doesn't allow string concatenation in the JS() form 617 // long line: dart2js doesn't allow string concatenation in the JS() form
598 return JS('Blob|Event|KeyRange|ImageData|Node|TypedData|Window', '#', o); 618 return JS('Blob|Event|KeyRange|ImageData|Node|TypedData|Window', '#', o);
599 } else if (JS('bool', '# instanceof Date', o)) { 619 } else if (JS('bool', '# instanceof Date', o)) {
600 var ms = JS('num', '#.getTime()', o); 620 var ms = JS('num', '#.getTime()', o);
601 return new DateTime.fromMillisecondsSinceEpoch(ms); 621 return new DateTime.fromMillisecondsSinceEpoch(ms);
602 } else if (JS('bool', '#.constructor === #', o, _dartProxyCtor)) { 622 } else if (JS('bool', '#.constructor === #', o, _dartProxyCtor)) {
603 return JS('', '#.o', o); 623 return JS('', '#.o', o);
604 } else { 624 } else {
605 return _wrapToDart(o); 625 return _wrapToDart(o);
606 } 626 }
607 } 627 }
608 628
609 JsObject _wrapToDart(o) { 629 JsObject _wrapToDart(o) {
610 if (JS('bool', 'typeof # == "function"', o)) { 630 if (JS('bool', 'typeof # == "function"', o)) {
611 return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME, 631 return _getDartProxy(
612 (o) => new JsFunction._fromJs(o)); 632 o, DART_CLOSURE_PROPERTY_NAME, (o) => new JsFunction._fromJs(o));
613 } 633 }
614 if (JS('bool', '# instanceof Array', o)) { 634 if (JS('bool', '# instanceof Array', o)) {
615 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, 635 return _getDartProxy(
616 (o) => new JsArray._fromJs(o)); 636 o, _DART_OBJECT_PROPERTY_NAME, (o) => new JsArray._fromJs(o));
617 } 637 }
618 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, 638 return _getDartProxy(
619 (o) => new JsObject._fromJs(o)); 639 o, _DART_OBJECT_PROPERTY_NAME, (o) => new JsObject._fromJs(o));
620 } 640 }
621 641
622 Object _getDartProxy(o, String propertyName, createProxy(o)) { 642 Object _getDartProxy(o, String propertyName, createProxy(o)) {
623 var dartProxy = _getOwnProperty(o, propertyName); 643 var dartProxy = _getOwnProperty(o, propertyName);
624 // Temporary fix for dartbug.com/15193 644 // Temporary fix for dartbug.com/15193
625 // In some cases it's possible to see a JavaScript object that 645 // In some cases it's possible to see a JavaScript object that
626 // came from a different context and was previously proxied to 646 // came from a different context and was previously proxied to
627 // Dart in that context. The JS object will have a cached proxy 647 // Dart in that context. The JS object will have a cached proxy
628 // but it won't be a valid Dart object in this context. 648 // but it won't be a valid Dart object in this context.
629 // For now we throw away the cached proxy, but we should be able 649 // For now we throw away the cached proxy, but we should be able
630 // to cache proxies from multiple JS contexts and Dart isolates. 650 // to cache proxies from multiple JS contexts and Dart isolates.
631 if (dartProxy == null || !_isLocalObject(o)) { 651 if (dartProxy == null || !_isLocalObject(o)) {
632 dartProxy = createProxy(o); 652 dartProxy = createProxy(o);
633 _defineProperty(o, propertyName, dartProxy); 653 _defineProperty(o, propertyName, dartProxy);
634 } 654 }
635 return dartProxy; 655 return dartProxy;
636 } 656 }
657
658 // Start of methods for new style Dart-JS interop.
659
660 class _JavaScriptFunctionHack implements Function {
661 call(
662 [a = const JS_CONST('void 0'),
663 b = const JS_CONST('void 0'),
664 c = const JS_CONST('void 0'),
665 d = const JS_CONST('void 0'),
666 e = const JS_CONST('void 0'),
667 f = const JS_CONST('void 0'),
668 g = const JS_CONST('void 0'),
669 h = const JS_CONST('void 0'),
670 i = const JS_CONST('void 0'),
671 j = const JS_CONST('void 0')]) {
672 // Exceedingly slow default implementation.
673 return JS('', '#.apply(null, #)', this,
674 _stripUndefinedArgs([a, b, c, d, e, f, g, h, i, j]));
675 }
676 }
677
678 void _copyOwnProperties(src, dest) {
679 JS(
680 '',
681 r"""(function(src, dest) {
682 var properties = Object.getOwnPropertyNames(src);
683 for (var i = 0, len = properties.length; i < len; i++) {
684 var name = properties[i];
685 dest[name] = src[name];
686 }
687 })(#, #)""",
688 src,
689 dest);
690 }
691
692 // TODO(jacobr): remove this method. So far it appears that specifying the list
693 // of registered types in Dart2Js has significant negative code size
694 // implications so it is better to specify usage purely based on which
695 // libraries are imported. Remove after Dartium is modified to function without
Siggi Cherem (dart-lang) 2015/09/18 20:34:10 would it make sense to use tree-shakeable lookup-m
Jacob 2015/10/02 20:08:15 could be. moved away from requiring this method in
696 // requiring this method.
697 void registerJsInterfaces([List<Type> types]) {
Siggi Cherem (dart-lang) 2015/09/18 20:34:10 I seem to be missing something, is `types` used an
Jacob 2015/10/01 00:47:33 This was used in Dartium. I've ripped it out there
698 // No need to actually register in Dart2JS.
699 var fnHackProto = JS('', '#.__proto__', new _JavaScriptFunctionHack());
700 var fnProto = JS('', 'Function.prototype');
701 _copyOwnProperties(fnHackProto, fnProto);
702 // Add optimized call methods for small numbers of arguments.
703 if (JS('bool', r'#.hasOwnProperty("call$0") ', fnHackProto)) {
704 JS('', r'#.call$0 = function() { return this(); }', fnProto);
705 JS('', r'#.call$1 = function(a) { return this(a); }', fnProto);
706 JS('', r'#.call$2 = function(a, b) { return this(a, b); }', fnProto);
707 JS('', r'#.call$3 = function(a, b, c) { return this(a, b, c); }', fnProto);
708 JS('', r'#.call$4 = function(a, b, c, d) { return this(a, b, c, d); }',
709 fnProto);
710 } else {
711 if (!JS('bool', r'#.hasOwnProperty("$0") ', fnHackProto)) {
712 throw 'Internal error. Unexpected minified output';
713 }
714 JS('', r'#.$0 = function() { return this(); }', fnProto);
715 JS('', r'#.$1 = function(a) { return this(a); }', fnProto);
716 JS('', r'#.$2 = function(a, b) { return this(a, b); }', fnProto);
717 JS('', r'#.$3 = function(a, b, c) { return this(a, b, c); }', fnProto);
718 JS('', r'#.$4 = function(a, b, c, d) { return this(a, b, c, d); }',
719 fnProto);
720 }
721 }
722
723 _convertDartFunctionFast(Function f, {bool captureThis: false}) {
724 var existing = JS('', '#.#', f, _JS_FUNCTION_PROPERTY_NAME);
725 if (existing != null) return existing;
726 var ret = JS(
727 '',
728 'function(_call, f) {'
729 'return function() {'
730 'return _call(f, Array.prototype.slice.apply(arguments));'
731 '}'
732 '}(#, #)',
733 DART_CLOSURE_TO_JS(_callDartFunctionFast),
734 f);
735 JS('', '#.# = #', ret, DART_CLOSURE_PROPERTY_NAME, f);
736 JS('', '#.# = #', f, _JS_FUNCTION_PROPERTY_NAME, ret);
737 return ret;
738 }
739
740 _convertDartFunctionFastCaptureThis(Function f) {
741 var existing =
742 JS('', '#.#', f, _JS_FUNCTION_PROPERTY_NAME_CAPTURE_THIS);
743 if (existing != null) return existing;
744 var ret = JS(
745 '',
746 'function(_call, f) {'
747 'return function() {'
748 'return _call(f, this, '
749 'Array.prototype.slice.apply(arguments));'
750 '}'
751 '}(#, #)',
752 DART_CLOSURE_TO_JS(_callDartFunctionFastCaptureThis),
753 f);
754 JS('', '#.# = #', ret, DART_CLOSURE_PROPERTY_NAME, f);
755 JS('', '#.# = #', f, _JS_FUNCTION_PROPERTY_NAME_CAPTURE_THIS, ret);
756 return ret;
757 }
758
759 _callDartFunctionFast(callback, List arguments) {
760 return Function.apply(callback, arguments);
761 }
762
763 _callDartFunctionFastCaptureThis(callback, self, List arguments) {
764 return _convertToJS(Function.apply(callback, [self]..addAll(arguments)));
765 }
766
767 Function allowInterop(Function f) {
768 if (JS('bool', 'typeof(#) == "function"', f)) {
769 // Already supports interop, just use the existing function.
770 return f;
771 } else {
772 return _convertDartFunctionFast(f);
773 }
774 }
775
776 Function allowInteropCaptureThis(Function f) {
777 if (JS('bool', 'typeof(#) == "function"', f)) {
778 // Behavior when the function is already a JS function is unspecified.
779 throw new ArgumentError(
780 "Function is already a JS function so cannot capture this.");
781 return f;
782 } else {
783 return _convertDartFunctionFastCaptureThis(f);
784 }
785 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698