Skip to content

Commit

Permalink
[unity]去掉plugin导出的SetNativePtr
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Sep 30, 2024
1 parent fe6970b commit da29490
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function Gen(genInfos) {
namespace puerts
{
${wrapperInfos.map((wrapperInfo) => `bool w_${wrapperInfo.Signature}(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, bool checkJSArgument, WrapData* wrapData);`).join('\n')}
${wrapperInfos.map((wrapperInfo) => `bool w_${wrapperInfo.Signature}(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, pesapi_env env, void* self, bool checkJSArgument, WrapData* wrapData);`).join('\n')}
static WrapFuncInfo g_wrapFuncInfos[] = {
${FOR(wrapperInfos, info => t`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ function genFuncWrapper(wrapperInfo) {

return t`
// ${wrapperInfo.CsName}
bool w_${wrapperInfo.Signature}(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, bool checkJSArgument, WrapData* wrapData) {
bool w_${wrapperInfo.Signature}(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, pesapi_env env, void* self, bool checkJSArgument, WrapData* wrapData) {
// PLog("Running w_${wrapperInfo.Signature}");
${il2cpp_snippets.declareTypeInfo(wrapperInfo)}
pesapi_env env = pesapi_get_env(info);
int js_args_len = pesapi_get_args_len(info);
${parameterSignatures.map((x, i) => ` pesapi_value _sv${i} = pesapi_get_arg(info, ${i});`).join('\n')}
Expand All @@ -29,7 +28,6 @@ ${parameterSignatures.map((x, i) => ` pesapi_value _sv${i} = pesapi_get_arg(i
${il2cpp_snippets.checkJSArg(x, i)}
`)}
}
${il2cpp_snippets.getThis(wrapperInfo.ThisSignature)}
${parameterSignatures.map((x, i) => il2cpp_snippets.JSValToCSVal(x, `_sv${i}`, `p${i}`)).join('\n')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,25 @@ static void MethodCallback(pesapi_callback_info info)
try
{
WrapData** wrapDatas = (WrapData**)pesapi_get_userdata(info);
void* self = nullptr;
pesapi_env env = pesapi_get_env(info);
if (!((*wrapDatas)->IsStatic))
{
pesapi_value jsThis = pesapi_get_holder(info);
self = pesapi_get_native_object_ptr(env, jsThis);
if ((*wrapDatas)->NeedBoxing)
{
auto ptrType = (Il2CppClass*) pesapi_get_native_object_typeid(env, jsThis);
if (Class::IsValuetype(ptrType))
{
self = Object::Box(ptrType, self);
}
}
}
bool checkArgument = *wrapDatas && *(wrapDatas + 1);
while(*wrapDatas)
{
if ((*wrapDatas)->Wrap((*wrapDatas)->Method, (*wrapDatas)->MethodPointer, info, checkArgument, *wrapDatas))
if ((*wrapDatas)->Wrap((*wrapDatas)->Method, (*wrapDatas)->MethodPointer, info, env, self, checkArgument, *wrapDatas))
{
return;
}
Expand Down Expand Up @@ -417,28 +432,27 @@ static void* CtorCallback(pesapi_callback_info info)
return nullptr;
}

void* Ptr = ObjectAllocate(classInfo->Class);

g_unityExports.SetNativePtr(pesapi_get_this(info), Ptr, classInfo->TypeId);
void* self = ObjectAllocate(classInfo->Class);

auto isValueType = Class::IsValuetype(classInfo->Class);

try
{
WrapData** wrapDatas = classInfo->CtorWrapDatas;
bool checkArgument = *wrapDatas && *(wrapDatas + 1);
pesapi_env env = pesapi_get_env(info);
while(*wrapDatas)
{
if ((*wrapDatas)->Wrap((*wrapDatas)->Method, (*wrapDatas)->MethodPointer, info, checkArgument, *wrapDatas))
if ((*wrapDatas)->Wrap((*wrapDatas)->Method, (*wrapDatas)->MethodPointer, info, env, self, checkArgument, *wrapDatas))
{
return Ptr;
return self;
}
++wrapDatas;
}


if (isValueType && pesapi_get_args_len(info) == 0)
return Ptr;
return self;
else
pesapi_throw_by_string(info, "invalid arguments");

Expand All @@ -464,7 +478,7 @@ static void* CtorCallback(pesapi_callback_info info)

if (isValueType)
{
ValueTypeFree(Ptr);
ValueTypeFree(self);
}

return nullptr;
Expand Down Expand Up @@ -1188,9 +1202,8 @@ static void JsObjectSetRef(pesapi_env env, pesapi_value outer, pesapi_value val)
}
}

static bool ReflectionWrapper(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, bool checkJSArgument, WrapData* wrapData)
static bool ReflectionWrapper(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, pesapi_env env, void* self, bool checkJSArgument, WrapData* wrapData)
{
pesapi_env env = pesapi_get_env(info);
int js_args_len = pesapi_get_args_len(info);
bool hasParamArray = wrapData->HasParamArray;
bool isExtensionMethod = wrapData->IsExtensionMethod;
Expand Down Expand Up @@ -1373,26 +1386,17 @@ handle_underlying:
}
void** args = method->parameters_count > 0 ? (void**)alloca(sizeof(void*) * method->parameters_count) : nullptr;
pesapi_value jsThis = pesapi_get_holder(info);
void* csThis = nullptr;
if (Method::IsInstance(method))
#ifndef UNITY_2021_1_OR_NEWER
if (self)
{
csThis = pesapi_get_native_object_ptr(env, jsThis);
Il2CppClass* thisType = method->klass;
#ifndef UNITY_2021_1_OR_NEWER

if (Class::IsValuetype(thisType))
{
csThis = ((uint8_t*)csThis) - sizeof(Il2CppObject);
}
#endif
if (thisType == il2cpp_defaults.object_class)
{
auto ptrType = (Il2CppClass*) pesapi_get_native_object_typeid(env, jsThis);
if (Class::IsValuetype(ptrType))
{
csThis = Object::Box(ptrType, csThis);
}
self = ((uint8_t*)self) - sizeof(Il2CppObject);
}
}
#endif
if (isExtensionMethod)
{
args[0] = pesapi_get_native_object_ptr(env, jsThis);
Expand Down Expand Up @@ -1520,7 +1524,7 @@ handle_underlying:
}
}

Il2CppObject* ret = Runtime::InvokeWithThrow(method, csThis, args); //返回ValueType有boxing
Il2CppObject* ret = Runtime::InvokeWithThrow(method, self, args); //返回ValueType有boxing

for (int i = csArgStart; i < method->parameters_count; ++i)
{
Expand Down Expand Up @@ -1910,6 +1914,7 @@ static void SetParamArrayFlagAndOptionalNum(puerts::WrapData* data, const char*
{
data->HasParamArray = false;
data->OptionalNum = 0;
data->NeedBoxing = false;

const char* p = signature;
while(*p)
Expand All @@ -1918,6 +1923,10 @@ static void SetParamArrayFlagAndOptionalNum(puerts::WrapData* data, const char*
{
data->HasParamArray = true;
}
if (*p == 'T')
{
data->NeedBoxing = true;
}
if (*p == 'D')
{
++data->OptionalNum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace puerts
{
typedef void (*FieldWrapFuncPtr)(pesapi_callback_info info, FieldInfo* field, size_t offset, Il2CppClass* fieldType);
typedef bool (*WrapFuncPtr)(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, bool checkArgument, struct WrapData* wrapData);
typedef bool (*WrapFuncPtr)(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, pesapi_env env, void* self, bool checkArgument, struct WrapData* wrapData);

struct FieldWrapData
{
Expand All @@ -31,6 +31,7 @@ struct WrapData
bool IsExtensionMethod;
bool HasParamArray;
int OptionalNum;
bool NeedBoxing;
Il2CppClass* TypeInfos[0];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ struct PObjectRefInfo

typedef void* (*GetJsClassInfoFunc)(const void* TypeId, bool TryLazyLoad);

typedef void (*SetNativePtrFunc)(v8::Object* obj, void* ptr, void* type_id);

typedef void (*UnrefJsObjectFunc)(struct PObjectRefInfo* objectInfo);

typedef const void* (*CSharpTypeToTypeIdFunc)(const void *type);
Expand All @@ -33,8 +31,6 @@ typedef v8::Value* (*GetModuleExecutorFunc)(v8::Context* env);

typedef struct JsClassInfoHeader* (*GetJsClassInfoFunc)(const void* TypeId, bool TryLazyLoad);

typedef void (*SetNativePtrFunc)(pesapi_value obj, void* ptr, const void* type_id);

typedef void (*UnrefJsObjectFunc)(struct PObjectRefInfo* delegateInfo);

typedef const void* (*CSharpTypeToTypeIdFunc)(Il2CppObject *type);
Expand All @@ -58,8 +54,6 @@ struct UnityExports
CSharpTypeToTypeIdFunc CSharpTypeToTypeId = nullptr;

//plugin api

SetNativePtrFunc SetNativePtr = nullptr;
UnrefJsObjectFunc UnrefJsObject = nullptr;
GetJsClassInfoFunc GetJsClassInfo = nullptr;

Expand Down
6 changes: 0 additions & 6 deletions unity/cli/UnityExports4Puerts.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ struct PObjectRefInfo

typedef void* (*GetJsClassInfoFunc)(const void* TypeId, bool TryLazyLoad);

typedef void (*SetNativePtrFunc)(v8::Object* obj, void* ptr, void* type_id);

typedef void (*UnrefJsObjectFunc)(struct PObjectRefInfo* objectInfo);

typedef const void* (*CSharpTypeToTypeIdFunc)(const void *type);
Expand All @@ -33,8 +31,6 @@ typedef v8::Value* (*GetModuleExecutorFunc)(v8::Context* env);

typedef struct JsClassInfoHeader* (*GetJsClassInfoFunc)(const void* TypeId, bool TryLazyLoad);

typedef void (*SetNativePtrFunc)(pesapi_value obj, void* ptr, const void* type_id);

typedef void (*UnrefJsObjectFunc)(struct PObjectRefInfo* delegateInfo);

typedef const void* (*CSharpTypeToTypeIdFunc)(Il2CppObject *type);
Expand All @@ -58,8 +54,6 @@ struct UnityExports
CSharpTypeToTypeIdFunc CSharpTypeToTypeId = nullptr;

//plugin api

SetNativePtrFunc SetNativePtr = nullptr;
UnrefJsObjectFunc UnrefJsObject = nullptr;
GetJsClassInfoFunc GetJsClassInfo = nullptr;

Expand Down
7 changes: 0 additions & 7 deletions unity/native_src_il2cpp/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ void PLog(LogLevel Level, const std::string Fmt, ...)
}
}

static void SetNativePtr(v8::Object* obj, void* ptr, void* type_id)
{
DataTransfer::SetPointer(obj, ptr, 0);
DataTransfer::SetPointer(obj, type_id, 1);
}

static void* _GetRuntimeObjectFromPersistentObject(v8::Local<v8::Context> Context, v8::Local<v8::Object> Obj)
{
auto Isolate = Context->GetIsolate();
Expand Down Expand Up @@ -344,7 +338,6 @@ V8_EXPORT pesapi_env_ref GetPesapiEnvHolder(puerts::JSEnv* jsEnv)

V8_EXPORT void ExchangeAPI(puerts::UnityExports * exports)
{
exports->SetNativePtr = &puerts::SetNativePtr;
exports->SetExtraData = &puerts::SetExtraData;
exports->UnrefJsObject = &puerts::UnrefJsObject;
exports->GetJsClassInfo = &puerts::GetJsClassInfo;
Expand Down

0 comments on commit da29490

Please sign in to comment.