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