The Squirrel interpreter. See http://www.squirrel-lang.org/
squirrel/sqapi.cpp@3:7268a3ceaffc, 2014-12-16 (annotated)
- Committer:
- jhnwkmn
- Date:
- Tue Dec 16 11:39:42 2014 +0000
- Revision:
- 3:7268a3ceaffc
- Parent:
- 0:97a4f8cc534c
Accepts \r as line terminator as well.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhnwkmn | 0:97a4f8cc534c | 1 | /* |
jhnwkmn | 0:97a4f8cc534c | 2 | see copyright notice in squirrel.h |
jhnwkmn | 0:97a4f8cc534c | 3 | */ |
jhnwkmn | 0:97a4f8cc534c | 4 | #include "sqpcheader.h" |
jhnwkmn | 0:97a4f8cc534c | 5 | #include "sqvm.h" |
jhnwkmn | 0:97a4f8cc534c | 6 | #include "sqstring.h" |
jhnwkmn | 0:97a4f8cc534c | 7 | #include "sqtable.h" |
jhnwkmn | 0:97a4f8cc534c | 8 | #include "sqarray.h" |
jhnwkmn | 0:97a4f8cc534c | 9 | #include "sqfuncproto.h" |
jhnwkmn | 0:97a4f8cc534c | 10 | #include "sqclosure.h" |
jhnwkmn | 0:97a4f8cc534c | 11 | #include "squserdata.h" |
jhnwkmn | 0:97a4f8cc534c | 12 | #include "sqcompiler.h" |
jhnwkmn | 0:97a4f8cc534c | 13 | #include "sqfuncstate.h" |
jhnwkmn | 0:97a4f8cc534c | 14 | #include "sqclass.h" |
jhnwkmn | 0:97a4f8cc534c | 15 | |
jhnwkmn | 0:97a4f8cc534c | 16 | bool sq_aux_gettypedarg(HSQUIRRELVM v,SQInteger idx,SQObjectType type,SQObjectPtr **o) |
jhnwkmn | 0:97a4f8cc534c | 17 | { |
jhnwkmn | 0:97a4f8cc534c | 18 | *o = &stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 19 | if(type(**o) != type){ |
jhnwkmn | 0:97a4f8cc534c | 20 | SQObjectPtr oval = v->PrintObjVal(**o); |
jhnwkmn | 0:97a4f8cc534c | 21 | v->Raise_Error(_SC("wrong argument type, expected '%s' got '%.50s'"),IdType2Name(type),_stringval(oval)); |
jhnwkmn | 0:97a4f8cc534c | 22 | return false; |
jhnwkmn | 0:97a4f8cc534c | 23 | } |
jhnwkmn | 0:97a4f8cc534c | 24 | return true; |
jhnwkmn | 0:97a4f8cc534c | 25 | } |
jhnwkmn | 0:97a4f8cc534c | 26 | |
jhnwkmn | 0:97a4f8cc534c | 27 | #define _GETSAFE_OBJ(v,idx,type,o) { if(!sq_aux_gettypedarg(v,idx,type,&o)) return SQ_ERROR; } |
jhnwkmn | 0:97a4f8cc534c | 28 | |
jhnwkmn | 0:97a4f8cc534c | 29 | #define sq_aux_paramscheck(v,count) \ |
jhnwkmn | 0:97a4f8cc534c | 30 | { \ |
jhnwkmn | 0:97a4f8cc534c | 31 | if(sq_gettop(v) < count){ v->Raise_Error(_SC("not enough params in the stack")); return SQ_ERROR; }\ |
jhnwkmn | 0:97a4f8cc534c | 32 | } |
jhnwkmn | 0:97a4f8cc534c | 33 | |
jhnwkmn | 0:97a4f8cc534c | 34 | |
jhnwkmn | 0:97a4f8cc534c | 35 | SQInteger sq_aux_invalidtype(HSQUIRRELVM v,SQObjectType type) |
jhnwkmn | 0:97a4f8cc534c | 36 | { |
jhnwkmn | 0:97a4f8cc534c | 37 | scsprintf(_ss(v)->GetScratchPad(100), _SC("unexpected type %s"), IdType2Name(type)); |
jhnwkmn | 0:97a4f8cc534c | 38 | return sq_throwerror(v, _ss(v)->GetScratchPad(-1)); |
jhnwkmn | 0:97a4f8cc534c | 39 | } |
jhnwkmn | 0:97a4f8cc534c | 40 | |
jhnwkmn | 0:97a4f8cc534c | 41 | HSQUIRRELVM sq_open(SQInteger initialstacksize) |
jhnwkmn | 0:97a4f8cc534c | 42 | { |
jhnwkmn | 0:97a4f8cc534c | 43 | SQSharedState *ss; |
jhnwkmn | 0:97a4f8cc534c | 44 | SQVM *v; |
jhnwkmn | 0:97a4f8cc534c | 45 | sq_new(ss, SQSharedState); |
jhnwkmn | 0:97a4f8cc534c | 46 | ss->Init(); |
jhnwkmn | 0:97a4f8cc534c | 47 | v = (SQVM *)SQ_MALLOC(sizeof(SQVM)); |
jhnwkmn | 0:97a4f8cc534c | 48 | new (v) SQVM(ss); |
jhnwkmn | 0:97a4f8cc534c | 49 | ss->_root_vm = v; |
jhnwkmn | 0:97a4f8cc534c | 50 | if(v->Init(NULL, initialstacksize)) { |
jhnwkmn | 0:97a4f8cc534c | 51 | return v; |
jhnwkmn | 0:97a4f8cc534c | 52 | } else { |
jhnwkmn | 0:97a4f8cc534c | 53 | sq_delete(v, SQVM); |
jhnwkmn | 0:97a4f8cc534c | 54 | return NULL; |
jhnwkmn | 0:97a4f8cc534c | 55 | } |
jhnwkmn | 0:97a4f8cc534c | 56 | return v; |
jhnwkmn | 0:97a4f8cc534c | 57 | } |
jhnwkmn | 0:97a4f8cc534c | 58 | |
jhnwkmn | 0:97a4f8cc534c | 59 | HSQUIRRELVM sq_newthread(HSQUIRRELVM friendvm, SQInteger initialstacksize) |
jhnwkmn | 0:97a4f8cc534c | 60 | { |
jhnwkmn | 0:97a4f8cc534c | 61 | SQSharedState *ss; |
jhnwkmn | 0:97a4f8cc534c | 62 | SQVM *v; |
jhnwkmn | 0:97a4f8cc534c | 63 | ss=_ss(friendvm); |
jhnwkmn | 0:97a4f8cc534c | 64 | |
jhnwkmn | 0:97a4f8cc534c | 65 | v= (SQVM *)SQ_MALLOC(sizeof(SQVM)); |
jhnwkmn | 0:97a4f8cc534c | 66 | new (v) SQVM(ss); |
jhnwkmn | 0:97a4f8cc534c | 67 | |
jhnwkmn | 0:97a4f8cc534c | 68 | if(v->Init(friendvm, initialstacksize)) { |
jhnwkmn | 0:97a4f8cc534c | 69 | friendvm->Push(v); |
jhnwkmn | 0:97a4f8cc534c | 70 | return v; |
jhnwkmn | 0:97a4f8cc534c | 71 | } else { |
jhnwkmn | 0:97a4f8cc534c | 72 | sq_delete(v, SQVM); |
jhnwkmn | 0:97a4f8cc534c | 73 | return NULL; |
jhnwkmn | 0:97a4f8cc534c | 74 | } |
jhnwkmn | 0:97a4f8cc534c | 75 | } |
jhnwkmn | 0:97a4f8cc534c | 76 | |
jhnwkmn | 0:97a4f8cc534c | 77 | SQInteger sq_getvmstate(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 78 | { |
jhnwkmn | 0:97a4f8cc534c | 79 | if(v->_suspended) |
jhnwkmn | 0:97a4f8cc534c | 80 | return SQ_VMSTATE_SUSPENDED; |
jhnwkmn | 0:97a4f8cc534c | 81 | else { |
jhnwkmn | 0:97a4f8cc534c | 82 | if(v->_callsstacksize != 0) return SQ_VMSTATE_RUNNING; |
jhnwkmn | 0:97a4f8cc534c | 83 | else return SQ_VMSTATE_IDLE; |
jhnwkmn | 0:97a4f8cc534c | 84 | } |
jhnwkmn | 0:97a4f8cc534c | 85 | } |
jhnwkmn | 0:97a4f8cc534c | 86 | |
jhnwkmn | 0:97a4f8cc534c | 87 | void sq_seterrorhandler(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 88 | { |
jhnwkmn | 0:97a4f8cc534c | 89 | SQObject o = stack_get(v, -1); |
jhnwkmn | 0:97a4f8cc534c | 90 | if(sq_isclosure(o) || sq_isnativeclosure(o) || sq_isnull(o)) { |
jhnwkmn | 0:97a4f8cc534c | 91 | v->_errorhandler = o; |
jhnwkmn | 0:97a4f8cc534c | 92 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 93 | } |
jhnwkmn | 0:97a4f8cc534c | 94 | } |
jhnwkmn | 0:97a4f8cc534c | 95 | |
jhnwkmn | 0:97a4f8cc534c | 96 | void sq_setnativedebughook(HSQUIRRELVM v,SQDEBUGHOOK hook) |
jhnwkmn | 0:97a4f8cc534c | 97 | { |
jhnwkmn | 0:97a4f8cc534c | 98 | v->_debughook_native = hook; |
jhnwkmn | 0:97a4f8cc534c | 99 | v->_debughook_closure.Null(); |
jhnwkmn | 0:97a4f8cc534c | 100 | v->_debughook = hook?true:false; |
jhnwkmn | 0:97a4f8cc534c | 101 | } |
jhnwkmn | 0:97a4f8cc534c | 102 | |
jhnwkmn | 0:97a4f8cc534c | 103 | void sq_setdebughook(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 104 | { |
jhnwkmn | 0:97a4f8cc534c | 105 | SQObject o = stack_get(v,-1); |
jhnwkmn | 0:97a4f8cc534c | 106 | if(sq_isclosure(o) || sq_isnativeclosure(o) || sq_isnull(o)) { |
jhnwkmn | 0:97a4f8cc534c | 107 | v->_debughook_closure = o; |
jhnwkmn | 0:97a4f8cc534c | 108 | v->_debughook_native = NULL; |
jhnwkmn | 0:97a4f8cc534c | 109 | v->_debughook = !sq_isnull(o); |
jhnwkmn | 0:97a4f8cc534c | 110 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 111 | } |
jhnwkmn | 0:97a4f8cc534c | 112 | } |
jhnwkmn | 0:97a4f8cc534c | 113 | |
jhnwkmn | 0:97a4f8cc534c | 114 | void sq_close(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 115 | { |
jhnwkmn | 0:97a4f8cc534c | 116 | SQSharedState *ss = _ss(v); |
jhnwkmn | 0:97a4f8cc534c | 117 | _thread(ss->_root_vm)->Finalize(); |
jhnwkmn | 0:97a4f8cc534c | 118 | sq_delete(ss, SQSharedState); |
jhnwkmn | 0:97a4f8cc534c | 119 | } |
jhnwkmn | 0:97a4f8cc534c | 120 | |
jhnwkmn | 0:97a4f8cc534c | 121 | SQInteger sq_getversion() |
jhnwkmn | 0:97a4f8cc534c | 122 | { |
jhnwkmn | 0:97a4f8cc534c | 123 | return SQUIRREL_VERSION_NUMBER; |
jhnwkmn | 0:97a4f8cc534c | 124 | } |
jhnwkmn | 0:97a4f8cc534c | 125 | |
jhnwkmn | 0:97a4f8cc534c | 126 | SQRESULT sq_compile(HSQUIRRELVM v,SQLEXREADFUNC read,SQUserPointer p,const SQChar *sourcename,SQBool raiseerror) |
jhnwkmn | 0:97a4f8cc534c | 127 | { |
jhnwkmn | 0:97a4f8cc534c | 128 | SQObjectPtr o; |
jhnwkmn | 0:97a4f8cc534c | 129 | #ifndef NO_COMPILER |
jhnwkmn | 0:97a4f8cc534c | 130 | if(Compile(v, read, p, sourcename, o, raiseerror?true:false, _ss(v)->_debuginfo)) { |
jhnwkmn | 0:97a4f8cc534c | 131 | v->Push(SQClosure::Create(_ss(v), _funcproto(o))); |
jhnwkmn | 0:97a4f8cc534c | 132 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 133 | } |
jhnwkmn | 0:97a4f8cc534c | 134 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 135 | #else |
jhnwkmn | 0:97a4f8cc534c | 136 | return sq_throwerror(v,_SC("this is a no compiler build")); |
jhnwkmn | 0:97a4f8cc534c | 137 | #endif |
jhnwkmn | 0:97a4f8cc534c | 138 | } |
jhnwkmn | 0:97a4f8cc534c | 139 | |
jhnwkmn | 0:97a4f8cc534c | 140 | void sq_enabledebuginfo(HSQUIRRELVM v, SQBool enable) |
jhnwkmn | 0:97a4f8cc534c | 141 | { |
jhnwkmn | 0:97a4f8cc534c | 142 | _ss(v)->_debuginfo = enable?true:false; |
jhnwkmn | 0:97a4f8cc534c | 143 | } |
jhnwkmn | 0:97a4f8cc534c | 144 | |
jhnwkmn | 0:97a4f8cc534c | 145 | void sq_notifyallexceptions(HSQUIRRELVM v, SQBool enable) |
jhnwkmn | 0:97a4f8cc534c | 146 | { |
jhnwkmn | 0:97a4f8cc534c | 147 | _ss(v)->_notifyallexceptions = enable?true:false; |
jhnwkmn | 0:97a4f8cc534c | 148 | } |
jhnwkmn | 0:97a4f8cc534c | 149 | |
jhnwkmn | 0:97a4f8cc534c | 150 | void sq_addref(HSQUIRRELVM v,HSQOBJECT *po) |
jhnwkmn | 0:97a4f8cc534c | 151 | { |
jhnwkmn | 0:97a4f8cc534c | 152 | if(!ISREFCOUNTED(type(*po))) return; |
jhnwkmn | 0:97a4f8cc534c | 153 | #ifdef NO_GARBAGE_COLLECTOR |
jhnwkmn | 0:97a4f8cc534c | 154 | __AddRef(po->_type,po->_unVal); |
jhnwkmn | 0:97a4f8cc534c | 155 | #else |
jhnwkmn | 0:97a4f8cc534c | 156 | _ss(v)->_refs_table.AddRef(*po); |
jhnwkmn | 0:97a4f8cc534c | 157 | #endif |
jhnwkmn | 0:97a4f8cc534c | 158 | } |
jhnwkmn | 0:97a4f8cc534c | 159 | |
jhnwkmn | 0:97a4f8cc534c | 160 | SQUnsignedInteger sq_getrefcount(HSQUIRRELVM v,HSQOBJECT *po) |
jhnwkmn | 0:97a4f8cc534c | 161 | { |
jhnwkmn | 0:97a4f8cc534c | 162 | if(!ISREFCOUNTED(type(*po))) return 0; |
jhnwkmn | 0:97a4f8cc534c | 163 | #ifdef NO_GARBAGE_COLLECTOR |
jhnwkmn | 0:97a4f8cc534c | 164 | return po->_unVal.pRefCounted->_uiRef; |
jhnwkmn | 0:97a4f8cc534c | 165 | #else |
jhnwkmn | 0:97a4f8cc534c | 166 | return _ss(v)->_refs_table.GetRefCount(*po); |
jhnwkmn | 0:97a4f8cc534c | 167 | #endif |
jhnwkmn | 0:97a4f8cc534c | 168 | } |
jhnwkmn | 0:97a4f8cc534c | 169 | |
jhnwkmn | 0:97a4f8cc534c | 170 | SQBool sq_release(HSQUIRRELVM v,HSQOBJECT *po) |
jhnwkmn | 0:97a4f8cc534c | 171 | { |
jhnwkmn | 0:97a4f8cc534c | 172 | if(!ISREFCOUNTED(type(*po))) return SQTrue; |
jhnwkmn | 0:97a4f8cc534c | 173 | #ifdef NO_GARBAGE_COLLECTOR |
jhnwkmn | 0:97a4f8cc534c | 174 | bool ret = (po->_unVal.pRefCounted->_uiRef <= 1) ? SQTrue : SQFalse; |
jhnwkmn | 0:97a4f8cc534c | 175 | __Release(po->_type,po->_unVal); |
jhnwkmn | 0:97a4f8cc534c | 176 | return ret; //the ret val doesn't work(and cannot be fixed) |
jhnwkmn | 0:97a4f8cc534c | 177 | #else |
jhnwkmn | 0:97a4f8cc534c | 178 | return _ss(v)->_refs_table.Release(*po); |
jhnwkmn | 0:97a4f8cc534c | 179 | #endif |
jhnwkmn | 0:97a4f8cc534c | 180 | } |
jhnwkmn | 0:97a4f8cc534c | 181 | |
jhnwkmn | 0:97a4f8cc534c | 182 | const SQChar *sq_objtostring(const HSQOBJECT *o) |
jhnwkmn | 0:97a4f8cc534c | 183 | { |
jhnwkmn | 0:97a4f8cc534c | 184 | if(sq_type(*o) == OT_STRING) { |
jhnwkmn | 0:97a4f8cc534c | 185 | return _stringval(*o); |
jhnwkmn | 0:97a4f8cc534c | 186 | } |
jhnwkmn | 0:97a4f8cc534c | 187 | return NULL; |
jhnwkmn | 0:97a4f8cc534c | 188 | } |
jhnwkmn | 0:97a4f8cc534c | 189 | |
jhnwkmn | 0:97a4f8cc534c | 190 | SQInteger sq_objtointeger(const HSQOBJECT *o) |
jhnwkmn | 0:97a4f8cc534c | 191 | { |
jhnwkmn | 0:97a4f8cc534c | 192 | if(sq_isnumeric(*o)) { |
jhnwkmn | 0:97a4f8cc534c | 193 | return tointeger(*o); |
jhnwkmn | 0:97a4f8cc534c | 194 | } |
jhnwkmn | 0:97a4f8cc534c | 195 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 196 | } |
jhnwkmn | 0:97a4f8cc534c | 197 | |
jhnwkmn | 0:97a4f8cc534c | 198 | SQFloat sq_objtofloat(const HSQOBJECT *o) |
jhnwkmn | 0:97a4f8cc534c | 199 | { |
jhnwkmn | 0:97a4f8cc534c | 200 | if(sq_isnumeric(*o)) { |
jhnwkmn | 0:97a4f8cc534c | 201 | return tofloat(*o); |
jhnwkmn | 0:97a4f8cc534c | 202 | } |
jhnwkmn | 0:97a4f8cc534c | 203 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 204 | } |
jhnwkmn | 0:97a4f8cc534c | 205 | |
jhnwkmn | 0:97a4f8cc534c | 206 | SQBool sq_objtobool(const HSQOBJECT *o) |
jhnwkmn | 0:97a4f8cc534c | 207 | { |
jhnwkmn | 0:97a4f8cc534c | 208 | if(sq_isbool(*o)) { |
jhnwkmn | 0:97a4f8cc534c | 209 | return _integer(*o); |
jhnwkmn | 0:97a4f8cc534c | 210 | } |
jhnwkmn | 0:97a4f8cc534c | 211 | return SQFalse; |
jhnwkmn | 0:97a4f8cc534c | 212 | } |
jhnwkmn | 0:97a4f8cc534c | 213 | |
jhnwkmn | 0:97a4f8cc534c | 214 | SQUserPointer sq_objtouserpointer(const HSQOBJECT *o) |
jhnwkmn | 0:97a4f8cc534c | 215 | { |
jhnwkmn | 0:97a4f8cc534c | 216 | if(sq_isuserpointer(*o)) { |
jhnwkmn | 0:97a4f8cc534c | 217 | return _userpointer(*o); |
jhnwkmn | 0:97a4f8cc534c | 218 | } |
jhnwkmn | 0:97a4f8cc534c | 219 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 220 | } |
jhnwkmn | 0:97a4f8cc534c | 221 | |
jhnwkmn | 0:97a4f8cc534c | 222 | void sq_pushnull(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 223 | { |
jhnwkmn | 0:97a4f8cc534c | 224 | v->PushNull(); |
jhnwkmn | 0:97a4f8cc534c | 225 | } |
jhnwkmn | 0:97a4f8cc534c | 226 | |
jhnwkmn | 0:97a4f8cc534c | 227 | void sq_pushstring(HSQUIRRELVM v,const SQChar *s,SQInteger len) |
jhnwkmn | 0:97a4f8cc534c | 228 | { |
jhnwkmn | 0:97a4f8cc534c | 229 | if(s) |
jhnwkmn | 0:97a4f8cc534c | 230 | v->Push(SQObjectPtr(SQString::Create(_ss(v), s, len))); |
jhnwkmn | 0:97a4f8cc534c | 231 | else v->PushNull(); |
jhnwkmn | 0:97a4f8cc534c | 232 | } |
jhnwkmn | 0:97a4f8cc534c | 233 | |
jhnwkmn | 0:97a4f8cc534c | 234 | void sq_pushinteger(HSQUIRRELVM v,SQInteger n) |
jhnwkmn | 0:97a4f8cc534c | 235 | { |
jhnwkmn | 0:97a4f8cc534c | 236 | v->Push(n); |
jhnwkmn | 0:97a4f8cc534c | 237 | } |
jhnwkmn | 0:97a4f8cc534c | 238 | |
jhnwkmn | 0:97a4f8cc534c | 239 | void sq_pushbool(HSQUIRRELVM v,SQBool b) |
jhnwkmn | 0:97a4f8cc534c | 240 | { |
jhnwkmn | 0:97a4f8cc534c | 241 | v->Push(b?true:false); |
jhnwkmn | 0:97a4f8cc534c | 242 | } |
jhnwkmn | 0:97a4f8cc534c | 243 | |
jhnwkmn | 0:97a4f8cc534c | 244 | void sq_pushfloat(HSQUIRRELVM v,SQFloat n) |
jhnwkmn | 0:97a4f8cc534c | 245 | { |
jhnwkmn | 0:97a4f8cc534c | 246 | v->Push(n); |
jhnwkmn | 0:97a4f8cc534c | 247 | } |
jhnwkmn | 0:97a4f8cc534c | 248 | |
jhnwkmn | 0:97a4f8cc534c | 249 | void sq_pushuserpointer(HSQUIRRELVM v,SQUserPointer p) |
jhnwkmn | 0:97a4f8cc534c | 250 | { |
jhnwkmn | 0:97a4f8cc534c | 251 | v->Push(p); |
jhnwkmn | 0:97a4f8cc534c | 252 | } |
jhnwkmn | 0:97a4f8cc534c | 253 | |
jhnwkmn | 0:97a4f8cc534c | 254 | SQUserPointer sq_newuserdata(HSQUIRRELVM v,SQUnsignedInteger size) |
jhnwkmn | 0:97a4f8cc534c | 255 | { |
jhnwkmn | 0:97a4f8cc534c | 256 | SQUserData *ud = SQUserData::Create(_ss(v), size); |
jhnwkmn | 0:97a4f8cc534c | 257 | v->Push(ud); |
jhnwkmn | 0:97a4f8cc534c | 258 | return (SQUserPointer)sq_aligning(ud + 1); |
jhnwkmn | 0:97a4f8cc534c | 259 | } |
jhnwkmn | 0:97a4f8cc534c | 260 | |
jhnwkmn | 0:97a4f8cc534c | 261 | void sq_newtable(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 262 | { |
jhnwkmn | 0:97a4f8cc534c | 263 | v->Push(SQTable::Create(_ss(v), 0)); |
jhnwkmn | 0:97a4f8cc534c | 264 | } |
jhnwkmn | 0:97a4f8cc534c | 265 | |
jhnwkmn | 0:97a4f8cc534c | 266 | void sq_newtableex(HSQUIRRELVM v,SQInteger initialcapacity) |
jhnwkmn | 0:97a4f8cc534c | 267 | { |
jhnwkmn | 0:97a4f8cc534c | 268 | v->Push(SQTable::Create(_ss(v), initialcapacity)); |
jhnwkmn | 0:97a4f8cc534c | 269 | } |
jhnwkmn | 0:97a4f8cc534c | 270 | |
jhnwkmn | 0:97a4f8cc534c | 271 | void sq_newarray(HSQUIRRELVM v,SQInteger size) |
jhnwkmn | 0:97a4f8cc534c | 272 | { |
jhnwkmn | 0:97a4f8cc534c | 273 | v->Push(SQArray::Create(_ss(v), size)); |
jhnwkmn | 0:97a4f8cc534c | 274 | } |
jhnwkmn | 0:97a4f8cc534c | 275 | |
jhnwkmn | 0:97a4f8cc534c | 276 | SQRESULT sq_newclass(HSQUIRRELVM v,SQBool hasbase) |
jhnwkmn | 0:97a4f8cc534c | 277 | { |
jhnwkmn | 0:97a4f8cc534c | 278 | SQClass *baseclass = NULL; |
jhnwkmn | 0:97a4f8cc534c | 279 | if(hasbase) { |
jhnwkmn | 0:97a4f8cc534c | 280 | SQObjectPtr &base = stack_get(v,-1); |
jhnwkmn | 0:97a4f8cc534c | 281 | if(type(base) != OT_CLASS) |
jhnwkmn | 0:97a4f8cc534c | 282 | return sq_throwerror(v,_SC("invalid base type")); |
jhnwkmn | 0:97a4f8cc534c | 283 | baseclass = _class(base); |
jhnwkmn | 0:97a4f8cc534c | 284 | } |
jhnwkmn | 0:97a4f8cc534c | 285 | SQClass *newclass = SQClass::Create(_ss(v), baseclass); |
jhnwkmn | 0:97a4f8cc534c | 286 | if(baseclass) v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 287 | v->Push(newclass); |
jhnwkmn | 0:97a4f8cc534c | 288 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 289 | } |
jhnwkmn | 0:97a4f8cc534c | 290 | |
jhnwkmn | 0:97a4f8cc534c | 291 | SQBool sq_instanceof(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 292 | { |
jhnwkmn | 0:97a4f8cc534c | 293 | SQObjectPtr &inst = stack_get(v,-1); |
jhnwkmn | 0:97a4f8cc534c | 294 | SQObjectPtr &cl = stack_get(v,-2); |
jhnwkmn | 0:97a4f8cc534c | 295 | if(type(inst) != OT_INSTANCE || type(cl) != OT_CLASS) |
jhnwkmn | 0:97a4f8cc534c | 296 | return sq_throwerror(v,_SC("invalid param type")); |
jhnwkmn | 0:97a4f8cc534c | 297 | return _instance(inst)->InstanceOf(_class(cl))?SQTrue:SQFalse; |
jhnwkmn | 0:97a4f8cc534c | 298 | } |
jhnwkmn | 0:97a4f8cc534c | 299 | |
jhnwkmn | 0:97a4f8cc534c | 300 | SQRESULT sq_arrayappend(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 301 | { |
jhnwkmn | 0:97a4f8cc534c | 302 | sq_aux_paramscheck(v,2); |
jhnwkmn | 0:97a4f8cc534c | 303 | SQObjectPtr *arr; |
jhnwkmn | 0:97a4f8cc534c | 304 | _GETSAFE_OBJ(v, idx, OT_ARRAY,arr); |
jhnwkmn | 0:97a4f8cc534c | 305 | _array(*arr)->Append(v->GetUp(-1)); |
jhnwkmn | 0:97a4f8cc534c | 306 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 307 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 308 | } |
jhnwkmn | 0:97a4f8cc534c | 309 | |
jhnwkmn | 0:97a4f8cc534c | 310 | SQRESULT sq_arraypop(HSQUIRRELVM v,SQInteger idx,SQBool pushval) |
jhnwkmn | 0:97a4f8cc534c | 311 | { |
jhnwkmn | 0:97a4f8cc534c | 312 | sq_aux_paramscheck(v, 1); |
jhnwkmn | 0:97a4f8cc534c | 313 | SQObjectPtr *arr; |
jhnwkmn | 0:97a4f8cc534c | 314 | _GETSAFE_OBJ(v, idx, OT_ARRAY,arr); |
jhnwkmn | 0:97a4f8cc534c | 315 | if(_array(*arr)->Size() > 0) { |
jhnwkmn | 0:97a4f8cc534c | 316 | if(pushval != 0){ v->Push(_array(*arr)->Top()); } |
jhnwkmn | 0:97a4f8cc534c | 317 | _array(*arr)->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 318 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 319 | } |
jhnwkmn | 0:97a4f8cc534c | 320 | return sq_throwerror(v, _SC("empty array")); |
jhnwkmn | 0:97a4f8cc534c | 321 | } |
jhnwkmn | 0:97a4f8cc534c | 322 | |
jhnwkmn | 0:97a4f8cc534c | 323 | SQRESULT sq_arrayresize(HSQUIRRELVM v,SQInteger idx,SQInteger newsize) |
jhnwkmn | 0:97a4f8cc534c | 324 | { |
jhnwkmn | 0:97a4f8cc534c | 325 | sq_aux_paramscheck(v,1); |
jhnwkmn | 0:97a4f8cc534c | 326 | SQObjectPtr *arr; |
jhnwkmn | 0:97a4f8cc534c | 327 | _GETSAFE_OBJ(v, idx, OT_ARRAY,arr); |
jhnwkmn | 0:97a4f8cc534c | 328 | if(newsize >= 0) { |
jhnwkmn | 0:97a4f8cc534c | 329 | _array(*arr)->Resize(newsize); |
jhnwkmn | 0:97a4f8cc534c | 330 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 331 | } |
jhnwkmn | 0:97a4f8cc534c | 332 | return sq_throwerror(v,_SC("negative size")); |
jhnwkmn | 0:97a4f8cc534c | 333 | } |
jhnwkmn | 0:97a4f8cc534c | 334 | |
jhnwkmn | 0:97a4f8cc534c | 335 | |
jhnwkmn | 0:97a4f8cc534c | 336 | SQRESULT sq_arrayreverse(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 337 | { |
jhnwkmn | 0:97a4f8cc534c | 338 | sq_aux_paramscheck(v, 1); |
jhnwkmn | 0:97a4f8cc534c | 339 | SQObjectPtr *o; |
jhnwkmn | 0:97a4f8cc534c | 340 | _GETSAFE_OBJ(v, idx, OT_ARRAY,o); |
jhnwkmn | 0:97a4f8cc534c | 341 | SQArray *arr = _array(*o); |
jhnwkmn | 0:97a4f8cc534c | 342 | if(arr->Size() > 0) { |
jhnwkmn | 0:97a4f8cc534c | 343 | SQObjectPtr t; |
jhnwkmn | 0:97a4f8cc534c | 344 | SQInteger size = arr->Size(); |
jhnwkmn | 0:97a4f8cc534c | 345 | SQInteger n = size >> 1; size -= 1; |
jhnwkmn | 0:97a4f8cc534c | 346 | for(SQInteger i = 0; i < n; i++) { |
jhnwkmn | 0:97a4f8cc534c | 347 | t = arr->_values[i]; |
jhnwkmn | 0:97a4f8cc534c | 348 | arr->_values[i] = arr->_values[size-i]; |
jhnwkmn | 0:97a4f8cc534c | 349 | arr->_values[size-i] = t; |
jhnwkmn | 0:97a4f8cc534c | 350 | } |
jhnwkmn | 0:97a4f8cc534c | 351 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 352 | } |
jhnwkmn | 0:97a4f8cc534c | 353 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 354 | } |
jhnwkmn | 0:97a4f8cc534c | 355 | |
jhnwkmn | 0:97a4f8cc534c | 356 | SQRESULT sq_arrayremove(HSQUIRRELVM v,SQInteger idx,SQInteger itemidx) |
jhnwkmn | 0:97a4f8cc534c | 357 | { |
jhnwkmn | 0:97a4f8cc534c | 358 | sq_aux_paramscheck(v, 1); |
jhnwkmn | 0:97a4f8cc534c | 359 | SQObjectPtr *arr; |
jhnwkmn | 0:97a4f8cc534c | 360 | _GETSAFE_OBJ(v, idx, OT_ARRAY,arr); |
jhnwkmn | 0:97a4f8cc534c | 361 | return _array(*arr)->Remove(itemidx) ? SQ_OK : sq_throwerror(v,_SC("index out of range")); |
jhnwkmn | 0:97a4f8cc534c | 362 | } |
jhnwkmn | 0:97a4f8cc534c | 363 | |
jhnwkmn | 0:97a4f8cc534c | 364 | SQRESULT sq_arrayinsert(HSQUIRRELVM v,SQInteger idx,SQInteger destpos) |
jhnwkmn | 0:97a4f8cc534c | 365 | { |
jhnwkmn | 0:97a4f8cc534c | 366 | sq_aux_paramscheck(v, 1); |
jhnwkmn | 0:97a4f8cc534c | 367 | SQObjectPtr *arr; |
jhnwkmn | 0:97a4f8cc534c | 368 | _GETSAFE_OBJ(v, idx, OT_ARRAY,arr); |
jhnwkmn | 0:97a4f8cc534c | 369 | SQRESULT ret = _array(*arr)->Insert(destpos, v->GetUp(-1)) ? SQ_OK : sq_throwerror(v,_SC("index out of range")); |
jhnwkmn | 0:97a4f8cc534c | 370 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 371 | return ret; |
jhnwkmn | 0:97a4f8cc534c | 372 | } |
jhnwkmn | 0:97a4f8cc534c | 373 | |
jhnwkmn | 0:97a4f8cc534c | 374 | void sq_newclosure(HSQUIRRELVM v,SQFUNCTION func,SQUnsignedInteger nfreevars) |
jhnwkmn | 0:97a4f8cc534c | 375 | { |
jhnwkmn | 0:97a4f8cc534c | 376 | SQNativeClosure *nc = SQNativeClosure::Create(_ss(v), func,nfreevars); |
jhnwkmn | 0:97a4f8cc534c | 377 | nc->_nparamscheck = 0; |
jhnwkmn | 0:97a4f8cc534c | 378 | for(SQUnsignedInteger i = 0; i < nfreevars; i++) { |
jhnwkmn | 0:97a4f8cc534c | 379 | nc->_outervalues[i] = v->Top(); |
jhnwkmn | 0:97a4f8cc534c | 380 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 381 | } |
jhnwkmn | 0:97a4f8cc534c | 382 | v->Push(SQObjectPtr(nc)); |
jhnwkmn | 0:97a4f8cc534c | 383 | } |
jhnwkmn | 0:97a4f8cc534c | 384 | |
jhnwkmn | 0:97a4f8cc534c | 385 | SQRESULT sq_getclosureinfo(HSQUIRRELVM v,SQInteger idx,SQUnsignedInteger *nparams,SQUnsignedInteger *nfreevars) |
jhnwkmn | 0:97a4f8cc534c | 386 | { |
jhnwkmn | 0:97a4f8cc534c | 387 | SQObject o = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 388 | if(type(o) == OT_CLOSURE) { |
jhnwkmn | 0:97a4f8cc534c | 389 | SQClosure *c = _closure(o); |
jhnwkmn | 0:97a4f8cc534c | 390 | SQFunctionProto *proto = c->_function; |
jhnwkmn | 0:97a4f8cc534c | 391 | *nparams = (SQUnsignedInteger)proto->_nparameters; |
jhnwkmn | 0:97a4f8cc534c | 392 | *nfreevars = (SQUnsignedInteger)proto->_noutervalues; |
jhnwkmn | 0:97a4f8cc534c | 393 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 394 | } |
jhnwkmn | 0:97a4f8cc534c | 395 | else if(type(o) == OT_NATIVECLOSURE) |
jhnwkmn | 0:97a4f8cc534c | 396 | { |
jhnwkmn | 0:97a4f8cc534c | 397 | SQNativeClosure *c = _nativeclosure(o); |
jhnwkmn | 0:97a4f8cc534c | 398 | *nparams = (SQUnsignedInteger)c->_nparamscheck; |
jhnwkmn | 0:97a4f8cc534c | 399 | *nfreevars = c->_noutervalues; |
jhnwkmn | 0:97a4f8cc534c | 400 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 401 | } |
jhnwkmn | 0:97a4f8cc534c | 402 | return sq_throwerror(v,_SC("the object is not a closure")); |
jhnwkmn | 0:97a4f8cc534c | 403 | } |
jhnwkmn | 0:97a4f8cc534c | 404 | |
jhnwkmn | 0:97a4f8cc534c | 405 | SQRESULT sq_setnativeclosurename(HSQUIRRELVM v,SQInteger idx,const SQChar *name) |
jhnwkmn | 0:97a4f8cc534c | 406 | { |
jhnwkmn | 0:97a4f8cc534c | 407 | SQObject o = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 408 | if(sq_isnativeclosure(o)) { |
jhnwkmn | 0:97a4f8cc534c | 409 | SQNativeClosure *nc = _nativeclosure(o); |
jhnwkmn | 0:97a4f8cc534c | 410 | nc->_name = SQString::Create(_ss(v),name); |
jhnwkmn | 0:97a4f8cc534c | 411 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 412 | } |
jhnwkmn | 0:97a4f8cc534c | 413 | return sq_throwerror(v,_SC("the object is not a nativeclosure")); |
jhnwkmn | 0:97a4f8cc534c | 414 | } |
jhnwkmn | 0:97a4f8cc534c | 415 | |
jhnwkmn | 0:97a4f8cc534c | 416 | SQRESULT sq_setparamscheck(HSQUIRRELVM v,SQInteger nparamscheck,const SQChar *typemask) |
jhnwkmn | 0:97a4f8cc534c | 417 | { |
jhnwkmn | 0:97a4f8cc534c | 418 | SQObject o = stack_get(v, -1); |
jhnwkmn | 0:97a4f8cc534c | 419 | if(!sq_isnativeclosure(o)) |
jhnwkmn | 0:97a4f8cc534c | 420 | return sq_throwerror(v, _SC("native closure expected")); |
jhnwkmn | 0:97a4f8cc534c | 421 | SQNativeClosure *nc = _nativeclosure(o); |
jhnwkmn | 0:97a4f8cc534c | 422 | nc->_nparamscheck = nparamscheck; |
jhnwkmn | 0:97a4f8cc534c | 423 | if(typemask) { |
jhnwkmn | 0:97a4f8cc534c | 424 | SQIntVec res; |
jhnwkmn | 0:97a4f8cc534c | 425 | if(!CompileTypemask(res, typemask)) |
jhnwkmn | 0:97a4f8cc534c | 426 | return sq_throwerror(v, _SC("invalid typemask")); |
jhnwkmn | 0:97a4f8cc534c | 427 | nc->_typecheck.copy(res); |
jhnwkmn | 0:97a4f8cc534c | 428 | } |
jhnwkmn | 0:97a4f8cc534c | 429 | else { |
jhnwkmn | 0:97a4f8cc534c | 430 | nc->_typecheck.resize(0); |
jhnwkmn | 0:97a4f8cc534c | 431 | } |
jhnwkmn | 0:97a4f8cc534c | 432 | if(nparamscheck == SQ_MATCHTYPEMASKSTRING) { |
jhnwkmn | 0:97a4f8cc534c | 433 | nc->_nparamscheck = nc->_typecheck.size(); |
jhnwkmn | 0:97a4f8cc534c | 434 | } |
jhnwkmn | 0:97a4f8cc534c | 435 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 436 | } |
jhnwkmn | 0:97a4f8cc534c | 437 | |
jhnwkmn | 0:97a4f8cc534c | 438 | SQRESULT sq_bindenv(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 439 | { |
jhnwkmn | 0:97a4f8cc534c | 440 | SQObjectPtr &o = stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 441 | if(!sq_isnativeclosure(o) && |
jhnwkmn | 0:97a4f8cc534c | 442 | !sq_isclosure(o)) |
jhnwkmn | 0:97a4f8cc534c | 443 | return sq_throwerror(v,_SC("the target is not a closure")); |
jhnwkmn | 0:97a4f8cc534c | 444 | SQObjectPtr &env = stack_get(v,-1); |
jhnwkmn | 0:97a4f8cc534c | 445 | if(!sq_istable(env) && |
jhnwkmn | 0:97a4f8cc534c | 446 | !sq_isclass(env) && |
jhnwkmn | 0:97a4f8cc534c | 447 | !sq_isinstance(env)) |
jhnwkmn | 0:97a4f8cc534c | 448 | return sq_throwerror(v,_SC("invalid environment")); |
jhnwkmn | 0:97a4f8cc534c | 449 | SQWeakRef *w = _refcounted(env)->GetWeakRef(type(env)); |
jhnwkmn | 0:97a4f8cc534c | 450 | SQObjectPtr ret; |
jhnwkmn | 0:97a4f8cc534c | 451 | if(sq_isclosure(o)) { |
jhnwkmn | 0:97a4f8cc534c | 452 | SQClosure *c = _closure(o)->Clone(); |
jhnwkmn | 0:97a4f8cc534c | 453 | __ObjRelease(c->_env); |
jhnwkmn | 0:97a4f8cc534c | 454 | c->_env = w; |
jhnwkmn | 0:97a4f8cc534c | 455 | __ObjAddRef(c->_env); |
jhnwkmn | 0:97a4f8cc534c | 456 | if(_closure(o)->_base) { |
jhnwkmn | 0:97a4f8cc534c | 457 | c->_base = _closure(o)->_base; |
jhnwkmn | 0:97a4f8cc534c | 458 | __ObjAddRef(c->_base); |
jhnwkmn | 0:97a4f8cc534c | 459 | } |
jhnwkmn | 0:97a4f8cc534c | 460 | ret = c; |
jhnwkmn | 0:97a4f8cc534c | 461 | } |
jhnwkmn | 0:97a4f8cc534c | 462 | else { //then must be a native closure |
jhnwkmn | 0:97a4f8cc534c | 463 | SQNativeClosure *c = _nativeclosure(o)->Clone(); |
jhnwkmn | 0:97a4f8cc534c | 464 | __ObjRelease(c->_env); |
jhnwkmn | 0:97a4f8cc534c | 465 | c->_env = w; |
jhnwkmn | 0:97a4f8cc534c | 466 | __ObjAddRef(c->_env); |
jhnwkmn | 0:97a4f8cc534c | 467 | ret = c; |
jhnwkmn | 0:97a4f8cc534c | 468 | } |
jhnwkmn | 0:97a4f8cc534c | 469 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 470 | v->Push(ret); |
jhnwkmn | 0:97a4f8cc534c | 471 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 472 | } |
jhnwkmn | 0:97a4f8cc534c | 473 | |
jhnwkmn | 0:97a4f8cc534c | 474 | SQRESULT sq_getclosurename(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 475 | { |
jhnwkmn | 0:97a4f8cc534c | 476 | SQObjectPtr &o = stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 477 | if(!sq_isnativeclosure(o) && |
jhnwkmn | 0:97a4f8cc534c | 478 | !sq_isclosure(o)) |
jhnwkmn | 0:97a4f8cc534c | 479 | return sq_throwerror(v,_SC("the target is not a closure")); |
jhnwkmn | 0:97a4f8cc534c | 480 | if(sq_isnativeclosure(o)) |
jhnwkmn | 0:97a4f8cc534c | 481 | { |
jhnwkmn | 0:97a4f8cc534c | 482 | v->Push(_nativeclosure(o)->_name); |
jhnwkmn | 0:97a4f8cc534c | 483 | } |
jhnwkmn | 0:97a4f8cc534c | 484 | else { //closure |
jhnwkmn | 0:97a4f8cc534c | 485 | v->Push(_closure(o)->_function->_name); |
jhnwkmn | 0:97a4f8cc534c | 486 | } |
jhnwkmn | 0:97a4f8cc534c | 487 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 488 | } |
jhnwkmn | 0:97a4f8cc534c | 489 | |
jhnwkmn | 0:97a4f8cc534c | 490 | SQRESULT sq_clear(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 491 | { |
jhnwkmn | 0:97a4f8cc534c | 492 | SQObject &o=stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 493 | switch(type(o)) { |
jhnwkmn | 0:97a4f8cc534c | 494 | case OT_TABLE: _table(o)->Clear(); break; |
jhnwkmn | 0:97a4f8cc534c | 495 | case OT_ARRAY: _array(o)->Resize(0); break; |
jhnwkmn | 0:97a4f8cc534c | 496 | default: |
jhnwkmn | 0:97a4f8cc534c | 497 | return sq_throwerror(v, _SC("clear only works on table and array")); |
jhnwkmn | 0:97a4f8cc534c | 498 | break; |
jhnwkmn | 0:97a4f8cc534c | 499 | |
jhnwkmn | 0:97a4f8cc534c | 500 | } |
jhnwkmn | 0:97a4f8cc534c | 501 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 502 | } |
jhnwkmn | 0:97a4f8cc534c | 503 | |
jhnwkmn | 0:97a4f8cc534c | 504 | void sq_pushroottable(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 505 | { |
jhnwkmn | 0:97a4f8cc534c | 506 | v->Push(v->_roottable); |
jhnwkmn | 0:97a4f8cc534c | 507 | } |
jhnwkmn | 0:97a4f8cc534c | 508 | |
jhnwkmn | 0:97a4f8cc534c | 509 | void sq_pushregistrytable(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 510 | { |
jhnwkmn | 0:97a4f8cc534c | 511 | v->Push(_ss(v)->_registry); |
jhnwkmn | 0:97a4f8cc534c | 512 | } |
jhnwkmn | 0:97a4f8cc534c | 513 | |
jhnwkmn | 0:97a4f8cc534c | 514 | void sq_pushconsttable(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 515 | { |
jhnwkmn | 0:97a4f8cc534c | 516 | v->Push(_ss(v)->_consts); |
jhnwkmn | 0:97a4f8cc534c | 517 | } |
jhnwkmn | 0:97a4f8cc534c | 518 | |
jhnwkmn | 0:97a4f8cc534c | 519 | SQRESULT sq_setroottable(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 520 | { |
jhnwkmn | 0:97a4f8cc534c | 521 | SQObject o = stack_get(v, -1); |
jhnwkmn | 0:97a4f8cc534c | 522 | if(sq_istable(o) || sq_isnull(o)) { |
jhnwkmn | 0:97a4f8cc534c | 523 | v->_roottable = o; |
jhnwkmn | 0:97a4f8cc534c | 524 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 525 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 526 | } |
jhnwkmn | 0:97a4f8cc534c | 527 | return sq_throwerror(v, _SC("ivalid type")); |
jhnwkmn | 0:97a4f8cc534c | 528 | } |
jhnwkmn | 0:97a4f8cc534c | 529 | |
jhnwkmn | 0:97a4f8cc534c | 530 | SQRESULT sq_setconsttable(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 531 | { |
jhnwkmn | 0:97a4f8cc534c | 532 | SQObject o = stack_get(v, -1); |
jhnwkmn | 0:97a4f8cc534c | 533 | if(sq_istable(o)) { |
jhnwkmn | 0:97a4f8cc534c | 534 | _ss(v)->_consts = o; |
jhnwkmn | 0:97a4f8cc534c | 535 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 536 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 537 | } |
jhnwkmn | 0:97a4f8cc534c | 538 | return sq_throwerror(v, _SC("ivalid type, expected table")); |
jhnwkmn | 0:97a4f8cc534c | 539 | } |
jhnwkmn | 0:97a4f8cc534c | 540 | |
jhnwkmn | 0:97a4f8cc534c | 541 | void sq_setforeignptr(HSQUIRRELVM v,SQUserPointer p) |
jhnwkmn | 0:97a4f8cc534c | 542 | { |
jhnwkmn | 0:97a4f8cc534c | 543 | v->_foreignptr = p; |
jhnwkmn | 0:97a4f8cc534c | 544 | } |
jhnwkmn | 0:97a4f8cc534c | 545 | |
jhnwkmn | 0:97a4f8cc534c | 546 | SQUserPointer sq_getforeignptr(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 547 | { |
jhnwkmn | 0:97a4f8cc534c | 548 | return v->_foreignptr; |
jhnwkmn | 0:97a4f8cc534c | 549 | } |
jhnwkmn | 0:97a4f8cc534c | 550 | |
jhnwkmn | 0:97a4f8cc534c | 551 | void sq_push(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 552 | { |
jhnwkmn | 0:97a4f8cc534c | 553 | v->Push(stack_get(v, idx)); |
jhnwkmn | 0:97a4f8cc534c | 554 | } |
jhnwkmn | 0:97a4f8cc534c | 555 | |
jhnwkmn | 0:97a4f8cc534c | 556 | SQObjectType sq_gettype(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 557 | { |
jhnwkmn | 0:97a4f8cc534c | 558 | return type(stack_get(v, idx)); |
jhnwkmn | 0:97a4f8cc534c | 559 | } |
jhnwkmn | 0:97a4f8cc534c | 560 | |
jhnwkmn | 0:97a4f8cc534c | 561 | SQRESULT sq_typeof(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 562 | { |
jhnwkmn | 0:97a4f8cc534c | 563 | SQObjectPtr &o = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 564 | SQObjectPtr res; |
jhnwkmn | 0:97a4f8cc534c | 565 | if(!v->TypeOf(o,res)) { |
jhnwkmn | 0:97a4f8cc534c | 566 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 567 | } |
jhnwkmn | 0:97a4f8cc534c | 568 | v->Push(res); |
jhnwkmn | 0:97a4f8cc534c | 569 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 570 | } |
jhnwkmn | 0:97a4f8cc534c | 571 | |
jhnwkmn | 0:97a4f8cc534c | 572 | SQRESULT sq_tostring(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 573 | { |
jhnwkmn | 0:97a4f8cc534c | 574 | SQObjectPtr &o = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 575 | SQObjectPtr res; |
jhnwkmn | 0:97a4f8cc534c | 576 | if(!v->ToString(o,res)) { |
jhnwkmn | 0:97a4f8cc534c | 577 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 578 | } |
jhnwkmn | 0:97a4f8cc534c | 579 | v->Push(res); |
jhnwkmn | 0:97a4f8cc534c | 580 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 581 | } |
jhnwkmn | 0:97a4f8cc534c | 582 | |
jhnwkmn | 0:97a4f8cc534c | 583 | void sq_tobool(HSQUIRRELVM v, SQInteger idx, SQBool *b) |
jhnwkmn | 0:97a4f8cc534c | 584 | { |
jhnwkmn | 0:97a4f8cc534c | 585 | SQObjectPtr &o = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 586 | *b = SQVM::IsFalse(o)?SQFalse:SQTrue; |
jhnwkmn | 0:97a4f8cc534c | 587 | } |
jhnwkmn | 0:97a4f8cc534c | 588 | |
jhnwkmn | 0:97a4f8cc534c | 589 | SQRESULT sq_getinteger(HSQUIRRELVM v,SQInteger idx,SQInteger *i) |
jhnwkmn | 0:97a4f8cc534c | 590 | { |
jhnwkmn | 0:97a4f8cc534c | 591 | SQObjectPtr &o = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 592 | if(sq_isnumeric(o)) { |
jhnwkmn | 0:97a4f8cc534c | 593 | *i = tointeger(o); |
jhnwkmn | 0:97a4f8cc534c | 594 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 595 | } |
jhnwkmn | 0:97a4f8cc534c | 596 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 597 | } |
jhnwkmn | 0:97a4f8cc534c | 598 | |
jhnwkmn | 0:97a4f8cc534c | 599 | SQRESULT sq_getfloat(HSQUIRRELVM v,SQInteger idx,SQFloat *f) |
jhnwkmn | 0:97a4f8cc534c | 600 | { |
jhnwkmn | 0:97a4f8cc534c | 601 | SQObjectPtr &o = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 602 | if(sq_isnumeric(o)) { |
jhnwkmn | 0:97a4f8cc534c | 603 | *f = tofloat(o); |
jhnwkmn | 0:97a4f8cc534c | 604 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 605 | } |
jhnwkmn | 0:97a4f8cc534c | 606 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 607 | } |
jhnwkmn | 0:97a4f8cc534c | 608 | |
jhnwkmn | 0:97a4f8cc534c | 609 | SQRESULT sq_getbool(HSQUIRRELVM v,SQInteger idx,SQBool *b) |
jhnwkmn | 0:97a4f8cc534c | 610 | { |
jhnwkmn | 0:97a4f8cc534c | 611 | SQObjectPtr &o = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 612 | if(sq_isbool(o)) { |
jhnwkmn | 0:97a4f8cc534c | 613 | *b = _integer(o); |
jhnwkmn | 0:97a4f8cc534c | 614 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 615 | } |
jhnwkmn | 0:97a4f8cc534c | 616 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 617 | } |
jhnwkmn | 0:97a4f8cc534c | 618 | |
jhnwkmn | 0:97a4f8cc534c | 619 | SQRESULT sq_getstring(HSQUIRRELVM v,SQInteger idx,const SQChar **c) |
jhnwkmn | 0:97a4f8cc534c | 620 | { |
jhnwkmn | 0:97a4f8cc534c | 621 | SQObjectPtr *o = NULL; |
jhnwkmn | 0:97a4f8cc534c | 622 | _GETSAFE_OBJ(v, idx, OT_STRING,o); |
jhnwkmn | 0:97a4f8cc534c | 623 | *c = _stringval(*o); |
jhnwkmn | 0:97a4f8cc534c | 624 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 625 | } |
jhnwkmn | 0:97a4f8cc534c | 626 | |
jhnwkmn | 0:97a4f8cc534c | 627 | SQRESULT sq_getthread(HSQUIRRELVM v,SQInteger idx,HSQUIRRELVM *thread) |
jhnwkmn | 0:97a4f8cc534c | 628 | { |
jhnwkmn | 0:97a4f8cc534c | 629 | SQObjectPtr *o = NULL; |
jhnwkmn | 0:97a4f8cc534c | 630 | _GETSAFE_OBJ(v, idx, OT_THREAD,o); |
jhnwkmn | 0:97a4f8cc534c | 631 | *thread = _thread(*o); |
jhnwkmn | 0:97a4f8cc534c | 632 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 633 | } |
jhnwkmn | 0:97a4f8cc534c | 634 | |
jhnwkmn | 0:97a4f8cc534c | 635 | SQRESULT sq_clone(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 636 | { |
jhnwkmn | 0:97a4f8cc534c | 637 | SQObjectPtr &o = stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 638 | v->PushNull(); |
jhnwkmn | 0:97a4f8cc534c | 639 | if(!v->Clone(o, stack_get(v, -1))){ |
jhnwkmn | 0:97a4f8cc534c | 640 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 641 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 642 | } |
jhnwkmn | 0:97a4f8cc534c | 643 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 644 | } |
jhnwkmn | 0:97a4f8cc534c | 645 | |
jhnwkmn | 0:97a4f8cc534c | 646 | SQInteger sq_getsize(HSQUIRRELVM v, SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 647 | { |
jhnwkmn | 0:97a4f8cc534c | 648 | SQObjectPtr &o = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 649 | SQObjectType type = type(o); |
jhnwkmn | 0:97a4f8cc534c | 650 | switch(type) { |
jhnwkmn | 0:97a4f8cc534c | 651 | case OT_STRING: return _string(o)->_len; |
jhnwkmn | 0:97a4f8cc534c | 652 | case OT_TABLE: return _table(o)->CountUsed(); |
jhnwkmn | 0:97a4f8cc534c | 653 | case OT_ARRAY: return _array(o)->Size(); |
jhnwkmn | 0:97a4f8cc534c | 654 | case OT_USERDATA: return _userdata(o)->_size; |
jhnwkmn | 0:97a4f8cc534c | 655 | case OT_INSTANCE: return _instance(o)->_class->_udsize; |
jhnwkmn | 0:97a4f8cc534c | 656 | case OT_CLASS: return _class(o)->_udsize; |
jhnwkmn | 0:97a4f8cc534c | 657 | default: |
jhnwkmn | 0:97a4f8cc534c | 658 | return sq_aux_invalidtype(v, type); |
jhnwkmn | 0:97a4f8cc534c | 659 | } |
jhnwkmn | 0:97a4f8cc534c | 660 | } |
jhnwkmn | 0:97a4f8cc534c | 661 | |
jhnwkmn | 0:97a4f8cc534c | 662 | SQHash sq_gethash(HSQUIRRELVM v, SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 663 | { |
jhnwkmn | 0:97a4f8cc534c | 664 | SQObjectPtr &o = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 665 | return HashObj(o); |
jhnwkmn | 0:97a4f8cc534c | 666 | } |
jhnwkmn | 0:97a4f8cc534c | 667 | |
jhnwkmn | 0:97a4f8cc534c | 668 | SQRESULT sq_getuserdata(HSQUIRRELVM v,SQInteger idx,SQUserPointer *p,SQUserPointer *typetag) |
jhnwkmn | 0:97a4f8cc534c | 669 | { |
jhnwkmn | 0:97a4f8cc534c | 670 | SQObjectPtr *o = NULL; |
jhnwkmn | 0:97a4f8cc534c | 671 | _GETSAFE_OBJ(v, idx, OT_USERDATA,o); |
jhnwkmn | 0:97a4f8cc534c | 672 | (*p) = _userdataval(*o); |
jhnwkmn | 0:97a4f8cc534c | 673 | if(typetag) *typetag = _userdata(*o)->_typetag; |
jhnwkmn | 0:97a4f8cc534c | 674 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 675 | } |
jhnwkmn | 0:97a4f8cc534c | 676 | |
jhnwkmn | 0:97a4f8cc534c | 677 | SQRESULT sq_settypetag(HSQUIRRELVM v,SQInteger idx,SQUserPointer typetag) |
jhnwkmn | 0:97a4f8cc534c | 678 | { |
jhnwkmn | 0:97a4f8cc534c | 679 | SQObjectPtr &o = stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 680 | switch(type(o)) { |
jhnwkmn | 0:97a4f8cc534c | 681 | case OT_USERDATA: _userdata(o)->_typetag = typetag; break; |
jhnwkmn | 0:97a4f8cc534c | 682 | case OT_CLASS: _class(o)->_typetag = typetag; break; |
jhnwkmn | 0:97a4f8cc534c | 683 | default: return sq_throwerror(v,_SC("invalid object type")); |
jhnwkmn | 0:97a4f8cc534c | 684 | } |
jhnwkmn | 0:97a4f8cc534c | 685 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 686 | } |
jhnwkmn | 0:97a4f8cc534c | 687 | |
jhnwkmn | 0:97a4f8cc534c | 688 | SQRESULT sq_getobjtypetag(const HSQOBJECT *o,SQUserPointer * typetag) |
jhnwkmn | 0:97a4f8cc534c | 689 | { |
jhnwkmn | 0:97a4f8cc534c | 690 | switch(type(*o)) { |
jhnwkmn | 0:97a4f8cc534c | 691 | case OT_INSTANCE: *typetag = _instance(*o)->_class->_typetag; break; |
jhnwkmn | 0:97a4f8cc534c | 692 | case OT_USERDATA: *typetag = _userdata(*o)->_typetag; break; |
jhnwkmn | 0:97a4f8cc534c | 693 | case OT_CLASS: *typetag = _class(*o)->_typetag; break; |
jhnwkmn | 0:97a4f8cc534c | 694 | default: return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 695 | } |
jhnwkmn | 0:97a4f8cc534c | 696 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 697 | } |
jhnwkmn | 0:97a4f8cc534c | 698 | |
jhnwkmn | 0:97a4f8cc534c | 699 | SQRESULT sq_gettypetag(HSQUIRRELVM v,SQInteger idx,SQUserPointer *typetag) |
jhnwkmn | 0:97a4f8cc534c | 700 | { |
jhnwkmn | 0:97a4f8cc534c | 701 | SQObjectPtr &o = stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 702 | if(SQ_FAILED(sq_getobjtypetag(&o,typetag))) |
jhnwkmn | 0:97a4f8cc534c | 703 | return sq_throwerror(v,_SC("invalid object type")); |
jhnwkmn | 0:97a4f8cc534c | 704 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 705 | } |
jhnwkmn | 0:97a4f8cc534c | 706 | |
jhnwkmn | 0:97a4f8cc534c | 707 | SQRESULT sq_getuserpointer(HSQUIRRELVM v, SQInteger idx, SQUserPointer *p) |
jhnwkmn | 0:97a4f8cc534c | 708 | { |
jhnwkmn | 0:97a4f8cc534c | 709 | SQObjectPtr *o = NULL; |
jhnwkmn | 0:97a4f8cc534c | 710 | _GETSAFE_OBJ(v, idx, OT_USERPOINTER,o); |
jhnwkmn | 0:97a4f8cc534c | 711 | (*p) = _userpointer(*o); |
jhnwkmn | 0:97a4f8cc534c | 712 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 713 | } |
jhnwkmn | 0:97a4f8cc534c | 714 | |
jhnwkmn | 0:97a4f8cc534c | 715 | SQRESULT sq_setinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer p) |
jhnwkmn | 0:97a4f8cc534c | 716 | { |
jhnwkmn | 0:97a4f8cc534c | 717 | SQObjectPtr &o = stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 718 | if(type(o) != OT_INSTANCE) return sq_throwerror(v,_SC("the object is not a class instance")); |
jhnwkmn | 0:97a4f8cc534c | 719 | _instance(o)->_userpointer = p; |
jhnwkmn | 0:97a4f8cc534c | 720 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 721 | } |
jhnwkmn | 0:97a4f8cc534c | 722 | |
jhnwkmn | 0:97a4f8cc534c | 723 | SQRESULT sq_setclassudsize(HSQUIRRELVM v, SQInteger idx, SQInteger udsize) |
jhnwkmn | 0:97a4f8cc534c | 724 | { |
jhnwkmn | 0:97a4f8cc534c | 725 | SQObjectPtr &o = stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 726 | if(type(o) != OT_CLASS) return sq_throwerror(v,_SC("the object is not a class")); |
jhnwkmn | 0:97a4f8cc534c | 727 | if(_class(o)->_locked) return sq_throwerror(v,_SC("the class is locked")); |
jhnwkmn | 0:97a4f8cc534c | 728 | _class(o)->_udsize = udsize; |
jhnwkmn | 0:97a4f8cc534c | 729 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 730 | } |
jhnwkmn | 0:97a4f8cc534c | 731 | |
jhnwkmn | 0:97a4f8cc534c | 732 | |
jhnwkmn | 0:97a4f8cc534c | 733 | SQRESULT sq_getinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer *p,SQUserPointer typetag) |
jhnwkmn | 0:97a4f8cc534c | 734 | { |
jhnwkmn | 0:97a4f8cc534c | 735 | SQObjectPtr &o = stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 736 | if(type(o) != OT_INSTANCE) return sq_throwerror(v,_SC("the object is not a class instance")); |
jhnwkmn | 0:97a4f8cc534c | 737 | (*p) = _instance(o)->_userpointer; |
jhnwkmn | 0:97a4f8cc534c | 738 | if(typetag != 0) { |
jhnwkmn | 0:97a4f8cc534c | 739 | SQClass *cl = _instance(o)->_class; |
jhnwkmn | 0:97a4f8cc534c | 740 | do{ |
jhnwkmn | 0:97a4f8cc534c | 741 | if(cl->_typetag == typetag) |
jhnwkmn | 0:97a4f8cc534c | 742 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 743 | cl = cl->_base; |
jhnwkmn | 0:97a4f8cc534c | 744 | }while(cl != NULL); |
jhnwkmn | 0:97a4f8cc534c | 745 | return sq_throwerror(v,_SC("invalid type tag")); |
jhnwkmn | 0:97a4f8cc534c | 746 | } |
jhnwkmn | 0:97a4f8cc534c | 747 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 748 | } |
jhnwkmn | 0:97a4f8cc534c | 749 | |
jhnwkmn | 0:97a4f8cc534c | 750 | SQInteger sq_gettop(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 751 | { |
jhnwkmn | 0:97a4f8cc534c | 752 | return (v->_top) - v->_stackbase; |
jhnwkmn | 0:97a4f8cc534c | 753 | } |
jhnwkmn | 0:97a4f8cc534c | 754 | |
jhnwkmn | 0:97a4f8cc534c | 755 | void sq_settop(HSQUIRRELVM v, SQInteger newtop) |
jhnwkmn | 0:97a4f8cc534c | 756 | { |
jhnwkmn | 0:97a4f8cc534c | 757 | SQInteger top = sq_gettop(v); |
jhnwkmn | 0:97a4f8cc534c | 758 | if(top > newtop) |
jhnwkmn | 0:97a4f8cc534c | 759 | sq_pop(v, top - newtop); |
jhnwkmn | 0:97a4f8cc534c | 760 | else |
jhnwkmn | 0:97a4f8cc534c | 761 | while(top++ < newtop) sq_pushnull(v); |
jhnwkmn | 0:97a4f8cc534c | 762 | } |
jhnwkmn | 0:97a4f8cc534c | 763 | |
jhnwkmn | 0:97a4f8cc534c | 764 | void sq_pop(HSQUIRRELVM v, SQInteger nelemstopop) |
jhnwkmn | 0:97a4f8cc534c | 765 | { |
jhnwkmn | 0:97a4f8cc534c | 766 | assert(v->_top >= nelemstopop); |
jhnwkmn | 0:97a4f8cc534c | 767 | v->Pop(nelemstopop); |
jhnwkmn | 0:97a4f8cc534c | 768 | } |
jhnwkmn | 0:97a4f8cc534c | 769 | |
jhnwkmn | 0:97a4f8cc534c | 770 | void sq_poptop(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 771 | { |
jhnwkmn | 0:97a4f8cc534c | 772 | assert(v->_top >= 1); |
jhnwkmn | 0:97a4f8cc534c | 773 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 774 | } |
jhnwkmn | 0:97a4f8cc534c | 775 | |
jhnwkmn | 0:97a4f8cc534c | 776 | |
jhnwkmn | 0:97a4f8cc534c | 777 | void sq_remove(HSQUIRRELVM v, SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 778 | { |
jhnwkmn | 0:97a4f8cc534c | 779 | v->Remove(idx); |
jhnwkmn | 0:97a4f8cc534c | 780 | } |
jhnwkmn | 0:97a4f8cc534c | 781 | |
jhnwkmn | 0:97a4f8cc534c | 782 | SQInteger sq_cmp(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 783 | { |
jhnwkmn | 0:97a4f8cc534c | 784 | SQInteger res; |
jhnwkmn | 0:97a4f8cc534c | 785 | v->ObjCmp(stack_get(v, -1), stack_get(v, -2),res); |
jhnwkmn | 0:97a4f8cc534c | 786 | return res; |
jhnwkmn | 0:97a4f8cc534c | 787 | } |
jhnwkmn | 0:97a4f8cc534c | 788 | |
jhnwkmn | 0:97a4f8cc534c | 789 | SQRESULT sq_newslot(HSQUIRRELVM v, SQInteger idx, SQBool bstatic) |
jhnwkmn | 0:97a4f8cc534c | 790 | { |
jhnwkmn | 0:97a4f8cc534c | 791 | sq_aux_paramscheck(v, 3); |
jhnwkmn | 0:97a4f8cc534c | 792 | SQObjectPtr &self = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 793 | if(type(self) == OT_TABLE || type(self) == OT_CLASS) { |
jhnwkmn | 0:97a4f8cc534c | 794 | SQObjectPtr &key = v->GetUp(-2); |
jhnwkmn | 0:97a4f8cc534c | 795 | if(type(key) == OT_NULL) return sq_throwerror(v, _SC("null is not a valid key")); |
jhnwkmn | 0:97a4f8cc534c | 796 | v->NewSlot(self, key, v->GetUp(-1),bstatic?true:false); |
jhnwkmn | 0:97a4f8cc534c | 797 | v->Pop(2); |
jhnwkmn | 0:97a4f8cc534c | 798 | } |
jhnwkmn | 0:97a4f8cc534c | 799 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 800 | } |
jhnwkmn | 0:97a4f8cc534c | 801 | |
jhnwkmn | 0:97a4f8cc534c | 802 | SQRESULT sq_deleteslot(HSQUIRRELVM v,SQInteger idx,SQBool pushval) |
jhnwkmn | 0:97a4f8cc534c | 803 | { |
jhnwkmn | 0:97a4f8cc534c | 804 | sq_aux_paramscheck(v, 2); |
jhnwkmn | 0:97a4f8cc534c | 805 | SQObjectPtr *self; |
jhnwkmn | 0:97a4f8cc534c | 806 | _GETSAFE_OBJ(v, idx, OT_TABLE,self); |
jhnwkmn | 0:97a4f8cc534c | 807 | SQObjectPtr &key = v->GetUp(-1); |
jhnwkmn | 0:97a4f8cc534c | 808 | if(type(key) == OT_NULL) return sq_throwerror(v, _SC("null is not a valid key")); |
jhnwkmn | 0:97a4f8cc534c | 809 | SQObjectPtr res; |
jhnwkmn | 0:97a4f8cc534c | 810 | if(!v->DeleteSlot(*self, key, res)){ |
jhnwkmn | 0:97a4f8cc534c | 811 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 812 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 813 | } |
jhnwkmn | 0:97a4f8cc534c | 814 | if(pushval) v->GetUp(-1) = res; |
jhnwkmn | 0:97a4f8cc534c | 815 | else v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 816 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 817 | } |
jhnwkmn | 0:97a4f8cc534c | 818 | |
jhnwkmn | 0:97a4f8cc534c | 819 | SQRESULT sq_set(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 820 | { |
jhnwkmn | 0:97a4f8cc534c | 821 | SQObjectPtr &self = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 822 | if(v->Set(self, v->GetUp(-2), v->GetUp(-1),DONT_FALL_BACK)) { |
jhnwkmn | 0:97a4f8cc534c | 823 | v->Pop(2); |
jhnwkmn | 0:97a4f8cc534c | 824 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 825 | } |
jhnwkmn | 0:97a4f8cc534c | 826 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 827 | } |
jhnwkmn | 0:97a4f8cc534c | 828 | |
jhnwkmn | 0:97a4f8cc534c | 829 | SQRESULT sq_rawset(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 830 | { |
jhnwkmn | 0:97a4f8cc534c | 831 | SQObjectPtr &self = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 832 | if(type(v->GetUp(-2)) == OT_NULL) { |
jhnwkmn | 0:97a4f8cc534c | 833 | v->Pop(2); |
jhnwkmn | 0:97a4f8cc534c | 834 | return sq_throwerror(v, _SC("null key")); |
jhnwkmn | 0:97a4f8cc534c | 835 | } |
jhnwkmn | 0:97a4f8cc534c | 836 | switch(type(self)) { |
jhnwkmn | 0:97a4f8cc534c | 837 | case OT_TABLE: |
jhnwkmn | 0:97a4f8cc534c | 838 | _table(self)->NewSlot(v->GetUp(-2), v->GetUp(-1)); |
jhnwkmn | 0:97a4f8cc534c | 839 | v->Pop(2); |
jhnwkmn | 0:97a4f8cc534c | 840 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 841 | break; |
jhnwkmn | 0:97a4f8cc534c | 842 | case OT_CLASS: |
jhnwkmn | 0:97a4f8cc534c | 843 | _class(self)->NewSlot(_ss(v), v->GetUp(-2), v->GetUp(-1),false); |
jhnwkmn | 0:97a4f8cc534c | 844 | v->Pop(2); |
jhnwkmn | 0:97a4f8cc534c | 845 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 846 | break; |
jhnwkmn | 0:97a4f8cc534c | 847 | case OT_INSTANCE: |
jhnwkmn | 0:97a4f8cc534c | 848 | if(_instance(self)->Set(v->GetUp(-2), v->GetUp(-1))) { |
jhnwkmn | 0:97a4f8cc534c | 849 | v->Pop(2); |
jhnwkmn | 0:97a4f8cc534c | 850 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 851 | } |
jhnwkmn | 0:97a4f8cc534c | 852 | break; |
jhnwkmn | 0:97a4f8cc534c | 853 | case OT_ARRAY: |
jhnwkmn | 0:97a4f8cc534c | 854 | if(v->Set(self, v->GetUp(-2), v->GetUp(-1),false)) { |
jhnwkmn | 0:97a4f8cc534c | 855 | v->Pop(2); |
jhnwkmn | 0:97a4f8cc534c | 856 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 857 | } |
jhnwkmn | 0:97a4f8cc534c | 858 | break; |
jhnwkmn | 0:97a4f8cc534c | 859 | default: |
jhnwkmn | 0:97a4f8cc534c | 860 | v->Pop(2); |
jhnwkmn | 0:97a4f8cc534c | 861 | return sq_throwerror(v, _SC("rawset works only on array/table/class and instance")); |
jhnwkmn | 0:97a4f8cc534c | 862 | } |
jhnwkmn | 0:97a4f8cc534c | 863 | v->Raise_IdxError(v->GetUp(-2));return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 864 | } |
jhnwkmn | 0:97a4f8cc534c | 865 | |
jhnwkmn | 0:97a4f8cc534c | 866 | SQRESULT sq_newmember(HSQUIRRELVM v,SQInteger idx,SQBool bstatic) |
jhnwkmn | 0:97a4f8cc534c | 867 | { |
jhnwkmn | 0:97a4f8cc534c | 868 | SQObjectPtr &self = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 869 | if(type(self) != OT_CLASS) return sq_throwerror(v, _SC("new member only works with classes")); |
jhnwkmn | 0:97a4f8cc534c | 870 | if(type(v->GetUp(-3)) == OT_NULL) return sq_throwerror(v, _SC("null key")); |
jhnwkmn | 0:97a4f8cc534c | 871 | if(!v->NewSlotA(self,v->GetUp(-3),v->GetUp(-2),v->GetUp(-1),bstatic?true:false,false)) |
jhnwkmn | 0:97a4f8cc534c | 872 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 873 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 874 | } |
jhnwkmn | 0:97a4f8cc534c | 875 | |
jhnwkmn | 0:97a4f8cc534c | 876 | SQRESULT sq_rawnewmember(HSQUIRRELVM v,SQInteger idx,SQBool bstatic) |
jhnwkmn | 0:97a4f8cc534c | 877 | { |
jhnwkmn | 0:97a4f8cc534c | 878 | SQObjectPtr &self = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 879 | if(type(self) != OT_CLASS) return sq_throwerror(v, _SC("new member only works with classes")); |
jhnwkmn | 0:97a4f8cc534c | 880 | if(type(v->GetUp(-3)) == OT_NULL) return sq_throwerror(v, _SC("null key")); |
jhnwkmn | 0:97a4f8cc534c | 881 | if(!v->NewSlotA(self,v->GetUp(-3),v->GetUp(-2),v->GetUp(-1),bstatic?true:false,true)) |
jhnwkmn | 0:97a4f8cc534c | 882 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 883 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 884 | } |
jhnwkmn | 0:97a4f8cc534c | 885 | |
jhnwkmn | 0:97a4f8cc534c | 886 | SQRESULT sq_setdelegate(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 887 | { |
jhnwkmn | 0:97a4f8cc534c | 888 | SQObjectPtr &self = stack_get(v, idx); |
jhnwkmn | 0:97a4f8cc534c | 889 | SQObjectPtr &mt = v->GetUp(-1); |
jhnwkmn | 0:97a4f8cc534c | 890 | SQObjectType type = type(self); |
jhnwkmn | 0:97a4f8cc534c | 891 | switch(type) { |
jhnwkmn | 0:97a4f8cc534c | 892 | case OT_TABLE: |
jhnwkmn | 0:97a4f8cc534c | 893 | if(type(mt) == OT_TABLE) { |
jhnwkmn | 0:97a4f8cc534c | 894 | if(!_table(self)->SetDelegate(_table(mt))) return sq_throwerror(v, _SC("delagate cycle")); v->Pop();} |
jhnwkmn | 0:97a4f8cc534c | 895 | else if(type(mt)==OT_NULL) { |
jhnwkmn | 0:97a4f8cc534c | 896 | _table(self)->SetDelegate(NULL); v->Pop(); } |
jhnwkmn | 0:97a4f8cc534c | 897 | else return sq_aux_invalidtype(v,type); |
jhnwkmn | 0:97a4f8cc534c | 898 | break; |
jhnwkmn | 0:97a4f8cc534c | 899 | case OT_USERDATA: |
jhnwkmn | 0:97a4f8cc534c | 900 | if(type(mt)==OT_TABLE) { |
jhnwkmn | 0:97a4f8cc534c | 901 | _userdata(self)->SetDelegate(_table(mt)); v->Pop(); } |
jhnwkmn | 0:97a4f8cc534c | 902 | else if(type(mt)==OT_NULL) { |
jhnwkmn | 0:97a4f8cc534c | 903 | _userdata(self)->SetDelegate(NULL); v->Pop(); } |
jhnwkmn | 0:97a4f8cc534c | 904 | else return sq_aux_invalidtype(v, type); |
jhnwkmn | 0:97a4f8cc534c | 905 | break; |
jhnwkmn | 0:97a4f8cc534c | 906 | default: |
jhnwkmn | 0:97a4f8cc534c | 907 | return sq_aux_invalidtype(v, type); |
jhnwkmn | 0:97a4f8cc534c | 908 | break; |
jhnwkmn | 0:97a4f8cc534c | 909 | } |
jhnwkmn | 0:97a4f8cc534c | 910 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 911 | } |
jhnwkmn | 0:97a4f8cc534c | 912 | |
jhnwkmn | 0:97a4f8cc534c | 913 | SQRESULT sq_rawdeleteslot(HSQUIRRELVM v,SQInteger idx,SQBool pushval) |
jhnwkmn | 0:97a4f8cc534c | 914 | { |
jhnwkmn | 0:97a4f8cc534c | 915 | sq_aux_paramscheck(v, 2); |
jhnwkmn | 0:97a4f8cc534c | 916 | SQObjectPtr *self; |
jhnwkmn | 0:97a4f8cc534c | 917 | _GETSAFE_OBJ(v, idx, OT_TABLE,self); |
jhnwkmn | 0:97a4f8cc534c | 918 | SQObjectPtr &key = v->GetUp(-1); |
jhnwkmn | 0:97a4f8cc534c | 919 | SQObjectPtr t; |
jhnwkmn | 0:97a4f8cc534c | 920 | if(_table(*self)->Get(key,t)) { |
jhnwkmn | 0:97a4f8cc534c | 921 | _table(*self)->Remove(key); |
jhnwkmn | 0:97a4f8cc534c | 922 | } |
jhnwkmn | 0:97a4f8cc534c | 923 | if(pushval != 0) |
jhnwkmn | 0:97a4f8cc534c | 924 | v->GetUp(-1) = t; |
jhnwkmn | 0:97a4f8cc534c | 925 | else |
jhnwkmn | 0:97a4f8cc534c | 926 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 927 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 928 | } |
jhnwkmn | 0:97a4f8cc534c | 929 | |
jhnwkmn | 0:97a4f8cc534c | 930 | SQRESULT sq_getdelegate(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 931 | { |
jhnwkmn | 0:97a4f8cc534c | 932 | SQObjectPtr &self=stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 933 | switch(type(self)){ |
jhnwkmn | 0:97a4f8cc534c | 934 | case OT_TABLE: |
jhnwkmn | 0:97a4f8cc534c | 935 | case OT_USERDATA: |
jhnwkmn | 0:97a4f8cc534c | 936 | if(!_delegable(self)->_delegate){ |
jhnwkmn | 0:97a4f8cc534c | 937 | v->PushNull(); |
jhnwkmn | 0:97a4f8cc534c | 938 | break; |
jhnwkmn | 0:97a4f8cc534c | 939 | } |
jhnwkmn | 0:97a4f8cc534c | 940 | v->Push(SQObjectPtr(_delegable(self)->_delegate)); |
jhnwkmn | 0:97a4f8cc534c | 941 | break; |
jhnwkmn | 0:97a4f8cc534c | 942 | default: return sq_throwerror(v,_SC("wrong type")); break; |
jhnwkmn | 0:97a4f8cc534c | 943 | } |
jhnwkmn | 0:97a4f8cc534c | 944 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 945 | |
jhnwkmn | 0:97a4f8cc534c | 946 | } |
jhnwkmn | 0:97a4f8cc534c | 947 | |
jhnwkmn | 0:97a4f8cc534c | 948 | SQRESULT sq_get(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 949 | { |
jhnwkmn | 0:97a4f8cc534c | 950 | SQObjectPtr &self=stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 951 | if(v->Get(self,v->GetUp(-1),v->GetUp(-1),false,DONT_FALL_BACK)) |
jhnwkmn | 0:97a4f8cc534c | 952 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 953 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 954 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 955 | } |
jhnwkmn | 0:97a4f8cc534c | 956 | |
jhnwkmn | 0:97a4f8cc534c | 957 | SQRESULT sq_rawget(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 958 | { |
jhnwkmn | 0:97a4f8cc534c | 959 | SQObjectPtr &self=stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 960 | switch(type(self)) { |
jhnwkmn | 0:97a4f8cc534c | 961 | case OT_TABLE: |
jhnwkmn | 0:97a4f8cc534c | 962 | if(_table(self)->Get(v->GetUp(-1),v->GetUp(-1))) |
jhnwkmn | 0:97a4f8cc534c | 963 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 964 | break; |
jhnwkmn | 0:97a4f8cc534c | 965 | case OT_CLASS: |
jhnwkmn | 0:97a4f8cc534c | 966 | if(_class(self)->Get(v->GetUp(-1),v->GetUp(-1))) |
jhnwkmn | 0:97a4f8cc534c | 967 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 968 | break; |
jhnwkmn | 0:97a4f8cc534c | 969 | case OT_INSTANCE: |
jhnwkmn | 0:97a4f8cc534c | 970 | if(_instance(self)->Get(v->GetUp(-1),v->GetUp(-1))) |
jhnwkmn | 0:97a4f8cc534c | 971 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 972 | break; |
jhnwkmn | 0:97a4f8cc534c | 973 | case OT_ARRAY:{ |
jhnwkmn | 0:97a4f8cc534c | 974 | SQObjectPtr& key = v->GetUp(-1); |
jhnwkmn | 0:97a4f8cc534c | 975 | if(sq_isnumeric(key)){ |
jhnwkmn | 0:97a4f8cc534c | 976 | if(_array(self)->Get(tointeger(key),v->GetUp(-1))) { |
jhnwkmn | 0:97a4f8cc534c | 977 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 978 | } |
jhnwkmn | 0:97a4f8cc534c | 979 | } |
jhnwkmn | 0:97a4f8cc534c | 980 | else { |
jhnwkmn | 0:97a4f8cc534c | 981 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 982 | return sq_throwerror(v,_SC("invalid index type for an array")); |
jhnwkmn | 0:97a4f8cc534c | 983 | } |
jhnwkmn | 0:97a4f8cc534c | 984 | } |
jhnwkmn | 0:97a4f8cc534c | 985 | break; |
jhnwkmn | 0:97a4f8cc534c | 986 | default: |
jhnwkmn | 0:97a4f8cc534c | 987 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 988 | return sq_throwerror(v,_SC("rawget works only on array/table/instance and class")); |
jhnwkmn | 0:97a4f8cc534c | 989 | } |
jhnwkmn | 0:97a4f8cc534c | 990 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 991 | return sq_throwerror(v,_SC("the index doesn't exist")); |
jhnwkmn | 0:97a4f8cc534c | 992 | } |
jhnwkmn | 0:97a4f8cc534c | 993 | |
jhnwkmn | 0:97a4f8cc534c | 994 | SQRESULT sq_getstackobj(HSQUIRRELVM v,SQInteger idx,HSQOBJECT *po) |
jhnwkmn | 0:97a4f8cc534c | 995 | { |
jhnwkmn | 0:97a4f8cc534c | 996 | *po=stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 997 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 998 | } |
jhnwkmn | 0:97a4f8cc534c | 999 | |
jhnwkmn | 0:97a4f8cc534c | 1000 | const SQChar *sq_getlocal(HSQUIRRELVM v,SQUnsignedInteger level,SQUnsignedInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 1001 | { |
jhnwkmn | 0:97a4f8cc534c | 1002 | SQUnsignedInteger cstksize=v->_callsstacksize; |
jhnwkmn | 0:97a4f8cc534c | 1003 | SQUnsignedInteger lvl=(cstksize-level)-1; |
jhnwkmn | 0:97a4f8cc534c | 1004 | SQInteger stackbase=v->_stackbase; |
jhnwkmn | 0:97a4f8cc534c | 1005 | if(lvl<cstksize){ |
jhnwkmn | 0:97a4f8cc534c | 1006 | for(SQUnsignedInteger i=0;i<level;i++){ |
jhnwkmn | 0:97a4f8cc534c | 1007 | SQVM::CallInfo &ci=v->_callsstack[(cstksize-i)-1]; |
jhnwkmn | 0:97a4f8cc534c | 1008 | stackbase-=ci._prevstkbase; |
jhnwkmn | 0:97a4f8cc534c | 1009 | } |
jhnwkmn | 0:97a4f8cc534c | 1010 | SQVM::CallInfo &ci=v->_callsstack[lvl]; |
jhnwkmn | 0:97a4f8cc534c | 1011 | if(type(ci._closure)!=OT_CLOSURE) |
jhnwkmn | 0:97a4f8cc534c | 1012 | return NULL; |
jhnwkmn | 0:97a4f8cc534c | 1013 | SQClosure *c=_closure(ci._closure); |
jhnwkmn | 0:97a4f8cc534c | 1014 | SQFunctionProto *func=c->_function; |
jhnwkmn | 0:97a4f8cc534c | 1015 | if(func->_noutervalues > (SQInteger)idx) { |
jhnwkmn | 0:97a4f8cc534c | 1016 | v->Push(*_outer(c->_outervalues[idx])->_valptr); |
jhnwkmn | 0:97a4f8cc534c | 1017 | return _stringval(func->_outervalues[idx]._name); |
jhnwkmn | 0:97a4f8cc534c | 1018 | } |
jhnwkmn | 0:97a4f8cc534c | 1019 | idx -= func->_noutervalues; |
jhnwkmn | 0:97a4f8cc534c | 1020 | return func->GetLocal(v,stackbase,idx,(SQInteger)(ci._ip-func->_instructions)-1); |
jhnwkmn | 0:97a4f8cc534c | 1021 | } |
jhnwkmn | 0:97a4f8cc534c | 1022 | return NULL; |
jhnwkmn | 0:97a4f8cc534c | 1023 | } |
jhnwkmn | 0:97a4f8cc534c | 1024 | |
jhnwkmn | 0:97a4f8cc534c | 1025 | void sq_pushobject(HSQUIRRELVM v,HSQOBJECT obj) |
jhnwkmn | 0:97a4f8cc534c | 1026 | { |
jhnwkmn | 0:97a4f8cc534c | 1027 | v->Push(SQObjectPtr(obj)); |
jhnwkmn | 0:97a4f8cc534c | 1028 | } |
jhnwkmn | 0:97a4f8cc534c | 1029 | |
jhnwkmn | 0:97a4f8cc534c | 1030 | void sq_resetobject(HSQOBJECT *po) |
jhnwkmn | 0:97a4f8cc534c | 1031 | { |
jhnwkmn | 0:97a4f8cc534c | 1032 | po->_unVal.pUserPointer=NULL;po->_type=OT_NULL; |
jhnwkmn | 0:97a4f8cc534c | 1033 | } |
jhnwkmn | 0:97a4f8cc534c | 1034 | |
jhnwkmn | 0:97a4f8cc534c | 1035 | SQRESULT sq_throwerror(HSQUIRRELVM v,const SQChar *err) |
jhnwkmn | 0:97a4f8cc534c | 1036 | { |
jhnwkmn | 0:97a4f8cc534c | 1037 | v->_lasterror=SQString::Create(_ss(v),err); |
jhnwkmn | 0:97a4f8cc534c | 1038 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 1039 | } |
jhnwkmn | 0:97a4f8cc534c | 1040 | |
jhnwkmn | 0:97a4f8cc534c | 1041 | SQRESULT sq_throwobject(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 1042 | { |
jhnwkmn | 0:97a4f8cc534c | 1043 | v->_lasterror = v->GetUp(-1); |
jhnwkmn | 0:97a4f8cc534c | 1044 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 1045 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 1046 | } |
jhnwkmn | 0:97a4f8cc534c | 1047 | |
jhnwkmn | 0:97a4f8cc534c | 1048 | |
jhnwkmn | 0:97a4f8cc534c | 1049 | void sq_reseterror(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 1050 | { |
jhnwkmn | 0:97a4f8cc534c | 1051 | v->_lasterror.Null(); |
jhnwkmn | 0:97a4f8cc534c | 1052 | } |
jhnwkmn | 0:97a4f8cc534c | 1053 | |
jhnwkmn | 0:97a4f8cc534c | 1054 | void sq_getlasterror(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 1055 | { |
jhnwkmn | 0:97a4f8cc534c | 1056 | v->Push(v->_lasterror); |
jhnwkmn | 0:97a4f8cc534c | 1057 | } |
jhnwkmn | 0:97a4f8cc534c | 1058 | |
jhnwkmn | 0:97a4f8cc534c | 1059 | SQRESULT sq_reservestack(HSQUIRRELVM v,SQInteger nsize) |
jhnwkmn | 0:97a4f8cc534c | 1060 | { |
jhnwkmn | 0:97a4f8cc534c | 1061 | if (((SQUnsignedInteger)v->_top + nsize) > v->_stack.size()) { |
jhnwkmn | 0:97a4f8cc534c | 1062 | if(v->_nmetamethodscall) { |
jhnwkmn | 0:97a4f8cc534c | 1063 | return sq_throwerror(v,_SC("cannot resize stack while in a metamethod")); |
jhnwkmn | 0:97a4f8cc534c | 1064 | } |
jhnwkmn | 0:97a4f8cc534c | 1065 | v->_stack.resize(v->_stack.size() + ((v->_top + nsize) - v->_stack.size())); |
jhnwkmn | 0:97a4f8cc534c | 1066 | } |
jhnwkmn | 0:97a4f8cc534c | 1067 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1068 | } |
jhnwkmn | 0:97a4f8cc534c | 1069 | |
jhnwkmn | 0:97a4f8cc534c | 1070 | SQRESULT sq_resume(HSQUIRRELVM v,SQBool retval,SQBool raiseerror) |
jhnwkmn | 0:97a4f8cc534c | 1071 | { |
jhnwkmn | 0:97a4f8cc534c | 1072 | if(type(v->GetUp(-1))==OT_GENERATOR){ |
jhnwkmn | 0:97a4f8cc534c | 1073 | v->PushNull(); //retval |
jhnwkmn | 0:97a4f8cc534c | 1074 | if(!v->Execute(v->GetUp(-2),0,v->_top,v->GetUp(-1),raiseerror,SQVM::ET_RESUME_GENERATOR)) |
jhnwkmn | 0:97a4f8cc534c | 1075 | {v->Raise_Error(v->_lasterror); return SQ_ERROR;} |
jhnwkmn | 0:97a4f8cc534c | 1076 | if(!retval) |
jhnwkmn | 0:97a4f8cc534c | 1077 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 1078 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1079 | } |
jhnwkmn | 0:97a4f8cc534c | 1080 | return sq_throwerror(v,_SC("only generators can be resumed")); |
jhnwkmn | 0:97a4f8cc534c | 1081 | } |
jhnwkmn | 0:97a4f8cc534c | 1082 | |
jhnwkmn | 0:97a4f8cc534c | 1083 | SQRESULT sq_call(HSQUIRRELVM v,SQInteger params,SQBool retval,SQBool raiseerror) |
jhnwkmn | 0:97a4f8cc534c | 1084 | { |
jhnwkmn | 0:97a4f8cc534c | 1085 | SQObjectPtr res; |
jhnwkmn | 0:97a4f8cc534c | 1086 | if(v->Call(v->GetUp(-(params+1)),params,v->_top-params,res,raiseerror?true:false)){ |
jhnwkmn | 0:97a4f8cc534c | 1087 | |
jhnwkmn | 0:97a4f8cc534c | 1088 | if(!v->_suspended) { |
jhnwkmn | 0:97a4f8cc534c | 1089 | v->Pop(params);//pop closure and args |
jhnwkmn | 0:97a4f8cc534c | 1090 | } |
jhnwkmn | 0:97a4f8cc534c | 1091 | if(retval){ |
jhnwkmn | 0:97a4f8cc534c | 1092 | v->Push(res); return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1093 | } |
jhnwkmn | 0:97a4f8cc534c | 1094 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1095 | } |
jhnwkmn | 0:97a4f8cc534c | 1096 | else { |
jhnwkmn | 0:97a4f8cc534c | 1097 | v->Pop(params); |
jhnwkmn | 0:97a4f8cc534c | 1098 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 1099 | } |
jhnwkmn | 0:97a4f8cc534c | 1100 | if(!v->_suspended) |
jhnwkmn | 0:97a4f8cc534c | 1101 | v->Pop(params); |
jhnwkmn | 0:97a4f8cc534c | 1102 | return sq_throwerror(v,_SC("call failed")); |
jhnwkmn | 0:97a4f8cc534c | 1103 | } |
jhnwkmn | 0:97a4f8cc534c | 1104 | |
jhnwkmn | 0:97a4f8cc534c | 1105 | SQRESULT sq_suspendvm(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 1106 | { |
jhnwkmn | 0:97a4f8cc534c | 1107 | return v->Suspend(); |
jhnwkmn | 0:97a4f8cc534c | 1108 | } |
jhnwkmn | 0:97a4f8cc534c | 1109 | |
jhnwkmn | 0:97a4f8cc534c | 1110 | SQRESULT sq_wakeupvm(HSQUIRRELVM v,SQBool wakeupret,SQBool retval,SQBool raiseerror,SQBool throwerror) |
jhnwkmn | 0:97a4f8cc534c | 1111 | { |
jhnwkmn | 0:97a4f8cc534c | 1112 | SQObjectPtr ret; |
jhnwkmn | 0:97a4f8cc534c | 1113 | if(!v->_suspended) |
jhnwkmn | 0:97a4f8cc534c | 1114 | return sq_throwerror(v,_SC("cannot resume a vm that is not running any code")); |
jhnwkmn | 0:97a4f8cc534c | 1115 | SQInteger target = v->_suspended_target; |
jhnwkmn | 0:97a4f8cc534c | 1116 | if(wakeupret) { |
jhnwkmn | 0:97a4f8cc534c | 1117 | if(target != -1) { |
jhnwkmn | 0:97a4f8cc534c | 1118 | v->GetAt(v->_stackbase+v->_suspended_target)=v->GetUp(-1); //retval |
jhnwkmn | 0:97a4f8cc534c | 1119 | } |
jhnwkmn | 0:97a4f8cc534c | 1120 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 1121 | } else if(target != -1) { v->GetAt(v->_stackbase+v->_suspended_target).Null(); } |
jhnwkmn | 0:97a4f8cc534c | 1122 | SQObjectPtr dummy; |
jhnwkmn | 0:97a4f8cc534c | 1123 | if(!v->Execute(dummy,-1,-1,ret,raiseerror,throwerror?SQVM::ET_RESUME_THROW_VM : SQVM::ET_RESUME_VM)) { |
jhnwkmn | 0:97a4f8cc534c | 1124 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 1125 | } |
jhnwkmn | 0:97a4f8cc534c | 1126 | if(retval) |
jhnwkmn | 0:97a4f8cc534c | 1127 | v->Push(ret); |
jhnwkmn | 0:97a4f8cc534c | 1128 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1129 | } |
jhnwkmn | 0:97a4f8cc534c | 1130 | |
jhnwkmn | 0:97a4f8cc534c | 1131 | void sq_setreleasehook(HSQUIRRELVM v,SQInteger idx,SQRELEASEHOOK hook) |
jhnwkmn | 0:97a4f8cc534c | 1132 | { |
jhnwkmn | 0:97a4f8cc534c | 1133 | if(sq_gettop(v) >= 1){ |
jhnwkmn | 0:97a4f8cc534c | 1134 | SQObjectPtr &ud=stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 1135 | switch( type(ud) ) { |
jhnwkmn | 0:97a4f8cc534c | 1136 | case OT_USERDATA: _userdata(ud)->_hook = hook; break; |
jhnwkmn | 0:97a4f8cc534c | 1137 | case OT_INSTANCE: _instance(ud)->_hook = hook; break; |
jhnwkmn | 0:97a4f8cc534c | 1138 | case OT_CLASS: _class(ud)->_hook = hook; break; |
jhnwkmn | 0:97a4f8cc534c | 1139 | default: break; //shutup compiler |
jhnwkmn | 0:97a4f8cc534c | 1140 | } |
jhnwkmn | 0:97a4f8cc534c | 1141 | } |
jhnwkmn | 0:97a4f8cc534c | 1142 | } |
jhnwkmn | 0:97a4f8cc534c | 1143 | |
jhnwkmn | 0:97a4f8cc534c | 1144 | void sq_setcompilererrorhandler(HSQUIRRELVM v,SQCOMPILERERROR f) |
jhnwkmn | 0:97a4f8cc534c | 1145 | { |
jhnwkmn | 0:97a4f8cc534c | 1146 | _ss(v)->_compilererrorhandler = f; |
jhnwkmn | 0:97a4f8cc534c | 1147 | } |
jhnwkmn | 0:97a4f8cc534c | 1148 | |
jhnwkmn | 0:97a4f8cc534c | 1149 | SQRESULT sq_writeclosure(HSQUIRRELVM v,SQWRITEFUNC w,SQUserPointer up) |
jhnwkmn | 0:97a4f8cc534c | 1150 | { |
jhnwkmn | 0:97a4f8cc534c | 1151 | SQObjectPtr *o = NULL; |
jhnwkmn | 0:97a4f8cc534c | 1152 | _GETSAFE_OBJ(v, -1, OT_CLOSURE,o); |
jhnwkmn | 0:97a4f8cc534c | 1153 | unsigned short tag = SQ_BYTECODE_STREAM_TAG; |
jhnwkmn | 0:97a4f8cc534c | 1154 | if(_closure(*o)->_function->_noutervalues) |
jhnwkmn | 0:97a4f8cc534c | 1155 | return sq_throwerror(v,_SC("a closure with free valiables bound it cannot be serialized")); |
jhnwkmn | 0:97a4f8cc534c | 1156 | if(w(up,&tag,2) != 2) |
jhnwkmn | 0:97a4f8cc534c | 1157 | return sq_throwerror(v,_SC("io error")); |
jhnwkmn | 0:97a4f8cc534c | 1158 | if(!_closure(*o)->Save(v,up,w)) |
jhnwkmn | 0:97a4f8cc534c | 1159 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 1160 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1161 | } |
jhnwkmn | 0:97a4f8cc534c | 1162 | |
jhnwkmn | 0:97a4f8cc534c | 1163 | SQRESULT sq_readclosure(HSQUIRRELVM v,SQREADFUNC r,SQUserPointer up) |
jhnwkmn | 0:97a4f8cc534c | 1164 | { |
jhnwkmn | 0:97a4f8cc534c | 1165 | SQObjectPtr closure; |
jhnwkmn | 0:97a4f8cc534c | 1166 | |
jhnwkmn | 0:97a4f8cc534c | 1167 | unsigned short tag; |
jhnwkmn | 0:97a4f8cc534c | 1168 | if(r(up,&tag,2) != 2) |
jhnwkmn | 0:97a4f8cc534c | 1169 | return sq_throwerror(v,_SC("io error")); |
jhnwkmn | 0:97a4f8cc534c | 1170 | if(tag != SQ_BYTECODE_STREAM_TAG) |
jhnwkmn | 0:97a4f8cc534c | 1171 | return sq_throwerror(v,_SC("invalid stream")); |
jhnwkmn | 0:97a4f8cc534c | 1172 | if(!SQClosure::Load(v,up,r,closure)) |
jhnwkmn | 0:97a4f8cc534c | 1173 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 1174 | v->Push(closure); |
jhnwkmn | 0:97a4f8cc534c | 1175 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1176 | } |
jhnwkmn | 0:97a4f8cc534c | 1177 | |
jhnwkmn | 0:97a4f8cc534c | 1178 | SQChar *sq_getscratchpad(HSQUIRRELVM v,SQInteger minsize) |
jhnwkmn | 0:97a4f8cc534c | 1179 | { |
jhnwkmn | 0:97a4f8cc534c | 1180 | return _ss(v)->GetScratchPad(minsize); |
jhnwkmn | 0:97a4f8cc534c | 1181 | } |
jhnwkmn | 0:97a4f8cc534c | 1182 | |
jhnwkmn | 0:97a4f8cc534c | 1183 | SQRESULT sq_resurrectunreachable(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 1184 | { |
jhnwkmn | 0:97a4f8cc534c | 1185 | #ifndef NO_GARBAGE_COLLECTOR |
jhnwkmn | 0:97a4f8cc534c | 1186 | _ss(v)->ResurrectUnreachable(v); |
jhnwkmn | 0:97a4f8cc534c | 1187 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1188 | #else |
jhnwkmn | 0:97a4f8cc534c | 1189 | return sq_throwerror(v,_SC("sq_resurrectunreachable requires a garbage collector build")); |
jhnwkmn | 0:97a4f8cc534c | 1190 | #endif |
jhnwkmn | 0:97a4f8cc534c | 1191 | } |
jhnwkmn | 0:97a4f8cc534c | 1192 | |
jhnwkmn | 0:97a4f8cc534c | 1193 | SQInteger sq_collectgarbage(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 1194 | { |
jhnwkmn | 0:97a4f8cc534c | 1195 | #ifndef NO_GARBAGE_COLLECTOR |
jhnwkmn | 0:97a4f8cc534c | 1196 | return _ss(v)->CollectGarbage(v); |
jhnwkmn | 0:97a4f8cc534c | 1197 | #else |
jhnwkmn | 0:97a4f8cc534c | 1198 | return -1; |
jhnwkmn | 0:97a4f8cc534c | 1199 | #endif |
jhnwkmn | 0:97a4f8cc534c | 1200 | } |
jhnwkmn | 0:97a4f8cc534c | 1201 | |
jhnwkmn | 0:97a4f8cc534c | 1202 | SQRESULT sq_getcallee(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 1203 | { |
jhnwkmn | 0:97a4f8cc534c | 1204 | if(v->_callsstacksize > 1) |
jhnwkmn | 0:97a4f8cc534c | 1205 | { |
jhnwkmn | 0:97a4f8cc534c | 1206 | v->Push(v->_callsstack[v->_callsstacksize - 2]._closure); |
jhnwkmn | 0:97a4f8cc534c | 1207 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1208 | } |
jhnwkmn | 0:97a4f8cc534c | 1209 | return sq_throwerror(v,_SC("no closure in the calls stack")); |
jhnwkmn | 0:97a4f8cc534c | 1210 | } |
jhnwkmn | 0:97a4f8cc534c | 1211 | |
jhnwkmn | 0:97a4f8cc534c | 1212 | const SQChar *sq_getfreevariable(HSQUIRRELVM v,SQInteger idx,SQUnsignedInteger nval) |
jhnwkmn | 0:97a4f8cc534c | 1213 | { |
jhnwkmn | 0:97a4f8cc534c | 1214 | SQObjectPtr &self=stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 1215 | const SQChar *name = NULL; |
jhnwkmn | 0:97a4f8cc534c | 1216 | switch(type(self)) |
jhnwkmn | 0:97a4f8cc534c | 1217 | { |
jhnwkmn | 0:97a4f8cc534c | 1218 | case OT_CLOSURE:{ |
jhnwkmn | 0:97a4f8cc534c | 1219 | SQClosure *clo = _closure(self); |
jhnwkmn | 0:97a4f8cc534c | 1220 | SQFunctionProto *fp = clo->_function; |
jhnwkmn | 0:97a4f8cc534c | 1221 | if(((SQUnsignedInteger)fp->_noutervalues) > nval) { |
jhnwkmn | 0:97a4f8cc534c | 1222 | v->Push(*(_outer(clo->_outervalues[nval])->_valptr)); |
jhnwkmn | 0:97a4f8cc534c | 1223 | SQOuterVar &ov = fp->_outervalues[nval]; |
jhnwkmn | 0:97a4f8cc534c | 1224 | name = _stringval(ov._name); |
jhnwkmn | 0:97a4f8cc534c | 1225 | } |
jhnwkmn | 0:97a4f8cc534c | 1226 | } |
jhnwkmn | 0:97a4f8cc534c | 1227 | break; |
jhnwkmn | 0:97a4f8cc534c | 1228 | case OT_NATIVECLOSURE:{ |
jhnwkmn | 0:97a4f8cc534c | 1229 | SQNativeClosure *clo = _nativeclosure(self); |
jhnwkmn | 0:97a4f8cc534c | 1230 | if(clo->_noutervalues > nval) { |
jhnwkmn | 0:97a4f8cc534c | 1231 | v->Push(clo->_outervalues[nval]); |
jhnwkmn | 0:97a4f8cc534c | 1232 | name = _SC("@NATIVE"); |
jhnwkmn | 0:97a4f8cc534c | 1233 | } |
jhnwkmn | 0:97a4f8cc534c | 1234 | } |
jhnwkmn | 0:97a4f8cc534c | 1235 | break; |
jhnwkmn | 0:97a4f8cc534c | 1236 | default: break; //shutup compiler |
jhnwkmn | 0:97a4f8cc534c | 1237 | } |
jhnwkmn | 0:97a4f8cc534c | 1238 | return name; |
jhnwkmn | 0:97a4f8cc534c | 1239 | } |
jhnwkmn | 0:97a4f8cc534c | 1240 | |
jhnwkmn | 0:97a4f8cc534c | 1241 | SQRESULT sq_setfreevariable(HSQUIRRELVM v,SQInteger idx,SQUnsignedInteger nval) |
jhnwkmn | 0:97a4f8cc534c | 1242 | { |
jhnwkmn | 0:97a4f8cc534c | 1243 | SQObjectPtr &self=stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 1244 | switch(type(self)) |
jhnwkmn | 0:97a4f8cc534c | 1245 | { |
jhnwkmn | 0:97a4f8cc534c | 1246 | case OT_CLOSURE:{ |
jhnwkmn | 0:97a4f8cc534c | 1247 | SQFunctionProto *fp = _closure(self)->_function; |
jhnwkmn | 0:97a4f8cc534c | 1248 | if(((SQUnsignedInteger)fp->_noutervalues) > nval){ |
jhnwkmn | 0:97a4f8cc534c | 1249 | *(_outer(_closure(self)->_outervalues[nval])->_valptr) = stack_get(v,-1); |
jhnwkmn | 0:97a4f8cc534c | 1250 | } |
jhnwkmn | 0:97a4f8cc534c | 1251 | else return sq_throwerror(v,_SC("invalid free var index")); |
jhnwkmn | 0:97a4f8cc534c | 1252 | } |
jhnwkmn | 0:97a4f8cc534c | 1253 | break; |
jhnwkmn | 0:97a4f8cc534c | 1254 | case OT_NATIVECLOSURE: |
jhnwkmn | 0:97a4f8cc534c | 1255 | if(_nativeclosure(self)->_noutervalues > nval){ |
jhnwkmn | 0:97a4f8cc534c | 1256 | _nativeclosure(self)->_outervalues[nval] = stack_get(v,-1); |
jhnwkmn | 0:97a4f8cc534c | 1257 | } |
jhnwkmn | 0:97a4f8cc534c | 1258 | else return sq_throwerror(v,_SC("invalid free var index")); |
jhnwkmn | 0:97a4f8cc534c | 1259 | break; |
jhnwkmn | 0:97a4f8cc534c | 1260 | default: |
jhnwkmn | 0:97a4f8cc534c | 1261 | return sq_aux_invalidtype(v,type(self)); |
jhnwkmn | 0:97a4f8cc534c | 1262 | } |
jhnwkmn | 0:97a4f8cc534c | 1263 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 1264 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1265 | } |
jhnwkmn | 0:97a4f8cc534c | 1266 | |
jhnwkmn | 0:97a4f8cc534c | 1267 | SQRESULT sq_setattributes(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 1268 | { |
jhnwkmn | 0:97a4f8cc534c | 1269 | SQObjectPtr *o = NULL; |
jhnwkmn | 0:97a4f8cc534c | 1270 | _GETSAFE_OBJ(v, idx, OT_CLASS,o); |
jhnwkmn | 0:97a4f8cc534c | 1271 | SQObjectPtr &key = stack_get(v,-2); |
jhnwkmn | 0:97a4f8cc534c | 1272 | SQObjectPtr &val = stack_get(v,-1); |
jhnwkmn | 0:97a4f8cc534c | 1273 | SQObjectPtr attrs; |
jhnwkmn | 0:97a4f8cc534c | 1274 | if(type(key) == OT_NULL) { |
jhnwkmn | 0:97a4f8cc534c | 1275 | attrs = _class(*o)->_attributes; |
jhnwkmn | 0:97a4f8cc534c | 1276 | _class(*o)->_attributes = val; |
jhnwkmn | 0:97a4f8cc534c | 1277 | v->Pop(2); |
jhnwkmn | 0:97a4f8cc534c | 1278 | v->Push(attrs); |
jhnwkmn | 0:97a4f8cc534c | 1279 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1280 | }else if(_class(*o)->GetAttributes(key,attrs)) { |
jhnwkmn | 0:97a4f8cc534c | 1281 | _class(*o)->SetAttributes(key,val); |
jhnwkmn | 0:97a4f8cc534c | 1282 | v->Pop(2); |
jhnwkmn | 0:97a4f8cc534c | 1283 | v->Push(attrs); |
jhnwkmn | 0:97a4f8cc534c | 1284 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1285 | } |
jhnwkmn | 0:97a4f8cc534c | 1286 | return sq_throwerror(v,_SC("wrong index")); |
jhnwkmn | 0:97a4f8cc534c | 1287 | } |
jhnwkmn | 0:97a4f8cc534c | 1288 | |
jhnwkmn | 0:97a4f8cc534c | 1289 | SQRESULT sq_getattributes(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 1290 | { |
jhnwkmn | 0:97a4f8cc534c | 1291 | SQObjectPtr *o = NULL; |
jhnwkmn | 0:97a4f8cc534c | 1292 | _GETSAFE_OBJ(v, idx, OT_CLASS,o); |
jhnwkmn | 0:97a4f8cc534c | 1293 | SQObjectPtr &key = stack_get(v,-1); |
jhnwkmn | 0:97a4f8cc534c | 1294 | SQObjectPtr attrs; |
jhnwkmn | 0:97a4f8cc534c | 1295 | if(type(key) == OT_NULL) { |
jhnwkmn | 0:97a4f8cc534c | 1296 | attrs = _class(*o)->_attributes; |
jhnwkmn | 0:97a4f8cc534c | 1297 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 1298 | v->Push(attrs); |
jhnwkmn | 0:97a4f8cc534c | 1299 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1300 | } |
jhnwkmn | 0:97a4f8cc534c | 1301 | else if(_class(*o)->GetAttributes(key,attrs)) { |
jhnwkmn | 0:97a4f8cc534c | 1302 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 1303 | v->Push(attrs); |
jhnwkmn | 0:97a4f8cc534c | 1304 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1305 | } |
jhnwkmn | 0:97a4f8cc534c | 1306 | return sq_throwerror(v,_SC("wrong index")); |
jhnwkmn | 0:97a4f8cc534c | 1307 | } |
jhnwkmn | 0:97a4f8cc534c | 1308 | |
jhnwkmn | 0:97a4f8cc534c | 1309 | SQRESULT sq_getmemberhandle(HSQUIRRELVM v,SQInteger idx,HSQMEMBERHANDLE *handle) |
jhnwkmn | 0:97a4f8cc534c | 1310 | { |
jhnwkmn | 0:97a4f8cc534c | 1311 | SQObjectPtr *o = NULL; |
jhnwkmn | 0:97a4f8cc534c | 1312 | _GETSAFE_OBJ(v, idx, OT_CLASS,o); |
jhnwkmn | 0:97a4f8cc534c | 1313 | SQObjectPtr &key = stack_get(v,-1); |
jhnwkmn | 0:97a4f8cc534c | 1314 | SQTable *m = _class(*o)->_members; |
jhnwkmn | 0:97a4f8cc534c | 1315 | SQObjectPtr val; |
jhnwkmn | 0:97a4f8cc534c | 1316 | if(m->Get(key,val)) { |
jhnwkmn | 0:97a4f8cc534c | 1317 | handle->_static = _isfield(val) ? SQFalse : SQTrue; |
jhnwkmn | 0:97a4f8cc534c | 1318 | handle->_index = _member_idx(val); |
jhnwkmn | 0:97a4f8cc534c | 1319 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 1320 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1321 | } |
jhnwkmn | 0:97a4f8cc534c | 1322 | return sq_throwerror(v,_SC("wrong index")); |
jhnwkmn | 0:97a4f8cc534c | 1323 | } |
jhnwkmn | 0:97a4f8cc534c | 1324 | |
jhnwkmn | 0:97a4f8cc534c | 1325 | SQRESULT _getmemberbyhandle(HSQUIRRELVM v,SQObjectPtr &self,const HSQMEMBERHANDLE *handle,SQObjectPtr *&val) |
jhnwkmn | 0:97a4f8cc534c | 1326 | { |
jhnwkmn | 0:97a4f8cc534c | 1327 | switch(type(self)) { |
jhnwkmn | 0:97a4f8cc534c | 1328 | case OT_INSTANCE: { |
jhnwkmn | 0:97a4f8cc534c | 1329 | SQInstance *i = _instance(self); |
jhnwkmn | 0:97a4f8cc534c | 1330 | if(handle->_static) { |
jhnwkmn | 0:97a4f8cc534c | 1331 | SQClass *c = i->_class; |
jhnwkmn | 0:97a4f8cc534c | 1332 | val = &c->_methods[handle->_index].val; |
jhnwkmn | 0:97a4f8cc534c | 1333 | } |
jhnwkmn | 0:97a4f8cc534c | 1334 | else { |
jhnwkmn | 0:97a4f8cc534c | 1335 | val = &i->_values[handle->_index]; |
jhnwkmn | 0:97a4f8cc534c | 1336 | |
jhnwkmn | 0:97a4f8cc534c | 1337 | } |
jhnwkmn | 0:97a4f8cc534c | 1338 | } |
jhnwkmn | 0:97a4f8cc534c | 1339 | break; |
jhnwkmn | 0:97a4f8cc534c | 1340 | case OT_CLASS: { |
jhnwkmn | 0:97a4f8cc534c | 1341 | SQClass *c = _class(self); |
jhnwkmn | 0:97a4f8cc534c | 1342 | if(handle->_static) { |
jhnwkmn | 0:97a4f8cc534c | 1343 | val = &c->_methods[handle->_index].val; |
jhnwkmn | 0:97a4f8cc534c | 1344 | } |
jhnwkmn | 0:97a4f8cc534c | 1345 | else { |
jhnwkmn | 0:97a4f8cc534c | 1346 | val = &c->_defaultvalues[handle->_index].val; |
jhnwkmn | 0:97a4f8cc534c | 1347 | } |
jhnwkmn | 0:97a4f8cc534c | 1348 | } |
jhnwkmn | 0:97a4f8cc534c | 1349 | break; |
jhnwkmn | 0:97a4f8cc534c | 1350 | default: |
jhnwkmn | 0:97a4f8cc534c | 1351 | return sq_throwerror(v,_SC("wrong type(expected class or instance)")); |
jhnwkmn | 0:97a4f8cc534c | 1352 | } |
jhnwkmn | 0:97a4f8cc534c | 1353 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1354 | } |
jhnwkmn | 0:97a4f8cc534c | 1355 | |
jhnwkmn | 0:97a4f8cc534c | 1356 | SQRESULT sq_getbyhandle(HSQUIRRELVM v,SQInteger idx,const HSQMEMBERHANDLE *handle) |
jhnwkmn | 0:97a4f8cc534c | 1357 | { |
jhnwkmn | 0:97a4f8cc534c | 1358 | SQObjectPtr &self = stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 1359 | SQObjectPtr *val = NULL; |
jhnwkmn | 0:97a4f8cc534c | 1360 | if(SQ_FAILED(_getmemberbyhandle(v,self,handle,val))) { |
jhnwkmn | 0:97a4f8cc534c | 1361 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 1362 | } |
jhnwkmn | 0:97a4f8cc534c | 1363 | v->Push(_realval(*val)); |
jhnwkmn | 0:97a4f8cc534c | 1364 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1365 | } |
jhnwkmn | 0:97a4f8cc534c | 1366 | |
jhnwkmn | 0:97a4f8cc534c | 1367 | SQRESULT sq_setbyhandle(HSQUIRRELVM v,SQInteger idx,const HSQMEMBERHANDLE *handle) |
jhnwkmn | 0:97a4f8cc534c | 1368 | { |
jhnwkmn | 0:97a4f8cc534c | 1369 | SQObjectPtr &self = stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 1370 | SQObjectPtr &newval = stack_get(v,-1); |
jhnwkmn | 0:97a4f8cc534c | 1371 | SQObjectPtr *val = NULL; |
jhnwkmn | 0:97a4f8cc534c | 1372 | if(SQ_FAILED(_getmemberbyhandle(v,self,handle,val))) { |
jhnwkmn | 0:97a4f8cc534c | 1373 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 1374 | } |
jhnwkmn | 0:97a4f8cc534c | 1375 | *val = newval; |
jhnwkmn | 0:97a4f8cc534c | 1376 | v->Pop(); |
jhnwkmn | 0:97a4f8cc534c | 1377 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1378 | } |
jhnwkmn | 0:97a4f8cc534c | 1379 | |
jhnwkmn | 0:97a4f8cc534c | 1380 | SQRESULT sq_getbase(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 1381 | { |
jhnwkmn | 0:97a4f8cc534c | 1382 | SQObjectPtr *o = NULL; |
jhnwkmn | 0:97a4f8cc534c | 1383 | _GETSAFE_OBJ(v, idx, OT_CLASS,o); |
jhnwkmn | 0:97a4f8cc534c | 1384 | if(_class(*o)->_base) |
jhnwkmn | 0:97a4f8cc534c | 1385 | v->Push(SQObjectPtr(_class(*o)->_base)); |
jhnwkmn | 0:97a4f8cc534c | 1386 | else |
jhnwkmn | 0:97a4f8cc534c | 1387 | v->PushNull(); |
jhnwkmn | 0:97a4f8cc534c | 1388 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1389 | } |
jhnwkmn | 0:97a4f8cc534c | 1390 | |
jhnwkmn | 0:97a4f8cc534c | 1391 | SQRESULT sq_getclass(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 1392 | { |
jhnwkmn | 0:97a4f8cc534c | 1393 | SQObjectPtr *o = NULL; |
jhnwkmn | 0:97a4f8cc534c | 1394 | _GETSAFE_OBJ(v, idx, OT_INSTANCE,o); |
jhnwkmn | 0:97a4f8cc534c | 1395 | v->Push(SQObjectPtr(_instance(*o)->_class)); |
jhnwkmn | 0:97a4f8cc534c | 1396 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1397 | } |
jhnwkmn | 0:97a4f8cc534c | 1398 | |
jhnwkmn | 0:97a4f8cc534c | 1399 | SQRESULT sq_createinstance(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 1400 | { |
jhnwkmn | 0:97a4f8cc534c | 1401 | SQObjectPtr *o = NULL; |
jhnwkmn | 0:97a4f8cc534c | 1402 | _GETSAFE_OBJ(v, idx, OT_CLASS,o); |
jhnwkmn | 0:97a4f8cc534c | 1403 | v->Push(_class(*o)->CreateInstance()); |
jhnwkmn | 0:97a4f8cc534c | 1404 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1405 | } |
jhnwkmn | 0:97a4f8cc534c | 1406 | |
jhnwkmn | 0:97a4f8cc534c | 1407 | void sq_weakref(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 1408 | { |
jhnwkmn | 0:97a4f8cc534c | 1409 | SQObject &o=stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 1410 | if(ISREFCOUNTED(type(o))) { |
jhnwkmn | 0:97a4f8cc534c | 1411 | v->Push(_refcounted(o)->GetWeakRef(type(o))); |
jhnwkmn | 0:97a4f8cc534c | 1412 | return; |
jhnwkmn | 0:97a4f8cc534c | 1413 | } |
jhnwkmn | 0:97a4f8cc534c | 1414 | v->Push(o); |
jhnwkmn | 0:97a4f8cc534c | 1415 | } |
jhnwkmn | 0:97a4f8cc534c | 1416 | |
jhnwkmn | 0:97a4f8cc534c | 1417 | SQRESULT sq_getweakrefval(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 1418 | { |
jhnwkmn | 0:97a4f8cc534c | 1419 | SQObjectPtr &o = stack_get(v,idx); |
jhnwkmn | 0:97a4f8cc534c | 1420 | if(type(o) != OT_WEAKREF) { |
jhnwkmn | 0:97a4f8cc534c | 1421 | return sq_throwerror(v,_SC("the object must be a weakref")); |
jhnwkmn | 0:97a4f8cc534c | 1422 | } |
jhnwkmn | 0:97a4f8cc534c | 1423 | v->Push(_weakref(o)->_obj); |
jhnwkmn | 0:97a4f8cc534c | 1424 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1425 | } |
jhnwkmn | 0:97a4f8cc534c | 1426 | |
jhnwkmn | 0:97a4f8cc534c | 1427 | SQRESULT sq_getdefaultdelegate(HSQUIRRELVM v,SQObjectType t) |
jhnwkmn | 0:97a4f8cc534c | 1428 | { |
jhnwkmn | 0:97a4f8cc534c | 1429 | SQSharedState *ss = _ss(v); |
jhnwkmn | 0:97a4f8cc534c | 1430 | switch(t) { |
jhnwkmn | 0:97a4f8cc534c | 1431 | case OT_TABLE: v->Push(ss->_table_default_delegate); break; |
jhnwkmn | 0:97a4f8cc534c | 1432 | case OT_ARRAY: v->Push(ss->_array_default_delegate); break; |
jhnwkmn | 0:97a4f8cc534c | 1433 | case OT_STRING: v->Push(ss->_string_default_delegate); break; |
jhnwkmn | 0:97a4f8cc534c | 1434 | case OT_INTEGER: case OT_FLOAT: v->Push(ss->_number_default_delegate); break; |
jhnwkmn | 0:97a4f8cc534c | 1435 | case OT_GENERATOR: v->Push(ss->_generator_default_delegate); break; |
jhnwkmn | 0:97a4f8cc534c | 1436 | case OT_CLOSURE: case OT_NATIVECLOSURE: v->Push(ss->_closure_default_delegate); break; |
jhnwkmn | 0:97a4f8cc534c | 1437 | case OT_THREAD: v->Push(ss->_thread_default_delegate); break; |
jhnwkmn | 0:97a4f8cc534c | 1438 | case OT_CLASS: v->Push(ss->_class_default_delegate); break; |
jhnwkmn | 0:97a4f8cc534c | 1439 | case OT_INSTANCE: v->Push(ss->_instance_default_delegate); break; |
jhnwkmn | 0:97a4f8cc534c | 1440 | case OT_WEAKREF: v->Push(ss->_weakref_default_delegate); break; |
jhnwkmn | 0:97a4f8cc534c | 1441 | default: return sq_throwerror(v,_SC("the type doesn't have a default delegate")); |
jhnwkmn | 0:97a4f8cc534c | 1442 | } |
jhnwkmn | 0:97a4f8cc534c | 1443 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1444 | } |
jhnwkmn | 0:97a4f8cc534c | 1445 | |
jhnwkmn | 0:97a4f8cc534c | 1446 | SQRESULT sq_next(HSQUIRRELVM v,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 1447 | { |
jhnwkmn | 0:97a4f8cc534c | 1448 | SQObjectPtr o=stack_get(v,idx),&refpos = stack_get(v,-1),realkey,val; |
jhnwkmn | 0:97a4f8cc534c | 1449 | if(type(o) == OT_GENERATOR) { |
jhnwkmn | 0:97a4f8cc534c | 1450 | return sq_throwerror(v,_SC("cannot iterate a generator")); |
jhnwkmn | 0:97a4f8cc534c | 1451 | } |
jhnwkmn | 0:97a4f8cc534c | 1452 | int faketojump; |
jhnwkmn | 0:97a4f8cc534c | 1453 | if(!v->FOREACH_OP(o,realkey,val,refpos,0,666,faketojump)) |
jhnwkmn | 0:97a4f8cc534c | 1454 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 1455 | if(faketojump != 666) { |
jhnwkmn | 0:97a4f8cc534c | 1456 | v->Push(realkey); |
jhnwkmn | 0:97a4f8cc534c | 1457 | v->Push(val); |
jhnwkmn | 0:97a4f8cc534c | 1458 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 1459 | } |
jhnwkmn | 0:97a4f8cc534c | 1460 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 1461 | } |
jhnwkmn | 0:97a4f8cc534c | 1462 | |
jhnwkmn | 0:97a4f8cc534c | 1463 | struct BufState{ |
jhnwkmn | 0:97a4f8cc534c | 1464 | const SQChar *buf; |
jhnwkmn | 0:97a4f8cc534c | 1465 | SQInteger ptr; |
jhnwkmn | 0:97a4f8cc534c | 1466 | SQInteger size; |
jhnwkmn | 0:97a4f8cc534c | 1467 | }; |
jhnwkmn | 0:97a4f8cc534c | 1468 | |
jhnwkmn | 0:97a4f8cc534c | 1469 | SQInteger buf_lexfeed(SQUserPointer file) |
jhnwkmn | 0:97a4f8cc534c | 1470 | { |
jhnwkmn | 0:97a4f8cc534c | 1471 | BufState *buf=(BufState*)file; |
jhnwkmn | 0:97a4f8cc534c | 1472 | if(buf->size<(buf->ptr+1)) |
jhnwkmn | 0:97a4f8cc534c | 1473 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 1474 | return buf->buf[buf->ptr++]; |
jhnwkmn | 0:97a4f8cc534c | 1475 | } |
jhnwkmn | 0:97a4f8cc534c | 1476 | |
jhnwkmn | 0:97a4f8cc534c | 1477 | SQRESULT sq_compilebuffer(HSQUIRRELVM v,const SQChar *s,SQInteger size,const SQChar *sourcename,SQBool raiseerror) { |
jhnwkmn | 0:97a4f8cc534c | 1478 | BufState buf; |
jhnwkmn | 0:97a4f8cc534c | 1479 | buf.buf = s; |
jhnwkmn | 0:97a4f8cc534c | 1480 | buf.size = size; |
jhnwkmn | 0:97a4f8cc534c | 1481 | buf.ptr = 0; |
jhnwkmn | 0:97a4f8cc534c | 1482 | return sq_compile(v, buf_lexfeed, &buf, sourcename, raiseerror); |
jhnwkmn | 0:97a4f8cc534c | 1483 | } |
jhnwkmn | 0:97a4f8cc534c | 1484 | |
jhnwkmn | 0:97a4f8cc534c | 1485 | void sq_move(HSQUIRRELVM dest,HSQUIRRELVM src,SQInteger idx) |
jhnwkmn | 0:97a4f8cc534c | 1486 | { |
jhnwkmn | 0:97a4f8cc534c | 1487 | dest->Push(stack_get(src,idx)); |
jhnwkmn | 0:97a4f8cc534c | 1488 | } |
jhnwkmn | 0:97a4f8cc534c | 1489 | |
jhnwkmn | 0:97a4f8cc534c | 1490 | void sq_setprintfunc(HSQUIRRELVM v, SQPRINTFUNCTION printfunc,SQPRINTFUNCTION errfunc) |
jhnwkmn | 0:97a4f8cc534c | 1491 | { |
jhnwkmn | 0:97a4f8cc534c | 1492 | _ss(v)->_printfunc = printfunc; |
jhnwkmn | 0:97a4f8cc534c | 1493 | _ss(v)->_errorfunc = errfunc; |
jhnwkmn | 0:97a4f8cc534c | 1494 | } |
jhnwkmn | 0:97a4f8cc534c | 1495 | |
jhnwkmn | 0:97a4f8cc534c | 1496 | SQPRINTFUNCTION sq_getprintfunc(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 1497 | { |
jhnwkmn | 0:97a4f8cc534c | 1498 | return _ss(v)->_printfunc; |
jhnwkmn | 0:97a4f8cc534c | 1499 | } |
jhnwkmn | 0:97a4f8cc534c | 1500 | |
jhnwkmn | 0:97a4f8cc534c | 1501 | SQPRINTFUNCTION sq_geterrorfunc(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 1502 | { |
jhnwkmn | 0:97a4f8cc534c | 1503 | return _ss(v)->_errorfunc; |
jhnwkmn | 0:97a4f8cc534c | 1504 | } |
jhnwkmn | 0:97a4f8cc534c | 1505 | |
jhnwkmn | 0:97a4f8cc534c | 1506 | void *sq_malloc(SQUnsignedInteger size) |
jhnwkmn | 0:97a4f8cc534c | 1507 | { |
jhnwkmn | 0:97a4f8cc534c | 1508 | return SQ_MALLOC(size); |
jhnwkmn | 0:97a4f8cc534c | 1509 | } |
jhnwkmn | 0:97a4f8cc534c | 1510 | |
jhnwkmn | 0:97a4f8cc534c | 1511 | void *sq_realloc(void* p,SQUnsignedInteger oldsize,SQUnsignedInteger newsize) |
jhnwkmn | 0:97a4f8cc534c | 1512 | { |
jhnwkmn | 0:97a4f8cc534c | 1513 | return SQ_REALLOC(p,oldsize,newsize); |
jhnwkmn | 0:97a4f8cc534c | 1514 | } |
jhnwkmn | 0:97a4f8cc534c | 1515 | |
jhnwkmn | 0:97a4f8cc534c | 1516 | void sq_free(void *p,SQUnsignedInteger size) |
jhnwkmn | 0:97a4f8cc534c | 1517 | { |
jhnwkmn | 0:97a4f8cc534c | 1518 | SQ_FREE(p,size); |
jhnwkmn | 0:97a4f8cc534c | 1519 | } |