Skip to content

Commit

Permalink
[unity]泛型函数也改为用createFunction实现,并把createFunction放到puer模块下
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Dec 19, 2024
1 parent ee15fd2 commit 48621e3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
31 changes: 30 additions & 1 deletion unity/Assets/core/upm/Runtime/Resources/puerts/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 3 additions & 30 deletions unity/Assets/core/upm/Runtime/Resources/puerts/init_il2cpp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion unity/test/Src/Cases/CrossLang/GenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})();
");
Expand Down

0 comments on commit 48621e3

Please sign in to comment.