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