blob: 200139c5b28fd7ef0ae75126d3f29bf0c6c29dcd [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 Moolenaar8d4be892021-02-13 18:33:02 +01002566 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002567 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002568 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2570 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
2571 }
2572 break;
2573
2574 case ISN_PUSHEXC:
2575 if (current_exception == NULL)
2576 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002577 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 iemsg("Evaluating catch while current_exception is NULL");
2579 goto failed;
2580 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002581 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 goto failed;
2583 tv = STACK_TV_BOT(0);
2584 ++ectx.ec_stack.ga_len;
2585 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002586 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 tv->vval.v_string = vim_strsave(
2588 (char_u *)current_exception->value);
2589 break;
2590
2591 case ISN_CATCH:
2592 {
2593 garray_T *trystack = &ectx.ec_trystack;
2594
Bram Moolenaar20a76292020-12-25 19:47:24 +01002595 if (restore_cmdmod)
2596 {
2597 cmdmod.cmod_filter_regmatch.regprog = NULL;
2598 undo_cmdmod(&cmdmod);
2599 cmdmod = save_cmdmod;
2600 restore_cmdmod = FALSE;
2601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 if (trystack->ga_len > 0)
2603 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002604 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 + trystack->ga_len - 1;
2606 trycmd->tcd_caught = TRUE;
2607 }
2608 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002609 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 catch_exception(current_exception);
2611 }
2612 break;
2613
Bram Moolenaarc150c092021-02-13 15:02:46 +01002614 case ISN_TRYCONT:
2615 {
2616 garray_T *trystack = &ectx.ec_trystack;
2617 trycont_T *trycont = &iptr->isn_arg.trycont;
2618 int i;
2619 trycmd_T *trycmd;
2620 int iidx = trycont->tct_where;
2621
2622 if (trystack->ga_len < trycont->tct_levels)
2623 {
2624 siemsg("TRYCONT: expected %d levels, found %d",
2625 trycont->tct_levels, trystack->ga_len);
2626 goto failed;
2627 }
2628 // Make :endtry jump to any outer try block and the last
2629 // :endtry inside the loop to the loop start.
2630 for (i = trycont->tct_levels; i > 0; --i)
2631 {
2632 trycmd = ((trycmd_T *)trystack->ga_data)
2633 + trystack->ga_len - i;
2634 trycmd->tcd_cont = iidx;
2635 iidx = trycmd->tcd_finally_idx;
2636 }
2637 // jump to :finally or :endtry of current try statement
2638 ectx.ec_iidx = iidx;
2639 }
2640 break;
2641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 // end of ":try" block
2643 case ISN_ENDTRY:
2644 {
2645 garray_T *trystack = &ectx.ec_trystack;
2646
2647 if (trystack->ga_len > 0)
2648 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002649 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 --trystack->ga_len;
2652 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002653 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 trycmd = ((trycmd_T *)trystack->ga_data)
2655 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002656 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 {
2658 // discard the exception
2659 if (caught_stack == current_exception)
2660 caught_stack = caught_stack->caught;
2661 discard_current_exception();
2662 }
2663
2664 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002665 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002666
2667 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2668 {
2669 --ectx.ec_stack.ga_len;
2670 clear_tv(STACK_TV_BOT(0));
2671 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002672 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002673 // handling :continue: jump to outer try block or
2674 // start of the loop
2675 ectx.ec_iidx = trycmd->tcd_cont;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 }
2677 }
2678 break;
2679
2680 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002681 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002682 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002683
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002684 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2685 {
2686 // throwing an exception while using "silent!" causes
2687 // the function to abort but not display an error.
2688 tv = STACK_TV_BOT(-1);
2689 clear_tv(tv);
2690 tv->v_type = VAR_NUMBER;
2691 tv->vval.v_number = 0;
2692 goto done;
2693 }
2694 --ectx.ec_stack.ga_len;
2695 tv = STACK_TV_BOT(0);
2696 if (tv->vval.v_string == NULL
2697 || *skipwhite(tv->vval.v_string) == NUL)
2698 {
2699 vim_free(tv->vval.v_string);
2700 SOURCING_LNUM = iptr->isn_lnum;
2701 emsg(_(e_throw_with_empty_string));
2702 goto failed;
2703 }
2704
2705 // Inside a "catch" we need to first discard the caught
2706 // exception.
2707 if (trystack->ga_len > 0)
2708 {
2709 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2710 + trystack->ga_len - 1;
2711 if (trycmd->tcd_caught && current_exception != NULL)
2712 {
2713 // discard the exception
2714 if (caught_stack == current_exception)
2715 caught_stack = caught_stack->caught;
2716 discard_current_exception();
2717 trycmd->tcd_caught = FALSE;
2718 }
2719 }
2720
2721 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2722 == FAIL)
2723 {
2724 vim_free(tv->vval.v_string);
2725 goto failed;
2726 }
2727 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 break;
2730
2731 // compare with special values
2732 case ISN_COMPAREBOOL:
2733 case ISN_COMPARESPECIAL:
2734 {
2735 typval_T *tv1 = STACK_TV_BOT(-2);
2736 typval_T *tv2 = STACK_TV_BOT(-1);
2737 varnumber_T arg1 = tv1->vval.v_number;
2738 varnumber_T arg2 = tv2->vval.v_number;
2739 int res;
2740
2741 switch (iptr->isn_arg.op.op_type)
2742 {
2743 case EXPR_EQUAL: res = arg1 == arg2; break;
2744 case EXPR_NEQUAL: res = arg1 != arg2; break;
2745 default: res = 0; break;
2746 }
2747
2748 --ectx.ec_stack.ga_len;
2749 tv1->v_type = VAR_BOOL;
2750 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2751 }
2752 break;
2753
2754 // Operation with two number arguments
2755 case ISN_OPNR:
2756 case ISN_COMPARENR:
2757 {
2758 typval_T *tv1 = STACK_TV_BOT(-2);
2759 typval_T *tv2 = STACK_TV_BOT(-1);
2760 varnumber_T arg1 = tv1->vval.v_number;
2761 varnumber_T arg2 = tv2->vval.v_number;
2762 varnumber_T res;
2763
2764 switch (iptr->isn_arg.op.op_type)
2765 {
2766 case EXPR_MULT: res = arg1 * arg2; break;
2767 case EXPR_DIV: res = arg1 / arg2; break;
2768 case EXPR_REM: res = arg1 % arg2; break;
2769 case EXPR_SUB: res = arg1 - arg2; break;
2770 case EXPR_ADD: res = arg1 + arg2; break;
2771
2772 case EXPR_EQUAL: res = arg1 == arg2; break;
2773 case EXPR_NEQUAL: res = arg1 != arg2; break;
2774 case EXPR_GREATER: res = arg1 > arg2; break;
2775 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2776 case EXPR_SMALLER: res = arg1 < arg2; break;
2777 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2778 default: res = 0; break;
2779 }
2780
2781 --ectx.ec_stack.ga_len;
2782 if (iptr->isn_type == ISN_COMPARENR)
2783 {
2784 tv1->v_type = VAR_BOOL;
2785 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2786 }
2787 else
2788 tv1->vval.v_number = res;
2789 }
2790 break;
2791
2792 // Computation with two float arguments
2793 case ISN_OPFLOAT:
2794 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002795#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 {
2797 typval_T *tv1 = STACK_TV_BOT(-2);
2798 typval_T *tv2 = STACK_TV_BOT(-1);
2799 float_T arg1 = tv1->vval.v_float;
2800 float_T arg2 = tv2->vval.v_float;
2801 float_T res = 0;
2802 int cmp = FALSE;
2803
2804 switch (iptr->isn_arg.op.op_type)
2805 {
2806 case EXPR_MULT: res = arg1 * arg2; break;
2807 case EXPR_DIV: res = arg1 / arg2; break;
2808 case EXPR_SUB: res = arg1 - arg2; break;
2809 case EXPR_ADD: res = arg1 + arg2; break;
2810
2811 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2812 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2813 case EXPR_GREATER: cmp = arg1 > arg2; break;
2814 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2815 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2816 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2817 default: cmp = 0; break;
2818 }
2819 --ectx.ec_stack.ga_len;
2820 if (iptr->isn_type == ISN_COMPAREFLOAT)
2821 {
2822 tv1->v_type = VAR_BOOL;
2823 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2824 }
2825 else
2826 tv1->vval.v_float = res;
2827 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002828#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 break;
2830
2831 case ISN_COMPARELIST:
2832 {
2833 typval_T *tv1 = STACK_TV_BOT(-2);
2834 typval_T *tv2 = STACK_TV_BOT(-1);
2835 list_T *arg1 = tv1->vval.v_list;
2836 list_T *arg2 = tv2->vval.v_list;
2837 int cmp = FALSE;
2838 int ic = iptr->isn_arg.op.op_ic;
2839
2840 switch (iptr->isn_arg.op.op_type)
2841 {
2842 case EXPR_EQUAL: cmp =
2843 list_equal(arg1, arg2, ic, FALSE); break;
2844 case EXPR_NEQUAL: cmp =
2845 !list_equal(arg1, arg2, ic, FALSE); break;
2846 case EXPR_IS: cmp = arg1 == arg2; break;
2847 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2848 default: cmp = 0; break;
2849 }
2850 --ectx.ec_stack.ga_len;
2851 clear_tv(tv1);
2852 clear_tv(tv2);
2853 tv1->v_type = VAR_BOOL;
2854 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2855 }
2856 break;
2857
2858 case ISN_COMPAREBLOB:
2859 {
2860 typval_T *tv1 = STACK_TV_BOT(-2);
2861 typval_T *tv2 = STACK_TV_BOT(-1);
2862 blob_T *arg1 = tv1->vval.v_blob;
2863 blob_T *arg2 = tv2->vval.v_blob;
2864 int cmp = FALSE;
2865
2866 switch (iptr->isn_arg.op.op_type)
2867 {
2868 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2869 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2870 case EXPR_IS: cmp = arg1 == arg2; break;
2871 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2872 default: cmp = 0; break;
2873 }
2874 --ectx.ec_stack.ga_len;
2875 clear_tv(tv1);
2876 clear_tv(tv2);
2877 tv1->v_type = VAR_BOOL;
2878 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2879 }
2880 break;
2881
2882 // TODO: handle separately
2883 case ISN_COMPARESTRING:
2884 case ISN_COMPAREDICT:
2885 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 case ISN_COMPAREANY:
2887 {
2888 typval_T *tv1 = STACK_TV_BOT(-2);
2889 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002890 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 int ic = iptr->isn_arg.op.op_ic;
2892
Bram Moolenaareb26f432020-09-14 16:50:05 +02002893 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002894 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 --ectx.ec_stack.ga_len;
2897 }
2898 break;
2899
2900 case ISN_ADDLIST:
2901 case ISN_ADDBLOB:
2902 {
2903 typval_T *tv1 = STACK_TV_BOT(-2);
2904 typval_T *tv2 = STACK_TV_BOT(-1);
2905
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002906 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 if (iptr->isn_type == ISN_ADDLIST)
2908 eval_addlist(tv1, tv2);
2909 else
2910 eval_addblob(tv1, tv2);
2911 clear_tv(tv2);
2912 --ectx.ec_stack.ga_len;
2913 }
2914 break;
2915
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002916 case ISN_LISTAPPEND:
2917 {
2918 typval_T *tv1 = STACK_TV_BOT(-2);
2919 typval_T *tv2 = STACK_TV_BOT(-1);
2920 list_T *l = tv1->vval.v_list;
2921
2922 // add an item to a list
2923 if (l == NULL)
2924 {
2925 SOURCING_LNUM = iptr->isn_lnum;
2926 emsg(_(e_cannot_add_to_null_list));
2927 goto on_error;
2928 }
2929 if (list_append_tv(l, tv2) == FAIL)
2930 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002931 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002932 --ectx.ec_stack.ga_len;
2933 }
2934 break;
2935
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002936 case ISN_BLOBAPPEND:
2937 {
2938 typval_T *tv1 = STACK_TV_BOT(-2);
2939 typval_T *tv2 = STACK_TV_BOT(-1);
2940 blob_T *b = tv1->vval.v_blob;
2941 int error = FALSE;
2942 varnumber_T n;
2943
2944 // add a number to a blob
2945 if (b == NULL)
2946 {
2947 SOURCING_LNUM = iptr->isn_lnum;
2948 emsg(_(e_cannot_add_to_null_blob));
2949 goto on_error;
2950 }
2951 n = tv_get_number_chk(tv2, &error);
2952 if (error)
2953 goto on_error;
2954 ga_append(&b->bv_ga, (int)n);
2955 --ectx.ec_stack.ga_len;
2956 }
2957 break;
2958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 // Computation with two arguments of unknown type
2960 case ISN_OPANY:
2961 {
2962 typval_T *tv1 = STACK_TV_BOT(-2);
2963 typval_T *tv2 = STACK_TV_BOT(-1);
2964 varnumber_T n1, n2;
2965#ifdef FEAT_FLOAT
2966 float_T f1 = 0, f2 = 0;
2967#endif
2968 int error = FALSE;
2969
2970 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2971 {
2972 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2973 {
2974 eval_addlist(tv1, tv2);
2975 clear_tv(tv2);
2976 --ectx.ec_stack.ga_len;
2977 break;
2978 }
2979 else if (tv1->v_type == VAR_BLOB
2980 && tv2->v_type == VAR_BLOB)
2981 {
2982 eval_addblob(tv1, tv2);
2983 clear_tv(tv2);
2984 --ectx.ec_stack.ga_len;
2985 break;
2986 }
2987 }
2988#ifdef FEAT_FLOAT
2989 if (tv1->v_type == VAR_FLOAT)
2990 {
2991 f1 = tv1->vval.v_float;
2992 n1 = 0;
2993 }
2994 else
2995#endif
2996 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002997 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 n1 = tv_get_number_chk(tv1, &error);
2999 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003000 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001#ifdef FEAT_FLOAT
3002 if (tv2->v_type == VAR_FLOAT)
3003 f1 = n1;
3004#endif
3005 }
3006#ifdef FEAT_FLOAT
3007 if (tv2->v_type == VAR_FLOAT)
3008 {
3009 f2 = tv2->vval.v_float;
3010 n2 = 0;
3011 }
3012 else
3013#endif
3014 {
3015 n2 = tv_get_number_chk(tv2, &error);
3016 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003017 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018#ifdef FEAT_FLOAT
3019 if (tv1->v_type == VAR_FLOAT)
3020 f2 = n2;
3021#endif
3022 }
3023#ifdef FEAT_FLOAT
3024 // if there is a float on either side the result is a float
3025 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3026 {
3027 switch (iptr->isn_arg.op.op_type)
3028 {
3029 case EXPR_MULT: f1 = f1 * f2; break;
3030 case EXPR_DIV: f1 = f1 / f2; break;
3031 case EXPR_SUB: f1 = f1 - f2; break;
3032 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003033 default: SOURCING_LNUM = iptr->isn_lnum;
3034 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003035 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 }
3037 clear_tv(tv1);
3038 clear_tv(tv2);
3039 tv1->v_type = VAR_FLOAT;
3040 tv1->vval.v_float = f1;
3041 --ectx.ec_stack.ga_len;
3042 }
3043 else
3044#endif
3045 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003046 int failed = FALSE;
3047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 switch (iptr->isn_arg.op.op_type)
3049 {
3050 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003051 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3052 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003053 goto on_error;
3054 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 case EXPR_SUB: n1 = n1 - n2; break;
3056 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003057 default: n1 = num_modulus(n1, n2, &failed);
3058 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003059 goto on_error;
3060 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 }
3062 clear_tv(tv1);
3063 clear_tv(tv2);
3064 tv1->v_type = VAR_NUMBER;
3065 tv1->vval.v_number = n1;
3066 --ectx.ec_stack.ga_len;
3067 }
3068 }
3069 break;
3070
3071 case ISN_CONCAT:
3072 {
3073 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3074 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3075 char_u *res;
3076
3077 res = concat_str(str1, str2);
3078 clear_tv(STACK_TV_BOT(-2));
3079 clear_tv(STACK_TV_BOT(-1));
3080 --ectx.ec_stack.ga_len;
3081 STACK_TV_BOT(-1)->vval.v_string = res;
3082 }
3083 break;
3084
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003085 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003086 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003087 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003088 int is_slice = iptr->isn_type == ISN_STRSLICE;
3089 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003090 char_u *res;
3091
3092 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003093 // string slice: string is at stack-3, first index at
3094 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003095 if (is_slice)
3096 {
3097 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003098 n1 = tv->vval.v_number;
3099 }
3100
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003101 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003102 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003103
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003104 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003105 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003106 if (is_slice)
3107 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003108 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003109 else
3110 // Index: The resulting variable is a string of a
3111 // single character. If the index is too big or
3112 // negative the result is empty.
3113 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003114 vim_free(tv->vval.v_string);
3115 tv->vval.v_string = res;
3116 }
3117 break;
3118
3119 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003120 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 {
Bram Moolenaared591872020-08-15 22:14:53 +02003122 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003124 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125
3126 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003127 // list slice: list is at stack-3, indexes at stack-2 and
3128 // stack-1
3129 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 list = tv->vval.v_list;
3131
3132 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003133 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003135
3136 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 {
Bram Moolenaared591872020-08-15 22:14:53 +02003138 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003139 n1 = tv->vval.v_number;
3140 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 }
Bram Moolenaared591872020-08-15 22:14:53 +02003142
3143 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003144 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003145 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003146 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3147 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003148 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 }
3150 break;
3151
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003152 case ISN_ANYINDEX:
3153 case ISN_ANYSLICE:
3154 {
3155 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3156 typval_T *var1, *var2;
3157 int res;
3158
3159 // index: composite is at stack-2, index at stack-1
3160 // slice: composite is at stack-3, indexes at stack-2 and
3161 // stack-1
3162 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003163 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003164 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3165 goto on_error;
3166 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3167 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003168 res = eval_index_inner(tv, is_slice, var1, var2,
3169 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003170 clear_tv(var1);
3171 if (is_slice)
3172 clear_tv(var2);
3173 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3174 if (res == FAIL)
3175 goto on_error;
3176 }
3177 break;
3178
Bram Moolenaar9af78762020-06-16 11:34:42 +02003179 case ISN_SLICE:
3180 {
3181 list_T *list;
3182 int count = iptr->isn_arg.number;
3183
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003184 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003185 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003186 list = tv->vval.v_list;
3187
3188 // no error for short list, expect it to be checked earlier
3189 if (list != NULL && list->lv_len >= count)
3190 {
3191 list_T *newlist = list_slice(list,
3192 count, list->lv_len - 1);
3193
3194 if (newlist != NULL)
3195 {
3196 list_unref(list);
3197 tv->vval.v_list = newlist;
3198 ++newlist->lv_refcount;
3199 }
3200 }
3201 }
3202 break;
3203
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003204 case ISN_GETITEM:
3205 {
3206 listitem_T *li;
3207 int index = iptr->isn_arg.number;
3208
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003209 // Get list item: list is at stack-1, push item.
3210 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003211 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003212 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003213
3214 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3215 goto failed;
3216 ++ectx.ec_stack.ga_len;
3217 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003218
3219 // Useful when used in unpack assignment. Reset at
3220 // ISN_DROP.
3221 where.wt_index = index + 1;
3222 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003223 }
3224 break;
3225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 case ISN_MEMBER:
3227 {
3228 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003229 char_u *key;
3230 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003231 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003232
3233 // dict member: dict is at stack-2, key at stack-1
3234 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003235 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003236 dict = tv->vval.v_dict;
3237
3238 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003239 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003240 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003241 if (key == NULL)
3242 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003243
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003244 if ((di = dict_find(dict, key, -1)) == NULL)
3245 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003246 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003247 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003248
3249 // If :silent! is used we will continue, make sure the
3250 // stack contents makes sense.
3251 clear_tv(tv);
3252 --ectx.ec_stack.ga_len;
3253 tv = STACK_TV_BOT(-1);
3254 clear_tv(tv);
3255 tv->v_type = VAR_NUMBER;
3256 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003257 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003258 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003259 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003260 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003261 // Clear the dict only after getting the item, to avoid
3262 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003263 tv = STACK_TV_BOT(-1);
3264 temp_tv = *tv;
3265 copy_tv(&di->di_tv, tv);
3266 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003267 }
3268 break;
3269
3270 // dict member with string key
3271 case ISN_STRINGMEMBER:
3272 {
3273 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003275 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276
3277 tv = STACK_TV_BOT(-1);
3278 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3279 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003280 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003282 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 }
3284 dict = tv->vval.v_dict;
3285
3286 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3287 == NULL)
3288 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003289 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003291 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003293 // Clear the dict after getting the item, to avoid that it
3294 // make the item invalid.
3295 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003297 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 }
3299 break;
3300
3301 case ISN_NEGATENR:
3302 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003303 if (tv->v_type != VAR_NUMBER
3304#ifdef FEAT_FLOAT
3305 && tv->v_type != VAR_FLOAT
3306#endif
3307 )
3308 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003309 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003310 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003311 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003312 }
3313#ifdef FEAT_FLOAT
3314 if (tv->v_type == VAR_FLOAT)
3315 tv->vval.v_float = -tv->vval.v_float;
3316 else
3317#endif
3318 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 break;
3320
3321 case ISN_CHECKNR:
3322 {
3323 int error = FALSE;
3324
3325 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003326 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003328 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 (void)tv_get_number_chk(tv, &error);
3330 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003331 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 }
3333 break;
3334
3335 case ISN_CHECKTYPE:
3336 {
3337 checktype_T *ct = &iptr->isn_arg.type;
3338
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003339 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003340 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003341 if (!where.wt_variable)
3342 where.wt_index = ct->ct_arg_idx;
3343 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003344 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003345 if (!where.wt_variable)
3346 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003347
3348 // number 0 is FALSE, number 1 is TRUE
3349 if (tv->v_type == VAR_NUMBER
3350 && ct->ct_type->tt_type == VAR_BOOL
3351 && (tv->vval.v_number == 0
3352 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003354 tv->v_type = VAR_BOOL;
3355 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003356 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 }
3358 }
3359 break;
3360
Bram Moolenaar9af78762020-06-16 11:34:42 +02003361 case ISN_CHECKLEN:
3362 {
3363 int min_len = iptr->isn_arg.checklen.cl_min_len;
3364 list_T *list = NULL;
3365
3366 tv = STACK_TV_BOT(-1);
3367 if (tv->v_type == VAR_LIST)
3368 list = tv->vval.v_list;
3369 if (list == NULL || list->lv_len < min_len
3370 || (list->lv_len > min_len
3371 && !iptr->isn_arg.checklen.cl_more_OK))
3372 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003373 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003374 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003375 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003376 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003377 }
3378 }
3379 break;
3380
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003381 case ISN_SETTYPE:
3382 {
3383 checktype_T *ct = &iptr->isn_arg.type;
3384
3385 tv = STACK_TV_BOT(-1);
3386 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3387 {
3388 free_type(tv->vval.v_dict->dv_type);
3389 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3390 }
3391 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3392 {
3393 free_type(tv->vval.v_list->lv_type);
3394 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3395 }
3396 }
3397 break;
3398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003400 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 {
3402 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003403 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404
3405 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003406 if (iptr->isn_type == ISN_2BOOL)
3407 {
3408 n = tv2bool(tv);
3409 if (iptr->isn_arg.number) // invert
3410 n = !n;
3411 }
3412 else
3413 {
3414 SOURCING_LNUM = iptr->isn_lnum;
3415 n = tv_get_bool_chk(tv, &error);
3416 if (error)
3417 goto on_error;
3418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 clear_tv(tv);
3420 tv->v_type = VAR_BOOL;
3421 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3422 }
3423 break;
3424
3425 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003426 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003427 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003428 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3429 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3430 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 break;
3432
Bram Moolenaar08597872020-12-10 19:43:40 +01003433 case ISN_RANGE:
3434 {
3435 exarg_T ea;
3436 char *errormsg;
3437
Bram Moolenaarece0b872021-01-08 20:40:45 +01003438 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003439 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003440 ea.addr_type = ADDR_LINES;
3441 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003442 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003443 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003444 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003445
3446 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3447 goto failed;
3448 ++ectx.ec_stack.ga_len;
3449 tv = STACK_TV_BOT(-1);
3450 tv->v_type = VAR_NUMBER;
3451 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003452 if (ea.addr_count == 0)
3453 tv->vval.v_number = curwin->w_cursor.lnum;
3454 else
3455 tv->vval.v_number = ea.line2;
3456 }
3457 break;
3458
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003459 case ISN_PUT:
3460 {
3461 int regname = iptr->isn_arg.put.put_regname;
3462 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3463 char_u *expr = NULL;
3464 int dir = FORWARD;
3465
Bram Moolenaar08597872020-12-10 19:43:40 +01003466 if (lnum < -2)
3467 {
3468 // line number was put on the stack by ISN_RANGE
3469 tv = STACK_TV_BOT(-1);
3470 curwin->w_cursor.lnum = tv->vval.v_number;
3471 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3472 dir = BACKWARD;
3473 --ectx.ec_stack.ga_len;
3474 }
3475 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003476 // :put! above cursor
3477 dir = BACKWARD;
3478 else if (lnum >= 0)
3479 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003480
3481 if (regname == '=')
3482 {
3483 tv = STACK_TV_BOT(-1);
3484 if (tv->v_type == VAR_STRING)
3485 expr = tv->vval.v_string;
3486 else
3487 {
3488 expr = typval2string(tv, TRUE); // allocates value
3489 clear_tv(tv);
3490 }
3491 --ectx.ec_stack.ga_len;
3492 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003493 check_cursor();
3494 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3495 vim_free(expr);
3496 }
3497 break;
3498
Bram Moolenaar02194d22020-10-24 23:08:38 +02003499 case ISN_CMDMOD:
3500 save_cmdmod = cmdmod;
3501 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003502 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003503 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3504 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003505 break;
3506
Bram Moolenaar02194d22020-10-24 23:08:38 +02003507 case ISN_CMDMOD_REV:
3508 // filter regprog is owned by the instruction, don't free it
3509 cmdmod.cmod_filter_regmatch.regprog = NULL;
3510 undo_cmdmod(&cmdmod);
3511 cmdmod = save_cmdmod;
3512 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003513 break;
3514
Bram Moolenaar792f7862020-11-23 08:31:18 +01003515 case ISN_UNPACK:
3516 {
3517 int count = iptr->isn_arg.unpack.unp_count;
3518 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3519 list_T *l;
3520 listitem_T *li;
3521 int i;
3522
3523 // Check there is a valid list to unpack.
3524 tv = STACK_TV_BOT(-1);
3525 if (tv->v_type != VAR_LIST)
3526 {
3527 SOURCING_LNUM = iptr->isn_lnum;
3528 emsg(_(e_for_argument_must_be_sequence_of_lists));
3529 goto on_error;
3530 }
3531 l = tv->vval.v_list;
3532 if (l == NULL
3533 || l->lv_len < (semicolon ? count - 1 : count))
3534 {
3535 SOURCING_LNUM = iptr->isn_lnum;
3536 emsg(_(e_list_value_does_not_have_enough_items));
3537 goto on_error;
3538 }
3539 else if (!semicolon && l->lv_len > count)
3540 {
3541 SOURCING_LNUM = iptr->isn_lnum;
3542 emsg(_(e_list_value_has_more_items_than_targets));
3543 goto on_error;
3544 }
3545
3546 CHECK_LIST_MATERIALIZE(l);
3547 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3548 goto failed;
3549 ectx.ec_stack.ga_len += count - 1;
3550
3551 // Variable after semicolon gets a list with the remaining
3552 // items.
3553 if (semicolon)
3554 {
3555 list_T *rem_list =
3556 list_alloc_with_items(l->lv_len - count + 1);
3557
3558 if (rem_list == NULL)
3559 goto failed;
3560 tv = STACK_TV_BOT(-count);
3561 tv->vval.v_list = rem_list;
3562 ++rem_list->lv_refcount;
3563 tv->v_lock = 0;
3564 li = l->lv_first;
3565 for (i = 0; i < count - 1; ++i)
3566 li = li->li_next;
3567 for (i = 0; li != NULL; ++i)
3568 {
3569 list_set_item(rem_list, i, &li->li_tv);
3570 li = li->li_next;
3571 }
3572 --count;
3573 }
3574
3575 // Produce the values in reverse order, first item last.
3576 li = l->lv_first;
3577 for (i = 0; i < count; ++i)
3578 {
3579 tv = STACK_TV_BOT(-i - 1);
3580 copy_tv(&li->li_tv, tv);
3581 li = li->li_next;
3582 }
3583
3584 list_unref(l);
3585 }
3586 break;
3587
Bram Moolenaarb2049902021-01-24 12:53:53 +01003588 case ISN_PROF_START:
3589 case ISN_PROF_END:
3590 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003591#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003592 funccall_T cookie;
3593 ufunc_T *cur_ufunc =
3594 (((dfunc_T *)def_functions.ga_data)
3595 + ectx.ec_dfunc_idx)->df_ufunc;
3596
3597 cookie.func = cur_ufunc;
3598 if (iptr->isn_type == ISN_PROF_START)
3599 {
3600 func_line_start(&cookie, iptr->isn_lnum);
3601 // if we get here the instruction is executed
3602 func_line_exec(&cookie);
3603 }
3604 else
3605 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003606#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003607 }
3608 break;
3609
Bram Moolenaar389df252020-07-09 21:20:47 +02003610 case ISN_SHUFFLE:
3611 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003612 typval_T tmp_tv;
3613 int item = iptr->isn_arg.shuffle.shfl_item;
3614 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003615
3616 tmp_tv = *STACK_TV_BOT(-item);
3617 for ( ; up > 0 && item > 1; --up)
3618 {
3619 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3620 --item;
3621 }
3622 *STACK_TV_BOT(-item) = tmp_tv;
3623 }
3624 break;
3625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 case ISN_DROP:
3627 --ectx.ec_stack.ga_len;
3628 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003629 where.wt_index = 0;
3630 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 break;
3632 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003633 continue;
3634
Bram Moolenaard032f342020-07-18 18:13:02 +02003635func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003636 // Restore previous function. If the frame pointer is where we started
3637 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003638 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003639 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003640
Bram Moolenaard032f342020-07-18 18:13:02 +02003641 if (func_return(&ectx) == FAIL)
3642 // only fails when out of memory
3643 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003644 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003645
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003646on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003647 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003648 // If "emsg_silent" is set then ignore the error, unless it was set
3649 // when calling the function.
3650 if (did_emsg_cumul + did_emsg == did_emsg_before
3651 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003652 {
3653 // If a sequence of instructions causes an error while ":silent!"
3654 // was used, restore the stack length and jump ahead to restoring
3655 // the cmdmod.
3656 if (restore_cmdmod)
3657 {
3658 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3659 {
3660 --ectx.ec_stack.ga_len;
3661 clear_tv(STACK_TV_BOT(0));
3662 }
3663 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3664 ++ectx.ec_iidx;
3665 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003666 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003667 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003668on_fatal_error:
3669 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003670 // If we are not inside a try-catch started here, abort execution.
3671 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003672 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 }
3674
3675done:
3676 // function finished, get result from the stack.
3677 tv = STACK_TV_BOT(-1);
3678 *rettv = *tv;
3679 tv->v_type = VAR_UNKNOWN;
3680 ret = OK;
3681
3682failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003683 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003684 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003685 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003686
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003687 // Deal with any remaining closures, they may be in use somewhere.
3688 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003689 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003690 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003691 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3692 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003693
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003694 estack_pop();
3695 current_sctx = save_current_sctx;
3696
Bram Moolenaar352134b2020-10-17 22:04:08 +02003697 if (*msg_list != NULL && saved_msg_list != NULL)
3698 {
3699 msglist_T **plist = saved_msg_list;
3700
3701 // Append entries from the current msg_list (uncaught exceptions) to
3702 // the saved msg_list.
3703 while (*plist != NULL)
3704 plist = &(*plist)->next;
3705
3706 *plist = *msg_list;
3707 }
3708 msg_list = saved_msg_list;
3709
Bram Moolenaar02194d22020-10-24 23:08:38 +02003710 if (restore_cmdmod)
3711 {
3712 cmdmod.cmod_filter_regmatch.regprog = NULL;
3713 undo_cmdmod(&cmdmod);
3714 cmdmod = save_cmdmod;
3715 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003716 emsg_silent_def = save_emsg_silent_def;
3717 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003718
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003719failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003720 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3722 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003725 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003726
Bram Moolenaar0186e582021-01-10 18:33:11 +01003727 while (ectx.ec_outer != NULL)
3728 {
3729 outer_T *up = ectx.ec_outer->out_up_is_copy
3730 ? NULL : ectx.ec_outer->out_up;
3731
3732 vim_free(ectx.ec_outer);
3733 ectx.ec_outer = up;
3734 }
3735
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003736 // Not sure if this is necessary.
3737 suppress_errthrow = save_suppress_errthrow;
3738
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003739 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003740 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003741 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003742 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 return ret;
3744}
3745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003747 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003748 * We don't really need this at runtime, but we do have tests that require it,
3749 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 */
3751 void
3752ex_disassemble(exarg_T *eap)
3753{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003754 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003755 char_u *fname;
3756 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 dfunc_T *dfunc;
3758 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003759 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 int current;
3761 int line_idx = 0;
3762 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003763 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003765 if (STRNCMP(arg, "<lambda>", 8) == 0)
3766 {
3767 arg += 8;
3768 (void)getdigits(&arg);
3769 fname = vim_strnsave(eap->arg, arg - eap->arg);
3770 }
3771 else
3772 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003773 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003774 if (fname == NULL)
3775 {
3776 semsg(_(e_invarg2), eap->arg);
3777 return;
3778 }
3779
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003780 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003781 if (ufunc == NULL)
3782 {
3783 char_u *p = untrans_function_name(fname);
3784
3785 if (p != NULL)
3786 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003787 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003788 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003789 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 if (ufunc == NULL)
3791 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003792 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 return;
3794 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003795 if (func_needs_compiling(ufunc, eap->forceit)
3796 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003797 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003798 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003800 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 return;
3802 }
3803 if (ufunc->uf_name_exp != NULL)
3804 msg((char *)ufunc->uf_name_exp);
3805 else
3806 msg((char *)ufunc->uf_name);
3807
3808 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003809#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003810 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3811 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3812 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003813#else
3814 instr = dfunc->df_instr;
3815 instr_count = dfunc->df_instr_count;
3816#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003817 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 {
3819 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003820 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821
3822 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3823 {
3824 if (current > prev_current)
3825 {
3826 msg_puts("\n\n");
3827 prev_current = current;
3828 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003829 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3830 if (line != NULL)
3831 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 }
3833
3834 switch (iptr->isn_type)
3835 {
3836 case ISN_EXEC:
3837 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3838 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003839 case ISN_EXECCONCAT:
3840 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003841 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003842 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 case ISN_ECHO:
3844 {
3845 echo_T *echo = &iptr->isn_arg.echo;
3846
3847 smsg("%4d %s %d", current,
3848 echo->echo_with_white ? "ECHO" : "ECHON",
3849 echo->echo_count);
3850 }
3851 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003852 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003853 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003854 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003855 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003856 case ISN_ECHOMSG:
3857 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003858 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003859 break;
3860 case ISN_ECHOERR:
3861 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003862 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003863 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003865 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003866 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003867 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003868 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003869 + STACK_FRAME_SIZE));
3870 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003871 smsg("%4d LOAD $%lld", current,
3872 (varnumber_T)(iptr->isn_arg.number));
3873 }
3874 break;
3875 case ISN_LOADOUTER:
3876 {
3877 if (iptr->isn_arg.number < 0)
3878 smsg("%4d LOADOUTER level %d arg[%d]", current,
3879 iptr->isn_arg.outer.outer_depth,
3880 iptr->isn_arg.outer.outer_idx
3881 + STACK_FRAME_SIZE);
3882 else
3883 smsg("%4d LOADOUTER level %d $%d", current,
3884 iptr->isn_arg.outer.outer_depth,
3885 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 break;
3888 case ISN_LOADV:
3889 smsg("%4d LOADV v:%s", current,
3890 get_vim_var_name(iptr->isn_arg.number));
3891 break;
3892 case ISN_LOADSCRIPT:
3893 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003894 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3895 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003897 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003899 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3900 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003901 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003902 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 }
3904 break;
3905 case ISN_LOADS:
3906 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003907 scriptitem_T *si = SCRIPT_ITEM(
3908 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909
3910 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003911 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 }
3913 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003914 case ISN_LOADAUTO:
3915 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3916 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 case ISN_LOADG:
3918 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3919 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003920 case ISN_LOADB:
3921 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3922 break;
3923 case ISN_LOADW:
3924 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3925 break;
3926 case ISN_LOADT:
3927 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3928 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003929 case ISN_LOADGDICT:
3930 smsg("%4d LOAD g:", current);
3931 break;
3932 case ISN_LOADBDICT:
3933 smsg("%4d LOAD b:", current);
3934 break;
3935 case ISN_LOADWDICT:
3936 smsg("%4d LOAD w:", current);
3937 break;
3938 case ISN_LOADTDICT:
3939 smsg("%4d LOAD t:", current);
3940 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 case ISN_LOADOPT:
3942 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3943 break;
3944 case ISN_LOADENV:
3945 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3946 break;
3947 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003948 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 break;
3950
3951 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003952 if (iptr->isn_arg.number < 0)
3953 smsg("%4d STORE arg[%lld]", current,
3954 iptr->isn_arg.number + STACK_FRAME_SIZE);
3955 else
3956 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3957 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003958 case ISN_STOREOUTER:
3959 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003960 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003961 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3962 iptr->isn_arg.outer.outer_depth,
3963 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003964 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003965 smsg("%4d STOREOUTER level %d $%d", current,
3966 iptr->isn_arg.outer.outer_depth,
3967 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003970 case ISN_STOREV:
3971 smsg("%4d STOREV v:%s", current,
3972 get_vim_var_name(iptr->isn_arg.number));
3973 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003974 case ISN_STOREAUTO:
3975 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3976 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003978 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3979 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003980 case ISN_STOREB:
3981 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3982 break;
3983 case ISN_STOREW:
3984 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3985 break;
3986 case ISN_STORET:
3987 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3988 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003989 case ISN_STORES:
3990 {
3991 scriptitem_T *si = SCRIPT_ITEM(
3992 iptr->isn_arg.loadstore.ls_sid);
3993
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003994 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003995 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 break;
3998 case ISN_STORESCRIPT:
3999 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004000 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4001 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004003 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004005 smsg("%4d STORESCRIPT %s-%d in %s", current,
4006 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004007 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004008 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 }
4010 break;
4011 case ISN_STOREOPT:
4012 smsg("%4d STOREOPT &%s", current,
4013 iptr->isn_arg.storeopt.so_name);
4014 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004015 case ISN_STOREENV:
4016 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4017 break;
4018 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004019 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004020 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 case ISN_STORENR:
4022 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004023 iptr->isn_arg.storenr.stnr_val,
4024 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 break;
4026
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004027 case ISN_STOREINDEX:
4028 switch (iptr->isn_arg.vartype)
4029 {
4030 case VAR_LIST:
4031 smsg("%4d STORELIST", current);
4032 break;
4033 case VAR_DICT:
4034 smsg("%4d STOREDICT", current);
4035 break;
4036 case VAR_ANY:
4037 smsg("%4d STOREINDEX", current);
4038 break;
4039 default: break;
4040 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004041 break;
4042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 // constants
4044 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004045 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004046 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 break;
4048 case ISN_PUSHBOOL:
4049 case ISN_PUSHSPEC:
4050 smsg("%4d PUSH %s", current,
4051 get_var_special_name(iptr->isn_arg.number));
4052 break;
4053 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004054#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004056#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 break;
4058 case ISN_PUSHS:
4059 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4060 break;
4061 case ISN_PUSHBLOB:
4062 {
4063 char_u *r;
4064 char_u numbuf[NUMBUFLEN];
4065 char_u *tofree;
4066
4067 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004068 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 vim_free(tofree);
4070 }
4071 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004072 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004073 {
4074 char *name = (char *)iptr->isn_arg.string;
4075
4076 smsg("%4d PUSHFUNC \"%s\"", current,
4077 name == NULL ? "[none]" : name);
4078 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004079 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004080 case ISN_PUSHCHANNEL:
4081#ifdef FEAT_JOB_CHANNEL
4082 {
4083 channel_T *channel = iptr->isn_arg.channel;
4084
4085 smsg("%4d PUSHCHANNEL %d", current,
4086 channel == NULL ? 0 : channel->ch_id);
4087 }
4088#endif
4089 break;
4090 case ISN_PUSHJOB:
4091#ifdef FEAT_JOB_CHANNEL
4092 {
4093 typval_T tv;
4094 char_u *name;
4095
4096 tv.v_type = VAR_JOB;
4097 tv.vval.v_job = iptr->isn_arg.job;
4098 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004099 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004100 }
4101#endif
4102 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 case ISN_PUSHEXC:
4104 smsg("%4d PUSH v:exception", current);
4105 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004106 case ISN_UNLET:
4107 smsg("%4d UNLET%s %s", current,
4108 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4109 iptr->isn_arg.unlet.ul_name);
4110 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004111 case ISN_UNLETENV:
4112 smsg("%4d UNLETENV%s $%s", current,
4113 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4114 iptr->isn_arg.unlet.ul_name);
4115 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004116 case ISN_UNLETINDEX:
4117 smsg("%4d UNLETINDEX", current);
4118 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004119 case ISN_LOCKCONST:
4120 smsg("%4d LOCKCONST", current);
4121 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004123 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004124 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 break;
4126 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004127 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004128 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 break;
4130
4131 // function call
4132 case ISN_BCALL:
4133 {
4134 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4135
4136 smsg("%4d BCALL %s(argc %d)", current,
4137 internal_func_name(cbfunc->cbf_idx),
4138 cbfunc->cbf_argcount);
4139 }
4140 break;
4141 case ISN_DCALL:
4142 {
4143 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4144 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4145 + cdfunc->cdf_idx;
4146
4147 smsg("%4d DCALL %s(argc %d)", current,
4148 df->df_ufunc->uf_name_exp != NULL
4149 ? df->df_ufunc->uf_name_exp
4150 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4151 }
4152 break;
4153 case ISN_UCALL:
4154 {
4155 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4156
4157 smsg("%4d UCALL %s(argc %d)", current,
4158 cufunc->cuf_name, cufunc->cuf_argcount);
4159 }
4160 break;
4161 case ISN_PCALL:
4162 {
4163 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4164
4165 smsg("%4d PCALL%s (argc %d)", current,
4166 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4167 }
4168 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004169 case ISN_PCALL_END:
4170 smsg("%4d PCALL end", current);
4171 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 case ISN_RETURN:
4173 smsg("%4d RETURN", current);
4174 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004175 case ISN_RETURN_ZERO:
4176 smsg("%4d RETURN 0", current);
4177 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 case ISN_FUNCREF:
4179 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004180 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004182 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004184 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 }
4186 break;
4187
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004188 case ISN_NEWFUNC:
4189 {
4190 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4191
4192 smsg("%4d NEWFUNC %s %s", current,
4193 newfunc->nf_lambda, newfunc->nf_global);
4194 }
4195 break;
4196
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004197 case ISN_DEF:
4198 {
4199 char_u *name = iptr->isn_arg.string;
4200
4201 smsg("%4d DEF %s", current,
4202 name == NULL ? (char_u *)"" : name);
4203 }
4204 break;
4205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 case ISN_JUMP:
4207 {
4208 char *when = "?";
4209
4210 switch (iptr->isn_arg.jump.jump_when)
4211 {
4212 case JUMP_ALWAYS:
4213 when = "JUMP";
4214 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 case JUMP_AND_KEEP_IF_TRUE:
4216 when = "JUMP_AND_KEEP_IF_TRUE";
4217 break;
4218 case JUMP_IF_FALSE:
4219 when = "JUMP_IF_FALSE";
4220 break;
4221 case JUMP_AND_KEEP_IF_FALSE:
4222 when = "JUMP_AND_KEEP_IF_FALSE";
4223 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004224 case JUMP_IF_COND_FALSE:
4225 when = "JUMP_IF_COND_FALSE";
4226 break;
4227 case JUMP_IF_COND_TRUE:
4228 when = "JUMP_IF_COND_TRUE";
4229 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004231 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 iptr->isn_arg.jump.jump_where);
4233 }
4234 break;
4235
4236 case ISN_FOR:
4237 {
4238 forloop_T *forloop = &iptr->isn_arg.forloop;
4239
4240 smsg("%4d FOR $%d -> %d", current,
4241 forloop->for_idx, forloop->for_end);
4242 }
4243 break;
4244
4245 case ISN_TRY:
4246 {
4247 try_T *try = &iptr->isn_arg.try;
4248
Bram Moolenaarc150c092021-02-13 15:02:46 +01004249 smsg("%4d TRY catch -> %d, %s -> %d", current,
4250 try->try_catch,
4251 instr[try->try_finally].isn_type == ISN_ENDTRY
4252 ? "end" : "finally",
4253 try->try_finally);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 }
4255 break;
4256 case ISN_CATCH:
4257 // TODO
4258 smsg("%4d CATCH", current);
4259 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004260 case ISN_TRYCONT:
4261 {
4262 trycont_T *trycont = &iptr->isn_arg.trycont;
4263
4264 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4265 trycont->tct_levels,
4266 trycont->tct_levels == 1 ? "" : "s",
4267 trycont->tct_where);
4268 }
4269 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 case ISN_ENDTRY:
4271 smsg("%4d ENDTRY", current);
4272 break;
4273 case ISN_THROW:
4274 smsg("%4d THROW", current);
4275 break;
4276
4277 // expression operations on number
4278 case ISN_OPNR:
4279 case ISN_OPFLOAT:
4280 case ISN_OPANY:
4281 {
4282 char *what;
4283 char *ins;
4284
4285 switch (iptr->isn_arg.op.op_type)
4286 {
4287 case EXPR_MULT: what = "*"; break;
4288 case EXPR_DIV: what = "/"; break;
4289 case EXPR_REM: what = "%"; break;
4290 case EXPR_SUB: what = "-"; break;
4291 case EXPR_ADD: what = "+"; break;
4292 default: what = "???"; break;
4293 }
4294 switch (iptr->isn_type)
4295 {
4296 case ISN_OPNR: ins = "OPNR"; break;
4297 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4298 case ISN_OPANY: ins = "OPANY"; break;
4299 default: ins = "???"; break;
4300 }
4301 smsg("%4d %s %s", current, ins, what);
4302 }
4303 break;
4304
4305 case ISN_COMPAREBOOL:
4306 case ISN_COMPARESPECIAL:
4307 case ISN_COMPARENR:
4308 case ISN_COMPAREFLOAT:
4309 case ISN_COMPARESTRING:
4310 case ISN_COMPAREBLOB:
4311 case ISN_COMPARELIST:
4312 case ISN_COMPAREDICT:
4313 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 case ISN_COMPAREANY:
4315 {
4316 char *p;
4317 char buf[10];
4318 char *type;
4319
4320 switch (iptr->isn_arg.op.op_type)
4321 {
4322 case EXPR_EQUAL: p = "=="; break;
4323 case EXPR_NEQUAL: p = "!="; break;
4324 case EXPR_GREATER: p = ">"; break;
4325 case EXPR_GEQUAL: p = ">="; break;
4326 case EXPR_SMALLER: p = "<"; break;
4327 case EXPR_SEQUAL: p = "<="; break;
4328 case EXPR_MATCH: p = "=~"; break;
4329 case EXPR_IS: p = "is"; break;
4330 case EXPR_ISNOT: p = "isnot"; break;
4331 case EXPR_NOMATCH: p = "!~"; break;
4332 default: p = "???"; break;
4333 }
4334 STRCPY(buf, p);
4335 if (iptr->isn_arg.op.op_ic == TRUE)
4336 strcat(buf, "?");
4337 switch(iptr->isn_type)
4338 {
4339 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4340 case ISN_COMPARESPECIAL:
4341 type = "COMPARESPECIAL"; break;
4342 case ISN_COMPARENR: type = "COMPARENR"; break;
4343 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4344 case ISN_COMPARESTRING:
4345 type = "COMPARESTRING"; break;
4346 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4347 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4348 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4349 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4351 default: type = "???"; break;
4352 }
4353
4354 smsg("%4d %s %s", current, type, buf);
4355 }
4356 break;
4357
4358 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4359 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4360
4361 // expression operations
4362 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004363 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004364 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004365 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004366 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004367 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004368 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004369 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4370 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004371 case ISN_SLICE: smsg("%4d SLICE %lld",
4372 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004373 case ISN_GETITEM: smsg("%4d ITEM %lld",
4374 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004375 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4376 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 iptr->isn_arg.string); break;
4378 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4379
4380 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004381 case ISN_CHECKTYPE:
4382 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004383 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004384 char *tofree;
4385
Bram Moolenaare32e5162021-01-21 20:21:29 +01004386 if (ct->ct_arg_idx == 0)
4387 smsg("%4d CHECKTYPE %s stack[%d]", current,
4388 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004389 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004390 else
4391 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4392 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004393 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004394 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004395 vim_free(tofree);
4396 break;
4397 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004398 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4399 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4400 iptr->isn_arg.checklen.cl_min_len);
4401 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004402 case ISN_SETTYPE:
4403 {
4404 char *tofree;
4405
4406 smsg("%4d SETTYPE %s", current,
4407 type_name(iptr->isn_arg.type.ct_type, &tofree));
4408 vim_free(tofree);
4409 break;
4410 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004411 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 case ISN_2BOOL: if (iptr->isn_arg.number)
4413 smsg("%4d INVERT (!val)", current);
4414 else
4415 smsg("%4d 2BOOL (!!val)", current);
4416 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004417 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004418 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004419 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004420 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004421 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004422 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004423 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4424 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004425 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004426 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4427 smsg("%4d PUT %c above range",
4428 current, iptr->isn_arg.put.put_regname);
4429 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4430 smsg("%4d PUT %c range",
4431 current, iptr->isn_arg.put.put_regname);
4432 else
4433 smsg("%4d PUT %c %ld", current,
4434 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004435 (long)iptr->isn_arg.put.put_lnum);
4436 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437
Bram Moolenaar02194d22020-10-24 23:08:38 +02004438 // TODO: summarize modifiers
4439 case ISN_CMDMOD:
4440 {
4441 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004442 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004443 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4444
4445 buf = alloc(len + 1);
4446 if (buf != NULL)
4447 {
4448 (void)produce_cmdmods(
4449 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4450 smsg("%4d CMDMOD %s", current, buf);
4451 vim_free(buf);
4452 }
4453 break;
4454 }
4455 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004456
Bram Moolenaarb2049902021-01-24 12:53:53 +01004457 case ISN_PROF_START:
4458 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4459 break;
4460
4461 case ISN_PROF_END:
4462 smsg("%4d PROFILE END", current);
4463 break;
4464
Bram Moolenaar792f7862020-11-23 08:31:18 +01004465 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4466 iptr->isn_arg.unpack.unp_count,
4467 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4468 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004469 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4470 iptr->isn_arg.shuffle.shfl_item,
4471 iptr->isn_arg.shuffle.shfl_up);
4472 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 case ISN_DROP: smsg("%4d DROP", current); break;
4474 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004475
4476 out_flush(); // output one line at a time
4477 ui_breakcheck();
4478 if (got_int)
4479 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481}
4482
4483/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004484 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 * list, etc. Mostly like what JavaScript does, except that empty list and
4486 * empty dictionary are FALSE.
4487 */
4488 int
4489tv2bool(typval_T *tv)
4490{
4491 switch (tv->v_type)
4492 {
4493 case VAR_NUMBER:
4494 return tv->vval.v_number != 0;
4495 case VAR_FLOAT:
4496#ifdef FEAT_FLOAT
4497 return tv->vval.v_float != 0.0;
4498#else
4499 break;
4500#endif
4501 case VAR_PARTIAL:
4502 return tv->vval.v_partial != NULL;
4503 case VAR_FUNC:
4504 case VAR_STRING:
4505 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4506 case VAR_LIST:
4507 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4508 case VAR_DICT:
4509 return tv->vval.v_dict != NULL
4510 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4511 case VAR_BOOL:
4512 case VAR_SPECIAL:
4513 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4514 case VAR_JOB:
4515#ifdef FEAT_JOB_CHANNEL
4516 return tv->vval.v_job != NULL;
4517#else
4518 break;
4519#endif
4520 case VAR_CHANNEL:
4521#ifdef FEAT_JOB_CHANNEL
4522 return tv->vval.v_channel != NULL;
4523#else
4524 break;
4525#endif
4526 case VAR_BLOB:
4527 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4528 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004529 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 case VAR_VOID:
4531 break;
4532 }
4533 return FALSE;
4534}
4535
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004536 void
4537emsg_using_string_as(typval_T *tv, int as_number)
4538{
4539 semsg(_(as_number ? e_using_string_as_number_str
4540 : e_using_string_as_bool_str),
4541 tv->vval.v_string == NULL
4542 ? (char_u *)"" : tv->vval.v_string);
4543}
4544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545/*
4546 * If "tv" is a string give an error and return FAIL.
4547 */
4548 int
4549check_not_string(typval_T *tv)
4550{
4551 if (tv->v_type == VAR_STRING)
4552 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004553 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 clear_tv(tv);
4555 return FAIL;
4556 }
4557 return OK;
4558}
4559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560
4561#endif // FEAT_EVAL