c读取lua

<span style="font-size:18px;">
#include <string.h>
#include <iostream>

extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}

int main()
{
    int windows_width = 0;
    int windows_height = 0;

    lua_State * L = luaL_newstate();
    luaL_openlibs(L);

    //load lua file 加载lua文件
    int bRet = luaL_loadfile(L, "cc.lua");

    if (bRet)
    {
        return false;
    }

    // run lua file 运行lua文件
    bRet = lua_pcall(L, 0, 0, 0);
    if (bRet)
    {
        return false;
    }

    // lua--> stack
    lua_getglobal(L, "width");
    lua_getglobal(L, "height");

    //type check 类型检测
    int a = lua_isnumber(L, -2);
    if (!a)
    {
        return false;
    }

    int b = lua_isnumber(L, -1);
    if(!b)
    {
        return false;
    }

    //stack-->c
    windows_height = lua_tointeger(L, -1);
    windows_width = lua_tointeger(L, -2);

    printf("width = %d, height = %d", windows_width, windows_height);

    lua_close(L);
    return 0;
}
</span>


你可能感兴趣的:(c读取lua)