helpers.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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_BOOL)] = function(t)
  124. return ({
  125. [tonumber(eval.kBoolVarFalse)] = false,
  126. [tonumber(eval.kBoolVarTrue)] = true,
  127. })[tonumber(t.vval.v_bool)]
  128. end,
  129. [tonumber(eval.VAR_SPECIAL)] = function(t)
  130. return ({
  131. [tonumber(eval.kSpecialVarNull)] = nil_value,
  132. })[tonumber(t.vval.v_special)]
  133. end,
  134. [tonumber(eval.VAR_NUMBER)] = function(t)
  135. return {[type_key]=int_type, value=tonumber(t.vval.v_number)}
  136. end,
  137. [tonumber(eval.VAR_FLOAT)] = function(t)
  138. return tonumber(t.vval.v_float)
  139. end,
  140. [tonumber(eval.VAR_STRING)] = function(t)
  141. local str = t.vval.v_string
  142. if str == nil then
  143. return null_string
  144. else
  145. return ffi.string(str)
  146. end
  147. end,
  148. [tonumber(eval.VAR_LIST)] = function(t, processed)
  149. return lst2tbl(t.vval.v_list, processed)
  150. end,
  151. [tonumber(eval.VAR_DICT)] = function(t, processed)
  152. return dct2tbl(t.vval.v_dict, processed)
  153. end,
  154. [tonumber(eval.VAR_FUNC)] = function(t, processed)
  155. return {[type_key]=func_type, value=typvalt2lua_tab[eval.VAR_STRING](t, processed or {})}
  156. end,
  157. [tonumber(eval.VAR_PARTIAL)] = function(t, processed)
  158. local p_key = ptr2key(t)
  159. if processed[p_key] then
  160. return processed[p_key]
  161. end
  162. return partial2lua(t.vval.v_partial, processed)
  163. end,
  164. }
  165. end
  166. typvalt2lua = function(t, processed)
  167. typvalt2lua_tab_init()
  168. return ((typvalt2lua_tab[tonumber(t.v_type)] or function(t_inner)
  169. assert(false, 'Converting ' .. tonumber(t_inner.v_type) .. ' was not implemented yet')
  170. end)(t, processed or {}))
  171. end
  172. local function list_iter(l)
  173. local init_s = {
  174. idx=0,
  175. li=l.lv_first,
  176. }
  177. local function f(s, _)
  178. -- (listitem_T *) NULL is equal to nil, but yet it is not false.
  179. if s.li == nil then
  180. return nil
  181. end
  182. local ret_li = s.li
  183. s.li = s.li.li_next
  184. s.idx = s.idx + 1
  185. return s.idx, ret_li
  186. end
  187. return f, init_s, nil
  188. end
  189. local function list_items(l)
  190. local ret = {}
  191. for i, li in list_iter(l) do
  192. ret[i] = li
  193. end
  194. return ret
  195. end
  196. lst2tbl = function(l, processed)
  197. if l == nil then
  198. return null_list
  199. end
  200. processed = processed or {}
  201. local p_key = ptr2key(l)
  202. if processed[p_key] then
  203. return processed[p_key]
  204. end
  205. local ret = {[type_key]=list_type}
  206. processed[p_key] = ret
  207. for i, li in list_iter(l) do
  208. ret[i] = typvalt2lua(li.li_tv, processed)
  209. end
  210. if ret[1] then
  211. ret[type_key] = nil
  212. end
  213. return ret
  214. end
  215. local hi_key_removed = nil
  216. local function dict_iter(d, return_hi)
  217. hi_key_removed = hi_key_removed or eval._hash_key_removed()
  218. local init_s = {
  219. todo=d.dv_hashtab.ht_used,
  220. hi=d.dv_hashtab.ht_array,
  221. }
  222. local function f(s, _)
  223. if s.todo == 0 then return nil end
  224. while s.todo > 0 do
  225. if s.hi.hi_key ~= nil and s.hi.hi_key ~= hi_key_removed then
  226. local key = ffi.string(s.hi.hi_key)
  227. local ret
  228. if return_hi then
  229. ret = s.hi
  230. else
  231. ret = ffi.cast('dictitem_T*',
  232. s.hi.hi_key - ffi.offsetof('dictitem_T', 'di_key'))
  233. end
  234. s.todo = s.todo - 1
  235. s.hi = s.hi + 1
  236. return key, ret
  237. end
  238. s.hi = s.hi + 1
  239. end
  240. end
  241. return f, init_s, nil
  242. end
  243. local function first_di(d)
  244. local f, init_s, v = dict_iter(d)
  245. return select(2, f(init_s, v))
  246. end
  247. local function dict_items(d)
  248. local ret = {[0]=0}
  249. for k, hi in dict_iter(d) do
  250. ret[k] = hi
  251. ret[0] = ret[0] + 1
  252. ret[ret[0]] = hi
  253. end
  254. return ret
  255. end
  256. dct2tbl = function(d, processed)
  257. if d == nil then
  258. return null_dict
  259. end
  260. processed = processed or {}
  261. local p_key = ptr2key(d)
  262. if processed[p_key] then
  263. return processed[p_key]
  264. end
  265. local ret = {}
  266. processed[p_key] = ret
  267. for k, di in dict_iter(d) do
  268. ret[k] = typvalt2lua(di.di_tv, processed)
  269. end
  270. return ret
  271. end
  272. local typvalt = function(typ, vval)
  273. if typ == nil then
  274. typ = eval.VAR_UNKNOWN
  275. elseif type(typ) == 'string' then
  276. typ = eval[typ]
  277. end
  278. return ffi.gc(ffi.new('typval_T', {v_type=typ, vval=vval}), eval.tv_clear)
  279. end
  280. local lua2typvalt_type_tab = {
  281. [int_type] = function(l, _)
  282. return typvalt(eval.VAR_NUMBER, {v_number=l.value})
  283. end,
  284. [flt_type] = function(l, processed)
  285. return lua2typvalt(l.value, processed)
  286. end,
  287. [list_type] = function(l, processed)
  288. if processed[l] then
  289. processed[l].lv_refcount = processed[l].lv_refcount + 1
  290. return typvalt(eval.VAR_LIST, {v_list=processed[l]})
  291. end
  292. local lst = populate_list(eval.tv_list_alloc(#l), l, processed)
  293. return typvalt(eval.VAR_LIST, {v_list=lst})
  294. end,
  295. [dict_type] = function(l, processed)
  296. if processed[l] then
  297. processed[l].dv_refcount = processed[l].dv_refcount + 1
  298. return typvalt(eval.VAR_DICT, {v_dict=processed[l]})
  299. end
  300. local dct = populate_dict(eval.tv_dict_alloc(), l, processed)
  301. return typvalt(eval.VAR_DICT, {v_dict=dct})
  302. end,
  303. [func_type] = function(l, processed)
  304. if processed[l] then
  305. processed[l].pt_refcount = processed[l].pt_refcount + 1
  306. return typvalt(eval.VAR_PARTIAL, {v_partial=processed[l]})
  307. end
  308. if l.args or l.dict then
  309. local pt = populate_partial(ffi.gc(ffi.cast('partial_T*',
  310. eval.xcalloc(1, ffi.sizeof('partial_T'))), nil), l, processed)
  311. return typvalt(eval.VAR_PARTIAL, {v_partial=pt})
  312. else
  313. return typvalt(eval.VAR_FUNC, {
  314. v_string=eval.xmemdupz(to_cstr(l.value), #l.value)
  315. })
  316. end
  317. end,
  318. }
  319. local special_vals = nil
  320. lua2typvalt = function(l, processed)
  321. if not special_vals then
  322. special_vals = {
  323. [null_string] = {'VAR_STRING', {v_string=ffi.cast('char_u*', nil)}},
  324. [null_list] = {'VAR_LIST', {v_list=ffi.cast('list_T*', nil)}},
  325. [null_dict] = {'VAR_DICT', {v_dict=ffi.cast('dict_T*', nil)}},
  326. [nil_value] = {'VAR_SPECIAL', {v_special=eval.kSpecialVarNull}},
  327. [true] = {'VAR_BOOL', {v_bool=eval.kBoolVarTrue}},
  328. [false] = {'VAR_BOOL', {v_bool=eval.kBoolVarFalse}},
  329. }
  330. for k, v in pairs(special_vals) do
  331. local tmp = function(typ, vval)
  332. special_vals[k] = function()
  333. return typvalt(eval[typ], vval)
  334. end
  335. end
  336. tmp(v[1], v[2])
  337. end
  338. end
  339. processed = processed or {}
  340. if l == nil or l == nil_value then
  341. return special_vals[nil_value]()
  342. elseif special_vals[l] then
  343. return special_vals[l]()
  344. elseif type(l) == 'table' then
  345. if l[type_key] then
  346. return lua2typvalt_type_tab[l[type_key]](l, processed)
  347. else
  348. if l[1] then
  349. return lua2typvalt_type_tab[list_type](l, processed)
  350. else
  351. return lua2typvalt_type_tab[dict_type](l, processed)
  352. end
  353. end
  354. elseif type(l) == 'number' then
  355. return typvalt(eval.VAR_FLOAT, {v_float=l})
  356. elseif type(l) == 'string' then
  357. return typvalt(eval.VAR_STRING, {v_string=eval.xmemdupz(to_cstr(l), #l)})
  358. elseif type(l) == 'cdata' then
  359. local tv = typvalt(eval.VAR_UNKNOWN)
  360. eval.tv_copy(l, tv)
  361. return tv
  362. end
  363. end
  364. local void_ptr = ffi.typeof('void *')
  365. local function void(ptr)
  366. return ffi.cast(void_ptr, ptr)
  367. end
  368. local function alloc_len(len, get_ptr)
  369. if type(len) == 'string' or type(len) == 'table' then
  370. return #len
  371. elseif len == nil then
  372. return eval.strlen(get_ptr())
  373. else
  374. return len
  375. end
  376. end
  377. local alloc_logging_helpers = {
  378. list = function(l) return {func='calloc', args={1, ffi.sizeof('list_T')}, ret=void(l)} end,
  379. li = function(li) return {func='malloc', args={ffi.sizeof('listitem_T')}, ret=void(li)} end,
  380. dict = function(d) return {func='calloc', args={1, ffi.sizeof('dict_T')}, ret=void(d)} end,
  381. di = function(di, size)
  382. size = alloc_len(size, function() return di.di_key end)
  383. return {func='malloc', args={ffi.offsetof('dictitem_T', 'di_key') + size + 1}, ret=void(di)}
  384. end,
  385. str = function(s, size)
  386. size = alloc_len(size, function() return s end)
  387. return {func='malloc', args={size + 1}, ret=void(s)}
  388. end,
  389. dwatcher = function(w) return {func='malloc', args={ffi.sizeof('DictWatcher')}, ret=void(w)} end,
  390. freed = function(p) return {func='free', args={type(p) == 'table' and p or void(p)}} end,
  391. -- lua_…: allocated by this file, not by some Neovim function
  392. lua_pt = function(pt) return {func='calloc', args={1, ffi.sizeof('partial_T')}, ret=void(pt)} end,
  393. lua_tvs = function(argv, argc)
  394. argc = alloc_len(argc)
  395. return {func='malloc', args={ffi.sizeof('typval_T')*argc}, ret=void(argv)}
  396. end,
  397. }
  398. local function int(n)
  399. return {[type_key]=int_type, value=n}
  400. end
  401. local function list(...)
  402. return populate_list(ffi.gc(eval.tv_list_alloc(select('#', ...)),
  403. eval.tv_list_unref),
  404. {...}, {})
  405. end
  406. local function dict(d)
  407. return populate_dict(ffi.gc(eval.tv_dict_alloc(), eval.tv_dict_free),
  408. d or {}, {})
  409. end
  410. local callback2tbl_type_tab = nil
  411. local function init_callback2tbl_type_tab()
  412. if callback2tbl_type_tab then
  413. return
  414. end
  415. callback2tbl_type_tab = {
  416. [tonumber(eval.kCallbackNone)] = function(_) return {type='none'} end,
  417. [tonumber(eval.kCallbackFuncref)] = function(cb)
  418. return {type='fref', fref=ffi.string(cb.data.funcref)}
  419. end,
  420. [tonumber(eval.kCallbackPartial)] = function(cb)
  421. local lua_pt = partial2lua(cb.data.partial)
  422. return {type='pt', fref=ffi.string(lua_pt.value), pt=lua_pt}
  423. end
  424. }
  425. end
  426. local function callback2tbl(cb)
  427. init_callback2tbl_type_tab()
  428. return callback2tbl_type_tab[tonumber(cb.type)](cb)
  429. end
  430. local function tbl2callback(tbl)
  431. local ret = nil
  432. if tbl.type == 'none' then
  433. ret = ffi.new('Callback[1]', {{type=eval.kCallbackNone}})
  434. elseif tbl.type == 'fref' then
  435. ret = ffi.new('Callback[1]', {{type=eval.kCallbackFuncref,
  436. data={funcref=eval.xstrdup(tbl.fref)}}})
  437. elseif tbl.type == 'pt' then
  438. local pt = ffi.gc(ffi.cast('partial_T*',
  439. eval.xcalloc(1, ffi.sizeof('partial_T'))), nil)
  440. ret = ffi.new('Callback[1]', {{type=eval.kCallbackPartial,
  441. data={partial=populate_partial(pt, tbl.pt, {})}}})
  442. else
  443. assert(false)
  444. end
  445. return ffi.gc(ffi.cast('Callback*', ret), helpers.callback_free)
  446. end
  447. local function dict_watchers(d)
  448. local ret = {}
  449. local h = d.watchers
  450. local q = h.next
  451. local qs = {}
  452. local key_patterns = {}
  453. while q ~= h do
  454. local qitem = ffi.cast('DictWatcher *',
  455. ffi.cast('char *', q) - ffi.offsetof('DictWatcher', 'node'))
  456. ret[#ret + 1] = {
  457. cb=callback2tbl(qitem.callback),
  458. pat=ffi.string(qitem.key_pattern, qitem.key_pattern_len),
  459. busy=qitem.busy,
  460. }
  461. qs[#qs + 1] = qitem
  462. key_patterns[#key_patterns + 1] = {qitem.key_pattern, qitem.key_pattern_len}
  463. q = q.next
  464. end
  465. return ret, qs, key_patterns
  466. end
  467. local function eval0(expr)
  468. local tv = ffi.gc(ffi.new('typval_T', {v_type=eval.VAR_UNKNOWN}),
  469. eval.tv_clear)
  470. if eval.eval0(to_cstr(expr), tv, nil, true) == 0 then
  471. return nil
  472. else
  473. return tv
  474. end
  475. end
  476. return {
  477. int=int,
  478. null_string=null_string,
  479. null_list=null_list,
  480. null_dict=null_dict,
  481. list_type=list_type,
  482. dict_type=dict_type,
  483. func_type=func_type,
  484. int_type=int_type,
  485. flt_type=flt_type,
  486. nil_value=nil_value,
  487. type_key=type_key,
  488. locks_key=locks_key,
  489. list=list,
  490. dict=dict,
  491. lst2tbl=lst2tbl,
  492. dct2tbl=dct2tbl,
  493. lua2typvalt=lua2typvalt,
  494. typvalt2lua=typvalt2lua,
  495. typvalt=typvalt,
  496. li_alloc=li_alloc,
  497. tv_list_item_free=tv_list_item_free,
  498. dict_iter=dict_iter,
  499. list_iter=list_iter,
  500. first_di=first_di,
  501. alloc_logging_helpers=alloc_logging_helpers,
  502. list_items=list_items,
  503. dict_items=dict_items,
  504. dict_watchers=dict_watchers,
  505. tbl2callback=tbl2callback,
  506. callback2tbl=callback2tbl,
  507. eval0=eval0,
  508. empty_list = {[type_key]=list_type},
  509. }