blob: 3345758032e67a5d91a854947c9fbfb335ac57b3 [file] [log] [blame]
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001" Test the :disassemble command, and compilation as a side effect
2
3func NotCompiled()
4 echo "not"
5endfunc
6
7let s:scriptvar = 4
8let g:globalvar = 'g'
9
10def 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
21enddef
22
23def 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)
39enddef
40
41def 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
48enddef
49
50def 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
66enddef
67
68def 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'
79enddef
80
81def 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)
101enddef
102
103def s:ScriptFuncTry()
104 try
105 echo 'yes'
106 catch /fail/
107 echo 'no'
108 finally
109 echo 'end'
110 endtry
111enddef
112
113def 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)
130enddef
131
132def s:ScriptFuncNew()
133 let ll = [1, "two", 333]
134 let dd = #{one: 1, two: "val"}
135enddef
136
137def 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)
152enddef
153
154def FuncWithArg(arg)
155 echo arg
156enddef
157
158func UserFunc()
159 echo 'nothing'
160endfunc
161
162func UserFuncWithArg(arg)
163 echo a:arg
164endfunc
165
166def 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"
180enddef
181
182def 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)
217enddef
218
219
220" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker