From 6b03a758d209540bf50339eb2dff776e3d03cfe2 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Mon, 21 Nov 2022 17:43:39 -0500 Subject: [PATCH] Refactored foreign method binding to be completely handled by the WrenState object --- src/scripting/coreAPI.cpp | 23 ++--------------- src/scripting/coreAPI.h | 20 ++------------- .../internal_scripts/world_interface.wren | 4 +-- src/scripting/wren_state.cpp | 22 ++++++++-------- src/scripting/wren_state.h | 25 ++++++++++++++++--- src/world/world.h | 3 +++ 6 files changed, 40 insertions(+), 57 deletions(-) diff --git a/src/scripting/coreAPI.cpp b/src/scripting/coreAPI.cpp index 3c3ea58..4cf168e 100644 --- a/src/scripting/coreAPI.cpp +++ b/src/scripting/coreAPI.cpp @@ -19,7 +19,6 @@ namespace lunarium { CoreAPI* CoreAPI::mpInstance = nullptr; WrenScript CoreAPI::mScript; - std::vector 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(); @@ -64,22 +61,6 @@ namespace lunarium // return OpRes::Fail("CoreAPI::Initialize not implemented yet!"); 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() { diff --git a/src/scripting/coreAPI.h b/src/scripting/coreAPI.h index bf019e5..e2df8ee 100644 --- a/src/scripting/coreAPI.h +++ b/src/scripting/coreAPI.h @@ -36,29 +36,13 @@ namespace lunarium static CoreAPI* mpInstance; 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 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 diff --git a/src/scripting/internal_scripts/world_interface.wren b/src/scripting/internal_scripts/world_interface.wren index cb2433d..f6c0939 100644 --- a/src/scripting/internal_scripts/world_interface.wren +++ b/src/scripting/internal_scripts/world_interface.wren @@ -55,9 +55,7 @@ class WorldInterface { // API ///////////////////////////////////////////////////////////////////// - static GetComponent(entity_id, component_id) { - - } + //foreign static GetComponent(entity_id, component_id) } diff --git a/src/scripting/wren_state.cpp b/src/scripting/wren_state.cpp index 68f6dce..2c0ef7e 100644 --- a/src/scripting/wren_state.cpp +++ b/src/scripting/wren_state.cpp @@ -15,7 +15,7 @@ namespace lunarium { - std::vector WrenState::mFMBinders; + std::vector 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; } } \ No newline at end of file diff --git a/src/scripting/wren_state.h b/src/scripting/wren_state.h index ccda63b..f02fefb 100644 --- a/src/scripting/wren_state.h +++ b/src/scripting/wren_state.h @@ -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 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 mFMBinders; + static std::vector mForeignMethods; }; } diff --git a/src/world/world.h b/src/world/world.h index b1e2796..f448960 100644 --- a/src/world/world.h +++ b/src/world/world.h @@ -133,6 +133,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();