luvit/luvit — Issue #39: fibers issue?
Author: dvv
Created: 2011-12-07 20:13:23+00:00
State: open

Hi!

The following code mimicking `find` doesn't work:
```

#!/usr/bin/env luvit

local Fiber = require('fiber')
local Fs = require('fs')
local Path = require('path')

--
-- mimicks find
--
local function find(path, match_fn, callback)
path = Path.normalize(path)
Fiber.new(function(resume, wait)
local err, files
-- match
match_fn(path)
-- read sub-files, if any
Fs.readdir(path, resume)
err, files = wait()
-- read error -> bail out
if err then
-- ENOTDIR: current path is not directory. reset error and proceed
-- ENOENT: e.g. broken symlink
if err.code == 'ENOTDIR' or err.code == 'ENOENT' then
err = nil
end
callback(err)
return
end
-- recursively iterate thru files
local i, file
for i, file in ipairs(files) do
-- FIXME: this is very dumb way to cope with self-pointing symlinks
if #path > 255 then
debug('TRIMMED', path)
else
find(Path.join(path, file), match_fn, resume)
-- bail out on any error
err = wait()
if (err) then break end
end
end
callback(err)
end)
end

find('/home', function(path, stat)
--debug('MATCH?', path)
end, function(err)
debug('DONE', err)
end)
```

It dumps with different errors, either segfault or:

```
[string "(�~������..."]:26: string length overflow
stack traceback:
[C]: ?
[C]: in function 'xpcall'
[string "luvit"]: in main chunk
[C]: in function 'run'
[string "luvit"]: in main chunk
[C]: in function 'require'
[string "assert(require('luvit'))"]:1: in main chunk
```