| 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): add tests and 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); |
| 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(); |
| 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) { |
| 74 // remember this proxy for the JS object |
| 75 _getDartProxy(_jsObject, _DART_OBJECT_PROPERTY_NAME, (o) => this); |
| 76 } |
| 46 | 77 |
| 47 // TODO(vsm): Type constructor as Serializable<JsFunction> when | 78 // TODO(vsm): Type constructor as Serializable<JsFunction> when |
| 48 // dartbug.com/11854 is fixed. | 79 // dartbug.com/11854 is fixed. |
| 49 factory JsObject(var constructor, [List arguments]) { | 80 factory JsObject(var constructor, [List arguments]) { |
| 50 final constr = _convertToJS(constructor); | 81 final constr = _convertToJS(constructor); |
| 51 if (arguments == null) { | 82 if (arguments == null) { |
| 52 return new JsObject._fromJs(JS('=Object', 'new #()', constr)); | 83 return new JsObject._fromJs(JS('=Object', 'new #()', constr)); |
| 53 } | 84 } |
| 54 final args = arguments.map(_convertToJS).toList(); | 85 final args = arguments.map(_convertToJS).toList(); |
| 55 switch (args.length) { | 86 switch (args.length) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 return data.map(_convertDataTree).toList(); | 144 return data.map(_convertDataTree).toList(); |
| 114 } else { | 145 } else { |
| 115 return _convertToJS(data); | 146 return _convertToJS(data); |
| 116 } | 147 } |
| 117 } | 148 } |
| 118 | 149 |
| 119 JsObject toJs() => this; | 150 JsObject toJs() => this; |
| 120 | 151 |
| 121 operator[](key) => | 152 operator[](key) => |
| 122 _convertToDart(JS('=Object', '#[#]', _convertToJS(this), key)); | 153 _convertToDart(JS('=Object', '#[#]', _convertToJS(this), key)); |
| 154 |
| 123 operator[]=(key, value) => JS('void', '#[#]=#', _convertToJS(this), key, | 155 operator[]=(key, value) => JS('void', '#[#]=#', _convertToJS(this), key, |
| 124 _convertToJS(value)); | 156 _convertToJS(value)); |
| 125 | 157 |
| 126 int get hashCode => 0; | 158 int get hashCode => 0; |
| 127 | 159 |
| 128 operator==(other) => other is JsObject && | 160 operator==(other) => other is JsObject && |
| 129 JS('bool', '# === #', _convertToJS(this), _convertToJS(other)); | 161 JS('bool', '# === #', _convertToJS(this), _convertToJS(other)); |
| 130 | 162 |
| 131 bool hasProperty(String property) => JS('bool', '# in #', property, | 163 bool hasProperty(String property) => JS('bool', '# in #', property, |
| 132 _convertToJS(this)); | 164 _convertToJS(this)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 159 apply(thisArg, [List args]) => | 191 apply(thisArg, [List args]) => |
| 160 _convertToDart(JS('=Object', '#.apply(#, #)', _convertToJS(this), | 192 _convertToDart(JS('=Object', '#.apply(#, #)', _convertToJS(this), |
| 161 _convertToJS(thisArg), | 193 _convertToJS(thisArg), |
| 162 args == null ? null : args.map(_convertToJS).toList())); | 194 args == null ? null : args.map(_convertToJS).toList())); |
| 163 } | 195 } |
| 164 | 196 |
| 165 abstract class Serializable<T> { | 197 abstract class Serializable<T> { |
| 166 T toJs(); | 198 T toJs(); |
| 167 } | 199 } |
| 168 | 200 |
| 201 // property added to a Dart object referencing its JS-side DartObject proxy |
| 202 const _DART_OBJECT_PROPERTY_NAME = r'_$dart_dartObject'; |
| 203 const _DART_CLOSURE_PROPERTY_NAME = r'_$dart_dartClosure'; |
| 204 |
| 205 // property added to a JS object referencing its Dart-side JsObject proxy |
| 206 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; |
| 207 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; |
| 208 |
| 209 bool _defineProperty(o, String name, value) { |
| 210 if (JS('bool', 'Object.isExtensible(#)', o)) { |
| 211 try { |
| 212 JS('void', 'Object.defineProperty(#, #, { value: #})', o, name, value); |
| 213 return true; |
| 214 } catch(e) { |
| 215 // object is native and lies about being extensible |
| 216 // see https://bugzilla.mozilla.org/show_bug.cgi?id=775185 |
| 217 } |
| 218 } |
| 219 return false; |
| 220 } |
| 221 |
| 169 dynamic _convertToJS(dynamic o) { | 222 dynamic _convertToJS(dynamic o) { |
| 170 if (o == null) { | 223 if (o == null) { |
| 171 return null; | 224 return null; |
| 172 } else if (o is String || o is num || o is bool) { | 225 } else if (o is String || o is num || o is bool) { |
| 173 return o; | 226 return o; |
| 174 } else if (o is JsObject) { | 227 } else if (o is JsObject) { |
| 175 return o._jsObject; | 228 return o._jsObject; |
| 176 } else if (o is Serializable) { | 229 } else if (o is Serializable) { |
| 177 return _convertToJS(o.toJs()); | 230 return _convertToJS(o.toJs()); |
| 178 } else if (o is Function) { | 231 } else if (o is Function) { |
| 179 return _convertToJS(new Callback(o)); | 232 return _getJsProxy(o, _JS_FUNCTION_PROPERTY_NAME, (o) { |
| 233 var jsFunction = _convertDartFunction(o); |
| 234 // set a property on the JS closure referencing the Dart closure |
| 235 _defineProperty(jsFunction, _DART_CLOSURE_PROPERTY_NAME, o); |
| 236 return jsFunction; |
| 237 }); |
| 180 } else { | 238 } else { |
| 181 return JS('=Object', 'new DartProxy(#)', o); | 239 return _getJsProxy(o, _JS_OBJECT_PROPERTY_NAME, |
| 240 (o) => JS('', 'new DartObject(#)', o)); |
| 182 } | 241 } |
| 183 } | 242 } |
| 184 | 243 |
| 244 dynamic _getJsProxy(o, String propertyName, createProxy(o)) { |
| 245 var jsProxy = JS('', '#[#]', o, propertyName); |
| 246 if (jsProxy == null) { |
| 247 jsProxy = createProxy(o); |
| 248 _defineProperty(o, propertyName, jsProxy); |
| 249 } |
| 250 return jsProxy; |
| 251 } |
| 252 |
| 253 // converts a Dart object to a reference to a native JS object |
| 254 // which might be a DartObject JS->Dart proxy |
| 185 dynamic _convertToDart(dynamic o) { | 255 dynamic _convertToDart(dynamic o) { |
| 186 if (JS('bool', '# == null', o)) { | 256 if (JS('bool', '# == null', o)) { |
| 187 return null; | 257 return null; |
| 188 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || | 258 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || |
| 189 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || | 259 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || |
| 190 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { | 260 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { |
| 191 return o; | 261 return o; |
| 192 } else if (JS('bool', '# instanceof Function', o)) { | 262 } else if (JS('bool', '# instanceof Function', o)) { |
| 193 return new JsFunction._fromJs(JS('=Object', '#', o)); | 263 return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME, |
| 194 } else if (JS('bool', '# instanceof DartProxy', o)) { | 264 (o) => new JsFunction._fromJs(o)); |
| 265 } else if (JS('bool', '# instanceof DartObject', o)) { |
| 195 return JS('var', '#.o', o); | 266 return JS('var', '#.o', o); |
| 196 } else { | 267 } else { |
| 197 return new JsObject._fromJs(JS('=Object', '#', o)); | 268 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, |
| 269 (o) => new JsObject._fromJs(o)); |
| 198 } | 270 } |
| 199 } | 271 } |
| 272 |
| 273 dynamic _getDartProxy(o, String propertyName, createProxy(o)) { |
| 274 var dartProxy = JS('', '#[#]', o, propertyName); |
| 275 if (dartProxy == null) { |
| 276 dartProxy = createProxy(o); |
| 277 _defineProperty(o, propertyName, dartProxy); |
| 278 } |
| 279 return dartProxy; |
| 280 } |
| OLD | NEW |