public void CannotUseMutableValueTypeAsTypeArgument()
        {
            var md = new MockMetadataImporter {
                GetTypeSemantics = t => t.Kind == TypeKind.Struct ? TypeScriptSemantics.MutableValueType(t.Name) : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"struct S1 {}
class C1<T> {}
class C {
	public void M() {
		var c = new C1<S1>();
	}
}"
            }, metadataImporter: md, errorReporter: er);

            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("S1"));

            er = new MockErrorReporter(false);

            Compile(new[] {
                @"struct S1 {}
class C1<T> {}
class C {
	public void M() {
		var c = new C1<C1<S1>>();
	}
}"
            }, metadataImporter: md, errorReporter: er);

            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("S1"));
        }
Ejemplo n.º 2
0
        public void CannotUseMutableValueTypeAsATypeArgumentInATypeOfExpression()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.MutableValueType(t.Name) : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"struct C1 {}
class C {
	public void M() {
		var t = typeof(C1);
	}
}"
            }, metadataImporter: metadataImporter, errorReporter: er);

            Assert.That(er.AllMessages.Count, Is.EqualTo(0));

            er = new MockErrorReporter(false);
            Compile(new[] {
                @"struct C1 {}
interface I1<T> {}
class C {
	public void M() {
		var t= typeof(I1<I1<C1>>);
	}
}"
            }, metadataImporter: metadataImporter, errorReporter: er);
            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("C1"));
        }
Ejemplo n.º 3
0
        public void ForeachStatementWithStrucElementType()
        {
            AssertCorrect(
                @"
class MyEnumerable {
	public MyEnumerator GetEnumerator() { return null; }
}
sealed class MyEnumerator {
	public int Current { get { return 0; } }
	public bool MoveNext() {}
}

public void M() {
	MyEnumerable list = null;
	// BEGIN
	foreach (var item in list) {
		int x = 0;
	}
	// END
}",
                @"	var $tmp1 = $list.GetEnumerator();
	while ($tmp1.MoveNext()) {
		var $item = $Clone($tmp1.Current, {to_Int32});
		var $x = $Clone(0, {to_Int32});
	}
", metadataImporter: new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.MutableValueType(t.Name), GetPropertySemantics = p => PropertyScriptSemantics.Field(p.Name)
            });
        }
        protected void AssertCorrect(string csharp, string expected, IMetadataImporter metadataImporter = null, IRuntimeLibrary runtimeLibrary = null, bool addSkeleton = true, IEnumerable <IAssemblyReference> references = null, string methodName = "M", bool mutableValueTypes = false)
        {
            CompileMethod(csharp, metadataImporter: metadataImporter ?? new MockMetadataImporter {
                GetPropertySemantics = p => {
                    if (p.DeclaringType.Kind == TypeKind.Anonymous || new Regex("^F[0-9]*$").IsMatch(p.Name) || (p.DeclaringType.FullName == "System.Array" && p.Name == "Length"))
                    {
                        return(PropertyScriptSemantics.Field("$" + p.Name));
                    }
                    else
                    {
                        return(PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_$" + p.Name), MethodScriptSemantics.NormalMethod("set_$" + p.Name)));
                    }
                },
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name),
                GetEventSemantics  = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_$" + e.Name), MethodScriptSemantics.NormalMethod("remove_$" + e.Name)),
                GetTypeSemantics   = t => {
                    return(mutableValueTypes && t.Kind == TypeKind.Struct ? TypeScriptSemantics.MutableValueType(t.FullName) : TypeScriptSemantics.NormalType(t.FullName));
                }
            }, runtimeLibrary: runtimeLibrary, methodName: methodName, addSkeleton: addSkeleton, references: references);
            string actual = OutputFormatter.Format(CompiledMethod, true);

            int begin = actual.IndexOf("// BEGIN");

            if (begin > -1)
            {
                while (begin < (actual.Length - 1) && actual[begin - 1] != '\n')
                {
                    begin++;
                }
                actual = actual.Substring(begin);
            }

            int end = actual.IndexOf("// END");

            if (end >= 0)
            {
                while (end >= 0 && actual[end] != '\n')
                {
                    end--;
                }
                actual = actual.Substring(0, end + 1);
            }
            Assert.That(actual.Replace("\r\n", "\n"), Is.EqualTo(expected.Replace("\r\n", "\n")), "Expected:\n" + expected + "\n\nActual:\n" + actual);
        }
