Skip to content

yieldable_c_functions

Philipp Janda edited this page Sep 10, 2017 · 3 revisions

Yieldable C functions

The emulation of lua_(p)callk for previous Lua versions is not 100% perfect, because the continuation functions in Lua 5.2 have a different type signature than the ones in Lua 5.3 (and Lua 5.1 doesn't have continuation functions at all). But with the help of a small macro the same code can be used for all three Lua versions (the 5.1 version won't support yielding obviously).

Original Lua 5.3 code (example adapted from the Lua 5.3 manual):

static int k (lua_State *L, int status, lua_KContext ctx) {
  ... /* code 2 */
}

int original_function (lua_State *L) {
  ... /* code 1 */
  return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
}

Portable version:

LUA_KFUNCTION( k ) {
  ... /* code 2; parameters L, status, and ctx available here */
}

int original_function (lua_State *L) {
  ... /* code 1 */
  return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
}
Clone this wiki locally