OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library dart.js; |
| 6 |
| 7 import 'dart:_foreign_helper' show JS; |
| 8 import 'dart:_js_helper' show convertDartClosureToJS; |
| 9 |
| 10 JsObject get context { |
| 11 return new JsObject._fromJs(JS('=Object', 'window')); |
| 12 } |
| 13 |
| 14 JsObject jsify(dynamic data) => data == null ? null : new JsObject._json(data); |
| 15 |
| 16 class Callback implements Serializable<JsFunction> { |
| 17 final Function _f; // here to allow capture in closure |
| 18 final bool _withThis; // here to allow capture in closure |
| 19 dynamic _jsFunction; |
| 20 |
| 21 Callback._(this._f, this._withThis) { |
| 22 _jsFunction = JS('=Object', r''' |
| 23 (function(){ |
| 24 var f = #; |
| 25 return function(){ |
| 26 return f(this, Array.prototype.slice.apply(arguments)); |
| 27 }; |
| 28 }).apply(this)''', convertDartClosureToJS(_call, 2)); |
| 29 } |
| 30 |
| 31 factory Callback(Function f) => new Callback._(f, false); |
| 32 factory Callback.withThis(Function f) => new Callback._(f, true); |
| 33 |
| 34 _call(thisArg, List args) { |
| 35 final arguments = new List.from(args); |
| 36 if (_withThis) arguments.insert(0, thisArg); |
| 37 final dartArgs = arguments.map(_convertToDart).toList(); |
| 38 return _convertToJS(Function.apply(_f, dartArgs)); |
| 39 } |
| 40 |
| 41 JsFunction toJs() => new JsFunction._fromJs(_jsFunction); |
| 42 } |
| 43 |
| 44 class JsObject implements Serializable<JsObject> { |
| 45 final dynamic _jsObject; |
| 46 |
| 47 JsObject._fromJs(this._jsObject); |
| 48 |
| 49 factory JsObject(Serializable<JsFunction> constructor, [List arguments]) { |
| 50 final constr = _convertToJS(constructor); |
| 51 if (arguments == null) { |
| 52 return new JsObject._fromJs(JS('=Object', 'new #()', constr)); |
| 53 } |
| 54 final args = arguments.map(_convertToJS).toList(); |
| 55 switch (args.length) { |
| 56 case 0: |
| 57 return new JsObject._fromJs(JS('=Object', 'new #()', constr)); |
| 58 case 1: |
| 59 return new JsObject._fromJs(JS('=Object', 'new #(#)', constr, args[0])); |
| 60 case 2: |
| 61 return new JsObject._fromJs(JS('=Object', 'new #(#,#)', constr, args[0], |
| 62 args[1])); |
| 63 case 3: |
| 64 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#)', constr, |
| 65 args[0], args[1], args[2])); |
| 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 } |
| 101 |
| 102 factory JsObject._json(data) => new JsObject._fromJs(_convertDataTree(data)); |
| 103 |
| 104 static _convertDataTree(data) { |
| 105 if (data is Map) { |
| 106 final convertedData = JS('=Object', '{}'); |
| 107 for (var key in data.keys) { |
| 108 JS('=Object', '#[#]=#', convertedData, key, |
| 109 _convertDataTree(data[key])); |
| 110 } |
| 111 return convertedData; |
| 112 } else if (data is Iterable) { |
| 113 return data.map(_convertDataTree).toList(); |
| 114 } else { |
| 115 return _convertToJS(data); |
| 116 } |
| 117 } |
| 118 |
| 119 JsObject toJs() => this; |
| 120 |
| 121 operator[](key) => |
| 122 _convertToDart(JS('=Object', '#[#]', _convertToJS(this), key)); |
| 123 operator[]=(key, value) => JS('void', '#[#]=#', _convertToJS(this), key, |
| 124 _convertToJS(value)); |
| 125 |
| 126 int get hashCode => 0; |
| 127 |
| 128 operator==(other) => other is JsObject && |
| 129 JS('bool', '# === #', _convertToJS(this), _convertToJS(other)); |
| 130 |
| 131 bool hasProperty(String property) => JS('bool', '# in #', property, |
| 132 _convertToJS(this)); |
| 133 |
| 134 void deleteProperty(String name) { |
| 135 JS('void', 'delete #[#]', _convertToJS(this), name); |
| 136 } |
| 137 |
| 138 bool instanceof(Serializable<JsFunction> type) => |
| 139 JS('bool', '# instanceof #', _convertToJS(this), _convertToJS(type)); |
| 140 |
| 141 String toString() { |
| 142 try { |
| 143 return JS('String', '#.toString()', _convertToJS(this)); |
| 144 } catch(e) { |
| 145 return super.toString(); |
| 146 } |
| 147 } |
| 148 |
| 149 callMethod(String name, [List args]) => |
| 150 _convertToDart(JS('=Object', '#[#].apply(#, #)', _convertToJS(this), name, |
| 151 _convertToJS(this), |
| 152 args == null ? null : args.map(_convertToJS).toList())); |
| 153 } |
| 154 |
| 155 class JsFunction extends JsObject implements Serializable<JsFunction> { |
| 156 JsFunction._fromJs(jsObject) : super._fromJs(jsObject); |
| 157 apply(thisArg, [List args]) => |
| 158 _convertToDart(JS('=Object', '#.apply(#, #)', _convertToJS(this), |
| 159 _convertToJS(thisArg), |
| 160 args == null ? null : args.map(_convertToJS).toList())); |
| 161 } |
| 162 |
| 163 abstract class Serializable<T> { |
| 164 T toJs(); |
| 165 } |
| 166 |
| 167 dynamic _convertToJS(dynamic o) { |
| 168 if (o == null) { |
| 169 return null; |
| 170 } else if (o is String || o is num || o is bool) { |
| 171 return o; |
| 172 } else if (o is JsObject) { |
| 173 return o._jsObject; |
| 174 } else if (o is Serializable) { |
| 175 return _convertToJS(o.toJs()); |
| 176 } else if (o is Function) { |
| 177 return _convertToJS(new Callback(o)); |
| 178 } else { |
| 179 return JS('=Object', 'new DartProxy(#)', o); |
| 180 } |
| 181 } |
| 182 |
| 183 dynamic _convertToDart(dynamic o) { |
| 184 if (JS('bool', '# == null', o)) { |
| 185 return null; |
| 186 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || |
| 187 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || |
| 188 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { |
| 189 return o; |
| 190 } else if (JS('bool', '# instanceof Function', o)) { |
| 191 return new JsFunction._fromJs(JS('=Object', '#', o)); |
| 192 } else if (JS('bool', '# instanceof DartProxy', o)) { |
| 193 return JS('var', '#.o', o); |
| 194 } else { |
| 195 return new JsObject._fromJs(JS('=Object', '#', o)); |
| 196 } |
| 197 } |
OLD | NEW |