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 Jan 12, 2025
2 parents c8a743d + a812369 commit 670c6da
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1957,11 +1957,10 @@ struct JsEnvPrivate
pesapi_env_ref envRef;
std::mutex pendingKillRefsMutex;
std::unordered_set<pesapi_value_ref> pendingKillRefs;
MethodInfoHelper<int32_t(Il2CppObject* obj)> objPoolAdd;
MethodInfoHelper<Il2CppObject*(int32_t index)> objPoolRemove;
std::unordered_set<Il2CppObject*, std::hash<void*>, std::equal_to<void>, il2cpp::gc::Allocator<Il2CppObject*>> objectPool;

JsEnvPrivate(struct pesapi_ffi* inApis, pesapi_env_ref inEnvRef, Il2CppObject *objPool, Il2CppReflectionMethod* objPoolAddMethodInfo, Il2CppReflectionMethod* objPoolRemoveMethodInfo)
: apis(inApis), envRef(inEnvRef), objPoolAdd(objPoolAddMethodInfo, objPool), objPoolRemove(objPoolRemoveMethodInfo, objPool)
JsEnvPrivate(struct pesapi_ffi* inApis, pesapi_env_ref inEnvRef)
: apis(inApis), envRef(inEnvRef)

{
}
Expand Down Expand Up @@ -2014,26 +2013,26 @@ struct JsEnvPrivate
pendingKillRefs.clear();
}

int32_t RefCSObject(Il2CppObject* obj)
Il2CppObject* RefCSObject(Il2CppObject* obj)
{
return objPoolAdd.CallWithInstance(obj);
objectPool.insert(obj);
return obj;
}

void UnRefCSObject(int32_t idx)
void UnRefCSObject(Il2CppObject* obj)
{
objPoolRemove.CallWithInstance(idx);
objectPool.erase(obj);
}
};

static void* OnCsObjectEnter(Il2CppObject* obj, void* class_data, JsEnvPrivate* jsEnvPrivate)
{
return reinterpret_cast<void*>(jsEnvPrivate->RefCSObject(obj));
return jsEnvPrivate->RefCSObject(obj);
}

static void OnCsObjectExit(void* ptr, void* class_data, JsEnvPrivate* jsEnvPrivate, void* userdata)
{
intptr_t idx = reinterpret_cast<intptr_t>(userdata);
jsEnvPrivate->UnRefCSObject(idx);
jsEnvPrivate->UnRefCSObject(static_cast<Il2CppObject*>(userdata));
}

static void FreeCSharpMethodInfo(CSharpMethodInfo* csharpMethodInfo)
Expand Down Expand Up @@ -2135,11 +2134,11 @@ static void LoadTypeWrapper(struct pesapi_ffi* apis, pesapi_callback_info info)
apis->add_return(info, ret);
}

