Index: sdk/lib/js/dart2js/js_dart2js.dart |
diff --git a/sdk/lib/js/dart2js/js_dart2js.dart b/sdk/lib/js/dart2js/js_dart2js.dart |
index 64b4af0b4771972ac4ac9b773480ece956d914b8..ee42853ade7967aed247ad43af6b839816a3012d 100644 |
--- a/sdk/lib/js/dart2js/js_dart2js.dart |
+++ b/sdk/lib/js/dart2js/js_dart2js.dart |
@@ -4,7 +4,7 @@ |
library dart.js; |
-import 'dart:_foreign_helper' show JS; |
+import 'dart:_foreign_helper' show JS, DART_CLOSURE_TO_JS; |
import 'dart:_js_helper' show Primitives, convertDartClosureToJS; |
final JsObject context = new JsObject._fromJs(Primitives.computeGlobalThis()); |
@@ -39,6 +39,34 @@ class Callback implements Serializable<JsFunction> { |
JsFunction toJs() => new JsFunction._fromJs(_jsFunction); |
} |
+/* |
+ * TODO(justinfagnani): make public when we remove Callback. |
+ * |
+ * Returns a [JsFunction] that captures its 'this' binding and calls [f] |
+ * with the value of this passed as the first argument. |
+ */ |
+JsFunction _captureThis(Function f) => |
+ new JsFunction._fromJs(_convertDartFunction(f, captureThis: true)); |
+ |
+_convertDartFunction(Function f, {bool captureThis: false}) { |
+ return JS('', |
+ 'function(_call, f, captureThis) {' |
+ 'return function() {' |
+ 'return _call(f, captureThis, this, ' |
+ 'Array.prototype.slice.apply(arguments));' |
+ '}' |
+ '}(#, #, #)', DART_CLOSURE_TO_JS(_callDartFunction), f, _captureThis); |
alexandre.ardhuin
2013/10/08 08:36:44
"captureThis" instead of "_captureThis"
justinfagnani
2013/10/10 23:00:07
Done.
|
+} |
+ |
+_callDartFunction(callback, bool captureThis, self, List arguments) { |
+ if (captureThis) { |
+ arguments = [self]..addAll(arguments); |
+ } |
+ var dartArgs = arguments.map(_convertToDart).toList(growable: false); |
+ return _convertToJS(Function.apply(callback, dartArgs)); |
+} |
+ |
+ |
class JsObject implements Serializable<JsObject> { |
final dynamic _jsObject; |
@@ -166,6 +194,14 @@ abstract class Serializable<T> { |
T toJs(); |
} |
+// property added to a Dart object referencing its JS-side DartObject proxy |
+const _DART_OBJECT_PROPERTY_NAME = r'_$dart_dartObject'; |
+const _DART_CLOSURE_PROPERTY_NAME = r'_$dart_dartClosure'; |
+ |
+// property added to a JS object referencing its Dart-side JsObject proxy |
+const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; |
+const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; |
+ |
dynamic _convertToJS(dynamic o) { |
if (o == null) { |
return null; |
@@ -176,12 +212,28 @@ dynamic _convertToJS(dynamic o) { |
} else if (o is Serializable) { |
return _convertToJS(o.toJs()); |
} else if (o is Function) { |
- return _convertToJS(new Callback(o)); |
+ var jsFunction = JS('', '#[#]', o, _JS_FUNCTION_PROPERTY_NAME); |
alexandre.ardhuin
2013/10/08 08:36:44
1. Use _getDartProxy ?
2. _DART_CLOSURE_PROPERTY_N
justinfagnani
2013/10/10 23:00:07
_getDartProxy could maybe work if we used the same
|
+ if (jsFunction == null) { |
+ jsFunction = _convertDartFunction(o); |
+ // set a property on the JS closure referencing the Dart closure |
+ JS('void', 'Object.defineProperty(#, #, { value: #})', jsFunction, |
alexandre.ardhuin
2013/10/08 08:36:44
_JS_FUNCTION_PROPERTY_NAME instead of _DART_CLOSUR
justinfagnani
2013/10/10 23:00:07
same response/reasoning as above
|
+ _DART_CLOSURE_PROPERTY_NAME, o); |
+ // set a property on the Dart closure referencing the JS closure |
+ JS('', '#[#] = #', o, _JS_FUNCTION_PROPERTY_NAME, jsFunction); |
alexandre.ardhuin
2013/10/08 08:36:44
1. Use Object.defineProperty.
2. _DART_CLOSURE_PRO
justinfagnani
2013/10/10 23:00:07
Done for defineProperty
|
+ } |
+ return jsFunction; |
} else { |
- return JS('=Object', 'new DartProxy(#)', o); |
+ var jsProxy = JS('', '#[#]', o, _DART_OBJECT_PROPERTY_NAME); |
alexandre.ardhuin
2013/10/08 08:36:44
Use _getDartProxy ?
justinfagnani
2013/10/10 23:00:07
used _getJsProxy
|
+ if (jsProxy == null) { |
+ jsProxy = JS('', 'new DartObject(#)', o); |
+ JS('', '#[#] = #', o, _DART_OBJECT_PROPERTY_NAME, jsProxy); |
alexandre.ardhuin
2013/10/08 08:36:44
Use Object.defineProperty.
justinfagnani
2013/10/10 23:00:07
Done.
|
+ } |
+ return jsProxy; |
} |
} |
+// converts a Dart object to a reference to a native JS object |
+// which might be a DartObject JS->Dart proxy |
dynamic _convertToDart(dynamic o) { |
if (JS('bool', '# == null', o)) { |
return null; |
@@ -190,10 +242,22 @@ dynamic _convertToDart(dynamic o) { |
JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { |
return o; |
} else if (JS('bool', '# instanceof Function', o)) { |
- return new JsFunction._fromJs(JS('=Object', '#', o)); |
- } else if (JS('bool', '# instanceof DartProxy', o)) { |
+ return _getDartProxy(o, _JS_FUNCTION_PROPERTY_NAME, |
+ (o) => new JsFunction._fromJs(o)); |
+ } else if (JS('bool', '# instanceof DartObject', o)) { |
return JS('var', '#.o', o); |
} else { |
- return new JsObject._fromJs(JS('=Object', '#', o)); |
+ return _getDartProxy(o, _JS_OBJECT_PROPERTY_NAME, |
+ (o) => new JsObject._fromJs(o)); |
} |
-} |
+} |
+ |
+dynamic _getDartProxy(o, String propertyName, createProxy(o)) { |
+ var dartProxy = JS('', '#[#]', o, propertyName); |
+ if (dartProxy == null) { |
+ dartProxy = createProxy(JS('=Object', '#', o)); |
+ JS('void', 'Object.defineProperty(#, #, { value: #})', o, propertyName, |
+ dartProxy); |
+ } |
+ return dartProxy; |
+} |