Skip to content

Commit

Permalink
Formatting the code as per suggestions from the build process.
Browse files Browse the repository at this point in the history
  • Loading branch information
parthpatel committed Oct 1, 2024
1 parent f6887da commit 3a0f4a0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 73 deletions.
76 changes: 38 additions & 38 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
*/

/*
* This file initializes the global LUA object and registers functions to call Valkey API from within the LUA language.
* This file initializes the global LUA object and registers functions to call Valkey API from within the LUA language.
* It heavily invokes LUA's C API documented at https://www.lua.org/pil/24.html. There are 2 entrypoint functions in
* this file:
* 1. evalCommand() - Gets invoked every time a user runs LUA script via eval command on Valkey.
* 2. scriptingInit() - initServer() function from server.c invokes this to initialize LUA at startup.
* It is also invoked between 2 eval invocations to reset Lua.
* 1. evalCommand() - Gets invoked every time a user runs LUA script via eval command on Valkey.
* 2. scriptingInit() - initServer() function from server.c invokes this to initialize LUA at startup.
* It is also invoked between 2 eval invocations to reset Lua.
*/
#include "server.h"
#include "sha1.h"
Expand Down Expand Up @@ -136,7 +136,7 @@ void sha1hex(char *digest, char *script, size_t len) {
digest[40] = '\0';
}

/* Adds server.breakpoint() function used by lua debugger.
/* Adds server.breakpoint() function used by lua debugger.
*
* Allows to stop execution during a debugging session from within
* the Lua code implementation, like if a breakpoint was set in the code
Expand Down Expand Up @@ -210,51 +210,51 @@ void scriptingInit(int setup) {

/* register debug commands. we only need to add it under 'server' as 'redis' is effectively aliased to 'server'
* table at this point. */
lua_getglobal(lua,"server");
lua_getglobal(lua, "server");

/* server.breakpoint */
lua_pushstring(lua,"breakpoint");
lua_pushcfunction(lua,luaServerBreakpointCommand);
lua_settable(lua,-3);
lua_pushstring(lua, "breakpoint");
lua_pushcfunction(lua, luaServerBreakpointCommand);
lua_settable(lua, -3);

/* server.debug */
lua_pushstring(lua,"debug");
lua_pushcfunction(lua,luaServerDebugCommand);
lua_settable(lua,-3);
lua_pushstring(lua, "debug");
lua_pushcfunction(lua, luaServerDebugCommand);
lua_settable(lua, -3);

/* server.replicate_commands */
lua_pushstring(lua, "replicate_commands");
lua_pushcfunction(lua, luaServerReplicateCommandsCommand);
lua_settable(lua, -3);

lua_setglobal(lua,"server");
lua_setglobal(lua, "server");

/* Add a helper function we use for pcall error reporting.
* Note that when the error is in the C function we want to report the
* information about the caller, that's what makes sense from the point
* of view of the user debugging a script. */
{
char *errh_func = "local dbg = debug\n"
"debug = nil\n"
"function __server__err__handler(err)\n"
" local i = dbg.getinfo(2,'nSl')\n"
" if i and i.what == 'C' then\n"
" i = dbg.getinfo(3,'nSl')\n"
" end\n"
" if type(err) ~= 'table' then\n"
" err = {err='ERR ' .. tostring(err)}"
" end"
" if i then\n"
" err['source'] = i.source\n"
" err['line'] = i.currentline\n"
" end"
" return err\n"
"end\n";
luaL_loadbuffer(lua,errh_func,strlen(errh_func),"@err_handler_def");
lua_pcall(lua,0,0,0);
char *errh_func = "local dbg = debug\n"
"debug = nil\n"
"function __server__err__handler(err)\n"
" local i = dbg.getinfo(2,'nSl')\n"
" if i and i.what == 'C' then\n"
" i = dbg.getinfo(3,'nSl')\n"
" end\n"
" if type(err) ~= 'table' then\n"
" err = {err='ERR ' .. tostring(err)}"
" end"
" if i then\n"
" err['source'] = i.source\n"
" err['line'] = i.currentline\n"
" end"
" return err\n"
"end\n";
luaL_loadbuffer(lua, errh_func, strlen(errh_func), "@err_handler_def");
lua_pcall(lua, 0, 0, 0);
/* Duplicate the function with __redis__err_handler name for backwards compatibility */
lua_getglobal(lua,"__server__err__handler");
lua_setglobal(lua,"__redis__err__handler");
lua_getglobal(lua, "__server__err__handler");
lua_setglobal(lua, "__redis__err__handler");
}

/* Create the (non connected) client that we use to execute server commands
Expand Down Expand Up @@ -1528,13 +1528,13 @@ void ldbServer(lua_State *lua, sds *argv, int argc) {

lua_getglobal(lua, "server");
lua_pushstring(lua, "call");
lua_gettable(lua, -2); /* Stack: server, server.call */
lua_gettable(lua, -2); /* Stack: server, server.call */
for (j = 1; j < argc; j++)
lua_pushlstring(lua, argv[j], sdslen(argv[j]));
ldb.step = 1; /* Force server.call() to log. */
lua_pcall(lua, argc-1, 1, 0); /* Stack: server, result */
ldb.step = 0; /* Disable logging. */
lua_pop(lua, 2); /* Discard the result and clean the stack. */
ldb.step = 1; /* Force server.call() to log. */
lua_pcall(lua, argc - 1, 1, 0); /* Stack: server, result */
ldb.step = 0; /* Disable logging. */
lua_pop(lua, 2); /* Discard the result and clean the stack. */
}

/* Implements "trace" command of the Lua debugger. It just prints a backtrace
Expand Down
64 changes: 32 additions & 32 deletions src/script_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ static char *deny_list[] = {
NULL,
};

static int server_math_random (lua_State *L);
static int server_math_randomseed (lua_State *L);
static int server_math_random(lua_State *L);
static int server_math_randomseed(lua_State *L);
static void redisProtocolToLuaType_Int(void *ctx, long long val, const char *proto, size_t proto_len);
static void
redisProtocolToLuaType_BulkString(void *ctx, const char *str, size_t len, const char *proto, size_t proto_len);
Expand All @@ -160,7 +160,7 @@ static void redisProtocolToLuaType_VerbatimString(void *ctx,
size_t proto_len);
static void redisProtocolToLuaType_Attribute(struct ReplyParser *parser, void *ctx, size_t len, const char *proto);

static void luaReplyToServerReply(client *c, client* script_client, lua_State *lua);
static void luaReplyToServerReply(client *c, client *script_client, lua_State *lua);

/*
* Save the give pointer on Lua registry, used to save the Lua context and
Expand Down Expand Up @@ -611,8 +611,8 @@ int luaError(lua_State *lua) {

/* Reply to client 'c' converting the top element in the Lua stack to a
* server reply. As a side effect the element is consumed from the stack. */
static void luaReplyToServerReply(client *c, client* script_client, lua_State *lua) {
int t = lua_type(lua,-1);
static void luaReplyToServerReply(client *c, client *script_client, lua_State *lua) {
int t = lua_type(lua, -1);

if (!lua_checkstack(lua, 4)) {
/* Increase the Lua stack if needed to make sure there is enough room
Expand Down Expand Up @@ -734,7 +734,7 @@ static void luaReplyToServerReply(client *c, client* script_client, lua_State *l
lua_pushnil(lua); /* Use nil to start iteration. */
while (lua_next(lua, -2)) {
/* Stack now: table, key, value */
lua_pushvalue(lua, -2); /* Dup key before consuming. */
lua_pushvalue(lua, -2); /* Dup key before consuming. */
luaReplyToServerReply(c, script_client, lua); /* Return key. */
luaReplyToServerReply(c, script_client, lua); /* Return value. */
/* Stack now: table, key. */
Expand All @@ -757,8 +757,8 @@ static void luaReplyToServerReply(client *c, client* script_client, lua_State *l
lua_pushnil(lua); /* Use nil to start iteration. */
while (lua_next(lua, -2)) {
/* Stack now: table, key, true */
lua_pop(lua,1); /* Discard the boolean value. */
lua_pushvalue(lua,-1); /* Dup key before consuming. */
lua_pop(lua, 1); /* Discard the boolean value. */
lua_pushvalue(lua, -1); /* Dup key before consuming. */
luaReplyToServerReply(c, script_client, lua); /* Return key. */
/* Stack now: table, key. */
setlen++;
Expand Down Expand Up @@ -1411,7 +1411,7 @@ void luaRegisterVersion(lua_State *lua) {
lua_settable(lua, -3);
}

void luaRegisterLogFunction(lua_State* lua) {
void luaRegisterLogFunction(lua_State *lua) {
/* server.log and log levels. */
lua_pushstring(lua, "log");
lua_pushcfunction(lua, luaLogCommand);
Expand All @@ -1435,11 +1435,11 @@ void luaRegisterLogFunction(lua_State* lua) {
}

/*
* Adds server.* functions/fields to lua such as server.call etc.
* This function only handles fields common between Functions and LUA scripting.
* scriptingInit() and functionsInit() may add additional fields specific to each.
* Adds server.* functions/fields to lua such as server.call etc.
* This function only handles fields common between Functions and LUA scripting.
* scriptingInit() and functionsInit() may add additional fields specific to each.
*/
void luaRegisterServerAPI(lua_State* lua) {
void luaRegisterServerAPI(lua_State *lua) {
/* In addition to registering server.call/pcall API, we will throw a customer message when a script accesses
* undefined global variable. LUA stores global variables in the global table, accessible to us on stack at virtual
* index = LUA_GLOBALSINDEX. We will set __index handler in global table's metatable to a custom C function to
Expand All @@ -1456,7 +1456,7 @@ void luaRegisterServerAPI(lua_State* lua) {
luaLoadLibraries(lua);

/* Before Redis OSS 7, Lua used to return error messages as strings from pcall function. With Valkey (or Redis OSS 7), Lua now returns
* error messages as tables. To keep backwards compatibility, we wrap the Lua pcall function with our own
* error messages as tables. To keep backwards compatibility, we wrap the Lua pcall function with our own
* implementation of C function that converts table to string. */
lua_pushcfunction(lua, luaRedisPcall);
lua_setglobal(lua, "pcall");
Expand All @@ -1479,9 +1479,9 @@ void luaRegisterServerAPI(lua_State* lua) {

/* Add server.setresp function to allow LUA scripts to change the RESP version for server.call and server.pcall
* invocations. */
lua_pushstring(lua,"setresp");
lua_pushcfunction(lua,luaSetResp);
lua_settable(lua,-3);
lua_pushstring(lua, "setresp");
lua_pushcfunction(lua, luaSetResp);
lua_settable(lua, -3);

/* Add server.sha1hex function. */
lua_pushstring(lua, "sha1hex");
Expand Down Expand Up @@ -1534,17 +1534,17 @@ void luaRegisterServerAPI(lua_State* lua) {
* This is not a deep copy but is enough for our purpose here. */
lua_getglobal(lua, SERVER_API_NAME);
lua_setglobal(lua, REDIS_API_NAME);

/* Replace math.random and math.randomseed with custom implementations. */
lua_getglobal(lua,"math");
lua_pushstring(lua,"random");
lua_pushcfunction(lua,server_math_random);
lua_settable(lua,-3);
lua_getglobal(lua, "math");
lua_pushstring(lua, "random");
lua_pushcfunction(lua, server_math_random);
lua_settable(lua, -3);
lua_pushstring(lua, "randomseed");
lua_pushcfunction(lua, server_math_randomseed);
lua_settable(lua,-3);
lua_settable(lua, -3);
/* overwrite value of global variable 'math' with this modified math table present on top of stack. */
lua_setglobal(lua,"math");
lua_setglobal(lua, "math");
}

/* Set an array of String Objects as a Lua array (table) stored into a
Expand All @@ -1570,14 +1570,14 @@ static void luaCreateArray(lua_State *lua, robj **elev, int elec) {
/* The following implementation is the one shipped with Lua itself but with
* rand() replaced by serverLrand48(). */
static int server_math_random(lua_State *L) {
/* the `%' avoids the (rare) case of r==1, and is needed also because on
some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
lua_Number r = (lua_Number)(serverLrand48()%SERVER_LRAND48_MAX) /
(lua_Number)SERVER_LRAND48_MAX;
switch (lua_gettop(L)) { /* check number of arguments */
case 0: { /* no arguments */
lua_pushnumber(L, r); /* Number between 0 and 1 */
break;
/* the `%' avoids the (rare) case of r==1, and is needed also because on
some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
lua_Number r = (lua_Number)(serverLrand48() % SERVER_LRAND48_MAX) /
(lua_Number)SERVER_LRAND48_MAX;
switch (lua_gettop(L)) { /* check number of arguments */
case 0: { /* no arguments */
lua_pushnumber(L, r); /* Number between 0 and 1 */
break;
}
case 1: { /* only upper limit */
int u = luaL_checkint(L, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/script_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ typedef struct errorInfo {
int ignore_err_stats_update;
} errorInfo;

void luaRegisterServerAPI(lua_State* lua);
void luaRegisterServerAPI(lua_State *lua);
sds luaGetStringSds(lua_State *lua, int index);
void luaRegisterGlobalProtectionFunction(lua_State *lua);
void luaSetErrorMetatable(lua_State *lua);
Expand Down
4 changes: 2 additions & 2 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2756,9 +2756,9 @@ void initServer(void) {
server.maxmemory_policy = MAXMEMORY_NO_EVICTION;
}

/* Initialize the LUA scripting engine. */
/* Initialize the LUA scripting engine. */
scriptingInit(1);
/* Initialize the functions engine based off of LUA initialization. */
/* Initialize the functions engine based off of LUA initialization. */
if (functionsInit() == C_ERR) {
serverPanic("Functions initialization failed, check the server logs.");
exit(1);
Expand Down

0 comments on commit 3a0f4a0

Please sign in to comment.