1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-15 22:57:12 +02:00

Update the module API and merge shared code between modules and host plugin.

This commit is contained in:
Sandu Liviu Catalin
2016-06-03 21:26:19 +03:00
parent 423bdfcdab
commit 0c92601362
56 changed files with 2781 additions and 2688 deletions

611
shared/Base/Buffer.cpp Normal file
View File

@ -0,0 +1,611 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdlib>
#include <cstring>
#include <exception>
#include <stdexcept>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Compute the next power of two for the specified number.
*/
inline unsigned int NextPow2(unsigned int num)
{
--num;
num |= num >> 1;
num |= num >> 2;
num |= num >> 4;
num |= num >> 8;
num |= num >> 16;
return ++num;
}
/* ------------------------------------------------------------------------------------------------
* Throw an memory exception.
*/
void ThrowMemExcept(const char * msg, ...)
{
// Exception messages should be concise
char buffer[128];
// Variable arguments structure
va_list args;
// Get the specified arguments
va_start(args, msg);
// Run the specified format
int ret = std::vsnprintf(buffer, sizeof(buffer), msg, args);
// Check for formatting errors
if (ret < 0)
{
throw std::runtime_error("Unknown memory error");
}
// Throw the actual exception
throw std::runtime_error(buffer);
}
/* ------------------------------------------------------------------------------------------------
* Allocate a memory buffer and return it.
*/
static Buffer::Pointer AllocMem(Buffer::SzType size)
{
// Attempt to allocate memory directly
Buffer::Pointer ptr = reinterpret_cast< Buffer::Pointer >(std::malloc(size));
// Validate the allocated memory
if (!ptr)
{
ThrowMemExcept("Unable to allocate (%u) bytes of memory", size);
}
// Return the allocated memory
return ptr;
}
/* ------------------------------------------------------------------------------------------------
* ...
*/
class MemCat
{
// --------------------------------------------------------------------------------------------
friend class Memory;
friend class Buffer;
public:
// --------------------------------------------------------------------------------------------
typedef Buffer::Value Value; // The type of value used to represent a byte.
// --------------------------------------------------------------------------------------------
typedef Buffer::Reference Reference; // A reference to the stored value type.
typedef Buffer::ConstRef ConstRef; // A const reference to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Buffer::Pointer Pointer; // A pointer to the stored value type.
typedef Buffer::ConstPtr ConstPtr; // A const pointer to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Buffer::SzType SzType; // The type used to represent size in general.
private:
/* --------------------------------------------------------------------------------------------
* Structure used to store a memory chunk in the linked list.
*/
struct Node
{
// ----------------------------------------------------------------------------------------
SzType mCap; // The size of the memory chunk.
Pointer mPtr; // Pointer to the memory chunk.
Node* mNext; // The next node in the list.
/* ----------------------------------------------------------------------------------------
* Base constructor.
*/
Node(Node * next)
: mCap(0)
, mPtr(nullptr)
, mNext(next)
{
/* ... */
}
};
// --------------------------------------------------------------------------------------------
static Node * s_Nodes; /* List of unused node instances. */
// --------------------------------------------------------------------------------------------
Node* m_Head; /* The head memory node. */
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
MemCat()
: m_Head(nullptr)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~MemCat()
{
for (Node * node = m_Head, * next = nullptr; node; node = next)
{
// Free the memory (if any)
if (node->mPtr)
{
std::free(node->mPtr);
}
// Save the next node
next = node->mNext;
// Release the node instance
delete node;
}
// Explicitly set the head node to null
m_Head = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Clear all memory buffers from the pool.
*/
void Clear()
{
for (Node * node = m_Head, * next = nullptr; node; node = next)
{
// Free the memory (if any)
if (node->mPtr)
{
free(node->mPtr);
}
// Save the next node
next = node->mNext;
// Release the node instance
Push(node);
}
// Explicitly set the head node to null
m_Head = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Grab a memory buffer from the pool.
*/
void Grab(Pointer & ptr, SzType & size)
{
// NOTE: Function assumes (size > 0)
// Find a buffer large enough to satisfy the requested size
for (Node * node = m_Head, * prev = nullptr; node; prev = node, node = node->mNext)
{
// Is this buffer large enough?
if (node->mCap >= size)
{
// Was there a previous node?
if (prev)
{
prev->mNext = node->mNext;
}
// Probably this was the head
else
{
m_Head = node->mNext;
}
// Assign the memory
ptr = node->mPtr;
// Assign the size
size = node->mCap;
// Release the node instance
Push(node);
// Exit the function
return;
}
}
// Round up the size to a power of two number
size = (size & (size - 1)) ? NextPow2(size) : size;
// Allocate the memory directly
ptr = AllocMem(size);
// See if the memory could be allocated
// (shouldn't reach this point if allocation failed)
if (!ptr)
{
// Revert the size
size = 0;
// Throw the exception
ThrowMemExcept("Unable to allocate (%u) bytes of memory", size);
}
}
/* --------------------------------------------------------------------------------------------
* Return a memory buffer to the pool.
*/
void Drop(Pointer & ptr, SzType & size)
{
if (!ptr)
{
ThrowMemExcept("Cannot store invalid memory buffer");
}
// Request a node instance
Node * node = Pull();
// Assign the specified memory
node->mPtr = ptr;
// Assign the specified size
node->mCap = size;
// Demote the current head node
node->mNext = m_Head;
// Promote as the head node
m_Head = node;
}
/* --------------------------------------------------------------------------------------------
* Allocate a group of nodes and pool them for later use.
*/
static void Make()
{
for (SzType n = 16; n; --n)
{
// Create a new node instance
s_Nodes = new Node(s_Nodes);
// Validate the head node
if (!s_Nodes)
{
ThrowMemExcept("Unable to allocate memory nodes");
}
}
}
/* --------------------------------------------------------------------------------------------
* Retrieve an unused node from the free list.
*/
static Node * Pull()
{
// Are there any nodes available?
if (!s_Nodes)
{
Make(); // Make some!
}
// Grab the head node
Node * node = s_Nodes;
// Promote the next node as the head
s_Nodes = node->mNext;
// Return the node
return node;
}
/* --------------------------------------------------------------------------------------------
* Return a node to the free list.
*/
static void Push(Node * node)
{
// See if the node is even valid
if (!node)
{
ThrowMemExcept("Attempting to push invalid node");
}
// Demote the current head node
node->mNext = s_Nodes;
// Promote as the head node
s_Nodes = node;
}
};
// ------------------------------------------------------------------------------------------------
MemCat::Node * MemCat::s_Nodes = nullptr;
/* ------------------------------------------------------------------------------------------------
* Lightweight memory allocator to reduce the overhead of small allocations.
*/
class Memory
{
// --------------------------------------------------------------------------------------------
friend class Buffer; // Allow the buffer type to access the memory categories.
friend class MemRef; // Allow the memory manager reference to create new instances.
private:
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Memory()
: m_Small()
, m_Medium()
, m_Large()
{
// Allocate several nodes for when memory starts pooling
MemCat::Make();
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Memory()
{
for (MemCat::Node * node = MemCat::s_Nodes, * next = nullptr; node; node = next)
{
// Save the next node
next = node->mNext;
// Release the node instance
delete node;
}
// Explicitly set the head node to null
MemCat::s_Nodes = nullptr;
}
private:
// --------------------------------------------------------------------------------------------
MemCat m_Small; // Small memory allocations of <= 1024 bytes.
MemCat m_Medium; // Medium memory allocations of <= 4096 bytes.
MemCat m_Large; // Large memory allocations of <= 4096 bytes.
};
// ------------------------------------------------------------------------------------------------
MemRef MemRef::s_Mem;
// ------------------------------------------------------------------------------------------------
void MemRef::Grab()
{
if (m_Ptr)
{
++(*m_Ref);
}
}
// ------------------------------------------------------------------------------------------------
void MemRef::Drop()
{
if (m_Ptr && --(*m_Ref) == 0)
{
delete m_Ptr;
delete m_Ref;
m_Ptr = nullptr;
m_Ref = nullptr;
}
}
// ------------------------------------------------------------------------------------------------
const MemRef & MemRef::Get()
{
if (!s_Mem.m_Ptr)
{
s_Mem.m_Ptr = new Memory();
s_Mem.m_Ref = new Counter(1);
}
return s_Mem;
}
// ------------------------------------------------------------------------------------------------
Buffer::Buffer(const Buffer & o)
: m_Ptr(nullptr)
, m_Cap(o.m_Cap)
, m_Cur(o.m_Cur)
, m_Mem(o.m_Mem)
{
if (m_Cap)
{
Request(o.m_Cap);
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
}
}
// ------------------------------------------------------------------------------------------------
Buffer::~Buffer()
{
// Do we have a buffer?
if (m_Ptr)
{
Release(); // Release it!
}
}
// ------------------------------------------------------------------------------------------------
Buffer & Buffer::operator = (const Buffer & o)
{
if (m_Ptr != o.m_Ptr)
{
// Can we work in the current buffer?
if (m_Cap && o.m_Cap <= m_Cap)
{
// It's safe to copy the data
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
}
// Do we even have data to copy?
else if (!o.m_Cap)
{
// Do we have a buffer?
if (m_Ptr)
{
Release(); // Release it!
}
}
else
{
// Do we have a buffer?
if (m_Ptr)
{
Release(); // Release it!
}
// Request a larger buffer
Request(o.m_Cap);
// Now it's safe to copy the data
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
}
// Also copy the edit cursor
m_Cur = o.m_Cur;
}
return *this;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Grow(SzType n)
{
// Backup the current memory
Buffer bkp(m_Ptr, m_Cap, m_Cur, m_Mem);
// Acquire a bigger buffer
Request(bkp.m_Cap + n);
// Copy the data from the old buffer
std::memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
// Copy the previous edit cursor
m_Cur = bkp.m_Cur;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Request(SzType n)
{
// NOTE: Function assumes (n > 0)
// Is there a memory manager available?
if (!m_Mem)
{
// Round up the size to a power of two number
n = (n & (n - 1)) ? NextPow2(n) : n;
// Allocate the memory directly
m_Ptr = AllocMem(n);
}
// Find out in which category does this buffer reside
else if (n <= 1024)
{
m_Mem->m_Small.Grab(m_Ptr, n);
}
else if (n <= 4096)
{
m_Mem->m_Medium.Grab(m_Ptr, n);
}
else
{
m_Mem->m_Large.Grab(m_Ptr, n);
}
// If no errors occurred then we can set the size
m_Cap = n;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Release()
{
// TODO: Implement a limit on how much memory can actually be pooled.
// Is there a memory manager available?
if (!m_Mem)
{
std::free(m_Ptr); // Deallocate the memory directly
}
// Find out to which category does this buffer belong
else if (m_Cap <= 1024)
{
m_Mem->m_Small.Drop(m_Ptr, m_Cap);
}
else if (m_Cap <= 4096)
{
m_Mem->m_Medium.Drop(m_Ptr, m_Cap);
}
else
{
m_Mem->m_Large.Drop(m_Ptr, m_Cap);
}
// Explicitly reset the buffer
m_Ptr = nullptr;
m_Cap = 0;
m_Cur = 0;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::Write(SzType pos, ConstPtr data, SzType size)
{
// Do we have what to write?
if (!data || !size)
{
return 0;
}
// See if the buffer size must be adjusted
else if ((pos + size) >= m_Cap)
{
// Acquire a larger buffer
Grow((pos + size) - m_Cap + 32);
}
// Copy the data into the internal buffer
std::memcpy(m_Ptr + pos, data, size);
// Return the amount of data written to the buffer
return size;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, ...)
{
// Initialize the variable argument list
va_list args;
va_start(args, fmt);
// Call the function that takes the variable argument list
const SzType ret = WriteF(pos, fmt, args);
// Finalize the variable argument list
va_end(args);
// Return the result
return ret;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, va_list args)
{
// Is the specified position within range?
if (pos >= m_Cap)
{
// Acquire a larger buffer
Grow(pos - m_Cap + 32);
}
// Backup the variable argument list
va_list args_cpy;
va_copy(args_cpy, args);
// Attempt to write to the current buffer
// (if empty, it should tell us the necessary size)
int ret = std::vsnprintf(m_Ptr + pos, m_Cap, fmt, args);
// Do we need a bigger buffer?
if ((pos + ret) >= m_Cap)
{
// Acquire a larger buffer
Grow((pos + ret) - m_Cap + 32);
// Retry writing the requested information
ret = std::vsnprintf(m_Ptr + pos, m_Cap, fmt, args_cpy);
}
// Return the value 0 if data could not be written
if (ret < 0)
{
return 0;
}
// Return the number of written characters
return static_cast< SzType >(ret);
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteS(SzType pos, ConstPtr str)
{
// Is there any string to write?
if (str && *str != '\0')
{
// Forward this to the regular write function
return Write(pos, str, std::strlen(str));
}
// Nothing to write
return 0;
}
// ------------------------------------------------------------------------------------------------
void Buffer::AppendF(const char * fmt, ...)
{
// Initialize the variable argument list
va_list args;
va_start(args, fmt);
// Forward this to the regular write function
m_Cur += WriteF(m_Cur, fmt, args);
// Finalize the variable argument list
va_end(args);
}
// ------------------------------------------------------------------------------------------------
void Buffer::AppendS(const char * str)
{
// Is there any string to write?
if (str)
{
m_Cur += Write(m_Cur, str, std::strlen(str));
}
}
} // Namespace:: SqMod

833
shared/Base/Buffer.hpp Normal file
View File

@ -0,0 +1,833 @@
#ifndef _BASE_BUFFER_HPP_
#define _BASE_BUFFER_HPP_
// ------------------------------------------------------------------------------------------------
#include <cassert>
#include <cstdarg>
// ------------------------------------------------------------------------------------------------
#include <utility>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
class Memory;
class Buffer;
/* ------------------------------------------------------------------------------------------------
* A counted reference to a memory manager instance.
*/
class MemRef
{
private:
// --------------------------------------------------------------------------------------------
static MemRef s_Mem;
// --------------------------------------------------------------------------------------------
typedef unsigned int Counter;
// --------------------------------------------------------------------------------------------
Memory* m_Ptr; // The memory manager instance.
Counter* m_Ref; // Reference count to the managed instance.
/* --------------------------------------------------------------------------------------------
* Grab a strong reference to a memory manager.
*/
void Grab();
/* --------------------------------------------------------------------------------------------
* Drop a strong reference to a memory manager.
*/
void Drop();
public:
/* --------------------------------------------------------------------------------------------
* Get a reference to the global memory manager instance.
*/
static const MemRef & Get();
/* --------------------------------------------------------------------------------------------
* Default constructor (null).
*/
MemRef()
: m_Ptr(s_Mem.m_Ptr)
, m_Ref(s_Mem.m_Ref)
{
Grab();
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
MemRef(const MemRef & o)
: m_Ptr(o.m_Ptr)
, m_Ref(o.m_Ref)
{
Grab();
}
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
MemRef(MemRef && o)
: m_Ptr(o.m_Ptr), m_Ref(o.m_Ref)
{
o.m_Ptr = nullptr;
o.m_Ref = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~MemRef()
{
Drop();
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
MemRef & operator = (const MemRef & o)
{
if (m_Ptr != o.m_Ptr)
{
Drop();
m_Ptr = o.m_Ptr;
m_Ref = o.m_Ref;
Grab();
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
MemRef & operator = (MemRef && o)
{
if (m_Ptr != o.m_Ptr)
{
Drop();
m_Ptr = o.m_Ptr;
m_Ref = o.m_Ref;
o.m_Ptr = nullptr;
o.m_Ref = nullptr;
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Perform an equality comparison between two memory managers.
*/
bool operator == (const MemRef & o) const
{
return (m_Ptr == o.m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Perform an inequality comparison between two memory managers.
*/
bool operator != (const MemRef & o) const
{
return (m_Ptr != o.m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean for use in boolean operations.
*/
operator bool () const
{
return m_Ptr;
}
/* --------------------------------------------------------------------------------------------
* Member operator for dereferencing the managed pointer.
*/
Memory * operator -> () const
{
assert(m_Ptr);
return m_Ptr;
}
/* --------------------------------------------------------------------------------------------
* Indirection operator for obtaining a reference of the managed pointer.
*/
Memory & operator * () const
{
assert(m_Ptr);
return *m_Ptr;
}
/* --------------------------------------------------------------------------------------------
* Release the reference to the managed instance.
*/
void Reset()
{
if (m_Ptr)
{
Drop();
}
}
};
// ------------------------------------------------------------------------------------------------
void ThrowMemExcept(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Reusable and re-scalable buffer memory for quick memory allocations.
*/
class Buffer
{
public:
// --------------------------------------------------------------------------------------------
typedef char Value; // The type of value used to represent a byte.
// --------------------------------------------------------------------------------------------
typedef Value & Reference; // A reference to the stored value type.
typedef const Value & ConstRef; // A const reference to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Value * Pointer; // A pointer to the stored value type.
typedef const Value * ConstPtr; // A const pointer to the stored value type.
// --------------------------------------------------------------------------------------------
typedef unsigned int SzType; // The type used to represent size in general.
private:
/* --------------------------------------------------------------------------------------------
* Construct and take ownership of the specified buffer.
*/
Buffer(Pointer & ptr, SzType & cap, SzType & cur, const MemRef & mem)
: m_Ptr(ptr)
, m_Cap(cap)
, m_Cur(cur)
, m_Mem(mem)
{
ptr = nullptr;
cap = 0;
cur = 0;
}
public:
/* --------------------------------------------------------------------------------------------
* Default constructor. (null)
*/
Buffer()
: m_Ptr(nullptr)
, m_Cap(0)
, m_Cur(0)
, m_Mem(MemRef::Get())
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Explicit size constructor.
*/
Buffer(SzType n)
: m_Ptr(nullptr)
, m_Cap(0)
, m_Cur(0)
, m_Mem(MemRef::Get())
{
Request(n < 8 ? 8 : n);
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Buffer(const Buffer & o);
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Buffer(Buffer && o)
: m_Ptr(o.m_Ptr)
, m_Cap(o.m_Cap)
, m_Cur(o.m_Cur)
, m_Mem(o.m_Mem)
{
o.m_Ptr = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Buffer();
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Buffer & operator = (const Buffer & o);
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Buffer & operator = (Buffer && o)
{
if (m_Ptr != o.m_Ptr)
{
if (m_Ptr)
{
Release();
}
m_Ptr = o.m_Ptr;
m_Cap = o.m_Cap;
m_Cur = o.m_Cur;
m_Mem = o.m_Mem;
o.m_Ptr = nullptr;
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Buffer & o) const
{
return (m_Cap == o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Buffer & o) const
{
return (m_Cap != o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Buffer & o) const
{
return (m_Cap < o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Buffer & o) const
{
return (m_Cap > o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Buffer & o) const
{
return (m_Cap <= o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Buffer & o) const
{
return (m_Cap >= o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean.
*/
operator bool () const
{
return m_Ptr;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer.
*/
Pointer Data()
{
return m_Ptr;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer.
*/
ConstPtr Data() const
{
return m_Ptr;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > T * Get()
{
return reinterpret_cast< T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > const T * Get() const
{
return reinterpret_cast< const T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Retrieve a certain element type at the specified position.
*/
template < typename T = Value > T & At(SzType n)
{
assert(n < static_cast< SzType >(m_Cap / sizeof(T)));
return reinterpret_cast< T * >(m_Ptr)[n];
}
/* --------------------------------------------------------------------------------------------
* Retrieve a certain element type at the specified position.
*/
template < typename T = Value > const T & At(SzType n) const
{
assert(n < static_cast< SzType >(m_Cap / sizeof(T)));
return reinterpret_cast< const T * >(m_Ptr)[n];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > T * Begin()
{
return reinterpret_cast< T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > const T * Begin() const
{
return reinterpret_cast< const T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > T * End()
{
return reinterpret_cast< T * >(m_Ptr) + (m_Cap / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > const T * End() const
{
return reinterpret_cast< const T * >(m_Ptr) + (m_Cap / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the front of the buffer.
*/
template < typename T = Value > T & Front()
{
assert(m_Cap >= sizeof(T));
return reinterpret_cast< T * >(m_Ptr)[0];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the front of the buffer.
*/
template < typename T = Value > const T & Front() const
{
assert(m_Cap >= sizeof(T));
return reinterpret_cast< const T * >(m_Ptr)[0];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element after the first element in the buffer.
*/
template < typename T = Value > T & Next()
{
assert(m_Cap >= (sizeof(T) * 2));
return reinterpret_cast< T * >(m_Ptr)[1];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element after the first element in the buffer.
*/
template < typename T = Value > const T & Next() const
{
assert(m_Cap >= (sizeof(T) * 2));
return reinterpret_cast< const T * >(m_Ptr)[1];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the back of the buffer.
*/
template < typename T = Value > T & Back()
{
assert(m_Cap >= sizeof(T));
return reinterpret_cast< T * >(m_Ptr)[(m_Cap / sizeof(T))-1];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the back of the buffer.
*/
template < typename T = Value > const T & Back() const
{
assert(m_Cap >= sizeof(T));
return reinterpret_cast< const T * >(m_Ptr)[(m_Cap / sizeof(T))-1];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element before the last element in the buffer.
*/
template < typename T = Value > T & Prev()
{
assert(m_Cap >= (sizeof(T) * 2));
return reinterpret_cast< T * >(m_Ptr)[(m_Cap / sizeof(T))-2];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element before the last element in the buffer.
*/
template < typename T = Value > const T & Prev() const
{
assert(m_Cap >= (sizeof(T) * 2));
return reinterpret_cast< const T * >(m_Ptr)[(m_Cap / sizeof(T))-2];
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to the specified number of elements ahead.
*/
template < typename T = Value > void Advance(SzType n)
{
// Do we need to scale the buffer?
if ((m_Cur + (n * sizeof(T))) >= m_Cap)
{
Grow(m_Cur + (n * sizeof(T)));
}
// Advance to the specified position
m_Cur += (n * sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to the specified number of elements behind.
*/
template < typename T = Value > void Retreat(SzType n)
{
// Can we move that much backward?
if ((n * sizeof(T)) <= m_Cur)
{
m_Cur -= (n * sizeof(T));
}
// Just got to the beginning
else
{
m_Cur = 0;
}
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to a fixed position within the buffer.
*/
template < typename T = Value > void Move(SzType n)
{
// Do we need to scale the buffer?
if ((n * sizeof(T)) >= m_Cap)
{
Grow(n * sizeof(T));
}
// Move to the specified position
m_Cur = (n * sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Append a value to the current cursor location and advance the cursor.
*/
template < typename T = Value > void Push(T v)
{
// Do we need to scale the buffer?
if ((m_Cur + sizeof(T)) >= m_Cap)
{
Grow(m_Cap + sizeof(T));
}
// Assign the specified value
reinterpret_cast< T * >(m_Ptr)[m_Cur] = v;
// Move to the next element
m_Cur += sizeof(T);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the cursor position.
*/
template < typename T = Value > T & Cursor()
{
assert((m_Cur / sizeof(T)) < (m_Cap / sizeof(T)));
return reinterpret_cast< T * >(m_Ptr)[m_Cur];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the cursor position.
*/
template < typename T = Value > const T & Cursor() const
{
assert((m_Cur / sizeof(T)) < (m_Cap / sizeof(T)));
return reinterpret_cast< const T * >(m_Ptr)[m_Cur];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element before the cursor position.
*/
template < typename T = Value > T & Before()
{
assert(m_Cur >= sizeof(T));
return reinterpret_cast< T * >(m_Ptr)[(m_Cur / sizeof(T))-1];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element before the cursor position.
*/
template < typename T = Value > const T & Before() const
{
assert(m_Cur >= sizeof(T));
return reinterpret_cast< const T * >(m_Ptr)[(m_Cur / sizeof(T))-1];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element after the cursor position.
*/
template < typename T = Value > T & After()
{
assert(m_Cap >= sizeof(T) && (m_Cur + sizeof(T)) <= (m_Cap - sizeof(T)));
return reinterpret_cast< T * >(m_Ptr)[(m_Cur / sizeof(T))+1];
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element after the cursor position.
*/
template < typename T = Value > const T & After() const
{
assert(m_Cap >= sizeof(T) && (m_Cur + sizeof(T)) <= (m_Cap - sizeof(T)));
return reinterpret_cast< const T * >(m_Ptr)[(m_Cur / sizeof(T))+1];
}
/* --------------------------------------------------------------------------------------------
* Retrieve maximum elements it can hold for a certain type.
*/
template < typename T = Value > static SzType Max()
{
return static_cast< SzType >(0xFFFFFFFF / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current buffer capacity in element count.
*/
template < typename T = Value > SzType Size() const
{
return static_cast< SzType >(m_Cap / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current buffer capacity in byte count.
*/
SzType Capacity() const
{
return m_Cap;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current position of the cursor in the buffer.
*/
template < typename T = Value > SzType Position() const
{
return static_cast< SzType >(m_Cur / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the amount of unused buffer after the edit cursor.
*/
template < typename T = Value > SzType Remaining() const
{
return static_cast< SzType >((m_Cap - m_Cur) / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Grow the size of the internal buffer by the specified amount of bytes.
*/
void Grow(SzType n);
/* --------------------------------------------------------------------------------------------
* Makes sure there is enough capacity to hold the specified element count.
*/
template < typename T = Value > Buffer Adjust(SzType n)
{
// Do we meet the minimum size?
if (n < 8)
{
n = 8; // Adjust to minimum size
}
// See if the requested capacity doesn't exceed the limit
if (n > Max< T >())
{
ThrowMemExcept("Requested buffer of (%u) elements exceeds the (%u) limit", n, Max< T >());
}
// Is there an existing buffer?
else if (n && !m_Cap)
{
Request(n * sizeof(T)); // Request the memory
}
// Should the size be increased?
else if (n > m_Cap)
{
// Backup the current memory
Buffer bkp(m_Ptr, m_Cap, m_Cur, m_Mem);
// Request the memory
Request(n * sizeof(T));
// Return the backup
return std::move(bkp);
}
// Return an empty buffer
return Buffer();
}
/* --------------------------------------------------------------------------------------------
* Release the managed memory.
*/
void Reset()
{
if (m_Ptr)
{
Release();
}
}
/* --------------------------------------------------------------------------------------------
* Release the managed memory and manager.
*/
void ResetAll()
{
if (m_Ptr)
{
Release();
}
m_Mem.Reset();
}
/* --------------------------------------------------------------------------------------------
* Swap the contents of two buffers.
*/
void Swap(Buffer & o)
{
Pointer p = m_Ptr;
SzType n = m_Cap;
m_Ptr = o.m_Ptr;
m_Cap = o.m_Cap;
o.m_Ptr = p;
o.m_Cap = n;
}
/* --------------------------------------------------------------------------------------------
* Write a portion of a buffer to the internal buffer.
*/
SzType Write(SzType pos, ConstPtr data, SzType size);
/* --------------------------------------------------------------------------------------------
* Write another buffer to the internal buffer.
*/
SzType Write(SzType pos, const Buffer & b)
{
return Write(pos, b.m_Ptr, b.m_Cur);
}
/* --------------------------------------------------------------------------------------------
* Write a formatted string to the internal buffer.
*/
SzType WriteF(SzType pos, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* Write a formatted string to the internal buffer.
*/
SzType WriteF(SzType pos, const char * fmt, va_list args);
/* --------------------------------------------------------------------------------------------
* Write a string to the internal buffer.
*/
SzType WriteS(SzType pos, const char * str);
/* --------------------------------------------------------------------------------------------
* Write a portion of a string to the internal buffer.
*/
SzType WriteS(SzType pos, const char * str, SzType size)
{
return Write(pos, str, size);
}
/* --------------------------------------------------------------------------------------------
* Append a portion of a buffer to the internal buffer.
*/
void Append(ConstPtr data, SzType size)
{
m_Cur += Write(m_Cur, data, size);
}
/* --------------------------------------------------------------------------------------------
* Append another buffer to the internal buffer.
*/
void Append(const Buffer & b)
{
m_Cur += Write(m_Cur, b.m_Ptr, b.m_Cur);
}
/* --------------------------------------------------------------------------------------------
* Append a formatted string to the internal buffer.
*/
void AppendF(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* Append a formatted string to the internal buffer.
*/
void AppendF(const char * fmt, va_list args)
{
m_Cur += WriteF(m_Cur, fmt, args);
}
/* --------------------------------------------------------------------------------------------
* Append a string to the internal buffer.
*/
void AppendS(const char * str);
/* --------------------------------------------------------------------------------------------
* Append a portion of a string to the internal buffer.
*/
void AppendS(const char * str, SzType size)
{
m_Cur += Write(m_Cur, str, size);
}
protected:
/* --------------------------------------------------------------------------------------------
* Request the memory specified in the capacity.
*/
void Request(SzType n);
/* --------------------------------------------------------------------------------------------
* Release the managed memory buffer.
*/
void Release();
private:
// --------------------------------------------------------------------------------------------
Pointer m_Ptr; /* Pointer to the memory buffer. */
SzType m_Cap; /* The total size of the buffer. */
SzType m_Cur; /* The buffer edit cursor. */
// --------------------------------------------------------------------------------------------
MemRef m_Mem; /* Reference to the associated memory manager. */
};
} // Namespace:: SqMod
#endif // _BASE_BUFFER_HPP_

981
shared/Base/Utility.cpp Normal file
View File

@ -0,0 +1,981 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Utility.hpp"
#include "Base/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include <ctime>
#include <cfloat>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdarg>
// ------------------------------------------------------------------------------------------------
#ifdef SQMOD_OS_WINDOWS
#include <windows.h>
#endif // SQMOD_OS_WINDOWS
// ------------------------------------------------------------------------------------------------
#ifndef SQMOD_PLUGIN_API
#include "Library/Numeric.hpp"
#include <sqstdstring.h>
#endif // SQMOD_PLUGIN_API
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
PluginFuncs* _Func = nullptr;
PluginCallbacks* _Clbk = nullptr;
PluginInfo* _Info = nullptr;
// ------------------------------------------------------------------------------------------------
#ifdef SQMOD_PLUGIN_API
HSQAPI _SqAPI = nullptr;
HSQEXPORTS _SqMod = nullptr;
HSQUIRRELVM _SqVM = nullptr;
#endif // SQMOD_PLUGIN_API
/* ------------------------------------------------------------------------------------------------
* Common buffers to reduce memory allocations. To be immediately copied upon return!
*/
static SQChar g_Buffer[4096];
static SQChar g_NumBuf[1024];
// ------------------------------------------------------------------------------------------------
SStr GetTempBuff()
{
return g_Buffer;
}
// ------------------------------------------------------------------------------------------------
Uint32 GetTempBuffSize()
{
return sizeof(g_Buffer);
}
/* --------------------------------------------------------------------------------------------
* Raw console message output.
*/
static inline void OutputMessageImpl(CCStr msg, va_list args)
{
#ifdef SQMOD_OS_WINDOWS
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csb_before;
GetConsoleScreenBufferInfo( hstdout, &csb_before);
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN);
std::printf("[SQMOD] ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
std::vprintf(msg, args);
std::puts("");
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
#else
std::printf("\033[21;32m[SQMOD]\033[0m");
std::vprintf(msg, args);
std::puts("");
#endif // SQMOD_OS_WINDOWS
}
/* --------------------------------------------------------------------------------------------
* Raw console error output.
*/
static inline void OutputErrorImpl(CCStr msg, va_list args)
{
#ifdef SQMOD_OS_WINDOWS
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csb_before;
GetConsoleScreenBufferInfo( hstdout, &csb_before);
SetConsoleTextAttribute(hstdout, FOREGROUND_RED | FOREGROUND_INTENSITY);
std::printf("[SQMOD] ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
std::vprintf(msg, args);
std::puts("");
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
#else
std::printf("\033[21;91m[SQMOD]\033[0m");
std::vprintf(msg, args);
std::puts("");
#endif // SQMOD_OS_WINDOWS
}
// --------------------------------------------------------------------------------------------
void OutputDebug(CCStr msg, ...)
{
#ifdef _DEBUG
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputMessageImpl(msg, args);
// Finalize the arguments list
va_end(args);
#else
SQMOD_UNUSED_VAR(msg);
#endif
}
// --------------------------------------------------------------------------------------------
void OutputMessage(CCStr msg, ...)
{
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputMessageImpl(msg, args);
// Finalize the arguments list
va_end(args);
}
// --------------------------------------------------------------------------------------------
void OutputError(CCStr msg, ...)
{
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputErrorImpl(msg, args);
// Finalize the arguments list
va_end(args);
}
// ------------------------------------------------------------------------------------------------
void SqThrowF(CSStr str, ...)
{
// Initialize the argument list
va_list args;
va_start (args, str);
// Write the requested contents
if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
{
// Write a generic message at least
std::strcpy(g_Buffer, "Unknown error has occurred");
}
// Finalize the argument list
va_end(args);
// Throw the exception with the resulted message
throw Sqrat::Exception(g_Buffer);
}
// ------------------------------------------------------------------------------------------------
CSStr FmtStr(CSStr str, ...)
{
// Initialize the argument list
va_list args;
va_start (args, str);
// Write the requested contents
if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
{
STHROWF("Failed to run the specified string format");
}
// Finalize the argument list
va_end(args);
// Return the data from the buffer
return g_Buffer;
}
// ------------------------------------------------------------------------------------------------
CSStr ToStrF(CSStr str, ...)
{
// Prepare the arguments list
va_list args;
va_start(args, str);
// Write the requested contents
if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
{
g_Buffer[0] = '\0'; // Make sure the string is null terminated
}
// Finalize the argument list
va_end(args);
// Return the resulted string
return g_Buffer;
}
// ------------------------------------------------------------------------------------------------
CSStr ToStringF(CSStr str, ...)
{
// Acquire a moderately sized buffer
Buffer b(128);
// Prepare the arguments list
va_list args;
va_start (args, str);
// Attempt to run the specified format
if (b.WriteF(0, str, args) == 0)
{
b.At(0) = '\0'; // Make sure the string is null terminated
}
// Finalize the argument list
va_end(args);
// Return the resulted string
return b.Get< SQChar >();
}
// ------------------------------------------------------------------------------------------------
bool SToB(CSStr str)
{
// Temporary buffer to store the lowercase string
SQChar buffer[8];
// The currently processed character
unsigned i = 0;
// Convert only the necessary characters to lowercase
while (i < 7 && *str != '\0')
{
buffer[i++] = static_cast< SQChar >(std::tolower(*(str++)));
}
// Add the null terminator
buffer[i] = '\0';
// Compare the lowercase string and return the result
return (std::strcmp(buffer, "true") == 0 || std::strcmp(buffer, "yes") == 0 ||
std::strcmp(buffer, "on") == 0 || std::strcmp(buffer, "1") == 0) ? true : false;
}
// ------------------------------------------------------------------------------------------------
Object & NullObject()
{
static Object o;
o.Release();
return o;
}
// ------------------------------------------------------------------------------------------------
Table & NullTable()
{
static Table t;
t.Release();
return t;
}
// ------------------------------------------------------------------------------------------------
Array & NullArray()
{
static Array a;
a.Release();
return a;
}
// ------------------------------------------------------------------------------------------------
Function & NullFunction()
{
static Function f;
f.Release();
return f;
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Int8 >::ToStr(Int8 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%d", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Int8 ConvNum< Int8 >::FromStr(CSStr s)
{
return ConvTo< Int8 >::From(std::strtol(s, nullptr, 10));
}
Int8 ConvNum< Int8 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Int8 >::From(std::strtol(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Uint8 >::ToStr(Uint8 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%u", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Uint8 ConvNum< Uint8 >::FromStr(CSStr s)
{
return ConvTo< Uint8 >::From(std::strtoul(s, nullptr, 10));
}
Uint8 ConvNum< Uint8 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Uint8 >::From(std::strtoul(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Int16 >::ToStr(Int16 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%d", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Int16 ConvNum< Int16 >::FromStr(CSStr s)
{
return ConvTo< Int16 >::From(std::strtol(s, nullptr, 10));
}
Int16 ConvNum< Int16 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Int16 >::From(std::strtol(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Uint16 >::ToStr(Uint16 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%u", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Uint16 ConvNum< Uint16 >::FromStr(CSStr s)
{
return ConvTo< Uint16 >::From(std::strtoul(s, nullptr, 10));
}
Uint16 ConvNum< Uint16 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Uint16 >::From(std::strtoul(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Int32 >::ToStr(Int32 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%d", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Int32 ConvNum< Int32 >::FromStr(CSStr s)
{
return ConvTo< Int32 >::From(std::strtol(s, nullptr, 10));
}
Int32 ConvNum< Int32 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Int32 >::From(std::strtol(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Uint32 >::ToStr(Uint32 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%u", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Uint32 ConvNum< Uint32 >::FromStr(CSStr s)
{
return ConvTo< Uint32 >::From(std::strtoul(s, nullptr, 10));
}
Uint32 ConvNum< Uint32 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Uint32 >::From(std::strtoul(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Int64 >::ToStr(Int64 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%lld", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Int64 ConvNum< Int64 >::FromStr(CSStr s)
{
return std::strtoll(s, nullptr, 10);
}
Int64 ConvNum< Int64 >::FromStr(CSStr s, Int32 base)
{
return std::strtoll(s, nullptr, base);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Uint64 >::ToStr(Uint64 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%llu", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Uint64 ConvNum< Uint64 >::FromStr(CSStr s)
{
return std::strtoull(s, nullptr, 10);
}
Uint64 ConvNum< Uint64 >::FromStr(CSStr s, Int32 base)
{
return std::strtoull(s, nullptr, base);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< LongI >::ToStr(LongI v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%ld", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
LongI ConvNum< LongI >::FromStr(CSStr s)
{
return std::strtol(s, nullptr, 10);
}
LongI ConvNum< LongI >::FromStr(CSStr s, Int32 base)
{
return std::strtol(s, nullptr, base);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Ulong >::ToStr(Ulong v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%lu", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Ulong ConvNum< Ulong >::FromStr(CSStr s)
{
return std::strtoul(s, nullptr, 10);
}
Ulong ConvNum< Ulong >::FromStr(CSStr s, Int32 base)
{
return std::strtoul(s, nullptr, base);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Float32 >::ToStr(Float32 v)
{
// Attempt to convert the value to a string
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%f", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the data from the buffer
return g_NumBuf;
}
Float32 ConvNum< Float32 >::FromStr(CSStr s)
{
return std::strtof(s, nullptr);
}
Float32 ConvNum< Float32 >::FromStr(CSStr s, Int32 /*base*/)
{
return std::strtof(s, nullptr);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Float64 >::ToStr(Float64 v)
{
// Attempt to convert the value to a string
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%f", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the data from the buffer
return g_NumBuf;
}
Float64 ConvNum< Float64 >::FromStr(CSStr s)
{
return std::strtod(s, nullptr);
}
Float64 ConvNum< Float64 >::FromStr(CSStr s, Int32 /*base*/)
{
return std::strtod(s, nullptr);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< bool >::ToStr(bool v)
{
if (v)
{
g_NumBuf[0] = 't';
g_NumBuf[1] = 'r';
g_NumBuf[2] = 'u';
g_NumBuf[3] = 'e';
g_NumBuf[4] = '\0';
}
else
{
g_NumBuf[0] = 'f';
g_NumBuf[1] = 'a';
g_NumBuf[2] = 'l';
g_NumBuf[3] = 's';
g_NumBuf[4] = 'e';
g_NumBuf[5] = '\0';
}
return g_NumBuf;
}
bool ConvNum< bool >::FromStr(CSStr s)
{
return (std::strcmp(s, "true") == 0) ? true : false;
}
bool ConvNum< bool >::FromStr(CSStr s, Int32 /*base*/)
{
return (std::strcmp(s, "true") == 0) ? true : false;
}
// --------------------------------------------------------------------------------------------
StackStrF::StackStrF(HSQUIRRELVM vm, SQInteger idx, bool fmt)
: mPtr(nullptr)
, mLen(-1)
, mRes(SQ_OK)
, mObj()
, mVM(vm)
{
const Int32 top = sq_gettop(vm);
// Reset the converted value object
sq_resetobject(&mObj);
// Was the string or value specified?
if (top <= (idx - 1))
{
mRes = sq_throwerror(vm, "Missing string or value");
}
// Do we have enough values to call the format function and are we allowed to?
else if (top > idx && fmt)
{
// Pointer to the generated string
SStr str = nullptr;
// Attempt to generate the specified string format
mRes = sqstd_format(vm, idx, &mLen, &str);
// Did the format succeeded but ended up with a null string pointer?
if (SQ_SUCCEEDED(mRes) && !str)
{
mRes = sq_throwerror(vm, "Unable to generate the string");
}
else
{
mPtr = const_cast< CSStr >(str);
}
}
// Is the value on the stack an actual string?
else if (sq_gettype(vm, idx) == OT_STRING)
{
// Obtain a reference to the string object
mRes = sq_getstackobj(vm, idx, &mObj);
// Could we retrieve the object from the stack?
if (SQ_SUCCEEDED(mRes))
{
// Keep a strong reference to the object
sq_addref(vm, &mObj);
// Attempt to retrieve the string value from the stack
mRes = sq_getstring(vm, idx, &mPtr);
}
// Did the retrieval succeeded but ended up with a null string pointer?
if (SQ_SUCCEEDED(mRes) && !mPtr)
{
mRes = sq_throwerror(vm, "Unable to retrieve the string");
}
}
// We have to try and convert it to string
else
{
// Attempt to convert the value from the stack to a string
mRes = sq_tostring(vm, idx);
// Could we convert the specified value to string?
if (SQ_SUCCEEDED(mRes))
{
// Obtain a reference to the resulted object
mRes = sq_getstackobj(vm, -1, &mObj);
// Could we retrieve the object from the stack?
if (SQ_SUCCEEDED(mRes))
{
// Keep a strong reference to the object
sq_addref(vm, &mObj);
// Attempt to obtain the string pointer
mRes = sq_getstring(vm, -1, &mPtr);
}
}
// Pop a value from the stack regardless of the result
sq_pop(vm, 1);
// Did the retrieval succeeded but ended up with a null string pointer?
if (SQ_SUCCEEDED(mRes) && !mPtr)
{
mRes = sq_throwerror(vm, "Unable to retrieve the value");
}
}
}
// ------------------------------------------------------------------------------------------------
StackStrF::~StackStrF()
{
if (mVM && !sq_isnull(mObj))
{
sq_release(mVM, &mObj);
}
}
// ------------------------------------------------------------------------------------------------
Object BufferToStrObj(const Buffer & b)
{
// Obtain the initial stack size
const StackGuard sg(DefaultVM::Get());
// Push the string onto the stack
sq_pushstring(DefaultVM::Get(), b.Data(), b.Position());
// Obtain the object from the stack and return it
return Var< Object >(DefaultVM::Get(), -1).value;
}
// --------------------------------------------------------------------------------------------
Object BufferToStrObj(const Buffer & b, Uint32 size)
{
// Perform a range check on the specified buffer
if (size > b.Capacity())
{
STHROWF("The specified buffer size is out of range: %u >= %u", size, b.Capacity());
}
// Obtain the initial stack size
const StackGuard sg(DefaultVM::Get());
// Push the string onto the stack
sq_pushstring(DefaultVM::Get(), b.Data(), size);
// Obtain the object from the stack and return it
return Var< Object >(DefaultVM::Get(), -1).value;
}
// ------------------------------------------------------------------------------------------------
SQInteger PopStackInteger(HSQUIRRELVM vm, SQInteger idx)
{
#ifdef SQMOD_PLUGIN_API
return _SqMod->PopStackInteger(vm, idx);
#else
// Identify which type must be extracted
switch (sq_gettype(vm, idx))
{
case OT_INTEGER:
{
SQInteger val;
sq_getinteger(vm, idx, &val);
return val;
} break;
case OT_FLOAT:
{
SQFloat val;
sq_getfloat(vm, idx, &val);
return ConvTo< SQInteger >::From(val);
} break;
case OT_BOOL:
{
SQBool val;
sq_getbool(vm, idx, &val);
return static_cast< SQInteger >(val);
} break;
case OT_STRING:
{
CSStr val = nullptr;
// Attempt to retrieve and convert the string
if (SQ_SUCCEEDED(sq_getstring(vm, idx, &val)) && val != nullptr && *val != '\0')
{
return ConvTo< SQInteger >::From(std::strtoll(val, nullptr, 10));
}
} break;
case OT_ARRAY:
case OT_TABLE:
case OT_CLASS:
case OT_USERDATA:
{
return sq_getsize(vm, idx);
} break;
case OT_INSTANCE:
{
// Attempt to treat the value as a signed long instance
try
{
return ConvTo< SQInteger >::From(Var< const SLongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
// Just ignore it...
}
// Attempt to treat the value as a unsigned long instance
try
{
return ConvTo< SQInteger >::From(Var< const ULongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
// Just ignore it...
}
// Attempt to get the size of the instance as a fallback
return sq_getsize(vm, idx);
} break;
default: break;
}
// Default to 0
return 0;
#endif // SQMOD_PLUGIN_API
}
// ------------------------------------------------------------------------------------------------
SQFloat PopStackFloat(HSQUIRRELVM vm, SQInteger idx)
{
#ifdef SQMOD_PLUGIN_API
return _SqMod->PopStackFloat(vm, idx);
#else
// Identify which type must be extracted
switch (sq_gettype(vm, idx))
{
case OT_FLOAT:
{
SQFloat val;
sq_getfloat(vm, idx, &val);
return val;
} break;
case OT_INTEGER:
{
SQInteger val;
sq_getinteger(vm, idx, &val);
return ConvTo< SQFloat >::From(val);
} break;
case OT_BOOL:
{
SQBool val;
sq_getbool(vm, idx, &val);
return ConvTo< SQFloat >::From(val);
} break;
case OT_STRING:
{
CSStr val = nullptr;
// Attempt to retrieve and convert the string
if (SQ_SUCCEEDED(sq_getstring(vm, idx, &val)) && val != nullptr && *val != '\0')
{
#ifdef SQUSEDOUBLE
return std::strtod(val, nullptr);
#else
return std::strtof(val, nullptr);
#endif // SQUSEDOUBLE
}
} break;
case OT_ARRAY:
case OT_TABLE:
case OT_CLASS:
case OT_USERDATA:
{
return ConvTo< SQFloat >::From(sq_getsize(vm, idx));
} break;
case OT_INSTANCE:
{
// Attempt to treat the value as a signed long instance
try
{
return ConvTo< SQFloat >::From(Var< const SLongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
// Just ignore it...
}
// Attempt to treat the value as a unsigned long instance
try
{
return ConvTo< SQFloat >::From(Var< const ULongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
// Just ignore it...
}
// Attempt to get the size of the instance as a fallback
return ConvTo< SQFloat >::From(sq_getsize(vm, idx));
} break;
default: break;
}
// Default to 0
return 0.0;
#endif // SQMOD_PLUGIN_API
}
// ------------------------------------------------------------------------------------------------
Int64 PopStackSLong(HSQUIRRELVM vm, SQInteger idx)
{
#ifdef SQMOD_PLUGIN_API
return _SqMod->PopStackSLong(vm, idx);
#else
// Identify which type must be extracted
switch (sq_gettype(vm, idx))
{
case OT_INTEGER:
{
SQInteger val;
sq_getinteger(vm, idx, &val);
return static_cast< Int64 >(val);
} break;
case OT_FLOAT:
{
SQFloat val;
sq_getfloat(vm, idx, &val);
return ConvTo< Int64 >::From(val);
} break;
case OT_BOOL:
{
SQBool val;
sq_getbool(vm, idx, &val);
return static_cast< Int64 >(val);
} break;
case OT_STRING:
{
CSStr val = nullptr;
// Attempt to retrieve and convert the string
if (SQ_SUCCEEDED(sq_getstring(vm, idx, &val)) && val != nullptr && *val != '\0')
{
return std::strtoll(val, nullptr, 10);
}
} break;
case OT_ARRAY:
case OT_TABLE:
case OT_CLASS:
case OT_USERDATA:
{
return static_cast< Int64 >(sq_getsize(vm, idx));
} break;
case OT_INSTANCE:
{
// Attempt to treat the value as a signed long instance
try
{
return Var< const SLongInt & >(vm, idx).value.GetNum();
}
catch (...)
{
// Just ignore it...
}
// Attempt to treat the value as a unsigned long instance
try
{
return ConvTo< Int64 >::From(Var< const ULongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
// Just ignore it...
}
// Attempt to get the size of the instance as a fallback
return static_cast< Int64 >(sq_getsize(vm, idx));
} break;
default: break;
}
// Default to 0
return 0;
#endif // SQMOD_PLUGIN_API
}
// ------------------------------------------------------------------------------------------------
Uint64 PopStackULong(HSQUIRRELVM vm, SQInteger idx)
{
#ifdef SQMOD_PLUGIN_API
return _SqMod->PopStackULong(vm, idx);
#else
// Identify which type must be extracted
switch (sq_gettype(vm, idx))
{
case OT_INTEGER:
{
SQInteger val;
sq_getinteger(vm, idx, &val);
return ConvTo< Uint64 >::From(val);
} break;
case OT_FLOAT:
{
SQFloat val;
sq_getfloat(vm, idx, &val);
return ConvTo< Uint64 >::From(val);
} break;
case OT_BOOL:
{
SQBool val;
sq_getbool(vm, idx, &val);
return ConvTo< Uint64 >::From(val);
} break;
case OT_STRING:
{
CSStr val = nullptr;
// Attempt to retrieve and convert the string
if (SQ_SUCCEEDED(sq_getstring(vm, idx, &val)) && val != nullptr && *val != '\0')
{
return std::strtoull(val, nullptr, 10);
}
} break;
case OT_ARRAY:
case OT_TABLE:
case OT_CLASS:
case OT_USERDATA:
{
return ConvTo< Uint64 >::From(sq_getsize(vm, idx));
} break;
case OT_INSTANCE:
{
// Attempt to treat the value as a signed long instance
try
{
return ConvTo< Uint64 >::From(Var< const SLongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
// Just ignore it...
}
// Attempt to treat the value as a unsigned long instance
try
{
return Var< const ULongInt & >(vm, idx).value.GetNum();
}
catch (...)
{
// Just ignore it...
}
// Attempt to get the size of the instance as a fallback
return ConvTo< Uint64 >::From(sq_getsize(vm, idx));
} break;
default: break;
}
// Default to 0
return 0;
#endif // SQMOD_PLUGIN_API
}
} // Namespace:: SqMod

1455
shared/Base/Utility.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
//
//
// Copyright (c) 2015 Sandu Liviu Catalin
// Copyright (c) 2016 Sandu Liviu Catalin (aka. S.L.C)
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
@ -34,7 +34,7 @@
extern "C" {
#endif
/*vm*/
//vm
typedef HSQUIRRELVM (*sqapi_open)(SQInteger initialstacksize);
typedef HSQUIRRELVM (*sqapi_newthread)(HSQUIRRELVM friendvm, SQInteger initialstacksize);
typedef void (*sqapi_seterrorhandler)(HSQUIRRELVM v);
@ -55,14 +55,14 @@ extern "C" {
typedef SQInteger (*sqapi_getvmstate)(HSQUIRRELVM v);
typedef SQInteger (*sqapi_getversion)();
/*compiler*/
//compiler
typedef SQRESULT (*sqapi_compile)(HSQUIRRELVM v,SQLEXREADFUNC read,SQUserPointer p,const SQChar *sourcename,SQBool raiseerror);
typedef SQRESULT (*sqapi_compilebuffer)(HSQUIRRELVM v,const SQChar *s,SQInteger size,const SQChar *sourcename,SQBool raiseerror);
typedef void (*sqapi_enabledebuginfo)(HSQUIRRELVM v, SQBool enable);
typedef void (*sqapi_notifyallexceptions)(HSQUIRRELVM v, SQBool enable);
typedef void (*sqapi_setcompilererrorhandler)(HSQUIRRELVM v,SQCOMPILERERROR f);
/*stack operations*/
//stack operations
typedef void (*sqapi_push)(HSQUIRRELVM v,SQInteger idx);
typedef void (*sqapi_pop)(HSQUIRRELVM v,SQInteger nelemstopop);
typedef void (*sqapi_poptop)(HSQUIRRELVM v);
@ -73,7 +73,7 @@ extern "C" {
typedef SQInteger (*sqapi_cmp)(HSQUIRRELVM v);
typedef void (*sqapi_move)(HSQUIRRELVM dest,HSQUIRRELVM src,SQInteger idx);
/*object creation handling*/
//object creation handling
typedef SQUserPointer (*sqapi_newuserdata)(HSQUIRRELVM v,SQUnsignedInteger size);
typedef void (*sqapi_newtable)(HSQUIRRELVM v);
typedef void (*sqapi_newtableex)(HSQUIRRELVM v,SQInteger initialcapacity);
@ -129,7 +129,7 @@ extern "C" {
typedef SQRESULT (*sqapi_getbyhandle)(HSQUIRRELVM v,SQInteger idx,const HSQMEMBERHANDLE *handle);
typedef SQRESULT (*sqapi_setbyhandle)(HSQUIRRELVM v,SQInteger idx,const HSQMEMBERHANDLE *handle);
/*object manipulation*/
//object manipulation
typedef void (*sqapi_pushroottable)(HSQUIRRELVM v);
typedef void (*sqapi_pushregistrytable)(HSQUIRRELVM v);
typedef void (*sqapi_pushconsttable)(HSQUIRRELVM v);
@ -158,7 +158,7 @@ extern "C" {
typedef SQRESULT (*sqapi_getweakrefval)(HSQUIRRELVM v,SQInteger idx);
typedef SQRESULT (*sqapi_clear)(HSQUIRRELVM v,SQInteger idx);
/*calls*/
//calls
typedef SQRESULT (*sqapi_call)(HSQUIRRELVM v,SQInteger params,SQBool retval,SQBool raiseerror);
typedef SQRESULT (*sqapi_resume)(HSQUIRRELVM v,SQBool retval,SQBool raiseerror);
typedef const SQChar* (*sqapi_getlocal)(HSQUIRRELVM v,SQUnsignedInteger level,SQUnsignedInteger idx);
@ -169,7 +169,7 @@ extern "C" {
typedef void (*sqapi_reseterror)(HSQUIRRELVM v);
typedef void (*sqapi_getlasterror)(HSQUIRRELVM v);
/*raw object handling*/
//raw object handling
typedef SQRESULT (*sqapi_getstackobj)(HSQUIRRELVM v,SQInteger idx,HSQOBJECT *po);
typedef void (*sqapi_pushobject)(HSQUIRRELVM v,HSQOBJECT obj);
typedef void (*sqapi_addref)(HSQUIRRELVM v,HSQOBJECT *po);
@ -185,44 +185,44 @@ extern "C" {
typedef SQUnsignedInteger (*sqapi_getvmrefcount)(HSQUIRRELVM v, const HSQOBJECT *po);
/*GC*/
//GC
typedef SQInteger (*sqapi_collectgarbage)(HSQUIRRELVM v);
typedef SQRESULT (*sqapi_resurrectunreachable)(HSQUIRRELVM v);
/*serialization*/
//serialization
typedef SQRESULT (*sqapi_writeclosure)(HSQUIRRELVM vm,SQWRITEFUNC writef,SQUserPointer up);
typedef SQRESULT (*sqapi_readclosure)(HSQUIRRELVM vm,SQREADFUNC readf,SQUserPointer up);
/*mem allocation*/
//mem allocation
typedef void* (*sqapi_malloc)(SQUnsignedInteger size);
typedef void* (*sqapi_realloc)(void* p,SQUnsignedInteger oldsize,SQUnsignedInteger newsize);
typedef void (*sqapi_free)(void *p,SQUnsignedInteger size);
/*debug*/
//debug
typedef SQRESULT (*sqapi_stackinfos)(HSQUIRRELVM v,SQInteger level,SQStackInfos *si);
typedef void (*sqapi_setdebughook)(HSQUIRRELVM v);
typedef void (*sqapi_setnativedebughook)(HSQUIRRELVM v,SQDEBUGHOOK hook);
/*compiler helpers*/
//compiler helpers
typedef SQRESULT (*sqapi_loadfile)(HSQUIRRELVM v,const SQChar *filename,SQBool printerror);
typedef SQRESULT (*sqapi_dofile)(HSQUIRRELVM v,const SQChar *filename,SQBool retval,SQBool printerror);
typedef SQRESULT (*sqapi_writeclosuretofile)(HSQUIRRELVM v,const SQChar *filename);
/*blob*/
//blob
typedef SQUserPointer (*sqapi_createblob)(HSQUIRRELVM v, SQInteger size);
typedef SQRESULT (*sqapi_getblob)(HSQUIRRELVM v,SQInteger idx,SQUserPointer *ptr);
typedef SQInteger (*sqapi_getblobsize)(HSQUIRRELVM v,SQInteger idx);
/*string*/
//string
typedef SQRESULT (*sqapi_format)(HSQUIRRELVM v,SQInteger nformatstringidx,SQInteger *outlen,SQChar **output);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @cond DEV
/// Allows modules to interface with Squirrel's C api without linking to the squirrel library
/// If new functions are added to the Squirrel API, they should be added here too
/// Allows modules to interface with Squirrel's C api without linking to the squirrel library.
/// If new functions are added to the Squirrel API, they should be added here too.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct {
/*vm*/
//vm
sqapi_open open;
sqapi_newthread newthread;
sqapi_seterrorhandler seterrorhandler;
@ -243,14 +243,14 @@ extern "C" {
sqapi_getvmstate getvmstate;
sqapi_getversion getversion;
/*compiler*/
//compiler
sqapi_compile compile;
sqapi_compilebuffer compilebuffer;
sqapi_enabledebuginfo enabledebuginfo;
sqapi_notifyallexceptions notifyallexceptions;
sqapi_setcompilererrorhandler setcompilererrorhandler;
/*stack operations*/
//stack operations
sqapi_push push;
sqapi_pop pop;
sqapi_poptop poptop;
@ -261,7 +261,7 @@ extern "C" {
sqapi_cmp cmp;
sqapi_move move;
/*object creation handling*/
//object creation handling
sqapi_newuserdata newuserdata;
sqapi_newtable newtable;
sqapi_newtableex newtableex;
@ -317,7 +317,7 @@ extern "C" {
sqapi_getbyhandle getbyhandle;
sqapi_setbyhandle setbyhandle;
/*object manipulation*/
//object manipulation
sqapi_pushroottable pushroottable;
sqapi_pushregistrytable pushregistrytable;
sqapi_pushconsttable pushconsttable;
@ -346,7 +346,7 @@ extern "C" {
sqapi_getweakrefval getweakrefval;
sqapi_clear clear;
/*calls*/
//calls
sqapi_call call;
sqapi_resume resume;
sqapi_getlocal getlocal;
@ -357,7 +357,7 @@ extern "C" {
sqapi_reseterror reseterror;
sqapi_getlasterror getlasterror;
/*raw object handling*/
//raw object handling
sqapi_getstackobj getstackobj;
sqapi_pushobject pushobject;
sqapi_addref addref;
@ -372,42 +372,42 @@ extern "C" {
sqapi_getobjtypetag getobjtypetag;
sqapi_getvmrefcount getvmrefcount;
/*GC*/
//GC
sqapi_collectgarbage collectgarbage;
sqapi_resurrectunreachable resurrectunreachable;
/*serialization*/
//serialization
sqapi_writeclosure writeclosure;
sqapi_readclosure readclosure;
/*mem allocation*/
//mem allocation
sqapi_malloc malloc;
sqapi_realloc realloc;
sqapi_free free;
/*debug*/
//debug
sqapi_stackinfos stackinfos;
sqapi_setdebughook setdebughook;
sqapi_setnativedebughook setnativedebughook;
/*compiler helpers*/
//compiler helpers
sqapi_loadfile loadfile;
sqapi_dofile dofile;
sqapi_writeclosuretofile writeclosuretofile;
/*blob*/
//blob
sqapi_createblob createblob;
sqapi_getblob getblob;
sqapi_getblobsize getblobsize;
/*string*/
//string
sqapi_format format;
} sq_api, SQAPI, *HSQAPI;
/// @endcond
#ifdef SQMOD_PLUGIN_API
/*vm*/
//vm
extern sqapi_open sq_open;
extern sqapi_newthread sq_newthread;
extern sqapi_seterrorhandler sq_seterrorhandler;
@ -428,14 +428,14 @@ extern "C" {
extern sqapi_getvmstate sq_getvmstate;
extern sqapi_getversion sq_getversion;
/*compiler*/
//compiler
extern sqapi_compile sq_compile;
extern sqapi_compilebuffer sq_compilebuffer;
extern sqapi_enabledebuginfo sq_enabledebuginfo;
extern sqapi_notifyallexceptions sq_notifyallexceptions;
extern sqapi_setcompilererrorhandler sq_setcompilererrorhandler;
/*stack operations*/
//stack operations
extern sqapi_push sq_push;
extern sqapi_pop sq_pop;
extern sqapi_poptop sq_poptop;
@ -446,7 +446,7 @@ extern "C" {
extern sqapi_cmp sq_cmp;
extern sqapi_move sq_move;
/*object creation handling*/
//object creation handling
extern sqapi_newuserdata sq_newuserdata;
extern sqapi_newtable sq_newtable;
extern sqapi_newtableex sq_newtableex;
@ -502,7 +502,7 @@ extern "C" {
extern sqapi_getbyhandle sq_getbyhandle;
extern sqapi_setbyhandle sq_setbyhandle;
/*object manipulation*/
//object manipulation
extern sqapi_pushroottable sq_pushroottable;
extern sqapi_pushregistrytable sq_pushregistrytable;
extern sqapi_pushconsttable sq_pushconsttable;
@ -531,7 +531,7 @@ extern "C" {
extern sqapi_getweakrefval sq_getweakrefval;
extern sqapi_clear sq_clear;
/*calls*/
//calls
extern sqapi_call sq_call;
extern sqapi_resume sq_resume;
extern sqapi_getlocal sq_getlocal;
@ -542,7 +542,7 @@ extern "C" {
extern sqapi_reseterror sq_reseterror;
extern sqapi_getlasterror sq_getlasterror;
/*raw object handling*/
//raw object handling
extern sqapi_getstackobj sq_getstackobj;
extern sqapi_pushobject sq_pushobject;
extern sqapi_addref sq_addref;
@ -557,35 +557,35 @@ extern "C" {
extern sqapi_getobjtypetag sq_getobjtypetag;
extern sqapi_getvmrefcount sq_getvmrefcount;
/*GC*/
//GC
extern sqapi_collectgarbage sq_collectgarbage;
extern sqapi_resurrectunreachable sq_resurrectunreachable;
/*serialization*/
//serialization
extern sqapi_writeclosure sq_writeclosure;
extern sqapi_readclosure sq_readclosure;
/*mem allocation*/
//mem allocation
extern sqapi_malloc sq_malloc;
extern sqapi_realloc sq_realloc;
extern sqapi_free sq_free;
/*debug*/
//debug
extern sqapi_stackinfos sq_stackinfos;
extern sqapi_setdebughook sq_setdebughook;
extern sqapi_setnativedebughook sq_setnativedebughook;
/*compiler helpers*/
//compiler helpers
extern sqapi_loadfile sqstd_loadfile;
extern sqapi_dofile sqstd_dofile;
extern sqapi_writeclosuretofile sqstd_writeclosuretofile;
/*blob*/
//blob
extern sqapi_createblob sqstd_createblob;
extern sqapi_getblob sqstd_getblob;
extern sqapi_getblobsize sqstd_getblobsize;
/*string*/
//string
extern sqapi_format sqstd_format;
#endif // SQMOD_PLUGIN_API

View File

@ -1,4 +1,5 @@
// ////////////////////////////////////////////////////////////////////////////////////////////////
#include "SqMod.h"
// This method is used to avoid compilers warning about C++ flags on C files and vice-versa.
#include "SqMod.inl"
/* This method is used toa void compilers warning about C++ flags on C files and viceversa. */

View File

@ -1,4 +1,5 @@
// ////////////////////////////////////////////////////////////////////////////////////////////////
#include "SqMod.h"
// This method is used to avoid compilers warning about C++ flags on C files and vice-versa.
#include "SqMod.inl"
/* This method is used toa void compilers warning about C++ flags on C files and viceversa. */

View File

@ -3,7 +3,7 @@
//
//
// Copyright (c) 2009 Sandu Liviu Catalin (aka. S.L.C)
// Copyright (c) 2016 Sandu Liviu Catalin (aka. S.L.C)
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
@ -78,9 +78,14 @@ extern "C" {
typedef SqInt64 (*SqEx_GetEpochTimeMilli) (void);
typedef SQRESULT (*SqEx_GetTimestamp) (HSQUIRRELVM vm, SQInteger idx, SqInt64 * num);
typedef void (*SqEx_PushTimestamp) (HSQUIRRELVM vm, SqInt64 num);
//stack utilities
typedef SQInteger (*SqEx_PopStackInteger) (HSQUIRRELVM vm, SQInteger idx);
typedef SQFloat (*SqEx_PopStackFloat) (HSQUIRRELVM vm, SQInteger idx);
typedef SqInt64 (*SqEx_PopStackSLong) (HSQUIRRELVM vm, SQInteger idx);
typedef SqUint64 (*SqEx_PopStackULong) (HSQUIRRELVM vm, SQInteger idx);
/* --------------------------------------------------------------------------------------------
* Allows modules to interface with the plugin API without linking of any sorts
* Allows modules to interface with the plug-in API without linking of any sorts
*/
typedef struct
{
@ -105,21 +110,26 @@ extern "C" {
SqEx_LogMessage LogSFtl;
//script loading
SqEx_LoadScript LoadScript;
/*long numbers*/
//long numbers
SqEx_GetSLongValue GetSLongValue;
SqEx_PushSLongObject PushSLongObject;
SqEx_GetULongValue GetULongValue;
SqEx_PushULongObject PushULongObject;
/*time utilities*/
//time utilities
SqEx_GetCurrentSysTime GetCurrentSysTime;
SqEx_GetEpochTimeMicro GetEpochTimeMicro;
SqEx_GetEpochTimeMilli GetEpochTimeMilli;
SqEx_GetTimestamp GetTimestamp;
SqEx_PushTimestamp PushTimestamp;
//stack utilities
SqEx_PopStackInteger PopStackInteger;
SqEx_PopStackFloat PopStackFloat;
SqEx_PopStackSLong PopStackSLong;
SqEx_PopStackULong PopStackULong;
} sq_exports, SQEXPORTS, *HSQEXPORTS;
/* --------------------------------------------------------------------------------------------
* Import the functions from the main squirrel plugin.
* Import the functions from the main squirrel plug-in.
*/
SQUIRREL_API HSQEXPORTS sq_api_import(PluginFuncs * vcapi);

View File

@ -1,201 +1,201 @@
// ------------------------------------------------------------------------------------------------
#ifdef SQMOD_PLUGIN_API
/*vm*/
sqapi_open sq_open = NULL;
sqapi_newthread sq_newthread = NULL;
sqapi_seterrorhandler sq_seterrorhandler = NULL;
sqapi_close sq_close = NULL;
sqapi_setforeignptr sq_setforeignptr = NULL;
sqapi_getforeignptr sq_getforeignptr = NULL;
sqapi_setsharedforeignptr sq_setsharedforeignptr = NULL;
sqapi_getsharedforeignptr sq_getsharedforeignptr = NULL;
sqapi_setvmreleasehook sq_setvmreleasehook = NULL;
sqapi_getvmreleasehook sq_getvmreleasehook = NULL;
sqapi_setsharedreleasehook sq_setsharedreleasehook = NULL;
sqapi_getsharedreleasehook sq_getsharedreleasehook = NULL;
sqapi_setprintfunc sq_setprintfunc = NULL;
sqapi_getprintfunc sq_getprintfunc = NULL;
sqapi_geterrorfunc sq_geterrorfunc = NULL;
sqapi_suspendvm sq_suspendvm = NULL;
sqapi_wakeupvm sq_wakeupvm = NULL;
sqapi_getvmstate sq_getvmstate = NULL;
sqapi_getversion sq_getversion = NULL;
//vm
sqapi_open sq_open = NULL;
sqapi_newthread sq_newthread = NULL;
sqapi_seterrorhandler sq_seterrorhandler = NULL;
sqapi_close sq_close = NULL;
sqapi_setforeignptr sq_setforeignptr = NULL;
sqapi_getforeignptr sq_getforeignptr = NULL;
sqapi_setsharedforeignptr sq_setsharedforeignptr = NULL;
sqapi_getsharedforeignptr sq_getsharedforeignptr = NULL;
sqapi_setvmreleasehook sq_setvmreleasehook = NULL;
sqapi_getvmreleasehook sq_getvmreleasehook = NULL;
sqapi_setsharedreleasehook sq_setsharedreleasehook = NULL;
sqapi_getsharedreleasehook sq_getsharedreleasehook = NULL;
sqapi_setprintfunc sq_setprintfunc = NULL;
sqapi_getprintfunc sq_getprintfunc = NULL;
sqapi_geterrorfunc sq_geterrorfunc = NULL;
sqapi_suspendvm sq_suspendvm = NULL;
sqapi_wakeupvm sq_wakeupvm = NULL;
sqapi_getvmstate sq_getvmstate = NULL;
sqapi_getversion sq_getversion = NULL;
/*compiler*/
sqapi_compile sq_compile = NULL;
sqapi_compilebuffer sq_compilebuffer = NULL;
sqapi_enabledebuginfo sq_enabledebuginfo = NULL;
sqapi_notifyallexceptions sq_notifyallexceptions = NULL;
sqapi_setcompilererrorhandler sq_setcompilererrorhandler = NULL;
//compiler
sqapi_compile sq_compile = NULL;
sqapi_compilebuffer sq_compilebuffer = NULL;
sqapi_enabledebuginfo sq_enabledebuginfo = NULL;
sqapi_notifyallexceptions sq_notifyallexceptions = NULL;
sqapi_setcompilererrorhandler sq_setcompilererrorhandler = NULL;
/*stack operations*/
sqapi_push sq_push = NULL;
sqapi_pop sq_pop = NULL;
sqapi_poptop sq_poptop = NULL;
sqapi_remove sq_remove = NULL;
sqapi_gettop sq_gettop = NULL;
sqapi_settop sq_settop = NULL;
sqapi_reservestack sq_reservestack = NULL;
sqapi_cmp sq_cmp = NULL;
sqapi_move sq_move = NULL;
//stack operations
sqapi_push sq_push = NULL;
sqapi_pop sq_pop = NULL;
sqapi_poptop sq_poptop = NULL;
sqapi_remove sq_remove = NULL;
sqapi_gettop sq_gettop = NULL;
sqapi_settop sq_settop = NULL;
sqapi_reservestack sq_reservestack = NULL;
sqapi_cmp sq_cmp = NULL;
sqapi_move sq_move = NULL;
/*object creation handling*/
sqapi_newuserdata sq_newuserdata = NULL;
sqapi_newtable sq_newtable = NULL;
sqapi_newtableex sq_newtableex = NULL;
sqapi_newarray sq_newarray = NULL;
sqapi_newclosure sq_newclosure = NULL;
sqapi_setparamscheck sq_setparamscheck = NULL;
sqapi_bindenv sq_bindenv = NULL;
sqapi_setclosureroot sq_setclosureroot = NULL;
sqapi_getclosureroot sq_getclosureroot = NULL;
sqapi_pushstring sq_pushstring = NULL;
sqapi_pushfloat sq_pushfloat = NULL;
sqapi_pushinteger sq_pushinteger = NULL;
sqapi_pushbool sq_pushbool = NULL;
sqapi_pushuserpointer sq_pushuserpointer = NULL;
sqapi_pushnull sq_pushnull = NULL;
sqapi_pushthread sq_pushthread = NULL;
sqapi_gettype sq_gettype = NULL;
sqapi_typeof sq_typeof = NULL;
sqapi_getsize sq_getsize = NULL;
sqapi_gethash sq_gethash = NULL;
sqapi_getbase sq_getbase = NULL;
sqapi_instanceof sq_instanceof = NULL;
sqapi_tostring sq_tostring = NULL;
sqapi_tobool sq_tobool = NULL;
sqapi_getstringandsize sq_getstringandsize = NULL;
sqapi_getstring sq_getstring = NULL;
sqapi_getinteger sq_getinteger = NULL;
sqapi_getfloat sq_getfloat = NULL;
sqapi_getbool sq_getbool = NULL;
sqapi_getthread sq_getthread = NULL;
sqapi_getuserpointer sq_getuserpointer = NULL;
sqapi_getuserdata sq_getuserdata = NULL;
sqapi_settypetag sq_settypetag = NULL;
sqapi_gettypetag sq_gettypetag = NULL;
sqapi_setreleasehook sq_setreleasehook = NULL;
sqapi_getreleasehook sq_getreleasehook = NULL;
sqapi_getscratchpad sq_getscratchpad = NULL;
sqapi_getfunctioninfo sq_getfunctioninfo = NULL;
sqapi_getclosureinfo sq_getclosureinfo = NULL;
sqapi_getclosurename sq_getclosurename = NULL;
sqapi_setnativeclosurename sq_setnativeclosurename = NULL;
sqapi_setinstanceup sq_setinstanceup = NULL;
sqapi_getinstanceup sq_getinstanceup = NULL;
sqapi_setclassudsize sq_setclassudsize = NULL;
sqapi_newclass sq_newclass = NULL;
sqapi_createinstance sq_createinstance = NULL;
sqapi_setattributes sq_setattributes = NULL;
sqapi_getattributes sq_getattributes = NULL;
sqapi_getclass sq_getclass = NULL;
sqapi_weakref sq_weakref = NULL;
sqapi_getdefaultdelegate sq_getdefaultdelegate = NULL;
sqapi_getmemberhandle sq_getmemberhandle = NULL;
sqapi_getbyhandle sq_getbyhandle = NULL;
sqapi_setbyhandle sq_setbyhandle = NULL;
//object creation handling
sqapi_newuserdata sq_newuserdata = NULL;
sqapi_newtable sq_newtable = NULL;
sqapi_newtableex sq_newtableex = NULL;
sqapi_newarray sq_newarray = NULL;
sqapi_newclosure sq_newclosure = NULL;
sqapi_setparamscheck sq_setparamscheck = NULL;
sqapi_bindenv sq_bindenv = NULL;
sqapi_setclosureroot sq_setclosureroot = NULL;
sqapi_getclosureroot sq_getclosureroot = NULL;
sqapi_pushstring sq_pushstring = NULL;
sqapi_pushfloat sq_pushfloat = NULL;
sqapi_pushinteger sq_pushinteger = NULL;
sqapi_pushbool sq_pushbool = NULL;
sqapi_pushuserpointer sq_pushuserpointer = NULL;
sqapi_pushnull sq_pushnull = NULL;
sqapi_pushthread sq_pushthread = NULL;
sqapi_gettype sq_gettype = NULL;
sqapi_typeof sq_typeof = NULL;
sqapi_getsize sq_getsize = NULL;
sqapi_gethash sq_gethash = NULL;
sqapi_getbase sq_getbase = NULL;
sqapi_instanceof sq_instanceof = NULL;
sqapi_tostring sq_tostring = NULL;
sqapi_tobool sq_tobool = NULL;
sqapi_getstringandsize sq_getstringandsize = NULL;
sqapi_getstring sq_getstring = NULL;
sqapi_getinteger sq_getinteger = NULL;
sqapi_getfloat sq_getfloat = NULL;
sqapi_getbool sq_getbool = NULL;
sqapi_getthread sq_getthread = NULL;
sqapi_getuserpointer sq_getuserpointer = NULL;
sqapi_getuserdata sq_getuserdata = NULL;
sqapi_settypetag sq_settypetag = NULL;
sqapi_gettypetag sq_gettypetag = NULL;
sqapi_setreleasehook sq_setreleasehook = NULL;
sqapi_getreleasehook sq_getreleasehook = NULL;
sqapi_getscratchpad sq_getscratchpad = NULL;
sqapi_getfunctioninfo sq_getfunctioninfo = NULL;
sqapi_getclosureinfo sq_getclosureinfo = NULL;
sqapi_getclosurename sq_getclosurename = NULL;
sqapi_setnativeclosurename sq_setnativeclosurename = NULL;
sqapi_setinstanceup sq_setinstanceup = NULL;
sqapi_getinstanceup sq_getinstanceup = NULL;
sqapi_setclassudsize sq_setclassudsize = NULL;
sqapi_newclass sq_newclass = NULL;
sqapi_createinstance sq_createinstance = NULL;
sqapi_setattributes sq_setattributes = NULL;
sqapi_getattributes sq_getattributes = NULL;
sqapi_getclass sq_getclass = NULL;
sqapi_weakref sq_weakref = NULL;
sqapi_getdefaultdelegate sq_getdefaultdelegate = NULL;
sqapi_getmemberhandle sq_getmemberhandle = NULL;
sqapi_getbyhandle sq_getbyhandle = NULL;
sqapi_setbyhandle sq_setbyhandle = NULL;
/*object manipulation*/
sqapi_pushroottable sq_pushroottable = NULL;
sqapi_pushregistrytable sq_pushregistrytable = NULL;
sqapi_pushconsttable sq_pushconsttable = NULL;
sqapi_setroottable sq_setroottable = NULL;
sqapi_setconsttable sq_setconsttable = NULL;
sqapi_newslot sq_newslot = NULL;
sqapi_deleteslot sq_deleteslot = NULL;
sqapi_set sq_set = NULL;
sqapi_get sq_get = NULL;
sqapi_rawget sq_rawget = NULL;
sqapi_rawset sq_rawset = NULL;
sqapi_rawdeleteslot sq_rawdeleteslot = NULL;
sqapi_newmember sq_newmember = NULL;
sqapi_rawnewmember sq_rawnewmember = NULL;
sqapi_arrayappend sq_arrayappend = NULL;
sqapi_arraypop sq_arraypop = NULL;
sqapi_arrayresize sq_arrayresize = NULL;
sqapi_arrayreverse sq_arrayreverse = NULL;
sqapi_arrayremove sq_arrayremove = NULL;
sqapi_arrayinsert sq_arrayinsert = NULL;
sqapi_setdelegate sq_setdelegate = NULL;
sqapi_getdelegate sq_getdelegate = NULL;
sqapi_clone sq_clone = NULL;
sqapi_setfreevariable sq_setfreevariable = NULL;
sqapi_next sq_next = NULL;
sqapi_getweakrefval sq_getweakrefval = NULL;
sqapi_clear sq_clear = NULL;
//object manipulation
sqapi_pushroottable sq_pushroottable = NULL;
sqapi_pushregistrytable sq_pushregistrytable = NULL;
sqapi_pushconsttable sq_pushconsttable = NULL;
sqapi_setroottable sq_setroottable = NULL;
sqapi_setconsttable sq_setconsttable = NULL;
sqapi_newslot sq_newslot = NULL;
sqapi_deleteslot sq_deleteslot = NULL;
sqapi_set sq_set = NULL;
sqapi_get sq_get = NULL;
sqapi_rawget sq_rawget = NULL;
sqapi_rawset sq_rawset = NULL;
sqapi_rawdeleteslot sq_rawdeleteslot = NULL;
sqapi_newmember sq_newmember = NULL;
sqapi_rawnewmember sq_rawnewmember = NULL;
sqapi_arrayappend sq_arrayappend = NULL;
sqapi_arraypop sq_arraypop = NULL;
sqapi_arrayresize sq_arrayresize = NULL;
sqapi_arrayreverse sq_arrayreverse = NULL;
sqapi_arrayremove sq_arrayremove = NULL;
sqapi_arrayinsert sq_arrayinsert = NULL;
sqapi_setdelegate sq_setdelegate = NULL;
sqapi_getdelegate sq_getdelegate = NULL;
sqapi_clone sq_clone = NULL;
sqapi_setfreevariable sq_setfreevariable = NULL;
sqapi_next sq_next = NULL;
sqapi_getweakrefval sq_getweakrefval = NULL;
sqapi_clear sq_clear = NULL;
/*calls*/
sqapi_call sq_call = NULL;
sqapi_resume sq_resume = NULL;
sqapi_getlocal sq_getlocal = NULL;
sqapi_getcallee sq_getcallee = NULL;
sqapi_getfreevariable sq_getfreevariable = NULL;
sqapi_throwerror sq_throwerror = NULL;
sqapi_throwobject sq_throwobject = NULL;
sqapi_reseterror sq_reseterror = NULL;
sqapi_getlasterror sq_getlasterror = NULL;
//calls
sqapi_call sq_call = NULL;
sqapi_resume sq_resume = NULL;
sqapi_getlocal sq_getlocal = NULL;
sqapi_getcallee sq_getcallee = NULL;
sqapi_getfreevariable sq_getfreevariable = NULL;
sqapi_throwerror sq_throwerror = NULL;
sqapi_throwobject sq_throwobject = NULL;
sqapi_reseterror sq_reseterror = NULL;
sqapi_getlasterror sq_getlasterror = NULL;
/*raw object handling*/
sqapi_getstackobj sq_getstackobj = NULL;
sqapi_pushobject sq_pushobject = NULL;
sqapi_addref sq_addref = NULL;
sqapi_release sq_release = NULL;
sqapi_getrefcount sq_getrefcount = NULL;
sqapi_resetobject sq_resetobject = NULL;
sqapi_objtostring sq_objtostring = NULL;
sqapi_objtobool sq_objtobool = NULL;
sqapi_objtointeger sq_objtointeger = NULL;
sqapi_objtofloat sq_objtofloat = NULL;
sqapi_objtouserpointer sq_objtouserpointer = NULL;
sqapi_getobjtypetag sq_getobjtypetag = NULL;
sqapi_getvmrefcount sq_getvmrefcount = NULL;
//raw object handling
sqapi_getstackobj sq_getstackobj = NULL;
sqapi_pushobject sq_pushobject = NULL;
sqapi_addref sq_addref = NULL;
sqapi_release sq_release = NULL;
sqapi_getrefcount sq_getrefcount = NULL;
sqapi_resetobject sq_resetobject = NULL;
sqapi_objtostring sq_objtostring = NULL;
sqapi_objtobool sq_objtobool = NULL;
sqapi_objtointeger sq_objtointeger = NULL;
sqapi_objtofloat sq_objtofloat = NULL;
sqapi_objtouserpointer sq_objtouserpointer = NULL;
sqapi_getobjtypetag sq_getobjtypetag = NULL;
sqapi_getvmrefcount sq_getvmrefcount = NULL;
/*GC*/
sqapi_collectgarbage sq_collectgarbage = NULL;
sqapi_resurrectunreachable sq_resurrectunreachable = NULL;
//GC
sqapi_collectgarbage sq_collectgarbage = NULL;
sqapi_resurrectunreachable sq_resurrectunreachable = NULL;
/*serialization*/
sqapi_writeclosure sq_writeclosure = NULL;
sqapi_readclosure sq_readclosure = NULL;
//serialization
sqapi_writeclosure sq_writeclosure = NULL;
sqapi_readclosure sq_readclosure = NULL;
/*mem allocation*/
sqapi_malloc sq_malloc = NULL;
sqapi_realloc sq_realloc = NULL;
sqapi_free sq_free = NULL;
//mem allocation
sqapi_malloc sq_malloc = NULL;
sqapi_realloc sq_realloc = NULL;
sqapi_free sq_free = NULL;
/*debug*/
sqapi_stackinfos sq_stackinfos = NULL;
sqapi_setdebughook sq_setdebughook = NULL;
sqapi_setnativedebughook sq_setnativedebughook = NULL;
//debug
sqapi_stackinfos sq_stackinfos = NULL;
sqapi_setdebughook sq_setdebughook = NULL;
sqapi_setnativedebughook sq_setnativedebughook = NULL;
/*compiler helpers*/
sqapi_loadfile sqstd_loadfile = NULL;
sqapi_dofile sqstd_dofile = NULL;
sqapi_writeclosuretofile sqstd_writeclosuretofile = NULL;
//compiler helpers
sqapi_loadfile sqstd_loadfile = NULL;
sqapi_dofile sqstd_dofile = NULL;
sqapi_writeclosuretofile sqstd_writeclosuretofile = NULL;
/*blob*/
sqapi_createblob sqstd_createblob = NULL;
sqapi_getblob sqstd_getblob = NULL;
sqapi_getblobsize sqstd_getblobsize = NULL;
//blob
sqapi_createblob sqstd_createblob = NULL;
sqapi_getblob sqstd_getblob = NULL;
sqapi_getblobsize sqstd_getblobsize = NULL;
/*string*/
sqapi_format sqstd_format = NULL;
//string
sqapi_format sqstd_format = NULL;
#endif // SQMOD_PLUGIN_API
// ------------------------------------------------------------------------------------------------
HSQEXPORTS sq_api_import(PluginFuncs * vcapi)
{
// Make sure a valid plugin api and reference to exports structure pointer was specified
// Make sure a valid plug-in api and reference to exports structure pointer was specified
if (!vcapi)
{
return NULL;
}
size_t struct_size;
// Attempt to find the main plugin ID
// Attempt to find the main plug-in ID
int plugin_id = vcapi->FindPlugin((char *)(SQMOD_HOST_NAME));
// Attempt to retrieve the plugin exports
// Attempt to retrieve the plug-in exports
const void ** plugin_exports = vcapi->GetPluginExports(plugin_id, &struct_size);
// See if we have any imports from Squirrel
if (plugin_exports == NULL || struct_size <= 0)
@ -210,11 +210,13 @@ HSQEXPORTS sq_api_import(PluginFuncs * vcapi)
SQRESULT sq_api_expand(HSQAPI sqapi)
{
if (!sqapi)
{
return SQ_ERROR;
}
#ifdef SQMOD_PLUGIN_API
/*vm*/
//vm
sq_open = sqapi->open;
sq_newthread = sqapi->newthread;
sq_seterrorhandler = sqapi->seterrorhandler;
@ -235,14 +237,14 @@ SQRESULT sq_api_expand(HSQAPI sqapi)
sq_getvmstate = sqapi->getvmstate;
sq_getversion = sqapi->getversion;
/*compiler*/
//compiler
sq_compile = sqapi->compile;
sq_compilebuffer = sqapi->compilebuffer;
sq_enabledebuginfo = sqapi->enabledebuginfo;
sq_notifyallexceptions = sqapi->notifyallexceptions;
sq_setcompilererrorhandler = sqapi->setcompilererrorhandler;
/*stack operations*/
//stack operations
sq_push = sqapi->push;
sq_pop = sqapi->pop;
sq_poptop = sqapi->poptop;
@ -253,7 +255,7 @@ SQRESULT sq_api_expand(HSQAPI sqapi)
sq_cmp = sqapi->cmp;
sq_move = sqapi->move;
/*object creation handling*/
//object creation handling
sq_newuserdata = sqapi->newuserdata;
sq_newtable = sqapi->newtable;
sq_newtableex = sqapi->newtableex;
@ -309,7 +311,7 @@ SQRESULT sq_api_expand(HSQAPI sqapi)
sq_getbyhandle = sqapi->getbyhandle;
sq_setbyhandle = sqapi->setbyhandle;
/*object manipulation*/
//object manipulation
sq_pushroottable = sqapi->pushroottable;
sq_pushregistrytable = sqapi->pushregistrytable;
sq_pushconsttable = sqapi->pushconsttable;
@ -338,7 +340,7 @@ SQRESULT sq_api_expand(HSQAPI sqapi)
sq_getweakrefval = sqapi->getweakrefval;
sq_clear = sqapi->clear;
/*calls*/
//calls
sq_call = sqapi->call;
sq_resume = sqapi->resume;
sq_getlocal = sqapi->getlocal;
@ -349,7 +351,7 @@ SQRESULT sq_api_expand(HSQAPI sqapi)
sq_reseterror = sqapi->reseterror;
sq_getlasterror = sqapi->getlasterror;
/*raw object handling*/
//raw object handling
sq_getstackobj = sqapi->getstackobj;
sq_pushobject = sqapi->pushobject;
sq_addref = sqapi->addref;
@ -364,35 +366,35 @@ SQRESULT sq_api_expand(HSQAPI sqapi)
sq_getobjtypetag = sqapi->getobjtypetag;
sq_getvmrefcount = sqapi->getvmrefcount;
/*GC*/
//GC
sq_collectgarbage = sqapi->collectgarbage;
sq_resurrectunreachable = sqapi->resurrectunreachable;
/*serialization*/
//serialization
sq_writeclosure = sqapi->writeclosure;
sq_readclosure = sqapi->readclosure;
/*mem allocation*/
//mem allocation
sq_malloc = sqapi->malloc;
sq_realloc = sqapi->realloc;
sq_free = sqapi->free;
/*debug*/
//debug
sq_stackinfos = sqapi->stackinfos;
sq_setdebughook = sqapi->setdebughook;
sq_setnativedebughook = sqapi->setnativedebughook;
/*compiler helpers*/
//compiler helpers
sqstd_loadfile = sqapi->loadfile;
sqstd_dofile = sqapi->dofile;
sqstd_writeclosuretofile = sqapi->writeclosuretofile;
/*blob*/
//blob
sqstd_createblob = sqapi->createblob;
sqstd_getblob = sqapi->getblob;
sqstd_getblobsize = sqapi->getblobsize;
/*string*/
//string
sqstd_format = sqapi->format;
#endif // SQMOD_PLUGIN_API
@ -405,7 +407,7 @@ void sq_api_collapse()
{
#ifdef SQMOD_PLUGIN_API
/*vm*/
//vm
sq_open = NULL;
sq_newthread = NULL;
sq_seterrorhandler = NULL;
@ -426,14 +428,14 @@ void sq_api_collapse()
sq_getvmstate = NULL;
sq_getversion = NULL;
/*compiler*/
//compiler
sq_compile = NULL;
sq_compilebuffer = NULL;
sq_enabledebuginfo = NULL;
sq_notifyallexceptions = NULL;
sq_setcompilererrorhandler = NULL;
/*stack operations*/
//stack operations
sq_push = NULL;
sq_pop = NULL;
sq_poptop = NULL;
@ -444,7 +446,7 @@ void sq_api_collapse()
sq_cmp = NULL;
sq_move = NULL;
/*object creation handling*/
//object creation handling
sq_newuserdata = NULL;
sq_newtable = NULL;
sq_newtableex = NULL;
@ -500,7 +502,7 @@ void sq_api_collapse()
sq_getbyhandle = NULL;
sq_setbyhandle = NULL;
/*object manipulation*/
//object manipulation
sq_pushroottable = NULL;
sq_pushregistrytable = NULL;
sq_pushconsttable = NULL;
@ -529,7 +531,7 @@ void sq_api_collapse()
sq_getweakrefval = NULL;
sq_clear = NULL;
/*calls*/
//calls
sq_call = NULL;
sq_resume = NULL;
sq_getlocal = NULL;
@ -540,7 +542,7 @@ void sq_api_collapse()
sq_reseterror = NULL;
sq_getlasterror = NULL;
/*raw object handling*/
//raw object handling
sq_getstackobj = NULL;
sq_pushobject = NULL;
sq_addref = NULL;
@ -555,35 +557,35 @@ void sq_api_collapse()
sq_getobjtypetag = NULL;
sq_getvmrefcount = NULL;
/*GC*/
//GC
sq_collectgarbage = NULL;
sq_resurrectunreachable = NULL;
/*serialization*/
//serialization
sq_writeclosure = NULL;
sq_readclosure = NULL;
/*mem allocation*/
//mem allocation
sq_malloc = NULL;
sq_realloc = NULL;
sq_free = NULL;
/*debug*/
//debug
sq_stackinfos = NULL;
sq_setdebughook = NULL;
sq_setnativedebughook = NULL;
/*compiler helpers*/
//compiler helpers
sqstd_loadfile = NULL;
sqstd_dofile = NULL;
sqstd_writeclosuretofile = NULL;
/*blob*/
//blob
sqstd_createblob = NULL;
sqstd_getblob = NULL;
sqstd_getblobsize = NULL;
/*string*/
//string
sqstd_format = NULL;
#endif // SQMOD_PLUGIN_API