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;
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 // Evaluate an expression with legacy syntax, push it onto the
1778 // stack.
1779 case ISN_LEGACY_EVAL:
1780 {
1781 char_u *arg = iptr->isn_arg.string;
1782 int res;
1783 int save_flags = cmdmod.cmod_flags;
1784
1785 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1786 goto theend;
1787 tv = STACK_TV_BOT(0);
1788 init_tv(tv);
1789 cmdmod.cmod_flags |= CMOD_LEGACY;
1790 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1791 cmdmod.cmod_flags = save_flags;
1792 if (res == FAIL)
1793 goto on_error;
1794 ++ectx->ec_stack.ga_len;
1795 }
1796 break;
1797
1798 // push typeval VAR_INSTR with instructions to be executed
1799 case ISN_INSTR:
1800 {
1801 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1802 goto theend;
1803 tv = STACK_TV_BOT(0);
1804 tv->vval.v_instr = ALLOC_ONE(instr_T);
1805 if (tv->vval.v_instr == NULL)
1806 goto on_error;
1807 ++ectx->ec_stack.ga_len;
1808
1809 tv->v_type = VAR_INSTR;
1810 tv->vval.v_instr->instr_ectx = ectx;
1811 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1812 }
1813 break;
1814
1815 // execute :substitute with an expression
1816 case ISN_SUBSTITUTE:
1817 {
1818 subs_T *subs = &iptr->isn_arg.subs;
1819 source_cookie_T cookie;
1820 struct subs_expr_S *save_instr = substitute_instr;
1821 struct subs_expr_S subs_instr;
1822 int res;
1823
1824 subs_instr.subs_ectx = ectx;
1825 subs_instr.subs_instr = subs->subs_instr;
1826 subs_instr.subs_status = OK;
1827 substitute_instr = &subs_instr;
1828
1829 SOURCING_LNUM = iptr->isn_lnum;
1830 // This is very much like ISN_EXEC
1831 CLEAR_FIELD(cookie);
1832 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1833 res = do_cmdline(subs->subs_cmd,
1834 getsourceline, &cookie,
1835 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1836 substitute_instr = save_instr;
1837
1838 if (res == FAIL || did_emsg
1839 || subs_instr.subs_status == FAIL)
1840 goto on_error;
1841 }
1842 break;
1843
1844 case ISN_FINISH:
1845 goto done;
1846
1847 case ISN_REDIRSTART:
1848 // create a dummy entry for var_redir_str()
1849 if (alloc_redir_lval() == FAIL)
1850 goto on_error;
1851
1852 // The output is stored in growarray "redir_ga" until
1853 // redirection ends.
1854 init_redir_ga();
1855 redir_vname = 1;
1856 break;
1857
1858 case ISN_REDIREND:
1859 {
1860 char_u *res = get_clear_redir_ga();
1861
1862 // End redirection, put redirected text on the stack.
1863 clear_redir_lval();
1864 redir_vname = 0;
1865
1866 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1867 {
1868 vim_free(res);
1869 goto theend;
1870 }
1871 tv = STACK_TV_BOT(0);
1872 tv->v_type = VAR_STRING;
1873 tv->vval.v_string = res;
1874 ++ectx->ec_stack.ga_len;
1875 }
1876 break;
1877
1878 case ISN_CEXPR_AUCMD:
1879 #ifdef FEAT_QUICKFIX
1880 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1881 goto on_error;
1882 #endif
1883 break;
1884
1885 case ISN_CEXPR_CORE:
1886 #ifdef FEAT_QUICKFIX
1887 {
1888 exarg_T ea;
1889 int res;
1890
1891 CLEAR_FIELD(ea);
1892 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1893 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1894 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1895 --ectx->ec_stack.ga_len;
1896 tv = STACK_TV_BOT(0);
1897 res = cexpr_core(&ea, tv);
1898 clear_tv(tv);
1899 if (res == FAIL)
1900 goto on_error;
1901 }
1902 #endif
1903 break;
1904
1905 // execute Ex command from pieces on the stack
1906 case ISN_EXECCONCAT:
1907 {
1908 int count = iptr->isn_arg.number;
1909 size_t len = 0;
1910 int pass;
1911 int i;
1912 char_u *cmd = NULL;
1913 char_u *str;
1914
1915 for (pass = 1; pass <= 2; ++pass)
1916 {
1917 for (i = 0; i < count; ++i)
1918 {
1919 tv = STACK_TV_BOT(i - count);
1920 str = tv->vval.v_string;
1921 if (str != NULL && *str != NUL)
1922 {
1923 if (pass == 2)
1924 STRCPY(cmd + len, str);
1925 len += STRLEN(str);
1926 }
1927 if (pass == 2)
1928 clear_tv(tv);
1929 }
1930 if (pass == 1)
1931 {
1932 cmd = alloc(len + 1);
1933 if (unlikely(cmd == NULL))
1934 goto theend;
1935 len = 0;
1936 }
1937 }
1938
1939 SOURCING_LNUM = iptr->isn_lnum;
1940 do_cmdline_cmd(cmd);
1941 vim_free(cmd);
1942 }
1943 break;
1944
1945 // execute :echo {string} ...
1946 case ISN_ECHO:
1947 {
1948 int count = iptr->isn_arg.echo.echo_count;
1949 int atstart = TRUE;
1950 int needclr = TRUE;
1951 int idx;
1952
1953 for (idx = 0; idx < count; ++idx)
1954 {
1955 tv = STACK_TV_BOT(idx - count);
1956 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1957 &atstart, &needclr);
1958 clear_tv(tv);
1959 }
1960 if (needclr)
1961 msg_clr_eos();
1962 ectx->ec_stack.ga_len -= count;
1963 }
1964 break;
1965
1966 // :execute {string} ...
1967 // :echomsg {string} ...
1968 // :echoconsole {string} ...
1969 // :echoerr {string} ...
1970 case ISN_EXECUTE:
1971 case ISN_ECHOMSG:
1972 case ISN_ECHOCONSOLE:
1973 case ISN_ECHOERR:
1974 {
1975 int count = iptr->isn_arg.number;
1976 garray_T ga;
1977 char_u buf[NUMBUFLEN];
1978 char_u *p;
1979 int len;
1980 int failed = FALSE;
1981 int idx;
1982
1983 ga_init2(&ga, 1, 80);
1984 for (idx = 0; idx < count; ++idx)
1985 {
1986 tv = STACK_TV_BOT(idx - count);
1987 if (iptr->isn_type == ISN_EXECUTE)
1988 {
1989 if (tv->v_type == VAR_CHANNEL
1990 || tv->v_type == VAR_JOB)
1991 {
1992 SOURCING_LNUM = iptr->isn_lnum;
1993 semsg(_(e_using_invalid_value_as_string_str),
1994 vartype_name(tv->v_type));
1995 break;
1996 }
1997 else
1998 p = tv_get_string_buf(tv, buf);
1999 }
2000 else
2001 p = tv_stringify(tv, buf);
2002
2003 len = (int)STRLEN(p);
2004 if (GA_GROW_FAILS(&ga, len + 2))
2005 failed = TRUE;
2006 else
2007 {
2008 if (ga.ga_len > 0)
2009 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2010 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2011 ga.ga_len += len;
2012 }
2013 clear_tv(tv);
2014 }
2015 ectx->ec_stack.ga_len -= count;
2016 if (failed)
2017 {
2018 ga_clear(&ga);
2019 goto on_error;
2020 }
2021
2022 if (ga.ga_data != NULL)
2023 {
2024 if (iptr->isn_type == ISN_EXECUTE)
2025 {
2026 SOURCING_LNUM = iptr->isn_lnum;
2027 do_cmdline_cmd((char_u *)ga.ga_data);
2028 if (did_emsg)
2029 {
2030 ga_clear(&ga);
2031 goto on_error;
2032 }
2033 }
2034 else
2035 {
2036 msg_sb_eol();
2037 if (iptr->isn_type == ISN_ECHOMSG)
2038 {
2039 msg_attr(ga.ga_data, echo_attr);
2040 out_flush();
2041 }
2042 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2043 {
2044 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2045 TRUE);
2046 ui_write((char_u *)"\r\n", 2, TRUE);
2047 }
2048 else
2049 {
2050 SOURCING_LNUM = iptr->isn_lnum;
2051 emsg(ga.ga_data);
2052 }
2053 }
2054 }
2055 ga_clear(&ga);
2056 }
2057 break;
2058
2059 // load local variable or argument
2060 case ISN_LOAD:
2061 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2062 goto theend;
2063 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
2064 ++ectx->ec_stack.ga_len;
2065 break;
2066
2067 // load v: variable
2068 case ISN_LOADV:
2069 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2070 goto theend;
2071 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
2072 ++ectx->ec_stack.ga_len;
2073 break;
2074
2075 // load s: variable in Vim9 script
2076 case ISN_LOADSCRIPT:
2077 {
2078 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2079 svar_T *sv;
2080
2081 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
2082 if (sv == NULL)
2083 goto theend;
2084 allocate_if_null(sv->sv_tv);
2085 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2086 goto theend;
2087 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
2088 ++ectx->ec_stack.ga_len;
2089 }
2090 break;
2091
2092 // load s: variable in old script
2093 case ISN_LOADS:
2094 {
2095 hashtab_T *ht = &SCRIPT_VARS(
2096 iptr->isn_arg.loadstore.ls_sid);
2097 char_u *name = iptr->isn_arg.loadstore.ls_name;
2098 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
2099
2100 if (di == NULL)
2101 {
2102 SOURCING_LNUM = iptr->isn_lnum;
2103 semsg(_(e_undefined_variable_str), name);
2104 goto on_error;
2105 }
2106 else
2107 {
2108 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2109 goto theend;
2110 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2111 ++ectx->ec_stack.ga_len;
2112 }
2113 }
2114 break;
2115
2116 // load g:/b:/w:/t: variable
2117 case ISN_LOADG:
2118 case ISN_LOADB:
2119 case ISN_LOADW:
2120 case ISN_LOADT:
2121 {
2122 dictitem_T *di = NULL;
2123 hashtab_T *ht = NULL;
2124 char namespace;
2125
2126 switch (iptr->isn_type)
2127 {
2128 case ISN_LOADG:
2129 ht = get_globvar_ht();
2130 namespace = 'g';
2131 break;
2132 case ISN_LOADB:
2133 ht = &curbuf->b_vars->dv_hashtab;
2134 namespace = 'b';
2135 break;
2136 case ISN_LOADW:
2137 ht = &curwin->w_vars->dv_hashtab;
2138 namespace = 'w';
2139 break;
2140 case ISN_LOADT:
2141 ht = &curtab->tp_vars->dv_hashtab;
2142 namespace = 't';
2143 break;
2144 default: // Cannot reach here
2145 goto theend;
2146 }
2147 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2148
2149 if (di == NULL)
2150 {
2151 SOURCING_LNUM = iptr->isn_lnum;
2152 semsg(_(e_undefined_variable_char_str),
2153 namespace, iptr->isn_arg.string);
2154 goto on_error;
2155 }
2156 else
2157 {
2158 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2159 goto theend;
2160 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2161 ++ectx->ec_stack.ga_len;
2162 }
2163 }
2164 break;
2165
2166 // load autoload variable
2167 case ISN_LOADAUTO:
2168 {
2169 char_u *name = iptr->isn_arg.string;
2170
2171 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2172 goto theend;
2173 SOURCING_LNUM = iptr->isn_lnum;
2174 if (eval_variable(name, (int)STRLEN(name),
2175 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
2176 goto on_error;
2177 ++ectx->ec_stack.ga_len;
2178 }
2179 break;
2180
2181 // load g:/b:/w:/t: namespace
2182 case ISN_LOADGDICT:
2183 case ISN_LOADBDICT:
2184 case ISN_LOADWDICT:
2185 case ISN_LOADTDICT:
2186 {
2187 dict_T *d = NULL;
2188
2189 switch (iptr->isn_type)
2190 {
2191 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2192 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2193 case ISN_LOADWDICT: d = curwin->w_vars; break;
2194 case ISN_LOADTDICT: d = curtab->tp_vars; break;
2195 default: // Cannot reach here
2196 goto theend;
2197 }
2198 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2199 goto theend;
2200 tv = STACK_TV_BOT(0);
2201 tv->v_type = VAR_DICT;
2202 tv->v_lock = 0;
2203 tv->vval.v_dict = d;
2204 ++d->dv_refcount;
2205 ++ectx->ec_stack.ga_len;
2206 }
2207 break;
2208
2209 // load &option
2210 case ISN_LOADOPT:
2211 {
2212 typval_T optval;
2213 char_u *name = iptr->isn_arg.string;
2214
2215 // This is not expected to fail, name is checked during
2216 // compilation: don't set SOURCING_LNUM.
2217 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2218 goto theend;
2219 if (eval_option(&name, &optval, TRUE) == FAIL)
2220 goto theend;
2221 *STACK_TV_BOT(0) = optval;
2222 ++ectx->ec_stack.ga_len;
2223 }
2224 break;
2225
2226 // load $ENV
2227 case ISN_LOADENV:
2228 {
2229 typval_T optval;
2230 char_u *name = iptr->isn_arg.string;
2231
2232 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2233 goto theend;
2234 // name is always valid, checked when compiling
2235 (void)eval_env_var(&name, &optval, TRUE);
2236 *STACK_TV_BOT(0) = optval;
2237 ++ectx->ec_stack.ga_len;
2238 }
2239 break;
2240
2241 // load @register
2242 case ISN_LOADREG:
2243 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2244 goto theend;
2245 tv = STACK_TV_BOT(0);
2246 tv->v_type = VAR_STRING;
2247 tv->v_lock = 0;
2248 // This may result in NULL, which should be equivalent to an
2249 // empty string.
2250 tv->vval.v_string = get_reg_contents(
2251 iptr->isn_arg.number, GREG_EXPR_SRC);
2252 ++ectx->ec_stack.ga_len;
2253 break;
2254
2255 // store local variable
2256 case ISN_STORE:
2257 --ectx->ec_stack.ga_len;
2258 tv = STACK_TV_VAR(iptr->isn_arg.number);
2259 clear_tv(tv);
2260 *tv = *STACK_TV_BOT(0);
2261 break;
2262
2263 // store s: variable in old script
2264 case ISN_STORES:
2265 {
2266 hashtab_T *ht = &SCRIPT_VARS(
2267 iptr->isn_arg.loadstore.ls_sid);
2268 char_u *name = iptr->isn_arg.loadstore.ls_name;
2269 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
2270
2271 --ectx->ec_stack.ga_len;
2272 if (di == NULL)
2273 store_var(name, STACK_TV_BOT(0));
2274 else
2275 {
2276 SOURCING_LNUM = iptr->isn_lnum;
2277 if (var_check_permission(di, name) == FAIL)
2278 {
2279 clear_tv(STACK_TV_BOT(0));
2280 goto on_error;
2281 }
2282 clear_tv(&di->di_tv);
2283 di->di_tv = *STACK_TV_BOT(0);
2284 }
2285 }
2286 break;
2287
2288 // store script-local variable in Vim9 script
2289 case ISN_STORESCRIPT:
2290 {
2291 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2292 svar_T *sv;
2293
2294 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
2295 if (sv == NULL)
2296 goto theend;
2297 --ectx->ec_stack.ga_len;
2298
2299 // "const" and "final" are checked at compile time, locking
2300 // the value needs to be checked here.
2301 SOURCING_LNUM = iptr->isn_lnum;
2302 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
2303 {
2304 clear_tv(STACK_TV_BOT(0));
2305 goto on_error;
2306 }
2307
2308 clear_tv(sv->sv_tv);
2309 *sv->sv_tv = *STACK_TV_BOT(0);
2310 }
2311 break;
2312
2313 // store option
2314 case ISN_STOREOPT:
2315 {
2316 long n = 0;
2317 char_u *s = NULL;
2318 char *msg;
2319
2320 --ectx->ec_stack.ga_len;
2321 tv = STACK_TV_BOT(0);
2322 if (tv->v_type == VAR_STRING)
2323 {
2324 s = tv->vval.v_string;
2325 if (s == NULL)
2326 s = (char_u *)"";
2327 }
2328 else
2329 // must be VAR_NUMBER, CHECKTYPE makes sure
2330 n = tv->vval.v_number;
2331 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2332 n, s, iptr->isn_arg.storeopt.so_flags);
2333 clear_tv(tv);
2334 if (msg != NULL)
2335 {
2336 SOURCING_LNUM = iptr->isn_lnum;
2337 emsg(_(msg));
2338 goto on_error;
2339 }
2340 }
2341 break;
2342
2343 // store $ENV
2344 case ISN_STOREENV:
2345 --ectx->ec_stack.ga_len;
2346 tv = STACK_TV_BOT(0);
2347 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2348 clear_tv(tv);
2349 break;
2350
2351 // store @r
2352 case ISN_STOREREG:
2353 {
2354 int reg = iptr->isn_arg.number;
2355
2356 --ectx->ec_stack.ga_len;
2357 tv = STACK_TV_BOT(0);
2358 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
2359 clear_tv(tv);
2360 }
2361 break;
2362
2363 // store v: variable
2364 case ISN_STOREV:
2365 --ectx->ec_stack.ga_len;
2366 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2367 == FAIL)
2368 // should not happen, type is checked when compiling
2369 goto on_error;
2370 break;
2371
2372 // store g:/b:/w:/t: variable
2373 case ISN_STOREG:
2374 case ISN_STOREB:
2375 case ISN_STOREW:
2376 case ISN_STORET:
2377 {
2378 dictitem_T *di;
2379 hashtab_T *ht;
2380 char_u *name = iptr->isn_arg.string + 2;
2381
2382 switch (iptr->isn_type)
2383 {
2384 case ISN_STOREG:
2385 ht = get_globvar_ht();
2386 break;
2387 case ISN_STOREB:
2388 ht = &curbuf->b_vars->dv_hashtab;
2389 break;
2390 case ISN_STOREW:
2391 ht = &curwin->w_vars->dv_hashtab;
2392 break;
2393 case ISN_STORET:
2394 ht = &curtab->tp_vars->dv_hashtab;
2395 break;
2396 default: // Cannot reach here
2397 goto theend;
2398 }
2399
2400 --ectx->ec_stack.ga_len;
2401 di = find_var_in_ht(ht, 0, name, TRUE);
2402 if (di == NULL)
2403 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
2404 else
2405 {
2406 SOURCING_LNUM = iptr->isn_lnum;
2407 if (var_check_permission(di, name) == FAIL)
2408 goto on_error;
2409 clear_tv(&di->di_tv);
2410 di->di_tv = *STACK_TV_BOT(0);
2411 }
2412 }
2413 break;
2414
2415 // store an autoload variable
2416 case ISN_STOREAUTO:
2417 SOURCING_LNUM = iptr->isn_lnum;
2418 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2419 clear_tv(STACK_TV_BOT(-1));
2420 --ectx->ec_stack.ga_len;
2421 break;
2422
2423 // store number in local variable
2424 case ISN_STORENR:
2425 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
2426 clear_tv(tv);
2427 tv->v_type = VAR_NUMBER;
2428 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
2429 break;
2430
2431 // store value in list or dict variable
2432 case ISN_STOREINDEX:
2433 {
2434 vartype_T dest_type = iptr->isn_arg.vartype;
2435 typval_T *tv_idx = STACK_TV_BOT(-2);
2436 typval_T *tv_dest = STACK_TV_BOT(-1);
2437 int status = OK;
2438
2439 // Stack contains:
2440 // -3 value to be stored
2441 // -2 index
2442 // -1 dict or list
2443 tv = STACK_TV_BOT(-3);
2444 SOURCING_LNUM = iptr->isn_lnum;
2445 if (dest_type == VAR_ANY)
2446 {
2447 dest_type = tv_dest->v_type;
2448 if (dest_type == VAR_DICT)
2449 status = do_2string(tv_idx, TRUE, FALSE);
2450 else if (dest_type == VAR_LIST
2451 && tv_idx->v_type != VAR_NUMBER)
2452 {
2453 emsg(_(e_number_expected));
2454 status = FAIL;
2455 }
2456 }
2457 else if (dest_type != tv_dest->v_type)
2458 {
2459 // just in case, should be OK
2460 semsg(_(e_expected_str_but_got_str),
2461 vartype_name(dest_type),
2462 vartype_name(tv_dest->v_type));
2463 status = FAIL;
2464 }
2465
2466 if (status == OK && dest_type == VAR_LIST)
2467 {
2468 long lidx = (long)tv_idx->vval.v_number;
2469 list_T *list = tv_dest->vval.v_list;
2470
2471 if (list == NULL)
2472 {
2473 emsg(_(e_list_not_set));
2474 goto on_error;
2475 }
2476 if (lidx < 0 && list->lv_len + lidx >= 0)
2477 // negative index is relative to the end
2478 lidx = list->lv_len + lidx;
2479 if (lidx < 0 || lidx > list->lv_len)
2480 {
2481 semsg(_(e_listidx), lidx);
2482 goto on_error;
2483 }
2484 if (lidx < list->lv_len)
2485 {
2486 listitem_T *li = list_find(list, lidx);
2487
2488 if (error_if_locked(li->li_tv.v_lock,
2489 e_cannot_change_list_item))
2490 goto on_error;
2491 // overwrite existing list item
2492 clear_tv(&li->li_tv);
2493 li->li_tv = *tv;
2494 }
2495 else
2496 {
2497 if (error_if_locked(list->lv_lock,
2498 e_cannot_change_list))
2499 goto on_error;
2500 // append to list, only fails when out of memory
2501 if (list_append_tv(list, tv) == FAIL)
2502 goto theend;
2503 clear_tv(tv);
2504 }
2505 }
2506 else if (status == OK && dest_type == VAR_DICT)
2507 {
2508 char_u *key = tv_idx->vval.v_string;
2509 dict_T *dict = tv_dest->vval.v_dict;
2510 dictitem_T *di;
2511
2512 SOURCING_LNUM = iptr->isn_lnum;
2513 if (dict == NULL)
2514 {
2515 emsg(_(e_dictionary_not_set));
2516 goto on_error;
2517 }
2518 if (key == NULL)
2519 key = (char_u *)"";
2520 di = dict_find(dict, key, -1);
2521 if (di != NULL)
2522 {
2523 if (error_if_locked(di->di_tv.v_lock,
2524 e_cannot_change_dict_item))
2525 goto on_error;
2526 // overwrite existing value
2527 clear_tv(&di->di_tv);
2528 di->di_tv = *tv;
2529 }
2530 else
2531 {
2532 if (error_if_locked(dict->dv_lock,
2533 e_cannot_change_dict))
2534 goto on_error;
2535 // add to dict, only fails when out of memory
2536 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2537 goto theend;
2538 clear_tv(tv);
2539 }
2540 }
2541 else if (status == OK && dest_type == VAR_BLOB)
2542 {
2543 long lidx = (long)tv_idx->vval.v_number;
2544 blob_T *blob = tv_dest->vval.v_blob;
2545 varnumber_T nr;
2546 int error = FALSE;
2547 int len;
2548
2549 if (blob == NULL)
2550 {
2551 emsg(_(e_blob_not_set));
2552 goto on_error;
2553 }
2554 len = blob_len(blob);
2555 if (lidx < 0 && len + lidx >= 0)
2556 // negative index is relative to the end
2557 lidx = len + lidx;
2558
2559 // Can add one byte at the end.
2560 if (lidx < 0 || lidx > len)
2561 {
2562 semsg(_(e_blobidx), lidx);
2563 goto on_error;
2564 }
2565 if (value_check_lock(blob->bv_lock,
2566 (char_u *)"blob", FALSE))
2567 goto on_error;
2568 nr = tv_get_number_chk(tv, &error);
2569 if (error)
2570 goto on_error;
2571 blob_set_append(blob, lidx, nr);
2572 }
2573 else
2574 {
2575 status = FAIL;
2576 semsg(_(e_cannot_index_str), vartype_name(dest_type));
2577 }
2578
2579 clear_tv(tv_idx);
2580 clear_tv(tv_dest);
2581 ectx->ec_stack.ga_len -= 3;
2582 if (status == FAIL)
2583 {
2584 clear_tv(tv);
2585 goto on_error;
2586 }
2587 }
2588 break;
2589
2590 // store value in blob range
2591 case ISN_STORERANGE:
2592 {
2593 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2594 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2595 typval_T *tv_dest = STACK_TV_BOT(-1);
2596 int status = OK;
2597
2598 // Stack contains:
2599 // -4 value to be stored
2600 // -3 first index or "none"
2601 // -2 second index or "none"
2602 // -1 destination list or blob
2603 tv = STACK_TV_BOT(-4);
2604 if (tv_dest->v_type == VAR_LIST)
2605 {
2606 long n1;
2607 long n2;
2608 int error = FALSE;
2609
2610 SOURCING_LNUM = iptr->isn_lnum;
2611 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2612 if (error)
2613 status = FAIL;
2614 else
2615 {
2616 if (tv_idx2->v_type == VAR_SPECIAL
2617 && tv_idx2->vval.v_number == VVAL_NONE)
2618 n2 = list_len(tv_dest->vval.v_list) - 1;
2619 else
2620 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2621 if (error)
2622 status = FAIL;
2623 else
2624 {
2625 listitem_T *li1 = check_range_index_one(
2626 tv_dest->vval.v_list, &n1, FALSE);
2627
2628 if (li1 == NULL)
2629 status = FAIL;
2630 else
2631 {
2632 status = check_range_index_two(
2633 tv_dest->vval.v_list,
2634 &n1, li1, &n2, FALSE);
2635 if (status != FAIL)
2636 status = list_assign_range(
2637 tv_dest->vval.v_list,
2638 tv->vval.v_list,
2639 n1,
2640 n2,
2641 tv_idx2->v_type == VAR_SPECIAL,
2642 (char_u *)"=",
2643 (char_u *)"[unknown]");
2644 }
2645 }
2646 }
2647 }
2648 else if (tv_dest->v_type == VAR_BLOB)
2649 {
2650 varnumber_T n1;
2651 varnumber_T n2;
2652 int error = FALSE;
2653
2654 n1 = tv_get_number_chk(tv_idx1, &error);
2655 if (error)
2656 status = FAIL;
2657 else
2658 {
2659 if (tv_idx2->v_type == VAR_SPECIAL
2660 && tv_idx2->vval.v_number == VVAL_NONE)
2661 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2662 else
2663 n2 = tv_get_number_chk(tv_idx2, &error);
2664 if (error)
2665 status = FAIL;
2666 else
2667 {
2668 long bloblen = blob_len(tv_dest->vval.v_blob);
2669
2670 if (check_blob_index(bloblen,
2671 n1, FALSE) == FAIL
2672 || check_blob_range(bloblen,
2673 n1, n2, FALSE) == FAIL)
2674 status = FAIL;
2675 else
2676 status = blob_set_range(
2677 tv_dest->vval.v_blob, n1, n2, tv);
2678 }
2679 }
2680 }
2681 else
2682 {
2683 status = FAIL;
2684 emsg(_(e_blob_required));
2685 }
2686
2687 clear_tv(tv_idx1);
2688 clear_tv(tv_idx2);
2689 clear_tv(tv_dest);
2690 ectx->ec_stack.ga_len -= 4;
2691 clear_tv(tv);
2692
2693 if (status == FAIL)
2694 goto on_error;
2695 }
2696 break;
2697
2698 // load or store variable or argument from outer scope
2699 case ISN_LOADOUTER:
2700 case ISN_STOREOUTER:
2701 {
2702 int depth = iptr->isn_arg.outer.outer_depth;
2703 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2704 : ectx->ec_outer_ref->or_outer;
2705
2706 while (depth > 1 && outer != NULL)
2707 {
2708 outer = outer->out_up;
2709 --depth;
2710 }
2711 if (outer == NULL)
2712 {
2713 SOURCING_LNUM = iptr->isn_lnum;
2714 iemsg("LOADOUTER depth more than scope levels");
2715 goto theend;
2716 }
2717 tv = ((typval_T *)outer->out_stack->ga_data)
2718 + outer->out_frame_idx + STACK_FRAME_SIZE
2719 + iptr->isn_arg.outer.outer_idx;
2720 if (iptr->isn_type == ISN_LOADOUTER)
2721 {
2722 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2723 goto theend;
2724 copy_tv(tv, STACK_TV_BOT(0));
2725 ++ectx->ec_stack.ga_len;
2726 }
2727 else
2728 {
2729 --ectx->ec_stack.ga_len;
2730 clear_tv(tv);
2731 *tv = *STACK_TV_BOT(0);
2732 }
2733 }
2734 break;
2735
2736 // unlet item in list or dict variable
2737 case ISN_UNLETINDEX:
2738 {
2739 typval_T *tv_idx = STACK_TV_BOT(-2);
2740 typval_T *tv_dest = STACK_TV_BOT(-1);
2741 int status = OK;
2742
2743 // Stack contains:
2744 // -2 index
2745 // -1 dict or list
2746 if (tv_dest->v_type == VAR_DICT)
2747 {
2748 // unlet a dict item, index must be a string
2749 if (tv_idx->v_type != VAR_STRING)
2750 {
2751 SOURCING_LNUM = iptr->isn_lnum;
2752 semsg(_(e_expected_str_but_got_str),
2753 vartype_name(VAR_STRING),
2754 vartype_name(tv_idx->v_type));
2755 status = FAIL;
2756 }
2757 else
2758 {
2759 dict_T *d = tv_dest->vval.v_dict;
2760 char_u *key = tv_idx->vval.v_string;
2761 dictitem_T *di = NULL;
2762
2763 if (key == NULL)
2764 key = (char_u *)"";
2765 if (d != NULL)
2766 di = dict_find(d, key, (int)STRLEN(key));
2767 if (di == NULL)
2768 {
2769 // NULL dict is equivalent to empty dict
2770 SOURCING_LNUM = iptr->isn_lnum;
2771 semsg(_(e_dictkey), key);
2772 status = FAIL;
2773 }
2774 else
2775 {
2776 // TODO: check for dict or item locked
2777 dictitem_remove(d, di);
2778 }
2779 }
2780 }
2781 else if (tv_dest->v_type == VAR_LIST)
2782 {
2783 // unlet a List item, index must be a number
2784 SOURCING_LNUM = iptr->isn_lnum;
2785 if (check_for_number(tv_idx) == FAIL)
2786 {
2787 status = FAIL;
2788 }
2789 else
2790 {
2791 list_T *l = tv_dest->vval.v_list;
2792 long n = (long)tv_idx->vval.v_number;
2793 listitem_T *li = NULL;
2794
2795 li = list_find(l, n);
2796 if (li == NULL)
2797 {
2798 SOURCING_LNUM = iptr->isn_lnum;
2799 semsg(_(e_listidx), n);
2800 status = FAIL;
2801 }
2802 else
2803 // TODO: check for list or item locked
2804 listitem_remove(l, li);
2805 }
2806 }
2807 else
2808 {
2809 status = FAIL;
2810 semsg(_(e_cannot_index_str),
2811 vartype_name(tv_dest->v_type));
2812 }
2813
2814 clear_tv(tv_idx);
2815 clear_tv(tv_dest);
2816 ectx->ec_stack.ga_len -= 2;
2817 if (status == FAIL)
2818 goto on_error;
2819 }
2820 break;
2821
2822 // unlet range of items in list variable
2823 case ISN_UNLETRANGE:
2824 {
2825 // Stack contains:
2826 // -3 index1
2827 // -2 index2
2828 // -1 dict or list
2829 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2830 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2831 typval_T *tv_dest = STACK_TV_BOT(-1);
2832 int status = OK;
2833
2834 if (tv_dest->v_type == VAR_LIST)
2835 {
2836 // indexes must be a number
2837 SOURCING_LNUM = iptr->isn_lnum;
2838 if (check_for_number(tv_idx1) == FAIL
2839 || (tv_idx2->v_type != VAR_SPECIAL
2840 && check_for_number(tv_idx2) == FAIL))
2841 {
2842 status = FAIL;
2843 }
2844 else
2845 {
2846 list_T *l = tv_dest->vval.v_list;
2847 long n1 = (long)tv_idx1->vval.v_number;
2848 long n2 = tv_idx2->v_type == VAR_SPECIAL
2849 ? 0 : (long)tv_idx2->vval.v_number;
2850 listitem_T *li;
2851
2852 li = list_find_index(l, &n1);
2853 if (li == NULL)
2854 status = FAIL;
2855 else
2856 {
2857 if (n1 < 0)
2858 n1 = list_idx_of_item(l, li);
2859 if (n2 < 0)
2860 {
2861 listitem_T *li2 = list_find(l, n2);
2862
2863 if (li2 == NULL)
2864 status = FAIL;
2865 else
2866 n2 = list_idx_of_item(l, li2);
2867 }
2868 if (status != FAIL
2869 && tv_idx2->v_type != VAR_SPECIAL
2870 && n2 < n1)
2871 {
2872 semsg(_(e_listidx), n2);
2873 status = FAIL;
2874 }
2875 if (status != FAIL
2876 && list_unlet_range(l, li, NULL, n1,
2877 tv_idx2->v_type != VAR_SPECIAL, n2)
2878 == FAIL)
2879 status = FAIL;
2880 }
2881 }
2882 }
2883 else
2884 {
2885 status = FAIL;
2886 SOURCING_LNUM = iptr->isn_lnum;
2887 semsg(_(e_cannot_index_str),
2888 vartype_name(tv_dest->v_type));
2889 }
2890
2891 clear_tv(tv_idx1);
2892 clear_tv(tv_idx2);
2893 clear_tv(tv_dest);
2894 ectx->ec_stack.ga_len -= 3;
2895 if (status == FAIL)
2896 goto on_error;
2897 }
2898 break;
2899
2900 // push constant
2901 case ISN_PUSHNR:
2902 case ISN_PUSHBOOL:
2903 case ISN_PUSHSPEC:
2904 case ISN_PUSHF:
2905 case ISN_PUSHS:
2906 case ISN_PUSHBLOB:
2907 case ISN_PUSHFUNC:
2908 case ISN_PUSHCHANNEL:
2909 case ISN_PUSHJOB:
2910 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2911 goto theend;
2912 tv = STACK_TV_BOT(0);
2913 tv->v_lock = 0;
2914 ++ectx->ec_stack.ga_len;
2915 switch (iptr->isn_type)
2916 {
2917 case ISN_PUSHNR:
2918 tv->v_type = VAR_NUMBER;
2919 tv->vval.v_number = iptr->isn_arg.number;
2920 break;
2921 case ISN_PUSHBOOL:
2922 tv->v_type = VAR_BOOL;
2923 tv->vval.v_number = iptr->isn_arg.number;
2924 break;
2925 case ISN_PUSHSPEC:
2926 tv->v_type = VAR_SPECIAL;
2927 tv->vval.v_number = iptr->isn_arg.number;
2928 break;
2929 #ifdef FEAT_FLOAT
2930 case ISN_PUSHF:
2931 tv->v_type = VAR_FLOAT;
2932 tv->vval.v_float = iptr->isn_arg.fnumber;
2933 break;
2934 #endif
2935 case ISN_PUSHBLOB:
2936 blob_copy(iptr->isn_arg.blob, tv);
2937 break;
2938 case ISN_PUSHFUNC:
2939 tv->v_type = VAR_FUNC;
2940 if (iptr->isn_arg.string == NULL)
2941 tv->vval.v_string = NULL;
2942 else
2943 tv->vval.v_string =
2944 vim_strsave(iptr->isn_arg.string);
2945 break;
2946 case ISN_PUSHCHANNEL:
2947 #ifdef FEAT_JOB_CHANNEL
2948 tv->v_type = VAR_CHANNEL;
2949 tv->vval.v_channel = iptr->isn_arg.channel;
2950 if (tv->vval.v_channel != NULL)
2951 ++tv->vval.v_channel->ch_refcount;
2952 #endif
2953 break;
2954 case ISN_PUSHJOB:
2955 #ifdef FEAT_JOB_CHANNEL
2956 tv->v_type = VAR_JOB;
2957 tv->vval.v_job = iptr->isn_arg.job;
2958 if (tv->vval.v_job != NULL)
2959 ++tv->vval.v_job->jv_refcount;
2960 #endif
2961 break;
2962 default:
2963 tv->v_type = VAR_STRING;
2964 tv->vval.v_string = vim_strsave(
2965 iptr->isn_arg.string == NULL
2966 ? (char_u *)"" : iptr->isn_arg.string);
2967 }
2968 break;
2969
2970 case ISN_UNLET:
2971 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2972 iptr->isn_arg.unlet.ul_forceit) == FAIL)
2973 goto on_error;
2974 break;
2975 case ISN_UNLETENV:
2976 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2977 break;
2978
2979 case ISN_LOCKUNLOCK:
2980 {
2981 typval_T *lval_root_save = lval_root;
2982 int res;
2983
2984 // Stack has the local variable, argument the whole :lock
2985 // or :unlock command, like ISN_EXEC.
2986 --ectx->ec_stack.ga_len;
2987 lval_root = STACK_TV_BOT(0);
2988 res = exec_command(iptr);
2989 clear_tv(lval_root);
2990 lval_root = lval_root_save;
2991 if (res == FAIL)
2992 goto on_error;
2993 }
2994 break;
2995
2996 case ISN_LOCKCONST:
2997 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2998 break;
2999
3000 // create a list from items on the stack; uses a single allocation
3001 // for the list header and the items
3002 case ISN_NEWLIST:
3003 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
3004 goto theend;
3005 break;
3006
3007 // create a dict from items on the stack
3008 case ISN_NEWDICT:
3009 {
3010 int count = iptr->isn_arg.number;
3011 dict_T *dict = dict_alloc();
3012 dictitem_T *item;
3013 char_u *key;
3014 int idx;
3015
3016 if (unlikely(dict == NULL))
3017 goto theend;
3018 for (idx = 0; idx < count; ++idx)
3019 {
3020 // have already checked key type is VAR_STRING
3021 tv = STACK_TV_BOT(2 * (idx - count));
3022 // check key is unique
3023 key = tv->vval.v_string == NULL
3024 ? (char_u *)"" : tv->vval.v_string;
3025 item = dict_find(dict, key, -1);
3026 if (item != NULL)
3027 {
3028 SOURCING_LNUM = iptr->isn_lnum;
3029 semsg(_(e_duplicate_key), key);
3030 dict_unref(dict);
3031 goto on_error;
3032 }
3033 item = dictitem_alloc(key);
3034 clear_tv(tv);
3035 if (unlikely(item == NULL))
3036 {
3037 dict_unref(dict);
3038 goto theend;
3039 }
3040 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3041 item->di_tv.v_lock = 0;
3042 if (dict_add(dict, item) == FAIL)
3043 {
3044 // can this ever happen?
3045 dict_unref(dict);
3046 goto theend;
3047 }
3048 }
3049
3050 if (count > 0)
3051 ectx->ec_stack.ga_len -= 2 * count - 1;
3052 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3053 goto theend;
3054 else
3055 ++ectx->ec_stack.ga_len;
3056 tv = STACK_TV_BOT(-1);
3057 tv->v_type = VAR_DICT;
3058 tv->v_lock = 0;
3059 tv->vval.v_dict = dict;
3060 ++dict->dv_refcount;
3061 }
3062 break;
3063
3064 // call a :def function
3065 case ISN_DCALL:
3066 SOURCING_LNUM = iptr->isn_lnum;
3067 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3068 NULL,
3069 iptr->isn_arg.dfunc.cdf_argcount,
3070 ectx) == FAIL)
3071 goto on_error;
3072 break;
3073
3074 // call a builtin function
3075 case ISN_BCALL:
3076 SOURCING_LNUM = iptr->isn_lnum;
3077 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3078 iptr->isn_arg.bfunc.cbf_argcount,
3079 ectx) == FAIL)
3080 goto on_error;
3081 break;
3082
3083 // call a funcref or partial
3084 case ISN_PCALL:
3085 {
3086 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3087 int r;
3088 typval_T partial_tv;
3089
3090 SOURCING_LNUM = iptr->isn_lnum;
3091 if (pfunc->cpf_top)
3092 {
3093 // funcref is above the arguments
3094 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3095 }
3096 else
3097 {
3098 // Get the funcref from the stack.
3099 --ectx->ec_stack.ga_len;
3100 partial_tv = *STACK_TV_BOT(0);
3101 tv = &partial_tv;
3102 }
3103 r = call_partial(tv, pfunc->cpf_argcount, ectx);
3104 if (tv == &partial_tv)
3105 clear_tv(&partial_tv);
3106 if (r == FAIL)
3107 goto on_error;
3108 }
3109 break;
3110
3111 case ISN_PCALL_END:
3112 // PCALL finished, arguments have been consumed and replaced by
3113 // the return value. Now clear the funcref from the stack,
3114 // and move the return value in its place.
3115 --ectx->ec_stack.ga_len;
3116 clear_tv(STACK_TV_BOT(-1));
3117 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3118 break;
3119
3120 // call a user defined function or funcref/partial
3121 case ISN_UCALL:
3122 {
3123 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3124
3125 SOURCING_LNUM = iptr->isn_lnum;
3126 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
3127 ectx, iptr) == FAIL)
3128 goto on_error;
3129 }
3130 break;
3131
3132 // return from a :def function call without a value
3133 case ISN_RETURN_VOID:
3134 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3135 goto theend;
3136 tv = STACK_TV_BOT(0);
3137 ++ectx->ec_stack.ga_len;
3138 tv->v_type = VAR_VOID;
3139 tv->vval.v_number = 0;
3140 tv->v_lock = 0;
3141 // FALLTHROUGH
3142
3143 // return from a :def function call with what is on the stack
3144 case ISN_RETURN:
3145 {
3146 garray_T *trystack = &ectx->ec_trystack;
3147 trycmd_T *trycmd = NULL;
3148
3149 if (trystack->ga_len > 0)
3150 trycmd = ((trycmd_T *)trystack->ga_data)
3151 + trystack->ga_len - 1;
3152 if (trycmd != NULL
3153 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3154 {
3155 // jump to ":finally" or ":endtry"
3156 if (trycmd->tcd_finally_idx != 0)
3157 ectx->ec_iidx = trycmd->tcd_finally_idx;
3158 else
3159 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3160 trycmd->tcd_return = TRUE;
3161 }
3162 else
3163 goto func_return;
3164 }
3165 break;
3166
3167 // push a partial, a reference to a compiled function
3168 case ISN_FUNCREF:
3169 {
3170 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
3171 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3172 + iptr->isn_arg.funcref.fr_func;
3173
3174 if (pt == NULL)
3175 goto theend;
3176 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3177 {
3178 vim_free(pt);
3179 goto theend;
3180 }
3181 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
3182 ectx) == FAIL)
3183 goto theend;
3184 tv = STACK_TV_BOT(0);
3185 ++ectx->ec_stack.ga_len;
3186 tv->vval.v_partial = pt;
3187 tv->v_type = VAR_PARTIAL;
3188 tv->v_lock = 0;
3189 }
3190 break;
3191
3192 // Create a global function from a lambda.
3193 case ISN_NEWFUNC:
3194 {
3195 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3196
3197 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
3198 ectx) == FAIL)
3199 goto theend;
3200 }
3201 break;
3202
3203 // List functions
3204 case ISN_DEF:
3205 if (iptr->isn_arg.string == NULL)
3206 list_functions(NULL);
3207 else
3208 {
3209 exarg_T ea;
3210
3211 CLEAR_FIELD(ea);
3212 ea.cmd = ea.arg = iptr->isn_arg.string;
3213 define_function(&ea, NULL);
3214 }
3215 break;
3216
3217 // jump if a condition is met
3218 case ISN_JUMP:
3219 {
3220 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
3221 int error = FALSE;
3222 int jump = TRUE;
3223
3224 if (when != JUMP_ALWAYS)
3225 {
3226 tv = STACK_TV_BOT(-1);
3227 if (when == JUMP_IF_COND_FALSE
3228 || when == JUMP_IF_FALSE
3229 || when == JUMP_IF_COND_TRUE)
3230 {
3231 SOURCING_LNUM = iptr->isn_lnum;
3232 jump = tv_get_bool_chk(tv, &error);
3233 if (error)
3234 goto on_error;
3235 }
3236 else
3237 jump = tv2bool(tv);
3238 if (when == JUMP_IF_FALSE
3239 || when == JUMP_AND_KEEP_IF_FALSE
3240 || when == JUMP_IF_COND_FALSE)
3241 jump = !jump;
3242 if (when == JUMP_IF_FALSE || !jump)
3243 {
3244 // drop the value from the stack
3245 clear_tv(tv);
3246 --ectx->ec_stack.ga_len;
3247 }
3248 }
3249 if (jump)
3250 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
3251 }
3252 break;
3253
3254 // Jump if an argument with a default value was already set and not
3255 // v:none.
3256 case ISN_JUMP_IF_ARG_SET:
3257 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3258 if (tv->v_type != VAR_UNKNOWN
3259 && !(tv->v_type == VAR_SPECIAL
3260 && tv->vval.v_number == VVAL_NONE))
3261 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
3262 break;
3263
3264 // top of a for loop
3265 case ISN_FOR:
3266 {
3267 typval_T *ltv = STACK_TV_BOT(-1);
3268 typval_T *idxtv =
3269 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3270
3271 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3272 goto theend;
3273 if (ltv->v_type == VAR_LIST)
3274 {
3275 list_T *list = ltv->vval.v_list;
3276
3277 // push the next item from the list
3278 ++idxtv->vval.v_number;
3279 if (list == NULL
3280 || idxtv->vval.v_number >= list->lv_len)
3281 {
3282 // past the end of the list, jump to "endfor"
3283 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3284 may_restore_cmdmod(&ectx->ec_funclocal);
3285 }
3286 else if (list->lv_first == &range_list_item)
3287 {
3288 // non-materialized range() list
3289 tv = STACK_TV_BOT(0);
3290 tv->v_type = VAR_NUMBER;
3291 tv->v_lock = 0;
3292 tv->vval.v_number = list_find_nr(
3293 list, idxtv->vval.v_number, NULL);
3294 ++ectx->ec_stack.ga_len;
3295 }
3296 else
3297 {
3298 listitem_T *li = list_find(list,
3299 idxtv->vval.v_number);
3300
3301 copy_tv(&li->li_tv, STACK_TV_BOT(0));
3302 ++ectx->ec_stack.ga_len;
3303 }
3304 }
3305 else if (ltv->v_type == VAR_STRING)
3306 {
3307 char_u *str = ltv->vval.v_string;
3308
3309 // The index is for the last byte of the previous
3310 // character.
3311 ++idxtv->vval.v_number;
3312 if (str == NULL || str[idxtv->vval.v_number] == NUL)
3313 {
3314 // past the end of the string, jump to "endfor"
3315 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3316 may_restore_cmdmod(&ectx->ec_funclocal);
3317 }
3318 else
3319 {
3320 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3321
3322 // Push the next character from the string.
3323 tv = STACK_TV_BOT(0);
3324 tv->v_type = VAR_STRING;
3325 tv->vval.v_string = vim_strnsave(
3326 str + idxtv->vval.v_number, clen);
3327 ++ectx->ec_stack.ga_len;
3328 idxtv->vval.v_number += clen - 1;
3329 }
3330 }
3331 else if (ltv->v_type == VAR_BLOB)
3332 {
3333 blob_T *blob = ltv->vval.v_blob;
3334
3335 // When we get here the first time make a copy of the
3336 // blob, so that the iteration still works when it is
3337 // changed.
3338 if (idxtv->vval.v_number == -1 && blob != NULL)
3339 {
3340 blob_copy(blob, ltv);
3341 blob_unref(blob);
3342 blob = ltv->vval.v_blob;
3343 }
3344
3345 // The index is for the previous byte.
3346 ++idxtv->vval.v_number;
3347 if (blob == NULL
3348 || idxtv->vval.v_number >= blob_len(blob))
3349 {
3350 // past the end of the blob, jump to "endfor"
3351 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3352 may_restore_cmdmod(&ectx->ec_funclocal);
3353 }
3354 else
3355 {
3356 // Push the next byte from the blob.
3357 tv = STACK_TV_BOT(0);
3358 tv->v_type = VAR_NUMBER;
3359 tv->vval.v_number = blob_get(blob,
3360 idxtv->vval.v_number);
3361 ++ectx->ec_stack.ga_len;
3362 }
3363 }
3364 else
3365 {
3366 semsg(_(e_for_loop_on_str_not_supported),
3367 vartype_name(ltv->v_type));
3368 goto theend;
3369 }
3370 }
3371 break;
3372
3373 // start of ":try" block
3374 case ISN_TRY:
3375 {
3376 trycmd_T *trycmd = NULL;
3377
3378 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
3379 goto theend;
3380 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3381 + ectx->ec_trystack.ga_len;
3382 ++ectx->ec_trystack.ga_len;
3383 ++trylevel;
3384 CLEAR_POINTER(trycmd);
3385 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3386 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
3387 trycmd->tcd_catch_idx =
3388 iptr->isn_arg.try.try_ref->try_catch;
3389 trycmd->tcd_finally_idx =
3390 iptr->isn_arg.try.try_ref->try_finally;
3391 trycmd->tcd_endtry_idx =
3392 iptr->isn_arg.try.try_ref->try_endtry;
3393 }
3394 break;
3395
3396 case ISN_PUSHEXC:
3397 if (current_exception == NULL)
3398 {
3399 SOURCING_LNUM = iptr->isn_lnum;
3400 iemsg("Evaluating catch while current_exception is NULL");
3401 goto theend;
3402 }
3403 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3404 goto theend;
3405 tv = STACK_TV_BOT(0);
3406 ++ectx->ec_stack.ga_len;
3407 tv->v_type = VAR_STRING;
3408 tv->v_lock = 0;
3409 tv->vval.v_string = vim_strsave(
3410 (char_u *)current_exception->value);
3411 break;
3412
3413 case ISN_CATCH:
3414 {
3415 garray_T *trystack = &ectx->ec_trystack;
3416
3417 may_restore_cmdmod(&ectx->ec_funclocal);
3418 if (trystack->ga_len > 0)
3419 {
3420 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3421 + trystack->ga_len - 1;
3422 trycmd->tcd_caught = TRUE;
3423 trycmd->tcd_did_throw = FALSE;
3424 }
3425 did_emsg = got_int = did_throw = FALSE;
3426 force_abort = need_rethrow = FALSE;
3427 catch_exception(current_exception);
3428 }
3429 break;
3430
3431 case ISN_TRYCONT:
3432 {
3433 garray_T *trystack = &ectx->ec_trystack;
3434 trycont_T *trycont = &iptr->isn_arg.trycont;
3435 int i;
3436 trycmd_T *trycmd;
3437 int iidx = trycont->tct_where;
3438
3439 if (trystack->ga_len < trycont->tct_levels)
3440 {
3441 siemsg("TRYCONT: expected %d levels, found %d",
3442 trycont->tct_levels, trystack->ga_len);
3443 goto theend;
3444 }
3445 // Make :endtry jump to any outer try block and the last
3446 // :endtry inside the loop to the loop start.
3447 for (i = trycont->tct_levels; i > 0; --i)
3448 {
3449 trycmd = ((trycmd_T *)trystack->ga_data)
3450 + trystack->ga_len - i;
3451 // Add one to tcd_cont to be able to jump to
3452 // instruction with index zero.
3453 trycmd->tcd_cont = iidx + 1;
3454 iidx = trycmd->tcd_finally_idx == 0
3455 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
3456 }
3457 // jump to :finally or :endtry of current try statement
3458 ectx->ec_iidx = iidx;
3459 }
3460 break;
3461
3462 case ISN_FINALLY:
3463 {
3464 garray_T *trystack = &ectx->ec_trystack;
3465 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3466 + trystack->ga_len - 1;
3467
3468 // Reset the index to avoid a return statement jumps here
3469 // again.
3470 trycmd->tcd_finally_idx = 0;
3471 break;
3472 }
3473
3474 // end of ":try" block
3475 case ISN_ENDTRY:
3476 {
3477 garray_T *trystack = &ectx->ec_trystack;
3478
3479 if (trystack->ga_len > 0)
3480 {
3481 trycmd_T *trycmd;
3482
3483 --trystack->ga_len;
3484 --trylevel;
3485 trycmd = ((trycmd_T *)trystack->ga_data)
3486 + trystack->ga_len;
3487 if (trycmd->tcd_did_throw)
3488 did_throw = TRUE;
3489 if (trycmd->tcd_caught && current_exception != NULL)
3490 {
3491 // discard the exception
3492 if (caught_stack == current_exception)
3493 caught_stack = caught_stack->caught;
3494 discard_current_exception();
3495 }
3496
3497 if (trycmd->tcd_return)
3498 goto func_return;
3499
3500 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
3501 {
3502 --ectx->ec_stack.ga_len;
3503 clear_tv(STACK_TV_BOT(0));
3504 }
3505 if (trycmd->tcd_cont != 0)
3506 // handling :continue: jump to outer try block or
3507 // start of the loop
3508 ectx->ec_iidx = trycmd->tcd_cont - 1;
3509 }
3510 }
3511 break;
3512
3513 case ISN_THROW:
3514 {
3515 garray_T *trystack = &ectx->ec_trystack;
3516
3517 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3518 {
3519 // throwing an exception while using "silent!" causes
3520 // the function to abort but not display an error.
3521 tv = STACK_TV_BOT(-1);
3522 clear_tv(tv);
3523 tv->v_type = VAR_NUMBER;
3524 tv->vval.v_number = 0;
3525 goto done;
3526 }
3527 --ectx->ec_stack.ga_len;
3528 tv = STACK_TV_BOT(0);
3529 if (tv->vval.v_string == NULL
3530 || *skipwhite(tv->vval.v_string) == NUL)
3531 {
3532 vim_free(tv->vval.v_string);
3533 SOURCING_LNUM = iptr->isn_lnum;
3534 emsg(_(e_throw_with_empty_string));
3535 goto theend;
3536 }
3537
3538 // Inside a "catch" we need to first discard the caught
3539 // exception.
3540 if (trystack->ga_len > 0)
3541 {
3542 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3543 + trystack->ga_len - 1;
3544 if (trycmd->tcd_caught && current_exception != NULL)
3545 {
3546 // discard the exception
3547 if (caught_stack == current_exception)
3548 caught_stack = caught_stack->caught;
3549 discard_current_exception();
3550 trycmd->tcd_caught = FALSE;
3551 }
3552 }
3553
3554 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3555 == FAIL)
3556 {
3557 vim_free(tv->vval.v_string);
3558 goto theend;
3559 }
3560 did_throw = TRUE;
3561 }
3562 break;
3563
3564 // compare with special values
3565 case ISN_COMPAREBOOL:
3566 case ISN_COMPARESPECIAL:
3567 {
3568 typval_T *tv1 = STACK_TV_BOT(-2);
3569 typval_T *tv2 = STACK_TV_BOT(-1);
3570 varnumber_T arg1 = tv1->vval.v_number;
3571 varnumber_T arg2 = tv2->vval.v_number;
3572 int res;
3573
3574 switch (iptr->isn_arg.op.op_type)
3575 {
3576 case EXPR_EQUAL: res = arg1 == arg2; break;
3577 case EXPR_NEQUAL: res = arg1 != arg2; break;
3578 default: res = 0; break;
3579 }
3580
3581 --ectx->ec_stack.ga_len;
3582 tv1->v_type = VAR_BOOL;
3583 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3584 }
3585 break;
3586
3587 // Operation with two number arguments
3588 case ISN_OPNR:
3589 case ISN_COMPARENR:
3590 {
3591 typval_T *tv1 = STACK_TV_BOT(-2);
3592 typval_T *tv2 = STACK_TV_BOT(-1);
3593 varnumber_T arg1 = tv1->vval.v_number;
3594 varnumber_T arg2 = tv2->vval.v_number;
3595 varnumber_T res = 0;
3596 int div_zero = FALSE;
3597
3598 switch (iptr->isn_arg.op.op_type)
3599 {
3600 case EXPR_MULT: res = arg1 * arg2; break;
3601 case EXPR_DIV: if (arg2 == 0)
3602 div_zero = TRUE;
3603 else
3604 res = arg1 / arg2;
3605 break;
3606 case EXPR_REM: if (arg2 == 0)
3607 div_zero = TRUE;
3608 else
3609 res = arg1 % arg2;
3610 break;
3611 case EXPR_SUB: res = arg1 - arg2; break;
3612 case EXPR_ADD: res = arg1 + arg2; break;
3613
3614 case EXPR_EQUAL: res = arg1 == arg2; break;
3615 case EXPR_NEQUAL: res = arg1 != arg2; break;
3616 case EXPR_GREATER: res = arg1 > arg2; break;
3617 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3618 case EXPR_SMALLER: res = arg1 < arg2; break;
3619 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3620 default: break;
3621 }
3622
3623 --ectx->ec_stack.ga_len;
3624 if (iptr->isn_type == ISN_COMPARENR)
3625 {
3626 tv1->v_type = VAR_BOOL;
3627 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3628 }
3629 else
3630 tv1->vval.v_number = res;
3631 if (div_zero)
3632 {
3633 SOURCING_LNUM = iptr->isn_lnum;
3634 emsg(_(e_divide_by_zero));
3635 goto on_error;
3636 }
3637 }
3638 break;
3639
3640 // Computation with two float arguments
3641 case ISN_OPFLOAT:
3642 case ISN_COMPAREFLOAT:
3643 #ifdef FEAT_FLOAT
3644 {
3645 typval_T *tv1 = STACK_TV_BOT(-2);
3646 typval_T *tv2 = STACK_TV_BOT(-1);
3647 float_T arg1 = tv1->vval.v_float;
3648 float_T arg2 = tv2->vval.v_float;
3649 float_T res = 0;
3650 int cmp = FALSE;
3651
3652 switch (iptr->isn_arg.op.op_type)
3653 {
3654 case EXPR_MULT: res = arg1 * arg2; break;
3655 case EXPR_DIV: res = arg1 / arg2; break;
3656 case EXPR_SUB: res = arg1 - arg2; break;
3657 case EXPR_ADD: res = arg1 + arg2; break;
3658
3659 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3660 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3661 case EXPR_GREATER: cmp = arg1 > arg2; break;
3662 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3663 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3664 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3665 default: cmp = 0; break;
3666 }
3667 --ectx->ec_stack.ga_len;
3668 if (iptr->isn_type == ISN_COMPAREFLOAT)
3669 {
3670 tv1->v_type = VAR_BOOL;
3671 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3672 }
3673 else
3674 tv1->vval.v_float = res;
3675 }
3676 #endif
3677 break;
3678
3679 case ISN_COMPARELIST:
3680 {
3681 typval_T *tv1 = STACK_TV_BOT(-2);
3682 typval_T *tv2 = STACK_TV_BOT(-1);
3683 list_T *arg1 = tv1->vval.v_list;
3684 list_T *arg2 = tv2->vval.v_list;
3685 int cmp = FALSE;
3686 int ic = iptr->isn_arg.op.op_ic;
3687
3688 switch (iptr->isn_arg.op.op_type)
3689 {
3690 case EXPR_EQUAL: cmp =
3691 list_equal(arg1, arg2, ic, FALSE); break;
3692 case EXPR_NEQUAL: cmp =
3693 !list_equal(arg1, arg2, ic, FALSE); break;
3694 case EXPR_IS: cmp = arg1 == arg2; break;
3695 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3696 default: cmp = 0; break;
3697 }
3698 --ectx->ec_stack.ga_len;
3699 clear_tv(tv1);
3700 clear_tv(tv2);
3701 tv1->v_type = VAR_BOOL;
3702 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3703 }
3704 break;
3705
3706 case ISN_COMPAREBLOB:
3707 {
3708 typval_T *tv1 = STACK_TV_BOT(-2);
3709 typval_T *tv2 = STACK_TV_BOT(-1);
3710 blob_T *arg1 = tv1->vval.v_blob;
3711 blob_T *arg2 = tv2->vval.v_blob;
3712 int cmp = FALSE;
3713
3714 switch (iptr->isn_arg.op.op_type)
3715 {
3716 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3717 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3718 case EXPR_IS: cmp = arg1 == arg2; break;
3719 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3720 default: cmp = 0; break;
3721 }
3722 --ectx->ec_stack.ga_len;
3723 clear_tv(tv1);
3724 clear_tv(tv2);
3725 tv1->v_type = VAR_BOOL;
3726 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3727 }
3728 break;
3729
3730 // TODO: handle separately
3731 case ISN_COMPARESTRING:
3732 case ISN_COMPAREDICT:
3733 case ISN_COMPAREFUNC:
3734 case ISN_COMPAREANY:
3735 {
3736 typval_T *tv1 = STACK_TV_BOT(-2);
3737 typval_T *tv2 = STACK_TV_BOT(-1);
3738 exprtype_T exprtype = iptr->isn_arg.op.op_type;
3739 int ic = iptr->isn_arg.op.op_ic;
3740
3741 SOURCING_LNUM = iptr->isn_lnum;
3742 typval_compare(tv1, tv2, exprtype, ic);
3743 clear_tv(tv2);
3744 --ectx->ec_stack.ga_len;
3745 }
3746 break;
3747
3748 case ISN_ADDLIST:
3749 case ISN_ADDBLOB:
3750 {
3751 typval_T *tv1 = STACK_TV_BOT(-2);
3752 typval_T *tv2 = STACK_TV_BOT(-1);
3753
3754 // add two lists or blobs
3755 if (iptr->isn_type == ISN_ADDLIST)
3756 {
3757 if (iptr->isn_arg.op.op_type == EXPR_APPEND
3758 && tv1->vval.v_list != NULL)
3759 list_extend(tv1->vval.v_list, tv2->vval.v_list,
3760 NULL);
3761 else
3762 eval_addlist(tv1, tv2);
3763 }
3764 else
3765 eval_addblob(tv1, tv2);
3766 clear_tv(tv2);
3767 --ectx->ec_stack.ga_len;
3768 }
3769 break;
3770
3771 case ISN_LISTAPPEND:
3772 {
3773 typval_T *tv1 = STACK_TV_BOT(-2);
3774 typval_T *tv2 = STACK_TV_BOT(-1);
3775 list_T *l = tv1->vval.v_list;
3776
3777 // add an item to a list
3778 if (l == NULL)
3779 {
3780 SOURCING_LNUM = iptr->isn_lnum;
3781 emsg(_(e_cannot_add_to_null_list));
3782 goto on_error;
3783 }
3784 if (list_append_tv(l, tv2) == FAIL)
3785 goto theend;
3786 clear_tv(tv2);
3787 --ectx->ec_stack.ga_len;
3788 }
3789 break;
3790
3791 case ISN_BLOBAPPEND:
3792 {
3793 typval_T *tv1 = STACK_TV_BOT(-2);
3794 typval_T *tv2 = STACK_TV_BOT(-1);
3795 blob_T *b = tv1->vval.v_blob;
3796 int error = FALSE;
3797 varnumber_T n;
3798
3799 // add a number to a blob
3800 if (b == NULL)
3801 {
3802 SOURCING_LNUM = iptr->isn_lnum;
3803 emsg(_(e_cannot_add_to_null_blob));
3804 goto on_error;
3805 }
3806 n = tv_get_number_chk(tv2, &error);
3807 if (error)
3808 goto on_error;
3809 ga_append(&b->bv_ga, (int)n);
3810 --ectx->ec_stack.ga_len;
3811 }
3812 break;
3813
3814 // Computation with two arguments of unknown type
3815 case ISN_OPANY:
3816 {
3817 typval_T *tv1 = STACK_TV_BOT(-2);
3818 typval_T *tv2 = STACK_TV_BOT(-1);
3819 varnumber_T n1, n2;
3820 #ifdef FEAT_FLOAT
3821 float_T f1 = 0, f2 = 0;
3822 #endif
3823 int error = FALSE;
3824
3825 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3826 {
3827 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3828 {
3829 eval_addlist(tv1, tv2);
3830 clear_tv(tv2);
3831 --ectx->ec_stack.ga_len;
3832 break;
3833 }
3834 else if (tv1->v_type == VAR_BLOB
3835 && tv2->v_type == VAR_BLOB)
3836 {
3837 eval_addblob(tv1, tv2);
3838 clear_tv(tv2);
3839 --ectx->ec_stack.ga_len;
3840 break;
3841 }
3842 }
3843 #ifdef FEAT_FLOAT
3844 if (tv1->v_type == VAR_FLOAT)
3845 {
3846 f1 = tv1->vval.v_float;
3847 n1 = 0;
3848 }
3849 else
3850 #endif
3851 {
3852 SOURCING_LNUM = iptr->isn_lnum;
3853 n1 = tv_get_number_chk(tv1, &error);
3854 if (error)
3855 goto on_error;
3856 #ifdef FEAT_FLOAT
3857 if (tv2->v_type == VAR_FLOAT)
3858 f1 = n1;
3859 #endif
3860 }
3861 #ifdef FEAT_FLOAT
3862 if (tv2->v_type == VAR_FLOAT)
3863 {
3864 f2 = tv2->vval.v_float;
3865 n2 = 0;
3866 }
3867 else
3868 #endif
3869 {
3870 n2 = tv_get_number_chk(tv2, &error);
3871 if (error)
3872 goto on_error;
3873 #ifdef FEAT_FLOAT
3874 if (tv1->v_type == VAR_FLOAT)
3875 f2 = n2;
3876 #endif
3877 }
3878 #ifdef FEAT_FLOAT
3879 // if there is a float on either side the result is a float
3880 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3881 {
3882 switch (iptr->isn_arg.op.op_type)
3883 {
3884 case EXPR_MULT: f1 = f1 * f2; break;
3885 case EXPR_DIV: f1 = f1 / f2; break;
3886 case EXPR_SUB: f1 = f1 - f2; break;
3887 case EXPR_ADD: f1 = f1 + f2; break;
3888 default: SOURCING_LNUM = iptr->isn_lnum;
3889 emsg(_(e_modulus));
3890 goto on_error;
3891 }
3892 clear_tv(tv1);
3893 clear_tv(tv2);
3894 tv1->v_type = VAR_FLOAT;
3895 tv1->vval.v_float = f1;
3896 --ectx->ec_stack.ga_len;
3897 }
3898 else
3899 #endif
3900 {
3901 int failed = FALSE;
3902
3903 switch (iptr->isn_arg.op.op_type)
3904 {
3905 case EXPR_MULT: n1 = n1 * n2; break;
3906 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3907 if (failed)
3908 goto on_error;
3909 break;
3910 case EXPR_SUB: n1 = n1 - n2; break;
3911 case EXPR_ADD: n1 = n1 + n2; break;
3912 default: n1 = num_modulus(n1, n2, &failed);
3913 if (failed)
3914 goto on_error;
3915 break;
3916 }
3917 clear_tv(tv1);
3918 clear_tv(tv2);
3919 tv1->v_type = VAR_NUMBER;
3920 tv1->vval.v_number = n1;
3921 --ectx->ec_stack.ga_len;
3922 }
3923 }
3924 break;
3925
3926 case ISN_CONCAT:
3927 {
3928 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3929 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3930 char_u *res;
3931
3932 res = concat_str(str1, str2);
3933 clear_tv(STACK_TV_BOT(-2));
3934 clear_tv(STACK_TV_BOT(-1));
3935 --ectx->ec_stack.ga_len;
3936 STACK_TV_BOT(-1)->vval.v_string = res;
3937 }
3938 break;
3939
3940 case ISN_STRINDEX:
3941 case ISN_STRSLICE:
3942 {
3943 int is_slice = iptr->isn_type == ISN_STRSLICE;
3944 varnumber_T n1 = 0, n2;
3945 char_u *res;
3946
3947 // string index: string is at stack-2, index at stack-1
3948 // string slice: string is at stack-3, first index at
3949 // stack-2, second index at stack-1
3950 if (is_slice)
3951 {
3952 tv = STACK_TV_BOT(-2);
3953 n1 = tv->vval.v_number;
3954 }
3955
3956 tv = STACK_TV_BOT(-1);
3957 n2 = tv->vval.v_number;
3958
3959 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
3960 tv = STACK_TV_BOT(-1);
3961 if (is_slice)
3962 // Slice: Select the characters from the string
3963 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
3964 else
3965 // Index: The resulting variable is a string of a
3966 // single character (including composing characters).
3967 // If the index is too big or negative the result is
3968 // empty.
3969 res = char_from_string(tv->vval.v_string, n2);
3970 vim_free(tv->vval.v_string);
3971 tv->vval.v_string = res;
3972 }
3973 break;
3974
3975 case ISN_LISTINDEX:
3976 case ISN_LISTSLICE:
3977 case ISN_BLOBINDEX:
3978 case ISN_BLOBSLICE:
3979 {
3980 int is_slice = iptr->isn_type == ISN_LISTSLICE
3981 || iptr->isn_type == ISN_BLOBSLICE;
3982 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3983 || iptr->isn_type == ISN_BLOBSLICE;
3984 varnumber_T n1, n2;
3985 typval_T *val_tv;
3986
3987 // list index: list is at stack-2, index at stack-1
3988 // list slice: list is at stack-3, indexes at stack-2 and
3989 // stack-1
3990 // Same for blob.
3991 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
3992
3993 tv = STACK_TV_BOT(-1);
3994 n1 = n2 = tv->vval.v_number;
3995 clear_tv(tv);
3996
3997 if (is_slice)
3998 {
3999 tv = STACK_TV_BOT(-2);
4000 n1 = tv->vval.v_number;
4001 clear_tv(tv);
4002 }
4003
4004 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
4005 tv = STACK_TV_BOT(-1);
4006 SOURCING_LNUM = iptr->isn_lnum;
4007 if (is_blob)
4008 {
4009 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4010 n1, n2, FALSE, tv) == FAIL)
4011 goto on_error;
4012 }
4013 else
4014 {
4015 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4016 n1, n2, FALSE, tv, TRUE) == FAIL)
4017 goto on_error;
4018 }
4019 }
4020 break;
4021
4022 case ISN_ANYINDEX:
4023 case ISN_ANYSLICE:
4024 {
4025 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4026 typval_T *var1, *var2;
4027 int res;
4028
4029 // index: composite is at stack-2, index at stack-1
4030 // slice: composite is at stack-3, indexes at stack-2 and
4031 // stack-1
4032 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
4033 SOURCING_LNUM = iptr->isn_lnum;
4034 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4035 goto on_error;
4036 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4037 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
4038 res = eval_index_inner(tv, is_slice, var1, var2,
4039 FALSE, NULL, -1, TRUE);
4040 clear_tv(var1);
4041 if (is_slice)
4042 clear_tv(var2);
4043 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
4044 if (res == FAIL)
4045 goto on_error;
4046 }
4047 break;
4048
4049 case ISN_SLICE:
4050 {
4051 list_T *list;
4052 int count = iptr->isn_arg.number;
4053
4054 // type will have been checked to be a list
4055 tv = STACK_TV_BOT(-1);
4056 list = tv->vval.v_list;
4057
4058 // no error for short list, expect it to be checked earlier
4059 if (list != NULL && list->lv_len >= count)
4060 {
4061 list_T *newlist = list_slice(list,
4062 count, list->lv_len - 1);
4063
4064 if (newlist != NULL)
4065 {
4066 list_unref(list);
4067 tv->vval.v_list = newlist;
4068 ++newlist->lv_refcount;
4069 }
4070 }
4071 }
4072 break;
4073
4074 case ISN_GETITEM:
4075 {
4076 listitem_T *li;
4077 getitem_T *gi = &iptr->isn_arg.getitem;
4078
4079 // Get list item: list is at stack-1, push item.
4080 // List type and length is checked for when compiling.
4081 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4082 li = list_find(tv->vval.v_list, gi->gi_index);
4083
4084 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4085 goto theend;
4086 ++ectx->ec_stack.ga_len;
4087 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
4088
4089 // Useful when used in unpack assignment. Reset at
4090 // ISN_DROP.
4091 ectx->ec_where.wt_index = gi->gi_index + 1;
4092 ectx->ec_where.wt_variable = TRUE;
4093 }
4094 break;
4095
4096 case ISN_MEMBER:
4097 {
4098 dict_T *dict;
4099 char_u *key;
4100 dictitem_T *di;
4101
4102 // dict member: dict is at stack-2, key at stack-1
4103 tv = STACK_TV_BOT(-2);
4104 // no need to check for VAR_DICT, CHECKTYPE will check.
4105 dict = tv->vval.v_dict;
4106
4107 tv = STACK_TV_BOT(-1);
4108 // no need to check for VAR_STRING, 2STRING will check.
4109 key = tv->vval.v_string;
4110 if (key == NULL)
4111 key = (char_u *)"";
4112
4113 if ((di = dict_find(dict, key, -1)) == NULL)
4114 {
4115 SOURCING_LNUM = iptr->isn_lnum;
4116 semsg(_(e_dictkey), key);
4117
4118 // If :silent! is used we will continue, make sure the
4119 // stack contents makes sense and the dict stack is
4120 // updated.
4121 clear_tv(tv);
4122 --ectx->ec_stack.ga_len;
4123 tv = STACK_TV_BOT(-1);
4124 (void) dict_stack_save(tv);
4125 tv->v_type = VAR_NUMBER;
4126 tv->vval.v_number = 0;
4127 goto on_fatal_error;
4128 }
4129 clear_tv(tv);
4130 --ectx->ec_stack.ga_len;
4131 // Put the dict used on the dict stack, it might be used by
4132 // a dict function later.
4133 tv = STACK_TV_BOT(-1);
4134 if (dict_stack_save(tv) == FAIL)
4135 goto on_fatal_error;
4136 copy_tv(&di->di_tv, tv);
4137 }
4138 break;
4139
4140 // dict member with string key
4141 case ISN_STRINGMEMBER:
4142 {
4143 dict_T *dict;
4144 dictitem_T *di;
4145
4146 tv = STACK_TV_BOT(-1);
4147 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4148 {
4149 SOURCING_LNUM = iptr->isn_lnum;
4150 emsg(_(e_dictreq));
4151 goto on_error;
4152 }
4153 dict = tv->vval.v_dict;
4154
4155 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4156 == NULL)
4157 {
4158 SOURCING_LNUM = iptr->isn_lnum;
4159 semsg(_(e_dictkey), iptr->isn_arg.string);
4160 goto on_error;
4161 }
4162 // Put the dict used on the dict stack, it might be used by
4163 // a dict function later.
4164 if (dict_stack_save(tv) == FAIL)
4165 goto on_fatal_error;
4166
4167 copy_tv(&di->di_tv, tv);
4168 }
4169 break;
4170
4171 case ISN_CLEARDICT:
4172 dict_stack_drop();
4173 break;
4174
4175 case ISN_USEDICT:
4176 {
4177 typval_T *dict_tv = dict_stack_get_tv();
4178
4179 // Turn "dict.Func" into a partial for "Func" bound to
4180 // "dict". Don't do this when "Func" is already a partial
4181 // that was bound explicitly (pt_auto is FALSE).
4182 tv = STACK_TV_BOT(-1);
4183 if (dict_tv != NULL
4184 && dict_tv->v_type == VAR_DICT
4185 && dict_tv->vval.v_dict != NULL
4186 && (tv->v_type == VAR_FUNC
4187 || (tv->v_type == VAR_PARTIAL
4188 && (tv->vval.v_partial->pt_auto
4189 || tv->vval.v_partial->pt_dict == NULL))))
4190 dict_tv->vval.v_dict =
4191 make_partial(dict_tv->vval.v_dict, tv);
4192 dict_stack_drop();
4193 }
4194 break;
4195
4196 case ISN_NEGATENR:
4197 tv = STACK_TV_BOT(-1);
4198 if (tv->v_type != VAR_NUMBER
4199 #ifdef FEAT_FLOAT
4200 && tv->v_type != VAR_FLOAT
4201 #endif
4202 )
4203 {
4204 SOURCING_LNUM = iptr->isn_lnum;
4205 emsg(_(e_number_expected));
4206 goto on_error;
4207 }
4208 #ifdef FEAT_FLOAT
4209 if (tv->v_type == VAR_FLOAT)
4210 tv->vval.v_float = -tv->vval.v_float;
4211 else
4212 #endif
4213 tv->vval.v_number = -tv->vval.v_number;
4214 break;
4215
4216 case ISN_CHECKNR:
4217 {
4218 int error = FALSE;
4219
4220 tv = STACK_TV_BOT(-1);
4221 SOURCING_LNUM = iptr->isn_lnum;
4222 if (check_not_string(tv) == FAIL)
4223 goto on_error;
4224 (void)tv_get_number_chk(tv, &error);
4225 if (error)
4226 goto on_error;
4227 }
4228 break;
4229
4230 case ISN_CHECKTYPE:
4231 {
4232 checktype_T *ct = &iptr->isn_arg.type;
4233
4234 tv = STACK_TV_BOT((int)ct->ct_off);
4235 SOURCING_LNUM = iptr->isn_lnum;
4236 if (!ectx->ec_where.wt_variable)
4237 ectx->ec_where.wt_index = ct->ct_arg_idx;
4238 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4239 == FAIL)
4240 goto on_error;
4241 if (!ectx->ec_where.wt_variable)
4242 ectx->ec_where.wt_index = 0;
4243
4244 // number 0 is FALSE, number 1 is TRUE
4245 if (tv->v_type == VAR_NUMBER
4246 && ct->ct_type->tt_type == VAR_BOOL
4247 && (tv->vval.v_number == 0
4248 || tv->vval.v_number == 1))
4249 {
4250 tv->v_type = VAR_BOOL;
4251 tv->vval.v_number = tv->vval.v_number
4252 ? VVAL_TRUE : VVAL_FALSE;
4253 }
4254 }
4255 break;
4256
4257 case ISN_CHECKLEN:
4258 {
4259 int min_len = iptr->isn_arg.checklen.cl_min_len;
4260 list_T *list = NULL;
4261
4262 tv = STACK_TV_BOT(-1);
4263 if (tv->v_type == VAR_LIST)
4264 list = tv->vval.v_list;
4265 if (list == NULL || list->lv_len < min_len
4266 || (list->lv_len > min_len
4267 && !iptr->isn_arg.checklen.cl_more_OK))
4268 {
4269 SOURCING_LNUM = iptr->isn_lnum;
4270 semsg(_(e_expected_nr_items_but_got_nr),
4271 min_len, list == NULL ? 0 : list->lv_len);
4272 goto on_error;
4273 }
4274 }
4275 break;
4276
4277 case ISN_SETTYPE:
4278 {
4279 checktype_T *ct = &iptr->isn_arg.type;
4280
4281 tv = STACK_TV_BOT(-1);
4282 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4283 {
4284 free_type(tv->vval.v_dict->dv_type);
4285 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4286 }
4287 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4288 {
4289 free_type(tv->vval.v_list->lv_type);
4290 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4291 }
4292 }
4293 break;
4294
4295 case ISN_2BOOL:
4296 case ISN_COND2BOOL:
4297 {
4298 int n;
4299 int error = FALSE;
4300
4301 if (iptr->isn_type == ISN_2BOOL)
4302 {
4303 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
4304 n = tv2bool(tv);
4305 if (iptr->isn_arg.tobool.invert)
4306 n = !n;
4307 }
4308 else
4309 {
4310 tv = STACK_TV_BOT(-1);
4311 SOURCING_LNUM = iptr->isn_lnum;
4312 n = tv_get_bool_chk(tv, &error);
4313 if (error)
4314 goto on_error;
4315 }
4316 clear_tv(tv);
4317 tv->v_type = VAR_BOOL;
4318 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4319 }
4320 break;
4321
4322 case ISN_2STRING:
4323 case ISN_2STRING_ANY:
4324 SOURCING_LNUM = iptr->isn_lnum;
4325 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4326 iptr->isn_type == ISN_2STRING_ANY,
4327 iptr->isn_arg.tostring.tolerant) == FAIL)
4328 goto on_error;
4329 break;
4330
4331 case ISN_RANGE:
4332 {
4333 exarg_T ea;
4334 char *errormsg;
4335
4336 ea.line2 = 0;
4337 ea.addr_count = 0;
4338 ea.addr_type = ADDR_LINES;
4339 ea.cmd = iptr->isn_arg.string;
4340 ea.skip = FALSE;
4341 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
4342 goto on_error;
4343
4344 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4345 goto theend;
4346 ++ectx->ec_stack.ga_len;
4347 tv = STACK_TV_BOT(-1);
4348 tv->v_type = VAR_NUMBER;
4349 tv->v_lock = 0;
4350 if (ea.addr_count == 0)
4351 tv->vval.v_number = curwin->w_cursor.lnum;
4352 else
4353 tv->vval.v_number = ea.line2;
4354 }
4355 break;
4356
4357 case ISN_PUT:
4358 {
4359 int regname = iptr->isn_arg.put.put_regname;
4360 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4361 char_u *expr = NULL;
4362 int dir = FORWARD;
4363
4364 if (lnum < -2)
4365 {
4366 // line number was put on the stack by ISN_RANGE
4367 tv = STACK_TV_BOT(-1);
4368 curwin->w_cursor.lnum = tv->vval.v_number;
4369 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4370 dir = BACKWARD;
4371 --ectx->ec_stack.ga_len;
4372 }
4373 else if (lnum == -2)
4374 // :put! above cursor
4375 dir = BACKWARD;
4376 else if (lnum >= 0)
4377 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
4378
4379 if (regname == '=')
4380 {
4381 tv = STACK_TV_BOT(-1);
4382 if (tv->v_type == VAR_STRING)
4383 expr = tv->vval.v_string;
4384 else
4385 {
4386 expr = typval2string(tv, TRUE); // allocates value
4387 clear_tv(tv);
4388 }
4389 --ectx->ec_stack.ga_len;
4390 }
4391 check_cursor();
4392 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4393 vim_free(expr);
4394 }
4395 break;
4396
4397 case ISN_CMDMOD:
4398 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4399 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4400 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4401 ectx->ec_stack.ga_len;
4402 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4403 apply_cmdmod(&cmdmod);
4404 break;
4405
4406 case ISN_CMDMOD_REV:
4407 // filter regprog is owned by the instruction, don't free it
4408 cmdmod.cmod_filter_regmatch.regprog = NULL;
4409 undo_cmdmod(&cmdmod);
4410 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4411 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
4412 break;
4413
4414 case ISN_UNPACK:
4415 {
4416 int count = iptr->isn_arg.unpack.unp_count;
4417 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4418 list_T *l;
4419 listitem_T *li;
4420 int i;
4421
4422 // Check there is a valid list to unpack.
4423 tv = STACK_TV_BOT(-1);
4424 if (tv->v_type != VAR_LIST)
4425 {
4426 SOURCING_LNUM = iptr->isn_lnum;
4427 emsg(_(e_for_argument_must_be_sequence_of_lists));
4428 goto on_error;
4429 }
4430 l = tv->vval.v_list;
4431 if (l == NULL
4432 || l->lv_len < (semicolon ? count - 1 : count))
4433 {
4434 SOURCING_LNUM = iptr->isn_lnum;
4435 emsg(_(e_list_value_does_not_have_enough_items));
4436 goto on_error;
4437 }
4438 else if (!semicolon && l->lv_len > count)
4439 {
4440 SOURCING_LNUM = iptr->isn_lnum;
4441 emsg(_(e_list_value_has_more_items_than_targets));
4442 goto on_error;
4443 }
4444
4445 CHECK_LIST_MATERIALIZE(l);
4446 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
4447 goto theend;
4448 ectx->ec_stack.ga_len += count - 1;
4449
4450 // Variable after semicolon gets a list with the remaining
4451 // items.
4452 if (semicolon)
4453 {
4454 list_T *rem_list =
4455 list_alloc_with_items(l->lv_len - count + 1);
4456
4457 if (rem_list == NULL)
4458 goto theend;
4459 tv = STACK_TV_BOT(-count);
4460 tv->vval.v_list = rem_list;
4461 ++rem_list->lv_refcount;
4462 tv->v_lock = 0;
4463 li = l->lv_first;
4464 for (i = 0; i < count - 1; ++i)
4465 li = li->li_next;
4466 for (i = 0; li != NULL; ++i)
4467 {
4468 list_set_item(rem_list, i, &li->li_tv);
4469 li = li->li_next;
4470 }
4471 --count;
4472 }
4473
4474 // Produce the values in reverse order, first item last.
4475 li = l->lv_first;
4476 for (i = 0; i < count; ++i)
4477 {
4478 tv = STACK_TV_BOT(-i - 1);
4479 copy_tv(&li->li_tv, tv);
4480 li = li->li_next;
4481 }
4482
4483 list_unref(l);
4484 }
4485 break;
4486
4487 case ISN_PROF_START:
4488 case ISN_PROF_END:
4489 {
4490 #ifdef FEAT_PROFILE
4491 funccall_T cookie;
4492 ufunc_T *cur_ufunc =
4493 (((dfunc_T *)def_functions.ga_data)
4494 + ectx->ec_dfunc_idx)->df_ufunc;
4495
4496 cookie.func = cur_ufunc;
4497 if (iptr->isn_type == ISN_PROF_START)
4498 {
4499 func_line_start(&cookie, iptr->isn_lnum);
4500 // if we get here the instruction is executed
4501 func_line_exec(&cookie);
4502 }
4503 else
4504 func_line_end(&cookie);
4505 #endif
4506 }
4507 break;
4508
4509 case ISN_DEBUG:
4510 handle_debug(iptr, ectx);
4511 break;
4512
4513 case ISN_SHUFFLE:
4514 {
4515 typval_T tmp_tv;
4516 int item = iptr->isn_arg.shuffle.shfl_item;
4517 int up = iptr->isn_arg.shuffle.shfl_up;
4518
4519 tmp_tv = *STACK_TV_BOT(-item);
4520 for ( ; up > 0 && item > 1; --up)
4521 {
4522 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4523 --item;
4524 }
4525 *STACK_TV_BOT(-item) = tmp_tv;
4526 }
4527 break;
4528
4529 case ISN_DROP:
4530 --ectx->ec_stack.ga_len;
4531 clear_tv(STACK_TV_BOT(0));
4532 ectx->ec_where.wt_index = 0;
4533 ectx->ec_where.wt_variable = FALSE;
4534 break;
4535 }
4536 continue;
4537
4538 func_return:
4539 // Restore previous function. If the frame pointer is where we started
4540 // then there is none and we are done.
4541 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
4542 goto done;
4543
4544 if (func_return(ectx) == FAIL)
4545 // only fails when out of memory
4546 goto theend;
4547 continue;
4548
4549 on_error:
4550 // Jump here for an error that does not require aborting execution.
4551 // If "emsg_silent" is set then ignore the error, unless it was set
4552 // when calling the function.
4553 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
4554 && emsg_silent && did_emsg_def == 0)
4555 {
4556 // If a sequence of instructions causes an error while ":silent!"
4557 // was used, restore the stack length and jump ahead to restoring
4558 // the cmdmod.
4559 if (ectx->ec_funclocal.floc_restore_cmdmod)
4560 {
4561 while (ectx->ec_stack.ga_len
4562 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
4563 {
4564 --ectx->ec_stack.ga_len;
4565 clear_tv(STACK_TV_BOT(0));
4566 }
4567 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4568 ++ectx->ec_iidx;
4569 }
4570 continue;
4571 }
4572 on_fatal_error:
4573 // Jump here for an error that messes up the stack.
4574 // If we are not inside a try-catch started here, abort execution.
4575 if (trylevel <= ectx->ec_trylevel_at_start)
4576 goto theend;
4577 }
4578
4579 done:
4580 ret = OK;
4581 theend:
4582 dict_stack_clear(dict_stack_len_at_start);
4583 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4584 return ret;
4585 }
4586
4587 /*
4588 * Execute the instructions from a VAR_INSTR typeval and put the result in
4589 * "rettv".
4590 * Return OK or FAIL.
4591 */
4592 int
exe_typval_instr(typval_T * tv,typval_T * rettv)4593 exe_typval_instr(typval_T *tv, typval_T *rettv)
4594 {
4595 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4596 isn_T *save_instr = ectx->ec_instr;
4597 int save_iidx = ectx->ec_iidx;
4598 int res;
4599
4600 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4601 res = exec_instructions(ectx);
4602 if (res == OK)
4603 {
4604 *rettv = *STACK_TV_BOT(-1);
4605 --ectx->ec_stack.ga_len;
4606 }
4607
4608 ectx->ec_instr = save_instr;
4609 ectx->ec_iidx = save_iidx;
4610
4611 return res;
4612 }
4613
4614 /*
4615 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4616 * "substitute_instr".
4617 */
4618 char_u *
exe_substitute_instr(void)4619 exe_substitute_instr(void)
4620 {
4621 ectx_T *ectx = substitute_instr->subs_ectx;
4622 isn_T *save_instr = ectx->ec_instr;
4623 int save_iidx = ectx->ec_iidx;
4624 char_u *res;
4625
4626 ectx->ec_instr = substitute_instr->subs_instr;
4627 if (exec_instructions(ectx) == OK)
4628 {
4629 typval_T *tv = STACK_TV_BOT(-1);
4630
4631 res = typval2string(tv, TRUE);
4632 --ectx->ec_stack.ga_len;
4633 clear_tv(tv);
4634 }
4635 else
4636 {
4637 substitute_instr->subs_status = FAIL;
4638 res = vim_strsave((char_u *)"");
4639 }
4640
4641 ectx->ec_instr = save_instr;
4642 ectx->ec_iidx = save_iidx;
4643
4644 return res;
4645 }
4646
4647 /*
4648 * Call a "def" function from old Vim script.
4649 * Return OK or FAIL.
4650 */
4651 int
call_def_function(ufunc_T * ufunc,int argc_arg,typval_T * argv,partial_T * partial,typval_T * rettv)4652 call_def_function(
4653 ufunc_T *ufunc,
4654 int argc_arg, // nr of arguments
4655 typval_T *argv, // arguments
4656 partial_T *partial, // optional partial for context
4657 typval_T *rettv) // return value
4658 {
4659 ectx_T ectx; // execution context
4660 int argc = argc_arg;
4661 typval_T *tv;
4662 int idx;
4663 int ret = FAIL;
4664 int defcount = ufunc->uf_args.ga_len - argc;
4665 sctx_T save_current_sctx = current_sctx;
4666 int did_emsg_before = did_emsg_cumul + did_emsg;
4667 int save_suppress_errthrow = suppress_errthrow;
4668 msglist_T **saved_msg_list = NULL;
4669 msglist_T *private_msg_list = NULL;
4670 int save_emsg_silent_def = emsg_silent_def;
4671 int save_did_emsg_def = did_emsg_def;
4672 int orig_funcdepth;
4673 int orig_nesting_level = ex_nesting_level;
4674
4675 // Get pointer to item in the stack.
4676 #undef STACK_TV
4677 #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4678
4679 // Get pointer to item at the bottom of the stack, -1 is the bottom.
4680 #undef STACK_TV_BOT
4681 #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4682
4683 // Get pointer to a local variable on the stack. Negative for arguments.
4684 #undef STACK_TV_VAR
4685 #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4686
4687 // Update uf_has_breakpoint if needed.
4688 update_has_breakpoint(ufunc);
4689
4690 if (ufunc->uf_def_status == UF_NOT_COMPILED
4691 || ufunc->uf_def_status == UF_COMPILE_ERROR
4692 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4693 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
4694 == FAIL))
4695 {
4696 if (did_emsg_cumul + did_emsg == did_emsg_before)
4697 semsg(_(e_function_is_not_compiled_str),
4698 printable_func_name(ufunc));
4699 return FAIL;
4700 }
4701
4702 {
4703 // Check the function was really compiled.
4704 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4705 + ufunc->uf_dfunc_idx;
4706 if (INSTRUCTIONS(dfunc) == NULL)
4707 {
4708 iemsg("using call_def_function() on not compiled function");
4709 return FAIL;
4710 }
4711 }
4712
4713 // If depth of calling is getting too high, don't execute the function.
4714 orig_funcdepth = funcdepth_get();
4715 if (funcdepth_increment() == FAIL)
4716 return FAIL;
4717
4718 CLEAR_FIELD(ectx);
4719 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4720 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4721 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
4722 {
4723 funcdepth_decrement();
4724 return FAIL;
4725 }
4726 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4727 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4728 ectx.ec_did_emsg_before = did_emsg_before;
4729 ++ex_nesting_level;
4730
4731 idx = argc - ufunc->uf_args.ga_len;
4732 if (idx > 0 && ufunc->uf_va_name == NULL)
4733 {
4734 if (idx == 1)
4735 emsg(_(e_one_argument_too_many));
4736 else
4737 semsg(_(e_nr_arguments_too_many), idx);
4738 goto failed_early;
4739 }
4740 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4741 if (idx < 0)
4742 {
4743 if (idx == -1)
4744 emsg(_(e_one_argument_too_few));
4745 else
4746 semsg(_(e_nr_arguments_too_few), -idx);
4747 goto failed_early;
4748 }
4749
4750 // Put arguments on the stack, but no more than what the function expects.
4751 // A lambda can be called with more arguments than it uses.
4752 for (idx = 0; idx < argc
4753 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4754 ++idx)
4755 {
4756 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4757 && argv[idx].v_type == VAR_SPECIAL
4758 && argv[idx].vval.v_number == VVAL_NONE)
4759 {
4760 // Use the default value.
4761 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4762 }
4763 else
4764 {
4765 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4766 && check_typval_arg_type(
4767 ufunc->uf_arg_types[idx], &argv[idx],
4768 NULL, idx + 1) == FAIL)
4769 goto failed_early;
4770 copy_tv(&argv[idx], STACK_TV_BOT(0));
4771 }
4772 ++ectx.ec_stack.ga_len;
4773 }
4774
4775 // Turn varargs into a list. Empty list if no args.
4776 if (ufunc->uf_va_name != NULL)
4777 {
4778 int vararg_count = argc - ufunc->uf_args.ga_len;
4779
4780 if (vararg_count < 0)
4781 vararg_count = 0;
4782 else
4783 argc -= vararg_count;
4784 if (exe_newlist(vararg_count, &ectx) == FAIL)
4785 goto failed_early;
4786
4787 // Check the type of the list items.
4788 tv = STACK_TV_BOT(-1);
4789 if (ufunc->uf_va_type != NULL
4790 && ufunc->uf_va_type != &t_list_any
4791 && ufunc->uf_va_type->tt_member != &t_any
4792 && tv->vval.v_list != NULL)
4793 {
4794 type_T *expected = ufunc->uf_va_type->tt_member;
4795 listitem_T *li = tv->vval.v_list->lv_first;
4796
4797 for (idx = 0; idx < vararg_count; ++idx)
4798 {
4799 if (check_typval_arg_type(expected, &li->li_tv,
4800 NULL, argc + idx + 1) == FAIL)
4801 goto failed_early;
4802 li = li->li_next;
4803 }
4804 }
4805
4806 if (defcount > 0)
4807 // Move varargs list to below missing default arguments.
4808 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4809 --ectx.ec_stack.ga_len;
4810 }
4811
4812 // Make space for omitted arguments, will store default value below.
4813 // Any varargs list goes after them.
4814 if (defcount > 0)
4815 for (idx = 0; idx < defcount; ++idx)
4816 {
4817 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4818 ++ectx.ec_stack.ga_len;
4819 }
4820 if (ufunc->uf_va_name != NULL)
4821 ++ectx.ec_stack.ga_len;
4822
4823 // Frame pointer points to just after arguments.
4824 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4825 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4826
4827 {
4828 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4829 + ufunc->uf_dfunc_idx;
4830 ufunc_T *base_ufunc = dfunc->df_ufunc;
4831
4832 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4833 // by copy_func().
4834 if (partial != NULL || base_ufunc->uf_partial != NULL)
4835 {
4836 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4837 if (ectx.ec_outer_ref == NULL)
4838 goto failed_early;
4839 if (partial != NULL)
4840 {
4841 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4842 {
4843 if (current_ectx->ec_outer_ref != NULL
4844 && current_ectx->ec_outer_ref->or_outer != NULL)
4845 ectx.ec_outer_ref->or_outer =
4846 current_ectx->ec_outer_ref->or_outer;
4847 }
4848 else
4849 {
4850 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4851 ++partial->pt_refcount;
4852 ectx.ec_outer_ref->or_partial = partial;
4853 }
4854 }
4855 else
4856 {
4857 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4858 ++base_ufunc->uf_partial->pt_refcount;
4859 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4860 }
4861 }
4862 }
4863
4864 // dummy frame entries
4865 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4866 {
4867 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4868 ++ectx.ec_stack.ga_len;
4869 }
4870
4871 {
4872 // Reserve space for local variables and any closure reference count.
4873 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4874 + ufunc->uf_dfunc_idx;
4875
4876 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4877 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4878 ectx.ec_stack.ga_len += dfunc->df_varcount;
4879 if (dfunc->df_has_closure)
4880 {
4881 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4882 STACK_TV_VAR(idx)->vval.v_number = 0;
4883 ++ectx.ec_stack.ga_len;
4884 }
4885
4886 ectx.ec_instr = INSTRUCTIONS(dfunc);
4887 }
4888
4889 // Following errors are in the function, not the caller.
4890 // Commands behave like vim9script.
4891 estack_push_ufunc(ufunc, 1);
4892 current_sctx = ufunc->uf_script_ctx;
4893 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4894
4895 // Use a specific location for storing error messages to be converted to an
4896 // exception.
4897 saved_msg_list = msg_list;
4898 msg_list = &private_msg_list;
4899
4900 // Do turn errors into exceptions.
4901 suppress_errthrow = FALSE;
4902
4903 // Do not delete the function while executing it.
4904 ++ufunc->uf_calls;
4905
4906 // When ":silent!" was used before calling then we still abort the
4907 // function. If ":silent!" is used in the function then we don't.
4908 emsg_silent_def = emsg_silent;
4909 did_emsg_def = 0;
4910
4911 ectx.ec_where.wt_index = 0;
4912 ectx.ec_where.wt_variable = FALSE;
4913
4914 // Execute the instructions until done.
4915 ret = exec_instructions(&ectx);
4916 if (ret == OK)
4917 {
4918 // function finished, get result from the stack.
4919 if (ufunc->uf_ret_type == &t_void)
4920 {
4921 rettv->v_type = VAR_VOID;
4922 }
4923 else
4924 {
4925 tv = STACK_TV_BOT(-1);
4926 *rettv = *tv;
4927 tv->v_type = VAR_UNKNOWN;
4928 }
4929 }
4930
4931 // When failed need to unwind the call stack.
4932 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4933 func_return(&ectx);
4934
4935 // Deal with any remaining closures, they may be in use somewhere.
4936 if (ectx.ec_funcrefs.ga_len > 0)
4937 {
4938 handle_closure_in_use(&ectx, FALSE);
4939 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4940 }
4941
4942 estack_pop();
4943 current_sctx = save_current_sctx;
4944
4945 // TODO: when is it safe to delete the function if it is no longer used?
4946 --ufunc->uf_calls;
4947
4948 if (*msg_list != NULL && saved_msg_list != NULL)
4949 {
4950 msglist_T **plist = saved_msg_list;
4951
4952 // Append entries from the current msg_list (uncaught exceptions) to
4953 // the saved msg_list.
4954 while (*plist != NULL)
4955 plist = &(*plist)->next;
4956
4957 *plist = *msg_list;
4958 }
4959 msg_list = saved_msg_list;
4960
4961 if (ectx.ec_funclocal.floc_restore_cmdmod)
4962 {
4963 cmdmod.cmod_filter_regmatch.regprog = NULL;
4964 undo_cmdmod(&cmdmod);
4965 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
4966 }
4967 emsg_silent_def = save_emsg_silent_def;
4968 did_emsg_def += save_did_emsg_def;
4969
4970 failed_early:
4971 // Free all local variables, but not arguments.
4972 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4973 clear_tv(STACK_TV(idx));
4974 ex_nesting_level = orig_nesting_level;
4975
4976 vim_free(ectx.ec_stack.ga_data);
4977 vim_free(ectx.ec_trystack.ga_data);
4978 if (ectx.ec_outer_ref != NULL)
4979 {
4980 if (ectx.ec_outer_ref->or_outer_allocated)
4981 vim_free(ectx.ec_outer_ref->or_outer);
4982 partial_unref(ectx.ec_outer_ref->or_partial);
4983 vim_free(ectx.ec_outer_ref);
4984 }
4985
4986 // Not sure if this is necessary.
4987 suppress_errthrow = save_suppress_errthrow;
4988
4989 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
4990 && !need_rethrow)
4991 semsg(_(e_unknown_error_while_executing_str),
4992 printable_func_name(ufunc));
4993 funcdepth_restore(orig_funcdepth);
4994 return ret;
4995 }
4996
4997 /*
4998 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4999 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5000 * "pfx" is prefixed to every line.
5001 */
5002 static void
list_instructions(char * pfx,isn_T * instr,int instr_count,ufunc_T * ufunc)5003 list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5004 {
5005 int line_idx = 0;
5006 int prev_current = 0;
5007 int current;
5008 int def_arg_idx = 0;
5009
5010 for (current = 0; current < instr_count; ++current)
5011 {
5012 isn_T *iptr = &instr[current];
5013 char *line;
5014
5015 if (ufunc != NULL)
5016 {
5017 while (line_idx < iptr->isn_lnum
5018 && line_idx < ufunc->uf_lines.ga_len)
5019 {
5020 if (current > prev_current)
5021 {
5022 msg_puts("\n\n");
5023 prev_current = current;
5024 }
5025 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5026 if (line != NULL)
5027 msg(line);
5028 }
5029 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5030 {
5031 int first_def_arg = ufunc->uf_args.ga_len
5032 - ufunc->uf_def_args.ga_len;
5033
5034 if (def_arg_idx > 0)
5035 msg_puts("\n\n");
5036 msg_start();
5037 msg_puts(" ");
5038 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5039 first_def_arg + def_arg_idx]);
5040 msg_puts(" = ");
5041 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5042 msg_clr_eos();
5043 msg_end();
5044 }
5045 }
5046
5047 switch (iptr->isn_type)
5048 {
5049 case ISN_EXEC:
5050 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5051 break;
5052 case ISN_EXEC_SPLIT:
5053 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5054 break;
5055 case ISN_LEGACY_EVAL:
5056 smsg("%s%4d EVAL legacy %s", pfx, current,
5057 iptr->isn_arg.string);
5058 break;
5059 case ISN_REDIRSTART:
5060 smsg("%s%4d REDIR", pfx, current);
5061 break;
5062 case ISN_REDIREND:
5063 smsg("%s%4d REDIR END%s", pfx, current,
5064 iptr->isn_arg.number ? " append" : "");
5065 break;
5066 case ISN_CEXPR_AUCMD:
5067 #ifdef FEAT_QUICKFIX
5068 smsg("%s%4d CEXPR pre %s", pfx, current,
5069 cexpr_get_auname(iptr->isn_arg.number));
5070 #endif
5071 break;
5072 case ISN_CEXPR_CORE:
5073 #ifdef FEAT_QUICKFIX
5074 {
5075 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5076
5077 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5078 cexpr_get_auname(cer->cer_cmdidx),
5079 cer->cer_forceit ? "!" : "",
5080 cer->cer_cmdline);
5081 }
5082 #endif
5083 break;
5084 case ISN_INSTR:
5085 {
5086 smsg("%s%4d INSTR", pfx, current);
5087 list_instructions(" ", iptr->isn_arg.instr,
5088 INT_MAX, NULL);
5089 msg(" -------------");
5090 }
5091 break;
5092 case ISN_SUBSTITUTE:
5093 {
5094 subs_T *subs = &iptr->isn_arg.subs;
5095
5096 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5097 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5098 msg(" -------------");
5099 }
5100 break;
5101 case ISN_EXECCONCAT:
5102 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5103 (varnumber_T)iptr->isn_arg.number);
5104 break;
5105 case ISN_ECHO:
5106 {
5107 echo_T *echo = &iptr->isn_arg.echo;
5108
5109 smsg("%s%4d %s %d", pfx, current,
5110 echo->echo_with_white ? "ECHO" : "ECHON",
5111 echo->echo_count);
5112 }
5113 break;
5114 case ISN_EXECUTE:
5115 smsg("%s%4d EXECUTE %lld", pfx, current,
5116 (varnumber_T)(iptr->isn_arg.number));
5117 break;
5118 case ISN_ECHOMSG:
5119 smsg("%s%4d ECHOMSG %lld", pfx, current,
5120 (varnumber_T)(iptr->isn_arg.number));
5121 break;
5122 case ISN_ECHOCONSOLE:
5123 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5124 (varnumber_T)(iptr->isn_arg.number));
5125 break;
5126 case ISN_ECHOERR:
5127 smsg("%s%4d ECHOERR %lld", pfx, current,
5128 (varnumber_T)(iptr->isn_arg.number));
5129 break;
5130 case ISN_LOAD:
5131 {
5132 if (iptr->isn_arg.number < 0)
5133 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5134 (varnumber_T)(iptr->isn_arg.number
5135 + STACK_FRAME_SIZE));
5136 else
5137 smsg("%s%4d LOAD $%lld", pfx, current,
5138 (varnumber_T)(iptr->isn_arg.number));
5139 }
5140 break;
5141 case ISN_LOADOUTER:
5142 {
5143 if (iptr->isn_arg.number < 0)
5144 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5145 iptr->isn_arg.outer.outer_depth,
5146 iptr->isn_arg.outer.outer_idx
5147 + STACK_FRAME_SIZE);
5148 else
5149 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5150 iptr->isn_arg.outer.outer_depth,
5151 iptr->isn_arg.outer.outer_idx);
5152 }
5153 break;
5154 case ISN_LOADV:
5155 smsg("%s%4d LOADV v:%s", pfx, current,
5156 get_vim_var_name(iptr->isn_arg.number));
5157 break;
5158 case ISN_LOADSCRIPT:
5159 {
5160 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5161 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5162 svar_T *sv;
5163
5164 sv = get_script_svar(sref, -1);
5165 if (sv == NULL)
5166 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5167 pfx, current, si->sn_name);
5168 else
5169 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
5170 sv->sv_name,
5171 sref->sref_idx,
5172 si->sn_name);
5173 }
5174 break;
5175 case ISN_LOADS:
5176 {
5177 scriptitem_T *si = SCRIPT_ITEM(
5178 iptr->isn_arg.loadstore.ls_sid);
5179
5180 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5181 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5182 }
5183 break;
5184 case ISN_LOADAUTO:
5185 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5186 break;
5187 case ISN_LOADG:
5188 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5189 break;
5190 case ISN_LOADB:
5191 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5192 break;
5193 case ISN_LOADW:
5194 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5195 break;
5196 case ISN_LOADT:
5197 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5198 break;
5199 case ISN_LOADGDICT:
5200 smsg("%s%4d LOAD g:", pfx, current);
5201 break;
5202 case ISN_LOADBDICT:
5203 smsg("%s%4d LOAD b:", pfx, current);
5204 break;
5205 case ISN_LOADWDICT:
5206 smsg("%s%4d LOAD w:", pfx, current);
5207 break;
5208 case ISN_LOADTDICT:
5209 smsg("%s%4d LOAD t:", pfx, current);
5210 break;
5211 case ISN_LOADOPT:
5212 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5213 break;
5214 case ISN_LOADENV:
5215 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5216 break;
5217 case ISN_LOADREG:
5218 smsg("%s%4d LOADREG @%c", pfx, current,
5219 (int)(iptr->isn_arg.number));
5220 break;
5221
5222 case ISN_STORE:
5223 if (iptr->isn_arg.number < 0)
5224 smsg("%s%4d STORE arg[%lld]", pfx, current,
5225 iptr->isn_arg.number + STACK_FRAME_SIZE);
5226 else
5227 smsg("%s%4d STORE $%lld", pfx, current,
5228 iptr->isn_arg.number);
5229 break;
5230 case ISN_STOREOUTER:
5231 {
5232 if (iptr->isn_arg.number < 0)
5233 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5234 iptr->isn_arg.outer.outer_depth,
5235 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5236 else
5237 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5238 iptr->isn_arg.outer.outer_depth,
5239 iptr->isn_arg.outer.outer_idx);
5240 }
5241 break;
5242 case ISN_STOREV:
5243 smsg("%s%4d STOREV v:%s", pfx, current,
5244 get_vim_var_name(iptr->isn_arg.number));
5245 break;
5246 case ISN_STOREAUTO:
5247 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5248 break;
5249 case ISN_STOREG:
5250 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5251 break;
5252 case ISN_STOREB:
5253 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5254 break;
5255 case ISN_STOREW:
5256 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5257 break;
5258 case ISN_STORET:
5259 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5260 break;
5261 case ISN_STORES:
5262 {
5263 scriptitem_T *si = SCRIPT_ITEM(
5264 iptr->isn_arg.loadstore.ls_sid);
5265
5266 smsg("%s%4d STORES %s in %s", pfx, current,
5267 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5268 }
5269 break;
5270 case ISN_STORESCRIPT:
5271 {
5272 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5273 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5274 svar_T *sv;
5275
5276 sv = get_script_svar(sref, -1);
5277 if (sv == NULL)
5278 smsg("%s%4d STORESCRIPT [deleted] in %s",
5279 pfx, current, si->sn_name);
5280 else
5281 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
5282 sv->sv_name,
5283 sref->sref_idx,
5284 si->sn_name);
5285 }
5286 break;
5287 case ISN_STOREOPT:
5288 smsg("%s%4d STOREOPT &%s", pfx, current,
5289 iptr->isn_arg.storeopt.so_name);
5290 break;
5291 case ISN_STOREENV:
5292 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5293 break;
5294 case ISN_STOREREG:
5295 smsg("%s%4d STOREREG @%c", pfx, current,
5296 (int)iptr->isn_arg.number);
5297 break;
5298 case ISN_STORENR:
5299 smsg("%s%4d STORE %lld in $%d", pfx, current,
5300 iptr->isn_arg.storenr.stnr_val,
5301 iptr->isn_arg.storenr.stnr_idx);
5302 break;
5303
5304 case ISN_STOREINDEX:
5305 smsg("%s%4d STOREINDEX %s", pfx, current,
5306 vartype_name(iptr->isn_arg.vartype));
5307 break;
5308
5309 case ISN_STORERANGE:
5310 smsg("%s%4d STORERANGE", pfx, current);
5311 break;
5312
5313 // constants
5314 case ISN_PUSHNR:
5315 smsg("%s%4d PUSHNR %lld", pfx, current,
5316 (varnumber_T)(iptr->isn_arg.number));
5317 break;
5318 case ISN_PUSHBOOL:
5319 case ISN_PUSHSPEC:
5320 smsg("%s%4d PUSH %s", pfx, current,
5321 get_var_special_name(iptr->isn_arg.number));
5322 break;
5323 case ISN_PUSHF:
5324 #ifdef FEAT_FLOAT
5325 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5326 #endif
5327 break;
5328 case ISN_PUSHS:
5329 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5330 break;
5331 case ISN_PUSHBLOB:
5332 {
5333 char_u *r;
5334 char_u numbuf[NUMBUFLEN];
5335 char_u *tofree;
5336
5337 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5338 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5339 vim_free(tofree);
5340 }
5341 break;
5342 case ISN_PUSHFUNC:
5343 {
5344 char *name = (char *)iptr->isn_arg.string;
5345
5346 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5347 name == NULL ? "[none]" : name);
5348 }
5349 break;
5350 case ISN_PUSHCHANNEL:
5351 #ifdef FEAT_JOB_CHANNEL
5352 {
5353 channel_T *channel = iptr->isn_arg.channel;
5354
5355 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5356 channel == NULL ? 0 : channel->ch_id);
5357 }
5358 #endif
5359 break;
5360 case ISN_PUSHJOB:
5361 #ifdef FEAT_JOB_CHANNEL
5362 {
5363 typval_T tv;
5364 char_u *name;
5365 char_u buf[NUMBUFLEN];
5366
5367 tv.v_type = VAR_JOB;
5368 tv.vval.v_job = iptr->isn_arg.job;
5369 name = job_to_string_buf(&tv, buf);
5370 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5371 }
5372 #endif
5373 break;
5374 case ISN_PUSHEXC:
5375 smsg("%s%4d PUSH v:exception", pfx, current);
5376 break;
5377 case ISN_UNLET:
5378 smsg("%s%4d UNLET%s %s", pfx, current,
5379 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5380 iptr->isn_arg.unlet.ul_name);
5381 break;
5382 case ISN_UNLETENV:
5383 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5384 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5385 iptr->isn_arg.unlet.ul_name);
5386 break;
5387 case ISN_UNLETINDEX:
5388 smsg("%s%4d UNLETINDEX", pfx, current);
5389 break;
5390 case ISN_UNLETRANGE:
5391 smsg("%s%4d UNLETRANGE", pfx, current);
5392 break;
5393 case ISN_LOCKUNLOCK:
5394 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5395 break;
5396 case ISN_LOCKCONST:
5397 smsg("%s%4d LOCKCONST", pfx, current);
5398 break;
5399 case ISN_NEWLIST:
5400 smsg("%s%4d NEWLIST size %lld", pfx, current,
5401 (varnumber_T)(iptr->isn_arg.number));
5402 break;
5403 case ISN_NEWDICT:
5404 smsg("%s%4d NEWDICT size %lld", pfx, current,
5405 (varnumber_T)(iptr->isn_arg.number));
5406 break;
5407
5408 // function call
5409 case ISN_BCALL:
5410 {
5411 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5412
5413 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5414 internal_func_name(cbfunc->cbf_idx),
5415 cbfunc->cbf_argcount);
5416 }
5417 break;
5418 case ISN_DCALL:
5419 {
5420 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5421 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5422 + cdfunc->cdf_idx;
5423
5424 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5425 printable_func_name(df->df_ufunc),
5426 cdfunc->cdf_argcount);
5427 }
5428 break;
5429 case ISN_UCALL:
5430 {
5431 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5432
5433 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5434 cufunc->cuf_name, cufunc->cuf_argcount);
5435 }
5436 break;
5437 case ISN_PCALL:
5438 {
5439 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5440
5441 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5442 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5443 }
5444 break;
5445 case ISN_PCALL_END:
5446 smsg("%s%4d PCALL end", pfx, current);
5447 break;
5448 case ISN_RETURN:
5449 smsg("%s%4d RETURN", pfx, current);
5450 break;
5451 case ISN_RETURN_VOID:
5452 smsg("%s%4d RETURN void", pfx, current);
5453 break;
5454 case ISN_FUNCREF:
5455 {
5456 funcref_T *funcref = &iptr->isn_arg.funcref;
5457 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5458 + funcref->fr_func;
5459
5460 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5461 }
5462 break;
5463
5464 case ISN_NEWFUNC:
5465 {
5466 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5467
5468 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5469 newfunc->nf_lambda, newfunc->nf_global);
5470 }
5471 break;
5472
5473 case ISN_DEF:
5474 {
5475 char_u *name = iptr->isn_arg.string;
5476
5477 smsg("%s%4d DEF %s", pfx, current,
5478 name == NULL ? (char_u *)"" : name);
5479 }
5480 break;
5481
5482 case ISN_JUMP:
5483 {
5484 char *when = "?";
5485
5486 switch (iptr->isn_arg.jump.jump_when)
5487 {
5488 case JUMP_ALWAYS:
5489 when = "JUMP";
5490 break;
5491 case JUMP_NEVER:
5492 iemsg("JUMP_NEVER should not be used");
5493 break;
5494 case JUMP_AND_KEEP_IF_TRUE:
5495 when = "JUMP_AND_KEEP_IF_TRUE";
5496 break;
5497 case JUMP_IF_FALSE:
5498 when = "JUMP_IF_FALSE";
5499 break;
5500 case JUMP_AND_KEEP_IF_FALSE:
5501 when = "JUMP_AND_KEEP_IF_FALSE";
5502 break;
5503 case JUMP_IF_COND_FALSE:
5504 when = "JUMP_IF_COND_FALSE";
5505 break;
5506 case JUMP_IF_COND_TRUE:
5507 when = "JUMP_IF_COND_TRUE";
5508 break;
5509 }
5510 smsg("%s%4d %s -> %d", pfx, current, when,
5511 iptr->isn_arg.jump.jump_where);
5512 }
5513 break;
5514
5515 case ISN_JUMP_IF_ARG_SET:
5516 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5517 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5518 iptr->isn_arg.jump.jump_where);
5519 break;
5520
5521 case ISN_FOR:
5522 {
5523 forloop_T *forloop = &iptr->isn_arg.forloop;
5524
5525 smsg("%s%4d FOR $%d -> %d", pfx, current,
5526 forloop->for_idx, forloop->for_end);
5527 }
5528 break;
5529
5530 case ISN_TRY:
5531 {
5532 try_T *try = &iptr->isn_arg.try;
5533
5534 if (try->try_ref->try_finally == 0)
5535 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5536 pfx, current,
5537 try->try_ref->try_catch,
5538 try->try_ref->try_endtry);
5539 else
5540 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5541 pfx, current,
5542 try->try_ref->try_catch,
5543 try->try_ref->try_finally,
5544 try->try_ref->try_endtry);
5545 }
5546 break;
5547 case ISN_CATCH:
5548 // TODO
5549 smsg("%s%4d CATCH", pfx, current);
5550 break;
5551 case ISN_TRYCONT:
5552 {
5553 trycont_T *trycont = &iptr->isn_arg.trycont;
5554
5555 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5556 trycont->tct_levels,
5557 trycont->tct_levels == 1 ? "" : "s",
5558 trycont->tct_where);
5559 }
5560 break;
5561 case ISN_FINALLY:
5562 smsg("%s%4d FINALLY", pfx, current);
5563 break;
5564 case ISN_ENDTRY:
5565 smsg("%s%4d ENDTRY", pfx, current);
5566 break;
5567 case ISN_THROW:
5568 smsg("%s%4d THROW", pfx, current);
5569 break;
5570
5571 // expression operations on number
5572 case ISN_OPNR:
5573 case ISN_OPFLOAT:
5574 case ISN_OPANY:
5575 {
5576 char *what;
5577 char *ins;
5578
5579 switch (iptr->isn_arg.op.op_type)
5580 {
5581 case EXPR_MULT: what = "*"; break;
5582 case EXPR_DIV: what = "/"; break;
5583 case EXPR_REM: what = "%"; break;
5584 case EXPR_SUB: what = "-"; break;
5585 case EXPR_ADD: what = "+"; break;
5586 default: what = "???"; break;
5587 }
5588 switch (iptr->isn_type)
5589 {
5590 case ISN_OPNR: ins = "OPNR"; break;
5591 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5592 case ISN_OPANY: ins = "OPANY"; break;
5593 default: ins = "???"; break;
5594 }
5595 smsg("%s%4d %s %s", pfx, current, ins, what);
5596 }
5597 break;
5598
5599 case ISN_COMPAREBOOL:
5600 case ISN_COMPARESPECIAL:
5601 case ISN_COMPARENR:
5602 case ISN_COMPAREFLOAT:
5603 case ISN_COMPARESTRING:
5604 case ISN_COMPAREBLOB:
5605 case ISN_COMPARELIST:
5606 case ISN_COMPAREDICT:
5607 case ISN_COMPAREFUNC:
5608 case ISN_COMPAREANY:
5609 {
5610 char *p;
5611 char buf[10];
5612 char *type;
5613
5614 switch (iptr->isn_arg.op.op_type)
5615 {
5616 case EXPR_EQUAL: p = "=="; break;
5617 case EXPR_NEQUAL: p = "!="; break;
5618 case EXPR_GREATER: p = ">"; break;
5619 case EXPR_GEQUAL: p = ">="; break;
5620 case EXPR_SMALLER: p = "<"; break;
5621 case EXPR_SEQUAL: p = "<="; break;
5622 case EXPR_MATCH: p = "=~"; break;
5623 case EXPR_IS: p = "is"; break;
5624 case EXPR_ISNOT: p = "isnot"; break;
5625 case EXPR_NOMATCH: p = "!~"; break;
5626 default: p = "???"; break;
5627 }
5628 STRCPY(buf, p);
5629 if (iptr->isn_arg.op.op_ic == TRUE)
5630 strcat(buf, "?");
5631 switch(iptr->isn_type)
5632 {
5633 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5634 case ISN_COMPARESPECIAL:
5635 type = "COMPARESPECIAL"; break;
5636 case ISN_COMPARENR: type = "COMPARENR"; break;
5637 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5638 case ISN_COMPARESTRING:
5639 type = "COMPARESTRING"; break;
5640 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5641 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5642 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5643 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5644 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5645 default: type = "???"; break;
5646 }
5647
5648 smsg("%s%4d %s %s", pfx, current, type, buf);
5649 }
5650 break;
5651
5652 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5653 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5654
5655 // expression operations
5656 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5657 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5658 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5659 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5660 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5661 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5662 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5663 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5664 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5665 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5666 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5667 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5668 pfx, current, iptr->isn_arg.number); break;
5669 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5670 iptr->isn_arg.getitem.gi_index,
5671 iptr->isn_arg.getitem.gi_with_op ?
5672 " with op" : ""); break;
5673 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5674 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5675 iptr->isn_arg.string); break;
5676 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5677 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5678
5679 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5680
5681 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5682 case ISN_CHECKTYPE:
5683 {
5684 checktype_T *ct = &iptr->isn_arg.type;
5685 char *tofree;
5686
5687 if (ct->ct_arg_idx == 0)
5688 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5689 type_name(ct->ct_type, &tofree),
5690 (int)ct->ct_off);
5691 else
5692 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5693 pfx, current,
5694 type_name(ct->ct_type, &tofree),
5695 (int)ct->ct_off,
5696 (int)ct->ct_arg_idx);
5697 vim_free(tofree);
5698 break;
5699 }
5700 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5701 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5702 iptr->isn_arg.checklen.cl_min_len);
5703 break;
5704 case ISN_SETTYPE:
5705 {
5706 char *tofree;
5707
5708 smsg("%s%4d SETTYPE %s", pfx, current,
5709 type_name(iptr->isn_arg.type.ct_type, &tofree));
5710 vim_free(tofree);
5711 break;
5712 }
5713 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
5714 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5715 smsg("%s%4d INVERT %d (!val)", pfx, current,
5716 iptr->isn_arg.tobool.offset);
5717 else
5718 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5719 iptr->isn_arg.tobool.offset);
5720 break;
5721 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
5722 (varnumber_T)(iptr->isn_arg.tostring.offset));
5723 break;
5724 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5725 pfx, current,
5726 (varnumber_T)(iptr->isn_arg.tostring.offset));
5727 break;
5728 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5729 iptr->isn_arg.string);
5730 break;
5731 case ISN_PUT:
5732 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5733 smsg("%s%4d PUT %c above range",
5734 pfx, current, iptr->isn_arg.put.put_regname);
5735 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5736 smsg("%s%4d PUT %c range",
5737 pfx, current, iptr->isn_arg.put.put_regname);
5738 else
5739 smsg("%s%4d PUT %c %ld", pfx, current,
5740 iptr->isn_arg.put.put_regname,
5741 (long)iptr->isn_arg.put.put_lnum);
5742 break;
5743
5744 // TODO: summarize modifiers
5745 case ISN_CMDMOD:
5746 {
5747 char_u *buf;
5748 size_t len = produce_cmdmods(
5749 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5750
5751 buf = alloc(len + 1);
5752 if (likely(buf != NULL))
5753 {
5754 (void)produce_cmdmods(
5755 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5756 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5757 vim_free(buf);
5758 }
5759 break;
5760 }
5761 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5762
5763 case ISN_PROF_START:
5764 smsg("%s%4d PROFILE START line %d", pfx, current,
5765 iptr->isn_lnum);
5766 break;
5767
5768 case ISN_PROF_END:
5769 smsg("%s%4d PROFILE END", pfx, current);
5770 break;
5771
5772 case ISN_DEBUG:
5773 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5774 iptr->isn_arg.debug.dbg_break_lnum + 1,
5775 iptr->isn_lnum,
5776 iptr->isn_arg.debug.dbg_var_names_len);
5777 break;
5778
5779 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5780 iptr->isn_arg.unpack.unp_count,
5781 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5782 break;
5783 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5784 iptr->isn_arg.shuffle.shfl_item,
5785 iptr->isn_arg.shuffle.shfl_up);
5786 break;
5787 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5788
5789 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5790 return;
5791 }
5792
5793 out_flush(); // output one line at a time
5794 ui_breakcheck();
5795 if (got_int)
5796 break;
5797 }
5798 }
5799
5800 /*
5801 * Handle command line completion for the :disassemble command.
5802 */
5803 void
set_context_in_disassemble_cmd(expand_T * xp,char_u * arg)5804 set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5805 {
5806 char_u *p;
5807
5808 // Default: expand user functions, "debug" and "profile"
5809 xp->xp_context = EXPAND_DISASSEMBLE;
5810 xp->xp_pattern = arg;
5811
5812 // first argument already typed: only user function names
5813 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5814 {
5815 xp->xp_context = EXPAND_USER_FUNC;
5816 xp->xp_pattern = skipwhite(p);
5817 }
5818 }
5819
5820 /*
5821 * Function given to ExpandGeneric() to obtain the list of :disassemble
5822 * arguments.
5823 */
5824 char_u *
get_disassemble_argument(expand_T * xp,int idx)5825 get_disassemble_argument(expand_T *xp, int idx)
5826 {
5827 if (idx == 0)
5828 return (char_u *)"debug";
5829 if (idx == 1)
5830 return (char_u *)"profile";
5831 return get_user_func_name(xp, idx - 2);
5832 }
5833
5834 /*
5835 * ":disassemble".
5836 * We don't really need this at runtime, but we do have tests that require it,
5837 * so always include this.
5838 */
5839 void
ex_disassemble(exarg_T * eap)5840 ex_disassemble(exarg_T *eap)
5841 {
5842 char_u *arg = eap->arg;
5843 char_u *fname;
5844 ufunc_T *ufunc;
5845 dfunc_T *dfunc;
5846 isn_T *instr;
5847 int instr_count;
5848 int is_global = FALSE;
5849 compiletype_T compile_type = CT_NONE;
5850
5851 if (STRNCMP(arg, "profile", 7) == 0)
5852 {
5853 compile_type = CT_PROFILE;
5854 arg = skipwhite(arg + 7);
5855 }
5856 else if (STRNCMP(arg, "debug", 5) == 0)
5857 {
5858 compile_type = CT_DEBUG;
5859 arg = skipwhite(arg + 5);
5860 }
5861
5862 if (STRNCMP(arg, "<lambda>", 8) == 0)
5863 {
5864 arg += 8;
5865 (void)getdigits(&arg);
5866 fname = vim_strnsave(eap->arg, arg - eap->arg);
5867 }
5868 else
5869 fname = trans_function_name(&arg, &is_global, FALSE,
5870 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
5871 if (fname == NULL)
5872 {
5873 semsg(_(e_invarg2), eap->arg);
5874 return;
5875 }
5876
5877 ufunc = find_func(fname, is_global, NULL);
5878 if (ufunc == NULL)
5879 {
5880 char_u *p = untrans_function_name(fname);
5881
5882 if (p != NULL)
5883 // Try again without making it script-local.
5884 ufunc = find_func(p, FALSE, NULL);
5885 }
5886 vim_free(fname);
5887 if (ufunc == NULL)
5888 {
5889 semsg(_(e_cannot_find_function_str), eap->arg);
5890 return;
5891 }
5892 if (func_needs_compiling(ufunc, compile_type)
5893 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
5894 return;
5895 if (ufunc->uf_def_status != UF_COMPILED)
5896 {
5897 semsg(_(e_function_is_not_compiled_str), eap->arg);
5898 return;
5899 }
5900 msg((char *)printable_func_name(ufunc));
5901
5902 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5903 switch (compile_type)
5904 {
5905 case CT_PROFILE:
5906 #ifdef FEAT_PROFILE
5907 instr = dfunc->df_instr_prof;
5908 instr_count = dfunc->df_instr_prof_count;
5909 break;
5910 #endif
5911 // FALLTHROUGH
5912 case CT_NONE:
5913 instr = dfunc->df_instr;
5914 instr_count = dfunc->df_instr_count;
5915 break;
5916 case CT_DEBUG:
5917 instr = dfunc->df_instr_debug;
5918 instr_count = dfunc->df_instr_debug_count;
5919 break;
5920 }
5921
5922 list_instructions("", instr, instr_count, ufunc);
5923 }
5924
5925 /*
5926 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
5927 * list, etc. Mostly like what JavaScript does, except that empty list and
5928 * empty dictionary are FALSE.
5929 */
5930 int
tv2bool(typval_T * tv)5931 tv2bool(typval_T *tv)
5932 {
5933 switch (tv->v_type)
5934 {
5935 case VAR_NUMBER:
5936 return tv->vval.v_number != 0;
5937 case VAR_FLOAT:
5938 #ifdef FEAT_FLOAT
5939 return tv->vval.v_float != 0.0;
5940 #else
5941 break;
5942 #endif
5943 case VAR_PARTIAL:
5944 return tv->vval.v_partial != NULL;
5945 case VAR_FUNC:
5946 case VAR_STRING:
5947 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5948 case VAR_LIST:
5949 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5950 case VAR_DICT:
5951 return tv->vval.v_dict != NULL
5952 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5953 case VAR_BOOL:
5954 case VAR_SPECIAL:
5955 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5956 case VAR_JOB:
5957 #ifdef FEAT_JOB_CHANNEL
5958 return tv->vval.v_job != NULL;
5959 #else
5960 break;
5961 #endif
5962 case VAR_CHANNEL:
5963 #ifdef FEAT_JOB_CHANNEL
5964 return tv->vval.v_channel != NULL;
5965 #else
5966 break;
5967 #endif
5968 case VAR_BLOB:
5969 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5970 case VAR_UNKNOWN:
5971 case VAR_ANY:
5972 case VAR_VOID:
5973 case VAR_INSTR:
5974 break;
5975 }
5976 return FALSE;
5977 }
5978
5979 void
emsg_using_string_as(typval_T * tv,int as_number)5980 emsg_using_string_as(typval_T *tv, int as_number)
5981 {
5982 semsg(_(as_number ? e_using_string_as_number_str
5983 : e_using_string_as_bool_str),
5984 tv->vval.v_string == NULL
5985 ? (char_u *)"" : tv->vval.v_string);
5986 }
5987
5988 /*
5989 * If "tv" is a string give an error and return FAIL.
5990 */
5991 int
check_not_string(typval_T * tv)5992 check_not_string(typval_T *tv)
5993 {
5994 if (tv->v_type == VAR_STRING)
5995 {
5996 emsg_using_string_as(tv, TRUE);
5997 clear_tv(tv);
5998 return FAIL;
5999 }
6000 return OK;
6001 }
6002
6003
6004 #endif // FEAT_EVAL
6005