Fix os.getenv of lua on Windows

Change to use os_getenv instead of getenv because environment variable
set by uv_os_setenv can not be get with getenv.
This commit is contained in:
erw7 2019-03-07 13:41:40 +09:00
parent 24a56cca30
commit c9264e6d52
3 changed files with 48 additions and 0 deletions

View File

@ -24,6 +24,10 @@
#include "nvim/undo.h"
#include "nvim/ascii.h"
#ifdef WIN32
#include "nvim/os/env.h"
#endif
#include "nvim/lua/executor.h"
#include "nvim/lua/converter.h"
@ -118,6 +122,14 @@ static int nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
lua_setfield(lstate, -2, "debug");
lua_pop(lstate, 1);
#ifdef WIN32
// os.getenv
lua_getglobal(lstate, "os");
lua_pushcfunction(lstate, &nlua_getenv);
lua_setfield(lstate, -2, "getenv");
lua_pop(lstate, 1);
#endif
// vim
if (luaL_dostring(lstate, (char *)&vim_module[0])) {
nlua_error(lstate, _("E5106: Error while creating vim module: %.*s"));
@ -337,6 +349,19 @@ int nlua_debug(lua_State *lstate)
return 0;
}
#ifdef WIN32
/// os.getenv implementation: On Windows, uv_os_setenv does not update _environ,
/// so we need to use os_getenv instead of getenv. Therefore we will apply a
/// monkey patch here.
///
/// @param lstate Lua interpreter state.
static int nlua_getenv(lua_State *lstate)
{
lua_pushstring(lstate, os_getenv(luaL_checkstring(lstate, 1)));
return 1;
}
#endif
/// Evaluate lua string
///
/// Used for luaeval().

7
src/nvim/os/env.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef NVIM_OS_ENV_H
#define NVIM_OS_ENV_H
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "os/env.h.generated.h"
#endif
#endif // NVIM_OS_ENV_H

View File

@ -300,3 +300,19 @@ describe('package.path/package.cpath', function()
eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str))
end)
end)
describe('os.getenv', function()
it('returns nothing for not set env var', function()
eq(NIL, funcs.luaeval('os.getenv("XTEST_1")'))
end)
it('returns env var set by the parent process', function()
local value = 'foo'
clear({env = {['XTEST_1']=value}})
eq(value, funcs.luaeval('os.getenv("XTEST_1")'))
end)
it('returns env var set by let', function()
local value = 'foo'
meths.command('let $XTEST_1 = "'..value..'"')
eq(value, funcs.luaeval('os.getenv("XTEST_1")'))
end)
end)