Re: cast a pointer to a callable lua function

  • From: Mike Pall <mike-1205@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 14 May 2012 19:58:59 +0200

GrrrWaaa wrote:
> Probably my mail wasn't clear, sorry. I'm using the FFI to call
> the factory function, which returns a lua_CFunction pointer. I
> want to convert (somehow) that pointer to a valid Lua function.
> The hacky solution is working, but is hacky and depends on a
> couple of C functions.

Well, why would you want to call the factory function via the FFI?

Anyway, whatever workaround you found to get at the 'L' of the
currently running state is not doing you any good: you MUST NOT
call any of the classic Lua/C functions via the FFI on the current
lua_State or you risk corrupting its internal data structures.
There's a reason why there's no 'ffi.L' ...

[You _may_ call them with a completely independent lua_State.
I.e. create and control an independent instance of the VM. But
that's not your use case.]

> I know that calling it will abort a trace, but in this case it
> isn't going to be part of performance-critical code. The factory
> is doing something similar to require(), maybe more frequent but
> not a tight loop, if that helps to explain the scenario.  

Then better use a classic Lua/C API function, which does:
  lua_pushcclosure(L, factory(...), 0)

--Mike

Other related posts: