lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


And if you like everything written in C

static int CreateFont(lua_State *L) {
  gfx_createfont(L);
  lua_pushvalue(L, -1);  /* font */
  lua_pushvalue(L, 1);   /* bmp */
  lua_rawset(L, lua_upvalueindex(1));  /* refs[font]=bmp */
  return 1;
}

and register it like so:

  lua_pushliteral(L, "CreateFont");
  lua_newtable(L);       /* refs table for closure */
  lua_pushvalue(L, -1);  /* table is its own metatable */
  lua_setmetatable(L, -2);
  lua_pushliteral(L, "__mode");
  lua_pushliteral(L, "k");
  lua_rawset(L, -3);   /* metatable.__mode = "k" */
  lua_pushcclosure(L, CreateFont, 1);
  lua_settable(L, LUA_GLOBALSINDEX);

- Peter Shook