Chromium Code Reviews| 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 library dart.js; | 5 library dart.js; |
| 6 | 6 |
| 7 import 'dart:_foreign_helper' show JS; | 7 import 'dart:_foreign_helper' show JS, DART_CLOSURE_TO_JS; |
| 8 import 'dart:_js_helper' show Primitives, convertDartClosureToJS; | 8 import 'dart:_js_helper' show Primitives, convertDartClosureToJS; |
| 9 | 9 |
| 10 final JsObject context = new JsObject._fromJs(Primitives.computeGlobalThis()); | 10 final JsObject context = new JsObject._fromJs(Primitives.computeGlobalThis()); |
| 11 | 11 |
| 12 JsObject jsify(dynamic data) => data == null ? null : new JsObject._json(data); | 12 JsObject jsify(dynamic data) => data == null ? null : new JsObject._json(data); |
| 13 | 13 |
| 14 class Callback implements Serializable<JsFunction> { | 14 class Callback implements Serializable<JsFunction> { |
| 15 final Function _f; // here to allow capture in closure | 15 final Function _f; // here to allow capture in closure |
| 16 final bool _withThis; // here to allow capture in closure | 16 final bool _withThis; // here to allow capture in closure |
| 17 dynamic _jsFunction; | 17 dynamic _jsFunction; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 32 _call(thisArg, List args) { | 32 _call(thisArg, List args) { |
| 33 final arguments = new List.from(args); | 33 final arguments = new List.from(args); |
| 34 if (_withThis) arguments.insert(0, thisArg); | 34 if (_withThis) arguments.insert(0, thisArg); |
| 35 final dartArgs = arguments.map(_convertToDart).toList(); | 35 final dartArgs = arguments.map(_convertToDart).toList(); |
| 36 return _convertToJS(Function.apply(_f, dartArgs)); | 36 return _convertToJS(Function.apply(_f, dartArgs)); |
| 37 } | 37 } |
| 38 | 38 |
| 39 JsFunction toJs() => new JsFunction._fromJs(_jsFunction); | 39 JsFunction toJs() => new JsFunction._fromJs(_jsFunction); |
| 40 } | 40 } |
| 41 | 41 |
| 42 /* | |
| 43 * TODO(justinfagnani): make public when we remove Callback. | |
| 44 * | |
| 45 * Returns a [JsFunction] that captures its 'this' binding and calls [f] | |
| 46 * with the value of this passed as the first argument. | |
| 47 */ | |
| 48 JsFunction _captureThis(Function f) => | |
| 49 new JsFunction._fromJs(_convertDartFunction(f, captureThis: true)); | |
| 50 | |
| 51 _convertDartFunction(Function f, {bool captureThis: false}) { | |
| 52 return JS('', | |
| 53 'function(_call, f, captureThis) {' | |
| 54 'return function() {' | |
| 55 'return _call(f, captureThis, this, ' | |
| 56 'Array.prototype.slice.apply(arguments));' | |
| 57 '}' | |
| 58 '}(#, #, #)', 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.
| |
| 59 } | |
| 60 | |
| 61 _callDartFunction(callback, bool captureThis, self, List arguments) { | |
| 62 if (captureThis) { | |
| 63 arguments = [self]..addAll(arguments); | |
| 64 } | |
| 65 var dartArgs = arguments.map(_convertToDart).toList(growable: false); | |
| 66 return _convertToJS(Function.apply(callback, dartArgs)); | |
| 67 } | |
| 68 | |
| 69 | |
| 42 class JsObject implements Serializable<JsObject> { | 70 class JsObject implements Serializable<JsObject> { |
| 43 final dynamic _jsObject; | 71 final dynamic _jsObject; |
| 44 | 72 |
| 45 JsObject._fromJs(this._jsObject); | 73 JsObject._fromJs(this._jsObject); |
| 46 | 74 |
| 47 // TODO(vsm): Type constructor as Serializable<JsFunction> when | 75 // TODO(vsm): Type constructor as Serializable<JsFunction> when |
| 48 // dartbug.com/11854 is fixed. | 76 // dartbug.com/11854 is fixed. |
| 49 factory JsObject(var constructor, [List arguments]) { | 77 factory JsObject(var constructor, [List arguments]) { |
| 50 final constr = _convertToJS(constructor); | 78 final constr = _convertToJS(constructor); |
| 51 if (arguments == null) { | 79 if (arguments == null) { |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 apply(thisArg, [List args]) => | 187 apply(thisArg, [List args]) => |
| 160 _convertToDart(JS('=Object', '#.apply(#, #)', _convertToJS(this), | 188 _convertToDart(JS('=Object', '#.apply(#, #)', _convertToJS(this), |
| 161 _convertToJS(thisArg), | 189 _convertToJS(thisArg), |
| 162 args == null ? null : args.map(_convertToJS).toList())); | 190 args == null ? null : args.map(_convertToJS).toList())); |
| 163 } | 191 } |
| 164 | 192 |
| 165 abstract class Serializable<T> { | 193 abstract class Serializable<T> { |
| 166 T toJs(); | 194 T toJs(); |
| 167 } | 195 } |
| 168 | 196 |
| 197 // property added to a Dart object referencing its JS-side DartObject proxy | |
| 198 const _DART_OBJECT_PROPERTY_NAME = r'_$dart_dartObject'; | |
| 199 const _DART_CLOSURE_PROPERTY_NAME = r'_$dart_dartClosure'; | |
| 200 | |
| 201 // property added to a JS object referencing its Dart-side JsObject proxy | |
| 202 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; | |
| 203 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; | |
| 204 | |
| 169 dynamic _convertToJS(dynamic o) { | 205 dynamic _convertToJS(dynamic o) { |
| 170 if (o == null) { | 206 if (o == null) { |
| 171 return null; | 207 return null; |
| 172 } else if (o is String || o is num || o is bool) { | 208 } else if (o is String || o is num || o is bool) { |
| 173 return o; | 209 return o; |
| 174 } else if (o is JsObject) { | 210 } else if (o is JsObject) { |
| 175 return o._jsObject; | 211 return o._jsObject; |
| 176 } else if (o is Serializable) { | 212 } else if (o is Serializable) { |
| 177 return _convertToJS(o.toJs()); | 213 return _convertToJS(o.toJs()); |
| 178 } else if (o is Function) { | 214 } else if (o is Function) { |
| 179 return _convertToJS(new Callback(o)); | 215 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
| |
| 216 if (jsFunction == null) { | |
| 217 jsFunction = _convertDartFunction(o); | |
| 218 // set a property on the JS closure referencing the Dart closure | |
| 219 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
| |
| 220 _DART_CLOSURE_PROPERTY_NAME, o); | |
| 221 // set a property on the Dart closure referencing the JS closure | |
| 222 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
| |
| 223 } | |
| 224 return jsFunction; | |
| 180 } else { | 225 } else { |
| 181 return JS('=Object', 'new DartProxy(#)', o); | 226 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
| |
| 227 if (jsProxy == null) { | |
| 228 jsProxy = JS('', 'new DartObject(#)', o); | |
| 229 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.
| |
| 230 } | |
| 231 return jsProxy; | |
| 182 } | 232 } |
| 183 } | 233 } |
| 184 | 234 |
| 235 // converts a Dart object to a reference to a native JS object | |
| 236 // which might be a DartObject JS->Dart proxy | |
| 185 dynamic _convertToDart(dynamic o) { | 237 dynamic _convertToDart(dynamic o) { |
| 186 if (JS('bool', '# == null', o)) { | 238 if (JS('bool', '# == null', o)) { |
| 187 return null; | 239 return null; |
| 188 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || | 240 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || |
| 189 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || | 241 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || |
| 190 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { | 242 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { |
| 191 return o; | 243 return o; |
| 192 } else if (JS('bool', '# instanceof Function', o)) { | 244 } else if (JS('bool', '# instanceof Function', o)) { |
| 193 return new JsFunction._fromJs(JS('=Object', '#', o)); | 245 return _getDartProxy(o, _JS_FUNCTION_PROPERTY_NAME, |
| 194 } else if (JS('bool', '# instanceof DartProxy', o)) { | 246 (o) => new JsFunction._fromJs(o)); |
| 247 } else if (JS('bool', '# instanceof DartObject', o)) { | |
| 195 return JS('var', '#.o', o); | 248 return JS('var', '#.o', o); |
| 196 } else { | 249 } else { |
| 197 return new JsObject._fromJs(JS('=Object', '#', o)); | 250 return _getDartProxy(o, _JS_OBJECT_PROPERTY_NAME, |
| 251 (o) => new JsObject._fromJs(o)); | |
| 198 } | 252 } |
| 199 } | 253 } |
| 254 | |
| 255 dynamic _getDartProxy(o, String propertyName, createProxy(o)) { | |
| 256 var dartProxy = JS('', '#[#]', o, propertyName); | |
| 257 if (dartProxy == null) { | |
| 258 dartProxy = createProxy(JS('=Object', '#', o)); | |
| 259 JS('void', 'Object.defineProperty(#, #, { value: #})', o, propertyName, | |
| 260 dartProxy); | |
| 261 } | |
| 262 return dartProxy; | |
| 263 } | |
| OLD | NEW |