From 05f26fcfdce37e4bc5bdb28c3ac3faf8cfd147fc Mon Sep 17 00:00:00 2001 From: Patrix Date: Mon, 5 Sep 2022 20:28:37 +0200 Subject: [PATCH] Updated squirrel & sqrat dependencies --- dependencies/sqrat/include/sqrat/sqratClass.h | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/dependencies/sqrat/include/sqrat/sqratClass.h b/dependencies/sqrat/include/sqrat/sqratClass.h index c66fea5..ef7febf 100644 --- a/dependencies/sqrat/include/sqrat/sqratClass.h +++ b/dependencies/sqrat/include/sqrat/sqratClass.h @@ -295,6 +295,45 @@ public: return *this; } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Binds a Squirrel function as defined by the Squirrel documentation as a class property + /// + /// \param name Name of the variable as it will appear in Squirrel + /// \param getter Getter for the variable + /// \param setter Setter for the variable + /// + /// \return The Class itself so the call can be chained + /// + /// \remarks + /// This method binds a squirrel setter and getter functions in C++ to Squirrel as if it is a class variable. + /// Inside of the function, the class instance the function was called with will be at index 1 on the stack. + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Class& SquirrelProp(const SQChar* name, SQFUNCTION getter, SQFUNCTION setter) + { + ClassData* cd = ClassType::getClassData(vm); + + if (getter != NULL) { + // Add the getter + sq_pushobject(vm, cd->getTable); + sq_pushstring(vm, name, -1); + sq_newclosure(vm, getter, 0); + sq_newslot(vm, -3, false); + sq_pop(vm, 1); + } + + if (setter != NULL) { + // Add the setter + sq_pushobject(vm, cd->setTable); + sq_pushstring(vm, name, -1); + sq_newclosure(vm, setter, 0); + sq_newslot(vm, -3, false); + sq_pop(vm, 1); + } + + return *this; + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Binds a class property (using global functions instead of member functions) /// @@ -350,6 +389,30 @@ public: return *this; } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Binds a Squirrel function as defined by the Squirrel documentation as a read-only class property + /// + /// \param name Name of the variable as it will appear in Squirrel + /// \param getter Getter for the variable + /// + /// \return The Class itself so the call can be chained + /// + /// \remarks + /// This method binds a squirrel getter function in C++ to Squirrel as if it is a class variable. + /// Inside of the function, the class instance the function was called with will be at index 1 on the stack. + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Class& SquirrelProp(const SQChar* name, SQFUNCTION getter) + { + sq_pushobject(vm, ClassType::getClassData(vm)->getTable); + sq_pushstring(vm, name, -1); + sq_newclosure(vm, getter, 0); + sq_newslot(vm, -3, false); + sq_pop(vm, 1); + + return *this; + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Binds a read-only class property (using a global function instead of a member function) ///