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

修复跨域适配器生成代码的 bug #750

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 14 additions & 17 deletions ILRuntime/Runtime/Enviorment/CrossBindingCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class PropertyGenerateInfo
public Type ReturnType;
public string GetterBody;
public string SettingBody;
public string Modifier;
public string GetterModifier;
public string SettingModifier;
public string OverrideString;
}

Expand Down Expand Up @@ -188,7 +189,6 @@ static void GenerateCrossBindingMethodBody(StringBuilder sb, List<MethodInfo> vi
}
else
{
pInfo.Modifier = modifier;
pInfo.OverrideString = overrideStr;
}
if (!i.IsAbstract)
Expand Down Expand Up @@ -221,10 +221,12 @@ static void GenerateCrossBindingMethodBody(StringBuilder sb, List<MethodInfo> vi
{
if (isGetter)
{
pInfo.GetterModifier = modifier;
pInfo.GetterBody = sb.ToString();
}
else
{
pInfo.SettingModifier = modifier;
pInfo.SettingBody = sb.ToString();
}
sb = oriBuilder;
Expand All @@ -243,19 +245,22 @@ static void GenerateCrossBindingMethodBody(StringBuilder sb, List<MethodInfo> vi
string clsName, realClsName;
bool isByRef;
pInfo.ReturnType.GetClassName(out clsName, out realClsName, out isByRef, true);
sb.AppendLine(string.Format(" {0} {3}{1} {2}", pInfo.Modifier, realClsName, pInfo.Name, pInfo.OverrideString));
var modifier = pInfo.GetterModifier == "public" || string.IsNullOrEmpty(pInfo.SettingModifier) ? pInfo.GetterModifier : pInfo.SettingModifier;
sb.AppendLine(string.Format(" {0} {3}{1} {2}", modifier, realClsName, pInfo.Name, pInfo.OverrideString));
sb.AppendLine(" {");
if (!string.IsNullOrEmpty(pInfo.GetterBody))
{
sb.AppendLine(" get");
if (pInfo.GetterModifier == modifier) sb.AppendLine(" get");
else sb.AppendLine($" {pInfo.GetterModifier} get");
sb.AppendLine(" {");
sb.AppendLine(pInfo.GetterBody);
sb.AppendLine(" }");

}
if (!string.IsNullOrEmpty(pInfo.SettingBody))
{
sb.AppendLine(" set");
if (pInfo.SettingModifier == modifier) sb.AppendLine(" set");
else sb.AppendLine($" {pInfo.SettingModifier} set");
sb.AppendLine(" {");
sb.AppendLine(pInfo.SettingBody);
sb.AppendLine(" }");
Expand Down Expand Up @@ -322,7 +327,7 @@ static bool ShouldSkip(MethodInfo info)
var returnType = info.ReturnType;
if (returnType.IsByRef)
returnType = returnType.GetElementType();
if (returnType.IsNotPublic || returnType.IsNested && !returnType.IsNestedPublic)
if (returnType.IsNotPublic || returnType.IsNested && !returnType.IsNestedPublic && !returnType.IsNestedFamily && !returnType.IsNestedFamORAssem)
return true;

return false;
Expand Down Expand Up @@ -373,15 +378,6 @@ static string GetParametersTypeString(ParameterInfo[] param, Type returnType)
if (isByRef)
sb.Append(".MakeByRefType()");
}
if (returnType != typeof(void))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

能否提供一个生成不正确的具体例子

{
if (!first)
sb.Append(", ");
string clsName, realClsName;
bool isByRef;
returnType.GetClassName(out clsName, out realClsName, out isByRef, true);
sb.Append(realClsName);
}
return sb.ToString();
}

Expand Down Expand Up @@ -531,7 +527,7 @@ static void GenerateCrossBindingMethodClass(StringBuilder sb, string funcName, i
sb.AppendLine(" }");
if (hasReturn)
sb.AppendLine(@" else
return default(TResult);");
return default;");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unity2018以前的版本不支持default关键字,只支持default(T)

sb.AppendLine(@" }

public override void Invoke(ILTypeInstance instance)
Expand Down Expand Up @@ -602,7 +598,8 @@ static void GenInitParams(StringBuilder sb, ParameterInfo[] param)
{
if (p.IsOut)
{
sb.AppendLine(string.Format(" {0} = default({1});", p.Name, p.ParameterType.GetElementType().FullName));
p.ParameterType.GetClassName(out var clsName, out var realClsName, out var isByRef, true);
sb.AppendLine(string.Format(" {0} = default({1});", p.Name, realClsName));
}
}
}
Expand Down