// File: /workspace/github_c_makefile_star2_O1/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 0x71d42
movq %rax, %rbx
movq %rax, %rdi
callq 0x577a3
movq %rbx, %rdi
callq 0x666bd
leaq 0x45f11(%rip), %rdx     # 0x9d8f7
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x66b6a
leaq 0x71f06(%rip), %rdi     # 0xc9900
xorl %esi, %esi
callq 0x21f97
leaq 0x725a0(%rip), %r15     # 0xc9fa8
movq %rax, 0x7f8(%r15)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x66a47
leaq 0x49a77(%rip), %rsi     # 0xa1499
movq %rbx, %rdi
callq 0x66757
leaq -0x603(%rip), %rsi      # 0x5742e
movq %rbx, %rdi
xorl %edx, %edx
callq 0x66885
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
leaq 0x49e6c(%rip), %rsi     # 0xa18bb
movq %rbx, %rdi
callq 0x66757
leaq -0x626(%rip), %rsi      # 0x57438
movq %rbx, %rdi
xorl %edx, %edx
callq 0x66885
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
leaq 0x41ccb(%rip), %rsi     # 0x99747
movq %rbx, %rdi
callq 0x66757
leaq -0x4d3(%rip), %rsi      # 0x575b8
movq %rbx, %rdi
xorl %edx, %edx
callq 0x66885
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
leaq 0x4605f(%rip), %rsi     # 0x9db08
movq %rbx, %rdi
callq 0x66757
xorps %xmm0, %xmm0
movq %rbx, %rdi
callq 0x666d1
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
leaq 0x46042(%rip), %rsi     # 0x9db12
movq %rbx, %rdi
callq 0x66757
movsd 0x3f7d0(%rip), %xmm0    # 0x972b0
movq %rbx, %rdi
callq 0x666d1
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
leaq 0x46022(%rip), %rsi     # 0x9db1e
movq %rbx, %rdi
callq 0x66757
movsd 0x45ae4(%rip), %xmm0    # 0x9d5f0
movq %rbx, %rdi
callq 0x666d1
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
leaq 0x46001(%rip), %rsi     # 0x9db29
movq %rbx, %rdi
callq 0x66757
movsd 0x45ac0(%rip), %xmm0    # 0x9d5f8
movq %rbx, %rdi
callq 0x666d1
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
leaq 0x45fe1(%rip), %rsi     # 0x9db35
movq %rbx, %rdi
callq 0x66757
leaq -0x724(%rip), %rsi      # 0x5743f
movq %rbx, %rdi
xorl %edx, %edx
callq 0x66885
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
leaq 0x45fbc(%rip), %rsi     # 0x9db3d
movq %rbx, %rdi
callq 0x66757
leaq -0x600(%rip), %rsi      # 0x57590
movq %rbx, %rdi
xorl %edx, %edx
callq 0x66885
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
leaq 0x45f9b(%rip), %rsi     # 0x9db49
movq %rbx, %rdi
callq 0x66757
leaq -0x619(%rip), %rsi      # 0x575a4
movq %rbx, %rdi
xorl %edx, %edx
callq 0x66885
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
leaq 0x3d126(%rip), %rdx     # 0x94d01
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x66b6a
leaq 0x45ced(%rip), %r14     # 0x9d8dc
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
movq %r14, %rdx
callq 0x6698e
leaq 0x41d6d(%rip), %rsi     # 0x99973
movq %rbx, %rdi
callq 0x66757
leaq 0xdd(%rip), %rsi        # 0x57cf2
movq %rbx, %rdi
xorl %edx, %edx
callq 0x66885
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
leaq 0x45f23(%rip), %rsi     # 0x9db56
movq %rbx, %rdi
callq 0x66757
leaq 0x1f1(%rip), %rsi       # 0x57e33
movq %rbx, %rdi
xorl %edx, %edx
callq 0x66885
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x66b43
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
movq %r14, %rdx
callq 0x66b6a
leaq 0x45ef1(%rip), %rsi     # 0x9db61
leaq 0x45f62(%rip), %rcx     # 0x9dbd9
movl $0x77, %edx
movq %rbx, %rdi
callq 0x71cc8
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x66dd8
leaq 0x45f4e(%rip), %rsi     # 0x9dbe7
leaq 0x46039(%rip), %rcx     # 0x9dcd9
movl $0xf1, %edx
movq %rbx, %rdi
callq 0x71cc8
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x66dd8
cmpq $0x0, 0x7e8(%r15)
jne 0x57cdd
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2cd47
movq %rax, 0x7e8(%r15)
orb $0x1, 0x91(%rax)
movq %rbx, %rdi
callq 0x57873
movq %rbx, 0x7e0(%r15)
popq %rbx
popq %r14
popq %r15
retq