Skip to content

Commit

Permalink
[unity]错误信息加上函数名
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Nov 7, 2024
1 parent 053d091 commit f0504df
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct CSharpMethodInfo
bool IsStatic;
bool IsGetter;
bool IsSetter;
bool NeedBoxing;
std::vector<WrapData*> OverloadDatas;
};

Expand Down Expand Up @@ -283,16 +284,16 @@ void SetGlobalType_TypedValue(Il2CppReflectionType *type)

static void MethodCallback(struct pesapi_ffi* apis, pesapi_callback_info info)
{
CSharpMethodInfo* csharpMethodInfo = (CSharpMethodInfo*)apis->get_userdata(info);
try
{
WrapData** wrapDatas = (WrapData**)apis->get_userdata(info);
void* self = nullptr;
pesapi_env env = apis->get_env(info);
if (!((*wrapDatas)->IsStatic))
if (!(csharpMethodInfo->IsStatic))
{
pesapi_value jsThis = apis->get_holder(info);
self = apis->get_native_object_ptr(env, jsThis);
if ((*wrapDatas)->NeedBoxing)
if (csharpMethodInfo->NeedBoxing)
{
auto ptrType = (Il2CppClass*) apis->get_native_object_typeid(env, jsThis);
if (Class::IsValuetype(ptrType))
Expand All @@ -301,6 +302,7 @@ static void MethodCallback(struct pesapi_ffi* apis, pesapi_callback_info info)
}
}
}
WrapData** wrapDatas = csharpMethodInfo->OverloadDatas.data();
bool checkArgument = *wrapDatas && *(wrapDatas + 1);
while(*wrapDatas)
{
Expand All @@ -310,7 +312,8 @@ static void MethodCallback(struct pesapi_ffi* apis, pesapi_callback_info info)
}
++wrapDatas;
}
apis->throw_by_string(info, "invalid arguments");
std::string err_info = "invalid arguments for " + csharpMethodInfo->Name;
apis->throw_by_string(info, err_info.c_str());
}
catch (Il2CppExceptionWrapper& exception)
{
Expand All @@ -321,7 +324,8 @@ static void MethodCallback(struct pesapi_ffi* apis, pesapi_callback_info info)
Il2CppString* result = (Il2CppString*)il2cpp::vm::Runtime::Invoke(toStringMethod, exception.ex, NULL, &outException);
if (outException != NULL)
{
apis->throw_by_string(info, "unknow c# execption!");
std::string err_info = "unknow c# execption for " + csharpMethodInfo->Name;
apis->throw_by_string(info, err_info.c_str());
}
else
{
Expand Down Expand Up @@ -439,7 +443,7 @@ void PLog(const char* Fmt, ...)

static void* CtorCallback(struct pesapi_ffi* apis, pesapi_callback_info info)
{
JsClassInfoHeader* classInfo = reinterpret_cast<JsClassInfoHeader*>(apis->get_userdata(info));
JsClassInfo* classInfo = reinterpret_cast<JsClassInfo*>(apis->get_userdata(info));
// or will crash in macos.
if (!classInfo->IsValueType && *(classInfo->CtorWrapDatas) == nullptr)
{
Expand Down Expand Up @@ -469,7 +473,10 @@ static void* CtorCallback(struct pesapi_ffi* apis, pesapi_callback_info info)
if (isValueType && apis->get_args_len(info) == 0)
return self;
else
apis->throw_by_string(info, "invalid arguments");
{
std::string err_info = "invalid arguments for constructor of " + classInfo->Name;
apis->throw_by_string(info, err_info.c_str());
}

}
catch (Il2CppExceptionWrapper& exception)
Expand All @@ -481,7 +488,8 @@ static void* CtorCallback(struct pesapi_ffi* apis, pesapi_callback_info info)
Il2CppString* result = (Il2CppString*)il2cpp::vm::Runtime::Invoke(toStringMethod, exception.ex, NULL, &outException);
if (outException != NULL)
{
apis->throw_by_string(info, "unknow c# execption!");
std::string err_info = "unknow c# execption for constructor of " + classInfo->Name;
apis->throw_by_string(info, err_info.c_str());
}
else
{
Expand Down Expand Up @@ -2071,11 +2079,11 @@ void ReleaseCSharpTypeInfo(puerts::JsClassInfo* classInfo)
delete classInfo;
}

static void SetParamArrayFlagAndOptionalNum(puerts::WrapData* data, const char* signature)
static bool SetParamArrayFlagAndOptionalNum(puerts::WrapData* data, const char* signature)
{
data->HasParamArray = false;
data->OptionalNum = 0;
data->NeedBoxing = false;
bool NeedBoxing = false;

const char* p = signature;
while(*p)
Expand All @@ -2086,14 +2094,15 @@ static void SetParamArrayFlagAndOptionalNum(puerts::WrapData* data, const char*
}
if (*p == 'T')
{
data->NeedBoxing = true;
NeedBoxing = true;
}
if (*p == 'D')
{
++data->OptionalNum;
}
++p;
}
return NeedBoxing;
}

puerts::WrapData* AddConstructor(puerts::JsClassInfo* classInfo, const char* signature, puerts::WrapFuncPtr WrapFunc, MethodInfo* method, Il2CppMethodPointer methodPointer, int typeInfoNum)
Expand Down Expand Up @@ -2125,7 +2134,7 @@ puerts::WrapData* AddMethod(puerts::JsClassInfo* classInfo, const char* signatur
data->Wrap = WrapFunc;
data->IsStatic = isStatic;
data->IsExtensionMethod = isExtensionMethod;
SetParamArrayFlagAndOptionalNum(data, signature);
bool NeedBoxing = SetParamArrayFlagAndOptionalNum(data, signature);

for(int i = 0; i < classInfo->Methods.size(); ++i)
{
Expand All @@ -2145,7 +2154,7 @@ puerts::WrapData* AddMethod(puerts::JsClassInfo* classInfo, const char* signatur
//puerts::PLog("%s %d %d %d %p", name, typeInfoNum, allocSize, sizeof(puerts::WrapData), data);
std::vector<puerts::WrapData*> OverloadDatas;
OverloadDatas.push_back(data);
classInfo->Methods.push_back({std::string(name), isStatic, isGetter, isSetter, std::move(OverloadDatas)});
classInfo->Methods.push_back({std::string(name), isStatic, isGetter, isSetter, NeedBoxing, std::move(OverloadDatas)});
return data;
}

Expand Down Expand Up @@ -2222,7 +2231,7 @@ bool RegisterCSharpType(puerts::JsClassInfo* classInfo)
for (auto const& method: methods)
{
pesapi_set_method_info(
properties, pos++, method->Name.c_str(), method->IsStatic, &puerts::MethodCallback, method->OverloadDatas.data(), nullptr);
properties, pos++, method->Name.c_str(), method->IsStatic, &puerts::MethodCallback, method, nullptr);
}

for (auto const& kv: gseters)
Expand All @@ -2231,8 +2240,8 @@ bool RegisterCSharpType(puerts::JsClassInfo* classInfo)
pesapi_set_property_info(properties, pos++, geter_or_setter->Name.c_str(), geter_or_setter->IsStatic,
kv.second.first ? &puerts::MethodCallback : nullptr,
kv.second.second ? &puerts::MethodCallback : nullptr,
kv.second.first ? kv.second.first->OverloadDatas.data() : nullptr,
kv.second.second ? kv.second.second->OverloadDatas.data() : nullptr,
kv.second.first,
kv.second.second,
nullptr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ struct WrapData
bool IsExtensionMethod;
bool HasParamArray;
int OptionalNum;
bool NeedBoxing;
Il2CppClass* TypeInfos[0];
};

Expand Down

0 comments on commit f0504df

Please sign in to comment.