static char* test_api_assertion() { lsb_lua_sandbox *sb = lsb_create(NULL, "lua/counter.lua", "", NULL); lsb_err_value ret = lsb_init(sb, NULL); mu_assert(!ret, "lsb_init() received: %s", ret); lsb_stop_sandbox(NULL); mu_assert(lsb_destroy(NULL) == NULL, "not null"); mu_assert(lsb_usage(NULL, 0, 0) == 0, "not 0"); mu_assert(lsb_usage(sb, LSB_UT_MAX, 0) == 0, "not 0"); mu_assert(lsb_usage(sb, 0, LSB_US_MAX) == 0, "not 0"); mu_assert(strcmp(lsb_get_error(NULL), "") == 0, "not empty"); lsb_set_error(NULL, "foo"); mu_assert(lsb_get_lua(NULL) == NULL, "not null"); mu_assert(lsb_get_lua_file(NULL) == NULL, "not null"); mu_assert(lsb_get_parent(NULL) == NULL, "not null"); mu_assert(lsb_get_logger(NULL) == NULL, "not null"); mu_assert(lsb_get_state(NULL) == LSB_UNKNOWN, "not unknown"); lsb_add_function(NULL, lsb_test_write_output, "foo"); lsb_add_function(sb, NULL, "foo"); lsb_add_function(sb, lsb_test_write_output, NULL); mu_assert(lsb_pcall_setup(NULL, "foo") == LSB_ERR_UTIL_NULL, "not null"); mu_assert(lsb_pcall_setup(sb, NULL) == LSB_ERR_UTIL_NULL, "not null"); lsb_add_function(NULL, NULL, NULL); lsb_pcall_teardown(NULL); lsb_terminate(NULL, NULL); lsb_terminate(sb, NULL); lsb_add_function(sb, lsb_test_write_output, "write_output"); e = lsb_destroy(sb); mu_assert(!e, "lsb_destroy() received: %s", e); return NULL; }
int lsb_heka_pm_input(lsb_heka_sandbox *hsb, double cp_numeric, const char *cp_string, bool profile) { if (!hsb || hsb->type != 'i') { return 1; } lsb_err_value ret = lsb_pcall_setup(hsb->lsb, pm_func_name); if (ret) { if (ret != LSB_ERR_TERMINATED) { char err[LSB_ERROR_SIZE]; snprintf(err, LSB_ERROR_SIZE, "%s() function was not found", pm_func_name); lsb_terminate(hsb->lsb, err); } return 1; } lua_State *lua = lsb_get_lua(hsb->lsb); if (!lua) return 1; if (!isnan(cp_numeric)) { lua_pushnumber(lua, cp_numeric); } else if (cp_string) { lua_pushstring(lua, cp_string); } else { lua_pushnil(lua); } return process_message(hsb, NULL, lua, 1, profile); }
int report(lua_sandbox* lsb, double tc) { static const char* func_name = "report"; lua_State* lua = lsb_get_lua(lsb); if (!lua) return 1; if (lsb_pcall_setup(lsb, func_name)) return 1; lua_pushnumber(lua, tc); if (lua_pcall(lua, 1, 0, 0) != 0) { char err[LSB_ERROR_SIZE]; int len = snprintf(err, LSB_ERROR_SIZE, "%s() %s", func_name, lua_tostring(lua, -1)); if (len >= LSB_ERROR_SIZE || len < 0) { err[LSB_ERROR_SIZE - 1] = 0; } lsb_terminate(lsb, err); return 1; } lsb_pcall_teardown(lsb); lua_gc(lua, LUA_GCCOLLECT, 0); return 0; }
int lsb_heka_pm_analysis(lsb_heka_sandbox *hsb, lsb_heka_message *msg, bool profile) { if (!hsb || !msg || hsb->type != 'a') return 1; if (lsb_pcall_setup(hsb->lsb, pm_func_name)) { char err[LSB_ERROR_SIZE]; snprintf(err, LSB_ERROR_SIZE, "%s() function was not found", pm_func_name); lsb_terminate(hsb->lsb, err); return 1; } lua_State *lua = lsb_get_lua(hsb->lsb); if (!lua) return 1; return process_message(hsb, msg, lua, 0, profile); }
int lsb_heka_timer_event(lsb_heka_sandbox *hsb, time_t t, bool shutdown) { static const char *func_name = "timer_event"; if (!hsb || (hsb->type != 'o' && hsb->type != 'a')) { return 1; } lua_State *lua = lsb_get_lua(hsb->lsb); if (!lua) return 1; if (lsb_pcall_setup(hsb->lsb, func_name)) { char err[LSB_ERROR_SIZE]; snprintf(err, LSB_ERROR_SIZE, "%s() function was not found", func_name); lsb_terminate(hsb->lsb, err); return 1; } // todo change if we need more than 1 sec resolution lua_pushnumber(lua, t * 1e9); lua_pushboolean(lua, shutdown); unsigned long long start, end; start = lsb_get_time(); if (lua_pcall(lua, 2, 0, 0) != 0) { char err[LSB_ERROR_SIZE]; size_t len = snprintf(err, LSB_ERROR_SIZE, "%s() %s", func_name, lua_tostring(lua, -1)); if (len >= LSB_ERROR_SIZE) { err[LSB_ERROR_SIZE - 1] = 0; } lsb_terminate(hsb->lsb, err); return 1; } end = lsb_get_time(); lsb_update_running_stats(&hsb->stats.te, (double)(end - start)); lsb_pcall_teardown(hsb->lsb); lua_gc(lua, LUA_GCCOLLECT, 0); return 0; }
int process(lua_sandbox* lsb, double ts) { static const char* func_name = "process"; lua_State* lua = lsb_get_lua(lsb); if (!lua) return 1; if (lsb_pcall_setup(lsb, func_name)) return 1; lua_pushnumber(lua, ts); if (lua_pcall(lua, 1, 1, 0) != 0) { char err[LSB_ERROR_SIZE]; int len = snprintf(err, LSB_ERROR_SIZE, "%s() %s", func_name, lua_tostring(lua, -1)); if (len >= LSB_ERROR_SIZE || len < 0) { err[LSB_ERROR_SIZE - 1] = 0; } lsb_terminate(lsb, err); return 1; } if (!lua_isnumber(lua, 1)) { char err[LSB_ERROR_SIZE]; int len = snprintf(err, LSB_ERROR_SIZE, "%s() must return a single numeric value", func_name); if (len >= LSB_ERROR_SIZE || len < 0) { err[LSB_ERROR_SIZE - 1] = 0; } lsb_terminate(lsb, err); return 1; } int status = (int)lua_tointeger(lua, 1); lua_pop(lua, 1); lsb_pcall_teardown(lsb); return status; }
int lsb_heka_pm_output(lsb_heka_sandbox *hsb, lsb_heka_message *msg, void *sequence_id, bool profile) { if (!hsb || !msg || hsb->type != 'o') return 1; if (lsb_pcall_setup(hsb->lsb, pm_func_name)) { char err[LSB_ERROR_SIZE]; snprintf(err, LSB_ERROR_SIZE, "%s() function was not found", pm_func_name); lsb_terminate(hsb->lsb, err); return 1; } lua_State *lua = lsb_get_lua(hsb->lsb); if (!lua) return 1; int nargs = 0; if (sequence_id) { nargs = 1; lua_pushlightuserdata(lua, sequence_id); } return process_message(hsb, msg, lua, nargs, profile); }