blob: 90bd67dbacc0a4f1e21e040500595b8ec9538587 [file] [log] [blame]
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001" Test the :disassemble command, and compilation as a side effect
2
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003source check.vim
4
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01005func NotCompiled()
6 echo "not"
7endfunc
8
9let s:scriptvar = 4
10let g:globalvar = 'g'
Bram Moolenaard3aac292020-04-19 14:32:17 +020011let b:buffervar = 'b'
12let w:windowvar = 'w'
13let t:tabpagevar = 't'
Bram Moolenaar5cab73f2020-02-06 19:25:19 +010014
15def s:ScriptFuncLoad(arg: string)
16 let local = 1
17 buffers
18 echo arg
19 echo local
Bram Moolenaar8a1c1012020-05-07 14:07:25 +020020 echo &lines
Bram Moolenaar5cab73f2020-02-06 19:25:19 +010021 echo v:version
22 echo s:scriptvar
23 echo g:globalvar
Bram Moolenaard3aac292020-04-19 14:32:17 +020024 echo b:buffervar
25 echo w:windowvar
26 echo t:tabpagevar
Bram Moolenaar5cab73f2020-02-06 19:25:19 +010027 echo &tabstop
28 echo $ENVVAR
29 echo @z
30enddef
31
Bram Moolenaarf2460a32020-02-07 22:09:54 +010032def Test_disassemble_load()
Bram Moolenaar5cab73f2020-02-06 19:25:19 +010033 assert_fails('disass NoFunc', 'E1061:')
34 assert_fails('disass NotCompiled', 'E1062:')
Bram Moolenaar21456cd2020-02-13 21:29:32 +010035 assert_fails('disass', 'E471:')
36 assert_fails('disass [', 'E475:')
37 assert_fails('disass 234', 'E475:')
38 assert_fails('disass <XX>foo', 'E475:')
Bram Moolenaar5cab73f2020-02-06 19:25:19 +010039
40 let res = execute('disass s:ScriptFuncLoad')
Bram Moolenaar675f7162020-04-12 22:53:54 +020041 assert_match('<SNR>\d*_ScriptFuncLoad.*' ..
42 'buffers.*' ..
43 ' EXEC \+buffers.*' ..
44 ' LOAD arg\[-1\].*' ..
45 ' LOAD $0.*' ..
Bram Moolenaar8a1c1012020-05-07 14:07:25 +020046 ' LOADOPT &lines.*' ..
Bram Moolenaar675f7162020-04-12 22:53:54 +020047 ' LOADV v:version.*' ..
48 ' LOADS s:scriptvar from .*test_vim9_disassemble.vim.*' ..
49 ' LOADG g:globalvar.*' ..
Bram Moolenaard3aac292020-04-19 14:32:17 +020050 ' LOADB b:buffervar.*' ..
51 ' LOADW w:windowvar.*' ..
52 ' LOADT t:tabpagevar.*' ..
Bram Moolenaar675f7162020-04-12 22:53:54 +020053 ' LOADENV $ENVVAR.*' ..
54 ' LOADREG @z.*',
55 res)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +010056enddef
57
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020058def s:EditExpand()
59 let filename = "file"
60 let filenr = 123
61 edit the`=filename``=filenr`.txt
62enddef
63
64def Test_disassemble_exec_expr()
65 let res = execute('disass s:EditExpand')
66 assert_match('<SNR>\d*_EditExpand.*' ..
67 ' let filename = "file".*' ..
68 '\d PUSHS "file".*' ..
69 '\d STORE $0.*' ..
70 ' let filenr = 123.*' ..
71 '\d STORE 123 in $1.*' ..
72 ' edit the`=filename``=filenr`.txt.*' ..
73 '\d PUSHS "edit the".*' ..
74 '\d LOAD $0.*' ..
75 '\d LOAD $1.*' ..
76 '\d 2STRING stack\[-1\].*' ..
77 '\d PUSHS ".txt".*' ..
78 '\d EXECCONCAT 4.*' ..
79 '\d PUSHNR 0.*' ..
80 '\d RETURN',
81 res)
82enddef
83
Bram Moolenaar5cab73f2020-02-06 19:25:19 +010084def s:ScriptFuncPush()
85 let localbool = true
86 let localspec = v:none
87 let localblob = 0z1234
88 if has('float')
89 let localfloat = 1.234
90 endif
91enddef
92
Bram Moolenaarf2460a32020-02-07 22:09:54 +010093def Test_disassemble_push()
Bram Moolenaar5cab73f2020-02-06 19:25:19 +010094 let res = execute('disass s:ScriptFuncPush')
Bram Moolenaar675f7162020-04-12 22:53:54 +020095 assert_match('<SNR>\d*_ScriptFuncPush.*' ..
96 'localbool = true.*' ..
97 ' PUSH v:true.*' ..
98 'localspec = v:none.*' ..
99 ' PUSH v:none.*' ..
100 'localblob = 0z1234.*' ..
101 ' PUSHBLOB 0z1234.*',
102 res)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100103 if has('float')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200104 assert_match('<SNR>\d*_ScriptFuncPush.*' ..
105 'localfloat = 1.234.*' ..
106 ' PUSHF 1.234.*',
107 res)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100108 endif
109enddef
110
111def s:ScriptFuncStore()
112 let localnr = 1
113 localnr = 2
114 let localstr = 'abc'
115 localstr = 'xyz'
116 v:char = 'abc'
117 s:scriptvar = 'sv'
118 g:globalvar = 'gv'
Bram Moolenaard3aac292020-04-19 14:32:17 +0200119 b:buffervar = 'bv'
120 w:windowvar = 'wv'
121 t:tabpagevar = 'tv'
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100122 &tabstop = 8
123 $ENVVAR = 'ev'
124 @z = 'rv'
125enddef
126
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100127def Test_disassemble_store()
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100128 let res = execute('disass s:ScriptFuncStore')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200129 assert_match('<SNR>\d*_ScriptFuncStore.*' ..
130 'let localnr = 1.*' ..
131 'localnr = 2.*' ..
132 ' STORE 2 in $0.*' ..
133 'let localstr = ''abc''.*' ..
134 'localstr = ''xyz''.*' ..
135 ' STORE $1.*' ..
136 'v:char = ''abc''.*' ..
137 'STOREV v:char.*' ..
138 's:scriptvar = ''sv''.*' ..
139 ' STORES s:scriptvar in .*test_vim9_disassemble.vim.*' ..
140 'g:globalvar = ''gv''.*' ..
141 ' STOREG g:globalvar.*' ..
Bram Moolenaard3aac292020-04-19 14:32:17 +0200142 'b:buffervar = ''bv''.*' ..
143 ' STOREB b:buffervar.*' ..
144 'w:windowvar = ''wv''.*' ..
145 ' STOREW w:windowvar.*' ..
146 't:tabpagevar = ''tv''.*' ..
147 ' STORET t:tabpagevar.*' ..
Bram Moolenaar675f7162020-04-12 22:53:54 +0200148 '&tabstop = 8.*' ..
149 ' STOREOPT &tabstop.*' ..
150 '$ENVVAR = ''ev''.*' ..
151 ' STOREENV $ENVVAR.*' ..
152 '@z = ''rv''.*' ..
153 ' STOREREG @z.*',
154 res)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100155enddef
156
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200157def s:ScriptFuncUnlet()
158 g:somevar = "value"
159 unlet g:somevar
160 unlet! g:somevar
Bram Moolenaar7bdaea62020-04-19 18:27:26 +0200161 unlet $SOMEVAR
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200162enddef
163
164def Test_disassemble_unlet()
165 let res = execute('disass s:ScriptFuncUnlet')
166 assert_match('<SNR>\d*_ScriptFuncUnlet.*' ..
167 'g:somevar = "value".*' ..
168 '\d PUSHS "value".*' ..
169 '\d STOREG g:somevar.*' ..
170 'unlet g:somevar.*' ..
171 '\d UNLET g:somevar.*' ..
172 'unlet! g:somevar.*' ..
Bram Moolenaar7bdaea62020-04-19 18:27:26 +0200173 '\d UNLET! g:somevar.*' ..
174 'unlet $SOMEVAR.*' ..
175 '\d UNLETENV $SOMEVAR.*',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200176 res)
177enddef
178
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100179def s:ScriptFuncTry()
180 try
181 echo 'yes'
182 catch /fail/
183 echo 'no'
184 finally
Bram Moolenaarc2a4b352020-02-06 22:41:16 +0100185 throw 'end'
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100186 endtry
187enddef
188
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100189def Test_disassemble_try()
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100190 let res = execute('disass s:ScriptFuncTry')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200191 assert_match('<SNR>\d*_ScriptFuncTry.*' ..
192 'try.*' ..
193 'TRY catch -> \d\+, finally -> \d\+.*' ..
194 'catch /fail/.*' ..
195 ' JUMP -> \d\+.*' ..
196 ' PUSH v:exception.*' ..
197 ' PUSHS "fail".*' ..
198 ' COMPARESTRING =\~.*' ..
199 ' JUMP_IF_FALSE -> \d\+.*' ..
200 ' CATCH.*' ..
201 'finally.*' ..
202 ' PUSHS "end".*' ..
203 ' THROW.*' ..
204 'endtry.*' ..
205 ' ENDTRY.*',
206 res)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100207enddef
208
209def s:ScriptFuncNew()
210 let ll = [1, "two", 333]
211 let dd = #{one: 1, two: "val"}
212enddef
213
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100214def Test_disassemble_new()
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100215 let res = execute('disass s:ScriptFuncNew')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200216 assert_match('<SNR>\d*_ScriptFuncNew.*' ..
217 'let ll = \[1, "two", 333].*' ..
218 'PUSHNR 1.*' ..
219 'PUSHS "two".*' ..
220 'PUSHNR 333.*' ..
221 'NEWLIST size 3.*' ..
222 'let dd = #{one: 1, two: "val"}.*' ..
223 'PUSHS "one".*' ..
224 'PUSHNR 1.*' ..
225 'PUSHS "two".*' ..
226 'PUSHS "val".*' ..
227 'NEWDICT size 2.*',
228 res)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100229enddef
230
Bram Moolenaar6e949782020-04-13 17:21:00 +0200231def FuncWithArg(arg: any)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100232 echo arg
233enddef
234
235func UserFunc()
236 echo 'nothing'
237endfunc
238
239func UserFuncWithArg(arg)
240 echo a:arg
241endfunc
242
243def s:ScriptFuncCall(): string
244 changenr()
245 char2nr("abc")
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100246 Test_disassemble_new()
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100247 FuncWithArg(343)
248 ScriptFuncNew()
249 s:ScriptFuncNew()
250 UserFunc()
251 UserFuncWithArg("foo")
252 let FuncRef = function("UserFunc")
253 FuncRef()
254 let FuncRefWithArg = function("UserFuncWithArg")
255 FuncRefWithArg("bar")
256 return "yes"
257enddef
258
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100259def Test_disassemble_call()
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100260 let res = execute('disass s:ScriptFuncCall')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200261 assert_match('<SNR>\d\+_ScriptFuncCall.*' ..
262 'changenr().*' ..
263 ' BCALL changenr(argc 0).*' ..
264 'char2nr("abc").*' ..
265 ' PUSHS "abc".*' ..
266 ' BCALL char2nr(argc 1).*' ..
267 'Test_disassemble_new().*' ..
268 ' DCALL Test_disassemble_new(argc 0).*' ..
269 'FuncWithArg(343).*' ..
270 ' PUSHNR 343.*' ..
271 ' DCALL FuncWithArg(argc 1).*' ..
272 'ScriptFuncNew().*' ..
273 ' DCALL <SNR>\d\+_ScriptFuncNew(argc 0).*' ..
274 's:ScriptFuncNew().*' ..
275 ' DCALL <SNR>\d\+_ScriptFuncNew(argc 0).*' ..
276 'UserFunc().*' ..
277 ' UCALL UserFunc(argc 0).*' ..
278 'UserFuncWithArg("foo").*' ..
279 ' PUSHS "foo".*' ..
280 ' UCALL UserFuncWithArg(argc 1).*' ..
281 'let FuncRef = function("UserFunc").*' ..
282 'FuncRef().*' ..
283 ' LOAD $\d.*' ..
284 ' PCALL (argc 0).*' ..
285 'let FuncRefWithArg = function("UserFuncWithArg").*' ..
286 'FuncRefWithArg("bar").*' ..
287 ' PUSHS "bar".*' ..
288 ' LOAD $\d.*' ..
289 ' PCALL (argc 1).*' ..
290 'return "yes".*' ..
291 ' PUSHS "yes".*' ..
292 ' RETURN.*',
293 res)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100294enddef
295
Bram Moolenaarb68b3462020-05-06 21:06:30 +0200296def s:CreateRefs()
297 let local = 'a'
298 def Append(arg: string)
299 local ..= arg
300 enddef
301 g:Append = Append
302 def Get(): string
303 return local
304 enddef
305 g:Get = Get
306enddef
307
308def Test_disassemble_closure()
309 CreateRefs()
310 let res = execute('disass g:Append')
311 assert_match('<lambda>\d.*' ..
312 'local ..= arg.*' ..
313 '\d LOADOUTER $0.*' ..
314 '\d LOAD arg\[-1\].*' ..
315 '\d CONCAT.*' ..
316 '\d STOREOUTER $0.*' ..
317 '\d PUSHNR 0.*' ..
318 '\d RETURN.*',
319 res)
320
321 res = execute('disass g:Get')
322 assert_match('<lambda>\d.*' ..
323 'return local.*' ..
324 '\d LOADOUTER $0.*' ..
325 '\d RETURN.*',
326 res)
327
328 unlet g:Append
329 unlet g:Get
330enddef
331
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100332
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200333def EchoArg(arg: string): string
334 return arg
335enddef
336def RefThis(): func
337 return function('EchoArg')
338enddef
339def s:ScriptPCall()
340 RefThis()("text")
341enddef
342
343def Test_disassemble_pcall()
344 let res = execute('disass s:ScriptPCall')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200345 assert_match('<SNR>\d\+_ScriptPCall.*' ..
346 'RefThis()("text").*' ..
347 '\d DCALL RefThis(argc 0).*' ..
348 '\d PUSHS "text".*' ..
349 '\d PCALL top (argc 1).*' ..
350 '\d PCALL end.*' ..
351 '\d DROP.*' ..
352 '\d PUSHNR 0.*' ..
353 '\d RETURN.*',
354 res)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200355enddef
356
357
Bram Moolenaara26b9702020-04-18 19:53:28 +0200358def s:FuncWithForwardCall(): string
359 return g:DefinedLater("yes")
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100360enddef
361
362def DefinedLater(arg: string): string
363 return arg
364enddef
365
366def Test_disassemble_update_instr()
Bram Moolenaara26b9702020-04-18 19:53:28 +0200367 let res = execute('disass s:FuncWithForwardCall')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200368 assert_match('FuncWithForwardCall.*' ..
Bram Moolenaara26b9702020-04-18 19:53:28 +0200369 'return g:DefinedLater("yes").*' ..
Bram Moolenaar675f7162020-04-12 22:53:54 +0200370 '\d PUSHS "yes".*' ..
Bram Moolenaara26b9702020-04-18 19:53:28 +0200371 '\d UCALL g:DefinedLater(argc 1).*' ..
Bram Moolenaar675f7162020-04-12 22:53:54 +0200372 '\d CHECKTYPE string stack\[-1].*' ..
373 '\d RETURN.*',
374 res)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100375
376 " Calling the function will change UCALL into the faster DCALL
377 assert_equal('yes', FuncWithForwardCall())
378
Bram Moolenaara26b9702020-04-18 19:53:28 +0200379 res = execute('disass s:FuncWithForwardCall')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200380 assert_match('FuncWithForwardCall.*' ..
Bram Moolenaara26b9702020-04-18 19:53:28 +0200381 'return g:DefinedLater("yes").*' ..
Bram Moolenaar675f7162020-04-12 22:53:54 +0200382 '\d PUSHS "yes".*' ..
383 '\d DCALL DefinedLater(argc 1).*' ..
384 '\d CHECKTYPE string stack\[-1].*' ..
385 '\d RETURN.*',
386 res)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100387enddef
388
389
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100390def FuncWithDefault(arg: string = 'default'): string
391 return arg
392enddef
393
394def Test_disassemble_call_default()
395 let res = execute('disass FuncWithDefault')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200396 assert_match('FuncWithDefault.*' ..
397 '\d PUSHS "default".*' ..
398 '\d STORE arg\[-1].*' ..
399 'return arg.*' ..
400 '\d LOAD arg\[-1].*' ..
401 '\d RETURN.*',
402 res)
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100403enddef
404
405
Bram Moolenaar158906c2020-02-06 20:39:45 +0100406def HasEval()
407 if has("eval")
408 echo "yes"
409 else
410 echo "no"
411 endif
412enddef
413
414def HasNothing()
415 if has("nothing")
416 echo "yes"
417 else
418 echo "no"
419 endif
420enddef
421
422def HasSomething()
423 if has("nothing")
424 echo "nothing"
425 elseif has("something")
426 echo "something"
427 elseif has("eval")
428 echo "eval"
429 elseif has("less")
430 echo "less"
431 endif
432enddef
433
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100434def Test_disassemble_const_expr()
Bram Moolenaar158906c2020-02-06 20:39:45 +0100435 assert_equal("\nyes", execute('call HasEval()'))
436 let instr = execute('disassemble HasEval')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200437 assert_match('HasEval.*' ..
438 'if has("eval").*' ..
439 ' PUSHS "yes".*',
440 instr)
Bram Moolenaar158906c2020-02-06 20:39:45 +0100441 assert_notmatch('JUMP', instr)
442
443 assert_equal("\nno", execute('call HasNothing()'))
444 instr = execute('disassemble HasNothing')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200445 assert_match('HasNothing.*' ..
446 'if has("nothing").*' ..
447 'else.*' ..
448 ' PUSHS "no".*',
449 instr)
Bram Moolenaar158906c2020-02-06 20:39:45 +0100450 assert_notmatch('PUSHS "yes"', instr)
451 assert_notmatch('JUMP', instr)
452
453 assert_equal("\neval", execute('call HasSomething()'))
454 instr = execute('disassemble HasSomething')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200455 assert_match('HasSomething.*' ..
456 'if has("nothing").*' ..
457 'elseif has("something").*' ..
458 'elseif has("eval").*' ..
459 ' PUSHS "eval".*' ..
460 'elseif has("less").*',
461 instr)
Bram Moolenaar158906c2020-02-06 20:39:45 +0100462 assert_notmatch('PUSHS "nothing"', instr)
463 assert_notmatch('PUSHS "something"', instr)
464 assert_notmatch('PUSHS "less"', instr)
465 assert_notmatch('JUMP', instr)
466enddef
467
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100468def WithFunc()
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200469 let Funky1: func
470 let Funky2: func = function("len")
471 let Party2: func = funcref("UserFunc")
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100472enddef
473
474def Test_disassemble_function()
475 let instr = execute('disassemble WithFunc')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200476 assert_match('WithFunc.*' ..
477 'let Funky1: func.*' ..
478 '0 PUSHFUNC "\[none]".*' ..
479 '1 STORE $0.*' ..
480 'let Funky2: func = function("len").*' ..
481 '2 PUSHS "len".*' ..
482 '3 BCALL function(argc 1).*' ..
483 '4 STORE $1.*' ..
484 'let Party2: func = funcref("UserFunc").*' ..
485 '\d PUSHS "UserFunc".*' ..
486 '\d BCALL funcref(argc 1).*' ..
487 '\d STORE $2.*' ..
488 '\d PUSHNR 0.*' ..
489 '\d RETURN.*',
490 instr)
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100491enddef
492
493if has('channel')
494 def WithChannel()
495 let job1: job
496 let job2: job = job_start("donothing")
497 let chan1: channel
498 enddef
499endif
500
501def Test_disassemble_channel()
502 CheckFeature channel
503
504 let instr = execute('disassemble WithChannel')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200505 assert_match('WithChannel.*' ..
506 'let job1: job.*' ..
507 '\d PUSHJOB "no process".*' ..
508 '\d STORE $0.*' ..
509 'let job2: job = job_start("donothing").*' ..
510 '\d PUSHS "donothing".*' ..
511 '\d BCALL job_start(argc 1).*' ..
512 '\d STORE $1.*' ..
513 'let chan1: channel.*' ..
514 '\d PUSHCHANNEL 0.*' ..
515 '\d STORE $2.*' ..
516 '\d PUSHNR 0.*' ..
517 '\d RETURN.*',
518 instr)
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100519enddef
520
Bram Moolenaar777770f2020-02-06 21:27:08 +0100521def WithLambda(): string
522 let F = {a -> "X" .. a .. "X"}
523 return F("x")
524enddef
525
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100526def Test_disassemble_lambda()
Bram Moolenaar777770f2020-02-06 21:27:08 +0100527 assert_equal("XxX", WithLambda())
528 let instr = execute('disassemble WithLambda')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200529 assert_match('WithLambda.*' ..
530 'let F = {a -> "X" .. a .. "X"}.*' ..
531 ' FUNCREF <lambda>\d\+.*' ..
532 'PUSHS "x".*' ..
533 ' LOAD $0.*' ..
534 ' PCALL (argc 1).*' ..
535 ' CHECKTYPE string stack\[-1].*',
536 instr)
Bram Moolenaar777770f2020-02-06 21:27:08 +0100537enddef
538
Bram Moolenaar6e949782020-04-13 17:21:00 +0200539def AndOr(arg: any): string
Bram Moolenaar777770f2020-02-06 21:27:08 +0100540 if arg == 1 && arg != 2 || arg == 4
541 return 'yes'
542 endif
543 return 'no'
544enddef
545
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100546def Test_disassemble_and_or()
Bram Moolenaar777770f2020-02-06 21:27:08 +0100547 assert_equal("yes", AndOr(1))
548 assert_equal("no", AndOr(2))
549 assert_equal("yes", AndOr(4))
550 let instr = execute('disassemble AndOr')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200551 assert_match('AndOr.*' ..
552 'if arg == 1 && arg != 2 || arg == 4.*' ..
553 '\d LOAD arg\[-1].*' ..
554 '\d PUSHNR 1.*' ..
555 '\d COMPAREANY ==.*' ..
556 '\d JUMP_AND_KEEP_IF_FALSE -> \d\+.*' ..
557 '\d LOAD arg\[-1].*' ..
558 '\d PUSHNR 2.*' ..
559 '\d COMPAREANY !=.*' ..
560 '\d JUMP_AND_KEEP_IF_TRUE -> \d\+.*' ..
561 '\d LOAD arg\[-1].*' ..
562 '\d PUSHNR 4.*' ..
563 '\d COMPAREANY ==.*' ..
564 '\d JUMP_IF_FALSE -> \d\+.*',
565 instr)
Bram Moolenaar777770f2020-02-06 21:27:08 +0100566enddef
567
Bram Moolenaar04d05222020-02-06 22:06:54 +0100568def ForLoop(): list<number>
569 let res: list<number>
570 for i in range(3)
571 res->add(i)
572 endfor
573 return res
574enddef
575
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100576def Test_disassemble_for_loop()
Bram Moolenaar04d05222020-02-06 22:06:54 +0100577 assert_equal([0, 1, 2], ForLoop())
578 let instr = execute('disassemble ForLoop')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200579 assert_match('ForLoop.*' ..
580 'let res: list<number>.*' ..
581 ' NEWLIST size 0.*' ..
582 '\d STORE $0.*' ..
583 'for i in range(3).*' ..
584 '\d STORE -1 in $1.*' ..
585 '\d PUSHNR 3.*' ..
586 '\d BCALL range(argc 1).*' ..
587 '\d FOR $1 -> \d\+.*' ..
588 '\d STORE $2.*' ..
589 'res->add(i).*' ..
590 '\d LOAD $0.*' ..
591 '\d LOAD $2.*' ..
592 '\d BCALL add(argc 2).*' ..
593 '\d DROP.*' ..
594 'endfor.*' ..
595 '\d JUMP -> \d\+.*' ..
596 '\d DROP.*',
597 instr)
Bram Moolenaar04d05222020-02-06 22:06:54 +0100598enddef
599
Bram Moolenaarc2a4b352020-02-06 22:41:16 +0100600let g:number = 42
601
602def Computing()
603 let nr = 3
604 let nrres = nr + 7
605 nrres = nr - 7
606 nrres = nr * 7
607 nrres = nr / 7
608 nrres = nr % 7
609
610 let anyres = g:number + 7
611 anyres = g:number - 7
612 anyres = g:number * 7
613 anyres = g:number / 7
614 anyres = g:number % 7
615
616 if has('float')
617 let fl = 3.0
618 let flres = fl + 7.0
619 flres = fl - 7.0
620 flres = fl * 7.0
621 flres = fl / 7.0
622 endif
623enddef
624
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100625def Test_disassemble_computing()
Bram Moolenaarc2a4b352020-02-06 22:41:16 +0100626 let instr = execute('disassemble Computing')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200627 assert_match('Computing.*' ..
628 'let nr = 3.*' ..
629 '\d STORE 3 in $0.*' ..
630 'let nrres = nr + 7.*' ..
631 '\d LOAD $0.*' ..
632 '\d PUSHNR 7.*' ..
633 '\d OPNR +.*' ..
634 '\d STORE $1.*' ..
635 'nrres = nr - 7.*' ..
636 '\d OPNR -.*' ..
637 'nrres = nr \* 7.*' ..
638 '\d OPNR \*.*' ..
639 'nrres = nr / 7.*' ..
640 '\d OPNR /.*' ..
641 'nrres = nr % 7.*' ..
642 '\d OPNR %.*' ..
643 'let anyres = g:number + 7.*' ..
644 '\d LOADG g:number.*' ..
645 '\d PUSHNR 7.*' ..
646 '\d OPANY +.*' ..
647 '\d STORE $2.*' ..
648 'anyres = g:number - 7.*' ..
649 '\d OPANY -.*' ..
650 'anyres = g:number \* 7.*' ..
651 '\d OPANY \*.*' ..
652 'anyres = g:number / 7.*' ..
653 '\d OPANY /.*' ..
654 'anyres = g:number % 7.*' ..
655 '\d OPANY %.*',
656 instr)
Bram Moolenaarc2a4b352020-02-06 22:41:16 +0100657 if has('float')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200658 assert_match('Computing.*' ..
659 'let fl = 3.0.*' ..
660 '\d PUSHF 3.0.*' ..
661 '\d STORE $3.*' ..
662 'let flres = fl + 7.0.*' ..
663 '\d LOAD $3.*' ..
664 '\d PUSHF 7.0.*' ..
665 '\d OPFLOAT +.*' ..
666 '\d STORE $4.*' ..
667 'flres = fl - 7.0.*' ..
668 '\d OPFLOAT -.*' ..
669 'flres = fl \* 7.0.*' ..
670 '\d OPFLOAT \*.*' ..
671 'flres = fl / 7.0.*' ..
672 '\d OPFLOAT /.*',
673 instr)
Bram Moolenaarc2a4b352020-02-06 22:41:16 +0100674 endif
675enddef
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100676
Bram Moolenaaree2e52a2020-02-19 14:17:18 +0100677def AddListBlob()
678 let reslist = [1, 2] + [3, 4]
679 let resblob = 0z1122 + 0z3344
680enddef
681
682def Test_disassemble_add_list_blob()
683 let instr = execute('disassemble AddListBlob')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200684 assert_match('AddListBlob.*' ..
685 'let reslist = \[1, 2] + \[3, 4].*' ..
686 '\d PUSHNR 1.*' ..
687 '\d PUSHNR 2.*' ..
688 '\d NEWLIST size 2.*' ..
689 '\d PUSHNR 3.*' ..
690 '\d PUSHNR 4.*' ..
691 '\d NEWLIST size 2.*' ..
692 '\d ADDLIST.*' ..
693 '\d STORE $.*.*' ..
694 'let resblob = 0z1122 + 0z3344.*' ..
695 '\d PUSHBLOB 0z1122.*' ..
696 '\d PUSHBLOB 0z3344.*' ..
697 '\d ADDBLOB.*' ..
698 '\d STORE $.*',
699 instr)
Bram Moolenaaree2e52a2020-02-19 14:17:18 +0100700enddef
701
702let g:aa = 'aa'
703def ConcatString(): string
704 let res = g:aa .. "bb"
705 return res
706enddef
707
708def Test_disassemble_concat()
709 let instr = execute('disassemble ConcatString')
Bram Moolenaar675f7162020-04-12 22:53:54 +0200710 assert_match('ConcatString.*' ..
711 'let res = g:aa .. "bb".*' ..
712 '\d LOADG g:aa.*' ..
713 '\d PUSHS "bb".*' ..
714 '\d 2STRING stack\[-2].*' ..
715 '\d CONCAT.*' ..
716 '\d STORE $.*',
717 instr)
Bram Moolenaaree2e52a2020-02-19 14:17:18 +0100718 assert_equal('aabb', ConcatString())
719enddef
720
721def ListIndex(): number
722 let l = [1, 2, 3]
723 let res = l[1]
724 return res
725enddef
726
727def Test_disassemble_list_index()
728 let instr = execute('disassemble ListIndex')
Bram Moolenaar4902ab12020-05-15 19:21:31 +0200729 assert_match('ListIndex\_s*' ..
730 'let l = \[1, 2, 3]\_s*' ..
731 '\d PUSHNR 1\_s*' ..
732 '\d PUSHNR 2\_s*' ..
733 '\d PUSHNR 3\_s*' ..
734 '\d NEWLIST size 3\_s*' ..
735 '\d STORE $0\_s*' ..
736 'let res = l\[1]\_s*' ..
737 '\d LOAD $0\_s*' ..
738 '\d PUSHNR 1\_s*' ..
739 '\d INDEX\_s*' ..
740 '\d STORE $1\_s*',
Bram Moolenaar675f7162020-04-12 22:53:54 +0200741 instr)
Bram Moolenaaree2e52a2020-02-19 14:17:18 +0100742 assert_equal(2, ListIndex())
743enddef
744
745def DictMember(): number
746 let d = #{item: 1}
747 let res = d.item
Bram Moolenaar4902ab12020-05-15 19:21:31 +0200748 res = d["item"]
Bram Moolenaaree2e52a2020-02-19 14:17:18 +0100749 return res
750enddef
751
752def Test_disassemble_dict_member()
753 let instr = execute('disassemble DictMember')
Bram Moolenaar4902ab12020-05-15 19:21:31 +0200754 assert_match('DictMember\_s*' ..
755 'let d = #{item: 1}\_s*' ..
756 '\d PUSHS "item"\_s*' ..
757 '\d PUSHNR 1\_s*' ..
758 '\d NEWDICT size 1\_s*' ..
759 '\d STORE $0\_s*' ..
760 'let res = d.item\_s*' ..
761 '\d\+ LOAD $0\_s*' ..
762 '\d\+ MEMBER item\_s*' ..
763 '\d\+ STORE $1\_s*' ..
764 'res = d\["item"\]\_s*' ..
765 '\d\+ LOAD $0\_s*' ..
766 '\d\+ PUSHS "item"\_s*' ..
767 '\d\+ MEMBER\_s*' ..
768 '\d\+ STORE $1\_s*',
Bram Moolenaar675f7162020-04-12 22:53:54 +0200769 instr)
Bram Moolenaaree2e52a2020-02-19 14:17:18 +0100770 call assert_equal(1, DictMember())
771enddef
772
773def NegateNumber(): number
774 let nr = 9
775 let plus = +nr
776 let res = -nr
777 return res
778enddef
779
780def Test_disassemble_negate_number()
781 let instr = execute('disassemble NegateNumber')
Bram Moolenaar4902ab12020-05-15 19:21:31 +0200782 assert_match('NegateNumber\_s*' ..
783 'let nr = 9\_s*' ..
784 '\d STORE 9 in $0\_s*' ..
785 'let plus = +nr\_s*' ..
786 '\d LOAD $0\_s*' ..
787 '\d CHECKNR\_s*' ..
788 '\d STORE $1\_s*' ..
789 'let res = -nr\_s*' ..
790 '\d LOAD $0\_s*' ..
791 '\d NEGATENR\_s*' ..
792 '\d STORE $2\_s*',
Bram Moolenaar675f7162020-04-12 22:53:54 +0200793 instr)
Bram Moolenaaree2e52a2020-02-19 14:17:18 +0100794 call assert_equal(-9, NegateNumber())
795enddef
796
797def InvertBool(): bool
798 let flag = true
799 let invert = !flag
800 let res = !!flag
801 return res
802enddef
803
804def Test_disassemble_invert_bool()
805 let instr = execute('disassemble InvertBool')
Bram Moolenaar4902ab12020-05-15 19:21:31 +0200806 assert_match('InvertBool\_s*' ..
807 'let flag = true\_s*' ..
808 '\d PUSH v:true\_s*' ..
809 '\d STORE $0\_s*' ..
810 'let invert = !flag\_s*' ..
811 '\d LOAD $0\_s*' ..
812 '\d INVERT (!val)\_s*' ..
813 '\d STORE $1\_s*' ..
814 'let res = !!flag\_s*' ..
815 '\d LOAD $0\_s*' ..
816 '\d 2BOOL (!!val)\_s*' ..
817 '\d STORE $2\_s*',
Bram Moolenaar675f7162020-04-12 22:53:54 +0200818 instr)
Bram Moolenaaree2e52a2020-02-19 14:17:18 +0100819 call assert_equal(true, InvertBool())
820enddef
821
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100822def Test_disassemble_compare()
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100823 let cases = [
Bram Moolenaara5565e42020-05-09 15:44:01 +0200824 ['true == isFalse', 'COMPAREBOOL =='],
825 ['true != isFalse', 'COMPAREBOOL !='],
826 ['v:none == isNull', 'COMPARESPECIAL =='],
827 ['v:none != isNull', 'COMPARESPECIAL !='],
Bram Moolenaar675f7162020-04-12 22:53:54 +0200828
Bram Moolenaara5565e42020-05-09 15:44:01 +0200829 ['111 == aNumber', 'COMPARENR =='],
830 ['111 != aNumber', 'COMPARENR !='],
831 ['111 > aNumber', 'COMPARENR >'],
832 ['111 < aNumber', 'COMPARENR <'],
833 ['111 >= aNumber', 'COMPARENR >='],
834 ['111 <= aNumber', 'COMPARENR <='],
835 ['111 =~ aNumber', 'COMPARENR =\~'],
836 ['111 !~ aNumber', 'COMPARENR !\~'],
Bram Moolenaar675f7162020-04-12 22:53:54 +0200837
Bram Moolenaara5565e42020-05-09 15:44:01 +0200838 ['"xx" != aString', 'COMPARESTRING !='],
839 ['"xx" > aString', 'COMPARESTRING >'],
840 ['"xx" < aString', 'COMPARESTRING <'],
841 ['"xx" >= aString', 'COMPARESTRING >='],
842 ['"xx" <= aString', 'COMPARESTRING <='],
843 ['"xx" =~ aString', 'COMPARESTRING =\~'],
844 ['"xx" !~ aString', 'COMPARESTRING !\~'],
845 ['"xx" is aString', 'COMPARESTRING is'],
846 ['"xx" isnot aString', 'COMPARESTRING isnot'],
Bram Moolenaar675f7162020-04-12 22:53:54 +0200847
Bram Moolenaara5565e42020-05-09 15:44:01 +0200848 ['0z11 == aBlob', 'COMPAREBLOB =='],
849 ['0z11 != aBlob', 'COMPAREBLOB !='],
850 ['0z11 is aBlob', 'COMPAREBLOB is'],
851 ['0z11 isnot aBlob', 'COMPAREBLOB isnot'],
Bram Moolenaar675f7162020-04-12 22:53:54 +0200852
Bram Moolenaara5565e42020-05-09 15:44:01 +0200853 ['[1, 2] == aList', 'COMPARELIST =='],
854 ['[1, 2] != aList', 'COMPARELIST !='],
855 ['[1, 2] is aList', 'COMPARELIST is'],
856 ['[1, 2] isnot aList', 'COMPARELIST isnot'],
Bram Moolenaar675f7162020-04-12 22:53:54 +0200857
Bram Moolenaara5565e42020-05-09 15:44:01 +0200858 ['#{a: 1} == aDict', 'COMPAREDICT =='],
859 ['#{a: 1} != aDict', 'COMPAREDICT !='],
860 ['#{a: 1} is aDict', 'COMPAREDICT is'],
861 ['#{a: 1} isnot aDict', 'COMPAREDICT isnot'],
Bram Moolenaar675f7162020-04-12 22:53:54 +0200862
863 ['{->33} == {->44}', 'COMPAREFUNC =='],
864 ['{->33} != {->44}', 'COMPAREFUNC !='],
865 ['{->33} is {->44}', 'COMPAREFUNC is'],
866 ['{->33} isnot {->44}', 'COMPAREFUNC isnot'],
867
868 ['77 == g:xx', 'COMPAREANY =='],
869 ['77 != g:xx', 'COMPAREANY !='],
870 ['77 > g:xx', 'COMPAREANY >'],
871 ['77 < g:xx', 'COMPAREANY <'],
872 ['77 >= g:xx', 'COMPAREANY >='],
873 ['77 <= g:xx', 'COMPAREANY <='],
874 ['77 =~ g:xx', 'COMPAREANY =\~'],
875 ['77 !~ g:xx', 'COMPAREANY !\~'],
876 ['77 is g:xx', 'COMPAREANY is'],
877 ['77 isnot g:xx', 'COMPAREANY isnot'],
878 ]
Bram Moolenaara5565e42020-05-09 15:44:01 +0200879 let floatDecl = ''
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100880 if has('float')
881 cases->extend([
Bram Moolenaara5565e42020-05-09 15:44:01 +0200882 ['1.1 == aFloat', 'COMPAREFLOAT =='],
883 ['1.1 != aFloat', 'COMPAREFLOAT !='],
884 ['1.1 > aFloat', 'COMPAREFLOAT >'],
885 ['1.1 < aFloat', 'COMPAREFLOAT <'],
886 ['1.1 >= aFloat', 'COMPAREFLOAT >='],
887 ['1.1 <= aFloat', 'COMPAREFLOAT <='],
888 ['1.1 =~ aFloat', 'COMPAREFLOAT =\~'],
889 ['1.1 !~ aFloat', 'COMPAREFLOAT !\~'],
Bram Moolenaar675f7162020-04-12 22:53:54 +0200890 ])
Bram Moolenaara5565e42020-05-09 15:44:01 +0200891 floatDecl = 'let aFloat = 2.2'
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100892 endif
893
894 let nr = 1
895 for case in cases
Bram Moolenaara5565e42020-05-09 15:44:01 +0200896 " declare local variables to get a non-constant with the right type
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100897 writefile(['def TestCase' .. nr .. '()',
Bram Moolenaara5565e42020-05-09 15:44:01 +0200898 ' let isFalse = false',
899 ' let isNull = v:null',
900 ' let aNumber = 222',
901 ' let aString = "yy"',
902 ' let aBlob = 0z22',
903 ' let aList = [3, 4]',
904 ' let aDict = #{x: 2}',
905 floatDecl,
Bram Moolenaar675f7162020-04-12 22:53:54 +0200906 ' if ' .. case[0],
907 ' echo 42'
908 ' endif',
909 'enddef'], 'Xdisassemble')
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100910 source Xdisassemble
911 let instr = execute('disassemble TestCase' .. nr)
Bram Moolenaar675f7162020-04-12 22:53:54 +0200912 assert_match('TestCase' .. nr .. '.*' ..
913 'if ' .. substitute(case[0], '[[~]', '\\\0', 'g') .. '.*' ..
914 '\d \(PUSH\|FUNCREF\).*' ..
Bram Moolenaara5565e42020-05-09 15:44:01 +0200915 '\d \(PUSH\|FUNCREF\|LOAD\).*' ..
Bram Moolenaar675f7162020-04-12 22:53:54 +0200916 '\d ' .. case[1] .. '.*' ..
917 '\d JUMP_IF_FALSE -> \d\+.*',
918 instr)
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100919
920 nr += 1
921 endfor
922
Bram Moolenaar22da5592020-03-19 14:52:20 +0100923 delete('Xdisassemble')
Bram Moolenaarf2460a32020-02-07 22:09:54 +0100924enddef
925
Bram Moolenaara4d4cf42020-04-02 13:50:27 +0200926def Test_disassemble_compare_const()
927 let cases = [
Bram Moolenaar675f7162020-04-12 22:53:54 +0200928 ['"xx" == "yy"', false],
929 ['"aa" == "aa"', true],
930 ['has("eval") ? true : false', true],
931 ['has("asdf") ? true : false', false],
932 ]
Bram Moolenaara4d4cf42020-04-02 13:50:27 +0200933
934 let nr = 1
935 for case in cases
936 writefile(['def TestCase' .. nr .. '()',
Bram Moolenaar675f7162020-04-12 22:53:54 +0200937 ' if ' .. case[0],
938 ' echo 42'
939 ' endif',
940 'enddef'], 'Xdisassemble')
Bram Moolenaara4d4cf42020-04-02 13:50:27 +0200941 source Xdisassemble
942 let instr = execute('disassemble TestCase' .. nr)
943 if case[1]
944 " condition true, "echo 42" executed
Bram Moolenaar675f7162020-04-12 22:53:54 +0200945 assert_match('TestCase' .. nr .. '.*' ..
946 'if ' .. substitute(case[0], '[[~]', '\\\0', 'g') .. '.*' ..
947 '\d PUSHNR 42.*' ..
948 '\d ECHO 1.*' ..
949 '\d PUSHNR 0.*' ..
950 '\d RETURN.*',
951 instr)
Bram Moolenaara4d4cf42020-04-02 13:50:27 +0200952 else
953 " condition false, function just returns
Bram Moolenaar675f7162020-04-12 22:53:54 +0200954 assert_match('TestCase' .. nr .. '.*' ..
955 'if ' .. substitute(case[0], '[[~]', '\\\0', 'g') .. '[ \n]*' ..
956 'echo 42[ \n]*' ..
957 'endif[ \n]*' ..
958 '\s*\d PUSHNR 0.*' ..
959 '\d RETURN.*',
960 instr)
Bram Moolenaara4d4cf42020-04-02 13:50:27 +0200961 endif
962
963 nr += 1
964 endfor
965
966 delete('Xdisassemble')
967enddef
968
Bram Moolenaarad39c092020-02-26 18:23:43 +0100969def s:Execute()
970 execute 'help vim9.txt'
971 let cmd = 'help vim9.txt'
972 execute cmd
973 let tag = 'vim9.txt'
974 execute 'help ' .. tag
975enddef
976
977def Test_disassemble_execute()
978 let res = execute('disass s:Execute')
Bram Moolenaar4902ab12020-05-15 19:21:31 +0200979 assert_match('\<SNR>\d*_Execute\_s*' ..
980 "execute 'help vim9.txt'\\_s*" ..
981 '\d PUSHS "help vim9.txt"\_s*' ..
982 '\d EXECUTE 1\_s*' ..
983 "let cmd = 'help vim9.txt'\\_s*" ..
984 '\d PUSHS "help vim9.txt"\_s*' ..
985 '\d STORE $0\_s*' ..
986 'execute cmd\_s*' ..
987 '\d LOAD $0\_s*' ..
988 '\d EXECUTE 1\_s*' ..
989 "let tag = 'vim9.txt'\\_s*" ..
990 '\d PUSHS "vim9.txt"\_s*' ..
991 '\d STORE $1\_s*' ..
992 "execute 'help ' .. tag\\_s*" ..
993 '\d\+ PUSHS "help "\_s*' ..
994 '\d\+ LOAD $1\_s*' ..
995 '\d\+ CONCAT\_s*' ..
996 '\d\+ EXECUTE 1\_s*' ..
997 '\d\+ PUSHNR 0\_s*' ..
998 '\d\+ RETURN',
Bram Moolenaar675f7162020-04-12 22:53:54 +0200999 res)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001000enddef
1001
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001002def s:Echomsg()
1003 echomsg 'some' 'message'
1004 echoerr 'went' .. 'wrong'
1005enddef
1006
1007def Test_disassemble_echomsg()
1008 let res = execute('disass s:Echomsg')
Bram Moolenaar4902ab12020-05-15 19:21:31 +02001009 assert_match('\<SNR>\d*_Echomsg\_s*' ..
1010 "echomsg 'some' 'message'\\_s*" ..
1011 '\d PUSHS "some"\_s*' ..
1012 '\d PUSHS "message"\_s*' ..
1013 '\d ECHOMSG 2\_s*' ..
1014 "echoerr 'went' .. 'wrong'\\_s*" ..
1015 '\d PUSHS "wentwrong"\_s*' ..
1016 '\d ECHOERR 1\_s*' ..
1017 '\d PUSHNR 0\_s*' ..
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001018 '\d RETURN',
1019 res)
1020enddef
1021
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001022def SomeStringArg(arg: string)
1023 echo arg
1024enddef
1025
1026def SomeAnyArg(arg: any)
1027 echo arg
1028enddef
1029
1030def SomeStringArgAndReturn(arg: string): string
1031 return arg
1032enddef
1033
1034def Test_display_func()
1035 let res1 = execute('function SomeStringArg')
Bram Moolenaar4902ab12020-05-15 19:21:31 +02001036 assert_match('.* def SomeStringArg(arg: string)\_s*' ..
1037 '\d *echo arg.*' ..
1038 ' *enddef',
Bram Moolenaar675f7162020-04-12 22:53:54 +02001039 res1)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001040
1041 let res2 = execute('function SomeAnyArg')
Bram Moolenaar4902ab12020-05-15 19:21:31 +02001042 assert_match('.* def SomeAnyArg(arg: any)\_s*' ..
1043 '\d *echo arg\_s*' ..
1044 ' *enddef',
Bram Moolenaar675f7162020-04-12 22:53:54 +02001045 res2)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001046
1047 let res3 = execute('function SomeStringArgAndReturn')
Bram Moolenaar4902ab12020-05-15 19:21:31 +02001048 assert_match('.* def SomeStringArgAndReturn(arg: string): string\_s*' ..
1049 '\d *return arg\_s*' ..
1050 ' *enddef',
Bram Moolenaar675f7162020-04-12 22:53:54 +02001051 res3)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001052enddef
1053
Bram Moolenaar09689a02020-05-09 22:50:08 +02001054def Test_vim9script_forward_func()
1055 let lines =<< trim END
1056 vim9script
1057 def FuncOne(): string
1058 return FuncTwo()
1059 enddef
1060 def FuncTwo(): string
1061 return 'two'
1062 enddef
Bram Moolenaar32e35112020-05-14 22:41:15 +02001063 let g:res_FuncOne: string = execute('disass FuncOne')
Bram Moolenaar09689a02020-05-09 22:50:08 +02001064 END
1065 writefile(lines, 'Xdisassemble')
1066 source Xdisassemble
1067
1068 " check that the first function calls the second with DCALL
Bram Moolenaar4902ab12020-05-15 19:21:31 +02001069 assert_match('\<SNR>\d*_FuncOne\_s*' ..
1070 'return FuncTwo()\_s*' ..
1071 '\d DCALL <SNR>\d\+_FuncTwo(argc 0)\_s*' ..
Bram Moolenaar09689a02020-05-09 22:50:08 +02001072 '\d RETURN',
1073 g:res_FuncOne)
1074
1075 delete('Xdisassemble')
1076 unlet g:res_FuncOne
1077enddef
1078
Bram Moolenaar61a89812020-05-07 16:58:17 +02001079def s:ConcatStrings(): string
1080 return 'one' .. 'two' .. 'three'
1081enddef
1082
Bram Moolenaar7d131b02020-05-08 19:10:34 +02001083def s:ComputeConst(): number
1084 return 2 + 3 * 4 / 6 + 7
1085enddef
1086
Bram Moolenaar1c747212020-05-09 18:28:34 +02001087def s:ComputeConstParen(): number
1088 return ((2 + 4) * (8 / 2)) / (3 + 4)
1089enddef
1090
Bram Moolenaar61a89812020-05-07 16:58:17 +02001091def Test_simplify_const_expr()
1092 let res = execute('disass s:ConcatStrings')
Bram Moolenaar4902ab12020-05-15 19:21:31 +02001093 assert_match('<SNR>\d*_ConcatStrings\_s*' ..
1094 "return 'one' .. 'two' .. 'three'\\_s*" ..
1095 '\d PUSHS "onetwothree"\_s*' ..
Bram Moolenaar61a89812020-05-07 16:58:17 +02001096 '\d RETURN',
1097 res)
Bram Moolenaar7d131b02020-05-08 19:10:34 +02001098
1099 res = execute('disass s:ComputeConst')
Bram Moolenaar4902ab12020-05-15 19:21:31 +02001100 assert_match('<SNR>\d*_ComputeConst\_s*' ..
1101 'return 2 + 3 \* 4 / 6 + 7\_s*' ..
1102 '\d PUSHNR 11\_s*' ..
Bram Moolenaar7d131b02020-05-08 19:10:34 +02001103 '\d RETURN',
1104 res)
Bram Moolenaar1c747212020-05-09 18:28:34 +02001105
1106 res = execute('disass s:ComputeConstParen')
Bram Moolenaar4902ab12020-05-15 19:21:31 +02001107 assert_match('<SNR>\d*_ComputeConstParen\_s*' ..
1108 'return ((2 + 4) \* (8 / 2)) / (3 + 4)\_s*' ..
1109 '\d PUSHNR 3\>\_s*' ..
Bram Moolenaar1c747212020-05-09 18:28:34 +02001110 '\d RETURN',
1111 res)
Bram Moolenaar61a89812020-05-07 16:58:17 +02001112enddef
1113
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001114" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker