From 0266b3c61656c649b9d7882a99ffe177abbb747d Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 12 Sep 2019 20:42:18 +0200 Subject: [PATCH] Better error handling for extern functions that retrieve userdata --- src/UserData/UserData.cpp | 30 +++++++++++++++++++++++++----- src/UserData/UserDataFunction.cpp | 3 ++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/UserData/UserData.cpp b/src/UserData/UserData.cpp index ef18121..0348c73 100644 --- a/src/UserData/UserData.cpp +++ b/src/UserData/UserData.cpp @@ -12,27 +12,47 @@ namespace Porygon::UserData { void RegisterUserDataField(uint32_t typeId, uint32_t fieldId, UserDataField *field) { 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) { 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)){ 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*)){ 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){ auto ud = UserDataStorage::GetUserDataType(typeId); - ud->Get()->AddBinaryOperation(static_cast(kind), operation); + auto get = ud->Get(); + if (get == nullptr){ + throw Exception("Userdata was not found."); + } + get->AddBinaryOperation(static_cast(kind), operation); } } } \ No newline at end of file diff --git a/src/UserData/UserDataFunction.cpp b/src/UserData/UserDataFunction.cpp index dc8461e..937c81b 100644 --- a/src/UserData/UserDataFunction.cpp +++ b/src/UserData/UserDataFunction.cpp @@ -1,5 +1,6 @@ #include "UserDataFunction.hpp" +#include "../Utilities/Random.hpp" using namespace Porygon::Evaluation; @@ -8,7 +9,7 @@ namespace Porygon::UserData{ const EvalValue * CreateFunctionEvalValue(const Evaluation::EvalValue* (*func)(void*, const ScriptOptions*, const EvalValue* [], int ), void* obj) { auto opt = new UserDataFunction(func, obj); - auto t = new GenericFunctionEvalValue(make_shared(), rand()); + auto t = new GenericFunctionEvalValue(make_shared(), Utilities::Random::Get()); t->RegisterOption(opt); return t; }