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