Imported Upstream version 5.4.0.167

Former-commit-id: 5624ac747d633e885131e8349322922b6a59baaa
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2017-08-21 15:34:15 +00:00
parent e49d6f06c0
commit 536cd135cc
12856 changed files with 563812 additions and 223249 deletions

View File

@@ -1844,5 +1844,68 @@ public class AssemblyBuilderTest
} catch (NotSupportedException e) {
}
}
class AssemblyBuilderResolver {
private Assembly mock;
private ResolveEventHandler d;
private string theName;
public AssemblyBuilderResolver (string theName) {
mock = CreateMock (theName);
d = new ResolveEventHandler (HandleResolveEvent);
this.theName = theName;
}
public void StartHandling () {
AppDomain.CurrentDomain.AssemblyResolve += d;
}
public void StopHandling () {
AppDomain.CurrentDomain.AssemblyResolve -= d;
}
public Assembly HandleResolveEvent (Object sender, ResolveEventArgs args) {
if (args.Name.StartsWith (theName))
return mock;
else
return null;
}
private static Assembly CreateMock (string name) {
var an = new AssemblyName (name);
var ab = AssemblyBuilder.DefineDynamicAssembly (an, AssemblyBuilderAccess.ReflectionOnly);
var mb = ab.DefineDynamicModule (an.Name);
// Just make some content for the assembly
var tb = mb.DefineType ("Foo", TypeAttributes.Public);
tb.DefineDefaultConstructor (MethodAttributes.Public);
tb.CreateType ();
return ab;
}
}
[Test]
public void ResolveEventHandlerReflectionOnlyError ()
{
// Regression test for 57850.
// If a ResolveEventHandler returns a reflection-only
// AssemblyBuilder, we should throw a FileNotFoundException.
var s = "ResolveEventHandlerReflectionOnlyErrorAssembly";
var h = new AssemblyBuilderResolver (s);
Assert.Throws<FileNotFoundException>(() => {
h.StartHandling ();
var aName = new AssemblyName (s);
try {
AppDomain.CurrentDomain.Load (aName);
} finally {
h.StopHandling ();
}
});
}
}
}

View File

