diff --git a/src/ScriptTypes/ScriptType.cpp b/src/ScriptTypes/ScriptType.cpp index c4cc8f7..2c005ae 100644 --- a/src/ScriptTypes/ScriptType.cpp +++ b/src/ScriptTypes/ScriptType.cpp @@ -36,6 +36,10 @@ namespace Porygon{ type->RegisterFunctionOption(option); return type; } + + TypeClass GetTypeClass(ScriptType* t){ + return t->GetClass(); + } } } diff --git a/src/UserData/UserData.cpp b/src/UserData/UserData.cpp index bbc0acb..6f8e0dc 100644 --- a/src/UserData/UserData.cpp +++ b/src/UserData/UserData.cpp @@ -18,5 +18,15 @@ namespace Porygon::UserData { auto ud = UserDataStorage::GetUserDataType(typeId); return ud->Get()->GetFieldCount(); } + + void SetIsCastableFunc(uint32_t typeId, bool (*cast)(const ScriptType*, bool)){ + auto ud = UserDataStorage::GetUserDataType(typeId); + ud->Get()->SetIsCastable(cast); + } + + void SetCastFunc(uint32_t typeId, Evaluation::EvalValue* (*cast)(void*, const ScriptType*)){ + auto ud = UserDataStorage::GetUserDataType(typeId); + ud->Get()->SetCastFunc(cast); + } } } \ No newline at end of file diff --git a/src/UserData/UserData.hpp b/src/UserData/UserData.hpp index de9a803..5aa1325 100644 --- a/src/UserData/UserData.hpp +++ b/src/UserData/UserData.hpp @@ -27,8 +27,11 @@ namespace Porygon::UserData { UserDataBinaryOperation* _logicalOr = nullptr; UserDataBinaryOperation* _concatenation = nullptr; + bool (*_isCastable)(const ScriptType* type, bool explicitCast); + Evaluation::EvalValue* (*_cast)(void* obj, const ScriptType* castType); + public: - explicit UserData(std::unordered_map fields) + explicit UserData(const std::unordered_map& fields) { for (auto f: fields){ _fields.insert({f.first, unique_ptr(f.second)}); @@ -73,6 +76,23 @@ namespace Porygon::UserData { return _fields.size(); } + void SetIsCastable(bool (*cast)(const ScriptType* type, bool explicitCast)){ + _isCastable = cast; + } + + bool IsCastable(const shared_ptr& castType, bool explicitCast) const{ + if (_isCastable == nullptr) return false; + return _isCastable(castType.get(), explicitCast); + } + + void SetCastFunc(Evaluation::EvalValue* (*cast)(void* obj, const ScriptType* castType)){ + _cast = cast; + } + + Evaluation::EvalValue* Cast(void* obj, const ScriptType* castType) const{ + return _cast(obj, castType); + } + [[nodiscard]] UserDataBinaryOperation* GetBinaryOperation(Binder::BoundBinaryOperation op){ switch (op){ diff --git a/src/UserData/UserDataScriptType.cpp b/src/UserData/UserDataScriptType.cpp index 842cc75..689a3ba 100644 --- a/src/UserData/UserDataScriptType.cpp +++ b/src/UserData/UserDataScriptType.cpp @@ -6,5 +6,9 @@ extern "C"{ UserDataScriptType* CreateUserDataType(uint32_t id){ return new UserDataScriptType(id); } + +uint32_t GetScriptTypeUserData(UserDataScriptType* t){ + return t->GetUserData()->GetKey(); +} } } \ No newline at end of file diff --git a/src/UserData/UserDataScriptType.hpp b/src/UserData/UserDataScriptType.hpp index 0b8571f..67ea7c7 100644 --- a/src/UserData/UserDataScriptType.hpp +++ b/src/UserData/UserDataScriptType.hpp @@ -25,6 +25,10 @@ namespace Porygon::UserData { delete _userData; } + RetrievedUserData* GetUserData() const{ + return _userData; + } + bool CanBeIndexedWith(const ScriptType *indexer) const final { if (indexer->GetClass() != TypeClass::String) { return false; @@ -54,6 +58,13 @@ namespace Porygon::UserData { [[nodiscard]] inline shared_ptr GetIndexedType(uint32_t hash) const final { return _userData->Get()->GetField(hash)->GetType(); } + + [[nodiscard]] CastResult CastableTo(const shared_ptr& castType, bool explicitCast) const final{ + if (_userData->Get()->IsCastable(castType, explicitCast)){ + return CastResult::ValidCast; + } + return CastResult ::InvalidCast; + } }; } diff --git a/src/UserData/UserDataValue.hpp b/src/UserData/UserDataValue.hpp index eb909c7..a8e874c 100644 --- a/src/UserData/UserDataValue.hpp +++ b/src/UserData/UserDataValue.hpp @@ -73,6 +73,11 @@ namespace Porygon::UserData { inline void* GetObjectPointer(){ return _obj; } + + [[nodiscard]] + EvalValue* Cast(shared_ptr castType) const final{ + return _userData->Get()->Cast(_obj, castType.get()); + } }; }