1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-15 22:57:12 +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:
Sandu Liviu Catalin
2016-07-17 03:24:07 +03:00
parent 5ddb222903
commit 66d1110733
32 changed files with 3049 additions and 2773 deletions

View File

@ -43,7 +43,7 @@ void SqThrowLast(HSQUIRRELVM vm, CSStr msg)
}
// ------------------------------------------------------------------------------------------------
Object SqFromJSON(HSQUIRRELVM vm, json_t * jval)
Object SqFromJSON(HSQUIRRELVM /*vm*/, json_t * jval)
{
switch (json_typeof(jval))
{

View File

@ -23,9 +23,9 @@ Object JArray::ToString() const
// Remember the current stack size
const StackGuard sg;
// Transform the string into a script object
sq_pushstring(_SqVM, csg.mPtr ? csg.mPtr : _SC(""), -1);
sq_pushstring(DefaultVM::Get(), csg.mPtr ? csg.mPtr : _SC(""), -1);
// Return the created script object
return Var< Object >(_SqVM, -1).value;
return Var< Object >(DefaultVM::Get(), -1).value;
}
// ================================================================================================

View File

@ -23,9 +23,9 @@ Object JObject::ToString() const
// Remember the current stack size
const StackGuard sg;
// Transform the string into a script object
sq_pushstring(_SqVM, csg.mPtr ? csg.mPtr : _SC(""), -1);
sq_pushstring(DefaultVM::Get(), csg.mPtr ? csg.mPtr : _SC(""), -1);
// Return the created script object
return Var< Object >(_SqVM, -1).value;
return Var< Object >(DefaultVM::Get(), -1).value;
}
// ================================================================================================

View File

@ -23,9 +23,9 @@ Object JValue::ToString() const
// Remember the current stack size
const StackGuard sg;
// Transform the string into a script object
sq_pushstring(_SqVM, csg.mPtr ? csg.mPtr : _SC(""), -1);
sq_pushstring(DefaultVM::Get(), csg.mPtr ? csg.mPtr : _SC(""), -1);
// Return the created script object
return Var< Object >(_SqVM, -1).value;
return Var< Object >(DefaultVM::Get(), -1).value;
}
// ------------------------------------------------------------------------------------------------

View File

@ -8,68 +8,91 @@
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Register the module API under the specified virtual machine.
*/
void RegisterAPI(HSQUIRRELVM vm);
// ------------------------------------------------------------------------------------------------
extern void Register_Common(Table & jns);
extern void Register_JArray(Table & jns);
extern void Register_JObject(Table & jns);
extern void Register_JValue(Table & jns);
/* ------------------------------------------------------------------------------------------------
* 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.", SQJSON_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", SQJSON_NAME);
// Registration failed
return false;
}
Table jns;
Register_Common(jns);
Register_JArray(jns);
Register_JObject(jns);
Register_JValue(jns);
RootTable().Bind(_SC("SqJSON"), jns);
Sqrat::ConstTable()
.Const(_SC("JSON_OBJECT"), JSON_OBJECT)
.Const(_SC("JSON_ARRAY"), JSON_ARRAY)
.Const(_SC("JSON_STRING"), JSON_STRING)
.Const(_SC("JSON_INTEGER"), JSON_INTEGER)
.Const(_SC("JSON_REAL"), JSON_REAL)
.Const(_SC("JSON_TRUE"), JSON_TRUE)
.Const(_SC("JSON_FALSE"), JSON_FALSE)
.Const(_SC("JSON_NULL"), JSON_NULL);
// 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", SQJSON_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", SQJSON_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", SQJSON_NAME);
if (RegisterAPI())
{
OutputMessage("Registered: %s", SQJSON_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", SQJSON_NAME);
// Release null objects just in case
@ -82,31 +105,12 @@ void OnSquirrelTerminate()
/* ------------------------------------------------------------------------------------------------
* 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", SQJSON_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 +119,33 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
switch(command_identifier)
{
case SQMOD_INITIALIZE_CMD:
if (CheckAPIVer(message))
{
if (CheckModuleAPIVer(message, SQJSON_NAME))
{
OnSquirrelInitialize();
try
{
ImportModuleAPI(_Func, SQJSON_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 +156,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,35 +170,6 @@ static void OnServerShutdown(void)
_Clbk->OnPluginCommand = nullptr;
}
// ------------------------------------------------------------------------------------------------
extern void Register_Common(Table & jns);
extern void Register_JArray(Table & jns);
extern void Register_JObject(Table & jns);
extern void Register_JValue(Table & jns);
// ------------------------------------------------------------------------------------------------
void RegisterAPI(HSQUIRRELVM vm)
{
Table jns(vm);
Register_Common(jns);
Register_JArray(jns);
Register_JObject(jns);
Register_JValue(jns);
RootTable(vm).Bind(_SC("SqJSON"), jns);
Sqrat::ConstTable(vm)
.Const(_SC("JSON_OBJECT"), JSON_OBJECT)
.Const(_SC("JSON_ARRAY"), JSON_ARRAY)
.Const(_SC("JSON_STRING"), JSON_STRING)
.Const(_SC("JSON_INTEGER"), JSON_INTEGER)
.Const(_SC("JSON_REAL"), JSON_REAL)
.Const(_SC("JSON_TRUE"), JSON_TRUE)
.Const(_SC("JSON_FALSE"), JSON_FALSE)
.Const(_SC("JSON_NULL"), JSON_NULL);
}
} // Namespace:: SqMod
// ------------------------------------------------------------------------------------------------
@ -193,20 +184,9 @@ SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs * functions, PluginCall
OutputMessage("Legal: %s", SQJSON_COPYRIGHT);
OutputMessage("--------------------------------------------------------------------");
std::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, SQJSON_NAME))
{
OutputError("%s could find the host plug-in", SQJSON_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", SQJSON_NAME);
// Don't load!
return SQMOD_FAILURE;
}
// Store server proxies