mirror of
https://github.com/neovim/neovim.git
synced 2024-12-20 19:25:11 -07:00
executor,functests: Add tests for :luado, also some fixes
Fixes: 1. Allocate space for the NUL byte. 2. Do not exclude last line from range. 3. Remove code for sandbox: it is handled earlier. 4. Fix index in new_line_transformed when converting NULs to NLs. 5. Always allocate new_line_transformed, but save allocated value.
This commit is contained in:
parent
9114d9be77
commit
e1bbaca7ac
@ -160,17 +160,19 @@ static int nlua_exec_lua_string(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
|
|||||||
static int nlua_exec_luado_string(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
|
static int nlua_exec_luado_string(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
|
||||||
{
|
{
|
||||||
const String *const str = (const String *)lua_touserdata(lstate, 1);
|
const String *const str = (const String *)lua_touserdata(lstate, 1);
|
||||||
const linenr_T *const range = (const linenr_T *)lua_touserdata(lstate, 1);
|
const linenr_T *const range = (const linenr_T *)lua_touserdata(lstate, 2);
|
||||||
lua_pop(lstate, 1);
|
lua_pop(lstate, 1);
|
||||||
|
|
||||||
#define DOSTART "return function(line, linenr) "
|
#define DOSTART "return function(line, linenr) "
|
||||||
#define DOEND " end"
|
#define DOEND " end"
|
||||||
const size_t lcmd_len = str->size + (sizeof(DOSTART) - 1) + (sizeof(DOEND) - 1);
|
const size_t lcmd_len = (str->size
|
||||||
|
+ (sizeof(DOSTART) - 1)
|
||||||
|
+ (sizeof(DOEND) - 1));
|
||||||
char *lcmd;
|
char *lcmd;
|
||||||
if (lcmd_len < IOSIZE) {
|
if (lcmd_len < IOSIZE) {
|
||||||
lcmd = (char *)IObuff;
|
lcmd = (char *)IObuff;
|
||||||
} else {
|
} else {
|
||||||
lcmd = xmalloc(lcmd_len);
|
lcmd = xmalloc(lcmd_len + 1);
|
||||||
}
|
}
|
||||||
memcpy(lcmd, S_LEN(DOSTART));
|
memcpy(lcmd, S_LEN(DOSTART));
|
||||||
memcpy(lcmd + sizeof(DOSTART) - 1, str->data, str->size);
|
memcpy(lcmd + sizeof(DOSTART) - 1, str->data, str->size);
|
||||||
@ -186,7 +188,7 @@ static int nlua_exec_luado_string(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
|
|||||||
nlua_error(lstate, _("E5110: Error while creating lua function: %.*s"));
|
nlua_error(lstate, _("E5110: Error while creating lua function: %.*s"));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
for (linenr_T l = range[0]; l < range[1]; l++) {
|
for (linenr_T l = range[0]; l <= range[1]; l++) {
|
||||||
if (l > curbuf->b_ml.ml_line_count) {
|
if (l > curbuf->b_ml.ml_line_count) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -198,24 +200,15 @@ static int nlua_exec_luado_string(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (lua_isstring(lstate, -1)) {
|
if (lua_isstring(lstate, -1)) {
|
||||||
if (sandbox) {
|
|
||||||
EMSG(_("E5112: Not allowed in sandbox"));
|
|
||||||
lua_pop(lstate, 1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
size_t new_line_len;
|
size_t new_line_len;
|
||||||
const char *new_line = lua_tolstring(lstate, -1, &new_line_len);
|
const char *const new_line = lua_tolstring(lstate, -1, &new_line_len);
|
||||||
char *const new_line_transformed = (
|
char *const new_line_transformed = xmemdupz(new_line, new_line_len);
|
||||||
new_line_len < IOSIZE
|
|
||||||
? memcpy(IObuff, new_line, new_line_len)
|
|
||||||
: xmemdupz(new_line, new_line_len));
|
|
||||||
new_line_transformed[new_line_len] = NUL;
|
|
||||||
for (size_t i = 0; i < new_line_len; i++) {
|
for (size_t i = 0; i < new_line_len; i++) {
|
||||||
if (new_line_transformed[new_line_len] == NUL) {
|
if (new_line_transformed[i] == NUL) {
|
||||||
new_line_transformed[new_line_len] = '\n';
|
new_line_transformed[i] = '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ml_replace(l, (char_u *)new_line_transformed, true);
|
ml_replace(l, (char_u *)new_line_transformed, false);
|
||||||
changed_bytes(l, 0);
|
changed_bytes(l, 0);
|
||||||
}
|
}
|
||||||
lua_pop(lstate, 1);
|
lua_pop(lstate, 1);
|
||||||
|
@ -63,3 +63,48 @@ describe(':lua command', function()
|
|||||||
eq(42, funcs.luaeval('gvar'))
|
eq(42, funcs.luaeval('gvar'))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe(':luado command', function()
|
||||||
|
it('works', function()
|
||||||
|
curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
|
||||||
|
eq('', redir_exec('luado lines = (lines or {}) lines[#lines + 1] = {linenr, line}'))
|
||||||
|
eq({'ABC', 'def', 'gHi'}, curbufmeths.get_lines(0, -1, false))
|
||||||
|
eq({{1, 'ABC'}, {2, 'def'}, {3, 'gHi'}}, funcs.luaeval('lines'))
|
||||||
|
|
||||||
|
-- Automatic transformation of numbers
|
||||||
|
eq('', redir_exec('luado return linenr'))
|
||||||
|
eq({'1', '2', '3'}, curbufmeths.get_lines(0, -1, false))
|
||||||
|
|
||||||
|
eq('', redir_exec('luado return ("<%02x>"):format(line:byte())'))
|
||||||
|
eq({'<31>', '<32>', '<33>'}, curbufmeths.get_lines(0, -1, false))
|
||||||
|
end)
|
||||||
|
it('stops processing lines when suddenly out of lines', function()
|
||||||
|
curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
|
||||||
|
eq('', redir_exec('2,$luado runs = ((runs or 0) + 1) vim.api.nvim_command("%d")'))
|
||||||
|
eq({''}, curbufmeths.get_lines(0, -1, false))
|
||||||
|
eq(1, funcs.luaeval('runs'))
|
||||||
|
end)
|
||||||
|
it('works correctly when changing lines out of range', function()
|
||||||
|
curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
|
||||||
|
eq('\nE322: line number out of range: 1 past the end\nE320: Cannot find line 2',
|
||||||
|
redir_exec('2,$luado vim.api.nvim_command("%d") return linenr'))
|
||||||
|
eq({''}, curbufmeths.get_lines(0, -1, false))
|
||||||
|
end)
|
||||||
|
it('fails on errors', function()
|
||||||
|
eq([[Vim(luado):E5109: Error while creating lua chunk: [string "<VimL compiled string>"]:1: unexpected symbol near ')']],
|
||||||
|
exc_exec('luado ()'))
|
||||||
|
eq([[Vim(luado):E5111: Error while calling lua function: [string "<VimL compiled string>"]:1: attempt to perform arithmetic on global 'liness' (a nil value)]],
|
||||||
|
exc_exec('luado return liness + 1'))
|
||||||
|
end)
|
||||||
|
it('fails in sandbox when needed', function()
|
||||||
|
curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
|
||||||
|
eq('\nE48: Not allowed in sandbox: sandbox luado runs = (runs or 0) + 1',
|
||||||
|
redir_exec('sandbox luado runs = (runs or 0) + 1'))
|
||||||
|
eq(NIL, funcs.luaeval('runs'))
|
||||||
|
end)
|
||||||
|
it('works with long strings', function()
|
||||||
|
local s = ('x'):rep(100500)
|
||||||
|
eq('', redir_exec(('luado return "%s"'):format(s)))
|
||||||
|
eq({s}, curbufmeths.get_lines(0, -1, false))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user