// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/scripting.c
// Function: luaRedisGenericCommand
int luaRedisGenericCommand(lua_State *lua, int raise_error) {
    int j, argc = lua_gettop(lua);
    struct redisCommand *cmd;
    redisClient *c = server.lua_client;
    sds reply;

    /* Cached across calls. */
    static robj **argv = NULL;
    static int argv_size = 0;
    static robj *cached_objects[LUA_CMD_OBJCACHE_SIZE];
    static int cached_objects_len[LUA_CMD_OBJCACHE_SIZE];

    /* Require at least one argument */
    // 命令参数检查（命令本身是 argv[0]）
    if (argc == 0) {
        luaPushError(lua,
            "Please specify at least one argument for redis.call()");
        return 1;
    }

    /* Build the arguments vector */
    // 构建参数数组
    if (!argv) {
        argv = zmalloc(sizeof(robj*)*argc);
    } else if (argv_size < argc) {
        argv = zrealloc(argv,sizeof(robj*)*argc);
        argv_size = argc;
    }

    for (j = 0; j < argc; j++) {
        char *obj_s;
        size_t obj_len;

        obj_s = (char*)lua_tolstring(lua,j+1,&obj_len);
        if (obj_s == NULL) break; /* Not a string. */

        /* Try to use a cached object. */
        if (cached_objects[j] && cached_objects_len[j] >= obj_len) {
            char *s = cached_objects[j]->ptr;
            struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));

            argv[j] = cached_objects[j];
            cached_objects[j] = NULL;
            memcpy(s,obj_s,obj_len+1);
            sh->free += sh->len - obj_len;
            sh->len = obj_len;
        } else {
            argv[j] = createStringObject(obj_s, obj_len);
        }
    }
    
    /* Check if one of the arguments passed by the Lua script
     * is not a string or an integer (lua_isstring() return true for
     * integers as well). 
     *
     * 对参数进行检查，确保它们只能是字符串或整数
     */
    if (j != argc) {
        j--;
        while (j >= 0) {
            decrRefCount(argv[j]);
            j--;
        }
        luaPushError(lua,
            "Lua redis() command arguments must be strings or integers");
        return 1;
    }

    /* Setup our fake client for command execution */
    // 设置参数和参数数量
    c->argv = argv;
    c->argc = argc;

    /* Command lookup */
    // 查找命令的实现函数
    cmd = lookupCommand(argv[0]->ptr);
    // 检查参数数量是否正确
    if (!cmd || ((cmd->arity > 0 && cmd->arity != argc) ||
                   (argc < -cmd->arity)))
    {
        if (cmd)
            luaPushError(lua,
                "Wrong number of args calling Redis command From Lua script");
        else
            luaPushError(lua,"Unknown Redis command called from Lua script");
        goto cleanup;
    }

    /* There are commands that are not allowed inside scripts. */
    // 阻止那些不能在脚本中执行的命令
    if (cmd->flags & REDIS_CMD_NOSCRIPT) {
        luaPushError(lua, "This Redis command is not allowed from scripts");
        goto cleanup;
    }

    /* Write commands are forbidden against read-only slaves, or if a
     * command marked as non-deterministic was already called in the context
     * of this script. 
     *
     * 写命令不能在只读附属节点中执行，
     * 也不能在已经有随机命令执行的情况下执行。
     */
    if (cmd->flags & REDIS_CMD_WRITE) {

        // 不能在已经执行过随机命令之后执行
        if (server.lua_random_dirty) {
            luaPushError(lua,
                "Write commands not allowed after non deterministic commands");
            goto cleanup;

        // 不能在只读节点中执行
        } else if (server.masterhost && server.repl_slave_ro &&
                   !server.loading &&
                   !(server.lua_caller->flags & REDIS_MASTER))
        {
            luaPushError(lua, shared.roslaveerr->ptr);
            goto cleanup;

        // 不能在已经有写入出错的情况下继续进行写入
        } else if (server.stop_writes_on_bgsave_err &&
                   server.saveparamslen > 0 &&
                   server.lastbgsave_status == REDIS_ERR)
        {
            luaPushError(lua, shared.bgsaveerr->ptr);
            goto cleanup;
        }
    }

    /* If we reached the memory limit configured via maxmemory, commands that
     * could enlarge the memory usage are not allowed, but only if this is the
     * first write in the context of this script, otherwise we can't stop
     * in the middle. 
     *
     * 如果脚本运行时 Redis 的内存到达了 maxmemory 的限制，
     * 并且之前没有执行过写操作，
     * 那么尝试释放内存，如果释放失败的话，就直接退出。
     */
    if (server.maxmemory && server.lua_write_dirty == 0 &&
        (cmd->flags & REDIS_CMD_DENYOOM))
    {
        // 尝试释放内存，如果失败的话，直接退出
        if (freeMemoryIfNeeded() == REDIS_ERR) {
            luaPushError(lua, shared.oomerr->ptr);
            goto cleanup;
        }
    }

    // 如果将要执行的是随机操作，那么设置 lua_random_dirty 状态
    if (cmd->flags & REDIS_CMD_RANDOM) server.lua_random_dirty = 1;
    // 如果将要执行的是写操作，那么设置 lua_write_dirty 状态
    if (cmd->flags & REDIS_CMD_WRITE) server.lua_write_dirty = 1;

    /* Run the command */
    // 执行命令
    c->cmd = cmd;
    call(c,REDIS_CALL_SLOWLOG | REDIS_CALL_STATS);

    /* Convert the result of the Redis command into a suitable Lua type.
     *
     * 将 Redis 命令的结果转换为适当的 Lua 类型。
     *
     * The first thing we need is to create a single string from the client
     * output buffers. 
     *
     * 首先要做的就是为客户端输出缓存中创建一个字符串
     */
    if (listLength(c->reply) == 0 && c->bufpos < REDIS_REPLY_CHUNK_BYTES) {
        /* This is a fast path for the common case of a reply inside the
         * client static buffer. Don't create an SDS string but just use
         * the client buffer directly. */
        c->buf[c->bufpos] = '\0';
        reply = c->buf;
        c->bufpos = 0;
    } else {
        reply = sdsnewlen(c->buf,c->bufpos);
        c->bufpos = 0;
        while(listLength(c->reply)) {
            robj *o = listNodeValue(listFirst(c->reply));
            reply = sdscatlen(reply,o->ptr,sdslen(o->ptr));
            listDelNode(c->reply,listFirst(c->reply));
        }
    }

    // 检测执行的命令是否出错
    if (raise_error && reply[0] != '-') raise_error = 0;

    // 将回复转换为 Lua 值，并将这些值推入 Lua 环境
    redisProtocolToLuaType(lua,reply);

    /* Sort the output array if needed, assuming it is a non-null multi bulk
     * reply as expected. 
     *
     * 如果输出是一个 multi bulk reply ，并且它不是一个 null multi bulk reply ，
     * 那么对它进行排序
     * (null multi bulk reply 的前缀为 *-1\r\n ，具体请参考协议文档）
     */
    if ((cmd->flags & REDIS_CMD_SORT_FOR_SCRIPT) &&
        (reply[0] == '*' && reply[1] != '-')) {
            // 排序
            luaSortArray(lua);
    }

    if (reply != c->buf) sdsfree(reply);
    c->reply_bytes = 0;

