【发布时间】:2018-03-13 00:42:36
【问题描述】:
(Lua 5.2)
我正在编写从 ncurses 到 Lua 的绑定,并且我想包含一些函数以外的值。我目前正在绑定这样的函数:
#define VERSION "0.1.0"
// Method implementation
static int example(lua_State* L){
return 0;
}
// Register library using this array
static const luaL_Reg examplelib[] = {
{"example", example},
{NULL, NULL}
}
// Piece it all together
LUALIB_API int luaopen_libexample(lua_State* L){
luaL_newlib(L, examplelib);
lua_pushstring(L, VERSION);
// Set global version string
lua_setglobal(L, "_EXAMPLE_VERSION");
return 1;
}
这会生成一个包含几个函数(在本例中只有一个)和一个全局字符串值的表,但我想在库中放置一个数字值。例如,现在lib = require("libexample"); 将返回一个带有一个函数example 的表,但我希望它也有一个数字exampleNumber。我将如何做到这一点?
谢谢
【问题讨论】: