blob: 6734f4f7daa1227b49f25e66173b7fb1ae68f9c2 [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
Bram Moolenaar12d26532021-02-19 19:13:21 +010075#ifdef FEAT_PROFILE
76// stack of profinfo_T used when profiling.
77static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
78#endif
79
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020081#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 +010082
Bram Moolenaar418f1df2020-08-12 21:34:49 +020083 void
84to_string_error(vartype_T vartype)
85{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020086 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020087}
88
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010089/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010090 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010091 */
92 static int
93ufunc_argcount(ufunc_T *ufunc)
94{
95 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
96}
97
98/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010099 * Set the instruction index, depending on omitted arguments, where the default
100 * values are to be computed. If all optional arguments are present, start
101 * with the function body.
102 * The expression evaluation is at the start of the instructions:
103 * 0 -> EVAL default1
104 * STORE arg[-2]
105 * 1 -> EVAL default2
106 * STORE arg[-1]
107 * 2 -> function body
108 */
109 static void
110init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
111{
112 if (ufunc->uf_def_args.ga_len == 0)
113 ectx->ec_iidx = 0;
114 else
115 {
116 int defcount = ufunc->uf_args.ga_len - argcount;
117
118 // If there is a varargs argument defcount can be negative, no defaults
119 // to evaluate then.
120 if (defcount < 0)
121 defcount = 0;
122 ectx->ec_iidx = ufunc->uf_def_arg_idx[
123 ufunc->uf_def_args.ga_len - defcount];
124 }
125}
126
127/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200128 * Create a new list from "count" items at the bottom of the stack.
129 * When "count" is zero an empty list is added to the stack.
130 */
131 static int
132exe_newlist(int count, ectx_T *ectx)
133{
134 list_T *list = list_alloc_with_items(count);
135 int idx;
136 typval_T *tv;
137
138 if (list == NULL)
139 return FAIL;
140 for (idx = 0; idx < count; ++idx)
141 list_set_item(list, idx, STACK_TV_BOT(idx - count));
142
143 if (count > 0)
144 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200145 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200146 return FAIL;
147 else
148 ++ectx->ec_stack.ga_len;
149 tv = STACK_TV_BOT(-1);
150 tv->v_type = VAR_LIST;
151 tv->vval.v_list = list;
152 ++list->lv_refcount;
153 return OK;
154}
155
156/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100158 * This adds a stack frame and sets the instruction pointer to the start of the
159 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100160 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161 *
162 * Stack has:
163 * - current arguments (already there)
164 * - omitted optional argument (default values) added here
165 * - stack frame:
166 * - pointer to calling function
167 * - Index of next instruction in calling function
168 * - previous frame pointer
169 * - reserved space for local variables
170 */
171 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100172call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200174 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
176 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200177 int arg_to_add;
178 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200179 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100180 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200181 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182
183 if (dfunc->df_deleted)
184 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100185 // don't use ufunc->uf_name, it may have been freed
186 emsg_funcname(e_func_deleted,
187 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188 return FAIL;
189 }
190
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100191#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100192 if (do_profiling == PROF_YES)
193 {
194 if (ga_grow(&profile_info_ga, 1) == OK)
195 {
196 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
197 + profile_info_ga.ga_len;
198 ++profile_info_ga.ga_len;
199 CLEAR_POINTER(info);
200 profile_may_start_func(info, ufunc,
201 (((dfunc_T *)def_functions.ga_data)
202 + ectx->ec_dfunc_idx)->df_ufunc);
203 }
204
205 // Profiling might be enabled/disabled along the way. This should not
206 // fail, since the function was compiled before and toggling profiling
207 // doesn't change any errors.
208 if (func_needs_compiling(ufunc, PROFILING(ufunc))
209 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100210 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100211 return FAIL;
212 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100213#endif
214
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200215 if (ufunc->uf_va_name != NULL)
216 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200217 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200218 // Stack at time of call with 2 varargs:
219 // normal_arg
220 // optional_arg
221 // vararg_1
222 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200223 // After creating the list:
224 // normal_arg
225 // optional_arg
226 // vararg-list
227 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200228 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200229 // After creating the list
230 // normal_arg
231 // (space for optional_arg)
232 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200233 vararg_count = argcount - ufunc->uf_args.ga_len;
234 if (vararg_count < 0)
235 vararg_count = 0;
236 else
237 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200238 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200239 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200240
241 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200242 }
243
Bram Moolenaarfe270812020-04-11 22:31:27 +0200244 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200245 if (arg_to_add < 0)
246 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200247 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200248 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200249 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200250 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200251 return FAIL;
252 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200253
254 // Reserve space for:
255 // - missing arguments
256 // - stack frame
257 // - local variables
258 // - if needed: a counter for number of closures created in
259 // ectx->ec_funcrefs.
260 varcount = dfunc->df_varcount + dfunc->df_has_closure;
261 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
262 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100263 return FAIL;
264
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100265 // If depth of calling is getting too high, don't execute the function.
266 if (funcdepth_increment() == FAIL)
267 return FAIL;
268
Bram Moolenaarfe270812020-04-11 22:31:27 +0200269 // Move the vararg-list to below the missing optional arguments.
270 if (vararg_count > 0 && arg_to_add > 0)
271 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100272
273 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200274 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200275 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200276 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277
278 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100279 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
280 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100281 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
282 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200283 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284
285 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200286 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200288 if (dfunc->df_has_closure)
289 {
290 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
291
292 tv->v_type = VAR_NUMBER;
293 tv->vval.v_number = 0;
294 }
295 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100297 if (pt != NULL || ufunc->uf_partial != NULL
298 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100299 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100300 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
301
302 if (outer == NULL)
303 return FAIL;
304 if (pt != NULL)
305 {
306 *outer = pt->pt_outer;
307 outer->out_up_is_copy = TRUE;
308 }
309 else if (ufunc->uf_partial != NULL)
310 {
311 *outer = ufunc->uf_partial->pt_outer;
312 outer->out_up_is_copy = TRUE;
313 }
314 else
315 {
316 outer->out_stack = &ectx->ec_stack;
317 outer->out_frame_idx = ectx->ec_frame_idx;
318 outer->out_up = ectx->ec_outer;
319 }
320 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100321 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100322 else
323 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325 // Set execution state to the start of the called function.
326 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100327 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100328 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200329 if (entry != NULL)
330 {
331 // Set the script context to the script where the function was defined.
332 // TODO: save more than the SID?
333 entry->es_save_sid = current_sctx.sc_sid;
334 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
335 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100336
337 // Decide where to start execution, handles optional arguments.
338 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100339
340 return OK;
341}
342
343// Get pointer to item in the stack.
344#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
345
346/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200347 * Used when returning from a function: Check if any closure is still
348 * referenced. If so then move the arguments and variables to a separate piece
349 * of stack to be used when the closure is called.
350 * When "free_arguments" is TRUE the arguments are to be freed.
351 * Returns FAIL when out of memory.
352 */
353 static int
354handle_closure_in_use(ectx_T *ectx, int free_arguments)
355{
356 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
357 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200358 int argcount;
359 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200360 int idx;
361 typval_T *tv;
362 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200363 garray_T *gap = &ectx->ec_funcrefs;
364 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200365
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200366 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200367 return OK; // function was freed
368 if (dfunc->df_has_closure == 0)
369 return OK; // no closures
370 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
371 closure_count = tv->vval.v_number;
372 if (closure_count == 0)
373 return OK; // no funcrefs created
374
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200375 argcount = ufunc_argcount(dfunc->df_ufunc);
376 top = ectx->ec_frame_idx - argcount;
377
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200378 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200379 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200380 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200381 partial_T *pt;
382 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200383
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200384 if (off < 0)
385 continue; // count is off or already done
386 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200387 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200388 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200389 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200390 int i;
391
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200392 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200393 // unreferenced on return.
394 for (i = 0; i < dfunc->df_varcount; ++i)
395 {
396 typval_T *stv = STACK_TV(ectx->ec_frame_idx
397 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200398 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200399 --refcount;
400 }
401 if (refcount > 1)
402 {
403 closure_in_use = TRUE;
404 break;
405 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200406 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200407 }
408
409 if (closure_in_use)
410 {
411 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
412 typval_T *stack;
413
414 // A closure is using the arguments and/or local variables.
415 // Move them to the called function.
416 if (funcstack == NULL)
417 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200418 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
419 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200420 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
421 funcstack->fs_ga.ga_data = stack;
422 if (stack == NULL)
423 {
424 vim_free(funcstack);
425 return FAIL;
426 }
427
428 // Move or copy the arguments.
429 for (idx = 0; idx < argcount; ++idx)
430 {
431 tv = STACK_TV(top + idx);
432 if (free_arguments)
433 {
434 *(stack + idx) = *tv;
435 tv->v_type = VAR_UNKNOWN;
436 }
437 else
438 copy_tv(tv, stack + idx);
439 }
440 // Move the local variables.
441 for (idx = 0; idx < dfunc->df_varcount; ++idx)
442 {
443 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200444
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200445 // A partial created for a local function, that is also used as a
446 // local variable, has a reference count for the variable, thus
447 // will never go down to zero. When all these refcounts are one
448 // then the funcstack is unused. We need to count how many we have
449 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200450 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
451 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200452 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200453
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200454 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200455 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
456 gap->ga_len - closure_count + i])
457 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200458 }
459
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200460 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200461 tv->v_type = VAR_UNKNOWN;
462 }
463
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200464 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200465 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200466 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
467 - closure_count + idx];
468 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200469 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200470 ++funcstack->fs_refcount;
471 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100472 pt->pt_outer.out_stack = &funcstack->fs_ga;
473 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
474 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200475 }
476 }
477 }
478
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200479 for (idx = 0; idx < closure_count; ++idx)
480 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
481 - closure_count + idx]);
482 gap->ga_len -= closure_count;
483 if (gap->ga_len == 0)
484 ga_clear(gap);
485
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200486 return OK;
487}
488
489/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200490 * Called when a partial is freed or its reference count goes down to one. The
491 * funcstack may be the only reference to the partials in the local variables.
492 * Go over all of them, the funcref and can be freed if all partials
493 * referencing the funcstack have a reference count of one.
494 */
495 void
496funcstack_check_refcount(funcstack_T *funcstack)
497{
498 int i;
499 garray_T *gap = &funcstack->fs_ga;
500 int done = 0;
501
502 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
503 return;
504 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
505 {
506 typval_T *tv = ((typval_T *)gap->ga_data) + i;
507
508 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
509 && tv->vval.v_partial->pt_funcstack == funcstack
510 && tv->vval.v_partial->pt_refcount == 1)
511 ++done;
512 }
513 if (done == funcstack->fs_min_refcount)
514 {
515 typval_T *stack = gap->ga_data;
516
517 // All partials referencing the funcstack have a reference count of
518 // one, thus the funcstack is no longer of use.
519 for (i = 0; i < gap->ga_len; ++i)
520 clear_tv(stack + i);
521 vim_free(stack);
522 vim_free(funcstack);
523 }
524}
525
526/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 * Return from the current function.
528 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200529 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530func_return(ectx_T *ectx)
531{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100533 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200534 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
535 + ectx->ec_dfunc_idx;
536 int argcount = ufunc_argcount(dfunc->df_ufunc);
537 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200538 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100539 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
540 + STACK_FRAME_FUNC_OFF)->vval.v_number;
541 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
542 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543
Bram Moolenaar12d26532021-02-19 19:13:21 +0100544#ifdef FEAT_PROFILE
545 if (do_profiling == PROF_YES)
546 {
547 ufunc_T *caller = prev_dfunc->df_ufunc;
548
549 if (dfunc->df_ufunc->uf_profiling
550 || (caller != NULL && caller->uf_profiling))
551 {
552 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
553 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
554 --profile_info_ga.ga_len;
555 }
556 }
557#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200559 entry = estack_pop();
560 if (entry != NULL)
561 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200563 if (handle_closure_in_use(ectx, TRUE) == FAIL)
564 return FAIL;
565
566 // Clear the arguments.
567 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
568 clear_tv(STACK_TV(idx));
569
570 // Clear local variables and temp values, but not the return value.
571 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100572 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100574
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100575 // The return value should be on top of the stack. However, when aborting
576 // it may not be there and ec_frame_idx is the top of the stack.
577 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100578 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100579 ret_idx = 0;
580
Bram Moolenaar0186e582021-01-10 18:33:11 +0100581 vim_free(ectx->ec_outer);
582
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100583 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100584 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100585 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
586 + STACK_FRAME_IIDX_OFF)->vval.v_number;
587 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
588 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200589 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100590 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
591 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100592 ectx->ec_instr = INSTRUCTIONS(prev_dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100593
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100594 if (ret_idx > 0)
595 {
596 // Reset the stack to the position before the call, with a spot for the
597 // return value, moved there from above the frame.
598 ectx->ec_stack.ga_len = top + 1;
599 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
600 }
601 else
602 // Reset the stack to the position before the call.
603 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200604
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100605 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200606 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607}
608
609#undef STACK_TV
610
611/*
612 * Prepare arguments and rettv for calling a builtin or user function.
613 */
614 static int
615call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
616{
617 int idx;
618 typval_T *tv;
619
620 // Move arguments from bottom of the stack to argvars[] and add terminator.
621 for (idx = 0; idx < argcount; ++idx)
622 argvars[idx] = *STACK_TV_BOT(idx - argcount);
623 argvars[argcount].v_type = VAR_UNKNOWN;
624
625 // Result replaces the arguments on the stack.
626 if (argcount > 0)
627 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200628 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 return FAIL;
630 else
631 ++ectx->ec_stack.ga_len;
632
633 // Default return value is zero.
634 tv = STACK_TV_BOT(-1);
635 tv->v_type = VAR_NUMBER;
636 tv->vval.v_number = 0;
637
638 return OK;
639}
640
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200641// Ugly global to avoid passing the execution context around through many
642// layers.
643static ectx_T *current_ectx = NULL;
644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645/*
646 * Call a builtin function by index.
647 */
648 static int
649call_bfunc(int func_idx, int argcount, ectx_T *ectx)
650{
651 typval_T argvars[MAX_FUNC_ARGS];
652 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100653 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200654 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655
656 if (call_prepare(argcount, argvars, ectx) == FAIL)
657 return FAIL;
658
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200659 // Call the builtin function. Set "current_ectx" so that when it
660 // recursively invokes call_def_function() a closure context can be set.
661 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100662 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200663 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664
665 // Clear the arguments.
666 for (idx = 0; idx < argcount; ++idx)
667 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200668
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100669 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200670 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 return OK;
672}
673
674/*
675 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100676 * If the function is compiled this will add a stack frame and set the
677 * instruction pointer at the start of the function.
678 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100679 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100680 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 */
682 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100683call_ufunc(
684 ufunc_T *ufunc,
685 partial_T *pt,
686 int argcount,
687 ectx_T *ectx,
688 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689{
690 typval_T argvars[MAX_FUNC_ARGS];
691 funcexe_T funcexe;
692 int error;
693 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100694 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100695#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100696 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100697#else
698# define profiling FALSE
699#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700
Bram Moolenaarb2049902021-01-24 12:53:53 +0100701 if (func_needs_compiling(ufunc, profiling)
702 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200703 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200704 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100705 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100706 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100707 if (error != FCERR_UNKNOWN)
708 {
709 if (error == FCERR_TOOMANY)
710 semsg(_(e_toomanyarg), ufunc->uf_name);
711 else
712 semsg(_(e_toofewarg), ufunc->uf_name);
713 return FAIL;
714 }
715
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100716 // The function has been compiled, can call it quickly. For a function
717 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100718 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100719 if (iptr != NULL)
720 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100721 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100722 iptr->isn_type = ISN_DCALL;
723 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
724 iptr->isn_arg.dfunc.cdf_argcount = argcount;
725 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100726 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728
729 if (call_prepare(argcount, argvars, ectx) == FAIL)
730 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200731 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732 funcexe.evaluate = TRUE;
733
734 // Call the user function. Result goes in last position on the stack.
735 // TODO: add selfdict if there is one
736 error = call_user_func_check(ufunc, argcount, argvars,
737 STACK_TV_BOT(-1), &funcexe, NULL);
738
739 // Clear the arguments.
740 for (idx = 0; idx < argcount; ++idx)
741 clear_tv(&argvars[idx]);
742
743 if (error != FCERR_NONE)
744 {
745 user_func_error(error, ufunc->uf_name);
746 return FAIL;
747 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100748 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200749 // Error other than from calling the function itself.
750 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751 return OK;
752}
753
754/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200755 * Return TRUE if an error was given or CTRL-C was pressed.
756 */
757 static int
758vim9_aborting(int prev_called_emsg)
759{
760 return called_emsg > prev_called_emsg || got_int || did_throw;
761}
762
763/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 * Execute a function by "name".
765 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100766 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 * Returns FAIL if not found without an error message.
768 */
769 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100770call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771{
772 ufunc_T *ufunc;
773
774 if (builtin_function(name, -1))
775 {
776 int func_idx = find_internal_func(name);
777
778 if (func_idx < 0)
779 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200780 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 return FAIL;
782 return call_bfunc(func_idx, argcount, ectx);
783 }
784
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200785 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200786
787 if (ufunc == NULL)
788 {
789 int called_emsg_before = called_emsg;
790
791 if (script_autoload(name, TRUE))
792 // loaded a package, search for the function again
793 ufunc = find_func(name, FALSE, NULL);
794 if (vim9_aborting(called_emsg_before))
795 return FAIL; // bail out if loading the script caused an error
796 }
797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100799 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800
801 return FAIL;
802}
803
804 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200805call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200807 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200808 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100810 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811
812 if (tv->v_type == VAR_PARTIAL)
813 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200814 partial_T *pt = tv->vval.v_partial;
815 int i;
816
817 if (pt->pt_argc > 0)
818 {
819 // Make space for arguments from the partial, shift the "argcount"
820 // arguments up.
821 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
822 return FAIL;
823 for (i = 1; i <= argcount; ++i)
824 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
825 ectx->ec_stack.ga_len += pt->pt_argc;
826 argcount += pt->pt_argc;
827
828 // copy the arguments from the partial onto the stack
829 for (i = 0; i < pt->pt_argc; ++i)
830 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
831 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832
833 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100834 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 name = pt->pt_name;
837 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200838 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200840 if (name != NULL)
841 {
842 char_u fname_buf[FLEN_FIXED + 1];
843 char_u *tofree = NULL;
844 int error = FCERR_NONE;
845 char_u *fname;
846
847 // May need to translate <SNR>123_ to K_SNR.
848 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
849 if (error != FCERR_NONE)
850 res = FAIL;
851 else
852 res = call_by_name(fname, argcount, ectx, NULL);
853 vim_free(tofree);
854 }
855
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100856 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 {
858 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200859 semsg(_(e_unknownfunc),
860 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 return FAIL;
862 }
863 return OK;
864}
865
866/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200867 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
868 * TRUE.
869 */
870 static int
871error_if_locked(int lock, char *error)
872{
873 if (lock & (VAR_LOCKED | VAR_FIXED))
874 {
875 emsg(_(error));
876 return TRUE;
877 }
878 return FALSE;
879}
880
881/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100882 * Give an error if "tv" is not a number and return FAIL.
883 */
884 static int
885check_for_number(typval_T *tv)
886{
887 if (tv->v_type != VAR_NUMBER)
888 {
889 semsg(_(e_expected_str_but_got_str),
890 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
891 return FAIL;
892 }
893 return OK;
894}
895
896/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100897 * Store "tv" in variable "name".
898 * This is for s: and g: variables.
899 */
900 static void
901store_var(char_u *name, typval_T *tv)
902{
903 funccal_entry_T entry;
904
905 save_funccal(&entry);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100906 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100907 restore_funccal();
908}
909
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100910/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100911 * Convert "tv" to a string.
912 * Return FAIL if not allowed.
913 */
914 static int
915do_2string(typval_T *tv, int is_2string_any)
916{
917 if (tv->v_type != VAR_STRING)
918 {
919 char_u *str;
920
921 if (is_2string_any)
922 {
923 switch (tv->v_type)
924 {
925 case VAR_SPECIAL:
926 case VAR_BOOL:
927 case VAR_NUMBER:
928 case VAR_FLOAT:
929 case VAR_BLOB: break;
930 default: to_string_error(tv->v_type);
931 return FAIL;
932 }
933 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100934 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100935 clear_tv(tv);
936 tv->v_type = VAR_STRING;
937 tv->vval.v_string = str;
938 }
939 return OK;
940}
941
942/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100943 * When the value of "sv" is a null list of dict, allocate it.
944 */
945 static void
946allocate_if_null(typval_T *tv)
947{
948 switch (tv->v_type)
949 {
950 case VAR_LIST:
951 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100952 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100953 break;
954 case VAR_DICT:
955 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100956 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100957 break;
958 default:
959 break;
960 }
961}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200962
Bram Moolenaare7525c52021-01-09 13:20:37 +0100963/*
964 * Return the character "str[index]" where "index" is the character index. If
965 * "index" is out of range NULL is returned.
966 */
967 char_u *
968char_from_string(char_u *str, varnumber_T index)
969{
970 size_t nbyte = 0;
971 varnumber_T nchar = index;
972 size_t slen;
973
974 if (str == NULL)
975 return NULL;
976 slen = STRLEN(str);
977
978 // do the same as for a list: a negative index counts from the end
979 if (index < 0)
980 {
981 int clen = 0;
982
983 for (nbyte = 0; nbyte < slen; ++clen)
984 nbyte += MB_CPTR2LEN(str + nbyte);
985 nchar = clen + index;
986 if (nchar < 0)
987 // unlike list: index out of range results in empty string
988 return NULL;
989 }
990
991 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
992 nbyte += MB_CPTR2LEN(str + nbyte);
993 if (nbyte >= slen)
994 return NULL;
995 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
996}
997
998/*
999 * Get the byte index for character index "idx" in string "str" with length
1000 * "str_len".
1001 * If going over the end return "str_len".
1002 * If "idx" is negative count from the end, -1 is the last character.
1003 * When going over the start return -1.
1004 */
1005 static long
1006char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1007{
1008 varnumber_T nchar = idx;
1009 size_t nbyte = 0;
1010
1011 if (nchar >= 0)
1012 {
1013 while (nchar > 0 && nbyte < str_len)
1014 {
1015 nbyte += MB_CPTR2LEN(str + nbyte);
1016 --nchar;
1017 }
1018 }
1019 else
1020 {
1021 nbyte = str_len;
1022 while (nchar < 0 && nbyte > 0)
1023 {
1024 --nbyte;
1025 nbyte -= mb_head_off(str, str + nbyte);
1026 ++nchar;
1027 }
1028 if (nchar < 0)
1029 return -1;
1030 }
1031 return (long)nbyte;
1032}
1033
1034/*
1035 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001036 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001037 * Return NULL when the result is empty.
1038 */
1039 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001040string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001041{
1042 long start_byte, end_byte;
1043 size_t slen;
1044
1045 if (str == NULL)
1046 return NULL;
1047 slen = STRLEN(str);
1048 start_byte = char_idx2byte(str, slen, first);
1049 if (start_byte < 0)
1050 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001051 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001052 end_byte = (long)slen;
1053 else
1054 {
1055 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001056 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001057 // end index is inclusive
1058 end_byte += MB_CPTR2LEN(str + end_byte);
1059 }
1060
1061 if (start_byte >= (long)slen || end_byte <= start_byte)
1062 return NULL;
1063 return vim_strnsave(str + start_byte, end_byte - start_byte);
1064}
1065
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001066 static svar_T *
1067get_script_svar(scriptref_T *sref, ectx_T *ectx)
1068{
1069 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1070 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1071 + ectx->ec_dfunc_idx;
1072 svar_T *sv;
1073
1074 if (sref->sref_seq != si->sn_script_seq)
1075 {
1076 // The script was reloaded after the function was
1077 // compiled, the script_idx may not be valid.
1078 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1079 dfunc->df_ufunc->uf_name_exp);
1080 return NULL;
1081 }
1082 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1083 if (!equal_type(sv->sv_type, sref->sref_type))
1084 {
1085 emsg(_(e_script_variable_type_changed));
1086 return NULL;
1087 }
1088 return sv;
1089}
1090
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001091/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 * Execute a function by "name".
1093 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001094 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 */
1096 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001097call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098{
Bram Moolenaared677f52020-08-12 16:38:10 +02001099 int called_emsg_before = called_emsg;
1100 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101
Bram Moolenaared677f52020-08-12 16:38:10 +02001102 res = call_by_name(name, argcount, ectx, iptr);
1103 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001105 dictitem_T *v;
1106
1107 v = find_var(name, NULL, FALSE);
1108 if (v == NULL)
1109 {
1110 semsg(_(e_unknownfunc), name);
1111 return FAIL;
1112 }
1113 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1114 {
1115 semsg(_(e_unknownfunc), name);
1116 return FAIL;
1117 }
1118 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001120 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121}
1122
1123/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001124 * When a function reference is used, fill a partial with the information
1125 * needed, especially when it is used as a closure.
1126 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001127 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001128fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1129{
1130 pt->pt_func = ufunc;
1131 pt->pt_refcount = 1;
1132
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001133 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001134 {
1135 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1136 + ectx->ec_dfunc_idx;
1137
1138 // The closure needs to find arguments and local
1139 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001140 pt->pt_outer.out_stack = &ectx->ec_stack;
1141 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1142 pt->pt_outer.out_up = ectx->ec_outer;
1143 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001144
1145 // If this function returns and the closure is still
1146 // being used, we need to make a copy of the context
1147 // (arguments and local variables). Store a reference
1148 // to the partial so we can handle that.
1149 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1150 {
1151 vim_free(pt);
1152 return FAIL;
1153 }
1154 // Extra variable keeps the count of closures created
1155 // in the current function call.
1156 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1157 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1158
1159 ((partial_T **)ectx->ec_funcrefs.ga_data)
1160 [ectx->ec_funcrefs.ga_len] = pt;
1161 ++pt->pt_refcount;
1162 ++ectx->ec_funcrefs.ga_len;
1163 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001164 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001165 return OK;
1166}
1167
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001168
Bram Moolenaarf112f302020-12-20 17:47:52 +01001169/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 * Call a "def" function from old Vim script.
1171 * Return OK or FAIL.
1172 */
1173 int
1174call_def_function(
1175 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001176 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001178 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 typval_T *rettv) // return value
1180{
1181 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001182 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001183 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184 typval_T *tv;
1185 int idx;
1186 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001187 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001188 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001189 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001190 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001191 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001192 msglist_T **saved_msg_list = NULL;
1193 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001194 cmdmod_T save_cmdmod;
1195 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001196 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001197 int save_emsg_silent_def = emsg_silent_def;
1198 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001199 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001200 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001201 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202
1203// Get pointer to item in the stack.
1204#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1205
1206// Get pointer to item at the bottom of the stack, -1 is the bottom.
1207#undef STACK_TV_BOT
1208#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1209
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001210// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001211#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 +01001212
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001213 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001214 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1215 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001216 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001217 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001218 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001219 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001220 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001221 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001222 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001223
Bram Moolenaar09689a02020-05-09 22:50:08 +02001224 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001225 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001226 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1227 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001228 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001229 {
1230 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001231 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001232 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001235 // If depth of calling is getting too high, don't execute the function.
1236 orig_funcdepth = funcdepth_get();
1237 if (funcdepth_increment() == FAIL)
1238 return FAIL;
1239
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001240 CLEAR_FIELD(ectx);
1241 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1242 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1243 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001244 {
1245 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001246 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001249 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001251 // Put arguments on the stack, but no more than what the function expects.
1252 // A lambda can be called with more arguments than it uses.
1253 for (idx = 0; idx < argc
1254 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1255 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001256 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001257 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001258 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001259 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001260 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 copy_tv(&argv[idx], STACK_TV_BOT(0));
1262 ++ectx.ec_stack.ga_len;
1263 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001264
1265 // Turn varargs into a list. Empty list if no args.
1266 if (ufunc->uf_va_name != NULL)
1267 {
1268 int vararg_count = argc - ufunc->uf_args.ga_len;
1269
1270 if (vararg_count < 0)
1271 vararg_count = 0;
1272 else
1273 argc -= vararg_count;
1274 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001275 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001276
1277 // Check the type of the list items.
1278 tv = STACK_TV_BOT(-1);
1279 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001280 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001281 && ufunc->uf_va_type->tt_member != &t_any
1282 && tv->vval.v_list != NULL)
1283 {
1284 type_T *expected = ufunc->uf_va_type->tt_member;
1285 listitem_T *li = tv->vval.v_list->lv_first;
1286
1287 for (idx = 0; idx < vararg_count; ++idx)
1288 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001289 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001290 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001291 goto failed_early;
1292 li = li->li_next;
1293 }
1294 }
1295
Bram Moolenaar23e03252020-04-12 22:22:31 +02001296 if (defcount > 0)
1297 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001298 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001299 --ectx.ec_stack.ga_len;
1300 }
1301
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001302 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001303 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001304 if (defcount > 0)
1305 for (idx = 0; idx < defcount; ++idx)
1306 {
1307 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1308 ++ectx.ec_stack.ga_len;
1309 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001310 if (ufunc->uf_va_name != NULL)
1311 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312
1313 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001314 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1315 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001317 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001318 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1319 + ufunc->uf_dfunc_idx;
1320 ufunc_T *base_ufunc = dfunc->df_ufunc;
1321
1322 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1323 // by copy_func().
1324 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001325 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001326 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1327 if (ectx.ec_outer == NULL)
1328 goto failed_early;
1329 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001330 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001331 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1332 {
1333 if (current_ectx->ec_outer != NULL)
1334 *ectx.ec_outer = *current_ectx->ec_outer;
1335 }
1336 else
1337 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001338 }
1339 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001340 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1341 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001342 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001343 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 // dummy frame entries
1346 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1347 {
1348 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1349 ++ectx.ec_stack.ga_len;
1350 }
1351
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001352 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001353 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001354 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1355 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001357 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001358 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001359 ectx.ec_stack.ga_len += dfunc->df_varcount;
1360 if (dfunc->df_has_closure)
1361 {
1362 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1363 STACK_TV_VAR(idx)->vval.v_number = 0;
1364 ++ectx.ec_stack.ga_len;
1365 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001366
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001367 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001368 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001369
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001370 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001371 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001372 estack_push_ufunc(ufunc, 1);
1373 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001374 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1375
Bram Moolenaar352134b2020-10-17 22:04:08 +02001376 // Use a specific location for storing error messages to be converted to an
1377 // exception.
1378 saved_msg_list = msg_list;
1379 msg_list = &private_msg_list;
1380
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001381 // Do turn errors into exceptions.
1382 suppress_errthrow = FALSE;
1383
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001384 // When ":silent!" was used before calling then we still abort the
1385 // function. If ":silent!" is used in the function then we don't.
1386 emsg_silent_def = emsg_silent;
1387 did_emsg_def = 0;
1388
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001389 where.wt_index = 0;
1390 where.wt_variable = FALSE;
1391
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001392 // Decide where to start execution, handles optional arguments.
1393 init_instr_idx(ufunc, argc, &ectx);
1394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 for (;;)
1396 {
1397 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001398
Bram Moolenaar270d0382020-05-15 21:42:53 +02001399 if (++breakcheck_count >= 100)
1400 {
1401 line_breakcheck();
1402 breakcheck_count = 0;
1403 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001404 if (got_int)
1405 {
1406 // Turn CTRL-C into an exception.
1407 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001408 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001409 goto failed;
1410 did_throw = TRUE;
1411 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412
Bram Moolenaara26b9702020-04-18 19:53:28 +02001413 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1414 {
1415 // Turn an error message into an exception.
1416 did_emsg = FALSE;
1417 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1418 goto failed;
1419 did_throw = TRUE;
1420 *msg_list = NULL;
1421 }
1422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 if (did_throw && !ectx.ec_in_catch)
1424 {
1425 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001426 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427
1428 // An exception jumps to the first catch, finally, or returns from
1429 // the current function.
1430 if (trystack->ga_len > 0)
1431 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001432 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 {
1434 // jump to ":catch" or ":finally"
1435 ectx.ec_in_catch = TRUE;
1436 ectx.ec_iidx = trycmd->tcd_catch_idx;
1437 }
1438 else
1439 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001440 // Not inside try or need to return from current functions.
1441 // Push a dummy return value.
1442 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1443 goto failed;
1444 tv = STACK_TV_BOT(0);
1445 tv->v_type = VAR_NUMBER;
1446 tv->vval.v_number = 0;
1447 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001448 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001450 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001451 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001452 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1453 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 goto done;
1455 }
1456
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001457 if (func_return(&ectx) == FAIL)
1458 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 }
1460 continue;
1461 }
1462
1463 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1464 switch (iptr->isn_type)
1465 {
1466 // execute Ex command line
1467 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001468 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001469 source_cookie_T cookie;
1470
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001471 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001472 // Pass getsourceline to get an error for a missing ":end"
1473 // command.
1474 CLEAR_FIELD(cookie);
1475 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1476 if (do_cmdline(iptr->isn_arg.string,
1477 getsourceline, &cookie,
1478 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1479 == FAIL
1480 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001481 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001482 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 break;
1484
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001485 // execute Ex command from pieces on the stack
1486 case ISN_EXECCONCAT:
1487 {
1488 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001489 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001490 int pass;
1491 int i;
1492 char_u *cmd = NULL;
1493 char_u *str;
1494
1495 for (pass = 1; pass <= 2; ++pass)
1496 {
1497 for (i = 0; i < count; ++i)
1498 {
1499 tv = STACK_TV_BOT(i - count);
1500 str = tv->vval.v_string;
1501 if (str != NULL && *str != NUL)
1502 {
1503 if (pass == 2)
1504 STRCPY(cmd + len, str);
1505 len += STRLEN(str);
1506 }
1507 if (pass == 2)
1508 clear_tv(tv);
1509 }
1510 if (pass == 1)
1511 {
1512 cmd = alloc(len + 1);
1513 if (cmd == NULL)
1514 goto failed;
1515 len = 0;
1516 }
1517 }
1518
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001519 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001520 do_cmdline_cmd(cmd);
1521 vim_free(cmd);
1522 }
1523 break;
1524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 // execute :echo {string} ...
1526 case ISN_ECHO:
1527 {
1528 int count = iptr->isn_arg.echo.echo_count;
1529 int atstart = TRUE;
1530 int needclr = TRUE;
1531
1532 for (idx = 0; idx < count; ++idx)
1533 {
1534 tv = STACK_TV_BOT(idx - count);
1535 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1536 &atstart, &needclr);
1537 clear_tv(tv);
1538 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001539 if (needclr)
1540 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 ectx.ec_stack.ga_len -= count;
1542 }
1543 break;
1544
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001545 // :execute {string} ...
1546 // :echomsg {string} ...
1547 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001548 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001549 case ISN_ECHOMSG:
1550 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001551 {
1552 int count = iptr->isn_arg.number;
1553 garray_T ga;
1554 char_u buf[NUMBUFLEN];
1555 char_u *p;
1556 int len;
1557 int failed = FALSE;
1558
1559 ga_init2(&ga, 1, 80);
1560 for (idx = 0; idx < count; ++idx)
1561 {
1562 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001563 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001564 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001565 if (tv->v_type == VAR_CHANNEL
1566 || tv->v_type == VAR_JOB)
1567 {
1568 SOURCING_LNUM = iptr->isn_lnum;
1569 emsg(_(e_inval_string));
1570 break;
1571 }
1572 else
1573 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001574 }
1575 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001576 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001577
1578 len = (int)STRLEN(p);
1579 if (ga_grow(&ga, len + 2) == FAIL)
1580 failed = TRUE;
1581 else
1582 {
1583 if (ga.ga_len > 0)
1584 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1585 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1586 ga.ga_len += len;
1587 }
1588 clear_tv(tv);
1589 }
1590 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001591 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001592 {
1593 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001594 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001595 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001596
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001597 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001598 {
1599 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001600 {
1601 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001602 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001603 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001604 {
1605 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001606 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001607 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001608 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001609 else
1610 {
1611 msg_sb_eol();
1612 if (iptr->isn_type == ISN_ECHOMSG)
1613 {
1614 msg_attr(ga.ga_data, echo_attr);
1615 out_flush();
1616 }
1617 else
1618 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001619 SOURCING_LNUM = iptr->isn_lnum;
1620 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001621 }
1622 }
1623 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001624 ga_clear(&ga);
1625 }
1626 break;
1627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 // load local variable or argument
1629 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001630 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 goto failed;
1632 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1633 ++ectx.ec_stack.ga_len;
1634 break;
1635
1636 // load v: variable
1637 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001638 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 goto failed;
1640 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1641 ++ectx.ec_stack.ga_len;
1642 break;
1643
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001644 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 case ISN_LOADSCRIPT:
1646 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001647 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 svar_T *sv;
1649
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001650 sv = get_script_svar(sref, &ectx);
1651 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001652 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001653 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001654 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 goto failed;
1656 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1657 ++ectx.ec_stack.ga_len;
1658 }
1659 break;
1660
1661 // load s: variable in old script
1662 case ISN_LOADS:
1663 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001664 hashtab_T *ht = &SCRIPT_VARS(
1665 iptr->isn_arg.loadstore.ls_sid);
1666 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 if (di == NULL)
1670 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001671 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001672 semsg(_(e_undefined_variable_str), name);
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 Moolenaard3aac292020-04-19 14:32:17 +02001685 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001687 case ISN_LOADB:
1688 case ISN_LOADW:
1689 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001691 dictitem_T *di = NULL;
1692 hashtab_T *ht = NULL;
1693 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001694
Bram Moolenaard3aac292020-04-19 14:32:17 +02001695 switch (iptr->isn_type)
1696 {
1697 case ISN_LOADG:
1698 ht = get_globvar_ht();
1699 namespace = 'g';
1700 break;
1701 case ISN_LOADB:
1702 ht = &curbuf->b_vars->dv_hashtab;
1703 namespace = 'b';
1704 break;
1705 case ISN_LOADW:
1706 ht = &curwin->w_vars->dv_hashtab;
1707 namespace = 'w';
1708 break;
1709 case ISN_LOADT:
1710 ht = &curtab->tp_vars->dv_hashtab;
1711 namespace = 't';
1712 break;
1713 default: // Cannot reach here
1714 goto failed;
1715 }
1716 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 if (di == NULL)
1719 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001720 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001721 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001722 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001723 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 }
1725 else
1726 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001727 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 goto failed;
1729 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1730 ++ectx.ec_stack.ga_len;
1731 }
1732 }
1733 break;
1734
Bram Moolenaar03290b82020-12-19 16:30:44 +01001735 // load autoload variable
1736 case ISN_LOADAUTO:
1737 {
1738 char_u *name = iptr->isn_arg.string;
1739
1740 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1741 goto failed;
1742 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001743 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001744 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1745 goto on_error;
1746 ++ectx.ec_stack.ga_len;
1747 }
1748 break;
1749
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001750 // load g:/b:/w:/t: namespace
1751 case ISN_LOADGDICT:
1752 case ISN_LOADBDICT:
1753 case ISN_LOADWDICT:
1754 case ISN_LOADTDICT:
1755 {
1756 dict_T *d = NULL;
1757
1758 switch (iptr->isn_type)
1759 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001760 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1761 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1762 case ISN_LOADWDICT: d = curwin->w_vars; break;
1763 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001764 default: // Cannot reach here
1765 goto failed;
1766 }
1767 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1768 goto failed;
1769 tv = STACK_TV_BOT(0);
1770 tv->v_type = VAR_DICT;
1771 tv->v_lock = 0;
1772 tv->vval.v_dict = d;
1773 ++ectx.ec_stack.ga_len;
1774 }
1775 break;
1776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 // load &option
1778 case ISN_LOADOPT:
1779 {
1780 typval_T optval;
1781 char_u *name = iptr->isn_arg.string;
1782
Bram Moolenaara8c17702020-04-01 21:17:24 +02001783 // This is not expected to fail, name is checked during
1784 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001785 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001787 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001788 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 *STACK_TV_BOT(0) = optval;
1790 ++ectx.ec_stack.ga_len;
1791 }
1792 break;
1793
1794 // load $ENV
1795 case ISN_LOADENV:
1796 {
1797 typval_T optval;
1798 char_u *name = iptr->isn_arg.string;
1799
Bram Moolenaar270d0382020-05-15 21:42:53 +02001800 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001802 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001803 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 *STACK_TV_BOT(0) = optval;
1805 ++ectx.ec_stack.ga_len;
1806 }
1807 break;
1808
1809 // load @register
1810 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001811 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 goto failed;
1813 tv = STACK_TV_BOT(0);
1814 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001815 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001816 // This may result in NULL, which should be equivalent to an
1817 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 tv->vval.v_string = get_reg_contents(
1819 iptr->isn_arg.number, GREG_EXPR_SRC);
1820 ++ectx.ec_stack.ga_len;
1821 break;
1822
1823 // store local variable
1824 case ISN_STORE:
1825 --ectx.ec_stack.ga_len;
1826 tv = STACK_TV_VAR(iptr->isn_arg.number);
1827 clear_tv(tv);
1828 *tv = *STACK_TV_BOT(0);
1829 break;
1830
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001831 // store s: variable in old script
1832 case ISN_STORES:
1833 {
1834 hashtab_T *ht = &SCRIPT_VARS(
1835 iptr->isn_arg.loadstore.ls_sid);
1836 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001837 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001838
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001839 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001840 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001841 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001842 else
1843 {
1844 clear_tv(&di->di_tv);
1845 di->di_tv = *STACK_TV_BOT(0);
1846 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001847 }
1848 break;
1849
1850 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 case ISN_STORESCRIPT:
1852 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001853 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1854 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001856 sv = get_script_svar(sref, &ectx);
1857 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001858 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 --ectx.ec_stack.ga_len;
1860 clear_tv(sv->sv_tv);
1861 *sv->sv_tv = *STACK_TV_BOT(0);
1862 }
1863 break;
1864
1865 // store option
1866 case ISN_STOREOPT:
1867 {
1868 long n = 0;
1869 char_u *s = NULL;
1870 char *msg;
1871
1872 --ectx.ec_stack.ga_len;
1873 tv = STACK_TV_BOT(0);
1874 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001875 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001877 if (s == NULL)
1878 s = (char_u *)"";
1879 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001881 // must be VAR_NUMBER, CHECKTYPE makes sure
1882 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1884 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001885 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 if (msg != NULL)
1887 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001888 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001890 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 }
1893 break;
1894
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001895 // store $ENV
1896 case ISN_STOREENV:
1897 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001898 tv = STACK_TV_BOT(0);
1899 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1900 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001901 break;
1902
1903 // store @r
1904 case ISN_STOREREG:
1905 {
1906 int reg = iptr->isn_arg.number;
1907
1908 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001909 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001910 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001911 tv_get_string(tv), -1, FALSE);
1912 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001913 }
1914 break;
1915
1916 // store v: variable
1917 case ISN_STOREV:
1918 --ectx.ec_stack.ga_len;
1919 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1920 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001921 // should not happen, type is checked when compiling
1922 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001923 break;
1924
Bram Moolenaard3aac292020-04-19 14:32:17 +02001925 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001927 case ISN_STOREB:
1928 case ISN_STOREW:
1929 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001931 dictitem_T *di;
1932 hashtab_T *ht;
1933 char_u *name = iptr->isn_arg.string + 2;
1934
Bram Moolenaard3aac292020-04-19 14:32:17 +02001935 switch (iptr->isn_type)
1936 {
1937 case ISN_STOREG:
1938 ht = get_globvar_ht();
1939 break;
1940 case ISN_STOREB:
1941 ht = &curbuf->b_vars->dv_hashtab;
1942 break;
1943 case ISN_STOREW:
1944 ht = &curwin->w_vars->dv_hashtab;
1945 break;
1946 case ISN_STORET:
1947 ht = &curtab->tp_vars->dv_hashtab;
1948 break;
1949 default: // Cannot reach here
1950 goto failed;
1951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952
1953 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001954 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001956 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 else
1958 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001959 SOURCING_LNUM = iptr->isn_lnum;
1960 if (var_check_permission(di, name) == FAIL)
1961 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 clear_tv(&di->di_tv);
1963 di->di_tv = *STACK_TV_BOT(0);
1964 }
1965 }
1966 break;
1967
Bram Moolenaar03290b82020-12-19 16:30:44 +01001968 // store an autoload variable
1969 case ISN_STOREAUTO:
1970 SOURCING_LNUM = iptr->isn_lnum;
1971 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1972 clear_tv(STACK_TV_BOT(-1));
1973 --ectx.ec_stack.ga_len;
1974 break;
1975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 // store number in local variable
1977 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001978 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 clear_tv(tv);
1980 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001981 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 break;
1983
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001984 // store value in list or dict variable
1985 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001986 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001987 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001988 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001989 typval_T *tv_dest = STACK_TV_BOT(-1);
1990 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001991
Bram Moolenaar752fc692021-01-04 21:57:11 +01001992 // Stack contains:
1993 // -3 value to be stored
1994 // -2 index
1995 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001996 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001997 SOURCING_LNUM = iptr->isn_lnum;
1998 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001999 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002000 dest_type = tv_dest->v_type;
2001 if (dest_type == VAR_DICT)
2002 status = do_2string(tv_idx, TRUE);
2003 else if (dest_type == VAR_LIST
2004 && tv_idx->v_type != VAR_NUMBER)
2005 {
2006 emsg(_(e_number_exp));
2007 status = FAIL;
2008 }
2009 }
2010 else if (dest_type != tv_dest->v_type)
2011 {
2012 // just in case, should be OK
2013 semsg(_(e_expected_str_but_got_str),
2014 vartype_name(dest_type),
2015 vartype_name(tv_dest->v_type));
2016 status = FAIL;
2017 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002018
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002019 if (status == OK && dest_type == VAR_LIST)
2020 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002021 long lidx = (long)tv_idx->vval.v_number;
2022 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002023
2024 if (list == NULL)
2025 {
2026 emsg(_(e_list_not_set));
2027 goto on_error;
2028 }
2029 if (lidx < 0 && list->lv_len + lidx >= 0)
2030 // negative index is relative to the end
2031 lidx = list->lv_len + lidx;
2032 if (lidx < 0 || lidx > list->lv_len)
2033 {
2034 semsg(_(e_listidx), lidx);
2035 goto on_error;
2036 }
2037 if (lidx < list->lv_len)
2038 {
2039 listitem_T *li = list_find(list, lidx);
2040
2041 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002042 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002043 goto on_error;
2044 // overwrite existing list item
2045 clear_tv(&li->li_tv);
2046 li->li_tv = *tv;
2047 }
2048 else
2049 {
2050 if (error_if_locked(list->lv_lock,
2051 e_cannot_change_list))
2052 goto on_error;
2053 // append to list, only fails when out of memory
2054 if (list_append_tv(list, tv) == FAIL)
2055 goto failed;
2056 clear_tv(tv);
2057 }
2058 }
2059 else if (status == OK && dest_type == VAR_DICT)
2060 {
2061 char_u *key = tv_idx->vval.v_string;
2062 dict_T *dict = tv_dest->vval.v_dict;
2063 dictitem_T *di;
2064
2065 SOURCING_LNUM = iptr->isn_lnum;
2066 if (dict == NULL)
2067 {
2068 emsg(_(e_dictionary_not_set));
2069 goto on_error;
2070 }
2071 if (key == NULL)
2072 key = (char_u *)"";
2073 di = dict_find(dict, key, -1);
2074 if (di != NULL)
2075 {
2076 if (error_if_locked(di->di_tv.v_lock,
2077 e_cannot_change_dict_item))
2078 goto on_error;
2079 // overwrite existing value
2080 clear_tv(&di->di_tv);
2081 di->di_tv = *tv;
2082 }
2083 else
2084 {
2085 if (error_if_locked(dict->dv_lock,
2086 e_cannot_change_dict))
2087 goto on_error;
2088 // add to dict, only fails when out of memory
2089 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2090 goto failed;
2091 clear_tv(tv);
2092 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002093 }
2094 else
2095 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002096 status = FAIL;
2097 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002098 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002099
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002100 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002101 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002102 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002103 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002104 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002105 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002106 goto on_error;
2107 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002108 }
2109 break;
2110
Bram Moolenaar0186e582021-01-10 18:33:11 +01002111 // load or store variable or argument from outer scope
2112 case ISN_LOADOUTER:
2113 case ISN_STOREOUTER:
2114 {
2115 int depth = iptr->isn_arg.outer.outer_depth;
2116 outer_T *outer = ectx.ec_outer;
2117
2118 while (depth > 1 && outer != NULL)
2119 {
2120 outer = outer->out_up;
2121 --depth;
2122 }
2123 if (outer == NULL)
2124 {
2125 SOURCING_LNUM = iptr->isn_lnum;
2126 iemsg("LOADOUTER depth more than scope levels");
2127 goto failed;
2128 }
2129 tv = ((typval_T *)outer->out_stack->ga_data)
2130 + outer->out_frame_idx + STACK_FRAME_SIZE
2131 + iptr->isn_arg.outer.outer_idx;
2132 if (iptr->isn_type == ISN_LOADOUTER)
2133 {
2134 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2135 goto failed;
2136 copy_tv(tv, STACK_TV_BOT(0));
2137 ++ectx.ec_stack.ga_len;
2138 }
2139 else
2140 {
2141 --ectx.ec_stack.ga_len;
2142 clear_tv(tv);
2143 *tv = *STACK_TV_BOT(0);
2144 }
2145 }
2146 break;
2147
Bram Moolenaar752fc692021-01-04 21:57:11 +01002148 // unlet item in list or dict variable
2149 case ISN_UNLETINDEX:
2150 {
2151 typval_T *tv_idx = STACK_TV_BOT(-2);
2152 typval_T *tv_dest = STACK_TV_BOT(-1);
2153 int status = OK;
2154
2155 // Stack contains:
2156 // -2 index
2157 // -1 dict or list
2158 if (tv_dest->v_type == VAR_DICT)
2159 {
2160 // unlet a dict item, index must be a string
2161 if (tv_idx->v_type != VAR_STRING)
2162 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002163 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002164 semsg(_(e_expected_str_but_got_str),
2165 vartype_name(VAR_STRING),
2166 vartype_name(tv_idx->v_type));
2167 status = FAIL;
2168 }
2169 else
2170 {
2171 dict_T *d = tv_dest->vval.v_dict;
2172 char_u *key = tv_idx->vval.v_string;
2173 dictitem_T *di = NULL;
2174
2175 if (key == NULL)
2176 key = (char_u *)"";
2177 if (d != NULL)
2178 di = dict_find(d, key, (int)STRLEN(key));
2179 if (di == NULL)
2180 {
2181 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002182 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002183 semsg(_(e_dictkey), key);
2184 status = FAIL;
2185 }
2186 else
2187 {
2188 // TODO: check for dict or item locked
2189 dictitem_remove(d, di);
2190 }
2191 }
2192 }
2193 else if (tv_dest->v_type == VAR_LIST)
2194 {
2195 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002196 SOURCING_LNUM = iptr->isn_lnum;
2197 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002198 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002199 status = FAIL;
2200 }
2201 else
2202 {
2203 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002204 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002205 listitem_T *li = NULL;
2206
2207 li = list_find(l, n);
2208 if (li == NULL)
2209 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002210 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002211 semsg(_(e_listidx), n);
2212 status = FAIL;
2213 }
2214 else
2215 // TODO: check for list or item locked
2216 listitem_remove(l, li);
2217 }
2218 }
2219 else
2220 {
2221 status = FAIL;
2222 semsg(_(e_cannot_index_str),
2223 vartype_name(tv_dest->v_type));
2224 }
2225
2226 clear_tv(tv_idx);
2227 clear_tv(tv_dest);
2228 ectx.ec_stack.ga_len -= 2;
2229 if (status == FAIL)
2230 goto on_error;
2231 }
2232 break;
2233
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002234 // unlet range of items in list variable
2235 case ISN_UNLETRANGE:
2236 {
2237 // Stack contains:
2238 // -3 index1
2239 // -2 index2
2240 // -1 dict or list
2241 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2242 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2243 typval_T *tv_dest = STACK_TV_BOT(-1);
2244 int status = OK;
2245
2246 if (tv_dest->v_type == VAR_LIST)
2247 {
2248 // indexes must be a number
2249 SOURCING_LNUM = iptr->isn_lnum;
2250 if (check_for_number(tv_idx1) == FAIL
2251 || check_for_number(tv_idx2) == FAIL)
2252 {
2253 status = FAIL;
2254 }
2255 else
2256 {
2257 list_T *l = tv_dest->vval.v_list;
2258 long n1 = (long)tv_idx1->vval.v_number;
2259 long n2 = (long)tv_idx2->vval.v_number;
2260 listitem_T *li;
2261
2262 li = list_find_index(l, &n1);
2263 if (li == NULL
2264 || list_unlet_range(l, li, NULL, n1,
2265 TRUE, n2) == FAIL)
2266 status = FAIL;
2267 }
2268 }
2269 else
2270 {
2271 status = FAIL;
2272 SOURCING_LNUM = iptr->isn_lnum;
2273 semsg(_(e_cannot_index_str),
2274 vartype_name(tv_dest->v_type));
2275 }
2276
2277 clear_tv(tv_idx1);
2278 clear_tv(tv_idx2);
2279 clear_tv(tv_dest);
2280 ectx.ec_stack.ga_len -= 3;
2281 if (status == FAIL)
2282 goto on_error;
2283 }
2284 break;
2285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 // push constant
2287 case ISN_PUSHNR:
2288 case ISN_PUSHBOOL:
2289 case ISN_PUSHSPEC:
2290 case ISN_PUSHF:
2291 case ISN_PUSHS:
2292 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002293 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002294 case ISN_PUSHCHANNEL:
2295 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002296 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 goto failed;
2298 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002299 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 ++ectx.ec_stack.ga_len;
2301 switch (iptr->isn_type)
2302 {
2303 case ISN_PUSHNR:
2304 tv->v_type = VAR_NUMBER;
2305 tv->vval.v_number = iptr->isn_arg.number;
2306 break;
2307 case ISN_PUSHBOOL:
2308 tv->v_type = VAR_BOOL;
2309 tv->vval.v_number = iptr->isn_arg.number;
2310 break;
2311 case ISN_PUSHSPEC:
2312 tv->v_type = VAR_SPECIAL;
2313 tv->vval.v_number = iptr->isn_arg.number;
2314 break;
2315#ifdef FEAT_FLOAT
2316 case ISN_PUSHF:
2317 tv->v_type = VAR_FLOAT;
2318 tv->vval.v_float = iptr->isn_arg.fnumber;
2319 break;
2320#endif
2321 case ISN_PUSHBLOB:
2322 blob_copy(iptr->isn_arg.blob, tv);
2323 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002324 case ISN_PUSHFUNC:
2325 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002326 if (iptr->isn_arg.string == NULL)
2327 tv->vval.v_string = NULL;
2328 else
2329 tv->vval.v_string =
2330 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002331 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002332 case ISN_PUSHCHANNEL:
2333#ifdef FEAT_JOB_CHANNEL
2334 tv->v_type = VAR_CHANNEL;
2335 tv->vval.v_channel = iptr->isn_arg.channel;
2336 if (tv->vval.v_channel != NULL)
2337 ++tv->vval.v_channel->ch_refcount;
2338#endif
2339 break;
2340 case ISN_PUSHJOB:
2341#ifdef FEAT_JOB_CHANNEL
2342 tv->v_type = VAR_JOB;
2343 tv->vval.v_job = iptr->isn_arg.job;
2344 if (tv->vval.v_job != NULL)
2345 ++tv->vval.v_job->jv_refcount;
2346#endif
2347 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 default:
2349 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002350 tv->vval.v_string = vim_strsave(
2351 iptr->isn_arg.string == NULL
2352 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 }
2354 break;
2355
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002356 case ISN_UNLET:
2357 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2358 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002359 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002360 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002361 case ISN_UNLETENV:
2362 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2363 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002364
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002365 case ISN_LOCKCONST:
2366 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2367 break;
2368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369 // create a list from items on the stack; uses a single allocation
2370 // for the list header and the items
2371 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002372 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2373 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 break;
2375
2376 // create a dict from items on the stack
2377 case ISN_NEWDICT:
2378 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002379 int count = iptr->isn_arg.number;
2380 dict_T *dict = dict_alloc();
2381 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002382 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383
2384 if (dict == NULL)
2385 goto failed;
2386 for (idx = 0; idx < count; ++idx)
2387 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002388 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002390 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002391 key = tv->vval.v_string == NULL
2392 ? (char_u *)"" : tv->vval.v_string;
2393 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002394 if (item != NULL)
2395 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002396 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002397 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002398 dict_unref(dict);
2399 goto on_error;
2400 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002401 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 clear_tv(tv);
2403 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002404 {
2405 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2409 item->di_tv.v_lock = 0;
2410 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002411 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002412 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002413 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 }
2417
2418 if (count > 0)
2419 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002420 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 goto failed;
2422 else
2423 ++ectx.ec_stack.ga_len;
2424 tv = STACK_TV_BOT(-1);
2425 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002426 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 tv->vval.v_dict = dict;
2428 ++dict->dv_refcount;
2429 }
2430 break;
2431
2432 // call a :def function
2433 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002434 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002435 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 iptr->isn_arg.dfunc.cdf_argcount,
2437 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002438 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 break;
2440
2441 // call a builtin function
2442 case ISN_BCALL:
2443 SOURCING_LNUM = iptr->isn_lnum;
2444 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2445 iptr->isn_arg.bfunc.cbf_argcount,
2446 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002447 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 break;
2449
2450 // call a funcref or partial
2451 case ISN_PCALL:
2452 {
2453 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2454 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002455 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456
2457 SOURCING_LNUM = iptr->isn_lnum;
2458 if (pfunc->cpf_top)
2459 {
2460 // funcref is above the arguments
2461 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2462 }
2463 else
2464 {
2465 // Get the funcref from the stack.
2466 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002467 partial_tv = *STACK_TV_BOT(0);
2468 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 }
2470 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002471 if (tv == &partial_tv)
2472 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002474 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 }
2476 break;
2477
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002478 case ISN_PCALL_END:
2479 // PCALL finished, arguments have been consumed and replaced by
2480 // the return value. Now clear the funcref from the stack,
2481 // and move the return value in its place.
2482 --ectx.ec_stack.ga_len;
2483 clear_tv(STACK_TV_BOT(-1));
2484 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2485 break;
2486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 // call a user defined function or funcref/partial
2488 case ISN_UCALL:
2489 {
2490 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2491
2492 SOURCING_LNUM = iptr->isn_lnum;
2493 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002494 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002495 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 }
2497 break;
2498
2499 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002500 case ISN_RETURN_ZERO:
2501 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2502 goto failed;
2503 tv = STACK_TV_BOT(0);
2504 ++ectx.ec_stack.ga_len;
2505 tv->v_type = VAR_NUMBER;
2506 tv->vval.v_number = 0;
2507 tv->v_lock = 0;
2508 // FALLTHROUGH
2509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 case ISN_RETURN:
2511 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002512 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002513 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002514
2515 if (trystack->ga_len > 0)
2516 trycmd = ((trycmd_T *)trystack->ga_data)
2517 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002518 if (trycmd != NULL
2519 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaarc150c092021-02-13 15:02:46 +01002520 && ectx.ec_instr[trycmd->tcd_finally_idx]
2521 .isn_type != ISN_ENDTRY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 {
2523 // jump to ":finally"
2524 ectx.ec_iidx = trycmd->tcd_finally_idx;
2525 trycmd->tcd_return = TRUE;
2526 }
2527 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002528 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 }
2530 break;
2531
2532 // push a function reference to a compiled function
2533 case ISN_FUNCREF:
2534 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002535 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2536 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2537 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 if (pt == NULL)
2540 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002541 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002542 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002543 vim_free(pt);
2544 goto failed;
2545 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002546 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2547 &ectx) == FAIL)
2548 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 tv = STACK_TV_BOT(0);
2551 ++ectx.ec_stack.ga_len;
2552 tv->vval.v_partial = pt;
2553 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002554 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 }
2556 break;
2557
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002558 // Create a global function from a lambda.
2559 case ISN_NEWFUNC:
2560 {
2561 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2562
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002563 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002564 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002565 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002566 }
2567 break;
2568
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002569 // List functions
2570 case ISN_DEF:
2571 if (iptr->isn_arg.string == NULL)
2572 list_functions(NULL);
2573 else
2574 {
2575 exarg_T ea;
2576
2577 CLEAR_FIELD(ea);
2578 ea.cmd = ea.arg = iptr->isn_arg.string;
2579 define_function(&ea, NULL);
2580 }
2581 break;
2582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 // jump if a condition is met
2584 case ISN_JUMP:
2585 {
2586 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002587 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 int jump = TRUE;
2589
2590 if (when != JUMP_ALWAYS)
2591 {
2592 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002593 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002594 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002595 || when == JUMP_IF_COND_TRUE)
2596 {
2597 SOURCING_LNUM = iptr->isn_lnum;
2598 jump = tv_get_bool_chk(tv, &error);
2599 if (error)
2600 goto on_error;
2601 }
2602 else
2603 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002605 || when == JUMP_AND_KEEP_IF_FALSE
2606 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002608 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 {
2610 // drop the value from the stack
2611 clear_tv(tv);
2612 --ectx.ec_stack.ga_len;
2613 }
2614 }
2615 if (jump)
2616 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2617 }
2618 break;
2619
2620 // top of a for loop
2621 case ISN_FOR:
2622 {
2623 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2624 typval_T *idxtv =
2625 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2626
2627 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002628 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002630 ++idxtv->vval.v_number;
2631 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 // past the end of the list, jump to "endfor"
2633 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2634 else if (list->lv_first == &range_list_item)
2635 {
2636 // non-materialized range() list
2637 tv = STACK_TV_BOT(0);
2638 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002639 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 tv->vval.v_number = list_find_nr(
2641 list, idxtv->vval.v_number, NULL);
2642 ++ectx.ec_stack.ga_len;
2643 }
2644 else
2645 {
2646 listitem_T *li = list_find(list, idxtv->vval.v_number);
2647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2649 ++ectx.ec_stack.ga_len;
2650 }
2651 }
2652 break;
2653
2654 // start of ":try" block
2655 case ISN_TRY:
2656 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002657 trycmd_T *trycmd = NULL;
2658
Bram Moolenaar270d0382020-05-15 21:42:53 +02002659 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 goto failed;
2661 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2662 + ectx.ec_trystack.ga_len;
2663 ++ectx.ec_trystack.ga_len;
2664 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002665 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002666 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002667 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2669 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
2670 }
2671 break;
2672
2673 case ISN_PUSHEXC:
2674 if (current_exception == NULL)
2675 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002676 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 iemsg("Evaluating catch while current_exception is NULL");
2678 goto failed;
2679 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002680 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 goto failed;
2682 tv = STACK_TV_BOT(0);
2683 ++ectx.ec_stack.ga_len;
2684 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002685 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 tv->vval.v_string = vim_strsave(
2687 (char_u *)current_exception->value);
2688 break;
2689
2690 case ISN_CATCH:
2691 {
2692 garray_T *trystack = &ectx.ec_trystack;
2693
Bram Moolenaar20a76292020-12-25 19:47:24 +01002694 if (restore_cmdmod)
2695 {
2696 cmdmod.cmod_filter_regmatch.regprog = NULL;
2697 undo_cmdmod(&cmdmod);
2698 cmdmod = save_cmdmod;
2699 restore_cmdmod = FALSE;
2700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 if (trystack->ga_len > 0)
2702 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002703 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 + trystack->ga_len - 1;
2705 trycmd->tcd_caught = TRUE;
2706 }
2707 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002708 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 catch_exception(current_exception);
2710 }
2711 break;
2712
Bram Moolenaarc150c092021-02-13 15:02:46 +01002713 case ISN_TRYCONT:
2714 {
2715 garray_T *trystack = &ectx.ec_trystack;
2716 trycont_T *trycont = &iptr->isn_arg.trycont;
2717 int i;
2718 trycmd_T *trycmd;
2719 int iidx = trycont->tct_where;
2720
2721 if (trystack->ga_len < trycont->tct_levels)
2722 {
2723 siemsg("TRYCONT: expected %d levels, found %d",
2724 trycont->tct_levels, trystack->ga_len);
2725 goto failed;
2726 }
2727 // Make :endtry jump to any outer try block and the last
2728 // :endtry inside the loop to the loop start.
2729 for (i = trycont->tct_levels; i > 0; --i)
2730 {
2731 trycmd = ((trycmd_T *)trystack->ga_data)
2732 + trystack->ga_len - i;
2733 trycmd->tcd_cont = iidx;
2734 iidx = trycmd->tcd_finally_idx;
2735 }
2736 // jump to :finally or :endtry of current try statement
2737 ectx.ec_iidx = iidx;
2738 }
2739 break;
2740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 // end of ":try" block
2742 case ISN_ENDTRY:
2743 {
2744 garray_T *trystack = &ectx.ec_trystack;
2745
2746 if (trystack->ga_len > 0)
2747 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002748 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 --trystack->ga_len;
2751 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002752 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 trycmd = ((trycmd_T *)trystack->ga_data)
2754 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002755 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 {
2757 // discard the exception
2758 if (caught_stack == current_exception)
2759 caught_stack = caught_stack->caught;
2760 discard_current_exception();
2761 }
2762
2763 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002764 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002765
2766 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2767 {
2768 --ectx.ec_stack.ga_len;
2769 clear_tv(STACK_TV_BOT(0));
2770 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002771 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002772 // handling :continue: jump to outer try block or
2773 // start of the loop
2774 ectx.ec_iidx = trycmd->tcd_cont;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 }
2776 }
2777 break;
2778
2779 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002780 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002781 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002782
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002783 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2784 {
2785 // throwing an exception while using "silent!" causes
2786 // the function to abort but not display an error.
2787 tv = STACK_TV_BOT(-1);
2788 clear_tv(tv);
2789 tv->v_type = VAR_NUMBER;
2790 tv->vval.v_number = 0;
2791 goto done;
2792 }
2793 --ectx.ec_stack.ga_len;
2794 tv = STACK_TV_BOT(0);
2795 if (tv->vval.v_string == NULL
2796 || *skipwhite(tv->vval.v_string) == NUL)
2797 {
2798 vim_free(tv->vval.v_string);
2799 SOURCING_LNUM = iptr->isn_lnum;
2800 emsg(_(e_throw_with_empty_string));
2801 goto failed;
2802 }
2803
2804 // Inside a "catch" we need to first discard the caught
2805 // exception.
2806 if (trystack->ga_len > 0)
2807 {
2808 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2809 + trystack->ga_len - 1;
2810 if (trycmd->tcd_caught && current_exception != NULL)
2811 {
2812 // discard the exception
2813 if (caught_stack == current_exception)
2814 caught_stack = caught_stack->caught;
2815 discard_current_exception();
2816 trycmd->tcd_caught = FALSE;
2817 }
2818 }
2819
2820 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2821 == FAIL)
2822 {
2823 vim_free(tv->vval.v_string);
2824 goto failed;
2825 }
2826 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 break;
2829
2830 // compare with special values
2831 case ISN_COMPAREBOOL:
2832 case ISN_COMPARESPECIAL:
2833 {
2834 typval_T *tv1 = STACK_TV_BOT(-2);
2835 typval_T *tv2 = STACK_TV_BOT(-1);
2836 varnumber_T arg1 = tv1->vval.v_number;
2837 varnumber_T arg2 = tv2->vval.v_number;
2838 int res;
2839
2840 switch (iptr->isn_arg.op.op_type)
2841 {
2842 case EXPR_EQUAL: res = arg1 == arg2; break;
2843 case EXPR_NEQUAL: res = arg1 != arg2; break;
2844 default: res = 0; break;
2845 }
2846
2847 --ectx.ec_stack.ga_len;
2848 tv1->v_type = VAR_BOOL;
2849 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2850 }
2851 break;
2852
2853 // Operation with two number arguments
2854 case ISN_OPNR:
2855 case ISN_COMPARENR:
2856 {
2857 typval_T *tv1 = STACK_TV_BOT(-2);
2858 typval_T *tv2 = STACK_TV_BOT(-1);
2859 varnumber_T arg1 = tv1->vval.v_number;
2860 varnumber_T arg2 = tv2->vval.v_number;
2861 varnumber_T res;
2862
2863 switch (iptr->isn_arg.op.op_type)
2864 {
2865 case EXPR_MULT: res = arg1 * arg2; break;
2866 case EXPR_DIV: res = arg1 / arg2; break;
2867 case EXPR_REM: res = arg1 % arg2; break;
2868 case EXPR_SUB: res = arg1 - arg2; break;
2869 case EXPR_ADD: res = arg1 + arg2; break;
2870
2871 case EXPR_EQUAL: res = arg1 == arg2; break;
2872 case EXPR_NEQUAL: res = arg1 != arg2; break;
2873 case EXPR_GREATER: res = arg1 > arg2; break;
2874 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2875 case EXPR_SMALLER: res = arg1 < arg2; break;
2876 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2877 default: res = 0; break;
2878 }
2879
2880 --ectx.ec_stack.ga_len;
2881 if (iptr->isn_type == ISN_COMPARENR)
2882 {
2883 tv1->v_type = VAR_BOOL;
2884 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2885 }
2886 else
2887 tv1->vval.v_number = res;
2888 }
2889 break;
2890
2891 // Computation with two float arguments
2892 case ISN_OPFLOAT:
2893 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002894#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 {
2896 typval_T *tv1 = STACK_TV_BOT(-2);
2897 typval_T *tv2 = STACK_TV_BOT(-1);
2898 float_T arg1 = tv1->vval.v_float;
2899 float_T arg2 = tv2->vval.v_float;
2900 float_T res = 0;
2901 int cmp = FALSE;
2902
2903 switch (iptr->isn_arg.op.op_type)
2904 {
2905 case EXPR_MULT: res = arg1 * arg2; break;
2906 case EXPR_DIV: res = arg1 / arg2; break;
2907 case EXPR_SUB: res = arg1 - arg2; break;
2908 case EXPR_ADD: res = arg1 + arg2; break;
2909
2910 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2911 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2912 case EXPR_GREATER: cmp = arg1 > arg2; break;
2913 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2914 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2915 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2916 default: cmp = 0; break;
2917 }
2918 --ectx.ec_stack.ga_len;
2919 if (iptr->isn_type == ISN_COMPAREFLOAT)
2920 {
2921 tv1->v_type = VAR_BOOL;
2922 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2923 }
2924 else
2925 tv1->vval.v_float = res;
2926 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002927#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 break;
2929
2930 case ISN_COMPARELIST:
2931 {
2932 typval_T *tv1 = STACK_TV_BOT(-2);
2933 typval_T *tv2 = STACK_TV_BOT(-1);
2934 list_T *arg1 = tv1->vval.v_list;
2935 list_T *arg2 = tv2->vval.v_list;
2936 int cmp = FALSE;
2937 int ic = iptr->isn_arg.op.op_ic;
2938
2939 switch (iptr->isn_arg.op.op_type)
2940 {
2941 case EXPR_EQUAL: cmp =
2942 list_equal(arg1, arg2, ic, FALSE); break;
2943 case EXPR_NEQUAL: cmp =
2944 !list_equal(arg1, arg2, ic, FALSE); break;
2945 case EXPR_IS: cmp = arg1 == arg2; break;
2946 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2947 default: cmp = 0; break;
2948 }
2949 --ectx.ec_stack.ga_len;
2950 clear_tv(tv1);
2951 clear_tv(tv2);
2952 tv1->v_type = VAR_BOOL;
2953 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2954 }
2955 break;
2956
2957 case ISN_COMPAREBLOB:
2958 {
2959 typval_T *tv1 = STACK_TV_BOT(-2);
2960 typval_T *tv2 = STACK_TV_BOT(-1);
2961 blob_T *arg1 = tv1->vval.v_blob;
2962 blob_T *arg2 = tv2->vval.v_blob;
2963 int cmp = FALSE;
2964
2965 switch (iptr->isn_arg.op.op_type)
2966 {
2967 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2968 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2969 case EXPR_IS: cmp = arg1 == arg2; break;
2970 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2971 default: cmp = 0; break;
2972 }
2973 --ectx.ec_stack.ga_len;
2974 clear_tv(tv1);
2975 clear_tv(tv2);
2976 tv1->v_type = VAR_BOOL;
2977 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2978 }
2979 break;
2980
2981 // TODO: handle separately
2982 case ISN_COMPARESTRING:
2983 case ISN_COMPAREDICT:
2984 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 case ISN_COMPAREANY:
2986 {
2987 typval_T *tv1 = STACK_TV_BOT(-2);
2988 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002989 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 int ic = iptr->isn_arg.op.op_ic;
2991
Bram Moolenaareb26f432020-09-14 16:50:05 +02002992 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002993 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 --ectx.ec_stack.ga_len;
2996 }
2997 break;
2998
2999 case ISN_ADDLIST:
3000 case ISN_ADDBLOB:
3001 {
3002 typval_T *tv1 = STACK_TV_BOT(-2);
3003 typval_T *tv2 = STACK_TV_BOT(-1);
3004
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003005 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 if (iptr->isn_type == ISN_ADDLIST)
3007 eval_addlist(tv1, tv2);
3008 else
3009 eval_addblob(tv1, tv2);
3010 clear_tv(tv2);
3011 --ectx.ec_stack.ga_len;
3012 }
3013 break;
3014
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003015 case ISN_LISTAPPEND:
3016 {
3017 typval_T *tv1 = STACK_TV_BOT(-2);
3018 typval_T *tv2 = STACK_TV_BOT(-1);
3019 list_T *l = tv1->vval.v_list;
3020
3021 // add an item to a list
3022 if (l == NULL)
3023 {
3024 SOURCING_LNUM = iptr->isn_lnum;
3025 emsg(_(e_cannot_add_to_null_list));
3026 goto on_error;
3027 }
3028 if (list_append_tv(l, tv2) == FAIL)
3029 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003030 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003031 --ectx.ec_stack.ga_len;
3032 }
3033 break;
3034
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003035 case ISN_BLOBAPPEND:
3036 {
3037 typval_T *tv1 = STACK_TV_BOT(-2);
3038 typval_T *tv2 = STACK_TV_BOT(-1);
3039 blob_T *b = tv1->vval.v_blob;
3040 int error = FALSE;
3041 varnumber_T n;
3042
3043 // add a number to a blob
3044 if (b == NULL)
3045 {
3046 SOURCING_LNUM = iptr->isn_lnum;
3047 emsg(_(e_cannot_add_to_null_blob));
3048 goto on_error;
3049 }
3050 n = tv_get_number_chk(tv2, &error);
3051 if (error)
3052 goto on_error;
3053 ga_append(&b->bv_ga, (int)n);
3054 --ectx.ec_stack.ga_len;
3055 }
3056 break;
3057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 // Computation with two arguments of unknown type
3059 case ISN_OPANY:
3060 {
3061 typval_T *tv1 = STACK_TV_BOT(-2);
3062 typval_T *tv2 = STACK_TV_BOT(-1);
3063 varnumber_T n1, n2;
3064#ifdef FEAT_FLOAT
3065 float_T f1 = 0, f2 = 0;
3066#endif
3067 int error = FALSE;
3068
3069 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3070 {
3071 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3072 {
3073 eval_addlist(tv1, tv2);
3074 clear_tv(tv2);
3075 --ectx.ec_stack.ga_len;
3076 break;
3077 }
3078 else if (tv1->v_type == VAR_BLOB
3079 && tv2->v_type == VAR_BLOB)
3080 {
3081 eval_addblob(tv1, tv2);
3082 clear_tv(tv2);
3083 --ectx.ec_stack.ga_len;
3084 break;
3085 }
3086 }
3087#ifdef FEAT_FLOAT
3088 if (tv1->v_type == VAR_FLOAT)
3089 {
3090 f1 = tv1->vval.v_float;
3091 n1 = 0;
3092 }
3093 else
3094#endif
3095 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003096 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 n1 = tv_get_number_chk(tv1, &error);
3098 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003099 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100#ifdef FEAT_FLOAT
3101 if (tv2->v_type == VAR_FLOAT)
3102 f1 = n1;
3103#endif
3104 }
3105#ifdef FEAT_FLOAT
3106 if (tv2->v_type == VAR_FLOAT)
3107 {
3108 f2 = tv2->vval.v_float;
3109 n2 = 0;
3110 }
3111 else
3112#endif
3113 {
3114 n2 = tv_get_number_chk(tv2, &error);
3115 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003116 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117#ifdef FEAT_FLOAT
3118 if (tv1->v_type == VAR_FLOAT)
3119 f2 = n2;
3120#endif
3121 }
3122#ifdef FEAT_FLOAT
3123 // if there is a float on either side the result is a float
3124 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3125 {
3126 switch (iptr->isn_arg.op.op_type)
3127 {
3128 case EXPR_MULT: f1 = f1 * f2; break;
3129 case EXPR_DIV: f1 = f1 / f2; break;
3130 case EXPR_SUB: f1 = f1 - f2; break;
3131 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003132 default: SOURCING_LNUM = iptr->isn_lnum;
3133 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003134 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 }
3136 clear_tv(tv1);
3137 clear_tv(tv2);
3138 tv1->v_type = VAR_FLOAT;
3139 tv1->vval.v_float = f1;
3140 --ectx.ec_stack.ga_len;
3141 }
3142 else
3143#endif
3144 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003145 int failed = FALSE;
3146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 switch (iptr->isn_arg.op.op_type)
3148 {
3149 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003150 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3151 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003152 goto on_error;
3153 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 case EXPR_SUB: n1 = n1 - n2; break;
3155 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003156 default: n1 = num_modulus(n1, n2, &failed);
3157 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003158 goto on_error;
3159 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 }
3161 clear_tv(tv1);
3162 clear_tv(tv2);
3163 tv1->v_type = VAR_NUMBER;
3164 tv1->vval.v_number = n1;
3165 --ectx.ec_stack.ga_len;
3166 }
3167 }
3168 break;
3169
3170 case ISN_CONCAT:
3171 {
3172 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3173 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3174 char_u *res;
3175
3176 res = concat_str(str1, str2);
3177 clear_tv(STACK_TV_BOT(-2));
3178 clear_tv(STACK_TV_BOT(-1));
3179 --ectx.ec_stack.ga_len;
3180 STACK_TV_BOT(-1)->vval.v_string = res;
3181 }
3182 break;
3183
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003184 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003185 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003186 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003187 int is_slice = iptr->isn_type == ISN_STRSLICE;
3188 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003189 char_u *res;
3190
3191 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003192 // string slice: string is at stack-3, first index at
3193 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003194 if (is_slice)
3195 {
3196 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003197 n1 = tv->vval.v_number;
3198 }
3199
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003200 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003201 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003202
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003203 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003204 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003205 if (is_slice)
3206 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003207 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003208 else
3209 // Index: The resulting variable is a string of a
3210 // single character. If the index is too big or
3211 // negative the result is empty.
3212 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003213 vim_free(tv->vval.v_string);
3214 tv->vval.v_string = res;
3215 }
3216 break;
3217
3218 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003219 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 {
Bram Moolenaared591872020-08-15 22:14:53 +02003221 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003223 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224
3225 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003226 // list slice: list is at stack-3, indexes at stack-2 and
3227 // stack-1
3228 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 list = tv->vval.v_list;
3230
3231 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003232 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003234
3235 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 {
Bram Moolenaared591872020-08-15 22:14:53 +02003237 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003238 n1 = tv->vval.v_number;
3239 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 }
Bram Moolenaared591872020-08-15 22:14:53 +02003241
3242 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003243 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003244 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003245 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3246 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003247 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 }
3249 break;
3250
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003251 case ISN_ANYINDEX:
3252 case ISN_ANYSLICE:
3253 {
3254 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3255 typval_T *var1, *var2;
3256 int res;
3257
3258 // index: composite is at stack-2, index at stack-1
3259 // slice: composite is at stack-3, indexes at stack-2 and
3260 // stack-1
3261 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003262 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003263 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3264 goto on_error;
3265 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3266 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003267 res = eval_index_inner(tv, is_slice, var1, var2,
3268 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003269 clear_tv(var1);
3270 if (is_slice)
3271 clear_tv(var2);
3272 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3273 if (res == FAIL)
3274 goto on_error;
3275 }
3276 break;
3277
Bram Moolenaar9af78762020-06-16 11:34:42 +02003278 case ISN_SLICE:
3279 {
3280 list_T *list;
3281 int count = iptr->isn_arg.number;
3282
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003283 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003284 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003285 list = tv->vval.v_list;
3286
3287 // no error for short list, expect it to be checked earlier
3288 if (list != NULL && list->lv_len >= count)
3289 {
3290 list_T *newlist = list_slice(list,
3291 count, list->lv_len - 1);
3292
3293 if (newlist != NULL)
3294 {
3295 list_unref(list);
3296 tv->vval.v_list = newlist;
3297 ++newlist->lv_refcount;
3298 }
3299 }
3300 }
3301 break;
3302
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003303 case ISN_GETITEM:
3304 {
3305 listitem_T *li;
3306 int index = iptr->isn_arg.number;
3307
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003308 // Get list item: list is at stack-1, push item.
3309 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003310 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003311 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003312
3313 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3314 goto failed;
3315 ++ectx.ec_stack.ga_len;
3316 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003317
3318 // Useful when used in unpack assignment. Reset at
3319 // ISN_DROP.
3320 where.wt_index = index + 1;
3321 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003322 }
3323 break;
3324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 case ISN_MEMBER:
3326 {
3327 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003328 char_u *key;
3329 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003330 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003331
3332 // dict member: dict is at stack-2, key at stack-1
3333 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003334 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003335 dict = tv->vval.v_dict;
3336
3337 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003338 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003339 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003340 if (key == NULL)
3341 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003342
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003343 if ((di = dict_find(dict, key, -1)) == NULL)
3344 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003345 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003346 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003347
3348 // If :silent! is used we will continue, make sure the
3349 // stack contents makes sense.
3350 clear_tv(tv);
3351 --ectx.ec_stack.ga_len;
3352 tv = STACK_TV_BOT(-1);
3353 clear_tv(tv);
3354 tv->v_type = VAR_NUMBER;
3355 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003356 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003357 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003358 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003359 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003360 // Clear the dict only after getting the item, to avoid
3361 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003362 tv = STACK_TV_BOT(-1);
3363 temp_tv = *tv;
3364 copy_tv(&di->di_tv, tv);
3365 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003366 }
3367 break;
3368
3369 // dict member with string key
3370 case ISN_STRINGMEMBER:
3371 {
3372 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003374 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375
3376 tv = STACK_TV_BOT(-1);
3377 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3378 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003379 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003381 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 }
3383 dict = tv->vval.v_dict;
3384
3385 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3386 == NULL)
3387 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003388 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003390 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003392 // Clear the dict after getting the item, to avoid that it
3393 // make the item invalid.
3394 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003396 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 }
3398 break;
3399
3400 case ISN_NEGATENR:
3401 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003402 if (tv->v_type != VAR_NUMBER
3403#ifdef FEAT_FLOAT
3404 && tv->v_type != VAR_FLOAT
3405#endif
3406 )
3407 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003408 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003409 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003410 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003411 }
3412#ifdef FEAT_FLOAT
3413 if (tv->v_type == VAR_FLOAT)
3414 tv->vval.v_float = -tv->vval.v_float;
3415 else
3416#endif
3417 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 break;
3419
3420 case ISN_CHECKNR:
3421 {
3422 int error = FALSE;
3423
3424 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003425 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003427 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 (void)tv_get_number_chk(tv, &error);
3429 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003430 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 }
3432 break;
3433
3434 case ISN_CHECKTYPE:
3435 {
3436 checktype_T *ct = &iptr->isn_arg.type;
3437
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003438 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003439 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003440 if (!where.wt_variable)
3441 where.wt_index = ct->ct_arg_idx;
3442 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003443 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003444 if (!where.wt_variable)
3445 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003446
3447 // number 0 is FALSE, number 1 is TRUE
3448 if (tv->v_type == VAR_NUMBER
3449 && ct->ct_type->tt_type == VAR_BOOL
3450 && (tv->vval.v_number == 0
3451 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003453 tv->v_type = VAR_BOOL;
3454 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003455 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 }
3457 }
3458 break;
3459
Bram Moolenaar9af78762020-06-16 11:34:42 +02003460 case ISN_CHECKLEN:
3461 {
3462 int min_len = iptr->isn_arg.checklen.cl_min_len;
3463 list_T *list = NULL;
3464
3465 tv = STACK_TV_BOT(-1);
3466 if (tv->v_type == VAR_LIST)
3467 list = tv->vval.v_list;
3468 if (list == NULL || list->lv_len < min_len
3469 || (list->lv_len > min_len
3470 && !iptr->isn_arg.checklen.cl_more_OK))
3471 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003472 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003473 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003474 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003475 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003476 }
3477 }
3478 break;
3479
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003480 case ISN_SETTYPE:
3481 {
3482 checktype_T *ct = &iptr->isn_arg.type;
3483
3484 tv = STACK_TV_BOT(-1);
3485 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3486 {
3487 free_type(tv->vval.v_dict->dv_type);
3488 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3489 }
3490 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3491 {
3492 free_type(tv->vval.v_list->lv_type);
3493 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3494 }
3495 }
3496 break;
3497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003499 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 {
3501 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003502 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503
3504 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003505 if (iptr->isn_type == ISN_2BOOL)
3506 {
3507 n = tv2bool(tv);
3508 if (iptr->isn_arg.number) // invert
3509 n = !n;
3510 }
3511 else
3512 {
3513 SOURCING_LNUM = iptr->isn_lnum;
3514 n = tv_get_bool_chk(tv, &error);
3515 if (error)
3516 goto on_error;
3517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 clear_tv(tv);
3519 tv->v_type = VAR_BOOL;
3520 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3521 }
3522 break;
3523
3524 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003525 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003526 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003527 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3528 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3529 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 break;
3531
Bram Moolenaar08597872020-12-10 19:43:40 +01003532 case ISN_RANGE:
3533 {
3534 exarg_T ea;
3535 char *errormsg;
3536
Bram Moolenaarece0b872021-01-08 20:40:45 +01003537 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003538 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003539 ea.addr_type = ADDR_LINES;
3540 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003541 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003542 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003543 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003544
3545 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3546 goto failed;
3547 ++ectx.ec_stack.ga_len;
3548 tv = STACK_TV_BOT(-1);
3549 tv->v_type = VAR_NUMBER;
3550 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003551 if (ea.addr_count == 0)
3552 tv->vval.v_number = curwin->w_cursor.lnum;
3553 else
3554 tv->vval.v_number = ea.line2;
3555 }
3556 break;
3557
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003558 case ISN_PUT:
3559 {
3560 int regname = iptr->isn_arg.put.put_regname;
3561 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3562 char_u *expr = NULL;
3563 int dir = FORWARD;
3564
Bram Moolenaar08597872020-12-10 19:43:40 +01003565 if (lnum < -2)
3566 {
3567 // line number was put on the stack by ISN_RANGE
3568 tv = STACK_TV_BOT(-1);
3569 curwin->w_cursor.lnum = tv->vval.v_number;
3570 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3571 dir = BACKWARD;
3572 --ectx.ec_stack.ga_len;
3573 }
3574 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003575 // :put! above cursor
3576 dir = BACKWARD;
3577 else if (lnum >= 0)
3578 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003579
3580 if (regname == '=')
3581 {
3582 tv = STACK_TV_BOT(-1);
3583 if (tv->v_type == VAR_STRING)
3584 expr = tv->vval.v_string;
3585 else
3586 {
3587 expr = typval2string(tv, TRUE); // allocates value
3588 clear_tv(tv);
3589 }
3590 --ectx.ec_stack.ga_len;
3591 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003592 check_cursor();
3593 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3594 vim_free(expr);
3595 }
3596 break;
3597
Bram Moolenaar02194d22020-10-24 23:08:38 +02003598 case ISN_CMDMOD:
3599 save_cmdmod = cmdmod;
3600 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003601 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003602 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3603 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003604 break;
3605
Bram Moolenaar02194d22020-10-24 23:08:38 +02003606 case ISN_CMDMOD_REV:
3607 // filter regprog is owned by the instruction, don't free it
3608 cmdmod.cmod_filter_regmatch.regprog = NULL;
3609 undo_cmdmod(&cmdmod);
3610 cmdmod = save_cmdmod;
3611 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003612 break;
3613
Bram Moolenaar792f7862020-11-23 08:31:18 +01003614 case ISN_UNPACK:
3615 {
3616 int count = iptr->isn_arg.unpack.unp_count;
3617 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3618 list_T *l;
3619 listitem_T *li;
3620 int i;
3621
3622 // Check there is a valid list to unpack.
3623 tv = STACK_TV_BOT(-1);
3624 if (tv->v_type != VAR_LIST)
3625 {
3626 SOURCING_LNUM = iptr->isn_lnum;
3627 emsg(_(e_for_argument_must_be_sequence_of_lists));
3628 goto on_error;
3629 }
3630 l = tv->vval.v_list;
3631 if (l == NULL
3632 || l->lv_len < (semicolon ? count - 1 : count))
3633 {
3634 SOURCING_LNUM = iptr->isn_lnum;
3635 emsg(_(e_list_value_does_not_have_enough_items));
3636 goto on_error;
3637 }
3638 else if (!semicolon && l->lv_len > count)
3639 {
3640 SOURCING_LNUM = iptr->isn_lnum;
3641 emsg(_(e_list_value_has_more_items_than_targets));
3642 goto on_error;
3643 }
3644
3645 CHECK_LIST_MATERIALIZE(l);
3646 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3647 goto failed;
3648 ectx.ec_stack.ga_len += count - 1;
3649
3650 // Variable after semicolon gets a list with the remaining
3651 // items.
3652 if (semicolon)
3653 {
3654 list_T *rem_list =
3655 list_alloc_with_items(l->lv_len - count + 1);
3656
3657 if (rem_list == NULL)
3658 goto failed;
3659 tv = STACK_TV_BOT(-count);
3660 tv->vval.v_list = rem_list;
3661 ++rem_list->lv_refcount;
3662 tv->v_lock = 0;
3663 li = l->lv_first;
3664 for (i = 0; i < count - 1; ++i)
3665 li = li->li_next;
3666 for (i = 0; li != NULL; ++i)
3667 {
3668 list_set_item(rem_list, i, &li->li_tv);
3669 li = li->li_next;
3670 }
3671 --count;
3672 }
3673
3674 // Produce the values in reverse order, first item last.
3675 li = l->lv_first;
3676 for (i = 0; i < count; ++i)
3677 {
3678 tv = STACK_TV_BOT(-i - 1);
3679 copy_tv(&li->li_tv, tv);
3680 li = li->li_next;
3681 }
3682
3683 list_unref(l);
3684 }
3685 break;
3686
Bram Moolenaarb2049902021-01-24 12:53:53 +01003687 case ISN_PROF_START:
3688 case ISN_PROF_END:
3689 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003690#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003691 funccall_T cookie;
3692 ufunc_T *cur_ufunc =
3693 (((dfunc_T *)def_functions.ga_data)
3694 + ectx.ec_dfunc_idx)->df_ufunc;
3695
3696 cookie.func = cur_ufunc;
3697 if (iptr->isn_type == ISN_PROF_START)
3698 {
3699 func_line_start(&cookie, iptr->isn_lnum);
3700 // if we get here the instruction is executed
3701 func_line_exec(&cookie);
3702 }
3703 else
3704 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003705#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003706 }
3707 break;
3708
Bram Moolenaar389df252020-07-09 21:20:47 +02003709 case ISN_SHUFFLE:
3710 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003711 typval_T tmp_tv;
3712 int item = iptr->isn_arg.shuffle.shfl_item;
3713 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003714
3715 tmp_tv = *STACK_TV_BOT(-item);
3716 for ( ; up > 0 && item > 1; --up)
3717 {
3718 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3719 --item;
3720 }
3721 *STACK_TV_BOT(-item) = tmp_tv;
3722 }
3723 break;
3724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 case ISN_DROP:
3726 --ectx.ec_stack.ga_len;
3727 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003728 where.wt_index = 0;
3729 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 break;
3731 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003732 continue;
3733
Bram Moolenaard032f342020-07-18 18:13:02 +02003734func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003735 // Restore previous function. If the frame pointer is where we started
3736 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003737 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003738 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003739
Bram Moolenaard032f342020-07-18 18:13:02 +02003740 if (func_return(&ectx) == FAIL)
3741 // only fails when out of memory
3742 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003743 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003744
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003745on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003746 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003747 // If "emsg_silent" is set then ignore the error, unless it was set
3748 // when calling the function.
3749 if (did_emsg_cumul + did_emsg == did_emsg_before
3750 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003751 {
3752 // If a sequence of instructions causes an error while ":silent!"
3753 // was used, restore the stack length and jump ahead to restoring
3754 // the cmdmod.
3755 if (restore_cmdmod)
3756 {
3757 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3758 {
3759 --ectx.ec_stack.ga_len;
3760 clear_tv(STACK_TV_BOT(0));
3761 }
3762 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3763 ++ectx.ec_iidx;
3764 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003765 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003766 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003767on_fatal_error:
3768 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003769 // If we are not inside a try-catch started here, abort execution.
3770 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003771 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 }
3773
3774done:
3775 // function finished, get result from the stack.
3776 tv = STACK_TV_BOT(-1);
3777 *rettv = *tv;
3778 tv->v_type = VAR_UNKNOWN;
3779 ret = OK;
3780
3781failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003782 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003783 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003784 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003785
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003786 // Deal with any remaining closures, they may be in use somewhere.
3787 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003788 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003789 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003790 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3791 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003792
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003793 estack_pop();
3794 current_sctx = save_current_sctx;
3795
Bram Moolenaar352134b2020-10-17 22:04:08 +02003796 if (*msg_list != NULL && saved_msg_list != NULL)
3797 {
3798 msglist_T **plist = saved_msg_list;
3799
3800 // Append entries from the current msg_list (uncaught exceptions) to
3801 // the saved msg_list.
3802 while (*plist != NULL)
3803 plist = &(*plist)->next;
3804
3805 *plist = *msg_list;
3806 }
3807 msg_list = saved_msg_list;
3808
Bram Moolenaar02194d22020-10-24 23:08:38 +02003809 if (restore_cmdmod)
3810 {
3811 cmdmod.cmod_filter_regmatch.regprog = NULL;
3812 undo_cmdmod(&cmdmod);
3813 cmdmod = save_cmdmod;
3814 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003815 emsg_silent_def = save_emsg_silent_def;
3816 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003817
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003818failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003819 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3821 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003824 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003825
Bram Moolenaar0186e582021-01-10 18:33:11 +01003826 while (ectx.ec_outer != NULL)
3827 {
3828 outer_T *up = ectx.ec_outer->out_up_is_copy
3829 ? NULL : ectx.ec_outer->out_up;
3830
3831 vim_free(ectx.ec_outer);
3832 ectx.ec_outer = up;
3833 }
3834
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003835 // Not sure if this is necessary.
3836 suppress_errthrow = save_suppress_errthrow;
3837
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003838 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003839 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003840 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003841 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 return ret;
3843}
3844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003846 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003847 * We don't really need this at runtime, but we do have tests that require it,
3848 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 */
3850 void
3851ex_disassemble(exarg_T *eap)
3852{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003853 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003854 char_u *fname;
3855 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 dfunc_T *dfunc;
3857 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003858 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 int current;
3860 int line_idx = 0;
3861 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003862 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003864 if (STRNCMP(arg, "<lambda>", 8) == 0)
3865 {
3866 arg += 8;
3867 (void)getdigits(&arg);
3868 fname = vim_strnsave(eap->arg, arg - eap->arg);
3869 }
3870 else
3871 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003872 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003873 if (fname == NULL)
3874 {
3875 semsg(_(e_invarg2), eap->arg);
3876 return;
3877 }
3878
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003879 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003880 if (ufunc == NULL)
3881 {
3882 char_u *p = untrans_function_name(fname);
3883
3884 if (p != NULL)
3885 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003886 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003887 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003888 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 if (ufunc == NULL)
3890 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003891 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 return;
3893 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003894 if (func_needs_compiling(ufunc, eap->forceit)
3895 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003896 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003897 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003899 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 return;
3901 }
3902 if (ufunc->uf_name_exp != NULL)
3903 msg((char *)ufunc->uf_name_exp);
3904 else
3905 msg((char *)ufunc->uf_name);
3906
3907 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003908#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003909 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3910 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3911 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003912#else
3913 instr = dfunc->df_instr;
3914 instr_count = dfunc->df_instr_count;
3915#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003916 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 {
3918 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003919 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920
3921 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3922 {
3923 if (current > prev_current)
3924 {
3925 msg_puts("\n\n");
3926 prev_current = current;
3927 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003928 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3929 if (line != NULL)
3930 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 }
3932
3933 switch (iptr->isn_type)
3934 {
3935 case ISN_EXEC:
3936 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3937 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003938 case ISN_EXECCONCAT:
3939 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003940 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003941 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 case ISN_ECHO:
3943 {
3944 echo_T *echo = &iptr->isn_arg.echo;
3945
3946 smsg("%4d %s %d", current,
3947 echo->echo_with_white ? "ECHO" : "ECHON",
3948 echo->echo_count);
3949 }
3950 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003951 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003952 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003953 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003954 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003955 case ISN_ECHOMSG:
3956 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003957 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003958 break;
3959 case ISN_ECHOERR:
3960 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003961 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003962 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003964 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003965 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003966 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003967 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003968 + STACK_FRAME_SIZE));
3969 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003970 smsg("%4d LOAD $%lld", current,
3971 (varnumber_T)(iptr->isn_arg.number));
3972 }
3973 break;
3974 case ISN_LOADOUTER:
3975 {
3976 if (iptr->isn_arg.number < 0)
3977 smsg("%4d LOADOUTER level %d arg[%d]", current,
3978 iptr->isn_arg.outer.outer_depth,
3979 iptr->isn_arg.outer.outer_idx
3980 + STACK_FRAME_SIZE);
3981 else
3982 smsg("%4d LOADOUTER level %d $%d", current,
3983 iptr->isn_arg.outer.outer_depth,
3984 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 break;
3987 case ISN_LOADV:
3988 smsg("%4d LOADV v:%s", current,
3989 get_vim_var_name(iptr->isn_arg.number));
3990 break;
3991 case ISN_LOADSCRIPT:
3992 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003993 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3994 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003996 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003998 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3999 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004000 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004001 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 }
4003 break;
4004 case ISN_LOADS:
4005 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004006 scriptitem_T *si = SCRIPT_ITEM(
4007 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008
4009 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004010 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 }
4012 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004013 case ISN_LOADAUTO:
4014 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4015 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 case ISN_LOADG:
4017 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4018 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004019 case ISN_LOADB:
4020 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4021 break;
4022 case ISN_LOADW:
4023 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4024 break;
4025 case ISN_LOADT:
4026 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4027 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004028 case ISN_LOADGDICT:
4029 smsg("%4d LOAD g:", current);
4030 break;
4031 case ISN_LOADBDICT:
4032 smsg("%4d LOAD b:", current);
4033 break;
4034 case ISN_LOADWDICT:
4035 smsg("%4d LOAD w:", current);
4036 break;
4037 case ISN_LOADTDICT:
4038 smsg("%4d LOAD t:", current);
4039 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 case ISN_LOADOPT:
4041 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4042 break;
4043 case ISN_LOADENV:
4044 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4045 break;
4046 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004047 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 break;
4049
4050 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004051 if (iptr->isn_arg.number < 0)
4052 smsg("%4d STORE arg[%lld]", current,
4053 iptr->isn_arg.number + STACK_FRAME_SIZE);
4054 else
4055 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4056 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004057 case ISN_STOREOUTER:
4058 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004059 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004060 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4061 iptr->isn_arg.outer.outer_depth,
4062 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004063 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004064 smsg("%4d STOREOUTER level %d $%d", current,
4065 iptr->isn_arg.outer.outer_depth,
4066 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004069 case ISN_STOREV:
4070 smsg("%4d STOREV v:%s", current,
4071 get_vim_var_name(iptr->isn_arg.number));
4072 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004073 case ISN_STOREAUTO:
4074 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4075 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004077 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4078 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004079 case ISN_STOREB:
4080 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4081 break;
4082 case ISN_STOREW:
4083 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4084 break;
4085 case ISN_STORET:
4086 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4087 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004088 case ISN_STORES:
4089 {
4090 scriptitem_T *si = SCRIPT_ITEM(
4091 iptr->isn_arg.loadstore.ls_sid);
4092
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004093 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004094 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 break;
4097 case ISN_STORESCRIPT:
4098 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004099 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4100 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004102 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004104 smsg("%4d STORESCRIPT %s-%d in %s", current,
4105 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004106 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004107 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 }
4109 break;
4110 case ISN_STOREOPT:
4111 smsg("%4d STOREOPT &%s", current,
4112 iptr->isn_arg.storeopt.so_name);
4113 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004114 case ISN_STOREENV:
4115 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4116 break;
4117 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004118 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004119 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 case ISN_STORENR:
4121 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004122 iptr->isn_arg.storenr.stnr_val,
4123 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 break;
4125
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004126 case ISN_STOREINDEX:
4127 switch (iptr->isn_arg.vartype)
4128 {
4129 case VAR_LIST:
4130 smsg("%4d STORELIST", current);
4131 break;
4132 case VAR_DICT:
4133 smsg("%4d STOREDICT", current);
4134 break;
4135 case VAR_ANY:
4136 smsg("%4d STOREINDEX", current);
4137 break;
4138 default: break;
4139 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004140 break;
4141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 // constants
4143 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004144 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004145 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 break;
4147 case ISN_PUSHBOOL:
4148 case ISN_PUSHSPEC:
4149 smsg("%4d PUSH %s", current,
4150 get_var_special_name(iptr->isn_arg.number));
4151 break;
4152 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004153#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004155#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 break;
4157 case ISN_PUSHS:
4158 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4159 break;
4160 case ISN_PUSHBLOB:
4161 {
4162 char_u *r;
4163 char_u numbuf[NUMBUFLEN];
4164 char_u *tofree;
4165
4166 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004167 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 vim_free(tofree);
4169 }
4170 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004171 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004172 {
4173 char *name = (char *)iptr->isn_arg.string;
4174
4175 smsg("%4d PUSHFUNC \"%s\"", current,
4176 name == NULL ? "[none]" : name);
4177 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004178 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004179 case ISN_PUSHCHANNEL:
4180#ifdef FEAT_JOB_CHANNEL
4181 {
4182 channel_T *channel = iptr->isn_arg.channel;
4183
4184 smsg("%4d PUSHCHANNEL %d", current,
4185 channel == NULL ? 0 : channel->ch_id);
4186 }
4187#endif
4188 break;
4189 case ISN_PUSHJOB:
4190#ifdef FEAT_JOB_CHANNEL
4191 {
4192 typval_T tv;
4193 char_u *name;
4194
4195 tv.v_type = VAR_JOB;
4196 tv.vval.v_job = iptr->isn_arg.job;
4197 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004198 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004199 }
4200#endif
4201 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 case ISN_PUSHEXC:
4203 smsg("%4d PUSH v:exception", current);
4204 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004205 case ISN_UNLET:
4206 smsg("%4d UNLET%s %s", current,
4207 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4208 iptr->isn_arg.unlet.ul_name);
4209 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004210 case ISN_UNLETENV:
4211 smsg("%4d UNLETENV%s $%s", current,
4212 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4213 iptr->isn_arg.unlet.ul_name);
4214 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004215 case ISN_UNLETINDEX:
4216 smsg("%4d UNLETINDEX", current);
4217 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004218 case ISN_UNLETRANGE:
4219 smsg("%4d UNLETRANGE", current);
4220 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004221 case ISN_LOCKCONST:
4222 smsg("%4d LOCKCONST", current);
4223 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004225 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004226 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 break;
4228 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004229 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004230 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 break;
4232
4233 // function call
4234 case ISN_BCALL:
4235 {
4236 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4237
4238 smsg("%4d BCALL %s(argc %d)", current,
4239 internal_func_name(cbfunc->cbf_idx),
4240 cbfunc->cbf_argcount);
4241 }
4242 break;
4243 case ISN_DCALL:
4244 {
4245 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4246 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4247 + cdfunc->cdf_idx;
4248
4249 smsg("%4d DCALL %s(argc %d)", current,
4250 df->df_ufunc->uf_name_exp != NULL
4251 ? df->df_ufunc->uf_name_exp
4252 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4253 }
4254 break;
4255 case ISN_UCALL:
4256 {
4257 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4258
4259 smsg("%4d UCALL %s(argc %d)", current,
4260 cufunc->cuf_name, cufunc->cuf_argcount);
4261 }
4262 break;
4263 case ISN_PCALL:
4264 {
4265 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4266
4267 smsg("%4d PCALL%s (argc %d)", current,
4268 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4269 }
4270 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004271 case ISN_PCALL_END:
4272 smsg("%4d PCALL end", current);
4273 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 case ISN_RETURN:
4275 smsg("%4d RETURN", current);
4276 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004277 case ISN_RETURN_ZERO:
4278 smsg("%4d RETURN 0", current);
4279 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 case ISN_FUNCREF:
4281 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004282 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004284 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004286 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 }
4288 break;
4289
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004290 case ISN_NEWFUNC:
4291 {
4292 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4293
4294 smsg("%4d NEWFUNC %s %s", current,
4295 newfunc->nf_lambda, newfunc->nf_global);
4296 }
4297 break;
4298
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004299 case ISN_DEF:
4300 {
4301 char_u *name = iptr->isn_arg.string;
4302
4303 smsg("%4d DEF %s", current,
4304 name == NULL ? (char_u *)"" : name);
4305 }
4306 break;
4307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 case ISN_JUMP:
4309 {
4310 char *when = "?";
4311
4312 switch (iptr->isn_arg.jump.jump_when)
4313 {
4314 case JUMP_ALWAYS:
4315 when = "JUMP";
4316 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 case JUMP_AND_KEEP_IF_TRUE:
4318 when = "JUMP_AND_KEEP_IF_TRUE";
4319 break;
4320 case JUMP_IF_FALSE:
4321 when = "JUMP_IF_FALSE";
4322 break;
4323 case JUMP_AND_KEEP_IF_FALSE:
4324 when = "JUMP_AND_KEEP_IF_FALSE";
4325 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004326 case JUMP_IF_COND_FALSE:
4327 when = "JUMP_IF_COND_FALSE";
4328 break;
4329 case JUMP_IF_COND_TRUE:
4330 when = "JUMP_IF_COND_TRUE";
4331 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004333 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 iptr->isn_arg.jump.jump_where);
4335 }
4336 break;
4337
4338 case ISN_FOR:
4339 {
4340 forloop_T *forloop = &iptr->isn_arg.forloop;
4341
4342 smsg("%4d FOR $%d -> %d", current,
4343 forloop->for_idx, forloop->for_end);
4344 }
4345 break;
4346
4347 case ISN_TRY:
4348 {
4349 try_T *try = &iptr->isn_arg.try;
4350
Bram Moolenaarc150c092021-02-13 15:02:46 +01004351 smsg("%4d TRY catch -> %d, %s -> %d", current,
4352 try->try_catch,
4353 instr[try->try_finally].isn_type == ISN_ENDTRY
4354 ? "end" : "finally",
4355 try->try_finally);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 }
4357 break;
4358 case ISN_CATCH:
4359 // TODO
4360 smsg("%4d CATCH", current);
4361 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004362 case ISN_TRYCONT:
4363 {
4364 trycont_T *trycont = &iptr->isn_arg.trycont;
4365
4366 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4367 trycont->tct_levels,
4368 trycont->tct_levels == 1 ? "" : "s",
4369 trycont->tct_where);
4370 }
4371 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 case ISN_ENDTRY:
4373 smsg("%4d ENDTRY", current);
4374 break;
4375 case ISN_THROW:
4376 smsg("%4d THROW", current);
4377 break;
4378
4379 // expression operations on number
4380 case ISN_OPNR:
4381 case ISN_OPFLOAT:
4382 case ISN_OPANY:
4383 {
4384 char *what;
4385 char *ins;
4386
4387 switch (iptr->isn_arg.op.op_type)
4388 {
4389 case EXPR_MULT: what = "*"; break;
4390 case EXPR_DIV: what = "/"; break;
4391 case EXPR_REM: what = "%"; break;
4392 case EXPR_SUB: what = "-"; break;
4393 case EXPR_ADD: what = "+"; break;
4394 default: what = "???"; break;
4395 }
4396 switch (iptr->isn_type)
4397 {
4398 case ISN_OPNR: ins = "OPNR"; break;
4399 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4400 case ISN_OPANY: ins = "OPANY"; break;
4401 default: ins = "???"; break;
4402 }
4403 smsg("%4d %s %s", current, ins, what);
4404 }
4405 break;
4406
4407 case ISN_COMPAREBOOL:
4408 case ISN_COMPARESPECIAL:
4409 case ISN_COMPARENR:
4410 case ISN_COMPAREFLOAT:
4411 case ISN_COMPARESTRING:
4412 case ISN_COMPAREBLOB:
4413 case ISN_COMPARELIST:
4414 case ISN_COMPAREDICT:
4415 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 case ISN_COMPAREANY:
4417 {
4418 char *p;
4419 char buf[10];
4420 char *type;
4421
4422 switch (iptr->isn_arg.op.op_type)
4423 {
4424 case EXPR_EQUAL: p = "=="; break;
4425 case EXPR_NEQUAL: p = "!="; break;
4426 case EXPR_GREATER: p = ">"; break;
4427 case EXPR_GEQUAL: p = ">="; break;
4428 case EXPR_SMALLER: p = "<"; break;
4429 case EXPR_SEQUAL: p = "<="; break;
4430 case EXPR_MATCH: p = "=~"; break;
4431 case EXPR_IS: p = "is"; break;
4432 case EXPR_ISNOT: p = "isnot"; break;
4433 case EXPR_NOMATCH: p = "!~"; break;
4434 default: p = "???"; break;
4435 }
4436 STRCPY(buf, p);
4437 if (iptr->isn_arg.op.op_ic == TRUE)
4438 strcat(buf, "?");
4439 switch(iptr->isn_type)
4440 {
4441 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4442 case ISN_COMPARESPECIAL:
4443 type = "COMPARESPECIAL"; break;
4444 case ISN_COMPARENR: type = "COMPARENR"; break;
4445 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4446 case ISN_COMPARESTRING:
4447 type = "COMPARESTRING"; break;
4448 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4449 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4450 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4451 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4453 default: type = "???"; break;
4454 }
4455
4456 smsg("%4d %s %s", current, type, buf);
4457 }
4458 break;
4459
4460 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4461 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4462
4463 // expression operations
4464 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004465 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004466 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004467 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004468 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004469 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004470 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004471 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4472 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004473 case ISN_SLICE: smsg("%4d SLICE %lld",
4474 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004475 case ISN_GETITEM: smsg("%4d ITEM %lld",
4476 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004477 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4478 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 iptr->isn_arg.string); break;
4480 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4481
4482 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004483 case ISN_CHECKTYPE:
4484 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004485 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004486 char *tofree;
4487
Bram Moolenaare32e5162021-01-21 20:21:29 +01004488 if (ct->ct_arg_idx == 0)
4489 smsg("%4d CHECKTYPE %s stack[%d]", current,
4490 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004491 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004492 else
4493 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4494 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004495 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004496 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004497 vim_free(tofree);
4498 break;
4499 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004500 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4501 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4502 iptr->isn_arg.checklen.cl_min_len);
4503 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004504 case ISN_SETTYPE:
4505 {
4506 char *tofree;
4507
4508 smsg("%4d SETTYPE %s", current,
4509 type_name(iptr->isn_arg.type.ct_type, &tofree));
4510 vim_free(tofree);
4511 break;
4512 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004513 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 case ISN_2BOOL: if (iptr->isn_arg.number)
4515 smsg("%4d INVERT (!val)", current);
4516 else
4517 smsg("%4d 2BOOL (!!val)", current);
4518 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004519 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004520 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004521 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004522 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004523 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004524 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004525 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4526 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004527 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004528 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4529 smsg("%4d PUT %c above range",
4530 current, iptr->isn_arg.put.put_regname);
4531 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4532 smsg("%4d PUT %c range",
4533 current, iptr->isn_arg.put.put_regname);
4534 else
4535 smsg("%4d PUT %c %ld", current,
4536 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004537 (long)iptr->isn_arg.put.put_lnum);
4538 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539
Bram Moolenaar02194d22020-10-24 23:08:38 +02004540 // TODO: summarize modifiers
4541 case ISN_CMDMOD:
4542 {
4543 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004544 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004545 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4546
4547 buf = alloc(len + 1);
4548 if (buf != NULL)
4549 {
4550 (void)produce_cmdmods(
4551 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4552 smsg("%4d CMDMOD %s", current, buf);
4553 vim_free(buf);
4554 }
4555 break;
4556 }
4557 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004558
Bram Moolenaarb2049902021-01-24 12:53:53 +01004559 case ISN_PROF_START:
4560 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4561 break;
4562
4563 case ISN_PROF_END:
4564 smsg("%4d PROFILE END", current);
4565 break;
4566
Bram Moolenaar792f7862020-11-23 08:31:18 +01004567 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4568 iptr->isn_arg.unpack.unp_count,
4569 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4570 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004571 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4572 iptr->isn_arg.shuffle.shfl_item,
4573 iptr->isn_arg.shuffle.shfl_up);
4574 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 case ISN_DROP: smsg("%4d DROP", current); break;
4576 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004577
4578 out_flush(); // output one line at a time
4579 ui_breakcheck();
4580 if (got_int)
4581 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583}
4584
4585/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004586 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 * list, etc. Mostly like what JavaScript does, except that empty list and
4588 * empty dictionary are FALSE.
4589 */
4590 int
4591tv2bool(typval_T *tv)
4592{
4593 switch (tv->v_type)
4594 {
4595 case VAR_NUMBER:
4596 return tv->vval.v_number != 0;
4597 case VAR_FLOAT:
4598#ifdef FEAT_FLOAT
4599 return tv->vval.v_float != 0.0;
4600#else
4601 break;
4602#endif
4603 case VAR_PARTIAL:
4604 return tv->vval.v_partial != NULL;
4605 case VAR_FUNC:
4606 case VAR_STRING:
4607 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4608 case VAR_LIST:
4609 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4610 case VAR_DICT:
4611 return tv->vval.v_dict != NULL
4612 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4613 case VAR_BOOL:
4614 case VAR_SPECIAL:
4615 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4616 case VAR_JOB:
4617#ifdef FEAT_JOB_CHANNEL
4618 return tv->vval.v_job != NULL;
4619#else
4620 break;
4621#endif
4622 case VAR_CHANNEL:
4623#ifdef FEAT_JOB_CHANNEL
4624 return tv->vval.v_channel != NULL;
4625#else
4626 break;
4627#endif
4628 case VAR_BLOB:
4629 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4630 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004631 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 case VAR_VOID:
4633 break;
4634 }
4635 return FALSE;
4636}
4637
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004638 void
4639emsg_using_string_as(typval_T *tv, int as_number)
4640{
4641 semsg(_(as_number ? e_using_string_as_number_str
4642 : e_using_string_as_bool_str),
4643 tv->vval.v_string == NULL
4644 ? (char_u *)"" : tv->vval.v_string);
4645}
4646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647/*
4648 * If "tv" is a string give an error and return FAIL.
4649 */
4650 int
4651check_not_string(typval_T *tv)
4652{
4653 if (tv->v_type == VAR_STRING)
4654 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004655 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 clear_tv(tv);
4657 return FAIL;
4658 }
4659 return OK;
4660}
4661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662
4663#endif // FEAT_EVAL