@@ -303,6 +303,86 @@ namespace MonoTests.System.Reflection.Emit
Assert.AreEqual (FieldAttributes.RTSpecialName, value.Attributes & FieldAttributes.RTSpecialName);
}
[Test]
public void TestCreateTypeIncompleteEnumStaticField ()
{
ModuleBuilder modBuilder = GenerateModule ();
EnumBuilder enumBuilder = GenerateEnum (modBuilder);
GenerateField (enumBuilder);
var tb = modBuilder.DefineType ("T", TypeAttributes.Public);
tb.DefineDefaultConstructor (MethodAttributes.Public);
tb.DefineField ("e", enumBuilder, FieldAttributes.Static | FieldAttributes.Public);
var t = tb.CreateType ();
Assert.IsNotNull (t);
bool caught = false;
try {
object x = Activator.CreateInstance (t);
} catch (TypeLoadException exn) {
Assert.AreEqual (t.Name, exn.TypeName);
caught = true;
}
if (!caught)
Assert.Fail ("Expected CreateInstance of a broken type to throw TLE");
}
[Test]
public void TestEnumBuilderTokenUsable () {
// Regression test for https://bugzilla.xamarin.com/show_bug.cgi?id=58361
// Create an EnumBuilder and use it in an ILGenerator that consumes its token.
var modBuilder = GenerateModule ();
EnumBuilder enumBuilder = GenerateEnum (modBuilder);
var tb = modBuilder.DefineType ("Foo", TypeAttributes.Public);
var cb = tb.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard,
Type.EmptyTypes);
var ilg = cb.GetILGenerator ();
ilg.Emit (OpCodes.Ldtoken, enumBuilder);
ilg.Emit (OpCodes.Pop);
ilg.Emit (OpCodes.Ret);
var t = tb.CreateType ();
enumBuilder.CreateType ();
var ci = t.GetConstructor (Type.EmptyTypes);
var x = ci.Invoke (null);
Assert.IsNotNull (x);
}
[Test]
public void TestEnumBuilderTokenUsableCrossAssembly () {
// Regression test for https://bugzilla.xamarin.com/show_bug.cgi?id=58361
// Create an EnumBuilder and use it in an ILGenerator that consumes its token in a different assembly.
var modBuilder = GenerateModule ();
var modBuilder2 = GenerateModule ();
EnumBuilder enumBuilder = GenerateEnum (modBuilder2);
// N.B. "tb" is in modBuilder but enumBuilder is in modBuilder2
var tb = modBuilder.DefineType ("Foo", TypeAttributes.Public);
var cb = tb.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard,
Type.EmptyTypes);
var ilg = cb.GetILGenerator ();
ilg.Emit (OpCodes.Ldtoken, enumBuilder);
ilg.Emit (OpCodes.Pop);
ilg.Emit (OpCodes.Ret);
var t = tb.CreateType ();
enumBuilder.CreateType ();
var ci = t.GetConstructor (Type.EmptyTypes);
var x = ci.Invoke (null);
Assert.IsNotNull (x);
}
private static void VerifyType (Type type)
{
Assert.IsNotNull (type.Assembly, "#V1");

View File

@@ -353,5 +353,60 @@ namespace MonoTests.System.Reflection.Emit
Assert.AreEqual (TypeAttributes.Public, gparam.Attributes, "#1");
}
[Test]
public void ActionConstructorInfoTest ()
{
// Regression test for https://bugzilla.xamarin.com/show_bug.cgi?id=58454
//
// Need to check that GenericTypeParameterBuilderTest:InternalResolve() passes the declaring type to GetMethodFromHandle()
//
/* Want to generate:
public class Store<TState> {
public Action<TSelection> Subscribe<TSelection> (TState state) {
return new Action<TSelection> (Foo<TSelection>);
}
public static void Foo<X> (X x) { }
}
... and then: new Store<string>().Subscribe<int>("x");
*/
SetUp (AssemblyBuilderAccess.Run);
var tb = module.DefineType ("Store");
var tparsStore = tb.DefineGenericParameters ("TState");
tb.DefineDefaultConstructor (MethodAttributes.Public);
var methFoo = tb.DefineMethod ("Foo", MethodAttributes.Public | MethodAttributes.Static);
var tparsFoo = methFoo.DefineGenericParameters ("X");
methFoo.SetReturnType (typeof(void));
methFoo.SetParameters (tparsFoo[0]);
methFoo.GetILGenerator().Emit (OpCodes.Ret);
var methSub = tb.DefineMethod ("Subscribe", MethodAttributes.Public | MethodAttributes.Static);
var tparsSub = methSub.DefineGenericParameters ("TSelection");
var actOfSel = typeof(Action<>).MakeGenericType (tparsSub[0]); // Action<TSelection>
methSub.SetReturnType (actOfSel);
methSub.SetParameters (tparsStore[0]); // TState
var ilg = methSub.GetILGenerator ();
ilg.Emit (OpCodes.Ldnull); // instance == null
ilg.Emit (OpCodes.Ldftn, methFoo.MakeGenericMethod (tparsSub[0])); // ldftn void class Store`1<!TState>::Foo<!!0> (!!0)
var aaa = TypeBuilder.GetConstructor (actOfSel, typeof(Action<>).GetConstructors()[0]);
ilg.Emit (OpCodes.Newobj, aaa); // new Action<TSelection> (Foo<TSelection>);
ilg.Emit (OpCodes.Ret);
var tgen = tb.CreateType (); // TState`1
var t = tgen.MakeGenericType(typeof(string));
var x = t.GetConstructor(Type.EmptyTypes).Invoke (null); // x = new Store<string> ()
var mgen = t.GetMethod("Subscribe");
var m = mgen.MakeGenericMethod (typeof (int)); // Action<int> Store<string>.Subscribe<int> (string)
var y = m.Invoke (x, new object[] {"hello"}); // x.Subscribte<int> ("hello")
Assert.IsNotNull (y);
}
}
}

View File

@@ -1 +1 @@
e011b6a087581526200eda571e3418538ec57da9
cf74124675f61fe27a093af6f9970fd092ba413d