diff --git a/shared/SqMod.h b/shared/SqMod.h index 969c76e1..49dd731d 100644 --- a/shared/SqMod.h +++ b/shared/SqMod.h @@ -67,11 +67,14 @@ extern "C" { typedef void (*SqEx_LogMessage) (const SQChar * fmt, ...); //script loading typedef SQRESULT (*SqEx_LoadScript) (const SQChar * filepath); - //long numbers + //numeric utilities typedef SQRESULT (*SqEx_GetSLongValue) (HSQUIRRELVM vm, SQInteger idx, SqInt64 * num); typedef SQRESULT (*SqEx_PushSLongObject) (HSQUIRRELVM vm, SqInt64 num); typedef SQRESULT (*SqEx_GetULongValue) (HSQUIRRELVM vm, SQInteger idx, SqUint64 * num); typedef SQRESULT (*SqEx_PushULongObject) (HSQUIRRELVM vm, SqUint64 num); + typedef SQRESULT (*SqEx_GetDecimal) (HSQUIRRELVM vm, SQInteger idx, SqInt64 * value, uint8_t * precision); + typedef SQRESULT (*SqEx_GetDecimalString) (HSQUIRRELVM vm, SQInteger idx, SQChar * buffer, uint32_t size); + typedef SQRESULT (*SqEx_PushDecimal) (HSQUIRRELVM vm, SqInt64 value, uint8_t precision); //time utilities typedef SqInt64 (*SqEx_GetCurrentSysTime) (void); typedef SqInt64 (*SqEx_GetEpochTimeMicro) (void); @@ -116,11 +119,14 @@ extern "C" { SqEx_LogMessage LogSFtl; //script loading SqEx_LoadScript LoadScript; - //long numbers + //numeric utilities SqEx_GetSLongValue GetSLongValue; SqEx_PushSLongObject PushSLongObject; SqEx_GetULongValue GetULongValue; SqEx_PushULongObject PushULongObject; + SqEx_GetDecimal GetDecimal; + SqEx_GetDecimalString GetDecimalString; + SqEx_PushDecimal PushDecimal; //time utilities SqEx_GetCurrentSysTime GetCurrentSysTime; SqEx_GetEpochTimeMicro GetEpochTimeMicro; diff --git a/source/Exports.cpp b/source/Exports.cpp index 2779ebf1..fe4d88b0 100644 --- a/source/Exports.cpp +++ b/source/Exports.cpp @@ -2,6 +2,7 @@ #include "Core.hpp" // ------------------------------------------------------------------------------------------------ +#include "Library/Numeric/Decimal.hpp" #include "Library/Numeric/LongInt.hpp" #include "Library/Chrono/Date.hpp" #include "Library/Chrono/Time.hpp" @@ -198,6 +199,90 @@ static SQRESULT SqEx_PushULongObject(HSQUIRRELVM vm, Uint64 num) return SQ_OK; } +// ------------------------------------------------------------------------------------------------ +SQRESULT SqEx_GetDecimal(HSQUIRRELVM vm, SQInteger idx, SqInt64 * value, uint8_t * precision) +{ + // Is this an instance that we can treat as a Decimal 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< Decimal * > var(vm, idx); + // Assign the value + if (value != nullptr) + { + *value = var.value->GetValue(); + } + // Assign the precision + if (precision != nullptr) + { + *precision = var.value->GetPrecision(); + } + } + catch (...) + { + return SQ_ERROR; // Unable to obtain the value! + } + } + // Unrecognized value + else + { + return SQ_ERROR; + } + // Value retrieved + return SQ_OK; +} + +// ------------------------------------------------------------------------------------------------ +SQRESULT SqEx_GetDecimalString(HSQUIRRELVM vm, SQInteger idx, SQChar * buffer, uint32_t size) +{ + // Is this an instance that we can treat as a Decimal 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< Decimal * > var(vm, idx); + // Generate the string + if (buffer != nullptr) + { + var.value->MakeString(buffer, size); + } + } + catch (...) + { + return SQ_ERROR; // Unable to obtain the value! + } + } + // Unrecognized value + else + { + return SQ_ERROR; + } + // Value retrieved + return SQ_OK; +} + +// ------------------------------------------------------------------------------------------------ +SQRESULT SqEx_PushDecimal(HSQUIRRELVM vm, SqInt64 value, uint8_t precision) +{ + // Attempt to push the requested instance + try + { + Var< const Decimal & >::push(vm, Decimal(value, precision)); + } + catch (...) + { + // Specify that we failed + return SQ_ERROR; + } + // Specify that we succeeded + return SQ_OK; +} + // ------------------------------------------------------------------------------------------------ static SQRESULT SqEx_GetTimestamp(HSQUIRRELVM vm, SQInteger idx, Int64 * num) { @@ -504,11 +589,14 @@ void InitExports() //script loading g_SqExports.LoadScript = SqEx_LoadScript; - //long numbers + //numeric utilities g_SqExports.GetSLongValue = SqEx_GetSLongValue; g_SqExports.PushSLongObject = SqEx_PushSLongObject; g_SqExports.GetULongValue = SqEx_GetULongValue; g_SqExports.PushULongObject = SqEx_PushULongObject; + g_SqExports.GetDecimal = SqEx_GetDecimal; + g_SqExports.GetDecimalString = SqEx_GetDecimalString; + g_SqExports.PushDecimal = SqEx_PushDecimal; //time utilities g_SqExports.GetCurrentSysTime = Chrono::GetCurrentSysTime;