Updated sqrat version to match version used by G2O_Client.

This commit is contained in:
Patrix
2021-08-28 05:00:24 +02:00
parent fb310bf1ae
commit fe8920d958
9 changed files with 117 additions and 328 deletions

View File

@@ -1,174 +0,0 @@
//
// SqratBytecode: Script bytecode saving and loading
//
//
// Copyright (c) 2009 Brandon Jones
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
#ifndef _SCRAT_BYTECODE_H_
#define _SCRAT_BYTECODE_H_
#include <squirrel.h>
#include <stdlib.h>
#include <string.h>
namespace Sqrat {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Helper class for managing Squirrel scripts bytecode
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Bytecode {
public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Default constructor
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Bytecode()
: m_data(0)
, m_size(0)
, m_readpos(0)
{
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Default destructor
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~Bytecode() {
if (m_data) {
free(m_data);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Returns pointer to bytecode
///
/// \returns Pointer to bytecode data
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline void * Data() const {
return m_data;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Copies bytecode from provided buffer
///
/// \param data Pointer to buffer containing bytecode
/// \param size Size of buffer containing bytecode
/// \returns SQ_OK on success, SQ_ERROR on failure
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SQRESULT SetData(const void * data, size_t size) {
if (m_data) {
free(m_data);
}
m_data = static_cast<char *>(malloc(size));
memcpy(m_data, data, size);
m_size = size;
m_readpos = 0;
return SQ_OK;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Appends bytecode from provided buffer
///
/// \param data Pointer to buffer containing bytecode to append
/// \param size Size of buffer containing bytecode to append
/// \returns Number of bytes appended
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SQInteger AppendData(const void * data, size_t size) {
if (!m_data) {
m_data = static_cast<char *>(malloc(size));
}
else {
m_data = static_cast<char *>(realloc(m_data, m_size + size));
}
memcpy(m_data + m_size, data, size);
m_size += size;
return static_cast<SQInteger>(size);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Reads bytecode
///
/// \param data Pointer to receiving buffer
/// \param size Number of bytes to read
/// \returns Number of read bytes or -1 if error occurs or there is no more data available
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SQInteger ReadData(void * data, size_t size) {
if (!m_data || m_size == 0 || m_readpos == m_size)
return -1;
size_t bytes_to_read = (m_readpos + size <= m_size) ? size : m_size - m_readpos;
memcpy(data, m_data + m_readpos, bytes_to_read);
m_readpos += bytes_to_read;
return static_cast<SQInteger>(bytes_to_read);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Returns bytecode size
///
/// \returns Bytecode size in bytes
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline size_t Size() const {
return m_size;
}
private:
char * m_data; // Buffer holding bytecode
size_t m_size; // Bytecode size
size_t m_readpos; // Current bytecode ReadData() position
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Helper bytecode reader callback to use with sq_readclosure
///
/// \param user_data Pointer to \a Bytecode object to read from
/// \param data Pointer to receiving buffer
/// \param size Number of bytes to read
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SQInteger BytecodeReader(SQUserPointer user_data, SQUserPointer data, SQInteger size) {
Bytecode * bytecode = reinterpret_cast<Bytecode *>(user_data);
return bytecode->ReadData(data, static_cast<size_t>(size));;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Helper bytecode writer callback to use with sq_writeclosure
///
/// \param user_data Pointer to \a Bytecode object to write to
/// \param data Pointer to bytecode data
/// \param size Number of bytes to write
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SQInteger BytecodeWriter(SQUserPointer user_data, SQUserPointer data, SQInteger size) {
Bytecode * bytecode = reinterpret_cast<Bytecode *>(user_data);
return bytecode->AppendData(data, static_cast<size_t>(size));
}
}
#endif //_SCRAT_BYTECODE_H_

View File

@@ -84,10 +84,10 @@ public:
Class(HSQUIRRELVM v, const string& className, bool createClass = true) : Object(v, false) { Class(HSQUIRRELVM v, const string& className, bool createClass = true) : Object(v, false) {
if (createClass && !ClassType<C>::hasClassData(v)) { if (createClass && !ClassType<C>::hasClassData(v)) {
sq_pushregistrytable(v); sq_pushregistrytable(v);
sq_pushstring(v, _SC("__classes"), -1); sq_pushstring(v, "__classes", -1);
if (SQ_FAILED(sq_rawget(v, -2))) { if (SQ_FAILED(sq_rawget(v, -2))) {
sq_newtable(v); sq_newtable(v);
sq_pushstring(v, _SC("__classes"), -1); sq_pushstring(v, "__classes", -1);
sq_push(v, -2); sq_push(v, -2);
sq_rawset(v, -4); sq_rawset(v, -4);
} }
@@ -932,10 +932,10 @@ public:
DerivedClass(HSQUIRRELVM v, const string& className) : Class<C, A>(v, string(), false) { DerivedClass(HSQUIRRELVM v, const string& className) : Class<C, A>(v, string(), false) {
if (!ClassType<C>::hasClassData(v)) { if (!ClassType<C>::hasClassData(v)) {
sq_pushregistrytable(v); sq_pushregistrytable(v);
sq_pushstring(v, _SC("__classes"), -1); sq_pushstring(v, "__classes", -1);
if (SQ_FAILED(sq_rawget(v, -2))) { if (SQ_FAILED(sq_rawget(v, -2))) {
sq_newtable(v); sq_newtable(v);
sq_pushstring(v, _SC("__classes"), -1); sq_pushstring(v, "__classes", -1);
sq_push(v, -2); sq_push(v, -2);
sq_rawset(v, -4); sq_rawset(v, -4);
} }

View File

@@ -97,7 +97,7 @@ public:
static inline ClassData<C>* getClassData(HSQUIRRELVM vm) { static inline ClassData<C>* getClassData(HSQUIRRELVM vm) {
sq_pushregistrytable(vm); sq_pushregistrytable(vm);
sq_pushstring(vm, _SC("__classes"), -1); sq_pushstring(vm, "__classes", -1);
#ifndef NDEBUG #ifndef NDEBUG
SQRESULT r = sq_rawget(vm, -2); SQRESULT r = sq_rawget(vm, -2);
assert(SQ_SUCCEEDED(r)); // fails if getClassData is called when the data does not exist for the given VM yet (bind the class) assert(SQ_SUCCEEDED(r)); // fails if getClassData is called when the data does not exist for the given VM yet (bind the class)
@@ -124,7 +124,7 @@ public:
static inline bool hasClassData(HSQUIRRELVM vm) { static inline bool hasClassData(HSQUIRRELVM vm) {
if (!getStaticClassData().Expired()) { if (!getStaticClassData().Expired()) {
sq_pushregistrytable(vm); sq_pushregistrytable(vm);
sq_pushstring(vm, _SC("__classes"), -1); sq_pushstring(vm, "__classes", -1);
if (SQ_SUCCEEDED(sq_rawget(vm, -2))) { if (SQ_SUCCEEDED(sq_rawget(vm, -2))) {
sq_pushstring(vm, ClassName().c_str(), -1); sq_pushstring(vm, ClassName().c_str(), -1);
if (SQ_SUCCEEDED(sq_rawget(vm, -2))) { if (SQ_SUCCEEDED(sq_rawget(vm, -2))) {
@@ -157,11 +157,23 @@ public:
SQUNUSED(size); SQUNUSED(size);
std::pair<C*, SharedPtr<typename unordered_map<C*, HSQOBJECT>::type> >* instance = reinterpret_cast<std::pair<C*, SharedPtr<typename unordered_map<C*, HSQOBJECT>::type> >*>(ptr); std::pair<C*, SharedPtr<typename unordered_map<C*, HSQOBJECT>::type> >* instance = reinterpret_cast<std::pair<C*, SharedPtr<typename unordered_map<C*, HSQOBJECT>::type> >*>(ptr);
instance->second->erase(instance->first); instance->second->erase(instance->first);
delete instance; delete instance;
return 0; return 0;
} }
static void PushInstance(HSQUIRRELVM vm, C* ptr) { static SQInteger DeleteInstanceFree(SQUserPointer ptr, SQInteger size) {
SQUNUSED(size);
std::pair<C*, SharedPtr<typename unordered_map<C*, HSQOBJECT>::type> >* instance = reinterpret_cast<std::pair<C*, SharedPtr<typename unordered_map<C*, HSQOBJECT>::type> >*>(ptr);
instance->second->erase(instance->first);
delete instance->first;
delete instance;
return 0;
}
static void PushInstance(HSQUIRRELVM vm, C* ptr, bool free = false) {
if (!ptr) { if (!ptr) {
sq_pushnull(vm); sq_pushnull(vm);
return; return;
@@ -179,7 +191,7 @@ public:
sq_createinstance(vm, -1); sq_createinstance(vm, -1);
sq_remove(vm, -2); sq_remove(vm, -2);
sq_setinstanceup(vm, -1, new std::pair<C*, SharedPtr<typename unordered_map<C*, HSQOBJECT>::type> >(ptr, cd->instances)); sq_setinstanceup(vm, -1, new std::pair<C*, SharedPtr<typename unordered_map<C*, HSQOBJECT>::type> >(ptr, cd->instances));
sq_setreleasehook(vm, -1, &DeleteInstance); free ? sq_setreleasehook(vm, -1, &DeleteInstanceFree) : sq_setreleasehook(vm, -1, &DeleteInstance);
sq_getstackobj(vm, -1, &((*cd->instances)[ptr])); sq_getstackobj(vm, -1, &((*cd->instances)[ptr]));
} }

View File

@@ -139,7 +139,7 @@ public:
/// \return True if the Function currently has a null value, otherwise false /// \return True if the Function currently has a null value, otherwise false
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool IsNull() const { bool IsNull() {
return sq_isnull(obj); return sq_isnull(obj);
} }

View File

@@ -36,6 +36,11 @@
#include "sqratOverloadMethods.h" #include "sqratOverloadMethods.h"
#include "sqratUtil.h" #include "sqratUtil.h"
#ifdef WIN32
// Windows defines fix
#undef GetObject
#endif
namespace Sqrat { namespace Sqrat {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -99,10 +104,11 @@ public:
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T> template<class T>
Object(T* instance, HSQUIRRELVM v = DefaultVM::Get()) : vm(v), release(true) { Object(T* instance, HSQUIRRELVM v = DefaultVM::Get(), bool free = false) : vm(v), release(true) {
ClassType<T>::PushInstance(vm, instance); ClassType<T>::PushInstance(vm, instance, free);
sq_getstackobj(vm, -1, &obj); sq_getstackobj(vm, -1, &obj);
sq_addref(vm, &obj); sq_addref(vm, &obj);
sq_pop(vm, 1);
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -276,44 +282,6 @@ public:
#endif #endif
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Checks if the object has a slot with a specified key
///
/// \param key Name of the key
///
/// \return True if the Object has a value associated with key, otherwise false
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool HasKey(const SQChar* key) const {
sq_pushobject(vm, GetObject());
sq_pushstring(vm, key, -1);
if (SQ_FAILED(sq_get(vm, -2))) {
sq_pop(vm, 1);
return false;
}
sq_pop(vm, 2);
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Checks if the object has a slot with a specified index
///
/// \param index Index to check
///
/// \return True if the Object has a value associated with index, otherwise false
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool HasKey(SQInteger index) const {
sq_pushobject(vm, GetObject());
sq_pushinteger(vm, index);
if (SQ_FAILED(sq_get(vm, -2))) {
sq_pop(vm, 1);
return false;
}
sq_pop(vm, 2);
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Casts the object to a certain C++ type /// Casts the object to a certain C++ type
/// ///

View File

@@ -33,7 +33,6 @@
#include <string.h> #include <string.h>
#include "sqratObject.h" #include "sqratObject.h"
#include "sqratBytecode.h"
namespace Sqrat { namespace Sqrat {
@@ -237,58 +236,6 @@ public:
#endif #endif
sq_pop(vm, 1); // needed? sq_pop(vm, 1); // needed?
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Saves script's bytecode to string
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::string SaveBytecode() {
Bytecode bytecode;
#if !defined (SCRAT_NO_ERROR_CHECKING)
if (!sq_isnull(obj)) {
sq_pushobject(vm, obj);
if (SQ_FAILED(sq_writeclosure(vm, BytecodeWriter, &bytecode))) {
SQTHROW(vm, LastErrorString(vm));
}
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, BytecodeWriter, &bytecode);
#endif
sq_pop(vm, 1); // needed?
return std::string(reinterpret_cast<char*>(bytecode.Data()), bytecode.Size());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Loads script's bytecode from string
///
/// \param str String containing script's bytecode
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool LoadBytecode(const std::string& str) {
#if !defined (SCRAT_NO_ERROR_CHECKING)
if (str.empty()) {
return false;
}
#endif
if(!sq_isnull(obj)) {
sq_release(vm, &obj);
sq_resetobject(&obj);
}
Bytecode bytecode;
bytecode.SetData(str.c_str(), str.size());
#if !defined (SCRAT_NO_ERROR_CHECKING)
if (SQ_FAILED(sq_readclosure(vm, BytecodeReader, &bytecode))) {
return false;
}
#else
sq_readclosure(vm, BytecodeReader, &bytecode);
#endif
sq_getstackobj(vm,-1,&obj);
sq_addref(vm, &obj);
sq_pop(vm, 1);
return true;
}
}; };
} }

View File

@@ -193,11 +193,11 @@ struct Var {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const T& val) { static void push(HSQUIRRELVM vm, const T& value) {
if (ClassType<T>::hasClassData(vm)) if (ClassType<T>::hasClassData(vm))
ClassType<T>::PushInstanceCopy(vm, val); ClassType<T>::PushInstanceCopy(vm, value);
else /* try integral type */ else /* try integral type */
pushAsInt<T, is_convertible<T, SQInteger>::YES>().push(vm, val); pushAsInt<T, is_convertible<T, SQInteger>::YES>().push(vm, value);
} }
private: private:
@@ -212,8 +212,8 @@ private:
template <class T2> template <class T2>
struct pushAsInt<T2, true> { struct pushAsInt<T2, true> {
void push(HSQUIRRELVM vm, const T2& val) { void push(HSQUIRRELVM vm, const T2& value) {
sq_pushinteger(vm, static_cast<SQInteger>(val)); sq_pushinteger(vm, static_cast<SQInteger>(value));
} }
}; };
}; };
@@ -268,8 +268,8 @@ private:
template <class T2> template <class T2>
struct pushAsInt<T2, true> { struct pushAsInt<T2, true> {
void push(HSQUIRRELVM vm, const T2& val) { void push(HSQUIRRELVM vm, const T2& value) {
sq_pushinteger(vm, static_cast<SQInteger>(val)); sq_pushinteger(vm, static_cast<SQInteger>(value));
} }
}; };
}; };
@@ -305,8 +305,8 @@ struct Var<T*> {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, T* val) { static void push(HSQUIRRELVM vm, T* value) {
ClassType<T>::PushInstance(vm, val); ClassType<T>::PushInstance(vm, value);
} }
}; };
@@ -341,8 +341,8 @@ struct Var<T* const> {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, T* const val) { static void push(HSQUIRRELVM vm, T* const value) {
ClassType<T>::PushInstance(vm, val); ClassType<T>::PushInstance(vm, value);
} }
}; };
@@ -377,8 +377,8 @@ struct Var<const T&> {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const T& val) { static void push(HSQUIRRELVM vm, const T& value) {
ClassType<T>::PushInstanceCopy(vm, val); ClassType<T>::PushInstanceCopy(vm, value);
} }
}; };
@@ -413,8 +413,8 @@ struct Var<const T*> {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const T* val) { static void push(HSQUIRRELVM vm, const T* value) {
ClassType<T>::PushInstance(vm, const_cast<T*>(val)); ClassType<T>::PushInstance(vm, const_cast<T*>(value));
} }
}; };
@@ -449,8 +449,8 @@ struct Var<const T* const> {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const T* const val) { static void push(HSQUIRRELVM vm, const T* const value) {
ClassType<T>::PushInstance(vm, const_cast<T*>(val)); ClassType<T>::PushInstance(vm, const_cast<T*>(value));
} }
}; };
@@ -493,8 +493,8 @@ struct Var<SharedPtr<T> > {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const SharedPtr<T>& val) { static void push(HSQUIRRELVM vm, const SharedPtr<T>& value) {
PushVarR(vm, *val); PushVarR(vm, *value);
} }
}; };
@@ -595,8 +595,8 @@ struct Var<bool> {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const bool& val) { static void push(HSQUIRRELVM vm, const bool& value) {
sq_pushbool(vm, static_cast<SQBool>(val)); sq_pushbool(vm, static_cast<SQBool>(value));
} }
}; };
@@ -628,8 +628,8 @@ struct Var<const bool&> {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const bool& val) { static void push(HSQUIRRELVM vm, const bool& value) {
sq_pushbool(vm, static_cast<SQBool>(val)); sq_pushbool(vm, static_cast<SQBool>(value));
} }
}; };
@@ -682,8 +682,8 @@ public:
/// \param len Length of the string (defaults to finding the length by searching for a terminating null-character) /// \param len Length of the string (defaults to finding the length by searching for a terminating null-character)
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const SQChar* val, SQInteger len = -1) { static void push(HSQUIRRELVM vm, const SQChar* value, SQInteger len = -1) {
sq_pushstring(vm, val, len); sq_pushstring(vm, value, len);
} }
}; };
@@ -736,8 +736,8 @@ public:
/// \param len Length of the string (defaults to finding the length by searching for a terminating null-character) /// \param len Length of the string (defaults to finding the length by searching for a terminating null-character)
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const SQChar* val, SQInteger len = -1) { static void push(HSQUIRRELVM vm, const SQChar* value, SQInteger len = -1) {
sq_pushstring(vm, val, len); sq_pushstring(vm, value, len);
} }
}; };
@@ -771,8 +771,8 @@ struct Var<string> {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const string& val) { static void push(HSQUIRRELVM vm, const string& value) {
sq_pushstring(vm, val.c_str(), val.size()); sq_pushstring(vm, value.c_str(), value.size());
} }
}; };
@@ -806,11 +806,51 @@ struct Var<const string&> {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const string& val) { static void push(HSQUIRRELVM vm, const string& value) {
sq_pushstring(vm, val.c_str(), val.size()); sq_pushstring(vm, value.c_str(), value.size());
} }
}; };
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Used to get and push SQUserPointer to and from the stack
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<>
struct Var<SQUserPointer> {
SQUserPointer value; ///< The actual value of get operations
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Attempts to get the value off the stack at idx as a bool
///
/// \param vm Target VM
/// \param idx Index trying to be read
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Var(HSQUIRRELVM vm, SQInteger idx) {
SQUserPointer sqValue;
if (sq_getuserpointer(vm, idx, &sqValue) == SQ_ERROR)
{
SQTHROW(vm, FormatTypeError(vm, idx, _SC("integer")));
value = nullptr;
return;
}
value = sqValue;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Called by Sqrat::PushVar to put a bool on the stack
///
/// \param vm Target VM
/// \param value Value to push on to the VM's stack
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const SQUserPointer value) {
sq_pushuserpointer(vm, value);
}
};
#ifdef SQUNICODE #ifdef SQUNICODE
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Used to get and push std::string to and from the stack when SQChar is not char (must define SQUNICODE) /// Used to get and push std::string to and from the stack when SQChar is not char (must define SQUNICODE)
@@ -842,8 +882,8 @@ struct Var<std::string> {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const std::string& val) { static void push(HSQUIRRELVM vm, const std::string& value) {
std::wstring s = string_to_wstring(val); std::wstring s = string_to_wstring(value);
sq_pushstring(vm, s.c_str(), s.size()); sq_pushstring(vm, s.c_str(), s.size());
} }
}; };
@@ -878,8 +918,8 @@ struct Var<const std::string&> {
/// \param value Value to push on to the VM's stack /// \param value Value to push on to the VM's stack
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const std::string& val) { static void push(HSQUIRRELVM vm, const std::string& value) {
std::wstring s = string_to_wstring(val); std::wstring s = string_to_wstring(value);
sq_pushstring(vm, s.c_str(), s.size()); sq_pushstring(vm, s.c_str(), s.size());
} }
}; };
@@ -938,8 +978,8 @@ public:
/// \param len Length of the string (defaults to finding the length by searching for a terminating null-character) /// \param len Length of the string (defaults to finding the length by searching for a terminating null-character)
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const char* val, SQInteger len = -1) { static void push(HSQUIRRELVM vm, const char* value, SQInteger len = -1) {
sq_pushstring(vm, string_to_wstring(std::string(val)).c_str(), len); sq_pushstring(vm, string_to_wstring(std::string(value)).c_str(), len);
} }
}; };
@@ -997,8 +1037,8 @@ public:
/// \param len Length of the string (defaults to finding the length by searching for a terminating null-character) /// \param len Length of the string (defaults to finding the length by searching for a terminating null-character)
/// ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void push(HSQUIRRELVM vm, const char* val, SQInteger len = -1) { static void push(HSQUIRRELVM vm, const char* value, SQInteger len = -1) {
sq_pushstring(vm, string_to_wstring(std::string(val)).c_str(), len); sq_pushstring(vm, string_to_wstring(std::string(value)).c_str(), len);
} }
}; };
#endif #endif

