lua_State * ngx_http_lua_new_state(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf) { lua_State *L = luaL_newstate(); if(L == NULL) { return NULL; } luaL_openlibs(L); lua_getglobal(L, "package"); if (! lua_istable(L, -1)) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "the \"package\" table does not exist"); return NULL; } if (lmcf->lua_path.len != 0) { const char *old_path; const char *new_path; lua_getfield(L, -1, "path"); /* get original package.path */ old_path = lua_tostring(L, -1); lua_pushlstring(L, (char *) lmcf->lua_path.data, lmcf->lua_path.len); new_path = lua_tostring(L, -1); setpath(L, -3, "path", new_path, old_path); lua_pop(L, 2); } if (lmcf->lua_cpath.len != 0) { const char *old_cpath; const char *new_cpath; lua_getfield(L, -1, "cpath"); /* get original package.cpath */ old_cpath = lua_tostring(L, -1); lua_pushlstring(L, (char *) lmcf->lua_cpath.data, lmcf->lua_cpath.len); new_cpath = lua_tostring(L, -1); setpath(L, -3, "cpath", new_cpath, old_cpath); lua_pop(L, 2); } lua_remove(L, -1); /* remove the "package" table */ init_ngx_lua_registry(L); init_ngx_lua_globals(L); return L; }
lua_State* ngx_http_lua_new_state() { lua_State *l = luaL_newstate(); if(l == NULL) { return NULL; } luaL_openlibs(l); init_ngx_lua_registry(l); init_ngx_lua_globals(l); return l; }