Skip to content

Commit 08f8d02

Browse files
fix: use lua stdlib in scripts (fix #437)
1 parent 4086034 commit 08f8d02

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lua/rest-nvim/script/lua.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ local function create_prescript_env(ctx)
3232
end,
3333
},
3434
},
35-
---Global variable `vim` you are familiar of
36-
vim = vim,
3735
}
3836
return env
3937
end
@@ -77,8 +75,6 @@ local function create_handler_env(ctx, res)
7775
---Raw response object
7876
---@type rest.Response
7977
response = res,
80-
---Global variable `vim` you are familiar of
81-
vim = vim,
8278
}
8379
return env
8480
end
@@ -88,6 +84,7 @@ end
8884
---@param env table
8985
---@return function
9086
local function load_lua(s, env)
87+
env = setmetatable(env, { __index = _G })
9188
local f, error_msg = load(s, "script_variable", "bt", env)
9289
if error_msg then
9390
logger.error(error_msg)

spec/script_spec.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,15 @@ client.global.set("MYVAR", json.var)
7474
end)
7575
assert.same("boo", ctx:resolve("MYVAR"))
7676
end)
77+
it("use lua stdlib from script", function ()
78+
local ctx = Context:new()
79+
local res = {}
80+
local script = [[
81+
local a = 123
82+
vim.env["__TEST"] = tostring(a)
83+
]]
84+
local h = require("rest-nvim.script.lua").load_post_req_hook(script, ctx)
85+
h(res)
86+
assert.same(vim.env["__TEST"], "123")
87+
end)
7788
end)

0 commit comments

Comments
 (0)