Better error handling for extern functions that retrieve userdata

This commit is contained in:
Deukhoofd 2019-09-12 20:42:18 +02:00
parent bae3e92577
commit 0266b3c616
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 27 additions and 6 deletions

View File

@ -12,27 +12,47 @@ namespace Porygon::UserData {
void RegisterUserDataField(uint32_t typeId, uint32_t fieldId, UserDataField *field) { void RegisterUserDataField(uint32_t typeId, uint32_t fieldId, UserDataField *field) {
auto ud = UserDataStorage::GetUserDataType(typeId); auto ud = UserDataStorage::GetUserDataType(typeId);
ud->Get()->CreateField(fieldId, field); auto get = ud->Get();
if (get == nullptr){
throw Exception("Userdata was not found.");
}
get->CreateField(fieldId, field);
} }
int32_t GetUserDataFieldCount(uint32_t typeId) { int32_t GetUserDataFieldCount(uint32_t typeId) {
auto ud = UserDataStorage::GetUserDataType(typeId); auto ud = UserDataStorage::GetUserDataType(typeId);
return ud->Get()->GetFieldCount(); auto get = ud->Get();
if (get == nullptr){
throw Exception("Userdata was not found.");
}
return get->GetFieldCount();
} }
void SetIsCastableFunc(uint32_t typeId, bool (*cast)(const ScriptType*, bool)){ void SetIsCastableFunc(uint32_t typeId, bool (*cast)(const ScriptType*, bool)){
auto ud = UserDataStorage::GetUserDataType(typeId); auto ud = UserDataStorage::GetUserDataType(typeId);
ud->Get()->SetIsCastable(cast); auto get = ud->Get();
if (get == nullptr){
throw Exception("Userdata was not found.");
}
get->SetIsCastable(cast);
} }
void SetCastFunc(uint32_t typeId, Evaluation::EvalValue* (*cast)(void*, const ScriptType*)){ void SetCastFunc(uint32_t typeId, Evaluation::EvalValue* (*cast)(void*, const ScriptType*)){
auto ud = UserDataStorage::GetUserDataType(typeId); auto ud = UserDataStorage::GetUserDataType(typeId);
ud->Get()->SetCastFunc(cast); auto get = ud->Get();
if (get == nullptr){
throw Exception("Userdata was not found.");
}
get->SetCastFunc(cast);
} }
void AddUserdataBinaryOperation(uint32_t typeId, char kind, const UserDataBinaryOperation* operation){ void AddUserdataBinaryOperation(uint32_t typeId, char kind, const UserDataBinaryOperation* operation){
auto ud = UserDataStorage::GetUserDataType(typeId); auto ud = UserDataStorage::GetUserDataType(typeId);
ud->Get()->AddBinaryOperation(static_cast<Binder::BoundBinaryOperation >(kind), operation); auto get = ud->Get();
if (get == nullptr){
throw Exception("Userdata was not found.");
}
get->AddBinaryOperation(static_cast<Binder::BoundBinaryOperation >(kind), operation);
} }
} }
} }

View File

@ -1,5 +1,6 @@
#include "UserDataFunction.hpp" #include "UserDataFunction.hpp"
#include "../Utilities/Random.hpp"
using namespace Porygon::Evaluation; using namespace Porygon::Evaluation;
@ -8,7 +9,7 @@ namespace Porygon::UserData{
const EvalValue * CreateFunctionEvalValue(const Evaluation::EvalValue* (*func)(void*, const ScriptOptions*, const EvalValue * CreateFunctionEvalValue(const Evaluation::EvalValue* (*func)(void*, const ScriptOptions*,
const EvalValue* [], int ), void* obj) { const EvalValue* [], int ), void* obj) {
auto opt = new UserDataFunction(func, obj); auto opt = new UserDataFunction(func, obj);
auto t = new GenericFunctionEvalValue(make_shared<GenericFunctionScriptType>(), rand()); auto t = new GenericFunctionEvalValue(make_shared<GenericFunctionScriptType>(), Utilities::Random::Get());
t->RegisterOption(opt); t->RegisterOption(opt);
return t; return t;
} }