diff --git a/unity/Assets/core/upm/Runtime/Resources/puerts/init.mjs b/unity/Assets/core/upm/Runtime/Resources/puerts/init.mjs index 34e3b0d66f..c512478726 100644 --- a/unity/Assets/core/upm/Runtime/Resources/puerts/init.mjs +++ b/unity/Assets/core/upm/Runtime/Resources/puerts/init.mjs @@ -15,8 +15,37 @@ puer.loadType = global.__tgjsLoadType; global.__tgjsLoadType = undefined; puer.getNestedTypes = global.__tgjsGetNestedTypes; global.__tgjsGetNestedTypes = undefined; -puer.getGenericMethod = global.__tgjsGetGenericMethod; +//puer.getGenericMethod = global.__tgjsGetGenericMethod; global.__tgjsGetGenericMethod = undefined; +puer.createFunction = global.createFunction; +global.createFunction = undefined; + +puer.getGenericMethod = function(csType, methodName, ...genericArgs) { + if (!csType || (typeof csType.GetMember != 'function')) { + throw new Error('the class must be a constructor'); + } + let members = CS.Puerts.Utils.GetMethodAndOverrideMethodByName(csType, methodName); + let overloadFunctions = []; + for (let i = 0; i < members.Length; i++) { + let method = members.GetValue(i) + if (method.IsGenericMethodDefinition && method.GetGenericArguments().Length == genericArgs.length) { + let methodImpl = method.MakeGenericMethod(...genericArgs.map((x, index) => { + const ret = puer.$typeof(x); + if (!ret) { + throw new Error("invalid Type for generic arguments " + index); + } + return ret; + })) + overloadFunctions.push(methodImpl) + } + } + let overloadCount = overloadFunctions.length + if (overloadCount == 0) { + console.error("puer.getGenericMethod not found", csType.Name, methodName, genericArgs.map(x => puer.$typeof(x).Name).join(",")) + return null + } + return puer.createFunction(...overloadFunctions); +} puer.evalScript = global.__tgjsEvalScript || function (script, debugPath) { return eval(script); diff --git a/unity/Assets/core/upm/Runtime/Resources/puerts/init_il2cpp.mjs b/unity/Assets/core/upm/Runtime/Resources/puerts/init_il2cpp.mjs index 0a1cdc8e22..54506858fc 100644 --- a/unity/Assets/core/upm/Runtime/Resources/puerts/init_il2cpp.mjs +++ b/unity/Assets/core/upm/Runtime/Resources/puerts/init_il2cpp.mjs @@ -45,20 +45,9 @@ puer.getNestedTypes = function(nameOrCSType) { } } -function jsArrToCsArr(jsarr, type) { - type = type || puer.$typeof(CS.System.Object) - let arr = CS.System.Array.CreateInstance(type, jsarr.length) - for (let i = 0; i < arr.Length; i++) { - arr.SetValue(jsarr[i], i) - } - return arr -} +puer.createFunction = global.createFunction; +global.createFunction = undefined; -let MemberTypes = puer.loadType("System.Reflection.MemberTypes") -let MemberTypes_Method = MemberTypes.Method -let GENERIC_INVOKE_ERR_ARG_CHECK_FAILED = {} -let ARG_FLAG_OUT = 0x01 -let ARG_FLAG_REF = 0x02 puer.getGenericMethod = function(csType, methodName, ...genericArgs) { if (!csType || (typeof csType.GetMember != 'function')) { throw new Error('the class must be a constructor'); @@ -83,7 +72,7 @@ puer.getGenericMethod = function(csType, methodName, ...genericArgs) { console.error("puer.getGenericMethod not found", csType.Name, methodName, genericArgs.map(x => puer.$typeof(x).Name).join(",")) return null } - return globalThis.createFunction(...overloadFunctions); + return puer.createFunction(...overloadFunctions); } puer.getLastException = function() { @@ -118,19 +107,3 @@ global.__tgjsRegisterTickHandler = function(fn) { jsEnv.TickHandler = CS.System.Delegate.Combine(jsEnv.TickHandler, fn) } -function createTypedValueByTypeCode(value, typecode) { - switch (typecode) { - case CS.System.TypeCode.Char: return new CS.Puerts.CharValue(value); - case CS.System.TypeCode.SByte: return new CS.Puerts.SByteValue(value); - case CS.System.TypeCode.Byte: return new CS.Puerts.ByteValue(value); - case CS.System.TypeCode.Int16: return new CS.Puerts.Int16Value(value); - case CS.System.TypeCode.UInt16: return new CS.Puerts.UInt16Value(value); - case CS.System.TypeCode.Int32: return new CS.Puerts.Int32Value(value); - case CS.System.TypeCode.UInt32: return new CS.Puerts.UInt32Value(value); - case CS.System.TypeCode.Int64: return new CS.Puerts.Int64Value(value); - case CS.System.TypeCode.UInt64: return new CS.Puerts.UInt64Value(value); - case CS.System.TypeCode.Single: return new CS.Puerts.FloatValue(value); - case CS.System.TypeCode.Double: return new CS.Puerts.DoubleValue(value); - default: return value; - } -} \ No newline at end of file diff --git a/unity/test/Src/Cases/CrossLang/GenericTest.cs b/unity/test/Src/Cases/CrossLang/GenericTest.cs index 1be18ae0e8..d34a127e1d 100644 --- a/unity/test/Src/Cases/CrossLang/GenericTest.cs +++ b/unity/test/Src/Cases/CrossLang/GenericTest.cs @@ -212,7 +212,7 @@ public void CreateFunctionByMethodInfoTest() let method = methods.GetValue(i) overloads.push(method.MakeGenericMethod(puer.$typeof(CS.System.Int32))); } - const func = createFunction(...overloads); + const func = puer.createFunction(...overloads); return func() + func(1024); })(); ");