mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-06-19 16:47:14 +02:00
Rvised the API distribution system to avoid segmentation fault crashes on Linux and make the overal code cleaner.
Moved the constants in IRC module into their own source and implemented a faster method of registering them. Various other minor changes and adjustments. Some of them in order to comply with the new API distribution system.
This commit is contained in:
@ -430,12 +430,12 @@ Object Column::GetString() const
|
||||
{
|
||||
SQMOD_VALIDATE_ROW(*this);
|
||||
// Obtain the initial stack size
|
||||
const StackGuard sg(_SqVM);
|
||||
const StackGuard sg;
|
||||
// Push the column text on the stack
|
||||
sq_pushstring(_SqVM, reinterpret_cast< CSStr >(sqlite3_column_text(m_Handle->mPtr, m_Index)),
|
||||
sq_pushstring(DefaultVM::Get(), reinterpret_cast< CSStr >(sqlite3_column_text(m_Handle->mPtr, m_Index)),
|
||||
sqlite3_column_bytes(m_Handle->mPtr, m_Index));
|
||||
// Get the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
return Var< Object >(DefaultVM::Get(), -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -470,7 +470,7 @@ Object Column::GetBuffer() const
|
||||
STHROWF("Unable to allocate buffer of at least (%d) bytes", size);
|
||||
}
|
||||
// Get the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
return Var< Object >(DefaultVM::Get(), -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -478,11 +478,11 @@ Object Column::GetBlob() const
|
||||
{
|
||||
SQMOD_VALIDATE_ROW(*this);
|
||||
// Obtain the initial stack size
|
||||
const StackGuard sg(_SqVM);
|
||||
const StackGuard sg;
|
||||
// Obtain the size of the data
|
||||
const Int32 sz = sqlite3_column_bytes(m_Handle->mPtr, m_Index);
|
||||
// Allocate a blob of the same size
|
||||
SQUserPointer p = sqstd_createblob(_SqVM, sz);
|
||||
SQUserPointer p = sqstd_createblob(DefaultVM::Get(), sz);
|
||||
// Obtain a pointer to the data
|
||||
const void * b = sqlite3_column_blob(m_Handle->mPtr, m_Index);
|
||||
// Could the memory blob be allocated?
|
||||
@ -494,9 +494,9 @@ Object Column::GetBlob() const
|
||||
else if (!b)
|
||||
{
|
||||
// Pop the memory blob from the stack
|
||||
sq_pop(_SqVM, 1);
|
||||
sq_pop(DefaultVM::Get(), 1);
|
||||
// Push a null value instead
|
||||
sq_pushnull(_SqVM);
|
||||
sq_pushnull(DefaultVM::Get());
|
||||
}
|
||||
// Copy the data into the memory blob
|
||||
else
|
||||
@ -504,7 +504,7 @@ Object Column::GetBlob() const
|
||||
std::memcpy(p, b, sz);
|
||||
}
|
||||
// Get the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
return Var< Object >(DefaultVM::Get(), -1).value;
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
|
@ -82,22 +82,22 @@ Int32 ReleaseMemory(Int32 bytes)
|
||||
Object GetMemoryUsage()
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const StackGuard sg(_SqVM);
|
||||
const StackGuard sg;
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
SqMod_PushSLongObject(_SqVM, sqlite3_memory_used());
|
||||
SqMod_PushSLongObject(DefaultVM::Get(), sqlite3_memory_used());
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
return Var< Object >(DefaultVM::Get(), -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Object GetMemoryHighwaterMark(bool reset)
|
||||
{
|
||||
// Obtain the initial stack size
|
||||
const StackGuard sg(_SqVM);
|
||||
const StackGuard sg;
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
SqMod_PushSLongObject(_SqVM, sqlite3_memory_highwater(reset));
|
||||
SqMod_PushSLongObject(DefaultVM::Get(), sqlite3_memory_highwater(reset));
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(_SqVM, -1).value;
|
||||
return Var< Object >(DefaultVM::Get(), -1).value;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
@ -8,68 +8,87 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Register the module API under the specified virtual machine.
|
||||
*/
|
||||
void RegisterAPI(HSQUIRRELVM vm);
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
extern void Register_Constants(Table & sqlns);
|
||||
extern void Register_Common(Table & sqlns);
|
||||
extern void Register_Connection(Table & sqlns);
|
||||
extern void Register_Statement(Table & sqlns);
|
||||
extern void Register_Parameter(Table & sqlns);
|
||||
extern void Register_Column(Table & sqlns);
|
||||
extern void Register_Transaction(Table & sqlns);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Initialize the plug-in by obtaining the API provided by the host plug-in.
|
||||
* Register the module API under the obtained virtual machine.
|
||||
*/
|
||||
void OnSquirrelInitialize()
|
||||
static bool RegisterAPI()
|
||||
{
|
||||
// Attempt to import the plug-in API exported by the host plug-in
|
||||
_SqMod = sq_api_import(_Func);
|
||||
// Did we failed to obtain the plug-in exports?
|
||||
if (!_SqMod)
|
||||
// Make sure there's a valid virtual machine before proceeding
|
||||
if (!DefaultVM::Get())
|
||||
{
|
||||
OutputError("Failed to attach [%s] on host plug-in.", SQSQLITE_NAME);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Expand the Squirrel plug-in API into global functions
|
||||
sqmod_api_expand(_SqMod);
|
||||
// Obtain the Squirrel API
|
||||
_SqAPI = SqMod_GetSquirrelAPI();
|
||||
// Expand the Squirrel API into global functions
|
||||
sq_api_expand(_SqAPI);
|
||||
OutputError("%s: Cannot register API without a valid virtual machine", SQSQLITE_NAME);
|
||||
// Registration failed
|
||||
return false;
|
||||
}
|
||||
|
||||
Table sqlns;
|
||||
|
||||
Register_Constants(sqlns);
|
||||
Register_Common(sqlns);
|
||||
Register_Connection(sqlns);
|
||||
Register_Statement(sqlns);
|
||||
Register_Parameter(sqlns);
|
||||
Register_Column(sqlns);
|
||||
Register_Transaction(sqlns);
|
||||
|
||||
RootTable().Bind(_SC("SQLite"), sqlns);
|
||||
|
||||
// Registration was successful
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Load the module on the virtual machine provided by the host module.
|
||||
*/
|
||||
void OnSquirrelLoad()
|
||||
static bool OnSquirrelLoad()
|
||||
{
|
||||
// Make sure that we have a valid plug-in API
|
||||
if (!_SqMod)
|
||||
// Make sure that we have a valid module API
|
||||
if (!SqMod_GetSquirrelVM)
|
||||
{
|
||||
return; // Unable to proceed!
|
||||
OutputError("%s: Cannot obtain the Squirrel virtual machine without the module API", SQSQLITE_NAME);
|
||||
// Unable to proceed!
|
||||
return false;
|
||||
}
|
||||
// Obtain the Squirrel API and VM
|
||||
_SqVM = SqMod_GetSquirrelVM();
|
||||
// Obtain the Squirrel virtual machine from the host plug-in
|
||||
DefaultVM::Set(SqMod_GetSquirrelVM());
|
||||
// Make sure that a valid virtual machine exists
|
||||
if (!_SqVM)
|
||||
if (!DefaultVM::Get())
|
||||
{
|
||||
return; // Unable to proceed!
|
||||
OutputError("%s: Squirrel virtual machine obtained from the host plug-in is invalid", SQSQLITE_NAME);
|
||||
// Unable to proceed!
|
||||
return false;
|
||||
}
|
||||
// Set this as the default database
|
||||
DefaultVM::Set(_SqVM);
|
||||
// Prevent common null objects from using dead virtual machines
|
||||
NullArray() = Array();
|
||||
NullTable() = Table();
|
||||
NullObject() = Object();
|
||||
NullFunction() = Function();
|
||||
// Register the module API
|
||||
RegisterAPI(_SqVM);
|
||||
// Notify about the current status
|
||||
OutputMessage("Registered: %s", SQSQLITE_NAME);
|
||||
if (RegisterAPI())
|
||||
{
|
||||
OutputMessage("Registered: %s", SQSQLITE_NAME);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// At this point, the module was successfully loaded
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* The virtual machine is about to be terminated and script resources should be released.
|
||||
*/
|
||||
void OnSquirrelTerminate()
|
||||
static void OnSquirrelTerminate()
|
||||
{
|
||||
OutputMessage("Terminating: %s", SQSQLITE_NAME);
|
||||
// Release null objects just in case
|
||||
@ -77,36 +96,18 @@ void OnSquirrelTerminate()
|
||||
NullTable().Release();
|
||||
NullArray().Release();
|
||||
NullFunction().ReleaseGently();
|
||||
// Release script resources...
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* The virtual machined was closed and all memory associated with it was released.
|
||||
*/
|
||||
void OnSquirrelReleased()
|
||||
static void OnSquirrelReleased()
|
||||
{
|
||||
// Release the current virtual machine, if any
|
||||
DefaultVM::Set(nullptr);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Validate the module API to make sure we don't run into issues.
|
||||
*/
|
||||
bool CheckAPIVer(CCStr ver)
|
||||
{
|
||||
// Obtain the numeric representation of the API version
|
||||
const LongI vernum = std::strtol(ver, nullptr, 10);
|
||||
// Check against version mismatch
|
||||
if (vernum == SQMOD_API_VER)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// Log the incident
|
||||
OutputError("API version mismatch on %s", SQSQLITE_NAME);
|
||||
OutputMessage("=> Requested: %ld Have: %ld", vernum, SQMOD_API_VER);
|
||||
// Invoker should not attempt to communicate through the module API
|
||||
return false;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* React to command sent by other plug-ins.
|
||||
*/
|
||||
@ -115,20 +116,33 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
|
||||
switch(command_identifier)
|
||||
{
|
||||
case SQMOD_INITIALIZE_CMD:
|
||||
if (CheckAPIVer(message))
|
||||
{
|
||||
if (CheckModuleAPIVer(message, SQSQLITE_NAME))
|
||||
{
|
||||
OnSquirrelInitialize();
|
||||
try
|
||||
{
|
||||
ImportModuleAPI(_Func, SQSQLITE_NAME);
|
||||
}
|
||||
catch (const Sqrat::Exception & e)
|
||||
{
|
||||
OutputError("%s", e.what());
|
||||
// Failed to initialize
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
case SQMOD_LOAD_CMD:
|
||||
OnSquirrelLoad();
|
||||
break;
|
||||
{
|
||||
return OnSquirrelLoad();
|
||||
} break;
|
||||
case SQMOD_TERMINATE_CMD:
|
||||
{
|
||||
OnSquirrelTerminate();
|
||||
break;
|
||||
} break;
|
||||
case SQMOD_RELEASED_CMD:
|
||||
{
|
||||
OnSquirrelReleased();
|
||||
break;
|
||||
} break;
|
||||
default: break;
|
||||
}
|
||||
return 1;
|
||||
@ -139,9 +153,12 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
|
||||
*/
|
||||
static uint8_t OnServerInitialise()
|
||||
{
|
||||
return 1;
|
||||
return 1; // Initialization was successful
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* The server is about to shutdown gracefully.
|
||||
*/
|
||||
static void OnServerShutdown(void)
|
||||
{
|
||||
// The server may still send callbacks
|
||||
@ -150,31 +167,6 @@ static void OnServerShutdown(void)
|
||||
_Clbk->OnPluginCommand = nullptr;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
extern void Register_Constants(Table & sqlns);
|
||||
extern void Register_Common(Table & sqlns);
|
||||
extern void Register_Connection(Table & sqlns);
|
||||
extern void Register_Statement(Table & sqlns);
|
||||
extern void Register_Parameter(Table & sqlns);
|
||||
extern void Register_Column(Table & sqlns);
|
||||
extern void Register_Transaction(Table & sqlns);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void RegisterAPI(HSQUIRRELVM vm)
|
||||
{
|
||||
Table sqlns(vm);
|
||||
|
||||
Register_Constants(sqlns);
|
||||
Register_Common(sqlns);
|
||||
Register_Connection(sqlns);
|
||||
Register_Statement(sqlns);
|
||||
Register_Parameter(sqlns);
|
||||
Register_Column(sqlns);
|
||||
Register_Transaction(sqlns);
|
||||
|
||||
RootTable(vm).Bind(_SC("SQLite"), sqlns);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -189,20 +181,9 @@ SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs * functions, PluginCall
|
||||
OutputMessage("Legal: %s", SQSQLITE_COPYRIGHT);
|
||||
OutputMessage("--------------------------------------------------------------------");
|
||||
puts("");
|
||||
// Attempt to find the host plug-in ID
|
||||
const int host_plugin_id = functions->FindPlugin(SQMOD_HOST_NAME);
|
||||
// See if our plug-in was loaded after the host plug-in
|
||||
if (host_plugin_id < 0)
|
||||
// Make sure that the module was loaded after the host plug-in
|
||||
if (!CheckModuleOrder(functions, info->pluginId, SQSQLITE_NAME))
|
||||
{
|
||||
OutputError("%s could find the host plug-in", SQSQLITE_NAME);
|
||||
// Don't load!
|
||||
return SQMOD_FAILURE;
|
||||
}
|
||||
// Should never reach this point but just in case
|
||||
else if (static_cast< Uint32 >(host_plugin_id) > info->pluginId)
|
||||
{
|
||||
OutputError("%s loaded after the host plug-in", SQSQLITE_NAME);
|
||||
// Don't load!
|
||||
return SQMOD_FAILURE;
|
||||
}
|
||||
// Store server proxies
|
||||
|
Reference in New Issue
Block a user