From 0f893a468231dee01b8798725b5ace15c315e712 Mon Sep 17 00:00:00 2001 From: Lorenzo Cogotti Date: Mon, 12 Dec 2022 12:35:48 +0100 Subject: [PATCH] [osx.c] Add directory __tostring() metamethod. --- osx.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/osx.c b/osx.c index 23879c1..7945ad1 100644 --- a/osx.c +++ b/osx.c @@ -276,6 +276,26 @@ static int os_dir_open(lua_State *L) return 1; } +static int os_dir_tostring(lua_State *L) +{ + df_os_dir *dir = luaL_checkudata(L, 1, DF_LUA_DIRHANDLE); + + if (dir->closeflag) { + lua_pushliteral(L, "directory (closed)"); + } else { + df_os_dirhn hn = dir->hn; + void *p; + +#ifdef _WIN32 + p = (void *) hn->dirh; +#else + p = hn; +#endif + lua_pushfstring(L, "directory (%p)", p); + } + return 1; +} + static int os_dir_close(lua_State *L) { df_os_dir *dir = todir(L); @@ -624,6 +644,7 @@ static void createmeta(lua_State *L) { static const luaL_Reg metameth[] = { { "__index", NULL }, // placeholder + { "__tostring", os_dir_tostring }, { "__gc", os_dir_gc }, #if LUA_VERSION_NUM >= 504 { "__close", os_dir_gc},