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(growable: false); | |
ahe
2013/10/11 12:54:40
Setting growable to false doesn't win you anything
justinfagnani
2013/10/11 18:47:09
Done.
| |
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 _defineProperty(o, String name, value) => | |
210 JS('void', 'Object.defineProperty(#, #, { value: #})', o, name, value); | |
211 | |
169 dynamic _convertToJS(dynamic o) { | 212 dynamic _convertToJS(dynamic o) { |
170 if (o == null) { | 213 if (o == null) { |
171 return null; | 214 return null; |
172 } else if (o is String || o is num || o is bool) { | 215 } else if (o is String || o is num || o is bool) { |
173 return o; | 216 return o; |
174 } else if (o is JsObject) { | 217 } else if (o is JsObject) { |
175 return o._jsObject; | 218 return o._jsObject; |
176 } else if (o is Serializable) { | 219 } else if (o is Serializable) { |
177 return _convertToJS(o.toJs()); | 220 return _convertToJS(o.toJs()); |
178 } else if (o is Function) { | 221 } else if (o is Function) { |
179 return _convertToJS(new Callback(o)); | 222 return _getJsProxy(o, _JS_FUNCTION_PROPERTY_NAME, (o) { |
223 var jsFunction = _convertDartFunction(o); | |
224 // set a property on the JS closure referencing the Dart closure | |
225 _defineProperty(jsFunction, _DART_CLOSURE_PROPERTY_NAME, o); | |
226 return jsFunction; | |
227 }); | |
180 } else { | 228 } else { |
181 return JS('=Object', 'new DartProxy(#)', o); | 229 return _getJsProxy(o, _JS_OBJECT_PROPERTY_NAME, |
230 (o) => JS('', 'new DartObject(#)', o)); | |
182 } | 231 } |
183 } | 232 } |
184 | 233 |
234 dynamic _getJsProxy(o, String propertyName, createProxy(o)) { | |
235 var jsProxy = JS('', '#[#]', o, propertyName); | |
236 if (jsProxy == null) { | |
237 jsProxy = createProxy(o); | |
238 _defineProperty(o, propertyName, jsProxy); | |
239 } | |
240 return jsProxy; | |
241 } | |
242 | |
243 // converts a Dart object to a reference to a native JS object | |
244 // which might be a DartObject JS->Dart proxy | |
185 dynamic _convertToDart(dynamic o) { | 245 dynamic _convertToDart(dynamic o) { |
186 if (JS('bool', '# == null', o)) { | 246 if (JS('bool', '# == null', o)) { |
187 return null; | 247 return null; |
188 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || | 248 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || |
189 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || | 249 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || |
190 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { | 250 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { |
191 return o; | 251 return o; |
192 } else if (JS('bool', '# instanceof Function', o)) { | 252 } else if (JS('bool', '# instanceof Function', o)) { |
193 return new JsFunction._fromJs(JS('=Object', '#', o)); | 253 return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME, |
194 } else if (JS('bool', '# instanceof DartProxy', o)) { | 254 (o) => new JsFunction._fromJs(o)); |
255 } else if (JS('bool', '# instanceof DartObject', o)) { | |
195 return JS('var', '#.o', o); | 256 return JS('var', '#.o', o); |
196 } else { | 257 } else { |
197 return new JsObject._fromJs(JS('=Object', '#', o)); | 258 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, |
259 (o) => new JsObject._fromJs(o)); | |
198 } | 260 } |
199 } | 261 } |
262 | |
263 dynamic _getDartProxy(o, String propertyName, createProxy(o)) { | |
264 var dartProxy = JS('', '#[#]', o, propertyName); | |
265 if (dartProxy == null) { | |
266 dartProxy = createProxy(JS('=Object', '#', o)); | |
ahe
2013/10/11 12:54:40
Is JS('=Object', '#', o) necessary?
justinfagnani
2013/10/11 18:47:09
This existed before, but if you don't think it is,
| |
267 _defineProperty(o, propertyName, dartProxy); | |
268 } | |
269 return dartProxy; | |
270 } | |
OLD | NEW |