Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 1 | " Test the :disassemble command, and compilation as a side effect |
| 2 | |
| 3 | func NotCompiled() |
| 4 | echo "not" |
| 5 | endfunc |
| 6 | |
| 7 | let s:scriptvar = 4 |
| 8 | let g:globalvar = 'g' |
| 9 | |
| 10 | def s:ScriptFuncLoad(arg: string) |
| 11 | let local = 1 |
| 12 | buffers |
| 13 | echo arg |
| 14 | echo local |
| 15 | echo v:version |
| 16 | echo s:scriptvar |
| 17 | echo g:globalvar |
| 18 | echo &tabstop |
| 19 | echo $ENVVAR |
| 20 | echo @z |
| 21 | enddef |
| 22 | |
| 23 | def Test_disassembleLoad() |
| 24 | assert_fails('disass NoFunc', 'E1061:') |
| 25 | assert_fails('disass NotCompiled', 'E1062:') |
| 26 | |
| 27 | let res = execute('disass s:ScriptFuncLoad') |
| 28 | assert_match('<SNR>\d*_ScriptFuncLoad.*' |
| 29 | \ .. 'buffers.*' |
| 30 | \ .. ' EXEC \+buffers.*' |
| 31 | \ .. ' LOAD arg\[-1\].*' |
| 32 | \ .. ' LOAD $0.*' |
| 33 | \ .. ' LOADV v:version.*' |
| 34 | \ .. ' LOADS s:scriptvar from .*test_vim9_disassemble.vim.*' |
| 35 | \ .. ' LOADG g:globalvar.*' |
| 36 | \ .. ' LOADENV $ENVVAR.*' |
| 37 | \ .. ' LOADREG @z.*' |
| 38 | \, res) |
| 39 | enddef |
| 40 | |
| 41 | def s:ScriptFuncPush() |
| 42 | let localbool = true |
| 43 | let localspec = v:none |
| 44 | let localblob = 0z1234 |
| 45 | if has('float') |
| 46 | let localfloat = 1.234 |
| 47 | endif |
| 48 | enddef |
| 49 | |
| 50 | def Test_disassemblePush() |
| 51 | let res = execute('disass s:ScriptFuncPush') |
| 52 | assert_match('<SNR>\d*_ScriptFuncPush.*' |
| 53 | \ .. 'localbool = true.*' |
| 54 | \ .. ' PUSH v:true.*' |
| 55 | \ .. 'localspec = v:none.*' |
| 56 | \ .. ' PUSH v:none.*' |
| 57 | \ .. 'localblob = 0z1234.*' |
| 58 | \ .. ' PUSHBLOB 0z1234.*' |
| 59 | \, res) |
| 60 | if has('float') |
| 61 | assert_match('<SNR>\d*_ScriptFuncPush.*' |
| 62 | \ .. 'localfloat = 1.234.*' |
| 63 | \ .. ' PUSHF 1.234.*' |
| 64 | \, res) |
| 65 | endif |
| 66 | enddef |
| 67 | |
| 68 | def s:ScriptFuncStore() |
| 69 | let localnr = 1 |
| 70 | localnr = 2 |
| 71 | let localstr = 'abc' |
| 72 | localstr = 'xyz' |
| 73 | v:char = 'abc' |
| 74 | s:scriptvar = 'sv' |
| 75 | g:globalvar = 'gv' |
| 76 | &tabstop = 8 |
| 77 | $ENVVAR = 'ev' |
| 78 | @z = 'rv' |
| 79 | enddef |
| 80 | |
| 81 | def Test_disassembleStore() |
| 82 | let res = execute('disass s:ScriptFuncStore') |
| 83 | assert_match('<SNR>\d*_ScriptFuncStore.*' |
| 84 | \ .. 'localnr = 2.*' |
| 85 | \ .. ' STORE 2 in $0.*' |
| 86 | \ .. 'localstr = ''xyz''.*' |
| 87 | \ .. ' STORE $1.*' |
| 88 | \ .. 'v:char = ''abc''.*' |
| 89 | \ .. 'STOREV v:char.*' |
| 90 | \ .. 's:scriptvar = ''sv''.*' |
| 91 | \ .. ' STORES s:scriptvar in .*test_vim9_disassemble.vim.*' |
| 92 | \ .. 'g:globalvar = ''gv''.*' |
| 93 | \ .. ' STOREG g:globalvar.*' |
| 94 | \ .. '&tabstop = 8.*' |
| 95 | \ .. ' STOREOPT &tabstop.*' |
| 96 | \ .. '$ENVVAR = ''ev''.*' |
| 97 | \ .. ' STOREENV $ENVVAR.*' |
| 98 | \ .. '@z = ''rv''.*' |
| 99 | \ .. ' STOREREG @z.*' |
| 100 | \, res) |
| 101 | enddef |
| 102 | |
| 103 | def s:ScriptFuncTry() |
| 104 | try |
| 105 | echo 'yes' |
| 106 | catch /fail/ |
| 107 | echo 'no' |
| 108 | finally |
| 109 | echo 'end' |
| 110 | endtry |
| 111 | enddef |
| 112 | |
| 113 | def Test_disassembleTry() |
| 114 | let res = execute('disass s:ScriptFuncTry') |
| 115 | assert_match('<SNR>\d*_ScriptFuncTry.*' |
| 116 | \ .. 'try.*' |
| 117 | \ .. 'TRY catch -> \d\+, finally -> \d\+.*' |
| 118 | \ .. 'catch /fail/.*' |
| 119 | \ .. ' JUMP -> \d\+.*' |
| 120 | \ .. ' PUSH v:exception.*' |
| 121 | \ .. ' PUSHS "fail".*' |
| 122 | \ .. ' COMPARESTRING =\~.*' |
| 123 | \ .. ' JUMP_IF_FALSE -> \d\+.*' |
| 124 | \ .. ' CATCH.*' |
| 125 | \ .. 'finally.*' |
| 126 | \ .. ' PUSHS "end".*' |
| 127 | \ .. 'endtry.*' |
| 128 | \ .. ' ENDTRY.*' |
| 129 | \, res) |
| 130 | enddef |
| 131 | |
| 132 | def s:ScriptFuncNew() |
| 133 | let ll = [1, "two", 333] |
| 134 | let dd = #{one: 1, two: "val"} |
| 135 | enddef |
| 136 | |
| 137 | def Test_disassembleNew() |
| 138 | let res = execute('disass s:ScriptFuncNew') |
| 139 | assert_match('<SNR>\d*_ScriptFuncNew.*' |
| 140 | \ .. 'let ll = \[1, "two", 333].*' |
| 141 | \ .. 'PUSHNR 1.*' |
| 142 | \ .. 'PUSHS "two".*' |
| 143 | \ .. 'PUSHNR 333.*' |
| 144 | \ .. 'NEWLIST size 3.*' |
| 145 | \ .. 'let dd = #{one: 1, two: "val"}.*' |
| 146 | \ .. 'PUSHS "one".*' |
| 147 | \ .. 'PUSHNR 1.*' |
| 148 | \ .. 'PUSHS "two".*' |
| 149 | \ .. 'PUSHS "val".*' |
| 150 | \ .. 'NEWDICT size 2.*' |
| 151 | \, res) |
| 152 | enddef |
| 153 | |
| 154 | def FuncWithArg(arg) |
| 155 | echo arg |
| 156 | enddef |
| 157 | |
| 158 | func UserFunc() |
| 159 | echo 'nothing' |
| 160 | endfunc |
| 161 | |
| 162 | func UserFuncWithArg(arg) |
| 163 | echo a:arg |
| 164 | endfunc |
| 165 | |
| 166 | def s:ScriptFuncCall(): string |
| 167 | changenr() |
| 168 | char2nr("abc") |
| 169 | Test_disassembleNew() |
| 170 | FuncWithArg(343) |
| 171 | ScriptFuncNew() |
| 172 | s:ScriptFuncNew() |
| 173 | UserFunc() |
| 174 | UserFuncWithArg("foo") |
| 175 | let FuncRef = function("UserFunc") |
| 176 | FuncRef() |
| 177 | let FuncRefWithArg = function("UserFuncWithArg") |
| 178 | FuncRefWithArg("bar") |
| 179 | return "yes" |
| 180 | enddef |
| 181 | |
| 182 | def Test_disassembleCall() |
| 183 | let res = execute('disass s:ScriptFuncCall') |
| 184 | assert_match('<SNR>\d\+_ScriptFuncCall.*' |
| 185 | \ .. 'changenr().*' |
| 186 | \ .. ' BCALL changenr(argc 0).*' |
| 187 | \ .. 'char2nr("abc").*' |
| 188 | \ .. ' PUSHS "abc".*' |
| 189 | \ .. ' BCALL char2nr(argc 1).*' |
| 190 | \ .. 'Test_disassembleNew().*' |
| 191 | \ .. ' DCALL Test_disassembleNew(argc 0).*' |
| 192 | \ .. 'FuncWithArg(343).*' |
| 193 | \ .. ' PUSHNR 343.*' |
| 194 | \ .. ' DCALL FuncWithArg(argc 1).*' |
| 195 | \ .. 'ScriptFuncNew().*' |
| 196 | \ .. ' DCALL <SNR>\d\+_ScriptFuncNew(argc 0).*' |
| 197 | \ .. 's:ScriptFuncNew().*' |
| 198 | \ .. ' DCALL <SNR>\d\+_ScriptFuncNew(argc 0).*' |
| 199 | \ .. 'UserFunc().*' |
| 200 | \ .. ' UCALL UserFunc(argc 0).*' |
| 201 | \ .. 'UserFuncWithArg("foo").*' |
| 202 | \ .. ' PUSHS "foo".*' |
| 203 | \ .. ' UCALL UserFuncWithArg(argc 1).*' |
| 204 | \ .. 'let FuncRef = function("UserFunc").*' |
| 205 | \ .. 'FuncRef().*' |
| 206 | \ .. ' LOAD $\d.*' |
| 207 | \ .. ' PCALL (argc 0).*' |
| 208 | \ .. 'let FuncRefWithArg = function("UserFuncWithArg").*' |
| 209 | \ .. 'FuncRefWithArg("bar").*' |
| 210 | \ .. ' PUSHS "bar".*' |
| 211 | \ .. ' LOAD $\d.*' |
| 212 | \ .. ' PCALL (argc 1).*' |
| 213 | \ .. 'return "yes".*' |
| 214 | \ .. ' PUSHS "yes".*' |
| 215 | \ .. ' RETURN.*' |
| 216 | \, res) |
| 217 | enddef |
| 218 | |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 219 | def HasEval() |
| 220 | if has("eval") |
| 221 | echo "yes" |
| 222 | else |
| 223 | echo "no" |
| 224 | endif |
| 225 | enddef |
| 226 | |
| 227 | def HasNothing() |
| 228 | if has("nothing") |
| 229 | echo "yes" |
| 230 | else |
| 231 | echo "no" |
| 232 | endif |
| 233 | enddef |
| 234 | |
| 235 | def HasSomething() |
| 236 | if has("nothing") |
| 237 | echo "nothing" |
| 238 | elseif has("something") |
| 239 | echo "something" |
| 240 | elseif has("eval") |
| 241 | echo "eval" |
| 242 | elseif has("less") |
| 243 | echo "less" |
| 244 | endif |
| 245 | enddef |
| 246 | |
| 247 | def Test_compile_const_expr() |
| 248 | assert_equal("\nyes", execute('call HasEval()')) |
| 249 | let instr = execute('disassemble HasEval') |
| 250 | assert_match('HasEval.*' |
| 251 | \ .. 'if has("eval").*' |
| 252 | \ .. ' PUSHS "yes".*' |
| 253 | \, instr) |
| 254 | assert_notmatch('JUMP', instr) |
| 255 | |
| 256 | assert_equal("\nno", execute('call HasNothing()')) |
| 257 | instr = execute('disassemble HasNothing') |
| 258 | assert_match('HasNothing.*' |
| 259 | \ .. 'if has("nothing").*' |
| 260 | \ .. 'else.*' |
| 261 | \ .. ' PUSHS "no".*' |
| 262 | \, instr) |
| 263 | assert_notmatch('PUSHS "yes"', instr) |
| 264 | assert_notmatch('JUMP', instr) |
| 265 | |
| 266 | assert_equal("\neval", execute('call HasSomething()')) |
| 267 | instr = execute('disassemble HasSomething') |
| 268 | assert_match('HasSomething.*' |
| 269 | \ .. 'if has("nothing").*' |
| 270 | \ .. 'elseif has("something").*' |
| 271 | \ .. 'elseif has("eval").*' |
| 272 | \ .. ' PUSHS "eval".*' |
| 273 | \ .. 'elseif has("less").*' |
| 274 | \, instr) |
| 275 | assert_notmatch('PUSHS "nothing"', instr) |
| 276 | assert_notmatch('PUSHS "something"', instr) |
| 277 | assert_notmatch('PUSHS "less"', instr) |
| 278 | assert_notmatch('JUMP', instr) |
| 279 | enddef |
| 280 | |
Bram Moolenaar | 777770f | 2020-02-06 21:27:08 +0100 | [diff] [blame^] | 281 | def WithLambda(): string |
| 282 | let F = {a -> "X" .. a .. "X"} |
| 283 | return F("x") |
| 284 | enddef |
| 285 | |
| 286 | def Test_compile_lambda() |
| 287 | assert_equal("XxX", WithLambda()) |
| 288 | let instr = execute('disassemble WithLambda') |
| 289 | assert_match('WithLambda.*' |
| 290 | \ .. 'let F = {a -> "X" .. a .. "X"}.*' |
| 291 | \ .. ' FUNCREF <lambda>\d\+.*' |
| 292 | \ .. 'PUSHS "x".*' |
| 293 | \ .. ' LOAD $0.*' |
| 294 | \ .. ' PCALL (argc 1).*' |
| 295 | \ .. ' CHECKTYPE string stack\[-1].*' |
| 296 | \, instr) |
| 297 | enddef |
| 298 | |
| 299 | def AndOr(arg): string |
| 300 | if arg == 1 && arg != 2 || arg == 4 |
| 301 | return 'yes' |
| 302 | endif |
| 303 | return 'no' |
| 304 | enddef |
| 305 | |
| 306 | def Test_compile_and_or() |
| 307 | assert_equal("yes", AndOr(1)) |
| 308 | assert_equal("no", AndOr(2)) |
| 309 | assert_equal("yes", AndOr(4)) |
| 310 | let instr = execute('disassemble AndOr') |
| 311 | assert_match('AndOr.*' |
| 312 | \ .. 'if arg == 1 && arg != 2 || arg == 4.*' |
| 313 | \ .. '\d LOAD arg\[-1].*' |
| 314 | \ .. '\d PUSHNR 1.*' |
| 315 | \ .. '\d COMPAREANY ==.*' |
| 316 | \ .. '\d JUMP_AND_KEEP_IF_FALSE -> \d\+.*' |
| 317 | \ .. '\d LOAD arg\[-1].*' |
| 318 | \ .. '\d PUSHNR 2.*' |
| 319 | \ .. '\d COMPAREANY !=.*' |
| 320 | \ .. '\d JUMP_AND_KEEP_IF_TRUE -> \d\+.*' |
| 321 | \ .. '\d LOAD arg\[-1].*' |
| 322 | \ .. '\d PUSHNR 4.*' |
| 323 | \ .. '\d COMPAREANY ==.*' |
| 324 | \ .. '\d JUMP_IF_FALSE -> \d\+.*' |
| 325 | \, instr) |
| 326 | enddef |
| 327 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 328 | |
| 329 | " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker |