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; |
18 | 18 |
19 Callback._(this._f, this._withThis) { | 19 Callback._(this._f, this._withThis) { |
20 _jsFunction = JS('=Object', r''' | 20 _jsFunction = JS('', r''' |
21 (function(){ | 21 (function(){ |
22 var f = #; | 22 var f = #; |
23 return function(){ | 23 return function(){ |
24 return f(this, Array.prototype.slice.apply(arguments)); | 24 return f(this, Array.prototype.slice.apply(arguments)); |
25 }; | 25 }; |
26 }).apply(this)''', convertDartClosureToJS(_call, 2)); | 26 }).apply(this)''', convertDartClosureToJS(_call, 2)); |
27 } | 27 } |
28 | 28 |
29 factory Callback(Function f) => new Callback._(f, false); | 29 factory Callback(Function f) => new Callback._(f, false); |
30 factory Callback.withThis(Function f) => new Callback._(f, true); | 30 factory Callback.withThis(Function f) => new Callback._(f, true); |
31 | 31 |
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> { |
| 71 // The wrapped JS object. |
43 final dynamic _jsObject; | 72 final dynamic _jsObject; |
44 | 73 |
45 JsObject._fromJs(this._jsObject); | 74 JsObject._fromJs(this._jsObject) { |
| 75 // Remember this proxy for the JS object |
| 76 _getDartProxy(_jsObject, _DART_OBJECT_PROPERTY_NAME, (o) => this); |
| 77 } |
46 | 78 |
47 // TODO(vsm): Type constructor as Serializable<JsFunction> when | 79 // TODO(vsm): Type constructor as Serializable<JsFunction> when |
48 // dartbug.com/11854 is fixed. | 80 // dartbug.com/11854 is fixed. |
49 factory JsObject(var constructor, [List arguments]) { | 81 factory JsObject(constructor, [List arguments]) { |
50 final constr = _convertToJS(constructor); | 82 var constr = _convertToJS(constructor); |
51 if (arguments == null) { | 83 if (arguments == null) { |
52 return new JsObject._fromJs(JS('=Object', 'new #()', constr)); | 84 return new JsObject._fromJs(JS('', 'new #()', constr)); |
53 } | 85 } |
54 final args = arguments.map(_convertToJS).toList(); | 86 // The following code solves the problem of invoking a JavaScript |
55 switch (args.length) { | 87 // constructor with an unknown number arguments. |
56 case 0: | 88 // First bind the constructor to the argument list using bind.apply(). |
57 return new JsObject._fromJs(JS('=Object', 'new #()', constr)); | 89 // The first argument to bind() is the binding of 'this', so add 'null' to |
58 case 1: | 90 // the arguments list passed to apply(). |
59 return new JsObject._fromJs(JS('=Object', 'new #(#)', constr, args[0])); | 91 // After that, use the JavaScript 'new' operator which overrides any binding |
60 case 2: | 92 // of 'this' with the new instance. |
61 return new JsObject._fromJs(JS('=Object', 'new #(#,#)', constr, args[0], | 93 var args = [null]..addAll(arguments.map(_convertToJS)); |
62 args[1])); | 94 var factoryFunction = JS('', '#.bind.apply(#, #)', constr, constr, args); |
63 case 3: | 95 // Without this line, calling factoryFunction as a constructor throws |
64 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#)', constr, | 96 JS('String', 'String(#)', factoryFunction); |
65 args[0], args[1], args[2])); | 97 return new JsObject._fromJs(JS('', 'new #()', factoryFunction)); |
66 case 4: | |
67 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#)', constr, | |
68 args[0], args[1], args[2], args[3])); | |
69 case 5: | |
70 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#)', constr, | |
71 args[0], args[1], args[2], args[3], args[4])); | |
72 case 6: | |
73 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#,#)', constr, | |
74 args[0], args[1], args[2], args[3], args[4], args[5])); | |
75 case 7: | |
76 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#,#,#)', | |
77 constr, args[0], args[1], args[2], args[3], args[4], args[5], | |
78 args[6])); | |
79 case 8: | |
80 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#,#,#,#)', | |
81 constr, args[0], args[1], args[2], args[3], args[4], args[5], | |
82 args[6], args[7])); | |
83 case 9: | |
84 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#,#,#,#,#)', | |
85 constr, args[0], args[1], args[2], args[3], args[4], args[5], | |
86 args[6], args[7], args[8])); | |
87 case 10: | |
88 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#,#,#,#,#,#)', | |
89 constr, args[0], args[1], args[2], args[3], args[4], args[5], | |
90 args[6], args[7], args[8], args[9])); | |
91 } | |
92 return new JsObject._fromJs(JS('=Object', r'''(function(){ | |
93 var Type = function(){}; | |
94 Type.prototype = #.prototype; | |
95 var instance = new Type(); | |
96 ret = #.apply(instance, #); | |
97 ret = Object(ret) === ret ? ret : instance; | |
98 return ret; | |
99 })()''', constr, constr, args)); | |
100 } | 98 } |
101 | 99 |
102 factory JsObject._json(data) => new JsObject._fromJs(_convertDataTree(data)); | 100 factory JsObject._json(data) => new JsObject._fromJs(_convertDataTree(data)); |
103 | 101 |
104 static _convertDataTree(data) { | 102 static _convertDataTree(data) { |
105 if (data is Map) { | 103 if (data is Map) { |
106 final convertedData = JS('=Object', '{}'); | 104 final convertedData = JS('=Object', '{}'); |
107 for (var key in data.keys) { | 105 for (var key in data.keys) { |
108 JS('=Object', '#[#]=#', convertedData, key, | 106 JS('=Object', '#[#]=#', convertedData, key, |
109 _convertDataTree(data[key])); | 107 _convertDataTree(data[key])); |
110 } | 108 } |
111 return convertedData; | 109 return convertedData; |
112 } else if (data is Iterable) { | 110 } else if (data is Iterable) { |
113 return data.map(_convertDataTree).toList(); | 111 return data.map(_convertDataTree).toList(); |
114 } else { | 112 } else { |
115 return _convertToJS(data); | 113 return _convertToJS(data); |
116 } | 114 } |
117 } | 115 } |
118 | 116 |
119 JsObject toJs() => this; | 117 JsObject toJs() => this; |
120 | 118 |
121 operator[](key) => | 119 /** |
122 _convertToDart(JS('=Object', '#[#]', _convertToJS(this), key)); | 120 * Returns the value associated with [key] from the proxied JavaScript |
123 operator[]=(key, value) => JS('void', '#[#]=#', _convertToJS(this), key, | 121 * object. |
124 _convertToJS(value)); | 122 * |
| 123 * [key] must either be a [String] or [int]. |
| 124 */ |
| 125 // TODO(justinfagnani): rename key/name to property |
| 126 dynamic operator[](key) { |
| 127 if (key is! String && key is! int) { |
| 128 throw new ArgumentError("key is not a String or int"); |
| 129 } |
| 130 return _convertToDart(JS('', '#[#]', _jsObject, key)); |
| 131 } |
| 132 |
| 133 /** |
| 134 * Sets the value associated with [key] from the proxied JavaScript |
| 135 * object. |
| 136 * |
| 137 * [key] must either be a [String] or [int]. |
| 138 */ |
| 139 operator[]=(key, value) { |
| 140 if (key is! String && key is! int) { |
| 141 throw new ArgumentError("key is not a String or int"); |
| 142 } |
| 143 JS('', '#[#]=#', _jsObject, key, _convertToJS(value)); |
| 144 } |
125 | 145 |
126 int get hashCode => 0; | 146 int get hashCode => 0; |
127 | 147 |
128 operator==(other) => other is JsObject && | 148 bool operator==(other) => other is JsObject && |
129 JS('bool', '# === #', _convertToJS(this), _convertToJS(other)); | 149 JS('bool', '# === #', _jsObject, other._jsObject); |
130 | 150 |
131 bool hasProperty(String property) => JS('bool', '# in #', property, | 151 bool hasProperty(name) { |
132 _convertToJS(this)); | 152 if (name is! String && name is! int) { |
| 153 throw new ArgumentError("name is not a String or int"); |
| 154 } |
| 155 return JS('bool', '# in #', name, _jsObject); |
| 156 } |
133 | 157 |
134 void deleteProperty(String name) { | 158 void deleteProperty(name) { |
135 JS('void', 'delete #[#]', _convertToJS(this), name); | 159 if (name is! String && name is! int) { |
| 160 throw new ArgumentError("name is not a String or int"); |
| 161 } |
| 162 JS('bool', 'delete #[#]', _jsObject, name); |
136 } | 163 } |
137 | 164 |
138 // TODO(vsm): Type type as Serializable<JsFunction> when | 165 // TODO(vsm): Type type as Serializable<JsFunction> when |
139 // dartbug.com/11854 is fixed. | 166 // dartbug.com/11854 is fixed. |
140 bool instanceof(var type) => | 167 bool instanceof(type) { |
141 JS('bool', '# instanceof #', _convertToJS(this), _convertToJS(type)); | 168 return JS('bool', '# instanceof #', _jsObject, _convertToJS(type)); |
| 169 } |
142 | 170 |
143 String toString() { | 171 String toString() { |
144 try { | 172 try { |
145 return JS('String', '#.toString()', _convertToJS(this)); | 173 return JS('String', 'String(#)', _jsObject); |
146 } catch(e) { | 174 } catch(e) { |
147 return super.toString(); | 175 return super.toString(); |
148 } | 176 } |
149 } | 177 } |
150 | 178 |
151 callMethod(String name, [List args]) => | 179 dynamic callMethod(name, [List args]) { |
152 _convertToDart(JS('=Object', '#[#].apply(#, #)', _convertToJS(this), name, | 180 if (name is! String && name is! int) { |
153 _convertToJS(this), | 181 throw new ArgumentError("name is not a String or int"); |
154 args == null ? null : args.map(_convertToJS).toList())); | 182 } |
| 183 return _convertToDart(JS('', '#[#].apply(#, #)', _jsObject, name, |
| 184 _jsObject, |
| 185 args == null ? null : args.map(_convertToJS).toList())); |
| 186 } |
155 } | 187 } |
156 | 188 |
157 class JsFunction extends JsObject implements Serializable<JsFunction> { | 189 class JsFunction extends JsObject implements Serializable<JsFunction> { |
| 190 |
158 JsFunction._fromJs(jsObject) : super._fromJs(jsObject); | 191 JsFunction._fromJs(jsObject) : super._fromJs(jsObject); |
| 192 |
159 apply(thisArg, [List args]) => | 193 apply(thisArg, [List args]) => |
160 _convertToDart(JS('=Object', '#.apply(#, #)', _convertToJS(this), | 194 _convertToDart(JS('', '#.apply(#, #)', _jsObject, |
161 _convertToJS(thisArg), | 195 _convertToJS(thisArg), |
162 args == null ? null : args.map(_convertToJS).toList())); | 196 args == null ? null : args.map(_convertToJS).toList())); |
163 } | 197 } |
164 | 198 |
165 abstract class Serializable<T> { | 199 abstract class Serializable<T> { |
166 T toJs(); | 200 T toJs(); |
167 } | 201 } |
168 | 202 |
| 203 // property added to a Dart object referencing its JS-side DartObject proxy |
| 204 const _DART_OBJECT_PROPERTY_NAME = r'_$dart_dartObject'; |
| 205 const _DART_CLOSURE_PROPERTY_NAME = r'_$dart_dartClosure'; |
| 206 |
| 207 // property added to a JS object referencing its Dart-side JsObject proxy |
| 208 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; |
| 209 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; |
| 210 |
| 211 bool _defineProperty(o, String name, value) { |
| 212 if (JS('bool', 'Object.isExtensible(#)', o)) { |
| 213 try { |
| 214 JS('void', 'Object.defineProperty(#, #, { value: #})', o, name, value); |
| 215 return true; |
| 216 } catch(e) { |
| 217 // object is native and lies about being extensible |
| 218 // see https://bugzilla.mozilla.org/show_bug.cgi?id=775185 |
| 219 } |
| 220 } |
| 221 return false; |
| 222 } |
| 223 |
169 dynamic _convertToJS(dynamic o) { | 224 dynamic _convertToJS(dynamic o) { |
170 if (o == null) { | 225 if (o == null) { |
171 return null; | 226 return null; |
172 } else if (o is String || o is num || o is bool) { | 227 } else if (o is String || o is num || o is bool) { |
173 return o; | 228 return o; |
174 } else if (o is JsObject) { | 229 } else if (o is JsObject) { |
175 return o._jsObject; | 230 return o._jsObject; |
176 } else if (o is Serializable) { | 231 } else if (o is Serializable) { |
177 return _convertToJS(o.toJs()); | 232 return _convertToJS(o.toJs()); |
178 } else if (o is Function) { | 233 } else if (o is Function) { |
179 return _convertToJS(new Callback(o)); | 234 return _getJsProxy(o, _JS_FUNCTION_PROPERTY_NAME, (o) { |
| 235 var jsFunction = _convertDartFunction(o); |
| 236 // set a property on the JS closure referencing the Dart closure |
| 237 _defineProperty(jsFunction, _DART_CLOSURE_PROPERTY_NAME, o); |
| 238 return jsFunction; |
| 239 }); |
180 } else { | 240 } else { |
181 return JS('=Object', 'new DartProxy(#)', o); | 241 return _getJsProxy(o, _JS_OBJECT_PROPERTY_NAME, |
| 242 (o) => JS('', 'new DartObject(#)', o)); |
182 } | 243 } |
183 } | 244 } |
184 | 245 |
185 dynamic _convertToDart(dynamic o) { | 246 Object _getJsProxy(o, String propertyName, createProxy(o)) { |
186 if (JS('bool', '# == null', o)) { | 247 var jsProxy = JS('', '#[#]', o, propertyName); |
187 return null; | 248 if (jsProxy == null) { |
188 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || | 249 jsProxy = createProxy(o); |
189 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || | 250 _defineProperty(o, propertyName, jsProxy); |
190 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { | 251 } |
| 252 return jsProxy; |
| 253 } |
| 254 |
| 255 // converts a Dart object to a reference to a native JS object |
| 256 // which might be a DartObject JS->Dart proxy |
| 257 Object _convertToDart(o) { |
| 258 if (JS('bool', '# == null', o) || |
| 259 JS('bool', 'typeof # == "string"', o) || |
| 260 JS('bool', 'typeof # == "number"', o) || |
| 261 JS('bool', 'typeof # == "boolean"', o)) { |
191 return o; | 262 return o; |
192 } else if (JS('bool', '# instanceof Function', o)) { | 263 } else if (JS('bool', 'typeof # == "function"', o)) { |
193 return new JsFunction._fromJs(JS('=Object', '#', o)); | 264 return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME, |
194 } else if (JS('bool', '# instanceof DartProxy', o)) { | 265 (o) => new JsFunction._fromJs(o)); |
195 return JS('var', '#.o', o); | 266 } else if (JS('bool', '#.constructor === DartObject', o)) { |
| 267 return JS('', '#.o', o); |
196 } else { | 268 } else { |
197 return new JsObject._fromJs(JS('=Object', '#', o)); | 269 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, |
| 270 (o) => new JsObject._fromJs(o)); |
198 } | 271 } |
199 } | 272 } |
| 273 |
| 274 Object _getDartProxy(o, String propertyName, createProxy(o)) { |
| 275 var dartProxy = JS('', '#[#]', o, propertyName); |
| 276 if (dartProxy == null) { |
| 277 dartProxy = createProxy(o); |
| 278 _defineProperty(o, propertyName, dartProxy); |
| 279 } |
| 280 return dartProxy; |
| 281 } |
OLD | NEW |