// File: /workspace/github_c_makefile_star2_O2/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 0x271bf
leaq 0x4068f(%rip), %rsi     # 0x93cf6
movq %rax, %rdi
callq 0x273bf
pushq $0x2a
popq %rdx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2736d
leaq 0x4067c(%rip), %rsi     # 0x93d00
pushq $0x3
popq %rdx
movq %rax, %rdi
callq 0x2736d
movq 0x8(%r15), %rsi
movslq -0x8(%rsi), %rdx
movq %rax, %rdi
callq 0x2736d
leaq 0x43e54(%rip), %rsi     # 0x974fa
pushq $0x4
popq %rdx
movq %rax, %rdi
callq 0x2736d
movq %rax, %r13
movslq -0x8(%rax), %rdx
leaq 0x40645(%rip), %rcx     # 0x93d04
movq %r14, %rdi
movq %rax, %rsi
callq 0x6b810
testl %eax, %eax
je 0x53709
pushq $-0x1
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x60bad
leaq 0x4062d(%rip), %rsi     # 0x93d11
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2c301
pushq $-0x2
popq %rsi
movq %r14, %rdi
callq 0x60744
movq %r13, %rdi
callq 0x271f7
jmp 0x5378c
movq %r13, %rdi
callq 0x271f7
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x61434
testl %eax, %eax
je 0x53753
pushq $-0x1
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x60bad
leaq 0x40603(%rip), %rsi     # 0x93d3c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2c301
pushq $-0x2
popq %rsi
movq %r14, %rdi
callq 0x60744
jmp 0x5378c
leaq 0x7384e(%rip), %rax     # 0xc6fa8
movq 0x7f8(%rax), %r14
addq $0x2, %r12
pushq $0x28
popq %rsi
movq %r12, %rdi
callq 0x27158
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x21956
testl %eax, %eax
jne 0x5379d
movq %r15, %rdi
callq 0x2eacc
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3b076(%rip), %rdx     # 0x8e81a
leaq 0x405ba(%rip), %rcx     # 0x93d65
movq %rbx, %rdi
xorl %esi, %esi
movl $0x450, %r8d            # imm = 0x450
callq 0x4980c
pushq $0x1
popq %rdi
callq 0x1f3b0