// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/scripting.c
// Function: luaCreateFunction
int luaCreateFunction(redisClient *c, lua_State *lua, char *funcname, robj *body) {
    sds funcdef = sdsempty();

    // 组成函数定义
    // 例子：
    // function sha1_function_name ()
    //     body
    // end
    funcdef = sdscat(funcdef,"function ");
    funcdef = sdscatlen(funcdef,funcname,42);
    funcdef = sdscatlen(funcdef,"() ",3);
    funcdef = sdscatlen(funcdef,body->ptr,sdslen(body->ptr));
    funcdef = sdscatlen(funcdef," end",4);

    // 将函数定义载入到 Lua 中（并编译），但并不执行该定义
    if (luaL_loadbuffer(lua,funcdef,sdslen(funcdef),"@user_script")) {

        // 如果编译出错，那么返回错误
        addReplyErrorFormat(c,"Error compiling script (new function): %s\n",
            lua_tostring(lua,-1));
        lua_pop(lua,1);
        sdsfree(funcdef);

        return REDIS_ERR;
    }

    // 释放函数定义内容
    sdsfree(funcdef);

    // 定义函数
    if (lua_pcall(lua,0,0,0)) {
        addReplyErrorFormat(c,"Error running script (new function): %s\n",
            lua_tostring(lua,-1));
        lua_pop(lua,1);
        return REDIS_ERR;
    }

    /* We also save a SHA1 -> Original script map in a dictionary
     * so that we can replicate / write in the AOF all the
     * EVALSHA commands as EVAL using the original script. 
     *
     * 以 SHA1 值为键，脚本原始内容为值，映射到 server.lua_scripts 字典
     *
     * 对脚本进行复制，或者写入到 AOF 文件时使用
     */
    {
        int retval = dictAdd(server.lua_scripts,
                             // SHA1 值，不包括前缀 f_
                             // body 为代码体
                             sdsnewlen(funcname+2,40),body);
        redisAssertWithInfo(c,NULL,retval == DICT_OK);
        incrRefCount(body);
    }

    return REDIS_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
callq 0x2845e
leaq 0x460e7(%rip), %rsi     # 0x9dd36
movq %rax, %rdi
callq 0x286db
movl $0x2a, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x28689
leaq 0x460d2(%rip), %rsi     # 0x9dd40
movl $0x3, %edx
movq %rax, %rdi
callq 0x28689
movq 0x8(%r15), %rsi
movslq -0x8(%rsi), %rdx
movq %rax, %rdi
callq 0x28689
leaq 0x498d8(%rip), %rsi     # 0xa156a
movl $0x4, %edx
movq %rax, %rdi
callq 0x28689
movq %rax, %r13
movslq -0x8(%rax), %rdx
leaq 0x46097(%rip), %rcx     # 0x9dd44
movq %r14, %rdi
movq %rax, %rsi
callq 0x71df0
testl %eax, %eax
je 0x57cfe
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x6608c
leaq 0x4607a(%rip), %rsi     # 0x9dd51
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2dc8a
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x65aed
movq %r13, %rdi
callq 0x2850d
jmp 0x57d8a
movq %r13, %rdi
callq 0x2850d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x66941
testl %eax, %eax
je 0x57d4f
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x6608c
leaq 0x46049(%rip), %rsi     # 0x9dd7c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2dc8a
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x65aed
jmp 0x57d8a
leaq 0x72252(%rip), %rax     # 0xc9fa8
movq 0x7f8(%rax), %r14
addq $0x2, %r12
movl $0x28, %esi
movq %r12, %rdi
callq 0x283f4
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x2249c
testl %eax, %eax
jne 0x57d9b
movq %r15, %rdi
callq 0x306f3
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40ab4(%rip), %rdx     # 0x98856
leaq 0x45ffc(%rip), %rcx     # 0x9dda5
movq %rbx, %rdi
xorl %esi, %esi
movl $0x450, %r8d            # imm = 0x450
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0