| 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 part of dart2js; | 5 part of dart2js; |
| 6 | 6 |
| 7 abstract class ConstantVisitor<R> { | 7 abstract class ConstantVisitor<R> { |
| 8 R visitFunction(FunctionConstant constant); | 8 R visitFunction(FunctionConstant constant); |
| 9 R visitNull(NullConstant constant); | 9 R visitNull(NullConstant constant); |
| 10 R visitInt(IntConstant constant); | 10 R visitInt(IntConstant constant); |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 class MapConstant extends ObjectConstant { | 367 class MapConstant extends ObjectConstant { |
| 368 /** | 368 /** |
| 369 * The [PROTO_PROPERTY] must not be used as normal property in any JavaScript | 369 * The [PROTO_PROPERTY] must not be used as normal property in any JavaScript |
| 370 * object. It would change the prototype chain. | 370 * object. It would change the prototype chain. |
| 371 */ | 371 */ |
| 372 static const LiteralDartString PROTO_PROPERTY = | 372 static const LiteralDartString PROTO_PROPERTY = |
| 373 const LiteralDartString("__proto__"); | 373 const LiteralDartString("__proto__"); |
| 374 | 374 |
| 375 /** The dart class implementing constant map literals. */ | 375 /** The dart class implementing constant map literals. */ |
| 376 static const SourceString DART_CLASS = const SourceString("ConstantMap"); | 376 static const SourceString DART_CLASS = const SourceString("ConstantMap"); |
| 377 static const SourceString DART_STRING_CLASS = |
| 378 const SourceString("ConstantStringMap"); |
| 377 static const SourceString DART_PROTO_CLASS = | 379 static const SourceString DART_PROTO_CLASS = |
| 378 const SourceString("ConstantProtoMap"); | 380 const SourceString("ConstantProtoMap"); |
| 381 static const SourceString DART_GENERAL_CLASS = |
| 382 const SourceString("GeneralConstantMap"); |
| 379 static const SourceString LENGTH_NAME = const SourceString("length"); | 383 static const SourceString LENGTH_NAME = const SourceString("length"); |
| 380 static const SourceString JS_OBJECT_NAME = const SourceString("_jsObject"); | 384 static const SourceString JS_OBJECT_NAME = const SourceString("_jsObject"); |
| 381 static const SourceString KEYS_NAME = const SourceString("_keys"); | 385 static const SourceString KEYS_NAME = const SourceString("_keys"); |
| 382 static const SourceString PROTO_VALUE = const SourceString("_protoValue"); | 386 static const SourceString PROTO_VALUE = const SourceString("_protoValue"); |
| 387 static const SourceString JS_DATA_NAME = const SourceString("_jsData"); |
| 383 | 388 |
| 384 final ListConstant keys; | 389 final ListConstant keys; |
| 385 final List<Constant> values; | 390 final List<Constant> values; |
| 386 final Constant protoValue; | 391 final Constant protoValue; |
| 387 final int hashCode; | 392 final int hashCode; |
| 393 final bool onlyStringKeys; |
| 388 | 394 |
| 389 MapConstant(DartType type, this.keys, List<Constant> values, this.protoValue) | 395 MapConstant(DartType type, this.keys, List<Constant> values, this.protoValue, |
| 396 this.onlyStringKeys) |
| 390 : this.values = values, | 397 : this.values = values, |
| 391 this.hashCode = computeHash(type, values), | 398 this.hashCode = computeHash(type, values), |
| 392 super(type); | 399 super(type); |
| 393 bool isMap() => true; | 400 bool isMap() => true; |
| 394 | 401 |
| 395 static int computeHash(DartType type, List<Constant> values) { | 402 static int computeHash(DartType type, List<Constant> values) { |
| 396 // TODO(floitsch): create a better hash. | 403 // TODO(floitsch): create a better hash. |
| 397 int hash = 0; | 404 int hash = 0; |
| 398 for (Constant value in values) { | 405 for (Constant value in values) { |
| 399 hash ^= value.hashCode; | 406 hash ^= value.hashCode; |
| 400 } | 407 } |
| 401 hash ^= type.hashCode; | 408 hash ^= type.hashCode; |
| 402 return hash; | 409 return hash; |
| 403 } | 410 } |
| 404 | 411 |
| 405 bool operator ==(var other) { | 412 bool operator ==(var other) { |
| 406 if (other is !MapConstant) return false; | 413 if (other is !MapConstant) return false; |
| 407 MapConstant otherMap = other; | 414 MapConstant otherMap = other; |
| 408 if (hashCode != otherMap.hashCode) return false; | 415 if (hashCode != otherMap.hashCode) return false; |
| 409 if (type != other.type) return false; | 416 if (type != other.type) return false; |
| 410 if (keys != otherMap.keys) return false; | 417 if (keys != otherMap.keys) return false; |
| 411 for (int i = 0; i < values.length; i++) { | 418 for (int i = 0; i < values.length; i++) { |
| 412 if (values[i] != otherMap.values[i]) return false; | 419 if (values[i] != otherMap.values[i]) return false; |
| 413 } | 420 } |
| 414 return true; | 421 return true; |
| 415 } | 422 } |
| 416 | 423 |
| 417 List<Constant> getDependencies() { | 424 List<Constant> getDependencies() { |
| 418 List<Constant> result = <Constant>[keys]; | 425 List<Constant> result = <Constant>[]; |
| 426 if (onlyStringKeys) { |
| 427 result.add(keys); |
| 428 } else { |
| 429 // Add the keys individually to avoid generating a unused list constant |
| 430 // for the keys. |
| 431 result.addAll(keys.entries); |
| 432 } |
| 419 result.addAll(values); | 433 result.addAll(values); |
| 420 return result; | 434 return result; |
| 421 } | 435 } |
| 422 | 436 |
| 423 int get length => keys.length; | 437 int get length => keys.length; |
| 424 | 438 |
| 425 accept(ConstantVisitor visitor) => visitor.visitMap(this); | 439 accept(ConstantVisitor visitor) => visitor.visitMap(this); |
| 426 } | 440 } |
| 427 | 441 |
| 428 class InterceptorConstant extends Constant { | 442 class InterceptorConstant extends Constant { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 // TODO(ahe): Refactor constant system to store this information directly. | 504 // TODO(ahe): Refactor constant system to store this information directly. |
| 491 ClassElement classElement = type.element; | 505 ClassElement classElement = type.element; |
| 492 int count = 0; | 506 int count = 0; |
| 493 Map<Element, Constant> result = new Map<Element, Constant>(); | 507 Map<Element, Constant> result = new Map<Element, Constant>(); |
| 494 classElement.implementation.forEachInstanceField((holder, field) { | 508 classElement.implementation.forEachInstanceField((holder, field) { |
| 495 result[field] = fields[count++]; | 509 result[field] = fields[count++]; |
| 496 }, includeSuperAndInjectedMembers: true); | 510 }, includeSuperAndInjectedMembers: true); |
| 497 return result; | 511 return result; |
| 498 } | 512 } |
| 499 } | 513 } |
| OLD | NEW |