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; CoreAPI* CoreAPI::mpInstance = nullptr;
WrenScript CoreAPI::mScript; WrenScript CoreAPI::mScript;
std::vector<CoreAPI::ForeignMethodID> CoreAPI::mForeignMethods;
CoreAPI& CoreAPI::GetInstance() CoreAPI& CoreAPI::GetInstance()
{ {
if (mpInstance == nullptr) if (mpInstance == nullptr)
@ -39,11 +38,9 @@ namespace lunarium
OpRes CoreAPI::Initialize(WrenState& sman) OpRes CoreAPI::Initialize(WrenState& sman)
{ {
mCat = sman.GetLogCat(); mCat = sman.GetLogCat();
// Initialize Foreign methods
mForeignMethods.push_back({"CoreAPI", "Core", true, "IsKeyDown(_)", (ForeignMethod)&CoreAPI::IsKeyDown});
// Register callbacks // Register Methods
sman.RegisterForeignMethodBinder((FMBinder*)&CoreAPI::ForeignMethodBinder); sman.RegisterForeignMethod({"CoreAPI", "Core", true, "IsKeyDown(_)", (ForeignMethod)&CoreAPI::IsKeyDown});
// Generate Code // Generate Code
std::string keys_script = GenerateWrenKeyCodes(); std::string keys_script = GenerateWrenKeyCodes();
@ -65,22 +62,6 @@ namespace lunarium
return OpRes::OK(); 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 CoreAPI::GenerateWrenKeyCodes()
{ {
std::string key_codes = "class KeyCodes {\n"; std::string key_codes = "class KeyCodes {\n";

@ -37,28 +37,12 @@ namespace lunarium
static WrenScript mScript; static WrenScript mScript;
u32 mCat; 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 private: // Helper methods
std::string GenerateWrenKeyCodes(); std::string GenerateWrenKeyCodes();
private: // Callbacks 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 public: // API

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

@ -15,7 +15,7 @@
namespace lunarium namespace lunarium
{ {
std::vector<FMBinder*> WrenState::mFMBinders; std::vector<WrenState::ForeignMethodDesc> WrenState::mForeignMethods;
WrenState::~WrenState() WrenState::~WrenState()
{ {
@ -49,9 +49,9 @@ namespace lunarium
return mLogCat; 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 = mForeignMethods.begin(); iter != mForeignMethods.end(); iter++)
for (auto iter = mFMBinders.begin(); iter != mFMBinders.end(); iter++)
{ {
auto FN = ((FMBinder)(*iter))(vm, module, className, isStatic, signature); if (FMID == (*iter))
if (FN)
{ {
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; return nullptr;
} }
} }

@ -42,17 +42,33 @@ namespace lunarium
class WrenScript; class WrenScript;
typedef WrenForeignMethodFn (*FMBinder) (WrenVM*, const char*, const char*, bool, const char*); typedef WrenForeignMethodFn ForeignMethod;
class WrenState 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: public:
~WrenState(); ~WrenState();
OpRes Initialize(); OpRes Initialize();
void Shutdown(); void Shutdown();
u32 GetLogCat() const; u32 GetLogCat() const;
void RegisterForeignMethodBinder(FMBinder* binder_method); void RegisterForeignMethod(ForeignMethodDesc method_desc);
void RunScript(WrenScript* script); void RunScript(WrenScript* script);
void RunSnippet(std::string name, std::string code); 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 CallWrenMethod(WrenHandle* method, WrenHandle* receiver, std::vector<WrenParameter> params, std::string name_tag);
void ReleaseWrenHandle(WrenHandle* handle); void ReleaseWrenHandle(WrenHandle* handle);
public:
// CALL BACKS // CALL BACKS
static void WriteFN(WrenVM* vm, const char* text); static void WriteFN(WrenVM* vm, const char* text);
static void ErrorFN(WrenVM* vm, WrenErrorType type, const char* module, int line, const char* message); static void ErrorFN(WrenVM* vm, WrenErrorType type, const char* module, int line, const char* message);
@ -72,7 +91,7 @@ namespace lunarium
private: private:
static u32 mLogCat; static u32 mLogCat;
WrenVM* mpVM; WrenVM* mpVM;
static std::vector<FMBinder*> mFMBinders; static std::vector<ForeignMethodDesc> mForeignMethods;
}; };
} }

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

Loading…
Cancel
Save