diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2021-11-05 09:17:54 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2021-11-05 09:17:54 -0700 |
commit | 37b05c2957657ca618dfc183a909705b32b7adc5 (patch) | |
tree | d70dbe5aa69e7c728bb4c075f4f6c203d7cc014b | |
parent | b4d86665b8bb42de74ffc52e123b7383e3128f9f (diff) | |
download | teliva-37b05c2957657ca618dfc183a909705b32b7adc5.tar.gz |
https://www.lua.org/pil/28.3.html
a = array.new(1000) for i=1,1000 do a:set(i, 1/i) end print(a:get(10)) -- 0.1
-rw-r--r-- | src/lua.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/lua.c b/src/lua.c index 2ee46d6..5dcc492 100644 --- a/src/lua.c +++ b/src/lua.c @@ -436,8 +436,12 @@ static int getsize (lua_State *L) { } -static const struct luaL_Reg arraylib[] = { +static const struct luaL_Reg arraylib_functions [] = { {"new", newarray}, + {NULL, NULL} +}; + +static const struct luaL_Reg array_methods [] = { {"set", setarray}, {"get", getarray}, {"size", getsize}, @@ -454,7 +458,14 @@ int main (int argc, char **argv) { return EXIT_FAILURE; } luaL_newmetatable(L, "meta.array"); - luaL_register(L, "array", arraylib); + /* stack: metatable */ + lua_pushstring(L, "__index"); + /* stack: metatable "__index" */ + lua_pushvalue(L, -2); /* metatable */ + lua_settable(L, -3); /* metatable.__index = metatable */ + /* stack: metatable */ + luaL_register(L, NULL, array_methods); /* register array_methods in metatable */ + luaL_register(L, "array", arraylib_functions); //? luaL_register(L, "curses.window", curses_window_fns); initscr(); echo(); |