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