helpers.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. local helpers = require('test.unit.helpers')(nil)
  2. local ptr2key = helpers.ptr2key
  3. local cimport = helpers.cimport
  4. local to_cstr = helpers.to_cstr
  5. local ffi = helpers.ffi
  6. local eq = helpers.eq
  7. local eval = cimport('./src/nvim/eval.h', './src/nvim/eval/typval.h',
  8. './src/nvim/hashtab.h', './src/nvim/memory.h')
  9. local null_string = {[true]='NULL string'}
  10. local null_list = {[true]='NULL list'}
  11. local null_dict = {[true]='NULL dict'}
  12. local type_key = {[true]='type key'}
  13. local locks_key = {[true]='locks key'}
  14. local list_type = {[true]='list type'}
  15. local dict_type = {[true]='dict type'}
  16. local func_type = {[true]='func type'}
  17. local int_type = {[true]='int type'}
  18. local flt_type = {[true]='flt type'}
  19. local nil_value = {[true]='nil'}
  20. local lua2typvalt
  21. local function tv_list_item_alloc()
  22. return ffi.cast('listitem_T*', eval.xmalloc(ffi.sizeof('listitem_T')))
  23. end
  24. local function tv_list_item_free(li)
  25. eval.tv_clear(li.li_tv)
  26. eval.xfree(li)
  27. end
  28. local function li_alloc(nogc)
  29. local gcfunc = tv_list_item_free
  30. if nogc then gcfunc = nil end
  31. local li = ffi.gc(tv_list_item_alloc(), gcfunc)
  32. li.li_next = nil
  33. li.li_prev = nil
  34. li.li_tv = {v_type=eval.VAR_UNKNOWN, v_lock=eval.VAR_UNLOCKED}
  35. return li
  36. end
  37. local function populate_list(l, lua_l, processed)
  38. processed = processed or {}
  39. eq(0, l.lv_refcount)
  40. l.lv_refcount = 1
  41. processed[lua_l] = l
  42. for i = 1, #lua_l do
  43. local item_tv = ffi.gc(lua2typvalt(lua_l[i], processed), nil)
  44. local item_li = tv_list_item_alloc()
  45. item_li.li_tv = item_tv
  46. eval.tv_list_append(l, item_li)
  47. end
  48. return l
  49. end
  50. local function populate_dict(d, lua_d, processed)
  51. processed = processed or {}
  52. eq(0, d.dv_refcount)
  53. d.dv_refcount = 1
  54. processed[lua_d] = d
  55. for k, v in pairs(lua_d) do
  56. if type(k) == 'string' then
  57. local di = eval.tv_dict_item_alloc(to_cstr(k))
  58. local val_tv = ffi.gc(lua2typvalt(v, processed), nil)
  59. eval.tv_copy(val_tv, di.di_tv)
  60. eval.tv_clear(val_tv)
  61. eval.tv_dict_add(d, di)
  62. end
  63. end
  64. return d
  65. end
  66. local function populate_partial(pt, lua_pt, processed)
  67. processed = processed or {}
  68. eq(0, pt.pt_refcount)
  69. processed[lua_pt] = pt
  70. local argv = nil
  71. if lua_pt.args and #lua_pt.args > 0 then
  72. argv = ffi.gc(ffi.cast('typval_T*', eval.xmalloc(ffi.sizeof('typval_T') * #lua_pt.args)), nil)
  73. for i, arg in ipairs(lua_pt.args) do
  74. local arg_tv = ffi.gc(lua2typvalt(arg, processed), nil)
  75. argv[i - 1] = arg_tv
  76. end
  77. end
  78. local dict = nil
  79. if lua_pt.dict then
  80. local dict_tv = ffi.gc(lua2typvalt(lua_pt.dict, processed), nil)
  81. assert(dict_tv.v_type == eval.VAR_DICT)
  82. dict = dict_tv.vval.v_dict
  83. end
  84. pt.pt_refcount = 1
  85. pt.pt_name = eval.xmemdupz(to_cstr(lua_pt.value), #lua_pt.value)
  86. pt.pt_auto = not not lua_pt.auto
  87. pt.pt_argc = lua_pt.args and #lua_pt.args or 0
  88. pt.pt_argv = argv
  89. pt.pt_dict = dict
  90. return pt
  91. end
  92. local lst2tbl
  93. local dct2tbl
  94. local typvalt2lua
  95. local function partial2lua(pt, processed)
  96. processed = processed or {}
  97. local value, auto, dict, argv = nil, nil, nil, nil
  98. if pt ~= nil then
  99. value = ffi.string(pt.pt_name)
  100. auto = pt.pt_auto and true or nil
  101. argv = {}
  102. for i = 1, pt.pt_argc do
  103. argv[i] = typvalt2lua(pt.pt_argv[i - 1], processed)
  104. end
  105. if pt.pt_dict ~= nil then
  106. dict = dct2tbl(pt.pt_dict)
  107. end
  108. end
  109. return {
  110. [type_key]=func_type,
  111. value=value,
  112. auto=auto,
  113. args=argv,
  114. dict=dict,
  115. }
  116. end
  117. local typvalt2lua_tab = nil
  118. local function typvalt2lua_tab_init()
  119. if typvalt2lua_tab then
  120. return
  121. end
  122. typvalt2lua_tab = {
  123. [tonumber(eval.VAR_SPECIAL)] = function(t)
  124. return ({
  125. [tonumber(eval.kSpecialVarFalse)] = false,
  126. [tonumber(eval.kSpecialVarNull)] = nil_value,
  127. [tonumber(eval.kSpecialVarTrue)] = true,
  128. })[tonumber(t.vval.v_special)]
  129. end,
  130. [tonumber(eval.VAR_NUMBER)] = function(t)
  131. return {[type_key]=int_type, value=tonumber(t.vval.v_number)}
  132. end,
  133. [tonumber(eval.VAR_FLOAT)] = function(t)
  134. return tonumber(t.vval.v_float)
  135. end,
  136. [tonumber(eval.VAR_STRING)] = function(t)
  137. local str = t.vval.v_string
  138. if str == nil then
  139. return null_string
  140. else
  141. return ffi.string(str)
  142. end
  143. end,
  144. [tonumber(eval.VAR_LIST)] = function(t, processed)
  145. return lst2tbl(t.vval.v_list, processed)
  146. end,
  147. [tonumber(eval.VAR_DICT)] = function(t, processed)
  148. return dct2tbl(t.vval.v_dict, processed)
  149. end,
  150. [tonumber(eval.VAR_FUNC)] = function(t, processed)
  151. return {[type_key]=func_type, value=typvalt2lua_tab[eval.VAR_STRING](t, processed or {})}
  152. end,
  153. [tonumber(eval.VAR_PARTIAL)] = function(t, processed)
  154. local p_key = ptr2key(t)
  155. if processed[p_key] then
  156. return processed[p_key]
  157. end
  158. return partial2lua(t.vval.v_partial, processed)
  159. end,
  160. }
  161. end
  162. typvalt2lua = function(t, processed)
  163. typvalt2lua_tab_init()
  164. return ((typvalt2lua_tab[tonumber(t.v_type)] or function(t_inner)
  165. assert(false, 'Converting ' .. tonumber(t_inner.v_type) .. ' was not implemented yet')
  166. end)(t, processed or {}))
  167. end
  168. local function list_iter(l)
  169. local init_s = {
  170. idx=0,
  171. li=l.lv_first,
  172. }
  173. local function f(s, _)
  174. -- (listitem_T *) NULL is equal to nil, but yet it is not false.
  175. if s.li == nil then
  176. return nil
  177. end
  178. local ret_li = s.li
  179. s.li = s.li.li_next
  180. s.idx = s.idx + 1
  181. return s.idx, ret_li
  182. end
  183. return f, init_s, nil
  184. end
  185. local function list_items(l)
  186. local ret = {}
  187. for i, li in list_iter(l) do
  188. ret[i] = li
  189. end
  190. return ret
  191. end
  192. lst2tbl = function(l, processed)
  193. if l == nil then
  194. return null_list
  195. end
  196. processed = processed or {}
  197. local p_key = ptr2key(l)
  198. if processed[p_key] then
  199. return processed[p_key]
  200. end
  201. local ret = {[type_key]=list_type}
  202. processed[p_key] = ret
  203. for i, li in list_iter(l) do
  204. ret[i] = typvalt2lua(li.li_tv, processed)
  205. end
  206. if ret[1] then
  207. ret[type_key] = nil
  208. end
  209. return ret
  210. end
  211. local hi_key_removed = nil
  212. local function dict_iter(d, return_hi)
  213. hi_key_removed = hi_key_removed or eval._hash_key_removed()
  214. local init_s = {
  215. todo=d.dv_hashtab.ht_used,
  216. hi=d.dv_hashtab.ht_array,
  217. }
  218. local function f(s, _)
  219. if s.todo == 0 then return nil end
  220. while s.todo > 0 do
  221. if s.hi.hi_key ~= nil and s.hi.hi_key ~= hi_key_removed then
  222. local key = ffi.string(s.hi.hi_key)
  223. local ret
  224. if return_hi then
  225. ret = s.hi
  226. else
  227. ret = ffi.cast('dictitem_T*',
  228. s.hi.hi_key - ffi.offsetof('dictitem_T', 'di_key'))
  229. end
  230. s.todo = s.todo - 1
  231. s.hi = s.hi + 1
  232. return key, ret
  233. end
  234. s.hi = s.hi + 1
  235. end
  236. end
  237. return f, init_s, nil
  238. end
  239. local function first_di(d)
  240. local f, init_s, v = dict_iter(d)
  241. return select(2, f(init_s, v))
  242. end
  243. local function dict_items(d)
  244. local ret = {[0]=0}
  245. for k, hi in dict_iter(d) do
  246. ret[k] = hi
  247. ret[0] = ret[0] + 1
  248. ret[ret[0]] = hi
  249. end
  250. return ret
  251. end
  252. dct2tbl = function(d, processed)
  253. if d == nil then
  254. return null_dict
  255. end
  256. processed = processed or {}
  257. local p_key = ptr2key(d)
  258. if processed[p_key] then
  259. return processed[p_key]
  260. end
  261. local ret = {}
  262. processed[p_key] = ret
  263. for k, di in dict_iter(d) do
  264. ret[k] = typvalt2lua(di.di_tv, processed)
  265. end
  266. return ret
  267. end
  268. local typvalt = function(typ, vval)
  269. if typ == nil then
  270. typ = eval.VAR_UNKNOWN
  271. elseif type(typ) == 'string' then
  272. typ = eval[typ]
  273. end
  274. return ffi.gc(ffi.new('typval_T', {v_type=typ, vval=vval}), eval.tv_clear)
  275. end
  276. local lua2typvalt_type_tab = {
  277. [int_type] = function(l, _)
  278. return typvalt(eval.VAR_NUMBER, {v_number=l.value})
  279. end,
  280. [flt_type] = function(l, processed)
  281. return lua2typvalt(l.value, processed)
  282. end,
  283. [list_type] = function(l, processed)
  284. if processed[l] then
  285. processed[l].lv_refcount = processed[l].lv_refcount + 1
  286. return typvalt(eval.VAR_LIST, {v_list=processed[l]})
  287. end
  288. local lst = populate_list(eval.tv_list_alloc(#l), l, processed)
  289. return typvalt(eval.VAR_LIST, {v_list=lst})
  290. end,
  291. [dict_type] = function(l, processed)
  292. if processed[l] then
  293. processed[l].dv_refcount = processed[l].dv_refcount + 1
  294. return typvalt(eval.VAR_DICT, {v_dict=processed[l]})
  295. end
  296. local dct = populate_dict(eval.tv_dict_alloc(), l, processed)
  297. return typvalt(eval.VAR_DICT, {v_dict=dct})
  298. end,
  299. [func_type] = function(l, processed)
  300. if processed[l] then
  301. processed[l].pt_refcount = processed[l].pt_refcount + 1
  302. return typvalt(eval.VAR_PARTIAL, {v_partial=processed[l]})
  303. end
  304. if l.args or l.dict then
  305. local pt = populate_partial(ffi.gc(ffi.cast('partial_T*',
  306. eval.xcalloc(1, ffi.sizeof('partial_T'))), nil), l, processed)
  307. return typvalt(eval.VAR_PARTIAL, {v_partial=pt})
  308. else
  309. return typvalt(eval.VAR_FUNC, {
  310. v_string=eval.xmemdupz(to_cstr(l.value), #l.value)
  311. })
  312. end
  313. end,
  314. }
  315. local special_vals = nil
  316. lua2typvalt = function(l, processed)
  317. if not special_vals then
  318. special_vals = {
  319. [null_string] = {'VAR_STRING', {v_string=ffi.cast('char_u*', nil)}},
  320. [null_list] = {'VAR_LIST', {v_list=ffi.cast('list_T*', nil)}},
  321. [null_dict] = {'VAR_DICT', {v_dict=ffi.cast('dict_T*', nil)}},
  322. [nil_value] = {'VAR_SPECIAL', {v_special=eval.kSpecialVarNull}},
  323. [true] = {'VAR_SPECIAL', {v_special=eval.kSpecialVarTrue}},
  324. [false] = {'VAR_SPECIAL', {v_special=eval.kSpecialVarFalse}},
  325. }
  326. for k, v in pairs(special_vals) do
  327. local tmp = function(typ, vval)
  328. special_vals[k] = function()
  329. return typvalt(eval[typ], vval)
  330. end
  331. end
  332. tmp(v[1], v[2])
  333. end
  334. end
  335. processed = processed or {}
  336. if l == nil or l == nil_value then
  337. return special_vals[nil_value]()
  338. elseif special_vals[l] then
  339. return special_vals[l]()
  340. elseif type(l) == 'table' then
  341. if l[type_key] then
  342. return lua2typvalt_type_tab[l[type_key]](l, processed)
  343. else
  344. if l[1] then
  345. return lua2typvalt_type_tab[list_type](l, processed)
  346. else
  347. return lua2typvalt_type_tab[dict_type](l, processed)
  348. end
  349. end
  350. elseif type(l) == 'number' then
  351. return typvalt(eval.VAR_FLOAT, {v_float=l})
  352. elseif type(l) == 'string' then
  353. return typvalt(eval.VAR_STRING, {v_string=eval.xmemdupz(to_cstr(l), #l)})
  354. elseif type(l) == 'cdata' then
  355. local tv = typvalt(eval.VAR_UNKNOWN)
  356. eval.tv_copy(l, tv)
  357. return tv
  358. end
  359. end
  360. local void_ptr = ffi.typeof('void *')
  361. local function void(ptr)
  362. return ffi.cast(void_ptr, ptr)
  363. end
  364. local function alloc_len(len, get_ptr)
  365. if type(len) == 'string' or type(len) == 'table' then
  366. return #len
  367. elseif len == nil then
  368. return eval.strlen(get_ptr())
  369. else
  370. return len
  371. end
  372. end
  373. local alloc_logging_helpers = {
  374. list = function(l) return {func='calloc', args={1, ffi.sizeof('list_T')}, ret=void(l)} end,
  375. li = function(li) return {func='malloc', args={ffi.sizeof('listitem_T')}, ret=void(li)} end,
  376. dict = function(d) return {func='malloc', args={ffi.sizeof('dict_T')}, ret=void(d)} end,
  377. di = function(di, size)
  378. size = alloc_len(size, function() return di.di_key end)
  379. return {func='malloc', args={ffi.offsetof('dictitem_T', 'di_key') + size + 1}, ret=void(di)}
  380. end,
  381. str = function(s, size)
  382. size = alloc_len(size, function() return s end)
  383. return {func='malloc', args={size + 1}, ret=void(s)}
  384. end,
  385. dwatcher = function(w) return {func='malloc', args={ffi.sizeof('DictWatcher')}, ret=void(w)} end,
  386. freed = function(p) return {func='free', args={type(p) == 'table' and p or void(p)}} end,
  387. -- lua_…: allocated by this file, not by some Neovim function
  388. lua_pt = function(pt) return {func='calloc', args={1, ffi.sizeof('partial_T')}, ret=void(pt)} end,
  389. lua_tvs = function(argv, argc)
  390. argc = alloc_len(argc)
  391. return {func='malloc', args={ffi.sizeof('typval_T')*argc}, ret=void(argv)}
  392. end,
  393. }
  394. local function int(n)
  395. return {[type_key]=int_type, value=n}
  396. end
  397. local function list(...)
  398. return populate_list(ffi.gc(eval.tv_list_alloc(select('#', ...)),
  399. eval.tv_list_unref),
  400. {...}, {})
  401. end
  402. local function dict(d)
  403. return populate_dict(ffi.gc(eval.tv_dict_alloc(), eval.tv_dict_free),
  404. d or {}, {})
  405. end
  406. local callback2tbl_type_tab = nil
  407. local function init_callback2tbl_type_tab()
  408. if callback2tbl_type_tab then
  409. return
  410. end
  411. callback2tbl_type_tab = {
  412. [tonumber(eval.kCallbackNone)] = function(_) return {type='none'} end,
  413. [tonumber(eval.kCallbackFuncref)] = function(cb)
  414. return {type='fref', fref=ffi.string(cb.data.funcref)}
  415. end,
  416. [tonumber(eval.kCallbackPartial)] = function(cb)
  417. local lua_pt = partial2lua(cb.data.partial)
  418. return {type='pt', fref=ffi.string(lua_pt.value), pt=lua_pt}
  419. end
  420. }
  421. end
  422. local function callback2tbl(cb)
  423. init_callback2tbl_type_tab()
  424. return callback2tbl_type_tab[tonumber(cb.type)](cb)
  425. end
  426. local function tbl2callback(tbl)
  427. local ret = nil
  428. if tbl.type == 'none' then
  429. ret = ffi.new('Callback[1]', {{type=eval.kCallbackNone}})
  430. elseif tbl.type == 'fref' then
  431. ret = ffi.new('Callback[1]', {{type=eval.kCallbackFuncref,
  432. data={funcref=eval.xstrdup(tbl.fref)}}})
  433. elseif tbl.type == 'pt' then
  434. local pt = ffi.gc(ffi.cast('partial_T*',
  435. eval.xcalloc(1, ffi.sizeof('partial_T'))), nil)
  436. ret = ffi.new('Callback[1]', {{type=eval.kCallbackPartial,
  437. data={partial=populate_partial(pt, tbl.pt, {})}}})
  438. else
  439. assert(false)
  440. end
  441. return ffi.gc(ffi.cast('Callback*', ret), helpers.callback_free)
  442. end
  443. local function dict_watchers(d)
  444. local ret = {}
  445. local h = d.watchers
  446. local q = h.next
  447. local qs = {}
  448. local key_patterns = {}
  449. while q ~= h do
  450. local qitem = ffi.cast('DictWatcher *',
  451. ffi.cast('char *', q) - ffi.offsetof('DictWatcher', 'node'))
  452. ret[#ret + 1] = {
  453. cb=callback2tbl(qitem.callback),
  454. pat=ffi.string(qitem.key_pattern, qitem.key_pattern_len),
  455. busy=qitem.busy,
  456. }
  457. qs[#qs + 1] = qitem
  458. key_patterns[#key_patterns + 1] = {qitem.key_pattern, qitem.key_pattern_len}
  459. q = q.next
  460. end
  461. return ret, qs, key_patterns
  462. end
  463. local function eval0(expr)
  464. local tv = ffi.gc(ffi.new('typval_T', {v_type=eval.VAR_UNKNOWN}),
  465. eval.tv_clear)
  466. if eval.eval0(to_cstr(expr), tv, nil, true) == 0 then
  467. return nil
  468. else
  469. return tv
  470. end
  471. end
  472. return {
  473. int=int,
  474. null_string=null_string,
  475. null_list=null_list,
  476. null_dict=null_dict,
  477. list_type=list_type,
  478. dict_type=dict_type,
  479. func_type=func_type,
  480. int_type=int_type,
  481. flt_type=flt_type,
  482. nil_value=nil_value,
  483. type_key=type_key,
  484. locks_key=locks_key,
  485. list=list,
  486. dict=dict,
  487. lst2tbl=lst2tbl,
  488. dct2tbl=dct2tbl,
  489. lua2typvalt=lua2typvalt,
  490. typvalt2lua=typvalt2lua,
  491. typvalt=typvalt,
  492. li_alloc=li_alloc,
  493. tv_list_item_free=tv_list_item_free,
  494. dict_iter=dict_iter,
  495. list_iter=list_iter,
  496. first_di=first_di,
  497. alloc_logging_helpers=alloc_logging_helpers,
  498. list_items=list_items,
  499. dict_items=dict_items,
  500. dict_watchers=dict_watchers,
  501. tbl2callback=tbl2callback,
  502. callback2tbl=callback2tbl,
  503. eval0=eval0,
  504. empty_list = {[type_key]=list_type},
  505. }