Include Userdata string key in userdata object for debugging

This commit is contained in:
2019-09-07 15:22:24 +02:00
parent bd054b1077
commit 629adafeaf
8 changed files with 36 additions and 13 deletions

View File

@@ -23,13 +23,15 @@ namespace Porygon{
shared_ptr<Binder::BoundScriptStatement> _boundScript; shared_ptr<Binder::BoundScriptStatement> _boundScript;
shared_ptr<const ScriptType> _returnType = nullptr; shared_ptr<const ScriptType> _returnType = nullptr;
ScriptOptions* _scriptOptions; ScriptOptions* _scriptOptions;
public:
shared_ptr<Diagnostics::DiagnosticsHolder> Diagnostics;
private:
std::string _treeString; std::string _treeString;
explicit Script(const u16string&, ScriptOptions*); explicit Script(const u16string&, ScriptOptions*);
Script(shared_ptr<BoundScriptStatement> boundScript, shared_ptr<Diagnostics::DiagnosticsHolder> diagnostics); Script(shared_ptr<BoundScriptStatement> boundScript, shared_ptr<Diagnostics::DiagnosticsHolder> diagnostics);
void Parse(const u16string& script); void Parse(const u16string& script);
public: public:
shared_ptr<Diagnostics::DiagnosticsHolder> Diagnostics;
static Script* Create(const u16string& script, ScriptOptions* = nullptr); static Script* Create(const u16string& script, ScriptOptions* = nullptr);
static Script* Create(const string& script, ScriptOptions* = nullptr); static Script* Create(const string& script, ScriptOptions* = nullptr);

View File

@@ -158,7 +158,7 @@ namespace Porygon::StandardLibraries {
public: public:
static UserData::UserData* CreateUserData(){ static UserData::UserData* CreateUserData(){
return new UserData::UserData({ return new UserData::UserData(new Utilities::HashedString(new u16string(u"__math__")), {
{ {
HashedString::ConstHash("abs"), HashedString::ConstHash("abs"),
new UserData::UserDataField( new UserData::UserDataField(

View File

@@ -8,12 +8,12 @@ namespace Porygon::UserData{
UserData* _ud; UserData* _ud;
uint32_t _key; uint32_t _key;
public: 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){} explicit RetrievedUserData(uint32_t id) : _ud(nullptr), _key(id){}
UserData * Get(); UserData * Get();
inline uint32_t GetKey() const{ [[nodiscard]] inline uint32_t GetKey() const{
return _key; return _key;
} }
}; };

View File

@@ -4,9 +4,10 @@
namespace Porygon::UserData { namespace Porygon::UserData {
extern "C" { extern "C" {
void RegisterUserDataType(uint32_t id) { void RegisterUserDataType(char16_t * key) {
auto ud = new UserData({}); auto hashedKey = new Utilities::HashedString(new u16string(key));
UserDataStorage::RegisterType(id, ud); auto ud = new UserData(hashedKey, {});
UserDataStorage::RegisterType(hashedKey->GetHash(), ud);
} }
void RegisterUserDataField(uint32_t typeId, uint32_t fieldId, UserDataField *field) { void RegisterUserDataField(uint32_t typeId, uint32_t fieldId, UserDataField *field) {

View File

@@ -9,6 +9,7 @@
namespace Porygon::UserData { namespace Porygon::UserData {
class UserData { class UserData {
Utilities::HashedString* _hashedString;
std::unordered_map<uint32_t, unique_ptr<UserDataField>> _fields; std::unordered_map<uint32_t, unique_ptr<UserDataField>> _fields;
std::mutex _mutex; std::mutex _mutex;
@@ -31,7 +32,8 @@ namespace Porygon::UserData {
Evaluation::EvalValue* (*_cast)(void* obj, const ScriptType* castType); Evaluation::EvalValue* (*_cast)(void* obj, const ScriptType* castType);
public: public:
explicit UserData(const std::unordered_map<uint32_t, UserDataField *>& fields) explicit UserData(Utilities::HashedString* hashedString, const std::unordered_map<uint32_t, UserDataField *>& fields)
: _hashedString(hashedString), _isCastable(nullptr), _cast(nullptr)
{ {
for (auto f: fields){ for (auto f: fields){
_fields.insert({f.first, unique_ptr<UserDataField>(f.second)}); _fields.insert({f.first, unique_ptr<UserDataField>(f.second)});
@@ -54,6 +56,12 @@ namespace Porygon::UserData {
delete _logicalAnd; delete _logicalAnd;
delete _logicalOr; delete _logicalOr;
delete _concatenation; delete _concatenation;
delete _hashedString;
}
Utilities::HashedString* GetIdentifier(){
return _hashedString;
} }
[[nodiscard]] [[nodiscard]]

View File

@@ -65,6 +65,12 @@ namespace Porygon::UserData {
} }
return CastResult ::InvalidCast; return CastResult ::InvalidCast;
} }
[[nodiscard]] string ToString() const override {
std::stringstream s;
s << ScriptType::ToString() << " (" << _userData->Get()->GetIdentifier()->GetDebugString() << ")";
return s.str();
}
}; };
} }

View File

@@ -1,14 +1,20 @@
#ifndef PORYGONLANG_USERDATATEMPLATES_HPP #ifndef PORYGONLANG_USERDATATEMPLATES_HPP
#define PORYGONLANG_USERDATATEMPLATES_HPP #define PORYGONLANG_USERDATATEMPLATES_HPP
static std::wstring_convert<std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::little_endian>, 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. \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. \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; \ using T_USERDATA = type; \
static Porygon::UserData::UserData* __createUserData(){ \ 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. \brief Ends creation of invokable function to create userdata.
@@ -20,8 +26,8 @@
\param fields The fields of the object. \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. \returns an invokable static function with the name __createUserData. This can be called to generate a userdata object.
*/ */
#define PORYGON_USERDATA(type, fields) \ #define PORYGON_USERDATA(key, type, fields) \
PORYGON_USERDATA_START(type) \ PORYGON_USERDATA_START(key, type) \
fields \ fields \
PORYGON_USERDATA_END() PORYGON_USERDATA_END()

View File

@@ -40,7 +40,7 @@ private:
} }
public: public:
PORYGON_USERDATA(UserDataTestObject, PORYGON_USERDATA(testObject, UserDataTestObject,
PORYGON_INTEGER_FIELD(foo) PORYGON_INTEGER_FIELD(foo)
PORYGON_READONLY_INTEGER_FIELD(readonly) PORYGON_READONLY_INTEGER_FIELD(readonly)
PORYGON_INTEGER_FUNCTION(getFoo) PORYGON_INTEGER_FUNCTION(getFoo)