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

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

Issue 26742008: Revert "Maintain referential integrity of proxy instances in dart:js" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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 | « pkg/browser/lib/interop.js ('k') | sdk/lib/js/dartium/js_dartium.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;
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
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
70 class JsObject implements Serializable<JsObject> { 42 class JsObject implements Serializable<JsObject> {
71 final dynamic _jsObject; 43 final dynamic _jsObject;
72 44
73 JsObject._fromJs(this._jsObject) { 45 JsObject._fromJs(this._jsObject);
74 // remember this proxy for the JS object
75 _getDartProxy(_jsObject, _DART_OBJECT_PROPERTY_NAME, (o) => this);
76 }
77 46
78 // TODO(vsm): Type constructor as Serializable<JsFunction> when 47 // TODO(vsm): Type constructor as Serializable<JsFunction> when
79 // dartbug.com/11854 is fixed. 48 // dartbug.com/11854 is fixed.
80 factory JsObject(var constructor, [List arguments]) { 49 factory JsObject(var constructor, [List arguments]) {
81 final constr = _convertToJS(constructor); 50 final constr = _convertToJS(constructor);
82 if (arguments == null) { 51 if (arguments == null) {
83 return new JsObject._fromJs(JS('=Object', 'new #()', constr)); 52 return new JsObject._fromJs(JS('=Object', 'new #()', constr));
84 } 53 }
85 final args = arguments.map(_convertToJS).toList(); 54 final args = arguments.map(_convertToJS).toList();
86 switch (args.length) { 55 switch (args.length) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 return data.map(_convertDataTree).toList(); 113 return data.map(_convertDataTree).toList();
145 } else { 114 } else {
146 return _convertToJS(data); 115 return _convertToJS(data);
147 } 116 }
148 } 117 }
149 118
150 JsObject toJs() => this; 119 JsObject toJs() => this;
151 120
152 operator[](key) => 121 operator[](key) =>
153 _convertToDart(JS('=Object', '#[#]', _convertToJS(this), key)); 122 _convertToDart(JS('=Object', '#[#]', _convertToJS(this), key));
154
155 operator[]=(key, value) => JS('void', '#[#]=#', _convertToJS(this), key, 123 operator[]=(key, value) => JS('void', '#[#]=#', _convertToJS(this), key,
156 _convertToJS(value)); 124 _convertToJS(value));
157 125
158 int get hashCode => 0; 126 int get hashCode => 0;
159 127
160 operator==(other) => other is JsObject && 128 operator==(other) => other is JsObject &&
161 JS('bool', '# === #', _convertToJS(this), _convertToJS(other)); 129 JS('bool', '# === #', _convertToJS(this), _convertToJS(other));
162 130
163 bool hasProperty(String property) => JS('bool', '# in #', property, 131 bool hasProperty(String property) => JS('bool', '# in #', property,
164 _convertToJS(this)); 132 _convertToJS(this));
(...skipping 26 matching lines...) Expand all
191 apply(thisArg, [List args]) => 159 apply(thisArg, [List args]) =>
192 _convertToDart(JS('=Object', '#.apply(#, #)', _convertToJS(this), 160 _convertToDart(JS('=Object', '#.apply(#, #)', _convertToJS(this),
193 _convertToJS(thisArg), 161 _convertToJS(thisArg),
194 args == null ? null : args.map(_convertToJS).toList())); 162 args == null ? null : args.map(_convertToJS).toList()));
195 } 163 }
196 164
197 abstract class Serializable<T> { 165 abstract class Serializable<T> {
198 T toJs(); 166 T toJs();
199 } 167 }
200 168
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
212 dynamic _convertToJS(dynamic o) { 169 dynamic _convertToJS(dynamic o) {
213 if (o == null) { 170 if (o == null) {
214 return null; 171 return null;
215 } else if (o is String || o is num || o is bool) { 172 } else if (o is String || o is num || o is bool) {
216 return o; 173 return o;
217 } else if (o is JsObject) { 174 } else if (o is JsObject) {
218 return o._jsObject; 175 return o._jsObject;
219 } else if (o is Serializable) { 176 } else if (o is Serializable) {
220 return _convertToJS(o.toJs()); 177 return _convertToJS(o.toJs());
221 } else if (o is Function) { 178 } else if (o is Function) {
222 return _getJsProxy(o, _JS_FUNCTION_PROPERTY_NAME, (o) { 179 return _convertToJS(new Callback(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 });
228 } else { 180 } else {
229 return _getJsProxy(o, _JS_OBJECT_PROPERTY_NAME, 181 return JS('=Object', 'new DartProxy(#)', o);
230 (o) => JS('', 'new DartObject(#)', o));
231 } 182 }
232 } 183 }
233 184
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
245 dynamic _convertToDart(dynamic o) { 185 dynamic _convertToDart(dynamic o) {
246 if (JS('bool', '# == null', o)) { 186 if (JS('bool', '# == null', o)) {
247 return null; 187 return null;
248 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) || 188 } else if (JS('bool', 'typeof # == "string" || # instanceof String', o, o) ||
249 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) || 189 JS('bool', 'typeof # == "number" || # instanceof Number', o, o) ||
250 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) { 190 JS('bool', 'typeof # == "boolean" || # instanceof Boolean', o, o)) {
251 return o; 191 return o;
252 } else if (JS('bool', '# instanceof Function', o)) { 192 } else if (JS('bool', '# instanceof Function', o)) {
253 return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME, 193 return new JsFunction._fromJs(JS('=Object', '#', o));
254 (o) => new JsFunction._fromJs(o)); 194 } else if (JS('bool', '# instanceof DartProxy', o)) {
255 } else if (JS('bool', '# instanceof DartObject', o)) {
256 return JS('var', '#.o', o); 195 return JS('var', '#.o', o);
257 } else { 196 } else {
258 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, 197 return new JsObject._fromJs(JS('=Object', '#', o));
259 (o) => new JsObject._fromJs(o));
260 } 198 }
261 } 199 }
262
263 dynamic _getDartProxy(o, String propertyName, createProxy(o)) {
264 var dartProxy = JS('', '#[#]', o, propertyName);
265 if (dartProxy == null) {
266 dartProxy = createProxy(o);
267 _defineProperty(o, propertyName, dartProxy);
268 }
269 return dartProxy;
270 }
OLDNEW
« no previous file with comments | « pkg/browser/lib/interop.js ('k') | sdk/lib/js/dartium/js_dartium.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698