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; |
| 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()); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 apply(thisArg, [List args]) => | 159 apply(thisArg, [List args]) => |
| 160 _convertToDart(JS('=Object', '#.apply(#, #)', _convertToJS(this), | 160 _convertToDart(JS('=Object', '#.apply(#, #)', _convertToJS(this), |
| 161 _convertToJS(thisArg), | 161 _convertToJS(thisArg), |
| 162 args == null ? null : args.map(_convertToJS).toList())); | 162 args == null ? null : args.map(_convertToJS).toList())); |
| 163 } | 163 } |
| 164 | 164 |
| 165 abstract class Serializable<T> { | 165 abstract class Serializable<T> { |
| 166 T toJs(); | 166 T toJs(); |
| 167 } | 167 } |
| 168 | 168 |
| 169 // property added to a Dart object referencing its JS-side DartObject proxy | |
| 170 const _DART_OBJECT_PROPERTY_NAME = r'_$dart_dartObject'; | |
|
justinfagnani
2013/10/06 00:37:45
Peter, how do I generate a unique name per-isolate
| |
| 171 const _DART_CLOSURE_PROPERTY_NAME = r'_$dart_dartClosure'; | |
| 172 | |
| 173 // property added to a JS object referencing its Dart-side JsObject proxy | |
| 174 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; | |
| 175 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; | |
| 176 | |
| 169 dynamic _convertToJS(dynamic o) { | 177 dynamic _convertToJS(dynamic o) { |
| 170 if (o == null) { | 178 if (o == null) { |
| 171 return null; | 179 return null; |
| 172 } else if (o is String || o is num || o is bool) { | 180 } else if (o is String || o is num || o is bool) { |
| 173 return o; | 181 return o; |
| 174 } else if (o is JsObject) { | 182 } else if (o is JsObject) { |
| 175 return o._jsObject; | 183 return o._jsObject; |
| 176 } else if (o is Serializable) { | 184 } else if (o is Serializable) { |
| 177 return _convertToJS(o.toJs()); | 185 return _convertToJS(o.toJs()); |
| 178 } else if (o is Function) { | 186 } else if (o is Function) { |
| 179 return _convertToJS(new Callback(o)); | 187 return _convertToJS(new Callback(o)); |
| 180 } else { | 188 } else { |
| 181 return JS('=Object', 'new DartProxy(#)', o); | 189 return JS('=Object', 'new DartObject(#)', o); |
| 182 } | 190 } |
| 183 } | 191 } |
| 184 | 192 |
| 193 // converts a Dart object to a reference to a native JS object | |
| 194 // which might be a DartObject JS->Dart proxy | |
| 185 dynamic _convertToDart(dynamic o) { | 195 dynamic _convertToDart(dynamic o) { |
| 186 if (JS('bool', '# == null', o)) { | 196 if (JS('bool', '# == null', o)) { |
| 187 return null; | 197 return null; |
| 188 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || | 198 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || |
| 189 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || | 199 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || |
| 190 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { | 200 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { |
| 191 return o; | 201 return o; |
| 192 } else if (JS('bool', '# instanceof Function', o)) { | 202 } else if (JS('bool', '# instanceof Function', o)) { |
| 193 return new JsFunction._fromJs(JS('=Object', '#', o)); | 203 var dartClosure = JS('', '#[#]', o, _DART_CLOSURE_PROPERTY_NAME); |
|
alexandre.ardhuin
2013/10/07 08:56:54
Extract a method for this case and default case ?
justinfagnani
2013/10/07 17:49:13
Good idea. Done.
| |
| 194 } else if (JS('bool', '# instanceof DartProxy', o)) { | 204 if (dartClosure == null) { |
| 205 dartClosure = new JsFunction._fromJs(o); | |
| 206 JS('void', 'Object.defineProperty(#, #, { value: #})', o, | |
| 207 _DART_CLOSURE_PROPERTY_NAME, dartClosure); | |
| 208 } | |
| 209 return dartClosure; | |
| 210 } else if (JS('bool', '# instanceof DartObject', o)) { | |
| 195 return JS('var', '#.o', o); | 211 return JS('var', '#.o', o); |
| 196 } else { | 212 } else { |
| 197 return new JsObject._fromJs(JS('=Object', '#', o)); | 213 var dartProxy = JS('', '#[#]', o, _JS_OBJECT_PROPERTY_NAME); |
| 214 if (dartProxy == null) { | |
| 215 // check the registered proxy factories to see if we should instanciate | |
| 216 // a typed interface | |
| 217 dartProxy = new JsObject._fromJs(JS('=Object', '#', o)); | |
|
alexandre.ardhuin
2013/10/07 08:56:54
I don't remember why we used `JS('=Object', '#', o
justinfagnani
2013/10/07 17:49:13
Peter can shed light here, but I've seen similar t
| |
| 218 JS('void', 'Object.defineProperty(#, #, { value: #})', o, | |
| 219 _JS_OBJECT_PROPERTY_NAME, dartProxy); | |
| 220 } | |
| 221 return dartProxy; | |
| 198 } | 222 } |
| 199 } | 223 } |
| OLD | NEW |