use of com.google.javascript.jscomp.colors.ColorId in project closure-compiler by google.
the class ColorPoolTest method uuid_mustNotBeAxiomatic.
@Test
public void uuid_mustNotBeAxiomatic() {
for (ColorId id : StandardColors.AXIOMATIC_COLORS.keySet()) {
// Given
TypePool typePool = singleObjectPool(ObjectTypeProto.newBuilder().setUuid(id.asByteString()));
// When & Then
assertThrows(MalformedTypedAstException.class, () -> ColorPool.builder().addShardAnd(typePool, StringPool.empty()).build());
}
}
use of com.google.javascript.jscomp.colors.ColorId in project closure-compiler by google.
the class JSTypeReconserializer method recordObjectType.
private SeenTypeRecord recordObjectType(ObjectType type) {
checkNotNull(type);
ColorId id = this.hasher.hashObjectType(type);
SeenTypeRecord record = this.getOrCreateRecord(id, type);
this.addSupertypeEdges(type, record.pointer);
if (type.isFunctionType()) {
FunctionType fnType = type.toMaybeFunctionType();
if (fnType.hasInstanceType() && fnType.getInstanceType() != null) {
// We have to serialize these here ahead of time to avoid a ConcurrentModificationException
// during reconciliation.
this.serializeType(fnType.getInstanceType());
this.serializeType(fnType.getPrototype());
}
}
return record;
}
use of com.google.javascript.jscomp.colors.ColorId in project closure-compiler by google.
the class JSTypeReconserializer method recordUnionType.
private SeenTypeRecord recordUnionType(UnionType type) {
checkNotNull(type);
LinkedHashSet<SeenTypeRecord> altRecords = new LinkedHashSet<>();
for (JSType altType : type.getAlternates()) {
SeenTypeRecord alt = this.recordType(altType);
if (alt.unionMembers == null) {
altRecords.add(alt);
} else {
// Flatten out any nested unions. They are possible due to proxy-like types.
altRecords.addAll(alt.unionMembers);
}
}
// Some elements of the union may be equal as Colors
if (altRecords.size() == 1) {
return Iterables.getOnlyElement(altRecords);
}
ImmutableSet.Builder<ColorId> alternateIds = ImmutableSet.builder();
for (SeenTypeRecord altRecord : altRecords) {
alternateIds.add(altRecord.colorId);
}
ColorId unionId = ColorId.union(alternateIds.build());
SeenTypeRecord record = this.getOrCreateRecord(unionId, type);
if (record.unionMembers == null) {
record.unionMembers = ImmutableSet.copyOf(altRecords);
} else if (this.serializationMode.runValidation()) {
checkState(altRecords.equals(record.unionMembers), "Unions with same ID must have same members: %s => %s == %s", unionId, altRecords.stream().map((r) -> r.colorId).collect(toImmutableSet()), record.unionMembers.stream().map((r) -> r.colorId).collect(toImmutableSet()));
}
return record;
}
use of com.google.javascript.jscomp.colors.ColorId in project closure-compiler by google.
the class JSTypeColorIdHasher method hashObjectType.
ColorId hashObjectType(ObjectType type) {
checkState(type != null && !type.isEnumElementType() && !type.isNoResolvedType() && !type.isTemplatizedType() && !type.isUnknownType() && !type.isTemplateType(), type);
ColorId boxId = this.standardColorObjectIds.get(type);
if (boxId != null) {
return boxId;
}
Hasher hasher = FARM_64.newHasher();
if (type.isFunctionType()) {
FunctionType fnType = type.toMaybeFunctionType();
if (fnType.hasInstanceType()) {
hasher.putInt(Marker.HAS_INSTANCE_TYPE);
}
}
if (type.hasReferenceName()) {
this.putReferenceNameType(hasher, type);
} else {
for (String prop : type.getOwnPropertyNames()) {
putUtf8(hasher, prop);
}
}
return ColorId.fromUnsigned(hasher.hash().asLong());
}
use of com.google.javascript.jscomp.colors.ColorId in project closure-compiler by google.
the class ColorPoolTest method reconcile_shards_preserveOriginalOffsets.
@Test
public void reconcile_shards_preserveOriginalOffsets() {
// Given
ColorId otherId = ColorId.fromAscii("other");
TypePool typePool0 = TypePool.newBuilder().addType(TypeProto.newBuilder().setObject(ObjectTypeProto.newBuilder().setUuid(otherId.asByteString()).build())).addType(TypeProto.newBuilder().setObject(ObjectTypeProto.newBuilder().setUuid(TEST_ID.asByteString()).build())).build();
TypePool typePool1 = TypePool.newBuilder().addType(TypeProto.newBuilder().setObject(ObjectTypeProto.newBuilder().setUuid(TEST_ID.asByteString()).build())).addType(TypeProto.newBuilder().setObject(ObjectTypeProto.newBuilder().setUuid(otherId.asByteString()).build())).build();
// When
ColorPool.Builder colorPoolBuilder = ColorPool.builder();
ColorPool.ShardView shard0 = colorPoolBuilder.addShard(typePool0, StringPool.empty());
ColorPool.ShardView shard1 = colorPoolBuilder.addShard(typePool1, StringPool.empty());
colorPoolBuilder.build();
// Then
assertThat(shard0.getColor(poolPointer(1))).hasId(TEST_ID);
assertThat(shard0.getColor(poolPointer(1))).isSameInstanceAs(shard1.getColor(poolPointer(0)));
}
Aggregations