// File: /workspace/github_c_makefile_star2_O1/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 0x281b2
leaq 0x45aef(%rip), %rsi     # 0x9dcf6
movq %rax, %rdi
callq 0x283d7
movl $0x2a, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x28380
leaq 0x45ada(%rip), %rsi     # 0x9dd00
movl $0x3, %edx
movq %rax, %rdi
callq 0x28380
movq 0x8(%r15), %rsi
movslq -0x8(%rsi), %rdx
movq %rax, %rdi
callq 0x28380
leaq 0x49290(%rip), %rsi     # 0xa14da
movl $0x4, %edx
movq %rax, %rdi
callq 0x28380
movq %rax, %r13
movslq -0x8(%rax), %rdx
leaq 0x45a9f(%rip), %rcx     # 0x9dd04
movq %r14, %rdi
movq %rax, %rsi
callq 0x71cc8
testl %eax, %eax
je 0x582b6
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x6652e
leaq 0x45a82(%rip), %rsi     # 0x9dd11
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2d9fe
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x660a2
movq %r13, %rdi
callq 0x28204
jmp 0x58342
movq %r13, %rdi
callq 0x28204
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x66dd8
testl %eax, %eax
je 0x58307
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x6652e
leaq 0x45a51(%rip), %rsi     # 0x9dd3c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2d9fe
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x660a2
jmp 0x58342
leaq 0x71c9a(%rip), %rax     # 0xc9fa8
movq 0x7f8(%rax), %r14
addq $0x2, %r12
movl $0x28, %esi
movq %r12, %rdi
callq 0x28148
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x222b6
testl %eax, %eax
jne 0x58353
movq %r15, %rdi
callq 0x3041b
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4048c(%rip), %rdx     # 0x987e6
leaq 0x45a04(%rip), %rcx     # 0x9dd65
movq %rbx, %rdi
xorl %esi, %esi
movl $0x450, %r8d            # imm = 0x450
callq 0x4d872
movl $0x1, %edi
callq 0x1f3b0