// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movl %esi, %ebp
movq %rdi, %rbx
callq 0x66095
testl %eax, %eax
je 0x56f63
movl %eax, %r15d
leaq 0x73086(%rip), %rax     # 0xc9fa8
movq 0x7e8(%rax), %rax
movq 0x87720(%rip), %rdi     # 0xde650
testq %rdi, %rdi
movl %ebp, 0xc(%rsp)
movq %rax, 0x38(%rsp)
je 0x56f72
cmpl %r15d, 0x87713(%rip)    # 0xde658
jge 0x56f85
movl %r15d, %esi
shlq $0x3, %rsi
callq 0x297b5
movq %rax, 0x876f6(%rip)     # 0xde650
movl %r15d, 0x876f7(%rip)    # 0xde658
jmp 0x56f85
leaq 0x466c5(%rip), %rsi     # 0x9d62f
movq %rbx, %rdi
jmp 0x57114
movslq %r15d, %rdi
shlq $0x3, %rdi
callq 0x29709
movq %rax, 0x876cb(%rip)     # 0xde650
xorl %r14d, %r14d
testl %r15d, %r15d
movl $0x0, %eax
movl %r15d, 0x24(%rsp)
cmovgl %r15d, %eax
movq %rax, 0x30(%rsp)
leaq (,%rax,4), %rax
movq %rax, 0x40(%rsp)
leaq 0x876ae(%rip), %r13     # 0xde660
movl $0x1, %r15d
leaq 0x877a1(%rip), %rax     # 0xde760
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
movq %rbx, 0x10(%rsp)
cmpq %r14, 0x40(%rsp)
je 0x57080
movq %rbx, %rdi
movl %r15d, %esi
leaq 0x18(%rsp), %rdx
callq 0x6652e
testq %rax, %rax
je 0x57087
movq %rax, %rbp
movq (%r13), %rax
testq %rax, %rax
je 0x57011
leaq 0x8775d(%rip), %rcx     # 0xde760
movslq (%r14,%rcx), %rcx
movq 0x18(%rsp), %rdx
cmpq %rcx, %rdx
jbe 0x5702b
movq 0x18(%rsp), %rsi
movq %rbp, %rdi
callq 0x303b7
movq 0x8762b(%rip), %rcx     # 0xde650
movq %rax, (%rcx,%r14,2)
jmp 0x57064
movq 0x8(%rax), %rbx
movq 0x8761a(%rip), %rcx     # 0xde650
movq %rax, (%rcx,%r14,2)
movq $0x0, (%r13)
incq %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1fbd0
movl -0x8(%rbx), %eax
movl 0x18(%rsp), %ecx
subl %ecx, %eax
addl %eax, -0x4(%rbx)
movl %ecx, -0x8(%rbx)
movq 0x10(%rsp), %rbx
decl %r12d
addq $0x4, %r14
addq $0x8, %r13
incl %r15d
testq %rbp, %rbp
jne 0x56fcc
notl %r12d
jmp 0x5708a
movq 0x30(%rsp), %rax
jmp 0x5708d
negl %r12d
movl %r12d, %eax
movl 0x24(%rsp), %ebp
cmpl %ebp, %eax
movq 0x38(%rsp), %r13
jne 0x570e5
movq 0x875af(%rip), %rax     # 0xde650
movq %rax, 0x38(%r13)
movl %ebp, 0x30(%r13)
movq (%rax), %rax
movq 0x8(%rax), %rdi
callq 0x25a1e
testq %rax, %rax
je 0x5712d
movq %rax, %r15
movl 0x10(%rax), %eax
testl %eax, %eax
setg %cl
cmpl %ebp, %eax
setne %dl
andb %cl, %dl
negl %eax
cmpl %eax, %ebp
setl %al
orb %dl, %al
cmpb $0x1, %al
jne 0x57139
leaq 0x465bf(%rip), %rsi     # 0x9d69f
jmp 0x57353
testl %eax, %eax
je 0x57108
movl %eax, %ebx
incq %rbx
movq 0x8755b(%rip), %rax     # 0xde650
movq -0x10(%rax,%rbx,8), %rdi
callq 0x30834
decq %rbx
cmpq $0x1, %rbx
jg 0x570ee
leaq 0x46556(%rip), %rsi     # 0x9d665
movq 0x10(%rsp), %rdi
callq 0x56d99
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x465a6(%rip), %rsi     # 0x9d6da
jmp 0x57353
movl 0x20(%r15), %eax
testb $0x40, %al
jne 0x57203
testb $0x1, %al
leaq 0x72e5a(%rip), %rdx     # 0xc9fa8
jne 0x5720f
testb $0x4, %al
je 0x57180
cmpq $0x0, 0x720(%rdx)
je 0x57180
cmpl $0x0, 0x810(%rdx)
jne 0x57180
callq 0x262cb
leaq 0x72e31(%rip), %rdx     # 0xc9fa8
cmpl $-0x1, %eax
je 0x57341
movl 0x20(%r15), %eax
testb %al, %al
jns 0x57192
movl $0x1, 0x814(%rdx)
testb $0x1, %al
je 0x571a0
movl $0x1, 0x810(%rdx)
movq %r15, 0x40(%r13)
movq %r13, %rdi
movl $0x3, %esi
callq 0x25b37
movq 0x60(%r13), %rax
cmpq $0x0, 0x28(%rax)
jne 0x57224
movslq 0x170(%r13), %rax
cmpq $0x3fff, %rax           # imm = 0x3FFF
jg 0x57224
leaq 0x174(%r13), %rbx
movb $0x0, 0x174(%r13,%rax)
movl $0x0, 0x170(%r13)
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
je 0x572bb
xorl %eax, %eax
cmpb $0x2d, (%rbx)
cmovnel %eax, %ecx
movl %ecx, 0xc(%rsp)
jmp 0x572c3
leaq 0x464fd(%rip), %rsi     # 0x9d707
jmp 0x57353
cmpl $0x0, 0x814(%rdx)
je 0x57278
leaq 0x46517(%rip), %rsi     # 0x9d736
jmp 0x57353
leaq 0x174(%r13), %rdi
movslq 0x170(%r13), %rsi
callq 0x28148
movq %rax, %rbx
movl $0x0, 0x170(%r13)
movq 0x60(%r13), %rax
cmpq $0x0, 0x28(%rax)
je 0x571e6
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movslq -0x8(%rsi), %rdx
movq %rbx, %rdi
callq 0x28380
movq %rax, %rbx
movq 0x60(%r13), %rdi
movq (%rdi), %rsi
callq 0x20544
jmp 0x57245
cmpq $0x0, 0x650(%rdx)
je 0x5730a
cmpl $0x0, 0x6ac(%rdx)
je 0x5730a
cmpl $0x0, 0x2f0(%rdx)
jne 0x5730a
movq 0x7f0(%rdx), %rcx
testb $0x2, 0x90(%rcx)
jne 0x5730a
leaq 0x73551(%rip), %rax     # 0xca800
movq 0xc0(%rax), %rax
jmp 0x5734f
movl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x56ae4
testb $0x1, 0x21(%r15)
je 0x572ec
cmpb $0x2a, (%rbx)
jne 0x572ec
cmpb $0x2d, 0x1(%rbx)
je 0x572ec
movq 0x10(%rsp), %rdi
callq 0x56e42
leaq 0x174(%r13), %rax
cmpq %rax, %rbx
je 0x57300
movq %rbx, %rdi
callq 0x28204
movq $0x0, 0x68(%r13)
jmp 0x5735b
cmpl $0x0, 0x5bc(%rdx)
je 0x57154
cmpl $0x0, 0x580(%rdx)
jle 0x57154
cmpl $-0x1, 0x5b8(%rdx)
jne 0x57154
leaq 0x734c8(%rip), %rax     # 0xca800
movq 0xb0(%rax), %rax
jmp 0x5734f
leaq 0x734b8(%rip), %rax     # 0xca800
movq 0xe8(%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x56d99
cmpl $0x0, 0x30(%r13)
jle 0x573d8
leaq 0x872f7(%rip), %rbx     # 0xde660
xorl %r14d, %r14d
movq 0x38(%r13), %rax
movq (%rax,%r14,8), %r15
cmpq $0x1f, %r14
ja 0x573ba
cmpl $0x1, 0x4(%r15)
jne 0x573ba
movl (%r15), %eax
shrl $0x4, %eax
testb $0x7, %al
jne 0x573ba
movq 0x8(%r15), %r12
cmpl $0x40, -0x8(%r12)
ja 0x573ba
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x573a4
callq 0x30834
movq %r15, (%rbx)
movl -0x8(%r12), %eax
addl -0x4(%r12), %eax
movq 0x28(%rsp), %rcx
movl %eax, (%rcx)
jmp 0x573c2
movq %r15, %rdi
callq 0x30834
incq %r14
movslq 0x30(%r13), %rax
addq $0x4, 0x28(%rsp)
addq $0x8, %rbx
cmpq %rax, %r14
jl 0x5736c
movq 0x38(%r13), %rdi
cmpq 0x8726d(%rip), %rdi     # 0xde650
je 0x573f5
callq 0x2984b
movq $0x0, 0x8725b(%rip) # 0xde650
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rbx
je 0x57119
leaq 0x3e511(%rip), %rsi     # 0x9591d
movq %rbx, %rdi
callq 0x66757
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x6696e
movq %rbx, %rdi
callq 0x6704e
jmp 0x5711e