Implements basics for UserData

This commit is contained in:
Deukhoofd 2019-06-14 14:59:38 +02:00
parent 831dbe6917
commit 996b5be496
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
14 changed files with 232 additions and 1 deletions

View File

@ -3,6 +3,7 @@
#include "Binder.hpp" #include "Binder.hpp"
#include "../TableScriptType.hpp" #include "../TableScriptType.hpp"
#include "BoundExpressions/BoundTableExpression.hpp" #include "BoundExpressions/BoundTableExpression.hpp"
#include "../UserData/UserDataScriptType.hpp"
#include <memory> #include <memory>
BoundScriptStatement *Binder::Bind(Script* script, const ParsedScriptStatement *s, BoundScope* scriptScope) { BoundScriptStatement *Binder::Bind(Script* script, const ParsedScriptStatement *s, BoundScope* scriptScope) {
@ -74,7 +75,7 @@ std::shared_ptr<ScriptType> ParseTypeIdentifier(HashedString s){
case HashedString::ConstHash("number"): return std::make_shared<NumericScriptType>(false, false); case HashedString::ConstHash("number"): return std::make_shared<NumericScriptType>(false, false);
case HashedString::ConstHash("bool"): return std::make_shared<ScriptType>(TypeClass::Bool); case HashedString::ConstHash("bool"): return std::make_shared<ScriptType>(TypeClass::Bool);
case HashedString::ConstHash("string"): return std::make_shared<StringScriptType>(false, 0); case HashedString::ConstHash("string"): return std::make_shared<StringScriptType>(false, 0);
default: return std::make_shared<ScriptType>(TypeClass::Error); // todo: change to userdata default: return std::make_shared<UserDataScriptType>(s.GetHash());
} }
} }

View File

@ -102,6 +102,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expre
case TypeClass ::Function: return this->EvaluateFunctionExpression(expression); case TypeClass ::Function: return this->EvaluateFunctionExpression(expression);
case TypeClass ::Nil: return this->EvaluateNilExpression(expression); case TypeClass ::Nil: return this->EvaluateNilExpression(expression);
case TypeClass ::Table: return this-> EvaluateTableExpression(expression); case TypeClass ::Table: return this-> EvaluateTableExpression(expression);
case TypeClass ::UserData: return this -> EvaluateUserDataExpression(expression);
default: throw; default: throw;
} }
} }
@ -291,3 +292,11 @@ shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpre
this -> _evaluationScope = currentEvaluator; this -> _evaluationScope = currentEvaluator;
return make_shared<TableEvalValue>(variables); return make_shared<TableEvalValue>(variables);
} }
shared_ptr<EvalValue> Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) {
switch (expression->GetKind()){
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
case BoundExpressionKind ::Index: return this -> EvaluateIndexExpression(expression);
default: throw;
}
}

View File

@ -48,6 +48,7 @@ class Evaluator {
shared_ptr<EvalValue> EvaluateIndexExpression(const BoundExpression* expression); shared_ptr<EvalValue> EvaluateIndexExpression(const BoundExpression* expression);
shared_ptr<EvalValue> EvaluateNumericTableExpression(const BoundExpression* expression); shared_ptr<EvalValue> EvaluateNumericTableExpression(const BoundExpression* expression);
shared_ptr<EvalValue> EvaluateComplexTableExpression(const BoundExpression *expression); shared_ptr<EvalValue> EvaluateComplexTableExpression(const BoundExpression *expression);
shared_ptr<EvalValue> EvaluateUserDataExpression(const BoundExpression *expression);
shared_ptr<EvalValue> GetVariable(const BoundVariableExpression *expression); shared_ptr<EvalValue> GetVariable(const BoundVariableExpression *expression);
public: public:

View File

@ -0,0 +1,2 @@
#include "UserData.hpp"

25
src/UserData/UserData.hpp Normal file
View File

@ -0,0 +1,25 @@
#ifndef PORYGONLANG_USERDATA_HPP
#define PORYGONLANG_USERDATA_HPP
#include <utility>
#include <unordered_map>
#include "UserDataField.hpp"
class UserData {
std::unordered_map<int, UserDataField*> _fields;
public:
explicit UserData(std::unordered_map<int, UserDataField*> fields){
_fields = std::move(fields);
}
bool ContainsField(int fieldId){
return _fields.find(fieldId) != _fields.end();
}
UserDataField* GetField(int fieldId){
return _fields[fieldId];
}
};
#endif //PORYGONLANG_USERDATA_HPP

View File

@ -0,0 +1,2 @@
#include "UserDataField.hpp"

View File

@ -0,0 +1,33 @@
#ifndef PORYGONLANG_USERDATAFIELD_HPP
#define PORYGONLANG_USERDATAFIELD_HPP
#include "../Evaluator/EvalValues/EvalValue.hpp"
class UserDataField {
shared_ptr<ScriptType> _type;
EvalValue* (*_get)(void* obj);
void (*_set)(void* obj, EvalValue*);
public:
UserDataField(ScriptType* type, EvalValue* (*getter)(void* obj), void (*setter)(void* obj, EvalValue*)){
_type = shared_ptr<ScriptType>(type);
_get = getter;
_set = setter;
}
shared_ptr<ScriptType> GetType(){
return _type;
}
EvalValue* Get(void* obj){
return this ->_get(obj);
}
void Set(void* obj, EvalValue* val){
this->_set(obj, val);
}
};
#endif //PORYGONLANG_USERDATAFIELD_HPP

View File

@ -0,0 +1,2 @@
#include "UserDataScriptType.hpp"

View File

@ -0,0 +1,31 @@
#ifndef PORYGONLANG_USERDATASCRIPTTYPE_HPP
#define PORYGONLANG_USERDATASCRIPTTYPE_HPP
#include "../ScriptType.hpp"
#include "UserData.hpp"
#include "UserDataStorage.hpp"
class UserDataScriptType : public ScriptType{
UserData* _userData;
public:
explicit UserDataScriptType(int id) : ScriptType(TypeClass::UserData){
_userData = UserDataStorage::GetUserDataType(id);
}
bool CanBeIndexedWith(ScriptType* indexer) final{
return indexer->GetClass() == TypeClass ::String;
}
shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) final{
auto stringKey = (StringScriptType*)indexer;
if (stringKey->IsKnownAtBind()){
return _userData->GetField(stringKey->GetHashValue())->GetType();
}
throw "TODO: indexing with dynamic keys";
}
};
#endif //PORYGONLANG_USERDATASCRIPTTYPE_HPP

