#include <uv.h>
+#include "daemon/engine.h"
#include "daemon/bindings.h"
#include "daemon/ffimodule.h"
#include "lib/module.h"
#include "lib/layer.h"
+#if LUA_VERSION_NUM >= 502
+#define l_resume(L, argc) lua_resume((L), NULL, (argc))
+#else
+#define l_resume(L, argc) lua_resume((L), (argc))
+#endif
+
/** @internal Helper for retrieving the right function entrypoint. */
static inline lua_State *l_ffi_preface(struct kr_module *module, const char *call) {
lua_State *L = module->lib;
}
/** @internal Helper for calling the entrypoint. */
-static inline int l_ffi_resume(lua_State *L, int argc)
+static inline int l_ffi_resume(lua_State *L, bool resume, int argc)
{
-#if LUA_VERSION_NUM >= 502
- int status = lua_resume(L, NULL, argc);
-#else
- int status = lua_resume(L, argc);
-#endif
+ int status = resume ? l_resume(L, argc) : engine_pcall(L, argc);
switch(status) {
case LUA_YIELD: /* Continuation */
return kr_error(EAGAIN);
static void l_ffi_resume_cb(uv_idle_t *check)
{
lua_State *L = check->data;
- int status = l_ffi_resume(L, 0);
+ int status = l_ffi_resume(L, true, 0);
if (status != kr_error(EAGAIN)) {
uv_idle_stop(check); /* Stop coroutine */
uv_close((uv_handle_t *)check, (uv_close_cb)free);
/** @internal Helper for calling the entrypoint. */
static inline int l_ffi_call(lua_State *L, int argc)
{
- int status = l_ffi_resume(L, argc);
+ int status = l_ffi_resume(L, false, argc);
if (status == kr_error(EAGAIN)) {
return l_ffi_defer(L);
}
{
lua_State *L = l_ffi_preface(module, "deinit");
/* @note Do not allow coroutine here. */
- int ret = l_ffi_resume(L, 1);
+ int ret = l_ffi_resume(L, false, 1);
/* Free the layer API wrapper */
lua_rawgeti(L, LUA_REGISTRYINDEX, (intptr_t)module->data);
lua_getfield(L, -1, "_layercdata");
LAYER_FFI_CALL(ctx, "begin");
lua_pushlightuserdata(L, module_param);
ctx->data = module_param;
- return l_ffi_resume(L, 2);
+ return l_ffi_resume(L, false, 2);
}
static int l_ffi_layer_reset(knot_layer_t *ctx)
{
LAYER_FFI_CALL(ctx, "reset");
lua_pushlightuserdata(L, ctx->data);
- return l_ffi_resume(L, 2);
+ return l_ffi_resume(L, false, 2);
}
static int l_ffi_layer_finish(knot_layer_t *ctx)
{
LAYER_FFI_CALL(ctx, "finish");
lua_pushlightuserdata(L, ctx->data);
- return l_ffi_resume(L, 2);
+ return l_ffi_resume(L, false, 2);
}
static int l_ffi_layer_consume(knot_layer_t *ctx, knot_pkt_t *pkt)
LAYER_FFI_CALL(ctx, "consume");
lua_pushlightuserdata(L, ctx->data);
lua_pushlightuserdata(L, pkt);
- return l_ffi_resume(L, 3);
+ return l_ffi_resume(L, false, 3);
}
static int l_ffi_layer_produce(knot_layer_t *ctx, knot_pkt_t *pkt)
LAYER_FFI_CALL(ctx, "produce");
lua_pushlightuserdata(L, ctx->data);
lua_pushlightuserdata(L, pkt);
- return l_ffi_resume(L, 3);
+ return l_ffi_resume(L, false, 3);
}
static int l_ffi_layer_fail(knot_layer_t *ctx, knot_pkt_t *pkt)
LAYER_FFI_CALL(ctx, "fail");
lua_pushlightuserdata(L, ctx->data);
lua_pushlightuserdata(L, pkt);
- return l_ffi_resume(L, 3);
+ return l_ffi_resume(L, false, 3);
}
/** @internal Retrieve C layer api wrapper. */
REGISTER_FFI_CALL(L, module->deinit, "deinit", &l_ffi_deinit);
REGISTER_FFI_CALL(L, module->layer, "layer", &l_ffi_layer);
module->data = (void *)(intptr_t)luaL_ref(L, LUA_REGISTRYINDEX);
- module->lib = lua_newthread(L);
+ module->lib = L;
return module->init(module);
}