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

Side by Side Diff: pkg/compiler/lib/src/js_backend/backend.dart

Issue 1318043005: Support user generated custom native JS classes. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of js_backend; 5 part of js_backend;
6 6
7 const VERBOSE_OPTIMIZER_HINTS = false; 7 const VERBOSE_OPTIMIZER_HINTS = false;
8 8
9 class JavaScriptItemCompilationContext extends ItemCompilationContext { 9 class JavaScriptItemCompilationContext extends ItemCompilationContext {
10 final Set<HInstruction> boundsChecked = new Set<HInstruction>(); 10 final Set<HInstruction> boundsChecked = new Set<HInstruction>();
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 ClassElement jsInterceptorClass; 282 ClassElement jsInterceptorClass;
283 ClassElement jsStringClass; 283 ClassElement jsStringClass;
284 ClassElement jsArrayClass; 284 ClassElement jsArrayClass;
285 ClassElement jsNumberClass; 285 ClassElement jsNumberClass;
286 ClassElement jsIntClass; 286 ClassElement jsIntClass;
287 ClassElement jsDoubleClass; 287 ClassElement jsDoubleClass;
288 ClassElement jsNullClass; 288 ClassElement jsNullClass;
289 ClassElement jsBoolClass; 289 ClassElement jsBoolClass;
290 ClassElement jsPlainJavaScriptObjectClass; 290 ClassElement jsPlainJavaScriptObjectClass;
291 ClassElement jsUnknownJavaScriptObjectClass; 291 ClassElement jsUnknownJavaScriptObjectClass;
292 ClassElement jsJavaScriptFunctionClass;
292 293
293 ClassElement jsIndexableClass; 294 ClassElement jsIndexableClass;
294 ClassElement jsMutableIndexableClass; 295 ClassElement jsMutableIndexableClass;
295 296
296 ClassElement jsMutableArrayClass; 297 ClassElement jsMutableArrayClass;
297 ClassElement jsFixedArrayClass; 298 ClassElement jsFixedArrayClass;
298 ClassElement jsExtendableArrayClass; 299 ClassElement jsExtendableArrayClass;
299 ClassElement jsUnmodifiableArrayClass; 300 ClassElement jsUnmodifiableArrayClass;
300 ClassElement jsPositiveIntClass; 301 ClassElement jsPositiveIntClass;
301 ClassElement jsUInt32Class; 302 ClassElement jsUInt32Class;
(...skipping 730 matching lines...) Expand 10 before | Expand all | Expand 10 after
1032 // For map literals, the dependency between the implementation class 1033 // For map literals, the dependency between the implementation class
1033 // and [Map] is not visible, so we have to add it manually. 1034 // and [Map] is not visible, so we have to add it manually.
1034 rti.registerRtiDependency(mapLiteralClass, cls); 1035 rti.registerRtiDependency(mapLiteralClass, cls);
1035 } else if (cls == boundClosureClass) { 1036 } else if (cls == boundClosureClass) {
1036 // TODO(johnniwinther): Is this a noop? 1037 // TODO(johnniwinther): Is this a noop?
1037 enqueueClass(enqueuer, boundClosureClass, registry); 1038 enqueueClass(enqueuer, boundClosureClass, registry);
1038 } else if (Elements.isNativeOrExtendsNative(cls)) { 1039 } else if (Elements.isNativeOrExtendsNative(cls)) {
1039 enqueue(enqueuer, getNativeInterceptorMethod, registry); 1040 enqueue(enqueuer, getNativeInterceptorMethod, registry);
1040 enqueueClass(enqueuer, jsInterceptorClass, compiler.globalDependencies); 1041 enqueueClass(enqueuer, jsInterceptorClass, compiler.globalDependencies);
1041 enqueueClass(enqueuer, jsPlainJavaScriptObjectClass, registry); 1042 enqueueClass(enqueuer, jsPlainJavaScriptObjectClass, registry);
1043 enqueueClass(enqueuer, jsJavaScriptFunctionClass, registry);
1042 } else if (cls == mapLiteralClass) { 1044 } else if (cls == mapLiteralClass) {
1043 // For map literals, the dependency between the implementation class 1045 // For map literals, the dependency between the implementation class
1044 // and [Map] is not visible, so we have to add it manually. 1046 // and [Map] is not visible, so we have to add it manually.
1045 Element getFactory(String name, int arity) { 1047 Element getFactory(String name, int arity) {
1046 // The constructor is on the patch class, but dart2js unit tests don't 1048 // The constructor is on the patch class, but dart2js unit tests don't
1047 // have a patch class. 1049 // have a patch class.
1048 ClassElement implementation = cls.patch != null ? cls.patch : cls; 1050 ClassElement implementation = cls.patch != null ? cls.patch : cls;
1049 ConstructorElement ctor = implementation.lookupConstructor(name); 1051 ConstructorElement ctor = implementation.lookupConstructor(name);
1050 if (ctor == null 1052 if (ctor == null
1051 || (Name.isPrivateName(name) 1053 || (Name.isPrivateName(name)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1112 addInterceptors(jsIntClass, enqueuer, registry); 1114 addInterceptors(jsIntClass, enqueuer, registry);
1113 addInterceptors(jsPositiveIntClass, enqueuer, registry); 1115 addInterceptors(jsPositiveIntClass, enqueuer, registry);
1114 addInterceptors(jsUInt32Class, enqueuer, registry); 1116 addInterceptors(jsUInt32Class, enqueuer, registry);
1115 addInterceptors(jsUInt31Class, enqueuer, registry); 1117 addInterceptors(jsUInt31Class, enqueuer, registry);
1116 addInterceptors(jsDoubleClass, enqueuer, registry); 1118 addInterceptors(jsDoubleClass, enqueuer, registry);
1117 addInterceptors(jsNumberClass, enqueuer, registry); 1119 addInterceptors(jsNumberClass, enqueuer, registry);
1118 } else if (cls == jsPlainJavaScriptObjectClass) { 1120 } else if (cls == jsPlainJavaScriptObjectClass) {
1119 addInterceptors(jsPlainJavaScriptObjectClass, enqueuer, registry); 1121 addInterceptors(jsPlainJavaScriptObjectClass, enqueuer, registry);
1120 } else if (cls == jsUnknownJavaScriptObjectClass) { 1122 } else if (cls == jsUnknownJavaScriptObjectClass) {
1121 addInterceptors(jsUnknownJavaScriptObjectClass, enqueuer, registry); 1123 addInterceptors(jsUnknownJavaScriptObjectClass, enqueuer, registry);
1124 } else if (cls == jsJavaScriptFunctionClass) {
1125 addInterceptors(jsJavaScriptFunctionClass, enqueuer, registry);
1122 } else if (Elements.isNativeOrExtendsNative(cls)) { 1126 } else if (Elements.isNativeOrExtendsNative(cls)) {
1123 addInterceptorsForNativeClassMembers(cls, enqueuer); 1127 addInterceptorsForNativeClassMembers(cls, enqueuer);
1124 } else if (cls == jsIndexingBehaviorInterface) { 1128 } else if (cls == jsIndexingBehaviorInterface) {
1125 // These two helpers are used by the emitter and the codegen. 1129 // These two helpers are used by the emitter and the codegen.
1126 // Because we cannot enqueue elements at the time of emission, 1130 // Because we cannot enqueue elements at the time of emission,
1127 // we make sure they are always generated. 1131 // we make sure they are always generated.
1128 enqueue(enqueuer, findHelper('isJsIndexable'), registry); 1132 enqueue(enqueuer, findHelper('isJsIndexable'), registry);
1129 } 1133 }
1130 1134
1131 customElementsAnalysis.registerInstantiatedClass(cls, enqueuer); 1135 customElementsAnalysis.registerInstantiatedClass(cls, enqueuer);
1132 } 1136 }
1133 1137
1134 void registerUseInterceptor(Enqueuer enqueuer) { 1138 void registerUseInterceptor(Enqueuer enqueuer) {
1135 assert(!enqueuer.isResolutionQueue); 1139 assert(!enqueuer.isResolutionQueue);
1136 if (!enqueuer.nativeEnqueuer.hasInstantiatedNativeClasses()) return; 1140 if (!enqueuer.nativeEnqueuer.hasInstantiatedNativeClasses()) return;
1137 Registry registry = compiler.globalDependencies; 1141 Registry registry = compiler.globalDependencies;
1138 enqueue(enqueuer, getNativeInterceptorMethod, registry); 1142 enqueue(enqueuer, getNativeInterceptorMethod, registry);
1139 enqueueClass(enqueuer, jsPlainJavaScriptObjectClass, registry); 1143 enqueueClass(enqueuer, jsPlainJavaScriptObjectClass, registry);
1144 enqueueClass(enqueuer, jsJavaScriptFunctionClass, registry);
1140 needToInitializeIsolateAffinityTag = true; 1145 needToInitializeIsolateAffinityTag = true;
1141 needToInitializeDispatchProperty = true; 1146 needToInitializeDispatchProperty = true;
1142 } 1147 }
1143 1148
1144 JavaScriptItemCompilationContext createItemCompilationContext() { 1149 JavaScriptItemCompilationContext createItemCompilationContext() {
1145 return new JavaScriptItemCompilationContext(); 1150 return new JavaScriptItemCompilationContext();
1146 } 1151 }
1147 1152
1148 void enqueueHelpers(ResolutionEnqueuer world, Registry registry) { 1153 void enqueueHelpers(ResolutionEnqueuer world, Registry registry) {
1149 assert(interceptorsLibrary != null); 1154 assert(interceptorsLibrary != null);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1265 } 1270 }
1266 // We also need the native variant of the check (for DOM types). 1271 // We also need the native variant of the check (for DOM types).
1267 helper = getNativeCheckedModeHelper(type, typeCast: false); 1272 helper = getNativeCheckedModeHelper(type, typeCast: false);
1268 if (helper != null) { 1273 if (helper != null) {
1269 enqueue(world, helper.getElement(compiler), registry); 1274 enqueue(world, helper.getElement(compiler), registry);
1270 } 1275 }
1271 } 1276 }
1272 if (!type.treatAsRaw || type.containsTypeVariables) { 1277 if (!type.treatAsRaw || type.containsTypeVariables) {
1273 enqueueClass(world, compiler.listClass, registry); 1278 enqueueClass(world, compiler.listClass, registry);
1274 } 1279 }
1275 if (type.element != null && type.element.isNative) { 1280 if (type.element != null && (type.element.isNative || type.element.isJsInter op)) {
alexandre.ardhuin 2015/09/03 20:14:11 nit: line length
1276 // We will neeed to add the "$is" and "$as" properties on the 1281 // We will neeed to add the "$is" and "$as" properties on the
1277 // JavaScript object prototype, so we make sure 1282 // JavaScript object prototype, so we make sure
1278 // [:defineProperty:] is compiled. 1283 // [:defineProperty:] is compiled.
1279 enqueue(world, findHelper('defineProperty'), registry); 1284 enqueue(world, findHelper('defineProperty'), registry);
1280 } 1285 }
1281 } 1286 }
1282 1287
1283 void registerTypeVariableBoundsSubtypeCheck(DartType typeArgument, 1288 void registerTypeVariableBoundsSubtypeCheck(DartType typeArgument,
1284 DartType bound) { 1289 DartType bound) {
1285 rti.registerTypeVariableBoundsSubtypeCheck(typeArgument, bound); 1290 rti.registerTypeVariableBoundsSubtypeCheck(typeArgument, bound);
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
1461 native.NativeEnqueuer nativeResolutionEnqueuer(Enqueuer world) { 1466 native.NativeEnqueuer nativeResolutionEnqueuer(Enqueuer world) {
1462 return new native.NativeResolutionEnqueuer(world, compiler); 1467 return new native.NativeResolutionEnqueuer(world, compiler);
1463 } 1468 }
1464 1469
1465 native.NativeEnqueuer nativeCodegenEnqueuer(Enqueuer world) { 1470 native.NativeEnqueuer nativeCodegenEnqueuer(Enqueuer world) {
1466 return new native.NativeCodegenEnqueuer(world, compiler, emitter); 1471 return new native.NativeCodegenEnqueuer(world, compiler, emitter);
1467 } 1472 }
1468 1473
1469 ClassElement defaultSuperclass(ClassElement element) { 1474 ClassElement defaultSuperclass(ClassElement element) {
1470 // Native classes inherit from Interceptor. 1475 // Native classes inherit from Interceptor.
1471 return element.isNative ? jsInterceptorClass : compiler.objectClass; 1476 return (element.isNative || element.isJsInterop) ? jsInterceptorClass : comp iler.objectClass;
alexandre.ardhuin 2015/09/03 20:14:11 nit: line length
1472 } 1477 }
1473 1478
1474 /** 1479 /**
1475 * Unit test hook that returns code of an element as a String. 1480 * Unit test hook that returns code of an element as a String.
1476 * 1481 *
1477 * Invariant: [element] must be a declaration element. 1482 * Invariant: [element] must be a declaration element.
1478 */ 1483 */
1479 String assembleCode(Element element) { 1484 String assembleCode(Element element) {
1480 assert(invariant(element, element.isDeclaration)); 1485 assert(invariant(element, element.isDeclaration));
1481 var code = generatedCode[element]; 1486 var code = generatedCode[element];
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after
2092 jsUInt31Class = findClass('JSUInt31'); 2097 jsUInt31Class = findClass('JSUInt31');
2093 jsDoubleClass = findClass('JSDouble'); 2098 jsDoubleClass = findClass('JSDouble');
2094 jsNumberClass = findClass('JSNumber'); 2099 jsNumberClass = findClass('JSNumber');
2095 jsNullClass = findClass('JSNull'); 2100 jsNullClass = findClass('JSNull');
2096 jsBoolClass = findClass('JSBool'); 2101 jsBoolClass = findClass('JSBool');
2097 jsMutableArrayClass = findClass('JSMutableArray'); 2102 jsMutableArrayClass = findClass('JSMutableArray');
2098 jsFixedArrayClass = findClass('JSFixedArray'); 2103 jsFixedArrayClass = findClass('JSFixedArray');
2099 jsExtendableArrayClass = findClass('JSExtendableArray'); 2104 jsExtendableArrayClass = findClass('JSExtendableArray');
2100 jsUnmodifiableArrayClass = findClass('JSUnmodifiableArray'); 2105 jsUnmodifiableArrayClass = findClass('JSUnmodifiableArray');
2101 jsPlainJavaScriptObjectClass = findClass('PlainJavaScriptObject'); 2106 jsPlainJavaScriptObjectClass = findClass('PlainJavaScriptObject');
2107 jsJavaScriptFunctionClass = findClass('JavaScriptFunction');
2102 jsUnknownJavaScriptObjectClass = findClass('UnknownJavaScriptObject'); 2108 jsUnknownJavaScriptObjectClass = findClass('UnknownJavaScriptObject');
2103 jsIndexableClass = findClass('JSIndexable'); 2109 jsIndexableClass = findClass('JSIndexable');
2104 jsMutableIndexableClass = findClass('JSMutableIndexable'); 2110 jsMutableIndexableClass = findClass('JSMutableIndexable');
2105 } else if (uri == DART_JS_HELPER) { 2111 } else if (uri == DART_JS_HELPER) {
2106 initializeHelperClasses(); 2112 initializeHelperClasses();
2107 assertMethod = findHelper('assertHelper'); 2113 assertMethod = findHelper('assertHelper');
2108 2114
2109 typeLiteralClass = findClass('TypeImpl'); 2115 typeLiteralClass = findClass('TypeImpl');
2110 constMapLiteralClass = findClass('ConstantMap'); 2116 constMapLiteralClass = findClass('ConstantMap');
2111 typeVariableClass = findClass('TypeVariable'); 2117 typeVariableClass = findClass('TypeVariable');
(...skipping 883 matching lines...) Expand 10 before | Expand all | Expand 10 after
2995 backend.getAssertSubtypeOfRuntimeType(), registry); 3001 backend.getAssertSubtypeOfRuntimeType(), registry);
2996 } 3002 }
2997 } 3003 }
2998 registerBackendInstantiation(backend.compiler.listClass, registry); 3004 registerBackendInstantiation(backend.compiler.listClass, registry);
2999 } 3005 }
3000 if (type is FunctionType) { 3006 if (type is FunctionType) {
3001 registerBackendStaticInvocation( 3007 registerBackendStaticInvocation(
3002 backend.find(backend.jsHelperLibrary, 'functionTypeTestMetaHelper'), 3008 backend.find(backend.jsHelperLibrary, 'functionTypeTestMetaHelper'),
3003 registry); 3009 registry);
3004 } 3010 }
3005 if (type.element != null && type.element.isNative) { 3011 if (type.element != null && (type.element.isNative || type.element.isJsInter op)) {
alexandre.ardhuin 2015/09/03 20:14:11 nit: line length
3006 // We will neeed to add the "$is" and "$as" properties on the 3012 // We will neeed to add the "$is" and "$as" properties on the
3007 // JavaScript object prototype, so we make sure 3013 // JavaScript object prototype, so we make sure
3008 // [:defineProperty:] is compiled. 3014 // [:defineProperty:] is compiled.
3009 registerBackendStaticInvocation( 3015 registerBackendStaticInvocation(
3010 backend.find(backend.jsHelperLibrary, 'defineProperty'), registry); 3016 backend.find(backend.jsHelperLibrary, 'defineProperty'), registry);
3011 } 3017 }
3012 } 3018 }
3013 3019
3014 void onTypeVariableBoundCheck(Registry registry) { 3020 void onTypeVariableBoundCheck(Registry registry) {
3015 assert(registry.isForResolution); 3021 assert(registry.isForResolution);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
3098 } 3104 }
3099 } 3105 }
3100 3106
3101 /// Records that [constant] is used by the element behind [registry]. 3107 /// Records that [constant] is used by the element behind [registry].
3102 class Dependency { 3108 class Dependency {
3103 final ConstantValue constant; 3109 final ConstantValue constant;
3104 final Element annotatedElement; 3110 final Element annotatedElement;
3105 3111
3106 const Dependency(this.constant, this.annotatedElement); 3112 const Dependency(this.constant, this.annotatedElement);
3107 } 3113 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698