Refactored foreign method binding to be completely handled by the WrenState object

master
Joey Pollack 3 years ago
parent df9eb0a4f7
commit 6b03a758d2

@ -19,7 +19,6 @@ namespace lunarium
{
CoreAPI* CoreAPI::mpInstance = nullptr;
WrenScript CoreAPI::mScript;
std::vector<CoreAPI::ForeignMethodID> CoreAPI::mForeignMethods;
CoreAPI& CoreAPI::GetInstance()
{
if (mpInstance == nullptr)
@ -39,11 +38,9 @@ namespace lunarium
OpRes CoreAPI::Initialize(WrenState& sman)
{
mCat = sman.GetLogCat();
// Initialize Foreign methods
mForeignMethods.push_back({"CoreAPI", "Core", true, "IsKeyDown(_)", (ForeignMethod)&CoreAPI::IsKeyDown});
// Register callbacks
sman.RegisterForeignMethodBinder((FMBinder*)&CoreAPI::ForeignMethodBinder);
// Register Methods
sman.RegisterForeignMethod({"CoreAPI", "Core", true, "IsKeyDown(_)", (ForeignMethod)&CoreAPI::IsKeyDown});
// Generate Code
std::string keys_script = GenerateWrenKeyCodes();
@ -65,22 +62,6 @@ namespace lunarium
return OpRes::OK();
}
WrenForeignMethodFn CoreAPI::ForeignMethodBinder(WrenVM* vm, const char* module, const char* class_name, bool isStatic, const char* signature)
{
ForeignMethodID FMID = { module, class_name, isStatic, signature };
for (auto iter = mForeignMethods.begin(); iter != mForeignMethods.end(); iter++)
{
if (FMID == (*iter))
{
return iter->FM;
}
}
return nullptr;
}
std::string CoreAPI::GenerateWrenKeyCodes()
{
std::string key_codes = "class KeyCodes {\n";

@ -37,28 +37,12 @@ namespace lunarium
static WrenScript mScript;
u32 mCat;
typedef void (*ForeignMethod)(WrenVM*);
struct ForeignMethodID
{
std::string Module;
std::string Class;
bool IsStatic;
std::string Signature;
ForeignMethod FM;
bool operator==(const ForeignMethodID& rhs)
{
return (Module == rhs.Module && Class == rhs.Class && IsStatic == rhs.IsStatic && Signature == rhs.Signature);
}
};
static std::vector<ForeignMethodID> mForeignMethods;
private: // Helper methods
std::string GenerateWrenKeyCodes();
private: // Callbacks
static ForeignMethod ForeignMethodBinder(WrenVM* vm, const char* module, const char* className, bool isStatic, const char* signature);
//static ForeignMethod ForeignMethodBinder(WrenVM* vm, const char* module, const char* className, bool isStatic, const char* signature);
public: // API

@ -55,9 +55,7 @@ class WorldInterface {
// API
/////////////////////////////////////////////////////////////////////
static GetComponent(entity_id, component_id) {
}
//foreign static GetComponent(entity_id, component_id)
}

@ -15,7 +15,7 @@
namespace lunarium
{
std::vector<FMBinder*> WrenState::mFMBinders;
std::vector<WrenState::ForeignMethodDesc> WrenState::mForeignMethods;
WrenState::~WrenState()
{
@ -49,9 +49,9 @@ namespace lunarium
return mLogCat;
}
void WrenState::RegisterForeignMethodBinder(FMBinder* binder_method)
void WrenState::RegisterForeignMethod(ForeignMethodDesc method_desc)
{
mFMBinders.push_back(binder_method);
mForeignMethods.push_back(method_desc);
}
/////////////////////////////////////////////////////////////////////
@ -177,21 +177,19 @@ namespace lunarium
}
WrenForeignMethodFn WrenState::BindForeignMethodFN(WrenVM* vm, const char* module, const char* className, bool isStatic, const char* signature)
WrenForeignMethodFn WrenState::BindForeignMethodFN(WrenVM* vm, const char* module, const char* class_name, bool isStatic, const char* signature)
{
// Bind methods
ForeignMethodDesc FMID = { module, class_name, isStatic, signature };
// If method not recognized, call any registered FMBinder methods
for (auto iter = mFMBinders.begin(); iter != mFMBinders.end(); iter++)
for (auto iter = mForeignMethods.begin(); iter != mForeignMethods.end(); iter++)
{
auto FN = ((FMBinder)(*iter))(vm, module, className, isStatic, signature);
if (FN)
if (FMID == (*iter))
{
return FN;
return iter->FM;
}
}
Logger::Error(mLogCat, "Failed to bind foreign method: %s::%s", className, signature);
Logger::Error(mLogCat, "Failed to bind foreign method: %s::%s", class_name, signature);
return nullptr;
}
}

@ -42,17 +42,33 @@ namespace lunarium
class WrenScript;
typedef WrenForeignMethodFn (*FMBinder) (WrenVM*, const char*, const char*, bool, const char*);
typedef WrenForeignMethodFn ForeignMethod;
class WrenState
{
public:
struct ForeignMethodDesc
{
std::string Module;
std::string Class;
bool IsStatic;
std::string Signature;
ForeignMethod FM;
bool operator==(const ForeignMethodDesc& rhs)
{
return (Module == rhs.Module && Class == rhs.Class && IsStatic == rhs.IsStatic && Signature == rhs.Signature);
}
};
public:
~WrenState();
OpRes Initialize();
void Shutdown();
u32 GetLogCat() const;
void RegisterForeignMethodBinder(FMBinder* binder_method);
void RegisterForeignMethod(ForeignMethodDesc method_desc);
void RunScript(WrenScript* script);
void RunSnippet(std::string name, std::string code);
@ -62,6 +78,9 @@ namespace lunarium
void CallWrenMethod(WrenHandle* method, WrenHandle* receiver, std::vector<WrenParameter> params, std::string name_tag);
void ReleaseWrenHandle(WrenHandle* handle);
public:
// CALL BACKS
static void WriteFN(WrenVM* vm, const char* text);
static void ErrorFN(WrenVM* vm, WrenErrorType type, const char* module, int line, const char* message);
@ -72,7 +91,7 @@ namespace lunarium
private:
static u32 mLogCat;
WrenVM* mpVM;
static std::vector<FMBinder*> mFMBinders;
static std::vector<ForeignMethodDesc> mForeignMethods;
};
}

@ -134,6 +134,9 @@ namespace lunarium
// TEST STUFF
//bool mGetAssetsFromEditor; // This is for testing until we get a proper asset manager
private: // SCRIPTING API
static void GetComponent(WrenVM* vm);
private: // HELPERS
void InitScriptState();
void RenderEditor(lunarium::Renderer2D* pGraphics) const;

Loading…
Cancel
Save