6 static SQInteger
getVarInfo(StackHandler & sa,VarRefPtr & vr) {
7 HSQOBJECT htable = sa.GetObjectHandle(1);
8 SquirrelObject table(htable);
10 SQObjectType type = (SQObjectType)sa.GetType(2);
12 const SQChar * el = sa.GetString(2);
13 ScriptStringVar256 varNameTag;
14 getVarNameTag(varNameTag,
sizeof(varNameTag),el);
16 if (!table.RawGetUserData(varNameTag,&data)) {
18 return sa.ThrowError(sqT(
"getVarInfo: Could not retrieve UserData"));
25 HSQOBJECT ho = sa.GetObjectHandle(1);
26 SquirrelObject instance(ho);
28 SQObjectType type = (SQObjectType)sa.GetType(2);
30 const SQChar * el = sa.GetString(2);
31 ScriptStringVar256 varNameTag;
32 getVarNameTag(varNameTag,
sizeof(varNameTag),el);
33 SQUserPointer ivrData=0;
34 if (!instance.RawGetUserData(varNameTag,&ivrData)) {
36 return sa.ThrowError(sqT(
"getInstanceVarInfo: Could not retrieve UserData"));
38 vr = (VarRefPtr)ivrData;
40 if (!(vr->access & (VAR_ACCESS_STATIC|VAR_ACCESS_CONSTANT))) {
41 #ifdef SQ_USE_CLASS_INHERITANCE 42 SQUserPointer typetag; instance.GetTypeTag(&typetag);
43 if (typetag != vr->instanceType) {
44 SquirrelObject typeTable = instance.GetValue(SQ_CLASS_OBJECT_TABLE_NAME);
45 up = (
unsigned char *)typeTable.GetUserPointer(INT_T((
size_t)vr->instanceType));
47 throw SquirrelError(sqT(
"Invalid Instance Type"));
50 up = (
unsigned char *)instance.GetInstanceUP(0);
53 up = (
unsigned char *)instance.GetInstanceUP(0);
55 up += (size_t)vr->offsetOrAddrOrConst;
57 up = (
unsigned char *)vr->offsetOrAddrOrConst;
63 static SQInteger
setVar(StackHandler & sa,VarRef * vr,
void * data) {
64 if (vr->access & (VAR_ACCESS_READ_ONLY|VAR_ACCESS_CONSTANT)) {
65 ScriptStringVar256 msg;
66 const SQChar * el = sa.GetString(2);
67 SCSNPRINTF(msg.s,
sizeof(msg.s),sqT(
"setVar(): Cannot write to constant: %s"),el);
68 throw SquirrelError(msg.s);
71 case TypeInfo<INT_T>::TypeID: {
72 INT_T * val = (INT_T *)data;
75 return sa.Return(*val);
79 case TypeInfo<FLOAT_T>::TypeID: {
80 FLOAT_T * val = (FLOAT_T *)data;
82 *val = sa.GetFloat(3);
83 return sa.Return(*val);
87 case TypeInfo<bool>::TypeID: {
88 bool * val = (
bool *)data;
90 *val = sa.GetBool(3) ? true :
false;
91 return sa.Return(*val);
95 case VAR_TYPE_INSTANCE: {
100 SQUserPointer src = sa.GetInstanceUp(3,(SQUserPointer)vr->copyFunc);
101 if (!src)
throw SquirrelError(sqT(
"INSTANCE type assignment mismatch"));
102 vr->copyFunc(data,src);
103 #if 0 // Return an instance on the stack (allocates memory) 105 ScriptStringVar256 msg;
106 SCSNPRINTF(msg.s,
sizeof(msg),sqT(
"getVar(): Could not create instance: %s"),vr->typeName);
107 throw SquirrelError(msg.s);
110 #else // Don't return on stack. 114 case TypeInfo<SQUserPointer>::TypeID: {
115 ScriptStringVar256 msg;
116 const SQChar * el = sa.GetString(2);
117 SCSNPRINTF(msg.s,
sizeof(msg.s),sqT(
"setVar(): Cannot write to an SQUserPointer: %s"),el);
118 throw SquirrelError(msg.s);
120 case TypeInfo<ScriptStringVarBase>::TypeID: {
121 ScriptStringVarBase * val = (ScriptStringVarBase *)data;
123 const SQChar * strVal = sa.GetString(3);
126 return sa.Return(val->s);
137 static SQInteger
getVar(StackHandler & sa,VarRef * vr,
void * data) {
139 case TypeInfo<INT_T>::TypeID: {
140 if (!(vr->access & VAR_ACCESS_CONSTANT)) {
141 INT_T * val = (INT_T *)data;
143 return sa.Return(*val);
146 INT_T * val = (INT_T *)&data;
147 return sa.Return(*val);
151 case TypeInfo<FLOAT_T>::TypeID: {
152 if (!(vr->access & VAR_ACCESS_CONSTANT)) {
153 FLOAT_T * val = (FLOAT_T *)data;
155 return sa.Return(*val);
158 FLOAT_T * val = (FLOAT_T *)&data;
159 return sa.Return(*val);
163 case TypeInfo<bool>::TypeID: {
164 if (!(vr->access & VAR_ACCESS_CONSTANT)) {
165 bool * val = (
bool *)data;
167 return sa.Return(*val);
170 bool * val = (
bool *)&data;
171 return sa.Return(*val);
175 case VAR_TYPE_INSTANCE:
177 ScriptStringVar256 msg;
178 SCSNPRINTF(msg.s,
sizeof(msg.s),sqT(
"getVar(): Could not create instance: %s"),vr->typeName);
179 throw SquirrelError(msg.s);
182 case TypeInfo<SQUserPointer>::TypeID: {
183 return sa.Return(data);
185 case TypeInfo<ScriptStringVarBase>::TypeID: {
186 if (!(vr->access & VAR_ACCESS_CONSTANT)) {
187 ScriptStringVarBase * val = (ScriptStringVarBase *)data;
189 return sa.Return(val->s);
192 throw SquirrelError(sqT(
"getVar(): Invalid type+access: 'ScriptStringVarBase' with VAR_ACCESS_CONSTANT (use VAR_ACCESS_READ_ONLY instead)"));
196 case TypeInfo<const SQChar *>::TypeID: {
197 if (!(vr->access & VAR_ACCESS_CONSTANT)) {
198 throw SquirrelError(sqT(
"getVar(): Invalid type+access: 'const SQChar *' without VAR_ACCESS_CONSTANT"));
200 return sa.Return((
const SQChar *)data);
214 if (sa.GetType(1) == OT_TABLE) {
217 if (res != SQ_OK)
return res;
218 return setVar(sa,vr,vr->offsetOrAddrOrConst);
225 if (sa.GetType(1) == OT_TABLE) {
228 if (res != SQ_OK)
return res;
229 return getVar(sa,vr,vr->offsetOrAddrOrConst);
238 if (sa.GetType(1) == OT_INSTANCE) {
242 if (res != SQ_OK)
return res;
243 return setVar(sa,vr,data);
250 if (sa.GetType(1) == OT_INSTANCE) {
254 if (res != SQ_OK)
return res;
255 return getVar(sa,vr,data);
262 BOOL_T
CreateClass(HSQUIRRELVM v,SquirrelObject & newClass,SQUserPointer classType,
const SQChar * name,
const SQChar * baseName) {
270 if (SQ_FAILED(
sq_get(v,-3))) {
279 newClass.AttachToStackObject(-1);
286 SquirrelObject
RegisterClassType(HSQUIRRELVM v,
const SQChar * scriptClassName,SQUserPointer classType,SQFUNCTION constructor) {
288 SquirrelObject newClass;
289 if (
CreateClass(v,newClass,classType,scriptClassName)) {
290 SquirrelVM::CreateFunction(newClass,constructor,sqT(
"constructor"));
SQInteger setInstanceVarFunc(HSQUIRRELVM v)
SQRESULT sq_get(HSQUIRRELVM v, SQInteger idx)
SQRESULT sq_newclass(HSQUIRRELVM v, SQBool hasbase)
static SQInteger setVar(StackHandler &sa, VarRef *vr, void *data)
BOOL_T CreateNativeClassInstance(HSQUIRRELVM v, const SQChar *classname, SQUserPointer ud, SQRELEASEHOOK hook)
void sq_pop(HSQUIRRELVM v, SQInteger nelemstopop)
SQInteger getVarFunc(HSQUIRRELVM v)
void sq_pushroottable(HSQUIRRELVM v)
SquirrelObject RegisterClassType(HSQUIRRELVM v, const SQChar *scriptClassName, SQUserPointer classType, SQFUNCTION constructor)
SQInteger sq_gettop(HSQUIRRELVM v)
static SQInteger getInstanceVarInfo(StackHandler &sa, VarRefPtr &vr, SQUserPointer &data)
void sq_settop(HSQUIRRELVM v, SQInteger newtop)
void sq_pushstring(HSQUIRRELVM v, const SQChar *s, SQInteger len)
SQInteger setVarFunc(HSQUIRRELVM v)
BOOL_T CreateClass(HSQUIRRELVM v, SquirrelObject &newClass, SQUserPointer classType, const SQChar *name, const SQChar *baseName)
SQInteger getInstanceVarFunc(HSQUIRRELVM v)
SQRESULT sq_settypetag(HSQUIRRELVM v, SQInteger idx, SQUserPointer typetag)
static SQInteger getVar(StackHandler &sa, VarRef *vr, void *data)
static SQInteger getVarInfo(StackHandler &sa, VarRefPtr &vr)