diff options
-rw-r--r-- | src/lfs.c | 21 | ||||
-rw-r--r-- | tests/test.lua | 17 |
2 files changed, 32 insertions, 6 deletions
@@ -17,7 +17,7 @@ | |||
17 | ** lfs.touch (filepath [, atime [, mtime]]) | 17 | ** lfs.touch (filepath [, atime [, mtime]]) |
18 | ** lfs.unlock (fh) | 18 | ** lfs.unlock (fh) |
19 | ** | 19 | ** |
20 | ** $Id: lfs.c,v 1.59 2009/04/24 22:24:07 mascarenhas Exp $ | 20 | ** $Id: lfs.c,v 1.60 2009/06/03 20:49:18 mascarenhas Exp $ |
21 | */ | 21 | */ |
22 | 22 | ||
23 | #ifndef _WIN32 | 23 | #ifndef _WIN32 |
@@ -431,7 +431,7 @@ static int dir_iter (lua_State *L) { | |||
431 | #else | 431 | #else |
432 | struct dirent *entry; | 432 | struct dirent *entry; |
433 | #endif | 433 | #endif |
434 | dir_data *d = (dir_data *)lua_touserdata (L, lua_upvalueindex (1)); | 434 | dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE); |
435 | luaL_argcheck (L, !d->closed, 1, "closed directory"); | 435 | luaL_argcheck (L, !d->closed, 1, "closed directory"); |
436 | #ifdef _WIN32 | 436 | #ifdef _WIN32 |
437 | if (d->hFile == 0L) { /* first entry */ | 437 | if (d->hFile == 0L) { /* first entry */ |
@@ -493,7 +493,9 @@ static int dir_close (lua_State *L) { | |||
493 | */ | 493 | */ |
494 | static int dir_iter_factory (lua_State *L) { | 494 | static int dir_iter_factory (lua_State *L) { |
495 | const char *path = luaL_checkstring (L, 1); | 495 | const char *path = luaL_checkstring (L, 1); |
496 | dir_data *d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); | 496 | dir_data *d; |
497 | lua_pushcfunction (L, dir_iter); | ||
498 | d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); | ||
497 | d->closed = 0; | 499 | d->closed = 0; |
498 | #ifdef _WIN32 | 500 | #ifdef _WIN32 |
499 | d->hFile = 0L; | 501 | d->hFile = 0L; |
@@ -510,8 +512,7 @@ static int dir_iter_factory (lua_State *L) { | |||
510 | if (d->dir == NULL) | 512 | if (d->dir == NULL) |
511 | luaL_error (L, "cannot open %s: %s", path, strerror (errno)); | 513 | luaL_error (L, "cannot open %s: %s", path, strerror (errno)); |
512 | #endif | 514 | #endif |
513 | lua_pushcclosure (L, dir_iter, 1); | 515 | return 2; |
514 | return 1; | ||
515 | } | 516 | } |
516 | 517 | ||
517 | 518 | ||
@@ -521,10 +522,18 @@ static int dir_iter_factory (lua_State *L) { | |||
521 | static int dir_create_meta (lua_State *L) { | 522 | static int dir_create_meta (lua_State *L) { |
522 | luaL_newmetatable (L, DIR_METATABLE); | 523 | luaL_newmetatable (L, DIR_METATABLE); |
523 | /* set its __gc field */ | 524 | /* set its __gc field */ |
525 | lua_pushstring (L, "__index"); | ||
526 | lua_newtable(L); | ||
527 | lua_pushstring (L, "next"); | ||
528 | lua_pushcfunction (L, dir_iter); | ||
529 | lua_settable(L, -3); | ||
530 | lua_pushstring (L, "close"); | ||
531 | lua_pushcfunction (L, dir_close); | ||
532 | lua_settable(L, -3); | ||
533 | lua_settable (L, -3); | ||
524 | lua_pushstring (L, "__gc"); | 534 | lua_pushstring (L, "__gc"); |
525 | lua_pushcfunction (L, dir_close); | 535 | lua_pushcfunction (L, dir_close); |
526 | lua_settable (L, -3); | 536 | lua_settable (L, -3); |
527 | |||
528 | return 1; | 537 | return 1; |
529 | } | 538 | } |
530 | 539 | ||
diff --git a/tests/test.lua b/tests/test.lua index 1fd6157..7111074 100644 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -110,4 +110,21 @@ for i = 1, 4000 do | |||
110 | count = count + 1 | 110 | count = count + 1 |
111 | end | 111 | end |
112 | end | 112 | end |
113 | |||
114 | -- Stressing directory iterator, explicit version | ||
115 | count = 0 | ||
116 | for i = 1, 4000 do | ||
117 | local iter, dir = lfs.dir(tmp) | ||
118 | local file = dir:next() | ||
119 | while file do | ||
120 | count = count + 1 | ||
121 | file = dir:next() | ||
122 | end | ||
123 | assert(not pcall(dir.next, dir)) | ||
124 | end | ||
125 | |||
126 | -- directory explicit close | ||
127 | local iter, dir = lfs.dir(tmp) | ||
128 | dir:close() | ||
129 | assert(not pcall(dir.next, dir)) | ||
113 | print"Ok!" | 130 | print"Ok!" |