View File

@@ -257,7 +257,7 @@ public:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void Clear(HSQUIRRELVM vm) { static void Clear(HSQUIRRELVM vm) {
sq_pushregistrytable(vm); sq_pushregistrytable(vm);
sq_pushstring(vm, _SC("__error"), -1); sq_pushstring(vm, "__error", -1);
sq_rawdeleteslot(vm, -2, false); sq_rawdeleteslot(vm, -2, false);
sq_pop(vm, 1); sq_pop(vm, 1);
} }
@@ -272,18 +272,18 @@ public:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static string Message(HSQUIRRELVM vm) { static string Message(HSQUIRRELVM vm) {
sq_pushregistrytable(vm); sq_pushregistrytable(vm);
sq_pushstring(vm, _SC("__error"), -1); sq_pushstring(vm, "__error", -1);
if (SQ_SUCCEEDED(sq_rawget(vm, -2))) { if (SQ_SUCCEEDED(sq_rawget(vm, -2))) {
string** ud; string** ud;
sq_getuserdata(vm, -1, (SQUserPointer*)&ud, NULL); sq_getuserdata(vm, -1, (SQUserPointer*)&ud, NULL);
sq_pop(vm, 1); sq_pop(vm, 1);
string err = **ud; string err = **ud;
sq_pushstring(vm, _SC("__error"), -1); sq_pushstring(vm, "__error", -1);
sq_rawdeleteslot(vm, -2, false); sq_rawdeleteslot(vm, -2, false);
sq_pop(vm, 1); sq_pop(vm, 1);
return err; return err;
} }
sq_pushstring(vm, _SC("__error"), -1); sq_pushstring(vm, "__error", -1);
sq_rawdeleteslot(vm, -2, false); sq_rawdeleteslot(vm, -2, false);
sq_pop(vm, 1); sq_pop(vm, 1);
return string(_SC("an unknown error has occurred")); return string(_SC("an unknown error has occurred"));
@@ -299,7 +299,7 @@ public:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static bool Occurred(HSQUIRRELVM vm) { static bool Occurred(HSQUIRRELVM vm) {
sq_pushregistrytable(vm); sq_pushregistrytable(vm);
sq_pushstring(vm, _SC("__error"), -1); sq_pushstring(vm, "__error", -1);
if (SQ_SUCCEEDED(sq_rawget(vm, -2))) { if (SQ_SUCCEEDED(sq_rawget(vm, -2))) {
sq_pop(vm, 2); sq_pop(vm, 2);
return true; return true;
@@ -317,9 +317,9 @@ public:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void Throw(HSQUIRRELVM vm, const string& err) { static void Throw(HSQUIRRELVM vm, const string& err) {
sq_pushregistrytable(vm); sq_pushregistrytable(vm);
sq_pushstring(vm, _SC("__error"), -1); sq_pushstring(vm, "__error", -1);
if (SQ_FAILED(sq_rawget(vm, -2))) { if (SQ_FAILED(sq_rawget(vm, -2))) {
sq_pushstring(vm, _SC("__error"), -1); sq_pushstring(vm, "__error", -1);
string** ud = reinterpret_cast<string**>(sq_newuserdata(vm, sizeof(string*))); string** ud = reinterpret_cast<string**>(sq_newuserdata(vm, sizeof(string*)));
*ud = new string(err); *ud = new string(err);
sq_setreleasehook(vm, -1, &error_cleanup_hook); sq_setreleasehook(vm, -1, &error_cleanup_hook);

View File

@@ -121,12 +121,8 @@ private:
SQInteger column) SQInteger column)
{ {
SQChar buf[512]; SQChar buf[512];
#ifdef _MSC_VER scsprintf(buf, _SC("%s:%d:%d: %s"), source, (int) line, (int) column, desc);
scsprintf(buf, 512, _SC("%s:%d:%d: %s"), source, (int) line, (int) column, desc); buf[sizeof(buf) - 1] = 0;
#else
scsprintf(buf, _SC("%s:%d:%d: %s"), source, (int) line, (int) column, desc);
#endif
buf[sizeof(buf)/sizeof(SQChar) - 1] = 0;
s_getVM(v)->m_lastErrorMsg = buf; s_getVM(v)->m_lastErrorMsg = buf;
} }