Skip to content

Commit

Permalink
Fixed Ourpalm#306
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Apr 1, 2020
1 parent 84d3910 commit 70bfd54
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ILRuntime/Runtime/Enviorment/AppDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public unsafe AppDomain()
{
RegisterCLRMethodRedirection(i, CLRRedirections.EnumGetName);
}
if(i.Name == "HasFlag")
{
RegisterCLRMethodRedirection(i, CLRRedirections.EnumHasFlag);
}
if (i.Name == "ToObject" && i.GetParameters()[1].ParameterType == typeof(int))
{
RegisterCLRMethodRedirection(i, CLRRedirections.EnumToObject);
Expand Down
30 changes: 30 additions & 0 deletions ILRuntime/Runtime/Enviorment/CLRRedirections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,5 +1165,35 @@ static object CheckCrossBindingAdapter(object obj)
else
return ILIntepreter.PushObject(ret, mStack, Enum.GetName(t, val), true);
}

public static StackObject* EnumHasFlag(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
{
var ret = esp - 1 - 1;
AppDomain domain = intp.AppDomain;

var p = esp - 1;
object val = StackObject.ToObject(p, domain, mStack);
intp.Free(p);

p = esp - 1 - 1;
object ins = StackObject.ToObject(p, domain, mStack);
intp.Free(p);

bool res = false;
if(ins is ILEnumTypeInstance enumIns)
{
int num = enumIns.Fields[0].Value;
int valNum = ((ILEnumTypeInstance)val).Fields[0].Value;
res = (num & valNum) == valNum;
}
else
{
res = ((Enum)ins).HasFlag((Enum)val);
}
if (res)
return ILIntepreter.PushOne(ret);
else
return ILIntepreter.PushZero(ret);
}
}
}
21 changes: 21 additions & 0 deletions TestCases/EnumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ enum TestEnumSByte : sbyte
Max = sbyte.MaxValue
}

enum TestEnumFlag
{
Feature1 = 1,
Feature2 = 2,
Feature3 = 4,
Feature4 = 8,
}

enum TestEnumEmpty
{

Expand Down Expand Up @@ -269,6 +277,19 @@ public static void Test19()
Console.WriteLine(Enum.GetValues(typeof(TestEnumEmpty)).Length);
}

public static void Test20()
{
TestEnumFlag flag = TestEnumFlag.Feature1 | TestEnumFlag.Feature3;
var res = flag.HasFlag(TestEnumFlag.Feature3);
Console.WriteLine(res);
if (!res)
throw new Exception();
res = flag.HasFlag(TestEnumFlag.Feature2);
Console.WriteLine(res);
if (res)
throw new Exception();
}

class SystemType
{
public int value = 10;
Expand Down
32 changes: 32 additions & 0 deletions TestCases/SimpleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,38 @@ public static void InstanceTest()
}
}

public class TestA
{
private static TestA _instance;
public static TestA Instance
{
get
{
if (_instance == null)
{
_instance = new TestA("testerror");
}
return _instance;
}
}

private string name = "";
public TestA(string name)
{
this.name = name;
}

public void TestCall(string name)
{
Console.WriteLine($"{this.name},{name}");
}
}

public static void TestStaticFieldInstance()
{
TestA.Instance.TestCall("error test");
}

public static void MultiDimensionalArrayTest()
{
int[,] arr = new int[3, 4];
Expand Down

0 comments on commit 70bfd54

Please sign in to comment.