lua-users home
lua-l archive

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


Mike Pall <mikelu-1107@mike.de> wrote:

> Thomas Lauer wrote:
> > The following code, run under Windows, produces an error message for
> > line 4 which IMHO is misleading at best but more probably spurious?
> > 
> > ffi=require('ffi')
> > ffi.cdef('void DebugBreak(void);')  -- Any function will do
> > if ffi.C.DebugBreak then print(ffi.C.DebugBreak) end  -- OK
> > if ffi.C.DebugBreak~=nil then print(ffi.C.DebugBreak) end
> > 
> > luajit.exe: f:\testffi.lua:4: attempt to compare 'void ()' with 'void *'
> 
> Well, the error message is correct. You're comparing a function
> with nil (which is treated like a pointer to void here).
> 
> But it's debatable whether comparing a function with a pointer
> should be disallowed. In C, functions and function pointers are
> almost universally exchangeable, so I guess this should be
> allowed here. I've changed the semantics in git HEAD.

Sorry to be a PITA with this but I have to revisit this topic. I still
get very similar error messages which, after thinking a bit about
things, I am convinced shouldn't be there.

The underlying problem is this. Somewhere buried in a module I have a
small function called handler which checks whether the value of an
incoming parameter ~= nil. See this snippet:

> function handler(m,n,v)
> 	m=getModulename(m)
> 	if m=='_G' then m=' in main program'
> 	else m=' in module '..m end
> 	if v~=nil then error('invalid assignment to '..n..m,3) -- Zonk!
> 	else error(n..' not declared'..m,3) end
> end

This function has no idea what type or value the incoming parameter v
might have. In standard Lua this ain't a problem as I can compare all
values, including userdata, with nil. So a line like this:

z=<any lua value> will trigger a call to

> error('invalid assignment to '..n..m,3)

Not so with ffi values, though. If I write, for instance, this:

z=ffi.typeof(<some ct>) the "v ~= nil" test itself crashes out with

> luajit.exe: f:\testffi.lua:46: attempt to compare 'enum 21' with 'void *'

So far, I've always assumed that Lua allows to compare any value with
nil without the comparison itself triggering an error.

Maybe I am mistaken in this assumption. Or maybe Lua does that but the
LuaJIT ffi doesn't. At any rate, I think being able to compare any value
against nil without having the comparison itself trigger an error to be
an important feature.

-- 
cheers  thomasl