// File: /workspace/github_c_makefile_star2_O3/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, %rbp
callq 0x65ae0
testl %eax, %eax
je 0x569b6
movl %eax, %r15d
leaq 0x7362a(%rip), %rax     # 0xc9fa8
movq 0x7e8(%rax), %r14
movq 0x87cc4(%rip), %rdi     # 0xde650
testq %rdi, %rdi
je 0x569c2
cmpl %r15d, 0x87cc0(%rip)    # 0xde658
jge 0x569d5
movl %r15d, %esi
shlq $0x3, %rsi
callq 0x29a61
movq %rax, 0x87ca3(%rip)     # 0xde650
movl %r15d, 0x87ca4(%rip)    # 0xde658
jmp 0x569d5
leaq 0x46cb2(%rip), %rsi     # 0x9d66f
jmp 0x56b4c
movslq %r15d, %rdi
shlq $0x3, %rdi
callq 0x299b5
movq %rax, 0x87c7b(%rip)     # 0xde650
testl %r15d, %r15d
jle 0x56b45
movq %r14, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movl %r15d, 0x14(%rsp)
movl %r15d, %ebx
negq %rbx
movl $0x1, %r15d
leaq 0x87d61(%rip), %r14     # 0xde760
leaq 0x87c5a(%rip), %r12     # 0xde660
leaq 0x18(%rsp), %r13
movq %rbx, 0x20(%rsp)
movq %rbp, %rdi
movl %r15d, %esi
movq %r13, %rdx
callq 0x6608c
testq %rax, %rax
je 0x56aba
movq (%r12), %rcx
movq 0x18(%rsp), %rdx
testq %rcx, %rcx
je 0x56a3d
movslq (%r14), %rsi
cmpq %rsi, %rdx
jbe 0x56a56
movq %rax, %rdi
movq %rdx, %rsi
callq 0x3068f
movq 0x87c01(%rip), %rcx     # 0xde650
movq %rax, -0x8(%rcx,%r15,8)
jmp 0x56a9c
movq %r13, %rbx
movq %rbp, %r13
movq 0x8(%rcx), %rbp
movq 0x87be9(%rip), %rsi     # 0xde650
movq %rcx, -0x8(%rsi,%r15,8)
movq $0x0, (%r12)
incq %rdx
movq %rbp, %rdi
movq %rax, %rsi
callq 0x1fbd0
movl -0x8(%rbp), %eax
movl 0x18(%rsp), %ecx
subl %ecx, %eax
addl %eax, -0x4(%rbp)
movl %ecx, -0x8(%rbp)
movq %r13, %rbp
movq %rbx, %r13
movq 0x20(%rsp), %rbx
addq $0x4, %r14
addq $0x8, %r12
leaq (%rbx,%r15), %rax
incq %rax
incq %r15
cmpq $0x1, %rax
jne 0x56a10
jmp 0x56ac6
movl %r15d, %eax
subl 0x14(%rsp), %eax
cmpl $0x1, %eax
jne 0x56b25
movq 0x87b83(%rip), %rax     # 0xde650
movq 0x8(%rsp), %rcx
movq %rax, 0x38(%rcx)
movl 0x14(%rsp), %ebx
movl %ebx, 0x30(%rcx)
movq (%rax), %rax
movq 0x8(%rax), %rdi
callq 0x25c20
testq %rax, %rax
je 0x56b19
movq %rax, %r15
movl 0x10(%rax), %eax
testl %eax, %eax
setg %cl
cmpl %ebx, %eax
setne %dl
andb %cl, %dl
negl %eax
cmpl %eax, %ebx
setl %al
orb %dl, %al
cmpb $0x1, %al
jne 0x56b68
leaq 0x46bcb(%rip), %rsi     # 0x9d6df
jmp 0x56d91
leaq 0x46bfa(%rip), %rsi     # 0x9d71a
jmp 0x56d91
cmpq $0x1, %r15
je 0x56b45
movq 0x87b1e(%rip), %rax     # 0xde650
movq -0x10(%rax,%r15,8), %rdi
callq 0x30b0c
decq %r15
cmpq $0x1, %r15
ja 0x56b2b
leaq 0x46b59(%rip), %rsi     # 0x9d6a5
movq %rbp, %rdi
callq 0x567f5
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x20(%r15), %eax
testb $0x40, %al
jne 0x56c1e
testb $0x1, %al
leaq 0x7342b(%rip), %rbx     # 0xc9fa8
jne 0x56c2a
testb $0x4, %al
je 0x56bac
cmpq $0x0, 0x720(%rbx)
je 0x56bac
cmpl $0x0, 0x810(%rbx)
jne 0x56bac
callq 0x264f7
cmpl $-0x1, %eax
je 0x56d7f
movl 0x20(%r15), %eax
testb %al, %al
jns 0x56bba
movl $0x1, 0x814(%rbx)
testb $0x1, %al
je 0x56bc8
movl $0x1, 0x810(%rbx)
movq 0x8(%rsp), %rbx
movq %r15, 0x40(%rbx)
movq %rbx, %rdi
movl $0x3, %esi
callq 0x25d39
movq 0x60(%rbx), %rax
cmpq $0x0, 0x28(%rax)
sete %al
movl 0x170(%rbx), %ecx
movslq %ecx, %rsi
cmpl $0x4000, %esi           # imm = 0x4000
setl %cl
andb %al, %cl
leaq 0x174(%rbx), %r12
cmpb $0x1, %cl
jne 0x56c3f
movb $0x0, 0x174(%rbx,%rsi)
movl $0x0, 0x170(%rbx)
jmp 0x56c95
leaq 0x46b22(%rip), %rsi     # 0x9d747
jmp 0x56d91
cmpl $0x0, 0x814(%rbx)
je 0x56cad
leaq 0x46b3c(%rip), %rsi     # 0x9d776
jmp 0x56d91
movq %r12, %rdi
callq 0x283f4
movq %rax, %r12
movl $0x0, 0x170(%rbx)
movq 0x60(%rbx), %rax
cmpq $0x0, 0x28(%rax)
je 0x56c95
movq 0x8(%rsp), %rbx
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movslq -0x8(%rsi), %rdx
movq %r12, %rdi
callq 0x28689
movq %rax, %r12
movq 0x60(%rbx), %rdi
movq (%rdi), %rsi
callq 0x205b1
movq 0x60(%rbx), %rax
cmpq $0x0, 0x28(%rax)
jne 0x56c64
movl 0x10(%rsp), %ecx
testl %ecx, %ecx
je 0x56cf4
xorl %eax, %eax
cmpb $0x2d, (%r12)
cmovnel %eax, %ecx
movl %ecx, 0x10(%rsp)
jmp 0x56cfc
cmpq $0x0, 0x650(%rbx)
je 0x56d48
cmpl $0x0, 0x6ac(%rbx)
je 0x56d48
cmpl $0x0, 0x2f0(%rbx)
jne 0x56d48
movq 0x7f0(%rbx), %rcx
testb $0x2, 0x90(%rcx)
jne 0x56d48
leaq 0x73b18(%rip), %rax     # 0xca800
movq 0xc0(%rax), %rax
jmp 0x56d8d
movl $0x0, 0x10(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x56540
testb $0x1, 0x21(%r15)
je 0x56d25
cmpb $0x2a, (%r12)
jne 0x56d25
cmpb $0x2d, 0x1(%r12)
je 0x56d25
movq %rbp, %rdi
callq 0x5689e
movq 0x8(%rsp), %r15
leaq 0x174(%r15), %rax
cmpq %rax, %r12
je 0x56d3e
movq %r12, %rdi
callq 0x2850d
movq $0x0, 0x68(%r15)
jmp 0x56d9e
cmpl $0x0, 0x5bc(%rbx)
je 0x56b83
cmpl $0x0, 0x580(%rbx)
jle 0x56b83
cmpl $-0x1, 0x5b8(%rbx)
jne 0x56b83
leaq 0x73a8a(%rip), %rax     # 0xca800
movq 0xb0(%rax), %rax
jmp 0x56d8d
leaq 0x73a7a(%rip), %rax     # 0xca800
movq 0xe8(%rax), %rax
movq 0x8(%rax), %rsi
movq %rbp, %rdi
callq 0x567f5
movq 0x8(%rsp), %r15
cmpl $0x0, 0x30(%r15)
jle 0x56e1f
leaq 0x879b4(%rip), %rbx     # 0xde760
leaq 0x878ad(%rip), %r14     # 0xde660
xorl %r12d, %r12d
movq 0x38(%r15), %rax
movq (%rax,%r12,8), %r15
cmpq $0x1f, %r12
ja 0x56dfe
cmpl $0x1, 0x4(%r15)
jne 0x56dfe
movl (%r15), %eax
shrl $0x4, %eax
testb $0x7, %al
jne 0x56dfe
movq 0x8(%r15), %r13
movl -0x8(%r13), %eax
cmpl $0x40, %eax
ja 0x56dfe
movq (%r14), %rdi
testq %rdi, %rdi
je 0x56df3
callq 0x30b0c
movl -0x8(%r13), %eax
movq %r15, (%r14)
addl -0x4(%r13), %eax
movl %eax, (%rbx)
jmp 0x56e06
movq %r15, %rdi
callq 0x30b0c
incq %r12
movq 0x8(%rsp), %r15
movslq 0x30(%r15), %rax
addq $0x4, %rbx
addq $0x8, %r14
cmpq %rax, %r12
jl 0x56db6
movq 0x38(%r15), %rdi
cmpq 0x87826(%rip), %rdi     # 0xde650
je 0x56e3c
callq 0x29af7
movq $0x0, 0x87814(%rip) # 0xde650
cmpl $0x0, 0x10(%rsp)
je 0x56b54
leaq 0x3eb1f(%rip), %rsi     # 0x9596d
movq %rbp, %rdi
callq 0x662b5
movq %rbp, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x664d3
movq %rbp, %rdi
callq 0x66bbd
jmp 0x56b59