diff --git a/sqrat/sqrat/sqratArray.h b/sqrat/sqrat/sqratArray.h index 02149410..5fbd214f 100644 --- a/sqrat/sqrat/sqratArray.h +++ b/sqrat/sqrat/sqratArray.h @@ -152,6 +152,7 @@ public: /// /// \param index The index in the array being assigned a function /// \param func Squirrel function that is being placed in the Array + /// \param name The name to associate with the function. /// /// \return The Array itself so the call can be chained /// @@ -167,6 +168,31 @@ public: return *this; } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Binds a raw Squirrel closure to the Array + /// + /// \param index The index in the array being assigned a function + /// \param func Squirrel function that is being placed in the Array + /// \param name The name to associate with the function. + /// \param pnum Number of parameters the function expects. + /// \param mask Types of parameters the function expects. + /// + /// \return The Array itself so the call can be chained + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ArrayBase& SquirrelFunc(const SQInteger index, SQFUNCTION func, SQInteger pnum, const SQChar * mask, const SQChar* name = nullptr) { + sq_pushobject(vm, GetObject()); + sq_pushinteger(vm, index); + sq_newclosure(vm, func, 0); + // Set the closure name (for debug purposes) + if (name) sq_setnativeclosurename(vm, -1, name); + // Set parameter validation + sq_setparamscheck(vm, pnum, mask); + sq_set(vm, -3); + sq_pop(vm,1); // pop array + return *this; + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Sets an index in the Array to a specific value /// diff --git a/sqrat/sqrat/sqratClass.h b/sqrat/sqrat/sqratClass.h index dfa8f0a4..5de499d5 100644 --- a/sqrat/sqrat/sqratClass.h +++ b/sqrat/sqrat/sqratClass.h @@ -566,6 +566,35 @@ public: return *this; } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Binds a Squirrel function as defined by the Squirrel documentation as a class function + /// + /// \param name Name of the function as it will appear in Squirrel + /// \param func Function to bind + /// \param pnum Number of parameters the function expects. + /// \param mask Types of parameters the function expects. + /// + /// \return The Class itself so the call can be chained + /// + /// \remarks + /// Inside of the function, the class instance the function was called with will be at index 1 on the + /// stack and all arguments will be after that index in the order they were given to the function. + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Class& SquirrelFunc(const SQChar* name, SQFUNCTION func, SQInteger pnum, const SQChar * mask) { + sq_pushobject(vm, ClassType::getClassData(vm)->classObj); + sq_pushstring(vm, name, -1); + sq_newclosure(vm, func, 0); + // Set the closure name (for debug purposes) + sq_setnativeclosurename(vm, -1, name); + // Set parameter validation + sq_setparamscheck(vm, pnum, mask); + sq_newslot(vm, -3, false); + sq_pop(vm, 1); // pop table + + return *this; + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Gets a Function from a name in the Class /// diff --git a/sqrat/sqrat/sqratTable.h b/sqrat/sqrat/sqratTable.h index 075eb5b3..8eca9e95 100644 --- a/sqrat/sqrat/sqratTable.h +++ b/sqrat/sqrat/sqratTable.h @@ -185,6 +185,30 @@ public: return *this; } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Binds a raw Squirrel closure to the Table + /// + /// \param name The key in the table being assigned a function + /// \param func Squirrel function that is being placed in the Table + /// \param pnum Number of parameters the function expects. + /// \param mask Types of parameters the function expects. + /// + /// \return The Table itself so the call can be chained + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + TableBase& SquirrelFunc(const SQChar* name, SQFUNCTION func, SQInteger pnum, const SQChar * mask) { + sq_pushobject(vm, GetObject()); + sq_pushstring(vm, name, -1); + sq_newclosure(vm, func, 0); + // Set the closure name (for debug purposes) + sq_setnativeclosurename(vm, -1, name); + // Set parameter validation + sq_setparamscheck(vm, pnum, mask); + sq_newslot(vm, -3, false); + sq_pop(vm,1); // pop table + return *this; + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Sets a key in the Table to a specific value ///