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

Unified Diff: sdk/lib/js/dart2js/js_dart2js.dart

Issue 1318043005: Support user generated custom native JS classes. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: ptal Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/js/dart2js/js_dart2js.dart
diff --git a/sdk/lib/js/dart2js/js_dart2js.dart b/sdk/lib/js/dart2js/js_dart2js.dart
index 849de82658db6c3982128d4d6d593c3fc165b4fb..402ebb6faa5e0fd5f450e3d5bfa5f07b95d65473 100644
--- a/sdk/lib/js/dart2js/js_dart2js.dart
+++ b/sdk/lib/js/dart2js/js_dart2js.dart
@@ -92,21 +92,28 @@ import 'dart:collection' show HashMap, ListMixin;
import 'dart:indexed_db' show KeyRange;
import 'dart:typed_data' show TypedData;
-import 'dart:_foreign_helper' show JS, DART_CLOSURE_TO_JS;
-import 'dart:_interceptors' show JavaScriptObject, UnknownJavaScriptObject;
-import 'dart:_js_helper' show Primitives, convertDartClosureToJS,
- getIsolateAffinityTag;
+import 'dart:_foreign_helper' show JS, JS_CONST, DART_CLOSURE_TO_JS;
+import 'dart:_interceptors'
+ show JavaScriptObject, UnknownJavaScriptObject, DART_CLOSURE_PROPERTY_NAME;
+import 'dart:_js_helper'
+ show Primitives, convertDartClosureToJS, getIsolateAffinityTag;
+
+export 'dart:_interceptors' show Interceptor, JavaScriptObject, JSArray;
final JsObject context = _wrapToDart(JS('', 'self'));
_convertDartFunction(Function f, {bool captureThis: false}) {
- return JS('',
- 'function(_call, f, captureThis) {'
+ return JS(
+ '',
+ 'function(_call, f, captureThis) {'
Siggi Cherem (dart-lang) 2015/09/18 20:34:10 +1 on using ''' to restore indentation
Jacob 2015/10/01 00:47:33 this file is now automatically formatted so please
sra1 2015/10/01 17:34:30 When this was originally written JS didn't parse f
Siggi Cherem (dart-lang) 2015/10/01 18:21:28 to clarify, what I meant was to use the triple-' g
Jacob 2015/10/02 20:08:15 fine. I switched to ''' and did a bunch of white s
'return function() {'
- 'return _call(f, captureThis, this, '
- 'Array.prototype.slice.apply(arguments));'
+ 'return _call(f, captureThis, this, '
+ 'Array.prototype.slice.apply(arguments));'
'}'
- '}(#, #, #)', DART_CLOSURE_TO_JS(_callDartFunction), f, captureThis);
+ '}(#, #, #)',
+ DART_CLOSURE_TO_JS(_callDartFunction),
+ f,
+ captureThis);
}
_callDartFunction(callback, bool captureThis, self, List arguments) {
@@ -212,8 +219,7 @@ class JsObject {
*/
factory JsObject.fromBrowserObject(object) {
if (object is num || object is String || object is bool || object == null) {
- throw new ArgumentError(
- "object cannot be a num, string, bool, or null");
+ throw new ArgumentError("object cannot be a num, string, bool, or null");
}
return _wrapToDart(_convertToJS(object));
}
@@ -267,7 +273,7 @@ class JsObject {
*
* The type of [property] must be either [String] or [num].
*/
- dynamic operator[](property) {
+ dynamic operator [](property) {
if (property is! String && property is! num) {
throw new ArgumentError("property is not a String or num");
}
@@ -280,7 +286,7 @@ class JsObject {
*
* The type of [property] must be either [String] or [num].
*/
- operator[]=(property, value) {
+ operator []=(property, value) {
if (property is! String && property is! num) {
throw new ArgumentError("property is not a String or num");
}
@@ -289,8 +295,8 @@ class JsObject {
int get hashCode => 0;
- bool operator==(other) => other is JsObject &&
- JS('bool', '# === #', _jsObject, other._jsObject);
+ bool operator ==(other) =>
+ other is JsObject && JS('bool', '# === #', _jsObject, other._jsObject);
/**
* Returns `true` if the JavaScript object contains the specified property
@@ -332,7 +338,7 @@ class JsObject {
String toString() {
try {
return JS('String', 'String(#)', _jsObject);
- } catch(e) {
+ } catch (e) {
return super.toString();
}
}
@@ -347,7 +353,11 @@ class JsObject {
if (method is! String && method is! num) {
throw new ArgumentError("method is not a String or num");
}
- return _convertToDart(JS('', '#[#].apply(#, #)', _jsObject, method,
+ return _convertToDart(JS(
+ '',
+ '#[#].apply(#, #)',
+ _jsObject,
+ method,
_jsObject,
args == null ? null : new List.from(args.map(_convertToJS))));
}
@@ -357,7 +367,6 @@ class JsObject {
* Proxies a JavaScript Function object.
*/
class JsFunction extends JsObject {
-
/**
* Returns a [JsFunction] that captures its 'this' binding and calls [f]
* with the value of this passed as the first argument.
@@ -373,17 +382,18 @@ class JsFunction extends JsObject {
* Invokes the JavaScript function with arguments [args]. If [thisArg] is
* supplied it is the value of `this` for the invocation.
*/
- dynamic apply(List args, { thisArg }) =>
- _convertToDart(JS('', '#.apply(#, #)', _jsObject,
- _convertToJS(thisArg),
- args == null ? null : new List.from(args.map(_convertToJS))));
+ dynamic apply(List args, {thisArg}) => _convertToDart(JS(
+ '',
+ '#.apply(#, #)',
+ _jsObject,
+ _convertToJS(thisArg),
+ args == null ? null : new List.from(args.map(_convertToJS))));
}
/**
* A [List] that proxies a JavaScript array.
*/
class JsArray<E> extends JsObject with ListMixin<E> {
-
/**
* Creates a new JavaScript array.
*/
@@ -449,8 +459,9 @@ class JsArray<E> extends JsObject with ListMixin<E> {
throw new StateError('Bad JsArray length');
}
- void set length(int length) { super['length'] = length; }
-
+ void set length(int length) {
+ super['length'] = length;
+ }
// Methods overriden for better performance
@@ -503,12 +514,11 @@ class JsArray<E> extends JsObject with ListMixin<E> {
// property added to a Dart object referencing its JS-side DartObject proxy
final String _DART_OBJECT_PROPERTY_NAME =
getIsolateAffinityTag(r'_$dart_dartObject');
-final String _DART_CLOSURE_PROPERTY_NAME =
- getIsolateAffinityTag(r'_$dart_dartClosure');
// property added to a JS object referencing its Dart-side JsObject proxy
const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject';
const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction';
+const _JS_FUNCTION_PROPERTY_NAME_CAPTURE_THIS = r'_$dart_jsFunctionCaptureThis';
bool _defineProperty(o, String name, value) {
if (_isExtensible(o) &&
@@ -554,8 +564,13 @@ dynamic _convertToJS(dynamic o) {
if (o is JsObject) {
return o._jsObject;
}
- if (o is Blob || o is Event || o is KeyRange || o is ImageData || o is Node ||
- o is TypedData || o is Window) {
+ if (o is Blob ||
+ o is Event ||
+ o is KeyRange ||
+ o is ImageData ||
+ o is Node ||
+ o is TypedData ||
+ o is Window) {
return o;
}
if (o is DateTime) {
@@ -565,13 +580,13 @@ dynamic _convertToJS(dynamic o) {
return _getJsProxy(o, _JS_FUNCTION_PROPERTY_NAME, (o) {
var jsFunction = _convertDartFunction(o);
// set a property on the JS closure referencing the Dart closure
- _defineProperty(jsFunction, _DART_CLOSURE_PROPERTY_NAME, o);
+ _defineProperty(jsFunction, DART_CLOSURE_PROPERTY_NAME, o);
return jsFunction;
});
}
var ctor = _dartProxyCtor;
- return _getJsProxy(o, _JS_OBJECT_PROPERTY_NAME,
- (o) => JS('', 'new #(#)', ctor, o));
+ return _getJsProxy(
+ o, _JS_OBJECT_PROPERTY_NAME, (o) => JS('', 'new #(#)', ctor, o));
}
Object _getJsProxy(o, String propertyName, createProxy(o)) {
@@ -591,9 +606,14 @@ Object _convertToDart(o) {
JS('bool', 'typeof # == "number"', o) ||
JS('bool', 'typeof # == "boolean"', o)) {
return o;
- } else if (_isLocalObject(o)
- && (o is Blob || o is Event || o is KeyRange || o is ImageData
- || o is Node || o is TypedData || o is Window)) {
+ } else if (_isLocalObject(o) &&
+ (o is Blob ||
+ o is Event ||
+ o is KeyRange ||
+ o is ImageData ||
+ o is Node ||
+ o is TypedData ||
+ o is Window)) {
// long line: dart2js doesn't allow string concatenation in the JS() form
return JS('Blob|Event|KeyRange|ImageData|Node|TypedData|Window', '#', o);
} else if (JS('bool', '# instanceof Date', o)) {
@@ -608,15 +628,15 @@ Object _convertToDart(o) {
JsObject _wrapToDart(o) {
if (JS('bool', 'typeof # == "function"', o)) {
- return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME,
- (o) => new JsFunction._fromJs(o));
+ return _getDartProxy(
+ o, DART_CLOSURE_PROPERTY_NAME, (o) => new JsFunction._fromJs(o));
}
if (JS('bool', '# instanceof Array', o)) {
- return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME,
- (o) => new JsArray._fromJs(o));
+ return _getDartProxy(
+ o, _DART_OBJECT_PROPERTY_NAME, (o) => new JsArray._fromJs(o));
}
- return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME,
- (o) => new JsObject._fromJs(o));
+ return _getDartProxy(
+ o, _DART_OBJECT_PROPERTY_NAME, (o) => new JsObject._fromJs(o));
}
Object _getDartProxy(o, String propertyName, createProxy(o)) {
@@ -634,3 +654,132 @@ Object _getDartProxy(o, String propertyName, createProxy(o)) {
}
return dartProxy;
}
+
+// Start of methods for new style Dart-JS interop.
+
+class _JavaScriptFunctionHack implements Function {
+ call(
+ [a = const JS_CONST('void 0'),
+ b = const JS_CONST('void 0'),
+ c = const JS_CONST('void 0'),
+ d = const JS_CONST('void 0'),
+ e = const JS_CONST('void 0'),
+ f = const JS_CONST('void 0'),
+ g = const JS_CONST('void 0'),
+ h = const JS_CONST('void 0'),
+ i = const JS_CONST('void 0'),
+ j = const JS_CONST('void 0')]) {
+ // Exceedingly slow default implementation.
+ return JS('', '#.apply(null, #)', this,
+ _stripUndefinedArgs([a, b, c, d, e, f, g, h, i, j]));
+ }
+}
+
+void _copyOwnProperties(src, dest) {
+ JS(
+ '',
+ r"""(function(src, dest) {
+ var properties = Object.getOwnPropertyNames(src);
+ for (var i = 0, len = properties.length; i < len; i++) {
+ var name = properties[i];
+ dest[name] = src[name];
+ }
+})(#, #)""",
+ src,
+ dest);
+}
+
+// TODO(jacobr): remove this method. So far it appears that specifying the list
+// of registered types in Dart2Js has significant negative code size
+// implications so it is better to specify usage purely based on which
+// libraries are imported. Remove after Dartium is modified to function without
Siggi Cherem (dart-lang) 2015/09/18 20:34:10 would it make sense to use tree-shakeable lookup-m
Jacob 2015/10/02 20:08:15 could be. moved away from requiring this method in
+// requiring this method.
+void registerJsInterfaces([List<Type> types]) {
Siggi Cherem (dart-lang) 2015/09/18 20:34:10 I seem to be missing something, is `types` used an
Jacob 2015/10/01 00:47:33 This was used in Dartium. I've ripped it out there
+ // No need to actually register in Dart2JS.
+ var fnHackProto = JS('', '#.__proto__', new _JavaScriptFunctionHack());
+ var fnProto = JS('', 'Function.prototype');
+ _copyOwnProperties(fnHackProto, fnProto);
+ // Add optimized call methods for small numbers of arguments.
+ if (JS('bool', r'#.hasOwnProperty("call$0") ', fnHackProto)) {
+ JS('', r'#.call$0 = function() { return this(); }', fnProto);
+ JS('', r'#.call$1 = function(a) { return this(a); }', fnProto);
+ JS('', r'#.call$2 = function(a, b) { return this(a, b); }', fnProto);
+ JS('', r'#.call$3 = function(a, b, c) { return this(a, b, c); }', fnProto);
+ JS('', r'#.call$4 = function(a, b, c, d) { return this(a, b, c, d); }',
+ fnProto);
+ } else {
+ if (!JS('bool', r'#.hasOwnProperty("$0") ', fnHackProto)) {
+ throw 'Internal error. Unexpected minified output';
+ }
+ JS('', r'#.$0 = function() { return this(); }', fnProto);
+ JS('', r'#.$1 = function(a) { return this(a); }', fnProto);
+ JS('', r'#.$2 = function(a, b) { return this(a, b); }', fnProto);
+ JS('', r'#.$3 = function(a, b, c) { return this(a, b, c); }', fnProto);
+ JS('', r'#.$4 = function(a, b, c, d) { return this(a, b, c, d); }',
+ fnProto);
+ }
+}
+
+_convertDartFunctionFast(Function f, {bool captureThis: false}) {
+ var existing = JS('', '#.#', f, _JS_FUNCTION_PROPERTY_NAME);
+ if (existing != null) return existing;
+ var ret = JS(
+ '',
+ 'function(_call, f) {'
+ 'return function() {'
+ 'return _call(f, Array.prototype.slice.apply(arguments));'
+ '}'
+ '}(#, #)',
+ DART_CLOSURE_TO_JS(_callDartFunctionFast),
+ f);
+ JS('', '#.# = #', ret, DART_CLOSURE_PROPERTY_NAME, f);
+ JS('', '#.# = #', f, _JS_FUNCTION_PROPERTY_NAME, ret);
+ return ret;
+}
+
+_convertDartFunctionFastCaptureThis(Function f) {
+ var existing =
+ JS('', '#.#', f, _JS_FUNCTION_PROPERTY_NAME_CAPTURE_THIS);
+ if (existing != null) return existing;
+ var ret = JS(
+ '',
+ 'function(_call, f) {'
+ 'return function() {'
+ 'return _call(f, this, '
+ 'Array.prototype.slice.apply(arguments));'
+ '}'
+ '}(#, #)',
+ DART_CLOSURE_TO_JS(_callDartFunctionFastCaptureThis),
+ f);
+ JS('', '#.# = #', ret, DART_CLOSURE_PROPERTY_NAME, f);
+ JS('', '#.# = #', f, _JS_FUNCTION_PROPERTY_NAME_CAPTURE_THIS, ret);
+ return ret;
+}
+
+_callDartFunctionFast(callback, List arguments) {
+ return Function.apply(callback, arguments);
+}
+
+_callDartFunctionFastCaptureThis(callback, self, List arguments) {
+ return _convertToJS(Function.apply(callback, [self]..addAll(arguments)));
+}
+
+Function allowInterop(Function f) {
+ if (JS('bool', 'typeof(#) == "function"', f)) {
+ // Already supports interop, just use the existing function.
+ return f;
+ } else {
+ return _convertDartFunctionFast(f);
+ }
+}
+
+Function allowInteropCaptureThis(Function f) {
+ if (JS('bool', 'typeof(#) == "function"', f)) {
+ // Behavior when the function is already a JS function is unspecified.
+ throw new ArgumentError(
+ "Function is already a JS function so cannot capture this.");
+ return f;
+ } else {
+ return _convertDartFunctionFastCaptureThis(f);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698