testutil.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. local t = require('test.unit.testutil')
  2. local t_eval = require('test.unit.eval.testutil')
  3. local cimport = t.cimport
  4. local to_cstr = t.to_cstr
  5. local ffi = t.ffi
  6. local list_type = t_eval.list_type
  7. local dict_type = t_eval.dict_type
  8. local func_type = t_eval.func_type
  9. local nil_value = t_eval.nil_value
  10. local int_type = t_eval.int_type
  11. local flt_type = t_eval.flt_type
  12. local type_key = t_eval.type_key
  13. local api =
  14. cimport('./src/nvim/api/private/defs.h', './src/nvim/api/private/t.h', './src/nvim/memory.h')
  15. local obj2lua
  16. local obj2lua_tab = nil
  17. local function init_obj2lua_tab()
  18. if obj2lua_tab then
  19. return
  20. end
  21. obj2lua_tab = {
  22. [tonumber(api.kObjectTypeArray)] = function(obj)
  23. local ret = { [type_key] = list_type }
  24. for i = 1, tonumber(obj.data.array.size) do
  25. ret[i] = obj2lua(obj.data.array.items[i - 1])
  26. end
  27. if ret[1] then
  28. ret[type_key] = nil
  29. end
  30. return ret
  31. end,
  32. [tonumber(api.kObjectTypeDict)] = function(obj)
  33. local ret = {}
  34. for i = 1, tonumber(obj.data.dict.size) do
  35. local kv_pair = obj.data.dict.items[i - 1]
  36. ret[ffi.string(kv_pair.key.data, kv_pair.key.size)] = obj2lua(kv_pair.value)
  37. end
  38. return ret
  39. end,
  40. [tonumber(api.kObjectTypeBoolean)] = function(obj)
  41. if obj.data.boolean == false then
  42. return false
  43. else
  44. return true
  45. end
  46. end,
  47. [tonumber(api.kObjectTypeNil)] = function(_)
  48. return nil_value
  49. end,
  50. [tonumber(api.kObjectTypeFloat)] = function(obj)
  51. return tonumber(obj.data.floating)
  52. end,
  53. [tonumber(api.kObjectTypeInteger)] = function(obj)
  54. return { [type_key] = int_type, value = tonumber(obj.data.integer) }
  55. end,
  56. [tonumber(api.kObjectTypeString)] = function(obj)
  57. return ffi.string(obj.data.string.data, obj.data.string.size)
  58. end,
  59. }
  60. end
  61. obj2lua = function(obj)
  62. init_obj2lua_tab()
  63. return (
  64. (obj2lua_tab[tonumber(obj['type'])] or function(obj_inner)
  65. assert(
  66. false,
  67. 'Converting ' .. tostring(tonumber(obj_inner['type'])) .. ' is not implementing yet'
  68. )
  69. end)(obj)
  70. )
  71. end
  72. local obj = function(typ, data)
  73. return ffi.gc(ffi.new('Object', { ['type'] = typ, data = data }), api.api_free_object)
  74. end
  75. local lua2obj
  76. local lua2obj_type_tab = {
  77. [int_type] = function(l)
  78. return obj(api.kObjectTypeInteger, { integer = l.value })
  79. end,
  80. [flt_type] = function(l)
  81. return obj(api.kObjectTypeFloat, { floating = l })
  82. end,
  83. [list_type] = function(l)
  84. local len = #l
  85. local arr = obj(api.kObjectTypeArray, {
  86. array = {
  87. size = len,
  88. capacity = len,
  89. items = ffi.cast('Object *', api.xmalloc(len * ffi.sizeof('Object'))),
  90. },
  91. })
  92. for i = 1, len do
  93. arr.data.array.items[i - 1] = ffi.gc(lua2obj(l[i]), nil)
  94. end
  95. return arr
  96. end,
  97. [dict_type] = function(l)
  98. local kvs = {}
  99. for k, v in pairs(l) do
  100. if type(k) == 'string' then
  101. kvs[#kvs + 1] = { k, v }
  102. end
  103. end
  104. local len = #kvs
  105. local dct = obj(api.kObjectTypeDict, {
  106. dict = {
  107. size = len,
  108. capacity = len,
  109. items = ffi.cast('KeyValuePair *', api.xmalloc(len * ffi.sizeof('KeyValuePair'))),
  110. },
  111. })
  112. for i = 1, len do
  113. local key, val = unpack(kvs[i])
  114. dct.data.dict.items[i - 1] = ffi.new(
  115. 'KeyValuePair',
  116. { key = ffi.gc(lua2obj(key), nil).data.string, value = ffi.gc(lua2obj(val), nil) }
  117. )
  118. end
  119. return dct
  120. end,
  121. }
  122. lua2obj = function(l)
  123. if type(l) == 'table' then
  124. if l[type_key] then
  125. return lua2obj_type_tab[l[type_key]](l)
  126. else
  127. if l[1] then
  128. return lua2obj_type_tab[list_type](l)
  129. else
  130. return lua2obj_type_tab[dict_type](l)
  131. end
  132. end
  133. elseif type(l) == 'number' then
  134. return lua2obj_type_tab[flt_type](l)
  135. elseif type(l) == 'boolean' then
  136. return obj(api.kObjectTypeBoolean, { boolean = l })
  137. elseif type(l) == 'string' then
  138. return obj(
  139. api.kObjectTypeString,
  140. { string = {
  141. size = #l,
  142. data = api.xmemdupz(to_cstr(l), #l),
  143. } }
  144. )
  145. elseif l == nil or l == nil_value then
  146. return obj(api.kObjectTypeNil, { integer = 0 })
  147. end
  148. end
  149. return {
  150. list_type = list_type,
  151. dict_type = dict_type,
  152. func_type = func_type,
  153. int_type = int_type,
  154. flt_type = flt_type,
  155. nil_value = nil_value,
  156. type_key = type_key,
  157. obj2lua = obj2lua,
  158. lua2obj = lua2obj,
  159. }