| 229 | } |
| 230 | |
| 231 | int RedisRegisterFunction(lua_State *lua) { |
| 232 | int argc = lua_gettop(lua); |
| 233 | |
| 234 | if (argc < 2 || argc > 3) { |
| 235 | lua_pushstring(lua, "wrong number of arguments to redis.register_function()."); |
| 236 | return lua_error(lua); |
| 237 | } |
| 238 | |
| 239 | lua_getglobal(lua, REDIS_FUNCTION_LIBNAME); |
| 240 | if (lua_isnil(lua, -1)) { |
| 241 | lua_pop(lua, 1); |
| 242 | lua_pushstring(lua, "redis.register_function() need to be called from FUNCTION LOAD."); |
| 243 | return lua_error(lua); |
| 244 | } |
| 245 | |
| 246 | std::string libname = lua_tostring(lua, -1); |
| 247 | lua_pop(lua, 1); |
| 248 | |
| 249 | // set this function to global |
| 250 | std::string name = lua_tostring(lua, 1); |
| 251 | if (argc == 3) { |
| 252 | auto flags = ExtractFlagsFromRegisterFunction(lua); |
| 253 | if (!flags) { |
| 254 | lua_pushstring(lua, flags.Msg().c_str()); |
| 255 | return lua_error(lua); |
| 256 | } |
| 257 | lua_pushinteger(lua, static_cast<lua_Integer>(flags.GetValue())); |
| 258 | lua_setglobal(lua, (REDIS_LUA_REGISTER_FUNC_FLAGS_PREFIX + name).c_str()); |
| 259 | } |
| 260 | lua_setglobal(lua, (REDIS_LUA_REGISTER_FUNC_PREFIX + name).c_str()); |
| 261 | |
| 262 | // set this function name to REDIS_FUNCTION_LIBRARIES[libname] |
| 263 | lua_getglobal(lua, REDIS_FUNCTION_LIBRARIES); |
| 264 | if (lua_isnil(lua, -1)) { |
| 265 | lua_pop(lua, 1); |
| 266 | lua_newtable(lua); |
| 267 | } |
| 268 | lua_getfield(lua, -1, libname.c_str()); |
| 269 | if (lua_isnil(lua, -1)) { |
| 270 | lua_pop(lua, 1); |
| 271 | lua_newtable(lua); |
| 272 | } |
| 273 | size_t len = lua_objlen(lua, -1); |
| 274 | lua_pushstring(lua, name.c_str()); |
| 275 | lua_rawseti(lua, -2, static_cast<int>(len) + 1); |
| 276 | lua_setfield(lua, -2, libname.c_str()); |
| 277 | lua_setglobal(lua, REDIS_FUNCTION_LIBRARIES); |
| 278 | |
| 279 | // check if it needs to store |
| 280 | lua_getglobal(lua, REDIS_FUNCTION_NEEDSTORE); |
| 281 | if (!lua_toboolean(lua, -1)) { |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | // store the map from function name to library name |
| 286 | auto *script_run_ctx = GetFromRegistry<ScriptRunCtx>(lua, REGISTRY_SCRIPT_RUN_CTX_NAME); |
| 287 | CHECK(script_run_ctx != nullptr); |
| 288 |
nothing calls this directly
no test coverage detected