Ejemplo n.º 5
0
        public void ForeachOverArrayIsOptimizedToForLoopStruct()
        {
            AssertCorrect(
                @"public void M() {
	var arr = new[] { 1, 2, 3};
	// BEGIN
	foreach (var item in arr) {
		int x = 0;
	}
	// END
}",
                @"	for (var $tmp1 = 0; $tmp1 < $arr.Length; $tmp1++) {
		var $item = $Clone($arr[$tmp1], {to_Int32});
		var $x = $Clone(0, {to_Int32});
	}
", metadataImporter: new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.MutableValueType(t.Name), GetPropertySemantics = p => PropertyScriptSemantics.Field(p.Name)
            });
        }
        public void MutableValueTypeCannotBeUsedAsGenericArgumentForABaseClassOrImplementedInterface()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.MutableValueType(t.Name) : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] { "struct C1 {} class B1<T> {} class D1 : B1<C1> {}" }, metadataImporter: metadataImporter, errorReporter: er);
            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("C1"));

            er = new MockErrorReporter(false);
            Compile(new[] { "struct C1 {} interface I1<T> {} class D1 : I1<C1> {}" }, metadataImporter: metadataImporter, errorReporter: er);
            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("C1"));

            er = new MockErrorReporter(false);
            Compile(new[] { "struct C1 {} interface I1<T> {} class D1 : I1<I1<C1>> {}" }, metadataImporter: metadataImporter, errorReporter: er);
            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("C1"));
        }
Ejemplo n.º 7
0
		public void AssigningToPropertyWithSetMethodImplementedAsInlineCodeWorksStruct() {
			AssertCorrect(
@"int P { get; set; }
public void M() {
	int i = 0;
	// BEGIN
	P = i;
	// END
}",
@"	set_(this)._($Clone($i, {to_Int32}));
", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.InlineCode("get_({this})"), MethodScriptSemantics.InlineCode("set_({this})._({value})")), GetTypeSemantics = t => TypeScriptSemantics.MutableValueType(t.Name) });
		}
Ejemplo n.º 8
0
		public void AssigningToPropertyImplementedAsNativeIndexerWorksStruct() {
			AssertCorrect(
@"int this[int x] { get { return 0; } set {} }
public void M() {
	int i = 0, j = 1, k = 2, l;
	// BEGIN
	l = this[i] = k;
	// END
}",
@"	this[$i] = $Clone($k, {to_Int32});
	$l = $Clone(this[$i], {to_Int32});
", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => p.IsIndexer ? PropertyScriptSemantics.NativeIndexer() : PropertyScriptSemantics.Field(p.Name), GetTypeSemantics = t => TypeScriptSemantics.MutableValueType(t.Name) });
		}
Ejemplo n.º 9
0
		public void AssigningToIndexerImplementedAsInlineCodeWorksStruct() {
			AssertCorrect(
@"int this[int x, int y] { get { return 0; } set {} }
public void M() {
	int i = 0, j = 1, k = 2;
	// BEGIN
	this[i, j] = k;
	// END
}",
@"	set_(this)._($Clone($i, {to_Int32}))._($Clone($j, {to_Int32}))._($Clone($k, {to_Int32}));
", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => p.IsIndexer ? PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.InlineCode("get_({this})._({x})._({y})"), MethodScriptSemantics.InlineCode("set_({this})._({x})._({y})._({value})")) : PropertyScriptSemantics.Field(p.Name), GetTypeSemantics = t => TypeScriptSemantics.MutableValueType(t.Name) });
		}