bool CSquirrel::Load(String strName, String strPath) { // Check if the script exists if(!SharedUtility::Exists(strPath.Get())) return false; // Set the script name m_strName = strName; // Set the script path m_strPath = strPath; // Create a squirrel VM with an initial stack size of 1024 bytes (stack will resize as needed) m_pVM = sq_open(1024); // Register the default error handlers sqstd_seterrorhandlers(m_pVM); // Set the print function and error function sq_setprintfunc(m_pVM, PrintFunction, PrintFunction); // Set the compiler error function sq_setcompilererrorhandler(m_pVM, CompilerErrorFunction); // Set our error handler sq_newclosure(m_pVM, PrintErrorFunction, 0); sq_seterrorhandler(m_pVM); // Push the root table onto the stack sq_pushroottable(m_pVM); #ifdef _SERVER // Register the input/output library sqstd_register_iolib(m_pVM); #endif // Register the blob library sqstd_register_bloblib(m_pVM); // Register the math library sqstd_register_mathlib(m_pVM); // Register the string library sqstd_register_stringlib(m_pVM); return true; }
void sqstd_seterrorhandlers(HSQUIRRELVM v) { sq_setcompilererrorhandler(v,_sqstd_compiler_error); sq_newclosure(v,_sqstd_aux_printerror,0); sq_seterrorhandler(v); // additional functions sq_pushroottable(v); sq_pushstring(v, _SC("printCallStack"), -1); sq_newclosure(v, printCallStack, 0); sq_createslot(v, -3); sq_pushstring(v, _SC("notifyAllExceptions"), -1); sq_newclosure(v, notifyAllExceptions, 0); sq_setparamscheck(v, 2, _SC(".n|b")); sq_createslot(v, -3); sq_pop(v,1); }
int main() { HSQUIRRELVM vm = sq_open(1024); sq_setprintfunc(vm, PrintFunc, PrintFunc); sq_setcompilererrorhandler(vm, CompileErrorHandler); //FILE* fp = fopen("../../../test/test.nut", "r"); //if(!fp) //{ // goto _CLEANUP; //} //fseek(fp, 0, SEEK_END); //long len = ftell(fp); //fseek(fp, 0, SEEK_SET); //char* buf = new char[len + 1]; //memset(buf, 0, len + 1); //fread(buf, 1, len, fp); //fclose(fp); //SQChar* source = new SQChar[len + 2]; //memset(source, 0, sizeof(SQChar) * (len + 2)); //mbstowcs(source, buf, len + 1); //delete[] buf; //if(SQ_FAILED(sq_compilebuffer(vm, source, _tcslen(source), _SC("test.nut"), SQTrue))) //{ // delete[] source; // goto _CLEANUP; //} //delete[] source; SQChar* apSrc[] = { _SC("../../../test/test.nut"), }; sq_compilestatic(vm, OpenSrcFunc, CloseSrcFunc, 1, apSrc, _SC("testproj"), SQTrue); sq_pushroottable(vm); if(SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) { printf("Run script succeeded."); } _CLEANUP: sq_close(vm); system("pause"); return 0; }
void Context::initialize() { sq_setforeignptr(vm_, this); sq_setprintfunc(vm_, Detail::print, Detail::printError); sq_pushroottable(vm_); sqstd_register_mathlib(vm_); sqstd_register_stringlib(vm_); sqstd_register_bloblib(vm_); sq_setcompilererrorhandler(vm_, Detail::handleCompilerError); sq_newclosure(vm_, Detail::handleError, 0); sq_seterrorhandler(vm_); sq_pop(vm_, 1); Table::create(*this, _SC("__sqrew_classes"), TableDomain::Registry); }
Squirrel::Squirrel() { this->vm = sq_open(1024); this->print_func = NULL; this->global_pointer = NULL; this->crashed = false; /* Handle compile-errors ourself, so we can display it nicely */ sq_setcompilererrorhandler(this->vm, &Squirrel::CompileError); sq_notifyallexceptions(this->vm, SQTrue); /* Set a good print-function */ sq_setprintfunc(this->vm, &Squirrel::PrintFunc); /* Handle runtime-errors ourself, so we can display it nicely */ sq_newclosure(this->vm, &Squirrel::_RunError, 0); sq_seterrorhandler(this->vm); /* Set the foreigh pointer, so we can always find this instance from within the VM */ sq_setforeignptr(this->vm, this); sq_pushroottable(this->vm); squirrel_register_global_std(this); }
CResourceScript::CResourceScript ( CString strFile, int ScriptType, CResource *pResource ) { // Warp some varibles. m_pResource = pResource; m_iScriptType = ScriptType; // Format script localization. m_strFile.Format("%s\\%s\\%s", m_pResource->GetDirectory().Get(), m_pResource->GetName().Get(), strFile.Get()); // Check if file exists. if(!Exists(m_strFile.Get())) { CLog::Printf( "[Resources] Script %s not found!", m_strFile.Get() ); return; } // Check script type - default server. if ( ScriptType == SCRIPT_CLIENT ) { /* TODO: - Warp to code WebServ, - Create class for client resources, - .. */ } else { // Create root stack for script. m_pVM = sq_open(1024); //Register error and print functions. sqstd_seterrorhandlers(m_pVM); sq_setprintfunc(m_pVM, PrintFunction, ErrorFunction); sq_setcompilererrorhandler(m_pVM, CompilerErrorFunction); // Push root vm table. sq_pushroottable(m_pVM); // Register basic systems. sqstd_register_systemlib(m_pVM); sqstd_register_iolib(m_pVM); sqstd_register_bloblib(m_pVM); sqstd_register_mathlib(m_pVM); sqstd_register_stringlib(m_pVM); // Register Squirrel Classes m_pResource->RegisterClass(this); // Register all functions. // Event functions. CEventNatives::Register(this); // Funkcje gracza. CPlayerNatives::Register(this); if ( SQ_FAILED ( sqstd_dofile(m_pVM, m_strFile.Get(), SQFalse, SQTrue) ) ) { // cannot compile script file. return; } // Define basic varibles. CSquirrelArguments pArgs; pArgs.push(MAX_PLAYERS); RegisterVarible ( "MAX_PLAYERS", &pArgs ); pArgs.clear(); } }
void sqstd_seterrorhandlers(HSQUIRRELVM v) { sq_setcompilererrorhandler(v,_sqstd_compiler_error); sq_newclosure(v,_sqstd_aux_printerror,0); sq_seterrorhandler(v); }