2024-04-09 04:26:16 -07:00
|
|
|
local t = require('test.unit.testutil')
|
2016-02-08 17:20:16 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local ptr2key = t.ptr2key
|
|
|
|
local cimport = t.cimport
|
|
|
|
local to_cstr = t.to_cstr
|
|
|
|
local ffi = t.ffi
|
|
|
|
local eq = t.eq
|
2016-02-08 17:20:16 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
local eval = cimport(
|
|
|
|
'./src/nvim/eval.h',
|
|
|
|
'./src/nvim/eval/typval.h',
|
|
|
|
'./src/nvim/hashtab.h',
|
|
|
|
'./src/nvim/memory.h'
|
|
|
|
)
|
|
|
|
|
|
|
|
local null_string = { [true] = 'NULL string' }
|
|
|
|
local null_list = { [true] = 'NULL list' }
|
|
|
|
local null_dict = { [true] = 'NULL dict' }
|
|
|
|
local type_key = { [true] = 'type key' }
|
|
|
|
local locks_key = { [true] = 'locks key' }
|
|
|
|
local list_type = { [true] = 'list type' }
|
|
|
|
local dict_type = { [true] = 'dict type' }
|
|
|
|
local func_type = { [true] = 'func type' }
|
|
|
|
local int_type = { [true] = 'int type' }
|
|
|
|
local flt_type = { [true] = 'flt type' }
|
|
|
|
|
|
|
|
local nil_value = { [true] = 'nil' }
|
2016-06-11 10:44:27 -07:00
|
|
|
|
2017-01-03 12:51:29 -07:00
|
|
|
local lua2typvalt
|
|
|
|
|
2017-12-23 15:52:11 -07:00
|
|
|
local function tv_list_item_alloc()
|
|
|
|
return ffi.cast('listitem_T*', eval.xmalloc(ffi.sizeof('listitem_T')))
|
|
|
|
end
|
|
|
|
|
2017-12-23 16:11:46 -07:00
|
|
|
local function tv_list_item_free(li)
|
|
|
|
eval.tv_clear(li.li_tv)
|
|
|
|
eval.xfree(li)
|
|
|
|
end
|
|
|
|
|
2017-01-03 12:51:29 -07:00
|
|
|
local function li_alloc(nogc)
|
2017-12-23 16:11:46 -07:00
|
|
|
local gcfunc = tv_list_item_free
|
2023-12-04 15:32:39 -07:00
|
|
|
if nogc then
|
|
|
|
gcfunc = nil
|
|
|
|
end
|
2017-12-23 15:52:11 -07:00
|
|
|
local li = ffi.gc(tv_list_item_alloc(), gcfunc)
|
2017-01-03 12:51:29 -07:00
|
|
|
li.li_next = nil
|
|
|
|
li.li_prev = nil
|
2023-12-04 15:32:39 -07:00
|
|
|
li.li_tv = { v_type = eval.VAR_UNKNOWN, v_lock = eval.VAR_UNLOCKED }
|
2017-01-03 12:51:29 -07:00
|
|
|
return li
|
|
|
|
end
|
|
|
|
|
2017-03-04 13:28:54 -07:00
|
|
|
local function populate_list(l, lua_l, processed)
|
|
|
|
processed = processed or {}
|
|
|
|
eq(0, l.lv_refcount)
|
|
|
|
l.lv_refcount = 1
|
|
|
|
processed[lua_l] = l
|
|
|
|
for i = 1, #lua_l do
|
|
|
|
local item_tv = ffi.gc(lua2typvalt(lua_l[i], processed), nil)
|
2017-12-23 15:52:11 -07:00
|
|
|
local item_li = tv_list_item_alloc()
|
2017-03-04 13:28:54 -07:00
|
|
|
item_li.li_tv = item_tv
|
|
|
|
eval.tv_list_append(l, item_li)
|
2016-02-08 17:20:16 -07:00
|
|
|
end
|
2017-03-04 13:28:54 -07:00
|
|
|
return l
|
|
|
|
end
|
|
|
|
|
|
|
|
local function populate_dict(d, lua_d, processed)
|
|
|
|
processed = processed or {}
|
|
|
|
eq(0, d.dv_refcount)
|
|
|
|
d.dv_refcount = 1
|
|
|
|
processed[lua_d] = d
|
|
|
|
for k, v in pairs(lua_d) do
|
|
|
|
if type(k) == 'string' then
|
|
|
|
local di = eval.tv_dict_item_alloc(to_cstr(k))
|
|
|
|
local val_tv = ffi.gc(lua2typvalt(v, processed), nil)
|
|
|
|
eval.tv_copy(val_tv, di.di_tv)
|
|
|
|
eval.tv_clear(val_tv)
|
|
|
|
eval.tv_dict_add(d, di)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return d
|
|
|
|
end
|
|
|
|
|
|
|
|
local function populate_partial(pt, lua_pt, processed)
|
|
|
|
processed = processed or {}
|
|
|
|
eq(0, pt.pt_refcount)
|
|
|
|
processed[lua_pt] = pt
|
|
|
|
local argv = nil
|
|
|
|
if lua_pt.args and #lua_pt.args > 0 then
|
|
|
|
argv = ffi.gc(ffi.cast('typval_T*', eval.xmalloc(ffi.sizeof('typval_T') * #lua_pt.args)), nil)
|
|
|
|
for i, arg in ipairs(lua_pt.args) do
|
|
|
|
local arg_tv = ffi.gc(lua2typvalt(arg, processed), nil)
|
|
|
|
argv[i - 1] = arg_tv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local dict = nil
|
|
|
|
if lua_pt.dict then
|
|
|
|
local dict_tv = ffi.gc(lua2typvalt(lua_pt.dict, processed), nil)
|
|
|
|
assert(dict_tv.v_type == eval.VAR_DICT)
|
|
|
|
dict = dict_tv.vval.v_dict
|
|
|
|
end
|
|
|
|
pt.pt_refcount = 1
|
|
|
|
pt.pt_name = eval.xmemdupz(to_cstr(lua_pt.value), #lua_pt.value)
|
|
|
|
pt.pt_auto = not not lua_pt.auto
|
|
|
|
pt.pt_argc = lua_pt.args and #lua_pt.args or 0
|
|
|
|
pt.pt_argv = argv
|
|
|
|
pt.pt_dict = dict
|
|
|
|
return pt
|
2016-02-08 17:20:16 -07:00
|
|
|
end
|
|
|
|
|
2016-04-19 09:17:09 -07:00
|
|
|
local lst2tbl
|
|
|
|
local dct2tbl
|
|
|
|
|
2017-01-03 12:51:29 -07:00
|
|
|
local typvalt2lua
|
2017-03-04 13:28:54 -07:00
|
|
|
|
|
|
|
local function partial2lua(pt, processed)
|
|
|
|
processed = processed or {}
|
|
|
|
local value, auto, dict, argv = nil, nil, nil, nil
|
|
|
|
if pt ~= nil then
|
|
|
|
value = ffi.string(pt.pt_name)
|
|
|
|
auto = pt.pt_auto and true or nil
|
|
|
|
argv = {}
|
|
|
|
for i = 1, pt.pt_argc do
|
|
|
|
argv[i] = typvalt2lua(pt.pt_argv[i - 1], processed)
|
|
|
|
end
|
|
|
|
if pt.pt_dict ~= nil then
|
|
|
|
dict = dct2tbl(pt.pt_dict)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return {
|
2023-12-04 15:32:39 -07:00
|
|
|
[type_key] = func_type,
|
|
|
|
value = value,
|
|
|
|
auto = auto,
|
|
|
|
args = argv,
|
|
|
|
dict = dict,
|
2017-03-04 13:28:54 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
local typvalt2lua_tab = nil
|
2016-06-24 07:38:33 -07:00
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
local function typvalt2lua_tab_init()
|
|
|
|
if typvalt2lua_tab then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
typvalt2lua_tab = {
|
2024-04-08 02:03:20 -07:00
|
|
|
[tonumber(eval.VAR_BOOL)] = function(q)
|
2020-04-28 08:21:50 -07:00
|
|
|
return ({
|
|
|
|
[tonumber(eval.kBoolVarFalse)] = false,
|
|
|
|
[tonumber(eval.kBoolVarTrue)] = true,
|
2024-04-08 02:03:20 -07:00
|
|
|
})[tonumber(q.vval.v_bool)]
|
2020-04-28 08:21:50 -07:00
|
|
|
end,
|
2024-04-08 02:03:20 -07:00
|
|
|
[tonumber(eval.VAR_SPECIAL)] = function(q)
|
2017-03-11 06:02:47 -07:00
|
|
|
return ({
|
2017-03-13 09:49:13 -07:00
|
|
|
[tonumber(eval.kSpecialVarNull)] = nil_value,
|
2024-04-08 02:03:20 -07:00
|
|
|
})[tonumber(q.vval.v_special)]
|
2017-03-11 06:02:47 -07:00
|
|
|
end,
|
2024-04-08 02:03:20 -07:00
|
|
|
[tonumber(eval.VAR_NUMBER)] = function(q)
|
|
|
|
return { [type_key] = int_type, value = tonumber(q.vval.v_number) }
|
2017-03-11 06:02:47 -07:00
|
|
|
end,
|
2024-04-08 02:03:20 -07:00
|
|
|
[tonumber(eval.VAR_FLOAT)] = function(q)
|
|
|
|
return tonumber(q.vval.v_float)
|
2017-03-11 06:02:47 -07:00
|
|
|
end,
|
2024-04-08 02:03:20 -07:00
|
|
|
[tonumber(eval.VAR_STRING)] = function(q)
|
|
|
|
local str = q.vval.v_string
|
2017-03-11 06:02:47 -07:00
|
|
|
if str == nil then
|
|
|
|
return null_string
|
|
|
|
else
|
|
|
|
return ffi.string(str)
|
2017-01-03 12:51:29 -07:00
|
|
|
end
|
2017-03-11 06:02:47 -07:00
|
|
|
end,
|
2024-04-08 02:03:20 -07:00
|
|
|
[tonumber(eval.VAR_LIST)] = function(q, processed)
|
|
|
|
return lst2tbl(q.vval.v_list, processed)
|
2017-03-11 06:02:47 -07:00
|
|
|
end,
|
2024-04-08 02:03:20 -07:00
|
|
|
[tonumber(eval.VAR_DICT)] = function(q, processed)
|
|
|
|
return dct2tbl(q.vval.v_dict, processed)
|
2017-03-11 06:02:47 -07:00
|
|
|
end,
|
2024-04-08 02:03:20 -07:00
|
|
|
[tonumber(eval.VAR_FUNC)] = function(q, processed)
|
|
|
|
return { [type_key] = func_type, value = typvalt2lua_tab[eval.VAR_STRING](q, processed or {}) }
|
2017-03-11 06:02:47 -07:00
|
|
|
end,
|
2024-04-08 02:03:20 -07:00
|
|
|
[tonumber(eval.VAR_PARTIAL)] = function(q, processed)
|
|
|
|
local p_key = ptr2key(q)
|
2017-03-11 06:02:47 -07:00
|
|
|
if processed[p_key] then
|
|
|
|
return processed[p_key]
|
2017-01-03 12:51:29 -07:00
|
|
|
end
|
2024-04-08 02:03:20 -07:00
|
|
|
return partial2lua(q.vval.v_partial, processed)
|
2017-03-11 06:02:47 -07:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
2016-04-19 09:17:09 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
typvalt2lua = function(q, processed)
|
2017-03-11 06:02:47 -07:00
|
|
|
typvalt2lua_tab_init()
|
2023-12-04 15:32:39 -07:00
|
|
|
return (
|
2024-04-08 02:03:20 -07:00
|
|
|
(typvalt2lua_tab[tonumber(q.v_type)] or function(t_inner)
|
2023-12-04 15:32:39 -07:00
|
|
|
assert(false, 'Converting ' .. tonumber(t_inner.v_type) .. ' was not implemented yet')
|
2024-04-08 02:03:20 -07:00
|
|
|
end)(q, processed or {})
|
2023-12-04 15:32:39 -07:00
|
|
|
)
|
2016-04-19 09:17:09 -07:00
|
|
|
end
|
|
|
|
|
2017-01-07 03:52:51 -07:00
|
|
|
local function list_iter(l)
|
|
|
|
local init_s = {
|
2023-12-04 15:32:39 -07:00
|
|
|
idx = 0,
|
|
|
|
li = l.lv_first,
|
2017-01-07 03:52:51 -07:00
|
|
|
}
|
|
|
|
local function f(s, _)
|
|
|
|
-- (listitem_T *) NULL is equal to nil, but yet it is not false.
|
|
|
|
if s.li == nil then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
local ret_li = s.li
|
|
|
|
s.li = s.li.li_next
|
|
|
|
s.idx = s.idx + 1
|
|
|
|
return s.idx, ret_li
|
|
|
|
end
|
|
|
|
return f, init_s, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local function list_items(l)
|
|
|
|
local ret = {}
|
|
|
|
for i, li in list_iter(l) do
|
|
|
|
ret[i] = li
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2017-01-03 12:51:29 -07:00
|
|
|
lst2tbl = function(l, processed)
|
2016-02-08 17:20:16 -07:00
|
|
|
if l == nil then
|
2017-01-03 12:51:29 -07:00
|
|
|
return null_list
|
2016-02-08 17:20:16 -07:00
|
|
|
end
|
2017-01-03 12:51:29 -07:00
|
|
|
processed = processed or {}
|
|
|
|
local p_key = ptr2key(l)
|
|
|
|
if processed[p_key] then
|
|
|
|
return processed[p_key]
|
|
|
|
end
|
2023-12-04 15:32:39 -07:00
|
|
|
local ret = { [type_key] = list_type }
|
2017-01-03 12:51:29 -07:00
|
|
|
processed[p_key] = ret
|
2017-01-07 03:52:51 -07:00
|
|
|
for i, li in list_iter(l) do
|
|
|
|
ret[i] = typvalt2lua(li.li_tv, processed)
|
2016-04-19 09:17:09 -07:00
|
|
|
end
|
|
|
|
if ret[1] then
|
|
|
|
ret[type_key] = nil
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
local hi_key_removed = nil
|
2017-01-07 05:26:34 -07:00
|
|
|
|
|
|
|
local function dict_iter(d, return_hi)
|
2017-03-11 06:02:47 -07:00
|
|
|
hi_key_removed = hi_key_removed or eval._hash_key_removed()
|
2017-01-03 12:51:29 -07:00
|
|
|
local init_s = {
|
2023-12-04 15:32:39 -07:00
|
|
|
todo = d.dv_hashtab.ht_used,
|
|
|
|
hi = d.dv_hashtab.ht_array,
|
2017-01-03 12:51:29 -07:00
|
|
|
}
|
|
|
|
local function f(s, _)
|
2023-12-04 15:32:39 -07:00
|
|
|
if s.todo == 0 then
|
|
|
|
return nil
|
|
|
|
end
|
2017-01-03 12:51:29 -07:00
|
|
|
while s.todo > 0 do
|
2017-01-07 05:26:34 -07:00
|
|
|
if s.hi.hi_key ~= nil and s.hi.hi_key ~= hi_key_removed then
|
2017-01-03 12:51:29 -07:00
|
|
|
local key = ffi.string(s.hi.hi_key)
|
2017-01-07 05:26:34 -07:00
|
|
|
local ret
|
|
|
|
if return_hi then
|
|
|
|
ret = s.hi
|
|
|
|
else
|
2023-12-04 15:32:39 -07:00
|
|
|
ret = ffi.cast('dictitem_T*', s.hi.hi_key - ffi.offsetof('dictitem_T', 'di_key'))
|
2017-01-07 05:26:34 -07:00
|
|
|
end
|
2017-01-03 12:51:29 -07:00
|
|
|
s.todo = s.todo - 1
|
|
|
|
s.hi = s.hi + 1
|
2017-01-07 05:26:34 -07:00
|
|
|
return key, ret
|
2017-01-03 12:51:29 -07:00
|
|
|
end
|
|
|
|
s.hi = s.hi + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return f, init_s, nil
|
2016-04-19 09:17:09 -07:00
|
|
|
end
|
|
|
|
|
2017-01-03 12:51:29 -07:00
|
|
|
local function first_di(d)
|
|
|
|
local f, init_s, v = dict_iter(d)
|
|
|
|
return select(2, f(init_s, v))
|
|
|
|
end
|
|
|
|
|
2017-01-07 05:26:34 -07:00
|
|
|
local function dict_items(d)
|
2023-12-04 15:32:39 -07:00
|
|
|
local ret = { [0] = 0 }
|
2017-01-07 05:26:34 -07:00
|
|
|
for k, hi in dict_iter(d) do
|
|
|
|
ret[k] = hi
|
|
|
|
ret[0] = ret[0] + 1
|
|
|
|
ret[ret[0]] = hi
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2017-01-03 12:51:29 -07:00
|
|
|
dct2tbl = function(d, processed)
|
|
|
|
if d == nil then
|
|
|
|
return null_dict
|
|
|
|
end
|
|
|
|
processed = processed or {}
|
|
|
|
local p_key = ptr2key(d)
|
|
|
|
if processed[p_key] then
|
|
|
|
return processed[p_key]
|
|
|
|
end
|
|
|
|
local ret = {}
|
|
|
|
processed[p_key] = ret
|
|
|
|
for k, di in dict_iter(d) do
|
|
|
|
ret[k] = typvalt2lua(di.di_tv, processed)
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
2016-04-19 09:17:09 -07:00
|
|
|
|
|
|
|
local typvalt = function(typ, vval)
|
2017-01-03 12:51:29 -07:00
|
|
|
if typ == nil then
|
|
|
|
typ = eval.VAR_UNKNOWN
|
|
|
|
elseif type(typ) == 'string' then
|
2016-04-19 09:17:09 -07:00
|
|
|
typ = eval[typ]
|
|
|
|
end
|
2023-12-04 15:32:39 -07:00
|
|
|
return ffi.gc(ffi.new('typval_T', { v_type = typ, vval = vval }), eval.tv_clear)
|
2016-04-19 09:17:09 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local lua2typvalt_type_tab = {
|
2016-06-24 07:38:33 -07:00
|
|
|
[int_type] = function(l, _)
|
2023-12-04 15:32:39 -07:00
|
|
|
return typvalt(eval.VAR_NUMBER, { v_number = l.value })
|
2016-04-19 09:17:09 -07:00
|
|
|
end,
|
|
|
|
[flt_type] = function(l, processed)
|
|
|
|
return lua2typvalt(l.value, processed)
|
|
|
|
end,
|
|
|
|
[list_type] = function(l, processed)
|
|
|
|
if processed[l] then
|
|
|
|
processed[l].lv_refcount = processed[l].lv_refcount + 1
|
2023-12-04 15:32:39 -07:00
|
|
|
return typvalt(eval.VAR_LIST, { v_list = processed[l] })
|
2016-04-19 09:17:09 -07:00
|
|
|
end
|
2018-01-02 14:00:16 -07:00
|
|
|
local lst = populate_list(eval.tv_list_alloc(#l), l, processed)
|
2023-12-04 15:32:39 -07:00
|
|
|
return typvalt(eval.VAR_LIST, { v_list = lst })
|
2016-04-19 09:17:09 -07:00
|
|
|
end,
|
|
|
|
[dict_type] = function(l, processed)
|
|
|
|
if processed[l] then
|
|
|
|
processed[l].dv_refcount = processed[l].dv_refcount + 1
|
2023-12-04 15:32:39 -07:00
|
|
|
return typvalt(eval.VAR_DICT, { v_dict = processed[l] })
|
2016-04-19 09:17:09 -07:00
|
|
|
end
|
2017-03-04 13:28:54 -07:00
|
|
|
local dct = populate_dict(eval.tv_dict_alloc(), l, processed)
|
2023-12-04 15:32:39 -07:00
|
|
|
return typvalt(eval.VAR_DICT, { v_dict = dct })
|
2016-04-19 09:17:09 -07:00
|
|
|
end,
|
2017-01-03 12:51:29 -07:00
|
|
|
[func_type] = function(l, processed)
|
|
|
|
if processed[l] then
|
|
|
|
processed[l].pt_refcount = processed[l].pt_refcount + 1
|
2023-12-04 15:32:39 -07:00
|
|
|
return typvalt(eval.VAR_PARTIAL, { v_partial = processed[l] })
|
2017-01-03 12:51:29 -07:00
|
|
|
end
|
|
|
|
if l.args or l.dict then
|
2023-12-04 15:32:39 -07:00
|
|
|
local pt = populate_partial(
|
|
|
|
ffi.gc(ffi.cast('partial_T*', eval.xcalloc(1, ffi.sizeof('partial_T'))), nil),
|
|
|
|
l,
|
|
|
|
processed
|
|
|
|
)
|
|
|
|
return typvalt(eval.VAR_PARTIAL, { v_partial = pt })
|
2017-01-03 12:51:29 -07:00
|
|
|
else
|
|
|
|
return typvalt(eval.VAR_FUNC, {
|
2023-12-04 15:32:39 -07:00
|
|
|
v_string = eval.xmemdupz(to_cstr(l.value), #l.value),
|
2017-01-03 12:51:29 -07:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end,
|
2016-04-19 09:17:09 -07:00
|
|
|
}
|
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
local special_vals = nil
|
2017-01-03 12:51:29 -07:00
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
lua2typvalt = function(l, processed)
|
|
|
|
if not special_vals then
|
|
|
|
special_vals = {
|
2023-12-04 15:32:39 -07:00
|
|
|
[null_string] = { 'VAR_STRING', { v_string = ffi.cast('char*', nil) } },
|
|
|
|
[null_list] = { 'VAR_LIST', { v_list = ffi.cast('list_T*', nil) } },
|
|
|
|
[null_dict] = { 'VAR_DICT', { v_dict = ffi.cast('dict_T*', nil) } },
|
|
|
|
[nil_value] = { 'VAR_SPECIAL', { v_special = eval.kSpecialVarNull } },
|
|
|
|
[true] = { 'VAR_BOOL', { v_bool = eval.kBoolVarTrue } },
|
|
|
|
[false] = { 'VAR_BOOL', { v_bool = eval.kBoolVarFalse } },
|
2017-03-11 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v in pairs(special_vals) do
|
|
|
|
local tmp = function(typ, vval)
|
|
|
|
special_vals[k] = function()
|
|
|
|
return typvalt(eval[typ], vval)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tmp(v[1], v[2])
|
2017-01-03 12:51:29 -07:00
|
|
|
end
|
|
|
|
end
|
2016-04-19 09:17:09 -07:00
|
|
|
processed = processed or {}
|
2016-06-11 10:44:27 -07:00
|
|
|
if l == nil or l == nil_value then
|
2017-01-03 12:51:29 -07:00
|
|
|
return special_vals[nil_value]()
|
|
|
|
elseif special_vals[l] then
|
|
|
|
return special_vals[l]()
|
2016-06-11 10:44:27 -07:00
|
|
|
elseif type(l) == 'table' then
|
2016-04-19 09:17:09 -07:00
|
|
|
if l[type_key] then
|
|
|
|
return lua2typvalt_type_tab[l[type_key]](l, processed)
|
2016-02-08 17:20:16 -07:00
|
|
|
else
|
2016-04-19 09:17:09 -07:00
|
|
|
if l[1] then
|
|
|
|
return lua2typvalt_type_tab[list_type](l, processed)
|
|
|
|
else
|
|
|
|
return lua2typvalt_type_tab[dict_type](l, processed)
|
|
|
|
end
|
2016-02-08 17:20:16 -07:00
|
|
|
end
|
2016-04-19 09:17:09 -07:00
|
|
|
elseif type(l) == 'number' then
|
2023-12-04 15:32:39 -07:00
|
|
|
return typvalt(eval.VAR_FLOAT, { v_float = l })
|
2016-04-19 09:17:09 -07:00
|
|
|
elseif type(l) == 'string' then
|
2023-12-04 15:32:39 -07:00
|
|
|
return typvalt(eval.VAR_STRING, { v_string = eval.xmemdupz(to_cstr(l), #l) })
|
2017-01-03 12:51:29 -07:00
|
|
|
elseif type(l) == 'cdata' then
|
|
|
|
local tv = typvalt(eval.VAR_UNKNOWN)
|
2016-09-16 18:25:49 -07:00
|
|
|
eval.tv_copy(l, tv)
|
2017-01-03 12:51:29 -07:00
|
|
|
return tv
|
2016-02-08 17:20:16 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-12 03:41:11 -07:00
|
|
|
local void_ptr = ffi.typeof('void *')
|
2017-01-07 03:52:51 -07:00
|
|
|
local function void(ptr)
|
2017-03-12 03:41:11 -07:00
|
|
|
return ffi.cast(void_ptr, ptr)
|
2017-01-07 03:52:51 -07:00
|
|
|
end
|
|
|
|
|
2016-09-16 18:25:49 -07:00
|
|
|
local function alloc_len(len, get_ptr)
|
|
|
|
if type(len) == 'string' or type(len) == 'table' then
|
|
|
|
return #len
|
|
|
|
elseif len == nil then
|
|
|
|
return eval.strlen(get_ptr())
|
|
|
|
else
|
|
|
|
return len
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local alloc_logging_t = {
|
2023-12-04 15:32:39 -07:00
|
|
|
list = function(l)
|
|
|
|
return { func = 'calloc', args = { 1, ffi.sizeof('list_T') }, ret = void(l) }
|
|
|
|
end,
|
|
|
|
li = function(li)
|
|
|
|
return { func = 'malloc', args = { ffi.sizeof('listitem_T') }, ret = void(li) }
|
|
|
|
end,
|
|
|
|
dict = function(d)
|
|
|
|
return { func = 'calloc', args = { 1, ffi.sizeof('dict_T') }, ret = void(d) }
|
|
|
|
end,
|
2017-01-07 03:52:51 -07:00
|
|
|
di = function(di, size)
|
2023-12-04 15:32:39 -07:00
|
|
|
size = alloc_len(size, function()
|
|
|
|
return di.di_key
|
|
|
|
end)
|
|
|
|
return {
|
|
|
|
func = 'malloc',
|
|
|
|
args = { ffi.offsetof('dictitem_T', 'di_key') + size + 1 },
|
|
|
|
ret = void(di),
|
|
|
|
}
|
2017-01-07 03:52:51 -07:00
|
|
|
end,
|
2016-09-16 18:25:49 -07:00
|
|
|
str = function(s, size)
|
2023-12-04 15:32:39 -07:00
|
|
|
size = alloc_len(size, function()
|
|
|
|
return s
|
|
|
|
end)
|
|
|
|
return { func = 'malloc', args = { size + 1 }, ret = void(s) }
|
2016-09-16 18:25:49 -07:00
|
|
|
end,
|
2017-01-07 03:52:51 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
dwatcher = function(w)
|
|
|
|
return { func = 'malloc', args = { ffi.sizeof('DictWatcher') }, ret = void(w) }
|
|
|
|
end,
|
2017-03-04 13:28:54 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
freed = function(p)
|
|
|
|
return { func = 'free', args = { type(p) == 'table' and p or void(p) } }
|
|
|
|
end,
|
2017-03-04 13:28:54 -07:00
|
|
|
|
|
|
|
-- lua_…: allocated by this file, not by some Neovim function
|
2023-12-04 15:32:39 -07:00
|
|
|
lua_pt = function(pt)
|
|
|
|
return { func = 'calloc', args = { 1, ffi.sizeof('partial_T') }, ret = void(pt) }
|
|
|
|
end,
|
2017-03-04 15:26:26 -07:00
|
|
|
lua_tvs = function(argv, argc)
|
|
|
|
argc = alloc_len(argc)
|
2023-12-04 15:32:39 -07:00
|
|
|
return { func = 'malloc', args = { ffi.sizeof('typval_T') * argc }, ret = void(argv) }
|
2017-03-04 15:26:26 -07:00
|
|
|
end,
|
2017-01-07 03:52:51 -07:00
|
|
|
}
|
|
|
|
|
2016-11-04 09:33:36 -07:00
|
|
|
local function int(n)
|
2023-12-04 15:32:39 -07:00
|
|
|
return { [type_key] = int_type, value = n }
|
2016-11-04 09:33:36 -07:00
|
|
|
end
|
|
|
|
|
2017-03-04 13:28:54 -07:00
|
|
|
local function list(...)
|
2023-12-04 15:32:39 -07:00
|
|
|
return populate_list(
|
|
|
|
ffi.gc(eval.tv_list_alloc(select('#', ...)), eval.tv_list_unref),
|
|
|
|
{ ... },
|
|
|
|
{}
|
|
|
|
)
|
2017-03-04 13:28:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function dict(d)
|
2023-12-04 15:32:39 -07:00
|
|
|
return populate_dict(ffi.gc(eval.tv_dict_alloc(), eval.tv_dict_free), d or {}, {})
|
2017-03-04 13:28:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local callback2tbl_type_tab = nil
|
|
|
|
|
|
|
|
local function init_callback2tbl_type_tab()
|
|
|
|
if callback2tbl_type_tab then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
callback2tbl_type_tab = {
|
2023-12-04 15:32:39 -07:00
|
|
|
[tonumber(eval.kCallbackNone)] = function(_)
|
|
|
|
return { type = 'none' }
|
|
|
|
end,
|
2017-03-04 13:28:54 -07:00
|
|
|
[tonumber(eval.kCallbackFuncref)] = function(cb)
|
2023-12-04 15:32:39 -07:00
|
|
|
return { type = 'fref', fref = ffi.string(cb.data.funcref) }
|
2017-03-04 13:28:54 -07:00
|
|
|
end,
|
|
|
|
[tonumber(eval.kCallbackPartial)] = function(cb)
|
|
|
|
local lua_pt = partial2lua(cb.data.partial)
|
2023-12-04 15:32:39 -07:00
|
|
|
return { type = 'pt', fref = ffi.string(lua_pt.value), pt = lua_pt }
|
|
|
|
end,
|
2017-03-04 13:28:54 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function callback2tbl(cb)
|
|
|
|
init_callback2tbl_type_tab()
|
|
|
|
return callback2tbl_type_tab[tonumber(cb.type)](cb)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function tbl2callback(tbl)
|
|
|
|
local ret = nil
|
|
|
|
if tbl.type == 'none' then
|
2023-12-04 15:32:39 -07:00
|
|
|
ret = ffi.new('Callback[1]', { { type = eval.kCallbackNone } })
|
2017-03-04 13:28:54 -07:00
|
|
|
elseif tbl.type == 'fref' then
|
2023-12-04 15:32:39 -07:00
|
|
|
ret = ffi.new(
|
|
|
|
'Callback[1]',
|
|
|
|
{ { type = eval.kCallbackFuncref, data = { funcref = eval.xstrdup(tbl.fref) } } }
|
|
|
|
)
|
2017-03-04 13:28:54 -07:00
|
|
|
elseif tbl.type == 'pt' then
|
2023-12-04 15:32:39 -07:00
|
|
|
local pt = ffi.gc(ffi.cast('partial_T*', eval.xcalloc(1, ffi.sizeof('partial_T'))), nil)
|
|
|
|
ret = ffi.new(
|
|
|
|
'Callback[1]',
|
|
|
|
{ { type = eval.kCallbackPartial, data = { partial = populate_partial(pt, tbl.pt, {}) } } }
|
|
|
|
)
|
2017-03-04 13:28:54 -07:00
|
|
|
else
|
|
|
|
assert(false)
|
|
|
|
end
|
2024-04-08 02:03:20 -07:00
|
|
|
return ffi.gc(ffi.cast('Callback*', ret), t.callback_free)
|
2017-03-04 13:28:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function dict_watchers(d)
|
|
|
|
local ret = {}
|
|
|
|
local h = d.watchers
|
|
|
|
local q = h.next
|
|
|
|
local qs = {}
|
|
|
|
local key_patterns = {}
|
|
|
|
while q ~= h do
|
2023-12-04 15:32:39 -07:00
|
|
|
local qitem =
|
|
|
|
ffi.cast('DictWatcher *', ffi.cast('char *', q) - ffi.offsetof('DictWatcher', 'node'))
|
2017-03-04 13:28:54 -07:00
|
|
|
ret[#ret + 1] = {
|
2023-12-04 15:32:39 -07:00
|
|
|
cb = callback2tbl(qitem.callback),
|
|
|
|
pat = ffi.string(qitem.key_pattern, qitem.key_pattern_len),
|
|
|
|
busy = qitem.busy,
|
2017-03-04 13:28:54 -07:00
|
|
|
}
|
|
|
|
qs[#qs + 1] = qitem
|
2023-12-04 15:32:39 -07:00
|
|
|
key_patterns[#key_patterns + 1] = { qitem.key_pattern, qitem.key_pattern_len }
|
2017-03-04 13:28:54 -07:00
|
|
|
q = q.next
|
|
|
|
end
|
|
|
|
return ret, qs, key_patterns
|
|
|
|
end
|
|
|
|
|
2017-03-13 04:35:53 -07:00
|
|
|
local function eval0(expr)
|
2023-12-04 15:32:39 -07:00
|
|
|
local tv = ffi.gc(ffi.new('typval_T', { v_type = eval.VAR_UNKNOWN }), eval.tv_clear)
|
|
|
|
local evalarg = ffi.new('evalarg_T', { eval_flags = eval.EVAL_EVALUATE })
|
2023-04-13 16:22:01 -07:00
|
|
|
if eval.eval0(to_cstr(expr), tv, nil, evalarg) == 0 then
|
2017-03-13 04:35:53 -07:00
|
|
|
return nil
|
|
|
|
else
|
|
|
|
return tv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-08 17:20:16 -07:00
|
|
|
return {
|
2023-12-04 15:32:39 -07:00
|
|
|
int = int,
|
2016-11-04 09:33:36 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
null_string = null_string,
|
|
|
|
null_list = null_list,
|
|
|
|
null_dict = null_dict,
|
|
|
|
list_type = list_type,
|
|
|
|
dict_type = dict_type,
|
|
|
|
func_type = func_type,
|
|
|
|
int_type = int_type,
|
|
|
|
flt_type = flt_type,
|
2016-06-11 10:44:27 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
nil_value = nil_value,
|
2016-06-11 10:44:27 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
type_key = type_key,
|
|
|
|
locks_key = locks_key,
|
2016-02-08 17:20:16 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
list = list,
|
|
|
|
dict = dict,
|
|
|
|
lst2tbl = lst2tbl,
|
|
|
|
dct2tbl = dct2tbl,
|
2016-04-19 09:17:09 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
lua2typvalt = lua2typvalt,
|
|
|
|
typvalt2lua = typvalt2lua,
|
2016-04-19 09:17:09 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
typvalt = typvalt,
|
2017-01-03 12:51:29 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
li_alloc = li_alloc,
|
|
|
|
tv_list_item_free = tv_list_item_free,
|
2017-01-03 12:51:29 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
dict_iter = dict_iter,
|
|
|
|
list_iter = list_iter,
|
|
|
|
first_di = first_di,
|
2017-01-07 03:52:51 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
alloc_logging_t = alloc_logging_t,
|
2017-01-07 03:52:51 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
list_items = list_items,
|
|
|
|
dict_items = dict_items,
|
2016-09-23 14:51:34 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
dict_watchers = dict_watchers,
|
|
|
|
tbl2callback = tbl2callback,
|
|
|
|
callback2tbl = callback2tbl,
|
2017-03-04 13:28:54 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
eval0 = eval0,
|
2017-03-13 04:35:53 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
empty_list = { [type_key] = list_type },
|
2016-02-08 17:20:16 -07:00
|
|
|
}
|