2014-08-30 06:35:42 -07:00
|
|
|
local ffi = require('ffi')
|
|
|
|
local formatc = require('test.unit.formatc')
|
|
|
|
local Set = require('test.unit.set')
|
|
|
|
local Preprocess = require('test.unit.preprocess')
|
2022-06-27 03:08:59 -07:00
|
|
|
local Paths = require('test.cmakeconfig.paths')
|
2016-04-23 16:53:11 -07:00
|
|
|
local global_helpers = require('test.helpers')
|
2016-09-25 13:48:22 -07:00
|
|
|
local assert = require('luassert')
|
|
|
|
local say = require('say')
|
2016-04-23 16:53:11 -07:00
|
|
|
|
2017-03-11 03:28:18 -07:00
|
|
|
local check_cores = global_helpers.check_cores
|
2017-04-01 02:52:28 -07:00
|
|
|
local dedent = global_helpers.dedent
|
2016-04-23 16:53:11 -07:00
|
|
|
local neq = global_helpers.neq
|
2020-02-18 01:41:29 -07:00
|
|
|
local map = global_helpers.tbl_map
|
2016-04-23 16:53:11 -07:00
|
|
|
local eq = global_helpers.eq
|
2019-03-30 10:14:20 -07:00
|
|
|
local trim = global_helpers.trim
|
2014-08-30 06:35:42 -07:00
|
|
|
|
|
|
|
-- add some standard header locations
|
2014-09-06 15:36:00 -07:00
|
|
|
for _, p in ipairs(Paths.include_paths) do
|
2014-08-30 06:35:42 -07:00
|
|
|
Preprocess.add_to_include_path(p)
|
|
|
|
end
|
|
|
|
|
2023-04-03 04:01:23 -07:00
|
|
|
local child_pid = nil --- @type integer
|
|
|
|
--- @generic F: function
|
|
|
|
--- @param func F
|
|
|
|
--- @return F
|
2017-03-11 06:02:47 -07:00
|
|
|
local function only_separate(func)
|
|
|
|
return function(...)
|
2017-03-11 13:48:16 -07:00
|
|
|
if child_pid ~= 0 then
|
|
|
|
error('This function must be run in a separate process only')
|
2017-03-11 06:02:47 -07:00
|
|
|
end
|
|
|
|
return func(...)
|
|
|
|
end
|
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
|
|
|
|
--- @class ChildCall
|
|
|
|
--- @field func function
|
|
|
|
--- @field args any[]
|
|
|
|
|
|
|
|
--- @class ChildCallLog
|
|
|
|
--- @field func string
|
|
|
|
--- @field args any[]
|
|
|
|
--- @field ret any?
|
|
|
|
|
|
|
|
local child_calls_init = {} --- @type ChildCall[]
|
|
|
|
local child_calls_mod = nil --- @type ChildCall[]
|
|
|
|
local child_calls_mod_once = nil --- @type ChildCall[]?
|
|
|
|
|
2017-03-11 16:54:23 -07:00
|
|
|
local function child_call(func, ret)
|
2017-03-11 06:02:47 -07:00
|
|
|
return function(...)
|
2017-03-11 16:54:23 -07:00
|
|
|
local child_calls = child_calls_mod or child_calls_init
|
2017-03-11 13:48:16 -07:00
|
|
|
if child_pid ~= 0 then
|
2017-03-11 16:54:23 -07:00
|
|
|
child_calls[#child_calls + 1] = {func=func, args={...}}
|
2017-03-11 06:02:47 -07:00
|
|
|
return ret
|
|
|
|
else
|
|
|
|
return func(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-11 16:54:23 -07:00
|
|
|
-- Run some code at the start of the child process, before running the test
|
|
|
|
-- itself. Is supposed to be run in `before_each`.
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @param func function
|
2017-03-11 16:54:23 -07:00
|
|
|
local function child_call_once(func, ...)
|
|
|
|
if child_pid ~= 0 then
|
2023-04-03 04:01:23 -07:00
|
|
|
child_calls_mod_once[#child_calls_mod_once + 1] = { func = func, args = {...} }
|
2017-03-11 16:54:23 -07:00
|
|
|
else
|
|
|
|
func(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-03 04:01:23 -07:00
|
|
|
local child_cleanups_mod_once = nil --- @type ChildCall[]?
|
2017-03-11 16:54:23 -07:00
|
|
|
|
|
|
|
-- Run some code at the end of the child process, before exiting. Is supposed to
|
|
|
|
-- be run in `before_each` because `after_each` is run after child has exited.
|
|
|
|
local function child_cleanup_once(func, ...)
|
|
|
|
local child_cleanups = child_cleanups_mod_once
|
|
|
|
if child_pid ~= 0 then
|
|
|
|
child_cleanups[#child_cleanups + 1] = {func=func, args={...}}
|
|
|
|
else
|
|
|
|
func(...)
|
2017-03-11 06:02:47 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-30 12:36:49 -07:00
|
|
|
-- Unittests are run from debug nvim binary in lua interpreter mode.
|
|
|
|
local libnvim = ffi.C
|
2017-03-11 06:02:47 -07:00
|
|
|
|
|
|
|
local lib = setmetatable({}, {
|
2017-03-11 13:48:16 -07:00
|
|
|
__index = only_separate(function(_, idx)
|
2017-03-11 06:02:47 -07:00
|
|
|
return libnvim[idx]
|
|
|
|
end),
|
2017-03-11 16:54:23 -07:00
|
|
|
__newindex = child_call(function(_, idx, val)
|
2017-03-11 06:02:47 -07:00
|
|
|
libnvim[idx] = val
|
|
|
|
end),
|
|
|
|
})
|
|
|
|
|
|
|
|
local init = only_separate(function()
|
2017-03-11 16:54:23 -07:00
|
|
|
for _, c in ipairs(child_calls_init) do
|
2017-03-11 06:02:47 -07:00
|
|
|
c.func(unpack(c.args))
|
|
|
|
end
|
|
|
|
libnvim.event_init()
|
2020-06-07 10:13:52 -07:00
|
|
|
libnvim.early_init(nil)
|
2017-03-11 16:54:23 -07:00
|
|
|
if child_calls_mod then
|
|
|
|
for _, c in ipairs(child_calls_mod) do
|
|
|
|
c.func(unpack(c.args))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if child_calls_mod_once then
|
|
|
|
for _, c in ipairs(child_calls_mod_once) do
|
2017-03-11 06:02:47 -07:00
|
|
|
c.func(unpack(c.args))
|
|
|
|
end
|
2017-03-11 16:54:23 -07:00
|
|
|
child_calls_mod_once = nil
|
2017-03-11 06:02:47 -07:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
local deinit = only_separate(function()
|
2017-03-11 16:54:23 -07:00
|
|
|
if child_cleanups_mod_once then
|
|
|
|
for _, c in ipairs(child_cleanups_mod_once) do
|
2017-03-11 06:02:47 -07:00
|
|
|
c.func(unpack(c.args))
|
|
|
|
end
|
2017-03-11 16:54:23 -07:00
|
|
|
child_cleanups_mod_once = nil
|
2017-03-11 06:02:47 -07:00
|
|
|
end
|
|
|
|
end)
|
2014-08-30 06:35:42 -07:00
|
|
|
|
|
|
|
-- a Set that keeps around the lines we've already seen
|
2017-03-11 17:02:14 -07:00
|
|
|
local cdefs_init = Set:new()
|
|
|
|
local cdefs_mod = nil
|
2015-11-17 07:49:12 -07:00
|
|
|
local imported = Set:new()
|
|
|
|
local pragma_pack_id = 1
|
2014-10-26 03:20:19 -07:00
|
|
|
|
2014-08-30 06:35:42 -07:00
|
|
|
-- some things are just too complex for the LuaJIT C parser to digest. We
|
|
|
|
-- usually don't need them anyway.
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @param body string
|
2014-09-06 15:36:00 -07:00
|
|
|
local function filter_complex_blocks(body)
|
2023-04-03 04:01:23 -07:00
|
|
|
local result = {} --- @type string[]
|
2014-08-30 06:35:42 -07:00
|
|
|
|
|
|
|
for line in body:gmatch("[^\r\n]+") do
|
2016-02-08 17:20:16 -07:00
|
|
|
if not (string.find(line, "(^)", 1, true) ~= nil
|
|
|
|
or string.find(line, "_ISwupper", 1, true)
|
2018-02-25 02:23:12 -07:00
|
|
|
or string.find(line, "_Float")
|
2016-02-08 17:20:16 -07:00
|
|
|
or string.find(line, "msgpack_zone_push_finalizer")
|
2016-06-21 15:01:21 -07:00
|
|
|
or string.find(line, "msgpack_unpacker_reserve_buffer")
|
refactor(map): enhanced implementation, Clean Code™, etc etc
This involves two redesigns of the map.c implementations:
1. Change of macro style and code organization
The old khash.h and map.c implementation used huge #define blocks with a
lot of backslash line continuations.
This instead uses the "implementation file" .c.h pattern. Such a file is
meant to be included multiple times, with different macros set prior to
inclusion as parameters. we already use this pattern e.g. for
eval/typval_encode.c.h to implement different typval encoders reusing a
similar structure.
We can structure this code into two parts. one that only depends on key
type and is enough to implement sets, and one which depends on both key
and value to implement maps (as a wrapper around sets, with an added
value[] array)
2. Separate the main hash buckets from the key / value arrays
Change the hack buckets to only contain an index into separate key /
value arrays
This is a common pattern in modern, state of the art hashmap
implementations. Even though this leads to one more allocated array, it
is this often is a net reduction of memory consumption. Consider
key+value consuming at least 12 bytes per pair. On average, we will have
twice as many buckets per item.
Thus old implementation:
2*12 = 24 bytes per item
New implementation
1*12 + 2*4 = 20 bytes per item
And the difference gets bigger with larger items.
One might think we have pulled a fast one here, as wouldn't the average size of
the new key/value arrays be 1.5 slots per items due to amortized grows?
But remember, these arrays are fully dense, and thus the accessed memory,
measured in _cache lines_, the unit which actually matters, will be the
fully used memory but just rounded up to the nearest cache line
boundary.
This has some other interesting properties, such as an insert-only
set/map will be fully ordered by insert only. Preserving this ordering
in face of deletions is more tricky tho. As we currently don't use
ordered maps, the "delete" operation maintains compactness of the item
arrays in the simplest way by breaking the ordering. It would be
possible to implement an order-preserving delete although at some cost,
like allowing the items array to become non-dense until the next rehash.
Finally, in face of these two major changes, all code used in khash.h
has been integrated into map.c and friends. Given the heavy edits it
makes no sense to "layer" the code into a vendored and a wrapper part.
Rather, the layered cake follows the specialization depth: code shared
for all maps, code specialized to a key type (and its equivalence
relation), and finally code specialized to value+key type.
2023-05-17 07:08:06 -07:00
|
|
|
or string.find(line, "value_init_")
|
2016-12-28 10:46:58 -07:00
|
|
|
or string.find(line, "UUID_NULL") -- static const uuid_t UUID_NULL = {...}
|
2016-06-21 15:01:21 -07:00
|
|
|
or string.find(line, "inline _Bool")) then
|
2014-08-30 06:35:42 -07:00
|
|
|
result[#result + 1] = line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return table.concat(result, "\n")
|
|
|
|
end
|
|
|
|
|
2017-01-02 22:57:43 -07:00
|
|
|
|
2016-10-31 23:50:49 -07:00
|
|
|
local cdef = ffi.cdef
|
|
|
|
|
|
|
|
local cimportstr
|
|
|
|
|
2023-04-03 02:27:14 -07:00
|
|
|
local previous_defines_init = [[
|
|
|
|
typedef struct { char bytes[16]; } __attribute__((aligned(16))) __uint128_t;
|
|
|
|
typedef struct { char bytes[16]; } __attribute__((aligned(16))) __float128;
|
|
|
|
]]
|
|
|
|
|
2023-04-03 04:01:23 -07:00
|
|
|
local preprocess_cache_init = {} --- @type table<string,string>
|
2017-03-11 06:02:47 -07:00
|
|
|
local previous_defines_mod = ''
|
2023-04-03 04:01:23 -07:00
|
|
|
local preprocess_cache_mod = nil --- @type table<string,string>
|
2017-03-11 06:02:47 -07:00
|
|
|
|
2017-03-11 17:14:34 -07:00
|
|
|
local function is_child_cdefs()
|
2023-04-03 04:01:23 -07:00
|
|
|
return os.getenv('NVIM_TEST_MAIN_CDEFS') ~= '1'
|
2017-03-11 17:14:34 -07:00
|
|
|
end
|
|
|
|
|
2014-08-30 06:35:42 -07:00
|
|
|
-- use this helper to import C files, you can pass multiple paths at once,
|
|
|
|
-- this helper will return the C namespace of the nvim library.
|
2023-04-03 04:01:23 -07:00
|
|
|
local function cimport(...)
|
|
|
|
local previous_defines --- @type string
|
|
|
|
local preprocess_cache --- @type table<string,string>
|
|
|
|
local cdefs
|
2017-03-11 17:14:34 -07:00
|
|
|
if is_child_cdefs() and preprocess_cache_mod then
|
2017-03-11 06:02:47 -07:00
|
|
|
preprocess_cache = preprocess_cache_mod
|
|
|
|
previous_defines = previous_defines_mod
|
2017-03-11 17:02:14 -07:00
|
|
|
cdefs = cdefs_mod
|
2017-03-11 06:02:47 -07:00
|
|
|
else
|
|
|
|
preprocess_cache = preprocess_cache_init
|
|
|
|
previous_defines = previous_defines_init
|
2017-03-11 17:02:14 -07:00
|
|
|
cdefs = cdefs_init
|
2014-08-30 06:35:42 -07:00
|
|
|
end
|
2017-03-11 06:02:47 -07:00
|
|
|
for _, path in ipairs({...}) do
|
|
|
|
if not (path:sub(1, 1) == '/' or path:sub(1, 1) == '.'
|
|
|
|
or path:sub(2, 2) == ':') then
|
|
|
|
path = './' .. path
|
|
|
|
end
|
2017-03-11 13:48:16 -07:00
|
|
|
if not preprocess_cache[path] then
|
2023-04-03 04:01:23 -07:00
|
|
|
local body --- @type string
|
2017-03-11 06:02:47 -07:00
|
|
|
body, previous_defines = Preprocess.preprocess(previous_defines, path)
|
|
|
|
-- format it (so that the lines are "unique" statements), also filter out
|
|
|
|
-- Objective-C blocks
|
|
|
|
if os.getenv('NVIM_TEST_PRINT_I') == '1' then
|
|
|
|
local lnum = 0
|
|
|
|
for line in body:gmatch('[^\n]+') do
|
|
|
|
lnum = lnum + 1
|
|
|
|
print(lnum, line)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
body = formatc(body)
|
|
|
|
body = filter_complex_blocks(body)
|
2017-03-11 17:02:14 -07:00
|
|
|
-- add the formatted lines to a set
|
|
|
|
local new_cdefs = Set:new()
|
|
|
|
for line in body:gmatch("[^\r\n]+") do
|
|
|
|
line = trim(line)
|
|
|
|
-- give each #pragma pack an unique id, so that they don't get removed
|
|
|
|
-- if they are inserted into the set
|
|
|
|
-- (they are needed in the right order with the struct definitions,
|
|
|
|
-- otherwise luajit has wrong memory layouts for the sturcts)
|
|
|
|
if line:match("#pragma%s+pack") then
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @type string
|
2017-03-11 17:02:14 -07:00
|
|
|
line = line .. " // " .. pragma_pack_id
|
|
|
|
pragma_pack_id = pragma_pack_id + 1
|
|
|
|
end
|
|
|
|
new_cdefs:add(line)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- subtract the lines we've already imported from the new lines, then add
|
|
|
|
-- the new unique lines to the old lines (so they won't be imported again)
|
|
|
|
new_cdefs:diff(cdefs)
|
|
|
|
cdefs:union(new_cdefs)
|
|
|
|
-- request a sorted version of the new lines (same relative order as the
|
|
|
|
-- original preprocessed file) and feed that to the LuaJIT ffi
|
|
|
|
local new_lines = new_cdefs:to_table()
|
|
|
|
if os.getenv('NVIM_TEST_PRINT_CDEF') == '1' then
|
|
|
|
for lnum, line in ipairs(new_lines) do
|
|
|
|
print(lnum, line)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
body = table.concat(new_lines, '\n')
|
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
preprocess_cache[path] = body
|
|
|
|
end
|
|
|
|
cimportstr(preprocess_cache, path)
|
2014-08-30 06:35:42 -07:00
|
|
|
end
|
2017-03-11 06:02:47 -07:00
|
|
|
return lib
|
|
|
|
end
|
2014-08-30 06:35:42 -07:00
|
|
|
|
2023-04-03 04:01:23 -07:00
|
|
|
local function cimport_immediate(...)
|
2017-03-11 13:48:16 -07:00
|
|
|
local saved_pid = child_pid
|
|
|
|
child_pid = 0
|
2017-03-11 06:02:47 -07:00
|
|
|
local err, emsg = pcall(cimport, ...)
|
2017-03-11 13:48:16 -07:00
|
|
|
child_pid = saved_pid
|
2017-03-11 06:02:47 -07:00
|
|
|
if not err then
|
2023-04-03 04:01:23 -07:00
|
|
|
io.stderr:write(tostring(emsg) .. '\n')
|
2017-03-11 06:02:47 -07:00
|
|
|
assert(false)
|
|
|
|
else
|
|
|
|
return lib
|
2014-08-30 06:35:42 -07:00
|
|
|
end
|
2016-10-31 23:50:49 -07:00
|
|
|
end
|
|
|
|
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @param preprocess_cache table<string,string[]>
|
|
|
|
--- @param path string
|
2017-03-11 17:14:34 -07:00
|
|
|
local function _cimportstr(preprocess_cache, path)
|
2017-03-11 06:02:47 -07:00
|
|
|
if imported:contains(path) then
|
|
|
|
return lib
|
2017-02-24 14:22:46 -07:00
|
|
|
end
|
2017-03-11 06:02:47 -07:00
|
|
|
local body = preprocess_cache[path]
|
2017-03-11 17:02:14 -07:00
|
|
|
if body == '' then
|
2017-03-11 06:02:47 -07:00
|
|
|
return lib
|
2014-08-30 06:35:42 -07:00
|
|
|
end
|
2017-03-11 17:02:14 -07:00
|
|
|
cdef(body)
|
2017-03-11 06:02:47 -07:00
|
|
|
imported:add(path)
|
2014-08-30 06:35:42 -07:00
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
return lib
|
2017-03-11 17:14:34 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if is_child_cdefs() then
|
|
|
|
cimportstr = child_call(_cimportstr, lib)
|
|
|
|
else
|
|
|
|
cimportstr = _cimportstr
|
|
|
|
end
|
2014-08-30 06:35:42 -07:00
|
|
|
|
2017-01-07 09:12:18 -07:00
|
|
|
local function alloc_log_new()
|
2017-01-07 03:52:51 -07:00
|
|
|
local log = {
|
2023-04-03 04:01:23 -07:00
|
|
|
log={}, --- @type ChildCallLog[]
|
|
|
|
lib=cimport('./src/nvim/memory.h'), --- @type table<string,function>
|
|
|
|
original_functions={}, --- @type table<string,function>
|
2017-01-07 03:52:51 -07:00
|
|
|
null={['\0:is_null']=true},
|
|
|
|
}
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-01-07 03:52:51 -07:00
|
|
|
local allocator_functions = {'malloc', 'free', 'calloc', 'realloc'}
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-01-07 03:52:51 -07:00
|
|
|
function log:save_original_functions()
|
|
|
|
for _, funcname in ipairs(allocator_functions) do
|
2017-03-11 06:02:47 -07:00
|
|
|
if not self.original_functions[funcname] then
|
|
|
|
self.original_functions[funcname] = self.lib['mem_' .. funcname]
|
|
|
|
end
|
2017-01-07 03:52:51 -07:00
|
|
|
end
|
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-03-11 16:54:23 -07:00
|
|
|
log.save_original_functions = child_call(log.save_original_functions)
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-01-07 03:52:51 -07:00
|
|
|
function log:set_mocks()
|
|
|
|
for _, k in ipairs(allocator_functions) do
|
|
|
|
do
|
|
|
|
local kk = k
|
|
|
|
self.lib['mem_' .. k] = function(...)
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @type ChildCallLog
|
|
|
|
local log_entry = { func = kk, args = {...} }
|
2017-01-07 03:52:51 -07:00
|
|
|
self.log[#self.log + 1] = log_entry
|
|
|
|
if kk == 'free' then
|
|
|
|
self.original_functions[kk](...)
|
|
|
|
else
|
|
|
|
log_entry.ret = self.original_functions[kk](...)
|
|
|
|
end
|
|
|
|
for i, v in ipairs(log_entry.args) do
|
|
|
|
if v == nil then
|
|
|
|
-- XXX This thing thinks that {NULL} ~= {NULL}.
|
|
|
|
log_entry.args[i] = self.null
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if self.hook then self:hook(log_entry) end
|
|
|
|
if log_entry.ret then
|
|
|
|
return log_entry.ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-03-11 16:54:23 -07:00
|
|
|
log.set_mocks = child_call(log.set_mocks)
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-01-07 03:52:51 -07:00
|
|
|
function log:clear()
|
|
|
|
self.log = {}
|
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-01-07 03:52:51 -07:00
|
|
|
function log:check(exp)
|
|
|
|
eq(exp, self.log)
|
|
|
|
self:clear()
|
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-10-15 17:06:34 -07:00
|
|
|
function log:clear_tmp_allocs(clear_null_frees)
|
2023-04-03 04:01:23 -07:00
|
|
|
local toremove = {} --- @type integer[]
|
|
|
|
local allocs = {} --- @type table<string,integer>
|
2017-03-12 11:58:57 -07:00
|
|
|
for i, v in ipairs(self.log) do
|
|
|
|
if v.func == 'malloc' or v.func == 'calloc' then
|
|
|
|
allocs[tostring(v.ret)] = i
|
|
|
|
elseif v.func == 'realloc' or v.func == 'free' then
|
|
|
|
if allocs[tostring(v.args[1])] then
|
|
|
|
toremove[#toremove + 1] = allocs[tostring(v.args[1])]
|
|
|
|
if v.func == 'free' then
|
|
|
|
toremove[#toremove + 1] = i
|
|
|
|
end
|
2017-10-15 17:06:34 -07:00
|
|
|
elseif clear_null_frees and v.args[1] == self.null then
|
|
|
|
toremove[#toremove + 1] = i
|
2017-03-12 11:58:57 -07:00
|
|
|
end
|
|
|
|
if v.func == 'realloc' then
|
|
|
|
allocs[tostring(v.ret)] = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(toremove)
|
|
|
|
for i = #toremove,1,-1 do
|
|
|
|
table.remove(self.log, toremove[i])
|
|
|
|
end
|
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
function log:setup()
|
2017-01-07 03:52:51 -07:00
|
|
|
log:save_original_functions()
|
|
|
|
log:set_mocks()
|
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
function log:before_each()
|
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-01-07 03:52:51 -07:00
|
|
|
function log:after_each()
|
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
log:setup()
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-01-07 03:52:51 -07:00
|
|
|
return log
|
|
|
|
end
|
|
|
|
|
2014-08-30 06:35:42 -07:00
|
|
|
-- take a pointer to a C-allocated string and return an interned
|
|
|
|
-- version while also freeing the memory
|
2014-09-06 15:36:00 -07:00
|
|
|
local function internalize(cdata, len)
|
2014-08-30 06:35:42 -07:00
|
|
|
ffi.gc(cdata, ffi.C.free)
|
|
|
|
return ffi.string(cdata, len)
|
|
|
|
end
|
|
|
|
|
|
|
|
local cstr = ffi.typeof('char[?]')
|
2014-09-06 15:36:00 -07:00
|
|
|
local function to_cstr(string)
|
2017-01-03 12:51:29 -07:00
|
|
|
return cstr(#string + 1, string)
|
2014-08-30 06:35:42 -07:00
|
|
|
end
|
|
|
|
|
2023-01-18 04:12:13 -07:00
|
|
|
cimport_immediate('./test/unit/fixtures/posix.h')
|
2023-04-03 04:01:23 -07:00
|
|
|
|
|
|
|
local sc = {}
|
|
|
|
|
|
|
|
function sc.fork()
|
|
|
|
return tonumber(ffi.C.fork())
|
|
|
|
end
|
|
|
|
|
|
|
|
function sc.pipe()
|
|
|
|
local ret = ffi.new('int[2]', {-1, -1})
|
|
|
|
ffi.errno(0)
|
|
|
|
local res = ffi.C.pipe(ret)
|
|
|
|
if (res ~= 0) then
|
|
|
|
local err = ffi.errno(0)
|
|
|
|
assert(res == 0, ("pipe() error: %u: %s"):format(
|
|
|
|
err, ffi.string(ffi.C.strerror(err))))
|
|
|
|
end
|
|
|
|
assert(ret[0] ~= -1 and ret[1] ~= -1)
|
|
|
|
return ret[0], ret[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @return string
|
|
|
|
function sc.read(rd, len)
|
|
|
|
local ret = ffi.new('char[?]', len, {0})
|
|
|
|
local total_bytes_read = 0
|
|
|
|
ffi.errno(0)
|
|
|
|
while total_bytes_read < len do
|
|
|
|
local bytes_read = tonumber(ffi.C.read(
|
|
|
|
rd,
|
|
|
|
ffi.cast('void*', ret + total_bytes_read),
|
|
|
|
len - total_bytes_read))
|
|
|
|
if bytes_read == -1 then
|
2023-01-18 04:12:13 -07:00
|
|
|
local err = ffi.errno(0)
|
2023-04-03 04:01:23 -07:00
|
|
|
if err ~= ffi.C.kPOSIXErrnoEINTR then
|
|
|
|
assert(false, ("read() error: %u: %s"):format(
|
|
|
|
err, ffi.string(ffi.C.strerror(err))))
|
2016-11-01 00:38:03 -07:00
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
elseif bytes_read == 0 then
|
|
|
|
break
|
|
|
|
else
|
|
|
|
total_bytes_read = total_bytes_read + bytes_read
|
2023-01-18 04:12:13 -07:00
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
end
|
|
|
|
return ffi.string(ret, total_bytes_read)
|
|
|
|
end
|
|
|
|
|
|
|
|
function sc.write(wr, s)
|
|
|
|
local wbuf = to_cstr(s)
|
|
|
|
local total_bytes_written = 0
|
|
|
|
ffi.errno(0)
|
|
|
|
while total_bytes_written < #s do
|
|
|
|
local bytes_written = tonumber(ffi.C.write(
|
|
|
|
wr,
|
|
|
|
ffi.cast('void*', wbuf + total_bytes_written),
|
|
|
|
#s - total_bytes_written))
|
|
|
|
if bytes_written == -1 then
|
|
|
|
local err = ffi.errno(0)
|
|
|
|
if err ~= ffi.C.kPOSIXErrnoEINTR then
|
|
|
|
assert(false, ("write() error: %u: %s ('%s')"):format(
|
|
|
|
err, ffi.string(ffi.C.strerror(err)), s))
|
2016-11-01 00:38:03 -07:00
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
elseif bytes_written == 0 then
|
|
|
|
break
|
|
|
|
else
|
|
|
|
total_bytes_written = total_bytes_written + bytes_written
|
2023-01-18 04:12:13 -07:00
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
end
|
|
|
|
return total_bytes_written
|
|
|
|
end
|
|
|
|
|
|
|
|
sc.close = ffi.C.close
|
|
|
|
|
|
|
|
--- @param pid integer
|
|
|
|
--- @return integer
|
|
|
|
function sc.wait(pid)
|
|
|
|
ffi.errno(0)
|
|
|
|
local stat_loc = ffi.new('int[1]', {0})
|
|
|
|
while true do
|
|
|
|
local r = ffi.C.waitpid(pid, stat_loc, ffi.C.kPOSIXWaitWUNTRACED)
|
|
|
|
if r == -1 then
|
|
|
|
local err = ffi.errno(0)
|
|
|
|
if err == ffi.C.kPOSIXErrnoECHILD then
|
|
|
|
break
|
|
|
|
elseif err ~= ffi.C.kPOSIXErrnoEINTR then
|
|
|
|
assert(false, ("waitpid() error: %u: %s"):format(
|
|
|
|
err, ffi.string(ffi.C.strerror(err))))
|
2016-11-01 00:38:03 -07:00
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
else
|
|
|
|
assert(r == pid)
|
2023-01-18 04:12:13 -07:00
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
end
|
|
|
|
return stat_loc[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
sc.exit = ffi.C._exit
|
2016-10-31 23:50:49 -07:00
|
|
|
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @param lst string[]
|
|
|
|
--- @return string
|
2016-11-01 02:20:42 -07:00
|
|
|
local function format_list(lst)
|
2023-04-03 04:01:23 -07:00
|
|
|
local ret = {} --- @type string[]
|
2016-11-01 02:20:42 -07:00
|
|
|
for _, v in ipairs(lst) do
|
2023-04-03 04:01:23 -07:00
|
|
|
ret[#ret+1] = assert:format({v, n=1})[1]
|
2016-11-01 02:20:42 -07:00
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
return table.concat(ret, ', ')
|
2016-11-01 02:20:42 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if os.getenv('NVIM_TEST_PRINT_SYSCALLS') == '1' then
|
|
|
|
for k_, v_ in pairs(sc) do
|
|
|
|
(function(k, v)
|
|
|
|
sc[k] = function(...)
|
2016-11-04 13:57:28 -07:00
|
|
|
local rets = {v(...)}
|
2016-11-01 02:20:42 -07:00
|
|
|
io.stderr:write(('%s(%s) = %s\n'):format(k, format_list({...}),
|
|
|
|
format_list(rets)))
|
|
|
|
return unpack(rets)
|
|
|
|
end
|
|
|
|
end)(k_, v_)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-01 01:19:41 -07:00
|
|
|
local function just_fail(_)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
say:set('assertion.just_fail.positive', '%s')
|
|
|
|
say:set('assertion.just_fail.negative', '%s')
|
|
|
|
assert:register('assertion', 'just_fail', just_fail,
|
|
|
|
'assertion.just_fail.positive',
|
|
|
|
'assertion.just_fail.negative')
|
|
|
|
|
2017-04-01 02:25:10 -07:00
|
|
|
local hook_fnamelen = 30
|
|
|
|
local hook_sfnamelen = 30
|
|
|
|
local hook_numlen = 5
|
|
|
|
local hook_msglen = 1 + 1 + 1 + (1 + hook_fnamelen) + (1 + hook_sfnamelen) + (1 + hook_numlen) + 1
|
|
|
|
|
2017-04-01 02:52:28 -07:00
|
|
|
local tracehelp = dedent([[
|
2017-11-11 13:50:37 -07:00
|
|
|
Trace: either in the format described below or custom debug output starting
|
|
|
|
with `>`. Latter lines still have the same width in byte.
|
|
|
|
|
2017-04-01 02:52:28 -07:00
|
|
|
┌ Trace type: _r_eturn from function , function _c_all, _l_ine executed,
|
2017-04-02 04:25:47 -07:00
|
|
|
│ _t_ail return, _C_ount (should not actually appear),
|
2017-11-11 13:50:37 -07:00
|
|
|
│ _s_aved from previous run for reference, _>_ for custom debug
|
|
|
|
│ output.
|
2017-04-01 02:52:28 -07:00
|
|
|
│┏ Function type: _L_ua function, _C_ function, _m_ain part of chunk,
|
|
|
|
│┃ function that did _t_ail call.
|
|
|
|
│┃┌ Function name type: _g_lobal, _l_ocal, _m_ethod, _f_ield, _u_pvalue,
|
|
|
|
│┃│ space for unknown.
|
|
|
|
│┃│ ┏ Source file name ┌ Function name ┏ Line
|
|
|
|
│┃│ ┃ (trunc to 30 bytes, no .lua) │ (truncated to last 30 bytes) ┃ number
|
|
|
|
CWN SSSSSSSSSSSSSSSSSSSSSSSSSSSSSS:FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:LLLLL\n
|
|
|
|
]])
|
|
|
|
|
2017-04-01 02:25:10 -07:00
|
|
|
local function child_sethook(wr)
|
2023-04-03 04:01:23 -07:00
|
|
|
local trace_level_str = os.getenv('NVIM_TEST_TRACE_LEVEL')
|
|
|
|
local trace_level = 0
|
|
|
|
if trace_level_str and trace_level_str ~= '' then
|
|
|
|
--- @type number
|
|
|
|
trace_level = assert(tonumber(trace_level_str))
|
2017-04-01 10:57:23 -07:00
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-04-01 10:57:23 -07:00
|
|
|
if trace_level <= 0 then
|
2017-04-01 02:25:10 -07:00
|
|
|
return
|
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
|
2017-04-01 10:57:23 -07:00
|
|
|
local trace_only_c = trace_level <= 1
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @type debuginfo?, string?, integer
|
2017-04-02 04:25:47 -07:00
|
|
|
local prev_info, prev_reason, prev_lnum
|
2023-04-03 04:01:23 -07:00
|
|
|
|
|
|
|
--- @param reason string
|
|
|
|
--- @param lnum integer
|
|
|
|
--- @param use_prev boolean
|
2017-04-02 04:25:47 -07:00
|
|
|
local function hook(reason, lnum, use_prev)
|
2023-04-03 04:01:23 -07:00
|
|
|
local info = nil --- @type debuginfo?
|
2017-04-02 04:25:47 -07:00
|
|
|
if use_prev then
|
|
|
|
info = prev_info
|
|
|
|
elseif reason ~= 'tail return' then -- tail return
|
2017-04-01 02:25:10 -07:00
|
|
|
info = debug.getinfo(2, 'nSl')
|
|
|
|
end
|
2017-04-01 03:16:25 -07:00
|
|
|
|
2017-04-02 04:25:47 -07:00
|
|
|
if trace_only_c and (not info or info.what ~= 'C') and not use_prev then
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @cast info -nil
|
2017-04-02 04:25:47 -07:00
|
|
|
if info.source:sub(-9) == '_spec.lua' then
|
|
|
|
prev_info = info
|
|
|
|
prev_reason = 'saved'
|
|
|
|
prev_lnum = lnum
|
|
|
|
end
|
2017-04-01 03:16:25 -07:00
|
|
|
return
|
|
|
|
end
|
2017-04-02 04:25:47 -07:00
|
|
|
if trace_only_c and not use_prev and prev_reason then
|
|
|
|
hook(prev_reason, prev_lnum, true)
|
|
|
|
prev_reason = nil
|
|
|
|
end
|
2017-04-01 03:16:25 -07:00
|
|
|
|
2017-04-01 02:25:10 -07:00
|
|
|
local whatchar = ' '
|
|
|
|
local namewhatchar = ' '
|
|
|
|
local funcname = ''
|
|
|
|
local source = ''
|
2017-04-01 03:16:25 -07:00
|
|
|
local msgchar = reason:sub(1, 1)
|
|
|
|
|
|
|
|
if reason == 'count' then
|
|
|
|
msgchar = 'C'
|
|
|
|
end
|
|
|
|
|
2017-04-01 02:25:10 -07:00
|
|
|
if info then
|
|
|
|
funcname = (info.name or ''):sub(1, hook_fnamelen)
|
|
|
|
whatchar = info.what:sub(1, 1)
|
|
|
|
namewhatchar = info.namewhat:sub(1, 1)
|
|
|
|
if namewhatchar == '' then
|
|
|
|
namewhatchar = ' '
|
|
|
|
end
|
|
|
|
source = info.source
|
|
|
|
if source:sub(1, 1) == '@' then
|
|
|
|
if source:sub(-4, -1) == '.lua' then
|
|
|
|
source = source:sub(1, -5)
|
|
|
|
end
|
|
|
|
source = source:sub(-hook_sfnamelen, -1)
|
|
|
|
end
|
|
|
|
lnum = lnum or info.currentline
|
|
|
|
end
|
|
|
|
|
|
|
|
-- assert(-1 <= lnum and lnum <= 99999)
|
2023-04-03 04:01:23 -07:00
|
|
|
local lnum_s = lnum == -1 and 'nknwn' or ('%u'):format(lnum)
|
|
|
|
--- @type string
|
2017-04-01 02:25:10 -07:00
|
|
|
local msg = ( -- lua does not support %*
|
|
|
|
''
|
|
|
|
.. msgchar
|
|
|
|
.. whatchar
|
|
|
|
.. namewhatchar
|
|
|
|
.. ' '
|
|
|
|
.. source .. (' '):rep(hook_sfnamelen - #source)
|
|
|
|
.. ':'
|
|
|
|
.. funcname .. (' '):rep(hook_fnamelen - #funcname)
|
|
|
|
.. ':'
|
|
|
|
.. ('0'):rep(hook_numlen - #lnum_s) .. lnum_s
|
|
|
|
.. '\n'
|
|
|
|
)
|
|
|
|
-- eq(hook_msglen, #msg)
|
|
|
|
sc.write(wr, msg)
|
|
|
|
end
|
2017-04-02 04:25:47 -07:00
|
|
|
debug.sethook(hook, 'crl')
|
2017-04-01 02:25:10 -07:00
|
|
|
end
|
|
|
|
|
2017-04-01 03:16:25 -07:00
|
|
|
local trace_end_msg = ('E%s\n'):format((' '):rep(hook_msglen - 2))
|
|
|
|
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @type function
|
2017-11-11 13:50:37 -07:00
|
|
|
local _debug_log
|
|
|
|
|
|
|
|
local debug_log = only_separate(function(...)
|
|
|
|
return _debug_log(...)
|
|
|
|
end)
|
|
|
|
|
2017-04-01 01:19:41 -07:00
|
|
|
local function itp_child(wr, func)
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @param s string
|
2017-11-11 13:50:37 -07:00
|
|
|
_debug_log = function(s)
|
|
|
|
s = s:sub(1, hook_msglen - 2)
|
|
|
|
sc.write(wr, '>' .. s .. (' '):rep(hook_msglen - 2 - #s) .. '\n')
|
|
|
|
end
|
2019-02-24 12:09:14 -07:00
|
|
|
local status, result = pcall(init)
|
|
|
|
if status then
|
2017-11-19 11:46:54 -07:00
|
|
|
collectgarbage('stop')
|
|
|
|
child_sethook(wr)
|
2019-02-24 12:09:14 -07:00
|
|
|
status, result = pcall(func)
|
2017-11-19 11:46:54 -07:00
|
|
|
debug.sethook()
|
|
|
|
end
|
2017-04-01 03:16:25 -07:00
|
|
|
sc.write(wr, trace_end_msg)
|
2019-02-24 12:09:14 -07:00
|
|
|
if not status then
|
|
|
|
local emsg = tostring(result)
|
2017-04-01 02:25:10 -07:00
|
|
|
if #emsg > 99999 then
|
|
|
|
emsg = emsg:sub(1, 99999)
|
|
|
|
end
|
2017-04-01 01:19:41 -07:00
|
|
|
sc.write(wr, ('-\n%05u\n%s'):format(#emsg, emsg))
|
|
|
|
deinit()
|
|
|
|
else
|
|
|
|
sc.write(wr, '+\n')
|
|
|
|
deinit()
|
|
|
|
end
|
2017-12-24 07:42:23 -07:00
|
|
|
collectgarbage('restart')
|
|
|
|
collectgarbage()
|
|
|
|
sc.write(wr, '$\n')
|
|
|
|
sc.close(wr)
|
2019-02-24 12:09:14 -07:00
|
|
|
sc.exit(status and 0 or 1)
|
2017-04-01 01:19:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function check_child_err(rd)
|
2023-04-03 04:01:23 -07:00
|
|
|
local trace = {} --- @type string[]
|
2017-04-05 22:23:33 -07:00
|
|
|
local did_traceline = false
|
2017-11-30 01:44:48 -07:00
|
|
|
local maxtrace = tonumber(os.getenv('NVIM_TEST_MAXTRACE')) or 1024
|
2017-04-01 02:25:10 -07:00
|
|
|
while true do
|
|
|
|
local traceline = sc.read(rd, hook_msglen)
|
|
|
|
if #traceline ~= hook_msglen then
|
|
|
|
if #traceline == 0 then
|
|
|
|
break
|
|
|
|
else
|
|
|
|
trace[#trace + 1] = 'Partial read: <' .. trace .. '>\n'
|
|
|
|
end
|
|
|
|
end
|
2017-04-01 03:16:25 -07:00
|
|
|
if traceline == trace_end_msg then
|
2017-04-05 22:23:33 -07:00
|
|
|
did_traceline = true
|
2017-04-01 02:25:10 -07:00
|
|
|
break
|
|
|
|
end
|
|
|
|
trace[#trace + 1] = traceline
|
2017-12-24 07:13:42 -07:00
|
|
|
if #trace > maxtrace then
|
|
|
|
table.remove(trace, 1)
|
|
|
|
end
|
2017-04-01 02:25:10 -07:00
|
|
|
end
|
2017-04-01 01:19:41 -07:00
|
|
|
local res = sc.read(rd, 2)
|
2017-12-24 07:42:23 -07:00
|
|
|
if #res == 2 then
|
|
|
|
local err = ''
|
|
|
|
if res ~= '+\n' then
|
|
|
|
eq('-\n', res)
|
|
|
|
local len_s = sc.read(rd, 5)
|
|
|
|
local len = tonumber(len_s)
|
|
|
|
neq(0, len)
|
|
|
|
if os.getenv('NVIM_TEST_TRACE_ON_ERROR') == '1' and #trace ~= 0 then
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @type string
|
2017-12-24 07:42:23 -07:00
|
|
|
err = '\nTest failed, trace:\n' .. tracehelp
|
|
|
|
for _, traceline in ipairs(trace) do
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @type string
|
2017-12-24 07:42:23 -07:00
|
|
|
err = err .. traceline
|
|
|
|
end
|
2017-04-01 10:57:23 -07:00
|
|
|
end
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @type string
|
2017-12-24 07:42:23 -07:00
|
|
|
err = err .. sc.read(rd, len + 1)
|
2017-04-01 02:25:10 -07:00
|
|
|
end
|
2017-12-24 07:42:23 -07:00
|
|
|
local eres = sc.read(rd, 2)
|
|
|
|
if eres ~= '$\n' then
|
|
|
|
if #trace == 0 then
|
2019-10-02 22:41:57 -07:00
|
|
|
err = '\nTest crashed, no trace available (check NVIM_TEST_TRACE_LEVEL)\n'
|
2017-12-24 07:42:23 -07:00
|
|
|
else
|
|
|
|
err = '\nTest crashed, trace:\n' .. tracehelp
|
|
|
|
for i = 1, #trace do
|
|
|
|
err = err .. trace[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not did_traceline then
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @type string
|
2017-12-24 07:42:23 -07:00
|
|
|
err = err .. '\nNo end of trace occurred'
|
|
|
|
end
|
|
|
|
local cc_err, cc_emsg = pcall(check_cores, Paths.test_luajit_prg, true)
|
|
|
|
if not cc_err then
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @type string
|
2017-12-24 07:42:23 -07:00
|
|
|
err = err .. '\ncheck_cores failed: ' .. cc_emsg
|
|
|
|
end
|
2017-04-05 22:23:33 -07:00
|
|
|
end
|
2017-12-24 07:42:23 -07:00
|
|
|
if err ~= '' then
|
|
|
|
assert.just_fail(err)
|
2017-04-05 22:23:33 -07:00
|
|
|
end
|
2017-04-01 02:25:10 -07:00
|
|
|
end
|
2017-04-01 01:19:41 -07:00
|
|
|
end
|
|
|
|
|
2023-01-18 03:54:49 -07:00
|
|
|
local function itp_parent(rd, pid, allow_failure, location)
|
|
|
|
local ok, emsg = pcall(check_child_err, rd)
|
|
|
|
local status = sc.wait(pid)
|
2017-04-01 01:19:41 -07:00
|
|
|
sc.close(rd)
|
2023-01-18 03:54:49 -07:00
|
|
|
if not ok then
|
2017-04-01 01:19:41 -07:00
|
|
|
if allow_failure then
|
2023-01-18 03:54:49 -07:00
|
|
|
io.stderr:write('Errorred out ('..status..'):\n' .. tostring(emsg) .. '\n')
|
2017-04-01 01:19:41 -07:00
|
|
|
os.execute([[
|
|
|
|
sh -c "source ci/common/test.sh
|
|
|
|
check_core_dumps --delete \"]] .. Paths.test_luajit_prg .. [[\""]])
|
|
|
|
else
|
2023-01-18 03:54:49 -07:00
|
|
|
error(tostring(emsg)..'\nexit code: '..status)
|
|
|
|
end
|
|
|
|
elseif status ~= 0 then
|
|
|
|
if not allow_failure then
|
|
|
|
error("child process errored out with status "..status.."!\n\n"..location)
|
2017-04-01 01:19:41 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-25 13:48:22 -07:00
|
|
|
local function gen_itp(it)
|
2017-03-11 16:54:23 -07:00
|
|
|
child_calls_mod = {}
|
|
|
|
child_calls_mod_once = {}
|
|
|
|
child_cleanups_mod_once = {}
|
2017-03-11 06:02:47 -07:00
|
|
|
preprocess_cache_mod = map(function(v) return v end, preprocess_cache_init)
|
|
|
|
previous_defines_mod = previous_defines_init
|
2017-03-11 17:02:14 -07:00
|
|
|
cdefs_mod = cdefs_init:copy()
|
2016-11-04 08:20:58 -07:00
|
|
|
local function itp(name, func, allow_failure)
|
2016-11-04 14:01:25 -07:00
|
|
|
if allow_failure and os.getenv('NVIM_TEST_RUN_FAILING_TESTS') ~= '1' then
|
|
|
|
-- FIXME Fix tests with this true
|
|
|
|
return
|
|
|
|
end
|
2023-01-18 03:54:49 -07:00
|
|
|
|
|
|
|
-- Pre-emptively calculating error location, wasteful, ugh!
|
|
|
|
-- But the way this code messes around with busted implies the real location is strictly
|
|
|
|
-- not available in the parent when an actual error occurs. so we have to do this here.
|
|
|
|
local location = debug.traceback()
|
2016-09-25 13:48:22 -07:00
|
|
|
it(name, function()
|
2016-10-31 23:50:49 -07:00
|
|
|
local rd, wr = sc.pipe()
|
2017-03-11 13:48:16 -07:00
|
|
|
child_pid = sc.fork()
|
|
|
|
if child_pid == 0 then
|
2016-10-31 23:50:49 -07:00
|
|
|
sc.close(rd)
|
2017-04-01 01:19:41 -07:00
|
|
|
itp_child(wr, func)
|
2016-09-25 13:48:22 -07:00
|
|
|
else
|
2016-10-31 23:50:49 -07:00
|
|
|
sc.close(wr)
|
2017-04-01 03:17:25 -07:00
|
|
|
local saved_child_pid = child_pid
|
2017-03-11 13:48:16 -07:00
|
|
|
child_pid = nil
|
2023-01-18 03:54:49 -07:00
|
|
|
itp_parent(rd, saved_child_pid, allow_failure, location)
|
2016-09-25 13:48:22 -07:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
return itp
|
|
|
|
end
|
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
local function cppimport(path)
|
2017-12-03 06:49:30 -07:00
|
|
|
return cimport(Paths.test_source_path .. '/test/includes/pre/' .. path)
|
2017-03-11 06:02:47 -07:00
|
|
|
end
|
|
|
|
|
2022-05-20 20:18:17 -07:00
|
|
|
cimport('./src/nvim/types.h', './src/nvim/main.h', './src/nvim/os/time.h',
|
|
|
|
'./src/nvim/os/fs.h')
|
2017-03-11 06:02:47 -07:00
|
|
|
|
2017-09-03 09:57:24 -07:00
|
|
|
local function conv_enum(etab, eval)
|
|
|
|
local n = tonumber(eval)
|
|
|
|
return etab[n] or n
|
|
|
|
end
|
|
|
|
|
|
|
|
local function array_size(arr)
|
|
|
|
return ffi.sizeof(arr) / ffi.sizeof(arr[0])
|
|
|
|
end
|
|
|
|
|
|
|
|
local function kvi_size(kvi)
|
|
|
|
return array_size(kvi.init_array)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function kvi_init(kvi)
|
|
|
|
kvi.capacity = kvi_size(kvi)
|
|
|
|
kvi.items = kvi.init_array
|
|
|
|
return kvi
|
|
|
|
end
|
|
|
|
|
2017-10-15 17:06:34 -07:00
|
|
|
local function kvi_destroy(kvi)
|
|
|
|
if kvi.items ~= kvi.init_array then
|
|
|
|
lib.xfree(kvi.items)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-03 09:57:24 -07:00
|
|
|
local function kvi_new(ct)
|
|
|
|
return kvi_init(ffi.new(ct))
|
|
|
|
end
|
|
|
|
|
2017-10-29 15:48:32 -07:00
|
|
|
local function make_enum_conv_tab(m, values, skip_pref, set_cb)
|
2017-09-03 11:58:16 -07:00
|
|
|
child_call_once(function()
|
|
|
|
local ret = {}
|
|
|
|
for _, v in ipairs(values) do
|
|
|
|
local str_v = v
|
|
|
|
if v:sub(1, #skip_pref) == skip_pref then
|
|
|
|
str_v = v:sub(#skip_pref + 1)
|
|
|
|
end
|
2017-10-29 15:48:32 -07:00
|
|
|
ret[tonumber(m[v])] = str_v
|
2017-09-03 11:58:16 -07:00
|
|
|
end
|
|
|
|
set_cb(ret)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function ptr2addr(ptr)
|
|
|
|
return tonumber(ffi.cast('intptr_t', ffi.cast('void *', ptr)))
|
|
|
|
end
|
|
|
|
|
|
|
|
local s = ffi.new('char[64]', {0})
|
|
|
|
|
|
|
|
local function ptr2key(ptr)
|
|
|
|
ffi.C.snprintf(s, ffi.sizeof(s), '%p', ffi.cast('void *', ptr))
|
|
|
|
return ffi.string(s)
|
|
|
|
end
|
|
|
|
|
feat(extmark): support proper multiline ranges
The removes the previous restriction that nvim_buf_set_extmark()
could not be used to highlight arbitrary multi-line regions
The problem can be summarized as follows: let's assume an extmark with a
hl_group is placed covering the region (5,0) to (50,0) Now, consider
what happens if nvim needs to redraw a window covering the lines 20-30.
It needs to be able to ask the marktree what extmarks cover this region,
even if they don't begin or end here.
Therefore the marktree needs to be augmented with the information covers
a point, not just what marks begin or end there. To do this, we augment
each node with a field "intersect" which is a set the ids of the
marks which overlap this node, but only if it is not part of the set of
any parent. This ensures the number of nodes that need to be explicitly
marked grows only logarithmically with the total number of explicitly
nodes (and thus the number of of overlapping marks).
Thus we can quickly iterate all marks which overlaps any query position
by looking up what leaf node contains that position. Then we only need
to consider all "start" marks within that leaf node, and the "intersect"
set of that node and all its parents.
Now, and the major source of complexity is that the tree restructuring
operations (to ensure that each node has T-1 <= size <= 2*T-1) also need
to update these sets. If a full inner node is split in two, one of the
new parents might start to completely overlap some ranges and its ids
will need to be moved from its children's sets to its own set.
Similarly, if two undersized nodes gets joined into one, it might no
longer completely overlap some ranges, and now the children which do
needs to have the have the ids in its set instead. And then there are
the pivots! Yes the pivot operations when a child gets moved from one
parent to another.
2020-11-22 02:10:37 -07:00
|
|
|
local function is_asan()
|
|
|
|
cimport('./src/nvim/version.h')
|
|
|
|
local status, res = pcall(function() return lib.version_cflags end)
|
|
|
|
if status then
|
|
|
|
return ffi.string(res):match('-fsanitize=[a-z,]*address')
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-11 03:28:18 -07:00
|
|
|
local module = {
|
2014-08-30 06:35:42 -07:00
|
|
|
cimport = cimport,
|
|
|
|
cppimport = cppimport,
|
|
|
|
internalize = internalize,
|
|
|
|
ffi = ffi,
|
2017-03-11 06:02:47 -07:00
|
|
|
lib = lib,
|
2014-08-30 06:35:42 -07:00
|
|
|
cstr = cstr,
|
|
|
|
to_cstr = to_cstr,
|
2023-04-03 04:01:23 -07:00
|
|
|
NULL = ffi.cast('void*', 0),
|
|
|
|
OK = 1,
|
|
|
|
FAIL = 0,
|
2017-01-07 03:52:51 -07:00
|
|
|
alloc_log_new = alloc_log_new,
|
2016-09-25 13:48:22 -07:00
|
|
|
gen_itp = gen_itp,
|
2017-03-11 06:02:47 -07:00
|
|
|
only_separate = only_separate,
|
2017-03-11 16:54:23 -07:00
|
|
|
child_call_once = child_call_once,
|
|
|
|
child_cleanup_once = child_cleanup_once,
|
2017-04-01 01:19:41 -07:00
|
|
|
sc = sc,
|
2017-09-03 09:57:24 -07:00
|
|
|
conv_enum = conv_enum,
|
2017-10-29 15:48:32 -07:00
|
|
|
array_size = array_size,
|
2017-10-15 17:06:34 -07:00
|
|
|
kvi_destroy = kvi_destroy,
|
2017-09-03 09:57:24 -07:00
|
|
|
kvi_size = kvi_size,
|
|
|
|
kvi_init = kvi_init,
|
|
|
|
kvi_new = kvi_new,
|
2017-09-03 11:58:16 -07:00
|
|
|
make_enum_conv_tab = make_enum_conv_tab,
|
|
|
|
ptr2addr = ptr2addr,
|
|
|
|
ptr2key = ptr2key,
|
2017-11-11 13:50:37 -07:00
|
|
|
debug_log = debug_log,
|
feat(extmark): support proper multiline ranges
The removes the previous restriction that nvim_buf_set_extmark()
could not be used to highlight arbitrary multi-line regions
The problem can be summarized as follows: let's assume an extmark with a
hl_group is placed covering the region (5,0) to (50,0) Now, consider
what happens if nvim needs to redraw a window covering the lines 20-30.
It needs to be able to ask the marktree what extmarks cover this region,
even if they don't begin or end here.
Therefore the marktree needs to be augmented with the information covers
a point, not just what marks begin or end there. To do this, we augment
each node with a field "intersect" which is a set the ids of the
marks which overlap this node, but only if it is not part of the set of
any parent. This ensures the number of nodes that need to be explicitly
marked grows only logarithmically with the total number of explicitly
nodes (and thus the number of of overlapping marks).
Thus we can quickly iterate all marks which overlaps any query position
by looking up what leaf node contains that position. Then we only need
to consider all "start" marks within that leaf node, and the "intersect"
set of that node and all its parents.
Now, and the major source of complexity is that the tree restructuring
operations (to ensure that each node has T-1 <= size <= 2*T-1) also need
to update these sets. If a full inner node is split in two, one of the
new parents might start to completely overlap some ranges and its ids
will need to be moved from its children's sets to its own set.
Similarly, if two undersized nodes gets joined into one, it might no
longer completely overlap some ranges, and now the children which do
needs to have the have the ids in its set instead. And then there are
the pivots! Yes the pivot operations when a child gets moved from one
parent to another.
2020-11-22 02:10:37 -07:00
|
|
|
is_asan = is_asan,
|
2014-08-30 06:35:42 -07:00
|
|
|
}
|
2019-01-17 16:44:35 -07:00
|
|
|
module = global_helpers.tbl_extend('error', module, global_helpers)
|
2017-04-05 22:23:33 -07:00
|
|
|
return function()
|
2017-03-11 03:28:18 -07:00
|
|
|
return module
|
|
|
|
end
|