blob: 05cf5da6fbda82ddfed43dbf8048fda2e813bc4f [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 Moolenaaree8580e2020-08-28 17:19:07 +0200729 sctx_T save_current_sctx = current_sctx;
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 Moolenaaree8580e2020-08-28 17:19:07 +0200870 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200871 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200872 estack_push_ufunc(ufunc, 1);
873 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200874 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
875
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100876 // Decide where to start execution, handles optional arguments.
877 init_instr_idx(ufunc, argc, &ectx);
878
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 for (;;)
880 {
881 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100882
Bram Moolenaar270d0382020-05-15 21:42:53 +0200883 if (++breakcheck_count >= 100)
884 {
885 line_breakcheck();
886 breakcheck_count = 0;
887 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100888 if (got_int)
889 {
890 // Turn CTRL-C into an exception.
891 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100892 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100893 goto failed;
894 did_throw = TRUE;
895 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896
Bram Moolenaara26b9702020-04-18 19:53:28 +0200897 if (did_emsg && msg_list != NULL && *msg_list != NULL)
898 {
899 // Turn an error message into an exception.
900 did_emsg = FALSE;
901 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
902 goto failed;
903 did_throw = TRUE;
904 *msg_list = NULL;
905 }
906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 if (did_throw && !ectx.ec_in_catch)
908 {
909 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100910 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911
912 // An exception jumps to the first catch, finally, or returns from
913 // the current function.
914 if (trystack->ga_len > 0)
915 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200916 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 {
918 // jump to ":catch" or ":finally"
919 ectx.ec_in_catch = TRUE;
920 ectx.ec_iidx = trycmd->tcd_catch_idx;
921 }
922 else
923 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200924 // Not inside try or need to return from current functions.
925 // Push a dummy return value.
926 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
927 goto failed;
928 tv = STACK_TV_BOT(0);
929 tv->v_type = VAR_NUMBER;
930 tv->vval.v_number = 0;
931 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200932 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200934 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100935 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200936 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
937 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 goto done;
939 }
940
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200941 if (func_return(&ectx) == FAIL)
942 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943 }
944 continue;
945 }
946
947 iptr = &ectx.ec_instr[ectx.ec_iidx++];
948 switch (iptr->isn_type)
949 {
950 // execute Ex command line
951 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200952 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 do_cmdline_cmd(iptr->isn_arg.string);
954 break;
955
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200956 // execute Ex command from pieces on the stack
957 case ISN_EXECCONCAT:
958 {
959 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200960 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200961 int pass;
962 int i;
963 char_u *cmd = NULL;
964 char_u *str;
965
966 for (pass = 1; pass <= 2; ++pass)
967 {
968 for (i = 0; i < count; ++i)
969 {
970 tv = STACK_TV_BOT(i - count);
971 str = tv->vval.v_string;
972 if (str != NULL && *str != NUL)
973 {
974 if (pass == 2)
975 STRCPY(cmd + len, str);
976 len += STRLEN(str);
977 }
978 if (pass == 2)
979 clear_tv(tv);
980 }
981 if (pass == 1)
982 {
983 cmd = alloc(len + 1);
984 if (cmd == NULL)
985 goto failed;
986 len = 0;
987 }
988 }
989
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200990 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200991 do_cmdline_cmd(cmd);
992 vim_free(cmd);
993 }
994 break;
995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 // execute :echo {string} ...
997 case ISN_ECHO:
998 {
999 int count = iptr->isn_arg.echo.echo_count;
1000 int atstart = TRUE;
1001 int needclr = TRUE;
1002
1003 for (idx = 0; idx < count; ++idx)
1004 {
1005 tv = STACK_TV_BOT(idx - count);
1006 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1007 &atstart, &needclr);
1008 clear_tv(tv);
1009 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001010 if (needclr)
1011 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 ectx.ec_stack.ga_len -= count;
1013 }
1014 break;
1015
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001016 // :execute {string} ...
1017 // :echomsg {string} ...
1018 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001019 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001020 case ISN_ECHOMSG:
1021 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001022 {
1023 int count = iptr->isn_arg.number;
1024 garray_T ga;
1025 char_u buf[NUMBUFLEN];
1026 char_u *p;
1027 int len;
1028 int failed = FALSE;
1029
1030 ga_init2(&ga, 1, 80);
1031 for (idx = 0; idx < count; ++idx)
1032 {
1033 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001034 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001035 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001036 if (tv->v_type == VAR_CHANNEL
1037 || tv->v_type == VAR_JOB)
1038 {
1039 SOURCING_LNUM = iptr->isn_lnum;
1040 emsg(_(e_inval_string));
1041 break;
1042 }
1043 else
1044 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001045 }
1046 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001047 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001048
1049 len = (int)STRLEN(p);
1050 if (ga_grow(&ga, len + 2) == FAIL)
1051 failed = TRUE;
1052 else
1053 {
1054 if (ga.ga_len > 0)
1055 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1056 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1057 ga.ga_len += len;
1058 }
1059 clear_tv(tv);
1060 }
1061 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001062 if (failed)
1063 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001064
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001065 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001066 {
1067 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001068 {
1069 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001070 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001071 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001072 else
1073 {
1074 msg_sb_eol();
1075 if (iptr->isn_type == ISN_ECHOMSG)
1076 {
1077 msg_attr(ga.ga_data, echo_attr);
1078 out_flush();
1079 }
1080 else
1081 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001082 SOURCING_LNUM = iptr->isn_lnum;
1083 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001084 }
1085 }
1086 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001087 ga_clear(&ga);
1088 }
1089 break;
1090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 // load local variable or argument
1092 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001093 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 goto failed;
1095 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1096 ++ectx.ec_stack.ga_len;
1097 break;
1098
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001099 // load variable or argument from outer scope
1100 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001101 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001102 goto failed;
1103 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1104 STACK_TV_BOT(0));
1105 ++ectx.ec_stack.ga_len;
1106 break;
1107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 // load v: variable
1109 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001110 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 goto failed;
1112 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1113 ++ectx.ec_stack.ga_len;
1114 break;
1115
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001116 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 case ISN_LOADSCRIPT:
1118 {
1119 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001120 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 svar_T *sv;
1122
1123 sv = ((svar_T *)si->sn_var_vals.ga_data)
1124 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001125 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 goto failed;
1127 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1128 ++ectx.ec_stack.ga_len;
1129 }
1130 break;
1131
1132 // load s: variable in old script
1133 case ISN_LOADS:
1134 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001135 hashtab_T *ht = &SCRIPT_VARS(
1136 iptr->isn_arg.loadstore.ls_sid);
1137 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 if (di == NULL)
1141 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001142 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001143 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001144 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 }
1146 else
1147 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001148 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 goto failed;
1150 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1151 ++ectx.ec_stack.ga_len;
1152 }
1153 }
1154 break;
1155
Bram Moolenaard3aac292020-04-19 14:32:17 +02001156 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001158 case ISN_LOADB:
1159 case ISN_LOADW:
1160 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001162 dictitem_T *di = NULL;
1163 hashtab_T *ht = NULL;
1164 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001165
Bram Moolenaard3aac292020-04-19 14:32:17 +02001166 switch (iptr->isn_type)
1167 {
1168 case ISN_LOADG:
1169 ht = get_globvar_ht();
1170 namespace = 'g';
1171 break;
1172 case ISN_LOADB:
1173 ht = &curbuf->b_vars->dv_hashtab;
1174 namespace = 'b';
1175 break;
1176 case ISN_LOADW:
1177 ht = &curwin->w_vars->dv_hashtab;
1178 namespace = 'w';
1179 break;
1180 case ISN_LOADT:
1181 ht = &curtab->tp_vars->dv_hashtab;
1182 namespace = 't';
1183 break;
1184 default: // Cannot reach here
1185 goto failed;
1186 }
1187 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 if (di == NULL)
1190 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001191 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001192 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001193 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001194 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 }
1196 else
1197 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001198 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 goto failed;
1200 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1201 ++ectx.ec_stack.ga_len;
1202 }
1203 }
1204 break;
1205
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001206 // load g:/b:/w:/t: namespace
1207 case ISN_LOADGDICT:
1208 case ISN_LOADBDICT:
1209 case ISN_LOADWDICT:
1210 case ISN_LOADTDICT:
1211 {
1212 dict_T *d = NULL;
1213
1214 switch (iptr->isn_type)
1215 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001216 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1217 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1218 case ISN_LOADWDICT: d = curwin->w_vars; break;
1219 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001220 default: // Cannot reach here
1221 goto failed;
1222 }
1223 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1224 goto failed;
1225 tv = STACK_TV_BOT(0);
1226 tv->v_type = VAR_DICT;
1227 tv->v_lock = 0;
1228 tv->vval.v_dict = d;
1229 ++ectx.ec_stack.ga_len;
1230 }
1231 break;
1232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 // load &option
1234 case ISN_LOADOPT:
1235 {
1236 typval_T optval;
1237 char_u *name = iptr->isn_arg.string;
1238
Bram Moolenaara8c17702020-04-01 21:17:24 +02001239 // This is not expected to fail, name is checked during
1240 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001241 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001243 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001244 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 *STACK_TV_BOT(0) = optval;
1246 ++ectx.ec_stack.ga_len;
1247 }
1248 break;
1249
1250 // load $ENV
1251 case ISN_LOADENV:
1252 {
1253 typval_T optval;
1254 char_u *name = iptr->isn_arg.string;
1255
Bram Moolenaar270d0382020-05-15 21:42:53 +02001256 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001258 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001259 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 *STACK_TV_BOT(0) = optval;
1261 ++ectx.ec_stack.ga_len;
1262 }
1263 break;
1264
1265 // load @register
1266 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001267 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 goto failed;
1269 tv = STACK_TV_BOT(0);
1270 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001271 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 tv->vval.v_string = get_reg_contents(
1273 iptr->isn_arg.number, GREG_EXPR_SRC);
1274 ++ectx.ec_stack.ga_len;
1275 break;
1276
1277 // store local variable
1278 case ISN_STORE:
1279 --ectx.ec_stack.ga_len;
1280 tv = STACK_TV_VAR(iptr->isn_arg.number);
1281 clear_tv(tv);
1282 *tv = *STACK_TV_BOT(0);
1283 break;
1284
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001285 // store variable or argument in outer scope
1286 case ISN_STOREOUTER:
1287 --ectx.ec_stack.ga_len;
1288 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1289 clear_tv(tv);
1290 *tv = *STACK_TV_BOT(0);
1291 break;
1292
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001293 // store s: variable in old script
1294 case ISN_STORES:
1295 {
1296 hashtab_T *ht = &SCRIPT_VARS(
1297 iptr->isn_arg.loadstore.ls_sid);
1298 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001299 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001300
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001301 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001302 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001303 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001304 else
1305 {
1306 clear_tv(&di->di_tv);
1307 di->di_tv = *STACK_TV_BOT(0);
1308 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001309 }
1310 break;
1311
1312 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 case ISN_STORESCRIPT:
1314 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001315 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 iptr->isn_arg.script.script_sid);
1317 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1318 + iptr->isn_arg.script.script_idx;
1319
1320 --ectx.ec_stack.ga_len;
1321 clear_tv(sv->sv_tv);
1322 *sv->sv_tv = *STACK_TV_BOT(0);
1323 }
1324 break;
1325
1326 // store option
1327 case ISN_STOREOPT:
1328 {
1329 long n = 0;
1330 char_u *s = NULL;
1331 char *msg;
1332
1333 --ectx.ec_stack.ga_len;
1334 tv = STACK_TV_BOT(0);
1335 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001336 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001338 if (s == NULL)
1339 s = (char_u *)"";
1340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001342 // must be VAR_NUMBER, CHECKTYPE makes sure
1343 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1345 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001346 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 if (msg != NULL)
1348 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001349 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001351 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 }
1354 break;
1355
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001356 // store $ENV
1357 case ISN_STOREENV:
1358 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001359 tv = STACK_TV_BOT(0);
1360 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1361 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001362 break;
1363
1364 // store @r
1365 case ISN_STOREREG:
1366 {
1367 int reg = iptr->isn_arg.number;
1368
1369 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001370 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001371 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001372 tv_get_string(tv), -1, FALSE);
1373 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001374 }
1375 break;
1376
1377 // store v: variable
1378 case ISN_STOREV:
1379 --ectx.ec_stack.ga_len;
1380 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1381 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001382 // should not happen, type is checked when compiling
1383 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001384 break;
1385
Bram Moolenaard3aac292020-04-19 14:32:17 +02001386 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001388 case ISN_STOREB:
1389 case ISN_STOREW:
1390 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 {
1392 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001393 hashtab_T *ht;
1394 switch (iptr->isn_type)
1395 {
1396 case ISN_STOREG:
1397 ht = get_globvar_ht();
1398 break;
1399 case ISN_STOREB:
1400 ht = &curbuf->b_vars->dv_hashtab;
1401 break;
1402 case ISN_STOREW:
1403 ht = &curwin->w_vars->dv_hashtab;
1404 break;
1405 case ISN_STORET:
1406 ht = &curtab->tp_vars->dv_hashtab;
1407 break;
1408 default: // Cannot reach here
1409 goto failed;
1410 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411
1412 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001413 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001415 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 else
1417 {
1418 clear_tv(&di->di_tv);
1419 di->di_tv = *STACK_TV_BOT(0);
1420 }
1421 }
1422 break;
1423
1424 // store number in local variable
1425 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001426 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 clear_tv(tv);
1428 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001429 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 break;
1431
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001432 // store value in list variable
1433 case ISN_STORELIST:
1434 {
1435 typval_T *tv_idx = STACK_TV_BOT(-2);
1436 varnumber_T lidx = tv_idx->vval.v_number;
1437 typval_T *tv_list = STACK_TV_BOT(-1);
1438 list_T *list = tv_list->vval.v_list;
1439
1440 if (lidx < 0 && list->lv_len + lidx >= 0)
1441 // negative index is relative to the end
1442 lidx = list->lv_len + lidx;
1443 if (lidx < 0 || lidx > list->lv_len)
1444 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001445 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001446 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001447 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001448 }
1449 tv = STACK_TV_BOT(-3);
1450 if (lidx < list->lv_len)
1451 {
1452 listitem_T *li = list_find(list, lidx);
1453
1454 // overwrite existing list item
1455 clear_tv(&li->li_tv);
1456 li->li_tv = *tv;
1457 }
1458 else
1459 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001460 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001461 if (list_append_tv(list, tv) == FAIL)
1462 goto failed;
1463 clear_tv(tv);
1464 }
1465 clear_tv(tv_idx);
1466 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001467 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001468 }
1469 break;
1470
1471 // store value in dict variable
1472 case ISN_STOREDICT:
1473 {
1474 typval_T *tv_key = STACK_TV_BOT(-2);
1475 char_u *key = tv_key->vval.v_string;
1476 typval_T *tv_dict = STACK_TV_BOT(-1);
1477 dict_T *dict = tv_dict->vval.v_dict;
1478 dictitem_T *di;
1479
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001480 if (dict == NULL)
1481 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001482 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001483 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001484 goto on_error;
1485 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001486 if (key == NULL)
1487 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001488 tv = STACK_TV_BOT(-3);
1489 di = dict_find(dict, key, -1);
1490 if (di != NULL)
1491 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001492 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001493 clear_tv(&di->di_tv);
1494 di->di_tv = *tv;
1495 }
1496 else
1497 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001498 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001499 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1500 goto failed;
1501 clear_tv(tv);
1502 }
1503 clear_tv(tv_key);
1504 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001505 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001506 }
1507 break;
1508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 // push constant
1510 case ISN_PUSHNR:
1511 case ISN_PUSHBOOL:
1512 case ISN_PUSHSPEC:
1513 case ISN_PUSHF:
1514 case ISN_PUSHS:
1515 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001516 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001517 case ISN_PUSHCHANNEL:
1518 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001519 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 goto failed;
1521 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001522 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 ++ectx.ec_stack.ga_len;
1524 switch (iptr->isn_type)
1525 {
1526 case ISN_PUSHNR:
1527 tv->v_type = VAR_NUMBER;
1528 tv->vval.v_number = iptr->isn_arg.number;
1529 break;
1530 case ISN_PUSHBOOL:
1531 tv->v_type = VAR_BOOL;
1532 tv->vval.v_number = iptr->isn_arg.number;
1533 break;
1534 case ISN_PUSHSPEC:
1535 tv->v_type = VAR_SPECIAL;
1536 tv->vval.v_number = iptr->isn_arg.number;
1537 break;
1538#ifdef FEAT_FLOAT
1539 case ISN_PUSHF:
1540 tv->v_type = VAR_FLOAT;
1541 tv->vval.v_float = iptr->isn_arg.fnumber;
1542 break;
1543#endif
1544 case ISN_PUSHBLOB:
1545 blob_copy(iptr->isn_arg.blob, tv);
1546 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001547 case ISN_PUSHFUNC:
1548 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001549 if (iptr->isn_arg.string == NULL)
1550 tv->vval.v_string = NULL;
1551 else
1552 tv->vval.v_string =
1553 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001554 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001555 case ISN_PUSHCHANNEL:
1556#ifdef FEAT_JOB_CHANNEL
1557 tv->v_type = VAR_CHANNEL;
1558 tv->vval.v_channel = iptr->isn_arg.channel;
1559 if (tv->vval.v_channel != NULL)
1560 ++tv->vval.v_channel->ch_refcount;
1561#endif
1562 break;
1563 case ISN_PUSHJOB:
1564#ifdef FEAT_JOB_CHANNEL
1565 tv->v_type = VAR_JOB;
1566 tv->vval.v_job = iptr->isn_arg.job;
1567 if (tv->vval.v_job != NULL)
1568 ++tv->vval.v_job->jv_refcount;
1569#endif
1570 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 default:
1572 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001573 tv->vval.v_string = vim_strsave(
1574 iptr->isn_arg.string == NULL
1575 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 }
1577 break;
1578
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001579 case ISN_UNLET:
1580 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1581 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001582 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001583 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001584 case ISN_UNLETENV:
1585 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1586 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 // create a list from items on the stack; uses a single allocation
1589 // for the list header and the items
1590 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001591 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1592 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 break;
1594
1595 // create a dict from items on the stack
1596 case ISN_NEWDICT:
1597 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001598 int count = iptr->isn_arg.number;
1599 dict_T *dict = dict_alloc();
1600 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601
1602 if (dict == NULL)
1603 goto failed;
1604 for (idx = 0; idx < count; ++idx)
1605 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001606 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001608 // check key is unique
1609 item = dict_find(dict, tv->vval.v_string, -1);
1610 if (item != NULL)
1611 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001612 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001613 semsg(_(e_duplicate_key), tv->vval.v_string);
1614 dict_unref(dict);
1615 goto on_error;
1616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 item = dictitem_alloc(tv->vval.v_string);
1618 clear_tv(tv);
1619 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001620 {
1621 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1625 item->di_tv.v_lock = 0;
1626 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001627 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001628 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001629 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 }
1633
1634 if (count > 0)
1635 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001636 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 goto failed;
1638 else
1639 ++ectx.ec_stack.ga_len;
1640 tv = STACK_TV_BOT(-1);
1641 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001642 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643 tv->vval.v_dict = dict;
1644 ++dict->dv_refcount;
1645 }
1646 break;
1647
1648 // call a :def function
1649 case ISN_DCALL:
1650 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1651 iptr->isn_arg.dfunc.cdf_argcount,
1652 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001653 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 break;
1655
1656 // call a builtin function
1657 case ISN_BCALL:
1658 SOURCING_LNUM = iptr->isn_lnum;
1659 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1660 iptr->isn_arg.bfunc.cbf_argcount,
1661 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001662 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 break;
1664
1665 // call a funcref or partial
1666 case ISN_PCALL:
1667 {
1668 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1669 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001670 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671
1672 SOURCING_LNUM = iptr->isn_lnum;
1673 if (pfunc->cpf_top)
1674 {
1675 // funcref is above the arguments
1676 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1677 }
1678 else
1679 {
1680 // Get the funcref from the stack.
1681 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001682 partial_tv = *STACK_TV_BOT(0);
1683 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 }
1685 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001686 if (tv == &partial_tv)
1687 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001689 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 }
1691 break;
1692
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001693 case ISN_PCALL_END:
1694 // PCALL finished, arguments have been consumed and replaced by
1695 // the return value. Now clear the funcref from the stack,
1696 // and move the return value in its place.
1697 --ectx.ec_stack.ga_len;
1698 clear_tv(STACK_TV_BOT(-1));
1699 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1700 break;
1701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 // call a user defined function or funcref/partial
1703 case ISN_UCALL:
1704 {
1705 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1706
1707 SOURCING_LNUM = iptr->isn_lnum;
1708 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001709 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001710 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 }
1712 break;
1713
1714 // return from a :def function call
1715 case ISN_RETURN:
1716 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001717 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001718 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001719
1720 if (trystack->ga_len > 0)
1721 trycmd = ((trycmd_T *)trystack->ga_data)
1722 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001723 if (trycmd != NULL
1724 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 && trycmd->tcd_finally_idx != 0)
1726 {
1727 // jump to ":finally"
1728 ectx.ec_iidx = trycmd->tcd_finally_idx;
1729 trycmd->tcd_return = TRUE;
1730 }
1731 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001732 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 }
1734 break;
1735
1736 // push a function reference to a compiled function
1737 case ISN_FUNCREF:
1738 {
1739 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001740 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741
1742 pt = ALLOC_CLEAR_ONE(partial_T);
1743 if (pt == NULL)
1744 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001745 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001746 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001747 vim_free(pt);
1748 goto failed;
1749 }
1750 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1751 + iptr->isn_arg.funcref.fr_func;
1752 pt->pt_func = pt_dfunc->df_ufunc;
1753 pt->pt_refcount = 1;
1754 ++pt_dfunc->df_ufunc->uf_refcount;
1755
1756 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1757 {
1758 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1759 + ectx.ec_dfunc_idx;
1760
1761 // The closure needs to find arguments and local
1762 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001763 pt->pt_ectx_stack = &ectx.ec_stack;
1764 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001765
1766 // If this function returns and the closure is still
1767 // used, we need to make a copy of the context
1768 // (arguments and local variables). Store a reference
1769 // to the partial so we can handle that.
1770 ++pt->pt_refcount;
1771 tv = STACK_TV_VAR(dfunc->df_varcount
1772 + iptr->isn_arg.funcref.fr_var_idx);
1773 if (tv->v_type == VAR_PARTIAL)
1774 {
1775 // TODO: use a garray_T on ectx.
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001776 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001777 emsg("Multiple closures not supported yet");
1778 goto failed;
1779 }
1780 tv->v_type = VAR_PARTIAL;
1781 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001782 }
1783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 tv = STACK_TV_BOT(0);
1785 ++ectx.ec_stack.ga_len;
1786 tv->vval.v_partial = pt;
1787 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001788 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 }
1790 break;
1791
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001792 // Create a global function from a lambda.
1793 case ISN_NEWFUNC:
1794 {
1795 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1796
1797 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1798 }
1799 break;
1800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 // jump if a condition is met
1802 case ISN_JUMP:
1803 {
1804 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1805 int jump = TRUE;
1806
1807 if (when != JUMP_ALWAYS)
1808 {
1809 tv = STACK_TV_BOT(-1);
1810 jump = tv2bool(tv);
1811 if (when == JUMP_IF_FALSE
1812 || when == JUMP_AND_KEEP_IF_FALSE)
1813 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001814 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 {
1816 // drop the value from the stack
1817 clear_tv(tv);
1818 --ectx.ec_stack.ga_len;
1819 }
1820 }
1821 if (jump)
1822 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1823 }
1824 break;
1825
1826 // top of a for loop
1827 case ISN_FOR:
1828 {
1829 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1830 typval_T *idxtv =
1831 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1832
1833 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001834 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 goto failed;
1836 if (++idxtv->vval.v_number >= list->lv_len)
1837 // past the end of the list, jump to "endfor"
1838 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1839 else if (list->lv_first == &range_list_item)
1840 {
1841 // non-materialized range() list
1842 tv = STACK_TV_BOT(0);
1843 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001844 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 tv->vval.v_number = list_find_nr(
1846 list, idxtv->vval.v_number, NULL);
1847 ++ectx.ec_stack.ga_len;
1848 }
1849 else
1850 {
1851 listitem_T *li = list_find(list, idxtv->vval.v_number);
1852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1854 ++ectx.ec_stack.ga_len;
1855 }
1856 }
1857 break;
1858
1859 // start of ":try" block
1860 case ISN_TRY:
1861 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001862 trycmd_T *trycmd = NULL;
1863
Bram Moolenaar270d0382020-05-15 21:42:53 +02001864 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 goto failed;
1866 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1867 + ectx.ec_trystack.ga_len;
1868 ++ectx.ec_trystack.ga_len;
1869 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001870 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1872 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001873 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 }
1875 break;
1876
1877 case ISN_PUSHEXC:
1878 if (current_exception == NULL)
1879 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001880 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 iemsg("Evaluating catch while current_exception is NULL");
1882 goto failed;
1883 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001884 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 goto failed;
1886 tv = STACK_TV_BOT(0);
1887 ++ectx.ec_stack.ga_len;
1888 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001889 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 tv->vval.v_string = vim_strsave(
1891 (char_u *)current_exception->value);
1892 break;
1893
1894 case ISN_CATCH:
1895 {
1896 garray_T *trystack = &ectx.ec_trystack;
1897
1898 if (trystack->ga_len > 0)
1899 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001900 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 + trystack->ga_len - 1;
1902 trycmd->tcd_caught = TRUE;
1903 }
1904 did_emsg = got_int = did_throw = FALSE;
1905 catch_exception(current_exception);
1906 }
1907 break;
1908
1909 // end of ":try" block
1910 case ISN_ENDTRY:
1911 {
1912 garray_T *trystack = &ectx.ec_trystack;
1913
1914 if (trystack->ga_len > 0)
1915 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001916 trycmd_T *trycmd = NULL;
1917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 --trystack->ga_len;
1919 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001920 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 trycmd = ((trycmd_T *)trystack->ga_data)
1922 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001923 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 {
1925 // discard the exception
1926 if (caught_stack == current_exception)
1927 caught_stack = caught_stack->caught;
1928 discard_current_exception();
1929 }
1930
1931 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001932 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 }
1934 }
1935 break;
1936
1937 case ISN_THROW:
1938 --ectx.ec_stack.ga_len;
1939 tv = STACK_TV_BOT(0);
1940 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1941 {
1942 vim_free(tv->vval.v_string);
1943 goto failed;
1944 }
1945 did_throw = TRUE;
1946 break;
1947
1948 // compare with special values
1949 case ISN_COMPAREBOOL:
1950 case ISN_COMPARESPECIAL:
1951 {
1952 typval_T *tv1 = STACK_TV_BOT(-2);
1953 typval_T *tv2 = STACK_TV_BOT(-1);
1954 varnumber_T arg1 = tv1->vval.v_number;
1955 varnumber_T arg2 = tv2->vval.v_number;
1956 int res;
1957
1958 switch (iptr->isn_arg.op.op_type)
1959 {
1960 case EXPR_EQUAL: res = arg1 == arg2; break;
1961 case EXPR_NEQUAL: res = arg1 != arg2; break;
1962 default: res = 0; break;
1963 }
1964
1965 --ectx.ec_stack.ga_len;
1966 tv1->v_type = VAR_BOOL;
1967 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1968 }
1969 break;
1970
1971 // Operation with two number arguments
1972 case ISN_OPNR:
1973 case ISN_COMPARENR:
1974 {
1975 typval_T *tv1 = STACK_TV_BOT(-2);
1976 typval_T *tv2 = STACK_TV_BOT(-1);
1977 varnumber_T arg1 = tv1->vval.v_number;
1978 varnumber_T arg2 = tv2->vval.v_number;
1979 varnumber_T res;
1980
1981 switch (iptr->isn_arg.op.op_type)
1982 {
1983 case EXPR_MULT: res = arg1 * arg2; break;
1984 case EXPR_DIV: res = arg1 / arg2; break;
1985 case EXPR_REM: res = arg1 % arg2; break;
1986 case EXPR_SUB: res = arg1 - arg2; break;
1987 case EXPR_ADD: res = arg1 + arg2; break;
1988
1989 case EXPR_EQUAL: res = arg1 == arg2; break;
1990 case EXPR_NEQUAL: res = arg1 != arg2; break;
1991 case EXPR_GREATER: res = arg1 > arg2; break;
1992 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1993 case EXPR_SMALLER: res = arg1 < arg2; break;
1994 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1995 default: res = 0; break;
1996 }
1997
1998 --ectx.ec_stack.ga_len;
1999 if (iptr->isn_type == ISN_COMPARENR)
2000 {
2001 tv1->v_type = VAR_BOOL;
2002 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2003 }
2004 else
2005 tv1->vval.v_number = res;
2006 }
2007 break;
2008
2009 // Computation with two float arguments
2010 case ISN_OPFLOAT:
2011 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002012#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 {
2014 typval_T *tv1 = STACK_TV_BOT(-2);
2015 typval_T *tv2 = STACK_TV_BOT(-1);
2016 float_T arg1 = tv1->vval.v_float;
2017 float_T arg2 = tv2->vval.v_float;
2018 float_T res = 0;
2019 int cmp = FALSE;
2020
2021 switch (iptr->isn_arg.op.op_type)
2022 {
2023 case EXPR_MULT: res = arg1 * arg2; break;
2024 case EXPR_DIV: res = arg1 / arg2; break;
2025 case EXPR_SUB: res = arg1 - arg2; break;
2026 case EXPR_ADD: res = arg1 + arg2; break;
2027
2028 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2029 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2030 case EXPR_GREATER: cmp = arg1 > arg2; break;
2031 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2032 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2033 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2034 default: cmp = 0; break;
2035 }
2036 --ectx.ec_stack.ga_len;
2037 if (iptr->isn_type == ISN_COMPAREFLOAT)
2038 {
2039 tv1->v_type = VAR_BOOL;
2040 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2041 }
2042 else
2043 tv1->vval.v_float = res;
2044 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002045#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 break;
2047
2048 case ISN_COMPARELIST:
2049 {
2050 typval_T *tv1 = STACK_TV_BOT(-2);
2051 typval_T *tv2 = STACK_TV_BOT(-1);
2052 list_T *arg1 = tv1->vval.v_list;
2053 list_T *arg2 = tv2->vval.v_list;
2054 int cmp = FALSE;
2055 int ic = iptr->isn_arg.op.op_ic;
2056
2057 switch (iptr->isn_arg.op.op_type)
2058 {
2059 case EXPR_EQUAL: cmp =
2060 list_equal(arg1, arg2, ic, FALSE); break;
2061 case EXPR_NEQUAL: cmp =
2062 !list_equal(arg1, arg2, ic, FALSE); break;
2063 case EXPR_IS: cmp = arg1 == arg2; break;
2064 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2065 default: cmp = 0; break;
2066 }
2067 --ectx.ec_stack.ga_len;
2068 clear_tv(tv1);
2069 clear_tv(tv2);
2070 tv1->v_type = VAR_BOOL;
2071 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2072 }
2073 break;
2074
2075 case ISN_COMPAREBLOB:
2076 {
2077 typval_T *tv1 = STACK_TV_BOT(-2);
2078 typval_T *tv2 = STACK_TV_BOT(-1);
2079 blob_T *arg1 = tv1->vval.v_blob;
2080 blob_T *arg2 = tv2->vval.v_blob;
2081 int cmp = FALSE;
2082
2083 switch (iptr->isn_arg.op.op_type)
2084 {
2085 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2086 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2087 case EXPR_IS: cmp = arg1 == arg2; break;
2088 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2089 default: cmp = 0; break;
2090 }
2091 --ectx.ec_stack.ga_len;
2092 clear_tv(tv1);
2093 clear_tv(tv2);
2094 tv1->v_type = VAR_BOOL;
2095 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2096 }
2097 break;
2098
2099 // TODO: handle separately
2100 case ISN_COMPARESTRING:
2101 case ISN_COMPAREDICT:
2102 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 case ISN_COMPAREANY:
2104 {
2105 typval_T *tv1 = STACK_TV_BOT(-2);
2106 typval_T *tv2 = STACK_TV_BOT(-1);
2107 exptype_T exptype = iptr->isn_arg.op.op_type;
2108 int ic = iptr->isn_arg.op.op_ic;
2109
2110 typval_compare(tv1, tv2, exptype, ic);
2111 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 --ectx.ec_stack.ga_len;
2113 }
2114 break;
2115
2116 case ISN_ADDLIST:
2117 case ISN_ADDBLOB:
2118 {
2119 typval_T *tv1 = STACK_TV_BOT(-2);
2120 typval_T *tv2 = STACK_TV_BOT(-1);
2121
2122 if (iptr->isn_type == ISN_ADDLIST)
2123 eval_addlist(tv1, tv2);
2124 else
2125 eval_addblob(tv1, tv2);
2126 clear_tv(tv2);
2127 --ectx.ec_stack.ga_len;
2128 }
2129 break;
2130
2131 // Computation with two arguments of unknown type
2132 case ISN_OPANY:
2133 {
2134 typval_T *tv1 = STACK_TV_BOT(-2);
2135 typval_T *tv2 = STACK_TV_BOT(-1);
2136 varnumber_T n1, n2;
2137#ifdef FEAT_FLOAT
2138 float_T f1 = 0, f2 = 0;
2139#endif
2140 int error = FALSE;
2141
2142 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2143 {
2144 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2145 {
2146 eval_addlist(tv1, tv2);
2147 clear_tv(tv2);
2148 --ectx.ec_stack.ga_len;
2149 break;
2150 }
2151 else if (tv1->v_type == VAR_BLOB
2152 && tv2->v_type == VAR_BLOB)
2153 {
2154 eval_addblob(tv1, tv2);
2155 clear_tv(tv2);
2156 --ectx.ec_stack.ga_len;
2157 break;
2158 }
2159 }
2160#ifdef FEAT_FLOAT
2161 if (tv1->v_type == VAR_FLOAT)
2162 {
2163 f1 = tv1->vval.v_float;
2164 n1 = 0;
2165 }
2166 else
2167#endif
2168 {
2169 n1 = tv_get_number_chk(tv1, &error);
2170 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002171 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172#ifdef FEAT_FLOAT
2173 if (tv2->v_type == VAR_FLOAT)
2174 f1 = n1;
2175#endif
2176 }
2177#ifdef FEAT_FLOAT
2178 if (tv2->v_type == VAR_FLOAT)
2179 {
2180 f2 = tv2->vval.v_float;
2181 n2 = 0;
2182 }
2183 else
2184#endif
2185 {
2186 n2 = tv_get_number_chk(tv2, &error);
2187 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002188 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189#ifdef FEAT_FLOAT
2190 if (tv1->v_type == VAR_FLOAT)
2191 f2 = n2;
2192#endif
2193 }
2194#ifdef FEAT_FLOAT
2195 // if there is a float on either side the result is a float
2196 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2197 {
2198 switch (iptr->isn_arg.op.op_type)
2199 {
2200 case EXPR_MULT: f1 = f1 * f2; break;
2201 case EXPR_DIV: f1 = f1 / f2; break;
2202 case EXPR_SUB: f1 = f1 - f2; break;
2203 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002204 default: SOURCING_LNUM = iptr->isn_lnum;
2205 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002206 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 }
2208 clear_tv(tv1);
2209 clear_tv(tv2);
2210 tv1->v_type = VAR_FLOAT;
2211 tv1->vval.v_float = f1;
2212 --ectx.ec_stack.ga_len;
2213 }
2214 else
2215#endif
2216 {
2217 switch (iptr->isn_arg.op.op_type)
2218 {
2219 case EXPR_MULT: n1 = n1 * n2; break;
2220 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2221 case EXPR_SUB: n1 = n1 - n2; break;
2222 case EXPR_ADD: n1 = n1 + n2; break;
2223 default: n1 = num_modulus(n1, n2); break;
2224 }
2225 clear_tv(tv1);
2226 clear_tv(tv2);
2227 tv1->v_type = VAR_NUMBER;
2228 tv1->vval.v_number = n1;
2229 --ectx.ec_stack.ga_len;
2230 }
2231 }
2232 break;
2233
2234 case ISN_CONCAT:
2235 {
2236 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2237 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2238 char_u *res;
2239
2240 res = concat_str(str1, str2);
2241 clear_tv(STACK_TV_BOT(-2));
2242 clear_tv(STACK_TV_BOT(-1));
2243 --ectx.ec_stack.ga_len;
2244 STACK_TV_BOT(-1)->vval.v_string = res;
2245 }
2246 break;
2247
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002248 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002249 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002250 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002251 int is_slice = iptr->isn_type == ISN_STRSLICE;
2252 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002253 char_u *res;
2254
2255 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002256 // string slice: string is at stack-3, first index at
2257 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002258 if (is_slice)
2259 {
2260 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002261 n1 = tv->vval.v_number;
2262 }
2263
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002264 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002265 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002266
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002267 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002268 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002269 if (is_slice)
2270 // Slice: Select the characters from the string
2271 res = string_slice(tv->vval.v_string, n1, n2);
2272 else
2273 // Index: The resulting variable is a string of a
2274 // single character. If the index is too big or
2275 // negative the result is empty.
2276 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002277 vim_free(tv->vval.v_string);
2278 tv->vval.v_string = res;
2279 }
2280 break;
2281
2282 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002283 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 {
Bram Moolenaared591872020-08-15 22:14:53 +02002285 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002287 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288
2289 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002290 // list slice: list is at stack-3, indexes at stack-2 and
2291 // stack-1
2292 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 list = tv->vval.v_list;
2294
2295 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002296 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002298
2299 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 {
Bram Moolenaared591872020-08-15 22:14:53 +02002301 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002302 n1 = tv->vval.v_number;
2303 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 }
Bram Moolenaared591872020-08-15 22:14:53 +02002305
2306 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002307 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002308 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002309 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2310 == FAIL)
2311 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 }
2313 break;
2314
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002315 case ISN_ANYINDEX:
2316 case ISN_ANYSLICE:
2317 {
2318 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2319 typval_T *var1, *var2;
2320 int res;
2321
2322 // index: composite is at stack-2, index at stack-1
2323 // slice: composite is at stack-3, indexes at stack-2 and
2324 // stack-1
2325 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002326 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002327 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2328 goto on_error;
2329 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2330 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2331 res = eval_index_inner(tv, is_slice,
2332 var1, var2, NULL, -1, TRUE);
2333 clear_tv(var1);
2334 if (is_slice)
2335 clear_tv(var2);
2336 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2337 if (res == FAIL)
2338 goto on_error;
2339 }
2340 break;
2341
Bram Moolenaar9af78762020-06-16 11:34:42 +02002342 case ISN_SLICE:
2343 {
2344 list_T *list;
2345 int count = iptr->isn_arg.number;
2346
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002347 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002348 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002349 list = tv->vval.v_list;
2350
2351 // no error for short list, expect it to be checked earlier
2352 if (list != NULL && list->lv_len >= count)
2353 {
2354 list_T *newlist = list_slice(list,
2355 count, list->lv_len - 1);
2356
2357 if (newlist != NULL)
2358 {
2359 list_unref(list);
2360 tv->vval.v_list = newlist;
2361 ++newlist->lv_refcount;
2362 }
2363 }
2364 }
2365 break;
2366
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002367 case ISN_GETITEM:
2368 {
2369 listitem_T *li;
2370 int index = iptr->isn_arg.number;
2371
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002372 // Get list item: list is at stack-1, push item.
2373 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002374 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002375 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002376
2377 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2378 goto failed;
2379 ++ectx.ec_stack.ga_len;
2380 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2381 }
2382 break;
2383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 case ISN_MEMBER:
2385 {
2386 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002387 char_u *key;
2388 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002389 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002390
2391 // dict member: dict is at stack-2, key at stack-1
2392 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002393 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002394 dict = tv->vval.v_dict;
2395
2396 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002397 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002398 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002399
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002400 if ((di = dict_find(dict, key, -1)) == NULL)
2401 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002402 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002403 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002404 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002405 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002406 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002407 --ectx.ec_stack.ga_len;
2408 // Clear the dict after getting the item, to avoid that it
2409 // make the item invalid.
2410 tv = STACK_TV_BOT(-1);
2411 temp_tv = *tv;
2412 copy_tv(&di->di_tv, tv);
2413 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002414 }
2415 break;
2416
2417 // dict member with string key
2418 case ISN_STRINGMEMBER:
2419 {
2420 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002422 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423
2424 tv = STACK_TV_BOT(-1);
2425 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2426 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002427 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002429 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 }
2431 dict = tv->vval.v_dict;
2432
2433 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2434 == NULL)
2435 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002436 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002438 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002440 // Clear the dict after getting the item, to avoid that it
2441 // make the item invalid.
2442 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002444 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 }
2446 break;
2447
2448 case ISN_NEGATENR:
2449 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002450 if (tv->v_type != VAR_NUMBER
2451#ifdef FEAT_FLOAT
2452 && tv->v_type != VAR_FLOAT
2453#endif
2454 )
2455 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002456 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002457 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002458 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002459 }
2460#ifdef FEAT_FLOAT
2461 if (tv->v_type == VAR_FLOAT)
2462 tv->vval.v_float = -tv->vval.v_float;
2463 else
2464#endif
2465 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 break;
2467
2468 case ISN_CHECKNR:
2469 {
2470 int error = FALSE;
2471
2472 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002473 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002475 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 (void)tv_get_number_chk(tv, &error);
2477 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002478 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 }
2480 break;
2481
2482 case ISN_CHECKTYPE:
2483 {
2484 checktype_T *ct = &iptr->isn_arg.type;
2485
2486 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002487 // TODO: better type comparison
2488 if (tv->v_type != ct->ct_type
2489 && !((tv->v_type == VAR_PARTIAL
2490 && ct->ct_type == VAR_FUNC)
2491 || (tv->v_type == VAR_FUNC
2492 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002494 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002495 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 vartype_name(ct->ct_type),
2497 vartype_name(tv->v_type));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002498 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 }
2500 }
2501 break;
2502
Bram Moolenaar9af78762020-06-16 11:34:42 +02002503 case ISN_CHECKLEN:
2504 {
2505 int min_len = iptr->isn_arg.checklen.cl_min_len;
2506 list_T *list = NULL;
2507
2508 tv = STACK_TV_BOT(-1);
2509 if (tv->v_type == VAR_LIST)
2510 list = tv->vval.v_list;
2511 if (list == NULL || list->lv_len < min_len
2512 || (list->lv_len > min_len
2513 && !iptr->isn_arg.checklen.cl_more_OK))
2514 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002515 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002516 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002517 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002518 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002519 }
2520 }
2521 break;
2522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 case ISN_2BOOL:
2524 {
2525 int n;
2526
2527 tv = STACK_TV_BOT(-1);
2528 n = tv2bool(tv);
2529 if (iptr->isn_arg.number) // invert
2530 n = !n;
2531 clear_tv(tv);
2532 tv->v_type = VAR_BOOL;
2533 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2534 }
2535 break;
2536
2537 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002538 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 {
2540 char_u *str;
2541
2542 tv = STACK_TV_BOT(iptr->isn_arg.number);
2543 if (tv->v_type != VAR_STRING)
2544 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002545 if (iptr->isn_type == ISN_2STRING_ANY)
2546 {
2547 switch (tv->v_type)
2548 {
2549 case VAR_SPECIAL:
2550 case VAR_BOOL:
2551 case VAR_NUMBER:
2552 case VAR_FLOAT:
2553 case VAR_BLOB: break;
2554 default: to_string_error(tv->v_type);
2555 goto on_error;
2556 }
2557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 str = typval_tostring(tv);
2559 clear_tv(tv);
2560 tv->v_type = VAR_STRING;
2561 tv->vval.v_string = str;
2562 }
2563 }
2564 break;
2565
Bram Moolenaar389df252020-07-09 21:20:47 +02002566 case ISN_SHUFFLE:
2567 {
2568 typval_T tmp_tv;
2569 int item = iptr->isn_arg.shuffle.shfl_item;
2570 int up = iptr->isn_arg.shuffle.shfl_up;
2571
2572 tmp_tv = *STACK_TV_BOT(-item);
2573 for ( ; up > 0 && item > 1; --up)
2574 {
2575 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2576 --item;
2577 }
2578 *STACK_TV_BOT(-item) = tmp_tv;
2579 }
2580 break;
2581
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 case ISN_DROP:
2583 --ectx.ec_stack.ga_len;
2584 clear_tv(STACK_TV_BOT(0));
2585 break;
2586 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002587 continue;
2588
Bram Moolenaard032f342020-07-18 18:13:02 +02002589func_return:
2590 // Restore previous function. If the frame pointer is zero then there
2591 // is none and we are done.
2592 if (ectx.ec_frame_idx == initial_frame_idx)
2593 {
2594 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2595 // only fails when out of memory
2596 goto failed;
2597 goto done;
2598 }
2599 if (func_return(&ectx) == FAIL)
2600 // only fails when out of memory
2601 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002602 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002603
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002604on_error:
2605 if (trylevel == 0)
2606 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 }
2608
2609done:
2610 // function finished, get result from the stack.
2611 tv = STACK_TV_BOT(-1);
2612 *rettv = *tv;
2613 tv->v_type = VAR_UNKNOWN;
2614 ret = OK;
2615
2616failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002617 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002618 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002619 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002620
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002621 estack_pop();
2622 current_sctx = save_current_sctx;
2623
2624failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002625 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2627 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002630 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002631
2632 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002633 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002634 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 return ret;
2636}
2637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638/*
2639 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002640 * We don't really need this at runtime, but we do have tests that require it,
2641 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 */
2643 void
2644ex_disassemble(exarg_T *eap)
2645{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002646 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002647 char_u *fname;
2648 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 dfunc_T *dfunc;
2650 isn_T *instr;
2651 int current;
2652 int line_idx = 0;
2653 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002654 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002656 if (STRNCMP(arg, "<lambda>", 8) == 0)
2657 {
2658 arg += 8;
2659 (void)getdigits(&arg);
2660 fname = vim_strnsave(eap->arg, arg - eap->arg);
2661 }
2662 else
2663 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002664 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002665 if (fname == NULL)
2666 {
2667 semsg(_(e_invarg2), eap->arg);
2668 return;
2669 }
2670
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002671 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002672 if (ufunc == NULL)
2673 {
2674 char_u *p = untrans_function_name(fname);
2675
2676 if (p != NULL)
2677 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002678 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002679 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002680 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 if (ufunc == NULL)
2682 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002683 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 return;
2685 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002686 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002687 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2688 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002689 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002691 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 return;
2693 }
2694 if (ufunc->uf_name_exp != NULL)
2695 msg((char *)ufunc->uf_name_exp);
2696 else
2697 msg((char *)ufunc->uf_name);
2698
2699 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2700 instr = dfunc->df_instr;
2701 for (current = 0; current < dfunc->df_instr_count; ++current)
2702 {
2703 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002704 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705
2706 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2707 {
2708 if (current > prev_current)
2709 {
2710 msg_puts("\n\n");
2711 prev_current = current;
2712 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002713 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2714 if (line != NULL)
2715 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 }
2717
2718 switch (iptr->isn_type)
2719 {
2720 case ISN_EXEC:
2721 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2722 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002723 case ISN_EXECCONCAT:
2724 smsg("%4d EXECCONCAT %lld", current,
2725 (long long)iptr->isn_arg.number);
2726 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 case ISN_ECHO:
2728 {
2729 echo_T *echo = &iptr->isn_arg.echo;
2730
2731 smsg("%4d %s %d", current,
2732 echo->echo_with_white ? "ECHO" : "ECHON",
2733 echo->echo_count);
2734 }
2735 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002736 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002737 smsg("%4d EXECUTE %lld", current,
2738 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002739 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002740 case ISN_ECHOMSG:
2741 smsg("%4d ECHOMSG %lld", current,
2742 (long long)(iptr->isn_arg.number));
2743 break;
2744 case ISN_ECHOERR:
2745 smsg("%4d ECHOERR %lld", current,
2746 (long long)(iptr->isn_arg.number));
2747 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002749 case ISN_LOADOUTER:
2750 {
2751 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2752
2753 if (iptr->isn_arg.number < 0)
2754 smsg("%4d LOAD%s arg[%lld]", current, add,
2755 (long long)(iptr->isn_arg.number
2756 + STACK_FRAME_SIZE));
2757 else
2758 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002759 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002760 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 break;
2762 case ISN_LOADV:
2763 smsg("%4d LOADV v:%s", current,
2764 get_vim_var_name(iptr->isn_arg.number));
2765 break;
2766 case ISN_LOADSCRIPT:
2767 {
2768 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002769 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2771 + iptr->isn_arg.script.script_idx;
2772
2773 smsg("%4d LOADSCRIPT %s from %s", current,
2774 sv->sv_name, si->sn_name);
2775 }
2776 break;
2777 case ISN_LOADS:
2778 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002779 scriptitem_T *si = SCRIPT_ITEM(
2780 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781
2782 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002783 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 }
2785 break;
2786 case ISN_LOADG:
2787 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2788 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002789 case ISN_LOADB:
2790 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2791 break;
2792 case ISN_LOADW:
2793 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2794 break;
2795 case ISN_LOADT:
2796 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2797 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002798 case ISN_LOADGDICT:
2799 smsg("%4d LOAD g:", current);
2800 break;
2801 case ISN_LOADBDICT:
2802 smsg("%4d LOAD b:", current);
2803 break;
2804 case ISN_LOADWDICT:
2805 smsg("%4d LOAD w:", current);
2806 break;
2807 case ISN_LOADTDICT:
2808 smsg("%4d LOAD t:", current);
2809 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 case ISN_LOADOPT:
2811 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2812 break;
2813 case ISN_LOADENV:
2814 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2815 break;
2816 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002817 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 break;
2819
2820 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002821 case ISN_STOREOUTER:
2822 {
2823 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2824
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002825 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002826 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002827 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002828 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002829 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002830 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002831 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002833 case ISN_STOREV:
2834 smsg("%4d STOREV v:%s", current,
2835 get_vim_var_name(iptr->isn_arg.number));
2836 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002838 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2839 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002840 case ISN_STOREB:
2841 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2842 break;
2843 case ISN_STOREW:
2844 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2845 break;
2846 case ISN_STORET:
2847 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2848 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002849 case ISN_STORES:
2850 {
2851 scriptitem_T *si = SCRIPT_ITEM(
2852 iptr->isn_arg.loadstore.ls_sid);
2853
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002854 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002855 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 break;
2858 case ISN_STORESCRIPT:
2859 {
2860 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002861 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2863 + iptr->isn_arg.script.script_idx;
2864
2865 smsg("%4d STORESCRIPT %s in %s", current,
2866 sv->sv_name, si->sn_name);
2867 }
2868 break;
2869 case ISN_STOREOPT:
2870 smsg("%4d STOREOPT &%s", current,
2871 iptr->isn_arg.storeopt.so_name);
2872 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002873 case ISN_STOREENV:
2874 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2875 break;
2876 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002877 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002878 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 case ISN_STORENR:
2880 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002881 iptr->isn_arg.storenr.stnr_val,
2882 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 break;
2884
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002885 case ISN_STORELIST:
2886 smsg("%4d STORELIST", current);
2887 break;
2888
2889 case ISN_STOREDICT:
2890 smsg("%4d STOREDICT", current);
2891 break;
2892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 // constants
2894 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002895 smsg("%4d PUSHNR %lld", current,
2896 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 break;
2898 case ISN_PUSHBOOL:
2899 case ISN_PUSHSPEC:
2900 smsg("%4d PUSH %s", current,
2901 get_var_special_name(iptr->isn_arg.number));
2902 break;
2903 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002904#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002906#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 break;
2908 case ISN_PUSHS:
2909 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2910 break;
2911 case ISN_PUSHBLOB:
2912 {
2913 char_u *r;
2914 char_u numbuf[NUMBUFLEN];
2915 char_u *tofree;
2916
2917 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002918 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 vim_free(tofree);
2920 }
2921 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002922 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002923 {
2924 char *name = (char *)iptr->isn_arg.string;
2925
2926 smsg("%4d PUSHFUNC \"%s\"", current,
2927 name == NULL ? "[none]" : name);
2928 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002929 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002930 case ISN_PUSHCHANNEL:
2931#ifdef FEAT_JOB_CHANNEL
2932 {
2933 channel_T *channel = iptr->isn_arg.channel;
2934
2935 smsg("%4d PUSHCHANNEL %d", current,
2936 channel == NULL ? 0 : channel->ch_id);
2937 }
2938#endif
2939 break;
2940 case ISN_PUSHJOB:
2941#ifdef FEAT_JOB_CHANNEL
2942 {
2943 typval_T tv;
2944 char_u *name;
2945
2946 tv.v_type = VAR_JOB;
2947 tv.vval.v_job = iptr->isn_arg.job;
2948 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002949 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002950 }
2951#endif
2952 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 case ISN_PUSHEXC:
2954 smsg("%4d PUSH v:exception", current);
2955 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002956 case ISN_UNLET:
2957 smsg("%4d UNLET%s %s", current,
2958 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2959 iptr->isn_arg.unlet.ul_name);
2960 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002961 case ISN_UNLETENV:
2962 smsg("%4d UNLETENV%s $%s", current,
2963 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2964 iptr->isn_arg.unlet.ul_name);
2965 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002967 smsg("%4d NEWLIST size %lld", current,
2968 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 break;
2970 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002971 smsg("%4d NEWDICT size %lld", current,
2972 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 break;
2974
2975 // function call
2976 case ISN_BCALL:
2977 {
2978 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2979
2980 smsg("%4d BCALL %s(argc %d)", current,
2981 internal_func_name(cbfunc->cbf_idx),
2982 cbfunc->cbf_argcount);
2983 }
2984 break;
2985 case ISN_DCALL:
2986 {
2987 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2988 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2989 + cdfunc->cdf_idx;
2990
2991 smsg("%4d DCALL %s(argc %d)", current,
2992 df->df_ufunc->uf_name_exp != NULL
2993 ? df->df_ufunc->uf_name_exp
2994 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2995 }
2996 break;
2997 case ISN_UCALL:
2998 {
2999 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3000
3001 smsg("%4d UCALL %s(argc %d)", current,
3002 cufunc->cuf_name, cufunc->cuf_argcount);
3003 }
3004 break;
3005 case ISN_PCALL:
3006 {
3007 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3008
3009 smsg("%4d PCALL%s (argc %d)", current,
3010 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3011 }
3012 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003013 case ISN_PCALL_END:
3014 smsg("%4d PCALL end", current);
3015 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 case ISN_RETURN:
3017 smsg("%4d RETURN", current);
3018 break;
3019 case ISN_FUNCREF:
3020 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003021 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003023 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003025 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003026 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 }
3028 break;
3029
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003030 case ISN_NEWFUNC:
3031 {
3032 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3033
3034 smsg("%4d NEWFUNC %s %s", current,
3035 newfunc->nf_lambda, newfunc->nf_global);
3036 }
3037 break;
3038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 case ISN_JUMP:
3040 {
3041 char *when = "?";
3042
3043 switch (iptr->isn_arg.jump.jump_when)
3044 {
3045 case JUMP_ALWAYS:
3046 when = "JUMP";
3047 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 case JUMP_AND_KEEP_IF_TRUE:
3049 when = "JUMP_AND_KEEP_IF_TRUE";
3050 break;
3051 case JUMP_IF_FALSE:
3052 when = "JUMP_IF_FALSE";
3053 break;
3054 case JUMP_AND_KEEP_IF_FALSE:
3055 when = "JUMP_AND_KEEP_IF_FALSE";
3056 break;
3057 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003058 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 iptr->isn_arg.jump.jump_where);
3060 }
3061 break;
3062
3063 case ISN_FOR:
3064 {
3065 forloop_T *forloop = &iptr->isn_arg.forloop;
3066
3067 smsg("%4d FOR $%d -> %d", current,
3068 forloop->for_idx, forloop->for_end);
3069 }
3070 break;
3071
3072 case ISN_TRY:
3073 {
3074 try_T *try = &iptr->isn_arg.try;
3075
3076 smsg("%4d TRY catch -> %d, finally -> %d", current,
3077 try->try_catch, try->try_finally);
3078 }
3079 break;
3080 case ISN_CATCH:
3081 // TODO
3082 smsg("%4d CATCH", current);
3083 break;
3084 case ISN_ENDTRY:
3085 smsg("%4d ENDTRY", current);
3086 break;
3087 case ISN_THROW:
3088 smsg("%4d THROW", current);
3089 break;
3090
3091 // expression operations on number
3092 case ISN_OPNR:
3093 case ISN_OPFLOAT:
3094 case ISN_OPANY:
3095 {
3096 char *what;
3097 char *ins;
3098
3099 switch (iptr->isn_arg.op.op_type)
3100 {
3101 case EXPR_MULT: what = "*"; break;
3102 case EXPR_DIV: what = "/"; break;
3103 case EXPR_REM: what = "%"; break;
3104 case EXPR_SUB: what = "-"; break;
3105 case EXPR_ADD: what = "+"; break;
3106 default: what = "???"; break;
3107 }
3108 switch (iptr->isn_type)
3109 {
3110 case ISN_OPNR: ins = "OPNR"; break;
3111 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3112 case ISN_OPANY: ins = "OPANY"; break;
3113 default: ins = "???"; break;
3114 }
3115 smsg("%4d %s %s", current, ins, what);
3116 }
3117 break;
3118
3119 case ISN_COMPAREBOOL:
3120 case ISN_COMPARESPECIAL:
3121 case ISN_COMPARENR:
3122 case ISN_COMPAREFLOAT:
3123 case ISN_COMPARESTRING:
3124 case ISN_COMPAREBLOB:
3125 case ISN_COMPARELIST:
3126 case ISN_COMPAREDICT:
3127 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 case ISN_COMPAREANY:
3129 {
3130 char *p;
3131 char buf[10];
3132 char *type;
3133
3134 switch (iptr->isn_arg.op.op_type)
3135 {
3136 case EXPR_EQUAL: p = "=="; break;
3137 case EXPR_NEQUAL: p = "!="; break;
3138 case EXPR_GREATER: p = ">"; break;
3139 case EXPR_GEQUAL: p = ">="; break;
3140 case EXPR_SMALLER: p = "<"; break;
3141 case EXPR_SEQUAL: p = "<="; break;
3142 case EXPR_MATCH: p = "=~"; break;
3143 case EXPR_IS: p = "is"; break;
3144 case EXPR_ISNOT: p = "isnot"; break;
3145 case EXPR_NOMATCH: p = "!~"; break;
3146 default: p = "???"; break;
3147 }
3148 STRCPY(buf, p);
3149 if (iptr->isn_arg.op.op_ic == TRUE)
3150 strcat(buf, "?");
3151 switch(iptr->isn_type)
3152 {
3153 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3154 case ISN_COMPARESPECIAL:
3155 type = "COMPARESPECIAL"; break;
3156 case ISN_COMPARENR: type = "COMPARENR"; break;
3157 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3158 case ISN_COMPARESTRING:
3159 type = "COMPARESTRING"; break;
3160 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3161 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3162 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3163 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3165 default: type = "???"; break;
3166 }
3167
3168 smsg("%4d %s %s", current, type, buf);
3169 }
3170 break;
3171
3172 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3173 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3174
3175 // expression operations
3176 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003177 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003178 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003179 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003180 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003181 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3182 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003183 case ISN_SLICE: smsg("%4d SLICE %lld",
3184 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003185 case ISN_GETITEM: smsg("%4d ITEM %lld",
3186 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003187 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3188 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 iptr->isn_arg.string); break;
3190 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3191
3192 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3193 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3194 vartype_name(iptr->isn_arg.type.ct_type),
3195 iptr->isn_arg.type.ct_off);
3196 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003197 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3198 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3199 iptr->isn_arg.checklen.cl_min_len);
3200 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 case ISN_2BOOL: if (iptr->isn_arg.number)
3202 smsg("%4d INVERT (!val)", current);
3203 else
3204 smsg("%4d 2BOOL (!!val)", current);
3205 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003206 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3207 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003208 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003209 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3210 (long long)(iptr->isn_arg.number));
3211 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212
Bram Moolenaar389df252020-07-09 21:20:47 +02003213 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3214 iptr->isn_arg.shuffle.shfl_item,
3215 iptr->isn_arg.shuffle.shfl_up);
3216 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 case ISN_DROP: smsg("%4d DROP", current); break;
3218 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003219
3220 out_flush(); // output one line at a time
3221 ui_breakcheck();
3222 if (got_int)
3223 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225}
3226
3227/*
3228 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3229 * list, etc. Mostly like what JavaScript does, except that empty list and
3230 * empty dictionary are FALSE.
3231 */
3232 int
3233tv2bool(typval_T *tv)
3234{
3235 switch (tv->v_type)
3236 {
3237 case VAR_NUMBER:
3238 return tv->vval.v_number != 0;
3239 case VAR_FLOAT:
3240#ifdef FEAT_FLOAT
3241 return tv->vval.v_float != 0.0;
3242#else
3243 break;
3244#endif
3245 case VAR_PARTIAL:
3246 return tv->vval.v_partial != NULL;
3247 case VAR_FUNC:
3248 case VAR_STRING:
3249 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3250 case VAR_LIST:
3251 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3252 case VAR_DICT:
3253 return tv->vval.v_dict != NULL
3254 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3255 case VAR_BOOL:
3256 case VAR_SPECIAL:
3257 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3258 case VAR_JOB:
3259#ifdef FEAT_JOB_CHANNEL
3260 return tv->vval.v_job != NULL;
3261#else
3262 break;
3263#endif
3264 case VAR_CHANNEL:
3265#ifdef FEAT_JOB_CHANNEL
3266 return tv->vval.v_channel != NULL;
3267#else
3268 break;
3269#endif
3270 case VAR_BLOB:
3271 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3272 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003273 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 case VAR_VOID:
3275 break;
3276 }
3277 return FALSE;
3278}
3279
3280/*
3281 * If "tv" is a string give an error and return FAIL.
3282 */
3283 int
3284check_not_string(typval_T *tv)
3285{
3286 if (tv->v_type == VAR_STRING)
3287 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003288 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 clear_tv(tv);
3290 return FAIL;
3291 }
3292 return OK;
3293}
3294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295
3296#endif // FEAT_EVAL