第10月第6天 lua 闭包

1.

static int mytest(lua_State *L) {
  //获取上值
  int upv = (int)lua_tonumber(L, lua_upvalueindex(1));
  printf("%d
", upv);
  upv += 5;
  lua_pushinteger(L, upv);
  lua_replace(L, lua_upvalueindex(1));
 
  //获取一般参数
  printf("%d
", (int)lua_tonumber(L,1));
 
  return 0;
}
 
int main(void) {
  lua_State *L = luaL_newstate();
  luaL_openlibs(L);
 
  //设置Cclosure函数的上值
  lua_pushinteger(L,10);
  lua_pushinteger(L,11);
  lua_pushcclosure(L,mytest,2);
 lua_setglobal(L,"upvalue_test");

http://www.cnblogs.com/biyeqingfeng/p/4990101.html

原文地址:https://www.cnblogs.com/javastart/p/7124740.html