diff --git a/src/Script.hpp b/src/Script.hpp index f29a3ca..aa776e6 100644 --- a/src/Script.hpp +++ b/src/Script.hpp @@ -23,13 +23,15 @@ namespace Porygon{ shared_ptr _boundScript; shared_ptr _returnType = nullptr; ScriptOptions* _scriptOptions; + public: + shared_ptr Diagnostics; + private: std::string _treeString; explicit Script(const u16string&, ScriptOptions*); Script(shared_ptr boundScript, shared_ptr diagnostics); void Parse(const u16string& script); public: - shared_ptr Diagnostics; static Script* Create(const u16string& script, ScriptOptions* = nullptr); static Script* Create(const string& script, ScriptOptions* = nullptr); diff --git a/src/StandardLibraries/MathLibrary.hpp b/src/StandardLibraries/MathLibrary.hpp index 4fb3981..cedbe73 100644 --- a/src/StandardLibraries/MathLibrary.hpp +++ b/src/StandardLibraries/MathLibrary.hpp @@ -158,7 +158,7 @@ namespace Porygon::StandardLibraries { public: static UserData::UserData* CreateUserData(){ - return new UserData::UserData({ + return new UserData::UserData(new Utilities::HashedString(new u16string(u"__math__")), { { HashedString::ConstHash("abs"), new UserData::UserDataField( diff --git a/src/UserData/RetrievedUserData.hpp b/src/UserData/RetrievedUserData.hpp index 23a4cff..d04fe51 100644 --- a/src/UserData/RetrievedUserData.hpp +++ b/src/UserData/RetrievedUserData.hpp @@ -8,12 +8,12 @@ namespace Porygon::UserData{ UserData* _ud; uint32_t _key; public: - explicit RetrievedUserData(UserData* ud) : _ud(ud), _key(0){} + explicit RetrievedUserData(UserData* ud) : _ud(ud), _key(ud->GetIdentifier()->GetHash()){} explicit RetrievedUserData(uint32_t id) : _ud(nullptr), _key(id){} UserData * Get(); - inline uint32_t GetKey() const{ + [[nodiscard]] inline uint32_t GetKey() const{ return _key; } }; diff --git a/src/UserData/UserData.cpp b/src/UserData/UserData.cpp index 6f8e0dc..11ee37d 100644 --- a/src/UserData/UserData.cpp +++ b/src/UserData/UserData.cpp @@ -4,9 +4,10 @@ namespace Porygon::UserData { extern "C" { - void RegisterUserDataType(uint32_t id) { - auto ud = new UserData({}); - UserDataStorage::RegisterType(id, ud); + void RegisterUserDataType(char16_t * key) { + auto hashedKey = new Utilities::HashedString(new u16string(key)); + auto ud = new UserData(hashedKey, {}); + UserDataStorage::RegisterType(hashedKey->GetHash(), ud); } void RegisterUserDataField(uint32_t typeId, uint32_t fieldId, UserDataField *field) { diff --git a/src/UserData/UserData.hpp b/src/UserData/UserData.hpp index 5aa1325..86ff499 100644 --- a/src/UserData/UserData.hpp +++ b/src/UserData/UserData.hpp @@ -9,6 +9,7 @@ namespace Porygon::UserData { class UserData { + Utilities::HashedString* _hashedString; std::unordered_map> _fields; std::mutex _mutex; @@ -31,7 +32,8 @@ namespace Porygon::UserData { Evaluation::EvalValue* (*_cast)(void* obj, const ScriptType* castType); public: - explicit UserData(const std::unordered_map& fields) + explicit UserData(Utilities::HashedString* hashedString, const std::unordered_map& fields) + : _hashedString(hashedString), _isCastable(nullptr), _cast(nullptr) { for (auto f: fields){ _fields.insert({f.first, unique_ptr(f.second)}); @@ -54,6 +56,12 @@ namespace Porygon::UserData { delete _logicalAnd; delete _logicalOr; delete _concatenation; + + delete _hashedString; + } + + Utilities::HashedString* GetIdentifier(){ + return _hashedString; } [[nodiscard]] diff --git a/src/UserData/UserDataScriptType.hpp b/src/UserData/UserDataScriptType.hpp index 67ea7c7..dc22f19 100644 --- a/src/UserData/UserDataScriptType.hpp +++ b/src/UserData/UserDataScriptType.hpp @@ -65,6 +65,12 @@ namespace Porygon::UserData { } return CastResult ::InvalidCast; } + + [[nodiscard]] string ToString() const override { + std::stringstream s; + s << ScriptType::ToString() << " (" << _userData->Get()->GetIdentifier()->GetDebugString() << ")"; + return s.str(); + } }; } diff --git a/src/UserData/UserDataTemplates.hpp b/src/UserData/UserDataTemplates.hpp index f5e7c17..b1f9892 100644 --- a/src/UserData/UserDataTemplates.hpp +++ b/src/UserData/UserDataTemplates.hpp @@ -1,14 +1,20 @@ #ifndef PORYGONLANG_USERDATATEMPLATES_HPP #define PORYGONLANG_USERDATATEMPLATES_HPP +static std::wstring_convert, char16_t> to_16; +Porygon::Utilities::HashedString* Convert(const char* k){ + auto conv = new u16string(to_16.from_bytes(k)); + return new Porygon::Utilities::HashedString(conv); +} /*! \brief Begins creating an invokable function. Make sure to call PORYGON_USERDATA_END after this. \returns The start of an invokable function with the name __createUserData. This can be called to return a userdata object. */ -#define PORYGON_USERDATA_START(type) \ +#define PORYGON_USERDATA_START(key, type) \ using T_USERDATA = type; \ static Porygon::UserData::UserData* __createUserData(){ \ - return new Porygon::UserData::UserData({ \ + return new Porygon::UserData::UserData( \ + Convert(#key), { \ /*! \brief Ends creation of invokable function to create userdata. @@ -20,8 +26,8 @@ \param fields The fields of the object. \returns an invokable static function with the name __createUserData. This can be called to generate a userdata object. */ -#define PORYGON_USERDATA(type, fields) \ - PORYGON_USERDATA_START(type) \ +#define PORYGON_USERDATA(key, type, fields) \ + PORYGON_USERDATA_START(key, type) \ fields \ PORYGON_USERDATA_END() diff --git a/tests/integration/UserDataTests.cpp b/tests/integration/UserDataTests.cpp index 7b695cd..47c4345 100644 --- a/tests/integration/UserDataTests.cpp +++ b/tests/integration/UserDataTests.cpp @@ -40,7 +40,7 @@ private: } public: - PORYGON_USERDATA(UserDataTestObject, + PORYGON_USERDATA(testObject, UserDataTestObject, PORYGON_INTEGER_FIELD(foo) PORYGON_READONLY_INTEGER_FIELD(readonly) PORYGON_INTEGER_FUNCTION(getFoo)