// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %rbx
callq 0x71e6a
movq %rax, %rbx
movq %rax, %rdi
callq 0x571f3
movq %rbx, %rdi
callq 0x6621b
leaq 0x46509(%rip), %rdx     # 0x9d937
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x666cf
leaq 0x724be(%rip), %rdi     # 0xc9900
xorl %esi, %esi
callq 0x2217b
leaq 0x72b58(%rip), %r15     # 0xc9fa8
movq %rax, 0x7f8(%r15)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x665ac
leaq 0x4a0bf(%rip), %rsi     # 0xa1529
movq %rbx, %rdi
callq 0x662b5
leaq -0x609(%rip), %rsi      # 0x56e70
movq %rbx, %rdi
xorl %edx, %edx
callq 0x663e3
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
leaq 0x4a4b4(%rip), %rsi     # 0xa194b
movq %rbx, %rdi
callq 0x662b5
leaq -0x62c(%rip), %rsi      # 0x56e7a
movq %rbx, %rdi
xorl %edx, %edx
callq 0x663e3
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
leaq 0x422cb(%rip), %rsi     # 0x9978f
movq %rbx, %rdi
callq 0x662b5
leaq -0x4d9(%rip), %rsi      # 0x56ffa
movq %rbx, %rdi
xorl %edx, %edx
callq 0x663e3
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
leaq 0x46657(%rip), %rsi     # 0x9db48
movq %rbx, %rdi
callq 0x662b5
xorps %xmm0, %xmm0
movq %rbx, %rdi
callq 0x6622f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
leaq 0x4663a(%rip), %rsi     # 0x9db52
movq %rbx, %rdi
callq 0x662b5
movsd 0x3fde8(%rip), %xmm0    # 0x97310
movq %rbx, %rdi
callq 0x6622f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
leaq 0x4661a(%rip), %rsi     # 0x9db5e
movq %rbx, %rdi
callq 0x662b5
movsd 0x460dc(%rip), %xmm0    # 0x9d630
movq %rbx, %rdi
callq 0x6622f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
leaq 0x465f9(%rip), %rsi     # 0x9db69
movq %rbx, %rdi
callq 0x662b5
movsd 0x460b8(%rip), %xmm0    # 0x9d638
movq %rbx, %rdi
callq 0x6622f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
leaq 0x465d9(%rip), %rsi     # 0x9db75
movq %rbx, %rdi
callq 0x662b5
leaq -0x72a(%rip), %rsi      # 0x56e81
movq %rbx, %rdi
xorl %edx, %edx
callq 0x663e3
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
leaq 0x465b4(%rip), %rsi     # 0x9db7d
movq %rbx, %rdi
callq 0x662b5
leaq -0x606(%rip), %rsi      # 0x56fd2
movq %rbx, %rdi
xorl %edx, %edx
callq 0x663e3
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
leaq 0x46593(%rip), %rsi     # 0x9db89
movq %rbx, %rdi
callq 0x662b5
leaq -0x61f(%rip), %rsi      # 0x56fe6
movq %rbx, %rdi
xorl %edx, %edx
callq 0x663e3
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
leaq 0x3d72e(%rip), %rdx     # 0x94d51
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x666cf
leaq 0x462e5(%rip), %r14     # 0x9d91c
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
movq %r14, %rdx
callq 0x664f3
leaq 0x4236d(%rip), %rsi     # 0x999bb
movq %rbx, %rdi
callq 0x662b5
leaq 0xdd(%rip), %rsi        # 0x5773a
movq %rbx, %rdi
xorl %edx, %edx
callq 0x663e3
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
leaq 0x4651b(%rip), %rsi     # 0x9db96
movq %rbx, %rdi
callq 0x662b5
leaq 0x1f1(%rip), %rsi       # 0x5787b
movq %rbx, %rdi
xorl %edx, %edx
callq 0x663e3
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x666a8
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
movq %r14, %rdx
callq 0x666cf
leaq 0x464e9(%rip), %rsi     # 0x9dba1
leaq 0x4655a(%rip), %rcx     # 0x9dc19
movl $0x77, %edx
movq %rbx, %rdi
callq 0x71df0
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x66941
leaq 0x46546(%rip), %rsi     # 0x9dc27
leaq 0x46631(%rip), %rcx     # 0x9dd19
movl $0xf1, %edx
movq %rbx, %rdi
callq 0x71df0
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x66941
cmpq $0x0, 0x7e8(%r15)
jne 0x57725
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2cfe7
movq %rax, 0x7e8(%r15)
orb $0x1, 0x91(%rax)
movq %rbx, %rdi
callq 0x572c3
movq %rbx, 0x7e0(%r15)
popq %rbx
popq %r14
popq %r15
retq