Re: Calling back into Lua: Coroutines, ffi and the C api

  • From: Mike Pall <mike-1311@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 19 Nov 2013 12:55:24 +0100

Nils Nordman wrote:
> A partial call stack can thus end up looking something like this:
> 
> C -> C api -> Lua -> ffi call -> C -> C api -> Lua

This is not a valid call stack. You must not use the Lua/C API
from an FFI call.

A simple test is to ask: where has the 'C api' part in your call
stack gotten its lua_State pointer? The FFI deliberately doesn't
provide one -- don't pass a lua_State behind its back.

> C -> C api -> Lua (coroutine.create/resume) -> ffi call -> C -> C api (SEGV)

Ditto.

> 3) In the coroutine case, if the SEGV is to be expected, would the
> situation change if a ffi callback was used instead of using the C api
> for call backs? Or if the C api was used instead of the ffi?

An FFI callback would work. But please read this first:
  http://luajit.org/ext_ffi_semantics.html#callback

--Mike

Other related posts: