Skip to content

Commit

Permalink
[unity]创建function时,函数信息数据的回收
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Nov 5, 2024
1 parent 2417f6b commit 46282ac
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
10 changes: 10 additions & 0 deletions unity/native_src_il2cpp/Inc/CppObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ struct PointerEqual
return lhs == rhs;
}
};

struct PesapiCallbackData
{
pesapi_callback Callback;
void* Data;
};

class FCppObjectMapper final : public ICppObjectMapper
{
public:
Expand All @@ -56,6 +63,8 @@ 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 v8::MaybeLocal<v8::Function> CreateFunction(v8::Local<v8::Context> Context, pesapi_callback Callback, void* Data) override;

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

Expand All @@ -79,6 +88,7 @@ class FCppObjectMapper final : public ICppObjectMapper

v8::UniquePersistent<v8::FunctionTemplate> PointerTemplate;

std::vector<PesapiCallbackData*> FunctionDatas;
#ifndef WITH_QUICKJS
v8::Global<v8::Symbol> PrivateKey;
#endif
Expand Down
3 changes: 3 additions & 0 deletions unity/native_src_il2cpp/Inc/ObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class ICppObjectMapper

virtual v8::Local<v8::Value> FindOrAddCppObject(
v8::Isolate* Isolate, v8::Local<v8::Context>& Context, const void* TypeId, void* Ptr, bool PassByPointer) = 0;


virtual v8::MaybeLocal<v8::Function> CreateFunction(v8::Local<v8::Context> Context, pesapi_callback Callback, void* Data) = 0;

virtual bool IsInstanceOfCppObject(v8::Isolate* Isolate, const void* TypeId, v8::Local<v8::Object> JsObject) = 0;

Expand Down
20 changes: 20 additions & 0 deletions unity/native_src_il2cpp/Src/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ v8::Local<v8::Value> FCppObjectMapper::FindOrAddCppObject(
}
}

static void PesapiFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
{
PesapiCallbackData* FunctionInfo = reinterpret_cast<PesapiCallbackData*>(
reinterpret_cast<char*>(v8::Local<v8::External>::Cast(info.Data())->Value()) - offsetof(PesapiCallbackData, Data));
FunctionInfo->Callback(&v8impl::g_pesapi_ffi, (pesapi_callback_info)(&info));
}

v8::MaybeLocal<v8::Function> FCppObjectMapper::CreateFunction(v8::Local<v8::Context> Context, pesapi_callback Callback, void* Data)
{
auto CallbackData = new PesapiCallbackData {Callback, Data};
FunctionDatas.push_back(CallbackData);
auto V8Data = v8::External::New(Context->GetIsolate(), &CallbackData->Data);
return v8::FunctionTemplate::New(Context->GetIsolate(), PesapiFunctionCallback, V8Data)->GetFunction(Context);
}

bool FCppObjectMapper::IsInstanceOfCppObject(v8::Isolate* Isolate, const void* TypeId, v8::Local<v8::Object> JsObject)
{
if (DataTransfer::GetPointerFast<const void>(JsObject, 1) == TypeId)
Expand Down Expand Up @@ -432,6 +447,11 @@ void FCppObjectMapper::UnInitialize(v8::Isolate* InIsolate)
PNode = PNode->Next;
}
}
for(int i = 0;i < FunctionDatas.size(); ++i)
{
delete FunctionDatas[i];
}
FunctionDatas.clear();
CDataCache.clear();
TypeIdToTemplateMap.clear();
#ifndef WITH_QUICKJS
Expand Down
19 changes: 1 addition & 18 deletions unity/native_src_il2cpp/Src/PesapiV8Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,27 +162,10 @@ pesapi_value pesapi_create_object(pesapi_env env)
return v8impl::PesapiValueFromV8LocalValue(v8::Object::New(context->GetIsolate()));
}

struct PesapiCallbackData
{
pesapi_callback callback;
void* data;
};

extern pesapi_ffi g_pesapi_ffi;

static void PesapiFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
{
PesapiCallbackData* FunctionInfo = reinterpret_cast<PesapiCallbackData*>(
reinterpret_cast<char*>(v8::Local<v8::External>::Cast(info.Data())->Value()) - offsetof(PesapiCallbackData, data));
FunctionInfo->callback(&g_pesapi_ffi, (pesapi_callback_info)(&info));
}

pesapi_value pesapi_create_function(pesapi_env env, pesapi_callback native_impl, void* data)
{
auto context = v8impl::V8LocalContextFromPesapiEnv(env);
auto callback_data = new PesapiCallbackData {native_impl, data}; // TODO: 没法回收
auto v8_data = v8::External::New(context->GetIsolate(), &callback_data->data);
auto func = v8::FunctionTemplate::New(context->GetIsolate(), PesapiFunctionCallback, v8_data)->GetFunction(context);
auto func = puerts::DataTransfer::IsolateData<puerts::ICppObjectMapper>(context->GetIsolate())->CreateFunction(context, native_impl, data);
if (func.IsEmpty())
return nullptr;
return v8impl::PesapiValueFromV8LocalValue(func.ToLocalChecked());
Expand Down

0 comments on commit 46282ac

Please sign in to comment.