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:html' show Blob, ImageData, Node; | |
| 8 import 'dart:indexed_db' show KeyRange; | |
| 9 import 'dart:collection' show HashMap; | |
| 7 import 'dart:_foreign_helper' show JS, DART_CLOSURE_TO_JS; | 10 import 'dart:_foreign_helper' show JS, DART_CLOSURE_TO_JS; |
| 11 import 'dart:_interceptors' show JavaScriptObject, UnknownJavaScriptObject; | |
| 8 import 'dart:_js_helper' show Primitives, convertDartClosureToJS; | 12 import 'dart:_js_helper' show Primitives, convertDartClosureToJS; |
| 9 | 13 |
| 10 final JsObject context = new JsObject._fromJs(Primitives.computeGlobalThis()); | 14 final JsObject context = new JsObject._fromJs(Primitives.computeGlobalThis()); |
| 11 | 15 |
| 12 JsObject jsify(dynamic data) => data == null ? null : new JsObject._json(data); | |
| 13 | |
| 14 class Callback implements Serializable<JsFunction> { | |
| 15 final Function _f; // here to allow capture in closure | |
| 16 final bool _withThis; // here to allow capture in closure | |
| 17 dynamic _jsFunction; | |
| 18 | |
| 19 Callback._(this._f, this._withThis) { | |
| 20 _jsFunction = JS('', r''' | |
| 21 (function(){ | |
| 22 var f = #; | |
| 23 return function(){ | |
| 24 return f(this, Array.prototype.slice.apply(arguments)); | |
| 25 }; | |
| 26 }).apply(this)''', convertDartClosureToJS(_call, 2)); | |
| 27 } | |
| 28 | |
| 29 factory Callback(Function f) => new Callback._(f, false); | |
| 30 factory Callback.withThis(Function f) => new Callback._(f, true); | |
| 31 | |
| 32 _call(thisArg, List args) { | |
| 33 final arguments = new List.from(args); | |
| 34 if (_withThis) arguments.insert(0, thisArg); | |
| 35 final dartArgs = arguments.map(_convertToDart).toList(); | |
| 36 return _convertToJS(Function.apply(_f, dartArgs)); | |
| 37 } | |
| 38 | |
| 39 JsFunction toJs() => new JsFunction._fromJs(_jsFunction); | |
| 40 } | |
| 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}) { | 16 _convertDartFunction(Function f, {bool captureThis: false}) { |
| 52 return JS('', | 17 return JS('', |
| 53 'function(_call, f, captureThis) {' | 18 'function(_call, f, captureThis) {' |
| 54 'return function() {' | 19 'return function() {' |
| 55 'return _call(f, captureThis, this, ' | 20 'return _call(f, captureThis, this, ' |
| 56 'Array.prototype.slice.apply(arguments));' | 21 'Array.prototype.slice.apply(arguments));' |
| 57 '}' | 22 '}' |
| 58 '}(#, #, #)', DART_CLOSURE_TO_JS(_callDartFunction), f, captureThis); | 23 '}(#, #, #)', DART_CLOSURE_TO_JS(_callDartFunction), f, captureThis); |
| 59 } | 24 } |
| 60 | 25 |
| 61 _callDartFunction(callback, bool captureThis, self, List arguments) { | 26 _callDartFunction(callback, bool captureThis, self, List arguments) { |
| 62 if (captureThis) { | 27 if (captureThis) { |
| 63 arguments = [self]..addAll(arguments); | 28 arguments = [self]..addAll(arguments); |
| 64 } | 29 } |
| 65 var dartArgs = arguments.map(_convertToDart).toList(); | 30 var dartArgs = arguments.map(_convertToDart).toList(); |
| 66 return _convertToJS(Function.apply(callback, dartArgs)); | 31 return _convertToJS(Function.apply(callback, dartArgs)); |
| 67 } | 32 } |
| 68 | 33 |
| 69 | 34 |
| 70 class JsObject implements Serializable<JsObject> { | 35 class JsObject { |
| 71 // The wrapped JS object. | 36 // The wrapped JS object. |
| 72 final dynamic _jsObject; | 37 final dynamic _jsObject; |
| 73 | 38 |
| 74 JsObject._fromJs(this._jsObject) { | 39 JsObject._fromJs(this._jsObject) { |
| 40 assert(_jsObject != null); | |
| 75 // Remember this proxy for the JS object | 41 // Remember this proxy for the JS object |
| 76 _getDartProxy(_jsObject, _DART_OBJECT_PROPERTY_NAME, (o) => this); | 42 _getDartProxy(_jsObject, _DART_OBJECT_PROPERTY_NAME, (o) => this); |
| 77 } | 43 } |
| 78 | 44 |
| 79 // TODO(vsm): Type constructor as Serializable<JsFunction> when | 45 /** |
| 80 // dartbug.com/11854 is fixed. | 46 * Expert users only: |
| 47 * Use this constructor only if you wish to get access to JS expandos | |
| 48 * attached to a WebKit native object such as a Node. | |
| 49 * An exception will be thrown if a primitive type is passed in passing one | |
| 50 * of these types to this method indicates an error. | |
| 51 */ | |
| 52 factory JsObject.fromDartObject(Object object) { | |
| 53 if (object is num || object is String || object is bool || object == null) { | |
| 54 throw new IllegalArgumentException( | |
| 55 "object cannot be a num, string, bool, or null"); | |
| 56 } | |
| 57 return new JsObject._fromJs(_convertToJS(object)); | |
| 58 } | |
| 59 | |
| 60 /** | |
| 61 * Converts a json-like [data] to a JavaScript map or array and return a | |
| 62 * [JsObject] to it. | |
| 63 */ | |
| 64 factory JsObject.jsify(Object object) { | |
| 65 if ((object is! Map) && (object is! Iterable)) { | |
| 66 throw new IllegalArgumentException("object must be a Map or Iterable"); | |
| 67 } | |
| 68 return new JsObject._fromJs(_convertDataTree(object)); | |
| 69 } | |
| 70 | |
| 81 factory JsObject(constructor, [List arguments]) { | 71 factory JsObject(constructor, [List arguments]) { |
| 82 var constr = _convertToJS(constructor); | 72 var constr = _convertToJS(constructor); |
| 83 if (arguments == null) { | 73 if (arguments == null) { |
| 84 return new JsObject._fromJs(JS('', 'new #()', constr)); | 74 return new JsObject._fromJs(JS('', 'new #()', constr)); |
| 85 } | 75 } |
| 86 // The following code solves the problem of invoking a JavaScript | 76 // The following code solves the problem of invoking a JavaScript |
| 87 // constructor with an unknown number arguments. | 77 // constructor with an unknown number arguments. |
| 88 // First bind the constructor to the argument list using bind.apply(). | 78 // First bind the constructor to the argument list using bind.apply(). |
| 89 // The first argument to bind() is the binding of 'this', so add 'null' to | 79 // The first argument to bind() is the binding of 'this', so add 'null' to |
| 90 // the arguments list passed to apply(). | 80 // the arguments list passed to apply(). |
| 91 // After that, use the JavaScript 'new' operator which overrides any binding | 81 // After that, use the JavaScript 'new' operator which overrides any binding |
| 92 // of 'this' with the new instance. | 82 // of 'this' with the new instance. |
| 93 var args = [null]..addAll(arguments.map(_convertToJS)); | 83 var args = [null]..addAll(arguments.map(_convertToJS)); |
| 94 var factoryFunction = JS('', '#.bind.apply(#, #)', constr, constr, args); | 84 var factoryFunction = JS('', '#.bind.apply(#, #)', constr, constr, args); |
| 95 // Without this line, calling factoryFunction as a constructor throws | 85 // Without this line, calling factoryFunction as a constructor throws |
| 96 JS('String', 'String(#)', factoryFunction); | 86 JS('String', 'String(#)', factoryFunction); |
| 97 return new JsObject._fromJs(JS('', 'new #()', factoryFunction)); | 87 // This could return an UnknownJavaScriptObject, or a native |
| 88 // object for which there is an interceptor | |
| 89 var jsObj = JS('JavaScriptObject', 'new #()', factoryFunction); | |
| 90 return new JsObject._fromJs(jsObj); | |
| 98 } | 91 } |
| 99 | 92 |
| 100 factory JsObject._json(data) => new JsObject._fromJs(_convertDataTree(data)); | 93 // TODO: handle cycles |
| 94 static _convertDataTree(data) { | |
| 95 var _convertedObjects = new HashMap.identity(); | |
| 101 | 96 |
| 102 static _convertDataTree(data) { | 97 _convert(o) { |
| 103 if (data is Map) { | 98 if (_convertedObjects.containsKey(o)) { |
| 104 final convertedData = JS('=Object', '{}'); | 99 return _convertedObjects[o]; |
| 105 for (var key in data.keys) { | |
| 106 JS('=Object', '#[#]=#', convertedData, key, | |
| 107 _convertDataTree(data[key])); | |
| 108 } | 100 } |
| 109 return convertedData; | 101 if (o is Map) { |
| 110 } else if (data is Iterable) { | 102 final convertedMap = JS('=Object', '{}'); |
| 111 return data.map(_convertDataTree).toList(); | 103 _convertedObjects[o] = convertedMap; |
| 112 } else { | 104 for (var key in o.keys) { |
| 113 return _convertToJS(data); | 105 JS('=Object', '#[#]=#', convertedMap, key, _convert(o[key])); |
| 106 } | |
| 107 return convertedMap; | |
| 108 } else if (o is Iterable) { | |
| 109 var convertedList = []; | |
| 110 _convertedObjects[o] = convertedList; | |
| 111 convertedList.addAll(o.map(_convert)); | |
| 112 return convertedList; | |
| 113 } else { | |
| 114 return _convertToJS(o); | |
| 115 } | |
| 114 } | 116 } |
| 117 | |
| 118 return _convert(data); | |
| 115 } | 119 } |
| 116 | 120 |
| 117 JsObject toJs() => this; | |
| 118 | |
| 119 /** | 121 /** |
| 120 * Returns the value associated with [key] from the proxied JavaScript | 122 * Returns the value associated with [key] from the proxied JavaScript |
| 121 * object. | 123 * object. |
| 122 * | 124 * |
| 123 * [key] must either be a [String] or [int]. | 125 * [key] must either be a [String] or [num]. |
| 124 */ | 126 */ |
| 125 // TODO(justinfagnani): rename key/name to property | 127 // TODO(justinfagnani): rename key/name to property |
| 126 dynamic operator[](key) { | 128 dynamic operator[](key) { |
| 127 if (key is! String && key is! int) { | 129 if (key is! String && key is! num) { |
| 128 throw new ArgumentError("key is not a String or int"); | 130 throw new ArgumentError("key is not a String or num"); |
| 129 } | 131 } |
| 130 return _convertToDart(JS('', '#[#]', _jsObject, key)); | 132 return _convertToDart(JS('', '#[#]', _jsObject, key)); |
| 131 } | 133 } |
| 132 | 134 |
| 133 /** | 135 /** |
| 134 * Sets the value associated with [key] from the proxied JavaScript | 136 * Sets the value associated with [key] from the proxied JavaScript |
| 135 * object. | 137 * object. |
| 136 * | 138 * |
| 137 * [key] must either be a [String] or [int]. | 139 * [key] must either be a [String] or [num]. |
| 138 */ | 140 */ |
| 139 operator[]=(key, value) { | 141 operator[]=(key, value) { |
| 140 if (key is! String && key is! int) { | 142 if (key is! String && key is! num) { |
| 141 throw new ArgumentError("key is not a String or int"); | 143 throw new ArgumentError("key is not a String or num"); |
| 142 } | 144 } |
| 143 JS('', '#[#]=#', _jsObject, key, _convertToJS(value)); | 145 JS('', '#[#]=#', _jsObject, key, _convertToJS(value)); |
| 144 } | 146 } |
| 145 | 147 |
| 146 int get hashCode => 0; | 148 int get hashCode => 0; |
| 147 | 149 |
| 148 bool operator==(other) => other is JsObject && | 150 bool operator==(other) => other is JsObject && |
| 149 JS('bool', '# === #', _jsObject, other._jsObject); | 151 JS('bool', '# === #', _jsObject, other._jsObject); |
| 150 | 152 |
| 151 bool hasProperty(name) { | 153 bool hasProperty(name) { |
| 152 if (name is! String && name is! int) { | 154 if (name is! String && name is! num) { |
| 153 throw new ArgumentError("name is not a String or int"); | 155 throw new ArgumentError("name is not a String or num"); |
| 154 } | 156 } |
| 155 return JS('bool', '# in #', name, _jsObject); | 157 return JS('bool', '# in #', name, _jsObject); |
| 156 } | 158 } |
| 157 | 159 |
| 158 void deleteProperty(name) { | 160 void deleteProperty(name) { |
| 159 if (name is! String && name is! int) { | 161 if (name is! String && name is! num) { |
| 160 throw new ArgumentError("name is not a String or int"); | 162 throw new ArgumentError("name is not a String or num"); |
| 161 } | 163 } |
| 162 JS('bool', 'delete #[#]', _jsObject, name); | 164 JS('bool', 'delete #[#]', _jsObject, name); |
| 163 } | 165 } |
| 164 | 166 |
| 165 // TODO(vsm): Type type as Serializable<JsFunction> when | |
| 166 // dartbug.com/11854 is fixed. | |
| 167 bool instanceof(type) { | 167 bool instanceof(type) { |
| 168 return JS('bool', '# instanceof #', _jsObject, _convertToJS(type)); | 168 return JS('bool', '# instanceof #', _jsObject, _convertToJS(type)); |
| 169 } | 169 } |
| 170 | 170 |
| 171 String toString() { | 171 String toString() { |
| 172 try { | 172 try { |
| 173 return JS('String', 'String(#)', _jsObject); | 173 return JS('String', 'String(#)', _jsObject); |
| 174 } catch(e) { | 174 } catch(e) { |
| 175 return super.toString(); | 175 return super.toString(); |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 dynamic callMethod(name, [List args]) { | 179 dynamic callMethod(name, [List args]) { |
| 180 if (name is! String && name is! int) { | 180 if (name is! String && name is! num) { |
| 181 throw new ArgumentError("name is not a String or int"); | 181 throw new ArgumentError("name is not a String or num"); |
| 182 } | 182 } |
| 183 return _convertToDart(JS('', '#[#].apply(#, #)', _jsObject, name, | 183 return _convertToDart(JS('', '#[#].apply(#, #)', _jsObject, name, |
| 184 _jsObject, | 184 _jsObject, |
| 185 args == null ? null : args.map(_convertToJS).toList())); | 185 args == null ? null : args.map(_convertToJS).toList())); |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 | 188 |
| 189 class JsFunction extends JsObject implements Serializable<JsFunction> { | 189 class JsFunction extends JsObject { |
| 190 | |
| 191 /* | |
| 192 * Returns a [JsFunction] that captures its 'this' binding and calls [f] | |
| 193 * with the value of this passed as the first argument. | |
| 194 */ | |
| 195 factory JsFunction.withThis(Function f) { | |
| 196 var jsFunc = _convertDartFunction(f, captureThis: true); | |
| 197 return new JsFunction._fromJs(jsFunc); | |
| 198 } | |
| 190 | 199 |
| 191 JsFunction._fromJs(jsObject) : super._fromJs(jsObject); | 200 JsFunction._fromJs(jsObject) : super._fromJs(jsObject); |
| 192 | 201 |
| 193 dynamic apply(thisArg, [List args]) => | 202 dynamic apply(thisArg, [List args]) => |
| 194 _convertToDart(JS('', '#.apply(#, #)', _jsObject, | 203 _convertToDart(JS('', '#.apply(#, #)', _jsObject, |
| 195 _convertToJS(thisArg), | 204 _convertToJS(thisArg), |
| 196 args == null ? null : args.map(_convertToJS).toList())); | 205 args == null ? null : args.map(_convertToJS).toList())); |
| 197 } | 206 } |
| 198 | 207 |
| 199 abstract class Serializable<T> { | |
| 200 T toJs(); | |
| 201 } | |
| 202 | |
| 203 // property added to a Dart object referencing its JS-side DartObject proxy | 208 // property added to a Dart object referencing its JS-side DartObject proxy |
| 204 const _DART_OBJECT_PROPERTY_NAME = r'_$dart_dartObject'; | 209 const _DART_OBJECT_PROPERTY_NAME = r'_$dart_dartObject'; |
| 205 const _DART_CLOSURE_PROPERTY_NAME = r'_$dart_dartClosure'; | 210 const _DART_CLOSURE_PROPERTY_NAME = r'_$dart_dartClosure'; |
| 206 | 211 |
| 207 // property added to a JS object referencing its Dart-side JsObject proxy | 212 // property added to a JS object referencing its Dart-side JsObject proxy |
| 208 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; | 213 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; |
| 209 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; | 214 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; |
| 210 | 215 |
| 211 bool _defineProperty(o, String name, value) { | 216 bool _defineProperty(o, String name, value) { |
| 212 if (JS('bool', 'Object.isExtensible(#)', o)) { | 217 if (JS('bool', 'Object.isExtensible(#)', o)) { |
| 213 try { | 218 try { |
| 214 JS('void', 'Object.defineProperty(#, #, { value: #})', o, name, value); | 219 JS('void', 'Object.defineProperty(#, #, { value: #})', o, name, value); |
| 215 return true; | 220 return true; |
| 216 } catch(e) { | 221 } catch(e) { |
| 217 // object is native and lies about being extensible | 222 // object is native and lies about being extensible |
| 218 // see https://bugzilla.mozilla.org/show_bug.cgi?id=775185 | 223 // see https://bugzilla.mozilla.org/show_bug.cgi?id=775185 |
| 219 } | 224 } |
| 220 } | 225 } |
| 221 return false; | 226 return false; |
| 222 } | 227 } |
| 223 | 228 |
| 224 dynamic _convertToJS(dynamic o) { | 229 dynamic _convertToJS(dynamic o) { |
| 225 if (o == null) { | 230 if (o == null) { |
| 226 return null; | 231 return null; |
| 227 } else if (o is String || o is num || o is bool) { | 232 } else if (o is String || o is num || o is bool |
| 233 || o is Blob || o is KeyRange || o is ImageData || o is Node ) { | |
| 228 return o; | 234 return o; |
| 235 } else if (o is DateTime) { | |
| 236 return Primitives.lazyAsJsDate(o); | |
| 229 } else if (o is JsObject) { | 237 } else if (o is JsObject) { |
| 230 return o._jsObject; | 238 return o._jsObject; |
| 231 } else if (o is Serializable) { | |
| 232 return _convertToJS(o.toJs()); | |
| 233 } else if (o is Function) { | 239 } else if (o is Function) { |
| 234 return _getJsProxy(o, _JS_FUNCTION_PROPERTY_NAME, (o) { | 240 return _getJsProxy(o, _JS_FUNCTION_PROPERTY_NAME, (o) { |
| 235 var jsFunction = _convertDartFunction(o); | 241 var jsFunction = _convertDartFunction(o); |
| 236 // set a property on the JS closure referencing the Dart closure | 242 // set a property on the JS closure referencing the Dart closure |
| 237 _defineProperty(jsFunction, _DART_CLOSURE_PROPERTY_NAME, o); | 243 _defineProperty(jsFunction, _DART_CLOSURE_PROPERTY_NAME, o); |
| 238 return jsFunction; | 244 return jsFunction; |
| 239 }); | 245 }); |
| 240 } else { | 246 } else { |
| 241 return _getJsProxy(o, _JS_OBJECT_PROPERTY_NAME, | 247 return _getJsProxy(o, _JS_OBJECT_PROPERTY_NAME, |
| 242 (o) => JS('', 'new DartObject(#)', o)); | 248 (o) => JS('', 'new DartObject(#)', o)); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 253 } | 259 } |
| 254 | 260 |
| 255 // converts a Dart object to a reference to a native JS object | 261 // converts a Dart object to a reference to a native JS object |
| 256 // which might be a DartObject JS->Dart proxy | 262 // which might be a DartObject JS->Dart proxy |
| 257 Object _convertToDart(o) { | 263 Object _convertToDart(o) { |
| 258 if (JS('bool', '# == null', o) || | 264 if (JS('bool', '# == null', o) || |
| 259 JS('bool', 'typeof # == "string"', o) || | 265 JS('bool', 'typeof # == "string"', o) || |
| 260 JS('bool', 'typeof # == "number"', o) || | 266 JS('bool', 'typeof # == "number"', o) || |
| 261 JS('bool', 'typeof # == "boolean"', o)) { | 267 JS('bool', 'typeof # == "boolean"', o)) { |
| 262 return o; | 268 return o; |
| 269 } else if (o is Blob || o is DateTime || o is KeyRange || o is ImageData || o is Node) { | |
| 270 return JS('Blob|DateTime|KeyRange|ImageData|Node', '#', o); | |
|
alexandre.ardhuin
2013/10/18 07:03:45
Is the JS call needed ? Isn't `return o` enough ?
| |
| 263 } else if (JS('bool', 'typeof # == "function"', o)) { | 271 } else if (JS('bool', 'typeof # == "function"', o)) { |
| 264 return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME, | 272 return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME, |
| 265 (o) => new JsFunction._fromJs(o)); | 273 (o) => new JsFunction._fromJs(o)); |
| 266 } else if (JS('bool', '#.constructor === DartObject', o)) { | 274 } else if (JS('bool', '#.constructor === DartObject', o)) { |
| 267 return JS('', '#.o', o); | 275 return JS('', '#.o', o); |
| 268 } else { | 276 } else { |
| 269 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, | 277 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, |
| 270 (o) => new JsObject._fromJs(o)); | 278 (o) => new JsObject._fromJs(o)); |
| 271 } | 279 } |
| 272 } | 280 } |
| 273 | 281 |
| 274 Object _getDartProxy(o, String propertyName, createProxy(o)) { | 282 Object _getDartProxy(o, String propertyName, createProxy(o)) { |
| 275 var dartProxy = JS('', '#[#]', o, propertyName); | 283 var dartProxy = JS('', '#[#]', o, propertyName); |
| 276 if (dartProxy == null) { | 284 if (dartProxy == null) { |
| 277 dartProxy = createProxy(o); | 285 dartProxy = createProxy(o); |
| 278 _defineProperty(o, propertyName, dartProxy); | 286 _defineProperty(o, propertyName, dartProxy); |
| 279 } | 287 } |
| 280 return dartProxy; | 288 return dartProxy; |
| 281 } | 289 } |
| OLD | NEW |