Work on extern support for userdata functions
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-06-21 23:07:17 +02:00
parent 95c322ed2c
commit 694b0ac0c0
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
10 changed files with 42 additions and 12 deletions

View File

@ -2,6 +2,7 @@
#include "NumericEvalValue.hpp" #include "NumericEvalValue.hpp"
#include "StringEvalValue.hpp" #include "StringEvalValue.hpp"
#include <cstring> #include <cstring>
#include <memory>
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
@ -26,6 +27,7 @@ namespace Porygon::Evaluation {
return v->EvaluateString()->c_str(); return v->EvaluateString()->c_str();
} }
EvalValue *CreateIntegerEvalValue(long l) { EvalValue *CreateIntegerEvalValue(long l) {
return new IntegerEvalValue(l); return new IntegerEvalValue(l);
} }

View File

@ -1,6 +1,3 @@
#include <utility>
#include <utility>
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP #ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP #define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
@ -10,7 +7,6 @@
#include "../../ScriptType.hpp" #include "../../ScriptType.hpp"
#include "EvalValue.hpp" #include "EvalValue.hpp"
#include "../../Binder/BoundStatements/BoundStatement.hpp" #include "../../Binder/BoundStatements/BoundStatement.hpp"
#include "../Evaluator.hpp"
#include "../EvaluationScope/EvaluationScope.hpp" #include "../EvaluationScope/EvaluationScope.hpp"
using namespace std; using namespace std;

View File

