1. 程式人生 > >用C語言擴充套件lua模組(入門)

用C語言擴充套件lua模組(入門)

複製程式碼
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
static void encode_sha(const char* src, char* des)
{
    /*
     *sha-hash  //hash演算法
     */
}
static int l_sha1(lua_State* lua)
{
    const char *src = NULL;
    char des[40] = {0};
    src    = luaL_checkstring(lua, 1);    //出棧獲取源字串
encode_sha(src, des); //something lua_pushstring(lua, des); //壓棧返回給lua return 1; //告訴lua返回了一個變數 } //對映表,"sha1"為lua中的函式名,l_sha1為真正C中的函式地址 static const struct luaL_Reg encode[] = { {"sha1", l_sha1}, {NULL, NULL}, }; //模組註冊函式 int luaopen_libencode(lua_State* lua) {
//註冊本模組中所有的功能函式,libencode為模組名,encode陣列儲存所有函式的對映關係 luaL_register(lua, "libencode", encode); return 1; }
複製程式碼