The general pattern of writing lua module with C C + +

As an embedded language, lua provides a complete C API to help lua interact with the host program. So it's easy to write modules for lua using C or C + +.

Take a look at the encapsulation source lstrlib.c of string module in lua (5.1)

At the end of lstrlib.c


/**
    Record the function name and the actual calling function in a struct and form an array
    --- It's a bit of a lua code catch-up---
**/
static const luaL_Reg strlib[] = {
  {"byte", str_byte},
  {"char", str_char},
  {"dump", str_dump},
  {"find", str_find},
  {"format", str_format},
  {"gmatch", gmatch},
  {"gsub", str_gsub},
  {"len", str_len},
  {"lower", str_lower},
  {"match", str_match},
  {"rep", str_rep},
  {"reverse", str_reverse},
  {"sub", str_sub},
  {"upper", str_upper},
  {"pack", str_pack},
  {"packsize", str_packsize},
  {"unpack", str_unpack},
  {NULL, NULL}
};

/**
    Here is the subsequent processing of the created lib meta table. See the source code for details
**/
static void createmetatable (lua_State *L) {
    ....
}


/*
** Open string library
** This is the main entry of string Library
*/
LUAMOD_API int luaopen_string (lua_State *L) {
  luaL_newlib(L, strlib); // Create a lib meta table, and put the function names and functions in lual [register (L, "modulename", modulename); before strlib 5.2 into the table
  createmetatable(L); // Handle the created lib metatable again
  return 1;
}

Similar codes appear, such as loadlib.c (package module) and loslib.c(os module)

Therefore, we can summarize the way to write lua module

  • 1. Defining functions in C/C + +
  • 2. Define the lual reg array
  • 3. Create a lib metatable. It should be noted that in the version after 5.2, lual ﹣ newlib can be used directly, while in the previous version, lual ﹣ register (L, "modulename", modulename) is required;

C/C + + and lua types

Passing data into lua interface

/**
    Press NULL into stack
**/
LUA_API void  (lua_pushnil) (lua_State *L);

/**
    Press double into stack
**/
LUA_API void  (lua_pushnumber) (lua_State *L, lua_Number n);

/**
    Stack
**/
LUA_API void  (lua_pushinteger) (lua_State *L, lua_Integer n);

/**
    Push the specified length len of string s into the stack
**/
LUA_API void  (lua_pushlstring) (lua_State *L, const char *s, size_t l);

/**
    Press a string into the stack
**/
LUA_API void  (lua_pushstring) (lua_State *L, const char *s);

/**
    Press a string into the stack and format it
**/
LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
                                                      va_list argp);
/**
    Press a string into the stack and format it
    Format with variable parameters
**/
LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);

/**
    Pressing a C function into the stack
**/
LUA_API void  (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);

/**
    Push b as a Boolean value into the stack
**/
LUA_API void  (lua_pushboolean) (lua_State *L, int b);

/**
    Push a lightweight user data into the stack
**/
LUA_API void  (lua_pushlightuserdata) (lua_State *L, void *p);

/**
  "L"Represents the thread in the stack. If this thread is the main thread of the current state machine, return 1.
**/
LUA_API int   (lua_pushthread) (lua_State *L);

Added by ziggy3000 on Sat, 25 Apr 2020 17:29:01 +0300