robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
534544cbf7
master
1--- @param f function 2--- @return table<string,any> 3local function get_upvalues ( f ) 4local i =1 5local upvalues ={} --- @type table<string,any> 6while true do 7local n , v =debug . getupvalue ( f , i ) 8if not n then 9break 10end 11upvalues [ n ] =v 12i =i + 1 13end 14return upvalues 15end 16 17--- @param f function 18--- @param upvalues table<string,any> 19local function set_upvalues ( f , upvalues ) 20local i =1 21while true do 22local n =debug . getupvalue ( f , i ) 23if not n then 24break 25end 26if upvalues [ n ] then 27debug . setupvalue ( f , i , upvalues [ n ]) 28end 29i =i + 1 30end 31end 32 33--- @param messages string[] 34--- @param ... ... 35local function add_print ( messages , ... ) 36local msg ={} --- @type string[] 37for i =1 , select ( '#' , ... ) do 38msg [ # msg + 1 ] =tostring ( select ( i , ... )) 39end 40table . insert ( messages , table . concat ( msg , '\t' )) 41end 42 43local invalid_types ={ 44[ 'thread' ] =true , 45[ 'function' ] =true , 46[ 'userdata' ] =true , 47} 48 49--- @param r any[] 50local function check_returns ( r ) 51for k , v in pairs ( r ) do 52if invalid_types [ type ( v )] then 53error ( 54string . format ( 55"Return index %d with value '%s' of type '%s' cannot be serialized over RPC" , 56k , 57tostring ( v ), 58type ( v ) 59), 602 61) 62end 63end 64end 65 66local M ={} 67 68--- This is run in the context of the remote Nvim instance. 69--- @param bytecode string 70--- @param upvalues table<string,any> 71--- @param ... any[] 72--- @return any[] result 73--- @return table<string,any> upvalues 74--- @return string[] messages 75function M . handler ( bytecode , upvalues , ... ) 76local messages ={} --- @type string[] 77local orig_print =_G . function _G . ( ... ) 80add_print ( messages , ... ) 81return orig_print ( ... ) 82end 83 84local f =assert ( loadstring ( bytecode )) 85 86set_upvalues ( f , upvalues ) 87 88-- Run in pcall so we can return any print messages 89local ret ={ pcall ( f , ... ) } --- @type any[] 90 91_G . orig_print 92 93local new_upvalues =get_upvalues ( f ) 94 95-- Check return value types for better error messages 96check_returns ( ret ) 97 98return ret , new_upvalues , messages 99end 100 101--- @param session test.Session 102--- @param lvl integer 103--- @param code function 104--- @param ... ... 105local function run ( session , lvl , code , ... ) 106local stat , rv =session : request ( 107'nvim_exec_lua' , 108[[return { require('test.functional.testnvim.exec_lua').handler(...) }]] , 109{ string . dump ( code ), get_upvalues ( code ), ... } 110) 111 112if not stat then 113error ( rv [ 2 ], 2 ) 114end 115 116--- @type any[], table<string,any>, string[] 117local ret , upvalues , messages =unpack ( rv ) 118 119for _ , m in ipairs ( messages ) do 120( m ) 121end 122 123if not ret [ 1 ] then 124error ( ret [ 2 ], 2 ) 125end 126 127-- Update upvalues 128if next ( upvalues ) then 129local caller =debug . getinfo ( lvl ) 130local i =0 131 132-- On PUC-Lua, if the function is a tail call, then func will be nil. 133-- In this case we need to use the caller. 134while not caller . func do 135i =i + 1 136caller =debug . getinfo ( lvl + i ) 137end 138set_upvalues ( caller . func , upvalues ) 139end 140 141return unpack ( ret , 2 , table . maxn ( ret )) 142end 143 144return setmetatable ( M , { 145__call =function ( _ , ... ) 146return run ( ... ) 147end , 148} )