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'; | |
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 return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME, |
194 } else if (JS('bool', '# instanceof DartProxy', o)) { | 204 (o) => JsFunction._fromJs(o)); |
alexandre.ardhuin
2013/10/07 19:41:30
"new" is missing.
justinfagnani
2013/10/07 20:17:51
Done.
| |
205 } else if (JS('bool', '# instanceof DartObject', o)) { | |
195 return JS('var', '#.o', o); | 206 return JS('var', '#.o', o); |
196 } else { | 207 } else { |
197 return new JsObject._fromJs(JS('=Object', '#', o)); | 208 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, |
209 (o) => JsObject._fromJs(o)); | |
alexandre.ardhuin
2013/10/07 19:41:30
"new" is missing.
justinfagnani
2013/10/07 20:17:51
Done.
| |
198 } | 210 } |
199 } | 211 } |
212 | |
213 dynamic _getDartProxy(o, String propertyName, createProxy(o)) { | |
214 var dartProxy = JS('', '#[#]', o, propertyName); | |
alexandre.ardhuin
2013/10/07 19:41:30
Indentation size.
justinfagnani
2013/10/07 20:17:51
Done.
| |
215 if (dartProxy == null) { | |
216 dartProxy = createProxy(JS('=Object', '#', o)); | |
217 JS('void', 'Object.defineProperty(#, #, { value: #})', o, propertyName, | |
218 dartProxy); | |
219 } | |
220 return dartProxy; | |
221 } | |
OLD | NEW |