void CSquirrel::Call(SQObjectPtr pFunction, CSquirrelArguments * pArguments, CSquirrelArgument * pReturn) { // Get the stack top int iTop = sq_gettop(m_pVM); // Process the parameters if needed int iParams = 1; if(pArguments) { pArguments->push_to_vm(m_pVM); iParams += pArguments->GetArguments()->size(); } // Call the function SQObjectPtr res; if(m_pVM->Call(pFunction, iParams, m_pVM->_top-iParams, res, true)) { // Set the return value if needed if(pReturn) *pReturn = res; } // Restore the stack top sq_settop(m_pVM, iTop); }
/* * invoke impact event */ static SQBool invokeImpactEvent(HSQUIRRELVM v, ContactPoint cp) { SQBool result = false; SQInteger top = sq_gettop(v); sq_pushroottable(v); sq_pushstring(v, "emo", -1); if (SQ_SUCCEEDED(sq_get(v, -2))) { sq_pushstring(v, "_onImpact", -1); if(SQ_SUCCEEDED(sq_get(v, -2))) { sq_pushroottable(v); sq_pushuserpointer(v, cp.fixtureA); sq_pushuserpointer(v, cp.fixtureB); sq_pushuserpointer(v, cp.fixtureA->GetBody()); sq_pushuserpointer(v, cp.fixtureB->GetBody()); pushVec2(v, cp.position); pushVec2(v, cp.normal); sq_pushfloat(v, cp.normalImpulse); sq_pushfloat(v, cp.tangentImpulse); result = SQ_SUCCEEDED(sq_call(v, 9, SQFalse, SQTrue)); } } sq_settop(v,top); return result; }
bool CSquirrel::Call(SQObjectPtr& pFunction, CSquirrelArguments* pArguments) { // Get the stack top int iTop = sq_gettop(m_pVM); // push the root table onto the stack sq_pushroottable(m_pVM); // push all arguments if(pArguments) pArguments->push_to_vm(m_pVM); // Call the function SQObjectPtr res; bool success = m_pVM->Call(pFunction, (pArguments ? pArguments->GetArguments()->size() : 0) + 1, m_pVM->_top - (pArguments ? pArguments->GetArguments()->size() : 0) - 1, res, true); // Restore the stack top sq_settop(m_pVM, iTop); // delete the 'source' variable //sq_pushroottable(m_pVM); //sq_pushstring(m_pVM, "source", -1); //sq_deleteslot(m_pVM, -2, false); // return value dependant on the function's return (if it's a boolean false, then this will return false - otherwise true) if( success ) return res._type != OT_BOOL || ( res._unVal.nInteger == 0 ? false : true ); else return false; }
BOOL SbuCreateClass(HSQUIRRELVM v,SquirrelClassDecl *cd) { int n = 0; int oldtop = sq_gettop(v); sq_pushroottable(v); sq_pushstring(v,cd->name,-1); if(cd->base) { sq_pushstring(v,cd->base,-1); if(SQ_FAILED(sq_get(v,-3))) { sq_settop(v,oldtop); return FALSE; } } if(SQ_FAILED(sq_newclass(v,cd->base?1:0))) { sq_settop(v,oldtop); return FALSE; } sq_settypetag(v,-1,(SQUserPointer)cd); const ScriptClassMemberDecl *members = cd->members; const ScriptClassMemberDecl *m = NULL; while(members[n].name) { m = &members[n]; sq_pushstring(v,m->name,-1); sq_newclosure(v,m->func,0); sq_setparamscheck(v,m->params,m->typemask); sq_setnativeclosurename(v,-1,m->name); sq_createslot(v,-3); n++; } sq_createslot(v,-3); sq_pop(v,1); return TRUE; }
SQInteger _stream_writestr(HSQUIRRELVM v) { SETUP_STREAM(v); const SQChar *str,*res; SQInteger trgformat = 'a',len = 0; sq_getstring(v,2,&str); len = sq_getsize(v,2); if(sq_gettop(v)>2) sq_getinteger(v,3,&trgformat); switch(trgformat) { case 'a': #ifdef _UNICODE res = sq_getscratchpad(v,len*3); len = (SQInteger) wcstombs((char *)res, (const wchar_t*)str, len); #else res = str; #endif self->Write((void *)res,len); break; case 'u': #ifdef _UNICODE res = str; #else res = sq_getscratchpad(v,len*sizeof(wchar_t)); len = (SQInteger) mbstowcs((wchar_t*)res, str, len); #endif self->Write((void *)res,len*sizeof(wchar_t)); break; default: return sq_throwerror(v,_SC("wrong encoding")); } return 0; }
bool sc_OnPlayerText(HSQUIRRELVM v, const short index, const wchar_t *text) { int result = 1; int top = sq_gettop(v); sq_pushroottable(v); sq_pushstring(v, _SC("OnPlayerText"), -1); if(SQ_SUCCEEDED(sq_get(v, -2))) { sq_pushroottable(v); sq_pushinteger(v, index); sq_pushstring(v, text, -1); sq_call(v, 3, 1, 1); sq_getinteger(v, sq_gettop(v), &result); } sq_settop(v, top); return result != 0; }
bool sc_CallSomeCallback(HSQUIRRELVM v, const wchar_t *callback, const int param) { int top = sq_gettop(v); int result = 0; sq_pushroottable(v); sq_pushstring(v, callback, -1); if(SQ_SUCCEEDED(sq_get(v, -2))) { sq_pushroottable(v); sq_pushinteger(v, param); sq_call(v, 2, 1, 1); sq_getinteger(v, sq_gettop(v), &result); } sq_settop(v, top); return result == 1; }
void CSquirrel::Call(SQObjectPtr pFunction, CSquirrelArguments * pArguments, CSquirrelArgument * pReturn) { // Lock the squirrel call mutex m_squirrelCallMutex.TryLock(10); // Get the stack top int iTop = sq_gettop(m_pVM); // Process the parameters if needed int iParams = 1; if(pArguments) { pArguments->push_to_vm(m_pVM); iParams += pArguments->size(); } // Call the function SQObjectPtr res; if(m_pVM->Call(pFunction, iParams, m_pVM->_top-iParams, res, true)) { // Set the return value if needed if(pReturn) pReturn->set(res); } // Restore the stack top sq_settop(m_pVM, iTop); // Unlock the squirrel call mutex m_squirrelCallMutex.Unlock(); }
ScriptText::ScriptText(HSQUIRRELVM vm) : ZeroedMemoryAllocator() { int nparam = sq_gettop(vm) - 1; if (nparam < 1) { throw sq_throwerror(vm, _SC("You need to pass at least a StringID to the constructor")); } /* First resolve the StringID. */ SQInteger sqstring; if (SQ_FAILED(sq_getinteger(vm, 2, &sqstring))) { throw sq_throwerror(vm, _SC("First argument must be a valid StringID")); } this->string = sqstring; /* The rest of the parameters must be arguments. */ for (int i = 0; i < nparam - 1; i++) { /* Push the parameter to the top of the stack. */ sq_push(vm, i + 3); if (SQ_FAILED(this->_SetParam(i, vm))) { this->~ScriptText(); throw sq_throwerror(vm, _SC("Invalid parameter")); } /* Pop the parameter again. */ sq_pop(vm, 1); } }
static SQRESULT sq_slave_vm_call(HSQUIRRELVM v) { SQ_FUNC_VARS_NO_TOP(v); GET_sq_slave_vm_INSTANCE(v, 1); SQ_GET_BOOL(v, 2, has_ret_val); SQ_GET_STRING(v, 3, func_name); SQInteger top = sq_gettop(self); SQRESULT result = SQ_ERROR; sq_pushroottable(self); sq_pushstring(self, func_name, func_name_size); if(sq_get(self, -2) == SQ_OK) { switch(sq_gettype(self, -1)) { case OT_CLOSURE: case OT_NATIVECLOSURE: case OT_CLASS: { sq_pushroottable(self); int argc = sq_gettop(v) - 3; if(argc && copy_values_between_vms(self, v, argc, 4) != SQ_OK) { sq_throwerror(v, sq_getlasterror_str(self)); goto cleanup; } if(sq_call(self, argc+1, has_ret_val, SQFalse) != SQ_OK) { sq_throwerror(v, sq_getlasterror_str(self)); goto cleanup; } if(has_ret_val) { if(copy_values_between_vms(v, self, 1, sq_gettop(self)) == SQ_OK) result = 1; } else result = SQ_OK; } } } else { sq_throwerror(v, sq_getlasterror_str(self)); } cleanup: sq_settop(self, top); return result; }
// コンストラクタ Thread::Thread(HSQUIRRELVM v) : Object(v), _currentTick(0), _fileHandler(NULL), _waitTimeout(-1), _status(THREAD_NONE) { _waitList.initArray(); // 実行 if (sq_gettop(v) >= 3) { _exec(v,3); _entryThread(v); } }
TransportPeopleCommand::TransportPeopleCommand(HSQUIRRELVM v, Entity &e){ int argc = sq_gettop(v); if(argc < 3) throw SQFArgumentError(); SQInteger i; if(SQ_FAILED(sq_getinteger(v, 3, &i))) throw SQFArgumentError(); people = i; }
SQInteger with_assert_sq_getfloat(HSQUIRRELVM v,SQInteger nargs,float & val){ SQInteger st_nargs = sq_gettop(v); if(st_nargs < nargs) return sq_throwerror(v, "too many args"); if(sq_gettype(v,nargs) != OT_FLOAT) return sq_throwerror(v, "param is not float"); sq_getfloat(v,nargs,&val); return 0; }
SquirrelObject RegisterClassType(HSQUIRRELVM v,const SQChar * scriptClassName,SQUserPointer classType,SQFUNCTION constructor) { int top = sq_gettop(v); SquirrelObject newClass; if (CreateClass(v,newClass,classType,scriptClassName)) { SquirrelVM::CreateFunction(newClass,constructor,_T("constructor")); } // if sq_settop(v,top); return newClass; } // RegisterClassType
bool CScripts::onPlayerText(int playerId, const char *text) { bool ret = true; for(int i = 0; i < MAX_SCRIPTS; i++) { if(m_pScripts[i]) { // get the script vm pointer SQVM * pVM = m_pScripts[i]->GetVM(); // Get the stack top int iTop = sq_gettop(pVM); // Push the root table onto the stack sq_pushroottable(pVM); // Push the function name onto the stack sq_pushstring(pVM, "onPlayerText", -1); // Get the closure for the function if(SQ_SUCCEEDED(sq_get(pVM, -2))) { // Push the root table onto the stack sq_pushroottable(pVM); // Push the player id onto the stack sq_pushinteger(pVM, playerId); // Push the text onto the stack sq_pushstring(pVM, text, -1); // Call the function // Call the function if (!SQ_FAILED(sq_call(pVM, 3, true, true))) { SQBool result; sq_getbool(pVM, sq_gettop(pVM), &result); if(result == false) ret = false; } } // Restore the stack top sq_settop(pVM, iTop); } } return ret; }
bool CScripts::onRconCommand(const char *command, const char *arg) { bool found = false; for(int i = 0; i < MAX_SCRIPTS; i++) { if(m_pScripts[i]) { // get the script vm pointer SQVM * pVM = m_pScripts[i]->GetVM(); // Get the stack top int iTop = sq_gettop(pVM); // Push the root table onto the stack sq_pushroottable(pVM); // Push the function name onto the stack sq_pushstring(pVM, "onRconCommand", -1); // Get the closure for the function if(SQ_SUCCEEDED(sq_get(pVM, -2))) { // Push the root table onto the stack sq_pushroottable(pVM); // Push the command onto the stack sq_pushstring(pVM, command, -1); // Push the command arguments onto the stack sq_pushstring(pVM, arg, -1); // Call the function if (!SQ_FAILED(sq_call(pVM, 3, true, true))) { SQBool result; sq_getbool(pVM, sq_gettop(pVM), &result); if(result) found = true; } } // Restore the stack top sq_settop(pVM, iTop); } } return found; }
// ------------------------------------------------------------------------------------------------ SQInteger Signal::SqEmit(HSQUIRRELVM vm) { const Int32 top = sq_gettop(vm); // The signal instance Signal * signal = nullptr; // Attempt to extract the signal instance try { signal = Var< Signal * >(vm, 1).value; } catch (const Sqrat::Exception & e) { return sq_throwerror(vm, e.what()); } // Do we have a valid signal instance? if (!signal) { return sq_throwerror(vm, "Invalid signal instance"); } // Walk down the chain and trigger slots for (Slot * node = signal->m_Head, * next = nullptr; node != nullptr; node = next) { // Grab the next node upfront next = node->mNext; // Remember the current stack size const StackGuard sg(vm); // Push the callback object sq_pushobject(vm, node->mFuncRef); // Is there an explicit environment? if (sq_isnull(node->mEnvRef)) { sq_pushroottable(vm); } else { sq_pushobject(vm, node->mEnvRef); } // Are there any parameters to forward? if (top > 1) { for (SQInteger i = 2; i <= top; ++i) { sq_push(vm, i); } } // Make the function call and store the result const SQRESULT res = sq_call(vm, top, false, ErrorHandling::IsEnabled()); // Validate the result if (SQ_FAILED(res)) { return res; // Propagate the error } } // Specify that we don't return anything return 0; }
// void VwSetWindowPos(HWND hWnd, HWND hWndlnsertAfter,int X, int Y, int cx, int cy, UNIT Flags) SQInteger VwSetWindowPos(HSQUIRRELVM v) { SQInteger uIDEvent = 0; SQInteger nargs = sq_gettop(v); SQInteger nWnd = 0; SQInteger nWndlnsertAfter = NULL; HWND hWnd = NULL; HWND hWndlnsertAfter = NULL; int X = 0; int Y = 0; int cx = 0; int cy = 0; UINT uFlags = 0; if (!v || 7 + 1 != nargs) {goto _Exit_;} if (OT_INTEGER != sq_gettype(v, 2)) {goto _Exit_;} if (OT_INTEGER != sq_gettype(v, 3)) {goto _Exit_;} if (OT_INTEGER != sq_gettype(v, 4) && OT_FLOAT != sq_gettype(v, 4)) {goto _Exit_;} if (OT_INTEGER != sq_gettype(v, 5) && OT_FLOAT != sq_gettype(v, 5)) {goto _Exit_;} if (OT_INTEGER != sq_gettype(v, 6) && OT_FLOAT != sq_gettype(v, 6)) {goto _Exit_;} if (OT_INTEGER != sq_gettype(v, 7) && OT_FLOAT != sq_gettype(v, 7)) {goto _Exit_;} if (OT_INTEGER != sq_gettype(v, 8)) {goto _Exit_;} sq_getinteger(v, 2, &nWnd); sq_getinteger(v, 3, &nWndlnsertAfter); sq_getinteger(v, 4, &X); sq_getinteger(v, 5, &Y); sq_getinteger(v, 6, &cx); sq_getinteger(v, 7, &cy); sq_getinteger(v, 8, (SQInteger*)&uFlags); if (-1 == nWnd) { CScriptMgr* pMgr = (CScriptMgr*)sq_getforeignptr(v); if (!pMgr) {goto _Exit_;} CPaintManagerUI* pPM = pMgr->GetManager(); if (!pPM) {goto _Exit_;} hWnd = pPM->GetPaintWindow(); } else { hWnd = QiHwHandleToWin(nWnd)->pWinObj->GetHWND(); } if (HWND_TOP == (HWND)nWndlnsertAfter || HWND_BOTTOM == (HWND)nWndlnsertAfter || HWND_TOPMOST == (HWND)nWndlnsertAfter || HWND_TOPMOST == (HWND)nWndlnsertAfter) { hWndlnsertAfter = (HWND)nWndlnsertAfter; } else { hWndlnsertAfter = QiHwHandleToWin(nWndlnsertAfter)->pWinObj->GetHWND(); } ::SetWindowPos(hWnd, hWndlnsertAfter, X, Y, cx, cy, uFlags); //::MoveWindow( hWnd, X, Y, cx, cy, TRUE); _Exit_: return 0; }
int CEventNatives::Call(SQVM* pVM) { // we need enough parameters if(sq_gettop(pVM)<3) { sq_pushbool(pVM, false); return 1; } // get the event name const char* szEventName; if(SQ_FAILED(sq_getstring(pVM, 2, &szEventName))) { sq_pushbool(pVM, false); return 1; } // get the entity which it should effect CEntity* pEntity = sq_toentity(pVM, 3); // if we have a valid entity, use it if(pEntity) { // collect the parameters CSquirrelArguments* pArguments = new CSquirrelArguments(); for(int i = 4; i <= sq_gettop(pVM); ++ i) { // push all arguments from the stack if(!pArguments->pushFromStack(pVM, i)) { sq_pushbool(pVM, false); delete pArguments; return 1; } } sq_pushbool(pVM, pEntity->CallEvent(szEventName, pArguments)); delete pArguments; } else sq_pushbool(pVM, false); return 1; }
void debugStackDump(HSQUIRRELVM v) { SN_LOG("Squirrel stack dump:"); for (SQInteger n = 1; n <= sq_gettop(v); ++n) { std::string s = "[" + std::to_string(n) + "] "; s += squirrel::getTypeName(sq_gettype(v, n)); SN_MORE(s); } }
bool sc_OnPlayerConnect(HSQUIRRELVM v, const short index, const wchar_t *name) { int result = 1; int top = sq_gettop(v); sq_pushroottable(v); sq_pushstring(v, _SC("OnPlayerConnect"), -1); if (SQ_SUCCEEDED(sq_get(v, -2))) { sq_pushroottable(v); sq_pushinteger(v, index); sq_pushstring(v, name, -1); if (SQ_FAILED(sq_call(v, 3, 1, 1))) { return true; } sq_getinteger(v, sq_gettop(v), &result); } sq_settop(v, top); return result; }
// Font _MEMBER_FUNCTION_IMPL(GUIFont, constructor) { if(sq_gettop(pVM) < 2) { sq_pushbool(pVM, false); return 1; } const char * szFont; int iSize = 8; bool bScaled = false; sq_getstring(pVM, 2, &szFont); if(sq_gettop(pVM) >= 3) { sq_getinteger(pVM, 3, &iSize); if(iSize < 3 || iSize > 100) { sq_pushbool(pVM, false); return 1; } if(sq_gettop(pVM) >= 4) { SQBool sqb; sq_getbool(pVM, 4, &sqb); bScaled = sqb != 0; } } CEGUI::Font * pFont = g_pClient->GetGUI()->GetFont(szFont, iSize, bScaled); if(!pFont || SQ_FAILED(sq_setinstance(pVM, pFont))) { CLogFile::Printf("Can't create GUIFont."); sq_pushbool(pVM, false); return 1; } sq_pushbool(pVM, true); return 1; }
SQInteger print_prova(HSQUIRRELVM v) { SQInteger nargs = sq_gettop(v); SQChar* str; sq_getstring(v, -1, &str); SQPRINTFUNCTION pf = sq_getprintfunc(v); pf(v, str); return 0; }
// variant fscan(handle file[, integer type = TEXT]) // Read data from file // For list of supported types look 'scan' function specification // If invalid file handle or unsupported type specified, read nothing and return 'null' value static SQInteger FScan (HSQUIRRELVM) { FILE* file = GetFile(); if (file) { SQInteger type = 't'; if (sq_gettop(sqvm) == 3) sq_getinteger(sqvm, 3, &type); SQInteger intVal; switch (type) { case 't': SQChar buf[4096]; fgets(buf, sizeof(buf), file); buf[sizeof(buf) - 1] = 0; for (SQChar* c = buf; *c; ++c) { if (*c == '\n') { *c = 0; break; } } sq_pushstring(sqvm, buf, -1); break; case 'c': sq_pushinteger(sqvm, fgetc(file)); break; case 'i': fscanf_s(file, FMT_INT, &intVal); sq_pushinteger(sqvm, intVal); break; case 'u': fscanf_s(file, FMT_UINT, &intVal); sq_pushinteger(sqvm, intVal); break; case 'f': SQFloat fltVal; fscanf_s(file, "%f", &fltVal); sq_pushfloat(sqvm, fltVal); break; default: sq_pushnull(sqvm); } } else sq_pushnull(sqvm); return 1; }
SQInteger Sq_Interpolate(HSQUIRRELVM v) { const SQChar *Str, *WordString; char Out[4096]; sq_getstring(v, 2, &Str); sq_getstring(v, 3, &WordString); char *Poke = Out; const char *Peek = Str; char WordBuff[strlen(Str)+1]; const char *Word[32]; const char *WordEol[32]; XChatTokenize(WordString, WordBuff, Word, WordEol, 32, TOKENIZE_MULTI_WORD); while(*Peek) { int Span = strcspn(Peek, "%&"); memcpy(Poke, Peek, Span); Poke += Span; Peek += Span; if(*Peek == '%' || *Peek == '&') { char Extra = Peek[1]; if(Extra == '%') *(Poke++) = '%'; else if(Extra == '&') *(Poke++) = '&'; else { if(isdigit(Extra)) { int WhichWord = Extra - '1'; strcpy(Poke, (*Peek=='%')?Word[WhichWord]:WordEol[WhichWord]); Poke = strrchr(Poke, 0); } else { // look in the list of extra words int top = sq_gettop(v);; sq_pushnull(v); //null iterator while(SQ_SUCCEEDED(sq_next(v,-2))) { const SQChar *ThisWord; sq_getstring(v, -1, &ThisWord); if(ThisWord[0]==Extra) { strcpy(Poke, ThisWord+1); Poke = strrchr(Poke, 0); break; } sq_pop(v,2); } sq_pop(v,1); //pops the null iterator sq_settop(v, top); } } Peek+= 2; } } *Poke = 0; sq_pushstring(v, Out, -1); return 1; }
void sc_OnGameModeInit(HSQUIRRELVM v) { int top = sq_gettop(v); sq_pushroottable(v); sq_pushstring(v, _SC("OnGameModeInit"), -1); if (SQ_SUCCEEDED(sq_get(v, -2))) { sq_pushroottable(v); sq_call(v, 1, 0, 1); } sq_settop(v, top); }
/** * 実行終了 */ SQRESULT Thread::exit(HSQUIRRELVM v) { if (sq_gettop(v) >= 2) { _exitCode.getStack(v,2); } else { _exitCode.clear(); } _exit(); return SQ_OK; }
/* * Returns a integer value with a positive sign, * greater than or equal to 0.0 and less than max. */ SQInteger emoRuntimeNativeRandom(HSQUIRRELVM v) { SQInteger nargs = sq_gettop(v); SQInteger max = RAND_MAX; if (nargs <= 2 && sq_gettype(v, 2) == OT_INTEGER) { sq_getinteger(v, 2, &max); } sq_pushinteger(v, engine->javaGlue->callInt_Int("random", max)); return 1; }
/* * Call Squirrel function with one float parameter * Returns SQTrue if sq_call succeeds. */ SQBool callSqFunction_Bool_Float(HSQUIRRELVM v, const SQChar* nname, const SQChar* name, SQFloat value, SQBool defaultValue) { SQBool result = SQFalse; SQInteger top = sq_gettop(v); sq_pushroottable(v); sq_pushstring(v, nname, -1); if (SQ_SUCCEEDED(sq_get(v, -2))) { sq_pushstring(v, name, -1); if(SQ_SUCCEEDED(sq_get(v, -2))) { sq_pushroottable(v); sq_pushfloat(v, value); if (SQ_SUCCEEDED(sq_call(v, 2, SQTrue, SQTrue))) { sq_getbool(v, sq_gettop(v), &result); } } } sq_settop(v,top); return result; }
/* * register sensors function called from script * * @param sensors to be registered */ SQInteger emoRegisterSensors(HSQUIRRELVM v) { SQInteger nargs = sq_gettop(v); for (SQInteger n = 1; n <= nargs; n++) { if (sq_gettype(v, n) == OT_INTEGER) { SQInteger value; sq_getinteger(v, n, &value); engine->createSensors(value); } } return 0; }