@ -285,8 +285,8 @@ namespace Porygon::Evaluation {
} }
auto type = std::dynamic_pointer_cast<GenericFunctionScriptType>(function->GetType()); auto type = std::dynamic_pointer_cast<GenericFunctionScriptType>(function->GetType());
auto parameterTypes = type->GetParameterTypes();
if (type -> IsScriptFunction()){ if (type -> IsScriptFunction()){
auto parameterTypes = type->GetParameterTypes();
auto scriptFunctionType = std::dynamic_pointer_cast<FunctionScriptType>(type); auto scriptFunctionType = std::dynamic_pointer_cast<FunctionScriptType>(type);
auto parameterKeys = scriptFunctionType->GetParameterKeys(); auto parameterKeys = scriptFunctionType->GetParameterKeys();
auto originalScope = this->_evaluationScope; auto originalScope = this->_evaluationScope;

View File

@ -1,4 +1,5 @@
#include "Script.hpp" #include "Script.hpp"
#include "UserData/UserDataFunctionType.hpp"
namespace Porygon{ namespace Porygon{
const bool ScriptType::CanBeIndexedWith(ScriptType *indexer) const{ const bool ScriptType::CanBeIndexedWith(ScriptType *indexer) const{
@ -25,6 +26,14 @@ namespace Porygon{
ScriptType* CreateStringScriptType(bool knownAtBind, uint32_t hash){ ScriptType* CreateStringScriptType(bool knownAtBind, uint32_t hash){
return new StringScriptType(knownAtBind, hash); return new StringScriptType(knownAtBind, hash);
} }
ScriptType* CreateUserDataFunctionScriptType(ScriptType* returnType, ScriptType* parameters[], size_t parameterCount){
vector<shared_ptr<ScriptType>> vector(parameterCount);
for (int i = 0; i < parameterCount; i++){
vector[i] = shared_ptr<ScriptType>(parameters[i]);
}
return new UserData::UserDataFunctionType(shared_ptr<ScriptType>(returnType), vector);
}
} }
} }

View File

@ -1,2 +1,12 @@
#include "UserDataFunction.hpp" #include "UserDataFunction.hpp"
using namespace Porygon::Evaluation;
namespace Porygon::UserData{
extern "C" {
EvalValue * CreateFunctionEvalValue(Evaluation::EvalValue* (*func)(void* , EvalValue* [], int ), void* obj) {
return new UserDataFunction(func, obj);
}
}
}

View File

@ -3,6 +3,7 @@
#include <utility> #include <utility>
#include "../Evaluator/EvalValues/ScriptFunctionEvalValue.hpp" #include "../Evaluator/EvalValues/ScriptFunctionEvalValue.hpp"
#include "UserDataFunctionType.hpp"
namespace Porygon::UserData{ namespace Porygon::UserData{
class UserDataFunction : public Evaluation::GenericFunctionEvalValue { class UserDataFunction : public Evaluation::GenericFunctionEvalValue {
@ -15,14 +16,18 @@ namespace Porygon::UserData{
_obj = obj; _obj = obj;
} }
public: public:
UserDataFunction(Evaluation::EvalValue* (*call)(void* obj, EvalValue* parameters[], int parameterCount), void* obj, UserDataFunction(Evaluation::EvalValue* (*call)(void* obj, EvalValue* parameters[], int parameterCount), void* obj) :
shared_ptr<GenericFunctionScriptType> type) : GenericFunctionEvalValue(std::move(type), rand()){ GenericFunctionEvalValue(make_shared<UserDataFunctionType>(nullptr, vector<shared_ptr<ScriptType>>{}), rand()){
_call = call; _call = call;
_obj = obj; _obj = obj;
} }
EvalValue* Call(EvalValue* parameters[], int parameterCount){ EvalValue* Call(EvalValue* parameters[], int parameterCount){
try{
return _call(_obj, parameters, parameterCount); return _call(_obj, parameters, parameterCount);
} catch (...){
throw Evaluation::EvaluationException("An error occurred while executing a userdata function.");
}
} }
const shared_ptr<EvalValue> Clone() const final { const shared_ptr<EvalValue> Clone() const final {

View File

@ -31,7 +31,7 @@ namespace Porygon::UserData {
} }
const bool CanBeIndexedWithIdentifier(uint32_t hash) const final { const bool CanBeIndexedWithIdentifier(uint32_t hash) const final {
return true; return _userData -> ContainsField(hash);
} }
UserDataField *GetField(uint32_t id) { UserDataField *GetField(uint32_t id) {

View File

@ -6,5 +6,10 @@ namespace Porygon::UserData {
UserDataValue *CreateUserDataEvalValue(uint32_t typeHash, void *obj) { UserDataValue *CreateUserDataEvalValue(uint32_t typeHash, void *obj) {
return new UserDataValue(typeHash, obj); return new UserDataValue(typeHash, obj);
} }
void* EvaluateUserDataObj(UserDataValue *v){
return v -> GetObjectPointer();
}
} }
} }

View File

@ -56,6 +56,10 @@ namespace Porygon::UserData {
auto field = _userData->GetField(fieldId); auto field = _userData->GetField(fieldId);
field->Set(_obj, value.get()); field->Set(_obj, value.get());
} }
void* GetObjectPointer(){
return _obj;
}
}; };
} }

View File

@ -37,8 +37,7 @@ private:
} }
static EvalValue* GetFooFunction(void* obj){ static EvalValue* GetFooFunction(void* obj){
return new UserDataFunction(CallFooFunction, obj, return new UserDataFunction(CallFooFunction, obj);
make_shared<UserDataFunctionType>(make_shared<NumericScriptType>(true, false), vector<shared_ptr<ScriptType>>(0)));
} }
static EvalValue* CallAddition(void* obj, EvalValue* parameters[], int parameterCount){ static EvalValue* CallAddition(void* obj, EvalValue* parameters[], int parameterCount){
@ -51,7 +50,7 @@ private:
static GenericFunctionScriptType* AdditionFunctionType; static GenericFunctionScriptType* AdditionFunctionType;
static EvalValue* GetAdditionFunction(void* obj){ static EvalValue* GetAdditionFunction(void* obj){
return new UserDataFunction(CallAddition, obj, shared_ptr<GenericFunctionScriptType>(AdditionFunctionType)); return new UserDataFunction(CallAddition, obj);
} }
public: public: