void ngx_http_lua_inject_log_api(lua_State *L) { ngx_http_lua_inject_log_consts(L); lua_pushcfunction(L, ngx_http_lua_ngx_log); lua_setfield(L, -2, "log"); lua_pushcfunction(L, ngx_http_lua_print); lua_setglobal(L, "print"); }
/** * Set environment table for the given code closure. * * Before: * | code closure | <- top * | ... | * * After: * | code closure | <- top * | ... | * */ static void ngx_http_lua_set_by_lua_env(lua_State *L, ngx_http_request_t *r, size_t nargs, ngx_http_variable_value_t *args) { /* set nginx request pointer to current lua thread's globals table */ lua_pushlightuserdata(L, r); lua_setglobal(L, GLOBALS_SYMBOL_REQUEST); /** * we want to create empty environment for current script * * setmetatable({}, {__index = _G}) * * if a function or symbol is not defined in our env, __index will lookup * in the global env. * * all variables created in the script-env will be thrown away at the end * of the script run. * */ lua_newtable(L); /* new empty environment aka {} */ /* override 'print' function */ lua_pushcfunction(L, ngx_http_lua_print); lua_setfield(L, -2, "print"); /* {{{ initialize ngx.* namespace */ lua_newtable(L); /* ngx.* */ lua_newtable(L); /* .arg table aka {} */ lua_newtable(L); /* the metatable for new param table */ lua_pushinteger(L, nargs); /* 1st upvalue: argument number */ lua_pushlightuserdata(L, args); /* 2nd upvalue: pointer to arguments */ lua_pushcclosure(L, ngx_http_lua_param_get, 2); /* binding upvalues to __index meta-method closure */ lua_setfield(L, -2, "__index"); lua_setmetatable(L, -2); /* tie the metatable to param table */ lua_setfield(L, -2, "arg"); /* set ngx.arg table */ /* {{{ register reference maps */ lua_newtable(L); /* ngx.var */ lua_newtable(L); /* metatable for ngx.var */ lua_pushcfunction(L, ngx_http_lua_var_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, ngx_http_lua_var_set); lua_setfield(L, -2, "__newindex"); lua_setmetatable(L, -2); lua_setfield(L, -2, "var"); /* }}} */ #if (NGX_PCRE) /* {{{ ngx.re table */ lua_newtable(L); /* .re */ lua_pushcfunction(L, ngx_http_lua_ngx_re_match); lua_setfield(L, -2, "match"); lua_pushcfunction(L, ngx_http_lua_ngx_re_gmatch); lua_setfield(L, -2, "gmatch"); lua_pushcfunction(L, ngx_http_lua_ngx_re_sub); lua_setfield(L, -2, "sub"); lua_pushcfunction(L, ngx_http_lua_ngx_re_gsub); lua_setfield(L, -2, "gsub"); lua_setfield(L, -2, "re"); /* }}} */ #endif /* NGX_PCRE */ lua_pushcfunction(L, ngx_http_lua_ngx_escape_uri); lua_setfield(L, -2, "escape_uri"); lua_pushcfunction(L, ngx_http_lua_ngx_unescape_uri); lua_setfield(L, -2, "unescape_uri"); lua_pushcfunction(L, ngx_http_lua_ngx_quote_sql_str); lua_setfield(L, -2, "quote_sql_str"); lua_pushcfunction(L, ngx_http_lua_ngx_decode_base64); lua_setfield(L, -2, "decode_base64"); lua_pushcfunction(L, ngx_http_lua_ngx_encode_base64); lua_setfield(L, -2, "encode_base64"); lua_pushcfunction(L, ngx_http_lua_ngx_md5_bin); lua_setfield(L, -2, "md5_bin"); lua_pushcfunction(L, ngx_http_lua_ngx_md5); lua_setfield(L, -2, "md5"); lua_pushcfunction(L, ngx_http_lua_ngx_time); lua_setfield(L, -2, "get_now_ts"); /* deprecated */ lua_pushcfunction(L, ngx_http_lua_ngx_utctime); lua_setfield(L, -2, "utctime"); lua_pushcfunction(L, ngx_http_lua_ngx_localtime); lua_setfield(L, -2, "get_now"); /* deprecated */ lua_pushcfunction(L, ngx_http_lua_ngx_time); lua_setfield(L, -2, "time"); lua_pushcfunction(L, ngx_http_lua_ngx_localtime); lua_setfield(L, -2, "localtime"); lua_pushcfunction(L, ngx_http_lua_ngx_today); lua_setfield(L, -2, "get_today"); /* deprecated */ lua_pushcfunction(L, ngx_http_lua_ngx_today); lua_setfield(L, -2, "today"); lua_pushcfunction(L, ngx_http_lua_ngx_http_time); lua_setfield(L, -2, "http_time"); lua_pushcfunction(L, ngx_http_lua_ngx_parse_http_time); lua_setfield(L, -2, "parse_http_time"); lua_pushcfunction(L, ngx_http_lua_ngx_cookie_time); lua_setfield(L, -2, "cookie_time"); lua_pushcfunction(L, ngx_http_lua_ngx_log); lua_setfield(L, -2, "log"); ngx_http_lua_inject_log_consts(L); /* ngx. getter and setter */ lua_createtable(L, 0, 2); /* metatable for .ngx */ lua_pushcfunction(L, ngx_http_lua_ngx_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, ngx_http_lua_ngx_set); lua_setfield(L, -2, "__newindex"); lua_setmetatable(L, -2); lua_setfield(L, -2, "ngx"); /* }}} */ /* {{{ make new env inheriting main thread's globals table */ lua_newtable(L); /* the metatable for the new env */ lua_pushvalue(L, LUA_GLOBALSINDEX); lua_setfield(L, -2, "__index"); lua_setmetatable(L, -2); /* setmetatable({}, {__index = _G}) */ /* }}} */ lua_setfenv(L, -2); /* set new running env for the code closure */ }
static void init_ngx_lua_globals(lua_State *L) { /* {{{ remove unsupported globals */ lua_pushnil(L); lua_setfield(L, LUA_GLOBALSINDEX, "coroutine"); /* }}} */ /* {{{ register global hook functions */ lua_pushcfunction(L, ngx_http_lua_print); lua_setglobal(L, "print"); /* }}} */ lua_createtable(L, 0, 1); /* ndk.* */ lua_newtable(L); /* .set_var */ lua_createtable(L, 0, 2); /* metatable for .set_var */ lua_pushcfunction(L, ngx_http_lua_ndk_set_var_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, ngx_http_lua_ndk_set_var_set); lua_setfield(L, -2, "__newindex"); lua_setmetatable(L, -2); lua_setfield(L, -2, "set_var"); lua_setglobal(L, "ndk"); lua_createtable(L, 0, 20); /* ngx.* */ /* {{{ register nginx hook functions */ lua_pushcfunction(L, ngx_http_lua_ngx_exec); lua_setfield(L, -2, "exec"); lua_pushcfunction(L, ngx_http_lua_ngx_send_headers); lua_setfield(L, -2, "send_headers"); lua_pushcfunction(L, ngx_http_lua_ngx_print); lua_setfield(L, -2, "print"); lua_pushcfunction(L, ngx_http_lua_ngx_say); lua_setfield(L, -2, "say"); lua_pushcfunction(L, ngx_http_lua_ngx_log); lua_setfield(L, -2, "log"); lua_pushcfunction(L, ngx_http_lua_ngx_exit); lua_setfield(L, -2, "throw_error"); /* deprecated */ lua_pushcfunction(L, ngx_http_lua_ngx_exit); lua_setfield(L, -2, "exit"); lua_pushcfunction(L, ngx_http_lua_ngx_flush); lua_setfield(L, -2, "flush"); lua_pushcfunction(L, ngx_http_lua_ngx_eof); lua_setfield(L, -2, "eof"); lua_pushcfunction(L, ngx_http_lua_ngx_escape_uri); lua_setfield(L, -2, "escape_uri"); lua_pushcfunction(L, ngx_http_lua_ngx_unescape_uri); lua_setfield(L, -2, "unescape_uri"); lua_pushcfunction(L, ngx_http_lua_ngx_quote_sql_str); lua_setfield(L, -2, "quote_sql_str"); lua_pushcfunction(L, ngx_http_lua_ngx_decode_base64); lua_setfield(L, -2, "decode_base64"); lua_pushcfunction(L, ngx_http_lua_ngx_encode_base64); lua_setfield(L, -2, "encode_base64"); lua_pushcfunction(L, ngx_http_lua_ngx_md5_bin); lua_setfield(L, -2, "md5_bin"); lua_pushcfunction(L, ngx_http_lua_ngx_md5); lua_setfield(L, -2, "md5"); lua_pushcfunction(L, ngx_http_lua_ngx_utctime); lua_setfield(L, -2, "utctime"); lua_pushcfunction(L, ngx_http_lua_ngx_time); lua_setfield(L, -2, "get_now_ts"); /* deprecated */ lua_pushcfunction(L, ngx_http_lua_ngx_localtime); lua_setfield(L, -2, "get_now"); /* deprecated */ lua_pushcfunction(L, ngx_http_lua_ngx_localtime); lua_setfield(L, -2, "localtime"); lua_pushcfunction(L, ngx_http_lua_ngx_time); lua_setfield(L, -2, "time"); lua_pushcfunction(L, ngx_http_lua_ngx_today); lua_setfield(L, -2, "get_today"); /* deprecated */ lua_pushcfunction(L, ngx_http_lua_ngx_today); lua_setfield(L, -2, "today"); lua_pushcfunction(L, ngx_http_lua_ngx_cookie_time); lua_setfield(L, -2, "cookie_time"); lua_pushcfunction(L, ngx_http_lua_ngx_redirect); lua_setfield(L, -2, "redirect"); lua_createtable(L, 0, 2); /* .location */ lua_pushcfunction(L, ngx_http_lua_ngx_location_capture); lua_setfield(L, -2, "capture"); lua_pushcfunction(L, ngx_http_lua_ngx_location_capture_multi); lua_setfield(L, -2, "capture_multi"); lua_setfield(L, -2, "location"); /* }}} */ inject_http_consts(L); ngx_http_lua_inject_log_consts(L); inject_core_consts(L); /* {{{ register reference maps */ lua_newtable(L); /* .var */ lua_createtable(L, 0, 2); /* metatable for .var */ lua_pushcfunction(L, ngx_http_lua_var_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, ngx_http_lua_var_set); lua_setfield(L, -2, "__newindex"); lua_setmetatable(L, -2); lua_setfield(L, -2, "var"); #if 1 lua_newtable(L); /* .header */ lua_createtable(L, 0, 2); /* metatable for .header */ lua_pushcfunction(L, ngx_http_lua_header_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, ngx_http_lua_header_set); lua_setfield(L, -2, "__newindex"); lua_setmetatable(L, -2); lua_setfield(L, -2, "header"); #endif /* ngx. getter and setter */ lua_createtable(L, 0, 2); /* metatable for .ngx */ lua_pushcfunction(L, ngx_http_lua_ngx_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, ngx_http_lua_ngx_set); lua_setfield(L, -2, "__newindex"); lua_setmetatable(L, -2); /* }}} */ lua_setglobal(L, "ngx"); }