blob: 53a748e971b993b2213f069bfd0803779ea60fd0 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
70} ectx_T;
71
72// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020073#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
Bram Moolenaar418f1df2020-08-12 21:34:49 +020075 void
76to_string_error(vartype_T vartype)
77{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020078 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020079}
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010082 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 */
84 static int
85ufunc_argcount(ufunc_T *ufunc)
86{
87 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
88}
89
90/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Set the instruction index, depending on omitted arguments, where the default
92 * values are to be computed. If all optional arguments are present, start
93 * with the function body.
94 * The expression evaluation is at the start of the instructions:
95 * 0 -> EVAL default1
96 * STORE arg[-2]
97 * 1 -> EVAL default2
98 * STORE arg[-1]
99 * 2 -> function body
100 */
101 static void
102init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
103{
104 if (ufunc->uf_def_args.ga_len == 0)
105 ectx->ec_iidx = 0;
106 else
107 {
108 int defcount = ufunc->uf_args.ga_len - argcount;
109
110 // If there is a varargs argument defcount can be negative, no defaults
111 // to evaluate then.
112 if (defcount < 0)
113 defcount = 0;
114 ectx->ec_iidx = ufunc->uf_def_arg_idx[
115 ufunc->uf_def_args.ga_len - defcount];
116 }
117}
118
119/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200120 * Create a new list from "count" items at the bottom of the stack.
121 * When "count" is zero an empty list is added to the stack.
122 */
123 static int
124exe_newlist(int count, ectx_T *ectx)
125{
126 list_T *list = list_alloc_with_items(count);
127 int idx;
128 typval_T *tv;
129
130 if (list == NULL)
131 return FAIL;
132 for (idx = 0; idx < count; ++idx)
133 list_set_item(list, idx, STACK_TV_BOT(idx - count));
134
135 if (count > 0)
136 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200137 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200138 return FAIL;
139 else
140 ++ectx->ec_stack.ga_len;
141 tv = STACK_TV_BOT(-1);
142 tv->v_type = VAR_LIST;
143 tv->vval.v_list = list;
144 ++list->lv_refcount;
145 return OK;
146}
147
148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 * Call compiled function "cdf_idx" from compiled code.
150 *
151 * Stack has:
152 * - current arguments (already there)
153 * - omitted optional argument (default values) added here
154 * - stack frame:
155 * - pointer to calling function
156 * - Index of next instruction in calling function
157 * - previous frame pointer
158 * - reserved space for local variables
159 */
160 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200161call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
165 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200166 int arg_to_add;
167 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200169 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170
171 if (dfunc->df_deleted)
172 {
173 emsg_funcname(e_func_deleted, ufunc->uf_name);
174 return FAIL;
175 }
176
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200177 if (ufunc->uf_va_name != NULL)
178 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 // Stack at time of call with 2 varargs:
181 // normal_arg
182 // optional_arg
183 // vararg_1
184 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200185 // After creating the list:
186 // normal_arg
187 // optional_arg
188 // vararg-list
189 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200190 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200191 // After creating the list
192 // normal_arg
193 // (space for optional_arg)
194 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 vararg_count = argcount - ufunc->uf_args.ga_len;
196 if (vararg_count < 0)
197 vararg_count = 0;
198 else
199 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200201 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200202
203 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 }
205
Bram Moolenaarfe270812020-04-11 22:31:27 +0200206 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 if (arg_to_add < 0)
208 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200209 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200210 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200211 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200212 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200213 return FAIL;
214 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200215 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
216 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 return FAIL;
218
Bram Moolenaarfe270812020-04-11 22:31:27 +0200219 // Move the vararg-list to below the missing optional arguments.
220 if (vararg_count > 0 && arg_to_add > 0)
221 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100222
223 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200224 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200225 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200226 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227
228 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
230 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200231 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
232 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
234 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200235 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200237 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
238 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239
240 // Set execution state to the start of the called function.
241 ectx->ec_dfunc_idx = cdf_idx;
242 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200243 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
244 if (entry != NULL)
245 {
246 // Set the script context to the script where the function was defined.
247 // TODO: save more than the SID?
248 entry->es_save_sid = current_sctx.sc_sid;
249 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
250 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100251
252 // Decide where to start execution, handles optional arguments.
253 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254
255 return OK;
256}
257
258// Get pointer to item in the stack.
259#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
260
261/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200262 * Used when returning from a function: Check if any closure is still
263 * referenced. If so then move the arguments and variables to a separate piece
264 * of stack to be used when the closure is called.
265 * When "free_arguments" is TRUE the arguments are to be freed.
266 * Returns FAIL when out of memory.
267 */
268 static int
269handle_closure_in_use(ectx_T *ectx, int free_arguments)
270{
271 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
272 + ectx->ec_dfunc_idx;
273 int argcount = ufunc_argcount(dfunc->df_ufunc);
274 int top = ectx->ec_frame_idx - argcount;
275 int idx;
276 typval_T *tv;
277 int closure_in_use = FALSE;
278
279 // Check if any created closure is still in use.
280 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
281 {
282 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
283 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200284 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
285 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200286 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200287 int refcount = tv->vval.v_partial->pt_refcount;
288 int i;
289
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200290 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200291 // unreferenced on return.
292 for (i = 0; i < dfunc->df_varcount; ++i)
293 {
294 typval_T *stv = STACK_TV(ectx->ec_frame_idx
295 + STACK_FRAME_SIZE + i);
296 if (stv->v_type == VAR_PARTIAL
297 && tv->vval.v_partial == stv->vval.v_partial)
298 --refcount;
299 }
300 if (refcount > 1)
301 {
302 closure_in_use = TRUE;
303 break;
304 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200305 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200306 }
307
308 if (closure_in_use)
309 {
310 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
311 typval_T *stack;
312
313 // A closure is using the arguments and/or local variables.
314 // Move them to the called function.
315 if (funcstack == NULL)
316 return FAIL;
317 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
318 + dfunc->df_varcount;
319 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
320 funcstack->fs_ga.ga_data = stack;
321 if (stack == NULL)
322 {
323 vim_free(funcstack);
324 return FAIL;
325 }
326
327 // Move or copy the arguments.
328 for (idx = 0; idx < argcount; ++idx)
329 {
330 tv = STACK_TV(top + idx);
331 if (free_arguments)
332 {
333 *(stack + idx) = *tv;
334 tv->v_type = VAR_UNKNOWN;
335 }
336 else
337 copy_tv(tv, stack + idx);
338 }
339 // Move the local variables.
340 for (idx = 0; idx < dfunc->df_varcount; ++idx)
341 {
342 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200343
344 // Do not copy a partial created for a local function.
345 // TODO: this won't work if the closure actually uses it. But when
346 // keeping it it gets complicated: it will create a reference cycle
347 // inside the partial, thus needs special handling for garbage
348 // collection.
349 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
350 {
351 int i;
352 typval_T *ctv;
353
354 for (i = 0; i < dfunc->df_closure_count; ++i)
355 {
356 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
357 + dfunc->df_varcount + i);
358 if (tv->vval.v_partial == ctv->vval.v_partial)
359 break;
360 }
361 if (i < dfunc->df_closure_count)
362 {
363 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
364 VAR_UNKNOWN;
365 continue;
366 }
367 }
368
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200369 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
370 tv->v_type = VAR_UNKNOWN;
371 }
372
373 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
374 {
375 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
376 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200377 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200378 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200379 partial_T *partial = tv->vval.v_partial;
380
381 if (partial->pt_refcount > 1)
382 {
383 ++funcstack->fs_refcount;
384 partial->pt_funcstack = funcstack;
385 partial->pt_ectx_stack = &funcstack->fs_ga;
386 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
387 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200388 }
389 }
390 }
391
392 return OK;
393}
394
395/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396 * Return from the current function.
397 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200398 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399func_return(ectx_T *ectx)
400{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100401 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200402 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
403 + ectx->ec_dfunc_idx;
404 int argcount = ufunc_argcount(dfunc->df_ufunc);
405 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200406 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407
408 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200409 entry = estack_pop();
410 if (entry != NULL)
411 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200413 if (handle_closure_in_use(ectx, TRUE) == FAIL)
414 return FAIL;
415
416 // Clear the arguments.
417 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
418 clear_tv(STACK_TV(idx));
419
420 // Clear local variables and temp values, but not the return value.
421 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100422 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100423 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100424
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100425 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200426 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
427 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
428 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100429 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
430 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100431
432 // Reset the stack to the position before the call, move the return value
433 // to the top of the stack.
434 idx = ectx->ec_stack.ga_len - 1;
435 ectx->ec_stack.ga_len = top + 1;
436 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200437
438 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439}
440
441#undef STACK_TV
442
443/*
444 * Prepare arguments and rettv for calling a builtin or user function.
445 */
446 static int
447call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
448{
449 int idx;
450 typval_T *tv;
451
452 // Move arguments from bottom of the stack to argvars[] and add terminator.
453 for (idx = 0; idx < argcount; ++idx)
454 argvars[idx] = *STACK_TV_BOT(idx - argcount);
455 argvars[argcount].v_type = VAR_UNKNOWN;
456
457 // Result replaces the arguments on the stack.
458 if (argcount > 0)
459 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200460 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100461 return FAIL;
462 else
463 ++ectx->ec_stack.ga_len;
464
465 // Default return value is zero.
466 tv = STACK_TV_BOT(-1);
467 tv->v_type = VAR_NUMBER;
468 tv->vval.v_number = 0;
469
470 return OK;
471}
472
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200473// Ugly global to avoid passing the execution context around through many
474// layers.
475static ectx_T *current_ectx = NULL;
476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477/*
478 * Call a builtin function by index.
479 */
480 static int
481call_bfunc(int func_idx, int argcount, ectx_T *ectx)
482{
483 typval_T argvars[MAX_FUNC_ARGS];
484 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200485 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200486 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
488 if (call_prepare(argcount, argvars, ectx) == FAIL)
489 return FAIL;
490
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200491 // Call the builtin function. Set "current_ectx" so that when it
492 // recursively invokes call_def_function() a closure context can be set.
493 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200495 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496
497 // Clear the arguments.
498 for (idx = 0; idx < argcount; ++idx)
499 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200500
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200501 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200502 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 return OK;
504}
505
506/*
507 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100508 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509 */
510 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100511call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512{
513 typval_T argvars[MAX_FUNC_ARGS];
514 funcexe_T funcexe;
515 int error;
516 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200517 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200519 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200520 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
521 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200522 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100523 {
524 // The function has been compiled, can call it quickly. For a function
525 // that was defined later: we can call it directly next time.
526 if (iptr != NULL)
527 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100528 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100529 iptr->isn_type = ISN_DCALL;
530 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
531 iptr->isn_arg.dfunc.cdf_argcount = argcount;
532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
536 if (call_prepare(argcount, argvars, ectx) == FAIL)
537 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200538 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539 funcexe.evaluate = TRUE;
540
541 // Call the user function. Result goes in last position on the stack.
542 // TODO: add selfdict if there is one
543 error = call_user_func_check(ufunc, argcount, argvars,
544 STACK_TV_BOT(-1), &funcexe, NULL);
545
546 // Clear the arguments.
547 for (idx = 0; idx < argcount; ++idx)
548 clear_tv(&argvars[idx]);
549
550 if (error != FCERR_NONE)
551 {
552 user_func_error(error, ufunc->uf_name);
553 return FAIL;
554 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200555 if (called_emsg > called_emsg_before)
556 // Error other than from calling the function itself.
557 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 return OK;
559}
560
561/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200562 * Return TRUE if an error was given or CTRL-C was pressed.
563 */
564 static int
565vim9_aborting(int prev_called_emsg)
566{
567 return called_emsg > prev_called_emsg || got_int || did_throw;
568}
569
570/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 * Execute a function by "name".
572 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100573 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 * Returns FAIL if not found without an error message.
575 */
576 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100577call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578{
579 ufunc_T *ufunc;
580
581 if (builtin_function(name, -1))
582 {
583 int func_idx = find_internal_func(name);
584
585 if (func_idx < 0)
586 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200587 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return FAIL;
589 return call_bfunc(func_idx, argcount, ectx);
590 }
591
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200592 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200593
594 if (ufunc == NULL)
595 {
596 int called_emsg_before = called_emsg;
597
598 if (script_autoload(name, TRUE))
599 // loaded a package, search for the function again
600 ufunc = find_func(name, FALSE, NULL);
601 if (vim9_aborting(called_emsg_before))
602 return FAIL; // bail out if loading the script caused an error
603 }
604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100606 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607
608 return FAIL;
609}
610
611 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200612call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200614 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200615 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 int called_emsg_before = called_emsg;
617
618 if (tv->v_type == VAR_PARTIAL)
619 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200620 partial_T *pt = tv->vval.v_partial;
621 int i;
622
623 if (pt->pt_argc > 0)
624 {
625 // Make space for arguments from the partial, shift the "argcount"
626 // arguments up.
627 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
628 return FAIL;
629 for (i = 1; i <= argcount; ++i)
630 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
631 ectx->ec_stack.ga_len += pt->pt_argc;
632 argcount += pt->pt_argc;
633
634 // copy the arguments from the partial onto the stack
635 for (i = 0; i < pt->pt_argc; ++i)
636 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638
639 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200640 {
641 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
642
643 // closure may need the function context where it was defined
644 ectx->ec_outer_stack = pt->pt_ectx_stack;
645 ectx->ec_outer_frame = pt->pt_ectx_frame;
646
647 return ret;
648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 name = pt->pt_name;
650 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200651 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200653 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654 {
655 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200656 semsg(_(e_unknownfunc),
657 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658 return FAIL;
659 }
660 return OK;
661}
662
663/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100664 * Store "tv" in variable "name".
665 * This is for s: and g: variables.
666 */
667 static void
668store_var(char_u *name, typval_T *tv)
669{
670 funccal_entry_T entry;
671
672 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200673 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100674 restore_funccal();
675}
676
Bram Moolenaard3aac292020-04-19 14:32:17 +0200677
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100678/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 * Execute a function by "name".
680 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100681 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 */
683 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100684call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685{
Bram Moolenaared677f52020-08-12 16:38:10 +0200686 int called_emsg_before = called_emsg;
687 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688
Bram Moolenaared677f52020-08-12 16:38:10 +0200689 res = call_by_name(name, argcount, ectx, iptr);
690 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200692 dictitem_T *v;
693
694 v = find_var(name, NULL, FALSE);
695 if (v == NULL)
696 {
697 semsg(_(e_unknownfunc), name);
698 return FAIL;
699 }
700 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
701 {
702 semsg(_(e_unknownfunc), name);
703 return FAIL;
704 }
705 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200707 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708}
709
710/*
711 * Call a "def" function from old Vim script.
712 * Return OK or FAIL.
713 */
714 int
715call_def_function(
716 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200717 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200719 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 typval_T *rettv) // return value
721{
722 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200723 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200724 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 typval_T *tv;
726 int idx;
727 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100728 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200729 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200730 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200731 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732
733// Get pointer to item in the stack.
734#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
735
736// Get pointer to item at the bottom of the stack, -1 is the bottom.
737#undef STACK_TV_BOT
738#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
739
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200740// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200741#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200743// Like STACK_TV_VAR but use the outer scope
744#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
745
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200746 if (ufunc->uf_def_status == UF_NOT_COMPILED
747 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200748 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200749 {
750 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200751 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200752 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200753 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200754 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200755
Bram Moolenaar09689a02020-05-09 22:50:08 +0200756 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200757 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200758 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
759 + ufunc->uf_dfunc_idx;
760 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200761 {
762 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200763 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200764 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200767 CLEAR_FIELD(ectx);
768 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
769 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
770 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
771 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
773
774 // Put arguments on the stack.
775 for (idx = 0; idx < argc; ++idx)
776 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200777 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200778 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
779 == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200780 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 copy_tv(&argv[idx], STACK_TV_BOT(0));
782 ++ectx.ec_stack.ga_len;
783 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200784
785 // Turn varargs into a list. Empty list if no args.
786 if (ufunc->uf_va_name != NULL)
787 {
788 int vararg_count = argc - ufunc->uf_args.ga_len;
789
790 if (vararg_count < 0)
791 vararg_count = 0;
792 else
793 argc -= vararg_count;
794 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200795 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200796
797 // Check the type of the list items.
798 tv = STACK_TV_BOT(-1);
799 if (ufunc->uf_va_type != NULL
800 && ufunc->uf_va_type->tt_member != &t_any
801 && tv->vval.v_list != NULL)
802 {
803 type_T *expected = ufunc->uf_va_type->tt_member;
804 listitem_T *li = tv->vval.v_list->lv_first;
805
806 for (idx = 0; idx < vararg_count; ++idx)
807 {
808 if (check_typval_type(expected, &li->li_tv) == FAIL)
809 goto failed_early;
810 li = li->li_next;
811 }
812 }
813
Bram Moolenaar23e03252020-04-12 22:22:31 +0200814 if (defcount > 0)
815 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200816 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200817 --ectx.ec_stack.ga_len;
818 }
819
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100820 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200821 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100822 if (defcount > 0)
823 for (idx = 0; idx < defcount; ++idx)
824 {
825 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
826 ++ectx.ec_stack.ga_len;
827 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200828 if (ufunc->uf_va_name != NULL)
829 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830
831 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200832 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
833 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200835 if (partial != NULL)
836 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200837 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
838 {
839 // TODO: is this always the right way?
840 ectx.ec_outer_stack = &current_ectx->ec_stack;
841 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
842 }
843 else
844 {
845 ectx.ec_outer_stack = partial->pt_ectx_stack;
846 ectx.ec_outer_frame = partial->pt_ectx_frame;
847 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200848 }
849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 // dummy frame entries
851 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
852 {
853 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
854 ++ectx.ec_stack.ga_len;
855 }
856
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200857 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200858 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200859 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
860 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200861 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200863 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200864 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200865 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200866
867 ectx.ec_instr = dfunc->df_instr;
868 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100869
Bram Moolenaara26b9702020-04-18 19:53:28 +0200870 // Commands behave like vim9script.
871 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
872
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100873 // Decide where to start execution, handles optional arguments.
874 init_instr_idx(ufunc, argc, &ectx);
875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 for (;;)
877 {
878 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100879
Bram Moolenaar270d0382020-05-15 21:42:53 +0200880 if (++breakcheck_count >= 100)
881 {
882 line_breakcheck();
883 breakcheck_count = 0;
884 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100885 if (got_int)
886 {
887 // Turn CTRL-C into an exception.
888 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100889 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100890 goto failed;
891 did_throw = TRUE;
892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893
Bram Moolenaara26b9702020-04-18 19:53:28 +0200894 if (did_emsg && msg_list != NULL && *msg_list != NULL)
895 {
896 // Turn an error message into an exception.
897 did_emsg = FALSE;
898 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
899 goto failed;
900 did_throw = TRUE;
901 *msg_list = NULL;
902 }
903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 if (did_throw && !ectx.ec_in_catch)
905 {
906 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100907 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908
909 // An exception jumps to the first catch, finally, or returns from
910 // the current function.
911 if (trystack->ga_len > 0)
912 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200913 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 {
915 // jump to ":catch" or ":finally"
916 ectx.ec_in_catch = TRUE;
917 ectx.ec_iidx = trycmd->tcd_catch_idx;
918 }
919 else
920 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200921 // Not inside try or need to return from current functions.
922 // Push a dummy return value.
923 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
924 goto failed;
925 tv = STACK_TV_BOT(0);
926 tv->v_type = VAR_NUMBER;
927 tv->vval.v_number = 0;
928 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200929 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200931 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100932 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200933 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
934 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935 goto done;
936 }
937
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200938 if (func_return(&ectx) == FAIL)
939 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 }
941 continue;
942 }
943
944 iptr = &ectx.ec_instr[ectx.ec_iidx++];
945 switch (iptr->isn_type)
946 {
947 // execute Ex command line
948 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200949 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 do_cmdline_cmd(iptr->isn_arg.string);
951 break;
952
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200953 // execute Ex command from pieces on the stack
954 case ISN_EXECCONCAT:
955 {
956 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200957 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200958 int pass;
959 int i;
960 char_u *cmd = NULL;
961 char_u *str;
962
963 for (pass = 1; pass <= 2; ++pass)
964 {
965 for (i = 0; i < count; ++i)
966 {
967 tv = STACK_TV_BOT(i - count);
968 str = tv->vval.v_string;
969 if (str != NULL && *str != NUL)
970 {
971 if (pass == 2)
972 STRCPY(cmd + len, str);
973 len += STRLEN(str);
974 }
975 if (pass == 2)
976 clear_tv(tv);
977 }
978 if (pass == 1)
979 {
980 cmd = alloc(len + 1);
981 if (cmd == NULL)
982 goto failed;
983 len = 0;
984 }
985 }
986
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200987 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200988 do_cmdline_cmd(cmd);
989 vim_free(cmd);
990 }
991 break;
992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 // execute :echo {string} ...
994 case ISN_ECHO:
995 {
996 int count = iptr->isn_arg.echo.echo_count;
997 int atstart = TRUE;
998 int needclr = TRUE;
999
1000 for (idx = 0; idx < count; ++idx)
1001 {
1002 tv = STACK_TV_BOT(idx - count);
1003 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1004 &atstart, &needclr);
1005 clear_tv(tv);
1006 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001007 if (needclr)
1008 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009 ectx.ec_stack.ga_len -= count;
1010 }
1011 break;
1012
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001013 // :execute {string} ...
1014 // :echomsg {string} ...
1015 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001016 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001017 case ISN_ECHOMSG:
1018 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001019 {
1020 int count = iptr->isn_arg.number;
1021 garray_T ga;
1022 char_u buf[NUMBUFLEN];
1023 char_u *p;
1024 int len;
1025 int failed = FALSE;
1026
1027 ga_init2(&ga, 1, 80);
1028 for (idx = 0; idx < count; ++idx)
1029 {
1030 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001031 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001032 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001033 if (tv->v_type == VAR_CHANNEL
1034 || tv->v_type == VAR_JOB)
1035 {
1036 SOURCING_LNUM = iptr->isn_lnum;
1037 emsg(_(e_inval_string));
1038 break;
1039 }
1040 else
1041 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001042 }
1043 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001044 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001045
1046 len = (int)STRLEN(p);
1047 if (ga_grow(&ga, len + 2) == FAIL)
1048 failed = TRUE;
1049 else
1050 {
1051 if (ga.ga_len > 0)
1052 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1053 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1054 ga.ga_len += len;
1055 }
1056 clear_tv(tv);
1057 }
1058 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001059 if (failed)
1060 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001061
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001062 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001063 {
1064 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001065 {
1066 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001067 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001068 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001069 else
1070 {
1071 msg_sb_eol();
1072 if (iptr->isn_type == ISN_ECHOMSG)
1073 {
1074 msg_attr(ga.ga_data, echo_attr);
1075 out_flush();
1076 }
1077 else
1078 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001079 SOURCING_LNUM = iptr->isn_lnum;
1080 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001081 }
1082 }
1083 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001084 ga_clear(&ga);
1085 }
1086 break;
1087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 // load local variable or argument
1089 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001090 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 goto failed;
1092 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1093 ++ectx.ec_stack.ga_len;
1094 break;
1095
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001096 // load variable or argument from outer scope
1097 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001098 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001099 goto failed;
1100 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1101 STACK_TV_BOT(0));
1102 ++ectx.ec_stack.ga_len;
1103 break;
1104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 // load v: variable
1106 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001107 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 goto failed;
1109 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1110 ++ectx.ec_stack.ga_len;
1111 break;
1112
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001113 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114 case ISN_LOADSCRIPT:
1115 {
1116 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001117 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 svar_T *sv;
1119
1120 sv = ((svar_T *)si->sn_var_vals.ga_data)
1121 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001122 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 goto failed;
1124 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1125 ++ectx.ec_stack.ga_len;
1126 }
1127 break;
1128
1129 // load s: variable in old script
1130 case ISN_LOADS:
1131 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001132 hashtab_T *ht = &SCRIPT_VARS(
1133 iptr->isn_arg.loadstore.ls_sid);
1134 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 if (di == NULL)
1138 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001139 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001140 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001141 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 }
1143 else
1144 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001145 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 goto failed;
1147 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1148 ++ectx.ec_stack.ga_len;
1149 }
1150 }
1151 break;
1152
Bram Moolenaard3aac292020-04-19 14:32:17 +02001153 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001155 case ISN_LOADB:
1156 case ISN_LOADW:
1157 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001159 dictitem_T *di = NULL;
1160 hashtab_T *ht = NULL;
1161 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001162
Bram Moolenaard3aac292020-04-19 14:32:17 +02001163 switch (iptr->isn_type)
1164 {
1165 case ISN_LOADG:
1166 ht = get_globvar_ht();
1167 namespace = 'g';
1168 break;
1169 case ISN_LOADB:
1170 ht = &curbuf->b_vars->dv_hashtab;
1171 namespace = 'b';
1172 break;
1173 case ISN_LOADW:
1174 ht = &curwin->w_vars->dv_hashtab;
1175 namespace = 'w';
1176 break;
1177 case ISN_LOADT:
1178 ht = &curtab->tp_vars->dv_hashtab;
1179 namespace = 't';
1180 break;
1181 default: // Cannot reach here
1182 goto failed;
1183 }
1184 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001185
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186 if (di == NULL)
1187 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001188 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001189 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001190 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001191 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 }
1193 else
1194 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001195 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 goto failed;
1197 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1198 ++ectx.ec_stack.ga_len;
1199 }
1200 }
1201 break;
1202
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001203 // load g:/b:/w:/t: namespace
1204 case ISN_LOADGDICT:
1205 case ISN_LOADBDICT:
1206 case ISN_LOADWDICT:
1207 case ISN_LOADTDICT:
1208 {
1209 dict_T *d = NULL;
1210
1211 switch (iptr->isn_type)
1212 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001213 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1214 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1215 case ISN_LOADWDICT: d = curwin->w_vars; break;
1216 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001217 default: // Cannot reach here
1218 goto failed;
1219 }
1220 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1221 goto failed;
1222 tv = STACK_TV_BOT(0);
1223 tv->v_type = VAR_DICT;
1224 tv->v_lock = 0;
1225 tv->vval.v_dict = d;
1226 ++ectx.ec_stack.ga_len;
1227 }
1228 break;
1229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 // load &option
1231 case ISN_LOADOPT:
1232 {
1233 typval_T optval;
1234 char_u *name = iptr->isn_arg.string;
1235
Bram Moolenaara8c17702020-04-01 21:17:24 +02001236 // This is not expected to fail, name is checked during
1237 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001238 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001240 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001241 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 *STACK_TV_BOT(0) = optval;
1243 ++ectx.ec_stack.ga_len;
1244 }
1245 break;
1246
1247 // load $ENV
1248 case ISN_LOADENV:
1249 {
1250 typval_T optval;
1251 char_u *name = iptr->isn_arg.string;
1252
Bram Moolenaar270d0382020-05-15 21:42:53 +02001253 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001255 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001256 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 *STACK_TV_BOT(0) = optval;
1258 ++ectx.ec_stack.ga_len;
1259 }
1260 break;
1261
1262 // load @register
1263 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001264 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 goto failed;
1266 tv = STACK_TV_BOT(0);
1267 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001268 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 tv->vval.v_string = get_reg_contents(
1270 iptr->isn_arg.number, GREG_EXPR_SRC);
1271 ++ectx.ec_stack.ga_len;
1272 break;
1273
1274 // store local variable
1275 case ISN_STORE:
1276 --ectx.ec_stack.ga_len;
1277 tv = STACK_TV_VAR(iptr->isn_arg.number);
1278 clear_tv(tv);
1279 *tv = *STACK_TV_BOT(0);
1280 break;
1281
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001282 // store variable or argument in outer scope
1283 case ISN_STOREOUTER:
1284 --ectx.ec_stack.ga_len;
1285 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1286 clear_tv(tv);
1287 *tv = *STACK_TV_BOT(0);
1288 break;
1289
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001290 // store s: variable in old script
1291 case ISN_STORES:
1292 {
1293 hashtab_T *ht = &SCRIPT_VARS(
1294 iptr->isn_arg.loadstore.ls_sid);
1295 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001296 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001297
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001298 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001299 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001300 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001301 else
1302 {
1303 clear_tv(&di->di_tv);
1304 di->di_tv = *STACK_TV_BOT(0);
1305 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001306 }
1307 break;
1308
1309 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 case ISN_STORESCRIPT:
1311 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001312 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 iptr->isn_arg.script.script_sid);
1314 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1315 + iptr->isn_arg.script.script_idx;
1316
1317 --ectx.ec_stack.ga_len;
1318 clear_tv(sv->sv_tv);
1319 *sv->sv_tv = *STACK_TV_BOT(0);
1320 }
1321 break;
1322
1323 // store option
1324 case ISN_STOREOPT:
1325 {
1326 long n = 0;
1327 char_u *s = NULL;
1328 char *msg;
1329
1330 --ectx.ec_stack.ga_len;
1331 tv = STACK_TV_BOT(0);
1332 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001333 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001335 if (s == NULL)
1336 s = (char_u *)"";
1337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001339 // must be VAR_NUMBER, CHECKTYPE makes sure
1340 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1342 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001343 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if (msg != NULL)
1345 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001346 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001348 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 }
1351 break;
1352
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001353 // store $ENV
1354 case ISN_STOREENV:
1355 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001356 tv = STACK_TV_BOT(0);
1357 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1358 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001359 break;
1360
1361 // store @r
1362 case ISN_STOREREG:
1363 {
1364 int reg = iptr->isn_arg.number;
1365
1366 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001367 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001368 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001369 tv_get_string(tv), -1, FALSE);
1370 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001371 }
1372 break;
1373
1374 // store v: variable
1375 case ISN_STOREV:
1376 --ectx.ec_stack.ga_len;
1377 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1378 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001379 // should not happen, type is checked when compiling
1380 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001381 break;
1382
Bram Moolenaard3aac292020-04-19 14:32:17 +02001383 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001385 case ISN_STOREB:
1386 case ISN_STOREW:
1387 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 {
1389 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001390 hashtab_T *ht;
1391 switch (iptr->isn_type)
1392 {
1393 case ISN_STOREG:
1394 ht = get_globvar_ht();
1395 break;
1396 case ISN_STOREB:
1397 ht = &curbuf->b_vars->dv_hashtab;
1398 break;
1399 case ISN_STOREW:
1400 ht = &curwin->w_vars->dv_hashtab;
1401 break;
1402 case ISN_STORET:
1403 ht = &curtab->tp_vars->dv_hashtab;
1404 break;
1405 default: // Cannot reach here
1406 goto failed;
1407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408
1409 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001410 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001412 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 else
1414 {
1415 clear_tv(&di->di_tv);
1416 di->di_tv = *STACK_TV_BOT(0);
1417 }
1418 }
1419 break;
1420
1421 // store number in local variable
1422 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001423 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 clear_tv(tv);
1425 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001426 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 break;
1428
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001429 // store value in list variable
1430 case ISN_STORELIST:
1431 {
1432 typval_T *tv_idx = STACK_TV_BOT(-2);
1433 varnumber_T lidx = tv_idx->vval.v_number;
1434 typval_T *tv_list = STACK_TV_BOT(-1);
1435 list_T *list = tv_list->vval.v_list;
1436
1437 if (lidx < 0 && list->lv_len + lidx >= 0)
1438 // negative index is relative to the end
1439 lidx = list->lv_len + lidx;
1440 if (lidx < 0 || lidx > list->lv_len)
1441 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001442 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001443 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001444 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001445 }
1446 tv = STACK_TV_BOT(-3);
1447 if (lidx < list->lv_len)
1448 {
1449 listitem_T *li = list_find(list, lidx);
1450
1451 // overwrite existing list item
1452 clear_tv(&li->li_tv);
1453 li->li_tv = *tv;
1454 }
1455 else
1456 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001457 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001458 if (list_append_tv(list, tv) == FAIL)
1459 goto failed;
1460 clear_tv(tv);
1461 }
1462 clear_tv(tv_idx);
1463 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001464 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001465 }
1466 break;
1467
1468 // store value in dict variable
1469 case ISN_STOREDICT:
1470 {
1471 typval_T *tv_key = STACK_TV_BOT(-2);
1472 char_u *key = tv_key->vval.v_string;
1473 typval_T *tv_dict = STACK_TV_BOT(-1);
1474 dict_T *dict = tv_dict->vval.v_dict;
1475 dictitem_T *di;
1476
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001477 if (dict == NULL)
1478 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001479 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001480 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001481 goto on_error;
1482 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001483 if (key == NULL)
1484 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001485 tv = STACK_TV_BOT(-3);
1486 di = dict_find(dict, key, -1);
1487 if (di != NULL)
1488 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001489 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001490 clear_tv(&di->di_tv);
1491 di->di_tv = *tv;
1492 }
1493 else
1494 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001495 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001496 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1497 goto failed;
1498 clear_tv(tv);
1499 }
1500 clear_tv(tv_key);
1501 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001502 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001503 }
1504 break;
1505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 // push constant
1507 case ISN_PUSHNR:
1508 case ISN_PUSHBOOL:
1509 case ISN_PUSHSPEC:
1510 case ISN_PUSHF:
1511 case ISN_PUSHS:
1512 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001513 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001514 case ISN_PUSHCHANNEL:
1515 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001516 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 goto failed;
1518 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001519 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 ++ectx.ec_stack.ga_len;
1521 switch (iptr->isn_type)
1522 {
1523 case ISN_PUSHNR:
1524 tv->v_type = VAR_NUMBER;
1525 tv->vval.v_number = iptr->isn_arg.number;
1526 break;
1527 case ISN_PUSHBOOL:
1528 tv->v_type = VAR_BOOL;
1529 tv->vval.v_number = iptr->isn_arg.number;
1530 break;
1531 case ISN_PUSHSPEC:
1532 tv->v_type = VAR_SPECIAL;
1533 tv->vval.v_number = iptr->isn_arg.number;
1534 break;
1535#ifdef FEAT_FLOAT
1536 case ISN_PUSHF:
1537 tv->v_type = VAR_FLOAT;
1538 tv->vval.v_float = iptr->isn_arg.fnumber;
1539 break;
1540#endif
1541 case ISN_PUSHBLOB:
1542 blob_copy(iptr->isn_arg.blob, tv);
1543 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001544 case ISN_PUSHFUNC:
1545 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001546 if (iptr->isn_arg.string == NULL)
1547 tv->vval.v_string = NULL;
1548 else
1549 tv->vval.v_string =
1550 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001551 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001552 case ISN_PUSHCHANNEL:
1553#ifdef FEAT_JOB_CHANNEL
1554 tv->v_type = VAR_CHANNEL;
1555 tv->vval.v_channel = iptr->isn_arg.channel;
1556 if (tv->vval.v_channel != NULL)
1557 ++tv->vval.v_channel->ch_refcount;
1558#endif
1559 break;
1560 case ISN_PUSHJOB:
1561#ifdef FEAT_JOB_CHANNEL
1562 tv->v_type = VAR_JOB;
1563 tv->vval.v_job = iptr->isn_arg.job;
1564 if (tv->vval.v_job != NULL)
1565 ++tv->vval.v_job->jv_refcount;
1566#endif
1567 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 default:
1569 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001570 tv->vval.v_string = vim_strsave(
1571 iptr->isn_arg.string == NULL
1572 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 }
1574 break;
1575
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001576 case ISN_UNLET:
1577 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1578 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001579 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001580 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001581 case ISN_UNLETENV:
1582 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1583 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 // create a list from items on the stack; uses a single allocation
1586 // for the list header and the items
1587 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001588 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1589 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 break;
1591
1592 // create a dict from items on the stack
1593 case ISN_NEWDICT:
1594 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001595 int count = iptr->isn_arg.number;
1596 dict_T *dict = dict_alloc();
1597 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598
1599 if (dict == NULL)
1600 goto failed;
1601 for (idx = 0; idx < count; ++idx)
1602 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001603 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001605 // check key is unique
1606 item = dict_find(dict, tv->vval.v_string, -1);
1607 if (item != NULL)
1608 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001609 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001610 semsg(_(e_duplicate_key), tv->vval.v_string);
1611 dict_unref(dict);
1612 goto on_error;
1613 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 item = dictitem_alloc(tv->vval.v_string);
1615 clear_tv(tv);
1616 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001617 {
1618 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001620 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1622 item->di_tv.v_lock = 0;
1623 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001624 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001625 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001626 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 }
1630
1631 if (count > 0)
1632 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001633 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 goto failed;
1635 else
1636 ++ectx.ec_stack.ga_len;
1637 tv = STACK_TV_BOT(-1);
1638 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001639 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 tv->vval.v_dict = dict;
1641 ++dict->dv_refcount;
1642 }
1643 break;
1644
1645 // call a :def function
1646 case ISN_DCALL:
1647 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1648 iptr->isn_arg.dfunc.cdf_argcount,
1649 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001650 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651 break;
1652
1653 // call a builtin function
1654 case ISN_BCALL:
1655 SOURCING_LNUM = iptr->isn_lnum;
1656 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1657 iptr->isn_arg.bfunc.cbf_argcount,
1658 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001659 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 break;
1661
1662 // call a funcref or partial
1663 case ISN_PCALL:
1664 {
1665 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1666 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001667 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668
1669 SOURCING_LNUM = iptr->isn_lnum;
1670 if (pfunc->cpf_top)
1671 {
1672 // funcref is above the arguments
1673 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1674 }
1675 else
1676 {
1677 // Get the funcref from the stack.
1678 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001679 partial_tv = *STACK_TV_BOT(0);
1680 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 }
1682 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001683 if (tv == &partial_tv)
1684 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001686 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 }
1688 break;
1689
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001690 case ISN_PCALL_END:
1691 // PCALL finished, arguments have been consumed and replaced by
1692 // the return value. Now clear the funcref from the stack,
1693 // and move the return value in its place.
1694 --ectx.ec_stack.ga_len;
1695 clear_tv(STACK_TV_BOT(-1));
1696 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1697 break;
1698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 // call a user defined function or funcref/partial
1700 case ISN_UCALL:
1701 {
1702 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1703
1704 SOURCING_LNUM = iptr->isn_lnum;
1705 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001706 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001707 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 }
1709 break;
1710
1711 // return from a :def function call
1712 case ISN_RETURN:
1713 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001714 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001715 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001716
1717 if (trystack->ga_len > 0)
1718 trycmd = ((trycmd_T *)trystack->ga_data)
1719 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001720 if (trycmd != NULL
1721 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 && trycmd->tcd_finally_idx != 0)
1723 {
1724 // jump to ":finally"
1725 ectx.ec_iidx = trycmd->tcd_finally_idx;
1726 trycmd->tcd_return = TRUE;
1727 }
1728 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001729 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 }
1731 break;
1732
1733 // push a function reference to a compiled function
1734 case ISN_FUNCREF:
1735 {
1736 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001737 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738
1739 pt = ALLOC_CLEAR_ONE(partial_T);
1740 if (pt == NULL)
1741 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001742 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001743 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001744 vim_free(pt);
1745 goto failed;
1746 }
1747 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1748 + iptr->isn_arg.funcref.fr_func;
1749 pt->pt_func = pt_dfunc->df_ufunc;
1750 pt->pt_refcount = 1;
1751 ++pt_dfunc->df_ufunc->uf_refcount;
1752
1753 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1754 {
1755 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1756 + ectx.ec_dfunc_idx;
1757
1758 // The closure needs to find arguments and local
1759 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001760 pt->pt_ectx_stack = &ectx.ec_stack;
1761 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001762
1763 // If this function returns and the closure is still
1764 // used, we need to make a copy of the context
1765 // (arguments and local variables). Store a reference
1766 // to the partial so we can handle that.
1767 ++pt->pt_refcount;
1768 tv = STACK_TV_VAR(dfunc->df_varcount
1769 + iptr->isn_arg.funcref.fr_var_idx);
1770 if (tv->v_type == VAR_PARTIAL)
1771 {
1772 // TODO: use a garray_T on ectx.
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001773 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001774 emsg("Multiple closures not supported yet");
1775 goto failed;
1776 }
1777 tv->v_type = VAR_PARTIAL;
1778 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001779 }
1780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 tv = STACK_TV_BOT(0);
1782 ++ectx.ec_stack.ga_len;
1783 tv->vval.v_partial = pt;
1784 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001785 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 }
1787 break;
1788
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001789 // Create a global function from a lambda.
1790 case ISN_NEWFUNC:
1791 {
1792 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1793
1794 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1795 }
1796 break;
1797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 // jump if a condition is met
1799 case ISN_JUMP:
1800 {
1801 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1802 int jump = TRUE;
1803
1804 if (when != JUMP_ALWAYS)
1805 {
1806 tv = STACK_TV_BOT(-1);
1807 jump = tv2bool(tv);
1808 if (when == JUMP_IF_FALSE
1809 || when == JUMP_AND_KEEP_IF_FALSE)
1810 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001811 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 {
1813 // drop the value from the stack
1814 clear_tv(tv);
1815 --ectx.ec_stack.ga_len;
1816 }
1817 }
1818 if (jump)
1819 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1820 }
1821 break;
1822
1823 // top of a for loop
1824 case ISN_FOR:
1825 {
1826 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1827 typval_T *idxtv =
1828 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1829
1830 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001831 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 goto failed;
1833 if (++idxtv->vval.v_number >= list->lv_len)
1834 // past the end of the list, jump to "endfor"
1835 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1836 else if (list->lv_first == &range_list_item)
1837 {
1838 // non-materialized range() list
1839 tv = STACK_TV_BOT(0);
1840 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001841 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 tv->vval.v_number = list_find_nr(
1843 list, idxtv->vval.v_number, NULL);
1844 ++ectx.ec_stack.ga_len;
1845 }
1846 else
1847 {
1848 listitem_T *li = list_find(list, idxtv->vval.v_number);
1849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1851 ++ectx.ec_stack.ga_len;
1852 }
1853 }
1854 break;
1855
1856 // start of ":try" block
1857 case ISN_TRY:
1858 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001859 trycmd_T *trycmd = NULL;
1860
Bram Moolenaar270d0382020-05-15 21:42:53 +02001861 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 goto failed;
1863 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1864 + ectx.ec_trystack.ga_len;
1865 ++ectx.ec_trystack.ga_len;
1866 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001867 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1869 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001870 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 }
1872 break;
1873
1874 case ISN_PUSHEXC:
1875 if (current_exception == NULL)
1876 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001877 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 iemsg("Evaluating catch while current_exception is NULL");
1879 goto failed;
1880 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001881 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 goto failed;
1883 tv = STACK_TV_BOT(0);
1884 ++ectx.ec_stack.ga_len;
1885 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001886 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 tv->vval.v_string = vim_strsave(
1888 (char_u *)current_exception->value);
1889 break;
1890
1891 case ISN_CATCH:
1892 {
1893 garray_T *trystack = &ectx.ec_trystack;
1894
1895 if (trystack->ga_len > 0)
1896 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001897 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 + trystack->ga_len - 1;
1899 trycmd->tcd_caught = TRUE;
1900 }
1901 did_emsg = got_int = did_throw = FALSE;
1902 catch_exception(current_exception);
1903 }
1904 break;
1905
1906 // end of ":try" block
1907 case ISN_ENDTRY:
1908 {
1909 garray_T *trystack = &ectx.ec_trystack;
1910
1911 if (trystack->ga_len > 0)
1912 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001913 trycmd_T *trycmd = NULL;
1914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 --trystack->ga_len;
1916 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001917 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 trycmd = ((trycmd_T *)trystack->ga_data)
1919 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001920 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 {
1922 // discard the exception
1923 if (caught_stack == current_exception)
1924 caught_stack = caught_stack->caught;
1925 discard_current_exception();
1926 }
1927
1928 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001929 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 }
1931 }
1932 break;
1933
1934 case ISN_THROW:
1935 --ectx.ec_stack.ga_len;
1936 tv = STACK_TV_BOT(0);
1937 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1938 {
1939 vim_free(tv->vval.v_string);
1940 goto failed;
1941 }
1942 did_throw = TRUE;
1943 break;
1944
1945 // compare with special values
1946 case ISN_COMPAREBOOL:
1947 case ISN_COMPARESPECIAL:
1948 {
1949 typval_T *tv1 = STACK_TV_BOT(-2);
1950 typval_T *tv2 = STACK_TV_BOT(-1);
1951 varnumber_T arg1 = tv1->vval.v_number;
1952 varnumber_T arg2 = tv2->vval.v_number;
1953 int res;
1954
1955 switch (iptr->isn_arg.op.op_type)
1956 {
1957 case EXPR_EQUAL: res = arg1 == arg2; break;
1958 case EXPR_NEQUAL: res = arg1 != arg2; break;
1959 default: res = 0; break;
1960 }
1961
1962 --ectx.ec_stack.ga_len;
1963 tv1->v_type = VAR_BOOL;
1964 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1965 }
1966 break;
1967
1968 // Operation with two number arguments
1969 case ISN_OPNR:
1970 case ISN_COMPARENR:
1971 {
1972 typval_T *tv1 = STACK_TV_BOT(-2);
1973 typval_T *tv2 = STACK_TV_BOT(-1);
1974 varnumber_T arg1 = tv1->vval.v_number;
1975 varnumber_T arg2 = tv2->vval.v_number;
1976 varnumber_T res;
1977
1978 switch (iptr->isn_arg.op.op_type)
1979 {
1980 case EXPR_MULT: res = arg1 * arg2; break;
1981 case EXPR_DIV: res = arg1 / arg2; break;
1982 case EXPR_REM: res = arg1 % arg2; break;
1983 case EXPR_SUB: res = arg1 - arg2; break;
1984 case EXPR_ADD: res = arg1 + arg2; break;
1985
1986 case EXPR_EQUAL: res = arg1 == arg2; break;
1987 case EXPR_NEQUAL: res = arg1 != arg2; break;
1988 case EXPR_GREATER: res = arg1 > arg2; break;
1989 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1990 case EXPR_SMALLER: res = arg1 < arg2; break;
1991 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1992 default: res = 0; break;
1993 }
1994
1995 --ectx.ec_stack.ga_len;
1996 if (iptr->isn_type == ISN_COMPARENR)
1997 {
1998 tv1->v_type = VAR_BOOL;
1999 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2000 }
2001 else
2002 tv1->vval.v_number = res;
2003 }
2004 break;
2005
2006 // Computation with two float arguments
2007 case ISN_OPFLOAT:
2008 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002009#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 {
2011 typval_T *tv1 = STACK_TV_BOT(-2);
2012 typval_T *tv2 = STACK_TV_BOT(-1);
2013 float_T arg1 = tv1->vval.v_float;
2014 float_T arg2 = tv2->vval.v_float;
2015 float_T res = 0;
2016 int cmp = FALSE;
2017
2018 switch (iptr->isn_arg.op.op_type)
2019 {
2020 case EXPR_MULT: res = arg1 * arg2; break;
2021 case EXPR_DIV: res = arg1 / arg2; break;
2022 case EXPR_SUB: res = arg1 - arg2; break;
2023 case EXPR_ADD: res = arg1 + arg2; break;
2024
2025 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2026 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2027 case EXPR_GREATER: cmp = arg1 > arg2; break;
2028 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2029 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2030 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2031 default: cmp = 0; break;
2032 }
2033 --ectx.ec_stack.ga_len;
2034 if (iptr->isn_type == ISN_COMPAREFLOAT)
2035 {
2036 tv1->v_type = VAR_BOOL;
2037 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2038 }
2039 else
2040 tv1->vval.v_float = res;
2041 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002042#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 break;
2044
2045 case ISN_COMPARELIST:
2046 {
2047 typval_T *tv1 = STACK_TV_BOT(-2);
2048 typval_T *tv2 = STACK_TV_BOT(-1);
2049 list_T *arg1 = tv1->vval.v_list;
2050 list_T *arg2 = tv2->vval.v_list;
2051 int cmp = FALSE;
2052 int ic = iptr->isn_arg.op.op_ic;
2053
2054 switch (iptr->isn_arg.op.op_type)
2055 {
2056 case EXPR_EQUAL: cmp =
2057 list_equal(arg1, arg2, ic, FALSE); break;
2058 case EXPR_NEQUAL: cmp =
2059 !list_equal(arg1, arg2, ic, FALSE); break;
2060 case EXPR_IS: cmp = arg1 == arg2; break;
2061 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2062 default: cmp = 0; break;
2063 }
2064 --ectx.ec_stack.ga_len;
2065 clear_tv(tv1);
2066 clear_tv(tv2);
2067 tv1->v_type = VAR_BOOL;
2068 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2069 }
2070 break;
2071
2072 case ISN_COMPAREBLOB:
2073 {
2074 typval_T *tv1 = STACK_TV_BOT(-2);
2075 typval_T *tv2 = STACK_TV_BOT(-1);
2076 blob_T *arg1 = tv1->vval.v_blob;
2077 blob_T *arg2 = tv2->vval.v_blob;
2078 int cmp = FALSE;
2079
2080 switch (iptr->isn_arg.op.op_type)
2081 {
2082 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2083 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2084 case EXPR_IS: cmp = arg1 == arg2; break;
2085 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2086 default: cmp = 0; break;
2087 }
2088 --ectx.ec_stack.ga_len;
2089 clear_tv(tv1);
2090 clear_tv(tv2);
2091 tv1->v_type = VAR_BOOL;
2092 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2093 }
2094 break;
2095
2096 // TODO: handle separately
2097 case ISN_COMPARESTRING:
2098 case ISN_COMPAREDICT:
2099 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 case ISN_COMPAREANY:
2101 {
2102 typval_T *tv1 = STACK_TV_BOT(-2);
2103 typval_T *tv2 = STACK_TV_BOT(-1);
2104 exptype_T exptype = iptr->isn_arg.op.op_type;
2105 int ic = iptr->isn_arg.op.op_ic;
2106
2107 typval_compare(tv1, tv2, exptype, ic);
2108 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 --ectx.ec_stack.ga_len;
2110 }
2111 break;
2112
2113 case ISN_ADDLIST:
2114 case ISN_ADDBLOB:
2115 {
2116 typval_T *tv1 = STACK_TV_BOT(-2);
2117 typval_T *tv2 = STACK_TV_BOT(-1);
2118
2119 if (iptr->isn_type == ISN_ADDLIST)
2120 eval_addlist(tv1, tv2);
2121 else
2122 eval_addblob(tv1, tv2);
2123 clear_tv(tv2);
2124 --ectx.ec_stack.ga_len;
2125 }
2126 break;
2127
2128 // Computation with two arguments of unknown type
2129 case ISN_OPANY:
2130 {
2131 typval_T *tv1 = STACK_TV_BOT(-2);
2132 typval_T *tv2 = STACK_TV_BOT(-1);
2133 varnumber_T n1, n2;
2134#ifdef FEAT_FLOAT
2135 float_T f1 = 0, f2 = 0;
2136#endif
2137 int error = FALSE;
2138
2139 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2140 {
2141 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2142 {
2143 eval_addlist(tv1, tv2);
2144 clear_tv(tv2);
2145 --ectx.ec_stack.ga_len;
2146 break;
2147 }
2148 else if (tv1->v_type == VAR_BLOB
2149 && tv2->v_type == VAR_BLOB)
2150 {
2151 eval_addblob(tv1, tv2);
2152 clear_tv(tv2);
2153 --ectx.ec_stack.ga_len;
2154 break;
2155 }
2156 }
2157#ifdef FEAT_FLOAT
2158 if (tv1->v_type == VAR_FLOAT)
2159 {
2160 f1 = tv1->vval.v_float;
2161 n1 = 0;
2162 }
2163 else
2164#endif
2165 {
2166 n1 = tv_get_number_chk(tv1, &error);
2167 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002168 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169#ifdef FEAT_FLOAT
2170 if (tv2->v_type == VAR_FLOAT)
2171 f1 = n1;
2172#endif
2173 }
2174#ifdef FEAT_FLOAT
2175 if (tv2->v_type == VAR_FLOAT)
2176 {
2177 f2 = tv2->vval.v_float;
2178 n2 = 0;
2179 }
2180 else
2181#endif
2182 {
2183 n2 = tv_get_number_chk(tv2, &error);
2184 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002185 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186#ifdef FEAT_FLOAT
2187 if (tv1->v_type == VAR_FLOAT)
2188 f2 = n2;
2189#endif
2190 }
2191#ifdef FEAT_FLOAT
2192 // if there is a float on either side the result is a float
2193 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2194 {
2195 switch (iptr->isn_arg.op.op_type)
2196 {
2197 case EXPR_MULT: f1 = f1 * f2; break;
2198 case EXPR_DIV: f1 = f1 / f2; break;
2199 case EXPR_SUB: f1 = f1 - f2; break;
2200 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002201 default: SOURCING_LNUM = iptr->isn_lnum;
2202 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002203 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 }
2205 clear_tv(tv1);
2206 clear_tv(tv2);
2207 tv1->v_type = VAR_FLOAT;
2208 tv1->vval.v_float = f1;
2209 --ectx.ec_stack.ga_len;
2210 }
2211 else
2212#endif
2213 {
2214 switch (iptr->isn_arg.op.op_type)
2215 {
2216 case EXPR_MULT: n1 = n1 * n2; break;
2217 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2218 case EXPR_SUB: n1 = n1 - n2; break;
2219 case EXPR_ADD: n1 = n1 + n2; break;
2220 default: n1 = num_modulus(n1, n2); break;
2221 }
2222 clear_tv(tv1);
2223 clear_tv(tv2);
2224 tv1->v_type = VAR_NUMBER;
2225 tv1->vval.v_number = n1;
2226 --ectx.ec_stack.ga_len;
2227 }
2228 }
2229 break;
2230
2231 case ISN_CONCAT:
2232 {
2233 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2234 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2235 char_u *res;
2236
2237 res = concat_str(str1, str2);
2238 clear_tv(STACK_TV_BOT(-2));
2239 clear_tv(STACK_TV_BOT(-1));
2240 --ectx.ec_stack.ga_len;
2241 STACK_TV_BOT(-1)->vval.v_string = res;
2242 }
2243 break;
2244
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002245 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002246 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002247 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002248 int is_slice = iptr->isn_type == ISN_STRSLICE;
2249 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002250 char_u *res;
2251
2252 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002253 // string slice: string is at stack-3, first index at
2254 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002255 if (is_slice)
2256 {
2257 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002258 n1 = tv->vval.v_number;
2259 }
2260
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002261 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002262 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002263
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002264 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002265 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002266 if (is_slice)
2267 // Slice: Select the characters from the string
2268 res = string_slice(tv->vval.v_string, n1, n2);
2269 else
2270 // Index: The resulting variable is a string of a
2271 // single character. If the index is too big or
2272 // negative the result is empty.
2273 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002274 vim_free(tv->vval.v_string);
2275 tv->vval.v_string = res;
2276 }
2277 break;
2278
2279 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002280 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 {
Bram Moolenaared591872020-08-15 22:14:53 +02002282 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002284 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285
2286 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002287 // list slice: list is at stack-3, indexes at stack-2 and
2288 // stack-1
2289 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 list = tv->vval.v_list;
2291
2292 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002293 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002295
2296 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 {
Bram Moolenaared591872020-08-15 22:14:53 +02002298 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002299 n1 = tv->vval.v_number;
2300 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 }
Bram Moolenaared591872020-08-15 22:14:53 +02002302
2303 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002304 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002305 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002306 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2307 == FAIL)
2308 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 }
2310 break;
2311
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002312 case ISN_ANYINDEX:
2313 case ISN_ANYSLICE:
2314 {
2315 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2316 typval_T *var1, *var2;
2317 int res;
2318
2319 // index: composite is at stack-2, index at stack-1
2320 // slice: composite is at stack-3, indexes at stack-2 and
2321 // stack-1
2322 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002323 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002324 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2325 goto on_error;
2326 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2327 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2328 res = eval_index_inner(tv, is_slice,
2329 var1, var2, NULL, -1, TRUE);
2330 clear_tv(var1);
2331 if (is_slice)
2332 clear_tv(var2);
2333 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2334 if (res == FAIL)
2335 goto on_error;
2336 }
2337 break;
2338
Bram Moolenaar9af78762020-06-16 11:34:42 +02002339 case ISN_SLICE:
2340 {
2341 list_T *list;
2342 int count = iptr->isn_arg.number;
2343
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002344 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002345 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002346 list = tv->vval.v_list;
2347
2348 // no error for short list, expect it to be checked earlier
2349 if (list != NULL && list->lv_len >= count)
2350 {
2351 list_T *newlist = list_slice(list,
2352 count, list->lv_len - 1);
2353
2354 if (newlist != NULL)
2355 {
2356 list_unref(list);
2357 tv->vval.v_list = newlist;
2358 ++newlist->lv_refcount;
2359 }
2360 }
2361 }
2362 break;
2363
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002364 case ISN_GETITEM:
2365 {
2366 listitem_T *li;
2367 int index = iptr->isn_arg.number;
2368
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002369 // Get list item: list is at stack-1, push item.
2370 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002371 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002372 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002373
2374 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2375 goto failed;
2376 ++ectx.ec_stack.ga_len;
2377 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2378 }
2379 break;
2380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 case ISN_MEMBER:
2382 {
2383 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002384 char_u *key;
2385 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002386 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002387
2388 // dict member: dict is at stack-2, key at stack-1
2389 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002390 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002391 dict = tv->vval.v_dict;
2392
2393 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002394 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002395 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002396
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002397 if ((di = dict_find(dict, key, -1)) == NULL)
2398 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002399 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002400 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002401 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002402 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002403 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002404 --ectx.ec_stack.ga_len;
2405 // Clear the dict after getting the item, to avoid that it
2406 // make the item invalid.
2407 tv = STACK_TV_BOT(-1);
2408 temp_tv = *tv;
2409 copy_tv(&di->di_tv, tv);
2410 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002411 }
2412 break;
2413
2414 // dict member with string key
2415 case ISN_STRINGMEMBER:
2416 {
2417 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002419 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420
2421 tv = STACK_TV_BOT(-1);
2422 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2423 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002424 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002426 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 }
2428 dict = tv->vval.v_dict;
2429
2430 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2431 == NULL)
2432 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002433 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002435 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002437 // Clear the dict after getting the item, to avoid that it
2438 // make the item invalid.
2439 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002441 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 }
2443 break;
2444
2445 case ISN_NEGATENR:
2446 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002447 if (tv->v_type != VAR_NUMBER
2448#ifdef FEAT_FLOAT
2449 && tv->v_type != VAR_FLOAT
2450#endif
2451 )
2452 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002453 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002454 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002455 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002456 }
2457#ifdef FEAT_FLOAT
2458 if (tv->v_type == VAR_FLOAT)
2459 tv->vval.v_float = -tv->vval.v_float;
2460 else
2461#endif
2462 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 break;
2464
2465 case ISN_CHECKNR:
2466 {
2467 int error = FALSE;
2468
2469 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002470 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002472 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 (void)tv_get_number_chk(tv, &error);
2474 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002475 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 }
2477 break;
2478
2479 case ISN_CHECKTYPE:
2480 {
2481 checktype_T *ct = &iptr->isn_arg.type;
2482
2483 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002484 // TODO: better type comparison
2485 if (tv->v_type != ct->ct_type
2486 && !((tv->v_type == VAR_PARTIAL
2487 && ct->ct_type == VAR_FUNC)
2488 || (tv->v_type == VAR_FUNC
2489 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002491 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002492 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 vartype_name(ct->ct_type),
2494 vartype_name(tv->v_type));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002495 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 }
2497 }
2498 break;
2499
Bram Moolenaar9af78762020-06-16 11:34:42 +02002500 case ISN_CHECKLEN:
2501 {
2502 int min_len = iptr->isn_arg.checklen.cl_min_len;
2503 list_T *list = NULL;
2504
2505 tv = STACK_TV_BOT(-1);
2506 if (tv->v_type == VAR_LIST)
2507 list = tv->vval.v_list;
2508 if (list == NULL || list->lv_len < min_len
2509 || (list->lv_len > min_len
2510 && !iptr->isn_arg.checklen.cl_more_OK))
2511 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002512 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002513 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002514 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002515 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002516 }
2517 }
2518 break;
2519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 case ISN_2BOOL:
2521 {
2522 int n;
2523
2524 tv = STACK_TV_BOT(-1);
2525 n = tv2bool(tv);
2526 if (iptr->isn_arg.number) // invert
2527 n = !n;
2528 clear_tv(tv);
2529 tv->v_type = VAR_BOOL;
2530 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2531 }
2532 break;
2533
2534 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002535 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 {
2537 char_u *str;
2538
2539 tv = STACK_TV_BOT(iptr->isn_arg.number);
2540 if (tv->v_type != VAR_STRING)
2541 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002542 if (iptr->isn_type == ISN_2STRING_ANY)
2543 {
2544 switch (tv->v_type)
2545 {
2546 case VAR_SPECIAL:
2547 case VAR_BOOL:
2548 case VAR_NUMBER:
2549 case VAR_FLOAT:
2550 case VAR_BLOB: break;
2551 default: to_string_error(tv->v_type);
2552 goto on_error;
2553 }
2554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 str = typval_tostring(tv);
2556 clear_tv(tv);
2557 tv->v_type = VAR_STRING;
2558 tv->vval.v_string = str;
2559 }
2560 }
2561 break;
2562
Bram Moolenaar389df252020-07-09 21:20:47 +02002563 case ISN_SHUFFLE:
2564 {
2565 typval_T tmp_tv;
2566 int item = iptr->isn_arg.shuffle.shfl_item;
2567 int up = iptr->isn_arg.shuffle.shfl_up;
2568
2569 tmp_tv = *STACK_TV_BOT(-item);
2570 for ( ; up > 0 && item > 1; --up)
2571 {
2572 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2573 --item;
2574 }
2575 *STACK_TV_BOT(-item) = tmp_tv;
2576 }
2577 break;
2578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 case ISN_DROP:
2580 --ectx.ec_stack.ga_len;
2581 clear_tv(STACK_TV_BOT(0));
2582 break;
2583 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002584 continue;
2585
Bram Moolenaard032f342020-07-18 18:13:02 +02002586func_return:
2587 // Restore previous function. If the frame pointer is zero then there
2588 // is none and we are done.
2589 if (ectx.ec_frame_idx == initial_frame_idx)
2590 {
2591 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2592 // only fails when out of memory
2593 goto failed;
2594 goto done;
2595 }
2596 if (func_return(&ectx) == FAIL)
2597 // only fails when out of memory
2598 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002599 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002600
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002601on_error:
2602 if (trylevel == 0)
2603 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 }
2605
2606done:
2607 // function finished, get result from the stack.
2608 tv = STACK_TV_BOT(-1);
2609 *rettv = *tv;
2610 tv->v_type = VAR_UNKNOWN;
2611 ret = OK;
2612
2613failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002614 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002615 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002616 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002617failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002618 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002619
2620 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2622 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002625 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002626
2627 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002628 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002629 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 return ret;
2631}
2632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633/*
2634 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002635 * We don't really need this at runtime, but we do have tests that require it,
2636 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 */
2638 void
2639ex_disassemble(exarg_T *eap)
2640{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002641 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002642 char_u *fname;
2643 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 dfunc_T *dfunc;
2645 isn_T *instr;
2646 int current;
2647 int line_idx = 0;
2648 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002649 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002651 if (STRNCMP(arg, "<lambda>", 8) == 0)
2652 {
2653 arg += 8;
2654 (void)getdigits(&arg);
2655 fname = vim_strnsave(eap->arg, arg - eap->arg);
2656 }
2657 else
2658 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002659 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002660 if (fname == NULL)
2661 {
2662 semsg(_(e_invarg2), eap->arg);
2663 return;
2664 }
2665
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002666 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002667 if (ufunc == NULL)
2668 {
2669 char_u *p = untrans_function_name(fname);
2670
2671 if (p != NULL)
2672 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002673 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002674 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002675 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 if (ufunc == NULL)
2677 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002678 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 return;
2680 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002681 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002682 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2683 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002684 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002686 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 return;
2688 }
2689 if (ufunc->uf_name_exp != NULL)
2690 msg((char *)ufunc->uf_name_exp);
2691 else
2692 msg((char *)ufunc->uf_name);
2693
2694 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2695 instr = dfunc->df_instr;
2696 for (current = 0; current < dfunc->df_instr_count; ++current)
2697 {
2698 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002699 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700
2701 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2702 {
2703 if (current > prev_current)
2704 {
2705 msg_puts("\n\n");
2706 prev_current = current;
2707 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002708 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2709 if (line != NULL)
2710 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 }
2712
2713 switch (iptr->isn_type)
2714 {
2715 case ISN_EXEC:
2716 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2717 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002718 case ISN_EXECCONCAT:
2719 smsg("%4d EXECCONCAT %lld", current,
2720 (long long)iptr->isn_arg.number);
2721 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 case ISN_ECHO:
2723 {
2724 echo_T *echo = &iptr->isn_arg.echo;
2725
2726 smsg("%4d %s %d", current,
2727 echo->echo_with_white ? "ECHO" : "ECHON",
2728 echo->echo_count);
2729 }
2730 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002731 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002732 smsg("%4d EXECUTE %lld", current,
2733 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002734 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002735 case ISN_ECHOMSG:
2736 smsg("%4d ECHOMSG %lld", current,
2737 (long long)(iptr->isn_arg.number));
2738 break;
2739 case ISN_ECHOERR:
2740 smsg("%4d ECHOERR %lld", current,
2741 (long long)(iptr->isn_arg.number));
2742 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002744 case ISN_LOADOUTER:
2745 {
2746 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2747
2748 if (iptr->isn_arg.number < 0)
2749 smsg("%4d LOAD%s arg[%lld]", current, add,
2750 (long long)(iptr->isn_arg.number
2751 + STACK_FRAME_SIZE));
2752 else
2753 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002754 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 break;
2757 case ISN_LOADV:
2758 smsg("%4d LOADV v:%s", current,
2759 get_vim_var_name(iptr->isn_arg.number));
2760 break;
2761 case ISN_LOADSCRIPT:
2762 {
2763 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002764 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2766 + iptr->isn_arg.script.script_idx;
2767
2768 smsg("%4d LOADSCRIPT %s from %s", current,
2769 sv->sv_name, si->sn_name);
2770 }
2771 break;
2772 case ISN_LOADS:
2773 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002774 scriptitem_T *si = SCRIPT_ITEM(
2775 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776
2777 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002778 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779 }
2780 break;
2781 case ISN_LOADG:
2782 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2783 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002784 case ISN_LOADB:
2785 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2786 break;
2787 case ISN_LOADW:
2788 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2789 break;
2790 case ISN_LOADT:
2791 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2792 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002793 case ISN_LOADGDICT:
2794 smsg("%4d LOAD g:", current);
2795 break;
2796 case ISN_LOADBDICT:
2797 smsg("%4d LOAD b:", current);
2798 break;
2799 case ISN_LOADWDICT:
2800 smsg("%4d LOAD w:", current);
2801 break;
2802 case ISN_LOADTDICT:
2803 smsg("%4d LOAD t:", current);
2804 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 case ISN_LOADOPT:
2806 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2807 break;
2808 case ISN_LOADENV:
2809 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2810 break;
2811 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002812 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 break;
2814
2815 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002816 case ISN_STOREOUTER:
2817 {
2818 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2819
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002820 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002821 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002822 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002823 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002824 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002825 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002826 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002828 case ISN_STOREV:
2829 smsg("%4d STOREV v:%s", current,
2830 get_vim_var_name(iptr->isn_arg.number));
2831 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002833 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2834 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002835 case ISN_STOREB:
2836 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2837 break;
2838 case ISN_STOREW:
2839 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2840 break;
2841 case ISN_STORET:
2842 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2843 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002844 case ISN_STORES:
2845 {
2846 scriptitem_T *si = SCRIPT_ITEM(
2847 iptr->isn_arg.loadstore.ls_sid);
2848
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002849 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002850 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002851 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 break;
2853 case ISN_STORESCRIPT:
2854 {
2855 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002856 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2858 + iptr->isn_arg.script.script_idx;
2859
2860 smsg("%4d STORESCRIPT %s in %s", current,
2861 sv->sv_name, si->sn_name);
2862 }
2863 break;
2864 case ISN_STOREOPT:
2865 smsg("%4d STOREOPT &%s", current,
2866 iptr->isn_arg.storeopt.so_name);
2867 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002868 case ISN_STOREENV:
2869 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2870 break;
2871 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002872 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002873 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 case ISN_STORENR:
2875 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002876 iptr->isn_arg.storenr.stnr_val,
2877 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 break;
2879
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002880 case ISN_STORELIST:
2881 smsg("%4d STORELIST", current);
2882 break;
2883
2884 case ISN_STOREDICT:
2885 smsg("%4d STOREDICT", current);
2886 break;
2887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 // constants
2889 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002890 smsg("%4d PUSHNR %lld", current,
2891 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 break;
2893 case ISN_PUSHBOOL:
2894 case ISN_PUSHSPEC:
2895 smsg("%4d PUSH %s", current,
2896 get_var_special_name(iptr->isn_arg.number));
2897 break;
2898 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002899#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002901#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 break;
2903 case ISN_PUSHS:
2904 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2905 break;
2906 case ISN_PUSHBLOB:
2907 {
2908 char_u *r;
2909 char_u numbuf[NUMBUFLEN];
2910 char_u *tofree;
2911
2912 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002913 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 vim_free(tofree);
2915 }
2916 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002917 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002918 {
2919 char *name = (char *)iptr->isn_arg.string;
2920
2921 smsg("%4d PUSHFUNC \"%s\"", current,
2922 name == NULL ? "[none]" : name);
2923 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002924 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002925 case ISN_PUSHCHANNEL:
2926#ifdef FEAT_JOB_CHANNEL
2927 {
2928 channel_T *channel = iptr->isn_arg.channel;
2929
2930 smsg("%4d PUSHCHANNEL %d", current,
2931 channel == NULL ? 0 : channel->ch_id);
2932 }
2933#endif
2934 break;
2935 case ISN_PUSHJOB:
2936#ifdef FEAT_JOB_CHANNEL
2937 {
2938 typval_T tv;
2939 char_u *name;
2940
2941 tv.v_type = VAR_JOB;
2942 tv.vval.v_job = iptr->isn_arg.job;
2943 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002944 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002945 }
2946#endif
2947 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 case ISN_PUSHEXC:
2949 smsg("%4d PUSH v:exception", current);
2950 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002951 case ISN_UNLET:
2952 smsg("%4d UNLET%s %s", current,
2953 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2954 iptr->isn_arg.unlet.ul_name);
2955 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002956 case ISN_UNLETENV:
2957 smsg("%4d UNLETENV%s $%s", current,
2958 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2959 iptr->isn_arg.unlet.ul_name);
2960 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002962 smsg("%4d NEWLIST size %lld", current,
2963 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 break;
2965 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002966 smsg("%4d NEWDICT size %lld", current,
2967 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 break;
2969
2970 // function call
2971 case ISN_BCALL:
2972 {
2973 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2974
2975 smsg("%4d BCALL %s(argc %d)", current,
2976 internal_func_name(cbfunc->cbf_idx),
2977 cbfunc->cbf_argcount);
2978 }
2979 break;
2980 case ISN_DCALL:
2981 {
2982 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2983 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2984 + cdfunc->cdf_idx;
2985
2986 smsg("%4d DCALL %s(argc %d)", current,
2987 df->df_ufunc->uf_name_exp != NULL
2988 ? df->df_ufunc->uf_name_exp
2989 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2990 }
2991 break;
2992 case ISN_UCALL:
2993 {
2994 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2995
2996 smsg("%4d UCALL %s(argc %d)", current,
2997 cufunc->cuf_name, cufunc->cuf_argcount);
2998 }
2999 break;
3000 case ISN_PCALL:
3001 {
3002 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3003
3004 smsg("%4d PCALL%s (argc %d)", current,
3005 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3006 }
3007 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003008 case ISN_PCALL_END:
3009 smsg("%4d PCALL end", current);
3010 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 case ISN_RETURN:
3012 smsg("%4d RETURN", current);
3013 break;
3014 case ISN_FUNCREF:
3015 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003016 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003018 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003020 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003021 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 }
3023 break;
3024
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003025 case ISN_NEWFUNC:
3026 {
3027 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3028
3029 smsg("%4d NEWFUNC %s %s", current,
3030 newfunc->nf_lambda, newfunc->nf_global);
3031 }
3032 break;
3033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 case ISN_JUMP:
3035 {
3036 char *when = "?";
3037
3038 switch (iptr->isn_arg.jump.jump_when)
3039 {
3040 case JUMP_ALWAYS:
3041 when = "JUMP";
3042 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 case JUMP_AND_KEEP_IF_TRUE:
3044 when = "JUMP_AND_KEEP_IF_TRUE";
3045 break;
3046 case JUMP_IF_FALSE:
3047 when = "JUMP_IF_FALSE";
3048 break;
3049 case JUMP_AND_KEEP_IF_FALSE:
3050 when = "JUMP_AND_KEEP_IF_FALSE";
3051 break;
3052 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003053 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 iptr->isn_arg.jump.jump_where);
3055 }
3056 break;
3057
3058 case ISN_FOR:
3059 {
3060 forloop_T *forloop = &iptr->isn_arg.forloop;
3061
3062 smsg("%4d FOR $%d -> %d", current,
3063 forloop->for_idx, forloop->for_end);
3064 }
3065 break;
3066
3067 case ISN_TRY:
3068 {
3069 try_T *try = &iptr->isn_arg.try;
3070
3071 smsg("%4d TRY catch -> %d, finally -> %d", current,
3072 try->try_catch, try->try_finally);
3073 }
3074 break;
3075 case ISN_CATCH:
3076 // TODO
3077 smsg("%4d CATCH", current);
3078 break;
3079 case ISN_ENDTRY:
3080 smsg("%4d ENDTRY", current);
3081 break;
3082 case ISN_THROW:
3083 smsg("%4d THROW", current);
3084 break;
3085
3086 // expression operations on number
3087 case ISN_OPNR:
3088 case ISN_OPFLOAT:
3089 case ISN_OPANY:
3090 {
3091 char *what;
3092 char *ins;
3093
3094 switch (iptr->isn_arg.op.op_type)
3095 {
3096 case EXPR_MULT: what = "*"; break;
3097 case EXPR_DIV: what = "/"; break;
3098 case EXPR_REM: what = "%"; break;
3099 case EXPR_SUB: what = "-"; break;
3100 case EXPR_ADD: what = "+"; break;
3101 default: what = "???"; break;
3102 }
3103 switch (iptr->isn_type)
3104 {
3105 case ISN_OPNR: ins = "OPNR"; break;
3106 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3107 case ISN_OPANY: ins = "OPANY"; break;
3108 default: ins = "???"; break;
3109 }
3110 smsg("%4d %s %s", current, ins, what);
3111 }
3112 break;
3113
3114 case ISN_COMPAREBOOL:
3115 case ISN_COMPARESPECIAL:
3116 case ISN_COMPARENR:
3117 case ISN_COMPAREFLOAT:
3118 case ISN_COMPARESTRING:
3119 case ISN_COMPAREBLOB:
3120 case ISN_COMPARELIST:
3121 case ISN_COMPAREDICT:
3122 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 case ISN_COMPAREANY:
3124 {
3125 char *p;
3126 char buf[10];
3127 char *type;
3128
3129 switch (iptr->isn_arg.op.op_type)
3130 {
3131 case EXPR_EQUAL: p = "=="; break;
3132 case EXPR_NEQUAL: p = "!="; break;
3133 case EXPR_GREATER: p = ">"; break;
3134 case EXPR_GEQUAL: p = ">="; break;
3135 case EXPR_SMALLER: p = "<"; break;
3136 case EXPR_SEQUAL: p = "<="; break;
3137 case EXPR_MATCH: p = "=~"; break;
3138 case EXPR_IS: p = "is"; break;
3139 case EXPR_ISNOT: p = "isnot"; break;
3140 case EXPR_NOMATCH: p = "!~"; break;
3141 default: p = "???"; break;
3142 }
3143 STRCPY(buf, p);
3144 if (iptr->isn_arg.op.op_ic == TRUE)
3145 strcat(buf, "?");
3146 switch(iptr->isn_type)
3147 {
3148 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3149 case ISN_COMPARESPECIAL:
3150 type = "COMPARESPECIAL"; break;
3151 case ISN_COMPARENR: type = "COMPARENR"; break;
3152 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3153 case ISN_COMPARESTRING:
3154 type = "COMPARESTRING"; break;
3155 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3156 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3157 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3158 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3160 default: type = "???"; break;
3161 }
3162
3163 smsg("%4d %s %s", current, type, buf);
3164 }
3165 break;
3166
3167 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3168 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3169
3170 // expression operations
3171 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003172 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003173 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003174 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003175 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003176 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3177 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003178 case ISN_SLICE: smsg("%4d SLICE %lld",
3179 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003180 case ISN_GETITEM: smsg("%4d ITEM %lld",
3181 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003182 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3183 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 iptr->isn_arg.string); break;
3185 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3186
3187 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3188 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3189 vartype_name(iptr->isn_arg.type.ct_type),
3190 iptr->isn_arg.type.ct_off);
3191 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003192 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3193 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3194 iptr->isn_arg.checklen.cl_min_len);
3195 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 case ISN_2BOOL: if (iptr->isn_arg.number)
3197 smsg("%4d INVERT (!val)", current);
3198 else
3199 smsg("%4d 2BOOL (!!val)", current);
3200 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003201 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3202 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003203 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003204 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3205 (long long)(iptr->isn_arg.number));
3206 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207
Bram Moolenaar389df252020-07-09 21:20:47 +02003208 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3209 iptr->isn_arg.shuffle.shfl_item,
3210 iptr->isn_arg.shuffle.shfl_up);
3211 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 case ISN_DROP: smsg("%4d DROP", current); break;
3213 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003214
3215 out_flush(); // output one line at a time
3216 ui_breakcheck();
3217 if (got_int)
3218 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220}
3221
3222/*
3223 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3224 * list, etc. Mostly like what JavaScript does, except that empty list and
3225 * empty dictionary are FALSE.
3226 */
3227 int
3228tv2bool(typval_T *tv)
3229{
3230 switch (tv->v_type)
3231 {
3232 case VAR_NUMBER:
3233 return tv->vval.v_number != 0;
3234 case VAR_FLOAT:
3235#ifdef FEAT_FLOAT
3236 return tv->vval.v_float != 0.0;
3237#else
3238 break;
3239#endif
3240 case VAR_PARTIAL:
3241 return tv->vval.v_partial != NULL;
3242 case VAR_FUNC:
3243 case VAR_STRING:
3244 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3245 case VAR_LIST:
3246 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3247 case VAR_DICT:
3248 return tv->vval.v_dict != NULL
3249 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3250 case VAR_BOOL:
3251 case VAR_SPECIAL:
3252 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3253 case VAR_JOB:
3254#ifdef FEAT_JOB_CHANNEL
3255 return tv->vval.v_job != NULL;
3256#else
3257 break;
3258#endif
3259 case VAR_CHANNEL:
3260#ifdef FEAT_JOB_CHANNEL
3261 return tv->vval.v_channel != NULL;
3262#else
3263 break;
3264#endif
3265 case VAR_BLOB:
3266 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3267 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003268 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 case VAR_VOID:
3270 break;
3271 }
3272 return FALSE;
3273}
3274
3275/*
3276 * If "tv" is a string give an error and return FAIL.
3277 */
3278 int
3279check_not_string(typval_T *tv)
3280{
3281 if (tv->v_type == VAR_STRING)
3282 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003283 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 clear_tv(tv);
3285 return FAIL;
3286 }
3287 return OK;
3288}
3289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290
3291#endif // FEAT_EVAL