Search in sources :

Example 1 with ColorId

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());
    }
}
Also used : ColorId(com.google.javascript.jscomp.colors.ColorId) Test(org.junit.Test)

Example 2 with ColorId

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;
}
Also used : FunctionType(com.google.javascript.rhino.jstype.FunctionType) ColorId(com.google.javascript.jscomp.colors.ColorId)

Example 3 with ColorId

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Iterables(com.google.common.collect.Iterables) JSType(com.google.javascript.rhino.jstype.JSType) Comparator.naturalOrder(java.util.Comparator.naturalOrder) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ColorId(com.google.javascript.jscomp.colors.ColorId) StandardColors(com.google.javascript.jscomp.colors.StandardColors) JSTypeRegistry(com.google.javascript.rhino.jstype.JSTypeRegistry) UnionType(com.google.javascript.rhino.jstype.UnionType) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) InvalidatingTypes(com.google.javascript.jscomp.InvalidatingTypes) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) Node(com.google.javascript.rhino.Node) ObjectType(com.google.javascript.rhino.jstype.ObjectType) ImmutableSet(com.google.common.collect.ImmutableSet) IdentityHashMap(java.util.IdentityHashMap) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ClosurePrimitive(com.google.javascript.rhino.ClosurePrimitive) JSTypeNative(com.google.javascript.rhino.jstype.JSTypeNative) SetMultimap(com.google.common.collect.SetMultimap) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Color(com.google.javascript.jscomp.colors.Color) FunctionType(com.google.javascript.rhino.jstype.FunctionType) TypePointers.isAxiomatic(com.google.javascript.jscomp.serialization.TypePointers.isAxiomatic) JSType(com.google.javascript.rhino.jstype.JSType) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ColorId(com.google.javascript.jscomp.colors.ColorId)

Example 4 with ColorId

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());
}
Also used : Hasher(com.google.common.hash.Hasher) FunctionType(com.google.javascript.rhino.jstype.FunctionType) ColorId(com.google.javascript.jscomp.colors.ColorId)

Example 5 with ColorId

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)));
}
Also used : ColorId(com.google.javascript.jscomp.colors.ColorId) Test(org.junit.Test)

Aggregations

ColorId (com.google.javascript.jscomp.colors.ColorId)5 FunctionType (com.google.javascript.rhino.jstype.FunctionType)3 Test (org.junit.Test)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)1 SetMultimap (com.google.common.collect.SetMultimap)1 Hasher (com.google.common.hash.Hasher)1 InvalidatingTypes (com.google.javascript.jscomp.InvalidatingTypes)1 Color (com.google.javascript.jscomp.colors.Color)1 StandardColors (com.google.javascript.jscomp.colors.StandardColors)1 TypePointers.isAxiomatic (com.google.javascript.jscomp.serialization.TypePointers.isAxiomatic)1 ClosurePrimitive (com.google.javascript.rhino.ClosurePrimitive)1