[Lua]在C函数中保存状态--注册表,环境表,upvalue(二)

2015-11-19 23:07:28 · 作者: · 浏览: 40
} static luaL_Reg myfuncs[] = { {"counter", counter}, {"newCounter", newCounter}, {NULL, NULL} }; extern "C" __declspec(dllexport) int luaopen_testupvalue(lua_State* L) { luaL_register(L,"testupvalue",myfuncs); return 1; } test.lua文件内容
require "testupvalue"
local fun = function()
    func = testupvalue.newCounter();
    print(func());
    print(func());
    print(func());

    func = testupvalue.newCounter();
    print(func());
    print(func());
    print(func());

    --[[ 输出结果为:
    1
    2
    3
    1
    2
    3
    --]]
end
xpcall(fun,print)
os.execute("pause")