Skip to content

Commit b1e5446

Browse files
committed
函数支持ref out
1 parent 4ff12d4 commit b1e5446

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

unity/Assets/core/upm/Runtime/Src/PInvoke/Wrapper/ExpressionsWrap.cs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,10 @@ private static Expression scriptToNative(CompileContext context, Type type, Expr
680680

681681
ret = createFunctionAdapter(context, tranType, scriptObject);
682682
}
683+
else if (tranType.IsByRef)
684+
{
685+
ret = scriptToNative(context, type.GetElementType(), callPApi(context.Apis, "get_property_uint32", context.Env, value, Expression.Constant((uint)0)));
686+
}
683687
else if (!tranType.IsValueType)
684688
{
685689
var scriptToNativeMethod = typeof(Helpper).GetMethod(nameof(Helpper.ScriptToNative_T)).MakeGenericMethod(tranType);
@@ -787,6 +791,10 @@ private static Expression checkArgument(CompileContext context, Type type, Expre
787791
{
788792
return directCheckArgumentConditions(context.Apis, context.Env, value, "is_null", "is_function");
789793
}
794+
else if (type.IsByRef)
795+
{
796+
return directCheckArgumentConditions(context.Apis, context.Env, value, "is_object");
797+
}
790798
else if (!type.IsValueType)
791799
{
792800
var isAssignableMethod = typeof(Helpper).GetMethod(nameof(Helpper.IsAssignable_ByRef)).MakeGenericMethod(type);
@@ -1032,13 +1040,51 @@ private static T BuildMethodBaseWrap<T>(MethodBase[] methodBases, bool forceChec
10321040

10331041
public static pesapi_callback BuildMethodWrap(MethodInfo[] methodInfos, bool forceCheckArgs)
10341042
{
1035-
return BuildMethodBaseWrap<pesapi_callback>(methodInfos, forceCheckArgs, (context, methodBase, info, self, getJsArg) =>
1043+
return BuildMethodBaseWrap<pesapi_callback>(methodInfos, forceCheckArgs, (contextOutside, methodBase, info, self, getJsArg) =>
10361044
{
10371045
var methodInfo = methodBase as MethodInfo;
1038-
var callMethod = Expression.Call(self, methodInfo, methodInfo.GetParameters().Select((ParameterInfo pi, int index) => scriptToNative(context, pi.ParameterType, getJsArg(index))));
1039-
var addReturn = returnToScript(context, methodInfo.ReturnType, info, callMethod);
10401046

1041-
return (addReturn != null) ? addReturn : callMethod;
1047+
var variables = new List<ParameterExpression>();
1048+
var blockExpressions = new List<Expression>();
1049+
var context = new CompileContext()
1050+
{
1051+
Variables = variables,
1052+
BlockExpressions = blockExpressions,
1053+
Apis = contextOutside.Apis,
1054+
Env = contextOutside.Env
1055+
};
1056+
1057+
var tempVariables = methodInfo.GetParameters().Select(pi => Expression.Variable(pi.ParameterType.IsByRef ? pi.ParameterType.GetElementType() : pi.ParameterType)).ToArray();
1058+
variables.AddRange(tempVariables);
1059+
var assignments = methodInfo.GetParameters().Select((ParameterInfo pi, int index) => Expression.Assign(tempVariables[index], scriptToNative(context, pi.ParameterType, getJsArg(index))));
1060+
blockExpressions.AddRange(assignments);
1061+
1062+
var callMethod = Expression.Call(self, methodInfo, tempVariables);
1063+
1064+
// call method
1065+
ParameterExpression tempResult = null;
1066+
if (methodInfo.ReturnType != typeof(void))
1067+
{
1068+
tempResult = Expression.Variable(methodInfo.ReturnType);
1069+
variables.Add(tempResult);
1070+
blockExpressions.Add(Expression.Assign(tempResult, callMethod));
1071+
}
1072+
else
1073+
{
1074+
blockExpressions.Add(callMethod);
1075+
}
1076+
1077+
blockExpressions.AddRange(methodBase.GetParameters()
1078+
.Where(pi => pi.ParameterType.IsByRef).
1079+
Select((ParameterInfo pi, int index) => callPApi(context.Apis, "set_property_uint32", context.Env, getJsArg(index), Expression.Constant((uint)0), nativeToScript(context, pi.ParameterType.GetElementType(), tempVariables[index]))));
1080+
1081+
// return if needed
1082+
if (methodInfo.ReturnType != typeof(void))
1083+
{
1084+
blockExpressions.Add(returnToScript(context, methodInfo.ReturnType, info, tempResult));
1085+
}
1086+
1087+
return Expression.Block(variables, blockExpressions);
10421088
});
10431089

10441090
}

0 commit comments

Comments
 (0)