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