about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-05 09:17:54 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-11-05 09:17:54 -0700
commit37b05c2957657ca618dfc183a909705b32b7adc5 (patch)
treed70dbe5aa69e7c728bb4c075f4f6c203d7cc014b
parentb4d86665b8bb42de74ffc52e123b7383e3128f9f (diff)
downloadteliva-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.c15
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();
mmit/baremetal/101screen.subx?h=main&id=6e36ce06dd035408d43a6599b75b933a0709cc78'>6e36ce06 ^
c5b573cf ^
6e36ce06 ^





3a0664e1 ^
6e36ce06 ^



0daf12c5 ^
520e3c25 ^
0daf12c5 ^




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42