View File

@ -0,0 +1,4 @@
#include "UserDataStorage.hpp"
std::unordered_map<int, UserData*> UserDataStorage::_userData = {};

View File

@ -0,0 +1,21 @@
#ifndef PORYGONLANG_USERDATASTORAGE_HPP
#define PORYGONLANG_USERDATASTORAGE_HPP
#include <unordered_map>
#include "UserData.hpp"
class UserDataStorage {
static std::unordered_map<int, UserData*> _userData;
public:
static void RegisterType(int i, UserData* ud){
UserDataStorage::_userData.insert({i, ud});
}
static UserData* GetUserDataType(int i){
return UserDataStorage::_userData[i];
}
};
#endif //PORYGONLANG_USERDATASTORAGE_HPP

View File

@ -0,0 +1,2 @@
#include "UserDataValue.hpp"

View File

@ -0,0 +1,50 @@
#ifndef PORYGONLANG_USERDATAVALUE_HPP
#define PORYGONLANG_USERDATAVALUE_HPP
#include "../Evaluator/EvalValues/EvalValue.hpp"
#include "UserData.hpp"
#include "UserDataStorage.hpp"
class UserDataValue : public EvalValue{
UserData* _userData;
void* _obj;
public:
UserDataValue(UserData* userData, void* obj){
_userData = userData;
_obj = obj;
}
UserDataValue(int userDataId, void* obj){
_userData = UserDataStorage::GetUserDataType(userDataId);
_obj = obj;
}
const TypeClass GetTypeClass() final{
return TypeClass ::UserData;
}
bool operator ==(EvalValue* b) final {
if (b->GetTypeClass() != TypeClass::UserData)
return false;
return _obj == ((UserDataValue*)b)->_obj;
}
shared_ptr<EvalValue> Clone() final{
return make_shared<UserDataValue>(_userData, _obj);
}
std::size_t GetHashCode() final{
return reinterpret_cast<intptr_t>(_obj);
}
shared_ptr<EvalValue> IndexValue(EvalValue* val) final {
auto fieldId = val->GetHashCode();
auto field = _userData->GetField(fieldId);
return shared_ptr<EvalValue>(field->Get(_obj));
}
};
#endif //PORYGONLANG_USERDATAVALUE_HPP

View File

@ -0,0 +1,48 @@
#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../src/Script.hpp"
#include "../../src/UserData/UserData.hpp"
#include "../../src/UserData/UserDataStorage.hpp"
#include "../../src/UserData/UserDataValue.hpp"
class UserDataTestObject{
public:
int foo = 10;
static EvalValue* GetFoo(void* obj){
return new IntegerEvalValue(((UserDataTestObject*)obj)->foo);
}
static void SetFoo(void* obj, EvalValue* val){
((UserDataTestObject*)obj)->foo = val->EvaluateInteger();
}
static UserData* CreateData(){
return new UserData({
{
HashedString::ConstHash("foo"),
new UserDataField(new NumericScriptType(true, false), GetFoo, SetFoo)
}
});
}
};
TEST_CASE( "Gets UserData value", "[integration]" ) {
UserDataStorage::RegisterType(HashedString::ConstHash("testObject"), UserDataTestObject::CreateData());
Script* script = Script::Create(R"(
function testFunc(testObject obj)
return obj["foo"]
end
)");
REQUIRE(!script->Diagnostics -> HasErrors());
script->Evaluate();
auto parameter = new UserDataValue(HashedString::ConstHash("testObject"), new UserDataTestObject());
auto variable = script->CallFunction("testFunc", {parameter});
REQUIRE(variable != nullptr);
REQUIRE(variable->EvaluateInteger() == 10);
delete script;
}
#endif