={
{"new",newarray},
{"set",setarray},
{"get",getarray},
{"size",getsize},
{NULL,NULL}
};
int luaopen_array...第一步是修改打开库的函数,让该函数为数组创建元表:
int luaopen_array(lua_State *L){
luaL_newmetatable(L,"LuaBook.array");
luaL_newlib...luaL_Reg arraylib_m[] = {
{"set",setarray},
{"get",getarray},
{"size",getsize},
{NULL,NULL}
};
新的打开函数luaopen_array...必须创建元表,并把它赋给自己的index字段,然后在元表中注册所有方法,创建和填充表array:
int luaopen_array (lua_State *L){
luaL+newetatable(...setarray},
{"__index",getarray},
{"__len",getsize},
("__tostring",array2string),
{NULL,NULL}
};
int luaopen_array