From 7ffb2f42194501a0644430a7aa547abef146bcb9 Mon Sep 17 00:00:00 2001 From: unknown <348063288@qq.com> Date: Sat, 1 Jul 2023 11:41:11 +0800 Subject: [PATCH] [unity] fix blittablecopy bug --- .../Resources/puerts/templates/registerinfo.tpl.mjs | 4 +--- .../Assets/core/upm/Editor/Src/Generator/FileExporter.cs | 9 ++++++++- .../Assets/core/upm/Editor/Src/Generator/RegisterInfo.cs | 4 +++- unity/native_src/Src/JSEngine.cpp | 4 ++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/unity/Assets/core/upm/Editor/Resources/puerts/templates/registerinfo.tpl.mjs b/unity/Assets/core/upm/Editor/Resources/puerts/templates/registerinfo.tpl.mjs index 9bdcdb08b7..1d54d5becc 100644 --- a/unity/Assets/core/upm/Editor/Resources/puerts/templates/registerinfo.tpl.mjs +++ b/unity/Assets/core/upm/Editor/Resources/puerts/templates/registerinfo.tpl.mjs @@ -25,9 +25,6 @@ namespace PuertsStaticWrap { return new RegisterInfo { -#if !EXPERIMENTAL_IL2CPP_PUERTS || !ENABLE_IL2CPP - BlittableCopy = ${item.BlittableCopy}, -#endif Members = new System.Collections.Generic.Dictionary { ${FOR(listToJsArray(item.Members), member=> ` @@ -46,6 +43,7 @@ namespace PuertsStaticWrap { ${FOR(typeRegisterInfos, item => ` jsEnv.AddRegisterInfoGetter(typeof(${CS.Puerts.TypeExtensions.GetFriendlyName(item.Type)}), GetRegisterInfo_${item.WrapperName}); + ${item.BlittableCopy ? item.WrapperName + ".InitBlittableCopy(jsEnv);": ""} `)} } } diff --git a/unity/Assets/core/upm/Editor/Src/Generator/FileExporter.cs b/unity/Assets/core/upm/Editor/Src/Generator/FileExporter.cs index 5975643f5c..91fcc1a745 100644 --- a/unity/Assets/core/upm/Editor/Src/Generator/FileExporter.cs +++ b/unity/Assets/core/upm/Editor/Src/Generator/FileExporter.cs @@ -155,6 +155,7 @@ public static void GenRegisterInfo(string outDir, ILoader loader = null) { var configure = Puerts.Configure.GetConfigureByTags(new List() { "Puerts.BindingAttribute", + "Puerts.BlittableCopyAttribute", }); var genTypes = configure["Puerts.BindingAttribute"].Select(kv => kv.Key) .Where(o => o is Type) @@ -163,12 +164,18 @@ public static void GenRegisterInfo(string outDir, ILoader loader = null) .Distinct() .ToList(); + var blittableCopyTypes = new HashSet(configure["Puerts.BlittableCopyAttribute"].Select(kv => kv.Key) + .Where(o => o is Type) + .Cast() + .Where(t => !t.IsPrimitive && Utils.isBlittableType(t)) + .Distinct()); + if (!Utils.HasFilter) { Utils.SetFilters(Configure.GetFilters()); } - var RegisterInfos = RegisterInfoGenerator.GetRegisterInfos(genTypes); + var RegisterInfos = RegisterInfoGenerator.GetRegisterInfos(genTypes, blittableCopyTypes); if (loader == null) { diff --git a/unity/Assets/core/upm/Editor/Src/Generator/RegisterInfo.cs b/unity/Assets/core/upm/Editor/Src/Generator/RegisterInfo.cs index 4fe4c2a7be..f5ddd8c07f 100644 --- a/unity/Assets/core/upm/Editor/Src/Generator/RegisterInfo.cs +++ b/unity/Assets/core/upm/Editor/Src/Generator/RegisterInfo.cs @@ -67,7 +67,7 @@ internal List GetAllMember() } } - public static List GetRegisterInfos(List genTypes) + public static List GetRegisterInfos(List genTypes, HashSet blittableCopyTypes) { BindingFlags flag = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public; @@ -255,6 +255,8 @@ public static List GetRegisterInfos(List genTypes { WrapperName = Utils.GetWrapTypeName(type), + BlittableCopy = blittableCopyTypes.Contains(type), + Type = type, Members = Collector.GetAllMember(), diff --git a/unity/native_src/Src/JSEngine.cpp b/unity/native_src/Src/JSEngine.cpp index 97cdbd5b45..a91501c1ad 100644 --- a/unity/native_src/Src/JSEngine.cpp +++ b/unity/native_src/Src/JSEngine.cpp @@ -446,8 +446,7 @@ namespace puerts { if (LifeCycleInfo->Constructor) Ptr = LifeCycleInfo->Constructor(Isolate, Info, Info.Length(), LifeCycleInfo->Data); } - if (Ptr != nullptr) - FV8Utils::IsolateData(Isolate)->BindObject(LifeCycleInfo, Ptr, Self); + FV8Utils::IsolateData(Isolate)->BindObject(LifeCycleInfo, Ptr, Self); } else { @@ -622,6 +621,7 @@ namespace puerts { JSObject->SetAlignedPointerInInternalField(0, Ptr); } + if (Ptr == nullptr) return; JSObject->SetAlignedPointerInInternalField(1, LifeCycleInfo); JSObject->SetAlignedPointerInInternalField(2, reinterpret_cast(OBJECT_MAGIC));