| OLD | NEW |
| 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 library dart2js.constant_system.js; | 5 library dart2js.constant_system.js; |
| 6 | 6 |
| 7 import '../common/backend_api.dart' show BackendClasses; | |
| 8 import '../constant_system_dart.dart'; | 7 import '../constant_system_dart.dart'; |
| 9 import '../constants/constant_system.dart'; | 8 import '../constants/constant_system.dart'; |
| 10 import '../constants/values.dart'; | 9 import '../constants/values.dart'; |
| 11 import '../common_elements.dart' show CommonElements; | 10 import '../common_elements.dart' show CommonElements; |
| 12 import '../elements/types.dart'; | 11 import '../elements/types.dart'; |
| 13 import '../elements/resolution_types.dart' show DartTypes; | 12 import '../elements/resolution_types.dart' show DartTypes; |
| 14 import '../elements/entities.dart'; | 13 import '../elements/entities.dart'; |
| 15 import '../tree/dartstring.dart' show DartString, LiteralDartString; | 14 import '../tree/dartstring.dart' show DartString, LiteralDartString; |
| 16 | 15 |
| 17 const JAVA_SCRIPT_CONSTANT_SYSTEM = const JavaScriptConstantSystem(); | 16 const JAVA_SCRIPT_CONSTANT_SYSTEM = const JavaScriptConstantSystem(); |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 | 310 |
| 312 BoolConstantValue createBool(bool value) => new BoolConstantValue(value); | 311 BoolConstantValue createBool(bool value) => new BoolConstantValue(value); |
| 313 NullConstantValue createNull() => new NullConstantValue(); | 312 NullConstantValue createNull() => new NullConstantValue(); |
| 314 | 313 |
| 315 @override | 314 @override |
| 316 ListConstantValue createList(InterfaceType type, List<ConstantValue> values) { | 315 ListConstantValue createList(InterfaceType type, List<ConstantValue> values) { |
| 317 return new ListConstantValue(type, values); | 316 return new ListConstantValue(type, values); |
| 318 } | 317 } |
| 319 | 318 |
| 320 @override | 319 @override |
| 321 ConstantValue createType(CommonElements commonElements, | 320 ConstantValue createType(CommonElements commonElements, DartType type) { |
| 322 BackendClasses backendClasses, DartType type) { | 321 InterfaceType instanceType = commonElements.typeLiteralType; |
| 323 InterfaceType instanceType = backendClasses.typeType; | |
| 324 return new TypeConstantValue(type, instanceType); | 322 return new TypeConstantValue(type, instanceType); |
| 325 } | 323 } |
| 326 | 324 |
| 327 // Integer checks report true for -0.0, INFINITY, and -INFINITY. At | 325 // Integer checks report true for -0.0, INFINITY, and -INFINITY. At |
| 328 // runtime an 'X is int' check is implemented as: | 326 // runtime an 'X is int' check is implemented as: |
| 329 // | 327 // |
| 330 // typeof(X) === "number" && Math.floor(X) === X | 328 // typeof(X) === "number" && Math.floor(X) === X |
| 331 // | 329 // |
| 332 // We consistently match that runtime semantics at compile time as well. | 330 // We consistently match that runtime semantics at compile time as well. |
| 333 bool isInt(ConstantValue constant) { | 331 bool isInt(ConstantValue constant) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 350 // which will return true for both integers and doubles. | 348 // which will return true for both integers and doubles. |
| 351 if (s == types.commonElements.intType && | 349 if (s == types.commonElements.intType && |
| 352 t == types.commonElements.doubleType) { | 350 t == types.commonElements.doubleType) { |
| 353 return true; | 351 return true; |
| 354 } | 352 } |
| 355 return types.isSubtype(s, t); | 353 return types.isSubtype(s, t); |
| 356 } | 354 } |
| 357 | 355 |
| 358 MapConstantValue createMap( | 356 MapConstantValue createMap( |
| 359 CommonElements commonElements, | 357 CommonElements commonElements, |
| 360 BackendClasses backendClasses, | |
| 361 InterfaceType sourceType, | 358 InterfaceType sourceType, |
| 362 List<ConstantValue> keys, | 359 List<ConstantValue> keys, |
| 363 List<ConstantValue> values) { | 360 List<ConstantValue> values) { |
| 364 bool onlyStringKeys = true; | 361 bool onlyStringKeys = true; |
| 365 ConstantValue protoValue = null; | 362 ConstantValue protoValue = null; |
| 366 for (int i = 0; i < keys.length; i++) { | 363 for (int i = 0; i < keys.length; i++) { |
| 367 var key = keys[i]; | 364 var key = keys[i]; |
| 368 if (key.isString) { | 365 if (key.isString) { |
| 369 if (key.primitiveValue == JavaScriptMapConstant.PROTO_PROPERTY) { | 366 if (key.primitiveValue == JavaScriptMapConstant.PROTO_PROPERTY) { |
| 370 protoValue = values[i]; | 367 protoValue = values[i]; |
| 371 } | 368 } |
| 372 } else { | 369 } else { |
| 373 onlyStringKeys = false; | 370 onlyStringKeys = false; |
| 374 // Don't handle __proto__ values specially in the general map case. | 371 // Don't handle __proto__ values specially in the general map case. |
| 375 protoValue = null; | 372 protoValue = null; |
| 376 break; | 373 break; |
| 377 } | 374 } |
| 378 } | 375 } |
| 379 | 376 |
| 380 bool hasProtoKey = (protoValue != null); | 377 bool hasProtoKey = (protoValue != null); |
| 381 InterfaceType keysType; | 378 InterfaceType keysType; |
| 382 if (sourceType.treatAsRaw) { | 379 if (sourceType.treatAsRaw) { |
| 383 keysType = commonElements.listType(); | 380 keysType = commonElements.listType(); |
| 384 } else { | 381 } else { |
| 385 keysType = commonElements.listType(sourceType.typeArguments.first); | 382 keysType = commonElements.listType(sourceType.typeArguments.first); |
| 386 } | 383 } |
| 387 ListConstantValue keysList = new ListConstantValue(keysType, keys); | 384 ListConstantValue keysList = new ListConstantValue(keysType, keys); |
| 388 InterfaceType type = backendClasses.getConstantMapTypeFor(sourceType, | 385 InterfaceType type = commonElements.getConstantMapTypeFor(sourceType, |
| 389 hasProtoKey: hasProtoKey, onlyStringKeys: onlyStringKeys); | 386 hasProtoKey: hasProtoKey, onlyStringKeys: onlyStringKeys); |
| 390 return new JavaScriptMapConstant( | 387 return new JavaScriptMapConstant( |
| 391 type, keysList, values, protoValue, onlyStringKeys); | 388 type, keysList, values, protoValue, onlyStringKeys); |
| 392 } | 389 } |
| 393 | 390 |
| 394 @override | 391 @override |
| 395 ConstantValue createSymbol(CommonElements commonElements, | 392 ConstantValue createSymbol(CommonElements commonElements, String text) { |
| 396 BackendClasses backendClasses, String text) { | 393 InterfaceType type = commonElements.symbolImplementationType; |
| 397 InterfaceType type = backendClasses.symbolType; | 394 FieldEntity field = commonElements.symbolField; |
| 398 FieldEntity field = backendClasses.symbolField; | |
| 399 ConstantValue argument = createString(new DartString.literal(text)); | 395 ConstantValue argument = createString(new DartString.literal(text)); |
| 400 // TODO(johnniwinther): Use type arguments when all uses no longer expect | 396 // TODO(johnniwinther): Use type arguments when all uses no longer expect |
| 401 // a [FieldElement]. | 397 // a [FieldElement]. |
| 402 Map<FieldEntity, ConstantValue> fields = /*<FieldElement, ConstantValue>*/ { | 398 Map<FieldEntity, ConstantValue> fields = /*<FieldElement, ConstantValue>*/ { |
| 403 field: argument | 399 field: argument |
| 404 }; | 400 }; |
| 405 return new ConstructedConstantValue(type, fields); | 401 return new ConstructedConstantValue(type, fields); |
| 406 } | 402 } |
| 407 } | 403 } |
| 408 | 404 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 result.add(keyList); | 437 result.add(keyList); |
| 442 } else { | 438 } else { |
| 443 // Add the keys individually to avoid generating an unused list constant | 439 // Add the keys individually to avoid generating an unused list constant |
| 444 // for the keys. | 440 // for the keys. |
| 445 result.addAll(keys); | 441 result.addAll(keys); |
| 446 } | 442 } |
| 447 result.addAll(values); | 443 result.addAll(values); |
| 448 return result; | 444 return result; |
| 449 } | 445 } |
| 450 } | 446 } |
| OLD | NEW |