Lua C语言API

阅前提示

该篇文章主要介绍 Lua标准库中常见的API
适合人群:lua使用人群
阅读方式:浏览


文章概述

Lua是一种嵌入式可扩展的语言,这也是Lua被运用的两种表现形式。

  • 嵌入式:把Lua作为库。以C语言为例,C语言为主体,拥有控制权,Lua语言被用作库,这种交互形式中的C代码被称为应用代码
  • 可扩展:Lua语言拥有控制权。此时,C语言被当作库,这时候称C语言为代码库

Lua标准库中,头文件lua.h声明了Lua提供的基础函数,头文件lauxlib.h声明了辅助库所提供的函数。辅助库使用Lua.h提供的基础API来提供更高层次的抽象。辅助库不能访问Lua的内部元素,只能通过lua.h中声明的官方基础API完成所有工作。
源码地址


API

lua_State *L = luaL_newState(); /* 打开lua (创建一个用来保存状态的结构体) */
luaL_openlibs(L); /* 打开标准库 */
...
lua_close(L); /* 关闭lua */

Lua将所用的状态都保存在动态的结构体lua_State中,Lua中的所用函数都接收一个指向该结构的指针作为参数

也就是说,lua的开始需要创建一个lua_State(luaL_newstate),这里没有包含预定的任何函数,所有的标准库都被组织成不同的包(头文件lualib.h中可以看到打开这些库的函数),luaL_openlibs用于打开所用标准库。

struct lua_State {
  CommonHeader;
  unsigned short nci;  /* number of items in 'ci' list */
  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 */
  UpVal *openupval;  /* list of open upvalues in this stack */
  GCObject *gclist;
  struct lua_State *twups;  /* list of threads with open upvalues */
  struct lua_longjmp *errorJmp;  /* current error recover point */
  CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */
  volatile lua_Hook hook;
  ptrdiff_t errfunc;  /* current error handling function (stack index) */
  int stacksize;
  int basehookcount;
  int hookcount;
  unsigned short nny;  /* number of non-yieldable calls in stack */
  unsigned short nCcalls;  /* number of nested C calls */
  l_signalT hookmask;
  lu_byte allowhook;
};

栈:Lua与C进行数据交换的关键

想要从lua中获取一个值时,调用Lua将指定的值压入栈,然后去栈上取得

想要将一个值传给lua时,首先将值压入栈,然后调用lua将其从栈中弹出。

C API 中使用索引来引用栈中元素。第一个被压入栈的元素索引为 1,第二个为 2。也可以用负数,-1 代表栈顶元素索引,-2 则代表栈顶之前的元素索引。

压栈函数:

  • lua_pushnil (lua_State *L) :常量nil
  • lua_pushboolean (lua_State *L,int bool):布尔值
  • lua_pushnumber (lua_State *L,lua_Number n):双精度浮点数
  • lua_pushinteger (lua_State *L,lua_Interger n):整型
  • lua_pushlstring (lua_State *L,const char * s,size_t len):char指针+长度
  • lua_pushstring (lua_State *L,const char * s):以\0终止的字符串

检查栈空间函数:

  • int lua_checkstack (lua_State *L,int sz); sz为需要的额外位置,该函数会增加栈的大小,如果失败则返回0
  • void luaL_checkstack (lua_State *L,int sz,const char *msg); 若空间不足则会抛出异常msg

查询元素函数:

  • 原型:int lua_is * (lua_State *L,int index); lua_isnil、lua_isnumber、lua_isstring…

获取函数:

  • int lua_toboolean (lua_State *L,int index) :获取布尔值
  • lua_State *lua_tothread (lua_State *L,int index):获取环境
  • lua_Number lua_tonumber (lua_State *L,int index):获取双精度浮点数
  • lua_Integer lua_tointeger (lua_State *L,int index):获取整型

其他栈操作

  • int lua_gettop (lua_State *L) :返回栈中元素个数,栈的深度,栈顶元素索引
  • int lua_settop (lua_State *L,int index) :设置栈中元素数量,若之前的数量大于设置值,则丢弃高出的部分,反之则压入nil补足。当index为负数时代表:从栈中弹出 n个元素。
  • int lua_pushvalue (lua_State *L,int index) :用于将指定索引上的元素的副本压入栈。
  • int lua_rotate (lua_State *L,int index,int n) :指定索引元素想栈顶方向转动n个位置,n为负数时方向相反。
  • int lua_remove (lua_State *L,int index) :删除指定索引元素,并将该位置之上元素下移。这里使用了宏:(lua_rotate(L,(idx),-1),lua_pop(L,1))
  • int lua_insert (lua_State *L,int index) :将栈顶元素移动到指定索引位置,依然使用了宏:lua_rotate(L,(idx),1)
  • int lua_replace (lua_State *L,int index) :弹出一个值,并将栈顶设置为指定索引上的值,不移动任何元素。
  • int lua_copy (lua_State *L,int fromidx ,int toidx):将索引上的值复制到另一个索引上,原值不受影响

.
.
.
.
.


嗨,我是作者Vin129,逐儿时之梦正在游戏制作的技术海洋中漂泊。知道的越多,不知道的也越多。希望我的文章对你有所帮助:)


猜你喜欢

转载自blog.csdn.net/qq_28820675/article/details/106797087