// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
callq 0x2c0c0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x73122(%rip), %rsi     # 0xe7d7e
callq 0x2c5b0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x2a, %edx
callq 0x2c500
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x73102(%rip), %rsi     # 0xe7d88
movl $0x3, %edx
callq 0x2c500
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72b70
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq %rax, %rdx
callq 0x2c500
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x76a06(%rip), %rsi     # 0xeb6da
movl $0x4, %edx
callq 0x2c500
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
callq 0x72b70
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %rax, %rdx
leaq 0x7307f(%rip), %rcx     # 0xe7d8c
callq 0xa0a80
cmpl $0x0, %eax
je 0x74d69
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %edx
callq 0x8c0e0
movq -0x60(%rbp), %rdi
movq %rax, %rdx
leaq 0x7305a(%rip), %rsi     # 0xe7d99
movb $0x0, %al
callq 0x35d80
movq -0x18(%rbp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x8b6f0
movq -0x30(%rbp), %rdi
callq 0x2c160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x74e46
movq -0x30(%rbp), %rdi
callq 0x2c160
movq -0x18(%rbp), %rdi
xorl %ecx, %ecx
movl %ecx, %esi
movl %ecx, %edx
callq 0x8d340
cmpl $0x0, %eax
je 0x74dcc
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %edx
callq 0x8c0e0
movq -0x68(%rbp), %rdi
movq %rax, %rdx
leaq 0x73016(%rip), %rsi     # 0xe7dc4
movb $0x0, %al
callq 0x35d80
movq -0x18(%rbp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x8b6f0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x74e46
leaq 0x9eb25(%rip), %rax     # 0x1138f8
movq 0x7f8(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rdi
addq $0x2, %rdi
movl $0x28, %esi
callq 0x2c000
movq -0x70(%rbp), %rdi
movq %rax, %rsi
movq -0x28(%rbp), %rdx
callq 0x236d0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x74e0b
jmp 0x74e36
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x6db08(%rip), %rdx     # 0xe2922
leaq 0x72fcc(%rip), %rcx     # 0xe7ded
movl $0x450, %r8d            # imm = 0x450
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x28(%rbp), %rdi
callq 0x3a3f0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nop