1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-20 17:17:13 +02:00

Move shared code into main source.

Remove extra module API.
This commit is contained in:
Sandu Liviu Catalin
2020-03-21 22:16:48 +02:00
parent 22c8d84b1e
commit 9978dbe88c
10 changed files with 2 additions and 1314 deletions

View File

@ -1,14 +1,5 @@
// ------------------------------------------------------------------------------------------------
#include "Core.hpp"
#include "Base/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include "Library/Numeric/LongInt.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Library/Chrono/Timestamp.hpp"
#include "Library/Utils/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include <cmath>
@ -42,597 +33,6 @@ static SQRESULT SqModImpl_LoadScript(const SQChar * filepath, SQBool delay)
return SQ_ERROR;
}
// ------------------------------------------------------------------------------------------------
static SQRESULT SqModImpl_GetSLongValue(HSQUIRRELVM vm, SQInteger idx, Int64 * num)
{
// Validate the specified number pointer and value type
if (!num)
{
return SQ_ERROR; // Nowhere to save!
}
// Is this an instance that we can treat as a SLongInt type?
else if (sq_gettype(vm, idx) == OT_INSTANCE)
{
// Attempt to obtain the long instance and it's value from the stack
try
{
*num = static_cast< Int64 >(Var< const SLongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
return SQ_ERROR; // Unable to obtain the value!
}
}
// Is this a pure integer value?
else if(sq_gettype(vm, idx) == OT_INTEGER)
{
SQInteger val = 0;
// Attempt to get the value from the stack
sq_getinteger(vm, idx, &val);
// Save it into the specified memory location
*num = static_cast< Int64 >(val);
}
// Is this a pure floating point value?
else if(sq_gettype(vm, idx) == OT_FLOAT)
{
SQFloat val = 0.0;
// Attempt to get the value from the stack
sq_getfloat(vm, idx, &val);
// Save it into the specified memory location
*num = static_cast< Int64 >(std::llround(val));
}
// Is this a pure boolean value?
else if(sq_gettype(vm, idx) == OT_BOOL)
{
SQBool val = SQFalse;
// Attempt to get the value from the stack
sq_getbool(vm, idx, &val);
// Save it into the specified memory location
*num = static_cast< Int64 >(val);
}
// Unrecognized value
else
{
return SQ_ERROR;
}
// Value retrieved
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
static SQRESULT SqModImpl_PushSLongObject(HSQUIRRELVM vm, Int64 num)
{
// Attempt to push the requested instance
try
{
Var< const SLongInt & >::push(vm, SLongInt(num));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
static SQRESULT SqModImpl_GetULongValue(HSQUIRRELVM vm, SQInteger idx, Uint64 * num)
{
// Validate the specified number pointer and value type
if (!num)
{
return SQ_ERROR; // Nowhere to save
}
// Is this an instance that we can treat as a ULongInt type?
else if (sq_gettype(vm, idx) == OT_INSTANCE)
{
// Attempt to obtain the long instance and it's value from the stack
try
{
*num = static_cast< Uint64 >(Var< const ULongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
return SQ_ERROR; // Unable to obtain the value!
}
}
// Is this a pure integer value?
else if(sq_gettype(vm, idx) == OT_INTEGER)
{
SQInteger val = 0;
// Attempt to get the value from the stack
sq_getinteger(vm, idx, &val);
// Save it into the specified memory location
*num = val ? static_cast< Uint64 >(val) : 0L;
}
// Is this a pure floating point value?
else if(sq_gettype(vm, idx) == OT_FLOAT)
{
SQFloat val = 0.0;
// Attempt to get the value from the stack
sq_getfloat(vm, idx, &val);
// Save it into the specified memory location
*num = EpsLt(val, SQFloat(0.0)) ? 0L : static_cast< Uint64 >(std::llround(val));
}
// Is this a pure boolean value?
else if(sq_gettype(vm, idx) == OT_BOOL)
{
SQBool val = SQFalse;
// Attempt to get the value from the stack
sq_getbool(vm, idx, &val);
// Save it into the specified memory location
*num = static_cast< Uint64 >(val);
}
// Unrecognized value
else
{
return SQ_ERROR;
}
// Value retrieved
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
static SQRESULT SqModImpl_PushULongObject(HSQUIRRELVM vm, Uint64 num)
{
// Attempt to push the requested instance
try
{
Var< const ULongInt & >::push(vm, ULongInt(num));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQBool SqModImpl_ValidDate(uint16_t year, uint8_t month, uint8_t day)
{
return Chrono::ValidDate(year, month, day);
}
// ------------------------------------------------------------------------------------------------
SQBool SqModImpl_IsLeapYear(uint16_t year)
{
return Chrono::IsLeapYear(year);
}
// ------------------------------------------------------------------------------------------------
static SQRESULT SqModImpl_GetTimestamp(HSQUIRRELVM vm, SQInteger idx, Int64 * num)
{
// Validate the specified number pointer and value type
if (!num)
{
return SQ_ERROR; // Nowhere to save
}
// Is this an instance that we can treat as a Time-stamp type?
else if (sq_gettype(vm, idx) == OT_INSTANCE)
{
// Attempt to obtain the time-stamp and it's value from the stack
try
{
*num = static_cast< Int64 >(Var< const Timestamp & >(vm, idx).value.GetNum());
}
catch (...)
{
return SQ_ERROR; // Unable to obtain the value!
}
}
// Is this a pure integer value?
else if(sq_gettype(vm, idx) == OT_INTEGER)
{
SQInteger val = 0;
// Attempt to get the value from the stack
sq_getinteger(vm, idx, &val);
// Save it into the specified memory location
*num = static_cast< Int64 >(val);
}
// Is this a pure floating point value?
else if(sq_gettype(vm, idx) == OT_FLOAT)
{
SQFloat val = 0.0;
// Attempt to get the value from the stack
sq_getfloat(vm, idx, &val);
// Save it into the specified memory location
*num = static_cast< Int64 >(std::llround(val));
}
// Is this a pure boolean value?
else if(sq_gettype(vm, idx) == OT_BOOL)
{
SQBool val = SQFalse;
// Attempt to get the value from the stack
sq_getbool(vm, idx, &val);
// Save it into the specified memory location
*num = static_cast< Int64 >(val);
}
// Unrecognized value
else
{
return SQ_ERROR;
}
// Value retrieved
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
static SQRESULT SqModImpl_PushTimestamp(HSQUIRRELVM vm, Int64 num)
{
// Attempt to push the requested instance
try
{
Var< const Timestamp & >::push(vm, Timestamp(num));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqModImpl_GetDate(HSQUIRRELVM vm, SQInteger idx, uint16_t * year, uint8_t * month, uint8_t * day)
{
// Is this an instance that we can treat as a Date type?
if (sq_gettype(vm, idx) == OT_INSTANCE)
{
// Attempt to obtain the time-stamp and it's value from the stack
try
{
// Attempt to retrieve the instance
Var< Date * > var(vm, idx);
// Assign the year
if (year != nullptr)
{
*year = var.value->GetYear();
}
// Assign the month
if (month != nullptr)
{
*month = var.value->GetMonth();
}
// Assign the day
if (day != nullptr)
{
*day = var.value->GetDay();
}
}
catch (...)
{
return SQ_ERROR; // Unable to obtain the value!
}
}
// Unrecognized value
else
{
return SQ_ERROR;
}
// Value retrieved
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqModImpl_PushDate(HSQUIRRELVM vm, uint16_t year, uint8_t month, uint8_t day)
{
// Attempt to push the requested instance
try
{
Var< const Date & >::push(vm, Date(year, month, day));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqModImpl_GetTime(HSQUIRRELVM vm, SQInteger idx, uint8_t * hour, uint8_t * minute,
uint8_t * second, uint16_t * millisecond)
{
// Is this an instance that we can treat as a Time type?
if (sq_gettype(vm, idx) == OT_INSTANCE)
{
// Attempt to obtain the time-stamp and it's value from the stack
try
{
// Attempt to retrieve the instance
Var< Time * > var(vm, idx);
// Assign the hour
if (hour != nullptr)
{
*hour = var.value->GetHour();
}
// Assign the minute
if (minute != nullptr)
{
*minute = var.value->GetMinute();
}
// Assign the second
if (second != nullptr)
{
*second = var.value->GetSecond();
}
// Assign the millisecond
if (millisecond != nullptr)
{
*millisecond = var.value->GetMillisecond();
}
}
catch (...)
{
return SQ_ERROR; // Unable to obtain the value!
}
}
// Unrecognized value
else
{
return SQ_ERROR;
}
// Value retrieved
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqModImpl_PushTime(HSQUIRRELVM vm, uint8_t hour, uint8_t minute, uint8_t second,
uint16_t millisecond)
{
// Attempt to push the requested instance
try
{
Var< const Time & >::push(vm, Time(hour, minute, second, millisecond));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqModImpl_GetDatetime(HSQUIRRELVM vm, SQInteger idx, uint16_t * year, uint8_t * month, uint8_t * day,
uint8_t * hour, uint8_t * minute, uint8_t * second, uint16_t * millisecond)
{
// Is this an instance that we can treat as a Date-time type?
if (sq_gettype(vm, idx) == OT_INSTANCE)
{
// Attempt to obtain the time-stamp and it's value from the stack
try
{
// Attempt to retrieve the instance
Var< Datetime * > var(vm, idx);
// Assign the year
if (year != nullptr)
{
*year = var.value->GetYear();
}
// Assign the month
if (month != nullptr)
{
*month = var.value->GetMonth();
}
// Assign the day
if (day != nullptr)
{
*day = var.value->GetDay();
}
// Assign the hour
if (hour != nullptr)
{
*hour = var.value->GetHour();
}
// Assign the minute
if (minute != nullptr)
{
*minute = var.value->GetMinute();
}
// Assign the second
if (second != nullptr)
{
*second = var.value->GetSecond();
}
// Assign the millisecond
if (millisecond != nullptr)
{
*millisecond = var.value->GetMillisecond();
}
}
catch (...)
{
return SQ_ERROR; // Unable to obtain the value!
}
}
// Unrecognized value
else
{
return SQ_ERROR;
}
// Value retrieved
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqModImpl_PushDatetime(HSQUIRRELVM vm, uint16_t year, uint8_t month, uint8_t day,
uint8_t hour, uint8_t minute, uint8_t second, uint16_t millisecond)
{
// Attempt to push the requested instance
try
{
Var< const Datetime & >::push(vm, Datetime(year, month, day, hour, minute, second, millisecond));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqModImpl_PushBuffer(HSQUIRRELVM vm, SQInteger size, SQInteger cursor)
{
// Attempt to push the requested instance
try
{
Var< const SqBuffer & >::push(vm, SqBuffer(size, cursor));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqModImpl_PushBufferData(HSQUIRRELVM vm, const char * data, SQInteger size, SQInteger cursor)
{
// Attempt to push the requested instance
try
{
Var< const SqBuffer & >::push(vm, SqBuffer(data, size, cursor));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqModImpl_GetBufferInfo(HSQUIRRELVM vm, SQInteger idx, const char ** ptr, SQInteger * size, SQInteger * cursor)
{
// Attempt to obtain the requested information
try
{
// Attempt to retrieve the instance
Var< SqBuffer * > var(vm, idx);
// Validate the obtained buffer
if (!(var.value) || !(var.value->GetRef()) || !(*var.value->GetRef()))
{
// Should we obtain the buffer contents?
if (ptr)
{
*ptr = nullptr; // Default to null data
}
// Should we obtain the buffer length?
if (size)
{
*size = 0; // Default to 0 length
}
// Should we obtain the cursor position?
if (cursor)
{
*cursor = 0; // Default to position 0
}
}
// Grab the internal buffer
const Buffer & b = *var.value->GetRef();
// Should we obtain the buffer contents?
if (ptr)
{
*ptr = b.Data();
}
// Should we obtain the buffer length?
if (size)
{
*size = ConvTo< SQInteger >::From(b.Capacity());
}
// Should we obtain the cursor position?
if (cursor)
{
*cursor = ConvTo< SQInteger >::From(b.Position());
}
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
const char * SqModImpl_GetBufferData(HSQUIRRELVM vm, SQInteger idx)
{
// Attempt to obtain the requested information
try
{
// Attempt to retrieve the instance
Var< SqBuffer * > var(vm, idx);
// Validate the obtained buffer and return the requested information
if ((var.value) && (var.value->GetRef()) && (*var.value->GetRef()))
{
return var.value->GetRef()->Data();
}
}
catch (...)
{
// Just ignore it...
}
// Specify that we failed
return nullptr;
}
// ------------------------------------------------------------------------------------------------
SQInteger SqModImpl_GetBufferSize(HSQUIRRELVM vm, SQInteger idx)
{
// Attempt to obtain the requested information
try
{
// Attempt to retrieve the instance
Var< SqBuffer * > var(vm, idx);
// Validate the obtained buffer and return the requested information
if ((var.value) && (var.value->GetRef()) && (*var.value->GetRef()))
{
return ConvTo< SQInteger >::From(var.value->GetRef()->Capacity());
}
}
catch (...)
{
// Just ignore it...
}
// Specify that we failed
return -1;
}
// ------------------------------------------------------------------------------------------------
SQInteger SqModImpl_GetBufferCursor(HSQUIRRELVM vm, SQInteger idx)
{
// Attempt to obtain the requested information
try
{
// Attempt to retrieve the instance
Var< SqBuffer * > var(vm, idx);
// Validate the obtained buffer and return the requested information
if ((var.value) && (var.value->GetRef()) && (*var.value->GetRef()))
{
return ConvTo< SQInteger >::From(var.value->GetRef()->Position());
}
}
catch (...)
{
// Just ignore it...
}
// Specify that we failed
return -1;
}
// ------------------------------------------------------------------------------------------------
static int32_t SqExport_PopulateModuleAPI(HSQMODAPI api, size_t size)
{
@ -667,45 +67,6 @@ static int32_t SqExport_PopulateModuleAPI(HSQMODAPI api, size_t size)
//script loading
api->LoadScript = SqModImpl_LoadScript;
//numeric utilities
api->GetSLongValue = SqModImpl_GetSLongValue;
api->PushSLongObject = SqModImpl_PushSLongObject;
api->GetULongValue = SqModImpl_GetULongValue;
api->PushULongObject = SqModImpl_PushULongObject;
//time utilities
api->GetCurrentSysTime = Chrono::GetCurrentSysTime;
api->GetEpochTimeMicro = Chrono::GetEpochTimeMicro;
api->GetEpochTimeMilli = Chrono::GetEpochTimeMilli;
api->ValidDate = SqModImpl_ValidDate;
api->IsLeapYear = SqModImpl_IsLeapYear;
api->DaysInYear = Chrono::DaysInYear;
api->DaysInMonth = Chrono::DaysInMonth;
api->DayOfYear = Chrono::DayOfYear;
api->DateRangeToSeconds = Chrono::DateRangeToSeconds;
api->GetTimestamp = SqModImpl_GetTimestamp;
api->PushTimestamp = SqModImpl_PushTimestamp;
api->GetDate = SqModImpl_GetDate;
api->PushDate = SqModImpl_PushDate;
api->GetTime = SqModImpl_GetTime;
api->PushTime = SqModImpl_PushTime;
api->GetDatetime = SqModImpl_GetDatetime;
api->PushDatetime = SqModImpl_PushDatetime;
//stack utilities
api->PopStackInteger = PopStackInteger;
api->PopStackFloat = PopStackFloat;
api->PopStackSLong = PopStackSLong;
api->PopStackULong = PopStackULong;
//buffer utilities
api->PushBuffer = SqModImpl_PushBuffer;
api->PushBufferData = SqModImpl_PushBufferData;
api->GetBufferInfo = SqModImpl_GetBufferInfo;
api->GetBufferData = SqModImpl_GetBufferData;
api->GetBufferSize = SqModImpl_GetBufferSize;
api->GetBufferCursor = SqModImpl_GetBufferCursor;
return 1; // Successfully populated!
}