Skip to content

Commit

Permalink
[unity]update p-api
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Oct 23, 2024
1 parent 6d5a0b7 commit 733c86a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1918,12 +1918,12 @@ struct JsEnvPrivate
}
};

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

static void OnCsObjectExit(void* ptr, const void* type_id, JsEnvPrivate* jsEnvPrivate, void* userdata)
static void OnCsObjectExit(void* ptr, void* class_data, JsEnvPrivate* jsEnvPrivate, void* userdata)
{
intptr_t idx = reinterpret_cast<intptr_t>(userdata);
jsEnvPrivate->UnRefCSObject(idx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ typedef struct pesapi_property_descriptor__* pesapi_property_descriptor;

typedef void (*pesapi_callback)(pesapi_callback_info info);
typedef void* (*pesapi_constructor)(pesapi_callback_info info);
typedef void (*pesapi_finalize)(void* ptr, const void* type_id, void* env_private);
typedef void* (*pesapi_on_native_object_enter)(void* ptr, const void* type_id, void* env_private);
typedef void (*pesapi_finalize)(void* ptr, void* class_data, void* env_private);
typedef void* (*pesapi_on_native_object_enter)(void* ptr, void* class_data, void* env_private);
// userdata: return of pesapi_on_native_object_enter
typedef void (*pesapi_on_native_object_exit)(void* ptr, const void* type_id, void* env_private, void* userdata);
typedef void (*pesapi_on_native_object_exit)(void* ptr, void* class_data, void* env_private, void* userdata);
typedef bool (*pesapi_class_not_found_callback)(const void* type_id);
typedef void (*pesapi_func_ptr)(void);

Expand Down
5 changes: 1 addition & 4 deletions unity/native_src_il2cpp/Inc/CppObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FCppObjectMapper final : public ICppObjectMapper
virtual v8::Local<v8::Value> FindOrAddCppObject(
v8::Isolate* Isolate, v8::Local<v8::Context>& Context, const void* TypeId, void* Ptr, bool PassByPointer) override;

virtual void UnBindCppObject(JSClassDefinition* ClassDefinition, void* Ptr) override;
virtual void UnBindCppObject(v8::Isolate* Isolate, JSClassDefinition* ClassDefinition, void* Ptr) override;

virtual void BindCppObject(v8::Isolate* Isolate, JSClassDefinition* ClassDefinition, void* Ptr, v8::Local<v8::Object> JSObject,
bool PassByPointer) override;
Expand All @@ -68,9 +68,6 @@ class FCppObjectMapper final : public ICppObjectMapper
v8::Local<v8::FunctionTemplate> GetTemplateOfClass(v8::Isolate* Isolate, const JSClassDefinition* ClassDefinition);

private:
// TODO: pass by UnBindCppObject parameter
v8::Isolate* Isolate;

std::unordered_map<void*, FObjectCacheNode, PointerHash, PointerEqual> CDataCache;

std::unordered_map<const void*, v8::UniquePersistent<v8::FunctionTemplate>, PointerHash, PointerEqual> CDataNameToTemplateMap;
Expand Down
6 changes: 3 additions & 3 deletions unity/native_src_il2cpp/Inc/JSClassRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ struct JSENV_API JSPropertyInfo
void* SetterData = nullptr;
};

typedef void (*FinalizeFunc)(void* Ptr, const void* TypeId, void* EnvData);
typedef void (*FinalizeFunc)(void* Ptr, void* ClassData, void* EnvData);

typedef void* (*InitializeFunc)(const v8::FunctionCallbackInfo<v8::Value>& Info);

typedef bool (*ClassNotFoundCallback)(const void* TypeId);

typedef void* (*OnObjectEnter)(void* Ptr, const void* TypeId, void* EnvData);
typedef void* (*OnObjectEnter)(void* Ptr, void* ClassData, void* EnvData);
// UserData: return of OnObjectEnter
typedef void (*OnObjectExit)(void* Ptr, const void* TypeId, void* EnvData, void* UserData);
typedef void (*OnObjectExit)(void* Ptr, void* ClassData, void* EnvData, void* UserData);

struct NamedFunctionInfo;
struct NamedPropertyInfo;
Expand Down
2 changes: 1 addition & 1 deletion unity/native_src_il2cpp/Inc/ObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ICppObjectMapper
virtual void BindCppObject(v8::Isolate* Isolate, JSClassDefinition* ClassDefinition, void* Ptr, v8::Local<v8::Object> JSObject,
bool PassByPointer) = 0;

virtual void UnBindCppObject(JSClassDefinition* ClassDefinition, void* Ptr) = 0;
virtual void UnBindCppObject(v8::Isolate* Isolate, JSClassDefinition* ClassDefinition, void* Ptr) = 0;

virtual v8::Local<v8::Value> FindOrAddCppObject(
v8::Isolate* Isolate, v8::Local<v8::Context>& Context, const void* TypeId, void* Ptr, bool PassByPointer) = 0;
Expand Down
6 changes: 3 additions & 3 deletions unity/native_src_il2cpp/Inc/pesapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ typedef struct pesapi_property_descriptor__* pesapi_property_descriptor;

typedef void (*pesapi_callback)(pesapi_callback_info info);
typedef void* (*pesapi_constructor)(pesapi_callback_info info);
typedef void (*pesapi_finalize)(void* ptr, const void* type_id, void* env_private);
typedef void* (*pesapi_on_native_object_enter)(void* ptr, const void* type_id, void* env_private);
typedef void (*pesapi_finalize)(void* ptr, void* class_data, void* env_private);
typedef void* (*pesapi_on_native_object_enter)(void* ptr, void* class_data, void* env_private);
// userdata: return of pesapi_on_native_object_enter
typedef void (*pesapi_on_native_object_exit)(void* ptr, const void* type_id, void* env_private, void* userdata);
typedef void (*pesapi_on_native_object_exit)(void* ptr, void* class_data, void* env_private, void* userdata);
typedef bool (*pesapi_class_not_found_callback)(const void* type_id);
typedef void (*pesapi_func_ptr)(void);

Expand Down
20 changes: 10 additions & 10 deletions unity/native_src_il2cpp/Src/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ static void PointerNew(const v8::FunctionCallbackInfo<v8::Value>& Info)

void FCppObjectMapper::Initialize(v8::Isolate* InIsolate, v8::Local<v8::Context> InContext)
{
Isolate = InIsolate;
auto LocalTemplate = v8::FunctionTemplate::New(InIsolate, PointerNew);
LocalTemplate->InstanceTemplate()->SetInternalFieldCount(4); // 0 Ptr, 1, CDataName
PointerConstructor = v8::UniquePersistent<v8::Function>(InIsolate, LocalTemplate->GetFunction(InContext).ToLocalChecked());
Expand Down Expand Up @@ -300,15 +299,15 @@ static void CDataGarbageCollectedWithFree(const v8::WeakCallbackInfo<JSClassDefi
JSClassDefinition* ClassDefinition = Data.GetParameter();
void* Ptr = DataTransfer::MakeAddressWithHighPartOfTwo(Data.GetInternalField(0), Data.GetInternalField(1));
if (ClassDefinition->Finalize)
ClassDefinition->Finalize(Ptr, ClassDefinition->TypeId, DataTransfer::GetIsolatePrivateData(Data.GetIsolate()));
DataTransfer::IsolateData<ICppObjectMapper>(Data.GetIsolate())->UnBindCppObject(ClassDefinition, Ptr);
ClassDefinition->Finalize(Ptr, ClassDefinition->Data, DataTransfer::GetIsolatePrivateData(Data.GetIsolate()));
DataTransfer::IsolateData<ICppObjectMapper>(Data.GetIsolate())->UnBindCppObject(Data.GetIsolate(), ClassDefinition, Ptr);
}

static void CDataGarbageCollectedWithoutFree(const v8::WeakCallbackInfo<JSClassDefinition>& Data)
{
JSClassDefinition* ClassDefinition = Data.GetParameter();
void* Ptr = DataTransfer::MakeAddressWithHighPartOfTwo(Data.GetInternalField(0), Data.GetInternalField(1));
DataTransfer::IsolateData<ICppObjectMapper>(Data.GetIsolate())->UnBindCppObject(ClassDefinition, Ptr);
DataTransfer::IsolateData<ICppObjectMapper>(Data.GetIsolate())->UnBindCppObject(Data.GetIsolate(), ClassDefinition, Ptr);
}

void FCppObjectMapper::BindCppObject(
Expand Down Expand Up @@ -344,8 +343,7 @@ void FCppObjectMapper::BindCppObject(

if (ClassDefinition->OnEnter)
{
CacheNodePtr->UserData =
ClassDefinition->OnEnter(Ptr, ClassDefinition->TypeId, DataTransfer::GetIsolatePrivateData(Isolate));
CacheNodePtr->UserData = ClassDefinition->OnEnter(Ptr, ClassDefinition->Data, DataTransfer::GetIsolatePrivateData(Isolate));
}
}

Expand Down Expand Up @@ -383,14 +381,15 @@ void FCppObjectMapper::SetPrivateData(v8::Local<v8::Context> Context, v8::Local<
(void) (JSObject->Set(Context, Key, v8::External::New(Context->GetIsolate(), Ptr)));
}

void FCppObjectMapper::UnBindCppObject(JSClassDefinition* ClassDefinition, void* Ptr)
void FCppObjectMapper::UnBindCppObject(v8::Isolate* Isolate, JSClassDefinition* ClassDefinition, void* Ptr)
{
auto Iter = CDataCache.find(Ptr);
if (Iter != CDataCache.end())
{
if (ClassDefinition->OnExit)
{
ClassDefinition->OnExit(Ptr, Iter->second.TypeId, DataTransfer::GetIsolatePrivateData(Isolate), Iter->second.UserData);
ClassDefinition->OnExit(
Ptr, ClassDefinition->Data, DataTransfer::GetIsolatePrivateData(Isolate), Iter->second.UserData);
}
auto Removed = Iter->second.Remove(ClassDefinition->TypeId, true);
if (!Iter->second.TypeId) // last one
Expand All @@ -413,13 +412,14 @@ void FCppObjectMapper::UnInitialize(v8::Isolate* InIsolate)
{
if (ClassDefinition && ClassDefinition->Finalize)
{
ClassDefinition->Finalize(KV.first, PNode->TypeId, PData);
ClassDefinition->Finalize(KV.first, ClassDefinition->Data, PData);
}
PNode->MustCallFinalize = false;
}
if (ClassDefinition->OnExit)
{
ClassDefinition->OnExit(KV.first, PNode->TypeId, DataTransfer::GetIsolatePrivateData(Isolate), PNode->UserData);
ClassDefinition->OnExit(
KV.first, ClassDefinition->Data, DataTransfer::GetIsolatePrivateData(InIsolate), PNode->UserData);
}
PNode = PNode->Next;
}
Expand Down

0 comments on commit 733c86a

Please sign in to comment.