puerts::JsEnvPrivate* InitialPapiEnvRef(struct pesapi_ffi* apis, pesapi_env_ref envRef, Il2CppObject *objPool, Il2CppReflectionMethod* objPoolAddMethodInfo, Il2CppReflectionMethod* objPoolRemoveMethodInfo)
puerts::JsEnvPrivate* InitialPapiEnvRef(struct pesapi_ffi* apis, pesapi_env_ref envRef)
{
puerts::AutoValueScope ValueScope(apis, envRef);
auto env = apis->get_env_from_ref(envRef);
auto jsEnvPrivate = new puerts::JsEnvPrivate(apis, envRef, objPool, objPoolAddMethodInfo, objPoolRemoveMethodInfo);
auto jsEnvPrivate = new puerts::JsEnvPrivate(apis, envRef);
apis->set_env_private(env, jsEnvPrivate);
auto loadType = apis->create_function(env, LoadTypeWrapper, nullptr, nullptr);
auto createFunction = apis->create_function(env, createFunctionWrapper, nullptr, nullptr);
Expand Down Expand Up @@ -2196,7 +2195,7 @@ void InitialPuerts(pesapi_func_ptr* func_array)
InternalCalls::Add("Puerts.NativeAPI::SetObjectToGlobal(System.IntPtr,System.IntPtr,System.String,System.Object)", (Il2CppMethodPointer)puerts::SetObjectToGlobal);
InternalCalls::Add("Puerts.NativeAPI::TypeIdToType(System.IntPtr)", (Il2CppMethodPointer)puerts::TypeIdToType);
InternalCalls::Add("Puerts.NativeAPI::GetModuleExecutor(System.IntPtr,System.IntPtr,System.Type)", (Il2CppMethodPointer)puerts::GetModuleExecutor);
InternalCalls::Add("Puerts.NativeAPI::InitialPapiEnvRef(System.IntPtr,System.IntPtr,System.Object,System.Reflection.MethodBase,System.Reflection.MethodBase)", (Il2CppMethodPointer)puerts::InitialPapiEnvRef);
InternalCalls::Add("Puerts.NativeAPI::InitialPapiEnvRef(System.IntPtr,System.IntPtr)", (Il2CppMethodPointer)puerts::InitialPapiEnvRef);
InternalCalls::Add("Puerts.NativeAPI::CleanupPapiEnvRef(System.IntPtr,System.IntPtr)", (Il2CppMethodPointer)puerts::CleanupPapiEnvRef);
InternalCalls::Add("Puerts.NativeAPI::DestroyJSEnvPrivate(System.IntPtr)", (Il2CppMethodPointer)puerts::DestroyJSEnvPrivate);
InternalCalls::Add("Puerts.JSObject::GetJSObjectValue(System.IntPtr,System.String,System.Type)", (Il2CppMethodPointer)puerts::GetJSObjectValue);
Expand Down
5 changes: 1 addition & 4 deletions unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public class JsEnv : IDisposable
MethodInfo objectPoolRemoveMethodInfo;
MethodInfo tryLoadTypeMethodInfo;

PuertsIl2cpp.ObjectPool objectPool = new PuertsIl2cpp.ObjectPool();

private Func<string, JSObject> moduleExecutor;

ILoader loader;
Expand Down Expand Up @@ -78,8 +76,7 @@ public JsEnv(ILoader loader, int debugPort = -1)

nativeJsEnv = Puerts.PuertsDLL.CreateJSEngine(0);
nativePesapiEnv = Puerts.NativeAPI.GetPapiEnvRef(nativeJsEnv);
var objectPoolType = typeof(PuertsIl2cpp.ObjectPool);
nativeScriptObjectsRefsMgr = Puerts.NativeAPI.InitialPapiEnvRef(apis, nativePesapiEnv, objectPool, objectPoolType.GetMethod("Add"), objectPoolType.GetMethod("Remove"));
nativeScriptObjectsRefsMgr = Puerts.NativeAPI.InitialPapiEnvRef(apis, nativePesapiEnv);

Puerts.NativeAPI.SetObjectToGlobal(apis, nativePesapiEnv, "jsEnv", this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class NativeAPI
public static extern void CleanupPendingKillScriptObjects(IntPtr jsEnv);

[MethodImpl(MethodImplOptions.InternalCall)]
public static IntPtr InitialPapiEnvRef(IntPtr api, IntPtr envRef, Object obj, MethodBase addMethodBase, MethodBase removeMethodBase)
public static IntPtr InitialPapiEnvRef(IntPtr api, IntPtr envRef)
{
throw new NotImplementedException();
}
Expand Down
1 change: 1 addition & 0 deletions unity/cli/cmd.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ program
.option('-G, --generator <generator-name>', 'cmake generator name')
.option('-ts, --thread_safe', 'thread safe')
.option('-wi, --with_inspector', 'with inspector')
.option('-jl, --jitless', 'jitless')
.action(function (quickcommand, options) {
let backend = options.backend;
let config = options.config;
Expand Down
4 changes: 4 additions & 0 deletions unity/cli/make.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ async function runPuertsMake(cwd, options) {
console.log('################################## thread_safe ##################################');
BackendConfig.definition.push("THREAD_SAFE");
}
if (options.jitless) {
console.log('################################## jitless ##################################');
BackendConfig.definition.push("JITLESS");
}
const definitionD = (BackendConfig.definition || []).join(';');
const linkD = (BackendConfig['link-libraries'][options.platform]?.[options.arch] || []).join(';');
const incD = (BackendConfig.include || []).join(';');
Expand Down
5 changes: 1 addition & 4 deletions unity/native_src/Src/BackendEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,8 @@ void FBackendEnv::GlobalPrepare()
std::string Flags = "--stack_size=856";
#if PUERTS_DEBUG
Flags += " --expose-gc";
#if PLATFORM_MAC
Flags += " --jitless --no-expose-wasm";
#endif
#endif
#if defined(PLATFORM_IOS) || defined(PLATFORM_OHOS)
#if defined(PLATFORM_IOS) || defined(PLATFORM_OHOS) || defined(JITLESS)
Flags += " --jitless --no-expose-wasm";
#endif
#if V8_MAJOR_VERSION <= 9
Expand Down
64 changes: 64 additions & 0 deletions unity/test/Src/Cases/CrossLang/CrossLangTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@

namespace Puerts.UnitTest
{
[UnityEngine.Scripting.Preserve]
public class TestGC
{
public static int ObjCount = 0;

[UnityEngine.Scripting.Preserve]
public TestGC()
{
++ObjCount;
}

[UnityEngine.Scripting.Preserve]

~TestGC()
{
--ObjCount;
}
}

public class TestObject
{
Expand Down Expand Up @@ -1040,5 +1058,51 @@ public void CallDelegateAfterJsEnvDisposed()
callback();
});
}

[Test]
public void TestJsGC()
{
#if PUERTS_GENERAL
var jsEnv = new JsEnv(new TxtLoader());
#else
var jsEnv = new JsEnv(new DefaultLoader());
#endif
var objCount = jsEnv.Eval<int>(@"
const randomCount = Math.floor(Math.random() * 50) + 1;
var objs = []
for (let i = 0; i < randomCount; i++) {
objs.push(new CS.Puerts.UnitTest.TestGC())
}
randomCount;
");

if (jsEnv.Backend is BackendV8)
{
jsEnv.Eval("gc()");
}

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Assert.AreEqual(objCount, TestGC.ObjCount);
Assert.True(objCount > 0);

jsEnv.Eval("objs = undefined");

if (jsEnv.Backend is BackendV8)
{
jsEnv.Eval("gc()");
}

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Assert.AreEqual(0, TestGC.ObjCount);

jsEnv.Dispose();
}
}
}
13 changes: 12 additions & 1 deletion unreal/Puerts/Content/JavaScript/puerts/uelazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,25 @@ var global = global || (function () { return this; }());

blueprint.unmixin = unmixin;

const bpns = new Set(['Game']);

function blueprint_createNamespace(ns) {
if (!bpns.has(ns)) {
rawSet(UE, ns, createNamespaceOrClass(ns, undefined, TNAMESPACE));
bpns.add(ns);
}
}

function blueprint_load(cls) {
if (cls.__path) {
let c = cls
let path = `.${c.__path}`
let path = `.${c.__path}`;
c = c.__parent;
while (c && c.__path) {
path = `/${c.__path}${path}`
c = c.__parent;
}

let ufield = UE.Field.Load(path, true);
if (!ufield) {
throw new Error(`load ${path} fail!`);
Expand All @@ -252,6 +262,7 @@ var global = global || (function () { return this; }());
}

blueprint.load = blueprint_load;
blueprint.namespace = blueprint_createNamespace;

function blueprint_unload(cls) {
if (cls.__puerts_ufield) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ class FDeclarationGenerator : public IDeclarationGenerator

FName PackagePath = (InSearchPath == NAME_None) ? FName(TEXT("/Game")) : InSearchPath;

FString DialogMessage = FString::Printf(TEXT("genertate finish, %s store in %s, ([PATH=%s])"), TEXT("ue.d.ts"),
FString DialogMessage = FString::Printf(TEXT("generate finish, %s store in %s, ([PATH=%s])"), TEXT("ue.d.ts"),
TEXT("Content/Typing/ue"), *PackagePath.ToString());

FText DialogText = FText::Format(LOCTEXT("PluginButtonDialogText", "{0}"), FText::FromString(DialogMessage));
Expand Down
1 change: 1 addition & 0 deletions unreal/Puerts/Typing/puerts/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ declare module "puerts" {
function unmixin<T extends typeof Object>(to:T): void
function load(cls: any): void
function unload(cls: any): void
function namespace(name: string);
}

function on(eventType: string, listener: Function, prepend?: boolean) : void;
Expand Down

0 comments on commit 670c6da

Please sign in to comment.