1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-04-02 18:37:13 +02:00

Allow customization of native squirrel functions when bound via the sqrat library.

This commit is contained in:
Sandu Liviu Catalin 2020-03-22 18:07:46 +02:00
parent 3b7568f13a
commit a3312de023
3 changed files with 79 additions and 0 deletions

View File

@ -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
///

View File

@ -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<C>::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
///

View File

@ -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
///