📄 luastate.cpp
字号:
//--------------------------------------------------------------------------------// LuaState.cpp//--------------------------------------------------------------------------------#include "LuaState.h"#include <time.h>#include <stdlib.h>#include <stdio.h>//--------------------------------------------------------------------------------// constructor / destructor//--------------------------------------------------------------------------------LuaState::LuaState(): m_pState(NULL){}LuaState::~LuaState(){ release();}//--------------------------------------------------------------------------------// init//--------------------------------------------------------------------------------voidLuaState::init(int stackSize){ open(stackSize); // 葛电 lib甫 load茄促.. baselibopen(); mathlibopen(); strlibopen(); iolibopen(); randomseed();}//--------------------------------------------------------------------------------// release//--------------------------------------------------------------------------------voidLuaState::release(){ if (m_pState!=NULL) { lua_close(m_pState); } m_pState = NULL;}//--------------------------------------------------------------------------------// dofile//--------------------------------------------------------------------------------intLuaState::dofile(const string& filename) throw (Error){ __BEGIN_TRY return lua_dofile(m_pState, filename.c_str()); __END_CATCH}//--------------------------------------------------------------------------------// randomseed//--------------------------------------------------------------------------------voidLuaState::randomseed(){ char str[80]; srand((unsigned int)time(NULL)); sprintf(str,"randomseed(%d)", rand()%10000); lua_dostring(m_pState, str);}//--------------------------------------------------------------------------------// open//--------------------------------------------------------------------------------voidLuaState::open(int stackSize){ release(); m_pState = lua_open(stackSize);}//--------------------------------------------------------------------------------// close//--------------------------------------------------------------------------------voidLuaState::close(){ if (m_pState!=NULL) { lua_close(m_pState); }}//--------------------------------------------------------------------------------// baselibopen//--------------------------------------------------------------------------------voidLuaState::baselibopen(){ lua_baselibopen(m_pState);}//--------------------------------------------------------------------------------// mathlibopen//--------------------------------------------------------------------------------voidLuaState::mathlibopen(){ lua_mathlibopen(m_pState);}//--------------------------------------------------------------------------------// strlibopen//--------------------------------------------------------------------------------voidLuaState::strlibopen(){ lua_strlibopen(m_pState);}//--------------------------------------------------------------------------------// iolibopen//--------------------------------------------------------------------------------voidLuaState::iolibopen(){ lua_iolibopen(m_pState);}//--------------------------------------------------------------------------------// getError to String//--------------------------------------------------------------------------------const string& LuaState::getErrorToString(int result){ switch (result) { case LUA_ERRRUN : { static string e = "error while running the chunk"; return e; } break; case LUA_ERRSYNTAX : { static string e = "syntax error during pre-compilation"; return e; } break; case LUA_ERRMEM : { static string e = "memory allocation error"; return e; } break; case LUA_ERRERR : { static string e = "error while running _ERRORMESSAGE."; return e; } break; case LUA_ERRFILE : { static string e = "error opening the file."; return e; } break; } static string e = "unknown error"; return e;}//--------------------------------------------------------------------------------// log Error//--------------------------------------------------------------------------------voidLuaState::logError(int result){ if (isError(result)) { filelog("luaError.log", "%s", getErrorToString(result).c_str()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -