lua_State和luaL_newstate,luaL_openlibs及lua_close
来源:互联网 发布:粘土淘宝 编辑:程序博客网 时间:2024/04/30 04:15
1,lua_State在Lua中的定义如下:
struct lua_State {
CommonHeader;
lu_byte status;
StkId top; /* first free slot in the stack */
global_State *l_G;
CallInfo *ci; /* call info for current function */
const Instruction *oldpc; /* last pc traced */
StkId stack_last; /* last free slot in the stack */
StkId stack; /* stack base */
int stacksize;
unsigned short nny; /* number of non-yieldable calls in stack */
unsigned short nCcalls; /* number of nested C calls */
lu_byte hookmask;
lu_byte allowhook;
int basehookcount;
int hookcount;
lua_Hook hook;
GCObject *openupval; /* list of open upvalues in this stack */
GCObject *gclist;
struct lua_longjmp *errorJmp; /* current error recover point */
ptrdiff_t errfunc; /* current error handling function (stack index) */
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
};
2,lua_State的生成
不知道是不是老的版本,我在网上看到有网友的例子里这样写:
lua_State *L = lua_open(); /* opens Lua */
我下了Lua5.2编译后执行例子是不行的,找了lua源码也没有找到lua_open()的定义。暂不论它,现在生成lua_State对像的方式应如下:
lua_State *L = luaL_newstate();
luaL_newstate在源码中的定义如下:
LUALIB_API lua_State *luaL_newstate (void) {
lua_State *L = lua_newstate(l_alloc, NULL);
if (L) lua_atpanic(L, &panic);
return L;
}
3,luaL_openlibs在源码中的定义:
LUALIB_API void luaL_openlibs (lua_State *L) {
const luaL_Reg *lib;
/* call open functions from 'loadedlibs' and set results to global table */
for (lib = loadedlibs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1); /* remove lib */
}
/* add open functions from 'preloadedlibs' into 'package.preload' table */
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
for (lib = preloadedlibs; lib->func; lib++) {
lua_pushcfunction(L, lib->func);
lua_setfield(L, -2, lib->name);
}
lua_pop(L, 1); /* remove _PRELOAD table */
}
官方文档说明:Opens all standard Lua libraries into the given state.
很简单,把所有标准类库加载到指定的虚拟机。疑问:有没有方法指定一个标准库到指定的虚拟机呢?
4,意义
从网上找到以下一段资料:“Lua脚本的编译执行是相互独立的,在不同的线程上执行。通过luaL_newstate()函数可以申请一个虚拟机,返回指针类型 lua_State。今后其他所有Lua Api函数的调用都需要此指针作为第一参数,用来指定某个虚拟机。”
中文大家都认识,不多说了,肤浅的认识是lua_State代表一个lua虚拟机对像,luaL_newstate()分配一个虚拟机。lua类库管理着所有的虚拟机。
5,lua_close
代码:
LUA_API void lua_close (lua_State *L) {
L = G(L)->mainthread; /* only the main thread can be closed */
lua_lock(L);
luai_userstateclose(L);
close_state(L);
}
文档说明:
Destroys all objects in the given Lua state (calling the corresponding garbage-collection metamethods, if any) and frees all dynamic memory used by this state. On several platforms, you may not need to call this function, because all resources are naturally released when the host program ends. On the other hand, long-running programs, such as a daemon or a web server, might need to release states as soon as they are not needed, to avoid growing too large.
销毁指定虚拟机的所有对像(如果有垃圾回收相关的无方法则会调用该方法)并收回所有由该虚拟机动态分配产生的内存,在有些平台下我们不需要调用此函数,因为当主程序退出时,资源会被自然的释放掉,但是但一个长时间运行的程序,比如后台运行的web服务器,需要立即回收虚拟机资源以避免内存过高占用。
http://blog.csdn.net/ym012/article/details/7186026
- lua_State和luaL_newstate,luaL_openlibs及lua_close
- lua_State和luaL_newstate,luaL_openlibs及lua_close
- lua_State和luaL_newstate,luaL_openlibs及lua_close
- lua_state
- 详解luaL_newstate()
- get c_function* from lua_State*
- lua源码阅读(5)-lua_State
- lua5.3源码基础阅读(luaL_openlibs)
- luaL_optinteger (lua_State *L, int narg, lua_Integer def)
- lua 源码分析之线程对象lua_State
- Lua数据结构 — lua_State(六)
- Lua数据结构 — lua_State(六)
- Undefined symbols for architecture i386: "register_all_MyClass(lua_State*)"
- eclipse Error处理:Invalid arguments ' Candidates are: void luaL_openlibs(*)'
- codeblocks调用lua“undefined reference to `luaL_newstate()”错误
- register_spine_module(lua_State*) in libluacocos2d iOS.a(lua_cocos2dx_spine_manual.o)
- Lua 5.3 源码分析(四)线程 lua_State
- 关于c调用lua 对‘luaL_newstate()’未定义的引用的问题解决办法
- 扔掉鼠标_.纯键盘操作
- HDU 4568 Hunter ( TSP + 状态压缩 )
- ubuntu设置
- Linx卷管理详解
- [leetcode]Swap Nodes in Pairs Reverse Nodes in k-Group
- lua_State和luaL_newstate,luaL_openlibs及lua_close
- UIKit 力学教程
- hasMoreElements的用法
- 微信操作整理
- HNOI 2014 Day1 米特运输
- Coreseek:区段查询及增量索引代替实时索引
- 利用扩展事件和警报实时监控死锁-命令方式
- Arrays.copyOf()与System.arraycopy()的源码分析
- Java Observable 模式