Search in sources :

Example 6 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class ColorPoolTest method deserializesNativeObjectTableIntoNativeColor.

@Test
public void deserializesNativeObjectTableIntoNativeColor() {
    TypePool typePool = TypePool.newBuilder().addType(TypeProto.newBuilder().setObject(ObjectTypeProto.newBuilder().setUuid(StandardColors.NUMBER_OBJECT_ID.asByteString()).setDebugInfo(ObjectTypeProto.DebugInfo.newBuilder().addTypename("Number")).setIsInvalidating(true)).build()).addType(TypeProto.newBuilder().setObject(ObjectTypeProto.newBuilder().setUuid(ByteString.copyFromUtf8("Num.pro"))).build()).addDisambiguationEdges(// Number is a subtype of Number.prototype
    SubtypingEdge.newBuilder().setSubtype(poolPointer(0)).setSupertype(poolPointer(1))).build();
    ColorPool colorPool = ColorPool.fromOnlyShard(typePool, StringPool.empty());
    ColorPool.ShardView view = colorPool.getOnlyShard();
    Color numberObject = colorPool.getColor(StandardColors.NUMBER_OBJECT_ID);
    assertThat(numberObject).isInvalidating();
    assertThat(numberObject).hasDisambiguationSupertypesThat(colorPool.getRegistry()).containsExactly(view.getColor(poolPointer(1)));
    assertThat(numberObject).isSameInstanceAs(view.getColor(poolPointer(0)));
}
Also used : Color(com.google.javascript.jscomp.colors.Color) Test(org.junit.Test)

Example 7 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class ColorPoolTest method recordsDisambiguationsSupertypes_multipleSupertypesPerSubtype.

@Test
public void recordsDisambiguationsSupertypes_multipleSupertypesPerSubtype() {
    TypePool typePool = TypePool.newBuilder().addType(TypeProto.newBuilder().setObject(ObjectTypeProto.newBuilder().setUuid(ByteString.copyFromUtf8("Foo")))).addType(TypeProto.newBuilder().setObject(ObjectTypeProto.newBuilder().setUuid(ByteString.copyFromUtf8("Bar")))).addType(TypeProto.newBuilder().setObject(ObjectTypeProto.newBuilder().setUuid(ByteString.copyFromUtf8("Baz")))).addDisambiguationEdges(SubtypingEdge.newBuilder().setSubtype(poolPointer(1)).setSupertype(poolPointer(0))).addDisambiguationEdges(SubtypingEdge.newBuilder().setSubtype(poolPointer(1)).setSupertype(poolPointer(2))).build();
    // When
    ColorPool.Builder colorPoolBuilder = ColorPool.builder();
    ColorPool.ShardView shard = colorPoolBuilder.addShard(typePool, StringPool.empty());
    ColorPool colorPool = colorPoolBuilder.build();
    // Then
    Color foo = shard.getColor(poolPointer(0));
    Color bar = shard.getColor(poolPointer(1));
    Color baz = shard.getColor(poolPointer(2));
    assertThat(bar).hasDisambiguationSupertypesThat(colorPool.getRegistry()).containsExactly(foo, baz);
}
Also used : Color(com.google.javascript.jscomp.colors.Color) Test(org.junit.Test)

Example 8 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class ColorPoolTest method deserializesTopObjectTypeFromEmptyTypePool.

@Test
public void deserializesTopObjectTypeFromEmptyTypePool() {
    ColorPool.ShardView colorPool = ColorPool.fromOnlyShard(TypePool.getDefaultInstance(), StringPool.empty()).getOnlyShard();
    Color object = colorPool.getColor(PrimitiveType.TOP_OBJECT.getNumber());
    assertThat(object).isInvalidating();
    assertThat(object.getInstanceColors()).isEmpty();
    assertThat(object.getPrototypes()).isEmpty();
}
Also used : Color(com.google.javascript.jscomp.colors.Color) Test(org.junit.Test)

Example 9 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class JSTypeReconserializer method seedCachesWithAxiomaticTypes.

/**
 * Inserts dummy pointers corresponding to all {@link PrimitiveType}s in the type pool.
 *
 * <p>These types will never correspond to an actual {@link TypeProto}. Instead, all normal {@link
 * Integer} offsets into the pool are offset by a number equivalent to the number of {@link
 * PrimitiveType} enum elements.
 */
private void seedCachesWithAxiomaticTypes() {
    checkState(this.seenTypeRecords.isEmpty());
    // Load all the axiomatic records in the right order without any types.
    for (Color axiomatic : TypePointers.OFFSET_TO_AXIOMATIC_COLOR) {
        int index = this.seenTypeRecords.size();
        SeenTypeRecord record = new SeenTypeRecord(axiomatic.getId(), index);
        this.seenTypeRecords.put(axiomatic.getId(), record);
    }
    // Add JSTypes corresponding to axiomatic IDs.
    JSTYPE_NATIVE_TO_AXIOMATIC_COLOR_MAP.forEach((jstypeNative, axiomatic) -> this.getOrCreateRecord(axiomatic.getId(), this.registry.getNativeType(jstypeNative)));
    checkState(this.seenTypeRecords.size() == TypePointers.OFFSET_TO_AXIOMATIC_COLOR.size());
}
Also used : Color(com.google.javascript.jscomp.colors.Color)

Example 10 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class AstAnalyzer method isTypedAsString.

private boolean isTypedAsString(Node n) {
    if (n.isStringLit()) {
        return true;
    }
    if (compiler.getOptions().useTypesForLocalOptimization) {
        Color color = n.getColor();
        if (color != null) {
            return color.equals(StandardColors.STRING);
        }
        JSType type = n.getJSType();
        if (type != null) {
            JSType nativeStringType = compiler.getTypeRegistry().getNativeType(JSTypeNative.STRING_TYPE);
            if (type.equals(nativeStringType)) {
                return true;
            }
        }
    }
    return false;
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Color(com.google.javascript.jscomp.colors.Color)

Aggregations

Color (com.google.javascript.jscomp.colors.Color)57 Node (com.google.javascript.rhino.Node)37 Test (org.junit.Test)37 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)29 JSType (com.google.javascript.rhino.jstype.JSType)6 TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)5 ImmutableSet (com.google.common.collect.ImmutableSet)2 CodeSubTree (com.google.javascript.jscomp.testing.CodeSubTree)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)1 CompilerPass (com.google.javascript.jscomp.CompilerPass)1 DefaultNameGenerator (com.google.javascript.jscomp.DefaultNameGenerator)1 GatherGetterAndSetterProperties (com.google.javascript.jscomp.GatherGetterAndSetterProperties)1 NameGenerator (com.google.javascript.jscomp.NameGenerator)1 NodeTraversal (com.google.javascript.jscomp.NodeTraversal)1 AbstractPostOrderCallback (com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback)1