lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Subject: Re: Problem using posix library pipe() from luajit2?
Reply-To: dslate@speakeasy.net
To: lua-l@lists.lua.org

Thanks to Mike and Javier for their responses.  Mike's examples of
using ffi to do mkdir() and rmdir() are "stand-alones", i.e. they do
their jobs and are done.  But the purpose of pipe() is to return file
objects for later use in I/O operations in lua code.  Without having
to rewrite that code as well, I would need (as Javier mentions) some
way to translate between FILE* and file objects, would I not?

Thanks,

-- Dave Slate
Happy user of Lua and LuaJIT


On Sun, 27 Mar 2011 07:48:48 -0500 Javier Guerra Giraldez <javier@guerrag.com> wrote:

> Subject: Re: Problem using posix library pipe() from luajit2?
> To: Lua mailing list <lua-l@lists.lua.org>
> Cc: Mike Pall <mikelu-1103@mike.de>
> Message-ID: <BANLkTimGB3msE8adp5R+sTyx4rOJX55xMw@mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
> 
> On Sun, Mar 27, 2011 at 6:11 AM, Mike Pall <mikelu-1103@mike.de> wrote:
> >
> > It looks like luaposix copies the internal metatable for files and
> > attaches it to their own userdata object. This doesn't work with
> > LuaJIT. luaposix shouldn't access internals of the VM in this way.
> 
> 
> that's unfortunate.  i've had to do similar things sometimes, since
> there's no clean way to get to the FILE* from the file object.  the
> alternative is not to accept Lua file objects for any C extension that
> uses files.
> 
> I guess the best would be to export the tofile() function in liolib.c
> (i'm reading some old sources here, maybe could be there for Lua 5.2?)
> 
> 
> > But with LuaJIT there's no need to use luaposix. You can directly
> > access all POSIX C library functions with the FFI library.
> 
> ....except for compatibility
> 
> 
> --
> Javier


On Sun, 27 Mar 2011 13:11:52 +0200 Mike Pall <mikelu-1103@mike.de> wrote:

> Subject: Re: Problem using posix library pipe() from luajit2?
> To: Lua mailing list <lua-l@lists.lua.org>
> 
> David J. Slate wrote:
> > userdata	userdata	posix library for Lua 5.1 / 5.1.9
> > /usr/local/bin/luajit: bad argument #1 to '?' (FILE* expected, got userdata)
> 
> It looks like luaposix copies the internal metatable for files and
> attaches it to their own userdata object. This doesn't work with
> LuaJIT. luaposix shouldn't access internals of the VM in this way.
> 
> But with LuaJIT there's no need to use luaposix. You can directly
> access all POSIX C library functions with the FFI library.
> Example:
> 
>   local ffi = require("ffi")
>   ffi.cdef[[
>   int mkdir(const char *pathname, unsigned int mode);
>   int rmdir(const char *pathname);
>   ]]
>   ffi.C.mkdir("/tmp/testdir", 0x1ff)
>   ffi.C.rmdir("/tmp/testdir")
> 
> --Mike


On Sun, 27 Mar 2011 00:52:25 -0500 "David J. Slate" <dslate@speakeasy.net> wrote:

> Subject: Problem using posix library pipe() from luajit2?
> Reply-To: dslate@speakeasy.net
> To: lua-l@lists.lua.org
> 
> I am having a problem trying to use the posix library pipe() function from
> LuaJIT 2.0.0-beta6.  The same code works in regular Lua 5.1.4.  It seems
> that with LuaJIT, the types of the values returned by pipe() seem to be
> getting confused somehow.
> 
> The script log below illustrates the problem, which seems to occur on both
> openSUSE and Ubuntu flavors of Linux.  Any ideas on what's going wrong?
> 
> Thanks,
> 
> -- Dave Slate
> 
> 
> Script started on Sun Mar 27 00:43:47 2011
> dave@faith2:~/lua> cat pipetst.lua
> #!/usr/local/bin/lua -v
> 
> local ox = require( "posix")
> local rd, wr = ox.pipe()
> print( type( rd), type( wr), ox.version)
> wr:write( "blah\n")
> print( "After wr:write, before wr:close")
> io.flush()
> wr:close()
> print( "After wr:close, before rd:read")
> io.flush()
> print( rd:read())
> rd:close()
> dave@faith2:~/lua> 
> dave@faith2:~/lua> ./pipetst.lua
> Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> userdata	userdata	posix library for Lua 5.1 / 5.1.9
> After wr:write, before wr:close
> After wr:close, before rd:read
> blah
> dave@faith2:~/lua> 
> dave@faith2:~/lua> cat pipetst.luajit
> #!/usr/local/bin/luajit -v
> 
> local ox = require( "posix")
> local rd, wr = ox.pipe()
> print( type( rd), type( wr), ox.version)
> wr:write( "blah\n")
> print( "After wr:write, before wr:close")
> io.flush()
> wr:close()
> print( "After wr:close, before rd:read")
> io.flush()
> print( rd:read())
> rd:close()
> dave@faith2:~/lua>
> dave@faith2:~/lua> diff pipetst.lua pipetst.luajit
> 1c1
> < #!/usr/local/bin/lua -v
> ---
> > #!/usr/local/bin/luajit -v
> dave@faith2:~/lua> 
> dave@faith2:~/lua> ./pipetst.luajit
> LuaJIT 2.0.0-beta6 -- Copyright (C) 2005-2011 Mike Pall. http://luajit.org/
> userdata	userdata	posix library for Lua 5.1 / 5.1.9
> /usr/local/bin/luajit: bad argument #1 to '?' (FILE* expected, got userdata)
> dave@faith2:~/lua> exit
> 
> Script done on Sun Mar 27 00:47:00 2011