Skip to content

Commit

Permalink
Merge branch 'Tencent:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoChenFx authored Dec 5, 2024
2 parents 5d72b62 + 006e6c6 commit 8dfb06c
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ void GetFieldValue(void *ptr, FieldInfo *field, size_t offset, void *value)
}
else
{
il2cpp::vm::Runtime::ClassInit(field->parent);
Field::StaticGetValue(field, value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

#include "pesapi.h"

#ifdef PUERTS_SHARED

EXTERN_C_START

#ifdef PUERTS_SHARED

typedef pesapi_type_info (*pesapi_alloc_type_infosType)(size_t count);
static pesapi_alloc_type_infosType pesapi_alloc_type_infos_ptr;
pesapi_type_info pesapi_alloc_type_infos (size_t count) {
Expand Down
127 changes: 65 additions & 62 deletions unity/Assets/core/upm/Runtime/Src/IL2Cpp/TypeRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,78 +206,81 @@ private static void Register(Type type, MethodBase[] ctors = null, MethodBase[]
}
}
}
}

Action<string, MethodInfo, bool, bool, bool> AddMethodToType = (string name, MethodInfo method, bool isGetter, bool isSetter, bool isExtensionMethod) =>
{
method = TypeUtils.HandleMaybeGenericMethod(method);
if (method == null) return;
List<Type> usedTypes = TypeUtils.GetUsedTypes(method, isExtensionMethod);
var signature = TypeUtils.GetMethodSignature(method, false, isExtensionMethod);
// UnityEngine.Debug.Log(string.Format("add method {0}, usedTypes count: {1}", method, usedTypes.Count));
Action<string, MethodInfo, bool, bool, bool> AddMethodToType = (string name, MethodInfo method, bool isGetter, bool isSetter, bool isExtensionMethod) =>
{
method = TypeUtils.HandleMaybeGenericMethod(method);
if (method == null) return;
List<Type> usedTypes = TypeUtils.GetUsedTypes(method, isExtensionMethod);
var signature = TypeUtils.GetMethodSignature(method, false, isExtensionMethod);
// UnityEngine.Debug.Log(string.Format("add method {0}, usedTypes count: {1}", method, usedTypes.Count));

var wrapper = GetWrapperFunc(registerInfo, method, signature);
if (wrapper == IntPtr.Zero)
{
UnityEngine.Debug.LogWarning(string.Format("wrapper is null for {0}:{1}, signature:{2}", type, method, TypeUtils.GetMethodSignature(method, false, isExtensionMethod)));
return;
}

var methodInfoPointer = NativeAPI.GetMethodInfoPointer(method);
var methodPointer = NativeAPI.GetMethodPointer(method);
if (methodInfoPointer == IntPtr.Zero)
var wrapper = GetWrapperFunc(registerInfo, method, signature);
if (wrapper == IntPtr.Zero)
{
UnityEngine.Debug.LogWarning(string.Format("wrapper is null for {0}:{1}, signature:{2}", type, method, TypeUtils.GetMethodSignature(method, false, isExtensionMethod)));
return;
}

var methodInfoPointer = NativeAPI.GetMethodInfoPointer(method);
var methodPointer = NativeAPI.GetMethodPointer(method);
if (methodInfoPointer == IntPtr.Zero)
{
UnityEngine.Debug.LogWarning(string.Format("cannot get method info for {0}:{1}, signature:{2}", type, method, TypeUtils.GetMethodSignature(method, false, isExtensionMethod)));
return;
}
if (methodPointer == IntPtr.Zero)
{
UnityEngine.Debug.LogWarning(string.Format("cannot get method pointer for {0}:{1}, signature:{2}", type, method, TypeUtils.GetMethodSignature(method, false, isExtensionMethod)));
return;
}
var wrapData = NativeAPI.AddMethod(
typeInfo,
signature,
wrapper,
name,
!isExtensionMethod && method.IsStatic,
isExtensionMethod,
isGetter,
isSetter,
methodInfoPointer,
methodPointer,
usedTypes.Count
);
if (wrapData == IntPtr.Zero)
{
if (throwIfMemberFail)
{
UnityEngine.Debug.LogWarning(string.Format("cannot get method info for {0}:{1}, signature:{2}", type, method, TypeUtils.GetMethodSignature(method, false, isExtensionMethod)));
return;
throw new Exception(string.Format("add method for {0}:{1} fail, signature:{2}", type, method, TypeUtils.GetMethodSignature(method, false, isExtensionMethod)));
}
if (methodPointer == IntPtr.Zero)
else
{
UnityEngine.Debug.LogWarning(string.Format("cannot get method pointer for {0}:{1}, signature:{2}", type, method, TypeUtils.GetMethodSignature(method, false, isExtensionMethod)));
#if WARNING_IF_MEMBERFAIL
UnityEngine.Debug.LogWarning(string.Format("add method for {0}:{1} fail, signature:{2}", type, method, TypeUtils.GetMethodSignature(method, false, isExtensionMethod)));
#endif
return;
}
var wrapData = NativeAPI.AddMethod(
typeInfo,
signature,
wrapper,
name,
!isExtensionMethod && method.IsStatic,
isExtensionMethod,
isGetter,
isSetter,
methodInfoPointer,
methodPointer,
usedTypes.Count
);
if (wrapData == IntPtr.Zero)
{
if (throwIfMemberFail)
{
throw new Exception(string.Format("add method for {0}:{1} fail, signature:{2}", type, method, TypeUtils.GetMethodSignature(method, false, isExtensionMethod)));
}
else
{
#if WARNING_IF_MEMBERFAIL
UnityEngine.Debug.LogWarning(string.Format("add method for {0}:{1} fail, signature:{2}", type, method, TypeUtils.GetMethodSignature(method, false, isExtensionMethod)));
#endif
return;
}
}
for (int i = 0; i < usedTypes.Count; ++i)
{
var usedTypeId = NativeAPI.GetTypeId(usedTypes[i]);
//UnityEngine.Debug.Log(string.Format("set used type for method {0}: {1}={2}, typeId:{3}", method, i, usedTypes[i], usedTypeId));
NativeAPI.SetTypeInfo(wrapData, i, usedTypeId);
}
};
}
for (int i = 0; i < usedTypes.Count; ++i)
{
var usedTypeId = NativeAPI.GetTypeId(usedTypes[i]);
//UnityEngine.Debug.Log(string.Format("set used type for method {0}: {1}={2}, typeId:{3}", method, i, usedTypes[i], usedTypeId));
NativeAPI.SetTypeInfo(wrapData, i, usedTypeId);
}
};

