xref: /vim-8.2.3635/src/vim9execute.c (revision d58a3bf7)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 
10 /*
11  * vim9execute.c: execute Vim9 script instructions
12  */
13 
14 #define USING_FLOAT_STUFF
15 #include "vim.h"
16 
17 #if defined(FEAT_EVAL) || defined(PROTO)
18 
19 #ifdef VMS
20 # include <float.h>
21 #endif
22 
23 #include "vim9.h"
24 
25 // Structure put on ec_trystack when ISN_TRY is encountered.
26 typedef struct {
27     int	    tcd_frame_idx;	// ec_frame_idx when ISN_TRY was encountered
28     int	    tcd_catch_idx;	// instruction of the first catch
29     int	    tcd_finally_idx;	// instruction of the finally block
30     int	    tcd_caught;		// catch block entered
31     int	    tcd_return;		// when TRUE return from end of :finally
32 } trycmd_T;
33 
34 
35 // A stack is used to store:
36 // - arguments passed to a :def function
37 // - info about the calling function, to use when returning
38 // - local variables
39 // - temporary values
40 //
41 // In detail (FP == Frame Pointer):
42 //	  arg1		first argument from caller (if present)
43 //	  arg2		second argument from caller (if present)
44 //	  extra_arg1	any missing optional argument default value
45 // FP ->  cur_func	calling function
46 //        current	previous instruction pointer
47 //        frame_ptr	previous Frame Pointer
48 //        var1		space for local variable
49 //        var2		space for local variable
50 //        ....		fixed space for max. number of local variables
51 //        temp		temporary values
52 //        ....		flexible space for temporary values (can grow big)
53 
54 /*
55  * Execution context.
56  */
57 typedef struct {
58     garray_T	ec_stack;	// stack of typval_T values
59     int		ec_frame_idx;	// index in ec_stack: context of ec_dfunc_idx
60 
61     garray_T	*ec_outer_stack;    // stack used for closures
62     int		ec_outer_frame;	    // stack frame in ec_outer_stack
63 
64     garray_T	ec_trystack;	// stack of trycmd_T values
65     int		ec_in_catch;	// when TRUE in catch or finally block
66 
67     int		ec_dfunc_idx;	// current function index
68     isn_T	*ec_instr;	// array with instructions
69     int		ec_iidx;	// index in ec_instr: instruction to execute
70 
71     garray_T	ec_funcrefs;	// partials that might be a closure
72 } ectx_T;
73 
74 // Get pointer to item relative to the bottom of the stack, -1 is the last one.
75 #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
76 
77     void
78 to_string_error(vartype_T vartype)
79 {
80     semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
81 }
82 
83 /*
84  * Return the number of arguments, including optional arguments and any vararg.
85  */
86     static int
87 ufunc_argcount(ufunc_T *ufunc)
88 {
89     return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
90 }
91 
92 /*
93  * Set the instruction index, depending on omitted arguments, where the default
94  * values are to be computed.  If all optional arguments are present, start
95  * with the function body.
96  * The expression evaluation is at the start of the instructions:
97  *  0 ->  EVAL default1
98  *	       STORE arg[-2]
99  *  1 ->  EVAL default2
100  *	       STORE arg[-1]
101  *  2 ->  function body
102  */
103     static void
104 init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
105 {
106     if (ufunc->uf_def_args.ga_len == 0)
107 	ectx->ec_iidx = 0;
108     else
109     {
110 	int	defcount = ufunc->uf_args.ga_len - argcount;
111 
112 	// If there is a varargs argument defcount can be negative, no defaults
113 	// to evaluate then.
114 	if (defcount < 0)
115 	    defcount = 0;
116 	ectx->ec_iidx = ufunc->uf_def_arg_idx[
117 					 ufunc->uf_def_args.ga_len - defcount];
118     }
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  *
153  * Stack has:
154  * - current arguments (already there)
155  * - omitted optional argument (default values) added here
156  * - stack frame:
157  *	- pointer to calling function
158  *	- Index of next instruction in calling function
159  *	- previous frame pointer
160  * - reserved space for local variables
161  */
162     static int
163 call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
164 {
165     int	    argcount = argcount_arg;
166     dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
167     ufunc_T *ufunc = dfunc->df_ufunc;
168     int	    arg_to_add;
169     int	    vararg_count = 0;
170     int	    varcount;
171     int	    idx;
172     estack_T *entry;
173 
174     if (dfunc->df_deleted)
175     {
176 	emsg_funcname(e_func_deleted, ufunc->uf_name);
177 	return FAIL;
178     }
179 
180     if (ufunc->uf_va_name != NULL)
181     {
182 	// Need to make a list out of the vararg arguments.
183 	// Stack at time of call with 2 varargs:
184 	//   normal_arg
185 	//   optional_arg
186 	//   vararg_1
187 	//   vararg_2
188 	// After creating the list:
189 	//   normal_arg
190 	//   optional_arg
191 	//   vararg-list
192 	// With missing optional arguments we get:
193 	//    normal_arg
194 	// After creating the list
195 	//    normal_arg
196 	//    (space for optional_arg)
197 	//    vararg-list
198 	vararg_count = argcount - ufunc->uf_args.ga_len;
199 	if (vararg_count < 0)
200 	    vararg_count = 0;
201 	else
202 	    argcount -= vararg_count;
203 	if (exe_newlist(vararg_count, ectx) == FAIL)
204 	    return FAIL;
205 
206 	vararg_count = 1;
207     }
208 
209     arg_to_add = ufunc->uf_args.ga_len - argcount;
210     if (arg_to_add < 0)
211     {
212 	if (arg_to_add == -1)
213 	    emsg(_(e_one_argument_too_many));
214 	else
215 	    semsg(_(e_nr_arguments_too_many), -arg_to_add);
216 	return FAIL;
217     }
218 
219     // Reserve space for:
220     // - missing arguments
221     // - stack frame
222     // - local variables
223     // - if needed: a counter for number of closures created in
224     //   ectx->ec_funcrefs.
225     varcount = dfunc->df_varcount + dfunc->df_has_closure;
226     if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
227 								       == FAIL)
228 	return FAIL;
229 
230     // Move the vararg-list to below the missing optional arguments.
231     if (vararg_count > 0 && arg_to_add > 0)
232 	*STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
233 
234     // Reserve space for omitted optional arguments, filled in soon.
235     for (idx = 0; idx < arg_to_add; ++idx)
236 	STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
237     ectx->ec_stack.ga_len += arg_to_add;
238 
239     // Store current execution state in stack frame for ISN_RETURN.
240     STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
241     STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
242     STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
243     ectx->ec_frame_idx = ectx->ec_stack.ga_len;
244 
245     // Initialize local variables
246     for (idx = 0; idx < dfunc->df_varcount; ++idx)
247 	STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
248     if (dfunc->df_has_closure)
249     {
250 	typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
251 
252 	tv->v_type = VAR_NUMBER;
253 	tv->vval.v_number = 0;
254     }
255     ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
256 
257     // Set execution state to the start of the called function.
258     ectx->ec_dfunc_idx = cdf_idx;
259     ectx->ec_instr = dfunc->df_instr;
260     entry = estack_push_ufunc(dfunc->df_ufunc, 1);
261     if (entry != NULL)
262     {
263 	// Set the script context to the script where the function was defined.
264 	// TODO: save more than the SID?
265 	entry->es_save_sid = current_sctx.sc_sid;
266 	current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
267     }
268 
269     // Decide where to start execution, handles optional arguments.
270     init_instr_idx(ufunc, argcount, ectx);
271 
272     return OK;
273 }
274 
275 // Get pointer to item in the stack.
276 #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
277 
278 /*
279  * Used when returning from a function: Check if any closure is still
280  * referenced.  If so then move the arguments and variables to a separate piece
281  * of stack to be used when the closure is called.
282  * When "free_arguments" is TRUE the arguments are to be freed.
283  * Returns FAIL when out of memory.
284  */
285     static int
286 handle_closure_in_use(ectx_T *ectx, int free_arguments)
287 {
288     dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
289 							  + ectx->ec_dfunc_idx;
290     int		argcount;
291     int		top;
292     int		idx;
293     typval_T	*tv;
294     int		closure_in_use = FALSE;
295     garray_T	*gap = &ectx->ec_funcrefs;
296     varnumber_T	closure_count;
297 
298     if (dfunc->df_ufunc == NULL)
299 	return OK;  // function was freed
300     if (dfunc->df_has_closure == 0)
301 	return OK;  // no closures
302     tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
303     closure_count = tv->vval.v_number;
304     if (closure_count == 0)
305 	return OK;  // no funcrefs created
306 
307     argcount = ufunc_argcount(dfunc->df_ufunc);
308     top = ectx->ec_frame_idx - argcount;
309 
310     // Check if any created closure is still in use.
311     for (idx = 0; idx < closure_count; ++idx)
312     {
313 	partial_T   *pt;
314 	int	    off = gap->ga_len - closure_count + idx;
315 
316 	if (off < 0)
317 	    continue;  // count is off or already done
318 	pt = ((partial_T **)gap->ga_data)[off];
319 	if (pt->pt_refcount > 1)
320 	{
321 	    int refcount = pt->pt_refcount;
322 	    int i;
323 
324 	    // A Reference in a local variables doesn't count, it gets
325 	    // unreferenced on return.
326 	    for (i = 0; i < dfunc->df_varcount; ++i)
327 	    {
328 		typval_T *stv = STACK_TV(ectx->ec_frame_idx
329 						       + STACK_FRAME_SIZE + i);
330 		if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
331 		    --refcount;
332 	    }
333 	    if (refcount > 1)
334 	    {
335 		closure_in_use = TRUE;
336 		break;
337 	    }
338 	}
339     }
340 
341     if (closure_in_use)
342     {
343 	funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
344 	typval_T    *stack;
345 
346 	// A closure is using the arguments and/or local variables.
347 	// Move them to the called function.
348 	if (funcstack == NULL)
349 	    return FAIL;
350 	funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
351 							  + dfunc->df_varcount;
352 	stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
353 	funcstack->fs_ga.ga_data = stack;
354 	if (stack == NULL)
355 	{
356 	    vim_free(funcstack);
357 	    return FAIL;
358 	}
359 
360 	// Move or copy the arguments.
361 	for (idx = 0; idx < argcount; ++idx)
362 	{
363 	    tv = STACK_TV(top + idx);
364 	    if (free_arguments)
365 	    {
366 		*(stack + idx) = *tv;
367 		tv->v_type = VAR_UNKNOWN;
368 	    }
369 	    else
370 		copy_tv(tv, stack + idx);
371 	}
372 	// Move the local variables.
373 	for (idx = 0; idx < dfunc->df_varcount; ++idx)
374 	{
375 	    tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
376 
377 	    // Do not copy a partial created for a local function.
378 	    // TODO: this won't work if the closure actually uses it.  But when
379 	    // keeping it it gets complicated: it will create a reference cycle
380 	    // inside the partial, thus needs special handling for garbage
381 	    // collection.
382 	    if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
383 	    {
384 		int i;
385 
386 		for (i = 0; i < closure_count; ++i)
387 		{
388 		    partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
389 							  - closure_count + i];
390 		    if (tv->vval.v_partial == pt)
391 			break;
392 		}
393 		if (i < closure_count)
394 		    continue;
395 	    }
396 
397 	    *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
398 	    tv->v_type = VAR_UNKNOWN;
399 	}
400 
401 	for (idx = 0; idx < closure_count; ++idx)
402 	{
403 	    partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
404 							- closure_count + idx];
405 	    if (pt->pt_refcount > 1)
406 	    {
407 		++funcstack->fs_refcount;
408 		pt->pt_funcstack = funcstack;
409 		pt->pt_ectx_stack = &funcstack->fs_ga;
410 		pt->pt_ectx_frame = ectx->ec_frame_idx - top;
411 	    }
412 	}
413     }
414 
415     for (idx = 0; idx < closure_count; ++idx)
416 	partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
417 						       - closure_count + idx]);
418     gap->ga_len -= closure_count;
419     if (gap->ga_len == 0)
420 	ga_clear(gap);
421 
422     return OK;
423 }
424 
425 /*
426  * Return from the current function.
427  */
428     static int
429 func_return(ectx_T *ectx)
430 {
431     int		idx;
432     dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
433 							  + ectx->ec_dfunc_idx;
434     int		argcount = ufunc_argcount(dfunc->df_ufunc);
435     int		top = ectx->ec_frame_idx - argcount;
436     estack_T	*entry;
437 
438     // execution context goes one level up
439     entry = estack_pop();
440     if (entry != NULL)
441 	current_sctx.sc_sid = entry->es_save_sid;
442 
443     if (handle_closure_in_use(ectx, TRUE) == FAIL)
444 	return FAIL;
445 
446     // Clear the arguments.
447     for (idx = top; idx < ectx->ec_frame_idx; ++idx)
448 	clear_tv(STACK_TV(idx));
449 
450     // Clear local variables and temp values, but not the return value.
451     for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
452 					idx < ectx->ec_stack.ga_len - 1; ++idx)
453 	clear_tv(STACK_TV(idx));
454 
455     // Restore the previous frame.
456     ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
457     ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
458     ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
459     dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
460     ectx->ec_instr = dfunc->df_instr;
461 
462     // Reset the stack to the position before the call, move the return value
463     // to the top of the stack.
464     idx = ectx->ec_stack.ga_len - 1;
465     ectx->ec_stack.ga_len = top + 1;
466     *STACK_TV_BOT(-1) = *STACK_TV(idx);
467 
468     return OK;
469 }
470 
471 #undef STACK_TV
472 
473 /*
474  * Prepare arguments and rettv for calling a builtin or user function.
475  */
476     static int
477 call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
478 {
479     int		idx;
480     typval_T	*tv;
481 
482     // Move arguments from bottom of the stack to argvars[] and add terminator.
483     for (idx = 0; idx < argcount; ++idx)
484 	argvars[idx] = *STACK_TV_BOT(idx - argcount);
485     argvars[argcount].v_type = VAR_UNKNOWN;
486 
487     // Result replaces the arguments on the stack.
488     if (argcount > 0)
489 	ectx->ec_stack.ga_len -= argcount - 1;
490     else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
491 	return FAIL;
492     else
493 	++ectx->ec_stack.ga_len;
494 
495     // Default return value is zero.
496     tv = STACK_TV_BOT(-1);
497     tv->v_type = VAR_NUMBER;
498     tv->vval.v_number = 0;
499 
500     return OK;
501 }
502 
503 // Ugly global to avoid passing the execution context around through many
504 // layers.
505 static ectx_T *current_ectx = NULL;
506 
507 /*
508  * Call a builtin function by index.
509  */
510     static int
511 call_bfunc(int func_idx, int argcount, ectx_T *ectx)
512 {
513     typval_T	argvars[MAX_FUNC_ARGS];
514     int		idx;
515     int		did_emsg_before = did_emsg;
516     ectx_T	*prev_ectx = current_ectx;
517 
518     if (call_prepare(argcount, argvars, ectx) == FAIL)
519 	return FAIL;
520 
521     // Call the builtin function.  Set "current_ectx" so that when it
522     // recursively invokes call_def_function() a closure context can be set.
523     current_ectx = ectx;
524     call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
525     current_ectx = prev_ectx;
526 
527     // Clear the arguments.
528     for (idx = 0; idx < argcount; ++idx)
529 	clear_tv(&argvars[idx]);
530 
531     if (did_emsg != did_emsg_before)
532 	return FAIL;
533     return OK;
534 }
535 
536 /*
537  * Execute a user defined function.
538  * "iptr" can be used to replace the instruction with a more efficient one.
539  */
540     static int
541 call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
542 {
543     typval_T	argvars[MAX_FUNC_ARGS];
544     funcexe_T   funcexe;
545     int		error;
546     int		idx;
547     int		called_emsg_before = called_emsg;
548 
549     if (ufunc->uf_def_status == UF_TO_BE_COMPILED
550 	    && compile_def_function(ufunc, FALSE, NULL) == FAIL)
551 	return FAIL;
552     if (ufunc->uf_def_status == UF_COMPILED)
553     {
554 	// The function has been compiled, can call it quickly.  For a function
555 	// that was defined later: we can call it directly next time.
556 	if (iptr != NULL)
557 	{
558 	    delete_instr(iptr);
559 	    iptr->isn_type = ISN_DCALL;
560 	    iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
561 	    iptr->isn_arg.dfunc.cdf_argcount = argcount;
562 	}
563 	return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
564     }
565 
566     if (call_prepare(argcount, argvars, ectx) == FAIL)
567 	return FAIL;
568     CLEAR_FIELD(funcexe);
569     funcexe.evaluate = TRUE;
570 
571     // Call the user function.  Result goes in last position on the stack.
572     // TODO: add selfdict if there is one
573     error = call_user_func_check(ufunc, argcount, argvars,
574 					     STACK_TV_BOT(-1), &funcexe, NULL);
575 
576     // Clear the arguments.
577     for (idx = 0; idx < argcount; ++idx)
578 	clear_tv(&argvars[idx]);
579 
580     if (error != FCERR_NONE)
581     {
582 	user_func_error(error, ufunc->uf_name);
583 	return FAIL;
584     }
585     if (called_emsg > called_emsg_before)
586 	// Error other than from calling the function itself.
587 	return FAIL;
588     return OK;
589 }
590 
591 /*
592  * Return TRUE if an error was given or CTRL-C was pressed.
593  */
594     static int
595 vim9_aborting(int prev_called_emsg)
596 {
597     return called_emsg > prev_called_emsg || got_int || did_throw;
598 }
599 
600 /*
601  * Execute a function by "name".
602  * This can be a builtin function or a user function.
603  * "iptr" can be used to replace the instruction with a more efficient one.
604  * Returns FAIL if not found without an error message.
605  */
606     static int
607 call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
608 {
609     ufunc_T *ufunc;
610 
611     if (builtin_function(name, -1))
612     {
613 	int func_idx = find_internal_func(name);
614 
615 	if (func_idx < 0)
616 	    return FAIL;
617 	if (check_internal_func(func_idx, argcount) < 0)
618 	    return FAIL;
619 	return call_bfunc(func_idx, argcount, ectx);
620     }
621 
622     ufunc = find_func(name, FALSE, NULL);
623 
624     if (ufunc == NULL)
625     {
626 	int called_emsg_before = called_emsg;
627 
628 	if (script_autoload(name, TRUE))
629 	    // loaded a package, search for the function again
630 	    ufunc = find_func(name, FALSE, NULL);
631 	if (vim9_aborting(called_emsg_before))
632 	    return FAIL;  // bail out if loading the script caused an error
633     }
634 
635     if (ufunc != NULL)
636 	return call_ufunc(ufunc, argcount, ectx, iptr);
637 
638     return FAIL;
639 }
640 
641     static int
642 call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
643 {
644     int		argcount = argcount_arg;
645     char_u	*name = NULL;
646     int		called_emsg_before = called_emsg;
647     int		res;
648 
649     if (tv->v_type == VAR_PARTIAL)
650     {
651 	partial_T   *pt = tv->vval.v_partial;
652 	int	    i;
653 
654 	if (pt->pt_argc > 0)
655 	{
656 	    // Make space for arguments from the partial, shift the "argcount"
657 	    // arguments up.
658 	    if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
659 		return FAIL;
660 	    for (i = 1; i <= argcount; ++i)
661 		*STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
662 	    ectx->ec_stack.ga_len += pt->pt_argc;
663 	    argcount += pt->pt_argc;
664 
665 	    // copy the arguments from the partial onto the stack
666 	    for (i = 0; i < pt->pt_argc; ++i)
667 		copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
668 	}
669 
670 	if (pt->pt_func != NULL)
671 	{
672 	    int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
673 
674 	    // closure may need the function context where it was defined
675 	    ectx->ec_outer_stack = pt->pt_ectx_stack;
676 	    ectx->ec_outer_frame = pt->pt_ectx_frame;
677 
678 	    return ret;
679 	}
680 	name = pt->pt_name;
681     }
682     else if (tv->v_type == VAR_FUNC)
683 	name = tv->vval.v_string;
684     if (name != NULL)
685     {
686 	char_u	fname_buf[FLEN_FIXED + 1];
687 	char_u	*tofree = NULL;
688 	int	error = FCERR_NONE;
689 	char_u	*fname;
690 
691 	// May need to translate <SNR>123_ to K_SNR.
692 	fname = fname_trans_sid(name, fname_buf, &tofree, &error);
693 	if (error != FCERR_NONE)
694 	    res = FAIL;
695 	else
696 	    res = call_by_name(fname, argcount, ectx, NULL);
697 	vim_free(tofree);
698     }
699 
700     if (name == NULL || res == FAIL)
701     {
702 	if (called_emsg == called_emsg_before)
703 	    semsg(_(e_unknownfunc),
704 				  name == NULL ? (char_u *)"[unknown]" : name);
705 	return FAIL;
706     }
707     return OK;
708 }
709 
710 /*
711  * Check if "lock" is VAR_LOCKED or VAR_FIXED.  If so give an error and return
712  * TRUE.
713  */
714     static int
715 error_if_locked(int lock, char *error)
716 {
717     if (lock & (VAR_LOCKED | VAR_FIXED))
718     {
719 	emsg(_(error));
720 	return TRUE;
721     }
722     return FALSE;
723 }
724 
725 /*
726  * Store "tv" in variable "name".
727  * This is for s: and g: variables.
728  */
729     static void
730 store_var(char_u *name, typval_T *tv)
731 {
732     funccal_entry_T entry;
733 
734     save_funccal(&entry);
735     set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
736     restore_funccal();
737 }
738 
739 
740 /*
741  * Execute a function by "name".
742  * This can be a builtin function, user function or a funcref.
743  * "iptr" can be used to replace the instruction with a more efficient one.
744  */
745     static int
746 call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
747 {
748     int	    called_emsg_before = called_emsg;
749     int	    res;
750 
751     res = call_by_name(name, argcount, ectx, iptr);
752     if (res == FAIL && called_emsg == called_emsg_before)
753     {
754 	dictitem_T	*v;
755 
756 	v = find_var(name, NULL, FALSE);
757 	if (v == NULL)
758 	{
759 	    semsg(_(e_unknownfunc), name);
760 	    return FAIL;
761 	}
762 	if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
763 	{
764 	    semsg(_(e_unknownfunc), name);
765 	    return FAIL;
766 	}
767 	return call_partial(&v->di_tv, argcount, ectx);
768     }
769     return res;
770 }
771 
772 /*
773  * Call a "def" function from old Vim script.
774  * Return OK or FAIL.
775  */
776     int
777 call_def_function(
778     ufunc_T	*ufunc,
779     int		argc_arg,	// nr of arguments
780     typval_T	*argv,		// arguments
781     partial_T	*partial,	// optional partial for context
782     typval_T	*rettv)		// return value
783 {
784     ectx_T	ectx;		// execution context
785     int		argc = argc_arg;
786     int		initial_frame_idx;
787     typval_T	*tv;
788     int		idx;
789     int		ret = FAIL;
790     int		defcount = ufunc->uf_args.ga_len - argc;
791     sctx_T	save_current_sctx = current_sctx;
792     int		breakcheck_count = 0;
793     int		called_emsg_before = called_emsg;
794     int		save_suppress_errthrow = suppress_errthrow;
795 
796 // Get pointer to item in the stack.
797 #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
798 
799 // Get pointer to item at the bottom of the stack, -1 is the bottom.
800 #undef STACK_TV_BOT
801 #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
802 
803 // Get pointer to a local variable on the stack.  Negative for arguments.
804 #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
805 
806 // Like STACK_TV_VAR but use the outer scope
807 #define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
808 
809     if (ufunc->uf_def_status == UF_NOT_COMPILED
810 	    || (ufunc->uf_def_status == UF_TO_BE_COMPILED
811 			  && compile_def_function(ufunc, FALSE, NULL) == FAIL))
812     {
813 	if (called_emsg == called_emsg_before)
814 	    semsg(_(e_function_is_not_compiled_str),
815 						   printable_func_name(ufunc));
816 	return FAIL;
817     }
818 
819     {
820 	// Check the function was really compiled.
821 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
822 							 + ufunc->uf_dfunc_idx;
823 	if (dfunc->df_instr == NULL)
824 	{
825 	    iemsg("using call_def_function() on not compiled function");
826 	    return FAIL;
827 	}
828     }
829 
830     CLEAR_FIELD(ectx);
831     ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
832     ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
833     if (ga_grow(&ectx.ec_stack, 20) == FAIL)
834 	return FAIL;
835     ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
836     ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
837 
838     // Put arguments on the stack.
839     for (idx = 0; idx < argc; ++idx)
840     {
841 	if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
842 		&& check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
843 							      idx + 1) == FAIL)
844 	    goto failed_early;
845 	copy_tv(&argv[idx], STACK_TV_BOT(0));
846 	++ectx.ec_stack.ga_len;
847     }
848 
849     // Turn varargs into a list.  Empty list if no args.
850     if (ufunc->uf_va_name != NULL)
851     {
852 	int vararg_count = argc - ufunc->uf_args.ga_len;
853 
854 	if (vararg_count < 0)
855 	    vararg_count = 0;
856 	else
857 	    argc -= vararg_count;
858 	if (exe_newlist(vararg_count, &ectx) == FAIL)
859 	    goto failed_early;
860 
861 	// Check the type of the list items.
862 	tv = STACK_TV_BOT(-1);
863 	if (ufunc->uf_va_type != NULL
864 		&& ufunc->uf_va_type != &t_any
865 		&& ufunc->uf_va_type->tt_member != &t_any
866 		&& tv->vval.v_list != NULL)
867 	{
868 	    type_T	*expected = ufunc->uf_va_type->tt_member;
869 	    listitem_T	*li = tv->vval.v_list->lv_first;
870 
871 	    for (idx = 0; idx < vararg_count; ++idx)
872 	    {
873 		if (check_typval_type(expected, &li->li_tv,
874 						       argc + idx + 1) == FAIL)
875 		    goto failed_early;
876 		li = li->li_next;
877 	    }
878 	}
879 
880 	if (defcount > 0)
881 	    // Move varargs list to below missing default arguments.
882 	    *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
883 	--ectx.ec_stack.ga_len;
884     }
885 
886     // Make space for omitted arguments, will store default value below.
887     // Any varargs list goes after them.
888     if (defcount > 0)
889 	for (idx = 0; idx < defcount; ++idx)
890 	{
891 	    STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
892 	    ++ectx.ec_stack.ga_len;
893 	}
894     if (ufunc->uf_va_name != NULL)
895 	    ++ectx.ec_stack.ga_len;
896 
897     // Frame pointer points to just after arguments.
898     ectx.ec_frame_idx = ectx.ec_stack.ga_len;
899     initial_frame_idx = ectx.ec_frame_idx;
900 
901     if (partial != NULL)
902     {
903 	if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
904 	{
905 	    // TODO: is this always the right way?
906 	    ectx.ec_outer_stack = &current_ectx->ec_stack;
907 	    ectx.ec_outer_frame = current_ectx->ec_frame_idx;
908 	}
909 	else
910 	{
911 	    ectx.ec_outer_stack = partial->pt_ectx_stack;
912 	    ectx.ec_outer_frame = partial->pt_ectx_frame;
913 	}
914     }
915 
916     // dummy frame entries
917     for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
918     {
919 	STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
920 	++ectx.ec_stack.ga_len;
921     }
922 
923     {
924 	// Reserve space for local variables and any closure reference count.
925 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
926 							 + ufunc->uf_dfunc_idx;
927 
928 	for (idx = 0; idx < dfunc->df_varcount; ++idx)
929 	    STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
930 	ectx.ec_stack.ga_len += dfunc->df_varcount;
931 	if (dfunc->df_has_closure)
932 	{
933 	    STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
934 	    STACK_TV_VAR(idx)->vval.v_number = 0;
935 	    ++ectx.ec_stack.ga_len;
936 	}
937 
938 	ectx.ec_instr = dfunc->df_instr;
939     }
940 
941     // Following errors are in the function, not the caller.
942     // Commands behave like vim9script.
943     estack_push_ufunc(ufunc, 1);
944     current_sctx = ufunc->uf_script_ctx;
945     current_sctx.sc_version = SCRIPT_VERSION_VIM9;
946 
947     // Do turn errors into exceptions.
948     suppress_errthrow = FALSE;
949 
950     // Decide where to start execution, handles optional arguments.
951     init_instr_idx(ufunc, argc, &ectx);
952 
953     for (;;)
954     {
955 	isn_T	    *iptr;
956 
957 	if (++breakcheck_count >= 100)
958 	{
959 	    line_breakcheck();
960 	    breakcheck_count = 0;
961 	}
962 	if (got_int)
963 	{
964 	    // Turn CTRL-C into an exception.
965 	    got_int = FALSE;
966 	    if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
967 		goto failed;
968 	    did_throw = TRUE;
969 	}
970 
971 	if (did_emsg && msg_list != NULL && *msg_list != NULL)
972 	{
973 	    // Turn an error message into an exception.
974 	    did_emsg = FALSE;
975 	    if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
976 		goto failed;
977 	    did_throw = TRUE;
978 	    *msg_list = NULL;
979 	}
980 
981 	if (did_throw && !ectx.ec_in_catch)
982 	{
983 	    garray_T	*trystack = &ectx.ec_trystack;
984 	    trycmd_T    *trycmd = NULL;
985 
986 	    // An exception jumps to the first catch, finally, or returns from
987 	    // the current function.
988 	    if (trystack->ga_len > 0)
989 		trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
990 	    if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
991 	    {
992 		// jump to ":catch" or ":finally"
993 		ectx.ec_in_catch = TRUE;
994 		ectx.ec_iidx = trycmd->tcd_catch_idx;
995 	    }
996 	    else
997 	    {
998 		// Not inside try or need to return from current functions.
999 		// Push a dummy return value.
1000 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1001 		    goto failed;
1002 		tv = STACK_TV_BOT(0);
1003 		tv->v_type = VAR_NUMBER;
1004 		tv->vval.v_number = 0;
1005 		++ectx.ec_stack.ga_len;
1006 		if (ectx.ec_frame_idx == initial_frame_idx)
1007 		{
1008 		    // At the toplevel we are done.
1009 		    need_rethrow = TRUE;
1010 		    if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1011 			goto failed;
1012 		    goto done;
1013 		}
1014 
1015 		if (func_return(&ectx) == FAIL)
1016 		    goto failed;
1017 	    }
1018 	    continue;
1019 	}
1020 
1021 	iptr = &ectx.ec_instr[ectx.ec_iidx++];
1022 	switch (iptr->isn_type)
1023 	{
1024 	    // execute Ex command line
1025 	    case ISN_EXEC:
1026 		SOURCING_LNUM = iptr->isn_lnum;
1027 		do_cmdline_cmd(iptr->isn_arg.string);
1028 		break;
1029 
1030 	    // execute Ex command from pieces on the stack
1031 	    case ISN_EXECCONCAT:
1032 		{
1033 		    int	    count = iptr->isn_arg.number;
1034 		    size_t  len = 0;
1035 		    int	    pass;
1036 		    int	    i;
1037 		    char_u  *cmd = NULL;
1038 		    char_u  *str;
1039 
1040 		    for (pass = 1; pass <= 2; ++pass)
1041 		    {
1042 			for (i = 0; i < count; ++i)
1043 			{
1044 			    tv = STACK_TV_BOT(i - count);
1045 			    str = tv->vval.v_string;
1046 			    if (str != NULL && *str != NUL)
1047 			    {
1048 				if (pass == 2)
1049 				    STRCPY(cmd + len, str);
1050 				len += STRLEN(str);
1051 			    }
1052 			    if (pass == 2)
1053 				clear_tv(tv);
1054 			}
1055 			if (pass == 1)
1056 			{
1057 			    cmd = alloc(len + 1);
1058 			    if (cmd == NULL)
1059 				goto failed;
1060 			    len = 0;
1061 			}
1062 		    }
1063 
1064 		    SOURCING_LNUM = iptr->isn_lnum;
1065 		    do_cmdline_cmd(cmd);
1066 		    vim_free(cmd);
1067 		}
1068 		break;
1069 
1070 	    // execute :echo {string} ...
1071 	    case ISN_ECHO:
1072 		{
1073 		    int count = iptr->isn_arg.echo.echo_count;
1074 		    int	atstart = TRUE;
1075 		    int needclr = TRUE;
1076 
1077 		    for (idx = 0; idx < count; ++idx)
1078 		    {
1079 			tv = STACK_TV_BOT(idx - count);
1080 			echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1081 							   &atstart, &needclr);
1082 			clear_tv(tv);
1083 		    }
1084 		    if (needclr)
1085 			msg_clr_eos();
1086 		    ectx.ec_stack.ga_len -= count;
1087 		}
1088 		break;
1089 
1090 	    // :execute {string} ...
1091 	    // :echomsg {string} ...
1092 	    // :echoerr {string} ...
1093 	    case ISN_EXECUTE:
1094 	    case ISN_ECHOMSG:
1095 	    case ISN_ECHOERR:
1096 		{
1097 		    int		count = iptr->isn_arg.number;
1098 		    garray_T	ga;
1099 		    char_u	buf[NUMBUFLEN];
1100 		    char_u	*p;
1101 		    int		len;
1102 		    int		failed = FALSE;
1103 
1104 		    ga_init2(&ga, 1, 80);
1105 		    for (idx = 0; idx < count; ++idx)
1106 		    {
1107 			tv = STACK_TV_BOT(idx - count);
1108 			if (iptr->isn_type == ISN_EXECUTE)
1109 			{
1110 			    if (tv->v_type == VAR_CHANNEL
1111 						      || tv->v_type == VAR_JOB)
1112 			    {
1113 				SOURCING_LNUM = iptr->isn_lnum;
1114 				emsg(_(e_inval_string));
1115 				break;
1116 			    }
1117 			    else
1118 				p = tv_get_string_buf(tv, buf);
1119 			}
1120 			else
1121 			    p = tv_stringify(tv, buf);
1122 
1123 			len = (int)STRLEN(p);
1124 			if (ga_grow(&ga, len + 2) == FAIL)
1125 			    failed = TRUE;
1126 			else
1127 			{
1128 			    if (ga.ga_len > 0)
1129 				((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1130 			    STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1131 			    ga.ga_len += len;
1132 			}
1133 			clear_tv(tv);
1134 		    }
1135 		    ectx.ec_stack.ga_len -= count;
1136 		    if (failed)
1137 			goto on_error;
1138 
1139 		    if (ga.ga_data != NULL)
1140 		    {
1141 			if (iptr->isn_type == ISN_EXECUTE)
1142 			{
1143 			    SOURCING_LNUM = iptr->isn_lnum;
1144 			    do_cmdline_cmd((char_u *)ga.ga_data);
1145 			}
1146 			else
1147 			{
1148 			    msg_sb_eol();
1149 			    if (iptr->isn_type == ISN_ECHOMSG)
1150 			    {
1151 				msg_attr(ga.ga_data, echo_attr);
1152 				out_flush();
1153 			    }
1154 			    else
1155 			    {
1156 				SOURCING_LNUM = iptr->isn_lnum;
1157 				emsg(ga.ga_data);
1158 			    }
1159 			}
1160 		    }
1161 		    ga_clear(&ga);
1162 		}
1163 		break;
1164 
1165 	    // load local variable or argument
1166 	    case ISN_LOAD:
1167 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1168 		    goto failed;
1169 		copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1170 		++ectx.ec_stack.ga_len;
1171 		break;
1172 
1173 	    // load variable or argument from outer scope
1174 	    case ISN_LOADOUTER:
1175 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1176 		    goto failed;
1177 		copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1178 							      STACK_TV_BOT(0));
1179 		++ectx.ec_stack.ga_len;
1180 		break;
1181 
1182 	    // load v: variable
1183 	    case ISN_LOADV:
1184 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1185 		    goto failed;
1186 		copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1187 		++ectx.ec_stack.ga_len;
1188 		break;
1189 
1190 	    // load s: variable in Vim9 script
1191 	    case ISN_LOADSCRIPT:
1192 		{
1193 		    scriptitem_T *si =
1194 				  SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
1195 		    svar_T	 *sv;
1196 
1197 		    sv = ((svar_T *)si->sn_var_vals.ga_data)
1198 					     + iptr->isn_arg.script.script_idx;
1199 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1200 			goto failed;
1201 		    copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1202 		    ++ectx.ec_stack.ga_len;
1203 		}
1204 		break;
1205 
1206 	    // load s: variable in old script
1207 	    case ISN_LOADS:
1208 		{
1209 		    hashtab_T	*ht = &SCRIPT_VARS(
1210 					       iptr->isn_arg.loadstore.ls_sid);
1211 		    char_u	*name = iptr->isn_arg.loadstore.ls_name;
1212 		    dictitem_T	*di = find_var_in_ht(ht, 0, name, TRUE);
1213 
1214 		    if (di == NULL)
1215 		    {
1216 			SOURCING_LNUM = iptr->isn_lnum;
1217 			semsg(_(e_undefined_variable_str), name);
1218 			goto on_error;
1219 		    }
1220 		    else
1221 		    {
1222 			if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1223 			    goto failed;
1224 			copy_tv(&di->di_tv, STACK_TV_BOT(0));
1225 			++ectx.ec_stack.ga_len;
1226 		    }
1227 		}
1228 		break;
1229 
1230 	    // load g:/b:/w:/t: variable
1231 	    case ISN_LOADG:
1232 	    case ISN_LOADB:
1233 	    case ISN_LOADW:
1234 	    case ISN_LOADT:
1235 		{
1236 		    dictitem_T *di = NULL;
1237 		    hashtab_T *ht = NULL;
1238 		    char namespace;
1239 
1240 		    switch (iptr->isn_type)
1241 		    {
1242 			case ISN_LOADG:
1243 			    ht = get_globvar_ht();
1244 			    namespace = 'g';
1245 			    break;
1246 			case ISN_LOADB:
1247 			    ht = &curbuf->b_vars->dv_hashtab;
1248 			    namespace = 'b';
1249 			    break;
1250 			case ISN_LOADW:
1251 			    ht = &curwin->w_vars->dv_hashtab;
1252 			    namespace = 'w';
1253 			    break;
1254 			case ISN_LOADT:
1255 			    ht = &curtab->tp_vars->dv_hashtab;
1256 			    namespace = 't';
1257 			    break;
1258 			default:  // Cannot reach here
1259 			    goto failed;
1260 		    }
1261 		    di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
1262 
1263 		    if (di == NULL)
1264 		    {
1265 			SOURCING_LNUM = iptr->isn_lnum;
1266 			semsg(_(e_undefined_variable_char_str),
1267 					     namespace, iptr->isn_arg.string);
1268 			goto on_error;
1269 		    }
1270 		    else
1271 		    {
1272 			if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1273 			    goto failed;
1274 			copy_tv(&di->di_tv, STACK_TV_BOT(0));
1275 			++ectx.ec_stack.ga_len;
1276 		    }
1277 		}
1278 		break;
1279 
1280 	    // load g:/b:/w:/t: namespace
1281 	    case ISN_LOADGDICT:
1282 	    case ISN_LOADBDICT:
1283 	    case ISN_LOADWDICT:
1284 	    case ISN_LOADTDICT:
1285 		{
1286 		    dict_T *d = NULL;
1287 
1288 		    switch (iptr->isn_type)
1289 		    {
1290 			case ISN_LOADGDICT: d = get_globvar_dict(); break;
1291 			case ISN_LOADBDICT: d = curbuf->b_vars; break;
1292 			case ISN_LOADWDICT: d = curwin->w_vars; break;
1293 			case ISN_LOADTDICT: d = curtab->tp_vars; break;
1294 			default:  // Cannot reach here
1295 			    goto failed;
1296 		    }
1297 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1298 			goto failed;
1299 		    tv = STACK_TV_BOT(0);
1300 		    tv->v_type = VAR_DICT;
1301 		    tv->v_lock = 0;
1302 		    tv->vval.v_dict = d;
1303 		    ++ectx.ec_stack.ga_len;
1304 		}
1305 		break;
1306 
1307 	    // load &option
1308 	    case ISN_LOADOPT:
1309 		{
1310 		    typval_T	optval;
1311 		    char_u	*name = iptr->isn_arg.string;
1312 
1313 		    // This is not expected to fail, name is checked during
1314 		    // compilation: don't set SOURCING_LNUM.
1315 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1316 			goto failed;
1317 		    if (eval_option(&name, &optval, TRUE) == FAIL)
1318 			goto failed;
1319 		    *STACK_TV_BOT(0) = optval;
1320 		    ++ectx.ec_stack.ga_len;
1321 		}
1322 		break;
1323 
1324 	    // load $ENV
1325 	    case ISN_LOADENV:
1326 		{
1327 		    typval_T	optval;
1328 		    char_u	*name = iptr->isn_arg.string;
1329 
1330 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1331 			goto failed;
1332 		    // name is always valid, checked when compiling
1333 		    (void)eval_env_var(&name, &optval, TRUE);
1334 		    *STACK_TV_BOT(0) = optval;
1335 		    ++ectx.ec_stack.ga_len;
1336 		}
1337 		break;
1338 
1339 	    // load @register
1340 	    case ISN_LOADREG:
1341 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1342 		    goto failed;
1343 		tv = STACK_TV_BOT(0);
1344 		tv->v_type = VAR_STRING;
1345 		tv->v_lock = 0;
1346 		tv->vval.v_string = get_reg_contents(
1347 					  iptr->isn_arg.number, GREG_EXPR_SRC);
1348 		++ectx.ec_stack.ga_len;
1349 		break;
1350 
1351 	    // store local variable
1352 	    case ISN_STORE:
1353 		--ectx.ec_stack.ga_len;
1354 		tv = STACK_TV_VAR(iptr->isn_arg.number);
1355 		clear_tv(tv);
1356 		*tv = *STACK_TV_BOT(0);
1357 		break;
1358 
1359 	    // store variable or argument in outer scope
1360 	    case ISN_STOREOUTER:
1361 		--ectx.ec_stack.ga_len;
1362 		tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1363 		clear_tv(tv);
1364 		*tv = *STACK_TV_BOT(0);
1365 		break;
1366 
1367 	    // store s: variable in old script
1368 	    case ISN_STORES:
1369 		{
1370 		    hashtab_T	*ht = &SCRIPT_VARS(
1371 					       iptr->isn_arg.loadstore.ls_sid);
1372 		    char_u	*name = iptr->isn_arg.loadstore.ls_name;
1373 		    dictitem_T	*di = find_var_in_ht(ht, 0, name + 2, TRUE);
1374 
1375 		    --ectx.ec_stack.ga_len;
1376 		    if (di == NULL)
1377 			store_var(name, STACK_TV_BOT(0));
1378 		    else
1379 		    {
1380 			clear_tv(&di->di_tv);
1381 			di->di_tv = *STACK_TV_BOT(0);
1382 		    }
1383 		}
1384 		break;
1385 
1386 	    // store script-local variable in Vim9 script
1387 	    case ISN_STORESCRIPT:
1388 		{
1389 		    scriptitem_T *si = SCRIPT_ITEM(
1390 					      iptr->isn_arg.script.script_sid);
1391 		    svar_T	 *sv = ((svar_T *)si->sn_var_vals.ga_data)
1392 					     + iptr->isn_arg.script.script_idx;
1393 
1394 		    --ectx.ec_stack.ga_len;
1395 		    clear_tv(sv->sv_tv);
1396 		    *sv->sv_tv = *STACK_TV_BOT(0);
1397 		}
1398 		break;
1399 
1400 	    // store option
1401 	    case ISN_STOREOPT:
1402 		{
1403 		    long	n = 0;
1404 		    char_u	*s = NULL;
1405 		    char	*msg;
1406 
1407 		    --ectx.ec_stack.ga_len;
1408 		    tv = STACK_TV_BOT(0);
1409 		    if (tv->v_type == VAR_STRING)
1410 		    {
1411 			s = tv->vval.v_string;
1412 			if (s == NULL)
1413 			    s = (char_u *)"";
1414 		    }
1415 		    else
1416 			// must be VAR_NUMBER, CHECKTYPE makes sure
1417 			n = tv->vval.v_number;
1418 		    msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1419 					n, s, iptr->isn_arg.storeopt.so_flags);
1420 		    clear_tv(tv);
1421 		    if (msg != NULL)
1422 		    {
1423 			SOURCING_LNUM = iptr->isn_lnum;
1424 			emsg(_(msg));
1425 			goto on_error;
1426 		    }
1427 		}
1428 		break;
1429 
1430 	    // store $ENV
1431 	    case ISN_STOREENV:
1432 		--ectx.ec_stack.ga_len;
1433 		tv = STACK_TV_BOT(0);
1434 		vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1435 		clear_tv(tv);
1436 		break;
1437 
1438 	    // store @r
1439 	    case ISN_STOREREG:
1440 		{
1441 		    int	reg = iptr->isn_arg.number;
1442 
1443 		    --ectx.ec_stack.ga_len;
1444 		    tv = STACK_TV_BOT(0);
1445 		    write_reg_contents(reg == '@' ? '"' : reg,
1446 						 tv_get_string(tv), -1, FALSE);
1447 		    clear_tv(tv);
1448 		}
1449 		break;
1450 
1451 	    // store v: variable
1452 	    case ISN_STOREV:
1453 		--ectx.ec_stack.ga_len;
1454 		if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1455 								       == FAIL)
1456 		    // should not happen, type is checked when compiling
1457 		    goto on_error;
1458 		break;
1459 
1460 	    // store g:/b:/w:/t: variable
1461 	    case ISN_STOREG:
1462 	    case ISN_STOREB:
1463 	    case ISN_STOREW:
1464 	    case ISN_STORET:
1465 		{
1466 		    dictitem_T *di;
1467 		    hashtab_T *ht;
1468 		    switch (iptr->isn_type)
1469 		    {
1470 			case ISN_STOREG:
1471 			    ht = get_globvar_ht();
1472 			    break;
1473 			case ISN_STOREB:
1474 			    ht = &curbuf->b_vars->dv_hashtab;
1475 			    break;
1476 			case ISN_STOREW:
1477 			    ht = &curwin->w_vars->dv_hashtab;
1478 			    break;
1479 			case ISN_STORET:
1480 			    ht = &curtab->tp_vars->dv_hashtab;
1481 			    break;
1482 			default:  // Cannot reach here
1483 			    goto failed;
1484 		    }
1485 
1486 		    --ectx.ec_stack.ga_len;
1487 		    di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
1488 		    if (di == NULL)
1489 			store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
1490 		    else
1491 		    {
1492 			clear_tv(&di->di_tv);
1493 			di->di_tv = *STACK_TV_BOT(0);
1494 		    }
1495 		}
1496 		break;
1497 
1498 	    // store number in local variable
1499 	    case ISN_STORENR:
1500 		tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
1501 		clear_tv(tv);
1502 		tv->v_type = VAR_NUMBER;
1503 		tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
1504 		break;
1505 
1506 	    // store value in list variable
1507 	    case ISN_STORELIST:
1508 		{
1509 		    typval_T	*tv_idx = STACK_TV_BOT(-2);
1510 		    varnumber_T	lidx = tv_idx->vval.v_number;
1511 		    typval_T	*tv_list = STACK_TV_BOT(-1);
1512 		    list_T	*list = tv_list->vval.v_list;
1513 
1514 		    SOURCING_LNUM = iptr->isn_lnum;
1515 		    if (lidx < 0 && list->lv_len + lidx >= 0)
1516 			// negative index is relative to the end
1517 			lidx = list->lv_len + lidx;
1518 		    if (lidx < 0 || lidx > list->lv_len)
1519 		    {
1520 			semsg(_(e_listidx), lidx);
1521 			goto on_error;
1522 		    }
1523 		    tv = STACK_TV_BOT(-3);
1524 		    if (lidx < list->lv_len)
1525 		    {
1526 			listitem_T *li = list_find(list, lidx);
1527 
1528 			if (error_if_locked(li->li_tv.v_lock,
1529 						    e_cannot_change_list_item))
1530 			    goto failed;
1531 			// overwrite existing list item
1532 			clear_tv(&li->li_tv);
1533 			li->li_tv = *tv;
1534 		    }
1535 		    else
1536 		    {
1537 			if (error_if_locked(list->lv_lock,
1538 							 e_cannot_change_list))
1539 			    goto failed;
1540 			// append to list, only fails when out of memory
1541 			if (list_append_tv(list, tv) == FAIL)
1542 			    goto failed;
1543 			clear_tv(tv);
1544 		    }
1545 		    clear_tv(tv_idx);
1546 		    clear_tv(tv_list);
1547 		    ectx.ec_stack.ga_len -= 3;
1548 		}
1549 		break;
1550 
1551 	    // store value in dict variable
1552 	    case ISN_STOREDICT:
1553 		{
1554 		    typval_T	*tv_key = STACK_TV_BOT(-2);
1555 		    char_u	*key = tv_key->vval.v_string;
1556 		    typval_T	*tv_dict = STACK_TV_BOT(-1);
1557 		    dict_T	*dict = tv_dict->vval.v_dict;
1558 		    dictitem_T	*di;
1559 
1560 		    SOURCING_LNUM = iptr->isn_lnum;
1561 		    if (dict == NULL)
1562 		    {
1563 			emsg(_(e_dictionary_not_set));
1564 			goto on_error;
1565 		    }
1566 		    if (key == NULL)
1567 			key = (char_u *)"";
1568 		    tv = STACK_TV_BOT(-3);
1569 		    di = dict_find(dict, key, -1);
1570 		    if (di != NULL)
1571 		    {
1572 			if (error_if_locked(di->di_tv.v_lock,
1573 						    e_cannot_change_dict_item))
1574 			    goto failed;
1575 			// overwrite existing value
1576 			clear_tv(&di->di_tv);
1577 			di->di_tv = *tv;
1578 		    }
1579 		    else
1580 		    {
1581 			if (error_if_locked(dict->dv_lock,
1582 							 e_cannot_change_dict))
1583 			    goto failed;
1584 			// add to dict, only fails when out of memory
1585 			if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1586 			    goto failed;
1587 			clear_tv(tv);
1588 		    }
1589 		    clear_tv(tv_key);
1590 		    clear_tv(tv_dict);
1591 		    ectx.ec_stack.ga_len -= 3;
1592 		}
1593 		break;
1594 
1595 	    // push constant
1596 	    case ISN_PUSHNR:
1597 	    case ISN_PUSHBOOL:
1598 	    case ISN_PUSHSPEC:
1599 	    case ISN_PUSHF:
1600 	    case ISN_PUSHS:
1601 	    case ISN_PUSHBLOB:
1602 	    case ISN_PUSHFUNC:
1603 	    case ISN_PUSHCHANNEL:
1604 	    case ISN_PUSHJOB:
1605 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1606 		    goto failed;
1607 		tv = STACK_TV_BOT(0);
1608 		tv->v_lock = 0;
1609 		++ectx.ec_stack.ga_len;
1610 		switch (iptr->isn_type)
1611 		{
1612 		    case ISN_PUSHNR:
1613 			tv->v_type = VAR_NUMBER;
1614 			tv->vval.v_number = iptr->isn_arg.number;
1615 			break;
1616 		    case ISN_PUSHBOOL:
1617 			tv->v_type = VAR_BOOL;
1618 			tv->vval.v_number = iptr->isn_arg.number;
1619 			break;
1620 		    case ISN_PUSHSPEC:
1621 			tv->v_type = VAR_SPECIAL;
1622 			tv->vval.v_number = iptr->isn_arg.number;
1623 			break;
1624 #ifdef FEAT_FLOAT
1625 		    case ISN_PUSHF:
1626 			tv->v_type = VAR_FLOAT;
1627 			tv->vval.v_float = iptr->isn_arg.fnumber;
1628 			break;
1629 #endif
1630 		    case ISN_PUSHBLOB:
1631 			blob_copy(iptr->isn_arg.blob, tv);
1632 			break;
1633 		    case ISN_PUSHFUNC:
1634 			tv->v_type = VAR_FUNC;
1635 			if (iptr->isn_arg.string == NULL)
1636 			    tv->vval.v_string = NULL;
1637 			else
1638 			    tv->vval.v_string =
1639 					     vim_strsave(iptr->isn_arg.string);
1640 			break;
1641 		    case ISN_PUSHCHANNEL:
1642 #ifdef FEAT_JOB_CHANNEL
1643 			tv->v_type = VAR_CHANNEL;
1644 			tv->vval.v_channel = iptr->isn_arg.channel;
1645 			if (tv->vval.v_channel != NULL)
1646 			    ++tv->vval.v_channel->ch_refcount;
1647 #endif
1648 			break;
1649 		    case ISN_PUSHJOB:
1650 #ifdef FEAT_JOB_CHANNEL
1651 			tv->v_type = VAR_JOB;
1652 			tv->vval.v_job = iptr->isn_arg.job;
1653 			if (tv->vval.v_job != NULL)
1654 			    ++tv->vval.v_job->jv_refcount;
1655 #endif
1656 			break;
1657 		    default:
1658 			tv->v_type = VAR_STRING;
1659 			tv->vval.v_string = vim_strsave(
1660 				iptr->isn_arg.string == NULL
1661 					? (char_u *)"" : iptr->isn_arg.string);
1662 		}
1663 		break;
1664 
1665 	    case ISN_UNLET:
1666 		if (do_unlet(iptr->isn_arg.unlet.ul_name,
1667 				       iptr->isn_arg.unlet.ul_forceit) == FAIL)
1668 		    goto on_error;
1669 		break;
1670 	    case ISN_UNLETENV:
1671 		vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1672 		break;
1673 
1674 	    case ISN_LOCKCONST:
1675 		item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1676 		break;
1677 
1678 	    // create a list from items on the stack; uses a single allocation
1679 	    // for the list header and the items
1680 	    case ISN_NEWLIST:
1681 		if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1682 		    goto failed;
1683 		break;
1684 
1685 	    // create a dict from items on the stack
1686 	    case ISN_NEWDICT:
1687 		{
1688 		    int		count = iptr->isn_arg.number;
1689 		    dict_T	*dict = dict_alloc();
1690 		    dictitem_T	*item;
1691 
1692 		    if (dict == NULL)
1693 			goto failed;
1694 		    for (idx = 0; idx < count; ++idx)
1695 		    {
1696 			// have already checked key type is VAR_STRING
1697 			tv = STACK_TV_BOT(2 * (idx - count));
1698 			// check key is unique
1699 			item = dict_find(dict, tv->vval.v_string, -1);
1700 			if (item != NULL)
1701 			{
1702 			    SOURCING_LNUM = iptr->isn_lnum;
1703 			    semsg(_(e_duplicate_key), tv->vval.v_string);
1704 			    dict_unref(dict);
1705 			    goto on_error;
1706 			}
1707 			item = dictitem_alloc(tv->vval.v_string);
1708 			clear_tv(tv);
1709 			if (item == NULL)
1710 			{
1711 			    dict_unref(dict);
1712 			    goto failed;
1713 			}
1714 			item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1715 			item->di_tv.v_lock = 0;
1716 			if (dict_add(dict, item) == FAIL)
1717 			{
1718 			    // can this ever happen?
1719 			    dict_unref(dict);
1720 			    goto failed;
1721 			}
1722 		    }
1723 
1724 		    if (count > 0)
1725 			ectx.ec_stack.ga_len -= 2 * count - 1;
1726 		    else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1727 			goto failed;
1728 		    else
1729 			++ectx.ec_stack.ga_len;
1730 		    tv = STACK_TV_BOT(-1);
1731 		    tv->v_type = VAR_DICT;
1732 		    tv->v_lock = 0;
1733 		    tv->vval.v_dict = dict;
1734 		    ++dict->dv_refcount;
1735 		}
1736 		break;
1737 
1738 	    // call a :def function
1739 	    case ISN_DCALL:
1740 		SOURCING_LNUM = iptr->isn_lnum;
1741 		if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1742 			      iptr->isn_arg.dfunc.cdf_argcount,
1743 			      &ectx) == FAIL)
1744 		    goto on_error;
1745 		break;
1746 
1747 	    // call a builtin function
1748 	    case ISN_BCALL:
1749 		SOURCING_LNUM = iptr->isn_lnum;
1750 		if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1751 			      iptr->isn_arg.bfunc.cbf_argcount,
1752 			      &ectx) == FAIL)
1753 		    goto on_error;
1754 		break;
1755 
1756 	    // call a funcref or partial
1757 	    case ISN_PCALL:
1758 		{
1759 		    cpfunc_T	*pfunc = &iptr->isn_arg.pfunc;
1760 		    int		r;
1761 		    typval_T	partial_tv;
1762 
1763 		    SOURCING_LNUM = iptr->isn_lnum;
1764 		    if (pfunc->cpf_top)
1765 		    {
1766 			// funcref is above the arguments
1767 			tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1768 		    }
1769 		    else
1770 		    {
1771 			// Get the funcref from the stack.
1772 			--ectx.ec_stack.ga_len;
1773 			partial_tv = *STACK_TV_BOT(0);
1774 			tv = &partial_tv;
1775 		    }
1776 		    r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1777 		    if (tv == &partial_tv)
1778 			clear_tv(&partial_tv);
1779 		    if (r == FAIL)
1780 			goto on_error;
1781 		}
1782 		break;
1783 
1784 	    case ISN_PCALL_END:
1785 		// PCALL finished, arguments have been consumed and replaced by
1786 		// the return value.  Now clear the funcref from the stack,
1787 		// and move the return value in its place.
1788 		--ectx.ec_stack.ga_len;
1789 		clear_tv(STACK_TV_BOT(-1));
1790 		*STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1791 		break;
1792 
1793 	    // call a user defined function or funcref/partial
1794 	    case ISN_UCALL:
1795 		{
1796 		    cufunc_T	*cufunc = &iptr->isn_arg.ufunc;
1797 
1798 		    SOURCING_LNUM = iptr->isn_lnum;
1799 		    if (call_eval_func(cufunc->cuf_name,
1800 				    cufunc->cuf_argcount, &ectx, iptr) == FAIL)
1801 			goto on_error;
1802 		}
1803 		break;
1804 
1805 	    // return from a :def function call
1806 	    case ISN_RETURN:
1807 		{
1808 		    garray_T	*trystack = &ectx.ec_trystack;
1809 		    trycmd_T    *trycmd = NULL;
1810 
1811 		    if (trystack->ga_len > 0)
1812 			trycmd = ((trycmd_T *)trystack->ga_data)
1813 							+ trystack->ga_len - 1;
1814 		    if (trycmd != NULL
1815 				  && trycmd->tcd_frame_idx == ectx.ec_frame_idx
1816 			    && trycmd->tcd_finally_idx != 0)
1817 		    {
1818 			// jump to ":finally"
1819 			ectx.ec_iidx = trycmd->tcd_finally_idx;
1820 			trycmd->tcd_return = TRUE;
1821 		    }
1822 		    else
1823 			goto func_return;
1824 		}
1825 		break;
1826 
1827 	    // push a function reference to a compiled function
1828 	    case ISN_FUNCREF:
1829 		{
1830 		    partial_T   *pt = NULL;
1831 		    dfunc_T	*pt_dfunc;
1832 
1833 		    pt = ALLOC_CLEAR_ONE(partial_T);
1834 		    if (pt == NULL)
1835 			goto failed;
1836 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1837 		    {
1838 			vim_free(pt);
1839 			goto failed;
1840 		    }
1841 		    pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1842 					       + iptr->isn_arg.funcref.fr_func;
1843 		    pt->pt_func = pt_dfunc->df_ufunc;
1844 		    pt->pt_refcount = 1;
1845 
1846 		    if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1847 		    {
1848 			dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
1849 							   + ectx.ec_dfunc_idx;
1850 
1851 			// The closure needs to find arguments and local
1852 			// variables in the current stack.
1853 			pt->pt_ectx_stack = &ectx.ec_stack;
1854 			pt->pt_ectx_frame = ectx.ec_frame_idx;
1855 
1856 			// If this function returns and the closure is still
1857 			// being used, we need to make a copy of the context
1858 			// (arguments and local variables). Store a reference
1859 			// to the partial so we can handle that.
1860 			if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
1861 			{
1862 			    vim_free(pt);
1863 			    goto failed;
1864 			}
1865 			// Extra variable keeps the count of closures created
1866 			// in the current function call.
1867 			tv = STACK_TV_VAR(dfunc->df_varcount);
1868 			++tv->vval.v_number;
1869 
1870 			((partial_T **)ectx.ec_funcrefs.ga_data)
1871 					       [ectx.ec_funcrefs.ga_len] = pt;
1872 			++pt->pt_refcount;
1873 			++ectx.ec_funcrefs.ga_len;
1874 		    }
1875 		    ++pt_dfunc->df_ufunc->uf_refcount;
1876 
1877 		    tv = STACK_TV_BOT(0);
1878 		    ++ectx.ec_stack.ga_len;
1879 		    tv->vval.v_partial = pt;
1880 		    tv->v_type = VAR_PARTIAL;
1881 		    tv->v_lock = 0;
1882 		}
1883 		break;
1884 
1885 	    // Create a global function from a lambda.
1886 	    case ISN_NEWFUNC:
1887 		{
1888 		    newfunc_T	*newfunc = &iptr->isn_arg.newfunc;
1889 
1890 		    copy_func(newfunc->nf_lambda, newfunc->nf_global);
1891 		}
1892 		break;
1893 
1894 	    // jump if a condition is met
1895 	    case ISN_JUMP:
1896 		{
1897 		    jumpwhen_T	when = iptr->isn_arg.jump.jump_when;
1898 		    int		jump = TRUE;
1899 
1900 		    if (when != JUMP_ALWAYS)
1901 		    {
1902 			tv = STACK_TV_BOT(-1);
1903 			jump = tv2bool(tv);
1904 			if (when == JUMP_IF_FALSE
1905 					     || when == JUMP_AND_KEEP_IF_FALSE)
1906 			    jump = !jump;
1907 			if (when == JUMP_IF_FALSE || !jump)
1908 			{
1909 			    // drop the value from the stack
1910 			    clear_tv(tv);
1911 			    --ectx.ec_stack.ga_len;
1912 			}
1913 		    }
1914 		    if (jump)
1915 			ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1916 		}
1917 		break;
1918 
1919 	    // top of a for loop
1920 	    case ISN_FOR:
1921 		{
1922 		    list_T	*list = STACK_TV_BOT(-1)->vval.v_list;
1923 		    typval_T	*idxtv =
1924 				   STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1925 
1926 		    // push the next item from the list
1927 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1928 			goto failed;
1929 		    ++idxtv->vval.v_number;
1930 		    if (list == NULL || idxtv->vval.v_number >= list->lv_len)
1931 			// past the end of the list, jump to "endfor"
1932 			ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1933 		    else if (list->lv_first == &range_list_item)
1934 		    {
1935 			// non-materialized range() list
1936 			tv = STACK_TV_BOT(0);
1937 			tv->v_type = VAR_NUMBER;
1938 			tv->v_lock = 0;
1939 			tv->vval.v_number = list_find_nr(
1940 					     list, idxtv->vval.v_number, NULL);
1941 			++ectx.ec_stack.ga_len;
1942 		    }
1943 		    else
1944 		    {
1945 			listitem_T *li = list_find(list, idxtv->vval.v_number);
1946 
1947 			copy_tv(&li->li_tv, STACK_TV_BOT(0));
1948 			++ectx.ec_stack.ga_len;
1949 		    }
1950 		}
1951 		break;
1952 
1953 	    // start of ":try" block
1954 	    case ISN_TRY:
1955 		{
1956 		    trycmd_T    *trycmd = NULL;
1957 
1958 		    if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
1959 			goto failed;
1960 		    trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1961 						     + ectx.ec_trystack.ga_len;
1962 		    ++ectx.ec_trystack.ga_len;
1963 		    ++trylevel;
1964 		    trycmd->tcd_frame_idx = ectx.ec_frame_idx;
1965 		    trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1966 		    trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
1967 		    trycmd->tcd_caught = FALSE;
1968 		    trycmd->tcd_return = FALSE;
1969 		}
1970 		break;
1971 
1972 	    case ISN_PUSHEXC:
1973 		if (current_exception == NULL)
1974 		{
1975 		    SOURCING_LNUM = iptr->isn_lnum;
1976 		    iemsg("Evaluating catch while current_exception is NULL");
1977 		    goto failed;
1978 		}
1979 		if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1980 		    goto failed;
1981 		tv = STACK_TV_BOT(0);
1982 		++ectx.ec_stack.ga_len;
1983 		tv->v_type = VAR_STRING;
1984 		tv->v_lock = 0;
1985 		tv->vval.v_string = vim_strsave(
1986 					   (char_u *)current_exception->value);
1987 		break;
1988 
1989 	    case ISN_CATCH:
1990 		{
1991 		    garray_T	*trystack = &ectx.ec_trystack;
1992 
1993 		    if (trystack->ga_len > 0)
1994 		    {
1995 			trycmd_T    *trycmd = ((trycmd_T *)trystack->ga_data)
1996 							+ trystack->ga_len - 1;
1997 			trycmd->tcd_caught = TRUE;
1998 		    }
1999 		    did_emsg = got_int = did_throw = FALSE;
2000 		    catch_exception(current_exception);
2001 		}
2002 		break;
2003 
2004 	    // end of ":try" block
2005 	    case ISN_ENDTRY:
2006 		{
2007 		    garray_T	*trystack = &ectx.ec_trystack;
2008 
2009 		    if (trystack->ga_len > 0)
2010 		    {
2011 			trycmd_T    *trycmd = NULL;
2012 
2013 			--trystack->ga_len;
2014 			--trylevel;
2015 			ectx.ec_in_catch = FALSE;
2016 			trycmd = ((trycmd_T *)trystack->ga_data)
2017 							    + trystack->ga_len;
2018 			if (trycmd->tcd_caught && current_exception != NULL)
2019 			{
2020 			    // discard the exception
2021 			    if (caught_stack == current_exception)
2022 				caught_stack = caught_stack->caught;
2023 			    discard_current_exception();
2024 			}
2025 
2026 			if (trycmd->tcd_return)
2027 			    goto func_return;
2028 		    }
2029 		}
2030 		break;
2031 
2032 	    case ISN_THROW:
2033 		--ectx.ec_stack.ga_len;
2034 		tv = STACK_TV_BOT(0);
2035 		if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2036 		{
2037 		    vim_free(tv->vval.v_string);
2038 		    goto failed;
2039 		}
2040 		did_throw = TRUE;
2041 		break;
2042 
2043 	    // compare with special values
2044 	    case ISN_COMPAREBOOL:
2045 	    case ISN_COMPARESPECIAL:
2046 		{
2047 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2048 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2049 		    varnumber_T arg1 = tv1->vval.v_number;
2050 		    varnumber_T arg2 = tv2->vval.v_number;
2051 		    int		res;
2052 
2053 		    switch (iptr->isn_arg.op.op_type)
2054 		    {
2055 			case EXPR_EQUAL: res = arg1 == arg2; break;
2056 			case EXPR_NEQUAL: res = arg1 != arg2; break;
2057 			default: res = 0; break;
2058 		    }
2059 
2060 		    --ectx.ec_stack.ga_len;
2061 		    tv1->v_type = VAR_BOOL;
2062 		    tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2063 		}
2064 		break;
2065 
2066 	    // Operation with two number arguments
2067 	    case ISN_OPNR:
2068 	    case ISN_COMPARENR:
2069 		{
2070 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2071 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2072 		    varnumber_T arg1 = tv1->vval.v_number;
2073 		    varnumber_T arg2 = tv2->vval.v_number;
2074 		    varnumber_T res;
2075 
2076 		    switch (iptr->isn_arg.op.op_type)
2077 		    {
2078 			case EXPR_MULT: res = arg1 * arg2; break;
2079 			case EXPR_DIV: res = arg1 / arg2; break;
2080 			case EXPR_REM: res = arg1 % arg2; break;
2081 			case EXPR_SUB: res = arg1 - arg2; break;
2082 			case EXPR_ADD: res = arg1 + arg2; break;
2083 
2084 			case EXPR_EQUAL: res = arg1 == arg2; break;
2085 			case EXPR_NEQUAL: res = arg1 != arg2; break;
2086 			case EXPR_GREATER: res = arg1 > arg2; break;
2087 			case EXPR_GEQUAL: res = arg1 >= arg2; break;
2088 			case EXPR_SMALLER: res = arg1 < arg2; break;
2089 			case EXPR_SEQUAL: res = arg1 <= arg2; break;
2090 			default: res = 0; break;
2091 		    }
2092 
2093 		    --ectx.ec_stack.ga_len;
2094 		    if (iptr->isn_type == ISN_COMPARENR)
2095 		    {
2096 			tv1->v_type = VAR_BOOL;
2097 			tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2098 		    }
2099 		    else
2100 			tv1->vval.v_number = res;
2101 		}
2102 		break;
2103 
2104 	    // Computation with two float arguments
2105 	    case ISN_OPFLOAT:
2106 	    case ISN_COMPAREFLOAT:
2107 #ifdef FEAT_FLOAT
2108 		{
2109 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2110 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2111 		    float_T	arg1 = tv1->vval.v_float;
2112 		    float_T	arg2 = tv2->vval.v_float;
2113 		    float_T	res = 0;
2114 		    int		cmp = FALSE;
2115 
2116 		    switch (iptr->isn_arg.op.op_type)
2117 		    {
2118 			case EXPR_MULT: res = arg1 * arg2; break;
2119 			case EXPR_DIV: res = arg1 / arg2; break;
2120 			case EXPR_SUB: res = arg1 - arg2; break;
2121 			case EXPR_ADD: res = arg1 + arg2; break;
2122 
2123 			case EXPR_EQUAL: cmp = arg1 == arg2; break;
2124 			case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2125 			case EXPR_GREATER: cmp = arg1 > arg2; break;
2126 			case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2127 			case EXPR_SMALLER: cmp = arg1 < arg2; break;
2128 			case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2129 			default: cmp = 0; break;
2130 		    }
2131 		    --ectx.ec_stack.ga_len;
2132 		    if (iptr->isn_type == ISN_COMPAREFLOAT)
2133 		    {
2134 			tv1->v_type = VAR_BOOL;
2135 			tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2136 		    }
2137 		    else
2138 			tv1->vval.v_float = res;
2139 		}
2140 #endif
2141 		break;
2142 
2143 	    case ISN_COMPARELIST:
2144 		{
2145 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2146 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2147 		    list_T	*arg1 = tv1->vval.v_list;
2148 		    list_T	*arg2 = tv2->vval.v_list;
2149 		    int		cmp = FALSE;
2150 		    int		ic = iptr->isn_arg.op.op_ic;
2151 
2152 		    switch (iptr->isn_arg.op.op_type)
2153 		    {
2154 			case EXPR_EQUAL: cmp =
2155 				      list_equal(arg1, arg2, ic, FALSE); break;
2156 			case EXPR_NEQUAL: cmp =
2157 				     !list_equal(arg1, arg2, ic, FALSE); break;
2158 			case EXPR_IS: cmp = arg1 == arg2; break;
2159 			case EXPR_ISNOT: cmp = arg1 != arg2; break;
2160 			default: cmp = 0; break;
2161 		    }
2162 		    --ectx.ec_stack.ga_len;
2163 		    clear_tv(tv1);
2164 		    clear_tv(tv2);
2165 		    tv1->v_type = VAR_BOOL;
2166 		    tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2167 		}
2168 		break;
2169 
2170 	    case ISN_COMPAREBLOB:
2171 		{
2172 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2173 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2174 		    blob_T	*arg1 = tv1->vval.v_blob;
2175 		    blob_T	*arg2 = tv2->vval.v_blob;
2176 		    int		cmp = FALSE;
2177 
2178 		    switch (iptr->isn_arg.op.op_type)
2179 		    {
2180 			case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2181 			case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2182 			case EXPR_IS: cmp = arg1 == arg2; break;
2183 			case EXPR_ISNOT: cmp = arg1 != arg2; break;
2184 			default: cmp = 0; break;
2185 		    }
2186 		    --ectx.ec_stack.ga_len;
2187 		    clear_tv(tv1);
2188 		    clear_tv(tv2);
2189 		    tv1->v_type = VAR_BOOL;
2190 		    tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2191 		}
2192 		break;
2193 
2194 		// TODO: handle separately
2195 	    case ISN_COMPARESTRING:
2196 	    case ISN_COMPAREDICT:
2197 	    case ISN_COMPAREFUNC:
2198 	    case ISN_COMPAREANY:
2199 		{
2200 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2201 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2202 		    exptype_T	exptype = iptr->isn_arg.op.op_type;
2203 		    int		ic = iptr->isn_arg.op.op_ic;
2204 
2205 		    SOURCING_LNUM = iptr->isn_lnum;
2206 		    typval_compare(tv1, tv2, exptype, ic);
2207 		    clear_tv(tv2);
2208 		    --ectx.ec_stack.ga_len;
2209 		}
2210 		break;
2211 
2212 	    case ISN_ADDLIST:
2213 	    case ISN_ADDBLOB:
2214 		{
2215 		    typval_T *tv1 = STACK_TV_BOT(-2);
2216 		    typval_T *tv2 = STACK_TV_BOT(-1);
2217 
2218 		    if (iptr->isn_type == ISN_ADDLIST)
2219 			eval_addlist(tv1, tv2);
2220 		    else
2221 			eval_addblob(tv1, tv2);
2222 		    clear_tv(tv2);
2223 		    --ectx.ec_stack.ga_len;
2224 		}
2225 		break;
2226 
2227 	    // Computation with two arguments of unknown type
2228 	    case ISN_OPANY:
2229 		{
2230 		    typval_T	*tv1 = STACK_TV_BOT(-2);
2231 		    typval_T	*tv2 = STACK_TV_BOT(-1);
2232 		    varnumber_T	n1, n2;
2233 #ifdef FEAT_FLOAT
2234 		    float_T	f1 = 0, f2 = 0;
2235 #endif
2236 		    int		error = FALSE;
2237 
2238 		    if (iptr->isn_arg.op.op_type == EXPR_ADD)
2239 		    {
2240 			if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2241 			{
2242 			    eval_addlist(tv1, tv2);
2243 			    clear_tv(tv2);
2244 			    --ectx.ec_stack.ga_len;
2245 			    break;
2246 			}
2247 			else if (tv1->v_type == VAR_BLOB
2248 						    && tv2->v_type == VAR_BLOB)
2249 			{
2250 			    eval_addblob(tv1, tv2);
2251 			    clear_tv(tv2);
2252 			    --ectx.ec_stack.ga_len;
2253 			    break;
2254 			}
2255 		    }
2256 #ifdef FEAT_FLOAT
2257 		    if (tv1->v_type == VAR_FLOAT)
2258 		    {
2259 			f1 = tv1->vval.v_float;
2260 			n1 = 0;
2261 		    }
2262 		    else
2263 #endif
2264 		    {
2265 			n1 = tv_get_number_chk(tv1, &error);
2266 			if (error)
2267 			    goto on_error;
2268 #ifdef FEAT_FLOAT
2269 			if (tv2->v_type == VAR_FLOAT)
2270 			    f1 = n1;
2271 #endif
2272 		    }
2273 #ifdef FEAT_FLOAT
2274 		    if (tv2->v_type == VAR_FLOAT)
2275 		    {
2276 			f2 = tv2->vval.v_float;
2277 			n2 = 0;
2278 		    }
2279 		    else
2280 #endif
2281 		    {
2282 			n2 = tv_get_number_chk(tv2, &error);
2283 			if (error)
2284 			    goto on_error;
2285 #ifdef FEAT_FLOAT
2286 			if (tv1->v_type == VAR_FLOAT)
2287 			    f2 = n2;
2288 #endif
2289 		    }
2290 #ifdef FEAT_FLOAT
2291 		    // if there is a float on either side the result is a float
2292 		    if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2293 		    {
2294 			switch (iptr->isn_arg.op.op_type)
2295 			{
2296 			    case EXPR_MULT: f1 = f1 * f2; break;
2297 			    case EXPR_DIV:  f1 = f1 / f2; break;
2298 			    case EXPR_SUB:  f1 = f1 - f2; break;
2299 			    case EXPR_ADD:  f1 = f1 + f2; break;
2300 			    default: SOURCING_LNUM = iptr->isn_lnum;
2301 				     emsg(_(e_modulus));
2302 				     goto on_error;
2303 			}
2304 			clear_tv(tv1);
2305 			clear_tv(tv2);
2306 			tv1->v_type = VAR_FLOAT;
2307 			tv1->vval.v_float = f1;
2308 			--ectx.ec_stack.ga_len;
2309 		    }
2310 		    else
2311 #endif
2312 		    {
2313 			switch (iptr->isn_arg.op.op_type)
2314 			{
2315 			    case EXPR_MULT: n1 = n1 * n2; break;
2316 			    case EXPR_DIV:  n1 = num_divide(n1, n2); break;
2317 			    case EXPR_SUB:  n1 = n1 - n2; break;
2318 			    case EXPR_ADD:  n1 = n1 + n2; break;
2319 			    default:	    n1 = num_modulus(n1, n2); break;
2320 			}
2321 			clear_tv(tv1);
2322 			clear_tv(tv2);
2323 			tv1->v_type = VAR_NUMBER;
2324 			tv1->vval.v_number = n1;
2325 			--ectx.ec_stack.ga_len;
2326 		    }
2327 		}
2328 		break;
2329 
2330 	    case ISN_CONCAT:
2331 		{
2332 		    char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2333 		    char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2334 		    char_u *res;
2335 
2336 		    res = concat_str(str1, str2);
2337 		    clear_tv(STACK_TV_BOT(-2));
2338 		    clear_tv(STACK_TV_BOT(-1));
2339 		    --ectx.ec_stack.ga_len;
2340 		    STACK_TV_BOT(-1)->vval.v_string = res;
2341 		}
2342 		break;
2343 
2344 	    case ISN_STRINDEX:
2345 	    case ISN_STRSLICE:
2346 		{
2347 		    int		is_slice = iptr->isn_type == ISN_STRSLICE;
2348 		    varnumber_T	n1 = 0, n2;
2349 		    char_u	*res;
2350 
2351 		    // string index: string is at stack-2, index at stack-1
2352 		    // string slice: string is at stack-3, first index at
2353 		    // stack-2, second index at stack-1
2354 		    if (is_slice)
2355 		    {
2356 			tv = STACK_TV_BOT(-2);
2357 			n1 = tv->vval.v_number;
2358 		    }
2359 
2360 		    tv = STACK_TV_BOT(-1);
2361 		    n2 = tv->vval.v_number;
2362 
2363 		    ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2364 		    tv = STACK_TV_BOT(-1);
2365 		    if (is_slice)
2366 			// Slice: Select the characters from the string
2367 			res = string_slice(tv->vval.v_string, n1, n2);
2368 		    else
2369 			// Index: The resulting variable is a string of a
2370 			// single character.  If the index is too big or
2371 			// negative the result is empty.
2372 			res = char_from_string(tv->vval.v_string, n2);
2373 		    vim_free(tv->vval.v_string);
2374 		    tv->vval.v_string = res;
2375 		}
2376 		break;
2377 
2378 	    case ISN_LISTINDEX:
2379 	    case ISN_LISTSLICE:
2380 		{
2381 		    int		is_slice = iptr->isn_type == ISN_LISTSLICE;
2382 		    list_T	*list;
2383 		    varnumber_T	n1, n2;
2384 
2385 		    // list index: list is at stack-2, index at stack-1
2386 		    // list slice: list is at stack-3, indexes at stack-2 and
2387 		    // stack-1
2388 		    tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
2389 		    list = tv->vval.v_list;
2390 
2391 		    tv = STACK_TV_BOT(-1);
2392 		    n1 = n2 = tv->vval.v_number;
2393 		    clear_tv(tv);
2394 
2395 		    if (is_slice)
2396 		    {
2397 			tv = STACK_TV_BOT(-2);
2398 			n1 = tv->vval.v_number;
2399 			clear_tv(tv);
2400 		    }
2401 
2402 		    ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2403 		    tv = STACK_TV_BOT(-1);
2404 		    SOURCING_LNUM = iptr->isn_lnum;
2405 		    if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2406 								       == FAIL)
2407 			goto on_error;
2408 		}
2409 		break;
2410 
2411 	    case ISN_ANYINDEX:
2412 	    case ISN_ANYSLICE:
2413 		{
2414 		    int		is_slice = iptr->isn_type == ISN_ANYSLICE;
2415 		    typval_T	*var1, *var2;
2416 		    int		res;
2417 
2418 		    // index: composite is at stack-2, index at stack-1
2419 		    // slice: composite is at stack-3, indexes at stack-2 and
2420 		    // stack-1
2421 		    tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
2422 		    SOURCING_LNUM = iptr->isn_lnum;
2423 		    if (check_can_index(tv, TRUE, TRUE) == FAIL)
2424 			goto on_error;
2425 		    var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2426 		    var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2427 		    res = eval_index_inner(tv, is_slice,
2428 						   var1, var2, NULL, -1, TRUE);
2429 		    clear_tv(var1);
2430 		    if (is_slice)
2431 			clear_tv(var2);
2432 		    ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2433 		    if (res == FAIL)
2434 			goto on_error;
2435 		}
2436 		break;
2437 
2438 	    case ISN_SLICE:
2439 		{
2440 		    list_T	*list;
2441 		    int		count = iptr->isn_arg.number;
2442 
2443 		    // type will have been checked to be a list
2444 		    tv = STACK_TV_BOT(-1);
2445 		    list = tv->vval.v_list;
2446 
2447 		    // no error for short list, expect it to be checked earlier
2448 		    if (list != NULL && list->lv_len >= count)
2449 		    {
2450 			list_T	*newlist = list_slice(list,
2451 						      count, list->lv_len - 1);
2452 
2453 			if (newlist != NULL)
2454 			{
2455 			    list_unref(list);
2456 			    tv->vval.v_list = newlist;
2457 			    ++newlist->lv_refcount;
2458 			}
2459 		    }
2460 		}
2461 		break;
2462 
2463 	    case ISN_GETITEM:
2464 		{
2465 		    listitem_T	*li;
2466 		    int		index = iptr->isn_arg.number;
2467 
2468 		    // Get list item: list is at stack-1, push item.
2469 		    // List type and length is checked for when compiling.
2470 		    tv = STACK_TV_BOT(-1);
2471 		    li = list_find(tv->vval.v_list, index);
2472 
2473 		    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2474 			goto failed;
2475 		    ++ectx.ec_stack.ga_len;
2476 		    copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2477 		}
2478 		break;
2479 
2480 	    case ISN_MEMBER:
2481 		{
2482 		    dict_T	*dict;
2483 		    char_u	*key;
2484 		    dictitem_T	*di;
2485 		    typval_T	temp_tv;
2486 
2487 		    // dict member: dict is at stack-2, key at stack-1
2488 		    tv = STACK_TV_BOT(-2);
2489 		    // no need to check for VAR_DICT, CHECKTYPE will check.
2490 		    dict = tv->vval.v_dict;
2491 
2492 		    tv = STACK_TV_BOT(-1);
2493 		    // no need to check for VAR_STRING, 2STRING will check.
2494 		    key = tv->vval.v_string;
2495 
2496 		    if ((di = dict_find(dict, key, -1)) == NULL)
2497 		    {
2498 			SOURCING_LNUM = iptr->isn_lnum;
2499 			semsg(_(e_dictkey), key);
2500 			goto on_error;
2501 		    }
2502 		    clear_tv(tv);
2503 		    --ectx.ec_stack.ga_len;
2504 		    // Clear the dict after getting the item, to avoid that it
2505 		    // make the item invalid.
2506 		    tv = STACK_TV_BOT(-1);
2507 		    temp_tv = *tv;
2508 		    copy_tv(&di->di_tv, tv);
2509 		    clear_tv(&temp_tv);
2510 		}
2511 		break;
2512 
2513 	    // dict member with string key
2514 	    case ISN_STRINGMEMBER:
2515 		{
2516 		    dict_T	*dict;
2517 		    dictitem_T	*di;
2518 		    typval_T	temp_tv;
2519 
2520 		    tv = STACK_TV_BOT(-1);
2521 		    if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2522 		    {
2523 			SOURCING_LNUM = iptr->isn_lnum;
2524 			emsg(_(e_dictreq));
2525 			goto on_error;
2526 		    }
2527 		    dict = tv->vval.v_dict;
2528 
2529 		    if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2530 								       == NULL)
2531 		    {
2532 			SOURCING_LNUM = iptr->isn_lnum;
2533 			semsg(_(e_dictkey), iptr->isn_arg.string);
2534 			goto on_error;
2535 		    }
2536 		    // Clear the dict after getting the item, to avoid that it
2537 		    // make the item invalid.
2538 		    temp_tv = *tv;
2539 		    copy_tv(&di->di_tv, tv);
2540 		    clear_tv(&temp_tv);
2541 		}
2542 		break;
2543 
2544 	    case ISN_NEGATENR:
2545 		tv = STACK_TV_BOT(-1);
2546 		if (tv->v_type != VAR_NUMBER
2547 #ifdef FEAT_FLOAT
2548 			&& tv->v_type != VAR_FLOAT
2549 #endif
2550 			)
2551 		{
2552 		    SOURCING_LNUM = iptr->isn_lnum;
2553 		    emsg(_(e_number_exp));
2554 		    goto on_error;
2555 		}
2556 #ifdef FEAT_FLOAT
2557 		if (tv->v_type == VAR_FLOAT)
2558 		    tv->vval.v_float = -tv->vval.v_float;
2559 		else
2560 #endif
2561 		    tv->vval.v_number = -tv->vval.v_number;
2562 		break;
2563 
2564 	    case ISN_CHECKNR:
2565 		{
2566 		    int		error = FALSE;
2567 
2568 		    tv = STACK_TV_BOT(-1);
2569 		    SOURCING_LNUM = iptr->isn_lnum;
2570 		    if (check_not_string(tv) == FAIL)
2571 			goto on_error;
2572 		    (void)tv_get_number_chk(tv, &error);
2573 		    if (error)
2574 			goto on_error;
2575 		}
2576 		break;
2577 
2578 	    case ISN_CHECKTYPE:
2579 		{
2580 		    checktype_T *ct = &iptr->isn_arg.type;
2581 
2582 		    tv = STACK_TV_BOT(ct->ct_off);
2583 		    SOURCING_LNUM = iptr->isn_lnum;
2584 		    if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2585 			goto on_error;
2586 
2587 		    // number 0 is FALSE, number 1 is TRUE
2588 		    if (tv->v_type == VAR_NUMBER
2589 			    && ct->ct_type->tt_type == VAR_BOOL
2590 			    && (tv->vval.v_number == 0
2591 						|| tv->vval.v_number == 1))
2592 		    {
2593 			tv->v_type = VAR_BOOL;
2594 			tv->vval.v_number = tv->vval.v_number
2595 						      ? VVAL_TRUE : VVAL_FALSE;
2596 		    }
2597 		}
2598 		break;
2599 
2600 	    case ISN_CHECKLEN:
2601 		{
2602 		    int	    min_len = iptr->isn_arg.checklen.cl_min_len;
2603 		    list_T  *list = NULL;
2604 
2605 		    tv = STACK_TV_BOT(-1);
2606 		    if (tv->v_type == VAR_LIST)
2607 			    list = tv->vval.v_list;
2608 		    if (list == NULL || list->lv_len < min_len
2609 			    || (list->lv_len > min_len
2610 					&& !iptr->isn_arg.checklen.cl_more_OK))
2611 		    {
2612 			SOURCING_LNUM = iptr->isn_lnum;
2613 			semsg(_(e_expected_nr_items_but_got_nr),
2614 				     min_len, list == NULL ? 0 : list->lv_len);
2615 			goto on_error;
2616 		    }
2617 		}
2618 		break;
2619 
2620 	    case ISN_2BOOL:
2621 		{
2622 		    int n;
2623 
2624 		    tv = STACK_TV_BOT(-1);
2625 		    n = tv2bool(tv);
2626 		    if (iptr->isn_arg.number)  // invert
2627 			n = !n;
2628 		    clear_tv(tv);
2629 		    tv->v_type = VAR_BOOL;
2630 		    tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2631 		}
2632 		break;
2633 
2634 	    case ISN_2STRING:
2635 	    case ISN_2STRING_ANY:
2636 		{
2637 		    char_u *str;
2638 
2639 		    tv = STACK_TV_BOT(iptr->isn_arg.number);
2640 		    if (tv->v_type != VAR_STRING)
2641 		    {
2642 			if (iptr->isn_type == ISN_2STRING_ANY)
2643 			{
2644 			    switch (tv->v_type)
2645 			    {
2646 				case VAR_SPECIAL:
2647 				case VAR_BOOL:
2648 				case VAR_NUMBER:
2649 				case VAR_FLOAT:
2650 				case VAR_BLOB:	break;
2651 				default:	to_string_error(tv->v_type);
2652 						goto on_error;
2653 			    }
2654 			}
2655 			str = typval_tostring(tv);
2656 			clear_tv(tv);
2657 			tv->v_type = VAR_STRING;
2658 			tv->vval.v_string = str;
2659 		    }
2660 		}
2661 		break;
2662 
2663 	    case ISN_PUT:
2664 		{
2665 		    int		regname = iptr->isn_arg.put.put_regname;
2666 		    linenr_T	lnum = iptr->isn_arg.put.put_lnum;
2667 		    char_u	*expr = NULL;
2668 		    int		dir = FORWARD;
2669 
2670 		    if (regname == '=')
2671 		    {
2672 			tv = STACK_TV_BOT(-1);
2673 			if (tv->v_type == VAR_STRING)
2674 			    expr = tv->vval.v_string;
2675 			else
2676 			{
2677 			    expr = typval_tostring(tv);  // allocates value
2678 			    clear_tv(tv);
2679 			}
2680 			--ectx.ec_stack.ga_len;
2681 		    }
2682 		    if (lnum == -2)
2683 			// :put! above cursor
2684 			dir = BACKWARD;
2685 		    else if (lnum >= 0)
2686 			curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2687 		    check_cursor();
2688 		    do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2689 		    vim_free(expr);
2690 		}
2691 		break;
2692 
2693 	    case ISN_SHUFFLE:
2694 		{
2695 		    typval_T	    tmp_tv;
2696 		    int		    item = iptr->isn_arg.shuffle.shfl_item;
2697 		    int		    up = iptr->isn_arg.shuffle.shfl_up;
2698 
2699 		    tmp_tv = *STACK_TV_BOT(-item);
2700 		    for ( ; up > 0 && item > 1; --up)
2701 		    {
2702 			*STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2703 			--item;
2704 		    }
2705 		    *STACK_TV_BOT(-item) = tmp_tv;
2706 		}
2707 		break;
2708 
2709 	    case ISN_DROP:
2710 		--ectx.ec_stack.ga_len;
2711 		clear_tv(STACK_TV_BOT(0));
2712 		break;
2713 	}
2714 	continue;
2715 
2716 func_return:
2717 	// Restore previous function. If the frame pointer is where we started
2718 	// then there is none and we are done.
2719 	if (ectx.ec_frame_idx == initial_frame_idx)
2720 	    goto done;
2721 
2722 	if (func_return(&ectx) == FAIL)
2723 	    // only fails when out of memory
2724 	    goto failed;
2725 	continue;
2726 
2727 on_error:
2728 	if (trylevel == 0)
2729 	    goto failed;
2730     }
2731 
2732 done:
2733     // function finished, get result from the stack.
2734     tv = STACK_TV_BOT(-1);
2735     *rettv = *tv;
2736     tv->v_type = VAR_UNKNOWN;
2737     ret = OK;
2738 
2739 failed:
2740     // When failed need to unwind the call stack.
2741     while (ectx.ec_frame_idx != initial_frame_idx)
2742 	func_return(&ectx);
2743 
2744     // Deal with any remaining closures, they may be in use somewhere.
2745     if (ectx.ec_funcrefs.ga_len > 0)
2746 	handle_closure_in_use(&ectx, FALSE);
2747 
2748     estack_pop();
2749     current_sctx = save_current_sctx;
2750 
2751 failed_early:
2752     // Free all local variables, but not arguments.
2753     for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2754 	clear_tv(STACK_TV(idx));
2755 
2756     vim_free(ectx.ec_stack.ga_data);
2757     vim_free(ectx.ec_trystack.ga_data);
2758 
2759     // Not sure if this is necessary.
2760     suppress_errthrow = save_suppress_errthrow;
2761 
2762     if (ret != OK && called_emsg == called_emsg_before)
2763 	semsg(_(e_unknown_error_while_executing_str),
2764 						   printable_func_name(ufunc));
2765     return ret;
2766 }
2767 
2768 /*
2769  * ":dissassemble".
2770  * We don't really need this at runtime, but we do have tests that require it,
2771  * so always include this.
2772  */
2773     void
2774 ex_disassemble(exarg_T *eap)
2775 {
2776     char_u	*arg = eap->arg;
2777     char_u	*fname;
2778     ufunc_T	*ufunc;
2779     dfunc_T	*dfunc;
2780     isn_T	*instr;
2781     int		current;
2782     int		line_idx = 0;
2783     int		prev_current = 0;
2784     int		is_global = FALSE;
2785 
2786     if (STRNCMP(arg, "<lambda>", 8) == 0)
2787     {
2788 	arg += 8;
2789 	(void)getdigits(&arg);
2790 	fname = vim_strnsave(eap->arg, arg - eap->arg);
2791     }
2792     else
2793 	fname = trans_function_name(&arg, &is_global, FALSE,
2794 			    TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
2795     if (fname == NULL)
2796     {
2797 	semsg(_(e_invarg2), eap->arg);
2798 	return;
2799     }
2800 
2801     ufunc = find_func(fname, is_global, NULL);
2802     if (ufunc == NULL)
2803     {
2804 	char_u *p = untrans_function_name(fname);
2805 
2806 	if (p != NULL)
2807 	    // Try again without making it script-local.
2808 	    ufunc = find_func(p, FALSE, NULL);
2809     }
2810     vim_free(fname);
2811     if (ufunc == NULL)
2812     {
2813 	semsg(_(e_cannot_find_function_str), eap->arg);
2814 	return;
2815     }
2816     if (ufunc->uf_def_status == UF_TO_BE_COMPILED
2817 	    && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2818 	return;
2819     if (ufunc->uf_def_status != UF_COMPILED)
2820     {
2821 	semsg(_(e_function_is_not_compiled_str), eap->arg);
2822 	return;
2823     }
2824     if (ufunc->uf_name_exp != NULL)
2825 	msg((char *)ufunc->uf_name_exp);
2826     else
2827 	msg((char *)ufunc->uf_name);
2828 
2829     dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2830     instr = dfunc->df_instr;
2831     for (current = 0; current < dfunc->df_instr_count; ++current)
2832     {
2833 	isn_T	    *iptr = &instr[current];
2834 	char	    *line;
2835 
2836 	while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2837 	{
2838 	    if (current > prev_current)
2839 	    {
2840 		msg_puts("\n\n");
2841 		prev_current = current;
2842 	    }
2843 	    line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2844 	    if (line != NULL)
2845 		msg(line);
2846 	}
2847 
2848 	switch (iptr->isn_type)
2849 	{
2850 	    case ISN_EXEC:
2851 		smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2852 		break;
2853 	    case ISN_EXECCONCAT:
2854 		smsg("%4d EXECCONCAT %lld", current,
2855 					      (long long)iptr->isn_arg.number);
2856 		break;
2857 	    case ISN_ECHO:
2858 		{
2859 		    echo_T *echo = &iptr->isn_arg.echo;
2860 
2861 		    smsg("%4d %s %d", current,
2862 			    echo->echo_with_white ? "ECHO" : "ECHON",
2863 			    echo->echo_count);
2864 		}
2865 		break;
2866 	    case ISN_EXECUTE:
2867 		smsg("%4d EXECUTE %lld", current,
2868 					    (long long)(iptr->isn_arg.number));
2869 		break;
2870 	    case ISN_ECHOMSG:
2871 		smsg("%4d ECHOMSG %lld", current,
2872 					    (long long)(iptr->isn_arg.number));
2873 		break;
2874 	    case ISN_ECHOERR:
2875 		smsg("%4d ECHOERR %lld", current,
2876 					    (long long)(iptr->isn_arg.number));
2877 		break;
2878 	    case ISN_LOAD:
2879 	    case ISN_LOADOUTER:
2880 		{
2881 		    char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2882 
2883 		    if (iptr->isn_arg.number < 0)
2884 			smsg("%4d LOAD%s arg[%lld]", current, add,
2885 				(long long)(iptr->isn_arg.number
2886 							  + STACK_FRAME_SIZE));
2887 		    else
2888 			smsg("%4d LOAD%s $%lld", current, add,
2889 					    (long long)(iptr->isn_arg.number));
2890 		}
2891 		break;
2892 	    case ISN_LOADV:
2893 		smsg("%4d LOADV v:%s", current,
2894 				       get_vim_var_name(iptr->isn_arg.number));
2895 		break;
2896 	    case ISN_LOADSCRIPT:
2897 		{
2898 		    scriptitem_T *si =
2899 				  SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
2900 		    svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2901 					     + iptr->isn_arg.script.script_idx;
2902 
2903 		    smsg("%4d LOADSCRIPT %s from %s", current,
2904 						     sv->sv_name, si->sn_name);
2905 		}
2906 		break;
2907 	    case ISN_LOADS:
2908 		{
2909 		    scriptitem_T *si = SCRIPT_ITEM(
2910 					       iptr->isn_arg.loadstore.ls_sid);
2911 
2912 		    smsg("%4d LOADS s:%s from %s", current,
2913 				 iptr->isn_arg.loadstore.ls_name, si->sn_name);
2914 		}
2915 		break;
2916 	    case ISN_LOADG:
2917 		smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2918 		break;
2919 	    case ISN_LOADB:
2920 		smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2921 		break;
2922 	    case ISN_LOADW:
2923 		smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2924 		break;
2925 	    case ISN_LOADT:
2926 		smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2927 		break;
2928 	    case ISN_LOADGDICT:
2929 		smsg("%4d LOAD g:", current);
2930 		break;
2931 	    case ISN_LOADBDICT:
2932 		smsg("%4d LOAD b:", current);
2933 		break;
2934 	    case ISN_LOADWDICT:
2935 		smsg("%4d LOAD w:", current);
2936 		break;
2937 	    case ISN_LOADTDICT:
2938 		smsg("%4d LOAD t:", current);
2939 		break;
2940 	    case ISN_LOADOPT:
2941 		smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2942 		break;
2943 	    case ISN_LOADENV:
2944 		smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2945 		break;
2946 	    case ISN_LOADREG:
2947 		smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
2948 		break;
2949 
2950 	    case ISN_STORE:
2951 	    case ISN_STOREOUTER:
2952 		{
2953 		    char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2954 
2955 		if (iptr->isn_arg.number < 0)
2956 		    smsg("%4d STORE%s arg[%lld]", current, add,
2957 			 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
2958 		else
2959 		    smsg("%4d STORE%s $%lld", current, add,
2960 					    (long long)(iptr->isn_arg.number));
2961 		}
2962 		break;
2963 	    case ISN_STOREV:
2964 		smsg("%4d STOREV v:%s", current,
2965 				       get_vim_var_name(iptr->isn_arg.number));
2966 		break;
2967 	    case ISN_STOREG:
2968 		smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2969 		break;
2970 	    case ISN_STOREB:
2971 		smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2972 		break;
2973 	    case ISN_STOREW:
2974 		smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2975 		break;
2976 	    case ISN_STORET:
2977 		smsg("%4d STORET %s", current, iptr->isn_arg.string);
2978 		break;
2979 	    case ISN_STORES:
2980 		{
2981 		    scriptitem_T *si = SCRIPT_ITEM(
2982 					       iptr->isn_arg.loadstore.ls_sid);
2983 
2984 		    smsg("%4d STORES %s in %s", current,
2985 				 iptr->isn_arg.loadstore.ls_name, si->sn_name);
2986 		}
2987 		break;
2988 	    case ISN_STORESCRIPT:
2989 		{
2990 		    scriptitem_T *si =
2991 				  SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
2992 		    svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2993 					     + iptr->isn_arg.script.script_idx;
2994 
2995 		    smsg("%4d STORESCRIPT %s in %s", current,
2996 						     sv->sv_name, si->sn_name);
2997 		}
2998 		break;
2999 	    case ISN_STOREOPT:
3000 		smsg("%4d STOREOPT &%s", current,
3001 					       iptr->isn_arg.storeopt.so_name);
3002 		break;
3003 	    case ISN_STOREENV:
3004 		smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3005 		break;
3006 	    case ISN_STOREREG:
3007 		smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
3008 		break;
3009 	    case ISN_STORENR:
3010 		smsg("%4d STORE %lld in $%d", current,
3011 				iptr->isn_arg.storenr.stnr_val,
3012 				iptr->isn_arg.storenr.stnr_idx);
3013 		break;
3014 
3015 	    case ISN_STORELIST:
3016 		smsg("%4d STORELIST", current);
3017 		break;
3018 
3019 	    case ISN_STOREDICT:
3020 		smsg("%4d STOREDICT", current);
3021 		break;
3022 
3023 	    // constants
3024 	    case ISN_PUSHNR:
3025 		smsg("%4d PUSHNR %lld", current,
3026 					    (long long)(iptr->isn_arg.number));
3027 		break;
3028 	    case ISN_PUSHBOOL:
3029 	    case ISN_PUSHSPEC:
3030 		smsg("%4d PUSH %s", current,
3031 				   get_var_special_name(iptr->isn_arg.number));
3032 		break;
3033 	    case ISN_PUSHF:
3034 #ifdef FEAT_FLOAT
3035 		smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
3036 #endif
3037 		break;
3038 	    case ISN_PUSHS:
3039 		smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3040 		break;
3041 	    case ISN_PUSHBLOB:
3042 		{
3043 		    char_u	*r;
3044 		    char_u	numbuf[NUMBUFLEN];
3045 		    char_u	*tofree;
3046 
3047 		    r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
3048 		    smsg("%4d PUSHBLOB %s", current, r);
3049 		    vim_free(tofree);
3050 		}
3051 		break;
3052 	    case ISN_PUSHFUNC:
3053 		{
3054 		    char *name = (char *)iptr->isn_arg.string;
3055 
3056 		    smsg("%4d PUSHFUNC \"%s\"", current,
3057 					       name == NULL ? "[none]" : name);
3058 		}
3059 		break;
3060 	    case ISN_PUSHCHANNEL:
3061 #ifdef FEAT_JOB_CHANNEL
3062 		{
3063 		    channel_T *channel = iptr->isn_arg.channel;
3064 
3065 		    smsg("%4d PUSHCHANNEL %d", current,
3066 					 channel == NULL ? 0 : channel->ch_id);
3067 		}
3068 #endif
3069 		break;
3070 	    case ISN_PUSHJOB:
3071 #ifdef FEAT_JOB_CHANNEL
3072 		{
3073 		    typval_T	tv;
3074 		    char_u	*name;
3075 
3076 		    tv.v_type = VAR_JOB;
3077 		    tv.vval.v_job = iptr->isn_arg.job;
3078 		    name = tv_get_string(&tv);
3079 		    smsg("%4d PUSHJOB \"%s\"", current, name);
3080 		}
3081 #endif
3082 		break;
3083 	    case ISN_PUSHEXC:
3084 		smsg("%4d PUSH v:exception", current);
3085 		break;
3086 	    case ISN_UNLET:
3087 		smsg("%4d UNLET%s %s", current,
3088 			iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3089 			iptr->isn_arg.unlet.ul_name);
3090 		break;
3091 	    case ISN_UNLETENV:
3092 		smsg("%4d UNLETENV%s $%s", current,
3093 			iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3094 			iptr->isn_arg.unlet.ul_name);
3095 		break;
3096 	    case ISN_LOCKCONST:
3097 		smsg("%4d LOCKCONST", current);
3098 		break;
3099 	    case ISN_NEWLIST:
3100 		smsg("%4d NEWLIST size %lld", current,
3101 					    (long long)(iptr->isn_arg.number));
3102 		break;
3103 	    case ISN_NEWDICT:
3104 		smsg("%4d NEWDICT size %lld", current,
3105 					    (long long)(iptr->isn_arg.number));
3106 		break;
3107 
3108 	    // function call
3109 	    case ISN_BCALL:
3110 		{
3111 		    cbfunc_T	*cbfunc = &iptr->isn_arg.bfunc;
3112 
3113 		    smsg("%4d BCALL %s(argc %d)", current,
3114 			    internal_func_name(cbfunc->cbf_idx),
3115 			    cbfunc->cbf_argcount);
3116 		}
3117 		break;
3118 	    case ISN_DCALL:
3119 		{
3120 		    cdfunc_T	*cdfunc = &iptr->isn_arg.dfunc;
3121 		    dfunc_T	*df = ((dfunc_T *)def_functions.ga_data)
3122 							     + cdfunc->cdf_idx;
3123 
3124 		    smsg("%4d DCALL %s(argc %d)", current,
3125 			    df->df_ufunc->uf_name_exp != NULL
3126 				? df->df_ufunc->uf_name_exp
3127 				: df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3128 		}
3129 		break;
3130 	    case ISN_UCALL:
3131 		{
3132 		    cufunc_T	*cufunc = &iptr->isn_arg.ufunc;
3133 
3134 		    smsg("%4d UCALL %s(argc %d)", current,
3135 				       cufunc->cuf_name, cufunc->cuf_argcount);
3136 		}
3137 		break;
3138 	    case ISN_PCALL:
3139 		{
3140 		    cpfunc_T	*cpfunc = &iptr->isn_arg.pfunc;
3141 
3142 		    smsg("%4d PCALL%s (argc %d)", current,
3143 			   cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3144 		}
3145 		break;
3146 	    case ISN_PCALL_END:
3147 		smsg("%4d PCALL end", current);
3148 		break;
3149 	    case ISN_RETURN:
3150 		smsg("%4d RETURN", current);
3151 		break;
3152 	    case ISN_FUNCREF:
3153 		{
3154 		    funcref_T	*funcref = &iptr->isn_arg.funcref;
3155 		    dfunc_T	*df = ((dfunc_T *)def_functions.ga_data)
3156 							    + funcref->fr_func;
3157 
3158 		    smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
3159 		}
3160 		break;
3161 
3162 	    case ISN_NEWFUNC:
3163 		{
3164 		    newfunc_T	*newfunc = &iptr->isn_arg.newfunc;
3165 
3166 		    smsg("%4d NEWFUNC %s %s", current,
3167 				       newfunc->nf_lambda, newfunc->nf_global);
3168 		}
3169 		break;
3170 
3171 	    case ISN_JUMP:
3172 		{
3173 		    char *when = "?";
3174 
3175 		    switch (iptr->isn_arg.jump.jump_when)
3176 		    {
3177 			case JUMP_ALWAYS:
3178 			    when = "JUMP";
3179 			    break;
3180 			case JUMP_AND_KEEP_IF_TRUE:
3181 			    when = "JUMP_AND_KEEP_IF_TRUE";
3182 			    break;
3183 			case JUMP_IF_FALSE:
3184 			    when = "JUMP_IF_FALSE";
3185 			    break;
3186 			case JUMP_AND_KEEP_IF_FALSE:
3187 			    when = "JUMP_AND_KEEP_IF_FALSE";
3188 			    break;
3189 		    }
3190 		    smsg("%4d %s -> %d", current, when,
3191 						iptr->isn_arg.jump.jump_where);
3192 		}
3193 		break;
3194 
3195 	    case ISN_FOR:
3196 		{
3197 		    forloop_T *forloop = &iptr->isn_arg.forloop;
3198 
3199 		    smsg("%4d FOR $%d -> %d", current,
3200 					   forloop->for_idx, forloop->for_end);
3201 		}
3202 		break;
3203 
3204 	    case ISN_TRY:
3205 		{
3206 		    try_T *try = &iptr->isn_arg.try;
3207 
3208 		    smsg("%4d TRY catch -> %d, finally -> %d", current,
3209 					     try->try_catch, try->try_finally);
3210 		}
3211 		break;
3212 	    case ISN_CATCH:
3213 		// TODO
3214 		smsg("%4d CATCH", current);
3215 		break;
3216 	    case ISN_ENDTRY:
3217 		smsg("%4d ENDTRY", current);
3218 		break;
3219 	    case ISN_THROW:
3220 		smsg("%4d THROW", current);
3221 		break;
3222 
3223 	    // expression operations on number
3224 	    case ISN_OPNR:
3225 	    case ISN_OPFLOAT:
3226 	    case ISN_OPANY:
3227 		{
3228 		    char *what;
3229 		    char *ins;
3230 
3231 		    switch (iptr->isn_arg.op.op_type)
3232 		    {
3233 			case EXPR_MULT: what = "*"; break;
3234 			case EXPR_DIV: what = "/"; break;
3235 			case EXPR_REM: what = "%"; break;
3236 			case EXPR_SUB: what = "-"; break;
3237 			case EXPR_ADD: what = "+"; break;
3238 			default:       what = "???"; break;
3239 		    }
3240 		    switch (iptr->isn_type)
3241 		    {
3242 			case ISN_OPNR: ins = "OPNR"; break;
3243 			case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3244 			case ISN_OPANY: ins = "OPANY"; break;
3245 			default: ins = "???"; break;
3246 		    }
3247 		    smsg("%4d %s %s", current, ins, what);
3248 		}
3249 		break;
3250 
3251 	    case ISN_COMPAREBOOL:
3252 	    case ISN_COMPARESPECIAL:
3253 	    case ISN_COMPARENR:
3254 	    case ISN_COMPAREFLOAT:
3255 	    case ISN_COMPARESTRING:
3256 	    case ISN_COMPAREBLOB:
3257 	    case ISN_COMPARELIST:
3258 	    case ISN_COMPAREDICT:
3259 	    case ISN_COMPAREFUNC:
3260 	    case ISN_COMPAREANY:
3261 		   {
3262 		       char *p;
3263 		       char buf[10];
3264 		       char *type;
3265 
3266 		       switch (iptr->isn_arg.op.op_type)
3267 		       {
3268 			   case EXPR_EQUAL:	 p = "=="; break;
3269 			   case EXPR_NEQUAL:    p = "!="; break;
3270 			   case EXPR_GREATER:   p = ">"; break;
3271 			   case EXPR_GEQUAL:    p = ">="; break;
3272 			   case EXPR_SMALLER:   p = "<"; break;
3273 			   case EXPR_SEQUAL:    p = "<="; break;
3274 			   case EXPR_MATCH:	 p = "=~"; break;
3275 			   case EXPR_IS:	 p = "is"; break;
3276 			   case EXPR_ISNOT:	 p = "isnot"; break;
3277 			   case EXPR_NOMATCH:	 p = "!~"; break;
3278 			   default:  p = "???"; break;
3279 		       }
3280 		       STRCPY(buf, p);
3281 		       if (iptr->isn_arg.op.op_ic == TRUE)
3282 			   strcat(buf, "?");
3283 		       switch(iptr->isn_type)
3284 		       {
3285 			   case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3286 			   case ISN_COMPARESPECIAL:
3287 						 type = "COMPARESPECIAL"; break;
3288 			   case ISN_COMPARENR: type = "COMPARENR"; break;
3289 			   case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3290 			   case ISN_COMPARESTRING:
3291 						  type = "COMPARESTRING"; break;
3292 			   case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3293 			   case ISN_COMPARELIST: type = "COMPARELIST"; break;
3294 			   case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3295 			   case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
3296 			   case ISN_COMPAREANY: type = "COMPAREANY"; break;
3297 			   default: type = "???"; break;
3298 		       }
3299 
3300 		       smsg("%4d %s %s", current, type, buf);
3301 		   }
3302 		   break;
3303 
3304 	    case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3305 	    case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3306 
3307 	    // expression operations
3308 	    case ISN_CONCAT: smsg("%4d CONCAT", current); break;
3309 	    case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
3310 	    case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
3311 	    case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
3312 	    case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
3313 	    case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3314 	    case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
3315 	    case ISN_SLICE: smsg("%4d SLICE %lld",
3316 					 current, iptr->isn_arg.number); break;
3317 	    case ISN_GETITEM: smsg("%4d ITEM %lld",
3318 					 current, iptr->isn_arg.number); break;
3319 	    case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3320 	    case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
3321 						  iptr->isn_arg.string); break;
3322 	    case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3323 
3324 	    case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3325 	    case ISN_CHECKTYPE:
3326 		  {
3327 		      char *tofree;
3328 
3329 		      smsg("%4d CHECKTYPE %s stack[%d]", current,
3330 			      type_name(iptr->isn_arg.type.ct_type, &tofree),
3331 			      iptr->isn_arg.type.ct_off);
3332 		      vim_free(tofree);
3333 		      break;
3334 		  }
3335 	    case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3336 				iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3337 				iptr->isn_arg.checklen.cl_min_len);
3338 			       break;
3339 	    case ISN_2BOOL: if (iptr->isn_arg.number)
3340 				smsg("%4d INVERT (!val)", current);
3341 			    else
3342 				smsg("%4d 2BOOL (!!val)", current);
3343 			    break;
3344 	    case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3345 					 (long long)(iptr->isn_arg.number));
3346 			      break;
3347 	    case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3348 					 (long long)(iptr->isn_arg.number));
3349 			      break;
3350 	    case ISN_PUT:
3351 		smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3352 					     (long)iptr->isn_arg.put.put_lnum);
3353 		break;
3354 
3355 	    case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3356 					 iptr->isn_arg.shuffle.shfl_item,
3357 					 iptr->isn_arg.shuffle.shfl_up);
3358 			      break;
3359 	    case ISN_DROP: smsg("%4d DROP", current); break;
3360 	}
3361 
3362 	out_flush();	    // output one line at a time
3363 	ui_breakcheck();
3364 	if (got_int)
3365 	    break;
3366     }
3367 }
3368 
3369 /*
3370  * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3371  * list, etc.  Mostly like what JavaScript does, except that empty list and
3372  * empty dictionary are FALSE.
3373  */
3374     int
3375 tv2bool(typval_T *tv)
3376 {
3377     switch (tv->v_type)
3378     {
3379 	case VAR_NUMBER:
3380 	    return tv->vval.v_number != 0;
3381 	case VAR_FLOAT:
3382 #ifdef FEAT_FLOAT
3383 	    return tv->vval.v_float != 0.0;
3384 #else
3385 	    break;
3386 #endif
3387 	case VAR_PARTIAL:
3388 	    return tv->vval.v_partial != NULL;
3389 	case VAR_FUNC:
3390 	case VAR_STRING:
3391 	    return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3392 	case VAR_LIST:
3393 	    return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3394 	case VAR_DICT:
3395 	    return tv->vval.v_dict != NULL
3396 				    && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3397 	case VAR_BOOL:
3398 	case VAR_SPECIAL:
3399 	    return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3400 	case VAR_JOB:
3401 #ifdef FEAT_JOB_CHANNEL
3402 	    return tv->vval.v_job != NULL;
3403 #else
3404 	    break;
3405 #endif
3406 	case VAR_CHANNEL:
3407 #ifdef FEAT_JOB_CHANNEL
3408 	    return tv->vval.v_channel != NULL;
3409 #else
3410 	    break;
3411 #endif
3412 	case VAR_BLOB:
3413 	    return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3414 	case VAR_UNKNOWN:
3415 	case VAR_ANY:
3416 	case VAR_VOID:
3417 	    break;
3418     }
3419     return FALSE;
3420 }
3421 
3422 /*
3423  * If "tv" is a string give an error and return FAIL.
3424  */
3425     int
3426 check_not_string(typval_T *tv)
3427 {
3428     if (tv->v_type == VAR_STRING)
3429     {
3430 	emsg(_(e_using_string_as_number));
3431 	clear_tv(tv);
3432 	return FAIL;
3433     }
3434     return OK;
3435 }
3436 
3437 
3438 #endif // FEAT_EVAL
3439