cleanup:
    /* Clean up. Command code may have changed argv/argc so we use the
     * argv/argc of the client instead of the local variables. */
    for (j = 0; j < c->argc; j++) {
        robj *o = c->argv[j];

        /* Try to cache the object in the cached_objects array.
         * The object must be small, SDS-encoded, and with refcount = 1
         * (we must be the only owner) for us to cache it. */
        if (j < LUA_CMD_OBJCACHE_SIZE &&
            o->refcount == 1 &&
            (o->encoding == REDIS_ENCODING_RAW ||
             o->encoding == REDIS_ENCODING_EMBSTR) &&
            sdslen(o->ptr) <= LUA_CMD_OBJCACHE_MAX_LEN)
        {
            struct sdshdr *sh = (void*)(((char*)(o->ptr))-(sizeof(struct sdshdr)));

            if (cached_objects[j]) decrRefCount(cached_objects[j]);
            cached_objects[j] = o;
            cached_objects_len[j] = sh->free + sh->len;
        } else {
            decrRefCount(o);
        }
    }

    if (c->argv != argv) {
        zfree(c->argv);
        argv = NULL;
    }

    // 返回错误
    if (raise_error) {
        /* If we are here we should have an error in the stack, in the
         * form of a table with an "err" field. Extract the string to
         * return the plain error. */
        lua_pushstring(lua,"err");
        lua_gettable(lua,-2);
        return lua_error(lua);
    }

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %ebx
movq %rdi, %r15
callq 0x60737
testl %eax, %eax
je 0x52523
movl %eax, %r14d
leaq 0x74acb(%rip), %rax     # 0xc6fa8
movq 0x7e8(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x89160(%rip), %rdi     # 0xdb650
testq %rdi, %rdi
movl %ebx, 0x8(%rsp)
movq %r15, 0x10(%rsp)
je 0x52532
cmpl %r14d, 0x89153(%rip)    # 0xdb658
jge 0x52545
movl %r14d, %esi
shlq $0x3, %rsi
callq 0x28337
movq %rax, 0x89136(%rip)     # 0xdb650
movl %r14d, 0x89137(%rip)    # 0xdb658
jmp 0x52545
leaq 0x41105(%rip), %rsi     # 0x9362f
movq %r15, %rdi
jmp 0x52684
movslq %r14d, %rdi
shlq $0x3, %rdi
callq 0x2828d
movq %rax, 0x8910b(%rip)     # 0xdb650
xorl %ebp, %ebp
testl %r14d, %r14d
movl $0x0, %ebx
movl %r14d, 0xc(%rsp)
cmovgl %r14d, %ebx
leaq 0x89201(%rip), %r12     # 0xdb760
leaq 0x890fa(%rip), %r14     # 0xdb660
leaq 0x20(%rsp), %r15
cmpq %rbp, %rbx
je 0x525fc
leal 0x1(%rbp), %esi
movq 0x10(%rsp), %rdi
movq %r15, %rdx
callq 0x60bad
testq %rax, %rax
je 0x525fa
movq (%r14), %rcx
movq 0x20(%rsp), %rdx
testq %rcx, %rcx
je 0x5259f
movslq (%r12), %rsi
cmpq %rsi, %rdx
jbe 0x525b7
movq %rax, %rdi
movq %rdx, %rsi
callq 0x2ea81
movq 0x8909f(%rip), %rcx     # 0xdb650
movq %rax, (%rcx,%rbp,8)
jmp 0x525ea
movq 0x8(%rcx), %r13
movq 0x8908e(%rip), %rsi     # 0xdb650
movq %rcx, (%rsi,%rbp,8)
andq $0x0, (%r14)
incq %rdx
movq %r13, %rdi
movq %rax, %rsi
callq 0x1fbd0
movl -0x8(%r13), %eax
movl 0x20(%rsp), %ecx
subl %ecx, %eax
addl %eax, -0x4(%r13)
movl %ecx, -0x8(%r13)
addq $0x4, %r12
addq $0x8, %r14
incq %rbp
jmp 0x5256b
movl %ebp, %ebx
movl 0xc(%rsp), %r12d
cmpl %r12d, %ebx
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %r14
jne 0x5265d
movq 0x89039(%rip), %rax     # 0xdb650
movq %rax, 0x38(%r14)
movl %r12d, 0x30(%r14)
movq (%rax), %rax
movq 0x8(%rax), %rdi
callq 0x24bcc
testq %rax, %rax
je 0x5269b
movq %rax, %r15
movl 0x10(%rax), %eax
testl %eax, %eax
setg %cl
cmpl %r12d, %eax
setne %dl
andb %cl, %dl
negl %eax
cmpl %eax, %r12d
setl %al
orb %dl, %al
cmpb $0x1, %al
jne 0x526a7
leaq 0x41047(%rip), %rsi     # 0x9369f
jmp 0x528b1
movl %ebx, %ebx
testq %rbx, %rbx
jle 0x5267a
movq 0x88fe5(%rip), %rax     # 0xdb650
movq -0x8(%rax,%rbx,8), %rdi
decq %rbx
callq 0x2ee1f
jmp 0x5265f
leaq 0x40fe4(%rip), %rsi     # 0x93665
movq %rbp, %rdi
callq 0x52360
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x41038(%rip), %rsi     # 0x936da
jmp 0x528b1
movl 0x20(%r15), %eax
testb $0x40, %al
jne 0x526f0
testb $0x1, %al
leaq 0x748f0(%rip), %rdx     # 0xc6fa8
jne 0x526fc
testb $0x4, %al
je 0x52715
cmpq $0x0, 0x720(%rdx)
je 0x52715
cmpl $0x0, 0x810(%rdx)
movl 0x8(%rsp), %ebx
jne 0x52719
callq 0x25411
cmpl $-0x1, %eax
je 0x5289f
movl 0x20(%r15), %eax
leaq 0x748ba(%rip), %rdx     # 0xc6fa8
jmp 0x52719
leaq 0x41010(%rip), %rsi     # 0x93707
jmp 0x528b1
cmpl $0x0, 0x814(%rdx)
je 0x527df
leaq 0x41026(%rip), %rsi     # 0x93736
jmp 0x528b1
movl 0x8(%rsp), %ebx
testb %al, %al
jns 0x52727
movl $0x1, 0x814(%rdx)
testb $0x1, %al
je 0x52735
movl $0x1, 0x810(%rdx)
movq %r15, 0x40(%r14)
pushq $0x3
popq %rsi
movq %r14, %rdi
callq 0x24c94
movq 0x60(%r14), %rax
cmpq $0x0, 0x28(%rax)
sete %al
movl 0x170(%r14), %ecx
movslq %ecx, %rsi
cmpl $0x4000, %esi           # imm = 0x4000
setl %cl
andb %al, %cl
leaq 0x174(%r14), %r12
cmpb $0x1, %cl
jne 0x52799
movb $0x0, 0x174(%r14,%rsi)
andl $0x0, 0x170(%r14)
testl %ebx, %ebx
je 0x5281e
xorl %eax, %eax
cmpb $0x2d, (%r12)
cmovnel %eax, %ebx
jmp 0x52820
movq %r12, %rdi
callq 0x27158
movq %rax, %r12
andl $0x0, 0x170(%r14)
movq 0x60(%r14), %rax
cmpq $0x0, 0x28(%rax)
je 0x52782
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movslq -0x8(%rsi), %rdx
movq %r12, %rdi
callq 0x2736d
movq %rax, %r12
movq 0x60(%r14), %rdi
movq (%rdi), %rsi
callq 0x1ff28
jmp 0x527ac
cmpq $0x0, 0x650(%rdx)
je 0x52868
cmpl $0x0, 0x6ac(%rdx)
je 0x52868
cmpl $0x0, 0x2f0(%rdx)
jne 0x52868
movq 0x7f0(%rdx), %rcx
testb $0x2, 0x90(%rcx)
jne 0x52868
leaq 0x74fee(%rip), %rax     # 0xc7800
movq 0xc0(%rax), %rax
jmp 0x528ad
xorl %ebx, %ebx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x520c8
testb $0x1, 0x21(%r15)
je 0x52849
cmpb $0x2a, (%r12)
jne 0x52849
cmpb $0x2d, 0x1(%r12)
je 0x52849
movq %rbp, %rdi
callq 0x52405
movl %ebx, 0x8(%rsp)
leaq 0x174(%r14), %rax
cmpq %rax, %r12
je 0x52861
movq %r12, %rdi
callq 0x271f7
andq $0x0, 0x68(%r14)
jmp 0x528b9
cmpl $0x0, 0x5bc(%rdx)
je 0x526ba
cmpl $0x0, 0x580(%rdx)
jle 0x526ba
cmpl $-0x1, 0x5b8(%rdx)
jne 0x526ba
leaq 0x74f6a(%rip), %rax     # 0xc7800
movq 0xb0(%rax), %rax
jmp 0x528ad
leaq 0x74f5a(%rip), %rax     # 0xc7800
movq 0xe8(%rax), %rax
movq 0x8(%rax), %rsi
movq %rbp, %rdi
callq 0x52360
leaq 0x88ea0(%rip), %rbx     # 0xdb760
leaq 0x88d99(%rip), %r14     # 0xdb660
xorl %r12d, %r12d
movq 0x18(%rsp), %rcx
movslq 0x30(%rcx), %rax
movq 0x38(%rcx), %rdi
cmpq %rax, %r12
jge 0x52935
movq (%rdi,%r12,8), %r15
cmpq $0x1f, %r12
ja 0x52920
cmpl $0x1, 0x4(%r15)
jne 0x52920
movl (%r15), %eax
shrl $0x4, %eax
testb $0x7, %al
jne 0x52920
movq 0x8(%r15), %r13
movl -0x8(%r13), %eax
cmpl $0x40, %eax
ja 0x52920
movq (%r14), %rdi
testq %rdi, %rdi
je 0x52915
callq 0x2ee1f
movl -0x8(%r13), %eax
movq %r15, (%r14)
addl -0x4(%r13), %eax
movl %eax, (%rbx)
jmp 0x52928
movq %r15, %rdi
callq 0x2ee1f
incq %r12
addq $0x4, %rbx
addq $0x8, %r14
jmp 0x528ca
cmpq 0x88d14(%rip), %rdi     # 0xdb650
je 0x5294b
callq 0x283cd
andq $0x0, 0x88d05(%rip)     # 0xdb650
cmpl $0x0, 0x8(%rsp)
je 0x52689
leaq 0x38fd0(%rip), %rsi     # 0x8b92d
movq %rbp, %rdi
callq 0x60db8
pushq $-0x2
popq %rsi
movq %rbp, %rdi
callq 0x60fce
movq %rbp, %rdi
callq 0x616b3
jmp 0x5268c