xref: /vim-8.2.3635/src/vim9execute.c (revision 98a29d00)
1 /* 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.
26 typedef struct {
27     int	    tcd_frame_idx;	// ec_frame_idx when ISN_TRY was encountered
28     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  */
57 struct ectx_S {
58     garray_T	ec_stack;	// stack of typval_T values
59     int		ec_frame_idx;	// index in ec_stack: context of ec_dfunc_idx
60 
61     outer_T	*ec_outer;	// outer scope used for closures, allocated
62 
63     garray_T	ec_trystack;	// stack of trycmd_T values
64     int		ec_in_catch;	// when TRUE in catch or finally block
65 
66     int		ec_dfunc_idx;	// current function index
67     isn_T	*ec_instr;	// array with instructions
68     int		ec_iidx;	// index in ec_instr: instruction to execute
69 
70     garray_T	ec_funcrefs;	// partials that might be a closure
71 };
72 
73 // Get pointer to item relative to the bottom of the stack, -1 is the last one.
74 #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
75 
76     void
77 to_string_error(vartype_T vartype)
78 {
79     semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
80 }
81 
82 /*
83  * Return the number of arguments, including optional arguments and any vararg.
84  */
85     static int
86 ufunc_argcount(ufunc_T *ufunc)
87 {
88     return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
89 }
90 
91 /*
92  * Set the instruction index, depending on omitted arguments, where the default
93  * values are to be computed.  If all optional arguments are present, start
94  * with the function body.
95  * The expression evaluation is at the start of the instructions:
96  *  0 ->  EVAL default1
97  *	       STORE arg[-2]
98  *  1 ->  EVAL default2
99  *	       STORE arg[-1]
100  *  2 ->  function body
101  */
102     static void
103 init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
104 {
105     if (ufunc->uf_def_args.ga_len == 0)
106 	ectx->ec_iidx = 0;
107     else
108     {
109 	int	defcount = ufunc->uf_args.ga_len - argcount;
110 
111 	// If there is a varargs argument defcount can be negative, no defaults
112 	// to evaluate then.
113 	if (defcount < 0)
114 	    defcount = 0;
115 	ectx->ec_iidx = ufunc->uf_def_arg_idx[
116 					 ufunc->uf_def_args.ga_len - defcount];
117     }
118 }
119 
120 /*
121  * Create a new list from "count" items at the bottom of the stack.
122  * When "count" is zero an empty list is added to the stack.
123  */
124     static int
125 exe_newlist(int count, ectx_T *ectx)
126 {
127     list_T	*list = list_alloc_with_items(count);
128     int		idx;
129     typval_T	*tv;
130 
131     if (list == NULL)
132 	return FAIL;
133     for (idx = 0; idx < count; ++idx)
134 	list_set_item(list, idx, STACK_TV_BOT(idx - count));
135 
136     if (count > 0)
137 	ectx->ec_stack.ga_len -= count - 1;
138     else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
139 	return FAIL;
140     else
141 	++ectx->ec_stack.ga_len;
142     tv = STACK_TV_BOT(-1);
143     tv->v_type = VAR_LIST;
144     tv->vval.v_list = list;
145     ++list->lv_refcount;
146     return OK;
147 }
148 
149 /*
150  * Call compiled function "cdf_idx" from compiled code.
151  * This adds a stack frame and sets the instruction pointer to the start of the
152  * called function.
153  * If "pt" is not null use "pt->pt_outer" for ec_outer.
154  *
155  * Stack has:
156  * - current arguments (already there)
157  * - omitted optional argument (default values) added here
158  * - stack frame:
159  *	- pointer to calling function
160  *	- Index of next instruction in calling function
161  *	- previous frame pointer
162  * - reserved space for local variables
163  */
164     static int
165 call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
166 {
167     int	    argcount = argcount_arg;
168     dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
169     ufunc_T *ufunc = dfunc->df_ufunc;
170     int	    arg_to_add;
171     int	    vararg_count = 0;
172     int	    varcount;
173     int	    idx;
174     estack_T *entry;
175 
176     if (dfunc->df_deleted)
177     {
178 	// don't use ufunc->uf_name, it may have been freed
179 	emsg_funcname(e_func_deleted,
180 		dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
181 	return FAIL;
182     }
183 
184     if (ufunc->uf_va_name != NULL)
185     {
186 	// Need to make a list out of the vararg arguments.
187 	// Stack at time of call with 2 varargs:
188 	//   normal_arg
189 	//   optional_arg
190 	//   vararg_1
191 	//   vararg_2
192 	// After creating the list:
193 	//   normal_arg
194 	//   optional_arg
195 	//   vararg-list
196 	// With missing optional arguments we get:
197 	//    normal_arg
198 	// After creating the list
199 	//    normal_arg
200 	//    (space for optional_arg)
201 	//    vararg-list
202 	vararg_count = argcount - ufunc->uf_args.ga_len;
203 	if (vararg_count < 0)
204 	    vararg_count = 0;
205 	else
206 	    argcount -= vararg_count;
207 	if (exe_newlist(vararg_count, ectx) == FAIL)
208 	    return FAIL;
209 
210 	vararg_count = 1;
211     }
212 
213     arg_to_add = ufunc->uf_args.ga_len - argcount;
214     if (arg_to_add < 0)
215     {
216 	if (arg_to_add == -1)
217 	    emsg(_(e_one_argument_too_many));
218 	else
219 	    semsg(_(e_nr_arguments_too_many), -arg_to_add);
220 	return FAIL;
221     }
222 
223     // Reserve space for:
224     // - missing arguments
225     // - stack frame
226     // - local variables
227     // - if needed: a counter for number of closures created in
228     //   ectx->ec_funcrefs.
229     varcount = dfunc->df_varcount + dfunc->df_has_closure;
230     if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
231 								       == FAIL)
232 	return FAIL;
233 
234     // If depth of calling is getting too high, don't execute the function.
235     if (funcdepth_increment() == FAIL)
236 	return FAIL;
237 
238     // Move the vararg-list to below the missing optional arguments.
239     if (vararg_count > 0 && arg_to_add > 0)
240 	*STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
241 
242     // Reserve space for omitted optional arguments, filled in soon.
243     for (idx = 0; idx < arg_to_add; ++idx)
244 	STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
245     ectx->ec_stack.ga_len += arg_to_add;
246 
247     // Store current execution state in stack frame for ISN_RETURN.
248     STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
249     STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
250     STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
251     STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
252     ectx->ec_frame_idx = ectx->ec_stack.ga_len;
253 
254     // Initialize local variables
255     for (idx = 0; idx < dfunc->df_varcount; ++idx)
256 	STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
257     if (dfunc->df_has_closure)
258     {
259 	typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
260 
261 	tv->v_type = VAR_NUMBER;
262 	tv->vval.v_number = 0;
263     }
264     ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
265 
266     if (pt != NULL || ufunc->uf_partial != NULL || ufunc->uf_flags & FC_CLOSURE)
267     {
268 	outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
269 
270 	if (outer == NULL)
271 	    return FAIL;
272 	if (pt != NULL)
273 	{
274 	    *outer = pt->pt_outer;
275 	    outer->out_up_is_copy = TRUE;
276 	}
277 	else if (ufunc->uf_partial != NULL)
278 	{
279 	    *outer = ufunc->uf_partial->pt_outer;
280 	    outer->out_up_is_copy = TRUE;
281 	}
282 	else
283 	{
284 	    outer->out_stack = &ectx->ec_stack;
285 	    outer->out_frame_idx = ectx->ec_frame_idx;
286 	    outer->out_up = ectx->ec_outer;
287 	}
288 	ectx->ec_outer = outer;
289     }
290     else
291 	ectx->ec_outer = NULL;
292 
293     // Set execution state to the start of the called function.
294     ectx->ec_dfunc_idx = cdf_idx;
295     ectx->ec_instr = dfunc->df_instr;
296     entry = estack_push_ufunc(dfunc->df_ufunc, 1);
297     if (entry != NULL)
298     {
299 	// Set the script context to the script where the function was defined.
300 	// TODO: save more than the SID?
301 	entry->es_save_sid = current_sctx.sc_sid;
302 	current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
303     }
304 
305     // Decide where to start execution, handles optional arguments.
306     init_instr_idx(ufunc, argcount, ectx);
307 
308     return OK;
309 }
310 
311 // Get pointer to item in the stack.
312 #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
313 
314 /*
315  * Used when returning from a function: Check if any closure is still
316  * referenced.  If so then move the arguments and variables to a separate piece
317  * of stack to be used when the closure is called.
318  * When "free_arguments" is TRUE the arguments are to be freed.
319  * Returns FAIL when out of memory.
320  */
321     static int
322 handle_closure_in_use(ectx_T *ectx, int free_arguments)
323 {
324     dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
325 							  + ectx->ec_dfunc_idx;
326     int		argcount;
327     int		top;
328     int		idx;
329     typval_T	*tv;
330     int		closure_in_use = FALSE;
331     garray_T	*gap = &ectx->ec_funcrefs;
332     varnumber_T	closure_count;
333 
334     if (dfunc->df_ufunc == NULL)
335 	return OK;  // function was freed
336     if (dfunc->df_has_closure == 0)
337 	return OK;  // no closures
338     tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
339     closure_count = tv->vval.v_number;
340     if (closure_count == 0)
341 	return OK;  // no funcrefs created
342 
343     argcount = ufunc_argcount(dfunc->df_ufunc);
344     top = ectx->ec_frame_idx - argcount;
345 
346     // Check if any created closure is still in use.
347     for (idx = 0; idx < closure_count; ++idx)
348     {
349 	partial_T   *pt;
350 	int	    off = gap->ga_len - closure_count + idx;
351 
352 	if (off < 0)
353 	    continue;  // count is off or already done
354 	pt = ((partial_T **)gap->ga_data)[off];
355 	if (pt->pt_refcount > 1)
356 	{
357 	    int refcount = pt->pt_refcount;
358 	    int i;
359 
360 	    // A Reference in a local variables doesn't count, it gets
361 	    // unreferenced on return.
362 	    for (i = 0; i < dfunc->df_varcount; ++i)
363 	    {
364 		typval_T *stv = STACK_TV(ectx->ec_frame_idx
365 						       + STACK_FRAME_SIZE + i);
366 		if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
367 		    --refcount;
368 	    }
369 	    if (refcount > 1)
370 	    {
371 		closure_in_use = TRUE;
372 		break;
373 	    }
374 	}
375     }
376 
377     if (closure_in_use)
378     {
379 	funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
380 	typval_T    *stack;
381 
382 	// A closure is using the arguments and/or local variables.
383 	// Move them to the called function.
384 	if (funcstack == NULL)
385 	    return FAIL;
386 	funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
387 	funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
388 	stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
389 	funcstack->fs_ga.ga_data = stack;
390 	if (stack == NULL)
391 	{
392 	    vim_free(funcstack);
393 	    return FAIL;
394 	}
395 
396 	// Move or copy the arguments.
397 	for (idx = 0; idx < argcount; ++idx)
398 	{
399 	    tv = STACK_TV(top + idx);
400 	    if (free_arguments)
401 	    {
402 		*(stack + idx) = *tv;
403 		tv->v_type = VAR_UNKNOWN;
404 	    }
405 	    else
406 		copy_tv(tv, stack + idx);
407 	}
408 	// Move the local variables.
409 	for (idx = 0; idx < dfunc->df_varcount; ++idx)
410 	{
411 	    tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
412 
413 	    // A partial created for a local function, that is also used as a
414 	    // local variable, has a reference count for the variable, thus
415 	    // will never go down to zero.  When all these refcounts are one
416 	    // then the funcstack is unused.  We need to count how many we have
417 	    // so we need when to check.
418 	    if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
419 	    {
420 		int	    i;
421 
422 		for (i = 0; i < closure_count; ++i)
423 		    if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
424 					      gap->ga_len - closure_count + i])
425 			++funcstack->fs_min_refcount;
426 	    }
427 
428 	    *(stack + funcstack->fs_var_offset + idx) = *tv;
429 	    tv->v_type = VAR_UNKNOWN;
430 	}
431 
432 	for (idx = 0; idx < closure_count; ++idx)
433 	{
434 	    partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
435 							- closure_count + idx];
436 	    if (pt->pt_refcount > 1)
437 	    {
438 		++funcstack->fs_refcount;
439 		pt->pt_funcstack = funcstack;
440 		pt->pt_outer.out_stack = &funcstack->fs_ga;
441 		pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
442 		pt->pt_outer.out_up = ectx->ec_outer;
443 	    }
444 	}
445     }
446 
447     for (idx = 0; idx < closure_count; ++idx)
448 	partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
449 						       - closure_count + idx]);
450     gap->ga_len -= closure_count;
451     if (gap->ga_len == 0)
452 	ga_clear(gap);
453 
454     return OK;
455 }
456 
457 /*
458  * Called when a partial is freed or its reference count goes down to one.  The
459  * funcstack may be the only reference to the partials in the local variables.
460  * Go over all of them, the funcref and can be freed if all partials
461  * referencing the funcstack have a reference count of one.
462  */
463     void
464 funcstack_check_refcount(funcstack_T *funcstack)
465 {
466     int		    i;
467     garray_T	    *gap = &funcstack->fs_ga;
468     int		    done = 0;
469 
470     if (funcstack->fs_refcount > funcstack->fs_min_refcount)
471 	return;
472     for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
473     {
474 	typval_T *tv = ((typval_T *)gap->ga_data) + i;
475 
476 	if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
477 		&& tv->vval.v_partial->pt_funcstack == funcstack
478 		&& tv->vval.v_partial->pt_refcount == 1)
479 	    ++done;
480     }
481     if (done == funcstack->fs_min_refcount)
482     {
483 	typval_T	*stack = gap->ga_data;
484 
485 	// All partials referencing the funcstack have a reference count of
486 	// one, thus the funcstack is no longer of use.
487 	for (i = 0; i < gap->ga_len; ++i)
488 	    clear_tv(stack + i);
489 	vim_free(stack);
490 	vim_free(funcstack);
491     }
492 }
493 
494 /*
495  * Return from the current function.
496  */
497     static int
498 func_return(ectx_T *ectx)
499 {
500     int		idx;
501     int		ret_idx;
502     dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
503 							  + ectx->ec_dfunc_idx;
504     int		argcount = ufunc_argcount(dfunc->df_ufunc);
505     int		top = ectx->ec_frame_idx - argcount;
506     estack_T	*entry;
507 
508     // execution context goes one level up
509     entry = estack_pop();
510     if (entry != NULL)
511 	current_sctx.sc_sid = entry->es_save_sid;
512 
513     if (handle_closure_in_use(ectx, TRUE) == FAIL)
514 	return FAIL;
515 
516     // Clear the arguments.
517     for (idx = top; idx < ectx->ec_frame_idx; ++idx)
518 	clear_tv(STACK_TV(idx));
519 
520     // Clear local variables and temp values, but not the return value.
521     for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
522 					idx < ectx->ec_stack.ga_len - 1; ++idx)
523 	clear_tv(STACK_TV(idx));
524 
525     // The return value should be on top of the stack.  However, when aborting
526     // it may not be there and ec_frame_idx is the top of the stack.
527     ret_idx = ectx->ec_stack.ga_len - 1;
528     if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
529 	ret_idx = 0;
530 
531     vim_free(ectx->ec_outer);
532 
533     // Restore the previous frame.
534     ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx
535 					+ STACK_FRAME_FUNC_OFF)->vval.v_number;
536     ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
537 					+ STACK_FRAME_IIDX_OFF)->vval.v_number;
538     ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
539 				       + STACK_FRAME_OUTER_OFF)->vval.v_string;
540     // restoring ec_frame_idx must be last
541     ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
542 				       + STACK_FRAME_IDX_OFF)->vval.v_number;
543     dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
544     ectx->ec_instr = dfunc->df_instr;
545 
546     if (ret_idx > 0)
547     {
548 	// Reset the stack to the position before the call, with a spot for the
549 	// return value, moved there from above the frame.
550 	ectx->ec_stack.ga_len = top + 1;
551 	*STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
552     }
553     else
554 	// Reset the stack to the position before the call.
555 	ectx->ec_stack.ga_len = top;
556 
557     funcdepth_decrement();
558     return OK;
559 }
560 
561 #undef STACK_TV
562 
563 /*
564  * Prepare arguments and rettv for calling a builtin or user function.
565  */
566     static int
567 call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
568 {
569     int		idx;
570     typval_T	*tv;
571 
572     // Move arguments from bottom of the stack to argvars[] and add terminator.
573     for (idx = 0; idx < argcount; ++idx)
574 	argvars[idx] = *STACK_TV_BOT(idx - argcount);
575     argvars[argcount].v_type = VAR_UNKNOWN;
576 
577     // Result replaces the arguments on the stack.
578     if (argcount > 0)
579 	ectx->ec_stack.ga_len -= argcount - 1;
580     else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
581 	return FAIL;
582     else
583 	++ectx->ec_stack.ga_len;
584 
585     // Default return value is zero.
586     tv = STACK_TV_BOT(-1);
587     tv->v_type = VAR_NUMBER;
588     tv->vval.v_number = 0;
589 
590     return OK;
591 }
592 
593 // Ugly global to avoid passing the execution context around through many
594 // layers.
595 static ectx_T *current_ectx = NULL;
596 
597 /*
598  * Call a builtin function by index.
599  */
600     static int
601 call_bfunc(int func_idx, int argcount, ectx_T *ectx)
602 {
603     typval_T	argvars[MAX_FUNC_ARGS];
604     int		idx;
605     int		did_emsg_before = did_emsg;
606     ectx_T	*prev_ectx = current_ectx;
607 
608     if (call_prepare(argcount, argvars, ectx) == FAIL)
609 	return FAIL;
610 
611     // Call the builtin function.  Set "current_ectx" so that when it
612     // recursively invokes call_def_function() a closure context can be set.
613     current_ectx = ectx;
614     call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
615     current_ectx = prev_ectx;
616 
617     // Clear the arguments.
618     for (idx = 0; idx < argcount; ++idx)
619 	clear_tv(&argvars[idx]);
620 
621     if (did_emsg > did_emsg_before)
622 	return FAIL;
623     return OK;
624 }
625 
626 /*
627  * Execute a user defined function.
628  * If the function is compiled this will add a stack frame and set the
629  * instruction pointer at the start of the function.
630  * Otherwise the function is called here.
631  * If "pt" is not null use "pt->pt_outer" for ec_outer.
632  * "iptr" can be used to replace the instruction with a more efficient one.
633  */
634     static int
635 call_ufunc(
636 	ufunc_T	    *ufunc,
637 	partial_T   *pt,
638 	int	    argcount,
639 	ectx_T	    *ectx,
640 	isn_T	    *iptr)
641 {
642     typval_T	argvars[MAX_FUNC_ARGS];
643     funcexe_T   funcexe;
644     int		error;
645     int		idx;
646     int		did_emsg_before = did_emsg;
647 
648     if (ufunc->uf_def_status == UF_TO_BE_COMPILED
649 	    && compile_def_function(ufunc, FALSE, NULL) == FAIL)
650 	return FAIL;
651     if (ufunc->uf_def_status == UF_COMPILED)
652     {
653 	error = check_user_func_argcount(ufunc, argcount);
654 	if (error != FCERR_UNKNOWN)
655 	{
656 	    if (error == FCERR_TOOMANY)
657 		semsg(_(e_toomanyarg), ufunc->uf_name);
658 	    else
659 		semsg(_(e_toofewarg), ufunc->uf_name);
660 	    return FAIL;
661 	}
662 
663 	// The function has been compiled, can call it quickly.  For a function
664 	// that was defined later: we can call it directly next time.
665 	// TODO: what if the function was deleted and then defined again?
666 	if (iptr != NULL)
667 	{
668 	    delete_instr(iptr);
669 	    iptr->isn_type = ISN_DCALL;
670 	    iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
671 	    iptr->isn_arg.dfunc.cdf_argcount = argcount;
672 	}
673 	return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
674     }
675 
676     if (call_prepare(argcount, argvars, ectx) == FAIL)
677 	return FAIL;
678     CLEAR_FIELD(funcexe);
679     funcexe.evaluate = TRUE;
680 
681     // Call the user function.  Result goes in last position on the stack.
682     // TODO: add selfdict if there is one
683     error = call_user_func_check(ufunc, argcount, argvars,
684 					     STACK_TV_BOT(-1), &funcexe, NULL);
685 
686     // Clear the arguments.
687     for (idx = 0; idx < argcount; ++idx)
688 	clear_tv(&argvars[idx]);
689 
690     if (error != FCERR_NONE)
691     {
692 	user_func_error(error, ufunc->uf_name);
693 	return FAIL;
694     }
695     if (did_emsg > did_emsg_before)
696 	// Error other than from calling the function itself.
697 	return FAIL;
698     return OK;
699 }
700 
701 /*
702  * Return TRUE if an error was given or CTRL-C was pressed.
703  */
704     static int
705 vim9_aborting(int prev_called_emsg)
706 {
707     return called_emsg > prev_called_emsg || got_int || did_throw;
708 }
709 
710 /*
711  * Execute a function by "name".
712  * This can be a builtin function or a user function.
713  * "iptr" can be used to replace the instruction with a more efficient one.
714  * Returns FAIL if not found without an error message.
715  */
716     static int
717 call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
718 {
719     ufunc_T *ufunc;
720 
721     if (builtin_function(name, -1))
722     {
723 	int func_idx = find_internal_func(name);
724 
725 	if (func_idx < 0)
726 	    return FAIL;
727 	if (check_internal_func(func_idx, argcount) < 0)
728 	    return FAIL;
729 	return call_bfunc(func_idx, argcount, ectx);
730     }
731 
732     ufunc = find_func(name, FALSE, NULL);
733 
734     if (ufunc == NULL)
735     {
736 	int called_emsg_before = called_emsg;
737 
738 	if (script_autoload(name, TRUE))
739 	    // loaded a package, search for the function again
740 	    ufunc = find_func(name, FALSE, NULL);
741 	if (vim9_aborting(called_emsg_before))
742 	    return FAIL;  // bail out if loading the script caused an error
743     }
744 
745     if (ufunc != NULL)
746 	return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
747 
748     return FAIL;
749 }
750 
751     static int
752 call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
753 {
754     int		argcount = argcount_arg;
755     char_u	*name = NULL;
756     int		called_emsg_before = called_emsg;
757     int		res = FAIL;
758 
759     if (tv->v_type == VAR_PARTIAL)
760     {
761 	partial_T   *pt = tv->vval.v_partial;
762 	int	    i;
763 
764 	if (pt->pt_argc > 0)
765 	{
766 	    // Make space for arguments from the partial, shift the "argcount"
767 	    // arguments up.
768 	    if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
769 		return FAIL;
770 	    for (i = 1; i <= argcount; ++i)
771 		*STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
772 	    ectx->ec_stack.ga_len += pt->pt_argc;
773 	    argcount += pt->pt_argc;
774 
775 	    // copy the arguments from the partial onto the stack
776 	    for (i = 0; i < pt->pt_argc; ++i)
777 		copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
778 	}
779 
780 	if (pt->pt_func != NULL)
781 	    return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
782 
783 	name = pt->pt_name;
784     }
785     else if (tv->v_type == VAR_FUNC)
786 	name = tv->vval.v_string;
787     if (name != NULL)
788     {
789 	char_u	fname_buf[FLEN_FIXED + 1];
790 	char_u	*tofree = NULL;
791 	int	error = FCERR_NONE;
792 	char_u	*fname;
793 
794 	// May need to translate <SNR>123_ to K_SNR.
795 	fname = fname_trans_sid(name, fname_buf, &tofree, &error);
796 	if (error != FCERR_NONE)
797 	    res = FAIL;
798 	else
799 	    res = call_by_name(fname, argcount, ectx, NULL);
800 	vim_free(tofree);
801     }
802 
803     if (res == FAIL)
804     {
805 	if (called_emsg == called_emsg_before)
806 	    semsg(_(e_unknownfunc),
807 				  name == NULL ? (char_u *)"[unknown]" : name);
808 	return FAIL;
809     }
810     return OK;
811 }
812 
813 /*
814  * Check if "lock" is VAR_LOCKED or VAR_FIXED.  If so give an error and return
815  * TRUE.
816  */
817     static int
818 error_if_locked(int lock, char *error)
819 {
820     if (lock & (VAR_LOCKED | VAR_FIXED))
821     {
822 	emsg(_(error));
823 	return TRUE;
824     }
825     return FALSE;
826 }
827 
828 /*
829  * Store "tv" in variable "name".
830  * This is for s: and g: variables.
831  */
832     static void
833 store_var(char_u *name, typval_T *tv)
834 {
835     funccal_entry_T entry;
836 
837     save_funccal(&entry);
838     set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL);
839     restore_funccal();
840 }
841 
842 /*
843  * Convert "tv" to a string.
844  * Return FAIL if not allowed.
845  */
846     static int
847 do_2string(typval_T *tv, int is_2string_any)
848 {
849     if (tv->v_type != VAR_STRING)
850     {
851 	char_u *str;
852 
853 	if (is_2string_any)
854 	{
855 	    switch (tv->v_type)
856 	    {
857 		case VAR_SPECIAL:
858 		case VAR_BOOL:
859 		case VAR_NUMBER:
860 		case VAR_FLOAT:
861 		case VAR_BLOB:	break;
862 		default:	to_string_error(tv->v_type);
863 				return FAIL;
864 	    }
865 	}
866 	str = typval_tostring(tv);
867 	clear_tv(tv);
868 	tv->v_type = VAR_STRING;
869 	tv->vval.v_string = str;
870     }
871     return OK;
872 }
873 
874 /*
875  * When the value of "sv" is a null list of dict, allocate it.
876  */
877     static void
878 allocate_if_null(typval_T *tv)
879 {
880     switch (tv->v_type)
881     {
882 	case VAR_LIST:
883 	    if (tv->vval.v_list == NULL)
884 		rettv_list_alloc(tv);
885 	    break;
886 	case VAR_DICT:
887 	    if (tv->vval.v_dict == NULL)
888 		rettv_dict_alloc(tv);
889 	    break;
890 	default:
891 	    break;
892     }
893 }
894 
895 /*
896  * Return the character "str[index]" where "index" is the character index.  If
897  * "index" is out of range NULL is returned.
898  */
899     char_u *
900 char_from_string(char_u *str, varnumber_T index)
901 {
902     size_t	    nbyte = 0;
903     varnumber_T	    nchar = index;
904     size_t	    slen;
905 
906     if (str == NULL)
907 	return NULL;
908     slen = STRLEN(str);
909 
910     // do the same as for a list: a negative index counts from the end
911     if (index < 0)
912     {
913 	int	clen = 0;
914 
915 	for (nbyte = 0; nbyte < slen; ++clen)
916 	    nbyte += MB_CPTR2LEN(str + nbyte);
917 	nchar = clen + index;
918 	if (nchar < 0)
919 	    // unlike list: index out of range results in empty string
920 	    return NULL;
921     }
922 
923     for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
924 	nbyte += MB_CPTR2LEN(str + nbyte);
925     if (nbyte >= slen)
926 	return NULL;
927     return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
928 }
929 
930 /*
931  * Get the byte index for character index "idx" in string "str" with length
932  * "str_len".
933  * If going over the end return "str_len".
934  * If "idx" is negative count from the end, -1 is the last character.
935  * When going over the start return -1.
936  */
937     static long
938 char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
939 {
940     varnumber_T nchar = idx;
941     size_t	nbyte = 0;
942 
943     if (nchar >= 0)
944     {
945 	while (nchar > 0 && nbyte < str_len)
946 	{
947 	    nbyte += MB_CPTR2LEN(str + nbyte);
948 	    --nchar;
949 	}
950     }
951     else
952     {
953 	nbyte = str_len;
954 	while (nchar < 0 && nbyte > 0)
955 	{
956 	    --nbyte;
957 	    nbyte -= mb_head_off(str, str + nbyte);
958 	    ++nchar;
959 	}
960 	if (nchar < 0)
961 	    return -1;
962     }
963     return (long)nbyte;
964 }
965 
966 /*
967  * Return the slice "str[first:last]" using character indexes.
968  * "exclusive" is TRUE for slice().
969  * Return NULL when the result is empty.
970  */
971     char_u *
972 string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
973 {
974     long	start_byte, end_byte;
975     size_t	slen;
976 
977     if (str == NULL)
978 	return NULL;
979     slen = STRLEN(str);
980     start_byte = char_idx2byte(str, slen, first);
981     if (start_byte < 0)
982 	start_byte = 0; // first index very negative: use zero
983     if ((last == -1 && !exclusive) || last == VARNUM_MAX)
984 	end_byte = (long)slen;
985     else
986     {
987 	end_byte = char_idx2byte(str, slen, last);
988 	if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
989 	    // end index is inclusive
990 	    end_byte += MB_CPTR2LEN(str + end_byte);
991     }
992 
993     if (start_byte >= (long)slen || end_byte <= start_byte)
994 	return NULL;
995     return vim_strnsave(str + start_byte, end_byte - start_byte);
996 }
997 
998     static svar_T *
999 get_script_svar(scriptref_T *sref, ectx_T *ectx)
1000 {
1001     scriptitem_T    *si = SCRIPT_ITEM(sref->sref_sid);
1002     dfunc_T	    *dfunc = ((dfunc_T *)def_functions.ga_data)
1003 							  + ectx->ec_dfunc_idx;
1004     svar_T	    *sv;
1005 
1006     if (sref->sref_seq != si->sn_script_seq)
1007     {
1008 	// The script was reloaded after the function was
1009 	// compiled, the script_idx may not be valid.
1010 	semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1011 						 dfunc->df_ufunc->uf_name_exp);
1012 	return NULL;
1013     }
1014     sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1015     if (!equal_type(sv->sv_type, sref->sref_type))
1016     {
1017 	emsg(_(e_script_variable_type_changed));
1018 	return NULL;
1019     }
1020     return sv;
1021 }
1022 
1023 /*
1024  * Execute a function by "name".
1025  * This can be a builtin function, user function or a funcref.
1026  * "iptr" can be used to replace the instruction with a more efficient one.
1027  */
1028     static int
1029 call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
1030 {
1031     int	    called_emsg_before = called_emsg;
1032     int	    res;
1033 
1034     res = call_by_name(name, argcount, ectx, iptr);
1035     if (res == FAIL && called_emsg == called_emsg_before)
1036     {
1037 	dictitem_T	*v;
1038 
1039 	v = find_var(name, NULL, FALSE);
1040 	if (v == NULL)
1041 	{
1042 	    semsg(_(e_unknownfunc), name);
1043 	    return FAIL;
1044 	}
1045 	if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1046 	{
1047 	    semsg(_(e_unknownfunc), name);
1048 	    return FAIL;
1049 	}
1050 	return call_partial(&v->di_tv, argcount, ectx);
1051     }
1052     return res;
1053 }
1054 
1055 /*
1056  * When a function reference is used, fill a partial with the information
1057  * needed, especially when it is used as a closure.
1058  */
1059     int
1060 fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1061 {
1062     pt->pt_func = ufunc;
1063     pt->pt_refcount = 1;
1064 
1065     if (pt->pt_func->uf_flags & FC_CLOSURE)
1066     {
1067 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
1068 							  + ectx->ec_dfunc_idx;
1069 
1070 	// The closure needs to find arguments and local
1071 	// variables in the current stack.
1072 	pt->pt_outer.out_stack = &ectx->ec_stack;
1073 	pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1074 	pt->pt_outer.out_up = ectx->ec_outer;
1075 	pt->pt_outer.out_up_is_copy = TRUE;
1076 
1077 	// If this function returns and the closure is still
1078 	// being used, we need to make a copy of the context
1079 	// (arguments and local variables). Store a reference
1080 	// to the partial so we can handle that.
1081 	if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1082 	{
1083 	    vim_free(pt);
1084 	    return FAIL;
1085 	}
1086 	// Extra variable keeps the count of closures created
1087 	// in the current function call.
1088 	++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1089 		       + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1090 
1091 	((partial_T **)ectx->ec_funcrefs.ga_data)
1092 			       [ectx->ec_funcrefs.ga_len] = pt;
1093 	++pt->pt_refcount;
1094 	++ectx->ec_funcrefs.ga_len;
1095     }
1096     ++pt->pt_func->uf_refcount;
1097     return OK;
1098 }
1099 
1100 /*
1101  * Call a "def" function from old Vim script.
1102  * Return OK or FAIL.
1103  */
1104     int
1105 call_def_function(
1106     ufunc_T	*ufunc,
1107     int		argc_arg,	// nr of arguments
1108     typval_T	*argv,		// arguments
1109     partial_T	*partial,	// optional partial for context
1110     typval_T	*rettv)		// return value
1111 {
1112     ectx_T	ectx;		// execution context
1113     int		argc = argc_arg;
1114     int		initial_frame_idx;
1115     typval_T	*tv;
1116     int		idx;
1117     int		ret = FAIL;
1118     int		defcount = ufunc->uf_args.ga_len - argc;
1119     sctx_T	save_current_sctx = current_sctx;
1120     int		breakcheck_count = 0;
1121     int		did_emsg_before = did_emsg_cumul + did_emsg;
1122     int		save_suppress_errthrow = suppress_errthrow;
1123     msglist_T	**saved_msg_list = NULL;
1124     msglist_T	*private_msg_list = NULL;
1125     cmdmod_T	save_cmdmod;
1126     int		restore_cmdmod = FALSE;
1127     int		save_emsg_silent_def = emsg_silent_def;
1128     int		save_did_emsg_def = did_emsg_def;
1129     int		trylevel_at_start = trylevel;
1130     int		orig_funcdepth;
1131 
1132 // Get pointer to item in the stack.
1133 #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1134 
1135 // Get pointer to item at the bottom of the stack, -1 is the bottom.
1136 #undef STACK_TV_BOT
1137 #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1138 
1139 // Get pointer to a local variable on the stack.  Negative for arguments.
1140 #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
1141 
1142     if (ufunc->uf_def_status == UF_NOT_COMPILED
1143 	    || (ufunc->uf_def_status == UF_TO_BE_COMPILED
1144 			  && compile_def_function(ufunc, FALSE, NULL) == FAIL))
1145     {
1146 	if (did_emsg_cumul + did_emsg == did_emsg_before)
1147 	    semsg(_(e_function_is_not_compiled_str),
1148 						   printable_func_name(ufunc));
1149 	return FAIL;
1150     }
1151 
1152     {
1153 	// Check the function was really compiled.
1154 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
1155 							 + ufunc->uf_dfunc_idx;
1156 	if (dfunc->df_instr == NULL)
1157 	{
1158 	    iemsg("using call_def_function() on not compiled function");
1159 	    return FAIL;
1160 	}
1161     }
1162 
1163     // If depth of calling is getting too high, don't execute the function.
1164     orig_funcdepth = funcdepth_get();
1165     if (funcdepth_increment() == FAIL)
1166 	return FAIL;
1167 
1168     CLEAR_FIELD(ectx);
1169     ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1170     ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1171     if (ga_grow(&ectx.ec_stack, 20) == FAIL)
1172     {
1173 	funcdepth_decrement();
1174 	return FAIL;
1175     }
1176     ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
1177     ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
1178 
1179     // Put arguments on the stack, but no more than what the function expects.
1180     // A lambda can be called with more arguments than it uses.
1181     for (idx = 0; idx < argc
1182 	    && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1183 									 ++idx)
1184     {
1185 	if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
1186 		&& check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1187 							      idx + 1) == FAIL)
1188 	    goto failed_early;
1189 	copy_tv(&argv[idx], STACK_TV_BOT(0));
1190 	++ectx.ec_stack.ga_len;
1191     }
1192 
1193     // Turn varargs into a list.  Empty list if no args.
1194     if (ufunc->uf_va_name != NULL)
1195     {
1196 	int vararg_count = argc - ufunc->uf_args.ga_len;
1197 
1198 	if (vararg_count < 0)
1199 	    vararg_count = 0;
1200 	else
1201 	    argc -= vararg_count;
1202 	if (exe_newlist(vararg_count, &ectx) == FAIL)
1203 	    goto failed_early;
1204 
1205 	// Check the type of the list items.
1206 	tv = STACK_TV_BOT(-1);
1207 	if (ufunc->uf_va_type != NULL
1208 		&& ufunc->uf_va_type != &t_any
1209 		&& ufunc->uf_va_type->tt_member != &t_any
1210 		&& tv->vval.v_list != NULL)
1211 	{
1212 	    type_T	*expected = ufunc->uf_va_type->tt_member;
1213 	    listitem_T	*li = tv->vval.v_list->lv_first;
1214 
1215 	    for (idx = 0; idx < vararg_count; ++idx)
1216 	    {
1217 		if (check_typval_type(expected, &li->li_tv,
1218 						       argc + idx + 1) == FAIL)
1219 		    goto failed_early;
1220 		li = li->li_next;
1221 	    }
1222 	}
1223 
1224 	if (defcount > 0)
1225 	    // Move varargs list to below missing default arguments.
1226 	    *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
1227 	--ectx.ec_stack.ga_len;
1228     }
1229 
1230     // Make space for omitted arguments, will store default value below.
1231     // Any varargs list goes after them.
1232     if (defcount > 0)
1233 	for (idx = 0; idx < defcount; ++idx)
1234 	{
1235 	    STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1236 	    ++ectx.ec_stack.ga_len;
1237 	}
1238     if (ufunc->uf_va_name != NULL)
1239 	    ++ectx.ec_stack.ga_len;
1240 
1241     // Frame pointer points to just after arguments.
1242     ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1243     initial_frame_idx = ectx.ec_frame_idx;
1244 
1245     if (partial != NULL || ufunc->uf_partial != NULL)
1246     {
1247 	ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1248 	if (ectx.ec_outer == NULL)
1249 	    goto failed_early;
1250 	if (partial != NULL)
1251 	{
1252 	    if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1253 	    {
1254 		if (current_ectx->ec_outer != NULL)
1255 		    *ectx.ec_outer = *current_ectx->ec_outer;
1256 	    }
1257 	    else
1258 		*ectx.ec_outer = partial->pt_outer;
1259 	}
1260 	else
1261 	    *ectx.ec_outer = ufunc->uf_partial->pt_outer;
1262 	ectx.ec_outer->out_up_is_copy = TRUE;
1263     }
1264 
1265     // dummy frame entries
1266     for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1267     {
1268 	STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1269 	++ectx.ec_stack.ga_len;
1270     }
1271 
1272     {
1273 	// Reserve space for local variables and any closure reference count.
1274 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
1275 							 + ufunc->uf_dfunc_idx;
1276 
1277 	for (idx = 0; idx < dfunc->df_varcount; ++idx)
1278 	    STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
1279 	ectx.ec_stack.ga_len += dfunc->df_varcount;
1280 	if (dfunc->df_has_closure)
1281 	{
1282 	    STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1283 	    STACK_TV_VAR(idx)->vval.v_number = 0;
1284 	    ++ectx.ec_stack.ga_len;
1285 	}
1286 
1287 	ectx.ec_instr = dfunc->df_instr;
1288     }
1289 
1290     // Following errors are in the function, not the caller.
1291     // Commands behave like vim9script.
1292     estack_push_ufunc(ufunc, 1);
1293     current_sctx = ufunc->uf_script_ctx;
1294     current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1295 
1296     // Use a specific location for storing error messages to be converted to an
1297     // exception.
1298     saved_msg_list = msg_list;
1299     msg_list = &private_msg_list;
1300 
1301     // Do turn errors into exceptions.
1302     suppress_errthrow = FALSE;
1303 
1304     // When ":silent!" was used before calling then we still abort the
1305     // function.  If ":silent!" is used in the function then we don't.
1306     emsg_silent_def = emsg_silent;
1307     did_emsg_def = 0;
1308 
1309     // Decide where to start execution, handles optional arguments.
1310     init_instr_idx(ufunc, argc, &ectx);
1311 
1312     for (;;)
1313     {
1314 	isn_T	    *iptr;
1315 
1316 	if (++breakcheck_count >= 100)
1317 	{
1318 	    line_breakcheck();
1319 	    breakcheck_count = 0;
1320 	}
1321 	if (got_int)
1322 	{
1323 	    // Turn CTRL-C into an exception.
1324 	    got_int = FALSE;
1325 	    if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
1326 		goto failed;
1327 	    did_throw = TRUE;
1328 	}
1329 
1330 	if (did_emsg && msg_list != NULL && *msg_list != NULL)
1331 	{
1332 	    // Turn an error message into an exception.
1333 	    did_emsg = FALSE;
1334 	    if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1335 		goto failed;
1336 	    did_throw = TRUE;
1337 	    *msg_list = NULL;
1338 	}
1339 
1340 	if (did_throw && !ectx.ec_in_catch)
1341 	{
1342 	    garray_T	*trystack = &ectx.ec_trystack;
1343 	    trycmd_T    *trycmd = NULL;
1344 
1345 	    // An exception jumps to the first catch, finally, or returns from
1346 	    // the current function.
1347 	    if (trystack->ga_len > 0)
1348 		trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
1349 	    if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
1350 	    {
1351 		// jump to ":catch" or ":finally"
1352 		ectx.ec_in_catch = TRUE;
1353 		ectx.ec_iidx = trycmd->tcd_catch_idx;
1354 	    }
1355 	    else
1356 	    {
1357 		// Not inside try or need to return from current functions.
1358 		// Push a dummy return value.
1359 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1360 		    goto failed;
1361 		tv = STACK_TV_BOT(0);
1362 		tv->v_type = VAR_NUMBER;
1363 		tv->vval.v_number = 0;
1364 		++ectx.ec_stack.ga_len;
1365 		if (ectx.ec_frame_idx == initial_frame_idx)
1366 		{
1367 		    // At the toplevel we are done.
1368 		    need_rethrow = TRUE;
1369 		    if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1370 			goto failed;
1371 		    goto done;
1372 		}
1373 
1374 		if (func_return(&ectx) == FAIL)
1375 		    goto failed;
1376 	    }
1377 	    continue;
1378 	}
1379 
1380 	iptr = &ectx.ec_instr[ectx.ec_iidx++];
1381 	switch (iptr->isn_type)
1382 	{
1383 	    // execute Ex command line
1384 	    case ISN_EXEC:
1385 		{
1386 		    source_cookie_T cookie;
1387 
1388 		    SOURCING_LNUM = iptr->isn_lnum;
1389 		    // Pass getsourceline to get an error for a missing ":end"
1390 		    // command.
1391 		    CLEAR_FIELD(cookie);
1392 		    cookie.sourcing_lnum = iptr->isn_lnum - 1;
1393 		    if (do_cmdline(iptr->isn_arg.string,
1394 				getsourceline, &cookie,
1395 				   DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1396 									== FAIL
1397 				|| did_emsg)
1398 			goto on_error;
1399 		}
1400 		break;
1401 
1402 	    // execute Ex command from pieces on the stack
1403 	    case ISN_EXECCONCAT:
1404 		{
1405 		    int	    count = iptr->isn_arg.number;
1406 		    size_t  len = 0;
1407 		    int	    pass;
1408 		    int	    i;
1409 		    char_u  *cmd = NULL;
1410 		    char_u  *str;
1411 
1412 		    for (pass = 1; pass <= 2; ++pass)
1413 		    {
1414 			for (i = 0; i < count; ++i)
1415 			{
1416 			    tv = STACK_TV_BOT(i - count);
1417 			    str = tv->vval.v_string;
1418 			    if (str != NULL && *str != NUL)
1419 			    {
1420 				if (pass == 2)
1421 				    STRCPY(cmd + len, str);
1422 				len += STRLEN(str);
1423 			    }
1424 			    if (pass == 2)
1425 				clear_tv(tv);
1426 			}
1427 			if (pass == 1)
1428 			{
1429 			    cmd = alloc(len + 1);
1430 			    if (cmd == NULL)
1431 				goto failed;
1432 			    len = 0;
1433 			}
1434 		    }
1435 
1436 		    SOURCING_LNUM = iptr->isn_lnum;
1437 		    do_cmdline_cmd(cmd);
1438 		    vim_free(cmd);
1439 		}
1440 		break;
1441 
1442 	    // execute :echo {string} ...
1443 	    case ISN_ECHO:
1444 		{
1445 		    int count = iptr->isn_arg.echo.echo_count;
1446 		    int	atstart = TRUE;
1447 		    int needclr = TRUE;
1448 
1449 		    for (idx = 0; idx < count; ++idx)
1450 		    {
1451 			tv = STACK_TV_BOT(idx - count);
1452 			echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1453 							   &atstart, &needclr);
1454 			clear_tv(tv);
1455 		    }
1456 		    if (needclr)
1457 			msg_clr_eos();
1458 		    ectx.ec_stack.ga_len -= count;
1459 		}
1460 		break;
1461 
1462 	    // :execute {string} ...
1463 	    // :echomsg {string} ...
1464 	    // :echoerr {string} ...
1465 	    case ISN_EXECUTE:
1466 	    case ISN_ECHOMSG:
1467 	    case ISN_ECHOERR:
1468 		{
1469 		    int		count = iptr->isn_arg.number;
1470 		    garray_T	ga;
1471 		    char_u	buf[NUMBUFLEN];
1472 		    char_u	*p;
1473 		    int		len;
1474 		    int		failed = FALSE;
1475 
1476 		    ga_init2(&ga, 1, 80);
1477 		    for (idx = 0; idx < count; ++idx)
1478 		    {
1479 			tv = STACK_TV_BOT(idx - count);
1480 			if (iptr->isn_type == ISN_EXECUTE)
1481 			{
1482 			    if (tv->v_type == VAR_CHANNEL
1483 						      || tv->v_type == VAR_JOB)
1484 			    {
1485 				SOURCING_LNUM = iptr->isn_lnum;
1486 				emsg(_(e_inval_string));
1487 				break;
1488 			    }
1489 			    else
1490 				p = tv_get_string_buf(tv, buf);
1491 			}
1492 			else
1493 			    p = tv_stringify(tv, buf);
1494 
1495 			len = (int)STRLEN(p);
1496 			if (ga_grow(&ga, len + 2) == FAIL)
1497 			    failed = TRUE;
1498 			else
1499 			{
1500 			    if (ga.ga_len > 0)
1501 				((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1502 			    STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1503 			    ga.ga_len += len;
1504 			}
1505 			clear_tv(tv);
1506 		    }
1507 		    ectx.ec_stack.ga_len -= count;
1508 		    if (failed)
1509 		    {
1510 			ga_clear(&ga);
1511 			goto on_error;
1512 		    }
1513 
1514 		    if (ga.ga_data != NULL)
1515 		    {
1516 			if (iptr->isn_type == ISN_EXECUTE)
1517 			{
1518 			    SOURCING_LNUM = iptr->isn_lnum;
1519 			    do_cmdline_cmd((char_u *)ga.ga_data);
1520 			    if (did_emsg)
1521 			    {
1522 				ga_clear(&ga);
1523 				goto on_error;
1524 			    }
1525 			}
1526 			else
1527 			{
1528 			    msg_sb_eol();
1529 			    if (iptr->isn_type == ISN_ECHOMSG)
1530 			    {
1531 				msg_attr(ga.ga_data, echo_attr);
1532 				out_flush();
1533 			    }
1534 			    else
1535 			    {
1536 				SOURCING_LNUM = iptr->isn_lnum;
1537 				emsg(ga.ga_data);
1538 			    }
1539 			}
1540 		    }
1541 		    ga_clear(&ga);
1542 		}
1543 		break;
1544 
1545 	    // load local variable or argument
1546 	    case ISN_LOAD:
1547 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1548 		    goto failed;
1549 		copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1550 		++ectx.ec_stack.ga_len;
1551 		break;
1552 
1553 	    // load v: variable
1554 	    case ISN_LOADV:
1555 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1556 		    goto failed;
1557 		copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1558 		++ectx.ec_stack.ga_len;
1559 		break;
1560 
1561 	    // load s: variable in Vim9 script
1562 	    case ISN_LOADSCRIPT:
1563 		{
1564 		    scriptref_T	*sref = iptr->isn_arg.script.scriptref;
1565 		    svar_T	 *sv;
1566 
1567 		    sv = get_script_svar(sref, &ectx);
1568 		    if (sv == NULL)
1569 			goto failed;
1570 		    allocate_if_null(sv->sv_tv);
1571 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1572 			goto failed;
1573 		    copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1574 		    ++ectx.ec_stack.ga_len;
1575 		}
1576 		break;
1577 
1578 	    // load s: variable in old script
1579 	    case ISN_LOADS:
1580 		{
1581 		    hashtab_T	*ht = &SCRIPT_VARS(
1582 					       iptr->isn_arg.loadstore.ls_sid);
1583 		    char_u	*name = iptr->isn_arg.loadstore.ls_name;
1584 		    dictitem_T	*di = find_var_in_ht(ht, 0, name, TRUE);
1585 
1586 		    if (di == NULL)
1587 		    {
1588 			SOURCING_LNUM = iptr->isn_lnum;
1589 			semsg(_(e_undefined_variable_str), name);
1590 			goto on_error;
1591 		    }
1592 		    else
1593 		    {
1594 			if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1595 			    goto failed;
1596 			copy_tv(&di->di_tv, STACK_TV_BOT(0));
1597 			++ectx.ec_stack.ga_len;
1598 		    }
1599 		}
1600 		break;
1601 
1602 	    // load g:/b:/w:/t: variable
1603 	    case ISN_LOADG:
1604 	    case ISN_LOADB:
1605 	    case ISN_LOADW:
1606 	    case ISN_LOADT:
1607 		{
1608 		    dictitem_T *di = NULL;
1609 		    hashtab_T *ht = NULL;
1610 		    char namespace;
1611 
1612 		    switch (iptr->isn_type)
1613 		    {
1614 			case ISN_LOADG:
1615 			    ht = get_globvar_ht();
1616 			    namespace = 'g';
1617 			    break;
1618 			case ISN_LOADB:
1619 			    ht = &curbuf->b_vars->dv_hashtab;
1620 			    namespace = 'b';
1621 			    break;
1622 			case ISN_LOADW:
1623 			    ht = &curwin->w_vars->dv_hashtab;
1624 			    namespace = 'w';
1625 			    break;
1626 			case ISN_LOADT:
1627 			    ht = &curtab->tp_vars->dv_hashtab;
1628 			    namespace = 't';
1629 			    break;
1630 			default:  // Cannot reach here
1631 			    goto failed;
1632 		    }
1633 		    di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
1634 
1635 		    if (di == NULL)
1636 		    {
1637 			SOURCING_LNUM = iptr->isn_lnum;
1638 			semsg(_(e_undefined_variable_char_str),
1639 					     namespace, iptr->isn_arg.string);
1640 			goto on_error;
1641 		    }
1642 		    else
1643 		    {
1644 			if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1645 			    goto failed;
1646 			copy_tv(&di->di_tv, STACK_TV_BOT(0));
1647 			++ectx.ec_stack.ga_len;
1648 		    }
1649 		}
1650 		break;
1651 
1652 	    // load autoload variable
1653 	    case ISN_LOADAUTO:
1654 		{
1655 		    char_u *name = iptr->isn_arg.string;
1656 
1657 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1658 			goto failed;
1659 		    SOURCING_LNUM = iptr->isn_lnum;
1660 		    if (eval_variable(name, (int)STRLEN(name),
1661 				  STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1662 			goto on_error;
1663 		    ++ectx.ec_stack.ga_len;
1664 		}
1665 		break;
1666 
1667 	    // load g:/b:/w:/t: namespace
1668 	    case ISN_LOADGDICT:
1669 	    case ISN_LOADBDICT:
1670 	    case ISN_LOADWDICT:
1671 	    case ISN_LOADTDICT:
1672 		{
1673 		    dict_T *d = NULL;
1674 
1675 		    switch (iptr->isn_type)
1676 		    {
1677 			case ISN_LOADGDICT: d = get_globvar_dict(); break;
1678 			case ISN_LOADBDICT: d = curbuf->b_vars; break;
1679 			case ISN_LOADWDICT: d = curwin->w_vars; break;
1680 			case ISN_LOADTDICT: d = curtab->tp_vars; break;
1681 			default:  // Cannot reach here
1682 			    goto failed;
1683 		    }
1684 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1685 			goto failed;
1686 		    tv = STACK_TV_BOT(0);
1687 		    tv->v_type = VAR_DICT;
1688 		    tv->v_lock = 0;
1689 		    tv->vval.v_dict = d;
1690 		    ++ectx.ec_stack.ga_len;
1691 		}
1692 		break;
1693 
1694 	    // load &option
1695 	    case ISN_LOADOPT:
1696 		{
1697 		    typval_T	optval;
1698 		    char_u	*name = iptr->isn_arg.string;
1699 
1700 		    // This is not expected to fail, name is checked during
1701 		    // compilation: don't set SOURCING_LNUM.
1702 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1703 			goto failed;
1704 		    if (eval_option(&name, &optval, TRUE) == FAIL)
1705 			goto failed;
1706 		    *STACK_TV_BOT(0) = optval;
1707 		    ++ectx.ec_stack.ga_len;
1708 		}
1709 		break;
1710 
1711 	    // load $ENV
1712 	    case ISN_LOADENV:
1713 		{
1714 		    typval_T	optval;
1715 		    char_u	*name = iptr->isn_arg.string;
1716 
1717 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1718 			goto failed;
1719 		    // name is always valid, checked when compiling
1720 		    (void)eval_env_var(&name, &optval, TRUE);
1721 		    *STACK_TV_BOT(0) = optval;
1722 		    ++ectx.ec_stack.ga_len;
1723 		}
1724 		break;
1725 
1726 	    // load @register
1727 	    case ISN_LOADREG:
1728 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1729 		    goto failed;
1730 		tv = STACK_TV_BOT(0);
1731 		tv->v_type = VAR_STRING;
1732 		tv->v_lock = 0;
1733 		// This may result in NULL, which should be equivalent to an
1734 		// empty string.
1735 		tv->vval.v_string = get_reg_contents(
1736 					  iptr->isn_arg.number, GREG_EXPR_SRC);
1737 		++ectx.ec_stack.ga_len;
1738 		break;
1739 
1740 	    // store local variable
1741 	    case ISN_STORE:
1742 		--ectx.ec_stack.ga_len;
1743 		tv = STACK_TV_VAR(iptr->isn_arg.number);
1744 		clear_tv(tv);
1745 		*tv = *STACK_TV_BOT(0);
1746 		break;
1747 
1748 	    // store s: variable in old script
1749 	    case ISN_STORES:
1750 		{
1751 		    hashtab_T	*ht = &SCRIPT_VARS(
1752 					       iptr->isn_arg.loadstore.ls_sid);
1753 		    char_u	*name = iptr->isn_arg.loadstore.ls_name;
1754 		    dictitem_T	*di = find_var_in_ht(ht, 0, name + 2, TRUE);
1755 
1756 		    --ectx.ec_stack.ga_len;
1757 		    if (di == NULL)
1758 			store_var(name, STACK_TV_BOT(0));
1759 		    else
1760 		    {
1761 			clear_tv(&di->di_tv);
1762 			di->di_tv = *STACK_TV_BOT(0);
1763 		    }
1764 		}
1765 		break;
1766 
1767 	    // store script-local variable in Vim9 script
1768 	    case ISN_STORESCRIPT:
1769 		{
1770 		    scriptref_T	    *sref = iptr->isn_arg.script.scriptref;
1771 		    svar_T	    *sv;
1772 
1773 		    sv = get_script_svar(sref, &ectx);
1774 		    if (sv == NULL)
1775 			goto failed;
1776 		    --ectx.ec_stack.ga_len;
1777 		    clear_tv(sv->sv_tv);
1778 		    *sv->sv_tv = *STACK_TV_BOT(0);
1779 		}
1780 		break;
1781 
1782 	    // store option
1783 	    case ISN_STOREOPT:
1784 		{
1785 		    long	n = 0;
1786 		    char_u	*s = NULL;
1787 		    char	*msg;
1788 
1789 		    --ectx.ec_stack.ga_len;
1790 		    tv = STACK_TV_BOT(0);
1791 		    if (tv->v_type == VAR_STRING)
1792 		    {
1793 			s = tv->vval.v_string;
1794 			if (s == NULL)
1795 			    s = (char_u *)"";
1796 		    }
1797 		    else
1798 			// must be VAR_NUMBER, CHECKTYPE makes sure
1799 			n = tv->vval.v_number;
1800 		    msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1801 					n, s, iptr->isn_arg.storeopt.so_flags);
1802 		    clear_tv(tv);
1803 		    if (msg != NULL)
1804 		    {
1805 			SOURCING_LNUM = iptr->isn_lnum;
1806 			emsg(_(msg));
1807 			goto on_error;
1808 		    }
1809 		}
1810 		break;
1811 
1812 	    // store $ENV
1813 	    case ISN_STOREENV:
1814 		--ectx.ec_stack.ga_len;
1815 		tv = STACK_TV_BOT(0);
1816 		vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1817 		clear_tv(tv);
1818 		break;
1819 
1820 	    // store @r
1821 	    case ISN_STOREREG:
1822 		{
1823 		    int	reg = iptr->isn_arg.number;
1824 
1825 		    --ectx.ec_stack.ga_len;
1826 		    tv = STACK_TV_BOT(0);
1827 		    write_reg_contents(reg == '@' ? '"' : reg,
1828 						 tv_get_string(tv), -1, FALSE);
1829 		    clear_tv(tv);
1830 		}
1831 		break;
1832 
1833 	    // store v: variable
1834 	    case ISN_STOREV:
1835 		--ectx.ec_stack.ga_len;
1836 		if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1837 								       == FAIL)
1838 		    // should not happen, type is checked when compiling
1839 		    goto on_error;
1840 		break;
1841 
1842 	    // store g:/b:/w:/t: variable
1843 	    case ISN_STOREG:
1844 	    case ISN_STOREB:
1845 	    case ISN_STOREW:
1846 	    case ISN_STORET:
1847 		{
1848 		    dictitem_T	*di;
1849 		    hashtab_T	*ht;
1850 		    char_u	*name = iptr->isn_arg.string + 2;
1851 
1852 		    switch (iptr->isn_type)
1853 		    {
1854 			case ISN_STOREG:
1855 			    ht = get_globvar_ht();
1856 			    break;
1857 			case ISN_STOREB:
1858 			    ht = &curbuf->b_vars->dv_hashtab;
1859 			    break;
1860 			case ISN_STOREW:
1861 			    ht = &curwin->w_vars->dv_hashtab;
1862 			    break;
1863 			case ISN_STORET:
1864 			    ht = &curtab->tp_vars->dv_hashtab;
1865 			    break;
1866 			default:  // Cannot reach here
1867 			    goto failed;
1868 		    }
1869 
1870 		    --ectx.ec_stack.ga_len;
1871 		    di = find_var_in_ht(ht, 0, name, TRUE);
1872 		    if (di == NULL)
1873 			store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
1874 		    else
1875 		    {
1876 			SOURCING_LNUM = iptr->isn_lnum;
1877 			if (var_check_permission(di, name) == FAIL)
1878 			    goto on_error;
1879 			clear_tv(&di->di_tv);
1880 			di->di_tv = *STACK_TV_BOT(0);
1881 		    }
1882 		}
1883 		break;
1884 
1885 	    // store an autoload variable
1886 	    case ISN_STOREAUTO:
1887 		SOURCING_LNUM = iptr->isn_lnum;
1888 		set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1889 		clear_tv(STACK_TV_BOT(-1));
1890 		--ectx.ec_stack.ga_len;
1891 		break;
1892 
1893 	    // store number in local variable
1894 	    case ISN_STORENR:
1895 		tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
1896 		clear_tv(tv);
1897 		tv->v_type = VAR_NUMBER;
1898 		tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
1899 		break;
1900 
1901 	    // store value in list or dict variable
1902 	    case ISN_STOREINDEX:
1903 		{
1904 		    vartype_T	dest_type = iptr->isn_arg.vartype;
1905 		    typval_T	*tv_idx = STACK_TV_BOT(-2);
1906 		    typval_T	*tv_dest = STACK_TV_BOT(-1);
1907 		    int		status = OK;
1908 
1909 		    // Stack contains:
1910 		    // -3 value to be stored
1911 		    // -2 index
1912 		    // -1 dict or list
1913 		    tv = STACK_TV_BOT(-3);
1914 		    SOURCING_LNUM = iptr->isn_lnum;
1915 		    if (dest_type == VAR_ANY)
1916 		    {
1917 			dest_type = tv_dest->v_type;
1918 			if (dest_type == VAR_DICT)
1919 			    status = do_2string(tv_idx, TRUE);
1920 			else if (dest_type == VAR_LIST
1921 					       && tv_idx->v_type != VAR_NUMBER)
1922 			{
1923 			    emsg(_(e_number_exp));
1924 			    status = FAIL;
1925 			}
1926 		    }
1927 		    else if (dest_type != tv_dest->v_type)
1928 		    {
1929 			// just in case, should be OK
1930 			semsg(_(e_expected_str_but_got_str),
1931 				    vartype_name(dest_type),
1932 				    vartype_name(tv_dest->v_type));
1933 			status = FAIL;
1934 		    }
1935 
1936 		    if (status == OK && dest_type == VAR_LIST)
1937 		    {
1938 			long	    lidx = (long)tv_idx->vval.v_number;
1939 			list_T	    *list = tv_dest->vval.v_list;
1940 
1941 			if (list == NULL)
1942 			{
1943 			    emsg(_(e_list_not_set));
1944 			    goto on_error;
1945 			}
1946 			if (lidx < 0 && list->lv_len + lidx >= 0)
1947 			    // negative index is relative to the end
1948 			    lidx = list->lv_len + lidx;
1949 			if (lidx < 0 || lidx > list->lv_len)
1950 			{
1951 			    semsg(_(e_listidx), lidx);
1952 			    goto on_error;
1953 			}
1954 			if (lidx < list->lv_len)
1955 			{
1956 			    listitem_T *li = list_find(list, lidx);
1957 
1958 			    if (error_if_locked(li->li_tv.v_lock,
1959 						    e_cannot_change_list_item))
1960 				goto on_error;
1961 			    // overwrite existing list item
1962 			    clear_tv(&li->li_tv);
1963 			    li->li_tv = *tv;
1964 			}
1965 			else
1966 			{
1967 			    if (error_if_locked(list->lv_lock,
1968 							 e_cannot_change_list))
1969 				goto on_error;
1970 			    // append to list, only fails when out of memory
1971 			    if (list_append_tv(list, tv) == FAIL)
1972 				goto failed;
1973 			    clear_tv(tv);
1974 			}
1975 		    }
1976 		    else if (status == OK && dest_type == VAR_DICT)
1977 		    {
1978 			char_u		*key = tv_idx->vval.v_string;
1979 			dict_T		*dict = tv_dest->vval.v_dict;
1980 			dictitem_T	*di;
1981 
1982 			SOURCING_LNUM = iptr->isn_lnum;
1983 			if (dict == NULL)
1984 			{
1985 			    emsg(_(e_dictionary_not_set));
1986 			    goto on_error;
1987 			}
1988 			if (key == NULL)
1989 			    key = (char_u *)"";
1990 			di = dict_find(dict, key, -1);
1991 			if (di != NULL)
1992 			{
1993 			    if (error_if_locked(di->di_tv.v_lock,
1994 						    e_cannot_change_dict_item))
1995 				goto on_error;
1996 			    // overwrite existing value
1997 			    clear_tv(&di->di_tv);
1998 			    di->di_tv = *tv;
1999 			}
2000 			else
2001 			{
2002 			    if (error_if_locked(dict->dv_lock,
2003 							 e_cannot_change_dict))
2004 				goto on_error;
2005 			    // add to dict, only fails when out of memory
2006 			    if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2007 				goto failed;
2008 			    clear_tv(tv);
2009 			}
2010 		    }
2011 		    else
2012 		    {
2013 			status = FAIL;
2014 			semsg(_(e_cannot_index_str), vartype_name(dest_type));
2015 		    }
2016 
2017 		    clear_tv(tv_idx);
2018 		    clear_tv(tv_dest);
2019 		    ectx.ec_stack.ga_len -= 3;
2020 		    if (status == FAIL)
2021 		    {
2022 			clear_tv(tv);
2023 			goto on_error;
2024 		    }
2025 		}
2026 		break;
2027 
2028 	    // load or store variable or argument from outer scope
2029 	    case ISN_LOADOUTER:
2030 	    case ISN_STOREOUTER:
2031 		{
2032 		    int		depth = iptr->isn_arg.outer.outer_depth;
2033 		    outer_T	*outer = ectx.ec_outer;
2034 
2035 		    while (depth > 1 && outer != NULL)
2036 		    {
2037 			outer = outer->out_up;
2038 			--depth;
2039 		    }
2040 		    if (outer == NULL)
2041 		    {
2042 			SOURCING_LNUM = iptr->isn_lnum;
2043 			iemsg("LOADOUTER depth more than scope levels");
2044 			goto failed;
2045 		    }
2046 		    tv = ((typval_T *)outer->out_stack->ga_data)
2047 				    + outer->out_frame_idx + STACK_FRAME_SIZE
2048 				    + iptr->isn_arg.outer.outer_idx;
2049 		    if (iptr->isn_type == ISN_LOADOUTER)
2050 		    {
2051 			if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2052 			    goto failed;
2053 			copy_tv(tv, STACK_TV_BOT(0));
2054 			++ectx.ec_stack.ga_len;
2055 		    }
2056 		    else
2057 		    {
2058 			--ectx.ec_stack.ga_len;
2059 			clear_tv(tv);
2060 			*tv = *STACK_TV_BOT(0);
2061 		    }
2062 		}
2063 		break;
2064 
2065 	    // unlet item in list or dict variable
2066 	    case ISN_UNLETINDEX:
2067 		{
2068 		    typval_T	*tv_idx = STACK_TV_BOT(-2);
2069 		    typval_T	*tv_dest = STACK_TV_BOT(-1);
2070 		    int		status = OK;
2071 
2072 		    // Stack contains:
2073 		    // -2 index
2074 		    // -1 dict or list
2075 		    if (tv_dest->v_type == VAR_DICT)
2076 		    {
2077 			// unlet a dict item, index must be a string
2078 			if (tv_idx->v_type != VAR_STRING)
2079 			{
2080 			    SOURCING_LNUM = iptr->isn_lnum;
2081 			    semsg(_(e_expected_str_but_got_str),
2082 					vartype_name(VAR_STRING),
2083 					vartype_name(tv_idx->v_type));
2084 			    status = FAIL;
2085 			}
2086 			else
2087 			{
2088 			    dict_T	*d = tv_dest->vval.v_dict;
2089 			    char_u	*key = tv_idx->vval.v_string;
2090 			    dictitem_T  *di = NULL;
2091 
2092 			    if (key == NULL)
2093 				key = (char_u *)"";
2094 			    if (d != NULL)
2095 				di = dict_find(d, key, (int)STRLEN(key));
2096 			    if (di == NULL)
2097 			    {
2098 				// NULL dict is equivalent to empty dict
2099 				SOURCING_LNUM = iptr->isn_lnum;
2100 				semsg(_(e_dictkey), key);
2101 				status = FAIL;
2102 			    }
2103 			    else
2104 			    {
2105 				// TODO: check for dict or item locked
2106 				dictitem_remove(d, di);
2107 			    }
2108 			}
2109 		    }
2110 		    else if (tv_dest->v_type == VAR_LIST)
2111 		    {
2112 			// unlet a List item, index must be a number
2113 			if (tv_idx->v_type != VAR_NUMBER)
2114 			{
2115 			    SOURCING_LNUM = iptr->isn_lnum;
2116 			    semsg(_(e_expected_str_but_got_str),
2117 					vartype_name(VAR_NUMBER),
2118 					vartype_name(tv_idx->v_type));
2119 			    status = FAIL;
2120 			}
2121 			else
2122 			{
2123 			    list_T	*l = tv_dest->vval.v_list;
2124 			    long	n = (long)tv_idx->vval.v_number;
2125 			    listitem_T	*li = NULL;
2126 
2127 			    li = list_find(l, n);
2128 			    if (li == NULL)
2129 			    {
2130 				SOURCING_LNUM = iptr->isn_lnum;
2131 				semsg(_(e_listidx), n);
2132 				status = FAIL;
2133 			    }
2134 			    else
2135 				// TODO: check for list or item locked
2136 				listitem_remove(l, li);
2137 			}
2138 		    }
2139 		    else
2140 		    {
2141 			status = FAIL;
2142 			semsg(_(e_cannot_index_str),
2143 						vartype_name(tv_dest->v_type));
2144 		    }
2145 
2146 		    clear_tv(tv_idx);
2147 		    clear_tv(tv_dest);
2148 		    ectx.ec_stack.ga_len -= 2;
2149 		    if (status == FAIL)
2150 			goto on_error;
2151 		}
2152 		break;
2153 
2154 	    // push constant
2155 	    case ISN_PUSHNR:
2156 	    case ISN_PUSHBOOL:
2157 	    case ISN_PUSHSPEC:
2158 	    case ISN_PUSHF:
2159 	    case ISN_PUSHS:
2160 	    case ISN_PUSHBLOB:
2161 	    case ISN_PUSHFUNC:
2162 	    case ISN_PUSHCHANNEL:
2163 	    case ISN_PUSHJOB:
2164 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2165 		    goto failed;
2166 		tv = STACK_TV_BOT(0);
2167 		tv->v_lock = 0;
2168 		++ectx.ec_stack.ga_len;
2169 		switch (iptr->isn_type)
2170 		{
2171 		    case ISN_PUSHNR:
2172 			tv->v_type = VAR_NUMBER;
2173 			tv->vval.v_number = iptr->isn_arg.number;
2174 			break;
2175 		    case ISN_PUSHBOOL:
2176 			tv->v_type = VAR_BOOL;
2177 			tv->vval.v_number = iptr->isn_arg.number;
2178 			break;
2179 		    case ISN_PUSHSPEC:
2180 			tv->v_type = VAR_SPECIAL;
2181 			tv->vval.v_number = iptr->isn_arg.number;
2182 			break;
2183 #ifdef FEAT_FLOAT
2184 		    case ISN_PUSHF:
2185 			tv->v_type = VAR_FLOAT;
2186 			tv->vval.v_float = iptr->isn_arg.fnumber;
2187 			break;
2188 #endif
2189 		    case ISN_PUSHBLOB:
2190 			blob_copy(iptr->isn_arg.blob, tv);
2191 			break;
2192 		    case ISN_PUSHFUNC:
2193 			tv->v_type = VAR_FUNC;
2194 			if (iptr->isn_arg.string == NULL)
2195 			    tv->vval.v_string = NULL;
2196 			else
2197 			    tv->vval.v_string =
2198 					     vim_strsave(iptr->isn_arg.string);
2199 			break;
2200 		    case ISN_PUSHCHANNEL:
2201 #ifdef FEAT_JOB_CHANNEL
2202 			tv->v_type = VAR_CHANNEL;
2203 			tv->vval.v_channel = iptr->isn_arg.channel;
2204 			if (tv->vval.v_channel != NULL)
2205 			    ++tv->vval.v_channel->ch_refcount;
2206 #endif
2207 			break;
2208 		    case ISN_PUSHJOB:
2209 #ifdef FEAT_JOB_CHANNEL
2210 			tv->v_type = VAR_JOB;
2211 			tv->vval.v_job = iptr->isn_arg.job;
2212 			if (tv->vval.v_job != NULL)
2213 			    ++tv->vval.v_job->jv_refcount;
2214 #endif
2215 			break;
2216 		    default:
2217 			tv->v_type = VAR_STRING;
2218 			tv->vval.v_string = vim_strsave(
2219 				iptr->isn_arg.string == NULL
2220 					? (char_u *)"" : iptr->isn_arg.string);
2221 		}
2222 		break;
2223 
2224 	    case ISN_UNLET:
2225 		if (do_unlet(iptr->isn_arg.unlet.ul_name,
2226 				       iptr->isn_arg.unlet.ul_forceit) == FAIL)
2227 		    goto on_error;
2228 		break;
2229 	    case ISN_UNLETENV:
2230 		vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2231 		break;
2232 
2233 	    case ISN_LOCKCONST:
2234 		item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2235 		break;
2236 
2237 	    // create a list from items on the stack; uses a single allocation
2238 	    // for the list header and the items
2239 	    case ISN_NEWLIST:
2240 		if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2241 		    goto failed;
2242 		break;
2243 
2244 	    // create a dict from items on the stack
2245 	    case ISN_NEWDICT:
2246 		{
2247 		    int		count = iptr->isn_arg.number;
2248 		    dict_T	*dict = dict_alloc();
2249 		    dictitem_T	*item;
2250 		    char_u	*key;
2251 
2252 		    if (dict == NULL)
2253 			goto failed;
2254 		    for (idx = 0; idx < count; ++idx)
2255 		    {
2256 			// have already checked key type is VAR_STRING
2257 			tv = STACK_TV_BOT(2 * (idx - count));
2258 			// check key is unique
2259 			key = tv->vval.v_string == NULL
2260 					    ? (char_u *)"" : tv->vval.v_string;
2261 			item = dict_find(dict, key, -1);
2262 			if (item != NULL)
2263 			{
2264 			    SOURCING_LNUM = iptr->isn_lnum;
2265 			    semsg(_(e_duplicate_key), key);
2266 			    dict_unref(dict);
2267 			    goto on_error;
2268 			}
2269 			item = dictitem_alloc(key);
2270 			clear_tv(tv);
2271 			if (item == NULL)
2272 			{
2273 			    dict_unref(dict);
2274 			    goto failed;
2275 			}
2276 			item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2277 			item->di_tv.v_lock = 0;
2278 			if (dict_add(dict, item) == FAIL)
2279 			{
2280 			    // can this ever happen?
2281 			    dict_unref(dict);
2282 			    goto failed;
2283 			}
2284 		    }
2285 
2286 		    if (count > 0)
2287 			ectx.ec_stack.ga_len -= 2 * count - 1;
2288 		    else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2289 			goto failed;
2290 		    else
2291 			++ectx.ec_stack.ga_len;
2292 		    tv = STACK_TV_BOT(-1);
2293 		    tv->v_type = VAR_DICT;
2294 		    tv->v_lock = 0;
2295 		    tv->vval.v_dict = dict;
2296 		    ++dict->dv_refcount;
2297 		}
2298 		break;
2299 
2300 	    // call a :def function
2301 	    case ISN_DCALL:
2302 		SOURCING_LNUM = iptr->isn_lnum;
2303 		if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
2304 			      iptr->isn_arg.dfunc.cdf_argcount,
2305 			      &ectx) == FAIL)
2306 		    goto on_error;
2307 		break;
2308 
2309 	    // call a builtin function
2310 	    case ISN_BCALL:
2311 		SOURCING_LNUM = iptr->isn_lnum;
2312 		if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2313 			      iptr->isn_arg.bfunc.cbf_argcount,
2314 			      &ectx) == FAIL)
2315 		    goto on_error;
2316 		break;
2317 
2318 	    // call a funcref or partial
2319 	    case ISN_PCALL:
2320 		{
2321 		    cpfunc_T	*pfunc = &iptr->isn_arg.pfunc;
2322 		    int		r;
2323 		    typval_T	partial_tv;
2324 
2325 		    SOURCING_LNUM = iptr->isn_lnum;
2326 		    if (pfunc->cpf_top)
2327 		    {
2328 			// funcref is above the arguments
2329 			tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2330 		    }
2331 		    else
2332 		    {
2333 			// Get the funcref from the stack.
2334 			--ectx.ec_stack.ga_len;
2335 			partial_tv = *STACK_TV_BOT(0);
2336 			tv = &partial_tv;
2337 		    }
2338 		    r = call_partial(tv, pfunc->cpf_argcount, &ectx);
2339 		    if (tv == &partial_tv)
2340 			clear_tv(&partial_tv);
2341 		    if (r == FAIL)
2342 			goto on_error;
2343 		}
2344 		break;
2345 
2346 	    case ISN_PCALL_END:
2347 		// PCALL finished, arguments have been consumed and replaced by
2348 		// the return value.  Now clear the funcref from the stack,
2349 		// and move the return value in its place.
2350 		--ectx.ec_stack.ga_len;
2351 		clear_tv(STACK_TV_BOT(-1));
2352 		*STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2353 		break;
2354 
2355 	    // call a user defined function or funcref/partial
2356 	    case ISN_UCALL:
2357 		{
2358 		    cufunc_T	*cufunc = &iptr->isn_arg.ufunc;
2359 
2360 		    SOURCING_LNUM = iptr->isn_lnum;
2361 		    if (call_eval_func(cufunc->cuf_name,
2362 				    cufunc->cuf_argcount, &ectx, iptr) == FAIL)
2363 			goto on_error;
2364 		}
2365 		break;
2366 
2367 	    // return from a :def function call
2368 	    case ISN_RETURN_ZERO:
2369 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2370 		    goto failed;
2371 		tv = STACK_TV_BOT(0);
2372 		++ectx.ec_stack.ga_len;
2373 		tv->v_type = VAR_NUMBER;
2374 		tv->vval.v_number = 0;
2375 		tv->v_lock = 0;
2376 		// FALLTHROUGH
2377 
2378 	    case ISN_RETURN:
2379 		{
2380 		    garray_T	*trystack = &ectx.ec_trystack;
2381 		    trycmd_T    *trycmd = NULL;
2382 
2383 		    if (trystack->ga_len > 0)
2384 			trycmd = ((trycmd_T *)trystack->ga_data)
2385 							+ trystack->ga_len - 1;
2386 		    if (trycmd != NULL
2387 				  && trycmd->tcd_frame_idx == ectx.ec_frame_idx
2388 			    && trycmd->tcd_finally_idx != 0)
2389 		    {
2390 			// jump to ":finally"
2391 			ectx.ec_iidx = trycmd->tcd_finally_idx;
2392 			trycmd->tcd_return = TRUE;
2393 		    }
2394 		    else
2395 			goto func_return;
2396 		}
2397 		break;
2398 
2399 	    // push a function reference to a compiled function
2400 	    case ISN_FUNCREF:
2401 		{
2402 		    partial_T   *pt = ALLOC_CLEAR_ONE(partial_T);
2403 		    dfunc_T	*pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2404 					       + iptr->isn_arg.funcref.fr_func;
2405 
2406 		    if (pt == NULL)
2407 			goto failed;
2408 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2409 		    {
2410 			vim_free(pt);
2411 			goto failed;
2412 		    }
2413 		    if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2414 								&ectx) == FAIL)
2415 			goto failed;
2416 
2417 		    tv = STACK_TV_BOT(0);
2418 		    ++ectx.ec_stack.ga_len;
2419 		    tv->vval.v_partial = pt;
2420 		    tv->v_type = VAR_PARTIAL;
2421 		    tv->v_lock = 0;
2422 		}
2423 		break;
2424 
2425 	    // Create a global function from a lambda.
2426 	    case ISN_NEWFUNC:
2427 		{
2428 		    newfunc_T	*newfunc = &iptr->isn_arg.newfunc;
2429 
2430 		    if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
2431 								&ectx) == FAIL)
2432 			goto failed;
2433 		}
2434 		break;
2435 
2436 	    // List functions
2437 	    case ISN_DEF:
2438 		if (iptr->isn_arg.string == NULL)
2439 		    list_functions(NULL);
2440 		else
2441 		{
2442 		    exarg_T ea;
2443 
2444 		    CLEAR_FIELD(ea);
2445 		    ea.cmd = ea.arg = iptr->isn_arg.string;
2446 		    define_function(&ea, NULL);
2447 		}
2448 		break;
2449 
2450 	    // jump if a condition is met
2451 	    case ISN_JUMP:
2452 		{
2453 		    jumpwhen_T	when = iptr->isn_arg.jump.jump_when;
2454 		    int		error = FALSE;
2455 		    int		jump = TRUE;
2456 
2457 		    if (when != JUMP_ALWAYS)
2458 		    {
2459 			tv = STACK_TV_BOT(-1);
2460 			if (when == JUMP_IF_COND_FALSE
2461 				|| when == JUMP_IF_FALSE
2462 				|| when == JUMP_IF_COND_TRUE)
2463 			{
2464 			    SOURCING_LNUM = iptr->isn_lnum;
2465 			    jump = tv_get_bool_chk(tv, &error);
2466 			    if (error)
2467 				goto on_error;
2468 			}
2469 			else
2470 			    jump = tv2bool(tv);
2471 			if (when == JUMP_IF_FALSE
2472 					     || when == JUMP_AND_KEEP_IF_FALSE
2473 					     || when == JUMP_IF_COND_FALSE)
2474 			    jump = !jump;
2475 			if (when == JUMP_IF_FALSE || !jump)
2476 			{
2477 			    // drop the value from the stack
2478 			    clear_tv(tv);
2479 			    --ectx.ec_stack.ga_len;
2480 			}
2481 		    }
2482 		    if (jump)
2483 			ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2484 		}
2485 		break;
2486 
2487 	    // top of a for loop
2488 	    case ISN_FOR:
2489 		{
2490 		    list_T	*list = STACK_TV_BOT(-1)->vval.v_list;
2491 		    typval_T	*idxtv =
2492 				   STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2493 
2494 		    // push the next item from the list
2495 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2496 			goto failed;
2497 		    ++idxtv->vval.v_number;
2498 		    if (list == NULL || idxtv->vval.v_number >= list->lv_len)
2499 			// past the end of the list, jump to "endfor"
2500 			ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2501 		    else if (list->lv_first == &range_list_item)
2502 		    {
2503 			// non-materialized range() list
2504 			tv = STACK_TV_BOT(0);
2505 			tv->v_type = VAR_NUMBER;
2506 			tv->v_lock = 0;
2507 			tv->vval.v_number = list_find_nr(
2508 					     list, idxtv->vval.v_number, NULL);
2509 			++ectx.ec_stack.ga_len;
2510 		    }
2511 		    else
2512 		    {
2513 			listitem_T *li = list_find(list, idxtv->vval.v_number);
2514 
2515 			copy_tv(&li->li_tv, STACK_TV_BOT(0));
2516 			++ectx.ec_stack.ga_len;
2517 		    }
2518 		}
2519 		break;
2520 
2521 	    // start of ":try" block
2522 	    case ISN_TRY:
2523 		{
2524 		    trycmd_T    *trycmd = NULL;
2525 
2526 		    if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
2527 			goto failed;
2528 		    trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2529 						     + ectx.ec_trystack.ga_len;
2530 		    ++ectx.ec_trystack.ga_len;
2531 		    ++trylevel;
2532 		    trycmd->tcd_frame_idx = ectx.ec_frame_idx;
2533 		    trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2534 		    trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
2535 		    trycmd->tcd_caught = FALSE;
2536 		    trycmd->tcd_return = FALSE;
2537 		}
2538 		break;
2539 
2540 	    case ISN_PUSHEXC:
2541 		if (current_exception == NULL)
2542 		{
2543 		    SOURCING_LNUM = iptr->isn_lnum;
2544 		    iemsg("Evaluating catch while current_exception is NULL");
2545 		    goto failed;
2546 		}
2547 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2548 		    goto failed;
2549 		tv = STACK_TV_BOT(0);
2550 		++ectx.ec_stack.ga_len;
2551 		tv->v_type = VAR_STRING;
2552 		tv->v_lock = 0;
2553 		tv->vval.v_string = vim_strsave(
2554 					   (char_u *)current_exception->value);
2555 		break;
2556 
2557 	    case ISN_CATCH:
2558 		{
2559 		    garray_T	*trystack = &ectx.ec_trystack;
2560 
2561 		    if (restore_cmdmod)
2562 		    {
2563 			cmdmod.cmod_filter_regmatch.regprog = NULL;
2564 			undo_cmdmod(&cmdmod);
2565 			cmdmod = save_cmdmod;
2566 			restore_cmdmod = FALSE;
2567 		    }
2568 		    if (trystack->ga_len > 0)
2569 		    {
2570 			trycmd_T    *trycmd = ((trycmd_T *)trystack->ga_data)
2571 							+ trystack->ga_len - 1;
2572 			trycmd->tcd_caught = TRUE;
2573 		    }
2574 		    did_emsg = got_int = did_throw = FALSE;
2575 		    force_abort = need_rethrow = FALSE;
2576 		    catch_exception(current_exception);
2577 		}
2578 		break;
2579 
2580 	    // end of ":try" block
2581 	    case ISN_ENDTRY:
2582 		{
2583 		    garray_T	*trystack = &ectx.ec_trystack;
2584 
2585 		    if (trystack->ga_len > 0)
2586 		    {
2587 			trycmd_T    *trycmd = NULL;
2588 
2589 			--trystack->ga_len;
2590 			--trylevel;
2591 			ectx.ec_in_catch = FALSE;
2592 			trycmd = ((trycmd_T *)trystack->ga_data)
2593 							    + trystack->ga_len;
2594 			if (trycmd->tcd_caught && current_exception != NULL)
2595 			{
2596 			    // discard the exception
2597 			    if (caught_stack == current_exception)
2598 				caught_stack = caught_stack->caught;
2599 			    discard_current_exception();
2600 			}
2601 
2602 			if (trycmd->tcd_return)
2603 			    goto func_return;
2604 		    }
2605 		}
2606 		break;
2607 
2608 	    case ISN_THROW:
2609 		if (ectx.ec_trystack.ga_len == 0 && trylevel == 0
2610 								&& emsg_silent)
2611 		{
2612 		    // throwing an exception while using "silent!" causes the
2613 		    // function to abort but not display an error.
2614 		    tv = STACK_TV_BOT(-1);
2615 		    clear_tv(tv);
2616 		    tv->v_type = VAR_NUMBER;
2617 		    tv->vval.v_number = 0;
2618 		    goto done;
2619 		}
2620 		--ectx.ec_stack.ga_len;
2621 		tv = STACK_TV_BOT(0);
2622 		if (tv->vval.v_string == NULL
2623 				       || *skipwhite(tv->vval.v_string) == NUL)
2624 		{
2625 		    vim_free(tv->vval.v_string);
2626 		    SOURCING_LNUM = iptr->isn_lnum;
2627 		    emsg(_(e_throw_with_empty_string));
2628 		    goto failed;
2629 		}
2630 
2631 		if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2632 		{
2633 		    vim_free(tv->vval.v_string);
2634 		    goto failed;
2635 		}
2636 		did_throw = TRUE;
2637 		break;
2638 
2639 	    // compare with special values
2640 	    case ISN_COMPAREBOOL:
2641 	    case ISN_COMPARESPECIAL:
2642 		{
2643 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2644 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2645 		    varnumber_T arg1 = tv1->vval.v_number;
2646 		    varnumber_T arg2 = tv2->vval.v_number;
2647 		    int		res;
2648 
2649 		    switch (iptr->isn_arg.op.op_type)
2650 		    {
2651 			case EXPR_EQUAL: res = arg1 == arg2; break;
2652 			case EXPR_NEQUAL: res = arg1 != arg2; break;
2653 			default: res = 0; break;
2654 		    }
2655 
2656 		    --ectx.ec_stack.ga_len;
2657 		    tv1->v_type = VAR_BOOL;
2658 		    tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2659 		}
2660 		break;
2661 
2662 	    // Operation with two number arguments
2663 	    case ISN_OPNR:
2664 	    case ISN_COMPARENR:
2665 		{
2666 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2667 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2668 		    varnumber_T arg1 = tv1->vval.v_number;
2669 		    varnumber_T arg2 = tv2->vval.v_number;
2670 		    varnumber_T res;
2671 
2672 		    switch (iptr->isn_arg.op.op_type)
2673 		    {
2674 			case EXPR_MULT: res = arg1 * arg2; break;
2675 			case EXPR_DIV: res = arg1 / arg2; break;
2676 			case EXPR_REM: res = arg1 % arg2; break;
2677 			case EXPR_SUB: res = arg1 - arg2; break;
2678 			case EXPR_ADD: res = arg1 + arg2; break;
2679 
2680 			case EXPR_EQUAL: res = arg1 == arg2; break;
2681 			case EXPR_NEQUAL: res = arg1 != arg2; break;
2682 			case EXPR_GREATER: res = arg1 > arg2; break;
2683 			case EXPR_GEQUAL: res = arg1 >= arg2; break;
2684 			case EXPR_SMALLER: res = arg1 < arg2; break;
2685 			case EXPR_SEQUAL: res = arg1 <= arg2; break;
2686 			default: res = 0; break;
2687 		    }
2688 
2689 		    --ectx.ec_stack.ga_len;
2690 		    if (iptr->isn_type == ISN_COMPARENR)
2691 		    {
2692 			tv1->v_type = VAR_BOOL;
2693 			tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2694 		    }
2695 		    else
2696 			tv1->vval.v_number = res;
2697 		}
2698 		break;
2699 
2700 	    // Computation with two float arguments
2701 	    case ISN_OPFLOAT:
2702 	    case ISN_COMPAREFLOAT:
2703 #ifdef FEAT_FLOAT
2704 		{
2705 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2706 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2707 		    float_T	arg1 = tv1->vval.v_float;
2708 		    float_T	arg2 = tv2->vval.v_float;
2709 		    float_T	res = 0;
2710 		    int		cmp = FALSE;
2711 
2712 		    switch (iptr->isn_arg.op.op_type)
2713 		    {
2714 			case EXPR_MULT: res = arg1 * arg2; break;
2715 			case EXPR_DIV: res = arg1 / arg2; break;
2716 			case EXPR_SUB: res = arg1 - arg2; break;
2717 			case EXPR_ADD: res = arg1 + arg2; break;
2718 
2719 			case EXPR_EQUAL: cmp = arg1 == arg2; break;
2720 			case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2721 			case EXPR_GREATER: cmp = arg1 > arg2; break;
2722 			case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2723 			case EXPR_SMALLER: cmp = arg1 < arg2; break;
2724 			case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2725 			default: cmp = 0; break;
2726 		    }
2727 		    --ectx.ec_stack.ga_len;
2728 		    if (iptr->isn_type == ISN_COMPAREFLOAT)
2729 		    {
2730 			tv1->v_type = VAR_BOOL;
2731 			tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2732 		    }
2733 		    else
2734 			tv1->vval.v_float = res;
2735 		}
2736 #endif
2737 		break;
2738 
2739 	    case ISN_COMPARELIST:
2740 		{
2741 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2742 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2743 		    list_T	*arg1 = tv1->vval.v_list;
2744 		    list_T	*arg2 = tv2->vval.v_list;
2745 		    int		cmp = FALSE;
2746 		    int		ic = iptr->isn_arg.op.op_ic;
2747 
2748 		    switch (iptr->isn_arg.op.op_type)
2749 		    {
2750 			case EXPR_EQUAL: cmp =
2751 				      list_equal(arg1, arg2, ic, FALSE); break;
2752 			case EXPR_NEQUAL: cmp =
2753 				     !list_equal(arg1, arg2, ic, FALSE); break;
2754 			case EXPR_IS: cmp = arg1 == arg2; break;
2755 			case EXPR_ISNOT: cmp = arg1 != arg2; break;
2756 			default: cmp = 0; break;
2757 		    }
2758 		    --ectx.ec_stack.ga_len;
2759 		    clear_tv(tv1);
2760 		    clear_tv(tv2);
2761 		    tv1->v_type = VAR_BOOL;
2762 		    tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2763 		}
2764 		break;
2765 
2766 	    case ISN_COMPAREBLOB:
2767 		{
2768 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2769 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2770 		    blob_T	*arg1 = tv1->vval.v_blob;
2771 		    blob_T	*arg2 = tv2->vval.v_blob;
2772 		    int		cmp = FALSE;
2773 
2774 		    switch (iptr->isn_arg.op.op_type)
2775 		    {
2776 			case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2777 			case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2778 			case EXPR_IS: cmp = arg1 == arg2; break;
2779 			case EXPR_ISNOT: cmp = arg1 != arg2; break;
2780 			default: cmp = 0; break;
2781 		    }
2782 		    --ectx.ec_stack.ga_len;
2783 		    clear_tv(tv1);
2784 		    clear_tv(tv2);
2785 		    tv1->v_type = VAR_BOOL;
2786 		    tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2787 		}
2788 		break;
2789 
2790 		// TODO: handle separately
2791 	    case ISN_COMPARESTRING:
2792 	    case ISN_COMPAREDICT:
2793 	    case ISN_COMPAREFUNC:
2794 	    case ISN_COMPAREANY:
2795 		{
2796 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2797 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2798 		    exprtype_T	exprtype = iptr->isn_arg.op.op_type;
2799 		    int		ic = iptr->isn_arg.op.op_ic;
2800 
2801 		    SOURCING_LNUM = iptr->isn_lnum;
2802 		    typval_compare(tv1, tv2, exprtype, ic);
2803 		    clear_tv(tv2);
2804 		    --ectx.ec_stack.ga_len;
2805 		}
2806 		break;
2807 
2808 	    case ISN_ADDLIST:
2809 	    case ISN_ADDBLOB:
2810 		{
2811 		    typval_T *tv1 = STACK_TV_BOT(-2);
2812 		    typval_T *tv2 = STACK_TV_BOT(-1);
2813 
2814 		    // add two lists or blobs
2815 		    if (iptr->isn_type == ISN_ADDLIST)
2816 			eval_addlist(tv1, tv2);
2817 		    else
2818 			eval_addblob(tv1, tv2);
2819 		    clear_tv(tv2);
2820 		    --ectx.ec_stack.ga_len;
2821 		}
2822 		break;
2823 
2824 	    case ISN_LISTAPPEND:
2825 		{
2826 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2827 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2828 		    list_T	*l = tv1->vval.v_list;
2829 
2830 		    // add an item to a list
2831 		    if (l == NULL)
2832 		    {
2833 			SOURCING_LNUM = iptr->isn_lnum;
2834 			emsg(_(e_cannot_add_to_null_list));
2835 			goto on_error;
2836 		    }
2837 		    if (list_append_tv(l, tv2) == FAIL)
2838 			goto failed;
2839 		    clear_tv(tv2);
2840 		    --ectx.ec_stack.ga_len;
2841 		}
2842 		break;
2843 
2844 	    case ISN_BLOBAPPEND:
2845 		{
2846 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2847 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2848 		    blob_T	*b = tv1->vval.v_blob;
2849 		    int		error = FALSE;
2850 		    varnumber_T n;
2851 
2852 		    // add a number to a blob
2853 		    if (b == NULL)
2854 		    {
2855 			SOURCING_LNUM = iptr->isn_lnum;
2856 			emsg(_(e_cannot_add_to_null_blob));
2857 			goto on_error;
2858 		    }
2859 		    n = tv_get_number_chk(tv2, &error);
2860 		    if (error)
2861 			goto on_error;
2862 		    ga_append(&b->bv_ga, (int)n);
2863 		    --ectx.ec_stack.ga_len;
2864 		}
2865 		break;
2866 
2867 	    // Computation with two arguments of unknown type
2868 	    case ISN_OPANY:
2869 		{
2870 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2871 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2872 		    varnumber_T	n1, n2;
2873 #ifdef FEAT_FLOAT
2874 		    float_T	f1 = 0, f2 = 0;
2875 #endif
2876 		    int		error = FALSE;
2877 
2878 		    if (iptr->isn_arg.op.op_type == EXPR_ADD)
2879 		    {
2880 			if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2881 			{
2882 			    eval_addlist(tv1, tv2);
2883 			    clear_tv(tv2);
2884 			    --ectx.ec_stack.ga_len;
2885 			    break;
2886 			}
2887 			else if (tv1->v_type == VAR_BLOB
2888 						    && tv2->v_type == VAR_BLOB)
2889 			{
2890 			    eval_addblob(tv1, tv2);
2891 			    clear_tv(tv2);
2892 			    --ectx.ec_stack.ga_len;
2893 			    break;
2894 			}
2895 		    }
2896 #ifdef FEAT_FLOAT
2897 		    if (tv1->v_type == VAR_FLOAT)
2898 		    {
2899 			f1 = tv1->vval.v_float;
2900 			n1 = 0;
2901 		    }
2902 		    else
2903 #endif
2904 		    {
2905 			SOURCING_LNUM = iptr->isn_lnum;
2906 			n1 = tv_get_number_chk(tv1, &error);
2907 			if (error)
2908 			    goto on_error;
2909 #ifdef FEAT_FLOAT
2910 			if (tv2->v_type == VAR_FLOAT)
2911 			    f1 = n1;
2912 #endif
2913 		    }
2914 #ifdef FEAT_FLOAT
2915 		    if (tv2->v_type == VAR_FLOAT)
2916 		    {
2917 			f2 = tv2->vval.v_float;
2918 			n2 = 0;
2919 		    }
2920 		    else
2921 #endif
2922 		    {
2923 			n2 = tv_get_number_chk(tv2, &error);
2924 			if (error)
2925 			    goto on_error;
2926 #ifdef FEAT_FLOAT
2927 			if (tv1->v_type == VAR_FLOAT)
2928 			    f2 = n2;
2929 #endif
2930 		    }
2931 #ifdef FEAT_FLOAT
2932 		    // if there is a float on either side the result is a float
2933 		    if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2934 		    {
2935 			switch (iptr->isn_arg.op.op_type)
2936 			{
2937 			    case EXPR_MULT: f1 = f1 * f2; break;
2938 			    case EXPR_DIV:  f1 = f1 / f2; break;
2939 			    case EXPR_SUB:  f1 = f1 - f2; break;
2940 			    case EXPR_ADD:  f1 = f1 + f2; break;
2941 			    default: SOURCING_LNUM = iptr->isn_lnum;
2942 				     emsg(_(e_modulus));
2943 				     goto on_error;
2944 			}
2945 			clear_tv(tv1);
2946 			clear_tv(tv2);
2947 			tv1->v_type = VAR_FLOAT;
2948 			tv1->vval.v_float = f1;
2949 			--ectx.ec_stack.ga_len;
2950 		    }
2951 		    else
2952 #endif
2953 		    {
2954 			switch (iptr->isn_arg.op.op_type)
2955 			{
2956 			    case EXPR_MULT: n1 = n1 * n2; break;
2957 			    case EXPR_DIV:  n1 = num_divide(n1, n2); break;
2958 			    case EXPR_SUB:  n1 = n1 - n2; break;
2959 			    case EXPR_ADD:  n1 = n1 + n2; break;
2960 			    default:	    n1 = num_modulus(n1, n2); break;
2961 			}
2962 			clear_tv(tv1);
2963 			clear_tv(tv2);
2964 			tv1->v_type = VAR_NUMBER;
2965 			tv1->vval.v_number = n1;
2966 			--ectx.ec_stack.ga_len;
2967 		    }
2968 		}
2969 		break;
2970 
2971 	    case ISN_CONCAT:
2972 		{
2973 		    char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2974 		    char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2975 		    char_u *res;
2976 
2977 		    res = concat_str(str1, str2);
2978 		    clear_tv(STACK_TV_BOT(-2));
2979 		    clear_tv(STACK_TV_BOT(-1));
2980 		    --ectx.ec_stack.ga_len;
2981 		    STACK_TV_BOT(-1)->vval.v_string = res;
2982 		}
2983 		break;
2984 
2985 	    case ISN_STRINDEX:
2986 	    case ISN_STRSLICE:
2987 		{
2988 		    int		is_slice = iptr->isn_type == ISN_STRSLICE;
2989 		    varnumber_T	n1 = 0, n2;
2990 		    char_u	*res;
2991 
2992 		    // string index: string is at stack-2, index at stack-1
2993 		    // string slice: string is at stack-3, first index at
2994 		    // stack-2, second index at stack-1
2995 		    if (is_slice)
2996 		    {
2997 			tv = STACK_TV_BOT(-2);
2998 			n1 = tv->vval.v_number;
2999 		    }
3000 
3001 		    tv = STACK_TV_BOT(-1);
3002 		    n2 = tv->vval.v_number;
3003 
3004 		    ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3005 		    tv = STACK_TV_BOT(-1);
3006 		    if (is_slice)
3007 			// Slice: Select the characters from the string
3008 			res = string_slice(tv->vval.v_string, n1, n2, FALSE);
3009 		    else
3010 			// Index: The resulting variable is a string of a
3011 			// single character.  If the index is too big or
3012 			// negative the result is empty.
3013 			res = char_from_string(tv->vval.v_string, n2);
3014 		    vim_free(tv->vval.v_string);
3015 		    tv->vval.v_string = res;
3016 		}
3017 		break;
3018 
3019 	    case ISN_LISTINDEX:
3020 	    case ISN_LISTSLICE:
3021 		{
3022 		    int		is_slice = iptr->isn_type == ISN_LISTSLICE;
3023 		    list_T	*list;
3024 		    varnumber_T	n1, n2;
3025 
3026 		    // list index: list is at stack-2, index at stack-1
3027 		    // list slice: list is at stack-3, indexes at stack-2 and
3028 		    // stack-1
3029 		    tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
3030 		    list = tv->vval.v_list;
3031 
3032 		    tv = STACK_TV_BOT(-1);
3033 		    n1 = n2 = tv->vval.v_number;
3034 		    clear_tv(tv);
3035 
3036 		    if (is_slice)
3037 		    {
3038 			tv = STACK_TV_BOT(-2);
3039 			n1 = tv->vval.v_number;
3040 			clear_tv(tv);
3041 		    }
3042 
3043 		    ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3044 		    tv = STACK_TV_BOT(-1);
3045 		    SOURCING_LNUM = iptr->isn_lnum;
3046 		    if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3047 							     tv, TRUE) == FAIL)
3048 			goto on_error;
3049 		}
3050 		break;
3051 
3052 	    case ISN_ANYINDEX:
3053 	    case ISN_ANYSLICE:
3054 		{
3055 		    int		is_slice = iptr->isn_type == ISN_ANYSLICE;
3056 		    typval_T	*var1, *var2;
3057 		    int		res;
3058 
3059 		    // index: composite is at stack-2, index at stack-1
3060 		    // slice: composite is at stack-3, indexes at stack-2 and
3061 		    // stack-1
3062 		    tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
3063 		    SOURCING_LNUM = iptr->isn_lnum;
3064 		    if (check_can_index(tv, TRUE, TRUE) == FAIL)
3065 			goto on_error;
3066 		    var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3067 		    var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
3068 		    res = eval_index_inner(tv, is_slice, var1, var2,
3069 							FALSE, NULL, -1, TRUE);
3070 		    clear_tv(var1);
3071 		    if (is_slice)
3072 			clear_tv(var2);
3073 		    ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3074 		    if (res == FAIL)
3075 			goto on_error;
3076 		}
3077 		break;
3078 
3079 	    case ISN_SLICE:
3080 		{
3081 		    list_T	*list;
3082 		    int		count = iptr->isn_arg.number;
3083 
3084 		    // type will have been checked to be a list
3085 		    tv = STACK_TV_BOT(-1);
3086 		    list = tv->vval.v_list;
3087 
3088 		    // no error for short list, expect it to be checked earlier
3089 		    if (list != NULL && list->lv_len >= count)
3090 		    {
3091 			list_T	*newlist = list_slice(list,
3092 						      count, list->lv_len - 1);
3093 
3094 			if (newlist != NULL)
3095 			{
3096 			    list_unref(list);
3097 			    tv->vval.v_list = newlist;
3098 			    ++newlist->lv_refcount;
3099 			}
3100 		    }
3101 		}
3102 		break;
3103 
3104 	    case ISN_GETITEM:
3105 		{
3106 		    listitem_T	*li;
3107 		    int		index = iptr->isn_arg.number;
3108 
3109 		    // Get list item: list is at stack-1, push item.
3110 		    // List type and length is checked for when compiling.
3111 		    tv = STACK_TV_BOT(-1);
3112 		    li = list_find(tv->vval.v_list, index);
3113 
3114 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3115 			goto failed;
3116 		    ++ectx.ec_stack.ga_len;
3117 		    copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3118 		}
3119 		break;
3120 
3121 	    case ISN_MEMBER:
3122 		{
3123 		    dict_T	*dict;
3124 		    char_u	*key;
3125 		    dictitem_T	*di;
3126 		    typval_T	temp_tv;
3127 
3128 		    // dict member: dict is at stack-2, key at stack-1
3129 		    tv = STACK_TV_BOT(-2);
3130 		    // no need to check for VAR_DICT, CHECKTYPE will check.
3131 		    dict = tv->vval.v_dict;
3132 
3133 		    tv = STACK_TV_BOT(-1);
3134 		    // no need to check for VAR_STRING, 2STRING will check.
3135 		    key = tv->vval.v_string;
3136 		    if (key == NULL)
3137 			key = (char_u *)"";
3138 
3139 		    if ((di = dict_find(dict, key, -1)) == NULL)
3140 		    {
3141 			SOURCING_LNUM = iptr->isn_lnum;
3142 			semsg(_(e_dictkey), key);
3143 
3144 			// If :silent! is used we will continue, make sure the
3145 			// stack contents makes sense.
3146 			clear_tv(tv);
3147 			--ectx.ec_stack.ga_len;
3148 			tv = STACK_TV_BOT(-1);
3149 			clear_tv(tv);
3150 			tv->v_type = VAR_NUMBER;
3151 			tv->vval.v_number = 0;
3152 			goto on_fatal_error;
3153 		    }
3154 		    clear_tv(tv);
3155 		    --ectx.ec_stack.ga_len;
3156 		    // Clear the dict only after getting the item, to avoid
3157 		    // that it makes the item invalid.
3158 		    tv = STACK_TV_BOT(-1);
3159 		    temp_tv = *tv;
3160 		    copy_tv(&di->di_tv, tv);
3161 		    clear_tv(&temp_tv);
3162 		}
3163 		break;
3164 
3165 	    // dict member with string key
3166 	    case ISN_STRINGMEMBER:
3167 		{
3168 		    dict_T	*dict;
3169 		    dictitem_T	*di;
3170 		    typval_T	temp_tv;
3171 
3172 		    tv = STACK_TV_BOT(-1);
3173 		    if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3174 		    {
3175 			SOURCING_LNUM = iptr->isn_lnum;
3176 			emsg(_(e_dictreq));
3177 			goto on_error;
3178 		    }
3179 		    dict = tv->vval.v_dict;
3180 
3181 		    if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3182 								       == NULL)
3183 		    {
3184 			SOURCING_LNUM = iptr->isn_lnum;
3185 			semsg(_(e_dictkey), iptr->isn_arg.string);
3186 			goto on_error;
3187 		    }
3188 		    // Clear the dict after getting the item, to avoid that it
3189 		    // make the item invalid.
3190 		    temp_tv = *tv;
3191 		    copy_tv(&di->di_tv, tv);
3192 		    clear_tv(&temp_tv);
3193 		}
3194 		break;
3195 
3196 	    case ISN_NEGATENR:
3197 		tv = STACK_TV_BOT(-1);
3198 		if (tv->v_type != VAR_NUMBER
3199 #ifdef FEAT_FLOAT
3200 			&& tv->v_type != VAR_FLOAT
3201 #endif
3202 			)
3203 		{
3204 		    SOURCING_LNUM = iptr->isn_lnum;
3205 		    emsg(_(e_number_exp));
3206 		    goto on_error;
3207 		}
3208 #ifdef FEAT_FLOAT
3209 		if (tv->v_type == VAR_FLOAT)
3210 		    tv->vval.v_float = -tv->vval.v_float;
3211 		else
3212 #endif
3213 		    tv->vval.v_number = -tv->vval.v_number;
3214 		break;
3215 
3216 	    case ISN_CHECKNR:
3217 		{
3218 		    int		error = FALSE;
3219 
3220 		    tv = STACK_TV_BOT(-1);
3221 		    SOURCING_LNUM = iptr->isn_lnum;
3222 		    if (check_not_string(tv) == FAIL)
3223 			goto on_error;
3224 		    (void)tv_get_number_chk(tv, &error);
3225 		    if (error)
3226 			goto on_error;
3227 		}
3228 		break;
3229 
3230 	    case ISN_CHECKTYPE:
3231 		{
3232 		    checktype_T *ct = &iptr->isn_arg.type;
3233 
3234 		    tv = STACK_TV_BOT(ct->ct_off);
3235 		    SOURCING_LNUM = iptr->isn_lnum;
3236 		    if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
3237 			goto on_error;
3238 
3239 		    // number 0 is FALSE, number 1 is TRUE
3240 		    if (tv->v_type == VAR_NUMBER
3241 			    && ct->ct_type->tt_type == VAR_BOOL
3242 			    && (tv->vval.v_number == 0
3243 						|| tv->vval.v_number == 1))
3244 		    {
3245 			tv->v_type = VAR_BOOL;
3246 			tv->vval.v_number = tv->vval.v_number
3247 						      ? VVAL_TRUE : VVAL_FALSE;
3248 		    }
3249 		}
3250 		break;
3251 
3252 	    case ISN_CHECKLEN:
3253 		{
3254 		    int	    min_len = iptr->isn_arg.checklen.cl_min_len;
3255 		    list_T  *list = NULL;
3256 
3257 		    tv = STACK_TV_BOT(-1);
3258 		    if (tv->v_type == VAR_LIST)
3259 			    list = tv->vval.v_list;
3260 		    if (list == NULL || list->lv_len < min_len
3261 			    || (list->lv_len > min_len
3262 					&& !iptr->isn_arg.checklen.cl_more_OK))
3263 		    {
3264 			SOURCING_LNUM = iptr->isn_lnum;
3265 			semsg(_(e_expected_nr_items_but_got_nr),
3266 				     min_len, list == NULL ? 0 : list->lv_len);
3267 			goto on_error;
3268 		    }
3269 		}
3270 		break;
3271 
3272 	    case ISN_SETTYPE:
3273 		{
3274 		    checktype_T *ct = &iptr->isn_arg.type;
3275 
3276 		    tv = STACK_TV_BOT(-1);
3277 		    if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3278 		    {
3279 			free_type(tv->vval.v_dict->dv_type);
3280 			tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3281 		    }
3282 		    else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3283 		    {
3284 			free_type(tv->vval.v_list->lv_type);
3285 			tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3286 		    }
3287 		}
3288 		break;
3289 
3290 	    case ISN_2BOOL:
3291 	    case ISN_COND2BOOL:
3292 		{
3293 		    int n;
3294 		    int error = FALSE;
3295 
3296 		    tv = STACK_TV_BOT(-1);
3297 		    if (iptr->isn_type == ISN_2BOOL)
3298 		    {
3299 			n = tv2bool(tv);
3300 			if (iptr->isn_arg.number)  // invert
3301 			    n = !n;
3302 		    }
3303 		    else
3304 		    {
3305 			SOURCING_LNUM = iptr->isn_lnum;
3306 			n = tv_get_bool_chk(tv, &error);
3307 			if (error)
3308 			    goto on_error;
3309 		    }
3310 		    clear_tv(tv);
3311 		    tv->v_type = VAR_BOOL;
3312 		    tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3313 		}
3314 		break;
3315 
3316 	    case ISN_2STRING:
3317 	    case ISN_2STRING_ANY:
3318 		SOURCING_LNUM = iptr->isn_lnum;
3319 		if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3320 			iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3321 			    goto on_error;
3322 		break;
3323 
3324 	    case ISN_RANGE:
3325 		{
3326 		    exarg_T	ea;
3327 		    char	*errormsg;
3328 
3329 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3330 			goto failed;
3331 		    ++ectx.ec_stack.ga_len;
3332 		    tv = STACK_TV_BOT(-1);
3333 		    ea.line2 = 0;
3334 		    ea.addr_count = 0;
3335 		    ea.addr_type = ADDR_LINES;
3336 		    ea.cmd = iptr->isn_arg.string;
3337 		    ea.skip = FALSE;
3338 		    if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
3339 			goto on_error;
3340 		    if (ea.addr_count == 0)
3341 			tv->vval.v_number = curwin->w_cursor.lnum;
3342 		    else
3343 			tv->vval.v_number = ea.line2;
3344 		}
3345 		break;
3346 
3347 	    case ISN_PUT:
3348 		{
3349 		    int		regname = iptr->isn_arg.put.put_regname;
3350 		    linenr_T	lnum = iptr->isn_arg.put.put_lnum;
3351 		    char_u	*expr = NULL;
3352 		    int		dir = FORWARD;
3353 
3354 		    if (regname == '=')
3355 		    {
3356 			tv = STACK_TV_BOT(-1);
3357 			if (tv->v_type == VAR_STRING)
3358 			    expr = tv->vval.v_string;
3359 			else
3360 			{
3361 			    expr = typval2string(tv, TRUE); // allocates value
3362 			    clear_tv(tv);
3363 			}
3364 			--ectx.ec_stack.ga_len;
3365 		    }
3366 		    if (lnum < -2)
3367 		    {
3368 			// line number was put on the stack by ISN_RANGE
3369 			tv = STACK_TV_BOT(-1);
3370 			curwin->w_cursor.lnum = tv->vval.v_number;
3371 			if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3372 			    dir = BACKWARD;
3373 			--ectx.ec_stack.ga_len;
3374 		    }
3375 		    else if (lnum == -2)
3376 			// :put! above cursor
3377 			dir = BACKWARD;
3378 		    else if (lnum >= 0)
3379 			curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
3380 		    check_cursor();
3381 		    do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3382 		    vim_free(expr);
3383 		}
3384 		break;
3385 
3386 	    case ISN_CMDMOD:
3387 		save_cmdmod = cmdmod;
3388 		restore_cmdmod = TRUE;
3389 		cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3390 		apply_cmdmod(&cmdmod);
3391 		break;
3392 
3393 	    case ISN_CMDMOD_REV:
3394 		// filter regprog is owned by the instruction, don't free it
3395 		cmdmod.cmod_filter_regmatch.regprog = NULL;
3396 		undo_cmdmod(&cmdmod);
3397 		cmdmod = save_cmdmod;
3398 		restore_cmdmod = FALSE;
3399 		break;
3400 
3401 	    case ISN_UNPACK:
3402 		{
3403 		    int		count = iptr->isn_arg.unpack.unp_count;
3404 		    int		semicolon = iptr->isn_arg.unpack.unp_semicolon;
3405 		    list_T	*l;
3406 		    listitem_T	*li;
3407 		    int		i;
3408 
3409 		    // Check there is a valid list to unpack.
3410 		    tv = STACK_TV_BOT(-1);
3411 		    if (tv->v_type != VAR_LIST)
3412 		    {
3413 			SOURCING_LNUM = iptr->isn_lnum;
3414 			emsg(_(e_for_argument_must_be_sequence_of_lists));
3415 			goto on_error;
3416 		    }
3417 		    l = tv->vval.v_list;
3418 		    if (l == NULL
3419 				|| l->lv_len < (semicolon ? count - 1 : count))
3420 		    {
3421 			SOURCING_LNUM = iptr->isn_lnum;
3422 			emsg(_(e_list_value_does_not_have_enough_items));
3423 			goto on_error;
3424 		    }
3425 		    else if (!semicolon && l->lv_len > count)
3426 		    {
3427 			SOURCING_LNUM = iptr->isn_lnum;
3428 			emsg(_(e_list_value_has_more_items_than_targets));
3429 			goto on_error;
3430 		    }
3431 
3432 		    CHECK_LIST_MATERIALIZE(l);
3433 		    if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3434 			goto failed;
3435 		    ectx.ec_stack.ga_len += count - 1;
3436 
3437 		    // Variable after semicolon gets a list with the remaining
3438 		    // items.
3439 		    if (semicolon)
3440 		    {
3441 			list_T	*rem_list =
3442 				  list_alloc_with_items(l->lv_len - count + 1);
3443 
3444 			if (rem_list == NULL)
3445 			    goto failed;
3446 			tv = STACK_TV_BOT(-count);
3447 			tv->vval.v_list = rem_list;
3448 			++rem_list->lv_refcount;
3449 			tv->v_lock = 0;
3450 			li = l->lv_first;
3451 			for (i = 0; i < count - 1; ++i)
3452 			    li = li->li_next;
3453 			for (i = 0; li != NULL; ++i)
3454 			{
3455 			    list_set_item(rem_list, i, &li->li_tv);
3456 			    li = li->li_next;
3457 			}
3458 			--count;
3459 		    }
3460 
3461 		    // Produce the values in reverse order, first item last.
3462 		    li = l->lv_first;
3463 		    for (i = 0; i < count; ++i)
3464 		    {
3465 			tv = STACK_TV_BOT(-i - 1);
3466 			copy_tv(&li->li_tv, tv);
3467 			li = li->li_next;
3468 		    }
3469 
3470 		    list_unref(l);
3471 		}
3472 		break;
3473 
3474 	    case ISN_SHUFFLE:
3475 		{
3476 		    typval_T	tmp_tv;
3477 		    int		item = iptr->isn_arg.shuffle.shfl_item;
3478 		    int		up = iptr->isn_arg.shuffle.shfl_up;
3479 
3480 		    tmp_tv = *STACK_TV_BOT(-item);
3481 		    for ( ; up > 0 && item > 1; --up)
3482 		    {
3483 			*STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3484 			--item;
3485 		    }
3486 		    *STACK_TV_BOT(-item) = tmp_tv;
3487 		}
3488 		break;
3489 
3490 	    case ISN_DROP:
3491 		--ectx.ec_stack.ga_len;
3492 		clear_tv(STACK_TV_BOT(0));
3493 		break;
3494 	}
3495 	continue;
3496 
3497 func_return:
3498 	// Restore previous function. If the frame pointer is where we started
3499 	// then there is none and we are done.
3500 	if (ectx.ec_frame_idx == initial_frame_idx)
3501 	    goto done;
3502 
3503 	if (func_return(&ectx) == FAIL)
3504 	    // only fails when out of memory
3505 	    goto failed;
3506 	continue;
3507 
3508 on_error:
3509 	// Jump here for an error that does not require aborting execution.
3510 	// If "emsg_silent" is set then ignore the error, unless it was set
3511 	// when calling the function.
3512 	if (did_emsg_cumul + did_emsg == did_emsg_before
3513 					   && emsg_silent && did_emsg_def == 0)
3514 	    continue;
3515 on_fatal_error:
3516 	// Jump here for an error that messes up the stack.
3517 	// If we are not inside a try-catch started here, abort execution.
3518 	if (trylevel <= trylevel_at_start)
3519 	    goto failed;
3520     }
3521 
3522 done:
3523     // function finished, get result from the stack.
3524     tv = STACK_TV_BOT(-1);
3525     *rettv = *tv;
3526     tv->v_type = VAR_UNKNOWN;
3527     ret = OK;
3528 
3529 failed:
3530     // When failed need to unwind the call stack.
3531     while (ectx.ec_frame_idx != initial_frame_idx)
3532 	func_return(&ectx);
3533 
3534     // Deal with any remaining closures, they may be in use somewhere.
3535     if (ectx.ec_funcrefs.ga_len > 0)
3536     {
3537 	handle_closure_in_use(&ectx, FALSE);
3538 	ga_clear(&ectx.ec_funcrefs);  // TODO: should not be needed?
3539     }
3540 
3541     estack_pop();
3542     current_sctx = save_current_sctx;
3543 
3544     if (*msg_list != NULL && saved_msg_list != NULL)
3545     {
3546 	msglist_T **plist = saved_msg_list;
3547 
3548 	// Append entries from the current msg_list (uncaught exceptions) to
3549 	// the saved msg_list.
3550 	while (*plist != NULL)
3551 	    plist = &(*plist)->next;
3552 
3553 	*plist = *msg_list;
3554     }
3555     msg_list = saved_msg_list;
3556 
3557     if (restore_cmdmod)
3558     {
3559 	cmdmod.cmod_filter_regmatch.regprog = NULL;
3560 	undo_cmdmod(&cmdmod);
3561 	cmdmod = save_cmdmod;
3562     }
3563     emsg_silent_def = save_emsg_silent_def;
3564     did_emsg_def += save_did_emsg_def;
3565 
3566 failed_early:
3567     // Free all local variables, but not arguments.
3568     for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3569 	clear_tv(STACK_TV(idx));
3570 
3571     vim_free(ectx.ec_stack.ga_data);
3572     vim_free(ectx.ec_trystack.ga_data);
3573 
3574     while (ectx.ec_outer != NULL)
3575     {
3576 	outer_T	    *up = ectx.ec_outer->out_up_is_copy
3577 						? NULL : ectx.ec_outer->out_up;
3578 
3579 	vim_free(ectx.ec_outer);
3580 	ectx.ec_outer = up;
3581     }
3582 
3583     // Not sure if this is necessary.
3584     suppress_errthrow = save_suppress_errthrow;
3585 
3586     if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
3587 	semsg(_(e_unknown_error_while_executing_str),
3588 						   printable_func_name(ufunc));
3589     funcdepth_restore(orig_funcdepth);
3590     return ret;
3591 }
3592 
3593 /*
3594  * ":disassemble".
3595  * We don't really need this at runtime, but we do have tests that require it,
3596  * so always include this.
3597  */
3598     void
3599 ex_disassemble(exarg_T *eap)
3600 {
3601     char_u	*arg = eap->arg;
3602     char_u	*fname;
3603     ufunc_T	*ufunc;
3604     dfunc_T	*dfunc;
3605     isn_T	*instr;
3606     int		current;
3607     int		line_idx = 0;
3608     int		prev_current = 0;
3609     int		is_global = FALSE;
3610 
3611     if (STRNCMP(arg, "<lambda>", 8) == 0)
3612     {
3613 	arg += 8;
3614 	(void)getdigits(&arg);
3615 	fname = vim_strnsave(eap->arg, arg - eap->arg);
3616     }
3617     else
3618 	fname = trans_function_name(&arg, &is_global, FALSE,
3619 		      TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
3620     if (fname == NULL)
3621     {
3622 	semsg(_(e_invarg2), eap->arg);
3623 	return;
3624     }
3625 
3626     ufunc = find_func(fname, is_global, NULL);
3627     if (ufunc == NULL)
3628     {
3629 	char_u *p = untrans_function_name(fname);
3630 
3631 	if (p != NULL)
3632 	    // Try again without making it script-local.
3633 	    ufunc = find_func(p, FALSE, NULL);
3634     }
3635     vim_free(fname);
3636     if (ufunc == NULL)
3637     {
3638 	semsg(_(e_cannot_find_function_str), eap->arg);
3639 	return;
3640     }
3641     if (ufunc->uf_def_status == UF_TO_BE_COMPILED
3642 	    && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3643 	return;
3644     if (ufunc->uf_def_status != UF_COMPILED)
3645     {
3646 	semsg(_(e_function_is_not_compiled_str), eap->arg);
3647 	return;
3648     }
3649     if (ufunc->uf_name_exp != NULL)
3650 	msg((char *)ufunc->uf_name_exp);
3651     else
3652 	msg((char *)ufunc->uf_name);
3653 
3654     dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3655     instr = dfunc->df_instr;
3656     for (current = 0; current < dfunc->df_instr_count; ++current)
3657     {
3658 	isn_T	    *iptr = &instr[current];
3659 	char	    *line;
3660 
3661 	while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3662 	{
3663 	    if (current > prev_current)
3664 	    {
3665 		msg_puts("\n\n");
3666 		prev_current = current;
3667 	    }
3668 	    line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3669 	    if (line != NULL)
3670 		msg(line);
3671 	}
3672 
3673 	switch (iptr->isn_type)
3674 	{
3675 	    case ISN_EXEC:
3676 		smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3677 		break;
3678 	    case ISN_EXECCONCAT:
3679 		smsg("%4d EXECCONCAT %lld", current,
3680 					      (varnumber_T)iptr->isn_arg.number);
3681 		break;
3682 	    case ISN_ECHO:
3683 		{
3684 		    echo_T *echo = &iptr->isn_arg.echo;
3685 
3686 		    smsg("%4d %s %d", current,
3687 			    echo->echo_with_white ? "ECHO" : "ECHON",
3688 			    echo->echo_count);
3689 		}
3690 		break;
3691 	    case ISN_EXECUTE:
3692 		smsg("%4d EXECUTE %lld", current,
3693 					    (varnumber_T)(iptr->isn_arg.number));
3694 		break;
3695 	    case ISN_ECHOMSG:
3696 		smsg("%4d ECHOMSG %lld", current,
3697 					    (varnumber_T)(iptr->isn_arg.number));
3698 		break;
3699 	    case ISN_ECHOERR:
3700 		smsg("%4d ECHOERR %lld", current,
3701 					    (varnumber_T)(iptr->isn_arg.number));
3702 		break;
3703 	    case ISN_LOAD:
3704 		{
3705 		    if (iptr->isn_arg.number < 0)
3706 			smsg("%4d LOAD arg[%lld]", current,
3707 				(varnumber_T)(iptr->isn_arg.number
3708 							  + STACK_FRAME_SIZE));
3709 		    else
3710 			smsg("%4d LOAD $%lld", current,
3711 					  (varnumber_T)(iptr->isn_arg.number));
3712 		}
3713 		break;
3714 	    case ISN_LOADOUTER:
3715 		{
3716 		    if (iptr->isn_arg.number < 0)
3717 			smsg("%4d LOADOUTER level %d arg[%d]", current,
3718 				iptr->isn_arg.outer.outer_depth,
3719 				iptr->isn_arg.outer.outer_idx
3720 							  + STACK_FRAME_SIZE);
3721 		    else
3722 			smsg("%4d LOADOUTER level %d $%d", current,
3723 					      iptr->isn_arg.outer.outer_depth,
3724 					      iptr->isn_arg.outer.outer_idx);
3725 		}
3726 		break;
3727 	    case ISN_LOADV:
3728 		smsg("%4d LOADV v:%s", current,
3729 				       get_vim_var_name(iptr->isn_arg.number));
3730 		break;
3731 	    case ISN_LOADSCRIPT:
3732 		{
3733 		    scriptref_T	*sref = iptr->isn_arg.script.scriptref;
3734 		    scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
3735 		    svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3736 							      + sref->sref_idx;
3737 
3738 		    smsg("%4d LOADSCRIPT %s-%d from %s", current,
3739 					    sv->sv_name,
3740 					    sref->sref_idx,
3741 					    si->sn_name);
3742 		}
3743 		break;
3744 	    case ISN_LOADS:
3745 		{
3746 		    scriptitem_T *si = SCRIPT_ITEM(
3747 					       iptr->isn_arg.loadstore.ls_sid);
3748 
3749 		    smsg("%4d LOADS s:%s from %s", current,
3750 				 iptr->isn_arg.loadstore.ls_name, si->sn_name);
3751 		}
3752 		break;
3753 	    case ISN_LOADAUTO:
3754 		smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3755 		break;
3756 	    case ISN_LOADG:
3757 		smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3758 		break;
3759 	    case ISN_LOADB:
3760 		smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3761 		break;
3762 	    case ISN_LOADW:
3763 		smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3764 		break;
3765 	    case ISN_LOADT:
3766 		smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3767 		break;
3768 	    case ISN_LOADGDICT:
3769 		smsg("%4d LOAD g:", current);
3770 		break;
3771 	    case ISN_LOADBDICT:
3772 		smsg("%4d LOAD b:", current);
3773 		break;
3774 	    case ISN_LOADWDICT:
3775 		smsg("%4d LOAD w:", current);
3776 		break;
3777 	    case ISN_LOADTDICT:
3778 		smsg("%4d LOAD t:", current);
3779 		break;
3780 	    case ISN_LOADOPT:
3781 		smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3782 		break;
3783 	    case ISN_LOADENV:
3784 		smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3785 		break;
3786 	    case ISN_LOADREG:
3787 		smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
3788 		break;
3789 
3790 	    case ISN_STORE:
3791 		if (iptr->isn_arg.number < 0)
3792 		    smsg("%4d STORE arg[%lld]", current,
3793 				      iptr->isn_arg.number + STACK_FRAME_SIZE);
3794 		else
3795 		    smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3796 		break;
3797 	    case ISN_STOREOUTER:
3798 		{
3799 		if (iptr->isn_arg.number < 0)
3800 		    smsg("%4d STOREOUTEr level %d arg[%d]", current,
3801 			    iptr->isn_arg.outer.outer_depth,
3802 			    iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
3803 		else
3804 		    smsg("%4d STOREOUTER level %d $%d", current,
3805 			    iptr->isn_arg.outer.outer_depth,
3806 			    iptr->isn_arg.outer.outer_idx);
3807 		}
3808 		break;
3809 	    case ISN_STOREV:
3810 		smsg("%4d STOREV v:%s", current,
3811 				       get_vim_var_name(iptr->isn_arg.number));
3812 		break;
3813 	    case ISN_STOREAUTO:
3814 		smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3815 		break;
3816 	    case ISN_STOREG:
3817 		smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3818 		break;
3819 	    case ISN_STOREB:
3820 		smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3821 		break;
3822 	    case ISN_STOREW:
3823 		smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3824 		break;
3825 	    case ISN_STORET:
3826 		smsg("%4d STORET %s", current, iptr->isn_arg.string);
3827 		break;
3828 	    case ISN_STORES:
3829 		{
3830 		    scriptitem_T *si = SCRIPT_ITEM(
3831 					       iptr->isn_arg.loadstore.ls_sid);
3832 
3833 		    smsg("%4d STORES %s in %s", current,
3834 				 iptr->isn_arg.loadstore.ls_name, si->sn_name);
3835 		}
3836 		break;
3837 	    case ISN_STORESCRIPT:
3838 		{
3839 		    scriptref_T	*sref = iptr->isn_arg.script.scriptref;
3840 		    scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
3841 		    svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3842 							      + sref->sref_idx;
3843 
3844 		    smsg("%4d STORESCRIPT %s-%d in %s", current,
3845 					     sv->sv_name,
3846 					     sref->sref_idx,
3847 					     si->sn_name);
3848 		}
3849 		break;
3850 	    case ISN_STOREOPT:
3851 		smsg("%4d STOREOPT &%s", current,
3852 					       iptr->isn_arg.storeopt.so_name);
3853 		break;
3854 	    case ISN_STOREENV:
3855 		smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3856 		break;
3857 	    case ISN_STOREREG:
3858 		smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
3859 		break;
3860 	    case ISN_STORENR:
3861 		smsg("%4d STORE %lld in $%d", current,
3862 				iptr->isn_arg.storenr.stnr_val,
3863 				iptr->isn_arg.storenr.stnr_idx);
3864 		break;
3865 
3866 	    case ISN_STOREINDEX:
3867 		switch (iptr->isn_arg.vartype)
3868 		{
3869 		    case VAR_LIST:
3870 			    smsg("%4d STORELIST", current);
3871 			    break;
3872 		    case VAR_DICT:
3873 			    smsg("%4d STOREDICT", current);
3874 			    break;
3875 		    case VAR_ANY:
3876 			    smsg("%4d STOREINDEX", current);
3877 			    break;
3878 		    default: break;
3879 		}
3880 		break;
3881 
3882 	    // constants
3883 	    case ISN_PUSHNR:
3884 		smsg("%4d PUSHNR %lld", current,
3885 					    (varnumber_T)(iptr->isn_arg.number));
3886 		break;
3887 	    case ISN_PUSHBOOL:
3888 	    case ISN_PUSHSPEC:
3889 		smsg("%4d PUSH %s", current,
3890 				   get_var_special_name(iptr->isn_arg.number));
3891 		break;
3892 	    case ISN_PUSHF:
3893 #ifdef FEAT_FLOAT
3894 		smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
3895 #endif
3896 		break;
3897 	    case ISN_PUSHS:
3898 		smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3899 		break;
3900 	    case ISN_PUSHBLOB:
3901 		{
3902 		    char_u	*r;
3903 		    char_u	numbuf[NUMBUFLEN];
3904 		    char_u	*tofree;
3905 
3906 		    r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
3907 		    smsg("%4d PUSHBLOB %s", current, r);
3908 		    vim_free(tofree);
3909 		}
3910 		break;
3911 	    case ISN_PUSHFUNC:
3912 		{
3913 		    char *name = (char *)iptr->isn_arg.string;
3914 
3915 		    smsg("%4d PUSHFUNC \"%s\"", current,
3916 					       name == NULL ? "[none]" : name);
3917 		}
3918 		break;
3919 	    case ISN_PUSHCHANNEL:
3920 #ifdef FEAT_JOB_CHANNEL
3921 		{
3922 		    channel_T *channel = iptr->isn_arg.channel;
3923 
3924 		    smsg("%4d PUSHCHANNEL %d", current,
3925 					 channel == NULL ? 0 : channel->ch_id);
3926 		}
3927 #endif
3928 		break;
3929 	    case ISN_PUSHJOB:
3930 #ifdef FEAT_JOB_CHANNEL
3931 		{
3932 		    typval_T	tv;
3933 		    char_u	*name;
3934 
3935 		    tv.v_type = VAR_JOB;
3936 		    tv.vval.v_job = iptr->isn_arg.job;
3937 		    name = tv_get_string(&tv);
3938 		    smsg("%4d PUSHJOB \"%s\"", current, name);
3939 		}
3940 #endif
3941 		break;
3942 	    case ISN_PUSHEXC:
3943 		smsg("%4d PUSH v:exception", current);
3944 		break;
3945 	    case ISN_UNLET:
3946 		smsg("%4d UNLET%s %s", current,
3947 			iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3948 			iptr->isn_arg.unlet.ul_name);
3949 		break;
3950 	    case ISN_UNLETENV:
3951 		smsg("%4d UNLETENV%s $%s", current,
3952 			iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3953 			iptr->isn_arg.unlet.ul_name);
3954 		break;
3955 	    case ISN_UNLETINDEX:
3956 		smsg("%4d UNLETINDEX", current);
3957 		break;
3958 	    case ISN_LOCKCONST:
3959 		smsg("%4d LOCKCONST", current);
3960 		break;
3961 	    case ISN_NEWLIST:
3962 		smsg("%4d NEWLIST size %lld", current,
3963 					    (varnumber_T)(iptr->isn_arg.number));
3964 		break;
3965 	    case ISN_NEWDICT:
3966 		smsg("%4d NEWDICT size %lld", current,
3967 					    (varnumber_T)(iptr->isn_arg.number));
3968 		break;
3969 
3970 	    // function call
3971 	    case ISN_BCALL:
3972 		{
3973 		    cbfunc_T	*cbfunc = &iptr->isn_arg.bfunc;
3974 
3975 		    smsg("%4d BCALL %s(argc %d)", current,
3976 			    internal_func_name(cbfunc->cbf_idx),
3977 			    cbfunc->cbf_argcount);
3978 		}
3979 		break;
3980 	    case ISN_DCALL:
3981 		{
3982 		    cdfunc_T	*cdfunc = &iptr->isn_arg.dfunc;
3983 		    dfunc_T	*df = ((dfunc_T *)def_functions.ga_data)
3984 							     + cdfunc->cdf_idx;
3985 
3986 		    smsg("%4d DCALL %s(argc %d)", current,
3987 			    df->df_ufunc->uf_name_exp != NULL
3988 				? df->df_ufunc->uf_name_exp
3989 				: df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3990 		}
3991 		break;
3992 	    case ISN_UCALL:
3993 		{
3994 		    cufunc_T	*cufunc = &iptr->isn_arg.ufunc;
3995 
3996 		    smsg("%4d UCALL %s(argc %d)", current,
3997 				       cufunc->cuf_name, cufunc->cuf_argcount);
3998 		}
3999 		break;
4000 	    case ISN_PCALL:
4001 		{
4002 		    cpfunc_T	*cpfunc = &iptr->isn_arg.pfunc;
4003 
4004 		    smsg("%4d PCALL%s (argc %d)", current,
4005 			   cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4006 		}
4007 		break;
4008 	    case ISN_PCALL_END:
4009 		smsg("%4d PCALL end", current);
4010 		break;
4011 	    case ISN_RETURN:
4012 		smsg("%4d RETURN", current);
4013 		break;
4014 	    case ISN_RETURN_ZERO:
4015 		smsg("%4d RETURN 0", current);
4016 		break;
4017 	    case ISN_FUNCREF:
4018 		{
4019 		    funcref_T	*funcref = &iptr->isn_arg.funcref;
4020 		    dfunc_T	*df = ((dfunc_T *)def_functions.ga_data)
4021 							    + funcref->fr_func;
4022 
4023 		    smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
4024 		}
4025 		break;
4026 
4027 	    case ISN_NEWFUNC:
4028 		{
4029 		    newfunc_T	*newfunc = &iptr->isn_arg.newfunc;
4030 
4031 		    smsg("%4d NEWFUNC %s %s", current,
4032 				       newfunc->nf_lambda, newfunc->nf_global);
4033 		}
4034 		break;
4035 
4036 	    case ISN_DEF:
4037 		{
4038 		    char_u *name = iptr->isn_arg.string;
4039 
4040 		    smsg("%4d DEF %s", current,
4041 					   name == NULL ? (char_u *)"" : name);
4042 		}
4043 		break;
4044 
4045 	    case ISN_JUMP:
4046 		{
4047 		    char *when = "?";
4048 
4049 		    switch (iptr->isn_arg.jump.jump_when)
4050 		    {
4051 			case JUMP_ALWAYS:
4052 			    when = "JUMP";
4053 			    break;
4054 			case JUMP_AND_KEEP_IF_TRUE:
4055 			    when = "JUMP_AND_KEEP_IF_TRUE";
4056 			    break;
4057 			case JUMP_IF_FALSE:
4058 			    when = "JUMP_IF_FALSE";
4059 			    break;
4060 			case JUMP_AND_KEEP_IF_FALSE:
4061 			    when = "JUMP_AND_KEEP_IF_FALSE";
4062 			    break;
4063 			case JUMP_IF_COND_FALSE:
4064 			    when = "JUMP_IF_COND_FALSE";
4065 			    break;
4066 			case JUMP_IF_COND_TRUE:
4067 			    when = "JUMP_IF_COND_TRUE";
4068 			    break;
4069 		    }
4070 		    smsg("%4d %s -> %d", current, when,
4071 						iptr->isn_arg.jump.jump_where);
4072 		}
4073 		break;
4074 
4075 	    case ISN_FOR:
4076 		{
4077 		    forloop_T *forloop = &iptr->isn_arg.forloop;
4078 
4079 		    smsg("%4d FOR $%d -> %d", current,
4080 					   forloop->for_idx, forloop->for_end);
4081 		}
4082 		break;
4083 
4084 	    case ISN_TRY:
4085 		{
4086 		    try_T *try = &iptr->isn_arg.try;
4087 
4088 		    smsg("%4d TRY catch -> %d, finally -> %d", current,
4089 					     try->try_catch, try->try_finally);
4090 		}
4091 		break;
4092 	    case ISN_CATCH:
4093 		// TODO
4094 		smsg("%4d CATCH", current);
4095 		break;
4096 	    case ISN_ENDTRY:
4097 		smsg("%4d ENDTRY", current);
4098 		break;
4099 	    case ISN_THROW:
4100 		smsg("%4d THROW", current);
4101 		break;
4102 
4103 	    // expression operations on number
4104 	    case ISN_OPNR:
4105 	    case ISN_OPFLOAT:
4106 	    case ISN_OPANY:
4107 		{
4108 		    char *what;
4109 		    char *ins;
4110 
4111 		    switch (iptr->isn_arg.op.op_type)
4112 		    {
4113 			case EXPR_MULT: what = "*"; break;
4114 			case EXPR_DIV: what = "/"; break;
4115 			case EXPR_REM: what = "%"; break;
4116 			case EXPR_SUB: what = "-"; break;
4117 			case EXPR_ADD: what = "+"; break;
4118 			default:       what = "???"; break;
4119 		    }
4120 		    switch (iptr->isn_type)
4121 		    {
4122 			case ISN_OPNR: ins = "OPNR"; break;
4123 			case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4124 			case ISN_OPANY: ins = "OPANY"; break;
4125 			default: ins = "???"; break;
4126 		    }
4127 		    smsg("%4d %s %s", current, ins, what);
4128 		}
4129 		break;
4130 
4131 	    case ISN_COMPAREBOOL:
4132 	    case ISN_COMPARESPECIAL:
4133 	    case ISN_COMPARENR:
4134 	    case ISN_COMPAREFLOAT:
4135 	    case ISN_COMPARESTRING:
4136 	    case ISN_COMPAREBLOB:
4137 	    case ISN_COMPARELIST:
4138 	    case ISN_COMPAREDICT:
4139 	    case ISN_COMPAREFUNC:
4140 	    case ISN_COMPAREANY:
4141 		   {
4142 		       char *p;
4143 		       char buf[10];
4144 		       char *type;
4145 
4146 		       switch (iptr->isn_arg.op.op_type)
4147 		       {
4148 			   case EXPR_EQUAL:	 p = "=="; break;
4149 			   case EXPR_NEQUAL:    p = "!="; break;
4150 			   case EXPR_GREATER:   p = ">"; break;
4151 			   case EXPR_GEQUAL:    p = ">="; break;
4152 			   case EXPR_SMALLER:   p = "<"; break;
4153 			   case EXPR_SEQUAL:    p = "<="; break;
4154 			   case EXPR_MATCH:	 p = "=~"; break;
4155 			   case EXPR_IS:	 p = "is"; break;
4156 			   case EXPR_ISNOT:	 p = "isnot"; break;
4157 			   case EXPR_NOMATCH:	 p = "!~"; break;
4158 			   default:  p = "???"; break;
4159 		       }
4160 		       STRCPY(buf, p);
4161 		       if (iptr->isn_arg.op.op_ic == TRUE)
4162 			   strcat(buf, "?");
4163 		       switch(iptr->isn_type)
4164 		       {
4165 			   case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4166 			   case ISN_COMPARESPECIAL:
4167 						 type = "COMPARESPECIAL"; break;
4168 			   case ISN_COMPARENR: type = "COMPARENR"; break;
4169 			   case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4170 			   case ISN_COMPARESTRING:
4171 						  type = "COMPARESTRING"; break;
4172 			   case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4173 			   case ISN_COMPARELIST: type = "COMPARELIST"; break;
4174 			   case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4175 			   case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
4176 			   case ISN_COMPAREANY: type = "COMPAREANY"; break;
4177 			   default: type = "???"; break;
4178 		       }
4179 
4180 		       smsg("%4d %s %s", current, type, buf);
4181 		   }
4182 		   break;
4183 
4184 	    case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4185 	    case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4186 
4187 	    // expression operations
4188 	    case ISN_CONCAT: smsg("%4d CONCAT", current); break;
4189 	    case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
4190 	    case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
4191 	    case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
4192 	    case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
4193 	    case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
4194 	    case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
4195 	    case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4196 	    case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
4197 	    case ISN_SLICE: smsg("%4d SLICE %lld",
4198 					 current, iptr->isn_arg.number); break;
4199 	    case ISN_GETITEM: smsg("%4d ITEM %lld",
4200 					 current, iptr->isn_arg.number); break;
4201 	    case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4202 	    case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
4203 						  iptr->isn_arg.string); break;
4204 	    case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4205 
4206 	    case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
4207 	    case ISN_CHECKTYPE:
4208 		  {
4209 		      char *tofree;
4210 
4211 		      smsg("%4d CHECKTYPE %s stack[%d]", current,
4212 			      type_name(iptr->isn_arg.type.ct_type, &tofree),
4213 			      iptr->isn_arg.type.ct_off);
4214 		      vim_free(tofree);
4215 		      break;
4216 		  }
4217 	    case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4218 				iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4219 				iptr->isn_arg.checklen.cl_min_len);
4220 			       break;
4221 	    case ISN_SETTYPE:
4222 		  {
4223 		      char *tofree;
4224 
4225 		      smsg("%4d SETTYPE %s", current,
4226 			      type_name(iptr->isn_arg.type.ct_type, &tofree));
4227 		      vim_free(tofree);
4228 		      break;
4229 		  }
4230 	    case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
4231 	    case ISN_2BOOL: if (iptr->isn_arg.number)
4232 				smsg("%4d INVERT (!val)", current);
4233 			    else
4234 				smsg("%4d 2BOOL (!!val)", current);
4235 			    break;
4236 	    case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
4237 					 (varnumber_T)(iptr->isn_arg.number));
4238 			      break;
4239 	    case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
4240 					 (varnumber_T)(iptr->isn_arg.number));
4241 			      break;
4242 	    case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4243 			    break;
4244 	    case ISN_PUT:
4245 	        if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4246 		    smsg("%4d PUT %c above range",
4247 				       current, iptr->isn_arg.put.put_regname);
4248 		else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4249 		    smsg("%4d PUT %c range",
4250 				       current, iptr->isn_arg.put.put_regname);
4251 		else
4252 		    smsg("%4d PUT %c %ld", current,
4253 						 iptr->isn_arg.put.put_regname,
4254 					     (long)iptr->isn_arg.put.put_lnum);
4255 		break;
4256 
4257 		// TODO: summarize modifiers
4258 	    case ISN_CMDMOD:
4259 		{
4260 		    char_u  *buf;
4261 		    size_t  len = produce_cmdmods(
4262 				  NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4263 
4264 		    buf = alloc(len + 1);
4265 		    if (buf != NULL)
4266 		    {
4267 			(void)produce_cmdmods(
4268 				   buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4269 			smsg("%4d CMDMOD %s", current, buf);
4270 			vim_free(buf);
4271 		    }
4272 		    break;
4273 		}
4274 	    case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
4275 
4276 	    case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4277 			iptr->isn_arg.unpack.unp_count,
4278 			iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4279 			      break;
4280 	    case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4281 					 iptr->isn_arg.shuffle.shfl_item,
4282 					 iptr->isn_arg.shuffle.shfl_up);
4283 			      break;
4284 	    case ISN_DROP: smsg("%4d DROP", current); break;
4285 	}
4286 
4287 	out_flush();	    // output one line at a time
4288 	ui_breakcheck();
4289 	if (got_int)
4290 	    break;
4291     }
4292 }
4293 
4294 /*
4295  * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
4296  * list, etc.  Mostly like what JavaScript does, except that empty list and
4297  * empty dictionary are FALSE.
4298  */
4299     int
4300 tv2bool(typval_T *tv)
4301 {
4302     switch (tv->v_type)
4303     {
4304 	case VAR_NUMBER:
4305 	    return tv->vval.v_number != 0;
4306 	case VAR_FLOAT:
4307 #ifdef FEAT_FLOAT
4308 	    return tv->vval.v_float != 0.0;
4309 #else
4310 	    break;
4311 #endif
4312 	case VAR_PARTIAL:
4313 	    return tv->vval.v_partial != NULL;
4314 	case VAR_FUNC:
4315 	case VAR_STRING:
4316 	    return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4317 	case VAR_LIST:
4318 	    return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4319 	case VAR_DICT:
4320 	    return tv->vval.v_dict != NULL
4321 				    && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4322 	case VAR_BOOL:
4323 	case VAR_SPECIAL:
4324 	    return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4325 	case VAR_JOB:
4326 #ifdef FEAT_JOB_CHANNEL
4327 	    return tv->vval.v_job != NULL;
4328 #else
4329 	    break;
4330 #endif
4331 	case VAR_CHANNEL:
4332 #ifdef FEAT_JOB_CHANNEL
4333 	    return tv->vval.v_channel != NULL;
4334 #else
4335 	    break;
4336 #endif
4337 	case VAR_BLOB:
4338 	    return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4339 	case VAR_UNKNOWN:
4340 	case VAR_ANY:
4341 	case VAR_VOID:
4342 	    break;
4343     }
4344     return FALSE;
4345 }
4346 
4347     void
4348 emsg_using_string_as(typval_T *tv, int as_number)
4349 {
4350     semsg(_(as_number ? e_using_string_as_number_str
4351 						 : e_using_string_as_bool_str),
4352 		       tv->vval.v_string == NULL
4353 					   ? (char_u *)"" : tv->vval.v_string);
4354 }
4355 
4356 /*
4357  * If "tv" is a string give an error and return FAIL.
4358  */
4359     int
4360 check_not_string(typval_T *tv)
4361 {
4362     if (tv->v_type == VAR_STRING)
4363     {
4364 	emsg_using_string_as(tv, TRUE);
4365 	clear_tv(tv);
4366 	return FAIL;
4367     }
4368     return OK;
4369 }
4370 
4371 
4372 #endif // FEAT_EVAL
4373