TEST_F(TestIronBeeModuleRulesLua, new_state) { int res = 0; ib_tx_t tx; tx.ib = ib_engine; ib_rule_exec_t rule_exec; memset(&rule_exec, 0, sizeof(rule_exec)); rule_exec.ib = ib_engine; rule_exec.tx = &tx; rule_exec.rule = rule; ib_tx_generate_id(&tx, ib_engine->mp); lua_State *L = luaL_newstate(); luaL_openlibs(L); setSearchPath(L); ASSERT_EQ(IB_OK, ib_lua_require(ib_engine, L, "ffi", "ffi")); ASSERT_EQ(IB_OK, ib_lua_require(ib_engine, L, "ibapi", "ironbee/api")); ASSERT_NE(static_cast<lua_State*>(NULL), L); lua_State *L2; ASSERT_EQ(IB_OK, ib_lua_new_thread(ib_engine, L, &L2)); ASSERT_NE(static_cast<lua_State*>(NULL), L2); ASSERT_EQ(IB_OK, ib_lua_load_func(ib_engine, L2, luafile, "f1")); ASSERT_EQ(IB_OK, ib_lua_func_eval_int(ib_engine, &tx, L2, "f1", &res)); ASSERT_EQ(IB_OK, ib_lua_join_thread(ib_engine, L, &L2)); ASSERT_EQ(5, res); }
TEST_F(TestIronBeeModuleRulesLua, load_func_eval) { int res = 0; ib_tx_t tx; tx.ib = ib_engine; tx.id = "tx_id.TestIronBeeModuleRulesLua.load_func_eval"; ASSERT_EQ(IB_OK, ib_conn_create(ib_engine, &tx.conn, NULL)); ib_rule_exec_t rule_exec; memset(&rule_exec, 0, sizeof(rule_exec)); rule_exec.ib = ib_engine; rule_exec.tx = &tx; rule_exec.rule = rule; lua_State *L = luaL_newstate(); ASSERT_NE(static_cast<lua_State*>(NULL), L); luaL_openlibs(L); setSearchPath(L); ASSERT_EQ(IB_OK, ib_lua_require(ib_engine, L, "ffi", "ffi")); ASSERT_EQ(IB_OK, ib_lua_require(ib_engine, L, "ibapi", "ironbee/api")); ASSERT_EQ(IB_OK, ib_lua_load_func(ib_engine, L, luafile, "f1")); ASSERT_EQ(IB_OK, ib_lua_func_eval_int(ib_engine, &tx, L, "f1", &res)); ASSERT_EQ(5, res); }
static ib_status_t lua_operator_execute( ib_tx_t *tx, const ib_field_t *field, ib_field_t *capture, ib_num_t *result, void *instance_data, void *cbdata ) { assert(tx != NULL); assert(tx->ib != NULL); assert(tx->ctx != NULL); assert(tx->conn != NULL); assert(result != NULL); assert(instance_data != NULL); assert(cbdata != NULL); ib_status_t rc; ib_status_t rc2; ib_module_t *module; int result_int; ib_engine_t *ib = tx->ib; ib_context_t *ctx = tx->ctx; modlua_cfg_t *cfg = NULL; modlua_runtime_t *luart = NULL; const char *func_name = (const char *)instance_data; modlua_rules_cbdata_t *modlua_rules_cbdata = (modlua_rules_cbdata_t *)cbdata; assert(modlua_rules_cbdata->module != NULL); /* Get the lua module configuration for this context. */ rc = modlua_cfg_get(ib, ctx, &cfg); if (rc != IB_OK) { return rc; } /* Get the lua module configuration for this context. */ rc = modlua_acquirestate(ib, cfg, &luart); if (rc != IB_OK) { return rc; } module = modlua_rules_cbdata->module; rc = modlua_reload_ctx_except_main(ib, module, ctx, luart->L); if (rc != IB_OK) { ib_log_error_tx(tx, "Failed to reload Lua stack."); goto exit; } /* Call the rule. */ rc = ib_lua_func_eval_int(ib, tx, luart->L, func_name, &result_int); if (rc != IB_OK) { *result = 0; goto exit; } /* Convert the passed in integer type to an ib_num_t. */ *result = result_int; exit: rc2 = modlua_releasestate(ib, cfg, luart); if (rc2 != IB_OK) { ib_log_error_tx(tx, "Failed to return Lua stack."); if (rc == IB_OK) { return rc2; } } return rc; }