lua-users home
lua-l archive

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


On Tuesday 15, Tim Caswell wrote:
> I should mention that this particular function doesn't need to call
> into lua, so if it's not supported, how would I implement the callback
> in C?

You can create a C function that will be used as the callback, then export it 
so Lua can pass it to uv_read_start().

in C:
uv_buf_t lua_ev_alloc(uv_handle_t* handle, size_t suggested_size) {
  uv_buf_t buf;
  buf.base = malloc(suggested_size);
  buf.len = suggested_size;
  return buf;
}

in Lua:
FFI.cdef[[
uv_buf_t lua_ev_alloc(uv_handle_t* handle, size_t suggested_size);
]]

function stream_prototype:read_start(on_read)
  uv_assert(C.uv_read_start(FFI.cast("uv_stream_t*", self),
    C.lua_ev_alloc, on_read))
end

> 
> On Tue, Nov 15, 2011 at 9:47 AM, Tim Caswell <tim@creationix.com> wrote:
> > Well, it seems this works for most callbacks, but not all.  In the
> > docs I see it says it doesn't work with "functions with pass-by-value
> > aggregate argument or result types".  I'm not sure what that means.
> > 
> > My function signature is:
> > 
> >    typedef uv_buf_t (*uv_alloc_cb)(uv_handle_t* handle, size_t
> > suggested_size);
> > 
> > When it tries to convert a lua function to this I get:
> > 
> >    /home/tim/luvit/examples/uvffi/uv.lua:46: bad argument #2 to
> > 'uv_read_start' (cannot convert 'function' to 'struct 2967 (*)()')
> > 
> > Am I doing something wrong, or is this kind of function not supported?
> > 
> > -Tim Caswell
> > 
> > The header file I'm feeding to luajit is
> > https://github.com/creationix/luvit/blob/master/examples/uvffi/ffi_uv.h
> > my lua script is
> > https://github.com/creationix/luvit/blob/master/examples/uvffi/uv.lua
> > 
> > On Tue, Nov 15, 2011 at 3:12 AM, Duncan Cross <duncan.cross@gmail.com> 
wrote:
> >> On Tue, Nov 15, 2011 at 8:25 AM, Duncan Cross <duncan.cross@gmail.com> 
wrote:
> >>> On Tue, Nov 15, 2011 at 5:15 AM, James McKaskill <james@foobar.co.nz> 
wrote:
> >>>> On Nov 14, 2011, at 19:30 , Duncan Cross wrote:
> >>>>> This is fantastic news! Thanks so much for your work, and thanks too
> >>>>> to the anonymous sponsor.
> >>>>> 
> >>>>> One thing - I'm not certain this is related to the addition of
> >>>>> callbacks, but I was just experimenting with Win32 calls and found
> >>>>> that GetLastError() seems to return the wrong value (127,
> >>>>> ERROR_PROC_NOT_FOUND) the first time it's called. This is a minimal
> >>>>> test case:
> >>>>> 
> >>>>>  local ffi = require("ffi")
> >>>>> 
> >>>>>  ffi.cdef [[
> >>>>>  uint32_t GetLastError();
> >>>>>  void SetLastError(uint32_t code);
> >>>>>  ]]
> >>>>> 
> >>>>>  ffi.C.SetLastError(0)
> >>>>>  print(ffi.C.GetLastError()) -- 127
> >>>>>  ffi.C.SetLastError(0)
> >>>>>  print(ffi.C.GetLastError()) -- 0
> >>>>> 
> >>>>> (I'm explicitly setting the error here, but I discovered it because
> >>>>> it was hiding a parameter error while I was experimenting with
> >>>>> trying to call RegisterClassExA().)
> >>>>> 
> >>>>> -Duncan
> >>>> 
> >>>> I would not suggest trying to get errors using Get/SetLastError as
> >>>> they will be invalidated by the vm. If you really want the error use
> >>>> ffi.errno().
> >>>> 
> >>>> -- James
> >>> 
> >>> It was specifically addressed before that it should work, and is not
> >>> the same thing as errno:
> >>> 
> >>> http://lua-users.org/lists/lua-l/2011-05/msg00219.html
> >>> 
> >>> "I've fixed this in git HEAD. LuaJIT now preserves errno across hooks,
> >>> memory allocations, invocations of the JIT compiler and other internal
> >>> VM activity. Ditto for GetLastError() on Windows.
> >>> 
> >>> (...)
> >>> 
> >>> On Windows you need to declare and call GetLastError() yourself (note:
> >>> this does _not_ hold the same value as errno)."
> >>> 
> >>> -Duncan
> >> 
> >> It's also mentioned at the end of the documentation for ffi.errno():
> >> 
> >> http://luajit.org/ext_ffi_api.html#ffi_errno
> >> 
> >> -Duncan


-- 
Robert G. Jakabosky