Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: sdk/lib/js/dart2js/js_dart2js.dart

Issue 26270003: Cleanup JS() expressions. New Constructor for JsObject that preserves the correct prototype. Type c… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: add expect() to constructor test Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/html/js_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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, DART_CLOSURE_TO_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);
(...skipping 30 matching lines...) Expand all
61 _callDartFunction(callback, bool captureThis, self, List arguments) { 61 _callDartFunction(callback, bool captureThis, self, List arguments) {
62 if (captureThis) { 62 if (captureThis) {
63 arguments = [self]..addAll(arguments); 63 arguments = [self]..addAll(arguments);
64 } 64 }
65 var dartArgs = arguments.map(_convertToDart).toList(growable: false); 65 var dartArgs = arguments.map(_convertToDart).toList(growable: false);
66 return _convertToJS(Function.apply(callback, dartArgs)); 66 return _convertToJS(Function.apply(callback, dartArgs));
67 } 67 }
68 68
69 69
70 class JsObject implements Serializable<JsObject> { 70 class JsObject implements Serializable<JsObject> {
71 // the wrapped JS object
ahe 2013/10/11 12:40:51 Please convert to proper sentence, that is, start
justinfagnani 2013/10/12 21:16:22 Done.
71 final dynamic _jsObject; 72 final dynamic _jsObject;
72 73
73 JsObject._fromJs(this._jsObject) { 74 JsObject._fromJs(this._jsObject) {
74 // remember this proxy for the JS object 75 // remember this proxy for the JS object
75 _getDartProxy(_jsObject, _DART_OBJECT_PROPERTY_NAME, (o) => this); 76 _getDartProxy(_jsObject, _DART_OBJECT_PROPERTY_NAME, (o) => this);
76 } 77 }
77 78
78 // TODO(vsm): Type constructor as Serializable<JsFunction> when 79 // TODO(vsm): Type constructor as Serializable<JsFunction> when
79 // dartbug.com/11854 is fixed. 80 // dartbug.com/11854 is fixed.
80 factory JsObject(var constructor, [List arguments]) { 81 factory JsObject(constructor, [List arguments]) {
81 final constr = _convertToJS(constructor); 82 var constr = _convertToJS(constructor);
82 if (arguments == null) { 83 if (arguments == null) {
83 return new JsObject._fromJs(JS('=Object', 'new #()', constr)); 84 return new JsObject._fromJs(JS('', 'new #()', constr));
84 } 85 }
85 final args = arguments.map(_convertToJS).toList(); 86 // The following code solves the problem of invoking a JavaScript
86 switch (args.length) { 87 // constructor with an unknown number arguments.
87 case 0: 88 // First bind the constructor to the argument list using bind.apply().
88 return new JsObject._fromJs(JS('=Object', 'new #()', constr)); 89 // The first argument to bind() is the binding of 'this', so add 'null' to
89 case 1: 90 // the arguments list passed to apply().
90 return new JsObject._fromJs(JS('=Object', 'new #(#)', constr, args[0])); 91 // After that, use the JavaScript 'new' operator which overrides any binding
91 case 2: 92 // of 'this' with the new instance.
92 return new JsObject._fromJs(JS('=Object', 'new #(#,#)', constr, args[0], 93 var args = [null]..addAll(arguments.map(_convertToJS));
93 args[1])); 94 var factoryFunction = JS('', '#.bind.apply(#, #)', constr, constr, args);
94 case 3: 95 JS('void', 'console.log(#)', factoryFunction);
alexandre.ardhuin 2013/10/11 14:07:13 log to remove ?
justinfagnani 2013/10/12 21:16:22 There's a very strange heisenbug here. Without the
95 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#)', constr, 96 return new JsObject._fromJs(JS('', 'new #()', factoryFunction));
96 args[0], args[1], args[2]));
97 case 4:
98 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#)', constr,
99 args[0], args[1], args[2], args[3]));
100 case 5:
101 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#)', constr,
102 args[0], args[1], args[2], args[3], args[4]));
103 case 6:
104 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#,#)', constr,
105 args[0], args[1], args[2], args[3], args[4], args[5]));
106 case 7:
107 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#,#,#)',
108 constr, args[0], args[1], args[2], args[3], args[4], args[5],
109 args[6]));
110 case 8:
111 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#,#,#,#)',
112 constr, args[0], args[1], args[2], args[3], args[4], args[5],
113 args[6], args[7]));
114 case 9:
115 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#,#,#,#,#)',
116 constr, args[0], args[1], args[2], args[3], args[4], args[5],
117 args[6], args[7], args[8]));
118 case 10:
119 return new JsObject._fromJs(JS('=Object', 'new #(#,#,#,#,#,#,#,#,#,#)',
120 constr, args[0], args[1], args[2], args[3], args[4], args[5],
121 args[6], args[7], args[8], args[9]));
122 }
123 return new JsObject._fromJs(JS('=Object', r'''(function(){
124 var Type = function(){};
125 Type.prototype = #.prototype;
126 var instance = new Type();
127 ret = #.apply(instance, #);
128 ret = Object(ret) === ret ? ret : instance;
129 return ret;
130 })()''', constr, constr, args));
131 } 97 }
132 98
133 factory JsObject._json(data) => new JsObject._fromJs(_convertDataTree(data)); 99 factory JsObject._json(data) => new JsObject._fromJs(_convertDataTree(data));
134 100
135 static _convertDataTree(data) { 101 static _convertDataTree(data) {
136 if (data is Map) { 102 if (data is Map) {
137 final convertedData = JS('=Object', '{}'); 103 final convertedData = JS('=Object', '{}');
138 for (var key in data.keys) { 104 for (var key in data.keys) {
139 JS('=Object', '#[#]=#', convertedData, key, 105 JS('=Object', '#[#]=#', convertedData, key,
140 _convertDataTree(data[key])); 106 _convertDataTree(data[key]));
141 } 107 }
142 return convertedData; 108 return convertedData;
143 } else if (data is Iterable) { 109 } else if (data is Iterable) {
144 return data.map(_convertDataTree).toList(); 110 return data.map(_convertDataTree).toList();
145 } else { 111 } else {
146 return _convertToJS(data); 112 return _convertToJS(data);
147 } 113 }
148 } 114 }
149 115
150 JsObject toJs() => this; 116 JsObject toJs() => this;
151 117
152 operator[](key) => 118 /**
153 _convertToDart(JS('=Object', '#[#]', _convertToJS(this), key)); 119 * Returns the value associated with [key] from the proxied JavaScript
154 120 * object.
155 operator[]=(key, value) => JS('void', '#[#]=#', _convertToJS(this), key, 121 *
156 _convertToJS(value)); 122 * [key] must either be a [String] or [int].
123 */
124 // TODO(justinfagnani): rename key/name to property
125 dynamic operator[](dynamic key) {
ahe 2013/10/11 12:40:51 Remove dynamic (twice).
justinfagnani 2013/10/12 21:16:22 Done.
126 if (key is! String && key is! int) {
127 throw new ArgumentError("key is not a String or int");
128 }
129 return _convertToDart(JS('', '#[#]', _jsObject, key));
130 }
131
132 /**
133 * Sets the value associated with [key] from the proxied JavaScript
134 * object.
135 *
136 * [key] must either be a [String] or [int].
137 */
138 operator[]=(String key, value) {
alexandre.ardhuin 2013/10/11 14:07:13 Change "key" type to dynamic to be consistant with
justinfagnani 2013/10/12 21:16:22 Done.
139 if (key is! String && key is! int) {
140 throw new ArgumentError("key is not a String or int");
141 }
142 JS('', '#[#]=#', _jsObject, key, _convertToJS(value));
143 }
157 144
158 int get hashCode => 0; 145 int get hashCode => 0;
159 146
160 operator==(other) => other is JsObject && 147 operator==(other) => other is JsObject &&
161 JS('bool', '# === #', _convertToJS(this), _convertToJS(other)); 148 JS('bool', '# === #', _jsObject, other._jsObject);
162 149
163 bool hasProperty(String property) => JS('bool', '# in #', property, 150 bool hasProperty(String name) {
alexandre.ardhuin 2013/10/11 14:07:13 Change "name" type to dynamic.
justinfagnani 2013/10/12 21:16:22 Done.
164 _convertToJS(this)); 151 if (name is! String && name is! int) {
152 throw new ArgumentError("name is not a String or int");
153 }
154 return JS('bool', '# in #', name, _jsObject);
155 }
165 156
166 void deleteProperty(String name) { 157 void deleteProperty(String name) {
alexandre.ardhuin 2013/10/11 14:07:13 Change "name" type to dynamic.
justinfagnani 2013/10/12 21:16:22 Done.
167 JS('void', 'delete #[#]', _convertToJS(this), name); 158 if (name is! String && name is! int) {
159 throw new ArgumentError("name is not a String or int");
160 }
161 JS('bool', 'delete #[#]', _jsObject, name);
168 } 162 }
169 163
170 // TODO(vsm): Type type as Serializable<JsFunction> when 164 // TODO(vsm): Type type as Serializable<JsFunction> when
171 // dartbug.com/11854 is fixed. 165 // dartbug.com/11854 is fixed.
172 bool instanceof(var type) => 166 bool instanceof(JsFunction type) {
alexandre.ardhuin 2013/10/11 14:07:13 Remove JsFunction otherwise Serializable<JsFunctio
justinfagnani 2013/10/12 21:16:22 Done.
173 JS('bool', '# instanceof #', _convertToJS(this), _convertToJS(type)); 167 if (type is! JsFunction) throw new ArgumentError("type is not a JsFunction") ;
ahe 2013/10/11 12:40:51 Long line.
justinfagnani 2013/10/12 21:16:22 Done.
168 return JS('bool', '# instanceof #', _jsObject, type._jsObject);
169 }
174 170
175 String toString() { 171 String toString() {
176 try { 172 try {
177 return JS('String', '#.toString()', _convertToJS(this)); 173 return JS('String', 'String(#)', _jsObject);
178 } catch(e) { 174 } catch(e) {
179 return super.toString(); 175 return super.toString();
180 } 176 }
181 } 177 }
182 178
183 callMethod(String name, [List args]) => 179 callMethod(String name, [List args]) {
184 _convertToDart(JS('=Object', '#[#].apply(#, #)', _convertToJS(this), name, 180 if (name is! String && name is! int) {
185 _convertToJS(this), 181 throw new ArgumentError("name is not a String or int");
186 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 }
187 } 187 }
188 188
189 class JsFunction extends JsObject implements Serializable<JsFunction> { 189 class JsFunction extends JsObject implements Serializable<JsFunction> {
190
190 JsFunction._fromJs(jsObject) : super._fromJs(jsObject); 191 JsFunction._fromJs(jsObject) : super._fromJs(jsObject);
192
191 apply(thisArg, [List args]) => 193 apply(thisArg, [List args]) =>
192 _convertToDart(JS('=Object', '#.apply(#, #)', _convertToJS(this), 194 _convertToDart(JS('', '#.apply(#, #)', _jsObject,
193 _convertToJS(thisArg), 195 _convertToJS(thisArg),
194 args == null ? null : args.map(_convertToJS).toList())); 196 args == null ? null : args.map(_convertToJS).toList()));
195 } 197 }
196 198
197 abstract class Serializable<T> { 199 abstract class Serializable<T> {
198 T toJs(); 200 T toJs();
199 } 201 }
200 202
201 // property added to a Dart object referencing its JS-side DartObject proxy 203 // property added to a Dart object referencing its JS-side DartObject proxy
202 const _DART_OBJECT_PROPERTY_NAME = r'_$dart_dartObject'; 204 const _DART_OBJECT_PROPERTY_NAME = r'_$dart_dartObject';
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 var jsProxy = JS('', '#[#]', o, propertyName); 237 var jsProxy = JS('', '#[#]', o, propertyName);
236 if (jsProxy == null) { 238 if (jsProxy == null) {
237 jsProxy = createProxy(o); 239 jsProxy = createProxy(o);
238 _defineProperty(o, propertyName, jsProxy); 240 _defineProperty(o, propertyName, jsProxy);
239 } 241 }
240 return jsProxy; 242 return jsProxy;
241 } 243 }
242 244
243 // converts a Dart object to a reference to a native JS object 245 // converts a Dart object to a reference to a native JS object
244 // which might be a DartObject JS->Dart proxy 246 // which might be a DartObject JS->Dart proxy
245 dynamic _convertToDart(dynamic o) { 247 dynamic _convertToDart(dynamic o) {
ahe 2013/10/11 12:40:51 Remove dynamic (twice).
justinfagnani 2013/10/12 21:16:22 Done, but changed the return type to Object to ind
246 if (JS('bool', '# == null', o)) { 248 if (JS('bool', '# == null', o) ||
247 return null; 249 JS('bool', 'typeof # == "string"', o) ||
248 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || 250 JS('bool', 'typeof # == "number"', o) ||
249 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || 251 JS('bool', 'typeof # == "boolean"', o)) {
250 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) {
251 return o; 252 return o;
252 } else if (JS('bool', '# instanceof Function', o)) { 253 } else if (JS('bool', 'typeof # == "function"', o)) {
253 return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME, 254 return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME,
254 (o) => new JsFunction._fromJs(o)); 255 (o) => new JsFunction._fromJs(o));
255 } else if (JS('bool', '# instanceof DartObject', o)) { 256 } else if (JS('bool', '#.constructor === DartObject', o)) {
256 return JS('var', '#.o', o); 257 return JS('', '#.o', o);
257 } else { 258 } else {
258 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, 259 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME,
259 (o) => new JsObject._fromJs(o)); 260 (o) => new JsObject._fromJs(o));
260 } 261 }
261 } 262 }
262 263
263 dynamic _getDartProxy(o, String propertyName, createProxy(o)) { 264 dynamic _getDartProxy(o, String propertyName, createProxy(o)) {
264 var dartProxy = JS('', '#[#]', o, propertyName); 265 var dartProxy = JS('', '#[#]', o, propertyName);
265 if (dartProxy == null) { 266 if (dartProxy == null) {
266 dartProxy = createProxy(JS('=Object', '#', o)); 267 dartProxy = createProxy(JS('=Object', '#', o));
267 _defineProperty(o, propertyName, dartProxy); 268 _defineProperty(o, propertyName, dartProxy);
268 } 269 }
269 return dartProxy; 270 return dartProxy;
270 } 271 }
OLDNEW
« no previous file with comments | « no previous file | tests/html/js_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698