| 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 '../compiler.dart' show Compiler; | 7 import '../compiler.dart' show Compiler; |
| 8 import '../constant_system_dart.dart'; | 8 import '../constant_system_dart.dart'; |
| 9 import '../constants/constant_system.dart'; | 9 import '../constants/constant_system.dart'; |
| 10 import '../constants/values.dart'; | 10 import '../constants/values.dart'; |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 NumConstantValue createDouble(double d) => | 291 NumConstantValue createDouble(double d) => |
| 292 convertToJavaScriptConstant(new DoubleConstantValue(d)); | 292 convertToJavaScriptConstant(new DoubleConstantValue(d)); |
| 293 StringConstantValue createString(DartString string) { | 293 StringConstantValue createString(DartString string) { |
| 294 return new StringConstantValue(string); | 294 return new StringConstantValue(string); |
| 295 } | 295 } |
| 296 | 296 |
| 297 BoolConstantValue createBool(bool value) => new BoolConstantValue(value); | 297 BoolConstantValue createBool(bool value) => new BoolConstantValue(value); |
| 298 NullConstantValue createNull() => new NullConstantValue(); | 298 NullConstantValue createNull() => new NullConstantValue(); |
| 299 | 299 |
| 300 @override | 300 @override |
| 301 ListConstantValue createList(InterfaceType type, List<ConstantValue> values) { | 301 ListConstantValue createList( |
| 302 ResolutionInterfaceType type, List<ConstantValue> values) { |
| 302 return new ListConstantValue(type, values); | 303 return new ListConstantValue(type, values); |
| 303 } | 304 } |
| 304 | 305 |
| 305 @override | 306 @override |
| 306 ConstantValue createType(Compiler compiler, DartType type) { | 307 ConstantValue createType(Compiler compiler, ResolutionDartType type) { |
| 307 return new TypeConstantValue( | 308 return new TypeConstantValue( |
| 308 type, | 309 type, |
| 309 compiler.backend.backendClasses.typeImplementation | 310 compiler.backend.backendClasses.typeImplementation |
| 310 .computeType(compiler.resolution)); | 311 .computeType(compiler.resolution)); |
| 311 } | 312 } |
| 312 | 313 |
| 313 // Integer checks report true for -0.0, INFINITY, and -INFINITY. At | 314 // Integer checks report true for -0.0, INFINITY, and -INFINITY. At |
| 314 // runtime an 'X is int' check is implemented as: | 315 // runtime an 'X is int' check is implemented as: |
| 315 // | 316 // |
| 316 // typeof(X) === "number" && Math.floor(X) === X | 317 // typeof(X) === "number" && Math.floor(X) === X |
| 317 // | 318 // |
| 318 // We consistently match that runtime semantics at compile time as well. | 319 // We consistently match that runtime semantics at compile time as well. |
| 319 bool isInt(ConstantValue constant) { | 320 bool isInt(ConstantValue constant) { |
| 320 return constant.isInt || | 321 return constant.isInt || |
| 321 constant.isMinusZero || | 322 constant.isMinusZero || |
| 322 constant.isPositiveInfinity || | 323 constant.isPositiveInfinity || |
| 323 constant.isNegativeInfinity; | 324 constant.isNegativeInfinity; |
| 324 } | 325 } |
| 325 | 326 |
| 326 bool isDouble(ConstantValue constant) => | 327 bool isDouble(ConstantValue constant) => |
| 327 constant.isDouble && !constant.isMinusZero; | 328 constant.isDouble && !constant.isMinusZero; |
| 328 bool isString(ConstantValue constant) => constant.isString; | 329 bool isString(ConstantValue constant) => constant.isString; |
| 329 bool isBool(ConstantValue constant) => constant.isBool; | 330 bool isBool(ConstantValue constant) => constant.isBool; |
| 330 bool isNull(ConstantValue constant) => constant.isNull; | 331 bool isNull(ConstantValue constant) => constant.isNull; |
| 331 | 332 |
| 332 bool isSubtype(DartTypes types, DartType s, DartType t) { | 333 bool isSubtype(DartTypes types, ResolutionDartType s, ResolutionDartType t) { |
| 333 // At runtime, an integer is both an integer and a double: the | 334 // At runtime, an integer is both an integer and a double: the |
| 334 // integer type check is Math.floor, which will return true only | 335 // integer type check is Math.floor, which will return true only |
| 335 // for real integers, and our double type check is 'typeof number' | 336 // for real integers, and our double type check is 'typeof number' |
| 336 // which will return true for both integers and doubles. | 337 // which will return true for both integers and doubles. |
| 337 if (s == types.commonElements.intType && | 338 if (s == types.commonElements.intType && |
| 338 t == types.commonElements.doubleType) { | 339 t == types.commonElements.doubleType) { |
| 339 return true; | 340 return true; |
| 340 } | 341 } |
| 341 return types.isSubtype(s, t); | 342 return types.isSubtype(s, t); |
| 342 } | 343 } |
| 343 | 344 |
| 344 MapConstantValue createMap(Compiler compiler, InterfaceType sourceType, | 345 MapConstantValue createMap( |
| 345 List<ConstantValue> keys, List<ConstantValue> values) { | 346 Compiler compiler, |
| 347 ResolutionInterfaceType sourceType, |
| 348 List<ConstantValue> keys, |
| 349 List<ConstantValue> values) { |
| 346 JavaScriptBackend backend = compiler.backend; | 350 JavaScriptBackend backend = compiler.backend; |
| 347 CommonElements commonElements = compiler.commonElements; | 351 CommonElements commonElements = compiler.commonElements; |
| 348 | 352 |
| 349 bool onlyStringKeys = true; | 353 bool onlyStringKeys = true; |
| 350 ConstantValue protoValue = null; | 354 ConstantValue protoValue = null; |
| 351 for (int i = 0; i < keys.length; i++) { | 355 for (int i = 0; i < keys.length; i++) { |
| 352 var key = keys[i]; | 356 var key = keys[i]; |
| 353 if (key.isString) { | 357 if (key.isString) { |
| 354 if (key.primitiveValue == JavaScriptMapConstant.PROTO_PROPERTY) { | 358 if (key.primitiveValue == JavaScriptMapConstant.PROTO_PROPERTY) { |
| 355 protoValue = values[i]; | 359 protoValue = values[i]; |
| 356 } | 360 } |
| 357 } else { | 361 } else { |
| 358 onlyStringKeys = false; | 362 onlyStringKeys = false; |
| 359 // Don't handle __proto__ values specially in the general map case. | 363 // Don't handle __proto__ values specially in the general map case. |
| 360 protoValue = null; | 364 protoValue = null; |
| 361 break; | 365 break; |
| 362 } | 366 } |
| 363 } | 367 } |
| 364 | 368 |
| 365 bool hasProtoKey = (protoValue != null); | 369 bool hasProtoKey = (protoValue != null); |
| 366 DartType keysType; | 370 ResolutionDartType keysType; |
| 367 if (sourceType.treatAsRaw) { | 371 if (sourceType.treatAsRaw) { |
| 368 keysType = commonElements.listType(); | 372 keysType = commonElements.listType(); |
| 369 } else { | 373 } else { |
| 370 keysType = commonElements.listType(sourceType.typeArguments.first); | 374 keysType = commonElements.listType(sourceType.typeArguments.first); |
| 371 } | 375 } |
| 372 ListConstantValue keysList = new ListConstantValue(keysType, keys); | 376 ListConstantValue keysList = new ListConstantValue(keysType, keys); |
| 373 ClassElement classElement = onlyStringKeys | 377 ClassElement classElement = onlyStringKeys |
| 374 ? (hasProtoKey | 378 ? (hasProtoKey |
| 375 ? backend.helpers.constantProtoMapClass | 379 ? backend.helpers.constantProtoMapClass |
| 376 : backend.helpers.constantStringMapClass) | 380 : backend.helpers.constantStringMapClass) |
| 377 : backend.helpers.generalConstantMapClass; | 381 : backend.helpers.generalConstantMapClass; |
| 378 classElement.ensureResolved(compiler.resolution); | 382 classElement.ensureResolved(compiler.resolution); |
| 379 List<DartType> typeArgument = sourceType.typeArguments; | 383 List<ResolutionDartType> typeArgument = sourceType.typeArguments; |
| 380 InterfaceType type; | 384 ResolutionInterfaceType type; |
| 381 if (sourceType.treatAsRaw) { | 385 if (sourceType.treatAsRaw) { |
| 382 type = classElement.rawType; | 386 type = classElement.rawType; |
| 383 } else { | 387 } else { |
| 384 type = new InterfaceType(classElement, typeArgument); | 388 type = new ResolutionInterfaceType(classElement, typeArgument); |
| 385 } | 389 } |
| 386 return new JavaScriptMapConstant( | 390 return new JavaScriptMapConstant( |
| 387 type, keysList, values, protoValue, onlyStringKeys); | 391 type, keysList, values, protoValue, onlyStringKeys); |
| 388 } | 392 } |
| 389 | 393 |
| 390 @override | 394 @override |
| 391 ConstantValue createSymbol(Compiler compiler, String text) { | 395 ConstantValue createSymbol(Compiler compiler, String text) { |
| 392 // TODO(johnniwinther): Create a backend agnostic value. | 396 // TODO(johnniwinther): Create a backend agnostic value. |
| 393 JavaScriptBackend backend = compiler.backend; | 397 JavaScriptBackend backend = compiler.backend; |
| 394 ClassElement symbolClass = backend.helpers.symbolImplementationClass; | 398 ClassElement symbolClass = backend.helpers.symbolImplementationClass; |
| 395 InterfaceType type = symbolClass.rawType; | 399 ResolutionInterfaceType type = symbolClass.rawType; |
| 396 ConstantValue argument = createString(new DartString.literal(text)); | 400 ConstantValue argument = createString(new DartString.literal(text)); |
| 397 Map<FieldElement, ConstantValue> fields = <FieldElement, ConstantValue>{}; | 401 Map<FieldElement, ConstantValue> fields = <FieldElement, ConstantValue>{}; |
| 398 symbolClass.forEachInstanceField( | 402 symbolClass.forEachInstanceField( |
| 399 (ClassElement enclosingClass, FieldElement field) { | 403 (ClassElement enclosingClass, FieldElement field) { |
| 400 fields[field] = argument; | 404 fields[field] = argument; |
| 401 }, includeSuperAndInjectedMembers: true); | 405 }, includeSuperAndInjectedMembers: true); |
| 402 assert(fields.length == 1); | 406 assert(fields.length == 1); |
| 403 return new ConstructedConstantValue(type, fields); | 407 return new ConstructedConstantValue(type, fields); |
| 404 } | 408 } |
| 405 } | 409 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 420 static const String LENGTH_NAME = "_length"; | 424 static const String LENGTH_NAME = "_length"; |
| 421 static const String JS_OBJECT_NAME = "_jsObject"; | 425 static const String JS_OBJECT_NAME = "_jsObject"; |
| 422 static const String KEYS_NAME = "_keys"; | 426 static const String KEYS_NAME = "_keys"; |
| 423 static const String PROTO_VALUE = "_protoValue"; | 427 static const String PROTO_VALUE = "_protoValue"; |
| 424 static const String JS_DATA_NAME = "_jsData"; | 428 static const String JS_DATA_NAME = "_jsData"; |
| 425 | 429 |
| 426 final ListConstantValue keyList; | 430 final ListConstantValue keyList; |
| 427 final ConstantValue protoValue; | 431 final ConstantValue protoValue; |
| 428 final bool onlyStringKeys; | 432 final bool onlyStringKeys; |
| 429 | 433 |
| 430 JavaScriptMapConstant(InterfaceType type, ListConstantValue keyList, | 434 JavaScriptMapConstant(ResolutionInterfaceType type, ListConstantValue keyList, |
| 431 List<ConstantValue> values, this.protoValue, this.onlyStringKeys) | 435 List<ConstantValue> values, this.protoValue, this.onlyStringKeys) |
| 432 : this.keyList = keyList, | 436 : this.keyList = keyList, |
| 433 super(type, keyList.entries, values); | 437 super(type, keyList.entries, values); |
| 434 bool get isMap => true; | 438 bool get isMap => true; |
| 435 | 439 |
| 436 List<ConstantValue> getDependencies() { | 440 List<ConstantValue> getDependencies() { |
| 437 List<ConstantValue> result = <ConstantValue>[]; | 441 List<ConstantValue> result = <ConstantValue>[]; |
| 438 if (onlyStringKeys) { | 442 if (onlyStringKeys) { |
| 439 result.add(keyList); | 443 result.add(keyList); |
| 440 } else { | 444 } else { |
| 441 // Add the keys individually to avoid generating an unused list constant | 445 // Add the keys individually to avoid generating an unused list constant |
| 442 // for the keys. | 446 // for the keys. |
| 443 result.addAll(keys); | 447 result.addAll(keys); |
| 444 } | 448 } |
| 445 result.addAll(values); | 449 result.addAll(values); |
| 446 return result; | 450 return result; |
| 447 } | 451 } |
| 448 } | 452 } |
| OLD | NEW |