xref: /vim-8.2.3635/src/vim9execute.c (revision 53f7fccc)
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 #if defined(__GNUC__) || defined(__clang__)
26 # define likely(x)    __builtin_expect((x), 1)
27 # define unlikely(x)  __builtin_expect((x), 0)
28 #else
29 # define unlikely(x)  (x)
30 # define likely(x)    (x)
31 #endif
32 
33 // Structure put on ec_trystack when ISN_TRY is encountered.
34 typedef struct {
35     int	    tcd_frame_idx;	// ec_frame_idx at ISN_TRY
36     int	    tcd_stack_len;	// size of ectx.ec_stack at ISN_TRY
37     int	    tcd_in_catch;	// in catch or finally block
38     int	    tcd_did_throw;	// set did_throw in :endtry
39     int	    tcd_catch_idx;	// instruction of the first :catch or :finally
40     int	    tcd_finally_idx;	// instruction of the :finally block or zero
41     int	    tcd_endtry_idx;	// instruction of the :endtry
42     int	    tcd_caught;		// catch block entered
43     int	    tcd_cont;		// :continue encountered, jump here (minus one)
44     int	    tcd_return;		// when TRUE return from end of :finally
45 } trycmd_T;
46 
47 // Data local to a function.
48 // On a function call, if not empty, is saved on the stack and restored when
49 // returning.
50 typedef struct {
51     int		floc_restore_cmdmod;
52     cmdmod_T	floc_save_cmdmod;
53     int		floc_restore_cmdmod_stacklen;
54 } funclocal_T;
55 
56 // Structure to hold a reference to an outer_T, with information of whether it
57 // was allocated.
58 typedef struct {
59     outer_T	*or_outer;
60     partial_T	*or_partial;	// decrement "or_partial->pt_refcount" later
61     int		or_outer_allocated;  // free "or_outer" later
62 } outer_ref_T;
63 
64 // A stack is used to store:
65 // - arguments passed to a :def function
66 // - info about the calling function, to use when returning
67 // - local variables
68 // - temporary values
69 //
70 // In detail (FP == Frame Pointer):
71 //	  arg1		first argument from caller (if present)
72 //	  arg2		second argument from caller (if present)
73 //	  extra_arg1	any missing optional argument default value
74 // FP ->  cur_func	calling function
75 //        current	previous instruction pointer
76 //        frame_ptr	previous Frame Pointer
77 //        var1		space for local variable
78 //        var2		space for local variable
79 //        ....		fixed space for max. number of local variables
80 //        temp		temporary values
81 //        ....		flexible space for temporary values (can grow big)
82 
83 /*
84  * Execution context.
85  */
86 struct ectx_S {
87     garray_T	ec_stack;	// stack of typval_T values
88     int		ec_frame_idx;	// index in ec_stack: context of ec_dfunc_idx
89     int		ec_initial_frame_idx;	// frame index when called
90 
91     outer_ref_T	*ec_outer_ref;	// outer scope used for closures, allocated
92     funclocal_T ec_funclocal;
93 
94     garray_T	ec_trystack;	// stack of trycmd_T values
95 
96     int		ec_dfunc_idx;	// current function index
97     isn_T	*ec_instr;	// array with instructions
98     int		ec_iidx;	// index in ec_instr: instruction to execute
99 
100     garray_T	ec_funcrefs;	// partials that might be a closure
101 
102     int		ec_did_emsg_before;
103     int		ec_trylevel_at_start;
104     where_T	ec_where;
105 };
106 
107 #ifdef FEAT_PROFILE
108 // stack of profinfo_T used when profiling.
109 static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
110 #endif
111 
112 // Get pointer to item relative to the bottom of the stack, -1 is the last one.
113 #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
114 
115     void
116 to_string_error(vartype_T vartype)
117 {
118     semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
119 }
120 
121 /*
122  * Return the number of arguments, including optional arguments and any vararg.
123  */
124     static int
125 ufunc_argcount(ufunc_T *ufunc)
126 {
127     return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
128 }
129 
130 /*
131  * Create a new list from "count" items at the bottom of the stack.
132  * When "count" is zero an empty list is added to the stack.
133  */
134     static int
135 exe_newlist(int count, ectx_T *ectx)
136 {
137     list_T	*list = list_alloc_with_items(count);
138     int		idx;
139     typval_T	*tv;
140 
141     if (list == NULL)
142 	return FAIL;
143     for (idx = 0; idx < count; ++idx)
144 	list_set_item(list, idx, STACK_TV_BOT(idx - count));
145 
146     if (count > 0)
147 	ectx->ec_stack.ga_len -= count - 1;
148     else if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
149 	return FAIL;
150     else
151 	++ectx->ec_stack.ga_len;
152     tv = STACK_TV_BOT(-1);
153     tv->v_type = VAR_LIST;
154     tv->vval.v_list = list;
155     ++list->lv_refcount;
156     return OK;
157 }
158 
159 /*
160  * If debug_tick changed check if "ufunc" has a breakpoint and update
161  * "uf_has_breakpoint".
162  */
163     static void
164 update_has_breakpoint(ufunc_T *ufunc)
165 {
166     if (ufunc->uf_debug_tick != debug_tick)
167     {
168 	linenr_T breakpoint;
169 
170 	ufunc->uf_debug_tick = debug_tick;
171 	breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
172 	ufunc->uf_has_breakpoint = breakpoint > 0;
173     }
174 }
175 
176 /*
177  * Call compiled function "cdf_idx" from compiled code.
178  * This adds a stack frame and sets the instruction pointer to the start of the
179  * called function.
180  * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
181  *
182  * Stack has:
183  * - current arguments (already there)
184  * - omitted optional argument (default values) added here
185  * - stack frame:
186  *	- pointer to calling function
187  *	- Index of next instruction in calling function
188  *	- previous frame pointer
189  * - reserved space for local variables
190  */
191     static int
192 call_dfunc(
193 	int		cdf_idx,
194 	partial_T	*pt,
195 	int		argcount_arg,
196 	ectx_T		*ectx)
197 {
198     int		argcount = argcount_arg;
199     dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
200     ufunc_T	*ufunc = dfunc->df_ufunc;
201     int		did_emsg_before = did_emsg_cumul + did_emsg;
202     int		arg_to_add;
203     int		vararg_count = 0;
204     int		varcount;
205     int		idx;
206     estack_T	*entry;
207     funclocal_T	*floc = NULL;
208     int		res = OK;
209 
210     if (dfunc->df_deleted)
211     {
212 	// don't use ufunc->uf_name, it may have been freed
213 	emsg_funcname(e_func_deleted,
214 		dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
215 	return FAIL;
216     }
217 
218 #ifdef FEAT_PROFILE
219     if (do_profiling == PROF_YES)
220     {
221 	if (likely(ga_grow(&profile_info_ga, 1) == OK))
222 	{
223 	    profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
224 						      + profile_info_ga.ga_len;
225 	    ++profile_info_ga.ga_len;
226 	    CLEAR_POINTER(info);
227 	    profile_may_start_func(info, ufunc,
228 			(((dfunc_T *)def_functions.ga_data)
229 					      + ectx->ec_dfunc_idx)->df_ufunc);
230 	}
231     }
232 #endif
233 
234     // Update uf_has_breakpoint if needed.
235     update_has_breakpoint(ufunc);
236 
237     // When debugging and using "cont" switches to the not-debugged
238     // instructions, may need to still compile them.
239     if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
240     {
241 	res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
242 
243 	// compile_def_function() may cause def_functions.ga_data to change
244 	dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
245     }
246     if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
247     {
248 	if (did_emsg_cumul + did_emsg == did_emsg_before)
249 	    semsg(_(e_function_is_not_compiled_str),
250 						   printable_func_name(ufunc));
251 	return FAIL;
252     }
253 
254     if (ufunc->uf_va_name != NULL)
255     {
256 	// Need to make a list out of the vararg arguments.
257 	// Stack at time of call with 2 varargs:
258 	//   normal_arg
259 	//   optional_arg
260 	//   vararg_1
261 	//   vararg_2
262 	// After creating the list:
263 	//   normal_arg
264 	//   optional_arg
265 	//   vararg-list
266 	// With missing optional arguments we get:
267 	//    normal_arg
268 	// After creating the list
269 	//    normal_arg
270 	//    (space for optional_arg)
271 	//    vararg-list
272 	vararg_count = argcount - ufunc->uf_args.ga_len;
273 	if (vararg_count < 0)
274 	    vararg_count = 0;
275 	else
276 	    argcount -= vararg_count;
277 	if (exe_newlist(vararg_count, ectx) == FAIL)
278 	    return FAIL;
279 
280 	vararg_count = 1;
281     }
282 
283     arg_to_add = ufunc->uf_args.ga_len - argcount;
284     if (arg_to_add < 0)
285     {
286 	if (arg_to_add == -1)
287 	    emsg(_(e_one_argument_too_many));
288 	else
289 	    semsg(_(e_nr_arguments_too_many), -arg_to_add);
290 	return FAIL;
291     }
292 
293     // Reserve space for:
294     // - missing arguments
295     // - stack frame
296     // - local variables
297     // - if needed: a counter for number of closures created in
298     //   ectx->ec_funcrefs.
299     varcount = dfunc->df_varcount + dfunc->df_has_closure;
300     if (unlikely(ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE
301 							  + varcount) == FAIL))
302 	return FAIL;
303 
304     // If depth of calling is getting too high, don't execute the function.
305     if (funcdepth_increment() == FAIL)
306 	return FAIL;
307     ++ex_nesting_level;
308 
309     // Only make a copy of funclocal if it contains something to restore.
310     if (ectx->ec_funclocal.floc_restore_cmdmod)
311     {
312 	floc = ALLOC_ONE(funclocal_T);
313 	if (floc == NULL)
314 	    return FAIL;
315 	*floc = ectx->ec_funclocal;
316 	ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
317     }
318 
319     // Move the vararg-list to below the missing optional arguments.
320     if (vararg_count > 0 && arg_to_add > 0)
321 	*STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
322 
323     // Reserve space for omitted optional arguments, filled in soon.
324     for (idx = 0; idx < arg_to_add; ++idx)
325 	STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
326     ectx->ec_stack.ga_len += arg_to_add;
327 
328     // Store current execution state in stack frame for ISN_RETURN.
329     STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
330     STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
331     STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
332     STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
333 						    (void *)ectx->ec_outer_ref;
334     STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
335     STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
336     ectx->ec_frame_idx = ectx->ec_stack.ga_len;
337 
338     // Initialize local variables
339     for (idx = 0; idx < dfunc->df_varcount; ++idx)
340 	STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
341     if (dfunc->df_has_closure)
342     {
343 	typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
344 
345 	tv->v_type = VAR_NUMBER;
346 	tv->vval.v_number = 0;
347     }
348     ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
349 
350     if (pt != NULL || ufunc->uf_partial != NULL
351 					     || (ufunc->uf_flags & FC_CLOSURE))
352     {
353 	outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
354 
355 	if (ref == NULL)
356 	    return FAIL;
357 	if (pt != NULL)
358 	{
359 	    ref->or_outer = &pt->pt_outer;
360 	    ++pt->pt_refcount;
361 	    ref->or_partial = pt;
362 	}
363 	else if (ufunc->uf_partial != NULL)
364 	{
365 	    ref->or_outer = &ufunc->uf_partial->pt_outer;
366 	    ++ufunc->uf_partial->pt_refcount;
367 	    ref->or_partial = ufunc->uf_partial;
368 	}
369 	else
370 	{
371 	    ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
372 	    if (unlikely(ref->or_outer == NULL))
373 	    {
374 		vim_free(ref);
375 		return FAIL;
376 	    }
377 	    ref->or_outer_allocated = TRUE;
378 	    ref->or_outer->out_stack = &ectx->ec_stack;
379 	    ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
380 	    if (ectx->ec_outer_ref != NULL)
381 		ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
382 	}
383 	ectx->ec_outer_ref = ref;
384     }
385     else
386 	ectx->ec_outer_ref = NULL;
387 
388     ++ufunc->uf_calls;
389 
390     // Set execution state to the start of the called function.
391     ectx->ec_dfunc_idx = cdf_idx;
392     ectx->ec_instr = INSTRUCTIONS(dfunc);
393     entry = estack_push_ufunc(ufunc, 1);
394     if (entry != NULL)
395     {
396 	// Set the script context to the script where the function was defined.
397 	// Save the current context so it can be restored on return.
398 	entry->es_save_sctx = current_sctx;
399 	current_sctx = ufunc->uf_script_ctx;
400     }
401 
402     // Start execution at the first instruction.
403     ectx->ec_iidx = 0;
404 
405     return OK;
406 }
407 
408 // Get pointer to item in the stack.
409 #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
410 
411 /*
412  * Used when returning from a function: Check if any closure is still
413  * referenced.  If so then move the arguments and variables to a separate piece
414  * of stack to be used when the closure is called.
415  * When "free_arguments" is TRUE the arguments are to be freed.
416  * Returns FAIL when out of memory.
417  */
418     static int
419 handle_closure_in_use(ectx_T *ectx, int free_arguments)
420 {
421     dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
422 							  + ectx->ec_dfunc_idx;
423     int		argcount;
424     int		top;
425     int		idx;
426     typval_T	*tv;
427     int		closure_in_use = FALSE;
428     garray_T	*gap = &ectx->ec_funcrefs;
429     varnumber_T	closure_count;
430 
431     if (dfunc->df_ufunc == NULL)
432 	return OK;  // function was freed
433     if (dfunc->df_has_closure == 0)
434 	return OK;  // no closures
435     tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
436     closure_count = tv->vval.v_number;
437     if (closure_count == 0)
438 	return OK;  // no funcrefs created
439 
440     argcount = ufunc_argcount(dfunc->df_ufunc);
441     top = ectx->ec_frame_idx - argcount;
442 
443     // Check if any created closure is still in use.
444     for (idx = 0; idx < closure_count; ++idx)
445     {
446 	partial_T   *pt;
447 	int	    off = gap->ga_len - closure_count + idx;
448 
449 	if (off < 0)
450 	    continue;  // count is off or already done
451 	pt = ((partial_T **)gap->ga_data)[off];
452 	if (pt->pt_refcount > 1)
453 	{
454 	    int refcount = pt->pt_refcount;
455 	    int i;
456 
457 	    // A Reference in a local variables doesn't count, it gets
458 	    // unreferenced on return.
459 	    for (i = 0; i < dfunc->df_varcount; ++i)
460 	    {
461 		typval_T *stv = STACK_TV(ectx->ec_frame_idx
462 						       + STACK_FRAME_SIZE + i);
463 		if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
464 		    --refcount;
465 	    }
466 	    if (refcount > 1)
467 	    {
468 		closure_in_use = TRUE;
469 		break;
470 	    }
471 	}
472     }
473 
474     if (closure_in_use)
475     {
476 	funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
477 	typval_T    *stack;
478 
479 	// A closure is using the arguments and/or local variables.
480 	// Move them to the called function.
481 	if (funcstack == NULL)
482 	    return FAIL;
483 	funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
484 	funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
485 	stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
486 	funcstack->fs_ga.ga_data = stack;
487 	if (stack == NULL)
488 	{
489 	    vim_free(funcstack);
490 	    return FAIL;
491 	}
492 
493 	// Move or copy the arguments.
494 	for (idx = 0; idx < argcount; ++idx)
495 	{
496 	    tv = STACK_TV(top + idx);
497 	    if (free_arguments)
498 	    {
499 		*(stack + idx) = *tv;
500 		tv->v_type = VAR_UNKNOWN;
501 	    }
502 	    else
503 		copy_tv(tv, stack + idx);
504 	}
505 	// Move the local variables.
506 	for (idx = 0; idx < dfunc->df_varcount; ++idx)
507 	{
508 	    tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
509 
510 	    // A partial created for a local function, that is also used as a
511 	    // local variable, has a reference count for the variable, thus
512 	    // will never go down to zero.  When all these refcounts are one
513 	    // then the funcstack is unused.  We need to count how many we have
514 	    // so we need when to check.
515 	    if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
516 	    {
517 		int	    i;
518 
519 		for (i = 0; i < closure_count; ++i)
520 		    if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
521 					      gap->ga_len - closure_count + i])
522 			++funcstack->fs_min_refcount;
523 	    }
524 
525 	    *(stack + funcstack->fs_var_offset + idx) = *tv;
526 	    tv->v_type = VAR_UNKNOWN;
527 	}
528 
529 	for (idx = 0; idx < closure_count; ++idx)
530 	{
531 	    partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
532 							- closure_count + idx];
533 	    if (pt->pt_refcount > 1)
534 	    {
535 		++funcstack->fs_refcount;
536 		pt->pt_funcstack = funcstack;
537 		pt->pt_outer.out_stack = &funcstack->fs_ga;
538 		pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
539 	    }
540 	}
541     }
542 
543     for (idx = 0; idx < closure_count; ++idx)
544 	partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
545 						       - closure_count + idx]);
546     gap->ga_len -= closure_count;
547     if (gap->ga_len == 0)
548 	ga_clear(gap);
549 
550     return OK;
551 }
552 
553 /*
554  * Called when a partial is freed or its reference count goes down to one.  The
555  * funcstack may be the only reference to the partials in the local variables.
556  * Go over all of them, the funcref and can be freed if all partials
557  * referencing the funcstack have a reference count of one.
558  */
559     void
560 funcstack_check_refcount(funcstack_T *funcstack)
561 {
562     int		    i;
563     garray_T	    *gap = &funcstack->fs_ga;
564     int		    done = 0;
565 
566     if (funcstack->fs_refcount > funcstack->fs_min_refcount)
567 	return;
568     for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
569     {
570 	typval_T *tv = ((typval_T *)gap->ga_data) + i;
571 
572 	if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
573 		&& tv->vval.v_partial->pt_funcstack == funcstack
574 		&& tv->vval.v_partial->pt_refcount == 1)
575 	    ++done;
576     }
577     if (done == funcstack->fs_min_refcount)
578     {
579 	typval_T	*stack = gap->ga_data;
580 
581 	// All partials referencing the funcstack have a reference count of
582 	// one, thus the funcstack is no longer of use.
583 	for (i = 0; i < gap->ga_len; ++i)
584 	    clear_tv(stack + i);
585 	vim_free(stack);
586 	vim_free(funcstack);
587     }
588 }
589 
590 /*
591  * Return from the current function.
592  */
593     static int
594 func_return(ectx_T *ectx)
595 {
596     int		idx;
597     int		ret_idx;
598     dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
599 							  + ectx->ec_dfunc_idx;
600     int		argcount = ufunc_argcount(dfunc->df_ufunc);
601     int		top = ectx->ec_frame_idx - argcount;
602     estack_T	*entry;
603     int		prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
604 					+ STACK_FRAME_FUNC_OFF)->vval.v_number;
605     funclocal_T	*floc;
606 #ifdef FEAT_PROFILE
607     dfunc_T	*prev_dfunc = ((dfunc_T *)def_functions.ga_data)
608 							      + prev_dfunc_idx;
609 
610     if (do_profiling == PROF_YES)
611     {
612 	ufunc_T *caller = prev_dfunc->df_ufunc;
613 
614 	if (dfunc->df_ufunc->uf_profiling
615 				   || (caller != NULL && caller->uf_profiling))
616 	{
617 	    profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
618 			+ profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
619 	    --profile_info_ga.ga_len;
620 	}
621     }
622 #endif
623     // TODO: when is it safe to delete the function when it is no longer used?
624     --dfunc->df_ufunc->uf_calls;
625 
626     // execution context goes one level up
627     entry = estack_pop();
628     if (entry != NULL)
629 	current_sctx = entry->es_save_sctx;
630 
631     if (handle_closure_in_use(ectx, TRUE) == FAIL)
632 	return FAIL;
633 
634     // Clear the arguments.
635     for (idx = top; idx < ectx->ec_frame_idx; ++idx)
636 	clear_tv(STACK_TV(idx));
637 
638     // Clear local variables and temp values, but not the return value.
639     for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
640 					idx < ectx->ec_stack.ga_len - 1; ++idx)
641 	clear_tv(STACK_TV(idx));
642 
643     // The return value should be on top of the stack.  However, when aborting
644     // it may not be there and ec_frame_idx is the top of the stack.
645     ret_idx = ectx->ec_stack.ga_len - 1;
646     if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
647 	ret_idx = 0;
648 
649     if (ectx->ec_outer_ref != NULL)
650     {
651 	if (ectx->ec_outer_ref->or_outer_allocated)
652 	    vim_free(ectx->ec_outer_ref->or_outer);
653 	partial_unref(ectx->ec_outer_ref->or_partial);
654 	vim_free(ectx->ec_outer_ref);
655     }
656 
657     // Restore the previous frame.
658     ectx->ec_dfunc_idx = prev_dfunc_idx;
659     ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
660 					+ STACK_FRAME_IIDX_OFF)->vval.v_number;
661     ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
662 				       + STACK_FRAME_INSTR_OFF)->vval.v_string;
663     ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
664 				       + STACK_FRAME_OUTER_OFF)->vval.v_string;
665     floc = (void *)STACK_TV(ectx->ec_frame_idx
666 				   + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
667     // restoring ec_frame_idx must be last
668     ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
669 				       + STACK_FRAME_IDX_OFF)->vval.v_number;
670 
671     if (floc == NULL)
672 	ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
673     else
674     {
675 	ectx->ec_funclocal = *floc;
676 	vim_free(floc);
677     }
678 
679     if (ret_idx > 0)
680     {
681 	// Reset the stack to the position before the call, with a spot for the
682 	// return value, moved there from above the frame.
683 	ectx->ec_stack.ga_len = top + 1;
684 	*STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
685     }
686     else
687 	// Reset the stack to the position before the call.
688 	ectx->ec_stack.ga_len = top;
689 
690     funcdepth_decrement();
691     --ex_nesting_level;
692     return OK;
693 }
694 
695 #undef STACK_TV
696 
697 /*
698  * Prepare arguments and rettv for calling a builtin or user function.
699  */
700     static int
701 call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
702 {
703     int		idx;
704     typval_T	*tv;
705 
706     // Move arguments from bottom of the stack to argvars[] and add terminator.
707     for (idx = 0; idx < argcount; ++idx)
708 	argvars[idx] = *STACK_TV_BOT(idx - argcount);
709     argvars[argcount].v_type = VAR_UNKNOWN;
710 
711     // Result replaces the arguments on the stack.
712     if (argcount > 0)
713 	ectx->ec_stack.ga_len -= argcount - 1;
714     else if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
715 	return FAIL;
716     else
717 	++ectx->ec_stack.ga_len;
718 
719     // Default return value is zero.
720     tv = STACK_TV_BOT(-1);
721     tv->v_type = VAR_NUMBER;
722     tv->vval.v_number = 0;
723 
724     return OK;
725 }
726 
727 // Ugly global to avoid passing the execution context around through many
728 // layers.
729 static ectx_T *current_ectx = NULL;
730 
731 /*
732  * Call a builtin function by index.
733  */
734     static int
735 call_bfunc(int func_idx, int argcount, ectx_T *ectx)
736 {
737     typval_T	argvars[MAX_FUNC_ARGS];
738     int		idx;
739     int		did_emsg_before = did_emsg;
740     ectx_T	*prev_ectx = current_ectx;
741     char	*save_func_name = ectx->ec_where.wt_func_name;
742 
743     if (call_prepare(argcount, argvars, ectx) == FAIL)
744 	return FAIL;
745     ectx->ec_where.wt_func_name = internal_func_name(func_idx);
746 
747     // Call the builtin function.  Set "current_ectx" so that when it
748     // recursively invokes call_def_function() a closure context can be set.
749     current_ectx = ectx;
750     call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
751     current_ectx = prev_ectx;
752     ectx->ec_where.wt_func_name = save_func_name;
753 
754     // Clear the arguments.
755     for (idx = 0; idx < argcount; ++idx)
756 	clear_tv(&argvars[idx]);
757 
758     if (did_emsg > did_emsg_before)
759 	return FAIL;
760     return OK;
761 }
762 
763 /*
764  * Execute a user defined function.
765  * If the function is compiled this will add a stack frame and set the
766  * instruction pointer at the start of the function.
767  * Otherwise the function is called here.
768  * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
769  * "iptr" can be used to replace the instruction with a more efficient one.
770  */
771     static int
772 call_ufunc(
773 	ufunc_T	    *ufunc,
774 	partial_T   *pt,
775 	int	    argcount,
776 	ectx_T	    *ectx,
777 	isn_T	    *iptr)
778 {
779     typval_T	argvars[MAX_FUNC_ARGS];
780     funcexe_T   funcexe;
781     int		error;
782     int		idx;
783     int		did_emsg_before = did_emsg;
784     compiletype_T compile_type = COMPILE_TYPE(ufunc);
785 
786     if (func_needs_compiling(ufunc, compile_type)
787 		&& compile_def_function(ufunc, FALSE, compile_type, NULL)
788 								       == FAIL)
789 	return FAIL;
790     if (ufunc->uf_def_status == UF_COMPILED)
791     {
792 	error = check_user_func_argcount(ufunc, argcount);
793 	if (error != FCERR_UNKNOWN)
794 	{
795 	    if (error == FCERR_TOOMANY)
796 		semsg(_(e_toomanyarg), ufunc->uf_name);
797 	    else
798 		semsg(_(e_toofewarg), ufunc->uf_name);
799 	    return FAIL;
800 	}
801 
802 	// The function has been compiled, can call it quickly.  For a function
803 	// that was defined later: we can call it directly next time.
804 	// TODO: what if the function was deleted and then defined again?
805 	if (iptr != NULL)
806 	{
807 	    delete_instr(iptr);
808 	    iptr->isn_type = ISN_DCALL;
809 	    iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
810 	    iptr->isn_arg.dfunc.cdf_argcount = argcount;
811 	}
812 	return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
813     }
814 
815     if (call_prepare(argcount, argvars, ectx) == FAIL)
816 	return FAIL;
817     CLEAR_FIELD(funcexe);
818     funcexe.evaluate = TRUE;
819 
820     // Call the user function.  Result goes in last position on the stack.
821     // TODO: add selfdict if there is one
822     error = call_user_func_check(ufunc, argcount, argvars,
823 					     STACK_TV_BOT(-1), &funcexe, NULL);
824 
825     // Clear the arguments.
826     for (idx = 0; idx < argcount; ++idx)
827 	clear_tv(&argvars[idx]);
828 
829     if (error != FCERR_NONE)
830     {
831 	user_func_error(error, ufunc->uf_name);
832 	return FAIL;
833     }
834     if (did_emsg > did_emsg_before)
835 	// Error other than from calling the function itself.
836 	return FAIL;
837     return OK;
838 }
839 
840 /*
841  * If command modifiers were applied restore them.
842  */
843     static void
844 may_restore_cmdmod(funclocal_T *funclocal)
845 {
846     if (funclocal->floc_restore_cmdmod)
847     {
848 	cmdmod.cmod_filter_regmatch.regprog = NULL;
849 	undo_cmdmod(&cmdmod);
850 	cmdmod = funclocal->floc_save_cmdmod;
851 	funclocal->floc_restore_cmdmod = FALSE;
852     }
853 }
854 
855 /*
856  * Return TRUE if an error was given or CTRL-C was pressed.
857  */
858     static int
859 vim9_aborting(int prev_called_emsg)
860 {
861     return called_emsg > prev_called_emsg || got_int || did_throw;
862 }
863 
864 /*
865  * Execute a function by "name".
866  * This can be a builtin function or a user function.
867  * "iptr" can be used to replace the instruction with a more efficient one.
868  * Returns FAIL if not found without an error message.
869  */
870     static int
871 call_by_name(
872 	char_u	    *name,
873 	int	    argcount,
874 	ectx_T	    *ectx,
875 	isn_T	    *iptr)
876 {
877     ufunc_T *ufunc;
878 
879     if (builtin_function(name, -1))
880     {
881 	int func_idx = find_internal_func(name);
882 
883 	if (func_idx < 0)
884 	    return FAIL;
885 	if (check_internal_func(func_idx, argcount) < 0)
886 	    return FAIL;
887 	return call_bfunc(func_idx, argcount, ectx);
888     }
889 
890     ufunc = find_func(name, FALSE, NULL);
891 
892     if (ufunc == NULL)
893     {
894 	int called_emsg_before = called_emsg;
895 
896 	if (script_autoload(name, TRUE))
897 	    // loaded a package, search for the function again
898 	    ufunc = find_func(name, FALSE, NULL);
899 	if (vim9_aborting(called_emsg_before))
900 	    return FAIL;  // bail out if loading the script caused an error
901     }
902 
903     if (ufunc != NULL)
904     {
905 	if (ufunc->uf_arg_types != NULL)
906 	{
907 	    int i;
908 	    typval_T	*argv = STACK_TV_BOT(0) - argcount;
909 
910 	    // The function can change at runtime, check that the argument
911 	    // types are correct.
912 	    for (i = 0; i < argcount; ++i)
913 	    {
914 		type_T *type = NULL;
915 
916 		if (i < ufunc->uf_args.ga_len)
917 		    type = ufunc->uf_arg_types[i];
918 		else if (ufunc->uf_va_type != NULL)
919 		    type = ufunc->uf_va_type->tt_member;
920 		if (type != NULL && check_typval_arg_type(type,
921 						&argv[i], NULL, i + 1) == FAIL)
922 		    return FAIL;
923 	    }
924 	}
925 
926 	return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
927     }
928 
929     return FAIL;
930 }
931 
932     static int
933 call_partial(
934 	typval_T    *tv,
935 	int	    argcount_arg,
936 	ectx_T	    *ectx)
937 {
938     int		argcount = argcount_arg;
939     char_u	*name = NULL;
940     int		called_emsg_before = called_emsg;
941     int		res = FAIL;
942 
943     if (tv->v_type == VAR_PARTIAL)
944     {
945 	partial_T   *pt = tv->vval.v_partial;
946 	int	    i;
947 
948 	if (pt->pt_argc > 0)
949 	{
950 	    // Make space for arguments from the partial, shift the "argcount"
951 	    // arguments up.
952 	    if (unlikely(ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL))
953 		return FAIL;
954 	    for (i = 1; i <= argcount; ++i)
955 		*STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
956 	    ectx->ec_stack.ga_len += pt->pt_argc;
957 	    argcount += pt->pt_argc;
958 
959 	    // copy the arguments from the partial onto the stack
960 	    for (i = 0; i < pt->pt_argc; ++i)
961 		copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
962 	}
963 
964 	if (pt->pt_func != NULL)
965 	    return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
966 
967 	name = pt->pt_name;
968     }
969     else if (tv->v_type == VAR_FUNC)
970 	name = tv->vval.v_string;
971     if (name != NULL)
972     {
973 	char_u	fname_buf[FLEN_FIXED + 1];
974 	char_u	*tofree = NULL;
975 	int	error = FCERR_NONE;
976 	char_u	*fname;
977 
978 	// May need to translate <SNR>123_ to K_SNR.
979 	fname = fname_trans_sid(name, fname_buf, &tofree, &error);
980 	if (error != FCERR_NONE)
981 	    res = FAIL;
982 	else
983 	    res = call_by_name(fname, argcount, ectx, NULL);
984 	vim_free(tofree);
985     }
986 
987     if (res == FAIL)
988     {
989 	if (called_emsg == called_emsg_before)
990 	    semsg(_(e_unknownfunc),
991 				  name == NULL ? (char_u *)"[unknown]" : name);
992 	return FAIL;
993     }
994     return OK;
995 }
996 
997 /*
998  * Check if "lock" is VAR_LOCKED or VAR_FIXED.  If so give an error and return
999  * TRUE.
1000  */
1001     static int
1002 error_if_locked(int lock, char *error)
1003 {
1004     if (lock & (VAR_LOCKED | VAR_FIXED))
1005     {
1006 	emsg(_(error));
1007 	return TRUE;
1008     }
1009     return FALSE;
1010 }
1011 
1012 /*
1013  * Give an error if "tv" is not a number and return FAIL.
1014  */
1015     static int
1016 check_for_number(typval_T *tv)
1017 {
1018     if (tv->v_type != VAR_NUMBER)
1019     {
1020 	semsg(_(e_expected_str_but_got_str),
1021 		vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1022 	return FAIL;
1023     }
1024     return OK;
1025 }
1026 
1027 /*
1028  * Store "tv" in variable "name".
1029  * This is for s: and g: variables.
1030  */
1031     static void
1032 store_var(char_u *name, typval_T *tv)
1033 {
1034     funccal_entry_T entry;
1035     int		    flags = ASSIGN_DECL;
1036 
1037     if (tv->v_lock)
1038 	flags |= ASSIGN_CONST;
1039     save_funccal(&entry);
1040     set_var_const(name, NULL, tv, FALSE, flags, 0);
1041     restore_funccal();
1042 }
1043 
1044 /*
1045  * Convert "tv" to a string.
1046  * Return FAIL if not allowed.
1047  */
1048     static int
1049 do_2string(typval_T *tv, int is_2string_any, int tolerant)
1050 {
1051     if (tv->v_type != VAR_STRING)
1052     {
1053 	char_u *str;
1054 
1055 	if (is_2string_any)
1056 	{
1057 	    switch (tv->v_type)
1058 	    {
1059 		case VAR_SPECIAL:
1060 		case VAR_BOOL:
1061 		case VAR_NUMBER:
1062 		case VAR_FLOAT:
1063 		case VAR_BLOB:	break;
1064 
1065 		case VAR_LIST:
1066 				if (tolerant)
1067 				{
1068 				    char_u	*s, *e, *p;
1069 				    garray_T	ga;
1070 
1071 				    ga_init2(&ga, sizeof(char_u *), 1);
1072 
1073 				    // Convert to NL separated items, then
1074 				    // escape the items and replace the NL with
1075 				    // a space.
1076 				    str = typval2string(tv, TRUE);
1077 				    if (str == NULL)
1078 					return FAIL;
1079 				    s = str;
1080 				    while ((e = vim_strchr(s, '\n')) != NULL)
1081 				    {
1082 					*e = NUL;
1083 					p = vim_strsave_fnameescape(s, FALSE);
1084 					if (p != NULL)
1085 					{
1086 					    ga_concat(&ga, p);
1087 					    ga_concat(&ga, (char_u *)" ");
1088 					    vim_free(p);
1089 					}
1090 					s = e + 1;
1091 				    }
1092 				    vim_free(str);
1093 				    clear_tv(tv);
1094 				    tv->v_type = VAR_STRING;
1095 				    tv->vval.v_string = ga.ga_data;
1096 				    return OK;
1097 				}
1098 				// FALLTHROUGH
1099 		default:	to_string_error(tv->v_type);
1100 				return FAIL;
1101 	    }
1102 	}
1103 	str = typval_tostring(tv, TRUE);
1104 	clear_tv(tv);
1105 	tv->v_type = VAR_STRING;
1106 	tv->vval.v_string = str;
1107     }
1108     return OK;
1109 }
1110 
1111 /*
1112  * When the value of "sv" is a null list of dict, allocate it.
1113  */
1114     static void
1115 allocate_if_null(typval_T *tv)
1116 {
1117     switch (tv->v_type)
1118     {
1119 	case VAR_LIST:
1120 	    if (tv->vval.v_list == NULL)
1121 		(void)rettv_list_alloc(tv);
1122 	    break;
1123 	case VAR_DICT:
1124 	    if (tv->vval.v_dict == NULL)
1125 		(void)rettv_dict_alloc(tv);
1126 	    break;
1127 	case VAR_BLOB:
1128 	    if (tv->vval.v_blob == NULL)
1129 		(void)rettv_blob_alloc(tv);
1130 	    break;
1131 	default:
1132 	    break;
1133     }
1134 }
1135 
1136 /*
1137  * Return the character "str[index]" where "index" is the character index,
1138  * including composing characters.
1139  * If "index" is out of range NULL is returned.
1140  */
1141     char_u *
1142 char_from_string(char_u *str, varnumber_T index)
1143 {
1144     size_t	    nbyte = 0;
1145     varnumber_T	    nchar = index;
1146     size_t	    slen;
1147 
1148     if (str == NULL)
1149 	return NULL;
1150     slen = STRLEN(str);
1151 
1152     // Do the same as for a list: a negative index counts from the end.
1153     // Optimization to check the first byte to be below 0x80 (and no composing
1154     // character follows) makes this a lot faster.
1155     if (index < 0)
1156     {
1157 	int	clen = 0;
1158 
1159 	for (nbyte = 0; nbyte < slen; ++clen)
1160 	{
1161 	    if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1162 		++nbyte;
1163 	    else if (enc_utf8)
1164 		nbyte += utfc_ptr2len(str + nbyte);
1165 	    else
1166 		nbyte += mb_ptr2len(str + nbyte);
1167 	}
1168 	nchar = clen + index;
1169 	if (nchar < 0)
1170 	    // unlike list: index out of range results in empty string
1171 	    return NULL;
1172     }
1173 
1174     for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
1175     {
1176 	if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1177 	    ++nbyte;
1178 	else if (enc_utf8)
1179 	    nbyte += utfc_ptr2len(str + nbyte);
1180 	else
1181 	    nbyte += mb_ptr2len(str + nbyte);
1182     }
1183     if (nbyte >= slen)
1184 	return NULL;
1185     return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
1186 }
1187 
1188 /*
1189  * Get the byte index for character index "idx" in string "str" with length
1190  * "str_len".  Composing characters are included.
1191  * If going over the end return "str_len".
1192  * If "idx" is negative count from the end, -1 is the last character.
1193  * When going over the start return -1.
1194  */
1195     static long
1196 char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1197 {
1198     varnumber_T nchar = idx;
1199     size_t	nbyte = 0;
1200 
1201     if (nchar >= 0)
1202     {
1203 	while (nchar > 0 && nbyte < str_len)
1204 	{
1205 	    nbyte += mb_ptr2len(str + nbyte);
1206 	    --nchar;
1207 	}
1208     }
1209     else
1210     {
1211 	nbyte = str_len;
1212 	while (nchar < 0 && nbyte > 0)
1213 	{
1214 	    --nbyte;
1215 	    nbyte -= mb_head_off(str, str + nbyte);
1216 	    ++nchar;
1217 	}
1218 	if (nchar < 0)
1219 	    return -1;
1220     }
1221     return (long)nbyte;
1222 }
1223 
1224 /*
1225  * Return the slice "str[first : last]" using character indexes.  Composing
1226  * characters are included.
1227  * "exclusive" is TRUE for slice().
1228  * Return NULL when the result is empty.
1229  */
1230     char_u *
1231 string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
1232 {
1233     long	start_byte, end_byte;
1234     size_t	slen;
1235 
1236     if (str == NULL)
1237 	return NULL;
1238     slen = STRLEN(str);
1239     start_byte = char_idx2byte(str, slen, first);
1240     if (start_byte < 0)
1241 	start_byte = 0; // first index very negative: use zero
1242     if ((last == -1 && !exclusive) || last == VARNUM_MAX)
1243 	end_byte = (long)slen;
1244     else
1245     {
1246 	end_byte = char_idx2byte(str, slen, last);
1247 	if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
1248 	    // end index is inclusive
1249 	    end_byte += mb_ptr2len(str + end_byte);
1250     }
1251 
1252     if (start_byte >= (long)slen || end_byte <= start_byte)
1253 	return NULL;
1254     return vim_strnsave(str + start_byte, end_byte - start_byte);
1255 }
1256 
1257     static svar_T *
1258 get_script_svar(scriptref_T *sref, ectx_T *ectx)
1259 {
1260     scriptitem_T    *si = SCRIPT_ITEM(sref->sref_sid);
1261     dfunc_T	    *dfunc = ((dfunc_T *)def_functions.ga_data)
1262 							  + ectx->ec_dfunc_idx;
1263     svar_T	    *sv;
1264 
1265     if (sref->sref_seq != si->sn_script_seq)
1266     {
1267 	// The script was reloaded after the function was
1268 	// compiled, the script_idx may not be valid.
1269 	semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1270 						 dfunc->df_ufunc->uf_name_exp);
1271 	return NULL;
1272     }
1273     sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1274     if (!equal_type(sv->sv_type, sref->sref_type))
1275     {
1276 	emsg(_(e_script_variable_type_changed));
1277 	return NULL;
1278     }
1279     return sv;
1280 }
1281 
1282 /*
1283  * Function passed to do_cmdline() for splitting a script joined by NL
1284  * characters.
1285  */
1286     static char_u *
1287 get_split_sourceline(
1288 	int c UNUSED,
1289 	void *cookie,
1290 	int indent UNUSED,
1291 	getline_opt_T options UNUSED)
1292 {
1293     source_cookie_T	*sp = (source_cookie_T *)cookie;
1294     char_u		*p;
1295     char_u		*line;
1296 
1297     if (*sp->nextline == NUL)
1298 	return NULL;
1299     p = vim_strchr(sp->nextline, '\n');
1300     if (p == NULL)
1301     {
1302 	line = vim_strsave(sp->nextline);
1303 	sp->nextline += STRLEN(sp->nextline);
1304     }
1305     else
1306     {
1307 	line = vim_strnsave(sp->nextline, p - sp->nextline);
1308 	sp->nextline = p + 1;
1309     }
1310     return line;
1311 }
1312 
1313 /*
1314  * Execute a function by "name".
1315  * This can be a builtin function, user function or a funcref.
1316  * "iptr" can be used to replace the instruction with a more efficient one.
1317  */
1318     static int
1319 call_eval_func(
1320 	char_u	    *name,
1321 	int	    argcount,
1322 	ectx_T	    *ectx,
1323 	isn_T	    *iptr)
1324 {
1325     int	    called_emsg_before = called_emsg;
1326     int	    res;
1327 
1328     res = call_by_name(name, argcount, ectx, iptr);
1329     if (res == FAIL && called_emsg == called_emsg_before)
1330     {
1331 	dictitem_T	*v;
1332 
1333 	v = find_var(name, NULL, FALSE);
1334 	if (v == NULL)
1335 	{
1336 	    semsg(_(e_unknownfunc), name);
1337 	    return FAIL;
1338 	}
1339 	if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1340 	{
1341 	    semsg(_(e_unknownfunc), name);
1342 	    return FAIL;
1343 	}
1344 	return call_partial(&v->di_tv, argcount, ectx);
1345     }
1346     return res;
1347 }
1348 
1349 /*
1350  * When a function reference is used, fill a partial with the information
1351  * needed, especially when it is used as a closure.
1352  */
1353     int
1354 fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1355 {
1356     pt->pt_func = ufunc;
1357     pt->pt_refcount = 1;
1358 
1359     if (ufunc->uf_flags & FC_CLOSURE)
1360     {
1361 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
1362 							  + ectx->ec_dfunc_idx;
1363 
1364 	// The closure may need to find arguments and local variables in the
1365 	// current stack.
1366 	pt->pt_outer.out_stack = &ectx->ec_stack;
1367 	pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1368 	if (ectx->ec_outer_ref != NULL)
1369 	{
1370 	    // The current context already has a context, link to that one.
1371 	    pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1372 	    if (ectx->ec_outer_ref->or_partial != NULL)
1373 	    {
1374 		pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1375 		++pt->pt_outer.out_up_partial->pt_refcount;
1376 	    }
1377 	}
1378 
1379 	// If this function returns and the closure is still being used, we
1380 	// need to make a copy of the context (arguments and local variables).
1381 	// Store a reference to the partial so we can handle that.
1382 	if (unlikely(ga_grow(&ectx->ec_funcrefs, 1) == FAIL))
1383 	{
1384 	    vim_free(pt);
1385 	    return FAIL;
1386 	}
1387 	// Extra variable keeps the count of closures created in the current
1388 	// function call.
1389 	++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1390 		       + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1391 
1392 	((partial_T **)ectx->ec_funcrefs.ga_data)
1393 			       [ectx->ec_funcrefs.ga_len] = pt;
1394 	++pt->pt_refcount;
1395 	++ectx->ec_funcrefs.ga_len;
1396     }
1397     ++ufunc->uf_refcount;
1398     return OK;
1399 }
1400 
1401 // used for v_instr of typval of VAR_INSTR
1402 struct instr_S {
1403     ectx_T	*instr_ectx;
1404     isn_T	*instr_instr;
1405 };
1406 
1407 // used for substitute_instr
1408 typedef struct subs_expr_S {
1409     ectx_T	*subs_ectx;
1410     isn_T	*subs_instr;
1411     int		subs_status;
1412 } subs_expr_T;
1413 
1414 // Get pointer to item in the stack.
1415 #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
1416 
1417 // Get pointer to item at the bottom of the stack, -1 is the bottom.
1418 #undef STACK_TV_BOT
1419 #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
1420 
1421 // Get pointer to a local variable on the stack.  Negative for arguments.
1422 #define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
1423 
1424 // Set when calling do_debug().
1425 static ectx_T	*debug_context = NULL;
1426 static int	debug_var_count;
1427 
1428 /*
1429  * When debugging lookup "name" and return the typeval.
1430  * When not found return NULL.
1431  */
1432     typval_T *
1433 lookup_debug_var(char_u *name)
1434 {
1435     int		    idx;
1436     dfunc_T	    *dfunc;
1437     ufunc_T	    *ufunc;
1438     ectx_T	    *ectx = debug_context;
1439     int		    varargs_off;
1440 
1441     if (ectx == NULL)
1442 	return NULL;
1443     dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1444 
1445     // Go through the local variable names, from last to first.
1446     for (idx = debug_var_count - 1; idx >= 0; --idx)
1447     {
1448 	if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
1449 	    return STACK_TV_VAR(idx);
1450     }
1451 
1452     // Go through argument names.
1453     ufunc = dfunc->df_ufunc;
1454     varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1455     for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1456 	if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1457 	    return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1458 							  - varargs_off + idx);
1459     if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1460 	return STACK_TV(ectx->ec_frame_idx - 1);
1461 
1462     return NULL;
1463 }
1464 
1465     static void
1466 handle_debug(isn_T *iptr, ectx_T *ectx)
1467 {
1468     char_u	*line;
1469     ufunc_T	*ufunc = (((dfunc_T *)def_functions.ga_data)
1470 					       + ectx->ec_dfunc_idx)->df_ufunc;
1471     isn_T	*ni;
1472     int		end_lnum = iptr->isn_lnum;
1473     garray_T	ga;
1474     int		lnum;
1475 
1476     if (ex_nesting_level > debug_break_level)
1477     {
1478 	linenr_T breakpoint;
1479 
1480 	if (!ufunc->uf_has_breakpoint)
1481 	    return;
1482 
1483 	// check for the next breakpoint if needed
1484 	breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
1485 					   iptr->isn_arg.debug.dbg_break_lnum);
1486 	if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1487 	    return;
1488     }
1489 
1490     SOURCING_LNUM = iptr->isn_lnum;
1491     debug_context = ectx;
1492     debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
1493 
1494     for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1495 	if (ni->isn_type == ISN_DEBUG
1496 		  || ni->isn_type == ISN_RETURN
1497 		  || ni->isn_type == ISN_RETURN_VOID)
1498 	{
1499 	    end_lnum = ni->isn_lnum;
1500 	    break;
1501 	}
1502 
1503     if (end_lnum > iptr->isn_lnum)
1504     {
1505 	ga_init2(&ga, sizeof(char_u *), 10);
1506 	for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
1507 	{
1508 	    char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
1509 
1510 	    if (p == NULL)
1511 		continue;  // left over from continuation line
1512 	    p = skipwhite(p);
1513 	    if (*p == '#')
1514 		break;
1515 	    if (likely(ga_grow(&ga, 1) == OK))
1516 		((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1517 	    if (STRNCMP(p, "def ", 4) == 0)
1518 		break;
1519 	}
1520 	line = ga_concat_strings(&ga, "  ");
1521 	vim_free(ga.ga_data);
1522     }
1523     else
1524 	line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
1525 
1526     do_debug(line == NULL ? (char_u *)"[empty]" : line);
1527     debug_context = NULL;
1528 
1529     if (end_lnum > iptr->isn_lnum)
1530 	vim_free(line);
1531 }
1532 
1533 /*
1534  * Execute instructions in execution context "ectx".
1535  * Return OK or FAIL;
1536  */
1537     static int
1538 exec_instructions(ectx_T *ectx)
1539 {
1540     int		ret = FAIL;
1541     int		save_trylevel_at_start = ectx->ec_trylevel_at_start;
1542 
1543     // Start execution at the first instruction.
1544     ectx->ec_iidx = 0;
1545 
1546     // Only catch exceptions in this instruction list.
1547     ectx->ec_trylevel_at_start = trylevel;
1548 
1549     for (;;)
1550     {
1551 	static int  breakcheck_count = 0;  // using "static" makes it faster
1552 	isn_T	    *iptr;
1553 	typval_T    *tv;
1554 
1555 	if (unlikely(++breakcheck_count >= 100))
1556 	{
1557 	    line_breakcheck();
1558 	    breakcheck_count = 0;
1559 	}
1560 	if (unlikely(got_int))
1561 	{
1562 	    // Turn CTRL-C into an exception.
1563 	    got_int = FALSE;
1564 	    if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
1565 		goto theend;
1566 	    did_throw = TRUE;
1567 	}
1568 
1569 	if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
1570 	{
1571 	    // Turn an error message into an exception.
1572 	    did_emsg = FALSE;
1573 	    if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1574 		goto theend;
1575 	    did_throw = TRUE;
1576 	    *msg_list = NULL;
1577 	}
1578 
1579 	if (unlikely(did_throw))
1580 	{
1581 	    garray_T	*trystack = &ectx->ec_trystack;
1582 	    trycmd_T    *trycmd = NULL;
1583 	    int		index = trystack->ga_len;
1584 
1585 	    // An exception jumps to the first catch, finally, or returns from
1586 	    // the current function.
1587 	    while (index > 0)
1588 	    {
1589 		trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
1590 		if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
1591 		    break;
1592 		// In the catch and finally block of this try we have to go up
1593 		// one level.
1594 		--index;
1595 		trycmd = NULL;
1596 	    }
1597 	    if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
1598 	    {
1599 		if (trycmd->tcd_in_catch)
1600 		{
1601 		    // exception inside ":catch", jump to ":finally" once
1602 		    ectx->ec_iidx = trycmd->tcd_finally_idx;
1603 		    trycmd->tcd_finally_idx = 0;
1604 		}
1605 		else
1606 		    // jump to first ":catch"
1607 		    ectx->ec_iidx = trycmd->tcd_catch_idx;
1608 		trycmd->tcd_in_catch = TRUE;
1609 		did_throw = FALSE;  // don't come back here until :endtry
1610 		trycmd->tcd_did_throw = TRUE;
1611 	    }
1612 	    else
1613 	    {
1614 		// Not inside try or need to return from current functions.
1615 		// Push a dummy return value.
1616 		if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
1617 		    goto theend;
1618 		tv = STACK_TV_BOT(0);
1619 		tv->v_type = VAR_NUMBER;
1620 		tv->vval.v_number = 0;
1621 		++ectx->ec_stack.ga_len;
1622 		if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
1623 		{
1624 		    // At the toplevel we are done.
1625 		    need_rethrow = TRUE;
1626 		    if (handle_closure_in_use(ectx, FALSE) == FAIL)
1627 			goto theend;
1628 		    goto done;
1629 		}
1630 
1631 		if (func_return(ectx) == FAIL)
1632 		    goto theend;
1633 	    }
1634 	    continue;
1635 	}
1636 
1637 	iptr = &ectx->ec_instr[ectx->ec_iidx++];
1638 	switch (iptr->isn_type)
1639 	{
1640 	    // execute Ex command line
1641 	    case ISN_EXEC:
1642 		{
1643 		    source_cookie_T cookie;
1644 
1645 		    SOURCING_LNUM = iptr->isn_lnum;
1646 		    // Pass getsourceline to get an error for a missing ":end"
1647 		    // command.
1648 		    CLEAR_FIELD(cookie);
1649 		    cookie.sourcing_lnum = iptr->isn_lnum - 1;
1650 		    if (do_cmdline(iptr->isn_arg.string,
1651 				getsourceline, &cookie,
1652 				   DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1653 									== FAIL
1654 				|| did_emsg)
1655 			goto on_error;
1656 		}
1657 		break;
1658 
1659 	    // execute Ex command line split at NL characters.
1660 	    case ISN_EXEC_SPLIT:
1661 		{
1662 		    source_cookie_T cookie;
1663 		    char_u	    *line;
1664 
1665 		    SOURCING_LNUM = iptr->isn_lnum;
1666 		    CLEAR_FIELD(cookie);
1667 		    cookie.sourcing_lnum = iptr->isn_lnum - 1;
1668 		    cookie.nextline = iptr->isn_arg.string;
1669 		    line = get_split_sourceline(0, &cookie, 0, 0);
1670 		    if (do_cmdline(line,
1671 				get_split_sourceline, &cookie,
1672 				   DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1673 									== FAIL
1674 				|| did_emsg)
1675 		    {
1676 			vim_free(line);
1677 			goto on_error;
1678 		    }
1679 		    vim_free(line);
1680 		}
1681 		break;
1682 
1683 	    // Evaluate an expression with legacy syntax, push it onto the
1684 	    // stack.
1685 	    case ISN_LEGACY_EVAL:
1686 		{
1687 		    char_u  *arg = iptr->isn_arg.string;
1688 		    int	    res;
1689 		    int	    save_flags = cmdmod.cmod_flags;
1690 
1691 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
1692 			goto theend;
1693 		    tv = STACK_TV_BOT(0);
1694 		    init_tv(tv);
1695 		    cmdmod.cmod_flags |= CMOD_LEGACY;
1696 		    res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1697 		    cmdmod.cmod_flags = save_flags;
1698 		    if (res == FAIL)
1699 			goto on_error;
1700 		    ++ectx->ec_stack.ga_len;
1701 		}
1702 		break;
1703 
1704 	    // push typeval VAR_INSTR with instructions to be executed
1705 	    case ISN_INSTR:
1706 		{
1707 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
1708 			goto theend;
1709 		    tv = STACK_TV_BOT(0);
1710 		    tv->vval.v_instr = ALLOC_ONE(instr_T);
1711 		    if (tv->vval.v_instr == NULL)
1712 			goto on_error;
1713 		    ++ectx->ec_stack.ga_len;
1714 
1715 		    tv->v_type = VAR_INSTR;
1716 		    tv->vval.v_instr->instr_ectx = ectx;
1717 		    tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1718 		}
1719 		break;
1720 
1721 	    // execute :substitute with an expression
1722 	    case ISN_SUBSTITUTE:
1723 		{
1724 		    subs_T		*subs = &iptr->isn_arg.subs;
1725 		    source_cookie_T	cookie;
1726 		    struct subs_expr_S	*save_instr = substitute_instr;
1727 		    struct subs_expr_S	subs_instr;
1728 		    int			res;
1729 
1730 		    subs_instr.subs_ectx = ectx;
1731 		    subs_instr.subs_instr = subs->subs_instr;
1732 		    subs_instr.subs_status = OK;
1733 		    substitute_instr = &subs_instr;
1734 
1735 		    SOURCING_LNUM = iptr->isn_lnum;
1736 		    // This is very much like ISN_EXEC
1737 		    CLEAR_FIELD(cookie);
1738 		    cookie.sourcing_lnum = iptr->isn_lnum - 1;
1739 		    res = do_cmdline(subs->subs_cmd,
1740 				getsourceline, &cookie,
1741 				   DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1742 		    substitute_instr = save_instr;
1743 
1744 		    if (res == FAIL || did_emsg
1745 					     || subs_instr.subs_status == FAIL)
1746 			goto on_error;
1747 		}
1748 		break;
1749 
1750 	    case ISN_FINISH:
1751 		goto done;
1752 
1753 	    case ISN_REDIRSTART:
1754 		// create a dummy entry for var_redir_str()
1755 		if (alloc_redir_lval() == FAIL)
1756 		    goto on_error;
1757 
1758 		// The output is stored in growarray "redir_ga" until
1759 		// redirection ends.
1760 		init_redir_ga();
1761 		redir_vname = 1;
1762 		break;
1763 
1764 	    case ISN_REDIREND:
1765 		{
1766 		    char_u *res = get_clear_redir_ga();
1767 
1768 		    // End redirection, put redirected text on the stack.
1769 		    clear_redir_lval();
1770 		    redir_vname = 0;
1771 
1772 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
1773 		    {
1774 			vim_free(res);
1775 			goto theend;
1776 		    }
1777 		    tv = STACK_TV_BOT(0);
1778 		    tv->v_type = VAR_STRING;
1779 		    tv->vval.v_string = res;
1780 		    ++ectx->ec_stack.ga_len;
1781 		}
1782 		break;
1783 
1784 	    case ISN_CEXPR_AUCMD:
1785 #ifdef FEAT_QUICKFIX
1786 		if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1787 		    goto on_error;
1788 #endif
1789 		break;
1790 
1791 	    case ISN_CEXPR_CORE:
1792 #ifdef FEAT_QUICKFIX
1793 		{
1794 		    exarg_T ea;
1795 		    int	    res;
1796 
1797 		    CLEAR_FIELD(ea);
1798 		    ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1799 		    ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1800 		    ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1801 		    --ectx->ec_stack.ga_len;
1802 		    tv = STACK_TV_BOT(0);
1803 		    res = cexpr_core(&ea, tv);
1804 		    clear_tv(tv);
1805 		    if (res == FAIL)
1806 			goto on_error;
1807 		}
1808 #endif
1809 		break;
1810 
1811 	    // execute Ex command from pieces on the stack
1812 	    case ISN_EXECCONCAT:
1813 		{
1814 		    int	    count = iptr->isn_arg.number;
1815 		    size_t  len = 0;
1816 		    int	    pass;
1817 		    int	    i;
1818 		    char_u  *cmd = NULL;
1819 		    char_u  *str;
1820 
1821 		    for (pass = 1; pass <= 2; ++pass)
1822 		    {
1823 			for (i = 0; i < count; ++i)
1824 			{
1825 			    tv = STACK_TV_BOT(i - count);
1826 			    str = tv->vval.v_string;
1827 			    if (str != NULL && *str != NUL)
1828 			    {
1829 				if (pass == 2)
1830 				    STRCPY(cmd + len, str);
1831 				len += STRLEN(str);
1832 			    }
1833 			    if (pass == 2)
1834 				clear_tv(tv);
1835 			}
1836 			if (pass == 1)
1837 			{
1838 			    cmd = alloc(len + 1);
1839 			    if (unlikely(cmd == NULL))
1840 				goto theend;
1841 			    len = 0;
1842 			}
1843 		    }
1844 
1845 		    SOURCING_LNUM = iptr->isn_lnum;
1846 		    do_cmdline_cmd(cmd);
1847 		    vim_free(cmd);
1848 		}
1849 		break;
1850 
1851 	    // execute :echo {string} ...
1852 	    case ISN_ECHO:
1853 		{
1854 		    int count = iptr->isn_arg.echo.echo_count;
1855 		    int	atstart = TRUE;
1856 		    int needclr = TRUE;
1857 		    int	idx;
1858 
1859 		    for (idx = 0; idx < count; ++idx)
1860 		    {
1861 			tv = STACK_TV_BOT(idx - count);
1862 			echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1863 							   &atstart, &needclr);
1864 			clear_tv(tv);
1865 		    }
1866 		    if (needclr)
1867 			msg_clr_eos();
1868 		    ectx->ec_stack.ga_len -= count;
1869 		}
1870 		break;
1871 
1872 	    // :execute {string} ...
1873 	    // :echomsg {string} ...
1874 	    // :echoerr {string} ...
1875 	    case ISN_EXECUTE:
1876 	    case ISN_ECHOMSG:
1877 	    case ISN_ECHOERR:
1878 		{
1879 		    int		count = iptr->isn_arg.number;
1880 		    garray_T	ga;
1881 		    char_u	buf[NUMBUFLEN];
1882 		    char_u	*p;
1883 		    int		len;
1884 		    int		failed = FALSE;
1885 		    int		idx;
1886 
1887 		    ga_init2(&ga, 1, 80);
1888 		    for (idx = 0; idx < count; ++idx)
1889 		    {
1890 			tv = STACK_TV_BOT(idx - count);
1891 			if (iptr->isn_type == ISN_EXECUTE)
1892 			{
1893 			    if (tv->v_type == VAR_CHANNEL
1894 						      || tv->v_type == VAR_JOB)
1895 			    {
1896 				SOURCING_LNUM = iptr->isn_lnum;
1897 				semsg(_(e_using_invalid_value_as_string_str),
1898 						    vartype_name(tv->v_type));
1899 				break;
1900 			    }
1901 			    else
1902 				p = tv_get_string_buf(tv, buf);
1903 			}
1904 			else
1905 			    p = tv_stringify(tv, buf);
1906 
1907 			len = (int)STRLEN(p);
1908 			if (unlikely(ga_grow(&ga, len + 2) == FAIL))
1909 			    failed = TRUE;
1910 			else
1911 			{
1912 			    if (ga.ga_len > 0)
1913 				((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1914 			    STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1915 			    ga.ga_len += len;
1916 			}
1917 			clear_tv(tv);
1918 		    }
1919 		    ectx->ec_stack.ga_len -= count;
1920 		    if (failed)
1921 		    {
1922 			ga_clear(&ga);
1923 			goto on_error;
1924 		    }
1925 
1926 		    if (ga.ga_data != NULL)
1927 		    {
1928 			if (iptr->isn_type == ISN_EXECUTE)
1929 			{
1930 			    SOURCING_LNUM = iptr->isn_lnum;
1931 			    do_cmdline_cmd((char_u *)ga.ga_data);
1932 			    if (did_emsg)
1933 			    {
1934 				ga_clear(&ga);
1935 				goto on_error;
1936 			    }
1937 			}
1938 			else
1939 			{
1940 			    msg_sb_eol();
1941 			    if (iptr->isn_type == ISN_ECHOMSG)
1942 			    {
1943 				msg_attr(ga.ga_data, echo_attr);
1944 				out_flush();
1945 			    }
1946 			    else
1947 			    {
1948 				SOURCING_LNUM = iptr->isn_lnum;
1949 				emsg(ga.ga_data);
1950 			    }
1951 			}
1952 		    }
1953 		    ga_clear(&ga);
1954 		}
1955 		break;
1956 
1957 	    // load local variable or argument
1958 	    case ISN_LOAD:
1959 		if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
1960 		    goto theend;
1961 		copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1962 		++ectx->ec_stack.ga_len;
1963 		break;
1964 
1965 	    // load v: variable
1966 	    case ISN_LOADV:
1967 		if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
1968 		    goto theend;
1969 		copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1970 		++ectx->ec_stack.ga_len;
1971 		break;
1972 
1973 	    // load s: variable in Vim9 script
1974 	    case ISN_LOADSCRIPT:
1975 		{
1976 		    scriptref_T	*sref = iptr->isn_arg.script.scriptref;
1977 		    svar_T	 *sv;
1978 
1979 		    sv = get_script_svar(sref, ectx);
1980 		    if (sv == NULL)
1981 			goto theend;
1982 		    allocate_if_null(sv->sv_tv);
1983 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
1984 			goto theend;
1985 		    copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1986 		    ++ectx->ec_stack.ga_len;
1987 		}
1988 		break;
1989 
1990 	    // load s: variable in old script
1991 	    case ISN_LOADS:
1992 		{
1993 		    hashtab_T	*ht = &SCRIPT_VARS(
1994 					       iptr->isn_arg.loadstore.ls_sid);
1995 		    char_u	*name = iptr->isn_arg.loadstore.ls_name;
1996 		    dictitem_T	*di = find_var_in_ht(ht, 0, name, TRUE);
1997 
1998 		    if (di == NULL)
1999 		    {
2000 			SOURCING_LNUM = iptr->isn_lnum;
2001 			semsg(_(e_undefined_variable_str), name);
2002 			goto on_error;
2003 		    }
2004 		    else
2005 		    {
2006 			if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
2007 			    goto theend;
2008 			copy_tv(&di->di_tv, STACK_TV_BOT(0));
2009 			++ectx->ec_stack.ga_len;
2010 		    }
2011 		}
2012 		break;
2013 
2014 	    // load g:/b:/w:/t: variable
2015 	    case ISN_LOADG:
2016 	    case ISN_LOADB:
2017 	    case ISN_LOADW:
2018 	    case ISN_LOADT:
2019 		{
2020 		    dictitem_T *di = NULL;
2021 		    hashtab_T *ht = NULL;
2022 		    char namespace;
2023 
2024 		    switch (iptr->isn_type)
2025 		    {
2026 			case ISN_LOADG:
2027 			    ht = get_globvar_ht();
2028 			    namespace = 'g';
2029 			    break;
2030 			case ISN_LOADB:
2031 			    ht = &curbuf->b_vars->dv_hashtab;
2032 			    namespace = 'b';
2033 			    break;
2034 			case ISN_LOADW:
2035 			    ht = &curwin->w_vars->dv_hashtab;
2036 			    namespace = 'w';
2037 			    break;
2038 			case ISN_LOADT:
2039 			    ht = &curtab->tp_vars->dv_hashtab;
2040 			    namespace = 't';
2041 			    break;
2042 			default:  // Cannot reach here
2043 			    goto theend;
2044 		    }
2045 		    di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2046 
2047 		    if (di == NULL)
2048 		    {
2049 			SOURCING_LNUM = iptr->isn_lnum;
2050 			semsg(_(e_undefined_variable_char_str),
2051 					     namespace, iptr->isn_arg.string);
2052 			goto on_error;
2053 		    }
2054 		    else
2055 		    {
2056 			if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
2057 			    goto theend;
2058 			copy_tv(&di->di_tv, STACK_TV_BOT(0));
2059 			++ectx->ec_stack.ga_len;
2060 		    }
2061 		}
2062 		break;
2063 
2064 	    // load autoload variable
2065 	    case ISN_LOADAUTO:
2066 		{
2067 		    char_u *name = iptr->isn_arg.string;
2068 
2069 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
2070 			goto theend;
2071 		    SOURCING_LNUM = iptr->isn_lnum;
2072 		    if (eval_variable(name, (int)STRLEN(name),
2073 			      STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
2074 			goto on_error;
2075 		    ++ectx->ec_stack.ga_len;
2076 		}
2077 		break;
2078 
2079 	    // load g:/b:/w:/t: namespace
2080 	    case ISN_LOADGDICT:
2081 	    case ISN_LOADBDICT:
2082 	    case ISN_LOADWDICT:
2083 	    case ISN_LOADTDICT:
2084 		{
2085 		    dict_T *d = NULL;
2086 
2087 		    switch (iptr->isn_type)
2088 		    {
2089 			case ISN_LOADGDICT: d = get_globvar_dict(); break;
2090 			case ISN_LOADBDICT: d = curbuf->b_vars; break;
2091 			case ISN_LOADWDICT: d = curwin->w_vars; break;
2092 			case ISN_LOADTDICT: d = curtab->tp_vars; break;
2093 			default:  // Cannot reach here
2094 			    goto theend;
2095 		    }
2096 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
2097 			goto theend;
2098 		    tv = STACK_TV_BOT(0);
2099 		    tv->v_type = VAR_DICT;
2100 		    tv->v_lock = 0;
2101 		    tv->vval.v_dict = d;
2102 		    ++d->dv_refcount;
2103 		    ++ectx->ec_stack.ga_len;
2104 		}
2105 		break;
2106 
2107 	    // load &option
2108 	    case ISN_LOADOPT:
2109 		{
2110 		    typval_T	optval;
2111 		    char_u	*name = iptr->isn_arg.string;
2112 
2113 		    // This is not expected to fail, name is checked during
2114 		    // compilation: don't set SOURCING_LNUM.
2115 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
2116 			goto theend;
2117 		    if (eval_option(&name, &optval, TRUE) == FAIL)
2118 			goto theend;
2119 		    *STACK_TV_BOT(0) = optval;
2120 		    ++ectx->ec_stack.ga_len;
2121 		}
2122 		break;
2123 
2124 	    // load $ENV
2125 	    case ISN_LOADENV:
2126 		{
2127 		    typval_T	optval;
2128 		    char_u	*name = iptr->isn_arg.string;
2129 
2130 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
2131 			goto theend;
2132 		    // name is always valid, checked when compiling
2133 		    (void)eval_env_var(&name, &optval, TRUE);
2134 		    *STACK_TV_BOT(0) = optval;
2135 		    ++ectx->ec_stack.ga_len;
2136 		}
2137 		break;
2138 
2139 	    // load @register
2140 	    case ISN_LOADREG:
2141 		if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
2142 		    goto theend;
2143 		tv = STACK_TV_BOT(0);
2144 		tv->v_type = VAR_STRING;
2145 		tv->v_lock = 0;
2146 		// This may result in NULL, which should be equivalent to an
2147 		// empty string.
2148 		tv->vval.v_string = get_reg_contents(
2149 					  iptr->isn_arg.number, GREG_EXPR_SRC);
2150 		++ectx->ec_stack.ga_len;
2151 		break;
2152 
2153 	    // store local variable
2154 	    case ISN_STORE:
2155 		--ectx->ec_stack.ga_len;
2156 		tv = STACK_TV_VAR(iptr->isn_arg.number);
2157 		clear_tv(tv);
2158 		*tv = *STACK_TV_BOT(0);
2159 		break;
2160 
2161 	    // store s: variable in old script
2162 	    case ISN_STORES:
2163 		{
2164 		    hashtab_T	*ht = &SCRIPT_VARS(
2165 					       iptr->isn_arg.loadstore.ls_sid);
2166 		    char_u	*name = iptr->isn_arg.loadstore.ls_name;
2167 		    dictitem_T	*di = find_var_in_ht(ht, 0, name + 2, TRUE);
2168 
2169 		    --ectx->ec_stack.ga_len;
2170 		    if (di == NULL)
2171 			store_var(name, STACK_TV_BOT(0));
2172 		    else
2173 		    {
2174 			SOURCING_LNUM = iptr->isn_lnum;
2175 			if (var_check_permission(di, name) == FAIL)
2176 			{
2177 			    clear_tv(STACK_TV_BOT(0));
2178 			    goto on_error;
2179 			}
2180 			clear_tv(&di->di_tv);
2181 			di->di_tv = *STACK_TV_BOT(0);
2182 		    }
2183 		}
2184 		break;
2185 
2186 	    // store script-local variable in Vim9 script
2187 	    case ISN_STORESCRIPT:
2188 		{
2189 		    scriptref_T	    *sref = iptr->isn_arg.script.scriptref;
2190 		    svar_T	    *sv;
2191 
2192 		    sv = get_script_svar(sref, ectx);
2193 		    if (sv == NULL)
2194 			goto theend;
2195 		    --ectx->ec_stack.ga_len;
2196 
2197 		    // "const" and "final" are checked at compile time, locking
2198 		    // the value needs to be checked here.
2199 		    SOURCING_LNUM = iptr->isn_lnum;
2200 		    if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
2201 		    {
2202 			clear_tv(STACK_TV_BOT(0));
2203 			goto on_error;
2204 		    }
2205 
2206 		    clear_tv(sv->sv_tv);
2207 		    *sv->sv_tv = *STACK_TV_BOT(0);
2208 		}
2209 		break;
2210 
2211 	    // store option
2212 	    case ISN_STOREOPT:
2213 		{
2214 		    long	n = 0;
2215 		    char_u	*s = NULL;
2216 		    char	*msg;
2217 
2218 		    --ectx->ec_stack.ga_len;
2219 		    tv = STACK_TV_BOT(0);
2220 		    if (tv->v_type == VAR_STRING)
2221 		    {
2222 			s = tv->vval.v_string;
2223 			if (s == NULL)
2224 			    s = (char_u *)"";
2225 		    }
2226 		    else
2227 			// must be VAR_NUMBER, CHECKTYPE makes sure
2228 			n = tv->vval.v_number;
2229 		    msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2230 					n, s, iptr->isn_arg.storeopt.so_flags);
2231 		    clear_tv(tv);
2232 		    if (msg != NULL)
2233 		    {
2234 			SOURCING_LNUM = iptr->isn_lnum;
2235 			emsg(_(msg));
2236 			goto on_error;
2237 		    }
2238 		}
2239 		break;
2240 
2241 	    // store $ENV
2242 	    case ISN_STOREENV:
2243 		--ectx->ec_stack.ga_len;
2244 		tv = STACK_TV_BOT(0);
2245 		vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2246 		clear_tv(tv);
2247 		break;
2248 
2249 	    // store @r
2250 	    case ISN_STOREREG:
2251 		{
2252 		    int	reg = iptr->isn_arg.number;
2253 
2254 		    --ectx->ec_stack.ga_len;
2255 		    tv = STACK_TV_BOT(0);
2256 		    write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
2257 		    clear_tv(tv);
2258 		}
2259 		break;
2260 
2261 	    // store v: variable
2262 	    case ISN_STOREV:
2263 		--ectx->ec_stack.ga_len;
2264 		if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2265 								       == FAIL)
2266 		    // should not happen, type is checked when compiling
2267 		    goto on_error;
2268 		break;
2269 
2270 	    // store g:/b:/w:/t: variable
2271 	    case ISN_STOREG:
2272 	    case ISN_STOREB:
2273 	    case ISN_STOREW:
2274 	    case ISN_STORET:
2275 		{
2276 		    dictitem_T	*di;
2277 		    hashtab_T	*ht;
2278 		    char_u	*name = iptr->isn_arg.string + 2;
2279 
2280 		    switch (iptr->isn_type)
2281 		    {
2282 			case ISN_STOREG:
2283 			    ht = get_globvar_ht();
2284 			    break;
2285 			case ISN_STOREB:
2286 			    ht = &curbuf->b_vars->dv_hashtab;
2287 			    break;
2288 			case ISN_STOREW:
2289 			    ht = &curwin->w_vars->dv_hashtab;
2290 			    break;
2291 			case ISN_STORET:
2292 			    ht = &curtab->tp_vars->dv_hashtab;
2293 			    break;
2294 			default:  // Cannot reach here
2295 			    goto theend;
2296 		    }
2297 
2298 		    --ectx->ec_stack.ga_len;
2299 		    di = find_var_in_ht(ht, 0, name, TRUE);
2300 		    if (di == NULL)
2301 			store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
2302 		    else
2303 		    {
2304 			SOURCING_LNUM = iptr->isn_lnum;
2305 			if (var_check_permission(di, name) == FAIL)
2306 			    goto on_error;
2307 			clear_tv(&di->di_tv);
2308 			di->di_tv = *STACK_TV_BOT(0);
2309 		    }
2310 		}
2311 		break;
2312 
2313 	    // store an autoload variable
2314 	    case ISN_STOREAUTO:
2315 		SOURCING_LNUM = iptr->isn_lnum;
2316 		set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2317 		clear_tv(STACK_TV_BOT(-1));
2318 		--ectx->ec_stack.ga_len;
2319 		break;
2320 
2321 	    // store number in local variable
2322 	    case ISN_STORENR:
2323 		tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
2324 		clear_tv(tv);
2325 		tv->v_type = VAR_NUMBER;
2326 		tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
2327 		break;
2328 
2329 	    // store value in list or dict variable
2330 	    case ISN_STOREINDEX:
2331 		{
2332 		    vartype_T	dest_type = iptr->isn_arg.vartype;
2333 		    typval_T	*tv_idx = STACK_TV_BOT(-2);
2334 		    typval_T	*tv_dest = STACK_TV_BOT(-1);
2335 		    int		status = OK;
2336 
2337 		    // Stack contains:
2338 		    // -3 value to be stored
2339 		    // -2 index
2340 		    // -1 dict or list
2341 		    tv = STACK_TV_BOT(-3);
2342 		    SOURCING_LNUM = iptr->isn_lnum;
2343 		    if (dest_type == VAR_ANY)
2344 		    {
2345 			dest_type = tv_dest->v_type;
2346 			if (dest_type == VAR_DICT)
2347 			    status = do_2string(tv_idx, TRUE, FALSE);
2348 			else if (dest_type == VAR_LIST
2349 					       && tv_idx->v_type != VAR_NUMBER)
2350 			{
2351 			    emsg(_(e_number_expected));
2352 			    status = FAIL;
2353 			}
2354 		    }
2355 		    else if (dest_type != tv_dest->v_type)
2356 		    {
2357 			// just in case, should be OK
2358 			semsg(_(e_expected_str_but_got_str),
2359 				    vartype_name(dest_type),
2360 				    vartype_name(tv_dest->v_type));
2361 			status = FAIL;
2362 		    }
2363 
2364 		    if (status == OK && dest_type == VAR_LIST)
2365 		    {
2366 			long	    lidx = (long)tv_idx->vval.v_number;
2367 			list_T	    *list = tv_dest->vval.v_list;
2368 
2369 			if (list == NULL)
2370 			{
2371 			    emsg(_(e_list_not_set));
2372 			    goto on_error;
2373 			}
2374 			if (lidx < 0 && list->lv_len + lidx >= 0)
2375 			    // negative index is relative to the end
2376 			    lidx = list->lv_len + lidx;
2377 			if (lidx < 0 || lidx > list->lv_len)
2378 			{
2379 			    semsg(_(e_listidx), lidx);
2380 			    goto on_error;
2381 			}
2382 			if (lidx < list->lv_len)
2383 			{
2384 			    listitem_T *li = list_find(list, lidx);
2385 
2386 			    if (error_if_locked(li->li_tv.v_lock,
2387 						    e_cannot_change_list_item))
2388 				goto on_error;
2389 			    // overwrite existing list item
2390 			    clear_tv(&li->li_tv);
2391 			    li->li_tv = *tv;
2392 			}
2393 			else
2394 			{
2395 			    if (error_if_locked(list->lv_lock,
2396 							 e_cannot_change_list))
2397 				goto on_error;
2398 			    // append to list, only fails when out of memory
2399 			    if (list_append_tv(list, tv) == FAIL)
2400 				goto theend;
2401 			    clear_tv(tv);
2402 			}
2403 		    }
2404 		    else if (status == OK && dest_type == VAR_DICT)
2405 		    {
2406 			char_u		*key = tv_idx->vval.v_string;
2407 			dict_T		*dict = tv_dest->vval.v_dict;
2408 			dictitem_T	*di;
2409 
2410 			SOURCING_LNUM = iptr->isn_lnum;
2411 			if (dict == NULL)
2412 			{
2413 			    emsg(_(e_dictionary_not_set));
2414 			    goto on_error;
2415 			}
2416 			if (key == NULL)
2417 			    key = (char_u *)"";
2418 			di = dict_find(dict, key, -1);
2419 			if (di != NULL)
2420 			{
2421 			    if (error_if_locked(di->di_tv.v_lock,
2422 						    e_cannot_change_dict_item))
2423 				goto on_error;
2424 			    // overwrite existing value
2425 			    clear_tv(&di->di_tv);
2426 			    di->di_tv = *tv;
2427 			}
2428 			else
2429 			{
2430 			    if (error_if_locked(dict->dv_lock,
2431 							 e_cannot_change_dict))
2432 				goto on_error;
2433 			    // add to dict, only fails when out of memory
2434 			    if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2435 				goto theend;
2436 			    clear_tv(tv);
2437 			}
2438 		    }
2439 		    else if (status == OK && dest_type == VAR_BLOB)
2440 		    {
2441 			long	    lidx = (long)tv_idx->vval.v_number;
2442 			blob_T	    *blob = tv_dest->vval.v_blob;
2443 			varnumber_T nr;
2444 			int	    error = FALSE;
2445 			int	    len;
2446 
2447 			if (blob == NULL)
2448 			{
2449 			    emsg(_(e_blob_not_set));
2450 			    goto on_error;
2451 			}
2452 			len = blob_len(blob);
2453 			if (lidx < 0 && len + lidx >= 0)
2454 			    // negative index is relative to the end
2455 			    lidx = len + lidx;
2456 
2457 			// Can add one byte at the end.
2458 			if (lidx < 0 || lidx > len)
2459 			{
2460 			    semsg(_(e_blobidx), lidx);
2461 			    goto on_error;
2462 			}
2463 			if (value_check_lock(blob->bv_lock,
2464 						      (char_u *)"blob", FALSE))
2465 			    goto on_error;
2466 			nr = tv_get_number_chk(tv, &error);
2467 			if (error)
2468 			    goto on_error;
2469 			blob_set_append(blob, lidx, nr);
2470 		    }
2471 		    else
2472 		    {
2473 			status = FAIL;
2474 			semsg(_(e_cannot_index_str), vartype_name(dest_type));
2475 		    }
2476 
2477 		    clear_tv(tv_idx);
2478 		    clear_tv(tv_dest);
2479 		    ectx->ec_stack.ga_len -= 3;
2480 		    if (status == FAIL)
2481 		    {
2482 			clear_tv(tv);
2483 			goto on_error;
2484 		    }
2485 		}
2486 		break;
2487 
2488 	    // store value in blob range
2489 	    case ISN_STORERANGE:
2490 		{
2491 		    typval_T	*tv_idx1 = STACK_TV_BOT(-3);
2492 		    typval_T	*tv_idx2 = STACK_TV_BOT(-2);
2493 		    typval_T	*tv_dest = STACK_TV_BOT(-1);
2494 		    int		status = OK;
2495 
2496 		    // Stack contains:
2497 		    // -4 value to be stored
2498 		    // -3 first index or "none"
2499 		    // -2 second index or "none"
2500 		    // -1 destination blob
2501 		    tv = STACK_TV_BOT(-4);
2502 		    if (tv_dest->v_type != VAR_BLOB)
2503 		    {
2504 			status = FAIL;
2505 			emsg(_(e_blob_required));
2506 		    }
2507 		    else
2508 		    {
2509 			varnumber_T n1;
2510 			varnumber_T n2;
2511 			int	    error = FALSE;
2512 
2513 			n1 = tv_get_number_chk(tv_idx1, &error);
2514 			if (error)
2515 			    status = FAIL;
2516 			else
2517 			{
2518 			    if (tv_idx2->v_type == VAR_SPECIAL
2519 					&& tv_idx2->vval.v_number == VVAL_NONE)
2520 				n2 = blob_len(tv_dest->vval.v_blob) - 1;
2521 			    else
2522 				n2 = tv_get_number_chk(tv_idx2, &error);
2523 			    if (error)
2524 				status = FAIL;
2525 			    else
2526 			    {
2527 				long	bloblen = blob_len(tv_dest->vval.v_blob);
2528 
2529 				if (check_blob_index(bloblen,
2530 							     n1, FALSE) == FAIL
2531 					|| check_blob_range(bloblen,
2532 							n1, n2, FALSE) == FAIL)
2533 				    status = FAIL;
2534 				else
2535 				    status = blob_set_range(
2536 					     tv_dest->vval.v_blob, n1, n2, tv);
2537 			    }
2538 			}
2539 		    }
2540 
2541 		    clear_tv(tv_idx1);
2542 		    clear_tv(tv_idx2);
2543 		    clear_tv(tv_dest);
2544 		    ectx->ec_stack.ga_len -= 4;
2545 		    clear_tv(tv);
2546 
2547 		    if (status == FAIL)
2548 			goto on_error;
2549 		}
2550 		break;
2551 
2552 	    // load or store variable or argument from outer scope
2553 	    case ISN_LOADOUTER:
2554 	    case ISN_STOREOUTER:
2555 		{
2556 		    int		depth = iptr->isn_arg.outer.outer_depth;
2557 		    outer_T	*outer = ectx->ec_outer_ref == NULL ? NULL
2558 						: ectx->ec_outer_ref->or_outer;
2559 
2560 		    while (depth > 1 && outer != NULL)
2561 		    {
2562 			outer = outer->out_up;
2563 			--depth;
2564 		    }
2565 		    if (outer == NULL)
2566 		    {
2567 			SOURCING_LNUM = iptr->isn_lnum;
2568 			iemsg("LOADOUTER depth more than scope levels");
2569 			goto theend;
2570 		    }
2571 		    tv = ((typval_T *)outer->out_stack->ga_data)
2572 				    + outer->out_frame_idx + STACK_FRAME_SIZE
2573 				    + iptr->isn_arg.outer.outer_idx;
2574 		    if (iptr->isn_type == ISN_LOADOUTER)
2575 		    {
2576 			if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
2577 			    goto theend;
2578 			copy_tv(tv, STACK_TV_BOT(0));
2579 			++ectx->ec_stack.ga_len;
2580 		    }
2581 		    else
2582 		    {
2583 			--ectx->ec_stack.ga_len;
2584 			clear_tv(tv);
2585 			*tv = *STACK_TV_BOT(0);
2586 		    }
2587 		}
2588 		break;
2589 
2590 	    // unlet item in list or dict variable
2591 	    case ISN_UNLETINDEX:
2592 		{
2593 		    typval_T	*tv_idx = STACK_TV_BOT(-2);
2594 		    typval_T	*tv_dest = STACK_TV_BOT(-1);
2595 		    int		status = OK;
2596 
2597 		    // Stack contains:
2598 		    // -2 index
2599 		    // -1 dict or list
2600 		    if (tv_dest->v_type == VAR_DICT)
2601 		    {
2602 			// unlet a dict item, index must be a string
2603 			if (tv_idx->v_type != VAR_STRING)
2604 			{
2605 			    SOURCING_LNUM = iptr->isn_lnum;
2606 			    semsg(_(e_expected_str_but_got_str),
2607 					vartype_name(VAR_STRING),
2608 					vartype_name(tv_idx->v_type));
2609 			    status = FAIL;
2610 			}
2611 			else
2612 			{
2613 			    dict_T	*d = tv_dest->vval.v_dict;
2614 			    char_u	*key = tv_idx->vval.v_string;
2615 			    dictitem_T  *di = NULL;
2616 
2617 			    if (key == NULL)
2618 				key = (char_u *)"";
2619 			    if (d != NULL)
2620 				di = dict_find(d, key, (int)STRLEN(key));
2621 			    if (di == NULL)
2622 			    {
2623 				// NULL dict is equivalent to empty dict
2624 				SOURCING_LNUM = iptr->isn_lnum;
2625 				semsg(_(e_dictkey), key);
2626 				status = FAIL;
2627 			    }
2628 			    else
2629 			    {
2630 				// TODO: check for dict or item locked
2631 				dictitem_remove(d, di);
2632 			    }
2633 			}
2634 		    }
2635 		    else if (tv_dest->v_type == VAR_LIST)
2636 		    {
2637 			// unlet a List item, index must be a number
2638 			SOURCING_LNUM = iptr->isn_lnum;
2639 			if (check_for_number(tv_idx) == FAIL)
2640 			{
2641 			    status = FAIL;
2642 			}
2643 			else
2644 			{
2645 			    list_T	*l = tv_dest->vval.v_list;
2646 			    long	n = (long)tv_idx->vval.v_number;
2647 			    listitem_T	*li = NULL;
2648 
2649 			    li = list_find(l, n);
2650 			    if (li == NULL)
2651 			    {
2652 				SOURCING_LNUM = iptr->isn_lnum;
2653 				semsg(_(e_listidx), n);
2654 				status = FAIL;
2655 			    }
2656 			    else
2657 				// TODO: check for list or item locked
2658 				listitem_remove(l, li);
2659 			}
2660 		    }
2661 		    else
2662 		    {
2663 			status = FAIL;
2664 			semsg(_(e_cannot_index_str),
2665 						vartype_name(tv_dest->v_type));
2666 		    }
2667 
2668 		    clear_tv(tv_idx);
2669 		    clear_tv(tv_dest);
2670 		    ectx->ec_stack.ga_len -= 2;
2671 		    if (status == FAIL)
2672 			goto on_error;
2673 		}
2674 		break;
2675 
2676 	    // unlet range of items in list variable
2677 	    case ISN_UNLETRANGE:
2678 		{
2679 		    // Stack contains:
2680 		    // -3 index1
2681 		    // -2 index2
2682 		    // -1 dict or list
2683 		    typval_T	*tv_idx1 = STACK_TV_BOT(-3);
2684 		    typval_T	*tv_idx2 = STACK_TV_BOT(-2);
2685 		    typval_T	*tv_dest = STACK_TV_BOT(-1);
2686 		    int		status = OK;
2687 
2688 		    if (tv_dest->v_type == VAR_LIST)
2689 		    {
2690 			// indexes must be a number
2691 			SOURCING_LNUM = iptr->isn_lnum;
2692 			if (check_for_number(tv_idx1) == FAIL
2693 				|| (tv_idx2->v_type != VAR_SPECIAL
2694 					 && check_for_number(tv_idx2) == FAIL))
2695 			{
2696 			    status = FAIL;
2697 			}
2698 			else
2699 			{
2700 			    list_T	*l = tv_dest->vval.v_list;
2701 			    long	n1 = (long)tv_idx1->vval.v_number;
2702 			    long	n2 = tv_idx2->v_type == VAR_SPECIAL
2703 					    ? 0 : (long)tv_idx2->vval.v_number;
2704 			    listitem_T	*li;
2705 
2706 			    li = list_find_index(l, &n1);
2707 			    if (li == NULL)
2708 				status = FAIL;
2709 			    else
2710 			    {
2711 				if (n1 < 0)
2712 				    n1 = list_idx_of_item(l, li);
2713 				if (n2 < 0)
2714 				{
2715 				    listitem_T *li2 = list_find(l, n2);
2716 
2717 				    if (li2 == NULL)
2718 					status = FAIL;
2719 				    else
2720 					n2 = list_idx_of_item(l, li2);
2721 				}
2722 				if (status != FAIL
2723 					&& tv_idx2->v_type != VAR_SPECIAL
2724 					&& n2 < n1)
2725 				{
2726 				    semsg(_(e_listidx), n2);
2727 				    status = FAIL;
2728 				}
2729 				if (status != FAIL
2730 					&& list_unlet_range(l, li, NULL, n1,
2731 					    tv_idx2->v_type != VAR_SPECIAL, n2)
2732 								       == FAIL)
2733 				    status = FAIL;
2734 			    }
2735 			}
2736 		    }
2737 		    else
2738 		    {
2739 			status = FAIL;
2740 			SOURCING_LNUM = iptr->isn_lnum;
2741 			semsg(_(e_cannot_index_str),
2742 						vartype_name(tv_dest->v_type));
2743 		    }
2744 
2745 		    clear_tv(tv_idx1);
2746 		    clear_tv(tv_idx2);
2747 		    clear_tv(tv_dest);
2748 		    ectx->ec_stack.ga_len -= 3;
2749 		    if (status == FAIL)
2750 			goto on_error;
2751 		}
2752 		break;
2753 
2754 	    // push constant
2755 	    case ISN_PUSHNR:
2756 	    case ISN_PUSHBOOL:
2757 	    case ISN_PUSHSPEC:
2758 	    case ISN_PUSHF:
2759 	    case ISN_PUSHS:
2760 	    case ISN_PUSHBLOB:
2761 	    case ISN_PUSHFUNC:
2762 	    case ISN_PUSHCHANNEL:
2763 	    case ISN_PUSHJOB:
2764 		if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
2765 		    goto theend;
2766 		tv = STACK_TV_BOT(0);
2767 		tv->v_lock = 0;
2768 		++ectx->ec_stack.ga_len;
2769 		switch (iptr->isn_type)
2770 		{
2771 		    case ISN_PUSHNR:
2772 			tv->v_type = VAR_NUMBER;
2773 			tv->vval.v_number = iptr->isn_arg.number;
2774 			break;
2775 		    case ISN_PUSHBOOL:
2776 			tv->v_type = VAR_BOOL;
2777 			tv->vval.v_number = iptr->isn_arg.number;
2778 			break;
2779 		    case ISN_PUSHSPEC:
2780 			tv->v_type = VAR_SPECIAL;
2781 			tv->vval.v_number = iptr->isn_arg.number;
2782 			break;
2783 #ifdef FEAT_FLOAT
2784 		    case ISN_PUSHF:
2785 			tv->v_type = VAR_FLOAT;
2786 			tv->vval.v_float = iptr->isn_arg.fnumber;
2787 			break;
2788 #endif
2789 		    case ISN_PUSHBLOB:
2790 			blob_copy(iptr->isn_arg.blob, tv);
2791 			break;
2792 		    case ISN_PUSHFUNC:
2793 			tv->v_type = VAR_FUNC;
2794 			if (iptr->isn_arg.string == NULL)
2795 			    tv->vval.v_string = NULL;
2796 			else
2797 			    tv->vval.v_string =
2798 					     vim_strsave(iptr->isn_arg.string);
2799 			break;
2800 		    case ISN_PUSHCHANNEL:
2801 #ifdef FEAT_JOB_CHANNEL
2802 			tv->v_type = VAR_CHANNEL;
2803 			tv->vval.v_channel = iptr->isn_arg.channel;
2804 			if (tv->vval.v_channel != NULL)
2805 			    ++tv->vval.v_channel->ch_refcount;
2806 #endif
2807 			break;
2808 		    case ISN_PUSHJOB:
2809 #ifdef FEAT_JOB_CHANNEL
2810 			tv->v_type = VAR_JOB;
2811 			tv->vval.v_job = iptr->isn_arg.job;
2812 			if (tv->vval.v_job != NULL)
2813 			    ++tv->vval.v_job->jv_refcount;
2814 #endif
2815 			break;
2816 		    default:
2817 			tv->v_type = VAR_STRING;
2818 			tv->vval.v_string = vim_strsave(
2819 				iptr->isn_arg.string == NULL
2820 					? (char_u *)"" : iptr->isn_arg.string);
2821 		}
2822 		break;
2823 
2824 	    case ISN_UNLET:
2825 		if (do_unlet(iptr->isn_arg.unlet.ul_name,
2826 				       iptr->isn_arg.unlet.ul_forceit) == FAIL)
2827 		    goto on_error;
2828 		break;
2829 	    case ISN_UNLETENV:
2830 		vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2831 		break;
2832 
2833 	    case ISN_LOCKCONST:
2834 		item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2835 		break;
2836 
2837 	    // create a list from items on the stack; uses a single allocation
2838 	    // for the list header and the items
2839 	    case ISN_NEWLIST:
2840 		if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
2841 		    goto theend;
2842 		break;
2843 
2844 	    // create a dict from items on the stack
2845 	    case ISN_NEWDICT:
2846 		{
2847 		    int		count = iptr->isn_arg.number;
2848 		    dict_T	*dict = dict_alloc();
2849 		    dictitem_T	*item;
2850 		    char_u	*key;
2851 		    int		idx;
2852 
2853 		    if (unlikely(dict == NULL))
2854 			goto theend;
2855 		    for (idx = 0; idx < count; ++idx)
2856 		    {
2857 			// have already checked key type is VAR_STRING
2858 			tv = STACK_TV_BOT(2 * (idx - count));
2859 			// check key is unique
2860 			key = tv->vval.v_string == NULL
2861 					    ? (char_u *)"" : tv->vval.v_string;
2862 			item = dict_find(dict, key, -1);
2863 			if (item != NULL)
2864 			{
2865 			    SOURCING_LNUM = iptr->isn_lnum;
2866 			    semsg(_(e_duplicate_key), key);
2867 			    dict_unref(dict);
2868 			    goto on_error;
2869 			}
2870 			item = dictitem_alloc(key);
2871 			clear_tv(tv);
2872 			if (unlikely(item == NULL))
2873 			{
2874 			    dict_unref(dict);
2875 			    goto theend;
2876 			}
2877 			item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2878 			item->di_tv.v_lock = 0;
2879 			if (dict_add(dict, item) == FAIL)
2880 			{
2881 			    // can this ever happen?
2882 			    dict_unref(dict);
2883 			    goto theend;
2884 			}
2885 		    }
2886 
2887 		    if (count > 0)
2888 			ectx->ec_stack.ga_len -= 2 * count - 1;
2889 		    else if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
2890 			goto theend;
2891 		    else
2892 			++ectx->ec_stack.ga_len;
2893 		    tv = STACK_TV_BOT(-1);
2894 		    tv->v_type = VAR_DICT;
2895 		    tv->v_lock = 0;
2896 		    tv->vval.v_dict = dict;
2897 		    ++dict->dv_refcount;
2898 		}
2899 		break;
2900 
2901 	    // call a :def function
2902 	    case ISN_DCALL:
2903 		SOURCING_LNUM = iptr->isn_lnum;
2904 		if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2905 				NULL,
2906 				iptr->isn_arg.dfunc.cdf_argcount,
2907 				ectx) == FAIL)
2908 		    goto on_error;
2909 		break;
2910 
2911 	    // call a builtin function
2912 	    case ISN_BCALL:
2913 		SOURCING_LNUM = iptr->isn_lnum;
2914 		if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2915 			      iptr->isn_arg.bfunc.cbf_argcount,
2916 			      ectx) == FAIL)
2917 		    goto on_error;
2918 		break;
2919 
2920 	    // call a funcref or partial
2921 	    case ISN_PCALL:
2922 		{
2923 		    cpfunc_T	*pfunc = &iptr->isn_arg.pfunc;
2924 		    int		r;
2925 		    typval_T	partial_tv;
2926 
2927 		    SOURCING_LNUM = iptr->isn_lnum;
2928 		    if (pfunc->cpf_top)
2929 		    {
2930 			// funcref is above the arguments
2931 			tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2932 		    }
2933 		    else
2934 		    {
2935 			// Get the funcref from the stack.
2936 			--ectx->ec_stack.ga_len;
2937 			partial_tv = *STACK_TV_BOT(0);
2938 			tv = &partial_tv;
2939 		    }
2940 		    r = call_partial(tv, pfunc->cpf_argcount, ectx);
2941 		    if (tv == &partial_tv)
2942 			clear_tv(&partial_tv);
2943 		    if (r == FAIL)
2944 			goto on_error;
2945 		}
2946 		break;
2947 
2948 	    case ISN_PCALL_END:
2949 		// PCALL finished, arguments have been consumed and replaced by
2950 		// the return value.  Now clear the funcref from the stack,
2951 		// and move the return value in its place.
2952 		--ectx->ec_stack.ga_len;
2953 		clear_tv(STACK_TV_BOT(-1));
2954 		*STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2955 		break;
2956 
2957 	    // call a user defined function or funcref/partial
2958 	    case ISN_UCALL:
2959 		{
2960 		    cufunc_T	*cufunc = &iptr->isn_arg.ufunc;
2961 
2962 		    SOURCING_LNUM = iptr->isn_lnum;
2963 		    if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
2964 							   ectx, iptr) == FAIL)
2965 			goto on_error;
2966 		}
2967 		break;
2968 
2969 	    // return from a :def function call without a value
2970 	    case ISN_RETURN_VOID:
2971 		if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
2972 		    goto theend;
2973 		tv = STACK_TV_BOT(0);
2974 		++ectx->ec_stack.ga_len;
2975 		tv->v_type = VAR_VOID;
2976 		tv->vval.v_number = 0;
2977 		tv->v_lock = 0;
2978 		// FALLTHROUGH
2979 
2980 	    // return from a :def function call with what is on the stack
2981 	    case ISN_RETURN:
2982 		{
2983 		    garray_T	*trystack = &ectx->ec_trystack;
2984 		    trycmd_T    *trycmd = NULL;
2985 
2986 		    if (trystack->ga_len > 0)
2987 			trycmd = ((trycmd_T *)trystack->ga_data)
2988 							+ trystack->ga_len - 1;
2989 		    if (trycmd != NULL
2990 				 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
2991 		    {
2992 			// jump to ":finally" or ":endtry"
2993 			if (trycmd->tcd_finally_idx != 0)
2994 			    ectx->ec_iidx = trycmd->tcd_finally_idx;
2995 			else
2996 			    ectx->ec_iidx = trycmd->tcd_endtry_idx;
2997 			trycmd->tcd_return = TRUE;
2998 		    }
2999 		    else
3000 			goto func_return;
3001 		}
3002 		break;
3003 
3004 	    // push a partial, a reference to a compiled function
3005 	    case ISN_FUNCREF:
3006 		{
3007 		    partial_T   *pt = ALLOC_CLEAR_ONE(partial_T);
3008 		    dfunc_T	*pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3009 					       + iptr->isn_arg.funcref.fr_func;
3010 
3011 		    if (pt == NULL)
3012 			goto theend;
3013 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
3014 		    {
3015 			vim_free(pt);
3016 			goto theend;
3017 		    }
3018 		    if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
3019 								 ectx) == FAIL)
3020 			goto theend;
3021 		    tv = STACK_TV_BOT(0);
3022 		    ++ectx->ec_stack.ga_len;
3023 		    tv->vval.v_partial = pt;
3024 		    tv->v_type = VAR_PARTIAL;
3025 		    tv->v_lock = 0;
3026 		}
3027 		break;
3028 
3029 	    // Create a global function from a lambda.
3030 	    case ISN_NEWFUNC:
3031 		{
3032 		    newfunc_T	*newfunc = &iptr->isn_arg.newfunc;
3033 
3034 		    if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
3035 								 ectx) == FAIL)
3036 			goto theend;
3037 		}
3038 		break;
3039 
3040 	    // List functions
3041 	    case ISN_DEF:
3042 		if (iptr->isn_arg.string == NULL)
3043 		    list_functions(NULL);
3044 		else
3045 		{
3046 		    exarg_T ea;
3047 
3048 		    CLEAR_FIELD(ea);
3049 		    ea.cmd = ea.arg = iptr->isn_arg.string;
3050 		    define_function(&ea, NULL);
3051 		}
3052 		break;
3053 
3054 	    // jump if a condition is met
3055 	    case ISN_JUMP:
3056 		{
3057 		    jumpwhen_T	when = iptr->isn_arg.jump.jump_when;
3058 		    int		error = FALSE;
3059 		    int		jump = TRUE;
3060 
3061 		    if (when != JUMP_ALWAYS)
3062 		    {
3063 			tv = STACK_TV_BOT(-1);
3064 			if (when == JUMP_IF_COND_FALSE
3065 				|| when == JUMP_IF_FALSE
3066 				|| when == JUMP_IF_COND_TRUE)
3067 			{
3068 			    SOURCING_LNUM = iptr->isn_lnum;
3069 			    jump = tv_get_bool_chk(tv, &error);
3070 			    if (error)
3071 				goto on_error;
3072 			}
3073 			else
3074 			    jump = tv2bool(tv);
3075 			if (when == JUMP_IF_FALSE
3076 					     || when == JUMP_AND_KEEP_IF_FALSE
3077 					     || when == JUMP_IF_COND_FALSE)
3078 			    jump = !jump;
3079 			if (when == JUMP_IF_FALSE || !jump)
3080 			{
3081 			    // drop the value from the stack
3082 			    clear_tv(tv);
3083 			    --ectx->ec_stack.ga_len;
3084 			}
3085 		    }
3086 		    if (jump)
3087 			ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
3088 		}
3089 		break;
3090 
3091 	    // Jump if an argument with a default value was already set and not
3092 	    // v:none.
3093 	    case ISN_JUMP_IF_ARG_SET:
3094 		tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3095 		if (tv->v_type != VAR_UNKNOWN
3096 			&& !(tv->v_type == VAR_SPECIAL
3097 					    && tv->vval.v_number == VVAL_NONE))
3098 		    ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
3099 		break;
3100 
3101 	    // top of a for loop
3102 	    case ISN_FOR:
3103 		{
3104 		    typval_T	*ltv = STACK_TV_BOT(-1);
3105 		    typval_T	*idxtv =
3106 				   STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3107 
3108 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
3109 			goto theend;
3110 		    if (ltv->v_type == VAR_LIST)
3111 		    {
3112 			list_T *list = ltv->vval.v_list;
3113 
3114 			// push the next item from the list
3115 			++idxtv->vval.v_number;
3116 			if (list == NULL
3117 				       || idxtv->vval.v_number >= list->lv_len)
3118 			{
3119 			    // past the end of the list, jump to "endfor"
3120 			    ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3121 			    may_restore_cmdmod(&ectx->ec_funclocal);
3122 			}
3123 			else if (list->lv_first == &range_list_item)
3124 			{
3125 			    // non-materialized range() list
3126 			    tv = STACK_TV_BOT(0);
3127 			    tv->v_type = VAR_NUMBER;
3128 			    tv->v_lock = 0;
3129 			    tv->vval.v_number = list_find_nr(
3130 					     list, idxtv->vval.v_number, NULL);
3131 			    ++ectx->ec_stack.ga_len;
3132 			}
3133 			else
3134 			{
3135 			    listitem_T *li = list_find(list,
3136 							 idxtv->vval.v_number);
3137 
3138 			    copy_tv(&li->li_tv, STACK_TV_BOT(0));
3139 			    ++ectx->ec_stack.ga_len;
3140 			}
3141 		    }
3142 		    else if (ltv->v_type == VAR_STRING)
3143 		    {
3144 			char_u	*str = ltv->vval.v_string;
3145 
3146 			// The index is for the last byte of the previous
3147 			// character.
3148 			++idxtv->vval.v_number;
3149 			if (str == NULL || str[idxtv->vval.v_number] == NUL)
3150 			{
3151 			    // past the end of the string, jump to "endfor"
3152 			    ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3153 			    may_restore_cmdmod(&ectx->ec_funclocal);
3154 			}
3155 			else
3156 			{
3157 			    int	clen = mb_ptr2len(str + idxtv->vval.v_number);
3158 
3159 			    // Push the next character from the string.
3160 			    tv = STACK_TV_BOT(0);
3161 			    tv->v_type = VAR_STRING;
3162 			    tv->vval.v_string = vim_strnsave(
3163 					     str + idxtv->vval.v_number, clen);
3164 			    ++ectx->ec_stack.ga_len;
3165 			    idxtv->vval.v_number += clen - 1;
3166 			}
3167 		    }
3168 		    else if (ltv->v_type == VAR_BLOB)
3169 		    {
3170 			blob_T	*blob = ltv->vval.v_blob;
3171 
3172 			// When we get here the first time make a copy of the
3173 			// blob, so that the iteration still works when it is
3174 			// changed.
3175 			if (idxtv->vval.v_number == -1 && blob != NULL)
3176 			{
3177 			    blob_copy(blob, ltv);
3178 			    blob_unref(blob);
3179 			    blob = ltv->vval.v_blob;
3180 			}
3181 
3182 			// The index is for the previous byte.
3183 			++idxtv->vval.v_number;
3184 			if (blob == NULL
3185 				     || idxtv->vval.v_number >= blob_len(blob))
3186 			{
3187 			    // past the end of the blob, jump to "endfor"
3188 			    ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3189 			    may_restore_cmdmod(&ectx->ec_funclocal);
3190 			}
3191 			else
3192 			{
3193 			    // Push the next byte from the blob.
3194 			    tv = STACK_TV_BOT(0);
3195 			    tv->v_type = VAR_NUMBER;
3196 			    tv->vval.v_number = blob_get(blob,
3197 							 idxtv->vval.v_number);
3198 			    ++ectx->ec_stack.ga_len;
3199 			}
3200 		    }
3201 		    else
3202 		    {
3203 			semsg(_(e_for_loop_on_str_not_supported),
3204 						    vartype_name(ltv->v_type));
3205 			goto theend;
3206 		    }
3207 		}
3208 		break;
3209 
3210 	    // start of ":try" block
3211 	    case ISN_TRY:
3212 		{
3213 		    trycmd_T    *trycmd = NULL;
3214 
3215 		    if (unlikely(GA_GROW(&ectx->ec_trystack, 1) == FAIL))
3216 			goto theend;
3217 		    trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3218 						     + ectx->ec_trystack.ga_len;
3219 		    ++ectx->ec_trystack.ga_len;
3220 		    ++trylevel;
3221 		    CLEAR_POINTER(trycmd);
3222 		    trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3223 		    trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
3224 		    trycmd->tcd_catch_idx =
3225 					  iptr->isn_arg.try.try_ref->try_catch;
3226 		    trycmd->tcd_finally_idx =
3227 					iptr->isn_arg.try.try_ref->try_finally;
3228 		    trycmd->tcd_endtry_idx =
3229 					 iptr->isn_arg.try.try_ref->try_endtry;
3230 		}
3231 		break;
3232 
3233 	    case ISN_PUSHEXC:
3234 		if (current_exception == NULL)
3235 		{
3236 		    SOURCING_LNUM = iptr->isn_lnum;
3237 		    iemsg("Evaluating catch while current_exception is NULL");
3238 		    goto theend;
3239 		}
3240 		if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
3241 		    goto theend;
3242 		tv = STACK_TV_BOT(0);
3243 		++ectx->ec_stack.ga_len;
3244 		tv->v_type = VAR_STRING;
3245 		tv->v_lock = 0;
3246 		tv->vval.v_string = vim_strsave(
3247 					   (char_u *)current_exception->value);
3248 		break;
3249 
3250 	    case ISN_CATCH:
3251 		{
3252 		    garray_T	*trystack = &ectx->ec_trystack;
3253 
3254 		    may_restore_cmdmod(&ectx->ec_funclocal);
3255 		    if (trystack->ga_len > 0)
3256 		    {
3257 			trycmd_T    *trycmd = ((trycmd_T *)trystack->ga_data)
3258 							+ trystack->ga_len - 1;
3259 			trycmd->tcd_caught = TRUE;
3260 			trycmd->tcd_did_throw = FALSE;
3261 		    }
3262 		    did_emsg = got_int = did_throw = FALSE;
3263 		    force_abort = need_rethrow = FALSE;
3264 		    catch_exception(current_exception);
3265 		}
3266 		break;
3267 
3268 	    case ISN_TRYCONT:
3269 		{
3270 		    garray_T	*trystack = &ectx->ec_trystack;
3271 		    trycont_T	*trycont = &iptr->isn_arg.trycont;
3272 		    int		i;
3273 		    trycmd_T    *trycmd;
3274 		    int		iidx = trycont->tct_where;
3275 
3276 		    if (trystack->ga_len < trycont->tct_levels)
3277 		    {
3278 			siemsg("TRYCONT: expected %d levels, found %d",
3279 					trycont->tct_levels, trystack->ga_len);
3280 			goto theend;
3281 		    }
3282 		    // Make :endtry jump to any outer try block and the last
3283 		    // :endtry inside the loop to the loop start.
3284 		    for (i = trycont->tct_levels; i > 0; --i)
3285 		    {
3286 			trycmd = ((trycmd_T *)trystack->ga_data)
3287 							+ trystack->ga_len - i;
3288 			// Add one to tcd_cont to be able to jump to
3289 			// instruction with index zero.
3290 			trycmd->tcd_cont = iidx + 1;
3291 			iidx = trycmd->tcd_finally_idx == 0
3292 			    ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
3293 		    }
3294 		    // jump to :finally or :endtry of current try statement
3295 		    ectx->ec_iidx = iidx;
3296 		}
3297 		break;
3298 
3299 	    case ISN_FINALLY:
3300 		{
3301 		    garray_T	*trystack = &ectx->ec_trystack;
3302 		    trycmd_T    *trycmd = ((trycmd_T *)trystack->ga_data)
3303 							+ trystack->ga_len - 1;
3304 
3305 		    // Reset the index to avoid a return statement jumps here
3306 		    // again.
3307 		    trycmd->tcd_finally_idx = 0;
3308 		    break;
3309 		}
3310 
3311 	    // end of ":try" block
3312 	    case ISN_ENDTRY:
3313 		{
3314 		    garray_T	*trystack = &ectx->ec_trystack;
3315 
3316 		    if (trystack->ga_len > 0)
3317 		    {
3318 			trycmd_T    *trycmd;
3319 
3320 			--trystack->ga_len;
3321 			--trylevel;
3322 			trycmd = ((trycmd_T *)trystack->ga_data)
3323 							    + trystack->ga_len;
3324 			if (trycmd->tcd_did_throw)
3325 			    did_throw = TRUE;
3326 			if (trycmd->tcd_caught && current_exception != NULL)
3327 			{
3328 			    // discard the exception
3329 			    if (caught_stack == current_exception)
3330 				caught_stack = caught_stack->caught;
3331 			    discard_current_exception();
3332 			}
3333 
3334 			if (trycmd->tcd_return)
3335 			    goto func_return;
3336 
3337 			while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
3338 			{
3339 			    --ectx->ec_stack.ga_len;
3340 			    clear_tv(STACK_TV_BOT(0));
3341 			}
3342 			if (trycmd->tcd_cont != 0)
3343 			    // handling :continue: jump to outer try block or
3344 			    // start of the loop
3345 			    ectx->ec_iidx = trycmd->tcd_cont - 1;
3346 		    }
3347 		}
3348 		break;
3349 
3350 	    case ISN_THROW:
3351 		{
3352 		    garray_T	*trystack = &ectx->ec_trystack;
3353 
3354 		    if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3355 		    {
3356 			// throwing an exception while using "silent!" causes
3357 			// the function to abort but not display an error.
3358 			tv = STACK_TV_BOT(-1);
3359 			clear_tv(tv);
3360 			tv->v_type = VAR_NUMBER;
3361 			tv->vval.v_number = 0;
3362 			goto done;
3363 		    }
3364 		    --ectx->ec_stack.ga_len;
3365 		    tv = STACK_TV_BOT(0);
3366 		    if (tv->vval.v_string == NULL
3367 				       || *skipwhite(tv->vval.v_string) == NUL)
3368 		    {
3369 			vim_free(tv->vval.v_string);
3370 			SOURCING_LNUM = iptr->isn_lnum;
3371 			emsg(_(e_throw_with_empty_string));
3372 			goto theend;
3373 		    }
3374 
3375 		    // Inside a "catch" we need to first discard the caught
3376 		    // exception.
3377 		    if (trystack->ga_len > 0)
3378 		    {
3379 			trycmd_T    *trycmd = ((trycmd_T *)trystack->ga_data)
3380 							+ trystack->ga_len - 1;
3381 			if (trycmd->tcd_caught && current_exception != NULL)
3382 			{
3383 			    // discard the exception
3384 			    if (caught_stack == current_exception)
3385 				caught_stack = caught_stack->caught;
3386 			    discard_current_exception();
3387 			    trycmd->tcd_caught = FALSE;
3388 			}
3389 		    }
3390 
3391 		    if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3392 								       == FAIL)
3393 		    {
3394 			vim_free(tv->vval.v_string);
3395 			goto theend;
3396 		    }
3397 		    did_throw = TRUE;
3398 		}
3399 		break;
3400 
3401 	    // compare with special values
3402 	    case ISN_COMPAREBOOL:
3403 	    case ISN_COMPARESPECIAL:
3404 		{
3405 		    typval_T	*tv1 = STACK_TV_BOT(-2);
3406 		    typval_T	*tv2 = STACK_TV_BOT(-1);
3407 		    varnumber_T arg1 = tv1->vval.v_number;
3408 		    varnumber_T arg2 = tv2->vval.v_number;
3409 		    int		res;
3410 
3411 		    switch (iptr->isn_arg.op.op_type)
3412 		    {
3413 			case EXPR_EQUAL: res = arg1 == arg2; break;
3414 			case EXPR_NEQUAL: res = arg1 != arg2; break;
3415 			default: res = 0; break;
3416 		    }
3417 
3418 		    --ectx->ec_stack.ga_len;
3419 		    tv1->v_type = VAR_BOOL;
3420 		    tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3421 		}
3422 		break;
3423 
3424 	    // Operation with two number arguments
3425 	    case ISN_OPNR:
3426 	    case ISN_COMPARENR:
3427 		{
3428 		    typval_T	*tv1 = STACK_TV_BOT(-2);
3429 		    typval_T	*tv2 = STACK_TV_BOT(-1);
3430 		    varnumber_T arg1 = tv1->vval.v_number;
3431 		    varnumber_T arg2 = tv2->vval.v_number;
3432 		    varnumber_T res;
3433 
3434 		    switch (iptr->isn_arg.op.op_type)
3435 		    {
3436 			case EXPR_MULT: res = arg1 * arg2; break;
3437 			case EXPR_DIV: res = arg1 / arg2; break;
3438 			case EXPR_REM: res = arg1 % arg2; break;
3439 			case EXPR_SUB: res = arg1 - arg2; break;
3440 			case EXPR_ADD: res = arg1 + arg2; break;
3441 
3442 			case EXPR_EQUAL: res = arg1 == arg2; break;
3443 			case EXPR_NEQUAL: res = arg1 != arg2; break;
3444 			case EXPR_GREATER: res = arg1 > arg2; break;
3445 			case EXPR_GEQUAL: res = arg1 >= arg2; break;
3446 			case EXPR_SMALLER: res = arg1 < arg2; break;
3447 			case EXPR_SEQUAL: res = arg1 <= arg2; break;
3448 			default: res = 0; break;
3449 		    }
3450 
3451 		    --ectx->ec_stack.ga_len;
3452 		    if (iptr->isn_type == ISN_COMPARENR)
3453 		    {
3454 			tv1->v_type = VAR_BOOL;
3455 			tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3456 		    }
3457 		    else
3458 			tv1->vval.v_number = res;
3459 		}
3460 		break;
3461 
3462 	    // Computation with two float arguments
3463 	    case ISN_OPFLOAT:
3464 	    case ISN_COMPAREFLOAT:
3465 #ifdef FEAT_FLOAT
3466 		{
3467 		    typval_T	*tv1 = STACK_TV_BOT(-2);
3468 		    typval_T	*tv2 = STACK_TV_BOT(-1);
3469 		    float_T	arg1 = tv1->vval.v_float;
3470 		    float_T	arg2 = tv2->vval.v_float;
3471 		    float_T	res = 0;
3472 		    int		cmp = FALSE;
3473 
3474 		    switch (iptr->isn_arg.op.op_type)
3475 		    {
3476 			case EXPR_MULT: res = arg1 * arg2; break;
3477 			case EXPR_DIV: res = arg1 / arg2; break;
3478 			case EXPR_SUB: res = arg1 - arg2; break;
3479 			case EXPR_ADD: res = arg1 + arg2; break;
3480 
3481 			case EXPR_EQUAL: cmp = arg1 == arg2; break;
3482 			case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3483 			case EXPR_GREATER: cmp = arg1 > arg2; break;
3484 			case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3485 			case EXPR_SMALLER: cmp = arg1 < arg2; break;
3486 			case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3487 			default: cmp = 0; break;
3488 		    }
3489 		    --ectx->ec_stack.ga_len;
3490 		    if (iptr->isn_type == ISN_COMPAREFLOAT)
3491 		    {
3492 			tv1->v_type = VAR_BOOL;
3493 			tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3494 		    }
3495 		    else
3496 			tv1->vval.v_float = res;
3497 		}
3498 #endif
3499 		break;
3500 
3501 	    case ISN_COMPARELIST:
3502 		{
3503 		    typval_T	*tv1 = STACK_TV_BOT(-2);
3504 		    typval_T	*tv2 = STACK_TV_BOT(-1);
3505 		    list_T	*arg1 = tv1->vval.v_list;
3506 		    list_T	*arg2 = tv2->vval.v_list;
3507 		    int		cmp = FALSE;
3508 		    int		ic = iptr->isn_arg.op.op_ic;
3509 
3510 		    switch (iptr->isn_arg.op.op_type)
3511 		    {
3512 			case EXPR_EQUAL: cmp =
3513 				      list_equal(arg1, arg2, ic, FALSE); break;
3514 			case EXPR_NEQUAL: cmp =
3515 				     !list_equal(arg1, arg2, ic, FALSE); break;
3516 			case EXPR_IS: cmp = arg1 == arg2; break;
3517 			case EXPR_ISNOT: cmp = arg1 != arg2; break;
3518 			default: cmp = 0; break;
3519 		    }
3520 		    --ectx->ec_stack.ga_len;
3521 		    clear_tv(tv1);
3522 		    clear_tv(tv2);
3523 		    tv1->v_type = VAR_BOOL;
3524 		    tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3525 		}
3526 		break;
3527 
3528 	    case ISN_COMPAREBLOB:
3529 		{
3530 		    typval_T	*tv1 = STACK_TV_BOT(-2);
3531 		    typval_T	*tv2 = STACK_TV_BOT(-1);
3532 		    blob_T	*arg1 = tv1->vval.v_blob;
3533 		    blob_T	*arg2 = tv2->vval.v_blob;
3534 		    int		cmp = FALSE;
3535 
3536 		    switch (iptr->isn_arg.op.op_type)
3537 		    {
3538 			case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3539 			case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3540 			case EXPR_IS: cmp = arg1 == arg2; break;
3541 			case EXPR_ISNOT: cmp = arg1 != arg2; break;
3542 			default: cmp = 0; break;
3543 		    }
3544 		    --ectx->ec_stack.ga_len;
3545 		    clear_tv(tv1);
3546 		    clear_tv(tv2);
3547 		    tv1->v_type = VAR_BOOL;
3548 		    tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3549 		}
3550 		break;
3551 
3552 		// TODO: handle separately
3553 	    case ISN_COMPARESTRING:
3554 	    case ISN_COMPAREDICT:
3555 	    case ISN_COMPAREFUNC:
3556 	    case ISN_COMPAREANY:
3557 		{
3558 		    typval_T	*tv1 = STACK_TV_BOT(-2);
3559 		    typval_T	*tv2 = STACK_TV_BOT(-1);
3560 		    exprtype_T	exprtype = iptr->isn_arg.op.op_type;
3561 		    int		ic = iptr->isn_arg.op.op_ic;
3562 
3563 		    SOURCING_LNUM = iptr->isn_lnum;
3564 		    typval_compare(tv1, tv2, exprtype, ic);
3565 		    clear_tv(tv2);
3566 		    --ectx->ec_stack.ga_len;
3567 		}
3568 		break;
3569 
3570 	    case ISN_ADDLIST:
3571 	    case ISN_ADDBLOB:
3572 		{
3573 		    typval_T *tv1 = STACK_TV_BOT(-2);
3574 		    typval_T *tv2 = STACK_TV_BOT(-1);
3575 
3576 		    // add two lists or blobs
3577 		    if (iptr->isn_type == ISN_ADDLIST)
3578 			eval_addlist(tv1, tv2);
3579 		    else
3580 			eval_addblob(tv1, tv2);
3581 		    clear_tv(tv2);
3582 		    --ectx->ec_stack.ga_len;
3583 		}
3584 		break;
3585 
3586 	    case ISN_LISTAPPEND:
3587 		{
3588 		    typval_T	*tv1 = STACK_TV_BOT(-2);
3589 		    typval_T	*tv2 = STACK_TV_BOT(-1);
3590 		    list_T	*l = tv1->vval.v_list;
3591 
3592 		    // add an item to a list
3593 		    if (l == NULL)
3594 		    {
3595 			SOURCING_LNUM = iptr->isn_lnum;
3596 			emsg(_(e_cannot_add_to_null_list));
3597 			goto on_error;
3598 		    }
3599 		    if (list_append_tv(l, tv2) == FAIL)
3600 			goto theend;
3601 		    clear_tv(tv2);
3602 		    --ectx->ec_stack.ga_len;
3603 		}
3604 		break;
3605 
3606 	    case ISN_BLOBAPPEND:
3607 		{
3608 		    typval_T	*tv1 = STACK_TV_BOT(-2);
3609 		    typval_T	*tv2 = STACK_TV_BOT(-1);
3610 		    blob_T	*b = tv1->vval.v_blob;
3611 		    int		error = FALSE;
3612 		    varnumber_T n;
3613 
3614 		    // add a number to a blob
3615 		    if (b == NULL)
3616 		    {
3617 			SOURCING_LNUM = iptr->isn_lnum;
3618 			emsg(_(e_cannot_add_to_null_blob));
3619 			goto on_error;
3620 		    }
3621 		    n = tv_get_number_chk(tv2, &error);
3622 		    if (error)
3623 			goto on_error;
3624 		    ga_append(&b->bv_ga, (int)n);
3625 		    --ectx->ec_stack.ga_len;
3626 		}
3627 		break;
3628 
3629 	    // Computation with two arguments of unknown type
3630 	    case ISN_OPANY:
3631 		{
3632 		    typval_T	*tv1 = STACK_TV_BOT(-2);
3633 		    typval_T	*tv2 = STACK_TV_BOT(-1);
3634 		    varnumber_T	n1, n2;
3635 #ifdef FEAT_FLOAT
3636 		    float_T	f1 = 0, f2 = 0;
3637 #endif
3638 		    int		error = FALSE;
3639 
3640 		    if (iptr->isn_arg.op.op_type == EXPR_ADD)
3641 		    {
3642 			if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3643 			{
3644 			    eval_addlist(tv1, tv2);
3645 			    clear_tv(tv2);
3646 			    --ectx->ec_stack.ga_len;
3647 			    break;
3648 			}
3649 			else if (tv1->v_type == VAR_BLOB
3650 						    && tv2->v_type == VAR_BLOB)
3651 			{
3652 			    eval_addblob(tv1, tv2);
3653 			    clear_tv(tv2);
3654 			    --ectx->ec_stack.ga_len;
3655 			    break;
3656 			}
3657 		    }
3658 #ifdef FEAT_FLOAT
3659 		    if (tv1->v_type == VAR_FLOAT)
3660 		    {
3661 			f1 = tv1->vval.v_float;
3662 			n1 = 0;
3663 		    }
3664 		    else
3665 #endif
3666 		    {
3667 			SOURCING_LNUM = iptr->isn_lnum;
3668 			n1 = tv_get_number_chk(tv1, &error);
3669 			if (error)
3670 			    goto on_error;
3671 #ifdef FEAT_FLOAT
3672 			if (tv2->v_type == VAR_FLOAT)
3673 			    f1 = n1;
3674 #endif
3675 		    }
3676 #ifdef FEAT_FLOAT
3677 		    if (tv2->v_type == VAR_FLOAT)
3678 		    {
3679 			f2 = tv2->vval.v_float;
3680 			n2 = 0;
3681 		    }
3682 		    else
3683 #endif
3684 		    {
3685 			n2 = tv_get_number_chk(tv2, &error);
3686 			if (error)
3687 			    goto on_error;
3688 #ifdef FEAT_FLOAT
3689 			if (tv1->v_type == VAR_FLOAT)
3690 			    f2 = n2;
3691 #endif
3692 		    }
3693 #ifdef FEAT_FLOAT
3694 		    // if there is a float on either side the result is a float
3695 		    if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3696 		    {
3697 			switch (iptr->isn_arg.op.op_type)
3698 			{
3699 			    case EXPR_MULT: f1 = f1 * f2; break;
3700 			    case EXPR_DIV:  f1 = f1 / f2; break;
3701 			    case EXPR_SUB:  f1 = f1 - f2; break;
3702 			    case EXPR_ADD:  f1 = f1 + f2; break;
3703 			    default: SOURCING_LNUM = iptr->isn_lnum;
3704 				     emsg(_(e_modulus));
3705 				     goto on_error;
3706 			}
3707 			clear_tv(tv1);
3708 			clear_tv(tv2);
3709 			tv1->v_type = VAR_FLOAT;
3710 			tv1->vval.v_float = f1;
3711 			--ectx->ec_stack.ga_len;
3712 		    }
3713 		    else
3714 #endif
3715 		    {
3716 			int failed = FALSE;
3717 
3718 			switch (iptr->isn_arg.op.op_type)
3719 			{
3720 			    case EXPR_MULT: n1 = n1 * n2; break;
3721 			    case EXPR_DIV:  n1 = num_divide(n1, n2, &failed);
3722 					    if (failed)
3723 						goto on_error;
3724 					    break;
3725 			    case EXPR_SUB:  n1 = n1 - n2; break;
3726 			    case EXPR_ADD:  n1 = n1 + n2; break;
3727 			    default:	    n1 = num_modulus(n1, n2, &failed);
3728 					    if (failed)
3729 						goto on_error;
3730 					    break;
3731 			}
3732 			clear_tv(tv1);
3733 			clear_tv(tv2);
3734 			tv1->v_type = VAR_NUMBER;
3735 			tv1->vval.v_number = n1;
3736 			--ectx->ec_stack.ga_len;
3737 		    }
3738 		}
3739 		break;
3740 
3741 	    case ISN_CONCAT:
3742 		{
3743 		    char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3744 		    char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3745 		    char_u *res;
3746 
3747 		    res = concat_str(str1, str2);
3748 		    clear_tv(STACK_TV_BOT(-2));
3749 		    clear_tv(STACK_TV_BOT(-1));
3750 		    --ectx->ec_stack.ga_len;
3751 		    STACK_TV_BOT(-1)->vval.v_string = res;
3752 		}
3753 		break;
3754 
3755 	    case ISN_STRINDEX:
3756 	    case ISN_STRSLICE:
3757 		{
3758 		    int		is_slice = iptr->isn_type == ISN_STRSLICE;
3759 		    varnumber_T	n1 = 0, n2;
3760 		    char_u	*res;
3761 
3762 		    // string index: string is at stack-2, index at stack-1
3763 		    // string slice: string is at stack-3, first index at
3764 		    // stack-2, second index at stack-1
3765 		    if (is_slice)
3766 		    {
3767 			tv = STACK_TV_BOT(-2);
3768 			n1 = tv->vval.v_number;
3769 		    }
3770 
3771 		    tv = STACK_TV_BOT(-1);
3772 		    n2 = tv->vval.v_number;
3773 
3774 		    ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
3775 		    tv = STACK_TV_BOT(-1);
3776 		    if (is_slice)
3777 			// Slice: Select the characters from the string
3778 			res = string_slice(tv->vval.v_string, n1, n2, FALSE);
3779 		    else
3780 			// Index: The resulting variable is a string of a
3781 			// single character (including composing characters).
3782 			// If the index is too big or negative the result is
3783 			// empty.
3784 			res = char_from_string(tv->vval.v_string, n2);
3785 		    vim_free(tv->vval.v_string);
3786 		    tv->vval.v_string = res;
3787 		}
3788 		break;
3789 
3790 	    case ISN_LISTINDEX:
3791 	    case ISN_LISTSLICE:
3792 	    case ISN_BLOBINDEX:
3793 	    case ISN_BLOBSLICE:
3794 		{
3795 		    int		is_slice = iptr->isn_type == ISN_LISTSLICE
3796 					    || iptr->isn_type == ISN_BLOBSLICE;
3797 		    int		is_blob = iptr->isn_type == ISN_BLOBINDEX
3798 					    || iptr->isn_type == ISN_BLOBSLICE;
3799 		    varnumber_T	n1, n2;
3800 		    typval_T	*val_tv;
3801 
3802 		    // list index: list is at stack-2, index at stack-1
3803 		    // list slice: list is at stack-3, indexes at stack-2 and
3804 		    // stack-1
3805 		    // Same for blob.
3806 		    val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
3807 
3808 		    tv = STACK_TV_BOT(-1);
3809 		    n1 = n2 = tv->vval.v_number;
3810 		    clear_tv(tv);
3811 
3812 		    if (is_slice)
3813 		    {
3814 			tv = STACK_TV_BOT(-2);
3815 			n1 = tv->vval.v_number;
3816 			clear_tv(tv);
3817 		    }
3818 
3819 		    ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
3820 		    tv = STACK_TV_BOT(-1);
3821 		    SOURCING_LNUM = iptr->isn_lnum;
3822 		    if (is_blob)
3823 		    {
3824 			if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3825 						    n1, n2, FALSE, tv) == FAIL)
3826 			    goto on_error;
3827 		    }
3828 		    else
3829 		    {
3830 			if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3831 					      n1, n2, FALSE, tv, TRUE) == FAIL)
3832 			    goto on_error;
3833 		    }
3834 		}
3835 		break;
3836 
3837 	    case ISN_ANYINDEX:
3838 	    case ISN_ANYSLICE:
3839 		{
3840 		    int		is_slice = iptr->isn_type == ISN_ANYSLICE;
3841 		    typval_T	*var1, *var2;
3842 		    int		res;
3843 
3844 		    // index: composite is at stack-2, index at stack-1
3845 		    // slice: composite is at stack-3, indexes at stack-2 and
3846 		    // stack-1
3847 		    tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
3848 		    SOURCING_LNUM = iptr->isn_lnum;
3849 		    if (check_can_index(tv, TRUE, TRUE) == FAIL)
3850 			goto on_error;
3851 		    var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3852 		    var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
3853 		    res = eval_index_inner(tv, is_slice, var1, var2,
3854 							FALSE, NULL, -1, TRUE);
3855 		    clear_tv(var1);
3856 		    if (is_slice)
3857 			clear_tv(var2);
3858 		    ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
3859 		    if (res == FAIL)
3860 			goto on_error;
3861 		}
3862 		break;
3863 
3864 	    case ISN_SLICE:
3865 		{
3866 		    list_T	*list;
3867 		    int		count = iptr->isn_arg.number;
3868 
3869 		    // type will have been checked to be a list
3870 		    tv = STACK_TV_BOT(-1);
3871 		    list = tv->vval.v_list;
3872 
3873 		    // no error for short list, expect it to be checked earlier
3874 		    if (list != NULL && list->lv_len >= count)
3875 		    {
3876 			list_T	*newlist = list_slice(list,
3877 						      count, list->lv_len - 1);
3878 
3879 			if (newlist != NULL)
3880 			{
3881 			    list_unref(list);
3882 			    tv->vval.v_list = newlist;
3883 			    ++newlist->lv_refcount;
3884 			}
3885 		    }
3886 		}
3887 		break;
3888 
3889 	    case ISN_GETITEM:
3890 		{
3891 		    listitem_T	*li;
3892 		    getitem_T	*gi = &iptr->isn_arg.getitem;
3893 
3894 		    // Get list item: list is at stack-1, push item.
3895 		    // List type and length is checked for when compiling.
3896 		    tv = STACK_TV_BOT(-1 - gi->gi_with_op);
3897 		    li = list_find(tv->vval.v_list, gi->gi_index);
3898 
3899 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
3900 			goto theend;
3901 		    ++ectx->ec_stack.ga_len;
3902 		    copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3903 
3904 		    // Useful when used in unpack assignment.  Reset at
3905 		    // ISN_DROP.
3906 		    ectx->ec_where.wt_index = gi->gi_index + 1;
3907 		    ectx->ec_where.wt_variable = TRUE;
3908 		}
3909 		break;
3910 
3911 	    case ISN_MEMBER:
3912 		{
3913 		    dict_T	*dict;
3914 		    char_u	*key;
3915 		    dictitem_T	*di;
3916 		    typval_T	temp_tv;
3917 
3918 		    // dict member: dict is at stack-2, key at stack-1
3919 		    tv = STACK_TV_BOT(-2);
3920 		    // no need to check for VAR_DICT, CHECKTYPE will check.
3921 		    dict = tv->vval.v_dict;
3922 
3923 		    tv = STACK_TV_BOT(-1);
3924 		    // no need to check for VAR_STRING, 2STRING will check.
3925 		    key = tv->vval.v_string;
3926 		    if (key == NULL)
3927 			key = (char_u *)"";
3928 
3929 		    if ((di = dict_find(dict, key, -1)) == NULL)
3930 		    {
3931 			SOURCING_LNUM = iptr->isn_lnum;
3932 			semsg(_(e_dictkey), key);
3933 
3934 			// If :silent! is used we will continue, make sure the
3935 			// stack contents makes sense.
3936 			clear_tv(tv);
3937 			--ectx->ec_stack.ga_len;
3938 			tv = STACK_TV_BOT(-1);
3939 			clear_tv(tv);
3940 			tv->v_type = VAR_NUMBER;
3941 			tv->vval.v_number = 0;
3942 			goto on_fatal_error;
3943 		    }
3944 		    clear_tv(tv);
3945 		    --ectx->ec_stack.ga_len;
3946 		    // Clear the dict only after getting the item, to avoid
3947 		    // that it makes the item invalid.
3948 		    tv = STACK_TV_BOT(-1);
3949 		    temp_tv = *tv;
3950 		    copy_tv(&di->di_tv, tv);
3951 		    clear_tv(&temp_tv);
3952 		}
3953 		break;
3954 
3955 	    // dict member with string key
3956 	    case ISN_STRINGMEMBER:
3957 		{
3958 		    dict_T	*dict;
3959 		    dictitem_T	*di;
3960 		    typval_T	temp_tv;
3961 
3962 		    tv = STACK_TV_BOT(-1);
3963 		    if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3964 		    {
3965 			SOURCING_LNUM = iptr->isn_lnum;
3966 			emsg(_(e_dictreq));
3967 			goto on_error;
3968 		    }
3969 		    dict = tv->vval.v_dict;
3970 
3971 		    if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3972 								       == NULL)
3973 		    {
3974 			SOURCING_LNUM = iptr->isn_lnum;
3975 			semsg(_(e_dictkey), iptr->isn_arg.string);
3976 			goto on_error;
3977 		    }
3978 		    // Clear the dict after getting the item, to avoid that it
3979 		    // make the item invalid.
3980 		    temp_tv = *tv;
3981 		    copy_tv(&di->di_tv, tv);
3982 		    clear_tv(&temp_tv);
3983 		}
3984 		break;
3985 
3986 	    case ISN_NEGATENR:
3987 		tv = STACK_TV_BOT(-1);
3988 		if (tv->v_type != VAR_NUMBER
3989 #ifdef FEAT_FLOAT
3990 			&& tv->v_type != VAR_FLOAT
3991 #endif
3992 			)
3993 		{
3994 		    SOURCING_LNUM = iptr->isn_lnum;
3995 		    emsg(_(e_number_expected));
3996 		    goto on_error;
3997 		}
3998 #ifdef FEAT_FLOAT
3999 		if (tv->v_type == VAR_FLOAT)
4000 		    tv->vval.v_float = -tv->vval.v_float;
4001 		else
4002 #endif
4003 		    tv->vval.v_number = -tv->vval.v_number;
4004 		break;
4005 
4006 	    case ISN_CHECKNR:
4007 		{
4008 		    int		error = FALSE;
4009 
4010 		    tv = STACK_TV_BOT(-1);
4011 		    SOURCING_LNUM = iptr->isn_lnum;
4012 		    if (check_not_string(tv) == FAIL)
4013 			goto on_error;
4014 		    (void)tv_get_number_chk(tv, &error);
4015 		    if (error)
4016 			goto on_error;
4017 		}
4018 		break;
4019 
4020 	    case ISN_CHECKTYPE:
4021 		{
4022 		    checktype_T *ct = &iptr->isn_arg.type;
4023 
4024 		    tv = STACK_TV_BOT((int)ct->ct_off);
4025 		    SOURCING_LNUM = iptr->isn_lnum;
4026 		    if (!ectx->ec_where.wt_variable)
4027 			ectx->ec_where.wt_index = ct->ct_arg_idx;
4028 		    if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4029 								       == FAIL)
4030 			goto on_error;
4031 		    if (!ectx->ec_where.wt_variable)
4032 			ectx->ec_where.wt_index = 0;
4033 
4034 		    // number 0 is FALSE, number 1 is TRUE
4035 		    if (tv->v_type == VAR_NUMBER
4036 			    && ct->ct_type->tt_type == VAR_BOOL
4037 			    && (tv->vval.v_number == 0
4038 						|| tv->vval.v_number == 1))
4039 		    {
4040 			tv->v_type = VAR_BOOL;
4041 			tv->vval.v_number = tv->vval.v_number
4042 						      ? VVAL_TRUE : VVAL_FALSE;
4043 		    }
4044 		}
4045 		break;
4046 
4047 	    case ISN_CHECKLEN:
4048 		{
4049 		    int	    min_len = iptr->isn_arg.checklen.cl_min_len;
4050 		    list_T  *list = NULL;
4051 
4052 		    tv = STACK_TV_BOT(-1);
4053 		    if (tv->v_type == VAR_LIST)
4054 			    list = tv->vval.v_list;
4055 		    if (list == NULL || list->lv_len < min_len
4056 			    || (list->lv_len > min_len
4057 					&& !iptr->isn_arg.checklen.cl_more_OK))
4058 		    {
4059 			SOURCING_LNUM = iptr->isn_lnum;
4060 			semsg(_(e_expected_nr_items_but_got_nr),
4061 				     min_len, list == NULL ? 0 : list->lv_len);
4062 			goto on_error;
4063 		    }
4064 		}
4065 		break;
4066 
4067 	    case ISN_SETTYPE:
4068 		{
4069 		    checktype_T *ct = &iptr->isn_arg.type;
4070 
4071 		    tv = STACK_TV_BOT(-1);
4072 		    if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4073 		    {
4074 			free_type(tv->vval.v_dict->dv_type);
4075 			tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4076 		    }
4077 		    else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4078 		    {
4079 			free_type(tv->vval.v_list->lv_type);
4080 			tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4081 		    }
4082 		}
4083 		break;
4084 
4085 	    case ISN_2BOOL:
4086 	    case ISN_COND2BOOL:
4087 		{
4088 		    int n;
4089 		    int error = FALSE;
4090 
4091 		    if (iptr->isn_type == ISN_2BOOL)
4092 		    {
4093 			tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
4094 			n = tv2bool(tv);
4095 			if (iptr->isn_arg.tobool.invert)
4096 			    n = !n;
4097 		    }
4098 		    else
4099 		    {
4100 			tv = STACK_TV_BOT(-1);
4101 			SOURCING_LNUM = iptr->isn_lnum;
4102 			n = tv_get_bool_chk(tv, &error);
4103 			if (error)
4104 			    goto on_error;
4105 		    }
4106 		    clear_tv(tv);
4107 		    tv->v_type = VAR_BOOL;
4108 		    tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4109 		}
4110 		break;
4111 
4112 	    case ISN_2STRING:
4113 	    case ISN_2STRING_ANY:
4114 		SOURCING_LNUM = iptr->isn_lnum;
4115 		if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4116 				iptr->isn_type == ISN_2STRING_ANY,
4117 				      iptr->isn_arg.tostring.tolerant) == FAIL)
4118 			    goto on_error;
4119 		break;
4120 
4121 	    case ISN_RANGE:
4122 		{
4123 		    exarg_T	ea;
4124 		    char	*errormsg;
4125 
4126 		    ea.line2 = 0;
4127 		    ea.addr_count = 0;
4128 		    ea.addr_type = ADDR_LINES;
4129 		    ea.cmd = iptr->isn_arg.string;
4130 		    ea.skip = FALSE;
4131 		    if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
4132 			goto on_error;
4133 
4134 		    if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
4135 			goto theend;
4136 		    ++ectx->ec_stack.ga_len;
4137 		    tv = STACK_TV_BOT(-1);
4138 		    tv->v_type = VAR_NUMBER;
4139 		    tv->v_lock = 0;
4140 		    if (ea.addr_count == 0)
4141 			tv->vval.v_number = curwin->w_cursor.lnum;
4142 		    else
4143 			tv->vval.v_number = ea.line2;
4144 		}
4145 		break;
4146 
4147 	    case ISN_PUT:
4148 		{
4149 		    int		regname = iptr->isn_arg.put.put_regname;
4150 		    linenr_T	lnum = iptr->isn_arg.put.put_lnum;
4151 		    char_u	*expr = NULL;
4152 		    int		dir = FORWARD;
4153 
4154 		    if (lnum < -2)
4155 		    {
4156 			// line number was put on the stack by ISN_RANGE
4157 			tv = STACK_TV_BOT(-1);
4158 			curwin->w_cursor.lnum = tv->vval.v_number;
4159 			if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4160 			    dir = BACKWARD;
4161 			--ectx->ec_stack.ga_len;
4162 		    }
4163 		    else if (lnum == -2)
4164 			// :put! above cursor
4165 			dir = BACKWARD;
4166 		    else if (lnum >= 0)
4167 			curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
4168 
4169 		    if (regname == '=')
4170 		    {
4171 			tv = STACK_TV_BOT(-1);
4172 			if (tv->v_type == VAR_STRING)
4173 			    expr = tv->vval.v_string;
4174 			else
4175 			{
4176 			    expr = typval2string(tv, TRUE); // allocates value
4177 			    clear_tv(tv);
4178 			}
4179 			--ectx->ec_stack.ga_len;
4180 		    }
4181 		    check_cursor();
4182 		    do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4183 		    vim_free(expr);
4184 		}
4185 		break;
4186 
4187 	    case ISN_CMDMOD:
4188 		ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4189 		ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4190 		ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4191 							 ectx->ec_stack.ga_len;
4192 		cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4193 		apply_cmdmod(&cmdmod);
4194 		break;
4195 
4196 	    case ISN_CMDMOD_REV:
4197 		// filter regprog is owned by the instruction, don't free it
4198 		cmdmod.cmod_filter_regmatch.regprog = NULL;
4199 		undo_cmdmod(&cmdmod);
4200 		cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4201 		ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
4202 		break;
4203 
4204 	    case ISN_UNPACK:
4205 		{
4206 		    int		count = iptr->isn_arg.unpack.unp_count;
4207 		    int		semicolon = iptr->isn_arg.unpack.unp_semicolon;
4208 		    list_T	*l;
4209 		    listitem_T	*li;
4210 		    int		i;
4211 
4212 		    // Check there is a valid list to unpack.
4213 		    tv = STACK_TV_BOT(-1);
4214 		    if (tv->v_type != VAR_LIST)
4215 		    {
4216 			SOURCING_LNUM = iptr->isn_lnum;
4217 			emsg(_(e_for_argument_must_be_sequence_of_lists));
4218 			goto on_error;
4219 		    }
4220 		    l = tv->vval.v_list;
4221 		    if (l == NULL
4222 				|| l->lv_len < (semicolon ? count - 1 : count))
4223 		    {
4224 			SOURCING_LNUM = iptr->isn_lnum;
4225 			emsg(_(e_list_value_does_not_have_enough_items));
4226 			goto on_error;
4227 		    }
4228 		    else if (!semicolon && l->lv_len > count)
4229 		    {
4230 			SOURCING_LNUM = iptr->isn_lnum;
4231 			emsg(_(e_list_value_has_more_items_than_targets));
4232 			goto on_error;
4233 		    }
4234 
4235 		    CHECK_LIST_MATERIALIZE(l);
4236 		    if (unlikely(GA_GROW(&ectx->ec_stack, count - 1) == FAIL))
4237 			goto theend;
4238 		    ectx->ec_stack.ga_len += count - 1;
4239 
4240 		    // Variable after semicolon gets a list with the remaining
4241 		    // items.
4242 		    if (semicolon)
4243 		    {
4244 			list_T	*rem_list =
4245 				  list_alloc_with_items(l->lv_len - count + 1);
4246 
4247 			if (rem_list == NULL)
4248 			    goto theend;
4249 			tv = STACK_TV_BOT(-count);
4250 			tv->vval.v_list = rem_list;
4251 			++rem_list->lv_refcount;
4252 			tv->v_lock = 0;
4253 			li = l->lv_first;
4254 			for (i = 0; i < count - 1; ++i)
4255 			    li = li->li_next;
4256 			for (i = 0; li != NULL; ++i)
4257 			{
4258 			    list_set_item(rem_list, i, &li->li_tv);
4259 			    li = li->li_next;
4260 			}
4261 			--count;
4262 		    }
4263 
4264 		    // Produce the values in reverse order, first item last.
4265 		    li = l->lv_first;
4266 		    for (i = 0; i < count; ++i)
4267 		    {
4268 			tv = STACK_TV_BOT(-i - 1);
4269 			copy_tv(&li->li_tv, tv);
4270 			li = li->li_next;
4271 		    }
4272 
4273 		    list_unref(l);
4274 		}
4275 		break;
4276 
4277 	    case ISN_PROF_START:
4278 	    case ISN_PROF_END:
4279 		{
4280 #ifdef FEAT_PROFILE
4281 		    funccall_T cookie;
4282 		    ufunc_T	    *cur_ufunc =
4283 				    (((dfunc_T *)def_functions.ga_data)
4284 					       + ectx->ec_dfunc_idx)->df_ufunc;
4285 
4286 		    cookie.func = cur_ufunc;
4287 		    if (iptr->isn_type == ISN_PROF_START)
4288 		    {
4289 			func_line_start(&cookie, iptr->isn_lnum);
4290 			// if we get here the instruction is executed
4291 			func_line_exec(&cookie);
4292 		    }
4293 		    else
4294 			func_line_end(&cookie);
4295 #endif
4296 		}
4297 		break;
4298 
4299 	    case ISN_DEBUG:
4300 		handle_debug(iptr, ectx);
4301 		break;
4302 
4303 	    case ISN_SHUFFLE:
4304 		{
4305 		    typval_T	tmp_tv;
4306 		    int		item = iptr->isn_arg.shuffle.shfl_item;
4307 		    int		up = iptr->isn_arg.shuffle.shfl_up;
4308 
4309 		    tmp_tv = *STACK_TV_BOT(-item);
4310 		    for ( ; up > 0 && item > 1; --up)
4311 		    {
4312 			*STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4313 			--item;
4314 		    }
4315 		    *STACK_TV_BOT(-item) = tmp_tv;
4316 		}
4317 		break;
4318 
4319 	    case ISN_DROP:
4320 		--ectx->ec_stack.ga_len;
4321 		clear_tv(STACK_TV_BOT(0));
4322 		ectx->ec_where.wt_index = 0;
4323 		ectx->ec_where.wt_variable = FALSE;
4324 		break;
4325 	}
4326 	continue;
4327 
4328 func_return:
4329 	// Restore previous function. If the frame pointer is where we started
4330 	// then there is none and we are done.
4331 	if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
4332 	    goto done;
4333 
4334 	if (func_return(ectx) == FAIL)
4335 	    // only fails when out of memory
4336 	    goto theend;
4337 	continue;
4338 
4339 on_error:
4340 	// Jump here for an error that does not require aborting execution.
4341 	// If "emsg_silent" is set then ignore the error, unless it was set
4342 	// when calling the function.
4343 	if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
4344 					   && emsg_silent && did_emsg_def == 0)
4345 	{
4346 	    // If a sequence of instructions causes an error while ":silent!"
4347 	    // was used, restore the stack length and jump ahead to restoring
4348 	    // the cmdmod.
4349 	    if (ectx->ec_funclocal.floc_restore_cmdmod)
4350 	    {
4351 		while (ectx->ec_stack.ga_len
4352 			     > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
4353 		{
4354 		    --ectx->ec_stack.ga_len;
4355 		    clear_tv(STACK_TV_BOT(0));
4356 		}
4357 		while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4358 		    ++ectx->ec_iidx;
4359 	    }
4360 	    continue;
4361 	}
4362 on_fatal_error:
4363 	// Jump here for an error that messes up the stack.
4364 	// If we are not inside a try-catch started here, abort execution.
4365 	if (trylevel <= ectx->ec_trylevel_at_start)
4366 	    goto theend;
4367     }
4368 
4369 done:
4370     ret = OK;
4371 theend:
4372     ectx->ec_trylevel_at_start = save_trylevel_at_start;
4373     return ret;
4374 }
4375 
4376 /*
4377  * Execute the instructions from a VAR_INSTR typeval and put the result in
4378  * "rettv".
4379  * Return OK or FAIL.
4380  */
4381     int
4382 exe_typval_instr(typval_T *tv, typval_T *rettv)
4383 {
4384     ectx_T	*ectx = tv->vval.v_instr->instr_ectx;
4385     isn_T	*save_instr = ectx->ec_instr;
4386     int		save_iidx = ectx->ec_iidx;
4387     int		res;
4388 
4389     ectx->ec_instr = tv->vval.v_instr->instr_instr;
4390     res = exec_instructions(ectx);
4391     if (res == OK)
4392     {
4393 	*rettv = *STACK_TV_BOT(-1);
4394 	--ectx->ec_stack.ga_len;
4395     }
4396 
4397     ectx->ec_instr = save_instr;
4398     ectx->ec_iidx = save_iidx;
4399 
4400     return res;
4401 }
4402 
4403 /*
4404  * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4405  * "substitute_instr".
4406  */
4407     char_u *
4408 exe_substitute_instr(void)
4409 {
4410     ectx_T	*ectx = substitute_instr->subs_ectx;
4411     isn_T	*save_instr = ectx->ec_instr;
4412     int		save_iidx = ectx->ec_iidx;
4413     char_u	*res;
4414 
4415     ectx->ec_instr = substitute_instr->subs_instr;
4416     if (exec_instructions(ectx) == OK)
4417     {
4418 	typval_T *tv = STACK_TV_BOT(-1);
4419 
4420 	res = typval2string(tv, TRUE);
4421 	--ectx->ec_stack.ga_len;
4422 	clear_tv(tv);
4423     }
4424     else
4425     {
4426 	substitute_instr->subs_status = FAIL;
4427 	res = vim_strsave((char_u *)"");
4428     }
4429 
4430     ectx->ec_instr = save_instr;
4431     ectx->ec_iidx = save_iidx;
4432 
4433     return res;
4434 }
4435 
4436 /*
4437  * Call a "def" function from old Vim script.
4438  * Return OK or FAIL.
4439  */
4440     int
4441 call_def_function(
4442     ufunc_T	*ufunc,
4443     int		argc_arg,	// nr of arguments
4444     typval_T	*argv,		// arguments
4445     partial_T	*partial,	// optional partial for context
4446     typval_T	*rettv)		// return value
4447 {
4448     ectx_T	ectx;		// execution context
4449     int		argc = argc_arg;
4450     typval_T	*tv;
4451     int		idx;
4452     int		ret = FAIL;
4453     int		defcount = ufunc->uf_args.ga_len - argc;
4454     sctx_T	save_current_sctx = current_sctx;
4455     int		did_emsg_before = did_emsg_cumul + did_emsg;
4456     int		save_suppress_errthrow = suppress_errthrow;
4457     msglist_T	**saved_msg_list = NULL;
4458     msglist_T	*private_msg_list = NULL;
4459     int		save_emsg_silent_def = emsg_silent_def;
4460     int		save_did_emsg_def = did_emsg_def;
4461     int		orig_funcdepth;
4462     int		orig_nesting_level = ex_nesting_level;
4463 
4464 // Get pointer to item in the stack.
4465 #undef STACK_TV
4466 #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4467 
4468 // Get pointer to item at the bottom of the stack, -1 is the bottom.
4469 #undef STACK_TV_BOT
4470 #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4471 
4472 // Get pointer to a local variable on the stack.  Negative for arguments.
4473 #undef STACK_TV_VAR
4474 #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4475 
4476     // Update uf_has_breakpoint if needed.
4477     update_has_breakpoint(ufunc);
4478 
4479     if (ufunc->uf_def_status == UF_NOT_COMPILED
4480 	    || ufunc->uf_def_status == UF_COMPILE_ERROR
4481 	    || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4482 		&& compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
4483 								      == FAIL))
4484     {
4485 	if (did_emsg_cumul + did_emsg == did_emsg_before)
4486 	    semsg(_(e_function_is_not_compiled_str),
4487 						   printable_func_name(ufunc));
4488 	return FAIL;
4489     }
4490 
4491     {
4492 	// Check the function was really compiled.
4493 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
4494 							 + ufunc->uf_dfunc_idx;
4495 	if (INSTRUCTIONS(dfunc) == NULL)
4496 	{
4497 	    iemsg("using call_def_function() on not compiled function");
4498 	    return FAIL;
4499 	}
4500     }
4501 
4502     // If depth of calling is getting too high, don't execute the function.
4503     orig_funcdepth = funcdepth_get();
4504     if (funcdepth_increment() == FAIL)
4505 	return FAIL;
4506 
4507     CLEAR_FIELD(ectx);
4508     ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4509     ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4510     if (unlikely(ga_grow(&ectx.ec_stack, 20) == FAIL))
4511     {
4512 	funcdepth_decrement();
4513 	return FAIL;
4514     }
4515     ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4516     ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4517     ectx.ec_did_emsg_before = did_emsg_before;
4518     ++ex_nesting_level;
4519 
4520     idx = argc - ufunc->uf_args.ga_len;
4521     if (idx > 0 && ufunc->uf_va_name == NULL)
4522     {
4523 	if (idx == 1)
4524 	    emsg(_(e_one_argument_too_many));
4525 	else
4526 	    semsg(_(e_nr_arguments_too_many), idx);
4527 	goto failed_early;
4528     }
4529     idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4530     if (idx < 0)
4531     {
4532 	if (idx == -1)
4533 	    emsg(_(e_one_argument_too_few));
4534 	else
4535 	    semsg(_(e_nr_arguments_too_few), -idx);
4536 	goto failed_early;
4537     }
4538 
4539     // Put arguments on the stack, but no more than what the function expects.
4540     // A lambda can be called with more arguments than it uses.
4541     for (idx = 0; idx < argc
4542 	    && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4543 									 ++idx)
4544     {
4545 	if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4546 		&& argv[idx].v_type == VAR_SPECIAL
4547 		&& argv[idx].vval.v_number == VVAL_NONE)
4548 	{
4549 	    // Use the default value.
4550 	    STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4551 	}
4552 	else
4553 	{
4554 	    if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4555 		    && check_typval_arg_type(
4556 			ufunc->uf_arg_types[idx], &argv[idx],
4557 							NULL, idx + 1) == FAIL)
4558 		goto failed_early;
4559 	    copy_tv(&argv[idx], STACK_TV_BOT(0));
4560 	}
4561 	++ectx.ec_stack.ga_len;
4562     }
4563 
4564     // Turn varargs into a list.  Empty list if no args.
4565     if (ufunc->uf_va_name != NULL)
4566     {
4567 	int vararg_count = argc - ufunc->uf_args.ga_len;
4568 
4569 	if (vararg_count < 0)
4570 	    vararg_count = 0;
4571 	else
4572 	    argc -= vararg_count;
4573 	if (exe_newlist(vararg_count, &ectx) == FAIL)
4574 	    goto failed_early;
4575 
4576 	// Check the type of the list items.
4577 	tv = STACK_TV_BOT(-1);
4578 	if (ufunc->uf_va_type != NULL
4579 		&& ufunc->uf_va_type != &t_list_any
4580 		&& ufunc->uf_va_type->tt_member != &t_any
4581 		&& tv->vval.v_list != NULL)
4582 	{
4583 	    type_T	*expected = ufunc->uf_va_type->tt_member;
4584 	    listitem_T	*li = tv->vval.v_list->lv_first;
4585 
4586 	    for (idx = 0; idx < vararg_count; ++idx)
4587 	    {
4588 		if (check_typval_arg_type(expected, &li->li_tv,
4589 						 NULL, argc + idx + 1) == FAIL)
4590 		    goto failed_early;
4591 		li = li->li_next;
4592 	    }
4593 	}
4594 
4595 	if (defcount > 0)
4596 	    // Move varargs list to below missing default arguments.
4597 	    *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4598 	--ectx.ec_stack.ga_len;
4599     }
4600 
4601     // Make space for omitted arguments, will store default value below.
4602     // Any varargs list goes after them.
4603     if (defcount > 0)
4604 	for (idx = 0; idx < defcount; ++idx)
4605 	{
4606 	    STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4607 	    ++ectx.ec_stack.ga_len;
4608 	}
4609     if (ufunc->uf_va_name != NULL)
4610 	++ectx.ec_stack.ga_len;
4611 
4612     // Frame pointer points to just after arguments.
4613     ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4614     ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4615 
4616     {
4617 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
4618 							 + ufunc->uf_dfunc_idx;
4619 	ufunc_T *base_ufunc = dfunc->df_ufunc;
4620 
4621 	// "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4622 	// by copy_func().
4623 	if (partial != NULL || base_ufunc->uf_partial != NULL)
4624 	{
4625 	    ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4626 	    if (ectx.ec_outer_ref == NULL)
4627 		goto failed_early;
4628 	    if (partial != NULL)
4629 	    {
4630 		if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4631 		{
4632 		    if (current_ectx->ec_outer_ref != NULL
4633 			    && current_ectx->ec_outer_ref->or_outer != NULL)
4634 			ectx.ec_outer_ref->or_outer =
4635 					  current_ectx->ec_outer_ref->or_outer;
4636 		}
4637 		else
4638 		{
4639 		    ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4640 		    ++partial->pt_refcount;
4641 		    ectx.ec_outer_ref->or_partial = partial;
4642 		}
4643 	    }
4644 	    else
4645 	    {
4646 		ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4647 		++base_ufunc->uf_partial->pt_refcount;
4648 		ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4649 	    }
4650 	}
4651     }
4652 
4653     // dummy frame entries
4654     for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4655     {
4656 	STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4657 	++ectx.ec_stack.ga_len;
4658     }
4659 
4660     {
4661 	// Reserve space for local variables and any closure reference count.
4662 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
4663 							 + ufunc->uf_dfunc_idx;
4664 
4665 	for (idx = 0; idx < dfunc->df_varcount; ++idx)
4666 	    STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4667 	ectx.ec_stack.ga_len += dfunc->df_varcount;
4668 	if (dfunc->df_has_closure)
4669 	{
4670 	    STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4671 	    STACK_TV_VAR(idx)->vval.v_number = 0;
4672 	    ++ectx.ec_stack.ga_len;
4673 	}
4674 
4675 	ectx.ec_instr = INSTRUCTIONS(dfunc);
4676     }
4677 
4678     // Following errors are in the function, not the caller.
4679     // Commands behave like vim9script.
4680     estack_push_ufunc(ufunc, 1);
4681     current_sctx = ufunc->uf_script_ctx;
4682     current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4683 
4684     // Use a specific location for storing error messages to be converted to an
4685     // exception.
4686     saved_msg_list = msg_list;
4687     msg_list = &private_msg_list;
4688 
4689     // Do turn errors into exceptions.
4690     suppress_errthrow = FALSE;
4691 
4692     // Do not delete the function while executing it.
4693     ++ufunc->uf_calls;
4694 
4695     // When ":silent!" was used before calling then we still abort the
4696     // function.  If ":silent!" is used in the function then we don't.
4697     emsg_silent_def = emsg_silent;
4698     did_emsg_def = 0;
4699 
4700     ectx.ec_where.wt_index = 0;
4701     ectx.ec_where.wt_variable = FALSE;
4702 
4703     // Execute the instructions until done.
4704     ret = exec_instructions(&ectx);
4705     if (ret == OK)
4706     {
4707 	// function finished, get result from the stack.
4708 	if (ufunc->uf_ret_type == &t_void)
4709 	{
4710 	    rettv->v_type = VAR_VOID;
4711 	}
4712 	else
4713 	{
4714 	    tv = STACK_TV_BOT(-1);
4715 	    *rettv = *tv;
4716 	    tv->v_type = VAR_UNKNOWN;
4717 	}
4718     }
4719 
4720     // When failed need to unwind the call stack.
4721     while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4722 	func_return(&ectx);
4723 
4724     // Deal with any remaining closures, they may be in use somewhere.
4725     if (ectx.ec_funcrefs.ga_len > 0)
4726     {
4727 	handle_closure_in_use(&ectx, FALSE);
4728 	ga_clear(&ectx.ec_funcrefs);  // TODO: should not be needed?
4729     }
4730 
4731     estack_pop();
4732     current_sctx = save_current_sctx;
4733 
4734     // TODO: when is it safe to delete the function if it is no longer used?
4735     --ufunc->uf_calls;
4736 
4737     if (*msg_list != NULL && saved_msg_list != NULL)
4738     {
4739 	msglist_T **plist = saved_msg_list;
4740 
4741 	// Append entries from the current msg_list (uncaught exceptions) to
4742 	// the saved msg_list.
4743 	while (*plist != NULL)
4744 	    plist = &(*plist)->next;
4745 
4746 	*plist = *msg_list;
4747     }
4748     msg_list = saved_msg_list;
4749 
4750     if (ectx.ec_funclocal.floc_restore_cmdmod)
4751     {
4752 	cmdmod.cmod_filter_regmatch.regprog = NULL;
4753 	undo_cmdmod(&cmdmod);
4754 	cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
4755     }
4756     emsg_silent_def = save_emsg_silent_def;
4757     did_emsg_def += save_did_emsg_def;
4758 
4759 failed_early:
4760     // Free all local variables, but not arguments.
4761     for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4762 	clear_tv(STACK_TV(idx));
4763     ex_nesting_level = orig_nesting_level;
4764 
4765     vim_free(ectx.ec_stack.ga_data);
4766     vim_free(ectx.ec_trystack.ga_data);
4767     if (ectx.ec_outer_ref != NULL)
4768     {
4769 	if (ectx.ec_outer_ref->or_outer_allocated)
4770 	    vim_free(ectx.ec_outer_ref->or_outer);
4771 	partial_unref(ectx.ec_outer_ref->or_partial);
4772 	vim_free(ectx.ec_outer_ref);
4773     }
4774 
4775     // Not sure if this is necessary.
4776     suppress_errthrow = save_suppress_errthrow;
4777 
4778     if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
4779 							      && !need_rethrow)
4780 	semsg(_(e_unknown_error_while_executing_str),
4781 						   printable_func_name(ufunc));
4782     funcdepth_restore(orig_funcdepth);
4783     return ret;
4784 }
4785 
4786 /*
4787  * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4788  * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4789  * "pfx" is prefixed to every line.
4790  */
4791     static void
4792 list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4793 {
4794     int		line_idx = 0;
4795     int		prev_current = 0;
4796     int		current;
4797     int		def_arg_idx = 0;
4798 
4799     for (current = 0; current < instr_count; ++current)
4800     {
4801 	isn_T	    *iptr = &instr[current];
4802 	char	    *line;
4803 
4804 	if (ufunc != NULL)
4805 	{
4806 	    while (line_idx < iptr->isn_lnum
4807 					  && line_idx < ufunc->uf_lines.ga_len)
4808 	    {
4809 		if (current > prev_current)
4810 		{
4811 		    msg_puts("\n\n");
4812 		    prev_current = current;
4813 		}
4814 		line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4815 		if (line != NULL)
4816 		    msg(line);
4817 	    }
4818 	    if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4819 	    {
4820 		int	first_def_arg = ufunc->uf_args.ga_len
4821 						   - ufunc->uf_def_args.ga_len;
4822 
4823 		if (def_arg_idx > 0)
4824 		    msg_puts("\n\n");
4825 		msg_start();
4826 		msg_puts("  ");
4827 		msg_puts(((char **)(ufunc->uf_args.ga_data))[
4828 						 first_def_arg + def_arg_idx]);
4829 		msg_puts(" = ");
4830 		msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4831 		msg_clr_eos();
4832 		msg_end();
4833 	    }
4834 	}
4835 
4836 	switch (iptr->isn_type)
4837 	{
4838 	    case ISN_EXEC:
4839 		smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4840 		break;
4841 	    case ISN_EXEC_SPLIT:
4842 		smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4843 		break;
4844 	    case ISN_LEGACY_EVAL:
4845 		smsg("%s%4d EVAL legacy %s", pfx, current,
4846 							 iptr->isn_arg.string);
4847 		break;
4848 	    case ISN_REDIRSTART:
4849 		smsg("%s%4d REDIR", pfx, current);
4850 		break;
4851 	    case ISN_REDIREND:
4852 		smsg("%s%4d REDIR END%s", pfx, current,
4853 					iptr->isn_arg.number ? " append" : "");
4854 		break;
4855 	    case ISN_CEXPR_AUCMD:
4856 #ifdef FEAT_QUICKFIX
4857 		smsg("%s%4d CEXPR pre %s", pfx, current,
4858 				       cexpr_get_auname(iptr->isn_arg.number));
4859 #endif
4860 		break;
4861 	    case ISN_CEXPR_CORE:
4862 #ifdef FEAT_QUICKFIX
4863 		{
4864 		    cexprref_T	    *cer = iptr->isn_arg.cexpr.cexpr_ref;
4865 
4866 		    smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4867 				       cexpr_get_auname(cer->cer_cmdidx),
4868 				       cer->cer_forceit ? "!" : "",
4869 				       cer->cer_cmdline);
4870 		}
4871 #endif
4872 		break;
4873 	    case ISN_INSTR:
4874 		{
4875 		    smsg("%s%4d INSTR", pfx, current);
4876 		    list_instructions("    ", iptr->isn_arg.instr,
4877 								INT_MAX, NULL);
4878 		    msg("     -------------");
4879 		}
4880 		break;
4881 	    case ISN_SUBSTITUTE:
4882 		{
4883 		    subs_T *subs = &iptr->isn_arg.subs;
4884 
4885 		    smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4886 		    list_instructions("    ", subs->subs_instr, INT_MAX, NULL);
4887 		    msg("     -------------");
4888 		}
4889 		break;
4890 	    case ISN_EXECCONCAT:
4891 		smsg("%s%4d EXECCONCAT %lld", pfx, current,
4892 					      (varnumber_T)iptr->isn_arg.number);
4893 		break;
4894 	    case ISN_ECHO:
4895 		{
4896 		    echo_T *echo = &iptr->isn_arg.echo;
4897 
4898 		    smsg("%s%4d %s %d", pfx, current,
4899 			    echo->echo_with_white ? "ECHO" : "ECHON",
4900 			    echo->echo_count);
4901 		}
4902 		break;
4903 	    case ISN_EXECUTE:
4904 		smsg("%s%4d EXECUTE %lld", pfx, current,
4905 					    (varnumber_T)(iptr->isn_arg.number));
4906 		break;
4907 	    case ISN_ECHOMSG:
4908 		smsg("%s%4d ECHOMSG %lld", pfx, current,
4909 					    (varnumber_T)(iptr->isn_arg.number));
4910 		break;
4911 	    case ISN_ECHOERR:
4912 		smsg("%s%4d ECHOERR %lld", pfx, current,
4913 					    (varnumber_T)(iptr->isn_arg.number));
4914 		break;
4915 	    case ISN_LOAD:
4916 		{
4917 		    if (iptr->isn_arg.number < 0)
4918 			smsg("%s%4d LOAD arg[%lld]", pfx, current,
4919 				(varnumber_T)(iptr->isn_arg.number
4920 							  + STACK_FRAME_SIZE));
4921 		    else
4922 			smsg("%s%4d LOAD $%lld", pfx, current,
4923 					  (varnumber_T)(iptr->isn_arg.number));
4924 		}
4925 		break;
4926 	    case ISN_LOADOUTER:
4927 		{
4928 		    if (iptr->isn_arg.number < 0)
4929 			smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4930 				iptr->isn_arg.outer.outer_depth,
4931 				iptr->isn_arg.outer.outer_idx
4932 							  + STACK_FRAME_SIZE);
4933 		    else
4934 			smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4935 					      iptr->isn_arg.outer.outer_depth,
4936 					      iptr->isn_arg.outer.outer_idx);
4937 		}
4938 		break;
4939 	    case ISN_LOADV:
4940 		smsg("%s%4d LOADV v:%s", pfx, current,
4941 				       get_vim_var_name(iptr->isn_arg.number));
4942 		break;
4943 	    case ISN_LOADSCRIPT:
4944 		{
4945 		    scriptref_T	*sref = iptr->isn_arg.script.scriptref;
4946 		    scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4947 		    svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4948 							      + sref->sref_idx;
4949 
4950 		    smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4951 					    sv->sv_name,
4952 					    sref->sref_idx,
4953 					    si->sn_name);
4954 		}
4955 		break;
4956 	    case ISN_LOADS:
4957 		{
4958 		    scriptitem_T *si = SCRIPT_ITEM(
4959 					       iptr->isn_arg.loadstore.ls_sid);
4960 
4961 		    smsg("%s%4d LOADS s:%s from %s", pfx, current,
4962 				 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4963 		}
4964 		break;
4965 	    case ISN_LOADAUTO:
4966 		smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4967 		break;
4968 	    case ISN_LOADG:
4969 		smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4970 		break;
4971 	    case ISN_LOADB:
4972 		smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4973 		break;
4974 	    case ISN_LOADW:
4975 		smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4976 		break;
4977 	    case ISN_LOADT:
4978 		smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4979 		break;
4980 	    case ISN_LOADGDICT:
4981 		smsg("%s%4d LOAD g:", pfx, current);
4982 		break;
4983 	    case ISN_LOADBDICT:
4984 		smsg("%s%4d LOAD b:", pfx, current);
4985 		break;
4986 	    case ISN_LOADWDICT:
4987 		smsg("%s%4d LOAD w:", pfx, current);
4988 		break;
4989 	    case ISN_LOADTDICT:
4990 		smsg("%s%4d LOAD t:", pfx, current);
4991 		break;
4992 	    case ISN_LOADOPT:
4993 		smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4994 		break;
4995 	    case ISN_LOADENV:
4996 		smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4997 		break;
4998 	    case ISN_LOADREG:
4999 		smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
5000 		break;
5001 
5002 	    case ISN_STORE:
5003 		if (iptr->isn_arg.number < 0)
5004 		    smsg("%s%4d STORE arg[%lld]", pfx, current,
5005 				      iptr->isn_arg.number + STACK_FRAME_SIZE);
5006 		else
5007 		    smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
5008 		break;
5009 	    case ISN_STOREOUTER:
5010 		{
5011 		if (iptr->isn_arg.number < 0)
5012 		    smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5013 			    iptr->isn_arg.outer.outer_depth,
5014 			    iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5015 		else
5016 		    smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5017 			    iptr->isn_arg.outer.outer_depth,
5018 			    iptr->isn_arg.outer.outer_idx);
5019 		}
5020 		break;
5021 	    case ISN_STOREV:
5022 		smsg("%s%4d STOREV v:%s", pfx, current,
5023 				       get_vim_var_name(iptr->isn_arg.number));
5024 		break;
5025 	    case ISN_STOREAUTO:
5026 		smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5027 		break;
5028 	    case ISN_STOREG:
5029 		smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5030 		break;
5031 	    case ISN_STOREB:
5032 		smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5033 		break;
5034 	    case ISN_STOREW:
5035 		smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5036 		break;
5037 	    case ISN_STORET:
5038 		smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5039 		break;
5040 	    case ISN_STORES:
5041 		{
5042 		    scriptitem_T *si = SCRIPT_ITEM(
5043 					       iptr->isn_arg.loadstore.ls_sid);
5044 
5045 		    smsg("%s%4d STORES %s in %s", pfx, current,
5046 				 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5047 		}
5048 		break;
5049 	    case ISN_STORESCRIPT:
5050 		{
5051 		    scriptref_T	*sref = iptr->isn_arg.script.scriptref;
5052 		    scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5053 		    svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
5054 							      + sref->sref_idx;
5055 
5056 		    smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
5057 					     sv->sv_name,
5058 					     sref->sref_idx,
5059 					     si->sn_name);
5060 		}
5061 		break;
5062 	    case ISN_STOREOPT:
5063 		smsg("%s%4d STOREOPT &%s", pfx, current,
5064 					       iptr->isn_arg.storeopt.so_name);
5065 		break;
5066 	    case ISN_STOREENV:
5067 		smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5068 		break;
5069 	    case ISN_STOREREG:
5070 		smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
5071 		break;
5072 	    case ISN_STORENR:
5073 		smsg("%s%4d STORE %lld in $%d", pfx, current,
5074 				iptr->isn_arg.storenr.stnr_val,
5075 				iptr->isn_arg.storenr.stnr_idx);
5076 		break;
5077 
5078 	    case ISN_STOREINDEX:
5079 		smsg("%s%4d STOREINDEX %s", pfx, current,
5080 					  vartype_name(iptr->isn_arg.vartype));
5081 		break;
5082 
5083 	    case ISN_STORERANGE:
5084 		smsg("%s%4d STORERANGE", pfx, current);
5085 		break;
5086 
5087 	    // constants
5088 	    case ISN_PUSHNR:
5089 		smsg("%s%4d PUSHNR %lld", pfx, current,
5090 					    (varnumber_T)(iptr->isn_arg.number));
5091 		break;
5092 	    case ISN_PUSHBOOL:
5093 	    case ISN_PUSHSPEC:
5094 		smsg("%s%4d PUSH %s", pfx, current,
5095 				   get_var_special_name(iptr->isn_arg.number));
5096 		break;
5097 	    case ISN_PUSHF:
5098 #ifdef FEAT_FLOAT
5099 		smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5100 #endif
5101 		break;
5102 	    case ISN_PUSHS:
5103 		smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5104 		break;
5105 	    case ISN_PUSHBLOB:
5106 		{
5107 		    char_u	*r;
5108 		    char_u	numbuf[NUMBUFLEN];
5109 		    char_u	*tofree;
5110 
5111 		    r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5112 		    smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5113 		    vim_free(tofree);
5114 		}
5115 		break;
5116 	    case ISN_PUSHFUNC:
5117 		{
5118 		    char *name = (char *)iptr->isn_arg.string;
5119 
5120 		    smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5121 					       name == NULL ? "[none]" : name);
5122 		}
5123 		break;
5124 	    case ISN_PUSHCHANNEL:
5125 #ifdef FEAT_JOB_CHANNEL
5126 		{
5127 		    channel_T *channel = iptr->isn_arg.channel;
5128 
5129 		    smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5130 					 channel == NULL ? 0 : channel->ch_id);
5131 		}
5132 #endif
5133 		break;
5134 	    case ISN_PUSHJOB:
5135 #ifdef FEAT_JOB_CHANNEL
5136 		{
5137 		    typval_T	tv;
5138 		    char_u	*name;
5139 		    char_u	buf[NUMBUFLEN];
5140 
5141 		    tv.v_type = VAR_JOB;
5142 		    tv.vval.v_job = iptr->isn_arg.job;
5143 		    name = job_to_string_buf(&tv, buf);
5144 		    smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5145 		}
5146 #endif
5147 		break;
5148 	    case ISN_PUSHEXC:
5149 		smsg("%s%4d PUSH v:exception", pfx, current);
5150 		break;
5151 	    case ISN_UNLET:
5152 		smsg("%s%4d UNLET%s %s", pfx, current,
5153 			iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5154 			iptr->isn_arg.unlet.ul_name);
5155 		break;
5156 	    case ISN_UNLETENV:
5157 		smsg("%s%4d UNLETENV%s $%s", pfx, current,
5158 			iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5159 			iptr->isn_arg.unlet.ul_name);
5160 		break;
5161 	    case ISN_UNLETINDEX:
5162 		smsg("%s%4d UNLETINDEX", pfx, current);
5163 		break;
5164 	    case ISN_UNLETRANGE:
5165 		smsg("%s%4d UNLETRANGE", pfx, current);
5166 		break;
5167 	    case ISN_LOCKCONST:
5168 		smsg("%s%4d LOCKCONST", pfx, current);
5169 		break;
5170 	    case ISN_NEWLIST:
5171 		smsg("%s%4d NEWLIST size %lld", pfx, current,
5172 					    (varnumber_T)(iptr->isn_arg.number));
5173 		break;
5174 	    case ISN_NEWDICT:
5175 		smsg("%s%4d NEWDICT size %lld", pfx, current,
5176 					    (varnumber_T)(iptr->isn_arg.number));
5177 		break;
5178 
5179 	    // function call
5180 	    case ISN_BCALL:
5181 		{
5182 		    cbfunc_T	*cbfunc = &iptr->isn_arg.bfunc;
5183 
5184 		    smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5185 			    internal_func_name(cbfunc->cbf_idx),
5186 			    cbfunc->cbf_argcount);
5187 		}
5188 		break;
5189 	    case ISN_DCALL:
5190 		{
5191 		    cdfunc_T	*cdfunc = &iptr->isn_arg.dfunc;
5192 		    dfunc_T	*df = ((dfunc_T *)def_functions.ga_data)
5193 							     + cdfunc->cdf_idx;
5194 
5195 		    smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5196 			    df->df_ufunc->uf_name_exp != NULL
5197 				? df->df_ufunc->uf_name_exp
5198 				: df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5199 		}
5200 		break;
5201 	    case ISN_UCALL:
5202 		{
5203 		    cufunc_T	*cufunc = &iptr->isn_arg.ufunc;
5204 
5205 		    smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5206 				       cufunc->cuf_name, cufunc->cuf_argcount);
5207 		}
5208 		break;
5209 	    case ISN_PCALL:
5210 		{
5211 		    cpfunc_T	*cpfunc = &iptr->isn_arg.pfunc;
5212 
5213 		    smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5214 			   cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5215 		}
5216 		break;
5217 	    case ISN_PCALL_END:
5218 		smsg("%s%4d PCALL end", pfx, current);
5219 		break;
5220 	    case ISN_RETURN:
5221 		smsg("%s%4d RETURN", pfx, current);
5222 		break;
5223 	    case ISN_RETURN_VOID:
5224 		smsg("%s%4d RETURN void", pfx, current);
5225 		break;
5226 	    case ISN_FUNCREF:
5227 		{
5228 		    funcref_T	*funcref = &iptr->isn_arg.funcref;
5229 		    dfunc_T	*df = ((dfunc_T *)def_functions.ga_data)
5230 							    + funcref->fr_func;
5231 
5232 		    smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5233 		}
5234 		break;
5235 
5236 	    case ISN_NEWFUNC:
5237 		{
5238 		    newfunc_T	*newfunc = &iptr->isn_arg.newfunc;
5239 
5240 		    smsg("%s%4d NEWFUNC %s %s", pfx, current,
5241 				       newfunc->nf_lambda, newfunc->nf_global);
5242 		}
5243 		break;
5244 
5245 	    case ISN_DEF:
5246 		{
5247 		    char_u *name = iptr->isn_arg.string;
5248 
5249 		    smsg("%s%4d DEF %s", pfx, current,
5250 					   name == NULL ? (char_u *)"" : name);
5251 		}
5252 		break;
5253 
5254 	    case ISN_JUMP:
5255 		{
5256 		    char *when = "?";
5257 
5258 		    switch (iptr->isn_arg.jump.jump_when)
5259 		    {
5260 			case JUMP_ALWAYS:
5261 			    when = "JUMP";
5262 			    break;
5263 			case JUMP_AND_KEEP_IF_TRUE:
5264 			    when = "JUMP_AND_KEEP_IF_TRUE";
5265 			    break;
5266 			case JUMP_IF_FALSE:
5267 			    when = "JUMP_IF_FALSE";
5268 			    break;
5269 			case JUMP_AND_KEEP_IF_FALSE:
5270 			    when = "JUMP_AND_KEEP_IF_FALSE";
5271 			    break;
5272 			case JUMP_IF_COND_FALSE:
5273 			    when = "JUMP_IF_COND_FALSE";
5274 			    break;
5275 			case JUMP_IF_COND_TRUE:
5276 			    when = "JUMP_IF_COND_TRUE";
5277 			    break;
5278 		    }
5279 		    smsg("%s%4d %s -> %d", pfx, current, when,
5280 						iptr->isn_arg.jump.jump_where);
5281 		}
5282 		break;
5283 
5284 	    case ISN_JUMP_IF_ARG_SET:
5285 		smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5286 			 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5287 						iptr->isn_arg.jump.jump_where);
5288 		break;
5289 
5290 	    case ISN_FOR:
5291 		{
5292 		    forloop_T *forloop = &iptr->isn_arg.forloop;
5293 
5294 		    smsg("%s%4d FOR $%d -> %d", pfx, current,
5295 					   forloop->for_idx, forloop->for_end);
5296 		}
5297 		break;
5298 
5299 	    case ISN_TRY:
5300 		{
5301 		    try_T *try = &iptr->isn_arg.try;
5302 
5303 		    if (try->try_ref->try_finally == 0)
5304 			smsg("%s%4d TRY catch -> %d, endtry -> %d",
5305 				pfx, current,
5306 				try->try_ref->try_catch,
5307 				try->try_ref->try_endtry);
5308 		    else
5309 			smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5310 				pfx, current,
5311 				try->try_ref->try_catch,
5312 				try->try_ref->try_finally,
5313 				try->try_ref->try_endtry);
5314 		}
5315 		break;
5316 	    case ISN_CATCH:
5317 		// TODO
5318 		smsg("%s%4d CATCH", pfx, current);
5319 		break;
5320 	    case ISN_TRYCONT:
5321 		{
5322 		    trycont_T *trycont = &iptr->isn_arg.trycont;
5323 
5324 		    smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5325 				      trycont->tct_levels,
5326 				      trycont->tct_levels == 1 ? "" : "s",
5327 				      trycont->tct_where);
5328 		}
5329 		break;
5330 	    case ISN_FINALLY:
5331 		smsg("%s%4d FINALLY", pfx, current);
5332 		break;
5333 	    case ISN_ENDTRY:
5334 		smsg("%s%4d ENDTRY", pfx, current);
5335 		break;
5336 	    case ISN_THROW:
5337 		smsg("%s%4d THROW", pfx, current);
5338 		break;
5339 
5340 	    // expression operations on number
5341 	    case ISN_OPNR:
5342 	    case ISN_OPFLOAT:
5343 	    case ISN_OPANY:
5344 		{
5345 		    char *what;
5346 		    char *ins;
5347 
5348 		    switch (iptr->isn_arg.op.op_type)
5349 		    {
5350 			case EXPR_MULT: what = "*"; break;
5351 			case EXPR_DIV: what = "/"; break;
5352 			case EXPR_REM: what = "%"; break;
5353 			case EXPR_SUB: what = "-"; break;
5354 			case EXPR_ADD: what = "+"; break;
5355 			default:       what = "???"; break;
5356 		    }
5357 		    switch (iptr->isn_type)
5358 		    {
5359 			case ISN_OPNR: ins = "OPNR"; break;
5360 			case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5361 			case ISN_OPANY: ins = "OPANY"; break;
5362 			default: ins = "???"; break;
5363 		    }
5364 		    smsg("%s%4d %s %s", pfx, current, ins, what);
5365 		}
5366 		break;
5367 
5368 	    case ISN_COMPAREBOOL:
5369 	    case ISN_COMPARESPECIAL:
5370 	    case ISN_COMPARENR:
5371 	    case ISN_COMPAREFLOAT:
5372 	    case ISN_COMPARESTRING:
5373 	    case ISN_COMPAREBLOB:
5374 	    case ISN_COMPARELIST:
5375 	    case ISN_COMPAREDICT:
5376 	    case ISN_COMPAREFUNC:
5377 	    case ISN_COMPAREANY:
5378 		   {
5379 		       char *p;
5380 		       char buf[10];
5381 		       char *type;
5382 
5383 		       switch (iptr->isn_arg.op.op_type)
5384 		       {
5385 			   case EXPR_EQUAL:	 p = "=="; break;
5386 			   case EXPR_NEQUAL:    p = "!="; break;
5387 			   case EXPR_GREATER:   p = ">"; break;
5388 			   case EXPR_GEQUAL:    p = ">="; break;
5389 			   case EXPR_SMALLER:   p = "<"; break;
5390 			   case EXPR_SEQUAL:    p = "<="; break;
5391 			   case EXPR_MATCH:	 p = "=~"; break;
5392 			   case EXPR_IS:	 p = "is"; break;
5393 			   case EXPR_ISNOT:	 p = "isnot"; break;
5394 			   case EXPR_NOMATCH:	 p = "!~"; break;
5395 			   default:  p = "???"; break;
5396 		       }
5397 		       STRCPY(buf, p);
5398 		       if (iptr->isn_arg.op.op_ic == TRUE)
5399 			   strcat(buf, "?");
5400 		       switch(iptr->isn_type)
5401 		       {
5402 			   case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5403 			   case ISN_COMPARESPECIAL:
5404 						 type = "COMPARESPECIAL"; break;
5405 			   case ISN_COMPARENR: type = "COMPARENR"; break;
5406 			   case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5407 			   case ISN_COMPARESTRING:
5408 						  type = "COMPARESTRING"; break;
5409 			   case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5410 			   case ISN_COMPARELIST: type = "COMPARELIST"; break;
5411 			   case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5412 			   case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5413 			   case ISN_COMPAREANY: type = "COMPAREANY"; break;
5414 			   default: type = "???"; break;
5415 		       }
5416 
5417 		       smsg("%s%4d %s %s", pfx, current, type, buf);
5418 		   }
5419 		   break;
5420 
5421 	    case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5422 	    case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5423 
5424 	    // expression operations
5425 	    case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5426 	    case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5427 	    case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5428 	    case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5429 	    case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5430 	    case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5431 	    case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5432 	    case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5433 	    case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5434 	    case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5435 	    case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5436 	    case ISN_SLICE: smsg("%s%4d SLICE %lld",
5437 					 pfx, current, iptr->isn_arg.number); break;
5438 	    case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5439 					 iptr->isn_arg.getitem.gi_index,
5440 					 iptr->isn_arg.getitem.gi_with_op ?
5441 						       " with op" : ""); break;
5442 	    case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5443 	    case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5444 						  iptr->isn_arg.string); break;
5445 	    case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5446 
5447 	    case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5448 	    case ISN_CHECKTYPE:
5449 		  {
5450 		      checktype_T *ct = &iptr->isn_arg.type;
5451 		      char *tofree;
5452 
5453 		      if (ct->ct_arg_idx == 0)
5454 			  smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5455 					  type_name(ct->ct_type, &tofree),
5456 					  (int)ct->ct_off);
5457 		      else
5458 			  smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5459 					  type_name(ct->ct_type, &tofree),
5460 					  (int)ct->ct_off,
5461 					  (int)ct->ct_arg_idx);
5462 		      vim_free(tofree);
5463 		      break;
5464 		  }
5465 	    case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5466 				iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5467 				iptr->isn_arg.checklen.cl_min_len);
5468 			       break;
5469 	    case ISN_SETTYPE:
5470 		  {
5471 		      char *tofree;
5472 
5473 		      smsg("%s%4d SETTYPE %s", pfx, current,
5474 			      type_name(iptr->isn_arg.type.ct_type, &tofree));
5475 		      vim_free(tofree);
5476 		      break;
5477 		  }
5478 	    case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
5479 	    case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5480 				smsg("%s%4d INVERT %d (!val)", pfx, current,
5481 					 iptr->isn_arg.tobool.offset);
5482 			    else
5483 				smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5484 					 iptr->isn_arg.tobool.offset);
5485 			    break;
5486 	    case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
5487 				 (varnumber_T)(iptr->isn_arg.tostring.offset));
5488 			      break;
5489 	    case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5490 								  pfx, current,
5491 				 (varnumber_T)(iptr->isn_arg.tostring.offset));
5492 			      break;
5493 	    case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5494 							 iptr->isn_arg.string);
5495 			    break;
5496 	    case ISN_PUT:
5497 	        if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5498 		    smsg("%s%4d PUT %c above range",
5499 				  pfx, current, iptr->isn_arg.put.put_regname);
5500 		else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5501 		    smsg("%s%4d PUT %c range",
5502 				  pfx, current, iptr->isn_arg.put.put_regname);
5503 		else
5504 		    smsg("%s%4d PUT %c %ld", pfx, current,
5505 						 iptr->isn_arg.put.put_regname,
5506 					     (long)iptr->isn_arg.put.put_lnum);
5507 		break;
5508 
5509 		// TODO: summarize modifiers
5510 	    case ISN_CMDMOD:
5511 		{
5512 		    char_u  *buf;
5513 		    size_t  len = produce_cmdmods(
5514 				  NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5515 
5516 		    buf = alloc(len + 1);
5517 		    if (likely(buf != NULL))
5518 		    {
5519 			(void)produce_cmdmods(
5520 				   buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5521 			smsg("%s%4d CMDMOD %s", pfx, current, buf);
5522 			vim_free(buf);
5523 		    }
5524 		    break;
5525 		}
5526 	    case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5527 
5528 	    case ISN_PROF_START:
5529 		 smsg("%s%4d PROFILE START line %d", pfx, current,
5530 							       iptr->isn_lnum);
5531 		 break;
5532 
5533 	    case ISN_PROF_END:
5534 		smsg("%s%4d PROFILE END", pfx, current);
5535 		break;
5536 
5537 	    case ISN_DEBUG:
5538 		smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5539 			iptr->isn_arg.debug.dbg_break_lnum + 1,
5540 			iptr->isn_lnum,
5541 			iptr->isn_arg.debug.dbg_var_names_len);
5542 		break;
5543 
5544 	    case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5545 			iptr->isn_arg.unpack.unp_count,
5546 			iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5547 			      break;
5548 	    case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5549 					 iptr->isn_arg.shuffle.shfl_item,
5550 					 iptr->isn_arg.shuffle.shfl_up);
5551 			      break;
5552 	    case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5553 
5554 	    case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5555 			   return;
5556 	}
5557 
5558 	out_flush();	    // output one line at a time
5559 	ui_breakcheck();
5560 	if (got_int)
5561 	    break;
5562     }
5563 }
5564 
5565 /*
5566  * Handle command line completion for the :disassemble command.
5567  */
5568     void
5569 set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5570 {
5571     char_u	*p;
5572 
5573     // Default: expand user functions, "debug" and "profile"
5574     xp->xp_context = EXPAND_DISASSEMBLE;
5575     xp->xp_pattern = arg;
5576 
5577     // first argument already typed: only user function names
5578     if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5579     {
5580 	xp->xp_context = EXPAND_USER_FUNC;
5581 	xp->xp_pattern = skipwhite(p);
5582     }
5583 }
5584 
5585 /*
5586  * Function given to ExpandGeneric() to obtain the list of :disassemble
5587  * arguments.
5588  */
5589     char_u *
5590 get_disassemble_argument(expand_T *xp, int idx)
5591 {
5592     if (idx == 0)
5593 	return (char_u *)"debug";
5594     if (idx == 1)
5595 	return (char_u *)"profile";
5596     return get_user_func_name(xp, idx - 2);
5597 }
5598 
5599 /*
5600  * ":disassemble".
5601  * We don't really need this at runtime, but we do have tests that require it,
5602  * so always include this.
5603  */
5604     void
5605 ex_disassemble(exarg_T *eap)
5606 {
5607     char_u	*arg = eap->arg;
5608     char_u	*fname;
5609     ufunc_T	*ufunc;
5610     dfunc_T	*dfunc;
5611     isn_T	*instr;
5612     int		instr_count;
5613     int		is_global = FALSE;
5614     compiletype_T compile_type = CT_NONE;
5615 
5616     if (STRNCMP(arg, "profile", 7) == 0)
5617     {
5618 	compile_type = CT_PROFILE;
5619 	arg = skipwhite(arg + 7);
5620     }
5621     else if (STRNCMP(arg, "debug", 5) == 0)
5622     {
5623 	compile_type = CT_DEBUG;
5624 	arg = skipwhite(arg + 5);
5625     }
5626 
5627     if (STRNCMP(arg, "<lambda>", 8) == 0)
5628     {
5629 	arg += 8;
5630 	(void)getdigits(&arg);
5631 	fname = vim_strnsave(eap->arg, arg - eap->arg);
5632     }
5633     else
5634 	fname = trans_function_name(&arg, &is_global, FALSE,
5635 		      TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
5636     if (fname == NULL)
5637     {
5638 	semsg(_(e_invarg2), eap->arg);
5639 	return;
5640     }
5641 
5642     ufunc = find_func(fname, is_global, NULL);
5643     if (ufunc == NULL)
5644     {
5645 	char_u *p = untrans_function_name(fname);
5646 
5647 	if (p != NULL)
5648 	    // Try again without making it script-local.
5649 	    ufunc = find_func(p, FALSE, NULL);
5650     }
5651     vim_free(fname);
5652     if (ufunc == NULL)
5653     {
5654 	semsg(_(e_cannot_find_function_str), eap->arg);
5655 	return;
5656     }
5657     if (func_needs_compiling(ufunc, compile_type)
5658 	    && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
5659 	return;
5660     if (ufunc->uf_def_status != UF_COMPILED)
5661     {
5662 	semsg(_(e_function_is_not_compiled_str), eap->arg);
5663 	return;
5664     }
5665     if (ufunc->uf_name_exp != NULL)
5666 	msg((char *)ufunc->uf_name_exp);
5667     else
5668 	msg((char *)ufunc->uf_name);
5669 
5670     dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5671     switch (compile_type)
5672     {
5673 	case CT_PROFILE:
5674 #ifdef FEAT_PROFILE
5675 	    instr = dfunc->df_instr_prof;
5676 	    instr_count = dfunc->df_instr_prof_count;
5677 	    break;
5678 #endif
5679 	    // FALLTHROUGH
5680 	case CT_NONE:
5681 	    instr = dfunc->df_instr;
5682 	    instr_count = dfunc->df_instr_count;
5683 	    break;
5684 	case CT_DEBUG:
5685 	    instr = dfunc->df_instr_debug;
5686 	    instr_count = dfunc->df_instr_debug_count;
5687 	    break;
5688     }
5689 
5690     list_instructions("", instr, instr_count, ufunc);
5691 }
5692 
5693 /*
5694  * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
5695  * list, etc.  Mostly like what JavaScript does, except that empty list and
5696  * empty dictionary are FALSE.
5697  */
5698     int
5699 tv2bool(typval_T *tv)
5700 {
5701     switch (tv->v_type)
5702     {
5703 	case VAR_NUMBER:
5704 	    return tv->vval.v_number != 0;
5705 	case VAR_FLOAT:
5706 #ifdef FEAT_FLOAT
5707 	    return tv->vval.v_float != 0.0;
5708 #else
5709 	    break;
5710 #endif
5711 	case VAR_PARTIAL:
5712 	    return tv->vval.v_partial != NULL;
5713 	case VAR_FUNC:
5714 	case VAR_STRING:
5715 	    return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5716 	case VAR_LIST:
5717 	    return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5718 	case VAR_DICT:
5719 	    return tv->vval.v_dict != NULL
5720 				    && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5721 	case VAR_BOOL:
5722 	case VAR_SPECIAL:
5723 	    return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5724 	case VAR_JOB:
5725 #ifdef FEAT_JOB_CHANNEL
5726 	    return tv->vval.v_job != NULL;
5727 #else
5728 	    break;
5729 #endif
5730 	case VAR_CHANNEL:
5731 #ifdef FEAT_JOB_CHANNEL
5732 	    return tv->vval.v_channel != NULL;
5733 #else
5734 	    break;
5735 #endif
5736 	case VAR_BLOB:
5737 	    return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5738 	case VAR_UNKNOWN:
5739 	case VAR_ANY:
5740 	case VAR_VOID:
5741 	case VAR_INSTR:
5742 	    break;
5743     }
5744     return FALSE;
5745 }
5746 
5747     void
5748 emsg_using_string_as(typval_T *tv, int as_number)
5749 {
5750     semsg(_(as_number ? e_using_string_as_number_str
5751 						 : e_using_string_as_bool_str),
5752 		       tv->vval.v_string == NULL
5753 					   ? (char_u *)"" : tv->vval.v_string);
5754 }
5755 
5756 /*
5757  * If "tv" is a string give an error and return FAIL.
5758  */
5759     int
5760 check_not_string(typval_T *tv)
5761 {
5762     if (tv->v_type == VAR_STRING)
5763     {
5764 	emsg_using_string_as(tv, TRUE);
5765 	clear_tv(tv);
5766 	return FAIL;
5767     }
5768     return OK;
5769 }
5770 
5771 
5772 #endif // FEAT_EVAL
5773