Skip to content

Commit

Permalink
[unity] fix Tencent#907
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieyang committed Jul 13, 2022
1 parent 6c6297e commit a62cea8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
17 changes: 13 additions & 4 deletions unity/Assets/Puerts/Editor/Src/Generator/Wrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,24 @@ public static StaticWrapperInfo FromType(Type type, List<Type> genTypes)
// 这里要先识别出NativeObject的参数位置,并将其替换
if (type.IsGenericType) {
var genericArguments = type.GetGenericArguments();
var definitionType = type.GetGenericTypeDefinition();
var definitionGenericArguments = definitionType.GetGenericArguments();

if (
genericArguments
.Where(t=> !t.IsPrimitive && t != typeof(System.String) && t != typeof(DateTime))
.Where((t, index)=>
!t.IsPrimitive && t != typeof(System.String) && t != typeof(DateTime)
&& (definitionGenericArguments[index].GetGenericParameterConstraints().Length == 0 || definitionGenericArguments[index].BaseType == null)
)
.Count() > 0
) {
IsGenericWrapper = true;
type = type.GetGenericTypeDefinition().MakeGenericType(
genericArguments.Select(t=> {
if (!t.IsPrimitive && t != typeof(System.String) && t != typeof(DateTime))
type = definitionType.MakeGenericType(
genericArguments.Select((t, index)=> {
if (
!t.IsPrimitive && t != typeof(System.String) && t != typeof(DateTime)
&& (definitionGenericArguments[index].GetGenericParameterConstraints().Length == 0 || definitionGenericArguments[index].BaseType == null)
)
{
GenericArgumentsCount++;
return GenericTSRMananger.GetDynamicGenericType(GenericArgumentsCount - 1);
Expand Down
4 changes: 0 additions & 4 deletions unity/Assets/Puerts/Runtime/Src/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,6 @@ void GetGenericMethod(IntPtr isolate, IntPtr info, IntPtr self, int paramLen)
{
try
{
System.Console.WriteLine("GetGenericMethod");
if (paramLen < 3) {
throw new Exception("invalid arguments length");
}
Expand Down Expand Up @@ -745,7 +744,6 @@ public Task WaitDebuggerAsync()
// private static void LogCallback(string msg)
// {
// #if PUERTS_GENERAL || (UNITY_WSA && !UNITY_EDITOR)
// System.Console.WriteLine(msg);
// #else
// UnityEngine.Debug.Log(msg);
// #endif
Expand All @@ -755,7 +753,6 @@ public Task WaitDebuggerAsync()
// private static void LogWarningCallback(string msg)
// {
// #if PUERTS_GENERAL || (UNITY_WSA && !UNITY_EDITOR)
// System.Console.WriteLine(msg);
// #else
// UnityEngine.Debug.Log(msg);
// #endif
Expand All @@ -765,7 +762,6 @@ public Task WaitDebuggerAsync()
// private static void LogErrorCallback(string msg)
// {
// #if PUERTS_GENERAL || (UNITY_WSA && !UNITY_EDITOR)
// System.Console.WriteLine(msg);
// #else
// UnityEngine.Debug.Log(msg);
// #endif
Expand Down
34 changes: 34 additions & 0 deletions unity/general/Src/UnitTest/GenTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Puerts.UnitTest
{
class Singleton1<T> where T: Singleton1<T>, new() {}
class Zombie: Singleton1<Zombie> {}
class Singleton2<T> where T: class, new() {}

[TestFixture]
public class GenUnitTest
{
Expand Down Expand Up @@ -34,5 +38,35 @@ public void GenericWrapper_2()
string wrapperContent = wrapRender(wrapperInfo);
Assert.True(wrapperContent.Contains("<T,S>"));
}

[Test]
public void GenericWrapper_3()
{
var jsEnv = new JsEnv(new TxtLoader());
var wrapRender = jsEnv.Eval<Func<Editor.Generator.Wrapper.StaticWrapperInfo, string>>("require('puerts/templates/wrapper.tpl.cjs')");
var genList = new List<Type>() {
typeof(Singleton1<Zombie>),
typeof(Zombie)
};
Editor.Generator.Wrapper.StaticWrapperInfo wrapperInfo = Editor.Generator.Wrapper.StaticWrapperInfo.FromType(typeof(Singleton1<Zombie>), genList);

string wrapperContent = wrapRender(wrapperInfo);
Assert.False(wrapperContent.Contains("<T>"));
}

[Test]
public void GenericWrapper_4()
{
var jsEnv = new JsEnv(new TxtLoader());
var wrapRender = jsEnv.Eval<Func<Editor.Generator.Wrapper.StaticWrapperInfo, string>>("require('puerts/templates/wrapper.tpl.cjs')");
var genList = new List<Type>() {
typeof(Singleton2<Zombie>),
typeof(Zombie)
};
Editor.Generator.Wrapper.StaticWrapperInfo wrapperInfo = Editor.Generator.Wrapper.StaticWrapperInfo.FromType(typeof(Singleton2<Zombie>), genList);

string wrapperContent = wrapRender(wrapperInfo);
Assert.True(wrapperContent.Contains("<T>"));
}
}
}

0 comments on commit a62cea8

Please sign in to comment.