Work on making userdata work through extern C entry points
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
2c313791d9
commit
7c345d85e8
|
@ -83,11 +83,16 @@ BoundStatement *Binder::BindIndexAssignmentStatement(const ParsedStatement *stat
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<ScriptType> ParseTypeIdentifier(HashedString s){
|
std::shared_ptr<ScriptType> ParseTypeIdentifier(HashedString s){
|
||||||
switch (s.GetHash()){
|
auto hash = s.GetHash();
|
||||||
|
switch (hash){
|
||||||
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<UserDataScriptType>(s.GetHash());
|
default:
|
||||||
|
if (!UserDataStorage::HasUserDataType(hash)){
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return std::make_shared<UserDataScriptType>(hash);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,6 +107,10 @@ BoundStatement *Binder::BindFunctionDeclarationStatement(const ParsedStatement *
|
||||||
for (int i = 0; i < parameters->size(); i++){
|
for (int i = 0; i < parameters->size(); i++){
|
||||||
auto var = parameters -> at(i);
|
auto var = parameters -> at(i);
|
||||||
auto parsedType = ParseTypeIdentifier(var->GetType());
|
auto parsedType = ParseTypeIdentifier(var->GetType());
|
||||||
|
if (parsedType == nullptr){
|
||||||
|
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::InvalidTypeName, statement->GetStartPosition(), statement->GetLength());
|
||||||
|
return new BoundBadStatement();
|
||||||
|
}
|
||||||
parameterTypes.at(i) = parsedType;
|
parameterTypes.at(i) = parsedType;
|
||||||
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), parsedType);
|
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), parsedType);
|
||||||
if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok){
|
if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok){
|
||||||
|
@ -442,7 +451,7 @@ BoundExpression* Binder::BindNumericalTableExpression(const ParsedNumericalTable
|
||||||
}
|
}
|
||||||
|
|
||||||
BoundExpression *Binder::BindTableExpression(const ParsedTableExpression *expression) {
|
BoundExpression *Binder::BindTableExpression(const ParsedTableExpression *expression) {
|
||||||
auto tableScope = new unordered_map<int, BoundVariable*>();
|
auto tableScope = new unordered_map<uint32_t, BoundVariable*>();
|
||||||
auto innerScope = new BoundScope(tableScope);
|
auto innerScope = new BoundScope(tableScope);
|
||||||
auto currentScope = this -> _scope;
|
auto currentScope = this -> _scope;
|
||||||
this -> _scope = innerScope;
|
this -> _scope = innerScope;
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
|
|
||||||
#include "BoundScope.hpp"
|
#include "BoundScope.hpp"
|
||||||
|
|
||||||
BoundScope::BoundScope(unordered_map<int, BoundVariable *> *tableScope) {
|
BoundScope::BoundScope(unordered_map<uint32_t, BoundVariable *> *tableScope) {
|
||||||
_tableScope = tableScope;
|
_tableScope = tableScope;
|
||||||
_currentScope = 1;
|
_currentScope = 1;
|
||||||
_lastCreatedScope = 1;
|
_lastCreatedScope = 1;
|
||||||
auto localUpmostScope = new unordered_map<int, BoundVariable*>();
|
auto localUpmostScope = new unordered_map<uint32_t, BoundVariable*>();
|
||||||
_localScope.push_back(localUpmostScope);
|
_localScope.push_back(localUpmostScope);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ void BoundScope::GoInnerScope() {
|
||||||
_lastCreatedScope++;
|
_lastCreatedScope++;
|
||||||
_currentScope = _lastCreatedScope;
|
_currentScope = _lastCreatedScope;
|
||||||
if (_localScope.size() < _currentScope){
|
if (_localScope.size() < _currentScope){
|
||||||
auto innerScope = new unordered_map<int, BoundVariable*>();
|
auto innerScope = new unordered_map<uint32_t, BoundVariable*>();
|
||||||
_localScope.push_back(innerScope);
|
_localScope.push_back(innerScope);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ int BoundScope::Exists(int key) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
BoundVariable *BoundScope::GetVariable(int scope, int identifier) {
|
BoundVariable *BoundScope::GetVariable(uint32_t scope, uint32_t identifier) {
|
||||||
if (scope == 0){
|
if (scope == 0){
|
||||||
auto find = this -> _tableScope->find(identifier);
|
auto find = this -> _tableScope->find(identifier);
|
||||||
if (find != _tableScope->end()){
|
if (find != _tableScope->end()){
|
||||||
|
@ -70,7 +70,7 @@ BoundVariable *BoundScope::GetVariable(int scope, int identifier) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VariableAssignment BoundScope::CreateExplicitLocal(int identifier, std::shared_ptr<ScriptType> type) {
|
VariableAssignment BoundScope::CreateExplicitLocal(uint32_t identifier, std::shared_ptr<ScriptType> type) {
|
||||||
auto scope = this->_localScope.at(this->_currentScope - 1);
|
auto scope = this->_localScope.at(this->_currentScope - 1);
|
||||||
if (scope -> find(identifier) != scope -> end()){
|
if (scope -> find(identifier) != scope -> end()){
|
||||||
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
|
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
|
||||||
|
@ -79,7 +79,7 @@ VariableAssignment BoundScope::CreateExplicitLocal(int identifier, std::shared_p
|
||||||
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, this->_currentScope, true));
|
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, this->_currentScope, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
VariableAssignment BoundScope::AssignVariable(int identifier, const std::shared_ptr<ScriptType>& type) {
|
VariableAssignment BoundScope::AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType>& type) {
|
||||||
int exists = this->Exists(identifier);
|
int exists = this->Exists(identifier);
|
||||||
if (exists == -1){
|
if (exists == -1){
|
||||||
// Creation
|
// Creation
|
||||||
|
|
|
@ -14,21 +14,21 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class BoundScope {
|
class BoundScope {
|
||||||
unordered_map<int, BoundVariable*>* _tableScope;
|
unordered_map<uint32_t, BoundVariable*>* _tableScope;
|
||||||
vector<unordered_map<int, BoundVariable*>*> _localScope;
|
vector<unordered_map<uint32_t, BoundVariable*>*> _localScope;
|
||||||
int _currentScope;
|
int _currentScope;
|
||||||
int _lastCreatedScope;
|
int _lastCreatedScope;
|
||||||
public:
|
public:
|
||||||
explicit BoundScope(unordered_map<int, BoundVariable*> *tableScope);
|
explicit BoundScope(unordered_map<uint32_t, BoundVariable*> *tableScope);
|
||||||
~BoundScope();
|
~BoundScope();
|
||||||
|
|
||||||
void GoInnerScope();
|
void GoInnerScope();
|
||||||
void GoOuterScope();
|
void GoOuterScope();
|
||||||
|
|
||||||
int Exists(int key);
|
int Exists(int key);
|
||||||
BoundVariable* GetVariable(int scope, int identifier);
|
BoundVariable* GetVariable(uint32_t scope, uint32_t identifier);
|
||||||
VariableAssignment CreateExplicitLocal(int identifier, std::shared_ptr<ScriptType> type);
|
VariableAssignment CreateExplicitLocal(uint32_t identifier, std::shared_ptr<ScriptType> type);
|
||||||
VariableAssignment AssignVariable(int identifier, const std::shared_ptr<ScriptType>& type);
|
VariableAssignment AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType>& type);
|
||||||
|
|
||||||
size_t GetLocalVariableCount(){
|
size_t GetLocalVariableCount(){
|
||||||
return _localScope.size();
|
return _localScope.size();
|
||||||
|
|
|
@ -22,6 +22,7 @@ enum class DiagnosticCode{
|
||||||
InvalidReturnType,
|
InvalidReturnType,
|
||||||
ConditionNotABool,
|
ConditionNotABool,
|
||||||
InvalidTableValueType,
|
InvalidTableValueType,
|
||||||
|
InvalidTypeName,
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //PORYGONLANG_DIAGNOSTICCODE_HPP
|
#endif //PORYGONLANG_DIAGNOSTICCODE_HPP
|
||||||
|
|
|
@ -7,15 +7,15 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class TableEvalValue : public EvalValue {
|
class TableEvalValue : public EvalValue {
|
||||||
shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> _table;
|
shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>> _table;
|
||||||
size_t _hash;
|
size_t _hash;
|
||||||
|
|
||||||
explicit TableEvalValue(shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> table, size_t hash){
|
explicit TableEvalValue(shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>> table, size_t hash){
|
||||||
_table = std::move(table);
|
_table = std::move(table);
|
||||||
_hash = hash;
|
_hash = hash;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
explicit TableEvalValue(shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> table){
|
explicit TableEvalValue(shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>> table){
|
||||||
_table = std::move(table);
|
_table = std::move(table);
|
||||||
_hash = rand();
|
_hash = rand();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
#include "EvaluationScope.hpp"
|
#include "EvaluationScope.hpp"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
EvaluationScope::EvaluationScope(unordered_map<size_t, shared_ptr<EvalValue>> *scriptVariables, int localVariableCount) {
|
EvaluationScope::EvaluationScope(unordered_map<uint32_t, shared_ptr<EvalValue>> *scriptVariables, int localVariableCount) {
|
||||||
_scriptScope = scriptVariables;
|
_scriptScope = scriptVariables;
|
||||||
_localScope = unordered_map<uint64_t, shared_ptr<EvalValue>>(localVariableCount);
|
_localScope = unordered_map<uint64_t, shared_ptr<EvalValue>>(localVariableCount);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,10 @@
|
||||||
#include "../EvalValues/EvalValue.hpp"
|
#include "../EvalValues/EvalValue.hpp"
|
||||||
|
|
||||||
class EvaluationScope {
|
class EvaluationScope {
|
||||||
unordered_map<size_t, shared_ptr<EvalValue>>* _scriptScope;
|
unordered_map<uint32_t, shared_ptr<EvalValue>>* _scriptScope;
|
||||||
unordered_map<uint64_t, shared_ptr<EvalValue>> _localScope;
|
unordered_map<uint64_t, shared_ptr<EvalValue>> _localScope;
|
||||||
public:
|
public:
|
||||||
explicit EvaluationScope(unordered_map<size_t, shared_ptr<EvalValue>>* scriptVariables, int deepestScope);
|
explicit EvaluationScope(unordered_map<uint32_t, shared_ptr<EvalValue>>* scriptVariables, int deepestScope);
|
||||||
~EvaluationScope() = default;
|
~EvaluationScope() = default;
|
||||||
|
|
||||||
void CreateVariable(const BoundVariableKey* key, const shared_ptr<EvalValue>& value);
|
void CreateVariable(const BoundVariableKey* key, const shared_ptr<EvalValue>& value);
|
||||||
|
|
|
@ -279,12 +279,12 @@ shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *
|
||||||
shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
|
shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
|
||||||
auto tableExpression = (BoundNumericalTableExpression*)expression;
|
auto tableExpression = (BoundNumericalTableExpression*)expression;
|
||||||
auto valueExpressions = tableExpression->GetExpressions();
|
auto valueExpressions = tableExpression->GetExpressions();
|
||||||
auto values = new unordered_map<size_t, shared_ptr<EvalValue>>(valueExpressions->size());
|
auto values = new unordered_map<uint32_t, shared_ptr<EvalValue>>(valueExpressions->size());
|
||||||
for (int i = 0; i < valueExpressions->size(); i++){
|
for (int i = 0; i < valueExpressions->size(); i++){
|
||||||
auto val = this -> EvaluateExpression(valueExpressions -> at(i));
|
auto val = this -> EvaluateExpression(valueExpressions -> at(i));
|
||||||
values -> insert({i + 1, val});
|
values -> insert({i + 1, val});
|
||||||
}
|
}
|
||||||
auto valuesPointer = shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>>(values);
|
auto valuesPointer = shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>>(values);
|
||||||
return make_shared<TableEvalValue>(valuesPointer);
|
return make_shared<TableEvalValue>(valuesPointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -292,7 +292,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpre
|
||||||
auto tableExpression = (BoundTableExpression*)expression;
|
auto tableExpression = (BoundTableExpression*)expression;
|
||||||
auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType());
|
auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType());
|
||||||
auto declaredVars = type -> GetValues();
|
auto declaredVars = type -> GetValues();
|
||||||
auto variables = make_shared<unordered_map<size_t, shared_ptr<EvalValue>>>(declaredVars->size());
|
auto variables = make_shared<unordered_map<uint32_t, shared_ptr<EvalValue>>>(declaredVars->size());
|
||||||
for (auto i : *declaredVars){
|
for (auto i : *declaredVars){
|
||||||
variables->insert({i.first, nullptr});
|
variables->insert({i.first, nullptr});
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ Script::Script() {
|
||||||
Diagnostics = new DiagnosticsHolder();
|
Diagnostics = new DiagnosticsHolder();
|
||||||
_evaluator = new Evaluator(this);
|
_evaluator = new Evaluator(this);
|
||||||
_boundScript = nullptr;
|
_boundScript = nullptr;
|
||||||
_scriptVariables = new unordered_map<size_t, shared_ptr<EvalValue>>(0);
|
_scriptVariables = new unordered_map<uint32_t, shared_ptr<EvalValue>>(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
EvalValue* Script::Evaluate() {
|
EvalValue* Script::Evaluate() {
|
||||||
|
@ -42,7 +42,7 @@ void Script::Parse(const string& script) {
|
||||||
}
|
}
|
||||||
lexResult.clear();
|
lexResult.clear();
|
||||||
if (!Diagnostics->HasErrors()){
|
if (!Diagnostics->HasErrors()){
|
||||||
unordered_map<int, BoundVariable*> scriptScope;
|
unordered_map<uint32_t, BoundVariable*> scriptScope;
|
||||||
auto bindScope = new BoundScope(&scriptScope);
|
auto bindScope = new BoundScope(&scriptScope);
|
||||||
this->_boundScript = Binder::Bind(this, parseResult, bindScope);
|
this->_boundScript = Binder::Bind(this, parseResult, bindScope);
|
||||||
for (const auto& v : scriptScope){
|
for (const auto& v : scriptScope){
|
||||||
|
@ -72,9 +72,9 @@ bool Script::HasFunction(const string &key) {
|
||||||
return f != _scriptVariables->end() && f.operator->()->second->GetTypeClass() == TypeClass ::Function;
|
return f != _scriptVariables->end() && f.operator->()->second->GetTypeClass() == TypeClass ::Function;
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<EvalValue> Script::CallFunction(const string &key, vector<EvalValue *> variables) {
|
shared_ptr<EvalValue> Script::CallFunction(const string &key, const vector<EvalValue *>& variables) {
|
||||||
auto var = (ScriptFunctionEvalValue*)GetVariable(key);
|
auto var = (ScriptFunctionEvalValue*)GetVariable(key);
|
||||||
return this->_evaluator->EvaluateFunction(var, std::move(variables));
|
return this->_evaluator->EvaluateFunction(var, variables);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
|
@ -18,7 +18,7 @@ class Script {
|
||||||
friend class Evaluator;
|
friend class Evaluator;
|
||||||
|
|
||||||
Evaluator* _evaluator;
|
Evaluator* _evaluator;
|
||||||
unordered_map<size_t, shared_ptr<EvalValue>>* _scriptVariables;
|
unordered_map<uint32_t, shared_ptr<EvalValue>>* _scriptVariables;
|
||||||
BoundScriptStatement* _boundScript;
|
BoundScriptStatement* _boundScript;
|
||||||
shared_ptr<ScriptType> _returnType;
|
shared_ptr<ScriptType> _returnType;
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ public:
|
||||||
EvalValue* GetVariable(const string& key);
|
EvalValue* GetVariable(const string& key);
|
||||||
bool HasVariable(const string& key);
|
bool HasVariable(const string& key);
|
||||||
|
|
||||||
shared_ptr<EvalValue> CallFunction(const string& key, vector<EvalValue*> variables);
|
shared_ptr<EvalValue> CallFunction(const string& key, const vector<EvalValue*>& variables);
|
||||||
bool HasFunction(const string& key);
|
bool HasFunction(const string& key);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -11,3 +11,17 @@ shared_ptr<ScriptType> ScriptType::GetIndexedType(ScriptType *indexer) {
|
||||||
}
|
}
|
||||||
return make_shared<ScriptType>(TypeClass::Error);
|
return make_shared<ScriptType>(TypeClass::Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C"{
|
||||||
|
ScriptType* CreateScriptType(TypeClass t){
|
||||||
|
return new ScriptType(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
ScriptType* CreateNumericScriptType(bool isAware, bool isFloat){
|
||||||
|
return new NumericScriptType(isAware, isFloat);
|
||||||
|
}
|
||||||
|
|
||||||
|
ScriptType* CreateStringScriptType(bool knownAtBind, uint32_t hash){
|
||||||
|
return new StringScriptType(knownAtBind, hash);
|
||||||
|
}
|
||||||
|
}
|
|
@ -76,9 +76,9 @@ public:
|
||||||
|
|
||||||
class StringScriptType : public ScriptType{
|
class StringScriptType : public ScriptType{
|
||||||
bool _isKnownAtBind;
|
bool _isKnownAtBind;
|
||||||
int _hashValue;
|
uint32_t _hashValue;
|
||||||
public:
|
public:
|
||||||
explicit StringScriptType(bool knownAtBind, int hashValue): ScriptType(TypeClass::String){
|
explicit StringScriptType(bool knownAtBind, uint32_t hashValue): ScriptType(TypeClass::String){
|
||||||
_isKnownAtBind = knownAtBind;
|
_isKnownAtBind = knownAtBind;
|
||||||
_hashValue = hashValue;
|
_hashValue = hashValue;
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ public:
|
||||||
return _isKnownAtBind;
|
return _isKnownAtBind;
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetHashValue(){
|
uint32_t GetHashValue(){
|
||||||
return _hashValue;
|
return _hashValue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,10 +5,10 @@
|
||||||
#include "Binder/BoundVariables/BoundVariable.hpp"
|
#include "Binder/BoundVariables/BoundVariable.hpp"
|
||||||
|
|
||||||
class TableScriptType : public ScriptType{
|
class TableScriptType : public ScriptType{
|
||||||
const unordered_map<int, BoundVariable*>* _values;
|
const unordered_map<uint32_t, BoundVariable*>* _values;
|
||||||
const int _localVariableCount;
|
const int _localVariableCount;
|
||||||
public:
|
public:
|
||||||
explicit TableScriptType(unordered_map<int, BoundVariable*>* values, int localVariableCount)
|
explicit TableScriptType(unordered_map<uint32_t, BoundVariable*>* values, int localVariableCount)
|
||||||
: ScriptType(TypeClass::Table),
|
: ScriptType(TypeClass::Table),
|
||||||
_values(values),
|
_values(values),
|
||||||
_localVariableCount(localVariableCount)
|
_localVariableCount(localVariableCount)
|
||||||
|
@ -33,7 +33,7 @@ public:
|
||||||
throw "TODO: indexing with dynamic keys";
|
throw "TODO: indexing with dynamic keys";
|
||||||
}
|
}
|
||||||
|
|
||||||
const unordered_map<int, BoundVariable*>* GetValues(){
|
const unordered_map<uint32_t, BoundVariable*>* GetValues(){
|
||||||
return _values;
|
return _values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,20 @@
|
||||||
|
|
||||||
#include "UserData.hpp"
|
#include "UserData.hpp"
|
||||||
|
#include "UserDataStorage.hpp"
|
||||||
|
|
||||||
|
extern "C"{
|
||||||
|
void RegisterUserDataType(uint32_t id){
|
||||||
|
auto ud = new UserData({});
|
||||||
|
UserDataStorage::RegisterType(id, ud);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RegisterUserDataField(uint32_t typeId, uint32_t fieldId, UserDataField* field){
|
||||||
|
auto ud = UserDataStorage::GetUserDataType(typeId);
|
||||||
|
ud -> CreateField(fieldId, field);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t GetUserDataFieldCount(uint32_t typeId){
|
||||||
|
auto ud = UserDataStorage::GetUserDataType(typeId);
|
||||||
|
return ud ->GetFieldCount();
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,19 +6,27 @@
|
||||||
#include "UserDataField.hpp"
|
#include "UserDataField.hpp"
|
||||||
|
|
||||||
class UserData {
|
class UserData {
|
||||||
std::unordered_map<int, UserDataField*> _fields;
|
std::unordered_map<uint32_t, UserDataField*> _fields;
|
||||||
public:
|
public:
|
||||||
explicit UserData(std::unordered_map<int, UserDataField*> fields){
|
explicit UserData(std::unordered_map<uint32_t, UserDataField*> fields){
|
||||||
_fields = std::move(fields);
|
_fields = std::move(fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ContainsField(int fieldId){
|
bool ContainsField(uint32_t fieldId){
|
||||||
return _fields.find(fieldId) != _fields.end();
|
return _fields.find(fieldId) != _fields.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
UserDataField* GetField(int fieldId){
|
UserDataField* GetField(uint32_t fieldId){
|
||||||
return _fields[fieldId];
|
return _fields[fieldId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CreateField(uint32_t fieldId, UserDataField* field){
|
||||||
|
_fields.insert({fieldId, field});
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t GetFieldCount(){
|
||||||
|
return _fields.size();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,8 @@
|
||||||
|
|
||||||
#include "UserDataField.hpp"
|
#include "UserDataField.hpp"
|
||||||
|
|
||||||
|
extern "C"{
|
||||||
|
UserDataField* CreateUserDataField(ScriptType* type, EvalValue* (*getter)(void* obj), void (*setter)(void* obj, EvalValue* val)){
|
||||||
|
return new UserDataField(type, getter, setter);
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
|
|
||||||
#include "../Evaluator/EvalValues/EvalValue.hpp"
|
#include "../Evaluator/EvalValues/EvalValue.hpp"
|
||||||
|
#include "../Evaluator/EvalValues/NumericEvalValue.hpp"
|
||||||
|
|
||||||
class UserDataField {
|
class UserDataField {
|
||||||
shared_ptr<ScriptType> _type;
|
shared_ptr<ScriptType> _type;
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
|
|
||||||
|
|
||||||
#ifndef PORYGONLANG_USERDATASCRIPTTYPE_HPP
|
#ifndef PORYGONLANG_USERDATASCRIPTTYPE_HPP
|
||||||
#define PORYGONLANG_USERDATASCRIPTTYPE_HPP
|
#define PORYGONLANG_USERDATASCRIPTTYPE_HPP
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
#include "../ScriptType.hpp"
|
#include "../ScriptType.hpp"
|
||||||
#include "UserData.hpp"
|
#include "UserData.hpp"
|
||||||
#include "UserDataStorage.hpp"
|
#include "UserDataStorage.hpp"
|
||||||
|
@ -10,12 +11,21 @@
|
||||||
class UserDataScriptType : public ScriptType{
|
class UserDataScriptType : public ScriptType{
|
||||||
shared_ptr<UserData> _userData;
|
shared_ptr<UserData> _userData;
|
||||||
public:
|
public:
|
||||||
explicit UserDataScriptType(int id) : ScriptType(TypeClass::UserData){
|
explicit UserDataScriptType(uint32_t id) : ScriptType(TypeClass::UserData){
|
||||||
_userData = UserDataStorage::GetUserDataType(id);
|
_userData = UserDataStorage::GetUserDataType(id);
|
||||||
}
|
}
|
||||||
|
explicit UserDataScriptType(shared_ptr<UserData> ud) : ScriptType(TypeClass::UserData){
|
||||||
|
_userData = std::move(ud);
|
||||||
|
}
|
||||||
|
|
||||||
bool CanBeIndexedWith(ScriptType* indexer) final{
|
bool CanBeIndexedWith(ScriptType* indexer) final{
|
||||||
return indexer->GetClass() == TypeClass ::String;
|
if (indexer->GetClass() != TypeClass ::String){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
auto str = (StringScriptType*)indexer;
|
||||||
|
if (!str->IsKnownAtBind())
|
||||||
|
return false;
|
||||||
|
return _userData->ContainsField(str->GetHashValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) final{
|
shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) final{
|
||||||
|
|
|
@ -8,16 +8,20 @@
|
||||||
class UserDataStorage {
|
class UserDataStorage {
|
||||||
class _internalDataStorage{
|
class _internalDataStorage{
|
||||||
public:
|
public:
|
||||||
std::unordered_map<int, shared_ptr<UserData>> _userData;
|
std::unordered_map<uint32_t, shared_ptr<UserData>> _userData;
|
||||||
};
|
};
|
||||||
static _internalDataStorage _internal;
|
static _internalDataStorage _internal;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void RegisterType(int i, UserData* ud){
|
static void RegisterType(uint32_t i, UserData* ud){
|
||||||
UserDataStorage::_internal._userData.insert({i, shared_ptr<UserData>(ud)});
|
UserDataStorage::_internal._userData.insert({i, shared_ptr<UserData>(ud)});
|
||||||
}
|
}
|
||||||
|
|
||||||
static shared_ptr<UserData> GetUserDataType(int i){
|
static bool HasUserDataType(uint32_t i){
|
||||||
|
return UserDataStorage::_internal._userData.find(i) != UserDataStorage::_internal._userData.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
static shared_ptr<UserData> GetUserDataType(uint32_t i){
|
||||||
return UserDataStorage::_internal._userData[i];
|
return UserDataStorage::_internal._userData[i];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,2 +1,8 @@
|
||||||
|
|
||||||
#include "UserDataValue.hpp"
|
#include "UserDataValue.hpp"
|
||||||
|
|
||||||
|
extern "C"{
|
||||||
|
UserDataValue* CreateUserDataEvalValue(uint32_t typeHash, void* obj){
|
||||||
|
return new UserDataValue(typeHash, obj);
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,7 +17,7 @@ public:
|
||||||
_obj = obj;
|
_obj = obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
UserDataValue(int userDataId, void* obj){
|
UserDataValue(uint32_t userDataId, void* obj){
|
||||||
_userData = UserDataStorage::GetUserDataType(userDataId);
|
_userData = UserDataStorage::GetUserDataType(userDataId);
|
||||||
_obj = obj;
|
_obj = obj;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class HashedString{
|
class HashedString{
|
||||||
const int _hash;
|
const uint32_t _hash;
|
||||||
public:
|
public:
|
||||||
explicit HashedString(const std::string& s) : _hash(ConstHash(s.c_str())){
|
explicit HashedString(const std::string& s) : _hash(ConstHash(s.c_str())){
|
||||||
}
|
}
|
||||||
|
@ -13,13 +13,13 @@ public:
|
||||||
}
|
}
|
||||||
HashedString(const HashedString& b) = default;
|
HashedString(const HashedString& b) = default;
|
||||||
|
|
||||||
static unsigned constexpr ConstHash(char const *input) {
|
static uint32_t constexpr ConstHash(char const *input) {
|
||||||
return *input ?
|
return *input ?
|
||||||
static_cast<unsigned int>(*input) + 33 * ConstHash(input + 1) :
|
static_cast<uint32_t>(*input) + 33 * ConstHash(input + 1) :
|
||||||
5381;
|
5381;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int GetHash() const{
|
const uint32_t GetHash() const{
|
||||||
return _hash;
|
return _hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue