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