1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
|
" Test the :disassemble command, and compilation as a side effect
source check.vim
func NotCompiled()
echo "not"
endfunc
let s:scriptvar = 4
let g:globalvar = 'g'
def s:ScriptFuncLoad(arg: string)
let local = 1
buffers
echo arg
echo local
echo v:version
echo s:scriptvar
echo g:globalvar
echo &tabstop
echo $ENVVAR
echo @z
enddef
def Test_disassemble_load()
assert_fails('disass NoFunc', 'E1061:')
assert_fails('disass NotCompiled', 'E1062:')
assert_fails('disass', 'E471:')
assert_fails('disass [', 'E475:')
assert_fails('disass 234', 'E475:')
assert_fails('disass <XX>foo', 'E475:')
let res = execute('disass s:ScriptFuncLoad')
assert_match('<SNR>\d*_ScriptFuncLoad.*'
\ .. 'buffers.*'
\ .. ' EXEC \+buffers.*'
\ .. ' LOAD arg\[-1\].*'
\ .. ' LOAD $0.*'
\ .. ' LOADV v:version.*'
\ .. ' LOADS s:scriptvar from .*test_vim9_disassemble.vim.*'
\ .. ' LOADG g:globalvar.*'
\ .. ' LOADENV $ENVVAR.*'
\ .. ' LOADREG @z.*'
\, res)
enddef
def s:ScriptFuncPush()
let localbool = true
let localspec = v:none
let localblob = 0z1234
if has('float')
let localfloat = 1.234
endif
enddef
def Test_disassemble_push()
let res = execute('disass s:ScriptFuncPush')
assert_match('<SNR>\d*_ScriptFuncPush.*'
\ .. 'localbool = true.*'
\ .. ' PUSH v:true.*'
\ .. 'localspec = v:none.*'
\ .. ' PUSH v:none.*'
\ .. 'localblob = 0z1234.*'
\ .. ' PUSHBLOB 0z1234.*'
\, res)
if has('float')
assert_match('<SNR>\d*_ScriptFuncPush.*'
\ .. 'localfloat = 1.234.*'
\ .. ' PUSHF 1.234.*'
\, res)
endif
enddef
def s:ScriptFuncStore()
let localnr = 1
localnr = 2
let localstr = 'abc'
localstr = 'xyz'
v:char = 'abc'
s:scriptvar = 'sv'
g:globalvar = 'gv'
&tabstop = 8
$ENVVAR = 'ev'
@z = 'rv'
enddef
def Test_disassemble_store()
let res = execute('disass s:ScriptFuncStore')
assert_match('<SNR>\d*_ScriptFuncStore.*'
\ .. 'localnr = 2.*'
\ .. ' STORE 2 in $0.*'
\ .. 'localstr = ''xyz''.*'
\ .. ' STORE $1.*'
\ .. 'v:char = ''abc''.*'
\ .. 'STOREV v:char.*'
\ .. 's:scriptvar = ''sv''.*'
\ .. ' STORES s:scriptvar in .*test_vim9_disassemble.vim.*'
\ .. 'g:globalvar = ''gv''.*'
\ .. ' STOREG g:globalvar.*'
\ .. '&tabstop = 8.*'
\ .. ' STOREOPT &tabstop.*'
\ .. '$ENVVAR = ''ev''.*'
\ .. ' STOREENV $ENVVAR.*'
\ .. '@z = ''rv''.*'
\ .. ' STOREREG @z.*'
\, res)
enddef
def s:ScriptFuncTry()
try
echo 'yes'
catch /fail/
echo 'no'
finally
throw 'end'
endtry
enddef
def Test_disassemble_try()
let res = execute('disass s:ScriptFuncTry')
assert_match('<SNR>\d*_ScriptFuncTry.*'
\ .. 'try.*'
\ .. 'TRY catch -> \d\+, finally -> \d\+.*'
\ .. 'catch /fail/.*'
\ .. ' JUMP -> \d\+.*'
\ .. ' PUSH v:exception.*'
\ .. ' PUSHS "fail".*'
\ .. ' COMPARESTRING =\~.*'
\ .. ' JUMP_IF_FALSE -> \d\+.*'
\ .. ' CATCH.*'
\ .. 'finally.*'
\ .. ' PUSHS "end".*'
\ .. ' THROW.*'
\ .. 'endtry.*'
\ .. ' ENDTRY.*'
\, res)
enddef
def s:ScriptFuncNew()
let ll = [1, "two", 333]
let dd = #{one: 1, two: "val"}
enddef
def Test_disassemble_new()
let res = execute('disass s:ScriptFuncNew')
assert_match('<SNR>\d*_ScriptFuncNew.*'
\ .. 'let ll = \[1, "two", 333].*'
\ .. 'PUSHNR 1.*'
\ .. 'PUSHS "two".*'
\ .. 'PUSHNR 333.*'
\ .. 'NEWLIST size 3.*'
\ .. 'let dd = #{one: 1, two: "val"}.*'
\ .. 'PUSHS "one".*'
\ .. 'PUSHNR 1.*'
\ .. 'PUSHS "two".*'
\ .. 'PUSHS "val".*'
\ .. 'NEWDICT size 2.*'
\, res)
enddef
def FuncWithArg(arg)
echo arg
enddef
func UserFunc()
echo 'nothing'
endfunc
func UserFuncWithArg(arg)
echo a:arg
endfunc
def s:ScriptFuncCall(): string
changenr()
char2nr("abc")
Test_disassemble_new()
FuncWithArg(343)
ScriptFuncNew()
s:ScriptFuncNew()
UserFunc()
UserFuncWithArg("foo")
let FuncRef = function("UserFunc")
FuncRef()
let FuncRefWithArg = function("UserFuncWithArg")
FuncRefWithArg("bar")
return "yes"
enddef
def Test_disassemble_call()
let res = execute('disass s:ScriptFuncCall')
assert_match('<SNR>\d\+_ScriptFuncCall.*'
\ .. 'changenr().*'
\ .. ' BCALL changenr(argc 0).*'
\ .. 'char2nr("abc").*'
\ .. ' PUSHS "abc".*'
\ .. ' BCALL char2nr(argc 1).*'
\ .. 'Test_disassemble_new().*'
\ .. ' DCALL Test_disassemble_new(argc 0).*'
\ .. 'FuncWithArg(343).*'
\ .. ' PUSHNR 343.*'
\ .. ' DCALL FuncWithArg(argc 1).*'
\ .. 'ScriptFuncNew().*'
\ .. ' DCALL <SNR>\d\+_ScriptFuncNew(argc 0).*'
\ .. 's:ScriptFuncNew().*'
\ .. ' DCALL <SNR>\d\+_ScriptFuncNew(argc 0).*'
\ .. 'UserFunc().*'
\ .. ' UCALL UserFunc(argc 0).*'
\ .. 'UserFuncWithArg("foo").*'
\ .. ' PUSHS "foo".*'
\ .. ' UCALL UserFuncWithArg(argc 1).*'
\ .. 'let FuncRef = function("UserFunc").*'
\ .. 'FuncRef().*'
\ .. ' LOAD $\d.*'
\ .. ' PCALL (argc 0).*'
\ .. 'let FuncRefWithArg = function("UserFuncWithArg").*'
\ .. 'FuncRefWithArg("bar").*'
\ .. ' PUSHS "bar".*'
\ .. ' LOAD $\d.*'
\ .. ' PCALL (argc 1).*'
\ .. 'return "yes".*'
\ .. ' PUSHS "yes".*'
\ .. ' RETURN.*'
\, res)
enddef
def EchoArg(arg: string): string
return arg
enddef
def RefThis(): func
return function('EchoArg')
enddef
def s:ScriptPCall()
RefThis()("text")
enddef
def Test_disassemble_pcall()
let res = execute('disass s:ScriptPCall')
assert_match('<SNR>\d\+_ScriptPCall.*'
\ .. 'RefThis()("text").*'
\ .. '\d DCALL RefThis(argc 0).*'
\ .. '\d PUSHS "text".*'
\ .. '\d PCALL top (argc 1).*'
\ .. '\d PCALL end.*'
\ .. '\d DROP.*'
\ .. '\d PUSHNR 0.*'
\ .. '\d RETURN.*'
\, res)
enddef
def FuncWithForwardCall(): string
return DefinedLater("yes")
enddef
def DefinedLater(arg: string): string
return arg
enddef
def Test_disassemble_update_instr()
let res = execute('disass FuncWithForwardCall')
assert_match('FuncWithForwardCall.*'
\ .. 'return DefinedLater("yes").*'
\ .. '\d PUSHS "yes".*'
\ .. '\d UCALL DefinedLater(argc 1).*'
\ .. '\d CHECKTYPE string stack\[-1].*'
\ .. '\d RETURN.*'
\, res)
" Calling the function will change UCALL into the faster DCALL
assert_equal('yes', FuncWithForwardCall())
res = execute('disass FuncWithForwardCall')
assert_match('FuncWithForwardCall.*'
\ .. 'return DefinedLater("yes").*'
\ .. '\d PUSHS "yes".*'
\ .. '\d DCALL DefinedLater(argc 1).*'
\ .. '\d CHECKTYPE string stack\[-1].*'
\ .. '\d RETURN.*'
\, res)
enddef
def FuncWithDefault(arg: string = 'default'): string
return arg
enddef
def Test_disassemble_call_default()
let res = execute('disass FuncWithDefault')
assert_match('FuncWithDefault.*'
\ .. '\d PUSHS "default".*'
\ .. '\d STORE arg\[-1].*'
\ .. 'return arg.*'
\ .. '\d LOAD arg\[-1].*'
\ .. '\d RETURN.*'
\, res)
enddef
def HasEval()
if has("eval")
echo "yes"
else
echo "no"
endif
enddef
def HasNothing()
if has("nothing")
echo "yes"
else
echo "no"
endif
enddef
def HasSomething()
if has("nothing")
echo "nothing"
elseif has("something")
echo "something"
elseif has("eval")
echo "eval"
elseif has("less")
echo "less"
endif
enddef
def Test_disassemble_const_expr()
assert_equal("\nyes", execute('call HasEval()'))
let instr = execute('disassemble HasEval')
assert_match('HasEval.*'
\ .. 'if has("eval").*'
\ .. ' PUSHS "yes".*'
\, instr)
assert_notmatch('JUMP', instr)
assert_equal("\nno", execute('call HasNothing()'))
instr = execute('disassemble HasNothing')
assert_match('HasNothing.*'
\ .. 'if has("nothing").*'
\ .. 'else.*'
\ .. ' PUSHS "no".*'
\, instr)
assert_notmatch('PUSHS "yes"', instr)
assert_notmatch('JUMP', instr)
assert_equal("\neval", execute('call HasSomething()'))
instr = execute('disassemble HasSomething')
assert_match('HasSomething.*'
\ .. 'if has("nothing").*'
\ .. 'elseif has("something").*'
\ .. 'elseif has("eval").*'
\ .. ' PUSHS "eval".*'
\ .. 'elseif has("less").*'
\, instr)
assert_notmatch('PUSHS "nothing"', instr)
assert_notmatch('PUSHS "something"', instr)
assert_notmatch('PUSHS "less"', instr)
assert_notmatch('JUMP', instr)
enddef
def WithFunc()
let funky1: func
let funky2: func = function("len")
let party1: partial
let party2: partial = funcref("UserFunc")
enddef
def Test_disassemble_function()
let instr = execute('disassemble WithFunc')
assert_match('WithFunc.*'
\ .. 'let funky1: func.*'
\ .. '0 PUSHFUNC "\[none]".*'
\ .. '1 STORE $0.*'
\ .. 'let funky2: func = function("len").*'
\ .. '2 PUSHS "len".*'
\ .. '3 BCALL function(argc 1).*'
\ .. '4 STORE $1.*'
\ .. 'let party1: partial.*'
\ .. '5 PUSHPARTIAL "\[none]".*'
\ .. '6 STORE $2.*'
\ .. 'let party2: partial = funcref("UserFunc").*'
\ .. '7 PUSHS "UserFunc".*'
\ .. '8 BCALL funcref(argc 1).*'
\ .. '9 STORE $3.*'
\ .. '10 PUSHNR 0.*'
\ .. '11 RETURN.*'
\, instr)
enddef
if has('channel')
def WithChannel()
let job1: job
let job2: job = job_start("donothing")
let chan1: channel
enddef
endif
def Test_disassemble_channel()
CheckFeature channel
let instr = execute('disassemble WithChannel')
assert_match('WithChannel.*'
\ .. 'let job1: job.*'
\ .. '\d PUSHJOB "no process".*'
\ .. '\d STORE $0.*'
\ .. 'let job2: job = job_start("donothing").*'
\ .. '\d PUSHS "donothing".*'
\ .. '\d BCALL job_start(argc 1).*'
\ .. '\d STORE $1.*'
\ .. 'let chan1: channel.*'
\ .. '\d PUSHCHANNEL 0.*'
\ .. '\d STORE $2.*'
\ .. '\d PUSHNR 0.*'
\ .. '\d RETURN.*'
\, instr)
enddef
def WithLambda(): string
let F = {a -> "X" .. a .. "X"}
return F("x")
enddef
def Test_disassemble_lambda()
assert_equal("XxX", WithLambda())
let instr = execute('disassemble WithLambda')
assert_match('WithLambda.*'
\ .. 'let F = {a -> "X" .. a .. "X"}.*'
\ .. ' FUNCREF <lambda>\d\+.*'
\ .. 'PUSHS "x".*'
\ .. ' LOAD $0.*'
\ .. ' PCALL (argc 1).*'
\ .. ' CHECKTYPE string stack\[-1].*'
\, instr)
enddef
def AndOr(arg): string
if arg == 1 && arg != 2 || arg == 4
return 'yes'
endif
return 'no'
enddef
def Test_disassemble_and_or()
assert_equal("yes", AndOr(1))
assert_equal("no", AndOr(2))
assert_equal("yes", AndOr(4))
let instr = execute('disassemble AndOr')
assert_match('AndOr.*'
\ .. 'if arg == 1 && arg != 2 || arg == 4.*'
\ .. '\d LOAD arg\[-1].*'
\ .. '\d PUSHNR 1.*'
\ .. '\d COMPAREANY ==.*'
\ .. '\d JUMP_AND_KEEP_IF_FALSE -> \d\+.*'
\ .. '\d LOAD arg\[-1].*'
\ .. '\d PUSHNR 2.*'
\ .. '\d COMPAREANY !=.*'
\ .. '\d JUMP_AND_KEEP_IF_TRUE -> \d\+.*'
\ .. '\d LOAD arg\[-1].*'
\ .. '\d PUSHNR 4.*'
\ .. '\d COMPAREANY ==.*'
\ .. '\d JUMP_IF_FALSE -> \d\+.*'
\, instr)
enddef
def ForLoop(): list<number>
let res: list<number>
for i in range(3)
res->add(i)
endfor
return res
enddef
def Test_disassemble_for_loop()
assert_equal([0, 1, 2], ForLoop())
let instr = execute('disassemble ForLoop')
assert_match('ForLoop.*'
\ .. 'let res: list<number>.*'
\ .. ' NEWLIST size 0.*'
\ .. '\d STORE $0.*'
\ .. 'for i in range(3).*'
\ .. '\d STORE -1 in $1.*'
\ .. '\d PUSHNR 3.*'
\ .. '\d BCALL range(argc 1).*'
\ .. '\d FOR $1 -> \d\+.*'
\ .. '\d STORE $2.*'
\ .. 'res->add(i).*'
\ .. '\d LOAD $0.*'
\ .. '\d LOAD $2.*'
\ .. '\d BCALL add(argc 2).*'
\ .. '\d DROP.*'
\ .. 'endfor.*'
\ .. '\d JUMP -> \d\+.*'
\ .. '\d DROP.*'
\, instr)
enddef
let g:number = 42
def Computing()
let nr = 3
let nrres = nr + 7
nrres = nr - 7
nrres = nr * 7
nrres = nr / 7
nrres = nr % 7
let anyres = g:number + 7
anyres = g:number - 7
anyres = g:number * 7
anyres = g:number / 7
anyres = g:number % 7
if has('float')
let fl = 3.0
let flres = fl + 7.0
flres = fl - 7.0
flres = fl * 7.0
flres = fl / 7.0
endif
enddef
def Test_disassemble_computing()
let instr = execute('disassemble Computing')
assert_match('Computing.*'
\ .. 'let nr = 3.*'
\ .. '\d STORE 3 in $0.*'
\ .. 'let nrres = nr + 7.*'
\ .. '\d LOAD $0.*'
\ .. '\d PUSHNR 7.*'
\ .. '\d OPNR +.*'
\ .. '\d STORE $1.*'
\ .. 'nrres = nr - 7.*'
\ .. '\d OPNR -.*'
\ .. 'nrres = nr \* 7.*'
\ .. '\d OPNR \*.*'
\ .. 'nrres = nr / 7.*'
\ .. '\d OPNR /.*'
\ .. 'nrres = nr % 7.*'
\ .. '\d OPNR %.*'
\ .. 'let anyres = g:number + 7.*'
\ .. '\d LOADG g:number.*'
\ .. '\d PUSHNR 7.*'
\ .. '\d OPANY +.*'
\ .. '\d STORE $2.*'
\ .. 'anyres = g:number - 7.*'
\ .. '\d OPANY -.*'
\ .. 'anyres = g:number \* 7.*'
\ .. '\d OPANY \*.*'
\ .. 'anyres = g:number / 7.*'
\ .. '\d OPANY /.*'
\ .. 'anyres = g:number % 7.*'
\ .. '\d OPANY %.*'
\, instr)
if has('float')
assert_match('Computing.*'
\ .. 'let fl = 3.0.*'
\ .. '\d PUSHF 3.0.*'
\ .. '\d STORE $3.*'
\ .. 'let flres = fl + 7.0.*'
\ .. '\d LOAD $3.*'
\ .. '\d PUSHF 7.0.*'
\ .. '\d OPFLOAT +.*'
\ .. '\d STORE $4.*'
\ .. 'flres = fl - 7.0.*'
\ .. '\d OPFLOAT -.*'
\ .. 'flres = fl \* 7.0.*'
\ .. '\d OPFLOAT \*.*'
\ .. 'flres = fl / 7.0.*'
\ .. '\d OPFLOAT /.*'
\, instr)
endif
enddef
def AddListBlob()
let reslist = [1, 2] + [3, 4]
let resblob = 0z1122 + 0z3344
enddef
def Test_disassemble_add_list_blob()
let instr = execute('disassemble AddListBlob')
assert_match('AddListBlob.*'
\ .. 'let reslist = \[1, 2] + \[3, 4].*'
\ .. '\d PUSHNR 1.*'
\ .. '\d PUSHNR 2.*'
\ .. '\d NEWLIST size 2.*'
\ .. '\d PUSHNR 3.*'
\ .. '\d PUSHNR 4.*'
\ .. '\d NEWLIST size 2.*'
\ .. '\d ADDLIST.*'
\ .. '\d STORE $.*.*'
\ .. 'let resblob = 0z1122 + 0z3344.*'
\ .. '\d PUSHBLOB 0z1122.*'
\ .. '\d PUSHBLOB 0z3344.*'
\ .. '\d ADDBLOB.*'
\ .. '\d STORE $.*'
\, instr)
enddef
let g:aa = 'aa'
def ConcatString(): string
let res = g:aa .. "bb"
return res
enddef
def Test_disassemble_concat()
let instr = execute('disassemble ConcatString')
assert_match('ConcatString.*'
\ .. 'let res = g:aa .. "bb".*'
\ .. '\d LOADG g:aa.*'
\ .. '\d PUSHS "bb".*'
\ .. '\d 2STRING stack\[-2].*'
\ .. '\d CONCAT.*'
\ .. '\d STORE $.*'
\, instr)
assert_equal('aabb', ConcatString())
enddef
def ListIndex(): number
let l = [1, 2, 3]
let res = l[1]
return res
enddef
def Test_disassemble_list_index()
let instr = execute('disassemble ListIndex')
assert_match('ListIndex.*'
\ .. 'let l = \[1, 2, 3].*'
\ .. '\d PUSHNR 1.*'
\ .. '\d PUSHNR 2.*'
\ .. '\d PUSHNR 3.*'
\ .. '\d NEWLIST size 3.*'
\ .. '\d STORE $0.*'
\ .. 'let res = l\[1].*'
\ .. '\d LOAD $0.*'
\ .. '\d PUSHNR 1.*'
\ .. '\d INDEX.*'
\ .. '\d STORE $1.*'
\, instr)
assert_equal(2, ListIndex())
enddef
def DictMember(): number
let d = #{item: 1}
let res = d.item
return res
enddef
def Test_disassemble_dict_member()
let instr = execute('disassemble DictMember')
assert_match('DictMember.*'
\ .. 'let d = #{item: 1}.*'
\ .. '\d PUSHS "item".*'
\ .. '\d PUSHNR 1.*'
\ .. '\d NEWDICT size 1.*'
\ .. '\d STORE $0.*'
\ .. 'let res = d.item.*'
\ .. '\d LOAD $0.*'
\ .. '\d MEMBER item.*'
\ .. '\d STORE $1.*'
\, instr)
call assert_equal(1, DictMember())
enddef
def NegateNumber(): number
let nr = 9
let plus = +nr
let res = -nr
return res
enddef
def Test_disassemble_negate_number()
let instr = execute('disassemble NegateNumber')
assert_match('NegateNumber.*'
\ .. 'let nr = 9.*'
\ .. '\d STORE 9 in $0.*'
\ .. 'let plus = +nr.*'
\ .. '\d LOAD $0.*'
\ .. '\d CHECKNR.*'
\ .. '\d STORE $1.*'
\ .. 'let res = -nr.*'
\ .. '\d LOAD $0.*'
\ .. '\d NEGATENR.*'
\ .. '\d STORE $2.*'
\, instr)
call assert_equal(-9, NegateNumber())
enddef
def InvertBool(): bool
let flag = true
let invert = !flag
let res = !!flag
return res
enddef
def Test_disassemble_invert_bool()
let instr = execute('disassemble InvertBool')
assert_match('InvertBool.*'
\ .. 'let flag = true.*'
\ .. '\d PUSH v:true.*'
\ .. '\d STORE $0.*'
\ .. 'let invert = !flag.*'
\ .. '\d LOAD $0.*'
\ .. '\d INVERT (!val).*'
\ .. '\d STORE $1.*'
\ .. 'let res = !!flag.*'
\ .. '\d LOAD $0.*'
\ .. '\d 2BOOL (!!val).*'
\ .. '\d STORE $2.*'
\, instr)
call assert_equal(true, InvertBool())
enddef
def Test_disassemble_compare()
" TODO: COMPAREFUNC
let cases = [
\ ['true == false', 'COMPAREBOOL =='],
\ ['true != false', 'COMPAREBOOL !='],
\ ['v:none == v:null', 'COMPARESPECIAL =='],
\ ['v:none != v:null', 'COMPARESPECIAL !='],
\
\ ['111 == 222', 'COMPARENR =='],
\ ['111 != 222', 'COMPARENR !='],
\ ['111 > 222', 'COMPARENR >'],
\ ['111 < 222', 'COMPARENR <'],
\ ['111 >= 222', 'COMPARENR >='],
\ ['111 <= 222', 'COMPARENR <='],
\ ['111 =~ 222', 'COMPARENR =\~'],
\ ['111 !~ 222', 'COMPARENR !\~'],
\
\ ['"xx" != "yy"', 'COMPARESTRING !='],
\ ['"xx" > "yy"', 'COMPARESTRING >'],
\ ['"xx" < "yy"', 'COMPARESTRING <'],
\ ['"xx" >= "yy"', 'COMPARESTRING >='],
\ ['"xx" <= "yy"', 'COMPARESTRING <='],
\ ['"xx" =~ "yy"', 'COMPARESTRING =\~'],
\ ['"xx" !~ "yy"', 'COMPARESTRING !\~'],
\ ['"xx" is "yy"', 'COMPARESTRING is'],
\ ['"xx" isnot "yy"', 'COMPARESTRING isnot'],
\
\ ['0z11 == 0z22', 'COMPAREBLOB =='],
\ ['0z11 != 0z22', 'COMPAREBLOB !='],
\ ['0z11 is 0z22', 'COMPAREBLOB is'],
\ ['0z11 isnot 0z22', 'COMPAREBLOB isnot'],
\
\ ['[1,2] == [3,4]', 'COMPARELIST =='],
\ ['[1,2] != [3,4]', 'COMPARELIST !='],
\ ['[1,2] is [3,4]', 'COMPARELIST is'],
\ ['[1,2] isnot [3,4]', 'COMPARELIST isnot'],
\
\ ['#{a:1} == #{x:2}', 'COMPAREDICT =='],
\ ['#{a:1} != #{x:2}', 'COMPAREDICT !='],
\ ['#{a:1} is #{x:2}', 'COMPAREDICT is'],
\ ['#{a:1} isnot #{x:2}', 'COMPAREDICT isnot'],
\
\ ['{->33} == {->44}', 'COMPAREPARTIAL =='],
\ ['{->33} != {->44}', 'COMPAREPARTIAL !='],
\ ['{->33} is {->44}', 'COMPAREPARTIAL is'],
\ ['{->33} isnot {->44}', 'COMPAREPARTIAL isnot'],
\
\ ['77 == g:xx', 'COMPAREANY =='],
\ ['77 != g:xx', 'COMPAREANY !='],
\ ['77 > g:xx', 'COMPAREANY >'],
\ ['77 < g:xx', 'COMPAREANY <'],
\ ['77 >= g:xx', 'COMPAREANY >='],
\ ['77 <= g:xx', 'COMPAREANY <='],
\ ['77 =~ g:xx', 'COMPAREANY =\~'],
\ ['77 !~ g:xx', 'COMPAREANY !\~'],
\ ['77 is g:xx', 'COMPAREANY is'],
\ ['77 isnot g:xx', 'COMPAREANY isnot'],
\ ]
if has('float')
cases->extend([
\ ['1.1 == 2.2', 'COMPAREFLOAT =='],
\ ['1.1 != 2.2', 'COMPAREFLOAT !='],
\ ['1.1 > 2.2', 'COMPAREFLOAT >'],
\ ['1.1 < 2.2', 'COMPAREFLOAT <'],
\ ['1.1 >= 2.2', 'COMPAREFLOAT >='],
\ ['1.1 <= 2.2', 'COMPAREFLOAT <='],
\ ['1.1 =~ 2.2', 'COMPAREFLOAT =\~'],
\ ['1.1 !~ 2.2', 'COMPAREFLOAT !\~'],
\ ])
endif
let nr = 1
for case in cases
writefile(['def TestCase' .. nr .. '()',
\ ' if ' .. case[0],
\ ' echo 42'
\ ' endif',
\ 'enddef'], 'Xdisassemble')
source Xdisassemble
let instr = execute('disassemble TestCase' .. nr)
assert_match('TestCase' .. nr .. '.*'
\ .. 'if ' .. substitute(case[0], '[[~]', '\\\0', 'g') .. '.*'
\ .. '\d \(PUSH\|FUNCREF\).*'
\ .. '\d \(PUSH\|FUNCREF\|LOADG\).*'
\ .. '\d ' .. case[1] .. '.*'
\ .. '\d JUMP_IF_FALSE -> \d\+.*'
\, instr)
nr += 1
endfor
delete('Xdisassemble')
enddef
def Test_disassemble_compare_const()
let cases = [
\ ['"xx" == "yy"', false],
\ ['"aa" == "aa"', true],
\ ]
let nr = 1
for case in cases
writefile(['def TestCase' .. nr .. '()',
\ ' if ' .. case[0],
\ ' echo 42'
\ ' endif',
\ 'enddef'], 'Xdisassemble')
source Xdisassemble
let instr = execute('disassemble TestCase' .. nr)
if case[1]
" condition true, "echo 42" executed
assert_match('TestCase' .. nr .. '.*'
\ .. 'if ' .. substitute(case[0], '[[~]', '\\\0', 'g') .. '.*'
\ .. '\d PUSHNR 42.*'
\ .. '\d ECHO 1.*'
\ .. '\d PUSHNR 0.*'
\ .. '\d RETURN.*'
\, instr)
else
" condition false, function just returns
assert_match('TestCase' .. nr .. '.*'
\ .. 'if ' .. substitute(case[0], '[[~]', '\\\0', 'g') .. '[ \n]*'
\ .. 'echo 42[ \n]*'
\ .. 'endif[ \n]*'
\ .. '\s*\d PUSHNR 0.*'
\ .. '\d RETURN.*'
\, instr)
endif
nr += 1
endfor
delete('Xdisassemble')
enddef
def s:Execute()
execute 'help vim9.txt'
let cmd = 'help vim9.txt'
execute cmd
let tag = 'vim9.txt'
execute 'help ' .. tag
enddef
def Test_disassemble_execute()
let res = execute('disass s:Execute')
assert_match('\<SNR>\d*_Execute.*'
\ .. "execute 'help vim9.txt'.*"
\ .. '\d PUSHS "help vim9.txt".*'
\ .. '\d EXECUTE 1.*'
\ .. "let cmd = 'help vim9.txt'.*"
\ .. '\d PUSHS "help vim9.txt".*'
\ .. '\d STORE $0.*'
\ .. 'execute cmd.*'
\ .. '\d LOAD $0.*'
\ .. '\d EXECUTE 1.*'
\ .. "let tag = 'vim9.txt'.*"
\ .. '\d PUSHS "vim9.txt".*'
\ .. '\d STORE $1.*'
\ .. "execute 'help ' .. tag.*"
\ .. '\d PUSHS "help ".*'
\ .. '\d LOAD $1.*'
\ .. '\d CONCAT.*'
\ .. '\d EXECUTE 1.*'
\ .. '\d PUSHNR 0.*'
\ .. '\d RETURN'
\, res)
enddef
def SomeStringArg(arg: string)
echo arg
enddef
def SomeAnyArg(arg: any)
echo arg
enddef
def SomeStringArgAndReturn(arg: string): string
return arg
enddef
def Test_display_func()
let res1 = execute('function SomeStringArg')
assert_match('.* def SomeStringArg(arg: string).*'
\ .. ' echo arg.*'
\ .. ' enddef'
\, res1)
let res2 = execute('function SomeAnyArg')
assert_match('.* def SomeAnyArg(arg: any).*'
\ .. ' echo arg.*'
\ .. ' enddef'
\, res2)
let res3 = execute('function SomeStringArgAndReturn')
assert_match('.* def SomeStringArgAndReturn(arg: string): string.*'
\ .. ' return arg.*'
\ .. ' enddef'
\, res3)
enddef
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|