Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clr binding improvement #53

Merged
merged 6 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ILRuntime/CLR/Method/ILMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ unsafe void InitToken(ref OpCode code, object token, Dictionary<Mono.Cecil.Cil.I
case OpCodeEnum.Ldfld:
case OpCodeEnum.Ldflda:
{
code.TokenInteger = appdomain.GetFieldIndex(token, declaringType, this);
code.TokenLong = appdomain.GetStaticFieldIndex(token, declaringType, this);
}
break;

Expand Down
8 changes: 8 additions & 0 deletions ILRuntime/CLR/TypeSystem/CLRType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ public void SetStaticFieldValue(int hash, object value)
if (fieldMapping == null)
InitializeFields();

var setter = GetFieldSetter(hash);
object target = null;
if (setter != null)
{
setter(ref target, value);
return;
}

var fieldInfo = GetField(hash);
if (fieldInfo != null)
{
Expand Down
1 change: 1 addition & 0 deletions ILRuntime/CLR/Utils/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using ILRuntime.CLR.TypeSystem;
using ILRuntime.CLR.Method;
using ILRuntime.Other;
using Mono.Cecil;
using ILRuntime.Runtime.Intepreter;
namespace ILRuntime.CLR.Utils
Expand Down
6 changes: 6 additions & 0 deletions ILRuntime/ILRuntime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
<Compile Include="Reflection\ILRuntimeWrapperType.cs" />
<Compile Include="Reflection\ILRuntimeType.cs" />
<Compile Include="Runtime\Adaptors\CLRCrossBindingAdaptors.cs" />
<Compile Include="Runtime\CLRBinding\BindingGeneratorExtensions.cs" />
<Compile Include="Runtime\CLRBinding\CommonBindingGenerator.cs" />
<Compile Include="Runtime\CLRBinding\ValueTypeBindingGenerator.cs" />
<Compile Include="Runtime\CLRBinding\FieldBindingGenerator.cs" />
<Compile Include="Runtime\CLRBinding\MethodBindingGenerator.cs" />
<Compile Include="Runtime\CLRBinding\ConstructorBindingGenerator.cs" />
<Compile Include="Runtime\CLRBinding\BindingCodeGenerator.cs" />
<Compile Include="Runtime\Debugger\BreakpointInfo.cs" />
<Compile Include="Runtime\Debugger\DebuggerServer\DebuggerServer.cs" />
Expand Down
17 changes: 10 additions & 7 deletions ILRuntime/Other/ByReferenceKeyComparer.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;

public class ByReferenceKeyComparer<T> : IEqualityComparer<T>
namespace ILRuntime.Other
{
public bool Equals(T x, T y)
class ByReferenceKeyComparer<T> : IEqualityComparer<T>
{
return object.ReferenceEquals(x, y);
}
public bool Equals(T x, T y)
{
return object.ReferenceEquals(x, y);
}

public int GetHashCode(T obj)
{
return RuntimeHelpers.GetHashCode(obj);
public int GetHashCode(T obj)
{
return RuntimeHelpers.GetHashCode(obj);
}
}
}
1,277 changes: 288 additions & 989 deletions ILRuntime/Runtime/CLRBinding/BindingCodeGenerator.cs

Large diffs are not rendered by default.

321 changes: 321 additions & 0 deletions ILRuntime/Runtime/CLRBinding/BindingGeneratorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using ILRuntime.Runtime.Enviorment;

namespace ILRuntime.Runtime.CLRBinding
{
static class BindingGeneratorExtensions
{
internal static bool ShouldSkipField(this Type type, FieldInfo i)
{
if (i.IsPrivate)
return true;
//EventHandler is currently not supported
if (i.IsSpecialName)
{
return true;
}
if (i.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length > 0)
return true;
return false;
}

internal static bool ShouldSkipMethod(this Type type, MethodBase i)
{
if (i.IsPrivate)
return true;
if (i.IsGenericMethod)
return true;
//EventHandler is currently not supported
if (i.IsSpecialName)
{
string[] t = i.Name.Split('_');
if (t[0] == "add" || t[0] == "remove")
return true;
if (t[0] == "get" || t[0] == "set")
{
var prop = type.GetProperty(t[1]);
if (prop == null)
return true;
if (prop.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length > 0)
return true;
}
}
if (i.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length > 0)
return true;
var param = i.GetParameters();
foreach (var j in param)
{
if (j.ParameterType.IsPointer)
return true;
}
return false;
}

internal static void AppendParameters(this ParameterInfo[] param, StringBuilder sb)
{
bool first = true;
foreach (var j in param)
{
if (first)
first = false;
else
sb.Append(", ");
if (j.IsOut)
sb.Append("out ");
else if (j.ParameterType.IsByRef)
sb.Append("ref ");
sb.Append(j.Name);
}
}

internal static string GetRetrieveValueCode(this Type type, string realClsName)
{
if (type.IsByRef)
type = type.GetElementType();
if (type.IsPrimitive)
{
if (type == typeof(int))
{
return "ptr_of_this_method->Value";
}
else if (type == typeof(long))
{
return "*(long*)&ptr_of_this_method->Value";
}
else if (type == typeof(short))
{
return "(short)ptr_of_this_method->Value";
}
else if (type == typeof(bool))
{
return "ptr_of_this_method->Value == 1";
}
else if (type == typeof(ushort))
{
return "(ushort)ptr_of_this_method->Value";
}
else if (type == typeof(float))
{
return "*(float*)&ptr_of_this_method->Value";
}
else if (type == typeof(double))
{
return "*(double*)&ptr_of_this_method->Value";
}
else if (type == typeof(byte))
{
return "(byte)ptr_of_this_method->Value";
}
else if (type == typeof(sbyte))
{
return "(sbyte)ptr_of_this_method->Value";
}
else if (type == typeof(uint))
{
return "(uint)ptr_of_this_method->Value";
}
else if (type == typeof(char))
{
return "(char)ptr_of_this_method->Value";
}
else if (type == typeof(ulong))
{
return "*(ulong*)&ptr_of_this_method->Value";
}
else
throw new NotImplementedException();
}
else
{
return string.Format("({0})typeof({0}).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack))", realClsName);
}
}

internal static void GetRefWriteBackValueCode(this Type type, StringBuilder sb, string paramName)
{
if (type.IsPrimitive)
{
if (type == typeof(int))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Integer;");
sb.Append(" ___dst->Value = " + paramName);
sb.AppendLine(";");
}
else if (type == typeof(long))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Long;");
sb.Append(" *(long*)&___dst->Value = " + paramName);
sb.AppendLine(";");
}
else if (type == typeof(short))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Integer;");
sb.Append(" ___dst->Value = " + paramName);
sb.AppendLine(";");
}
else if (type == typeof(bool))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Integer;");
sb.Append(" ___dst->Value = " + paramName + " ? 1 : 0;");
sb.AppendLine(";");
}
else if (type == typeof(ushort))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Integer;");
sb.Append(" ___dst->Value = " + paramName);
sb.AppendLine(";");
}
else if (type == typeof(float))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Float;");
sb.Append(" *(float*)&___dst->Value = " + paramName);
sb.AppendLine(";");
}
else if (type == typeof(double))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Double;");
sb.Append(" *(double*)&___dst->Value = " + paramName);
sb.AppendLine(";");
}
else if (type == typeof(byte))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Integer;");
sb.Append(" ___dst->Value = " + paramName);
sb.AppendLine(";");
}
else if (type == typeof(sbyte))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Integer;");
sb.Append(" ___dst->Value = " + paramName);
sb.AppendLine(";");
}
else if (type == typeof(uint))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Integer;");
sb.Append(" ___dst->Value = (int)" + paramName);
sb.AppendLine(";");
}
else if (type == typeof(char))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Integer;");
sb.Append(" ___dst->Value = (int)" + paramName);
sb.AppendLine(";");
}
else if (type == typeof(ulong))
{
sb.AppendLine(" ___dst->ObjectType = ObjectTypes.Long;");
sb.Append(" *(ulong*)&___dst->Value = " + paramName);
sb.AppendLine(";");
}
else
throw new NotImplementedException();
}
else
{
if (!type.IsValueType)
{
sb.Append(@" object ___obj = ");
sb.Append(paramName);
sb.AppendLine(";");

sb.AppendLine(@" if (___obj is CrossBindingAdaptorType)
___obj = ((CrossBindingAdaptorType)___obj).ILInstance;
__mStack[___dst->Value] = ___obj; ");
}
else
{
sb.Append(" __mStack[___dst->Value] = ");
sb.Append(paramName);
sb.AppendLine(";");
}
}
}

internal static void GetReturnValueCode(this Type type, StringBuilder sb)
{
if (type.IsPrimitive)
{
if (type == typeof(int))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Integer;");
sb.AppendLine(" __ret->Value = result_of_this_method;");
}
else if (type == typeof(long))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Long;");
sb.AppendLine(" *(long*)&__ret->Value = result_of_this_method;");
}
else if (type == typeof(short))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Integer;");
sb.AppendLine(" __ret->Value = result_of_this_method;");
}
else if (type == typeof(bool))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Integer;");
sb.AppendLine(" __ret->Value = result_of_this_method ? 1 : 0;");
}
else if (type == typeof(ushort))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Integer;");
sb.AppendLine(" __ret->Value = result_of_this_method;");
}
else if (type == typeof(float))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Float;");
sb.AppendLine(" *(float*)&__ret->Value = result_of_this_method;");
}
else if (type == typeof(double))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Double;");
sb.AppendLine(" *(double*)&__ret->Value = result_of_this_method;");
}
else if (type == typeof(byte))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Integer;");
sb.AppendLine(" __ret->Value = result_of_this_method;");
}
else if (type == typeof(sbyte))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Integer;");
sb.AppendLine(" __ret->Value = result_of_this_method;");
}
else if (type == typeof(uint))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Integer;");
sb.AppendLine(" __ret->Value = (int)result_of_this_method;");
}
else if (type == typeof(char))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Integer;");
sb.AppendLine(" __ret->Value = (int)result_of_this_method;");
}
else if (type == typeof(ulong))
{
sb.AppendLine(" __ret->ObjectType = ObjectTypes.Long;");
sb.AppendLine(" *(ulong*)&__ret->Value = result_of_this_method;");
}
else
throw new NotImplementedException();
sb.AppendLine(" return __ret + 1;");

}
else
{
if (!type.IsSealed && type != typeof(ILRuntime.Runtime.Intepreter.ILTypeInstance))
{
sb.AppendLine(@" object obj_result_of_this_method = result_of_this_method;
if(obj_result_of_this_method is CrossBindingAdaptorType)
{
return ILIntepreter.PushObject(__ret, __mStack, ((CrossBindingAdaptorType)obj_result_of_this_method).ILInstance);
}");
}
sb.AppendLine(" return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method);");
}
}
}
}
Loading