if (methods != null && (!type.IsArray || type == typeof(System.Array)))
if (methods != null && (!type.IsArray || type == typeof(System.Array)))
{
foreach (var method in methods)
{
foreach (var method in methods)
{
if (method.IsAbstract) continue;
AddMethodToType(method.Name, method as MethodInfo, false, false, false);
}
if (method.IsAbstract) continue;
AddMethodToType(method.Name, method as MethodInfo, false, false, false);
}

}

if (!isDelegate)
{
var extensionMethods = ExtensionMethodInfo.Get(type);
if (extensionMethods != null)
{
Expand Down
7 changes: 4 additions & 3 deletions unity/native_src/Src/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
Template->InstanceTemplate()->SetInternalFieldCount(4);

JSPropertyInfo* PropertyInfo = ClassDefinition->Properties;
while (PropertyInfo && PropertyInfo->Name && PropertyInfo->Getter)
while (PropertyInfo && PropertyInfo->Name)
{
v8::PropertyAttribute PropertyAttribute = v8::DontDelete;
if (!PropertyInfo->Setter)
Expand All @@ -224,7 +224,7 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
}

PropertyInfo = ClassDefinition->Variables;
while (PropertyInfo && PropertyInfo->Name && PropertyInfo->Getter)
while (PropertyInfo && PropertyInfo->Name)
{
v8::PropertyAttribute PropertyAttribute = v8::DontDelete;
if (!PropertyInfo->Setter)
Expand Down Expand Up @@ -340,7 +340,8 @@ void FCppObjectMapper::BindCppObject(
FObjectCacheNode* CacheNodePtr;
if (Iter != CDataCache.end())
{
CacheNodePtr = Iter->second.Add(ClassDefinition->TypeId);
auto Temp = Iter->second.Find(ClassDefinition->TypeId);
CacheNodePtr = Temp ? Temp : Iter->second.Add(ClassDefinition->TypeId);
}
else
{
Expand Down
40 changes: 40 additions & 0 deletions unity/test/Src/Cases/CrossLang/DelegateTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace Puerts.UnitTest
{
public delegate void TestCallback(string msg);
class DelegateTestClass
{
public TestCallback Callback;

public void CSMessage()
{
Callback("cs_msg");
}
};

[TestFixture]
public class DelegateTest
{
[Test]
public void DelegateBase()
{
var jsEnv = UnitTestEnv.GetEnv();

jsEnv.Eval(@"
globalThis.deleteobj = new CS.Puerts.UnitTest.DelegateTestClass()
deleteobj.Callback = (msg) => globalThis.info = msg;
deleteobj.CSMessage();
");

string info = jsEnv.Eval<string>("globalThis.info");
Assert.AreEqual("cs_msg", info);

jsEnv.Eval(@"deleteobj.Callback.Invoke('js_msg')");
info = jsEnv.Eval<string>("globalThis.info");
Assert.AreEqual("js_msg", info);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
<PropertyGroup>


<EngineVersion >$([System.Text.RegularExpressions.Regex]::Match($(EngineDir), "\d+.\d+"))</EngineVersion >
<TargetFramework Condition="$([MSBuild]::VersionGreaterThanOrEquals($(EngineVersion), '5.5'))">net8.0</TargetFramework>
<TargetFramework Condition="$([MSBuild]::VersionLessThan($(EngineVersion), '5.5'))">net6.0</TargetFramework>
<EngineVersionText >$([System.IO.File]::ReadAllText("$(EngineDir)\Build\Build.version"))</EngineVersionText >
<EngineVersionMajorText >$([System.Text.RegularExpressions.Regex]::Match($(EngineVersionText), "MajorVersion.*\d+"))</EngineVersionMajorText >
<EngineVersionMajor >$([System.Text.RegularExpressions.Regex]::Match($(EngineVersionMajorText), "\d+"))</EngineVersionMajor >
<EngineVersionMinorText >$([System.Text.RegularExpressions.Regex]::Match($(EngineVersionText), "MinorVersion.*\d+"))</EngineVersionMinorText >
<EngineVersionMinor >$([System.Text.RegularExpressions.Regex]::Match($(EngineVersionMinorText), "\d+"))</EngineVersionMinor >
<TargetFramework Condition="$([MSBuild]::VersionGreaterThanOrEquals($(EngineVersionMajor).$(EngineVersionMinor), '5.5'))">net8.0</TargetFramework>
<TargetFramework Condition="$([MSBuild]::VersionLessThan($(EngineVersionMajor).$(EngineVersionMinor), '5.5'))">net6.0</TargetFramework>

<Configuration Condition=" '$(Configuration)' == '' ">Development</Configuration>
<OutputType>Library</OutputType>
Expand Down
7 changes: 4 additions & 3 deletions unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
Template->InstanceTemplate()->SetInternalFieldCount(4);

JSPropertyInfo* PropertyInfo = ClassDefinition->Properties;
while (PropertyInfo && PropertyInfo->Name && PropertyInfo->Getter)
while (PropertyInfo && PropertyInfo->Name)
{
v8::PropertyAttribute PropertyAttribute = v8::DontDelete;
if (!PropertyInfo->Setter)
Expand All @@ -187,7 +187,7 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
}

PropertyInfo = ClassDefinition->Variables;
while (PropertyInfo && PropertyInfo->Name && PropertyInfo->Getter)
while (PropertyInfo && PropertyInfo->Name)
{
v8::PropertyAttribute PropertyAttribute = v8::DontDelete;
if (!PropertyInfo->Setter)
Expand Down Expand Up @@ -304,7 +304,8 @@ void FCppObjectMapper::BindCppObject(
FObjectCacheNode* CacheNodePtr;
if (Iter != CDataCache.end())
{
CacheNodePtr = Iter->second.Add(ClassDefinition->TypeId);
auto Temp = Iter->second.Find(ClassDefinition->TypeId);
CacheNodePtr = Temp ? Temp : Iter->second.Add(ClassDefinition->TypeId);
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion unreal/Puerts/Source/JsEnv/Private/JsEnvImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2915,7 +2915,8 @@ void FJsEnvImpl::BindStruct(
auto CacheNodePtr = StructCache.Find(Ptr);
if (CacheNodePtr)
{
CacheNodePtr = CacheNodePtr->Add(ScriptStructWrapper->Struct.Get());
auto Temp = CacheNodePtr->Find(ScriptStructWrapper->Struct.Get());
CacheNodePtr = Temp ? Temp : CacheNodePtr->Add(ScriptStructWrapper->Struct.Get());
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ bool V8InspectorClientImpl::Tick(float /* DeltaTime */)
v8::Context::Scope ContextScope(LocalContext);
v8::TryCatch TryCatch(Isolate);

MicroTasksRunner.Get(Isolate)->Call(LocalContext, LocalContext->Global(), 0, nullptr);
(void) (MicroTasksRunner.Get(Isolate)->Call(LocalContext, LocalContext->Global(), 0, nullptr));
}
}
}
Expand Down

0 comments on commit 8dfb06c

Please sign in to comment.