Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pesapi_trace_native_object_lifecycle功能及其测试 #4

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions source/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,24 @@ void CppObjectMapper::BindAndAddToCache(const puerts::JSClassDefinition* typeInf
auto Ret = CDataCache.insert({ptr, FObjectCacheNode(typeInfo->TypeId)});
CacheNodePtr = &Ret.first->second;
}
CacheNodePtr->MustCallFinalize = callFinalize;
CacheNodePtr->Value = value;

if (typeInfo->OnEnter)
{
CacheNodePtr->UserData = typeInfo->OnEnter((void*)ptr, typeInfo->Data, (void*)GetEnvPrivate());
}
}

void CppObjectMapper::RemoveFromCache(const puerts::JSClassDefinition* typeInfo, const void* ptr)
{
auto Iter = CDataCache.find(ptr);
if (Iter != CDataCache.end())
{
if (typeInfo->OnExit)
{
typeInfo->OnExit( (void*)ptr, typeInfo->Data, (void*)GetEnvPrivate(), Iter->second.UserData);
}
auto Removed = Iter->second.Remove(typeInfo->TypeId, true);
if (!Iter->second.TypeId) // last one
{
Expand Down Expand Up @@ -369,6 +379,32 @@ void CppObjectMapper::Cleanup()
{
JS_FreeAtom(ctx, privateDataKey);

auto PData = GetEnvPrivate();
for (auto& KV : CDataCache)
{
FObjectCacheNode* PNode = &KV.second;
while (PNode)
{
const puerts::JSClassDefinition* ClassDefinition = puerts::FindClassByID(PNode->TypeId);
// quickjs是可以保证释放的,所以这里不需要释放
/*
if (PNode->MustCallFinalize)
{
if (ClassDefinition && ClassDefinition->Finalize)
{
ClassDefinition->Finalize(&g_pesapi_ffi, (void*)KV.first, ClassDefinition->Data, (void*)PData);
}
PNode->MustCallFinalize = false;
}
*/
if (ClassDefinition->OnExit)
{
ClassDefinition->OnExit((void*)KV.first, ClassDefinition->Data, (void*)PData, PNode->UserData);
}
PNode = PNode->Next;
}
}

for(auto& kv : TypeIdToFunctionMap)
{
JS_FreeValue(ctx, kv.second);
Expand Down
81 changes: 80 additions & 1 deletion test/papi_qjs_base_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,33 @@ class PApiBaseTest : public ::testing::Test {
pesapi_set_property_info(properties, 3, "ctor_count", true, CtorCountGetterWrap, CtorCountSetterWrap, NULL, NULL, NULL);
pesapi_set_method_info(properties, 4, "GetSelf", false, GetSelfWrap, NULL, NULL);
pesapi_set_method_info(properties, 5, "Inc", false, IncWrap, NULL, NULL);
pesapi_define_class(typeId, baseTypeId, "TestStruct", TestStructCtor, TestStructFinalize, properties_count, properties, NULL);
pesapi_define_class(typeId, baseTypeId, "TestStruct", TestStructCtor, TestStructFinalize, properties_count, properties, (void*)typeId);

pesapi_trace_native_object_lifecycle(baseTypeId, OnObjEnter, OnObjExit);
pesapi_trace_native_object_lifecycle(typeId, OnObjEnter, OnObjExit);
}

static void* BindData;
static void* ObjPtr;
static void* ClassData;
static void* EnvPrivate;

static void* OnObjEnter(void* ptr, void* class_data, void* env_private)
{
//printf("OnObjEnter:%p, %p, %p\n", ptr, class_data, env_private);
ObjPtr = ptr;
ClassData = class_data;
EnvPrivate = env_private;
return BindData;
}

static void OnObjExit(void* ptr, void* class_data, void* env_private, void* userdata)
{
//printf("OnObjExit:%p, %p, %p, %p\n", ptr, class_data, env_private, userdata);
BindData = userdata;
ObjPtr = ptr;
ClassData = class_data;
EnvPrivate = env_private;
}

static void TearDownTestCase() {
Expand Down Expand Up @@ -622,6 +648,59 @@ TEST_F(PApiBaseTest, SuperAccess) {
EXPECT_STREQ("122:11", str);
}

void* PApiBaseTest::BindData = nullptr;
void* PApiBaseTest::ObjPtr = nullptr;
void* PApiBaseTest::ClassData = nullptr;
void* PApiBaseTest::EnvPrivate = nullptr;

TEST_F(PApiBaseTest, LifecycleTrace) {
auto scopeInner = apis->open_scope(env_ref);
auto env = apis->get_env_from_ref(env_ref);

ObjPtr = nullptr;
ClassData = nullptr;
EnvPrivate = nullptr;

int p;
apis->set_env_private(env, &p);
int p2;
BindData = &p2;

auto code = R"(
const TestStruct = loadClass('TestStruct');
obj = new TestStruct(123);
)";

apis->eval(env, (const uint8_t*)(code), strlen(code), "test.js");
ASSERT_FALSE(apis->has_caught(scopeInner));
EXPECT_EQ(&p, EnvPrivate);
EXPECT_EQ((void*)typeId, ClassData);
EXPECT_NE(nullptr, ObjPtr);

void* OrgObjPtr = ObjPtr;

ObjPtr = nullptr;
ClassData = nullptr;
EnvPrivate = nullptr;
BindData = nullptr;

code = R"(
obj = undefined;
)";

apis->eval(env, (const uint8_t*)(code), strlen(code), "test.js");

ASSERT_FALSE(apis->has_caught(scopeInner));

apis->close_scope(scopeInner); //还存放引用在scope里,通过close_scope释放

EXPECT_EQ(&p, EnvPrivate);
EXPECT_EQ((void*)typeId, ClassData);
EXPECT_EQ(OrgObjPtr, ObjPtr);
EXPECT_EQ(&p2, BindData);

}

} // namespace qjsimpl
} // namespace pesapi

Expand Down
Loading