blob: 60b58f98a3674f35413a4bd3bb263dc563db058a [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 Moolenaar7e82c5f2021-02-21 21:32:45 +010029 int tcd_catch_idx; // instruction of the first :catch or :finally
30 int tcd_finally_idx; // instruction of the :finally block or zero
31 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010032 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010033 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_return; // when TRUE return from end of :finally
35} trycmd_T;
36
37
38// A stack is used to store:
39// - arguments passed to a :def function
40// - info about the calling function, to use when returning
41// - local variables
42// - temporary values
43//
44// In detail (FP == Frame Pointer):
45// arg1 first argument from caller (if present)
46// arg2 second argument from caller (if present)
47// extra_arg1 any missing optional argument default value
48// FP -> cur_func calling function
49// current previous instruction pointer
50// frame_ptr previous Frame Pointer
51// var1 space for local variable
52// var2 space for local variable
53// .... fixed space for max. number of local variables
54// temp temporary values
55// .... flexible space for temporary values (can grow big)
56
57/*
58 * Execution context.
59 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010060struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010061 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020062 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063
Bram Moolenaar0186e582021-01-10 18:33:11 +010064 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 garray_T ec_trystack; // stack of trycmd_T values
67 int ec_in_catch; // when TRUE in catch or finally block
68
69 int ec_dfunc_idx; // current function index
70 isn_T *ec_instr; // array with instructions
71 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020072
73 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010074};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
Bram Moolenaar12d26532021-02-19 19:13:21 +010076#ifdef FEAT_PROFILE
77// stack of profinfo_T used when profiling.
78static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
79#endif
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020082#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 +010083
Bram Moolenaar418f1df2020-08-12 21:34:49 +020084 void
85to_string_error(vartype_T vartype)
86{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020087 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020088}
89
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092 */
93 static int
94ufunc_argcount(ufunc_T *ufunc)
95{
96 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
97}
98
99/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200100 * Create a new list from "count" items at the bottom of the stack.
101 * When "count" is zero an empty list is added to the stack.
102 */
103 static int
104exe_newlist(int count, ectx_T *ectx)
105{
106 list_T *list = list_alloc_with_items(count);
107 int idx;
108 typval_T *tv;
109
110 if (list == NULL)
111 return FAIL;
112 for (idx = 0; idx < count; ++idx)
113 list_set_item(list, idx, STACK_TV_BOT(idx - count));
114
115 if (count > 0)
116 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200117 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200118 return FAIL;
119 else
120 ++ectx->ec_stack.ga_len;
121 tv = STACK_TV_BOT(-1);
122 tv->v_type = VAR_LIST;
123 tv->vval.v_list = list;
124 ++list->lv_refcount;
125 return OK;
126}
127
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100128// Data local to a function.
129// On a function call, if not empty, is saved on the stack and restored when
130// returning.
131typedef struct {
132 int floc_restore_cmdmod;
133 cmdmod_T floc_save_cmdmod;
134 int floc_restore_cmdmod_stacklen;
135} funclocal_T;
136
Bram Moolenaarfe270812020-04-11 22:31:27 +0200137/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100139 * This adds a stack frame and sets the instruction pointer to the start of the
140 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100141 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 *
143 * Stack has:
144 * - current arguments (already there)
145 * - omitted optional argument (default values) added here
146 * - stack frame:
147 * - pointer to calling function
148 * - Index of next instruction in calling function
149 * - previous frame pointer
150 * - reserved space for local variables
151 */
152 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100153call_dfunc(
154 int cdf_idx,
155 partial_T *pt,
156 int argcount_arg,
157 funclocal_T *funclocal,
158 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100160 int argcount = argcount_arg;
161 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
162 ufunc_T *ufunc = dfunc->df_ufunc;
163 int arg_to_add;
164 int vararg_count = 0;
165 int varcount;
166 int idx;
167 estack_T *entry;
168 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169
170 if (dfunc->df_deleted)
171 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100172 // don't use ufunc->uf_name, it may have been freed
173 emsg_funcname(e_func_deleted,
174 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 return FAIL;
176 }
177
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100178#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100179 if (do_profiling == PROF_YES)
180 {
181 if (ga_grow(&profile_info_ga, 1) == OK)
182 {
183 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
184 + profile_info_ga.ga_len;
185 ++profile_info_ga.ga_len;
186 CLEAR_POINTER(info);
187 profile_may_start_func(info, ufunc,
188 (((dfunc_T *)def_functions.ga_data)
189 + ectx->ec_dfunc_idx)->df_ufunc);
190 }
191
192 // Profiling might be enabled/disabled along the way. This should not
193 // fail, since the function was compiled before and toggling profiling
194 // doesn't change any errors.
195 if (func_needs_compiling(ufunc, PROFILING(ufunc))
196 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100197 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100198 return FAIL;
199 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100200#endif
201
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200202 if (ufunc->uf_va_name != NULL)
203 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200204 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200205 // Stack at time of call with 2 varargs:
206 // normal_arg
207 // optional_arg
208 // vararg_1
209 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200210 // After creating the list:
211 // normal_arg
212 // optional_arg
213 // vararg-list
214 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200215 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200216 // After creating the list
217 // normal_arg
218 // (space for optional_arg)
219 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200220 vararg_count = argcount - ufunc->uf_args.ga_len;
221 if (vararg_count < 0)
222 vararg_count = 0;
223 else
224 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200225 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200226 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200227
228 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200229 }
230
Bram Moolenaarfe270812020-04-11 22:31:27 +0200231 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200232 if (arg_to_add < 0)
233 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200234 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200235 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200236 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200237 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200238 return FAIL;
239 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200240
241 // Reserve space for:
242 // - missing arguments
243 // - stack frame
244 // - local variables
245 // - if needed: a counter for number of closures created in
246 // ectx->ec_funcrefs.
247 varcount = dfunc->df_varcount + dfunc->df_has_closure;
248 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
249 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250 return FAIL;
251
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100252 // If depth of calling is getting too high, don't execute the function.
253 if (funcdepth_increment() == FAIL)
254 return FAIL;
255
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100256 // Only make a copy of funclocal if it contains something to restore.
257 if (funclocal->floc_restore_cmdmod)
258 {
259 floc = ALLOC_ONE(funclocal_T);
260 if (floc == NULL)
261 return FAIL;
262 *floc = *funclocal;
263 funclocal->floc_restore_cmdmod = FALSE;
264 }
265
Bram Moolenaarfe270812020-04-11 22:31:27 +0200266 // Move the vararg-list to below the missing optional arguments.
267 if (vararg_count > 0 && arg_to_add > 0)
268 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100269
270 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200271 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200273 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
275 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100276 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
277 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100278 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100279 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100280 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200281 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282
283 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200284 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200286 if (dfunc->df_has_closure)
287 {
288 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
289
290 tv->v_type = VAR_NUMBER;
291 tv->vval.v_number = 0;
292 }
293 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100295 if (pt != NULL || ufunc->uf_partial != NULL
296 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100297 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100298 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
299
300 if (outer == NULL)
301 return FAIL;
302 if (pt != NULL)
303 {
304 *outer = pt->pt_outer;
305 outer->out_up_is_copy = TRUE;
306 }
307 else if (ufunc->uf_partial != NULL)
308 {
309 *outer = ufunc->uf_partial->pt_outer;
310 outer->out_up_is_copy = TRUE;
311 }
312 else
313 {
314 outer->out_stack = &ectx->ec_stack;
315 outer->out_frame_idx = ectx->ec_frame_idx;
316 outer->out_up = ectx->ec_outer;
317 }
318 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100319 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100320 else
321 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100322
Bram Moolenaarc970e422021-03-17 15:03:04 +0100323 ++ufunc->uf_calls;
324
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.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200332 // Save the current context so it can be restored on return.
333 entry->es_save_sctx = current_sctx;
334 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200335 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100336
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200337 // Start execution at the first instruction.
338 ectx->ec_iidx = 0;
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 Moolenaar2fecb532021-03-24 22:00:56 +0100530func_return(funclocal_T *funclocal, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531{
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 Moolenaar2fecb532021-03-24 22:00:56 +0100543 funclocal_T *floc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544
Bram Moolenaar12d26532021-02-19 19:13:21 +0100545#ifdef FEAT_PROFILE
546 if (do_profiling == PROF_YES)
547 {
548 ufunc_T *caller = prev_dfunc->df_ufunc;
549
550 if (dfunc->df_ufunc->uf_profiling
551 || (caller != NULL && caller->uf_profiling))
552 {
553 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
554 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
555 --profile_info_ga.ga_len;
556 }
557 }
558#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100559 // TODO: when is it safe to delete the function when it is no longer used?
560 --dfunc->df_ufunc->uf_calls;
561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200563 entry = estack_pop();
564 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200565 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200567 if (handle_closure_in_use(ectx, TRUE) == FAIL)
568 return FAIL;
569
570 // Clear the arguments.
571 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
572 clear_tv(STACK_TV(idx));
573
574 // Clear local variables and temp values, but not the return value.
575 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100576 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100578
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100579 // The return value should be on top of the stack. However, when aborting
580 // it may not be there and ec_frame_idx is the top of the stack.
581 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100582 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100583 ret_idx = 0;
584
Bram Moolenaar0186e582021-01-10 18:33:11 +0100585 vim_free(ectx->ec_outer);
586
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100587 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100588 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100589 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
590 + STACK_FRAME_IIDX_OFF)->vval.v_number;
591 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
592 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100593 floc = (void *)STACK_TV(ectx->ec_frame_idx
594 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200595 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100596 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
597 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100598 ectx->ec_instr = INSTRUCTIONS(prev_dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100599
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100600 if (floc == NULL)
601 funclocal->floc_restore_cmdmod = FALSE;
602 else
603 {
604 *funclocal = *floc;
605 vim_free(floc);
606 }
607
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100608 if (ret_idx > 0)
609 {
610 // Reset the stack to the position before the call, with a spot for the
611 // return value, moved there from above the frame.
612 ectx->ec_stack.ga_len = top + 1;
613 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
614 }
615 else
616 // Reset the stack to the position before the call.
617 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200618
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100619 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200620 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621}
622
623#undef STACK_TV
624
625/*
626 * Prepare arguments and rettv for calling a builtin or user function.
627 */
628 static int
629call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
630{
631 int idx;
632 typval_T *tv;
633
634 // Move arguments from bottom of the stack to argvars[] and add terminator.
635 for (idx = 0; idx < argcount; ++idx)
636 argvars[idx] = *STACK_TV_BOT(idx - argcount);
637 argvars[argcount].v_type = VAR_UNKNOWN;
638
639 // Result replaces the arguments on the stack.
640 if (argcount > 0)
641 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200642 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 return FAIL;
644 else
645 ++ectx->ec_stack.ga_len;
646
647 // Default return value is zero.
648 tv = STACK_TV_BOT(-1);
649 tv->v_type = VAR_NUMBER;
650 tv->vval.v_number = 0;
651
652 return OK;
653}
654
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200655// Ugly global to avoid passing the execution context around through many
656// layers.
657static ectx_T *current_ectx = NULL;
658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659/*
660 * Call a builtin function by index.
661 */
662 static int
663call_bfunc(int func_idx, int argcount, ectx_T *ectx)
664{
665 typval_T argvars[MAX_FUNC_ARGS];
666 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100667 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200668 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669
670 if (call_prepare(argcount, argvars, ectx) == FAIL)
671 return FAIL;
672
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200673 // Call the builtin function. Set "current_ectx" so that when it
674 // recursively invokes call_def_function() a closure context can be set.
675 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200677 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678
679 // Clear the arguments.
680 for (idx = 0; idx < argcount; ++idx)
681 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200682
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100683 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200684 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 return OK;
686}
687
688/*
689 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100690 * If the function is compiled this will add a stack frame and set the
691 * instruction pointer at the start of the function.
692 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100693 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100694 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 */
696 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100697call_ufunc(
698 ufunc_T *ufunc,
699 partial_T *pt,
700 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100701 funclocal_T *funclocal,
Bram Moolenaar0186e582021-01-10 18:33:11 +0100702 ectx_T *ectx,
703 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704{
705 typval_T argvars[MAX_FUNC_ARGS];
706 funcexe_T funcexe;
707 int error;
708 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100709 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100710#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100711 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100712#else
713# define profiling FALSE
714#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715
Bram Moolenaarb2049902021-01-24 12:53:53 +0100716 if (func_needs_compiling(ufunc, profiling)
717 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200718 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200719 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100720 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100721 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100722 if (error != FCERR_UNKNOWN)
723 {
724 if (error == FCERR_TOOMANY)
725 semsg(_(e_toomanyarg), ufunc->uf_name);
726 else
727 semsg(_(e_toofewarg), ufunc->uf_name);
728 return FAIL;
729 }
730
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100731 // The function has been compiled, can call it quickly. For a function
732 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100733 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100734 if (iptr != NULL)
735 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100736 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100737 iptr->isn_type = ISN_DCALL;
738 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
739 iptr->isn_arg.dfunc.cdf_argcount = argcount;
740 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100741 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, funclocal, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
744 if (call_prepare(argcount, argvars, ectx) == FAIL)
745 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200746 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 funcexe.evaluate = TRUE;
748
749 // Call the user function. Result goes in last position on the stack.
750 // TODO: add selfdict if there is one
751 error = call_user_func_check(ufunc, argcount, argvars,
752 STACK_TV_BOT(-1), &funcexe, NULL);
753
754 // Clear the arguments.
755 for (idx = 0; idx < argcount; ++idx)
756 clear_tv(&argvars[idx]);
757
758 if (error != FCERR_NONE)
759 {
760 user_func_error(error, ufunc->uf_name);
761 return FAIL;
762 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100763 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200764 // Error other than from calling the function itself.
765 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 return OK;
767}
768
769/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100770 * If command modifiers were applied restore them.
771 */
772 static void
773may_restore_cmdmod(funclocal_T *funclocal)
774{
775 if (funclocal->floc_restore_cmdmod)
776 {
777 cmdmod.cmod_filter_regmatch.regprog = NULL;
778 undo_cmdmod(&cmdmod);
779 cmdmod = funclocal->floc_save_cmdmod;
780 funclocal->floc_restore_cmdmod = FALSE;
781 }
782}
783
784/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200785 * Return TRUE if an error was given or CTRL-C was pressed.
786 */
787 static int
788vim9_aborting(int prev_called_emsg)
789{
790 return called_emsg > prev_called_emsg || got_int || did_throw;
791}
792
793/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 * Execute a function by "name".
795 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100796 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100797 * Returns FAIL if not found without an error message.
798 */
799 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100800call_by_name(
801 char_u *name,
802 int argcount,
803 funclocal_T *funclocal,
804 ectx_T *ectx,
805 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806{
807 ufunc_T *ufunc;
808
809 if (builtin_function(name, -1))
810 {
811 int func_idx = find_internal_func(name);
812
813 if (func_idx < 0)
814 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200815 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816 return FAIL;
817 return call_bfunc(func_idx, argcount, ectx);
818 }
819
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200820 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200821
822 if (ufunc == NULL)
823 {
824 int called_emsg_before = called_emsg;
825
826 if (script_autoload(name, TRUE))
827 // loaded a package, search for the function again
828 ufunc = find_func(name, FALSE, NULL);
829 if (vim9_aborting(called_emsg_before))
830 return FAIL; // bail out if loading the script caused an error
831 }
832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100834 {
835 if (ufunc->uf_arg_types != NULL)
836 {
837 int i;
838 typval_T *argv = STACK_TV_BOT(0) - argcount;
839
840 // The function can change at runtime, check that the argument
841 // types are correct.
842 for (i = 0; i < argcount; ++i)
843 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100844 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100845
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100846 if (i < ufunc->uf_args.ga_len)
847 type = ufunc->uf_arg_types[i];
848 else if (ufunc->uf_va_type != NULL)
849 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100850 if (type != NULL && check_typval_arg_type(type,
851 &argv[i], i + 1) == FAIL)
852 return FAIL;
853 }
854 }
855
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100856 return call_ufunc(ufunc, NULL, argcount, funclocal, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858
859 return FAIL;
860}
861
862 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100863call_partial(
864 typval_T *tv,
865 int argcount_arg,
866 funclocal_T *funclocal,
867 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200869 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200870 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100872 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873
874 if (tv->v_type == VAR_PARTIAL)
875 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200876 partial_T *pt = tv->vval.v_partial;
877 int i;
878
879 if (pt->pt_argc > 0)
880 {
881 // Make space for arguments from the partial, shift the "argcount"
882 // arguments up.
883 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
884 return FAIL;
885 for (i = 1; i <= argcount; ++i)
886 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
887 ectx->ec_stack.ga_len += pt->pt_argc;
888 argcount += pt->pt_argc;
889
890 // copy the arguments from the partial onto the stack
891 for (i = 0; i < pt->pt_argc; ++i)
892 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894
895 if (pt->pt_func != NULL)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100896 return call_ufunc(pt->pt_func, pt, argcount, funclocal, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 name = pt->pt_name;
899 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200900 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200902 if (name != NULL)
903 {
904 char_u fname_buf[FLEN_FIXED + 1];
905 char_u *tofree = NULL;
906 int error = FCERR_NONE;
907 char_u *fname;
908
909 // May need to translate <SNR>123_ to K_SNR.
910 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
911 if (error != FCERR_NONE)
912 res = FAIL;
913 else
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100914 res = call_by_name(fname, argcount, funclocal, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200915 vim_free(tofree);
916 }
917
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100918 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919 {
920 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200921 semsg(_(e_unknownfunc),
922 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 return FAIL;
924 }
925 return OK;
926}
927
928/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200929 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
930 * TRUE.
931 */
932 static int
933error_if_locked(int lock, char *error)
934{
935 if (lock & (VAR_LOCKED | VAR_FIXED))
936 {
937 emsg(_(error));
938 return TRUE;
939 }
940 return FALSE;
941}
942
943/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100944 * Give an error if "tv" is not a number and return FAIL.
945 */
946 static int
947check_for_number(typval_T *tv)
948{
949 if (tv->v_type != VAR_NUMBER)
950 {
951 semsg(_(e_expected_str_but_got_str),
952 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
953 return FAIL;
954 }
955 return OK;
956}
957
958/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100959 * Store "tv" in variable "name".
960 * This is for s: and g: variables.
961 */
962 static void
963store_var(char_u *name, typval_T *tv)
964{
965 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +0200966 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100967
Bram Moolenaard877a572021-04-01 19:42:48 +0200968 if (tv->v_lock)
969 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100970 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +0200971 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100972 restore_funccal();
973}
974
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100975/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100976 * Convert "tv" to a string.
977 * Return FAIL if not allowed.
978 */
979 static int
980do_2string(typval_T *tv, int is_2string_any)
981{
982 if (tv->v_type != VAR_STRING)
983 {
984 char_u *str;
985
986 if (is_2string_any)
987 {
988 switch (tv->v_type)
989 {
990 case VAR_SPECIAL:
991 case VAR_BOOL:
992 case VAR_NUMBER:
993 case VAR_FLOAT:
994 case VAR_BLOB: break;
995 default: to_string_error(tv->v_type);
996 return FAIL;
997 }
998 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100999 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001000 clear_tv(tv);
1001 tv->v_type = VAR_STRING;
1002 tv->vval.v_string = str;
1003 }
1004 return OK;
1005}
1006
1007/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001008 * When the value of "sv" is a null list of dict, allocate it.
1009 */
1010 static void
1011allocate_if_null(typval_T *tv)
1012{
1013 switch (tv->v_type)
1014 {
1015 case VAR_LIST:
1016 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001017 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001018 break;
1019 case VAR_DICT:
1020 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001021 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001022 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001023 case VAR_BLOB:
1024 if (tv->vval.v_blob == NULL)
1025 (void)rettv_blob_alloc(tv);
1026 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001027 default:
1028 break;
1029 }
1030}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001031
Bram Moolenaare7525c52021-01-09 13:20:37 +01001032/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001033 * Return the character "str[index]" where "index" is the character index,
1034 * including composing characters.
1035 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001036 */
1037 char_u *
1038char_from_string(char_u *str, varnumber_T index)
1039{
1040 size_t nbyte = 0;
1041 varnumber_T nchar = index;
1042 size_t slen;
1043
1044 if (str == NULL)
1045 return NULL;
1046 slen = STRLEN(str);
1047
Bram Moolenaarff871402021-03-26 13:34:05 +01001048 // Do the same as for a list: a negative index counts from the end.
1049 // Optimization to check the first byte to be below 0x80 (and no composing
1050 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001051 if (index < 0)
1052 {
1053 int clen = 0;
1054
1055 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001056 {
1057 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1058 ++nbyte;
1059 else if (enc_utf8)
1060 nbyte += utfc_ptr2len(str + nbyte);
1061 else
1062 nbyte += mb_ptr2len(str + nbyte);
1063 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001064 nchar = clen + index;
1065 if (nchar < 0)
1066 // unlike list: index out of range results in empty string
1067 return NULL;
1068 }
1069
1070 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001071 {
1072 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1073 ++nbyte;
1074 else if (enc_utf8)
1075 nbyte += utfc_ptr2len(str + nbyte);
1076 else
1077 nbyte += mb_ptr2len(str + nbyte);
1078 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001079 if (nbyte >= slen)
1080 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001081 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001082}
1083
1084/*
1085 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001086 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001087 * If going over the end return "str_len".
1088 * If "idx" is negative count from the end, -1 is the last character.
1089 * When going over the start return -1.
1090 */
1091 static long
1092char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1093{
1094 varnumber_T nchar = idx;
1095 size_t nbyte = 0;
1096
1097 if (nchar >= 0)
1098 {
1099 while (nchar > 0 && nbyte < str_len)
1100 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001101 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001102 --nchar;
1103 }
1104 }
1105 else
1106 {
1107 nbyte = str_len;
1108 while (nchar < 0 && nbyte > 0)
1109 {
1110 --nbyte;
1111 nbyte -= mb_head_off(str, str + nbyte);
1112 ++nchar;
1113 }
1114 if (nchar < 0)
1115 return -1;
1116 }
1117 return (long)nbyte;
1118}
1119
1120/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001121 * Return the slice "str[first : last]" using character indexes. Composing
1122 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001123 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001124 * Return NULL when the result is empty.
1125 */
1126 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001127string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001128{
1129 long start_byte, end_byte;
1130 size_t slen;
1131
1132 if (str == NULL)
1133 return NULL;
1134 slen = STRLEN(str);
1135 start_byte = char_idx2byte(str, slen, first);
1136 if (start_byte < 0)
1137 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001138 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001139 end_byte = (long)slen;
1140 else
1141 {
1142 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001143 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001144 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001145 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001146 }
1147
1148 if (start_byte >= (long)slen || end_byte <= start_byte)
1149 return NULL;
1150 return vim_strnsave(str + start_byte, end_byte - start_byte);
1151}
1152
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001153 static svar_T *
1154get_script_svar(scriptref_T *sref, ectx_T *ectx)
1155{
1156 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1157 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1158 + ectx->ec_dfunc_idx;
1159 svar_T *sv;
1160
1161 if (sref->sref_seq != si->sn_script_seq)
1162 {
1163 // The script was reloaded after the function was
1164 // compiled, the script_idx may not be valid.
1165 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1166 dfunc->df_ufunc->uf_name_exp);
1167 return NULL;
1168 }
1169 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1170 if (!equal_type(sv->sv_type, sref->sref_type))
1171 {
1172 emsg(_(e_script_variable_type_changed));
1173 return NULL;
1174 }
1175 return sv;
1176}
1177
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001178/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 * Execute a function by "name".
1180 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001181 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 */
1183 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001184call_eval_func(
1185 char_u *name,
1186 int argcount,
1187 funclocal_T *funclocal,
1188 ectx_T *ectx,
1189 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190{
Bram Moolenaared677f52020-08-12 16:38:10 +02001191 int called_emsg_before = called_emsg;
1192 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001194 res = call_by_name(name, argcount, funclocal, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001195 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001197 dictitem_T *v;
1198
1199 v = find_var(name, NULL, FALSE);
1200 if (v == NULL)
1201 {
1202 semsg(_(e_unknownfunc), name);
1203 return FAIL;
1204 }
1205 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1206 {
1207 semsg(_(e_unknownfunc), name);
1208 return FAIL;
1209 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001210 return call_partial(&v->di_tv, argcount, funclocal, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001212 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213}
1214
1215/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001216 * When a function reference is used, fill a partial with the information
1217 * needed, especially when it is used as a closure.
1218 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001219 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001220fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1221{
1222 pt->pt_func = ufunc;
1223 pt->pt_refcount = 1;
1224
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001225 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001226 {
1227 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1228 + ectx->ec_dfunc_idx;
1229
1230 // The closure needs to find arguments and local
1231 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001232 pt->pt_outer.out_stack = &ectx->ec_stack;
1233 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1234 pt->pt_outer.out_up = ectx->ec_outer;
1235 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001236
1237 // If this function returns and the closure is still
1238 // being used, we need to make a copy of the context
1239 // (arguments and local variables). Store a reference
1240 // to the partial so we can handle that.
1241 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1242 {
1243 vim_free(pt);
1244 return FAIL;
1245 }
1246 // Extra variable keeps the count of closures created
1247 // in the current function call.
1248 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1249 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1250
1251 ((partial_T **)ectx->ec_funcrefs.ga_data)
1252 [ectx->ec_funcrefs.ga_len] = pt;
1253 ++pt->pt_refcount;
1254 ++ectx->ec_funcrefs.ga_len;
1255 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001256 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001257 return OK;
1258}
1259
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001260
Bram Moolenaarf112f302020-12-20 17:47:52 +01001261/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 * Call a "def" function from old Vim script.
1263 * Return OK or FAIL.
1264 */
1265 int
1266call_def_function(
1267 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001268 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001270 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 typval_T *rettv) // return value
1272{
1273 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001274 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001275 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 typval_T *tv;
1277 int idx;
1278 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001279 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001280 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001281 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001282 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001283 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001284 msglist_T **saved_msg_list = NULL;
1285 msglist_T *private_msg_list = NULL;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001286 funclocal_T funclocal;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001287 int save_emsg_silent_def = emsg_silent_def;
1288 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001289 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001290 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001291 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292
1293// Get pointer to item in the stack.
1294#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1295
1296// Get pointer to item at the bottom of the stack, -1 is the bottom.
1297#undef STACK_TV_BOT
1298#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1299
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001300// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001301#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 +01001302
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001303 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001304 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001305 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1306 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001307 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001308 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001309 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001310 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001311 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001312 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001313 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001314
Bram Moolenaar09689a02020-05-09 22:50:08 +02001315 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001316 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001317 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1318 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001319 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001320 {
1321 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001322 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001323 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001324 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001326 // If depth of calling is getting too high, don't execute the function.
1327 orig_funcdepth = funcdepth_get();
1328 if (funcdepth_increment() == FAIL)
1329 return FAIL;
1330
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001331 CLEAR_FIELD(funclocal);
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001332 CLEAR_FIELD(ectx);
1333 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1334 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1335 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001336 {
1337 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001338 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001339 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001341 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02001343 idx = argc - ufunc->uf_args.ga_len;
1344 if (idx > 0 && ufunc->uf_va_name == NULL)
1345 {
1346 if (idx == 1)
1347 emsg(_(e_one_argument_too_many));
1348 else
1349 semsg(_(e_nr_arguments_too_many), idx);
Bram Moolenaarc4297692021-04-10 20:46:48 +02001350 goto failed_early;
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02001351 }
1352
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001353 // Put arguments on the stack, but no more than what the function expects.
1354 // A lambda can be called with more arguments than it uses.
1355 for (idx = 0; idx < argc
1356 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1357 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 {
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001359 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
1360 && argv[idx].v_type == VAR_SPECIAL
1361 && argv[idx].vval.v_number == VVAL_NONE)
1362 {
1363 // Use the default value.
1364 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1365 }
1366 else
1367 {
1368 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
1369 && check_typval_arg_type(
1370 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
1371 goto failed_early;
1372 copy_tv(&argv[idx], STACK_TV_BOT(0));
1373 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 ++ectx.ec_stack.ga_len;
1375 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001376
1377 // Turn varargs into a list. Empty list if no args.
1378 if (ufunc->uf_va_name != NULL)
1379 {
1380 int vararg_count = argc - ufunc->uf_args.ga_len;
1381
1382 if (vararg_count < 0)
1383 vararg_count = 0;
1384 else
1385 argc -= vararg_count;
1386 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001387 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001388
1389 // Check the type of the list items.
1390 tv = STACK_TV_BOT(-1);
1391 if (ufunc->uf_va_type != NULL
Bram Moolenaar2a389082021-04-09 20:24:31 +02001392 && ufunc->uf_va_type != &t_list_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001393 && ufunc->uf_va_type->tt_member != &t_any
1394 && tv->vval.v_list != NULL)
1395 {
1396 type_T *expected = ufunc->uf_va_type->tt_member;
1397 listitem_T *li = tv->vval.v_list->lv_first;
1398
1399 for (idx = 0; idx < vararg_count; ++idx)
1400 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001401 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001402 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001403 goto failed_early;
1404 li = li->li_next;
1405 }
1406 }
1407
Bram Moolenaar23e03252020-04-12 22:22:31 +02001408 if (defcount > 0)
1409 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001410 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001411 --ectx.ec_stack.ga_len;
1412 }
1413
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001414 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001415 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001416 if (defcount > 0)
1417 for (idx = 0; idx < defcount; ++idx)
1418 {
1419 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1420 ++ectx.ec_stack.ga_len;
1421 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001422 if (ufunc->uf_va_name != NULL)
Bram Moolenaarc970e422021-03-17 15:03:04 +01001423 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424
1425 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001426 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1427 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001429 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001430 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1431 + ufunc->uf_dfunc_idx;
1432 ufunc_T *base_ufunc = dfunc->df_ufunc;
1433
1434 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1435 // by copy_func().
1436 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001437 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001438 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1439 if (ectx.ec_outer == NULL)
1440 goto failed_early;
1441 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001442 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001443 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1444 {
1445 if (current_ectx->ec_outer != NULL)
1446 *ectx.ec_outer = *current_ectx->ec_outer;
1447 }
1448 else
1449 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001450 }
1451 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001452 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1453 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001454 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001455 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 // dummy frame entries
1458 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1459 {
1460 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1461 ++ectx.ec_stack.ga_len;
1462 }
1463
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001464 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001465 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001466 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1467 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001469 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001470 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001471 ectx.ec_stack.ga_len += dfunc->df_varcount;
1472 if (dfunc->df_has_closure)
1473 {
1474 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1475 STACK_TV_VAR(idx)->vval.v_number = 0;
1476 ++ectx.ec_stack.ga_len;
1477 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001478
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001479 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001480 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001481
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001482 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001483 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001484 estack_push_ufunc(ufunc, 1);
1485 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001486 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1487
Bram Moolenaar352134b2020-10-17 22:04:08 +02001488 // Use a specific location for storing error messages to be converted to an
1489 // exception.
1490 saved_msg_list = msg_list;
1491 msg_list = &private_msg_list;
1492
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001493 // Do turn errors into exceptions.
1494 suppress_errthrow = FALSE;
1495
Bram Moolenaarc970e422021-03-17 15:03:04 +01001496 // Do not delete the function while executing it.
1497 ++ufunc->uf_calls;
1498
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001499 // When ":silent!" was used before calling then we still abort the
1500 // function. If ":silent!" is used in the function then we don't.
1501 emsg_silent_def = emsg_silent;
1502 did_emsg_def = 0;
1503
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001504 where.wt_index = 0;
1505 where.wt_variable = FALSE;
1506
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001507 // Start execution at the first instruction.
1508 ectx.ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 for (;;)
1511 {
1512 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001513
Bram Moolenaar270d0382020-05-15 21:42:53 +02001514 if (++breakcheck_count >= 100)
1515 {
1516 line_breakcheck();
1517 breakcheck_count = 0;
1518 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001519 if (got_int)
1520 {
1521 // Turn CTRL-C into an exception.
1522 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001523 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001524 goto failed;
1525 did_throw = TRUE;
1526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527
Bram Moolenaara26b9702020-04-18 19:53:28 +02001528 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1529 {
1530 // Turn an error message into an exception.
1531 did_emsg = FALSE;
1532 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1533 goto failed;
1534 did_throw = TRUE;
1535 *msg_list = NULL;
1536 }
1537
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 if (did_throw && !ectx.ec_in_catch)
1539 {
1540 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001541 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542
1543 // An exception jumps to the first catch, finally, or returns from
1544 // the current function.
1545 if (trystack->ga_len > 0)
1546 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001547 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 {
1549 // jump to ":catch" or ":finally"
1550 ectx.ec_in_catch = TRUE;
1551 ectx.ec_iidx = trycmd->tcd_catch_idx;
1552 }
1553 else
1554 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001555 // Not inside try or need to return from current functions.
1556 // Push a dummy return value.
1557 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1558 goto failed;
1559 tv = STACK_TV_BOT(0);
1560 tv->v_type = VAR_NUMBER;
1561 tv->vval.v_number = 0;
1562 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001563 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001565 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001566 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001567 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1568 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 goto done;
1570 }
1571
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001572 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001573 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 }
1575 continue;
1576 }
1577
1578 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1579 switch (iptr->isn_type)
1580 {
1581 // execute Ex command line
1582 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001583 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001584 source_cookie_T cookie;
1585
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001586 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001587 // Pass getsourceline to get an error for a missing ":end"
1588 // command.
1589 CLEAR_FIELD(cookie);
1590 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1591 if (do_cmdline(iptr->isn_arg.string,
1592 getsourceline, &cookie,
1593 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1594 == FAIL
1595 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001596 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 break;
1599
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001600 // execute Ex command from pieces on the stack
1601 case ISN_EXECCONCAT:
1602 {
1603 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001604 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001605 int pass;
1606 int i;
1607 char_u *cmd = NULL;
1608 char_u *str;
1609
1610 for (pass = 1; pass <= 2; ++pass)
1611 {
1612 for (i = 0; i < count; ++i)
1613 {
1614 tv = STACK_TV_BOT(i - count);
1615 str = tv->vval.v_string;
1616 if (str != NULL && *str != NUL)
1617 {
1618 if (pass == 2)
1619 STRCPY(cmd + len, str);
1620 len += STRLEN(str);
1621 }
1622 if (pass == 2)
1623 clear_tv(tv);
1624 }
1625 if (pass == 1)
1626 {
1627 cmd = alloc(len + 1);
1628 if (cmd == NULL)
1629 goto failed;
1630 len = 0;
1631 }
1632 }
1633
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001634 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001635 do_cmdline_cmd(cmd);
1636 vim_free(cmd);
1637 }
1638 break;
1639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 // execute :echo {string} ...
1641 case ISN_ECHO:
1642 {
1643 int count = iptr->isn_arg.echo.echo_count;
1644 int atstart = TRUE;
1645 int needclr = TRUE;
1646
1647 for (idx = 0; idx < count; ++idx)
1648 {
1649 tv = STACK_TV_BOT(idx - count);
1650 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1651 &atstart, &needclr);
1652 clear_tv(tv);
1653 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001654 if (needclr)
1655 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 ectx.ec_stack.ga_len -= count;
1657 }
1658 break;
1659
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001660 // :execute {string} ...
1661 // :echomsg {string} ...
1662 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001663 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001664 case ISN_ECHOMSG:
1665 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001666 {
1667 int count = iptr->isn_arg.number;
1668 garray_T ga;
1669 char_u buf[NUMBUFLEN];
1670 char_u *p;
1671 int len;
1672 int failed = FALSE;
1673
1674 ga_init2(&ga, 1, 80);
1675 for (idx = 0; idx < count; ++idx)
1676 {
1677 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001678 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001679 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001680 if (tv->v_type == VAR_CHANNEL
1681 || tv->v_type == VAR_JOB)
1682 {
1683 SOURCING_LNUM = iptr->isn_lnum;
1684 emsg(_(e_inval_string));
1685 break;
1686 }
1687 else
1688 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001689 }
1690 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001691 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001692
1693 len = (int)STRLEN(p);
1694 if (ga_grow(&ga, len + 2) == FAIL)
1695 failed = TRUE;
1696 else
1697 {
1698 if (ga.ga_len > 0)
1699 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1700 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1701 ga.ga_len += len;
1702 }
1703 clear_tv(tv);
1704 }
1705 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001706 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001707 {
1708 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001709 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001710 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001711
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001712 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001713 {
1714 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001715 {
1716 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001717 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001718 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001719 {
1720 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001721 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001722 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001723 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001724 else
1725 {
1726 msg_sb_eol();
1727 if (iptr->isn_type == ISN_ECHOMSG)
1728 {
1729 msg_attr(ga.ga_data, echo_attr);
1730 out_flush();
1731 }
1732 else
1733 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001734 SOURCING_LNUM = iptr->isn_lnum;
1735 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001736 }
1737 }
1738 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001739 ga_clear(&ga);
1740 }
1741 break;
1742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 // load local variable or argument
1744 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001745 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 goto failed;
1747 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1748 ++ectx.ec_stack.ga_len;
1749 break;
1750
1751 // load v: variable
1752 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001753 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 goto failed;
1755 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1756 ++ectx.ec_stack.ga_len;
1757 break;
1758
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001759 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 case ISN_LOADSCRIPT:
1761 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001762 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 svar_T *sv;
1764
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001765 sv = get_script_svar(sref, &ectx);
1766 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001767 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001768 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001769 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 goto failed;
1771 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1772 ++ectx.ec_stack.ga_len;
1773 }
1774 break;
1775
1776 // load s: variable in old script
1777 case ISN_LOADS:
1778 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001779 hashtab_T *ht = &SCRIPT_VARS(
1780 iptr->isn_arg.loadstore.ls_sid);
1781 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 if (di == NULL)
1785 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001786 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001787 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001788 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 }
1790 else
1791 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001792 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 goto failed;
1794 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1795 ++ectx.ec_stack.ga_len;
1796 }
1797 }
1798 break;
1799
Bram Moolenaard3aac292020-04-19 14:32:17 +02001800 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001802 case ISN_LOADB:
1803 case ISN_LOADW:
1804 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001806 dictitem_T *di = NULL;
1807 hashtab_T *ht = NULL;
1808 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001809
Bram Moolenaard3aac292020-04-19 14:32:17 +02001810 switch (iptr->isn_type)
1811 {
1812 case ISN_LOADG:
1813 ht = get_globvar_ht();
1814 namespace = 'g';
1815 break;
1816 case ISN_LOADB:
1817 ht = &curbuf->b_vars->dv_hashtab;
1818 namespace = 'b';
1819 break;
1820 case ISN_LOADW:
1821 ht = &curwin->w_vars->dv_hashtab;
1822 namespace = 'w';
1823 break;
1824 case ISN_LOADT:
1825 ht = &curtab->tp_vars->dv_hashtab;
1826 namespace = 't';
1827 break;
1828 default: // Cannot reach here
1829 goto failed;
1830 }
1831 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 if (di == NULL)
1834 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001835 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001836 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001837 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001838 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 }
1840 else
1841 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001842 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 goto failed;
1844 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1845 ++ectx.ec_stack.ga_len;
1846 }
1847 }
1848 break;
1849
Bram Moolenaar03290b82020-12-19 16:30:44 +01001850 // load autoload variable
1851 case ISN_LOADAUTO:
1852 {
1853 char_u *name = iptr->isn_arg.string;
1854
1855 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1856 goto failed;
1857 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001858 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001859 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001860 goto on_error;
1861 ++ectx.ec_stack.ga_len;
1862 }
1863 break;
1864
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001865 // load g:/b:/w:/t: namespace
1866 case ISN_LOADGDICT:
1867 case ISN_LOADBDICT:
1868 case ISN_LOADWDICT:
1869 case ISN_LOADTDICT:
1870 {
1871 dict_T *d = NULL;
1872
1873 switch (iptr->isn_type)
1874 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001875 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1876 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1877 case ISN_LOADWDICT: d = curwin->w_vars; break;
1878 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001879 default: // Cannot reach here
1880 goto failed;
1881 }
1882 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1883 goto failed;
1884 tv = STACK_TV_BOT(0);
1885 tv->v_type = VAR_DICT;
1886 tv->v_lock = 0;
1887 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001888 ++d->dv_refcount;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001889 ++ectx.ec_stack.ga_len;
1890 }
1891 break;
1892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 // load &option
1894 case ISN_LOADOPT:
1895 {
1896 typval_T optval;
1897 char_u *name = iptr->isn_arg.string;
1898
Bram Moolenaara8c17702020-04-01 21:17:24 +02001899 // This is not expected to fail, name is checked during
1900 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001901 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001903 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001904 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 *STACK_TV_BOT(0) = optval;
1906 ++ectx.ec_stack.ga_len;
1907 }
1908 break;
1909
1910 // load $ENV
1911 case ISN_LOADENV:
1912 {
1913 typval_T optval;
1914 char_u *name = iptr->isn_arg.string;
1915
Bram Moolenaar270d0382020-05-15 21:42:53 +02001916 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001918 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001919 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 *STACK_TV_BOT(0) = optval;
1921 ++ectx.ec_stack.ga_len;
1922 }
1923 break;
1924
1925 // load @register
1926 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001927 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 goto failed;
1929 tv = STACK_TV_BOT(0);
1930 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001931 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001932 // This may result in NULL, which should be equivalent to an
1933 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 tv->vval.v_string = get_reg_contents(
1935 iptr->isn_arg.number, GREG_EXPR_SRC);
1936 ++ectx.ec_stack.ga_len;
1937 break;
1938
1939 // store local variable
1940 case ISN_STORE:
1941 --ectx.ec_stack.ga_len;
1942 tv = STACK_TV_VAR(iptr->isn_arg.number);
1943 clear_tv(tv);
1944 *tv = *STACK_TV_BOT(0);
1945 break;
1946
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001947 // store s: variable in old script
1948 case ISN_STORES:
1949 {
1950 hashtab_T *ht = &SCRIPT_VARS(
1951 iptr->isn_arg.loadstore.ls_sid);
1952 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001953 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001954
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001955 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001956 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001957 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001958 else
1959 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001960 SOURCING_LNUM = iptr->isn_lnum;
1961 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001962 {
1963 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001964 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001965 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001966 clear_tv(&di->di_tv);
1967 di->di_tv = *STACK_TV_BOT(0);
1968 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001969 }
1970 break;
1971
1972 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 case ISN_STORESCRIPT:
1974 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001975 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1976 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001978 sv = get_script_svar(sref, &ectx);
1979 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001980 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 --ectx.ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001982
1983 // "const" and "final" are checked at compile time, locking
1984 // the value needs to be checked here.
1985 SOURCING_LNUM = iptr->isn_lnum;
1986 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001987 {
1988 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001989 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001990 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 clear_tv(sv->sv_tv);
1993 *sv->sv_tv = *STACK_TV_BOT(0);
1994 }
1995 break;
1996
1997 // store option
1998 case ISN_STOREOPT:
1999 {
2000 long n = 0;
2001 char_u *s = NULL;
2002 char *msg;
2003
2004 --ectx.ec_stack.ga_len;
2005 tv = STACK_TV_BOT(0);
2006 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002007 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002009 if (s == NULL)
2010 s = (char_u *)"";
2011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002013 // must be VAR_NUMBER, CHECKTYPE makes sure
2014 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2016 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002017 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 if (msg != NULL)
2019 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002020 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002022 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 }
2025 break;
2026
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002027 // store $ENV
2028 case ISN_STOREENV:
2029 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002030 tv = STACK_TV_BOT(0);
2031 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2032 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002033 break;
2034
2035 // store @r
2036 case ISN_STOREREG:
2037 {
2038 int reg = iptr->isn_arg.number;
2039
2040 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002041 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002042 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002043 tv_get_string(tv), -1, FALSE);
2044 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002045 }
2046 break;
2047
2048 // store v: variable
2049 case ISN_STOREV:
2050 --ectx.ec_stack.ga_len;
2051 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2052 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002053 // should not happen, type is checked when compiling
2054 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002055 break;
2056
Bram Moolenaard3aac292020-04-19 14:32:17 +02002057 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002059 case ISN_STOREB:
2060 case ISN_STOREW:
2061 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002063 dictitem_T *di;
2064 hashtab_T *ht;
2065 char_u *name = iptr->isn_arg.string + 2;
2066
Bram Moolenaard3aac292020-04-19 14:32:17 +02002067 switch (iptr->isn_type)
2068 {
2069 case ISN_STOREG:
2070 ht = get_globvar_ht();
2071 break;
2072 case ISN_STOREB:
2073 ht = &curbuf->b_vars->dv_hashtab;
2074 break;
2075 case ISN_STOREW:
2076 ht = &curwin->w_vars->dv_hashtab;
2077 break;
2078 case ISN_STORET:
2079 ht = &curtab->tp_vars->dv_hashtab;
2080 break;
2081 default: // Cannot reach here
2082 goto failed;
2083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084
2085 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002086 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002088 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 else
2090 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002091 SOURCING_LNUM = iptr->isn_lnum;
2092 if (var_check_permission(di, name) == FAIL)
2093 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 clear_tv(&di->di_tv);
2095 di->di_tv = *STACK_TV_BOT(0);
2096 }
2097 }
2098 break;
2099
Bram Moolenaar03290b82020-12-19 16:30:44 +01002100 // store an autoload variable
2101 case ISN_STOREAUTO:
2102 SOURCING_LNUM = iptr->isn_lnum;
2103 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2104 clear_tv(STACK_TV_BOT(-1));
2105 --ectx.ec_stack.ga_len;
2106 break;
2107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 // store number in local variable
2109 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002110 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 clear_tv(tv);
2112 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002113 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 break;
2115
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002116 // store value in list or dict variable
2117 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002118 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002119 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002120 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002121 typval_T *tv_dest = STACK_TV_BOT(-1);
2122 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002123
Bram Moolenaar752fc692021-01-04 21:57:11 +01002124 // Stack contains:
2125 // -3 value to be stored
2126 // -2 index
2127 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002128 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002129 SOURCING_LNUM = iptr->isn_lnum;
2130 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002131 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002132 dest_type = tv_dest->v_type;
2133 if (dest_type == VAR_DICT)
2134 status = do_2string(tv_idx, TRUE);
2135 else if (dest_type == VAR_LIST
2136 && tv_idx->v_type != VAR_NUMBER)
2137 {
2138 emsg(_(e_number_exp));
2139 status = FAIL;
2140 }
2141 }
2142 else if (dest_type != tv_dest->v_type)
2143 {
2144 // just in case, should be OK
2145 semsg(_(e_expected_str_but_got_str),
2146 vartype_name(dest_type),
2147 vartype_name(tv_dest->v_type));
2148 status = FAIL;
2149 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002150
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002151 if (status == OK && dest_type == VAR_LIST)
2152 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002153 long lidx = (long)tv_idx->vval.v_number;
2154 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002155
2156 if (list == NULL)
2157 {
2158 emsg(_(e_list_not_set));
2159 goto on_error;
2160 }
2161 if (lidx < 0 && list->lv_len + lidx >= 0)
2162 // negative index is relative to the end
2163 lidx = list->lv_len + lidx;
2164 if (lidx < 0 || lidx > list->lv_len)
2165 {
2166 semsg(_(e_listidx), lidx);
2167 goto on_error;
2168 }
2169 if (lidx < list->lv_len)
2170 {
2171 listitem_T *li = list_find(list, lidx);
2172
2173 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002174 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002175 goto on_error;
2176 // overwrite existing list item
2177 clear_tv(&li->li_tv);
2178 li->li_tv = *tv;
2179 }
2180 else
2181 {
2182 if (error_if_locked(list->lv_lock,
2183 e_cannot_change_list))
2184 goto on_error;
2185 // append to list, only fails when out of memory
2186 if (list_append_tv(list, tv) == FAIL)
2187 goto failed;
2188 clear_tv(tv);
2189 }
2190 }
2191 else if (status == OK && dest_type == VAR_DICT)
2192 {
2193 char_u *key = tv_idx->vval.v_string;
2194 dict_T *dict = tv_dest->vval.v_dict;
2195 dictitem_T *di;
2196
2197 SOURCING_LNUM = iptr->isn_lnum;
2198 if (dict == NULL)
2199 {
2200 emsg(_(e_dictionary_not_set));
2201 goto on_error;
2202 }
2203 if (key == NULL)
2204 key = (char_u *)"";
2205 di = dict_find(dict, key, -1);
2206 if (di != NULL)
2207 {
2208 if (error_if_locked(di->di_tv.v_lock,
2209 e_cannot_change_dict_item))
2210 goto on_error;
2211 // overwrite existing value
2212 clear_tv(&di->di_tv);
2213 di->di_tv = *tv;
2214 }
2215 else
2216 {
2217 if (error_if_locked(dict->dv_lock,
2218 e_cannot_change_dict))
2219 goto on_error;
2220 // add to dict, only fails when out of memory
2221 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2222 goto failed;
2223 clear_tv(tv);
2224 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002225 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002226 else if (status == OK && dest_type == VAR_BLOB)
2227 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002228 long lidx = (long)tv_idx->vval.v_number;
2229 blob_T *blob = tv_dest->vval.v_blob;
2230 varnumber_T nr;
2231 int error = FALSE;
2232 int len;
2233
2234 if (blob == NULL)
2235 {
2236 emsg(_(e_blob_not_set));
2237 goto on_error;
2238 }
2239 len = blob_len(blob);
2240 if (lidx < 0 && len + lidx >= 0)
2241 // negative index is relative to the end
2242 lidx = len + lidx;
2243
2244 // Can add one byte at the end.
2245 if (lidx < 0 || lidx > len)
2246 {
2247 semsg(_(e_blobidx), lidx);
2248 goto on_error;
2249 }
2250 if (value_check_lock(blob->bv_lock,
2251 (char_u *)"blob", FALSE))
2252 goto on_error;
2253 nr = tv_get_number_chk(tv, &error);
2254 if (error)
2255 goto on_error;
2256 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002257 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002258 else
2259 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002260 status = FAIL;
2261 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002262 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002263
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002264 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002265 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002266 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002267 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002268 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002269 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002270 goto on_error;
2271 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002272 }
2273 break;
2274
Bram Moolenaar68452172021-04-12 21:21:02 +02002275 // store value in blob range
2276 case ISN_STORERANGE:
2277 {
2278 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2279 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2280 typval_T *tv_dest = STACK_TV_BOT(-1);
2281 int status = OK;
2282
2283 // Stack contains:
2284 // -4 value to be stored
2285 // -3 first index or "none"
2286 // -2 second index or "none"
2287 // -1 destination blob
2288 tv = STACK_TV_BOT(-4);
2289 if (tv_dest->v_type != VAR_BLOB)
2290 {
2291 status = FAIL;
2292 emsg(_(e_blob_required));
2293 }
2294 else
2295 {
2296 varnumber_T n1;
2297 varnumber_T n2;
2298 int error = FALSE;
2299
2300 n1 = tv_get_number_chk(tv_idx1, &error);
2301 if (error)
2302 status = FAIL;
2303 else
2304 {
2305 if (tv_idx2->v_type == VAR_SPECIAL
2306 && tv_idx2->vval.v_number == VVAL_NONE)
2307 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2308 else
2309 n2 = tv_get_number_chk(tv_idx2, &error);
2310 if (error)
2311 status = FAIL;
2312 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002313 {
2314 long bloblen = blob_len(tv_dest->vval.v_blob);
2315
2316 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002317 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002318 || check_blob_range(bloblen,
2319 n1, n2, FALSE) == FAIL)
2320 status = FAIL;
2321 else
2322 status = blob_set_range(
2323 tv_dest->vval.v_blob, n1, n2, tv);
2324 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002325 }
2326 }
2327
2328 clear_tv(tv_idx1);
2329 clear_tv(tv_idx2);
2330 clear_tv(tv_dest);
2331 ectx.ec_stack.ga_len -= 4;
2332 clear_tv(tv);
2333
2334 if (status == FAIL)
2335 goto on_error;
2336 }
2337 break;
2338
Bram Moolenaar0186e582021-01-10 18:33:11 +01002339 // load or store variable or argument from outer scope
2340 case ISN_LOADOUTER:
2341 case ISN_STOREOUTER:
2342 {
2343 int depth = iptr->isn_arg.outer.outer_depth;
2344 outer_T *outer = ectx.ec_outer;
2345
2346 while (depth > 1 && outer != NULL)
2347 {
2348 outer = outer->out_up;
2349 --depth;
2350 }
2351 if (outer == NULL)
2352 {
2353 SOURCING_LNUM = iptr->isn_lnum;
2354 iemsg("LOADOUTER depth more than scope levels");
2355 goto failed;
2356 }
2357 tv = ((typval_T *)outer->out_stack->ga_data)
2358 + outer->out_frame_idx + STACK_FRAME_SIZE
2359 + iptr->isn_arg.outer.outer_idx;
2360 if (iptr->isn_type == ISN_LOADOUTER)
2361 {
2362 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2363 goto failed;
2364 copy_tv(tv, STACK_TV_BOT(0));
2365 ++ectx.ec_stack.ga_len;
2366 }
2367 else
2368 {
2369 --ectx.ec_stack.ga_len;
2370 clear_tv(tv);
2371 *tv = *STACK_TV_BOT(0);
2372 }
2373 }
2374 break;
2375
Bram Moolenaar752fc692021-01-04 21:57:11 +01002376 // unlet item in list or dict variable
2377 case ISN_UNLETINDEX:
2378 {
2379 typval_T *tv_idx = STACK_TV_BOT(-2);
2380 typval_T *tv_dest = STACK_TV_BOT(-1);
2381 int status = OK;
2382
2383 // Stack contains:
2384 // -2 index
2385 // -1 dict or list
2386 if (tv_dest->v_type == VAR_DICT)
2387 {
2388 // unlet a dict item, index must be a string
2389 if (tv_idx->v_type != VAR_STRING)
2390 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002391 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002392 semsg(_(e_expected_str_but_got_str),
2393 vartype_name(VAR_STRING),
2394 vartype_name(tv_idx->v_type));
2395 status = FAIL;
2396 }
2397 else
2398 {
2399 dict_T *d = tv_dest->vval.v_dict;
2400 char_u *key = tv_idx->vval.v_string;
2401 dictitem_T *di = NULL;
2402
2403 if (key == NULL)
2404 key = (char_u *)"";
2405 if (d != NULL)
2406 di = dict_find(d, key, (int)STRLEN(key));
2407 if (di == NULL)
2408 {
2409 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002410 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002411 semsg(_(e_dictkey), key);
2412 status = FAIL;
2413 }
2414 else
2415 {
2416 // TODO: check for dict or item locked
2417 dictitem_remove(d, di);
2418 }
2419 }
2420 }
2421 else if (tv_dest->v_type == VAR_LIST)
2422 {
2423 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002424 SOURCING_LNUM = iptr->isn_lnum;
2425 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002426 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002427 status = FAIL;
2428 }
2429 else
2430 {
2431 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002432 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002433 listitem_T *li = NULL;
2434
2435 li = list_find(l, n);
2436 if (li == NULL)
2437 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002438 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002439 semsg(_(e_listidx), n);
2440 status = FAIL;
2441 }
2442 else
2443 // TODO: check for list or item locked
2444 listitem_remove(l, li);
2445 }
2446 }
2447 else
2448 {
2449 status = FAIL;
2450 semsg(_(e_cannot_index_str),
2451 vartype_name(tv_dest->v_type));
2452 }
2453
2454 clear_tv(tv_idx);
2455 clear_tv(tv_dest);
2456 ectx.ec_stack.ga_len -= 2;
2457 if (status == FAIL)
2458 goto on_error;
2459 }
2460 break;
2461
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002462 // unlet range of items in list variable
2463 case ISN_UNLETRANGE:
2464 {
2465 // Stack contains:
2466 // -3 index1
2467 // -2 index2
2468 // -1 dict or list
2469 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2470 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2471 typval_T *tv_dest = STACK_TV_BOT(-1);
2472 int status = OK;
2473
2474 if (tv_dest->v_type == VAR_LIST)
2475 {
2476 // indexes must be a number
2477 SOURCING_LNUM = iptr->isn_lnum;
2478 if (check_for_number(tv_idx1) == FAIL
2479 || check_for_number(tv_idx2) == FAIL)
2480 {
2481 status = FAIL;
2482 }
2483 else
2484 {
2485 list_T *l = tv_dest->vval.v_list;
2486 long n1 = (long)tv_idx1->vval.v_number;
2487 long n2 = (long)tv_idx2->vval.v_number;
2488 listitem_T *li;
2489
2490 li = list_find_index(l, &n1);
2491 if (li == NULL
2492 || list_unlet_range(l, li, NULL, n1,
2493 TRUE, n2) == FAIL)
2494 status = FAIL;
2495 }
2496 }
2497 else
2498 {
2499 status = FAIL;
2500 SOURCING_LNUM = iptr->isn_lnum;
2501 semsg(_(e_cannot_index_str),
2502 vartype_name(tv_dest->v_type));
2503 }
2504
2505 clear_tv(tv_idx1);
2506 clear_tv(tv_idx2);
2507 clear_tv(tv_dest);
2508 ectx.ec_stack.ga_len -= 3;
2509 if (status == FAIL)
2510 goto on_error;
2511 }
2512 break;
2513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 // push constant
2515 case ISN_PUSHNR:
2516 case ISN_PUSHBOOL:
2517 case ISN_PUSHSPEC:
2518 case ISN_PUSHF:
2519 case ISN_PUSHS:
2520 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002521 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002522 case ISN_PUSHCHANNEL:
2523 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002524 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 goto failed;
2526 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002527 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 ++ectx.ec_stack.ga_len;
2529 switch (iptr->isn_type)
2530 {
2531 case ISN_PUSHNR:
2532 tv->v_type = VAR_NUMBER;
2533 tv->vval.v_number = iptr->isn_arg.number;
2534 break;
2535 case ISN_PUSHBOOL:
2536 tv->v_type = VAR_BOOL;
2537 tv->vval.v_number = iptr->isn_arg.number;
2538 break;
2539 case ISN_PUSHSPEC:
2540 tv->v_type = VAR_SPECIAL;
2541 tv->vval.v_number = iptr->isn_arg.number;
2542 break;
2543#ifdef FEAT_FLOAT
2544 case ISN_PUSHF:
2545 tv->v_type = VAR_FLOAT;
2546 tv->vval.v_float = iptr->isn_arg.fnumber;
2547 break;
2548#endif
2549 case ISN_PUSHBLOB:
2550 blob_copy(iptr->isn_arg.blob, tv);
2551 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002552 case ISN_PUSHFUNC:
2553 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002554 if (iptr->isn_arg.string == NULL)
2555 tv->vval.v_string = NULL;
2556 else
2557 tv->vval.v_string =
2558 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002559 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002560 case ISN_PUSHCHANNEL:
2561#ifdef FEAT_JOB_CHANNEL
2562 tv->v_type = VAR_CHANNEL;
2563 tv->vval.v_channel = iptr->isn_arg.channel;
2564 if (tv->vval.v_channel != NULL)
2565 ++tv->vval.v_channel->ch_refcount;
2566#endif
2567 break;
2568 case ISN_PUSHJOB:
2569#ifdef FEAT_JOB_CHANNEL
2570 tv->v_type = VAR_JOB;
2571 tv->vval.v_job = iptr->isn_arg.job;
2572 if (tv->vval.v_job != NULL)
2573 ++tv->vval.v_job->jv_refcount;
2574#endif
2575 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 default:
2577 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002578 tv->vval.v_string = vim_strsave(
2579 iptr->isn_arg.string == NULL
2580 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 }
2582 break;
2583
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002584 case ISN_UNLET:
2585 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2586 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002587 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002588 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002589 case ISN_UNLETENV:
2590 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2591 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002592
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002593 case ISN_LOCKCONST:
2594 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2595 break;
2596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 // create a list from items on the stack; uses a single allocation
2598 // for the list header and the items
2599 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002600 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2601 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 break;
2603
2604 // create a dict from items on the stack
2605 case ISN_NEWDICT:
2606 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002607 int count = iptr->isn_arg.number;
2608 dict_T *dict = dict_alloc();
2609 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002610 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611
2612 if (dict == NULL)
2613 goto failed;
2614 for (idx = 0; idx < count; ++idx)
2615 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002616 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002618 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002619 key = tv->vval.v_string == NULL
2620 ? (char_u *)"" : tv->vval.v_string;
2621 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002622 if (item != NULL)
2623 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002624 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002625 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002626 dict_unref(dict);
2627 goto on_error;
2628 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002629 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 clear_tv(tv);
2631 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002632 {
2633 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2637 item->di_tv.v_lock = 0;
2638 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002639 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002640 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002641 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 }
2645
2646 if (count > 0)
2647 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002648 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 goto failed;
2650 else
2651 ++ectx.ec_stack.ga_len;
2652 tv = STACK_TV_BOT(-1);
2653 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002654 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 tv->vval.v_dict = dict;
2656 ++dict->dv_refcount;
2657 }
2658 break;
2659
2660 // call a :def function
2661 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002662 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002663 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2664 NULL,
2665 iptr->isn_arg.dfunc.cdf_argcount,
2666 &funclocal,
2667 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002668 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 break;
2670
2671 // call a builtin function
2672 case ISN_BCALL:
2673 SOURCING_LNUM = iptr->isn_lnum;
2674 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2675 iptr->isn_arg.bfunc.cbf_argcount,
2676 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002677 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 break;
2679
2680 // call a funcref or partial
2681 case ISN_PCALL:
2682 {
2683 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2684 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002685 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686
2687 SOURCING_LNUM = iptr->isn_lnum;
2688 if (pfunc->cpf_top)
2689 {
2690 // funcref is above the arguments
2691 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2692 }
2693 else
2694 {
2695 // Get the funcref from the stack.
2696 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002697 partial_tv = *STACK_TV_BOT(0);
2698 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002700 r = call_partial(tv, pfunc->cpf_argcount,
2701 &funclocal, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002702 if (tv == &partial_tv)
2703 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002705 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 }
2707 break;
2708
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002709 case ISN_PCALL_END:
2710 // PCALL finished, arguments have been consumed and replaced by
2711 // the return value. Now clear the funcref from the stack,
2712 // and move the return value in its place.
2713 --ectx.ec_stack.ga_len;
2714 clear_tv(STACK_TV_BOT(-1));
2715 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2716 break;
2717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 // call a user defined function or funcref/partial
2719 case ISN_UCALL:
2720 {
2721 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2722
2723 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002724 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
2725 &funclocal, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002726 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 }
2728 break;
2729
2730 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002731 case ISN_RETURN_ZERO:
2732 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2733 goto failed;
2734 tv = STACK_TV_BOT(0);
2735 ++ectx.ec_stack.ga_len;
2736 tv->v_type = VAR_NUMBER;
2737 tv->vval.v_number = 0;
2738 tv->v_lock = 0;
2739 // FALLTHROUGH
2740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 case ISN_RETURN:
2742 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002743 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002744 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002745
2746 if (trystack->ga_len > 0)
2747 trycmd = ((trycmd_T *)trystack->ga_data)
2748 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002749 if (trycmd != NULL
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002750 && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002752 // jump to ":finally" or ":endtry"
2753 if (trycmd->tcd_finally_idx != 0)
2754 ectx.ec_iidx = trycmd->tcd_finally_idx;
2755 else
2756 ectx.ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 trycmd->tcd_return = TRUE;
2758 }
2759 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002760 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 }
2762 break;
2763
2764 // push a function reference to a compiled function
2765 case ISN_FUNCREF:
2766 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002767 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2768 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2769 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 if (pt == NULL)
2772 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002773 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002774 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002775 vim_free(pt);
2776 goto failed;
2777 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002778 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2779 &ectx) == FAIL)
2780 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 tv = STACK_TV_BOT(0);
2783 ++ectx.ec_stack.ga_len;
2784 tv->vval.v_partial = pt;
2785 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002786 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 }
2788 break;
2789
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002790 // Create a global function from a lambda.
2791 case ISN_NEWFUNC:
2792 {
2793 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2794
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002795 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002796 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002797 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002798 }
2799 break;
2800
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002801 // List functions
2802 case ISN_DEF:
2803 if (iptr->isn_arg.string == NULL)
2804 list_functions(NULL);
2805 else
2806 {
2807 exarg_T ea;
2808
2809 CLEAR_FIELD(ea);
2810 ea.cmd = ea.arg = iptr->isn_arg.string;
2811 define_function(&ea, NULL);
2812 }
2813 break;
2814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 // jump if a condition is met
2816 case ISN_JUMP:
2817 {
2818 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002819 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 int jump = TRUE;
2821
2822 if (when != JUMP_ALWAYS)
2823 {
2824 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002825 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002826 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002827 || when == JUMP_IF_COND_TRUE)
2828 {
2829 SOURCING_LNUM = iptr->isn_lnum;
2830 jump = tv_get_bool_chk(tv, &error);
2831 if (error)
2832 goto on_error;
2833 }
2834 else
2835 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002837 || when == JUMP_AND_KEEP_IF_FALSE
2838 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002840 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 {
2842 // drop the value from the stack
2843 clear_tv(tv);
2844 --ectx.ec_stack.ga_len;
2845 }
2846 }
2847 if (jump)
2848 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2849 }
2850 break;
2851
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002852 // Jump if an argument with a default value was already set and not
2853 // v:none.
2854 case ISN_JUMP_IF_ARG_SET:
2855 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2856 if (tv->v_type != VAR_UNKNOWN
2857 && !(tv->v_type == VAR_SPECIAL
2858 && tv->vval.v_number == VVAL_NONE))
2859 ectx.ec_iidx = iptr->isn_arg.jumparg.jump_where;
2860 break;
2861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 // top of a for loop
2863 case ISN_FOR:
2864 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002865 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 typval_T *idxtv =
2867 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2868
Bram Moolenaar270d0382020-05-15 21:42:53 +02002869 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 goto failed;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002871 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002872 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002873 list_T *list = ltv->vval.v_list;
2874
2875 // push the next item from the list
2876 ++idxtv->vval.v_number;
2877 if (list == NULL
2878 || idxtv->vval.v_number >= list->lv_len)
2879 {
2880 // past the end of the list, jump to "endfor"
2881 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2882 may_restore_cmdmod(&funclocal);
2883 }
2884 else if (list->lv_first == &range_list_item)
2885 {
2886 // non-materialized range() list
2887 tv = STACK_TV_BOT(0);
2888 tv->v_type = VAR_NUMBER;
2889 tv->v_lock = 0;
2890 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 list, idxtv->vval.v_number, NULL);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002892 ++ectx.ec_stack.ga_len;
2893 }
2894 else
2895 {
2896 listitem_T *li = list_find(list,
2897 idxtv->vval.v_number);
2898
2899 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2900 ++ectx.ec_stack.ga_len;
2901 }
2902 }
2903 else if (ltv->v_type == VAR_STRING)
2904 {
2905 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002906
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002907 // The index is for the last byte of the previous
2908 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002909 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002910 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002911 {
2912 // past the end of the string, jump to "endfor"
2913 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2914 may_restore_cmdmod(&funclocal);
2915 }
2916 else
2917 {
2918 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2919
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002920 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002921 tv = STACK_TV_BOT(0);
2922 tv->v_type = VAR_STRING;
2923 tv->vval.v_string = vim_strnsave(
2924 str + idxtv->vval.v_number, clen);
2925 ++ectx.ec_stack.ga_len;
2926 idxtv->vval.v_number += clen - 1;
2927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002929 else if (ltv->v_type == VAR_BLOB)
2930 {
2931 blob_T *blob = ltv->vval.v_blob;
2932
2933 // When we get here the first time make a copy of the
2934 // blob, so that the iteration still works when it is
2935 // changed.
2936 if (idxtv->vval.v_number == -1 && blob != NULL)
2937 {
2938 blob_copy(blob, ltv);
2939 blob_unref(blob);
2940 blob = ltv->vval.v_blob;
2941 }
2942
2943 // The index is for the previous byte.
2944 ++idxtv->vval.v_number;
2945 if (blob == NULL
2946 || idxtv->vval.v_number >= blob_len(blob))
2947 {
2948 // past the end of the blob, jump to "endfor"
2949 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2950 may_restore_cmdmod(&funclocal);
2951 }
2952 else
2953 {
2954 // Push the next byte from the blob.
2955 tv = STACK_TV_BOT(0);
2956 tv->v_type = VAR_NUMBER;
2957 tv->vval.v_number = blob_get(blob,
2958 idxtv->vval.v_number);
2959 ++ectx.ec_stack.ga_len;
2960 }
2961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 else
2963 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002964 semsg(_(e_for_loop_on_str_not_supported),
2965 vartype_name(ltv->v_type));
2966 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 }
2968 }
2969 break;
2970
2971 // start of ":try" block
2972 case ISN_TRY:
2973 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002974 trycmd_T *trycmd = NULL;
2975
Bram Moolenaar270d0382020-05-15 21:42:53 +02002976 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 goto failed;
2978 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2979 + ectx.ec_trystack.ga_len;
2980 ++ectx.ec_trystack.ga_len;
2981 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002982 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002983 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002984 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002985 trycmd->tcd_catch_idx =
2986 iptr->isn_arg.try.try_ref->try_catch;
2987 trycmd->tcd_finally_idx =
2988 iptr->isn_arg.try.try_ref->try_finally;
2989 trycmd->tcd_endtry_idx =
2990 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 }
2992 break;
2993
2994 case ISN_PUSHEXC:
2995 if (current_exception == NULL)
2996 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002997 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 iemsg("Evaluating catch while current_exception is NULL");
2999 goto failed;
3000 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02003001 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 goto failed;
3003 tv = STACK_TV_BOT(0);
3004 ++ectx.ec_stack.ga_len;
3005 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003006 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 tv->vval.v_string = vim_strsave(
3008 (char_u *)current_exception->value);
3009 break;
3010
3011 case ISN_CATCH:
3012 {
3013 garray_T *trystack = &ectx.ec_trystack;
3014
Bram Moolenaara91a7132021-03-25 21:12:15 +01003015 may_restore_cmdmod(&funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 if (trystack->ga_len > 0)
3017 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003018 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 + trystack->ga_len - 1;
3020 trycmd->tcd_caught = TRUE;
3021 }
3022 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003023 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 catch_exception(current_exception);
3025 }
3026 break;
3027
Bram Moolenaarc150c092021-02-13 15:02:46 +01003028 case ISN_TRYCONT:
3029 {
3030 garray_T *trystack = &ectx.ec_trystack;
3031 trycont_T *trycont = &iptr->isn_arg.trycont;
3032 int i;
3033 trycmd_T *trycmd;
3034 int iidx = trycont->tct_where;
3035
3036 if (trystack->ga_len < trycont->tct_levels)
3037 {
3038 siemsg("TRYCONT: expected %d levels, found %d",
3039 trycont->tct_levels, trystack->ga_len);
3040 goto failed;
3041 }
3042 // Make :endtry jump to any outer try block and the last
3043 // :endtry inside the loop to the loop start.
3044 for (i = trycont->tct_levels; i > 0; --i)
3045 {
3046 trycmd = ((trycmd_T *)trystack->ga_data)
3047 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003048 // Add one to tcd_cont to be able to jump to
3049 // instruction with index zero.
3050 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003051 iidx = trycmd->tcd_finally_idx == 0
3052 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003053 }
3054 // jump to :finally or :endtry of current try statement
3055 ectx.ec_iidx = iidx;
3056 }
3057 break;
3058
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003059 case ISN_FINALLY:
3060 {
3061 garray_T *trystack = &ectx.ec_trystack;
3062 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3063 + trystack->ga_len - 1;
3064
3065 // Reset the index to avoid a return statement jumps here
3066 // again.
3067 trycmd->tcd_finally_idx = 0;
3068 break;
3069 }
3070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 // end of ":try" block
3072 case ISN_ENDTRY:
3073 {
3074 garray_T *trystack = &ectx.ec_trystack;
3075
3076 if (trystack->ga_len > 0)
3077 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003078 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 --trystack->ga_len;
3081 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02003082 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 trycmd = ((trycmd_T *)trystack->ga_data)
3084 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003085 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 {
3087 // discard the exception
3088 if (caught_stack == current_exception)
3089 caught_stack = caught_stack->caught;
3090 discard_current_exception();
3091 }
3092
3093 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003094 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003095
3096 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
3097 {
3098 --ectx.ec_stack.ga_len;
3099 clear_tv(STACK_TV_BOT(0));
3100 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003101 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003102 // handling :continue: jump to outer try block or
3103 // start of the loop
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003104 ectx.ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 }
3106 }
3107 break;
3108
3109 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003110 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003111 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003112
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003113 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3114 {
3115 // throwing an exception while using "silent!" causes
3116 // the function to abort but not display an error.
3117 tv = STACK_TV_BOT(-1);
3118 clear_tv(tv);
3119 tv->v_type = VAR_NUMBER;
3120 tv->vval.v_number = 0;
3121 goto done;
3122 }
3123 --ectx.ec_stack.ga_len;
3124 tv = STACK_TV_BOT(0);
3125 if (tv->vval.v_string == NULL
3126 || *skipwhite(tv->vval.v_string) == NUL)
3127 {
3128 vim_free(tv->vval.v_string);
3129 SOURCING_LNUM = iptr->isn_lnum;
3130 emsg(_(e_throw_with_empty_string));
3131 goto failed;
3132 }
3133
3134 // Inside a "catch" we need to first discard the caught
3135 // exception.
3136 if (trystack->ga_len > 0)
3137 {
3138 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3139 + trystack->ga_len - 1;
3140 if (trycmd->tcd_caught && current_exception != NULL)
3141 {
3142 // discard the exception
3143 if (caught_stack == current_exception)
3144 caught_stack = caught_stack->caught;
3145 discard_current_exception();
3146 trycmd->tcd_caught = FALSE;
3147 }
3148 }
3149
3150 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3151 == FAIL)
3152 {
3153 vim_free(tv->vval.v_string);
3154 goto failed;
3155 }
3156 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 break;
3159
3160 // compare with special values
3161 case ISN_COMPAREBOOL:
3162 case ISN_COMPARESPECIAL:
3163 {
3164 typval_T *tv1 = STACK_TV_BOT(-2);
3165 typval_T *tv2 = STACK_TV_BOT(-1);
3166 varnumber_T arg1 = tv1->vval.v_number;
3167 varnumber_T arg2 = tv2->vval.v_number;
3168 int res;
3169
3170 switch (iptr->isn_arg.op.op_type)
3171 {
3172 case EXPR_EQUAL: res = arg1 == arg2; break;
3173 case EXPR_NEQUAL: res = arg1 != arg2; break;
3174 default: res = 0; break;
3175 }
3176
3177 --ectx.ec_stack.ga_len;
3178 tv1->v_type = VAR_BOOL;
3179 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3180 }
3181 break;
3182
3183 // Operation with two number arguments
3184 case ISN_OPNR:
3185 case ISN_COMPARENR:
3186 {
3187 typval_T *tv1 = STACK_TV_BOT(-2);
3188 typval_T *tv2 = STACK_TV_BOT(-1);
3189 varnumber_T arg1 = tv1->vval.v_number;
3190 varnumber_T arg2 = tv2->vval.v_number;
3191 varnumber_T res;
3192
3193 switch (iptr->isn_arg.op.op_type)
3194 {
3195 case EXPR_MULT: res = arg1 * arg2; break;
3196 case EXPR_DIV: res = arg1 / arg2; break;
3197 case EXPR_REM: res = arg1 % arg2; break;
3198 case EXPR_SUB: res = arg1 - arg2; break;
3199 case EXPR_ADD: res = arg1 + arg2; break;
3200
3201 case EXPR_EQUAL: res = arg1 == arg2; break;
3202 case EXPR_NEQUAL: res = arg1 != arg2; break;
3203 case EXPR_GREATER: res = arg1 > arg2; break;
3204 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3205 case EXPR_SMALLER: res = arg1 < arg2; break;
3206 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3207 default: res = 0; break;
3208 }
3209
3210 --ectx.ec_stack.ga_len;
3211 if (iptr->isn_type == ISN_COMPARENR)
3212 {
3213 tv1->v_type = VAR_BOOL;
3214 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3215 }
3216 else
3217 tv1->vval.v_number = res;
3218 }
3219 break;
3220
3221 // Computation with two float arguments
3222 case ISN_OPFLOAT:
3223 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003224#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 {
3226 typval_T *tv1 = STACK_TV_BOT(-2);
3227 typval_T *tv2 = STACK_TV_BOT(-1);
3228 float_T arg1 = tv1->vval.v_float;
3229 float_T arg2 = tv2->vval.v_float;
3230 float_T res = 0;
3231 int cmp = FALSE;
3232
3233 switch (iptr->isn_arg.op.op_type)
3234 {
3235 case EXPR_MULT: res = arg1 * arg2; break;
3236 case EXPR_DIV: res = arg1 / arg2; break;
3237 case EXPR_SUB: res = arg1 - arg2; break;
3238 case EXPR_ADD: res = arg1 + arg2; break;
3239
3240 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3241 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3242 case EXPR_GREATER: cmp = arg1 > arg2; break;
3243 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3244 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3245 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3246 default: cmp = 0; break;
3247 }
3248 --ectx.ec_stack.ga_len;
3249 if (iptr->isn_type == ISN_COMPAREFLOAT)
3250 {
3251 tv1->v_type = VAR_BOOL;
3252 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3253 }
3254 else
3255 tv1->vval.v_float = res;
3256 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003257#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 break;
3259
3260 case ISN_COMPARELIST:
3261 {
3262 typval_T *tv1 = STACK_TV_BOT(-2);
3263 typval_T *tv2 = STACK_TV_BOT(-1);
3264 list_T *arg1 = tv1->vval.v_list;
3265 list_T *arg2 = tv2->vval.v_list;
3266 int cmp = FALSE;
3267 int ic = iptr->isn_arg.op.op_ic;
3268
3269 switch (iptr->isn_arg.op.op_type)
3270 {
3271 case EXPR_EQUAL: cmp =
3272 list_equal(arg1, arg2, ic, FALSE); break;
3273 case EXPR_NEQUAL: cmp =
3274 !list_equal(arg1, arg2, ic, FALSE); break;
3275 case EXPR_IS: cmp = arg1 == arg2; break;
3276 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3277 default: cmp = 0; break;
3278 }
3279 --ectx.ec_stack.ga_len;
3280 clear_tv(tv1);
3281 clear_tv(tv2);
3282 tv1->v_type = VAR_BOOL;
3283 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3284 }
3285 break;
3286
3287 case ISN_COMPAREBLOB:
3288 {
3289 typval_T *tv1 = STACK_TV_BOT(-2);
3290 typval_T *tv2 = STACK_TV_BOT(-1);
3291 blob_T *arg1 = tv1->vval.v_blob;
3292 blob_T *arg2 = tv2->vval.v_blob;
3293 int cmp = FALSE;
3294
3295 switch (iptr->isn_arg.op.op_type)
3296 {
3297 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3298 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3299 case EXPR_IS: cmp = arg1 == arg2; break;
3300 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3301 default: cmp = 0; break;
3302 }
3303 --ectx.ec_stack.ga_len;
3304 clear_tv(tv1);
3305 clear_tv(tv2);
3306 tv1->v_type = VAR_BOOL;
3307 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3308 }
3309 break;
3310
3311 // TODO: handle separately
3312 case ISN_COMPARESTRING:
3313 case ISN_COMPAREDICT:
3314 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 case ISN_COMPAREANY:
3316 {
3317 typval_T *tv1 = STACK_TV_BOT(-2);
3318 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003319 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 int ic = iptr->isn_arg.op.op_ic;
3321
Bram Moolenaareb26f432020-09-14 16:50:05 +02003322 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003323 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 --ectx.ec_stack.ga_len;
3326 }
3327 break;
3328
3329 case ISN_ADDLIST:
3330 case ISN_ADDBLOB:
3331 {
3332 typval_T *tv1 = STACK_TV_BOT(-2);
3333 typval_T *tv2 = STACK_TV_BOT(-1);
3334
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003335 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 if (iptr->isn_type == ISN_ADDLIST)
3337 eval_addlist(tv1, tv2);
3338 else
3339 eval_addblob(tv1, tv2);
3340 clear_tv(tv2);
3341 --ectx.ec_stack.ga_len;
3342 }
3343 break;
3344
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003345 case ISN_LISTAPPEND:
3346 {
3347 typval_T *tv1 = STACK_TV_BOT(-2);
3348 typval_T *tv2 = STACK_TV_BOT(-1);
3349 list_T *l = tv1->vval.v_list;
3350
3351 // add an item to a list
3352 if (l == NULL)
3353 {
3354 SOURCING_LNUM = iptr->isn_lnum;
3355 emsg(_(e_cannot_add_to_null_list));
3356 goto on_error;
3357 }
3358 if (list_append_tv(l, tv2) == FAIL)
3359 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003360 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003361 --ectx.ec_stack.ga_len;
3362 }
3363 break;
3364
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003365 case ISN_BLOBAPPEND:
3366 {
3367 typval_T *tv1 = STACK_TV_BOT(-2);
3368 typval_T *tv2 = STACK_TV_BOT(-1);
3369 blob_T *b = tv1->vval.v_blob;
3370 int error = FALSE;
3371 varnumber_T n;
3372
3373 // add a number to a blob
3374 if (b == NULL)
3375 {
3376 SOURCING_LNUM = iptr->isn_lnum;
3377 emsg(_(e_cannot_add_to_null_blob));
3378 goto on_error;
3379 }
3380 n = tv_get_number_chk(tv2, &error);
3381 if (error)
3382 goto on_error;
3383 ga_append(&b->bv_ga, (int)n);
3384 --ectx.ec_stack.ga_len;
3385 }
3386 break;
3387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 // Computation with two arguments of unknown type
3389 case ISN_OPANY:
3390 {
3391 typval_T *tv1 = STACK_TV_BOT(-2);
3392 typval_T *tv2 = STACK_TV_BOT(-1);
3393 varnumber_T n1, n2;
3394#ifdef FEAT_FLOAT
3395 float_T f1 = 0, f2 = 0;
3396#endif
3397 int error = FALSE;
3398
3399 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3400 {
3401 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3402 {
3403 eval_addlist(tv1, tv2);
3404 clear_tv(tv2);
3405 --ectx.ec_stack.ga_len;
3406 break;
3407 }
3408 else if (tv1->v_type == VAR_BLOB
3409 && tv2->v_type == VAR_BLOB)
3410 {
3411 eval_addblob(tv1, tv2);
3412 clear_tv(tv2);
3413 --ectx.ec_stack.ga_len;
3414 break;
3415 }
3416 }
3417#ifdef FEAT_FLOAT
3418 if (tv1->v_type == VAR_FLOAT)
3419 {
3420 f1 = tv1->vval.v_float;
3421 n1 = 0;
3422 }
3423 else
3424#endif
3425 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003426 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 n1 = tv_get_number_chk(tv1, &error);
3428 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003429 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430#ifdef FEAT_FLOAT
3431 if (tv2->v_type == VAR_FLOAT)
3432 f1 = n1;
3433#endif
3434 }
3435#ifdef FEAT_FLOAT
3436 if (tv2->v_type == VAR_FLOAT)
3437 {
3438 f2 = tv2->vval.v_float;
3439 n2 = 0;
3440 }
3441 else
3442#endif
3443 {
3444 n2 = tv_get_number_chk(tv2, &error);
3445 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003446 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447#ifdef FEAT_FLOAT
3448 if (tv1->v_type == VAR_FLOAT)
3449 f2 = n2;
3450#endif
3451 }
3452#ifdef FEAT_FLOAT
3453 // if there is a float on either side the result is a float
3454 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3455 {
3456 switch (iptr->isn_arg.op.op_type)
3457 {
3458 case EXPR_MULT: f1 = f1 * f2; break;
3459 case EXPR_DIV: f1 = f1 / f2; break;
3460 case EXPR_SUB: f1 = f1 - f2; break;
3461 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003462 default: SOURCING_LNUM = iptr->isn_lnum;
3463 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003464 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 }
3466 clear_tv(tv1);
3467 clear_tv(tv2);
3468 tv1->v_type = VAR_FLOAT;
3469 tv1->vval.v_float = f1;
3470 --ectx.ec_stack.ga_len;
3471 }
3472 else
3473#endif
3474 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003475 int failed = FALSE;
3476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 switch (iptr->isn_arg.op.op_type)
3478 {
3479 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003480 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3481 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003482 goto on_error;
3483 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 case EXPR_SUB: n1 = n1 - n2; break;
3485 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003486 default: n1 = num_modulus(n1, n2, &failed);
3487 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003488 goto on_error;
3489 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 }
3491 clear_tv(tv1);
3492 clear_tv(tv2);
3493 tv1->v_type = VAR_NUMBER;
3494 tv1->vval.v_number = n1;
3495 --ectx.ec_stack.ga_len;
3496 }
3497 }
3498 break;
3499
3500 case ISN_CONCAT:
3501 {
3502 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3503 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3504 char_u *res;
3505
3506 res = concat_str(str1, str2);
3507 clear_tv(STACK_TV_BOT(-2));
3508 clear_tv(STACK_TV_BOT(-1));
3509 --ectx.ec_stack.ga_len;
3510 STACK_TV_BOT(-1)->vval.v_string = res;
3511 }
3512 break;
3513
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003514 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003515 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003516 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003517 int is_slice = iptr->isn_type == ISN_STRSLICE;
3518 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003519 char_u *res;
3520
3521 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003522 // string slice: string is at stack-3, first index at
3523 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003524 if (is_slice)
3525 {
3526 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003527 n1 = tv->vval.v_number;
3528 }
3529
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003530 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003531 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003532
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003533 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003534 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003535 if (is_slice)
3536 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003537 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003538 else
3539 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003540 // single character (including composing characters).
3541 // If the index is too big or negative the result is
3542 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003543 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003544 vim_free(tv->vval.v_string);
3545 tv->vval.v_string = res;
3546 }
3547 break;
3548
3549 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003550 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003551 case ISN_BLOBINDEX:
3552 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003554 int is_slice = iptr->isn_type == ISN_LISTSLICE
3555 || iptr->isn_type == ISN_BLOBSLICE;
3556 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3557 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003558 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003559 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560
3561 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003562 // list slice: list is at stack-3, indexes at stack-2 and
3563 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003564 // Same for blob.
3565 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566
3567 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003568 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003570
3571 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 {
Bram Moolenaared591872020-08-15 22:14:53 +02003573 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003574 n1 = tv->vval.v_number;
3575 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 }
Bram Moolenaared591872020-08-15 22:14:53 +02003577
3578 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003579 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003580 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003581 if (is_blob)
3582 {
3583 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3584 n1, n2, FALSE, tv) == FAIL)
3585 goto on_error;
3586 }
3587 else
3588 {
3589 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3590 n1, n2, FALSE, tv, TRUE) == FAIL)
3591 goto on_error;
3592 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 }
3594 break;
3595
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003596 case ISN_ANYINDEX:
3597 case ISN_ANYSLICE:
3598 {
3599 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3600 typval_T *var1, *var2;
3601 int res;
3602
3603 // index: composite is at stack-2, index at stack-1
3604 // slice: composite is at stack-3, indexes at stack-2 and
3605 // stack-1
3606 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003607 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003608 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3609 goto on_error;
3610 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3611 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003612 res = eval_index_inner(tv, is_slice, var1, var2,
3613 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003614 clear_tv(var1);
3615 if (is_slice)
3616 clear_tv(var2);
3617 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3618 if (res == FAIL)
3619 goto on_error;
3620 }
3621 break;
3622
Bram Moolenaar9af78762020-06-16 11:34:42 +02003623 case ISN_SLICE:
3624 {
3625 list_T *list;
3626 int count = iptr->isn_arg.number;
3627
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003628 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003629 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003630 list = tv->vval.v_list;
3631
3632 // no error for short list, expect it to be checked earlier
3633 if (list != NULL && list->lv_len >= count)
3634 {
3635 list_T *newlist = list_slice(list,
3636 count, list->lv_len - 1);
3637
3638 if (newlist != NULL)
3639 {
3640 list_unref(list);
3641 tv->vval.v_list = newlist;
3642 ++newlist->lv_refcount;
3643 }
3644 }
3645 }
3646 break;
3647
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003648 case ISN_GETITEM:
3649 {
3650 listitem_T *li;
3651 int index = iptr->isn_arg.number;
3652
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003653 // Get list item: list is at stack-1, push item.
3654 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003655 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003656 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003657
3658 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3659 goto failed;
3660 ++ectx.ec_stack.ga_len;
3661 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003662
3663 // Useful when used in unpack assignment. Reset at
3664 // ISN_DROP.
3665 where.wt_index = index + 1;
3666 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003667 }
3668 break;
3669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 case ISN_MEMBER:
3671 {
3672 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003673 char_u *key;
3674 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003675 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003676
3677 // dict member: dict is at stack-2, key at stack-1
3678 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003679 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003680 dict = tv->vval.v_dict;
3681
3682 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003683 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003684 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003685 if (key == NULL)
3686 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003687
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003688 if ((di = dict_find(dict, key, -1)) == NULL)
3689 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003690 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003691 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003692
3693 // If :silent! is used we will continue, make sure the
3694 // stack contents makes sense.
3695 clear_tv(tv);
3696 --ectx.ec_stack.ga_len;
3697 tv = STACK_TV_BOT(-1);
3698 clear_tv(tv);
3699 tv->v_type = VAR_NUMBER;
3700 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003701 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003702 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003703 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003704 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003705 // Clear the dict only after getting the item, to avoid
3706 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003707 tv = STACK_TV_BOT(-1);
3708 temp_tv = *tv;
3709 copy_tv(&di->di_tv, tv);
3710 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003711 }
3712 break;
3713
3714 // dict member with string key
3715 case ISN_STRINGMEMBER:
3716 {
3717 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003719 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720
3721 tv = STACK_TV_BOT(-1);
3722 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3723 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003724 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003726 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 }
3728 dict = tv->vval.v_dict;
3729
3730 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3731 == NULL)
3732 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003733 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003735 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003737 // Clear the dict after getting the item, to avoid that it
3738 // make the item invalid.
3739 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003741 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 }
3743 break;
3744
3745 case ISN_NEGATENR:
3746 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003747 if (tv->v_type != VAR_NUMBER
3748#ifdef FEAT_FLOAT
3749 && tv->v_type != VAR_FLOAT
3750#endif
3751 )
3752 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003753 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003754 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003755 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003756 }
3757#ifdef FEAT_FLOAT
3758 if (tv->v_type == VAR_FLOAT)
3759 tv->vval.v_float = -tv->vval.v_float;
3760 else
3761#endif
3762 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 break;
3764
3765 case ISN_CHECKNR:
3766 {
3767 int error = FALSE;
3768
3769 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003770 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003772 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 (void)tv_get_number_chk(tv, &error);
3774 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003775 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 }
3777 break;
3778
3779 case ISN_CHECKTYPE:
3780 {
3781 checktype_T *ct = &iptr->isn_arg.type;
3782
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003783 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003784 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003785 if (!where.wt_variable)
3786 where.wt_index = ct->ct_arg_idx;
3787 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003788 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003789 if (!where.wt_variable)
3790 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003791
3792 // number 0 is FALSE, number 1 is TRUE
3793 if (tv->v_type == VAR_NUMBER
3794 && ct->ct_type->tt_type == VAR_BOOL
3795 && (tv->vval.v_number == 0
3796 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003798 tv->v_type = VAR_BOOL;
3799 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003800 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 }
3802 }
3803 break;
3804
Bram Moolenaar9af78762020-06-16 11:34:42 +02003805 case ISN_CHECKLEN:
3806 {
3807 int min_len = iptr->isn_arg.checklen.cl_min_len;
3808 list_T *list = NULL;
3809
3810 tv = STACK_TV_BOT(-1);
3811 if (tv->v_type == VAR_LIST)
3812 list = tv->vval.v_list;
3813 if (list == NULL || list->lv_len < min_len
3814 || (list->lv_len > min_len
3815 && !iptr->isn_arg.checklen.cl_more_OK))
3816 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003817 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003818 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003819 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003820 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003821 }
3822 }
3823 break;
3824
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003825 case ISN_SETTYPE:
3826 {
3827 checktype_T *ct = &iptr->isn_arg.type;
3828
3829 tv = STACK_TV_BOT(-1);
3830 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3831 {
3832 free_type(tv->vval.v_dict->dv_type);
3833 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3834 }
3835 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3836 {
3837 free_type(tv->vval.v_list->lv_type);
3838 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3839 }
3840 }
3841 break;
3842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003844 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 {
3846 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003847 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848
3849 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003850 if (iptr->isn_type == ISN_2BOOL)
3851 {
3852 n = tv2bool(tv);
3853 if (iptr->isn_arg.number) // invert
3854 n = !n;
3855 }
3856 else
3857 {
3858 SOURCING_LNUM = iptr->isn_lnum;
3859 n = tv_get_bool_chk(tv, &error);
3860 if (error)
3861 goto on_error;
3862 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 clear_tv(tv);
3864 tv->v_type = VAR_BOOL;
3865 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3866 }
3867 break;
3868
3869 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003870 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003871 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003872 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3873 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3874 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 break;
3876
Bram Moolenaar08597872020-12-10 19:43:40 +01003877 case ISN_RANGE:
3878 {
3879 exarg_T ea;
3880 char *errormsg;
3881
Bram Moolenaarece0b872021-01-08 20:40:45 +01003882 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003883 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003884 ea.addr_type = ADDR_LINES;
3885 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003886 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003887 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003888 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003889
3890 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3891 goto failed;
3892 ++ectx.ec_stack.ga_len;
3893 tv = STACK_TV_BOT(-1);
3894 tv->v_type = VAR_NUMBER;
3895 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003896 if (ea.addr_count == 0)
3897 tv->vval.v_number = curwin->w_cursor.lnum;
3898 else
3899 tv->vval.v_number = ea.line2;
3900 }
3901 break;
3902
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003903 case ISN_PUT:
3904 {
3905 int regname = iptr->isn_arg.put.put_regname;
3906 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3907 char_u *expr = NULL;
3908 int dir = FORWARD;
3909
Bram Moolenaar08597872020-12-10 19:43:40 +01003910 if (lnum < -2)
3911 {
3912 // line number was put on the stack by ISN_RANGE
3913 tv = STACK_TV_BOT(-1);
3914 curwin->w_cursor.lnum = tv->vval.v_number;
3915 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3916 dir = BACKWARD;
3917 --ectx.ec_stack.ga_len;
3918 }
3919 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003920 // :put! above cursor
3921 dir = BACKWARD;
3922 else if (lnum >= 0)
3923 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003924
3925 if (regname == '=')
3926 {
3927 tv = STACK_TV_BOT(-1);
3928 if (tv->v_type == VAR_STRING)
3929 expr = tv->vval.v_string;
3930 else
3931 {
3932 expr = typval2string(tv, TRUE); // allocates value
3933 clear_tv(tv);
3934 }
3935 --ectx.ec_stack.ga_len;
3936 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003937 check_cursor();
3938 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3939 vim_free(expr);
3940 }
3941 break;
3942
Bram Moolenaar02194d22020-10-24 23:08:38 +02003943 case ISN_CMDMOD:
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003944 funclocal.floc_save_cmdmod = cmdmod;
3945 funclocal.floc_restore_cmdmod = TRUE;
3946 funclocal.floc_restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003947 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3948 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003949 break;
3950
Bram Moolenaar02194d22020-10-24 23:08:38 +02003951 case ISN_CMDMOD_REV:
3952 // filter regprog is owned by the instruction, don't free it
3953 cmdmod.cmod_filter_regmatch.regprog = NULL;
3954 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003955 cmdmod = funclocal.floc_save_cmdmod;
3956 funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003957 break;
3958
Bram Moolenaar792f7862020-11-23 08:31:18 +01003959 case ISN_UNPACK:
3960 {
3961 int count = iptr->isn_arg.unpack.unp_count;
3962 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3963 list_T *l;
3964 listitem_T *li;
3965 int i;
3966
3967 // Check there is a valid list to unpack.
3968 tv = STACK_TV_BOT(-1);
3969 if (tv->v_type != VAR_LIST)
3970 {
3971 SOURCING_LNUM = iptr->isn_lnum;
3972 emsg(_(e_for_argument_must_be_sequence_of_lists));
3973 goto on_error;
3974 }
3975 l = tv->vval.v_list;
3976 if (l == NULL
3977 || l->lv_len < (semicolon ? count - 1 : count))
3978 {
3979 SOURCING_LNUM = iptr->isn_lnum;
3980 emsg(_(e_list_value_does_not_have_enough_items));
3981 goto on_error;
3982 }
3983 else if (!semicolon && l->lv_len > count)
3984 {
3985 SOURCING_LNUM = iptr->isn_lnum;
3986 emsg(_(e_list_value_has_more_items_than_targets));
3987 goto on_error;
3988 }
3989
3990 CHECK_LIST_MATERIALIZE(l);
3991 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3992 goto failed;
3993 ectx.ec_stack.ga_len += count - 1;
3994
3995 // Variable after semicolon gets a list with the remaining
3996 // items.
3997 if (semicolon)
3998 {
3999 list_T *rem_list =
4000 list_alloc_with_items(l->lv_len - count + 1);
4001
4002 if (rem_list == NULL)
4003 goto failed;
4004 tv = STACK_TV_BOT(-count);
4005 tv->vval.v_list = rem_list;
4006 ++rem_list->lv_refcount;
4007 tv->v_lock = 0;
4008 li = l->lv_first;
4009 for (i = 0; i < count - 1; ++i)
4010 li = li->li_next;
4011 for (i = 0; li != NULL; ++i)
4012 {
4013 list_set_item(rem_list, i, &li->li_tv);
4014 li = li->li_next;
4015 }
4016 --count;
4017 }
4018
4019 // Produce the values in reverse order, first item last.
4020 li = l->lv_first;
4021 for (i = 0; i < count; ++i)
4022 {
4023 tv = STACK_TV_BOT(-i - 1);
4024 copy_tv(&li->li_tv, tv);
4025 li = li->li_next;
4026 }
4027
4028 list_unref(l);
4029 }
4030 break;
4031
Bram Moolenaarb2049902021-01-24 12:53:53 +01004032 case ISN_PROF_START:
4033 case ISN_PROF_END:
4034 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004035#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004036 funccall_T cookie;
4037 ufunc_T *cur_ufunc =
4038 (((dfunc_T *)def_functions.ga_data)
4039 + ectx.ec_dfunc_idx)->df_ufunc;
4040
4041 cookie.func = cur_ufunc;
4042 if (iptr->isn_type == ISN_PROF_START)
4043 {
4044 func_line_start(&cookie, iptr->isn_lnum);
4045 // if we get here the instruction is executed
4046 func_line_exec(&cookie);
4047 }
4048 else
4049 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004050#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004051 }
4052 break;
4053
Bram Moolenaar389df252020-07-09 21:20:47 +02004054 case ISN_SHUFFLE:
4055 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004056 typval_T tmp_tv;
4057 int item = iptr->isn_arg.shuffle.shfl_item;
4058 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004059
4060 tmp_tv = *STACK_TV_BOT(-item);
4061 for ( ; up > 0 && item > 1; --up)
4062 {
4063 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4064 --item;
4065 }
4066 *STACK_TV_BOT(-item) = tmp_tv;
4067 }
4068 break;
4069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 case ISN_DROP:
4071 --ectx.ec_stack.ga_len;
4072 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004073 where.wt_index = 0;
4074 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 break;
4076 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004077 continue;
4078
Bram Moolenaard032f342020-07-18 18:13:02 +02004079func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004080 // Restore previous function. If the frame pointer is where we started
4081 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02004082 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004083 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004084
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004085 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004086 // only fails when out of memory
4087 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004088 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004089
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004090on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004091 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004092 // If "emsg_silent" is set then ignore the error, unless it was set
4093 // when calling the function.
4094 if (did_emsg_cumul + did_emsg == did_emsg_before
4095 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004096 {
4097 // If a sequence of instructions causes an error while ":silent!"
4098 // was used, restore the stack length and jump ahead to restoring
4099 // the cmdmod.
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004100 if (funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004101 {
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004102 while (ectx.ec_stack.ga_len
4103 > funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004104 {
4105 --ectx.ec_stack.ga_len;
4106 clear_tv(STACK_TV_BOT(0));
4107 }
4108 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
4109 ++ectx.ec_iidx;
4110 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004111 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004112 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004113on_fatal_error:
4114 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004115 // If we are not inside a try-catch started here, abort execution.
4116 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004117 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 }
4119
4120done:
4121 // function finished, get result from the stack.
Bram Moolenaar90193e62021-04-04 20:49:50 +02004122 if (ufunc->uf_ret_type == &t_void)
4123 {
4124 rettv->v_type = VAR_VOID;
4125 }
4126 else
4127 {
4128 tv = STACK_TV_BOT(-1);
4129 *rettv = *tv;
4130 tv->v_type = VAR_UNKNOWN;
4131 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 ret = OK;
4133
4134failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004135 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004136 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004137 func_return(&funclocal, &ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004138
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004139 // Deal with any remaining closures, they may be in use somewhere.
4140 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004141 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004142 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004143 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4144 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004145
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004146 estack_pop();
4147 current_sctx = save_current_sctx;
4148
Bram Moolenaarc970e422021-03-17 15:03:04 +01004149 // TODO: when is it safe to delete the function if it is no longer used?
4150 --ufunc->uf_calls;
4151
Bram Moolenaar352134b2020-10-17 22:04:08 +02004152 if (*msg_list != NULL && saved_msg_list != NULL)
4153 {
4154 msglist_T **plist = saved_msg_list;
4155
4156 // Append entries from the current msg_list (uncaught exceptions) to
4157 // the saved msg_list.
4158 while (*plist != NULL)
4159 plist = &(*plist)->next;
4160
4161 *plist = *msg_list;
4162 }
4163 msg_list = saved_msg_list;
4164
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004165 if (funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004166 {
4167 cmdmod.cmod_filter_regmatch.regprog = NULL;
4168 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004169 cmdmod = funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004170 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004171 emsg_silent_def = save_emsg_silent_def;
4172 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004173
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004174failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004175 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4177 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004180 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004181
Bram Moolenaar0186e582021-01-10 18:33:11 +01004182 while (ectx.ec_outer != NULL)
4183 {
4184 outer_T *up = ectx.ec_outer->out_up_is_copy
4185 ? NULL : ectx.ec_outer->out_up;
4186
4187 vim_free(ectx.ec_outer);
4188 ectx.ec_outer = up;
4189 }
4190
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004191 // Not sure if this is necessary.
4192 suppress_errthrow = save_suppress_errthrow;
4193
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004194 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004195 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004196 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004197 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 return ret;
4199}
4200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004202 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01004203 * We don't really need this at runtime, but we do have tests that require it,
4204 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 */
4206 void
4207ex_disassemble(exarg_T *eap)
4208{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01004209 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004210 char_u *fname;
4211 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 dfunc_T *dfunc;
4213 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004214 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 int current;
4216 int line_idx = 0;
4217 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004218 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219
Bram Moolenaarbfd65582020-07-13 18:18:00 +02004220 if (STRNCMP(arg, "<lambda>", 8) == 0)
4221 {
4222 arg += 8;
4223 (void)getdigits(&arg);
4224 fname = vim_strnsave(eap->arg, arg - eap->arg);
4225 }
4226 else
4227 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004228 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01004229 if (fname == NULL)
4230 {
4231 semsg(_(e_invarg2), eap->arg);
4232 return;
4233 }
4234
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004235 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004236 if (ufunc == NULL)
4237 {
4238 char_u *p = untrans_function_name(fname);
4239
4240 if (p != NULL)
4241 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004242 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004243 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004244 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 if (ufunc == NULL)
4246 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004247 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 return;
4249 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01004250 if (func_needs_compiling(ufunc, eap->forceit)
4251 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02004252 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004253 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004255 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 return;
4257 }
4258 if (ufunc->uf_name_exp != NULL)
4259 msg((char *)ufunc->uf_name_exp);
4260 else
4261 msg((char *)ufunc->uf_name);
4262
4263 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004264#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004265 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
4266 instr_count = eap->forceit ? dfunc->df_instr_prof_count
4267 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004268#else
4269 instr = dfunc->df_instr;
4270 instr_count = dfunc->df_instr_count;
4271#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004272 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 {
4274 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004275 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276
4277 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
4278 {
4279 if (current > prev_current)
4280 {
4281 msg_puts("\n\n");
4282 prev_current = current;
4283 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004284 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4285 if (line != NULL)
4286 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 }
4288
4289 switch (iptr->isn_type)
4290 {
4291 case ISN_EXEC:
4292 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
4293 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004294 case ISN_EXECCONCAT:
4295 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004296 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004297 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 case ISN_ECHO:
4299 {
4300 echo_T *echo = &iptr->isn_arg.echo;
4301
4302 smsg("%4d %s %d", current,
4303 echo->echo_with_white ? "ECHO" : "ECHON",
4304 echo->echo_count);
4305 }
4306 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004307 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01004308 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004309 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01004310 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004311 case ISN_ECHOMSG:
4312 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004313 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004314 break;
4315 case ISN_ECHOERR:
4316 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004317 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004318 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004320 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004321 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004322 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004323 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004324 + STACK_FRAME_SIZE));
4325 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004326 smsg("%4d LOAD $%lld", current,
4327 (varnumber_T)(iptr->isn_arg.number));
4328 }
4329 break;
4330 case ISN_LOADOUTER:
4331 {
4332 if (iptr->isn_arg.number < 0)
4333 smsg("%4d LOADOUTER level %d arg[%d]", current,
4334 iptr->isn_arg.outer.outer_depth,
4335 iptr->isn_arg.outer.outer_idx
4336 + STACK_FRAME_SIZE);
4337 else
4338 smsg("%4d LOADOUTER level %d $%d", current,
4339 iptr->isn_arg.outer.outer_depth,
4340 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 break;
4343 case ISN_LOADV:
4344 smsg("%4d LOADV v:%s", current,
4345 get_vim_var_name(iptr->isn_arg.number));
4346 break;
4347 case ISN_LOADSCRIPT:
4348 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004349 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4350 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004352 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004354 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4355 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004356 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004357 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 }
4359 break;
4360 case ISN_LOADS:
4361 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004362 scriptitem_T *si = SCRIPT_ITEM(
4363 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364
4365 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004366 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 }
4368 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004369 case ISN_LOADAUTO:
4370 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4371 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 case ISN_LOADG:
4373 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4374 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004375 case ISN_LOADB:
4376 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4377 break;
4378 case ISN_LOADW:
4379 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4380 break;
4381 case ISN_LOADT:
4382 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4383 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004384 case ISN_LOADGDICT:
4385 smsg("%4d LOAD g:", current);
4386 break;
4387 case ISN_LOADBDICT:
4388 smsg("%4d LOAD b:", current);
4389 break;
4390 case ISN_LOADWDICT:
4391 smsg("%4d LOAD w:", current);
4392 break;
4393 case ISN_LOADTDICT:
4394 smsg("%4d LOAD t:", current);
4395 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 case ISN_LOADOPT:
4397 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4398 break;
4399 case ISN_LOADENV:
4400 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4401 break;
4402 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004403 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 break;
4405
4406 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004407 if (iptr->isn_arg.number < 0)
4408 smsg("%4d STORE arg[%lld]", current,
4409 iptr->isn_arg.number + STACK_FRAME_SIZE);
4410 else
4411 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4412 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004413 case ISN_STOREOUTER:
4414 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004415 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004416 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4417 iptr->isn_arg.outer.outer_depth,
4418 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004419 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004420 smsg("%4d STOREOUTER level %d $%d", current,
4421 iptr->isn_arg.outer.outer_depth,
4422 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004423 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004425 case ISN_STOREV:
4426 smsg("%4d STOREV v:%s", current,
4427 get_vim_var_name(iptr->isn_arg.number));
4428 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004429 case ISN_STOREAUTO:
4430 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4431 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004433 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4434 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004435 case ISN_STOREB:
4436 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4437 break;
4438 case ISN_STOREW:
4439 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4440 break;
4441 case ISN_STORET:
4442 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4443 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004444 case ISN_STORES:
4445 {
4446 scriptitem_T *si = SCRIPT_ITEM(
4447 iptr->isn_arg.loadstore.ls_sid);
4448
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004449 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004450 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004451 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 break;
4453 case ISN_STORESCRIPT:
4454 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004455 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4456 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004458 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004460 smsg("%4d STORESCRIPT %s-%d in %s", current,
4461 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004462 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004463 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 }
4465 break;
4466 case ISN_STOREOPT:
4467 smsg("%4d STOREOPT &%s", current,
4468 iptr->isn_arg.storeopt.so_name);
4469 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004470 case ISN_STOREENV:
4471 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4472 break;
4473 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004474 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004475 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 case ISN_STORENR:
4477 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004478 iptr->isn_arg.storenr.stnr_val,
4479 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 break;
4481
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004482 case ISN_STOREINDEX:
Bram Moolenaar51e93322021-04-17 20:44:56 +02004483 smsg("%4d STOREINDEX %s", current,
4484 vartype_name(iptr->isn_arg.vartype));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004485 break;
4486
Bram Moolenaar68452172021-04-12 21:21:02 +02004487 case ISN_STORERANGE:
4488 smsg("%4d STORERANGE", current);
4489 break;
4490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 // constants
4492 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004493 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004494 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 break;
4496 case ISN_PUSHBOOL:
4497 case ISN_PUSHSPEC:
4498 smsg("%4d PUSH %s", current,
4499 get_var_special_name(iptr->isn_arg.number));
4500 break;
4501 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004502#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004504#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 break;
4506 case ISN_PUSHS:
4507 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4508 break;
4509 case ISN_PUSHBLOB:
4510 {
4511 char_u *r;
4512 char_u numbuf[NUMBUFLEN];
4513 char_u *tofree;
4514
4515 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004516 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 vim_free(tofree);
4518 }
4519 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004520 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004521 {
4522 char *name = (char *)iptr->isn_arg.string;
4523
4524 smsg("%4d PUSHFUNC \"%s\"", current,
4525 name == NULL ? "[none]" : name);
4526 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004527 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004528 case ISN_PUSHCHANNEL:
4529#ifdef FEAT_JOB_CHANNEL
4530 {
4531 channel_T *channel = iptr->isn_arg.channel;
4532
4533 smsg("%4d PUSHCHANNEL %d", current,
4534 channel == NULL ? 0 : channel->ch_id);
4535 }
4536#endif
4537 break;
4538 case ISN_PUSHJOB:
4539#ifdef FEAT_JOB_CHANNEL
4540 {
4541 typval_T tv;
4542 char_u *name;
4543
4544 tv.v_type = VAR_JOB;
4545 tv.vval.v_job = iptr->isn_arg.job;
4546 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004547 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004548 }
4549#endif
4550 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 case ISN_PUSHEXC:
4552 smsg("%4d PUSH v:exception", current);
4553 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004554 case ISN_UNLET:
4555 smsg("%4d UNLET%s %s", current,
4556 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4557 iptr->isn_arg.unlet.ul_name);
4558 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004559 case ISN_UNLETENV:
4560 smsg("%4d UNLETENV%s $%s", current,
4561 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4562 iptr->isn_arg.unlet.ul_name);
4563 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004564 case ISN_UNLETINDEX:
4565 smsg("%4d UNLETINDEX", current);
4566 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004567 case ISN_UNLETRANGE:
4568 smsg("%4d UNLETRANGE", current);
4569 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004570 case ISN_LOCKCONST:
4571 smsg("%4d LOCKCONST", current);
4572 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004574 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004575 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 break;
4577 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004578 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004579 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 break;
4581
4582 // function call
4583 case ISN_BCALL:
4584 {
4585 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4586
4587 smsg("%4d BCALL %s(argc %d)", current,
4588 internal_func_name(cbfunc->cbf_idx),
4589 cbfunc->cbf_argcount);
4590 }
4591 break;
4592 case ISN_DCALL:
4593 {
4594 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4595 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4596 + cdfunc->cdf_idx;
4597
4598 smsg("%4d DCALL %s(argc %d)", current,
4599 df->df_ufunc->uf_name_exp != NULL
4600 ? df->df_ufunc->uf_name_exp
4601 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4602 }
4603 break;
4604 case ISN_UCALL:
4605 {
4606 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4607
4608 smsg("%4d UCALL %s(argc %d)", current,
4609 cufunc->cuf_name, cufunc->cuf_argcount);
4610 }
4611 break;
4612 case ISN_PCALL:
4613 {
4614 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4615
4616 smsg("%4d PCALL%s (argc %d)", current,
4617 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4618 }
4619 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004620 case ISN_PCALL_END:
4621 smsg("%4d PCALL end", current);
4622 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 case ISN_RETURN:
4624 smsg("%4d RETURN", current);
4625 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004626 case ISN_RETURN_ZERO:
4627 smsg("%4d RETURN 0", current);
4628 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 case ISN_FUNCREF:
4630 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004631 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004633 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004635 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 }
4637 break;
4638
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004639 case ISN_NEWFUNC:
4640 {
4641 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4642
4643 smsg("%4d NEWFUNC %s %s", current,
4644 newfunc->nf_lambda, newfunc->nf_global);
4645 }
4646 break;
4647
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004648 case ISN_DEF:
4649 {
4650 char_u *name = iptr->isn_arg.string;
4651
4652 smsg("%4d DEF %s", current,
4653 name == NULL ? (char_u *)"" : name);
4654 }
4655 break;
4656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 case ISN_JUMP:
4658 {
4659 char *when = "?";
4660
4661 switch (iptr->isn_arg.jump.jump_when)
4662 {
4663 case JUMP_ALWAYS:
4664 when = "JUMP";
4665 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 case JUMP_AND_KEEP_IF_TRUE:
4667 when = "JUMP_AND_KEEP_IF_TRUE";
4668 break;
4669 case JUMP_IF_FALSE:
4670 when = "JUMP_IF_FALSE";
4671 break;
4672 case JUMP_AND_KEEP_IF_FALSE:
4673 when = "JUMP_AND_KEEP_IF_FALSE";
4674 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004675 case JUMP_IF_COND_FALSE:
4676 when = "JUMP_IF_COND_FALSE";
4677 break;
4678 case JUMP_IF_COND_TRUE:
4679 when = "JUMP_IF_COND_TRUE";
4680 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004682 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 iptr->isn_arg.jump.jump_where);
4684 }
4685 break;
4686
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004687 case ISN_JUMP_IF_ARG_SET:
4688 smsg("%4d JUMP_IF_ARG_SET arg[%d] -> %d", current,
4689 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
4690 iptr->isn_arg.jump.jump_where);
4691 break;
4692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 case ISN_FOR:
4694 {
4695 forloop_T *forloop = &iptr->isn_arg.forloop;
4696
4697 smsg("%4d FOR $%d -> %d", current,
4698 forloop->for_idx, forloop->for_end);
4699 }
4700 break;
4701
4702 case ISN_TRY:
4703 {
4704 try_T *try = &iptr->isn_arg.try;
4705
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004706 if (try->try_ref->try_finally == 0)
4707 smsg("%4d TRY catch -> %d, endtry -> %d",
4708 current,
4709 try->try_ref->try_catch,
4710 try->try_ref->try_endtry);
4711 else
4712 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4713 current,
4714 try->try_ref->try_catch,
4715 try->try_ref->try_finally,
4716 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 }
4718 break;
4719 case ISN_CATCH:
4720 // TODO
4721 smsg("%4d CATCH", current);
4722 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004723 case ISN_TRYCONT:
4724 {
4725 trycont_T *trycont = &iptr->isn_arg.trycont;
4726
4727 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4728 trycont->tct_levels,
4729 trycont->tct_levels == 1 ? "" : "s",
4730 trycont->tct_where);
4731 }
4732 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004733 case ISN_FINALLY:
4734 smsg("%4d FINALLY", current);
4735 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 case ISN_ENDTRY:
4737 smsg("%4d ENDTRY", current);
4738 break;
4739 case ISN_THROW:
4740 smsg("%4d THROW", current);
4741 break;
4742
4743 // expression operations on number
4744 case ISN_OPNR:
4745 case ISN_OPFLOAT:
4746 case ISN_OPANY:
4747 {
4748 char *what;
4749 char *ins;
4750
4751 switch (iptr->isn_arg.op.op_type)
4752 {
4753 case EXPR_MULT: what = "*"; break;
4754 case EXPR_DIV: what = "/"; break;
4755 case EXPR_REM: what = "%"; break;
4756 case EXPR_SUB: what = "-"; break;
4757 case EXPR_ADD: what = "+"; break;
4758 default: what = "???"; break;
4759 }
4760 switch (iptr->isn_type)
4761 {
4762 case ISN_OPNR: ins = "OPNR"; break;
4763 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4764 case ISN_OPANY: ins = "OPANY"; break;
4765 default: ins = "???"; break;
4766 }
4767 smsg("%4d %s %s", current, ins, what);
4768 }
4769 break;
4770
4771 case ISN_COMPAREBOOL:
4772 case ISN_COMPARESPECIAL:
4773 case ISN_COMPARENR:
4774 case ISN_COMPAREFLOAT:
4775 case ISN_COMPARESTRING:
4776 case ISN_COMPAREBLOB:
4777 case ISN_COMPARELIST:
4778 case ISN_COMPAREDICT:
4779 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 case ISN_COMPAREANY:
4781 {
4782 char *p;
4783 char buf[10];
4784 char *type;
4785
4786 switch (iptr->isn_arg.op.op_type)
4787 {
4788 case EXPR_EQUAL: p = "=="; break;
4789 case EXPR_NEQUAL: p = "!="; break;
4790 case EXPR_GREATER: p = ">"; break;
4791 case EXPR_GEQUAL: p = ">="; break;
4792 case EXPR_SMALLER: p = "<"; break;
4793 case EXPR_SEQUAL: p = "<="; break;
4794 case EXPR_MATCH: p = "=~"; break;
4795 case EXPR_IS: p = "is"; break;
4796 case EXPR_ISNOT: p = "isnot"; break;
4797 case EXPR_NOMATCH: p = "!~"; break;
4798 default: p = "???"; break;
4799 }
4800 STRCPY(buf, p);
4801 if (iptr->isn_arg.op.op_ic == TRUE)
4802 strcat(buf, "?");
4803 switch(iptr->isn_type)
4804 {
4805 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4806 case ISN_COMPARESPECIAL:
4807 type = "COMPARESPECIAL"; break;
4808 case ISN_COMPARENR: type = "COMPARENR"; break;
4809 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4810 case ISN_COMPARESTRING:
4811 type = "COMPARESTRING"; break;
4812 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4813 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4814 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4815 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4817 default: type = "???"; break;
4818 }
4819
4820 smsg("%4d %s %s", current, type, buf);
4821 }
4822 break;
4823
4824 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4825 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4826
4827 // expression operations
4828 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004829 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004830 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004831 case ISN_BLOBINDEX: smsg("%4d BLOBINDEX", current); break;
4832 case ISN_BLOBSLICE: smsg("%4d BLOBSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004833 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004834 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004835 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004836 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004837 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4838 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004839 case ISN_SLICE: smsg("%4d SLICE %lld",
4840 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004841 case ISN_GETITEM: smsg("%4d ITEM %lld",
4842 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004843 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4844 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 iptr->isn_arg.string); break;
4846 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4847
4848 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004849 case ISN_CHECKTYPE:
4850 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004851 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004852 char *tofree;
4853
Bram Moolenaare32e5162021-01-21 20:21:29 +01004854 if (ct->ct_arg_idx == 0)
4855 smsg("%4d CHECKTYPE %s stack[%d]", current,
4856 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004857 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004858 else
4859 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4860 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004861 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004862 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004863 vim_free(tofree);
4864 break;
4865 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004866 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4867 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4868 iptr->isn_arg.checklen.cl_min_len);
4869 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004870 case ISN_SETTYPE:
4871 {
4872 char *tofree;
4873
4874 smsg("%4d SETTYPE %s", current,
4875 type_name(iptr->isn_arg.type.ct_type, &tofree));
4876 vim_free(tofree);
4877 break;
4878 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004879 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 case ISN_2BOOL: if (iptr->isn_arg.number)
4881 smsg("%4d INVERT (!val)", current);
4882 else
4883 smsg("%4d 2BOOL (!!val)", current);
4884 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004885 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004886 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004887 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004888 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004889 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004890 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004891 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4892 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004893 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004894 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4895 smsg("%4d PUT %c above range",
4896 current, iptr->isn_arg.put.put_regname);
4897 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4898 smsg("%4d PUT %c range",
4899 current, iptr->isn_arg.put.put_regname);
4900 else
4901 smsg("%4d PUT %c %ld", current,
4902 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004903 (long)iptr->isn_arg.put.put_lnum);
4904 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905
Bram Moolenaar02194d22020-10-24 23:08:38 +02004906 // TODO: summarize modifiers
4907 case ISN_CMDMOD:
4908 {
4909 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004910 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004911 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4912
4913 buf = alloc(len + 1);
4914 if (buf != NULL)
4915 {
4916 (void)produce_cmdmods(
4917 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4918 smsg("%4d CMDMOD %s", current, buf);
4919 vim_free(buf);
4920 }
4921 break;
4922 }
4923 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004924
Bram Moolenaarb2049902021-01-24 12:53:53 +01004925 case ISN_PROF_START:
4926 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4927 break;
4928
4929 case ISN_PROF_END:
4930 smsg("%4d PROFILE END", current);
4931 break;
4932
Bram Moolenaar792f7862020-11-23 08:31:18 +01004933 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4934 iptr->isn_arg.unpack.unp_count,
4935 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4936 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004937 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4938 iptr->isn_arg.shuffle.shfl_item,
4939 iptr->isn_arg.shuffle.shfl_up);
4940 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 case ISN_DROP: smsg("%4d DROP", current); break;
4942 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004943
4944 out_flush(); // output one line at a time
4945 ui_breakcheck();
4946 if (got_int)
4947 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949}
4950
4951/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004952 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 * list, etc. Mostly like what JavaScript does, except that empty list and
4954 * empty dictionary are FALSE.
4955 */
4956 int
4957tv2bool(typval_T *tv)
4958{
4959 switch (tv->v_type)
4960 {
4961 case VAR_NUMBER:
4962 return tv->vval.v_number != 0;
4963 case VAR_FLOAT:
4964#ifdef FEAT_FLOAT
4965 return tv->vval.v_float != 0.0;
4966#else
4967 break;
4968#endif
4969 case VAR_PARTIAL:
4970 return tv->vval.v_partial != NULL;
4971 case VAR_FUNC:
4972 case VAR_STRING:
4973 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4974 case VAR_LIST:
4975 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4976 case VAR_DICT:
4977 return tv->vval.v_dict != NULL
4978 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4979 case VAR_BOOL:
4980 case VAR_SPECIAL:
4981 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4982 case VAR_JOB:
4983#ifdef FEAT_JOB_CHANNEL
4984 return tv->vval.v_job != NULL;
4985#else
4986 break;
4987#endif
4988 case VAR_CHANNEL:
4989#ifdef FEAT_JOB_CHANNEL
4990 return tv->vval.v_channel != NULL;
4991#else
4992 break;
4993#endif
4994 case VAR_BLOB:
4995 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4996 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004997 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 case VAR_VOID:
4999 break;
5000 }
5001 return FALSE;
5002}
5003
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005004 void
5005emsg_using_string_as(typval_T *tv, int as_number)
5006{
5007 semsg(_(as_number ? e_using_string_as_number_str
5008 : e_using_string_as_bool_str),
5009 tv->vval.v_string == NULL
5010 ? (char_u *)"" : tv->vval.v_string);
5011}
5012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013/*
5014 * If "tv" is a string give an error and return FAIL.
5015 */
5016 int
5017check_not_string(typval_T *tv)
5018{
5019 if (tv->v_type == VAR_STRING)
5020 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005021 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022 clear_tv(tv);
5023 return FAIL;
5024 }
5025 return OK;
5026}
5027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028
5029#endif // FEAT_EVAL