// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/scripting.c
// Function: scriptingInit
void scriptingInit(void) {

    // 创建一个新的 lua 环境
    lua_State *lua = lua_open();

    // 载入内置函数库
    luaLoadLibraries(lua);

    // 屏蔽所有不想暴露给脚本环境的函数（目前只有 loadfile）
    luaRemoveUnsupportedFunctions(lua);

    /* Initialize a dictionary we use to map SHAs to scripts.
     *
     * 创建一个字典，用于将 SHA 校验码映射到脚本
     *
     * This is useful for replication, as we need to replicate EVALSHA
     * as EVAL, so we need to remember the associated script. 
     *
     * 因为 Redis 目前复制脚本的方法是将 EVALSHA 转换为 EVAL 来进行的，
     * 所以程序需要保存和 SHA 校验值相对应的脚本。
     */
    server.lua_scripts = dictCreate(&shaScriptObjectDictType,NULL);

    /* Register the redis commands table and fields */
    // 创建 lua 表，并以给定名称将 C 函数注册到表格上
    lua_newtable(lua);

    /* redis.call */
    /* 注册 redis.call */
    lua_pushstring(lua,"call");
    lua_pushcfunction(lua,luaRedisCallCommand);
    lua_settable(lua,-3);

    /* redis.pcall */
    /* 注册 redis.pcall */
    lua_pushstring(lua,"pcall");
    lua_pushcfunction(lua,luaRedisPCallCommand);
    lua_settable(lua,-3);

    /* redis.log and log levels. */
    /* 注册 redis.log 和 log levels. */
    lua_pushstring(lua,"log");
    lua_pushcfunction(lua,luaLogCommand);
    lua_settable(lua,-3);

    lua_pushstring(lua,"LOG_DEBUG");
    lua_pushnumber(lua,REDIS_DEBUG);
    lua_settable(lua,-3);

    lua_pushstring(lua,"LOG_VERBOSE");
    lua_pushnumber(lua,REDIS_VERBOSE);
    lua_settable(lua,-3);

    lua_pushstring(lua,"LOG_NOTICE");
    lua_pushnumber(lua,REDIS_NOTICE);
    lua_settable(lua,-3);

    lua_pushstring(lua,"LOG_WARNING");
    lua_pushnumber(lua,REDIS_WARNING);
    lua_settable(lua,-3);

    /* redis.sha1hex */
    /* 注册 redis.sha1hex */
    lua_pushstring(lua, "sha1hex");
    lua_pushcfunction(lua, luaRedisSha1hexCommand);
    lua_settable(lua, -3);

    /* redis.error_reply and redis.status_reply */
    /* 注册 redis.error_reply and redis.status_reply */
    lua_pushstring(lua, "error_reply");
    lua_pushcfunction(lua, luaRedisErrorReplyCommand);
    lua_settable(lua, -3);
    lua_pushstring(lua, "status_reply");
    lua_pushcfunction(lua, luaRedisStatusReplyCommand);
    lua_settable(lua, -3);

    /* Finally set the table as 'redis' global var. */
    // 将 table 设置为 redis 全局变量
    lua_setglobal(lua,"redis");

    /* Replace math.random and math.randomseed with our implementations. 
     *
     * 使用 Redis 自己的函数来替换 Lua 原来的 math.random 和 math.randomseed 实现
     *
     * 使得随机数的生成带有确定性：对于同一个 seed ，总是产生相同的随机数序列
     */
    lua_getglobal(lua,"math");

    lua_pushstring(lua,"random");
    lua_pushcfunction(lua,redis_math_random);
    lua_settable(lua,-3);

    lua_pushstring(lua,"randomseed");
    lua_pushcfunction(lua,redis_math_randomseed);
    lua_settable(lua,-3);

    lua_setglobal(lua,"math");

    /* Add a helper function that we use to sort the multi bulk output of non
     * deterministic commands, when containing 'false' elements. 
     *
     * 辅助函数，用于对带有不确定性的 multi bulk reply 进行排序
     */
    {
        char *compare_func =    "function __redis__compare_helper(a,b)\n"
                                "  if a == false then a = '' end\n"
                                "  if b == false then b = '' end\n"
                                "  return a<b\n"
                                "end\n";
        luaL_loadbuffer(lua,compare_func,strlen(compare_func),"@cmp_func_def");
        lua_pcall(lua,0,0,0);
    }

    /* Add a helper function we use for pcall error reporting.
     * Note that when the error is in the C function we want to report the
     * information about the caller, that's what makes sense from the point
     * of view of the user debugging a script. 
     *
     * 创建一个用于报告 pcall 错误的报告函数
     */
    {
        char *errh_func =       "function __redis__err__handler(err)\n"
                                "  local i = debug.getinfo(2,'nSl')\n"
                                "  if i and i.what == 'C' then\n"
                                "    i = debug.getinfo(3,'nSl')\n"
                                "  end\n"
                                "  if i then\n"
                                "    return i.source .. ':' .. i.currentline .. ': ' .. err\n"
                                "  else\n"
                                "    return err\n"
                                "  end\n"
                                "end\n";
        luaL_loadbuffer(lua,errh_func,strlen(errh_func),"@err_handler_def");
        lua_pcall(lua,0,0,0);
    }

    /* Create the (non connected) client that we use to execute Redis commands
     * inside the Lua interpreter.
     *
     * 因为 Redis 的命令只能通过客户端来执行，
     * 所以创建一个无网络链接的伪客户端来专门执行 Lua 脚本中的 Redis 命令。
     *
     * Note: there is no need to create it again when this function is called
     * by scriptingReset(). 
     *
     * 伪客户端只需要创建一次即可，因为这个函数会被 scriptingReset() 调用。
     */
    if (server.lua_client == NULL) {
        server.lua_client = createClient(-1);
        server.lua_client->flags |= REDIS_LUA_CLIENT;
    }

    /* Lua beginners ofter don't use "local", this is likely to introduce
     * subtle bugs in their code. To prevent problems we protect accesses
     * to global variables. 
     *
     * 为了避免全局变量被意外地修改，保护全局变量。
     */
    scriptingEnableGlobalsProtection(lua);

    // 将 Lua 环境赋值到服务器属性中
    server.lua = lua;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
callq 0x6b876
movq %rax, %rbx
movq %rax, %rdi
callq 0x52ce0
movq %rbx, %rdi
callq 0x52d92
leaq 0x739e4(%rip), %rdi     # 0xc6900
xorl %esi, %esi
callq 0x216f0
leaq 0x7407e(%rip), %r15     # 0xc6fa8
movq %rax, 0x7f8(%r15)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x610a7
leaq 0x44575(%rip), %rsi     # 0x974b9
movq %rbx, %rdi
callq 0x60db8
leaq -0x5d6(%rip), %rsi      # 0x5297d
movq %rbx, %rdi
xorl %edx, %edx
callq 0x60ee3
pushq $-0x3
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
leaq 0x4496a(%rip), %rsi     # 0x978db
movq %rbx, %rdi
callq 0x60db8
leaq -0x5fb(%rip), %rsi      # 0x52985
movq %rbx, %rdi
xorl %edx, %edx
callq 0x60ee3
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
leaq 0x3c7b8(%rip), %rsi     # 0x8f753
movq %rbx, %rdi
callq 0x60db8
leaq -0x4b5(%rip), %rsi      # 0x52af5
movq %rbx, %rdi
xorl %edx, %edx
callq 0x60ee3
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
leaq 0x40b43(%rip), %rsi     # 0x93b08
movq %rbx, %rdi
callq 0x60db8
xorps %xmm0, %xmm0
movq %rbx, %rdi
callq 0x60d32
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
leaq 0x40b29(%rip), %rsi     # 0x93b12
movq %rbx, %rdi
callq 0x60db8
movsd 0x3a2e7(%rip), %xmm0    # 0x8d2e0
movq %rbx, %rdi
callq 0x60d32
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
leaq 0x40b0c(%rip), %rsi     # 0x93b1e
movq %rbx, %rdi
callq 0x60db8
movsd 0x405ce(%rip), %xmm0    # 0x935f0
movq %rbx, %rdi
callq 0x60d32
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
leaq 0x40aee(%rip), %rsi     # 0x93b29
movq %rbx, %rdi
callq 0x60db8
movsd 0x405ad(%rip), %xmm0    # 0x935f8
movq %rbx, %rdi
callq 0x60d32
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
leaq 0x40ad1(%rip), %rsi     # 0x93b35
movq %rbx, %rdi
callq 0x60db8
leaq -0x6e7(%rip), %rsi      # 0x5298c
movq %rbx, %rdi
xorl %edx, %edx
callq 0x60ee3
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
leaq 0x40aaf(%rip), %rsi     # 0x93b3d
movq %rbx, %rdi
callq 0x60db8
leaq -0x5cc(%rip), %rsi      # 0x52ad1
movq %rbx, %rdi
xorl %edx, %edx
callq 0x60ee3
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
leaq 0x40a91(%rip), %rsi     # 0x93b49
movq %rbx, %rdi
callq 0x60db8
leaq -0x5e4(%rip), %rsi      # 0x52ae3
movq %rbx, %rdi
xorl %edx, %edx
callq 0x60ee3
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
leaq 0x37c2f(%rip), %rdx     # 0x8ad11
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x611c6
leaq 0x407e6(%rip), %r14     # 0x938dc
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
movq %r14, %rdx
callq 0x60fee
leaq 0x3c872(%rip), %rsi     # 0x8f97f
movq %rbx, %rdi
callq 0x60db8
leaq 0xd8(%rip), %rsi        # 0x531f4
movq %rbx, %rdi
xorl %edx, %edx
callq 0x60ee3
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
leaq 0x40a1f(%rip), %rsi     # 0x93b56
movq %rbx, %rdi
callq 0x60db8
leaq 0x1c8(%rip), %rsi       # 0x5330e
movq %rbx, %rdi
xorl %edx, %edx
callq 0x60ee3
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6119f
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
movq %r14, %rdx
callq 0x611c6
leaq 0x409f0(%rip), %rsi     # 0x93b61
leaq 0x40a61(%rip), %rcx     # 0x93bd9
pushq $0x77
popq %rdx
movq %rbx, %rdi
callq 0x6b810
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x61434
leaq 0x40a4f(%rip), %rsi     # 0x93be7
leaq 0x40b3a(%rip), %rcx     # 0x93cd9
movl $0xf1, %edx
movq %rbx, %rdi
callq 0x6b810
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x61434
cmpq $0x0, 0x7e8(%r15)
jne 0x531da
pushq $-0x1
popq %rdi
callq 0x2b7d3
movq %rax, 0x7e8(%r15)
orb $0x1, 0x91(%rax)
movq %rbx, %rdi
callq 0x52db0
movq %rbx, 0x7e0(%r15)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq