天天看點

Lua 中的常用API

luaL_newstate

  • lua_State *luaL_newstate (void);
  • 建立一個新的state

luaL_openlibs

  • void luaL_openlibs (lua_State *L);
  • 打開所有的标準lua庫到指定狀态,也就是把所有标準類庫加載到指定的虛拟機.

luaL_loadfile

  • int luaL_loadfile (lua_State *L, const char *filename);
  • 加載檔案的時候把它當一個lua子產品
  • luaL_dofile和luaL_loadfile的差別
  • LUAL_LOADFILE的坑

lua_pcall

  • int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);
  • nargs:參數個數
  • nresults:傳回值個數
  • errFunc:錯誤處理函數,0表示無,表示錯誤處理函數在棧中的索引
  • 在保護模式下調用一個函數

lua_getglobal

  • void lua_getglobal (lua_State *L, const char *name);
  • 将全局名稱的值壓入堆棧

lua_newtable

  • void lua_newtable (lua_State *L);
  • 建立一個新的table并将其推入堆棧,它等價于lua_createtable(L, 0, 0).

lua_pushliteral

  • const char *lua_pushliteral (lua_State *L, const char *s);
  • 這個宏相當于lua_pushstsring,但隻能在s是一個字元串時使用,它會自動提供字元串的長度.

lua_settable

  • void lua_settable (lua_State *L, int index);
  • 就是把表在lua堆棧中的值彈出來,index 是table 在堆棧中的位置,假如 table 在 -3, 則key 應該是 -2,value 是 -1

    相當于 table[key] = value.

lua_call

  • void lua_call (lua_State *L, int nargs, int nresults);
  • 調用一個函數
  • nargs是你推入堆棧的參數個數. 函數調用時,所有參數和函數值從堆棧彈出.