blob: 89a33b5c6af938365d60ec31245adef0430790b8 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* 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.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010029 int tcd_catch_idx; // instruction of the first catch
Bram Moolenaarc150c092021-02-13 15:02:46 +010030 int tcd_finally_idx; // instruction of the finally block or :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010031 int tcd_caught; // catch block entered
Bram Moolenaarc150c092021-02-13 15:02:46 +010032 int tcd_cont; // :continue encountered, jump here
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033 int tcd_return; // when TRUE return from end of :finally
34} trycmd_T;
35
36
37// A stack is used to store:
38// - arguments passed to a :def function
39// - info about the calling function, to use when returning
40// - local variables
41// - temporary values
42//
43// In detail (FP == Frame Pointer):
44// arg1 first argument from caller (if present)
45// arg2 second argument from caller (if present)
46// extra_arg1 any missing optional argument default value
47// FP -> cur_func calling function
48// current previous instruction pointer
49// frame_ptr previous Frame Pointer
50// var1 space for local variable
51// var2 space for local variable
52// .... fixed space for max. number of local variables
53// temp temporary values
54// .... flexible space for temporary values (can grow big)
55
56/*
57 * Execution context.
58 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010059struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020061 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010062
Bram Moolenaar0186e582021-01-10 18:33:11 +010063 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010065 garray_T ec_trystack; // stack of trycmd_T values
66 int ec_in_catch; // when TRUE in catch or finally block
67
68 int ec_dfunc_idx; // current function index
69 isn_T *ec_instr; // array with instructions
70 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020071
72 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010073};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020076#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
Bram Moolenaar418f1df2020-08-12 21:34:49 +020078 void
79to_string_error(vartype_T vartype)
80{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020081 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020082}
83
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010085 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 */
87 static int
88ufunc_argcount(ufunc_T *ufunc)
89{
90 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
91}
92
93/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010094 * Set the instruction index, depending on omitted arguments, where the default
95 * values are to be computed. If all optional arguments are present, start
96 * with the function body.
97 * The expression evaluation is at the start of the instructions:
98 * 0 -> EVAL default1
99 * STORE arg[-2]
100 * 1 -> EVAL default2
101 * STORE arg[-1]
102 * 2 -> function body
103 */
104 static void
105init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
106{
107 if (ufunc->uf_def_args.ga_len == 0)
108 ectx->ec_iidx = 0;
109 else
110 {
111 int defcount = ufunc->uf_args.ga_len - argcount;
112
113 // If there is a varargs argument defcount can be negative, no defaults
114 // to evaluate then.
115 if (defcount < 0)
116 defcount = 0;
117 ectx->ec_iidx = ufunc->uf_def_arg_idx[
118 ufunc->uf_def_args.ga_len - defcount];
119 }
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * 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
127exe_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;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200140 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200141 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/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100153 * This adds a stack frame and sets the instruction pointer to the start of the
154 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100155 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100156 *
157 * Stack has:
158 * - current arguments (already there)
159 * - omitted optional argument (default values) added here
160 * - stack frame:
161 * - pointer to calling function
162 * - Index of next instruction in calling function
163 * - previous frame pointer
164 * - reserved space for local variables
165 */
166 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100167call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200169 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
171 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200172 int arg_to_add;
173 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200174 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200176 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177
178 if (dfunc->df_deleted)
179 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100180 // don't use ufunc->uf_name, it may have been freed
181 emsg_funcname(e_func_deleted,
182 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183 return FAIL;
184 }
185
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100186#ifdef FEAT_PROFILE
187 // Profiling might be enabled/disabled along the way. This should not
188 // fail, since the function was compiled before and toggling profiling
189 // doesn't change any errors.
190 if (func_needs_compiling(ufunc, PROFILING(ufunc))
191 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
192 == FAIL)
193 return FAIL;
194#endif
195
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200196 if (ufunc->uf_va_name != NULL)
197 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200199 // Stack at time of call with 2 varargs:
200 // normal_arg
201 // optional_arg
202 // vararg_1
203 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200204 // After creating the list:
205 // normal_arg
206 // optional_arg
207 // vararg-list
208 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200209 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200210 // After creating the list
211 // normal_arg
212 // (space for optional_arg)
213 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200214 vararg_count = argcount - ufunc->uf_args.ga_len;
215 if (vararg_count < 0)
216 vararg_count = 0;
217 else
218 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200219 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200220 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200221
222 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200223 }
224
Bram Moolenaarfe270812020-04-11 22:31:27 +0200225 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200226 if (arg_to_add < 0)
227 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200228 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200229 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200230 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200231 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200232 return FAIL;
233 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200234
235 // Reserve space for:
236 // - missing arguments
237 // - stack frame
238 // - local variables
239 // - if needed: a counter for number of closures created in
240 // ectx->ec_funcrefs.
241 varcount = dfunc->df_varcount + dfunc->df_has_closure;
242 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
243 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 return FAIL;
245
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100246 // If depth of calling is getting too high, don't execute the function.
247 if (funcdepth_increment() == FAIL)
248 return FAIL;
249
Bram Moolenaarfe270812020-04-11 22:31:27 +0200250 // Move the vararg-list to below the missing optional arguments.
251 if (vararg_count > 0 && arg_to_add > 0)
252 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100253
254 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200255 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200256 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200257 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258
259 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100260 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
261 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100262 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
263 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200264 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265
266 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200267 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200269 if (dfunc->df_has_closure)
270 {
271 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
272
273 tv->v_type = VAR_NUMBER;
274 tv->vval.v_number = 0;
275 }
276 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100278 if (pt != NULL || ufunc->uf_partial != NULL
279 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100280 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100281 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
282
283 if (outer == NULL)
284 return FAIL;
285 if (pt != NULL)
286 {
287 *outer = pt->pt_outer;
288 outer->out_up_is_copy = TRUE;
289 }
290 else if (ufunc->uf_partial != NULL)
291 {
292 *outer = ufunc->uf_partial->pt_outer;
293 outer->out_up_is_copy = TRUE;
294 }
295 else
296 {
297 outer->out_stack = &ectx->ec_stack;
298 outer->out_frame_idx = ectx->ec_frame_idx;
299 outer->out_up = ectx->ec_outer;
300 }
301 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100302 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100303 else
304 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 // Set execution state to the start of the called function.
307 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100308 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100309 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200310 if (entry != NULL)
311 {
312 // Set the script context to the script where the function was defined.
313 // TODO: save more than the SID?
314 entry->es_save_sid = current_sctx.sc_sid;
315 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
316 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100317
318 // Decide where to start execution, handles optional arguments.
319 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100320
321 return OK;
322}
323
324// Get pointer to item in the stack.
325#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
326
327/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200328 * Used when returning from a function: Check if any closure is still
329 * referenced. If so then move the arguments and variables to a separate piece
330 * of stack to be used when the closure is called.
331 * When "free_arguments" is TRUE the arguments are to be freed.
332 * Returns FAIL when out of memory.
333 */
334 static int
335handle_closure_in_use(ectx_T *ectx, int free_arguments)
336{
337 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
338 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200339 int argcount;
340 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200341 int idx;
342 typval_T *tv;
343 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200344 garray_T *gap = &ectx->ec_funcrefs;
345 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200346
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200347 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200348 return OK; // function was freed
349 if (dfunc->df_has_closure == 0)
350 return OK; // no closures
351 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
352 closure_count = tv->vval.v_number;
353 if (closure_count == 0)
354 return OK; // no funcrefs created
355
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200356 argcount = ufunc_argcount(dfunc->df_ufunc);
357 top = ectx->ec_frame_idx - argcount;
358
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200359 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200360 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200361 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200362 partial_T *pt;
363 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200364
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200365 if (off < 0)
366 continue; // count is off or already done
367 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200368 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200369 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200370 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200371 int i;
372
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200373 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200374 // unreferenced on return.
375 for (i = 0; i < dfunc->df_varcount; ++i)
376 {
377 typval_T *stv = STACK_TV(ectx->ec_frame_idx
378 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200379 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200380 --refcount;
381 }
382 if (refcount > 1)
383 {
384 closure_in_use = TRUE;
385 break;
386 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200387 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200388 }
389
390 if (closure_in_use)
391 {
392 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
393 typval_T *stack;
394
395 // A closure is using the arguments and/or local variables.
396 // Move them to the called function.
397 if (funcstack == NULL)
398 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200399 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
400 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200401 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
402 funcstack->fs_ga.ga_data = stack;
403 if (stack == NULL)
404 {
405 vim_free(funcstack);
406 return FAIL;
407 }
408
409 // Move or copy the arguments.
410 for (idx = 0; idx < argcount; ++idx)
411 {
412 tv = STACK_TV(top + idx);
413 if (free_arguments)
414 {
415 *(stack + idx) = *tv;
416 tv->v_type = VAR_UNKNOWN;
417 }
418 else
419 copy_tv(tv, stack + idx);
420 }
421 // Move the local variables.
422 for (idx = 0; idx < dfunc->df_varcount; ++idx)
423 {
424 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200425
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200426 // A partial created for a local function, that is also used as a
427 // local variable, has a reference count for the variable, thus
428 // will never go down to zero. When all these refcounts are one
429 // then the funcstack is unused. We need to count how many we have
430 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200431 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
432 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200433 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200434
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200435 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200436 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
437 gap->ga_len - closure_count + i])
438 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200439 }
440
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200441 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200442 tv->v_type = VAR_UNKNOWN;
443 }
444
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200445 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200446 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200447 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
448 - closure_count + idx];
449 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200450 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200451 ++funcstack->fs_refcount;
452 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100453 pt->pt_outer.out_stack = &funcstack->fs_ga;
454 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
455 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200456 }
457 }
458 }
459
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200460 for (idx = 0; idx < closure_count; ++idx)
461 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
462 - closure_count + idx]);
463 gap->ga_len -= closure_count;
464 if (gap->ga_len == 0)
465 ga_clear(gap);
466
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200467 return OK;
468}
469
470/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200471 * Called when a partial is freed or its reference count goes down to one. The
472 * funcstack may be the only reference to the partials in the local variables.
473 * Go over all of them, the funcref and can be freed if all partials
474 * referencing the funcstack have a reference count of one.
475 */
476 void
477funcstack_check_refcount(funcstack_T *funcstack)
478{
479 int i;
480 garray_T *gap = &funcstack->fs_ga;
481 int done = 0;
482
483 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
484 return;
485 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
486 {
487 typval_T *tv = ((typval_T *)gap->ga_data) + i;
488
489 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
490 && tv->vval.v_partial->pt_funcstack == funcstack
491 && tv->vval.v_partial->pt_refcount == 1)
492 ++done;
493 }
494 if (done == funcstack->fs_min_refcount)
495 {
496 typval_T *stack = gap->ga_data;
497
498 // All partials referencing the funcstack have a reference count of
499 // one, thus the funcstack is no longer of use.
500 for (i = 0; i < gap->ga_len; ++i)
501 clear_tv(stack + i);
502 vim_free(stack);
503 vim_free(funcstack);
504 }
505}
506
507/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 * Return from the current function.
509 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200510 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100511func_return(ectx_T *ectx)
512{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100514 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200515 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
516 + ectx->ec_dfunc_idx;
517 int argcount = ufunc_argcount(dfunc->df_ufunc);
518 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200519 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100520
521 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200522 entry = estack_pop();
523 if (entry != NULL)
524 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100525
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200526 if (handle_closure_in_use(ectx, TRUE) == FAIL)
527 return FAIL;
528
529 // Clear the arguments.
530 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
531 clear_tv(STACK_TV(idx));
532
533 // Clear local variables and temp values, but not the return value.
534 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100535 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100537
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100538 // The return value should be on top of the stack. However, when aborting
539 // it may not be there and ec_frame_idx is the top of the stack.
540 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100541 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100542 ret_idx = 0;
543
Bram Moolenaar0186e582021-01-10 18:33:11 +0100544 vim_free(ectx->ec_outer);
545
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100546 // Restore the previous frame.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100547 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx
548 + STACK_FRAME_FUNC_OFF)->vval.v_number;
549 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
550 + STACK_FRAME_IIDX_OFF)->vval.v_number;
551 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
552 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200553 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100554 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
555 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100557 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100558
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100559 if (ret_idx > 0)
560 {
561 // Reset the stack to the position before the call, with a spot for the
562 // return value, moved there from above the frame.
563 ectx->ec_stack.ga_len = top + 1;
564 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
565 }
566 else
567 // Reset the stack to the position before the call.
568 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200569
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100570 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200571 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572}
573
574#undef STACK_TV
575
576/*
577 * Prepare arguments and rettv for calling a builtin or user function.
578 */
579 static int
580call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
581{
582 int idx;
583 typval_T *tv;
584
585 // Move arguments from bottom of the stack to argvars[] and add terminator.
586 for (idx = 0; idx < argcount; ++idx)
587 argvars[idx] = *STACK_TV_BOT(idx - argcount);
588 argvars[argcount].v_type = VAR_UNKNOWN;
589
590 // Result replaces the arguments on the stack.
591 if (argcount > 0)
592 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200593 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 return FAIL;
595 else
596 ++ectx->ec_stack.ga_len;
597
598 // Default return value is zero.
599 tv = STACK_TV_BOT(-1);
600 tv->v_type = VAR_NUMBER;
601 tv->vval.v_number = 0;
602
603 return OK;
604}
605
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200606// Ugly global to avoid passing the execution context around through many
607// layers.
608static ectx_T *current_ectx = NULL;
609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610/*
611 * Call a builtin function by index.
612 */
613 static int
614call_bfunc(int func_idx, int argcount, ectx_T *ectx)
615{
616 typval_T argvars[MAX_FUNC_ARGS];
617 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100618 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200619 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620
621 if (call_prepare(argcount, argvars, ectx) == FAIL)
622 return FAIL;
623
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200624 // Call the builtin function. Set "current_ectx" so that when it
625 // recursively invokes call_def_function() a closure context can be set.
626 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200628 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629
630 // Clear the arguments.
631 for (idx = 0; idx < argcount; ++idx)
632 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200633
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100634 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200635 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 return OK;
637}
638
639/*
640 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100641 * If the function is compiled this will add a stack frame and set the
642 * instruction pointer at the start of the function.
643 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100644 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100645 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646 */
647 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100648call_ufunc(
649 ufunc_T *ufunc,
650 partial_T *pt,
651 int argcount,
652 ectx_T *ectx,
653 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654{
655 typval_T argvars[MAX_FUNC_ARGS];
656 funcexe_T funcexe;
657 int error;
658 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100659 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100660#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100661 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100662#else
663# define profiling FALSE
664#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665
Bram Moolenaarb2049902021-01-24 12:53:53 +0100666 if (func_needs_compiling(ufunc, profiling)
667 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200668 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200669 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100670 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100671 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100672 if (error != FCERR_UNKNOWN)
673 {
674 if (error == FCERR_TOOMANY)
675 semsg(_(e_toomanyarg), ufunc->uf_name);
676 else
677 semsg(_(e_toofewarg), ufunc->uf_name);
678 return FAIL;
679 }
680
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100681 // The function has been compiled, can call it quickly. For a function
682 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100683 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100684 if (iptr != NULL)
685 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100686 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100687 iptr->isn_type = ISN_DCALL;
688 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
689 iptr->isn_arg.dfunc.cdf_argcount = argcount;
690 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100691 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100692 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693
694 if (call_prepare(argcount, argvars, ectx) == FAIL)
695 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200696 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 funcexe.evaluate = TRUE;
698
699 // Call the user function. Result goes in last position on the stack.
700 // TODO: add selfdict if there is one
701 error = call_user_func_check(ufunc, argcount, argvars,
702 STACK_TV_BOT(-1), &funcexe, NULL);
703
704 // Clear the arguments.
705 for (idx = 0; idx < argcount; ++idx)
706 clear_tv(&argvars[idx]);
707
708 if (error != FCERR_NONE)
709 {
710 user_func_error(error, ufunc->uf_name);
711 return FAIL;
712 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100713 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200714 // Error other than from calling the function itself.
715 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 return OK;
717}
718
719/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200720 * Return TRUE if an error was given or CTRL-C was pressed.
721 */
722 static int
723vim9_aborting(int prev_called_emsg)
724{
725 return called_emsg > prev_called_emsg || got_int || did_throw;
726}
727
728/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 * Execute a function by "name".
730 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100731 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732 * Returns FAIL if not found without an error message.
733 */
734 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100735call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736{
737 ufunc_T *ufunc;
738
739 if (builtin_function(name, -1))
740 {
741 int func_idx = find_internal_func(name);
742
743 if (func_idx < 0)
744 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200745 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746 return FAIL;
747 return call_bfunc(func_idx, argcount, ectx);
748 }
749
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200750 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200751
752 if (ufunc == NULL)
753 {
754 int called_emsg_before = called_emsg;
755
756 if (script_autoload(name, TRUE))
757 // loaded a package, search for the function again
758 ufunc = find_func(name, FALSE, NULL);
759 if (vim9_aborting(called_emsg_before))
760 return FAIL; // bail out if loading the script caused an error
761 }
762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100764 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765
766 return FAIL;
767}
768
769 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200770call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200772 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200773 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100775 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776
777 if (tv->v_type == VAR_PARTIAL)
778 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200779 partial_T *pt = tv->vval.v_partial;
780 int i;
781
782 if (pt->pt_argc > 0)
783 {
784 // Make space for arguments from the partial, shift the "argcount"
785 // arguments up.
786 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
787 return FAIL;
788 for (i = 1; i <= argcount; ++i)
789 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
790 ectx->ec_stack.ga_len += pt->pt_argc;
791 argcount += pt->pt_argc;
792
793 // copy the arguments from the partial onto the stack
794 for (i = 0; i < pt->pt_argc; ++i)
795 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100797
798 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100799 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801 name = pt->pt_name;
802 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200803 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200805 if (name != NULL)
806 {
807 char_u fname_buf[FLEN_FIXED + 1];
808 char_u *tofree = NULL;
809 int error = FCERR_NONE;
810 char_u *fname;
811
812 // May need to translate <SNR>123_ to K_SNR.
813 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
814 if (error != FCERR_NONE)
815 res = FAIL;
816 else
817 res = call_by_name(fname, argcount, ectx, NULL);
818 vim_free(tofree);
819 }
820
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100821 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100822 {
823 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200824 semsg(_(e_unknownfunc),
825 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 return FAIL;
827 }
828 return OK;
829}
830
831/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200832 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
833 * TRUE.
834 */
835 static int
836error_if_locked(int lock, char *error)
837{
838 if (lock & (VAR_LOCKED | VAR_FIXED))
839 {
840 emsg(_(error));
841 return TRUE;
842 }
843 return FALSE;
844}
845
846/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100847 * Store "tv" in variable "name".
848 * This is for s: and g: variables.
849 */
850 static void
851store_var(char_u *name, typval_T *tv)
852{
853 funccal_entry_T entry;
854
855 save_funccal(&entry);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100856 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100857 restore_funccal();
858}
859
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100860/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100861 * Convert "tv" to a string.
862 * Return FAIL if not allowed.
863 */
864 static int
865do_2string(typval_T *tv, int is_2string_any)
866{
867 if (tv->v_type != VAR_STRING)
868 {
869 char_u *str;
870
871 if (is_2string_any)
872 {
873 switch (tv->v_type)
874 {
875 case VAR_SPECIAL:
876 case VAR_BOOL:
877 case VAR_NUMBER:
878 case VAR_FLOAT:
879 case VAR_BLOB: break;
880 default: to_string_error(tv->v_type);
881 return FAIL;
882 }
883 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100884 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100885 clear_tv(tv);
886 tv->v_type = VAR_STRING;
887 tv->vval.v_string = str;
888 }
889 return OK;
890}
891
892/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100893 * When the value of "sv" is a null list of dict, allocate it.
894 */
895 static void
896allocate_if_null(typval_T *tv)
897{
898 switch (tv->v_type)
899 {
900 case VAR_LIST:
901 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100902 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100903 break;
904 case VAR_DICT:
905 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100906 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100907 break;
908 default:
909 break;
910 }
911}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200912
Bram Moolenaare7525c52021-01-09 13:20:37 +0100913/*
914 * Return the character "str[index]" where "index" is the character index. If
915 * "index" is out of range NULL is returned.
916 */
917 char_u *
918char_from_string(char_u *str, varnumber_T index)
919{
920 size_t nbyte = 0;
921 varnumber_T nchar = index;
922 size_t slen;
923
924 if (str == NULL)
925 return NULL;
926 slen = STRLEN(str);
927
928 // do the same as for a list: a negative index counts from the end
929 if (index < 0)
930 {
931 int clen = 0;
932
933 for (nbyte = 0; nbyte < slen; ++clen)
934 nbyte += MB_CPTR2LEN(str + nbyte);
935 nchar = clen + index;
936 if (nchar < 0)
937 // unlike list: index out of range results in empty string
938 return NULL;
939 }
940
941 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
942 nbyte += MB_CPTR2LEN(str + nbyte);
943 if (nbyte >= slen)
944 return NULL;
945 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
946}
947
948/*
949 * Get the byte index for character index "idx" in string "str" with length
950 * "str_len".
951 * If going over the end return "str_len".
952 * If "idx" is negative count from the end, -1 is the last character.
953 * When going over the start return -1.
954 */
955 static long
956char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
957{
958 varnumber_T nchar = idx;
959 size_t nbyte = 0;
960
961 if (nchar >= 0)
962 {
963 while (nchar > 0 && nbyte < str_len)
964 {
965 nbyte += MB_CPTR2LEN(str + nbyte);
966 --nchar;
967 }
968 }
969 else
970 {
971 nbyte = str_len;
972 while (nchar < 0 && nbyte > 0)
973 {
974 --nbyte;
975 nbyte -= mb_head_off(str, str + nbyte);
976 ++nchar;
977 }
978 if (nchar < 0)
979 return -1;
980 }
981 return (long)nbyte;
982}
983
984/*
985 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +0100986 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +0100987 * Return NULL when the result is empty.
988 */
989 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +0100990string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100991{
992 long start_byte, end_byte;
993 size_t slen;
994
995 if (str == NULL)
996 return NULL;
997 slen = STRLEN(str);
998 start_byte = char_idx2byte(str, slen, first);
999 if (start_byte < 0)
1000 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001001 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001002 end_byte = (long)slen;
1003 else
1004 {
1005 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001006 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001007 // end index is inclusive
1008 end_byte += MB_CPTR2LEN(str + end_byte);
1009 }
1010
1011 if (start_byte >= (long)slen || end_byte <= start_byte)
1012 return NULL;
1013 return vim_strnsave(str + start_byte, end_byte - start_byte);
1014}
1015
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001016 static svar_T *
1017get_script_svar(scriptref_T *sref, ectx_T *ectx)
1018{
1019 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1020 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1021 + ectx->ec_dfunc_idx;
1022 svar_T *sv;
1023
1024 if (sref->sref_seq != si->sn_script_seq)
1025 {
1026 // The script was reloaded after the function was
1027 // compiled, the script_idx may not be valid.
1028 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1029 dfunc->df_ufunc->uf_name_exp);
1030 return NULL;
1031 }
1032 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1033 if (!equal_type(sv->sv_type, sref->sref_type))
1034 {
1035 emsg(_(e_script_variable_type_changed));
1036 return NULL;
1037 }
1038 return sv;
1039}
1040
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001041/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 * Execute a function by "name".
1043 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001044 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045 */
1046 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001047call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048{
Bram Moolenaared677f52020-08-12 16:38:10 +02001049 int called_emsg_before = called_emsg;
1050 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051
Bram Moolenaared677f52020-08-12 16:38:10 +02001052 res = call_by_name(name, argcount, ectx, iptr);
1053 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001055 dictitem_T *v;
1056
1057 v = find_var(name, NULL, FALSE);
1058 if (v == NULL)
1059 {
1060 semsg(_(e_unknownfunc), name);
1061 return FAIL;
1062 }
1063 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1064 {
1065 semsg(_(e_unknownfunc), name);
1066 return FAIL;
1067 }
1068 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001070 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071}
1072
1073/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001074 * When a function reference is used, fill a partial with the information
1075 * needed, especially when it is used as a closure.
1076 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001077 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001078fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1079{
1080 pt->pt_func = ufunc;
1081 pt->pt_refcount = 1;
1082
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001083 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001084 {
1085 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1086 + ectx->ec_dfunc_idx;
1087
1088 // The closure needs to find arguments and local
1089 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001090 pt->pt_outer.out_stack = &ectx->ec_stack;
1091 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1092 pt->pt_outer.out_up = ectx->ec_outer;
1093 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001094
1095 // If this function returns and the closure is still
1096 // being used, we need to make a copy of the context
1097 // (arguments and local variables). Store a reference
1098 // to the partial so we can handle that.
1099 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1100 {
1101 vim_free(pt);
1102 return FAIL;
1103 }
1104 // Extra variable keeps the count of closures created
1105 // in the current function call.
1106 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1107 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1108
1109 ((partial_T **)ectx->ec_funcrefs.ga_data)
1110 [ectx->ec_funcrefs.ga_len] = pt;
1111 ++pt->pt_refcount;
1112 ++ectx->ec_funcrefs.ga_len;
1113 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001114 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001115 return OK;
1116}
1117
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001118
Bram Moolenaarf112f302020-12-20 17:47:52 +01001119/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 * Call a "def" function from old Vim script.
1121 * Return OK or FAIL.
1122 */
1123 int
1124call_def_function(
1125 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001126 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001128 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 typval_T *rettv) // return value
1130{
1131 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001132 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001133 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 typval_T *tv;
1135 int idx;
1136 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001137 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001138 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001139 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001140 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001141 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001142 msglist_T **saved_msg_list = NULL;
1143 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001144 cmdmod_T save_cmdmod;
1145 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001146 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001147 int save_emsg_silent_def = emsg_silent_def;
1148 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001149 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001150 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001151 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152
1153// Get pointer to item in the stack.
1154#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1155
1156// Get pointer to item at the bottom of the stack, -1 is the bottom.
1157#undef STACK_TV_BOT
1158#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1159
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001160// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001161#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001163 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001164 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1165 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001166 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001167 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001168 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001169 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001170 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001171 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001172 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001173
Bram Moolenaar09689a02020-05-09 22:50:08 +02001174 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001175 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001176 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1177 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001178 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001179 {
1180 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001181 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001182 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001185 // If depth of calling is getting too high, don't execute the function.
1186 orig_funcdepth = funcdepth_get();
1187 if (funcdepth_increment() == FAIL)
1188 return FAIL;
1189
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001190 CLEAR_FIELD(ectx);
1191 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1192 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1193 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001194 {
1195 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001196 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001199 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001201 // Put arguments on the stack, but no more than what the function expects.
1202 // A lambda can be called with more arguments than it uses.
1203 for (idx = 0; idx < argc
1204 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1205 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001207 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001208 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001209 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001210 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 copy_tv(&argv[idx], STACK_TV_BOT(0));
1212 ++ectx.ec_stack.ga_len;
1213 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001214
1215 // Turn varargs into a list. Empty list if no args.
1216 if (ufunc->uf_va_name != NULL)
1217 {
1218 int vararg_count = argc - ufunc->uf_args.ga_len;
1219
1220 if (vararg_count < 0)
1221 vararg_count = 0;
1222 else
1223 argc -= vararg_count;
1224 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001225 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001226
1227 // Check the type of the list items.
1228 tv = STACK_TV_BOT(-1);
1229 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001230 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001231 && ufunc->uf_va_type->tt_member != &t_any
1232 && tv->vval.v_list != NULL)
1233 {
1234 type_T *expected = ufunc->uf_va_type->tt_member;
1235 listitem_T *li = tv->vval.v_list->lv_first;
1236
1237 for (idx = 0; idx < vararg_count; ++idx)
1238 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001239 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001240 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001241 goto failed_early;
1242 li = li->li_next;
1243 }
1244 }
1245
Bram Moolenaar23e03252020-04-12 22:22:31 +02001246 if (defcount > 0)
1247 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001248 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001249 --ectx.ec_stack.ga_len;
1250 }
1251
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001252 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001253 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001254 if (defcount > 0)
1255 for (idx = 0; idx < defcount; ++idx)
1256 {
1257 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1258 ++ectx.ec_stack.ga_len;
1259 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001260 if (ufunc->uf_va_name != NULL)
1261 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262
1263 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001264 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1265 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001267 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001268 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1269 + ufunc->uf_dfunc_idx;
1270 ufunc_T *base_ufunc = dfunc->df_ufunc;
1271
1272 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1273 // by copy_func().
1274 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001275 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001276 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1277 if (ectx.ec_outer == NULL)
1278 goto failed_early;
1279 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001280 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001281 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1282 {
1283 if (current_ectx->ec_outer != NULL)
1284 *ectx.ec_outer = *current_ectx->ec_outer;
1285 }
1286 else
1287 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001288 }
1289 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001290 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1291 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001292 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001293 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 // dummy frame entries
1296 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1297 {
1298 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1299 ++ectx.ec_stack.ga_len;
1300 }
1301
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001302 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001303 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001304 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1305 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001307 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001308 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001309 ectx.ec_stack.ga_len += dfunc->df_varcount;
1310 if (dfunc->df_has_closure)
1311 {
1312 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1313 STACK_TV_VAR(idx)->vval.v_number = 0;
1314 ++ectx.ec_stack.ga_len;
1315 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001316
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001317 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001318 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001319
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001320 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001321 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001322 estack_push_ufunc(ufunc, 1);
1323 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001324 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1325
Bram Moolenaar352134b2020-10-17 22:04:08 +02001326 // Use a specific location for storing error messages to be converted to an
1327 // exception.
1328 saved_msg_list = msg_list;
1329 msg_list = &private_msg_list;
1330
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001331 // Do turn errors into exceptions.
1332 suppress_errthrow = FALSE;
1333
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001334 // When ":silent!" was used before calling then we still abort the
1335 // function. If ":silent!" is used in the function then we don't.
1336 emsg_silent_def = emsg_silent;
1337 did_emsg_def = 0;
1338
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001339 where.wt_index = 0;
1340 where.wt_variable = FALSE;
1341
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001342 // Decide where to start execution, handles optional arguments.
1343 init_instr_idx(ufunc, argc, &ectx);
1344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 for (;;)
1346 {
1347 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001348
Bram Moolenaar270d0382020-05-15 21:42:53 +02001349 if (++breakcheck_count >= 100)
1350 {
1351 line_breakcheck();
1352 breakcheck_count = 0;
1353 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001354 if (got_int)
1355 {
1356 // Turn CTRL-C into an exception.
1357 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001358 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001359 goto failed;
1360 did_throw = TRUE;
1361 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362
Bram Moolenaara26b9702020-04-18 19:53:28 +02001363 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1364 {
1365 // Turn an error message into an exception.
1366 did_emsg = FALSE;
1367 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1368 goto failed;
1369 did_throw = TRUE;
1370 *msg_list = NULL;
1371 }
1372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 if (did_throw && !ectx.ec_in_catch)
1374 {
1375 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001376 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377
1378 // An exception jumps to the first catch, finally, or returns from
1379 // the current function.
1380 if (trystack->ga_len > 0)
1381 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001382 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 {
1384 // jump to ":catch" or ":finally"
1385 ectx.ec_in_catch = TRUE;
1386 ectx.ec_iidx = trycmd->tcd_catch_idx;
1387 }
1388 else
1389 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001390 // Not inside try or need to return from current functions.
1391 // Push a dummy return value.
1392 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1393 goto failed;
1394 tv = STACK_TV_BOT(0);
1395 tv->v_type = VAR_NUMBER;
1396 tv->vval.v_number = 0;
1397 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001398 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001400 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001401 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001402 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1403 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 goto done;
1405 }
1406
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001407 if (func_return(&ectx) == FAIL)
1408 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 }
1410 continue;
1411 }
1412
1413 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1414 switch (iptr->isn_type)
1415 {
1416 // execute Ex command line
1417 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001418 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001419 source_cookie_T cookie;
1420
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001421 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001422 // Pass getsourceline to get an error for a missing ":end"
1423 // command.
1424 CLEAR_FIELD(cookie);
1425 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1426 if (do_cmdline(iptr->isn_arg.string,
1427 getsourceline, &cookie,
1428 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1429 == FAIL
1430 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001431 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 break;
1434
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001435 // execute Ex command from pieces on the stack
1436 case ISN_EXECCONCAT:
1437 {
1438 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001439 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001440 int pass;
1441 int i;
1442 char_u *cmd = NULL;
1443 char_u *str;
1444
1445 for (pass = 1; pass <= 2; ++pass)
1446 {
1447 for (i = 0; i < count; ++i)
1448 {
1449 tv = STACK_TV_BOT(i - count);
1450 str = tv->vval.v_string;
1451 if (str != NULL && *str != NUL)
1452 {
1453 if (pass == 2)
1454 STRCPY(cmd + len, str);
1455 len += STRLEN(str);
1456 }
1457 if (pass == 2)
1458 clear_tv(tv);
1459 }
1460 if (pass == 1)
1461 {
1462 cmd = alloc(len + 1);
1463 if (cmd == NULL)
1464 goto failed;
1465 len = 0;
1466 }
1467 }
1468
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001469 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001470 do_cmdline_cmd(cmd);
1471 vim_free(cmd);
1472 }
1473 break;
1474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 // execute :echo {string} ...
1476 case ISN_ECHO:
1477 {
1478 int count = iptr->isn_arg.echo.echo_count;
1479 int atstart = TRUE;
1480 int needclr = TRUE;
1481
1482 for (idx = 0; idx < count; ++idx)
1483 {
1484 tv = STACK_TV_BOT(idx - count);
1485 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1486 &atstart, &needclr);
1487 clear_tv(tv);
1488 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001489 if (needclr)
1490 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 ectx.ec_stack.ga_len -= count;
1492 }
1493 break;
1494
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001495 // :execute {string} ...
1496 // :echomsg {string} ...
1497 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001498 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001499 case ISN_ECHOMSG:
1500 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001501 {
1502 int count = iptr->isn_arg.number;
1503 garray_T ga;
1504 char_u buf[NUMBUFLEN];
1505 char_u *p;
1506 int len;
1507 int failed = FALSE;
1508
1509 ga_init2(&ga, 1, 80);
1510 for (idx = 0; idx < count; ++idx)
1511 {
1512 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001513 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001514 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001515 if (tv->v_type == VAR_CHANNEL
1516 || tv->v_type == VAR_JOB)
1517 {
1518 SOURCING_LNUM = iptr->isn_lnum;
1519 emsg(_(e_inval_string));
1520 break;
1521 }
1522 else
1523 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001524 }
1525 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001526 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001527
1528 len = (int)STRLEN(p);
1529 if (ga_grow(&ga, len + 2) == FAIL)
1530 failed = TRUE;
1531 else
1532 {
1533 if (ga.ga_len > 0)
1534 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1535 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1536 ga.ga_len += len;
1537 }
1538 clear_tv(tv);
1539 }
1540 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001541 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001542 {
1543 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001544 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001545 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001546
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001547 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001548 {
1549 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001550 {
1551 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001552 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001553 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001554 {
1555 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001556 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001557 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001558 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001559 else
1560 {
1561 msg_sb_eol();
1562 if (iptr->isn_type == ISN_ECHOMSG)
1563 {
1564 msg_attr(ga.ga_data, echo_attr);
1565 out_flush();
1566 }
1567 else
1568 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001569 SOURCING_LNUM = iptr->isn_lnum;
1570 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001571 }
1572 }
1573 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001574 ga_clear(&ga);
1575 }
1576 break;
1577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 // load local variable or argument
1579 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001580 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 goto failed;
1582 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1583 ++ectx.ec_stack.ga_len;
1584 break;
1585
1586 // load v: variable
1587 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001588 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 goto failed;
1590 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1591 ++ectx.ec_stack.ga_len;
1592 break;
1593
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001594 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 case ISN_LOADSCRIPT:
1596 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001597 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 svar_T *sv;
1599
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001600 sv = get_script_svar(sref, &ectx);
1601 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001602 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001603 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001604 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 goto failed;
1606 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1607 ++ectx.ec_stack.ga_len;
1608 }
1609 break;
1610
1611 // load s: variable in old script
1612 case ISN_LOADS:
1613 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001614 hashtab_T *ht = &SCRIPT_VARS(
1615 iptr->isn_arg.loadstore.ls_sid);
1616 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 if (di == NULL)
1620 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001621 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001622 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001623 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 }
1625 else
1626 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001627 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 goto failed;
1629 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1630 ++ectx.ec_stack.ga_len;
1631 }
1632 }
1633 break;
1634
Bram Moolenaard3aac292020-04-19 14:32:17 +02001635 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001637 case ISN_LOADB:
1638 case ISN_LOADW:
1639 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001641 dictitem_T *di = NULL;
1642 hashtab_T *ht = NULL;
1643 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001644
Bram Moolenaard3aac292020-04-19 14:32:17 +02001645 switch (iptr->isn_type)
1646 {
1647 case ISN_LOADG:
1648 ht = get_globvar_ht();
1649 namespace = 'g';
1650 break;
1651 case ISN_LOADB:
1652 ht = &curbuf->b_vars->dv_hashtab;
1653 namespace = 'b';
1654 break;
1655 case ISN_LOADW:
1656 ht = &curwin->w_vars->dv_hashtab;
1657 namespace = 'w';
1658 break;
1659 case ISN_LOADT:
1660 ht = &curtab->tp_vars->dv_hashtab;
1661 namespace = 't';
1662 break;
1663 default: // Cannot reach here
1664 goto failed;
1665 }
1666 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668 if (di == NULL)
1669 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001670 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001671 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001672 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001673 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 }
1675 else
1676 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001677 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 goto failed;
1679 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1680 ++ectx.ec_stack.ga_len;
1681 }
1682 }
1683 break;
1684
Bram Moolenaar03290b82020-12-19 16:30:44 +01001685 // load autoload variable
1686 case ISN_LOADAUTO:
1687 {
1688 char_u *name = iptr->isn_arg.string;
1689
1690 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1691 goto failed;
1692 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001693 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001694 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1695 goto on_error;
1696 ++ectx.ec_stack.ga_len;
1697 }
1698 break;
1699
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001700 // load g:/b:/w:/t: namespace
1701 case ISN_LOADGDICT:
1702 case ISN_LOADBDICT:
1703 case ISN_LOADWDICT:
1704 case ISN_LOADTDICT:
1705 {
1706 dict_T *d = NULL;
1707
1708 switch (iptr->isn_type)
1709 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001710 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1711 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1712 case ISN_LOADWDICT: d = curwin->w_vars; break;
1713 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001714 default: // Cannot reach here
1715 goto failed;
1716 }
1717 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1718 goto failed;
1719 tv = STACK_TV_BOT(0);
1720 tv->v_type = VAR_DICT;
1721 tv->v_lock = 0;
1722 tv->vval.v_dict = d;
1723 ++ectx.ec_stack.ga_len;
1724 }
1725 break;
1726
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 // load &option
1728 case ISN_LOADOPT:
1729 {
1730 typval_T optval;
1731 char_u *name = iptr->isn_arg.string;
1732
Bram Moolenaara8c17702020-04-01 21:17:24 +02001733 // This is not expected to fail, name is checked during
1734 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001735 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001737 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001738 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 *STACK_TV_BOT(0) = optval;
1740 ++ectx.ec_stack.ga_len;
1741 }
1742 break;
1743
1744 // load $ENV
1745 case ISN_LOADENV:
1746 {
1747 typval_T optval;
1748 char_u *name = iptr->isn_arg.string;
1749
Bram Moolenaar270d0382020-05-15 21:42:53 +02001750 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001752 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001753 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 *STACK_TV_BOT(0) = optval;
1755 ++ectx.ec_stack.ga_len;
1756 }
1757 break;
1758
1759 // load @register
1760 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001761 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 goto failed;
1763 tv = STACK_TV_BOT(0);
1764 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001765 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001766 // This may result in NULL, which should be equivalent to an
1767 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 tv->vval.v_string = get_reg_contents(
1769 iptr->isn_arg.number, GREG_EXPR_SRC);
1770 ++ectx.ec_stack.ga_len;
1771 break;
1772
1773 // store local variable
1774 case ISN_STORE:
1775 --ectx.ec_stack.ga_len;
1776 tv = STACK_TV_VAR(iptr->isn_arg.number);
1777 clear_tv(tv);
1778 *tv = *STACK_TV_BOT(0);
1779 break;
1780
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001781 // store s: variable in old script
1782 case ISN_STORES:
1783 {
1784 hashtab_T *ht = &SCRIPT_VARS(
1785 iptr->isn_arg.loadstore.ls_sid);
1786 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001787 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001788
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001789 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001790 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001791 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001792 else
1793 {
1794 clear_tv(&di->di_tv);
1795 di->di_tv = *STACK_TV_BOT(0);
1796 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001797 }
1798 break;
1799
1800 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 case ISN_STORESCRIPT:
1802 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001803 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1804 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001806 sv = get_script_svar(sref, &ectx);
1807 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001808 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 --ectx.ec_stack.ga_len;
1810 clear_tv(sv->sv_tv);
1811 *sv->sv_tv = *STACK_TV_BOT(0);
1812 }
1813 break;
1814
1815 // store option
1816 case ISN_STOREOPT:
1817 {
1818 long n = 0;
1819 char_u *s = NULL;
1820 char *msg;
1821
1822 --ectx.ec_stack.ga_len;
1823 tv = STACK_TV_BOT(0);
1824 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001825 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001827 if (s == NULL)
1828 s = (char_u *)"";
1829 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001831 // must be VAR_NUMBER, CHECKTYPE makes sure
1832 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1834 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001835 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 if (msg != NULL)
1837 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001838 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001840 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 }
1843 break;
1844
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001845 // store $ENV
1846 case ISN_STOREENV:
1847 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001848 tv = STACK_TV_BOT(0);
1849 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1850 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001851 break;
1852
1853 // store @r
1854 case ISN_STOREREG:
1855 {
1856 int reg = iptr->isn_arg.number;
1857
1858 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001859 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001860 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001861 tv_get_string(tv), -1, FALSE);
1862 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001863 }
1864 break;
1865
1866 // store v: variable
1867 case ISN_STOREV:
1868 --ectx.ec_stack.ga_len;
1869 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1870 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001871 // should not happen, type is checked when compiling
1872 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001873 break;
1874
Bram Moolenaard3aac292020-04-19 14:32:17 +02001875 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001877 case ISN_STOREB:
1878 case ISN_STOREW:
1879 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001881 dictitem_T *di;
1882 hashtab_T *ht;
1883 char_u *name = iptr->isn_arg.string + 2;
1884
Bram Moolenaard3aac292020-04-19 14:32:17 +02001885 switch (iptr->isn_type)
1886 {
1887 case ISN_STOREG:
1888 ht = get_globvar_ht();
1889 break;
1890 case ISN_STOREB:
1891 ht = &curbuf->b_vars->dv_hashtab;
1892 break;
1893 case ISN_STOREW:
1894 ht = &curwin->w_vars->dv_hashtab;
1895 break;
1896 case ISN_STORET:
1897 ht = &curtab->tp_vars->dv_hashtab;
1898 break;
1899 default: // Cannot reach here
1900 goto failed;
1901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902
1903 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001904 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001906 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 else
1908 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001909 SOURCING_LNUM = iptr->isn_lnum;
1910 if (var_check_permission(di, name) == FAIL)
1911 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 clear_tv(&di->di_tv);
1913 di->di_tv = *STACK_TV_BOT(0);
1914 }
1915 }
1916 break;
1917
Bram Moolenaar03290b82020-12-19 16:30:44 +01001918 // store an autoload variable
1919 case ISN_STOREAUTO:
1920 SOURCING_LNUM = iptr->isn_lnum;
1921 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1922 clear_tv(STACK_TV_BOT(-1));
1923 --ectx.ec_stack.ga_len;
1924 break;
1925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 // store number in local variable
1927 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001928 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 clear_tv(tv);
1930 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001931 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 break;
1933
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001934 // store value in list or dict variable
1935 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001936 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001937 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001938 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001939 typval_T *tv_dest = STACK_TV_BOT(-1);
1940 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001941
Bram Moolenaar752fc692021-01-04 21:57:11 +01001942 // Stack contains:
1943 // -3 value to be stored
1944 // -2 index
1945 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001946 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001947 SOURCING_LNUM = iptr->isn_lnum;
1948 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001949 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001950 dest_type = tv_dest->v_type;
1951 if (dest_type == VAR_DICT)
1952 status = do_2string(tv_idx, TRUE);
1953 else if (dest_type == VAR_LIST
1954 && tv_idx->v_type != VAR_NUMBER)
1955 {
1956 emsg(_(e_number_exp));
1957 status = FAIL;
1958 }
1959 }
1960 else if (dest_type != tv_dest->v_type)
1961 {
1962 // just in case, should be OK
1963 semsg(_(e_expected_str_but_got_str),
1964 vartype_name(dest_type),
1965 vartype_name(tv_dest->v_type));
1966 status = FAIL;
1967 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001968
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001969 if (status == OK && dest_type == VAR_LIST)
1970 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001971 long lidx = (long)tv_idx->vval.v_number;
1972 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001973
1974 if (list == NULL)
1975 {
1976 emsg(_(e_list_not_set));
1977 goto on_error;
1978 }
1979 if (lidx < 0 && list->lv_len + lidx >= 0)
1980 // negative index is relative to the end
1981 lidx = list->lv_len + lidx;
1982 if (lidx < 0 || lidx > list->lv_len)
1983 {
1984 semsg(_(e_listidx), lidx);
1985 goto on_error;
1986 }
1987 if (lidx < list->lv_len)
1988 {
1989 listitem_T *li = list_find(list, lidx);
1990
1991 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001992 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001993 goto on_error;
1994 // overwrite existing list item
1995 clear_tv(&li->li_tv);
1996 li->li_tv = *tv;
1997 }
1998 else
1999 {
2000 if (error_if_locked(list->lv_lock,
2001 e_cannot_change_list))
2002 goto on_error;
2003 // append to list, only fails when out of memory
2004 if (list_append_tv(list, tv) == FAIL)
2005 goto failed;
2006 clear_tv(tv);
2007 }
2008 }
2009 else if (status == OK && dest_type == VAR_DICT)
2010 {
2011 char_u *key = tv_idx->vval.v_string;
2012 dict_T *dict = tv_dest->vval.v_dict;
2013 dictitem_T *di;
2014
2015 SOURCING_LNUM = iptr->isn_lnum;
2016 if (dict == NULL)
2017 {
2018 emsg(_(e_dictionary_not_set));
2019 goto on_error;
2020 }
2021 if (key == NULL)
2022 key = (char_u *)"";
2023 di = dict_find(dict, key, -1);
2024 if (di != NULL)
2025 {
2026 if (error_if_locked(di->di_tv.v_lock,
2027 e_cannot_change_dict_item))
2028 goto on_error;
2029 // overwrite existing value
2030 clear_tv(&di->di_tv);
2031 di->di_tv = *tv;
2032 }
2033 else
2034 {
2035 if (error_if_locked(dict->dv_lock,
2036 e_cannot_change_dict))
2037 goto on_error;
2038 // add to dict, only fails when out of memory
2039 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2040 goto failed;
2041 clear_tv(tv);
2042 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002043 }
2044 else
2045 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002046 status = FAIL;
2047 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002048 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002049
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002050 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002051 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002052 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002053 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002054 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002055 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002056 goto on_error;
2057 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002058 }
2059 break;
2060
Bram Moolenaar0186e582021-01-10 18:33:11 +01002061 // load or store variable or argument from outer scope
2062 case ISN_LOADOUTER:
2063 case ISN_STOREOUTER:
2064 {
2065 int depth = iptr->isn_arg.outer.outer_depth;
2066 outer_T *outer = ectx.ec_outer;
2067
2068 while (depth > 1 && outer != NULL)
2069 {
2070 outer = outer->out_up;
2071 --depth;
2072 }
2073 if (outer == NULL)
2074 {
2075 SOURCING_LNUM = iptr->isn_lnum;
2076 iemsg("LOADOUTER depth more than scope levels");
2077 goto failed;
2078 }
2079 tv = ((typval_T *)outer->out_stack->ga_data)
2080 + outer->out_frame_idx + STACK_FRAME_SIZE
2081 + iptr->isn_arg.outer.outer_idx;
2082 if (iptr->isn_type == ISN_LOADOUTER)
2083 {
2084 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2085 goto failed;
2086 copy_tv(tv, STACK_TV_BOT(0));
2087 ++ectx.ec_stack.ga_len;
2088 }
2089 else
2090 {
2091 --ectx.ec_stack.ga_len;
2092 clear_tv(tv);
2093 *tv = *STACK_TV_BOT(0);
2094 }
2095 }
2096 break;
2097
Bram Moolenaar752fc692021-01-04 21:57:11 +01002098 // unlet item in list or dict variable
2099 case ISN_UNLETINDEX:
2100 {
2101 typval_T *tv_idx = STACK_TV_BOT(-2);
2102 typval_T *tv_dest = STACK_TV_BOT(-1);
2103 int status = OK;
2104
2105 // Stack contains:
2106 // -2 index
2107 // -1 dict or list
2108 if (tv_dest->v_type == VAR_DICT)
2109 {
2110 // unlet a dict item, index must be a string
2111 if (tv_idx->v_type != VAR_STRING)
2112 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002113 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002114 semsg(_(e_expected_str_but_got_str),
2115 vartype_name(VAR_STRING),
2116 vartype_name(tv_idx->v_type));
2117 status = FAIL;
2118 }
2119 else
2120 {
2121 dict_T *d = tv_dest->vval.v_dict;
2122 char_u *key = tv_idx->vval.v_string;
2123 dictitem_T *di = NULL;
2124
2125 if (key == NULL)
2126 key = (char_u *)"";
2127 if (d != NULL)
2128 di = dict_find(d, key, (int)STRLEN(key));
2129 if (di == NULL)
2130 {
2131 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002132 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002133 semsg(_(e_dictkey), key);
2134 status = FAIL;
2135 }
2136 else
2137 {
2138 // TODO: check for dict or item locked
2139 dictitem_remove(d, di);
2140 }
2141 }
2142 }
2143 else if (tv_dest->v_type == VAR_LIST)
2144 {
2145 // unlet a List item, index must be a number
2146 if (tv_idx->v_type != VAR_NUMBER)
2147 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002148 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002149 semsg(_(e_expected_str_but_got_str),
2150 vartype_name(VAR_NUMBER),
2151 vartype_name(tv_idx->v_type));
2152 status = FAIL;
2153 }
2154 else
2155 {
2156 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002157 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002158 listitem_T *li = NULL;
2159
2160 li = list_find(l, n);
2161 if (li == NULL)
2162 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002163 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002164 semsg(_(e_listidx), n);
2165 status = FAIL;
2166 }
2167 else
2168 // TODO: check for list or item locked
2169 listitem_remove(l, li);
2170 }
2171 }
2172 else
2173 {
2174 status = FAIL;
2175 semsg(_(e_cannot_index_str),
2176 vartype_name(tv_dest->v_type));
2177 }
2178
2179 clear_tv(tv_idx);
2180 clear_tv(tv_dest);
2181 ectx.ec_stack.ga_len -= 2;
2182 if (status == FAIL)
2183 goto on_error;
2184 }
2185 break;
2186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 // push constant
2188 case ISN_PUSHNR:
2189 case ISN_PUSHBOOL:
2190 case ISN_PUSHSPEC:
2191 case ISN_PUSHF:
2192 case ISN_PUSHS:
2193 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002194 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002195 case ISN_PUSHCHANNEL:
2196 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002197 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 goto failed;
2199 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002200 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 ++ectx.ec_stack.ga_len;
2202 switch (iptr->isn_type)
2203 {
2204 case ISN_PUSHNR:
2205 tv->v_type = VAR_NUMBER;
2206 tv->vval.v_number = iptr->isn_arg.number;
2207 break;
2208 case ISN_PUSHBOOL:
2209 tv->v_type = VAR_BOOL;
2210 tv->vval.v_number = iptr->isn_arg.number;
2211 break;
2212 case ISN_PUSHSPEC:
2213 tv->v_type = VAR_SPECIAL;
2214 tv->vval.v_number = iptr->isn_arg.number;
2215 break;
2216#ifdef FEAT_FLOAT
2217 case ISN_PUSHF:
2218 tv->v_type = VAR_FLOAT;
2219 tv->vval.v_float = iptr->isn_arg.fnumber;
2220 break;
2221#endif
2222 case ISN_PUSHBLOB:
2223 blob_copy(iptr->isn_arg.blob, tv);
2224 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002225 case ISN_PUSHFUNC:
2226 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002227 if (iptr->isn_arg.string == NULL)
2228 tv->vval.v_string = NULL;
2229 else
2230 tv->vval.v_string =
2231 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002232 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002233 case ISN_PUSHCHANNEL:
2234#ifdef FEAT_JOB_CHANNEL
2235 tv->v_type = VAR_CHANNEL;
2236 tv->vval.v_channel = iptr->isn_arg.channel;
2237 if (tv->vval.v_channel != NULL)
2238 ++tv->vval.v_channel->ch_refcount;
2239#endif
2240 break;
2241 case ISN_PUSHJOB:
2242#ifdef FEAT_JOB_CHANNEL
2243 tv->v_type = VAR_JOB;
2244 tv->vval.v_job = iptr->isn_arg.job;
2245 if (tv->vval.v_job != NULL)
2246 ++tv->vval.v_job->jv_refcount;
2247#endif
2248 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 default:
2250 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002251 tv->vval.v_string = vim_strsave(
2252 iptr->isn_arg.string == NULL
2253 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 }
2255 break;
2256
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002257 case ISN_UNLET:
2258 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2259 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002260 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002261 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002262 case ISN_UNLETENV:
2263 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2264 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002265
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002266 case ISN_LOCKCONST:
2267 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2268 break;
2269
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 // create a list from items on the stack; uses a single allocation
2271 // for the list header and the items
2272 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002273 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2274 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 break;
2276
2277 // create a dict from items on the stack
2278 case ISN_NEWDICT:
2279 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002280 int count = iptr->isn_arg.number;
2281 dict_T *dict = dict_alloc();
2282 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002283 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284
2285 if (dict == NULL)
2286 goto failed;
2287 for (idx = 0; idx < count; ++idx)
2288 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002289 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002291 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002292 key = tv->vval.v_string == NULL
2293 ? (char_u *)"" : tv->vval.v_string;
2294 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002295 if (item != NULL)
2296 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002297 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002298 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002299 dict_unref(dict);
2300 goto on_error;
2301 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002302 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 clear_tv(tv);
2304 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002305 {
2306 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002308 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2310 item->di_tv.v_lock = 0;
2311 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002312 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002313 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002314 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002316 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 }
2318
2319 if (count > 0)
2320 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002321 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 goto failed;
2323 else
2324 ++ectx.ec_stack.ga_len;
2325 tv = STACK_TV_BOT(-1);
2326 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002327 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 tv->vval.v_dict = dict;
2329 ++dict->dv_refcount;
2330 }
2331 break;
2332
2333 // call a :def function
2334 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002335 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002336 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 iptr->isn_arg.dfunc.cdf_argcount,
2338 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002339 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 break;
2341
2342 // call a builtin function
2343 case ISN_BCALL:
2344 SOURCING_LNUM = iptr->isn_lnum;
2345 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2346 iptr->isn_arg.bfunc.cbf_argcount,
2347 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002348 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349 break;
2350
2351 // call a funcref or partial
2352 case ISN_PCALL:
2353 {
2354 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2355 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002356 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357
2358 SOURCING_LNUM = iptr->isn_lnum;
2359 if (pfunc->cpf_top)
2360 {
2361 // funcref is above the arguments
2362 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2363 }
2364 else
2365 {
2366 // Get the funcref from the stack.
2367 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002368 partial_tv = *STACK_TV_BOT(0);
2369 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 }
2371 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002372 if (tv == &partial_tv)
2373 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002375 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 }
2377 break;
2378
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002379 case ISN_PCALL_END:
2380 // PCALL finished, arguments have been consumed and replaced by
2381 // the return value. Now clear the funcref from the stack,
2382 // and move the return value in its place.
2383 --ectx.ec_stack.ga_len;
2384 clear_tv(STACK_TV_BOT(-1));
2385 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2386 break;
2387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 // call a user defined function or funcref/partial
2389 case ISN_UCALL:
2390 {
2391 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2392
2393 SOURCING_LNUM = iptr->isn_lnum;
2394 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002395 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002396 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 }
2398 break;
2399
2400 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002401 case ISN_RETURN_ZERO:
2402 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2403 goto failed;
2404 tv = STACK_TV_BOT(0);
2405 ++ectx.ec_stack.ga_len;
2406 tv->v_type = VAR_NUMBER;
2407 tv->vval.v_number = 0;
2408 tv->v_lock = 0;
2409 // FALLTHROUGH
2410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 case ISN_RETURN:
2412 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002413 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002414 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002415
2416 if (trystack->ga_len > 0)
2417 trycmd = ((trycmd_T *)trystack->ga_data)
2418 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002419 if (trycmd != NULL
2420 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaarc150c092021-02-13 15:02:46 +01002421 && ectx.ec_instr[trycmd->tcd_finally_idx]
2422 .isn_type != ISN_ENDTRY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 {
2424 // jump to ":finally"
2425 ectx.ec_iidx = trycmd->tcd_finally_idx;
2426 trycmd->tcd_return = TRUE;
2427 }
2428 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002429 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 }
2431 break;
2432
2433 // push a function reference to a compiled function
2434 case ISN_FUNCREF:
2435 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002436 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2437 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2438 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 if (pt == NULL)
2441 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002442 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002443 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002444 vim_free(pt);
2445 goto failed;
2446 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002447 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2448 &ectx) == FAIL)
2449 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 tv = STACK_TV_BOT(0);
2452 ++ectx.ec_stack.ga_len;
2453 tv->vval.v_partial = pt;
2454 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002455 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 }
2457 break;
2458
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002459 // Create a global function from a lambda.
2460 case ISN_NEWFUNC:
2461 {
2462 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2463
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002464 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002465 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002466 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002467 }
2468 break;
2469
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002470 // List functions
2471 case ISN_DEF:
2472 if (iptr->isn_arg.string == NULL)
2473 list_functions(NULL);
2474 else
2475 {
2476 exarg_T ea;
2477
2478 CLEAR_FIELD(ea);
2479 ea.cmd = ea.arg = iptr->isn_arg.string;
2480 define_function(&ea, NULL);
2481 }
2482 break;
2483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 // jump if a condition is met
2485 case ISN_JUMP:
2486 {
2487 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002488 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 int jump = TRUE;
2490
2491 if (when != JUMP_ALWAYS)
2492 {
2493 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002494 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002495 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002496 || when == JUMP_IF_COND_TRUE)
2497 {
2498 SOURCING_LNUM = iptr->isn_lnum;
2499 jump = tv_get_bool_chk(tv, &error);
2500 if (error)
2501 goto on_error;
2502 }
2503 else
2504 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002506 || when == JUMP_AND_KEEP_IF_FALSE
2507 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002509 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 {
2511 // drop the value from the stack
2512 clear_tv(tv);
2513 --ectx.ec_stack.ga_len;
2514 }
2515 }
2516 if (jump)
2517 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2518 }
2519 break;
2520
2521 // top of a for loop
2522 case ISN_FOR:
2523 {
2524 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2525 typval_T *idxtv =
2526 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2527
2528 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002529 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002531 ++idxtv->vval.v_number;
2532 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 // past the end of the list, jump to "endfor"
2534 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2535 else if (list->lv_first == &range_list_item)
2536 {
2537 // non-materialized range() list
2538 tv = STACK_TV_BOT(0);
2539 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002540 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 tv->vval.v_number = list_find_nr(
2542 list, idxtv->vval.v_number, NULL);
2543 ++ectx.ec_stack.ga_len;
2544 }
2545 else
2546 {
2547 listitem_T *li = list_find(list, idxtv->vval.v_number);
2548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2550 ++ectx.ec_stack.ga_len;
2551 }
2552 }
2553 break;
2554
2555 // start of ":try" block
2556 case ISN_TRY:
2557 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002558 trycmd_T *trycmd = NULL;
2559
Bram Moolenaar270d0382020-05-15 21:42:53 +02002560 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 goto failed;
2562 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2563 + ectx.ec_trystack.ga_len;
2564 ++ectx.ec_trystack.ga_len;
2565 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002566 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002567 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2569 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002570 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002571 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 }
2573 break;
2574
2575 case ISN_PUSHEXC:
2576 if (current_exception == NULL)
2577 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002578 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 iemsg("Evaluating catch while current_exception is NULL");
2580 goto failed;
2581 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002582 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 goto failed;
2584 tv = STACK_TV_BOT(0);
2585 ++ectx.ec_stack.ga_len;
2586 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002587 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 tv->vval.v_string = vim_strsave(
2589 (char_u *)current_exception->value);
2590 break;
2591
2592 case ISN_CATCH:
2593 {
2594 garray_T *trystack = &ectx.ec_trystack;
2595
Bram Moolenaar20a76292020-12-25 19:47:24 +01002596 if (restore_cmdmod)
2597 {
2598 cmdmod.cmod_filter_regmatch.regprog = NULL;
2599 undo_cmdmod(&cmdmod);
2600 cmdmod = save_cmdmod;
2601 restore_cmdmod = FALSE;
2602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 if (trystack->ga_len > 0)
2604 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002605 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 + trystack->ga_len - 1;
2607 trycmd->tcd_caught = TRUE;
2608 }
2609 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002610 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 catch_exception(current_exception);
2612 }
2613 break;
2614
Bram Moolenaarc150c092021-02-13 15:02:46 +01002615 case ISN_TRYCONT:
2616 {
2617 garray_T *trystack = &ectx.ec_trystack;
2618 trycont_T *trycont = &iptr->isn_arg.trycont;
2619 int i;
2620 trycmd_T *trycmd;
2621 int iidx = trycont->tct_where;
2622
2623 if (trystack->ga_len < trycont->tct_levels)
2624 {
2625 siemsg("TRYCONT: expected %d levels, found %d",
2626 trycont->tct_levels, trystack->ga_len);
2627 goto failed;
2628 }
2629 // Make :endtry jump to any outer try block and the last
2630 // :endtry inside the loop to the loop start.
2631 for (i = trycont->tct_levels; i > 0; --i)
2632 {
2633 trycmd = ((trycmd_T *)trystack->ga_data)
2634 + trystack->ga_len - i;
2635 trycmd->tcd_cont = iidx;
2636 iidx = trycmd->tcd_finally_idx;
2637 }
2638 // jump to :finally or :endtry of current try statement
2639 ectx.ec_iidx = iidx;
2640 }
2641 break;
2642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 // end of ":try" block
2644 case ISN_ENDTRY:
2645 {
2646 garray_T *trystack = &ectx.ec_trystack;
2647
2648 if (trystack->ga_len > 0)
2649 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002650 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 --trystack->ga_len;
2653 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002654 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 trycmd = ((trycmd_T *)trystack->ga_data)
2656 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002657 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 {
2659 // discard the exception
2660 if (caught_stack == current_exception)
2661 caught_stack = caught_stack->caught;
2662 discard_current_exception();
2663 }
2664
2665 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002666 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002667
2668 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2669 {
2670 --ectx.ec_stack.ga_len;
2671 clear_tv(STACK_TV_BOT(0));
2672 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01002673 if (trycmd->tcd_cont)
2674 // handling :continue: jump to outer try block or
2675 // start of the loop
2676 ectx.ec_iidx = trycmd->tcd_cont;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 }
2678 }
2679 break;
2680
2681 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002682 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002683 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002684
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002685 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2686 {
2687 // throwing an exception while using "silent!" causes
2688 // the function to abort but not display an error.
2689 tv = STACK_TV_BOT(-1);
2690 clear_tv(tv);
2691 tv->v_type = VAR_NUMBER;
2692 tv->vval.v_number = 0;
2693 goto done;
2694 }
2695 --ectx.ec_stack.ga_len;
2696 tv = STACK_TV_BOT(0);
2697 if (tv->vval.v_string == NULL
2698 || *skipwhite(tv->vval.v_string) == NUL)
2699 {
2700 vim_free(tv->vval.v_string);
2701 SOURCING_LNUM = iptr->isn_lnum;
2702 emsg(_(e_throw_with_empty_string));
2703 goto failed;
2704 }
2705
2706 // Inside a "catch" we need to first discard the caught
2707 // exception.
2708 if (trystack->ga_len > 0)
2709 {
2710 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2711 + trystack->ga_len - 1;
2712 if (trycmd->tcd_caught && current_exception != NULL)
2713 {
2714 // discard the exception
2715 if (caught_stack == current_exception)
2716 caught_stack = caught_stack->caught;
2717 discard_current_exception();
2718 trycmd->tcd_caught = FALSE;
2719 }
2720 }
2721
2722 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2723 == FAIL)
2724 {
2725 vim_free(tv->vval.v_string);
2726 goto failed;
2727 }
2728 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 break;
2731
2732 // compare with special values
2733 case ISN_COMPAREBOOL:
2734 case ISN_COMPARESPECIAL:
2735 {
2736 typval_T *tv1 = STACK_TV_BOT(-2);
2737 typval_T *tv2 = STACK_TV_BOT(-1);
2738 varnumber_T arg1 = tv1->vval.v_number;
2739 varnumber_T arg2 = tv2->vval.v_number;
2740 int res;
2741
2742 switch (iptr->isn_arg.op.op_type)
2743 {
2744 case EXPR_EQUAL: res = arg1 == arg2; break;
2745 case EXPR_NEQUAL: res = arg1 != arg2; break;
2746 default: res = 0; break;
2747 }
2748
2749 --ectx.ec_stack.ga_len;
2750 tv1->v_type = VAR_BOOL;
2751 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2752 }
2753 break;
2754
2755 // Operation with two number arguments
2756 case ISN_OPNR:
2757 case ISN_COMPARENR:
2758 {
2759 typval_T *tv1 = STACK_TV_BOT(-2);
2760 typval_T *tv2 = STACK_TV_BOT(-1);
2761 varnumber_T arg1 = tv1->vval.v_number;
2762 varnumber_T arg2 = tv2->vval.v_number;
2763 varnumber_T res;
2764
2765 switch (iptr->isn_arg.op.op_type)
2766 {
2767 case EXPR_MULT: res = arg1 * arg2; break;
2768 case EXPR_DIV: res = arg1 / arg2; break;
2769 case EXPR_REM: res = arg1 % arg2; break;
2770 case EXPR_SUB: res = arg1 - arg2; break;
2771 case EXPR_ADD: res = arg1 + arg2; break;
2772
2773 case EXPR_EQUAL: res = arg1 == arg2; break;
2774 case EXPR_NEQUAL: res = arg1 != arg2; break;
2775 case EXPR_GREATER: res = arg1 > arg2; break;
2776 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2777 case EXPR_SMALLER: res = arg1 < arg2; break;
2778 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2779 default: res = 0; break;
2780 }
2781
2782 --ectx.ec_stack.ga_len;
2783 if (iptr->isn_type == ISN_COMPARENR)
2784 {
2785 tv1->v_type = VAR_BOOL;
2786 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2787 }
2788 else
2789 tv1->vval.v_number = res;
2790 }
2791 break;
2792
2793 // Computation with two float arguments
2794 case ISN_OPFLOAT:
2795 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002796#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 {
2798 typval_T *tv1 = STACK_TV_BOT(-2);
2799 typval_T *tv2 = STACK_TV_BOT(-1);
2800 float_T arg1 = tv1->vval.v_float;
2801 float_T arg2 = tv2->vval.v_float;
2802 float_T res = 0;
2803 int cmp = FALSE;
2804
2805 switch (iptr->isn_arg.op.op_type)
2806 {
2807 case EXPR_MULT: res = arg1 * arg2; break;
2808 case EXPR_DIV: res = arg1 / arg2; break;
2809 case EXPR_SUB: res = arg1 - arg2; break;
2810 case EXPR_ADD: res = arg1 + arg2; break;
2811
2812 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2813 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2814 case EXPR_GREATER: cmp = arg1 > arg2; break;
2815 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2816 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2817 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2818 default: cmp = 0; break;
2819 }
2820 --ectx.ec_stack.ga_len;
2821 if (iptr->isn_type == ISN_COMPAREFLOAT)
2822 {
2823 tv1->v_type = VAR_BOOL;
2824 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2825 }
2826 else
2827 tv1->vval.v_float = res;
2828 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002829#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 break;
2831
2832 case ISN_COMPARELIST:
2833 {
2834 typval_T *tv1 = STACK_TV_BOT(-2);
2835 typval_T *tv2 = STACK_TV_BOT(-1);
2836 list_T *arg1 = tv1->vval.v_list;
2837 list_T *arg2 = tv2->vval.v_list;
2838 int cmp = FALSE;
2839 int ic = iptr->isn_arg.op.op_ic;
2840
2841 switch (iptr->isn_arg.op.op_type)
2842 {
2843 case EXPR_EQUAL: cmp =
2844 list_equal(arg1, arg2, ic, FALSE); break;
2845 case EXPR_NEQUAL: cmp =
2846 !list_equal(arg1, arg2, ic, FALSE); break;
2847 case EXPR_IS: cmp = arg1 == arg2; break;
2848 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2849 default: cmp = 0; break;
2850 }
2851 --ectx.ec_stack.ga_len;
2852 clear_tv(tv1);
2853 clear_tv(tv2);
2854 tv1->v_type = VAR_BOOL;
2855 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2856 }
2857 break;
2858
2859 case ISN_COMPAREBLOB:
2860 {
2861 typval_T *tv1 = STACK_TV_BOT(-2);
2862 typval_T *tv2 = STACK_TV_BOT(-1);
2863 blob_T *arg1 = tv1->vval.v_blob;
2864 blob_T *arg2 = tv2->vval.v_blob;
2865 int cmp = FALSE;
2866
2867 switch (iptr->isn_arg.op.op_type)
2868 {
2869 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2870 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2871 case EXPR_IS: cmp = arg1 == arg2; break;
2872 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2873 default: cmp = 0; break;
2874 }
2875 --ectx.ec_stack.ga_len;
2876 clear_tv(tv1);
2877 clear_tv(tv2);
2878 tv1->v_type = VAR_BOOL;
2879 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2880 }
2881 break;
2882
2883 // TODO: handle separately
2884 case ISN_COMPARESTRING:
2885 case ISN_COMPAREDICT:
2886 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 case ISN_COMPAREANY:
2888 {
2889 typval_T *tv1 = STACK_TV_BOT(-2);
2890 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002891 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 int ic = iptr->isn_arg.op.op_ic;
2893
Bram Moolenaareb26f432020-09-14 16:50:05 +02002894 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002895 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 --ectx.ec_stack.ga_len;
2898 }
2899 break;
2900
2901 case ISN_ADDLIST:
2902 case ISN_ADDBLOB:
2903 {
2904 typval_T *tv1 = STACK_TV_BOT(-2);
2905 typval_T *tv2 = STACK_TV_BOT(-1);
2906
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002907 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 if (iptr->isn_type == ISN_ADDLIST)
2909 eval_addlist(tv1, tv2);
2910 else
2911 eval_addblob(tv1, tv2);
2912 clear_tv(tv2);
2913 --ectx.ec_stack.ga_len;
2914 }
2915 break;
2916
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002917 case ISN_LISTAPPEND:
2918 {
2919 typval_T *tv1 = STACK_TV_BOT(-2);
2920 typval_T *tv2 = STACK_TV_BOT(-1);
2921 list_T *l = tv1->vval.v_list;
2922
2923 // add an item to a list
2924 if (l == NULL)
2925 {
2926 SOURCING_LNUM = iptr->isn_lnum;
2927 emsg(_(e_cannot_add_to_null_list));
2928 goto on_error;
2929 }
2930 if (list_append_tv(l, tv2) == FAIL)
2931 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002932 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002933 --ectx.ec_stack.ga_len;
2934 }
2935 break;
2936
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002937 case ISN_BLOBAPPEND:
2938 {
2939 typval_T *tv1 = STACK_TV_BOT(-2);
2940 typval_T *tv2 = STACK_TV_BOT(-1);
2941 blob_T *b = tv1->vval.v_blob;
2942 int error = FALSE;
2943 varnumber_T n;
2944
2945 // add a number to a blob
2946 if (b == NULL)
2947 {
2948 SOURCING_LNUM = iptr->isn_lnum;
2949 emsg(_(e_cannot_add_to_null_blob));
2950 goto on_error;
2951 }
2952 n = tv_get_number_chk(tv2, &error);
2953 if (error)
2954 goto on_error;
2955 ga_append(&b->bv_ga, (int)n);
2956 --ectx.ec_stack.ga_len;
2957 }
2958 break;
2959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 // Computation with two arguments of unknown type
2961 case ISN_OPANY:
2962 {
2963 typval_T *tv1 = STACK_TV_BOT(-2);
2964 typval_T *tv2 = STACK_TV_BOT(-1);
2965 varnumber_T n1, n2;
2966#ifdef FEAT_FLOAT
2967 float_T f1 = 0, f2 = 0;
2968#endif
2969 int error = FALSE;
2970
2971 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2972 {
2973 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2974 {
2975 eval_addlist(tv1, tv2);
2976 clear_tv(tv2);
2977 --ectx.ec_stack.ga_len;
2978 break;
2979 }
2980 else if (tv1->v_type == VAR_BLOB
2981 && tv2->v_type == VAR_BLOB)
2982 {
2983 eval_addblob(tv1, tv2);
2984 clear_tv(tv2);
2985 --ectx.ec_stack.ga_len;
2986 break;
2987 }
2988 }
2989#ifdef FEAT_FLOAT
2990 if (tv1->v_type == VAR_FLOAT)
2991 {
2992 f1 = tv1->vval.v_float;
2993 n1 = 0;
2994 }
2995 else
2996#endif
2997 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002998 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 n1 = tv_get_number_chk(tv1, &error);
3000 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003001 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002#ifdef FEAT_FLOAT
3003 if (tv2->v_type == VAR_FLOAT)
3004 f1 = n1;
3005#endif
3006 }
3007#ifdef FEAT_FLOAT
3008 if (tv2->v_type == VAR_FLOAT)
3009 {
3010 f2 = tv2->vval.v_float;
3011 n2 = 0;
3012 }
3013 else
3014#endif
3015 {
3016 n2 = tv_get_number_chk(tv2, &error);
3017 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003018 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019#ifdef FEAT_FLOAT
3020 if (tv1->v_type == VAR_FLOAT)
3021 f2 = n2;
3022#endif
3023 }
3024#ifdef FEAT_FLOAT
3025 // if there is a float on either side the result is a float
3026 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3027 {
3028 switch (iptr->isn_arg.op.op_type)
3029 {
3030 case EXPR_MULT: f1 = f1 * f2; break;
3031 case EXPR_DIV: f1 = f1 / f2; break;
3032 case EXPR_SUB: f1 = f1 - f2; break;
3033 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003034 default: SOURCING_LNUM = iptr->isn_lnum;
3035 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003036 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 }
3038 clear_tv(tv1);
3039 clear_tv(tv2);
3040 tv1->v_type = VAR_FLOAT;
3041 tv1->vval.v_float = f1;
3042 --ectx.ec_stack.ga_len;
3043 }
3044 else
3045#endif
3046 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003047 int failed = FALSE;
3048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 switch (iptr->isn_arg.op.op_type)
3050 {
3051 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003052 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3053 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003054 goto on_error;
3055 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 case EXPR_SUB: n1 = n1 - n2; break;
3057 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003058 default: n1 = num_modulus(n1, n2, &failed);
3059 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003060 goto on_error;
3061 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 }
3063 clear_tv(tv1);
3064 clear_tv(tv2);
3065 tv1->v_type = VAR_NUMBER;
3066 tv1->vval.v_number = n1;
3067 --ectx.ec_stack.ga_len;
3068 }
3069 }
3070 break;
3071
3072 case ISN_CONCAT:
3073 {
3074 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3075 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3076 char_u *res;
3077
3078 res = concat_str(str1, str2);
3079 clear_tv(STACK_TV_BOT(-2));
3080 clear_tv(STACK_TV_BOT(-1));
3081 --ectx.ec_stack.ga_len;
3082 STACK_TV_BOT(-1)->vval.v_string = res;
3083 }
3084 break;
3085
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003086 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003087 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003088 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003089 int is_slice = iptr->isn_type == ISN_STRSLICE;
3090 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003091 char_u *res;
3092
3093 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003094 // string slice: string is at stack-3, first index at
3095 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003096 if (is_slice)
3097 {
3098 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003099 n1 = tv->vval.v_number;
3100 }
3101
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003102 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003103 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003104
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003105 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003106 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003107 if (is_slice)
3108 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003109 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003110 else
3111 // Index: The resulting variable is a string of a
3112 // single character. If the index is too big or
3113 // negative the result is empty.
3114 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003115 vim_free(tv->vval.v_string);
3116 tv->vval.v_string = res;
3117 }
3118 break;
3119
3120 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003121 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 {
Bram Moolenaared591872020-08-15 22:14:53 +02003123 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003125 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126
3127 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003128 // list slice: list is at stack-3, indexes at stack-2 and
3129 // stack-1
3130 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 list = tv->vval.v_list;
3132
3133 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003134 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003136
3137 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 {
Bram Moolenaared591872020-08-15 22:14:53 +02003139 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003140 n1 = tv->vval.v_number;
3141 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 }
Bram Moolenaared591872020-08-15 22:14:53 +02003143
3144 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003145 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003146 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003147 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3148 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003149 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 }
3151 break;
3152
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003153 case ISN_ANYINDEX:
3154 case ISN_ANYSLICE:
3155 {
3156 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3157 typval_T *var1, *var2;
3158 int res;
3159
3160 // index: composite is at stack-2, index at stack-1
3161 // slice: composite is at stack-3, indexes at stack-2 and
3162 // stack-1
3163 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003164 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003165 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3166 goto on_error;
3167 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3168 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003169 res = eval_index_inner(tv, is_slice, var1, var2,
3170 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003171 clear_tv(var1);
3172 if (is_slice)
3173 clear_tv(var2);
3174 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3175 if (res == FAIL)
3176 goto on_error;
3177 }
3178 break;
3179
Bram Moolenaar9af78762020-06-16 11:34:42 +02003180 case ISN_SLICE:
3181 {
3182 list_T *list;
3183 int count = iptr->isn_arg.number;
3184
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003185 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003186 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003187 list = tv->vval.v_list;
3188
3189 // no error for short list, expect it to be checked earlier
3190 if (list != NULL && list->lv_len >= count)
3191 {
3192 list_T *newlist = list_slice(list,
3193 count, list->lv_len - 1);
3194
3195 if (newlist != NULL)
3196 {
3197 list_unref(list);
3198 tv->vval.v_list = newlist;
3199 ++newlist->lv_refcount;
3200 }
3201 }
3202 }
3203 break;
3204
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003205 case ISN_GETITEM:
3206 {
3207 listitem_T *li;
3208 int index = iptr->isn_arg.number;
3209
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003210 // Get list item: list is at stack-1, push item.
3211 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003212 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003213 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003214
3215 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3216 goto failed;
3217 ++ectx.ec_stack.ga_len;
3218 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003219
3220 // Useful when used in unpack assignment. Reset at
3221 // ISN_DROP.
3222 where.wt_index = index + 1;
3223 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003224 }
3225 break;
3226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 case ISN_MEMBER:
3228 {
3229 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003230 char_u *key;
3231 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003232 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003233
3234 // dict member: dict is at stack-2, key at stack-1
3235 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003236 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003237 dict = tv->vval.v_dict;
3238
3239 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003240 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003241 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003242 if (key == NULL)
3243 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003244
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003245 if ((di = dict_find(dict, key, -1)) == NULL)
3246 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003247 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003248 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003249
3250 // If :silent! is used we will continue, make sure the
3251 // stack contents makes sense.
3252 clear_tv(tv);
3253 --ectx.ec_stack.ga_len;
3254 tv = STACK_TV_BOT(-1);
3255 clear_tv(tv);
3256 tv->v_type = VAR_NUMBER;
3257 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003258 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003259 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003260 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003261 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003262 // Clear the dict only after getting the item, to avoid
3263 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003264 tv = STACK_TV_BOT(-1);
3265 temp_tv = *tv;
3266 copy_tv(&di->di_tv, tv);
3267 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003268 }
3269 break;
3270
3271 // dict member with string key
3272 case ISN_STRINGMEMBER:
3273 {
3274 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003276 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277
3278 tv = STACK_TV_BOT(-1);
3279 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3280 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003281 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003283 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 }
3285 dict = tv->vval.v_dict;
3286
3287 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3288 == NULL)
3289 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003290 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003292 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003294 // Clear the dict after getting the item, to avoid that it
3295 // make the item invalid.
3296 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003298 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 }
3300 break;
3301
3302 case ISN_NEGATENR:
3303 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003304 if (tv->v_type != VAR_NUMBER
3305#ifdef FEAT_FLOAT
3306 && tv->v_type != VAR_FLOAT
3307#endif
3308 )
3309 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003310 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003311 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003312 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003313 }
3314#ifdef FEAT_FLOAT
3315 if (tv->v_type == VAR_FLOAT)
3316 tv->vval.v_float = -tv->vval.v_float;
3317 else
3318#endif
3319 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 break;
3321
3322 case ISN_CHECKNR:
3323 {
3324 int error = FALSE;
3325
3326 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003327 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003329 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 (void)tv_get_number_chk(tv, &error);
3331 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003332 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 }
3334 break;
3335
3336 case ISN_CHECKTYPE:
3337 {
3338 checktype_T *ct = &iptr->isn_arg.type;
3339
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003340 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003341 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003342 if (!where.wt_variable)
3343 where.wt_index = ct->ct_arg_idx;
3344 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003345 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003346 if (!where.wt_variable)
3347 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003348
3349 // number 0 is FALSE, number 1 is TRUE
3350 if (tv->v_type == VAR_NUMBER
3351 && ct->ct_type->tt_type == VAR_BOOL
3352 && (tv->vval.v_number == 0
3353 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003355 tv->v_type = VAR_BOOL;
3356 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003357 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 }
3359 }
3360 break;
3361
Bram Moolenaar9af78762020-06-16 11:34:42 +02003362 case ISN_CHECKLEN:
3363 {
3364 int min_len = iptr->isn_arg.checklen.cl_min_len;
3365 list_T *list = NULL;
3366
3367 tv = STACK_TV_BOT(-1);
3368 if (tv->v_type == VAR_LIST)
3369 list = tv->vval.v_list;
3370 if (list == NULL || list->lv_len < min_len
3371 || (list->lv_len > min_len
3372 && !iptr->isn_arg.checklen.cl_more_OK))
3373 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003374 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003375 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003376 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003377 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003378 }
3379 }
3380 break;
3381
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003382 case ISN_SETTYPE:
3383 {
3384 checktype_T *ct = &iptr->isn_arg.type;
3385
3386 tv = STACK_TV_BOT(-1);
3387 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3388 {
3389 free_type(tv->vval.v_dict->dv_type);
3390 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3391 }
3392 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3393 {
3394 free_type(tv->vval.v_list->lv_type);
3395 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3396 }
3397 }
3398 break;
3399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003401 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 {
3403 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003404 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405
3406 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003407 if (iptr->isn_type == ISN_2BOOL)
3408 {
3409 n = tv2bool(tv);
3410 if (iptr->isn_arg.number) // invert
3411 n = !n;
3412 }
3413 else
3414 {
3415 SOURCING_LNUM = iptr->isn_lnum;
3416 n = tv_get_bool_chk(tv, &error);
3417 if (error)
3418 goto on_error;
3419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 clear_tv(tv);
3421 tv->v_type = VAR_BOOL;
3422 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3423 }
3424 break;
3425
3426 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003427 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003428 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003429 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3430 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3431 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 break;
3433
Bram Moolenaar08597872020-12-10 19:43:40 +01003434 case ISN_RANGE:
3435 {
3436 exarg_T ea;
3437 char *errormsg;
3438
Bram Moolenaarece0b872021-01-08 20:40:45 +01003439 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003440 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003441 ea.addr_type = ADDR_LINES;
3442 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003443 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003444 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003445 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003446
3447 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3448 goto failed;
3449 ++ectx.ec_stack.ga_len;
3450 tv = STACK_TV_BOT(-1);
3451 tv->v_type = VAR_NUMBER;
3452 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003453 if (ea.addr_count == 0)
3454 tv->vval.v_number = curwin->w_cursor.lnum;
3455 else
3456 tv->vval.v_number = ea.line2;
3457 }
3458 break;
3459
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003460 case ISN_PUT:
3461 {
3462 int regname = iptr->isn_arg.put.put_regname;
3463 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3464 char_u *expr = NULL;
3465 int dir = FORWARD;
3466
Bram Moolenaar08597872020-12-10 19:43:40 +01003467 if (lnum < -2)
3468 {
3469 // line number was put on the stack by ISN_RANGE
3470 tv = STACK_TV_BOT(-1);
3471 curwin->w_cursor.lnum = tv->vval.v_number;
3472 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3473 dir = BACKWARD;
3474 --ectx.ec_stack.ga_len;
3475 }
3476 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003477 // :put! above cursor
3478 dir = BACKWARD;
3479 else if (lnum >= 0)
3480 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003481
3482 if (regname == '=')
3483 {
3484 tv = STACK_TV_BOT(-1);
3485 if (tv->v_type == VAR_STRING)
3486 expr = tv->vval.v_string;
3487 else
3488 {
3489 expr = typval2string(tv, TRUE); // allocates value
3490 clear_tv(tv);
3491 }
3492 --ectx.ec_stack.ga_len;
3493 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003494 check_cursor();
3495 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3496 vim_free(expr);
3497 }
3498 break;
3499
Bram Moolenaar02194d22020-10-24 23:08:38 +02003500 case ISN_CMDMOD:
3501 save_cmdmod = cmdmod;
3502 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003503 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003504 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3505 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003506 break;
3507
Bram Moolenaar02194d22020-10-24 23:08:38 +02003508 case ISN_CMDMOD_REV:
3509 // filter regprog is owned by the instruction, don't free it
3510 cmdmod.cmod_filter_regmatch.regprog = NULL;
3511 undo_cmdmod(&cmdmod);
3512 cmdmod = save_cmdmod;
3513 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003514 break;
3515
Bram Moolenaar792f7862020-11-23 08:31:18 +01003516 case ISN_UNPACK:
3517 {
3518 int count = iptr->isn_arg.unpack.unp_count;
3519 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3520 list_T *l;
3521 listitem_T *li;
3522 int i;
3523
3524 // Check there is a valid list to unpack.
3525 tv = STACK_TV_BOT(-1);
3526 if (tv->v_type != VAR_LIST)
3527 {
3528 SOURCING_LNUM = iptr->isn_lnum;
3529 emsg(_(e_for_argument_must_be_sequence_of_lists));
3530 goto on_error;
3531 }
3532 l = tv->vval.v_list;
3533 if (l == NULL
3534 || l->lv_len < (semicolon ? count - 1 : count))
3535 {
3536 SOURCING_LNUM = iptr->isn_lnum;
3537 emsg(_(e_list_value_does_not_have_enough_items));
3538 goto on_error;
3539 }
3540 else if (!semicolon && l->lv_len > count)
3541 {
3542 SOURCING_LNUM = iptr->isn_lnum;
3543 emsg(_(e_list_value_has_more_items_than_targets));
3544 goto on_error;
3545 }
3546
3547 CHECK_LIST_MATERIALIZE(l);
3548 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3549 goto failed;
3550 ectx.ec_stack.ga_len += count - 1;
3551
3552 // Variable after semicolon gets a list with the remaining
3553 // items.
3554 if (semicolon)
3555 {
3556 list_T *rem_list =
3557 list_alloc_with_items(l->lv_len - count + 1);
3558
3559 if (rem_list == NULL)
3560 goto failed;
3561 tv = STACK_TV_BOT(-count);
3562 tv->vval.v_list = rem_list;
3563 ++rem_list->lv_refcount;
3564 tv->v_lock = 0;
3565 li = l->lv_first;
3566 for (i = 0; i < count - 1; ++i)
3567 li = li->li_next;
3568 for (i = 0; li != NULL; ++i)
3569 {
3570 list_set_item(rem_list, i, &li->li_tv);
3571 li = li->li_next;
3572 }
3573 --count;
3574 }
3575
3576 // Produce the values in reverse order, first item last.
3577 li = l->lv_first;
3578 for (i = 0; i < count; ++i)
3579 {
3580 tv = STACK_TV_BOT(-i - 1);
3581 copy_tv(&li->li_tv, tv);
3582 li = li->li_next;
3583 }
3584
3585 list_unref(l);
3586 }
3587 break;
3588
Bram Moolenaarb2049902021-01-24 12:53:53 +01003589 case ISN_PROF_START:
3590 case ISN_PROF_END:
3591 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003592#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003593 funccall_T cookie;
3594 ufunc_T *cur_ufunc =
3595 (((dfunc_T *)def_functions.ga_data)
3596 + ectx.ec_dfunc_idx)->df_ufunc;
3597
3598 cookie.func = cur_ufunc;
3599 if (iptr->isn_type == ISN_PROF_START)
3600 {
3601 func_line_start(&cookie, iptr->isn_lnum);
3602 // if we get here the instruction is executed
3603 func_line_exec(&cookie);
3604 }
3605 else
3606 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003607#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003608 }
3609 break;
3610
Bram Moolenaar389df252020-07-09 21:20:47 +02003611 case ISN_SHUFFLE:
3612 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003613 typval_T tmp_tv;
3614 int item = iptr->isn_arg.shuffle.shfl_item;
3615 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003616
3617 tmp_tv = *STACK_TV_BOT(-item);
3618 for ( ; up > 0 && item > 1; --up)
3619 {
3620 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3621 --item;
3622 }
3623 *STACK_TV_BOT(-item) = tmp_tv;
3624 }
3625 break;
3626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 case ISN_DROP:
3628 --ectx.ec_stack.ga_len;
3629 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003630 where.wt_index = 0;
3631 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 break;
3633 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003634 continue;
3635
Bram Moolenaard032f342020-07-18 18:13:02 +02003636func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003637 // Restore previous function. If the frame pointer is where we started
3638 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003639 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003640 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003641
Bram Moolenaard032f342020-07-18 18:13:02 +02003642 if (func_return(&ectx) == FAIL)
3643 // only fails when out of memory
3644 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003645 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003646
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003647on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003648 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003649 // If "emsg_silent" is set then ignore the error, unless it was set
3650 // when calling the function.
3651 if (did_emsg_cumul + did_emsg == did_emsg_before
3652 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003653 {
3654 // If a sequence of instructions causes an error while ":silent!"
3655 // was used, restore the stack length and jump ahead to restoring
3656 // the cmdmod.
3657 if (restore_cmdmod)
3658 {
3659 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3660 {
3661 --ectx.ec_stack.ga_len;
3662 clear_tv(STACK_TV_BOT(0));
3663 }
3664 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3665 ++ectx.ec_iidx;
3666 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003667 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003668 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003669on_fatal_error:
3670 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003671 // If we are not inside a try-catch started here, abort execution.
3672 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003673 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 }
3675
3676done:
3677 // function finished, get result from the stack.
3678 tv = STACK_TV_BOT(-1);
3679 *rettv = *tv;
3680 tv->v_type = VAR_UNKNOWN;
3681 ret = OK;
3682
3683failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003684 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003685 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003686 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003687
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003688 // Deal with any remaining closures, they may be in use somewhere.
3689 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003690 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003691 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003692 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3693 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003694
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003695 estack_pop();
3696 current_sctx = save_current_sctx;
3697
Bram Moolenaar352134b2020-10-17 22:04:08 +02003698 if (*msg_list != NULL && saved_msg_list != NULL)
3699 {
3700 msglist_T **plist = saved_msg_list;
3701
3702 // Append entries from the current msg_list (uncaught exceptions) to
3703 // the saved msg_list.
3704 while (*plist != NULL)
3705 plist = &(*plist)->next;
3706
3707 *plist = *msg_list;
3708 }
3709 msg_list = saved_msg_list;
3710
Bram Moolenaar02194d22020-10-24 23:08:38 +02003711 if (restore_cmdmod)
3712 {
3713 cmdmod.cmod_filter_regmatch.regprog = NULL;
3714 undo_cmdmod(&cmdmod);
3715 cmdmod = save_cmdmod;
3716 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003717 emsg_silent_def = save_emsg_silent_def;
3718 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003719
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003720failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003721 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3723 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003726 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003727
Bram Moolenaar0186e582021-01-10 18:33:11 +01003728 while (ectx.ec_outer != NULL)
3729 {
3730 outer_T *up = ectx.ec_outer->out_up_is_copy
3731 ? NULL : ectx.ec_outer->out_up;
3732
3733 vim_free(ectx.ec_outer);
3734 ectx.ec_outer = up;
3735 }
3736
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003737 // Not sure if this is necessary.
3738 suppress_errthrow = save_suppress_errthrow;
3739
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003740 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003741 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003742 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003743 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 return ret;
3745}
3746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003748 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003749 * We don't really need this at runtime, but we do have tests that require it,
3750 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 */
3752 void
3753ex_disassemble(exarg_T *eap)
3754{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003755 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003756 char_u *fname;
3757 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 dfunc_T *dfunc;
3759 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003760 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 int current;
3762 int line_idx = 0;
3763 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003764 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003766 if (STRNCMP(arg, "<lambda>", 8) == 0)
3767 {
3768 arg += 8;
3769 (void)getdigits(&arg);
3770 fname = vim_strnsave(eap->arg, arg - eap->arg);
3771 }
3772 else
3773 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003774 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003775 if (fname == NULL)
3776 {
3777 semsg(_(e_invarg2), eap->arg);
3778 return;
3779 }
3780
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003781 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003782 if (ufunc == NULL)
3783 {
3784 char_u *p = untrans_function_name(fname);
3785
3786 if (p != NULL)
3787 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003788 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003789 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003790 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 if (ufunc == NULL)
3792 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003793 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 return;
3795 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003796 if (func_needs_compiling(ufunc, eap->forceit)
3797 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003798 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003799 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003801 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 return;
3803 }
3804 if (ufunc->uf_name_exp != NULL)
3805 msg((char *)ufunc->uf_name_exp);
3806 else
3807 msg((char *)ufunc->uf_name);
3808
3809 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003810#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003811 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3812 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3813 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003814#else
3815 instr = dfunc->df_instr;
3816 instr_count = dfunc->df_instr_count;
3817#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003818 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 {
3820 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003821 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822
3823 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3824 {
3825 if (current > prev_current)
3826 {
3827 msg_puts("\n\n");
3828 prev_current = current;
3829 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003830 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3831 if (line != NULL)
3832 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 }
3834
3835 switch (iptr->isn_type)
3836 {
3837 case ISN_EXEC:
3838 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3839 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003840 case ISN_EXECCONCAT:
3841 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003842 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003843 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 case ISN_ECHO:
3845 {
3846 echo_T *echo = &iptr->isn_arg.echo;
3847
3848 smsg("%4d %s %d", current,
3849 echo->echo_with_white ? "ECHO" : "ECHON",
3850 echo->echo_count);
3851 }
3852 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003853 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003854 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003855 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003856 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003857 case ISN_ECHOMSG:
3858 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003859 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003860 break;
3861 case ISN_ECHOERR:
3862 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003863 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003864 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003866 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003867 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003868 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003869 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003870 + STACK_FRAME_SIZE));
3871 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003872 smsg("%4d LOAD $%lld", current,
3873 (varnumber_T)(iptr->isn_arg.number));
3874 }
3875 break;
3876 case ISN_LOADOUTER:
3877 {
3878 if (iptr->isn_arg.number < 0)
3879 smsg("%4d LOADOUTER level %d arg[%d]", current,
3880 iptr->isn_arg.outer.outer_depth,
3881 iptr->isn_arg.outer.outer_idx
3882 + STACK_FRAME_SIZE);
3883 else
3884 smsg("%4d LOADOUTER level %d $%d", current,
3885 iptr->isn_arg.outer.outer_depth,
3886 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 break;
3889 case ISN_LOADV:
3890 smsg("%4d LOADV v:%s", current,
3891 get_vim_var_name(iptr->isn_arg.number));
3892 break;
3893 case ISN_LOADSCRIPT:
3894 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003895 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3896 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003898 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003900 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3901 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003902 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003903 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 }
3905 break;
3906 case ISN_LOADS:
3907 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003908 scriptitem_T *si = SCRIPT_ITEM(
3909 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910
3911 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003912 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 }
3914 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003915 case ISN_LOADAUTO:
3916 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3917 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 case ISN_LOADG:
3919 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3920 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003921 case ISN_LOADB:
3922 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3923 break;
3924 case ISN_LOADW:
3925 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3926 break;
3927 case ISN_LOADT:
3928 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3929 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003930 case ISN_LOADGDICT:
3931 smsg("%4d LOAD g:", current);
3932 break;
3933 case ISN_LOADBDICT:
3934 smsg("%4d LOAD b:", current);
3935 break;
3936 case ISN_LOADWDICT:
3937 smsg("%4d LOAD w:", current);
3938 break;
3939 case ISN_LOADTDICT:
3940 smsg("%4d LOAD t:", current);
3941 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 case ISN_LOADOPT:
3943 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3944 break;
3945 case ISN_LOADENV:
3946 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3947 break;
3948 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003949 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 break;
3951
3952 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003953 if (iptr->isn_arg.number < 0)
3954 smsg("%4d STORE arg[%lld]", current,
3955 iptr->isn_arg.number + STACK_FRAME_SIZE);
3956 else
3957 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3958 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003959 case ISN_STOREOUTER:
3960 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003961 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003962 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3963 iptr->isn_arg.outer.outer_depth,
3964 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003965 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003966 smsg("%4d STOREOUTER level %d $%d", current,
3967 iptr->isn_arg.outer.outer_depth,
3968 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003971 case ISN_STOREV:
3972 smsg("%4d STOREV v:%s", current,
3973 get_vim_var_name(iptr->isn_arg.number));
3974 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003975 case ISN_STOREAUTO:
3976 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3977 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003979 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3980 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003981 case ISN_STOREB:
3982 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3983 break;
3984 case ISN_STOREW:
3985 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3986 break;
3987 case ISN_STORET:
3988 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3989 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003990 case ISN_STORES:
3991 {
3992 scriptitem_T *si = SCRIPT_ITEM(
3993 iptr->isn_arg.loadstore.ls_sid);
3994
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003995 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003996 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003997 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 break;
3999 case ISN_STORESCRIPT:
4000 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004001 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4002 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004004 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004006 smsg("%4d STORESCRIPT %s-%d in %s", current,
4007 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004008 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004009 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 }
4011 break;
4012 case ISN_STOREOPT:
4013 smsg("%4d STOREOPT &%s", current,
4014 iptr->isn_arg.storeopt.so_name);
4015 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004016 case ISN_STOREENV:
4017 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4018 break;
4019 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004020 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004021 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 case ISN_STORENR:
4023 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004024 iptr->isn_arg.storenr.stnr_val,
4025 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 break;
4027
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004028 case ISN_STOREINDEX:
4029 switch (iptr->isn_arg.vartype)
4030 {
4031 case VAR_LIST:
4032 smsg("%4d STORELIST", current);
4033 break;
4034 case VAR_DICT:
4035 smsg("%4d STOREDICT", current);
4036 break;
4037 case VAR_ANY:
4038 smsg("%4d STOREINDEX", current);
4039 break;
4040 default: break;
4041 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004042 break;
4043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 // constants
4045 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004046 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004047 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 break;
4049 case ISN_PUSHBOOL:
4050 case ISN_PUSHSPEC:
4051 smsg("%4d PUSH %s", current,
4052 get_var_special_name(iptr->isn_arg.number));
4053 break;
4054 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004055#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004057#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 break;
4059 case ISN_PUSHS:
4060 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4061 break;
4062 case ISN_PUSHBLOB:
4063 {
4064 char_u *r;
4065 char_u numbuf[NUMBUFLEN];
4066 char_u *tofree;
4067
4068 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004069 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 vim_free(tofree);
4071 }
4072 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004073 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004074 {
4075 char *name = (char *)iptr->isn_arg.string;
4076
4077 smsg("%4d PUSHFUNC \"%s\"", current,
4078 name == NULL ? "[none]" : name);
4079 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004080 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004081 case ISN_PUSHCHANNEL:
4082#ifdef FEAT_JOB_CHANNEL
4083 {
4084 channel_T *channel = iptr->isn_arg.channel;
4085
4086 smsg("%4d PUSHCHANNEL %d", current,
4087 channel == NULL ? 0 : channel->ch_id);
4088 }
4089#endif
4090 break;
4091 case ISN_PUSHJOB:
4092#ifdef FEAT_JOB_CHANNEL
4093 {
4094 typval_T tv;
4095 char_u *name;
4096
4097 tv.v_type = VAR_JOB;
4098 tv.vval.v_job = iptr->isn_arg.job;
4099 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004100 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004101 }
4102#endif
4103 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 case ISN_PUSHEXC:
4105 smsg("%4d PUSH v:exception", current);
4106 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004107 case ISN_UNLET:
4108 smsg("%4d UNLET%s %s", current,
4109 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4110 iptr->isn_arg.unlet.ul_name);
4111 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004112 case ISN_UNLETENV:
4113 smsg("%4d UNLETENV%s $%s", current,
4114 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4115 iptr->isn_arg.unlet.ul_name);
4116 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004117 case ISN_UNLETINDEX:
4118 smsg("%4d UNLETINDEX", current);
4119 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004120 case ISN_LOCKCONST:
4121 smsg("%4d LOCKCONST", current);
4122 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004124 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004125 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 break;
4127 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004128 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004129 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 break;
4131
4132 // function call
4133 case ISN_BCALL:
4134 {
4135 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4136
4137 smsg("%4d BCALL %s(argc %d)", current,
4138 internal_func_name(cbfunc->cbf_idx),
4139 cbfunc->cbf_argcount);
4140 }
4141 break;
4142 case ISN_DCALL:
4143 {
4144 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4145 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4146 + cdfunc->cdf_idx;
4147
4148 smsg("%4d DCALL %s(argc %d)", current,
4149 df->df_ufunc->uf_name_exp != NULL
4150 ? df->df_ufunc->uf_name_exp
4151 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4152 }
4153 break;
4154 case ISN_UCALL:
4155 {
4156 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4157
4158 smsg("%4d UCALL %s(argc %d)", current,
4159 cufunc->cuf_name, cufunc->cuf_argcount);
4160 }
4161 break;
4162 case ISN_PCALL:
4163 {
4164 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4165
4166 smsg("%4d PCALL%s (argc %d)", current,
4167 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4168 }
4169 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004170 case ISN_PCALL_END:
4171 smsg("%4d PCALL end", current);
4172 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 case ISN_RETURN:
4174 smsg("%4d RETURN", current);
4175 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004176 case ISN_RETURN_ZERO:
4177 smsg("%4d RETURN 0", current);
4178 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 case ISN_FUNCREF:
4180 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004181 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004183 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004185 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 }
4187 break;
4188
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004189 case ISN_NEWFUNC:
4190 {
4191 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4192
4193 smsg("%4d NEWFUNC %s %s", current,
4194 newfunc->nf_lambda, newfunc->nf_global);
4195 }
4196 break;
4197
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004198 case ISN_DEF:
4199 {
4200 char_u *name = iptr->isn_arg.string;
4201
4202 smsg("%4d DEF %s", current,
4203 name == NULL ? (char_u *)"" : name);
4204 }
4205 break;
4206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 case ISN_JUMP:
4208 {
4209 char *when = "?";
4210
4211 switch (iptr->isn_arg.jump.jump_when)
4212 {
4213 case JUMP_ALWAYS:
4214 when = "JUMP";
4215 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 case JUMP_AND_KEEP_IF_TRUE:
4217 when = "JUMP_AND_KEEP_IF_TRUE";
4218 break;
4219 case JUMP_IF_FALSE:
4220 when = "JUMP_IF_FALSE";
4221 break;
4222 case JUMP_AND_KEEP_IF_FALSE:
4223 when = "JUMP_AND_KEEP_IF_FALSE";
4224 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004225 case JUMP_IF_COND_FALSE:
4226 when = "JUMP_IF_COND_FALSE";
4227 break;
4228 case JUMP_IF_COND_TRUE:
4229 when = "JUMP_IF_COND_TRUE";
4230 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004232 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 iptr->isn_arg.jump.jump_where);
4234 }
4235 break;
4236
4237 case ISN_FOR:
4238 {
4239 forloop_T *forloop = &iptr->isn_arg.forloop;
4240
4241 smsg("%4d FOR $%d -> %d", current,
4242 forloop->for_idx, forloop->for_end);
4243 }
4244 break;
4245
4246 case ISN_TRY:
4247 {
4248 try_T *try = &iptr->isn_arg.try;
4249
Bram Moolenaarc150c092021-02-13 15:02:46 +01004250 smsg("%4d TRY catch -> %d, %s -> %d", current,
4251 try->try_catch,
4252 instr[try->try_finally].isn_type == ISN_ENDTRY
4253 ? "end" : "finally",
4254 try->try_finally);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 }
4256 break;
4257 case ISN_CATCH:
4258 // TODO
4259 smsg("%4d CATCH", current);
4260 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004261 case ISN_TRYCONT:
4262 {
4263 trycont_T *trycont = &iptr->isn_arg.trycont;
4264
4265 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4266 trycont->tct_levels,
4267 trycont->tct_levels == 1 ? "" : "s",
4268 trycont->tct_where);
4269 }
4270 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 case ISN_ENDTRY:
4272 smsg("%4d ENDTRY", current);
4273 break;
4274 case ISN_THROW:
4275 smsg("%4d THROW", current);
4276 break;
4277
4278 // expression operations on number
4279 case ISN_OPNR:
4280 case ISN_OPFLOAT:
4281 case ISN_OPANY:
4282 {
4283 char *what;
4284 char *ins;
4285
4286 switch (iptr->isn_arg.op.op_type)
4287 {
4288 case EXPR_MULT: what = "*"; break;
4289 case EXPR_DIV: what = "/"; break;
4290 case EXPR_REM: what = "%"; break;
4291 case EXPR_SUB: what = "-"; break;
4292 case EXPR_ADD: what = "+"; break;
4293 default: what = "???"; break;
4294 }
4295 switch (iptr->isn_type)
4296 {
4297 case ISN_OPNR: ins = "OPNR"; break;
4298 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4299 case ISN_OPANY: ins = "OPANY"; break;
4300 default: ins = "???"; break;
4301 }
4302 smsg("%4d %s %s", current, ins, what);
4303 }
4304 break;
4305
4306 case ISN_COMPAREBOOL:
4307 case ISN_COMPARESPECIAL:
4308 case ISN_COMPARENR:
4309 case ISN_COMPAREFLOAT:
4310 case ISN_COMPARESTRING:
4311 case ISN_COMPAREBLOB:
4312 case ISN_COMPARELIST:
4313 case ISN_COMPAREDICT:
4314 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 case ISN_COMPAREANY:
4316 {
4317 char *p;
4318 char buf[10];
4319 char *type;
4320
4321 switch (iptr->isn_arg.op.op_type)
4322 {
4323 case EXPR_EQUAL: p = "=="; break;
4324 case EXPR_NEQUAL: p = "!="; break;
4325 case EXPR_GREATER: p = ">"; break;
4326 case EXPR_GEQUAL: p = ">="; break;
4327 case EXPR_SMALLER: p = "<"; break;
4328 case EXPR_SEQUAL: p = "<="; break;
4329 case EXPR_MATCH: p = "=~"; break;
4330 case EXPR_IS: p = "is"; break;
4331 case EXPR_ISNOT: p = "isnot"; break;
4332 case EXPR_NOMATCH: p = "!~"; break;
4333 default: p = "???"; break;
4334 }
4335 STRCPY(buf, p);
4336 if (iptr->isn_arg.op.op_ic == TRUE)
4337 strcat(buf, "?");
4338 switch(iptr->isn_type)
4339 {
4340 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4341 case ISN_COMPARESPECIAL:
4342 type = "COMPARESPECIAL"; break;
4343 case ISN_COMPARENR: type = "COMPARENR"; break;
4344 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4345 case ISN_COMPARESTRING:
4346 type = "COMPARESTRING"; break;
4347 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4348 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4349 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4350 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4352 default: type = "???"; break;
4353 }
4354
4355 smsg("%4d %s %s", current, type, buf);
4356 }
4357 break;
4358
4359 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4360 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4361
4362 // expression operations
4363 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004364 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004365 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004366 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004367 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004368 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004369 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004370 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4371 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004372 case ISN_SLICE: smsg("%4d SLICE %lld",
4373 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004374 case ISN_GETITEM: smsg("%4d ITEM %lld",
4375 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004376 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4377 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 iptr->isn_arg.string); break;
4379 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4380
4381 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004382 case ISN_CHECKTYPE:
4383 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004384 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004385 char *tofree;
4386
Bram Moolenaare32e5162021-01-21 20:21:29 +01004387 if (ct->ct_arg_idx == 0)
4388 smsg("%4d CHECKTYPE %s stack[%d]", current,
4389 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004390 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004391 else
4392 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4393 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004394 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004395 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004396 vim_free(tofree);
4397 break;
4398 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004399 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4400 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4401 iptr->isn_arg.checklen.cl_min_len);
4402 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004403 case ISN_SETTYPE:
4404 {
4405 char *tofree;
4406
4407 smsg("%4d SETTYPE %s", current,
4408 type_name(iptr->isn_arg.type.ct_type, &tofree));
4409 vim_free(tofree);
4410 break;
4411 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004412 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 case ISN_2BOOL: if (iptr->isn_arg.number)
4414 smsg("%4d INVERT (!val)", current);
4415 else
4416 smsg("%4d 2BOOL (!!val)", current);
4417 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004418 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004419 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004420 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004421 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004422 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004423 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004424 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4425 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004426 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004427 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4428 smsg("%4d PUT %c above range",
4429 current, iptr->isn_arg.put.put_regname);
4430 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4431 smsg("%4d PUT %c range",
4432 current, iptr->isn_arg.put.put_regname);
4433 else
4434 smsg("%4d PUT %c %ld", current,
4435 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004436 (long)iptr->isn_arg.put.put_lnum);
4437 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438
Bram Moolenaar02194d22020-10-24 23:08:38 +02004439 // TODO: summarize modifiers
4440 case ISN_CMDMOD:
4441 {
4442 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004443 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004444 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4445
4446 buf = alloc(len + 1);
4447 if (buf != NULL)
4448 {
4449 (void)produce_cmdmods(
4450 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4451 smsg("%4d CMDMOD %s", current, buf);
4452 vim_free(buf);
4453 }
4454 break;
4455 }
4456 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004457
Bram Moolenaarb2049902021-01-24 12:53:53 +01004458 case ISN_PROF_START:
4459 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4460 break;
4461
4462 case ISN_PROF_END:
4463 smsg("%4d PROFILE END", current);
4464 break;
4465
Bram Moolenaar792f7862020-11-23 08:31:18 +01004466 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4467 iptr->isn_arg.unpack.unp_count,
4468 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4469 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004470 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4471 iptr->isn_arg.shuffle.shfl_item,
4472 iptr->isn_arg.shuffle.shfl_up);
4473 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 case ISN_DROP: smsg("%4d DROP", current); break;
4475 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004476
4477 out_flush(); // output one line at a time
4478 ui_breakcheck();
4479 if (got_int)
4480 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482}
4483
4484/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004485 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 * list, etc. Mostly like what JavaScript does, except that empty list and
4487 * empty dictionary are FALSE.
4488 */
4489 int
4490tv2bool(typval_T *tv)
4491{
4492 switch (tv->v_type)
4493 {
4494 case VAR_NUMBER:
4495 return tv->vval.v_number != 0;
4496 case VAR_FLOAT:
4497#ifdef FEAT_FLOAT
4498 return tv->vval.v_float != 0.0;
4499#else
4500 break;
4501#endif
4502 case VAR_PARTIAL:
4503 return tv->vval.v_partial != NULL;
4504 case VAR_FUNC:
4505 case VAR_STRING:
4506 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4507 case VAR_LIST:
4508 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4509 case VAR_DICT:
4510 return tv->vval.v_dict != NULL
4511 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4512 case VAR_BOOL:
4513 case VAR_SPECIAL:
4514 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4515 case VAR_JOB:
4516#ifdef FEAT_JOB_CHANNEL
4517 return tv->vval.v_job != NULL;
4518#else
4519 break;
4520#endif
4521 case VAR_CHANNEL:
4522#ifdef FEAT_JOB_CHANNEL
4523 return tv->vval.v_channel != NULL;
4524#else
4525 break;
4526#endif
4527 case VAR_BLOB:
4528 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4529 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004530 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 case VAR_VOID:
4532 break;
4533 }
4534 return FALSE;
4535}
4536
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004537 void
4538emsg_using_string_as(typval_T *tv, int as_number)
4539{
4540 semsg(_(as_number ? e_using_string_as_number_str
4541 : e_using_string_as_bool_str),
4542 tv->vval.v_string == NULL
4543 ? (char_u *)"" : tv->vval.v_string);
4544}
4545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546/*
4547 * If "tv" is a string give an error and return FAIL.
4548 */
4549 int
4550check_not_string(typval_T *tv)
4551{
4552 if (tv->v_type == VAR_STRING)
4553 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004554 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 clear_tv(tv);
4556 return FAIL;
4557 }
4558 return OK;
4559}
4560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561
4562#endif // FEAT_EVAL