blob: fc195337b0fbf386b847a41f08cc4d430bcde233 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010029 int tcd_catch_idx; // instruction of the first catch
Bram Moolenaarc150c092021-02-13 15:02:46 +010030 int tcd_finally_idx; // instruction of the finally block or :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010031 int tcd_caught; // catch block entered
Bram Moolenaarc150c092021-02-13 15:02:46 +010032 int tcd_cont; // :continue encountered, jump here
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033 int tcd_return; // when TRUE return from end of :finally
34} trycmd_T;
35
36
37// A stack is used to store:
38// - arguments passed to a :def function
39// - info about the calling function, to use when returning
40// - local variables
41// - temporary values
42//
43// In detail (FP == Frame Pointer):
44// arg1 first argument from caller (if present)
45// arg2 second argument from caller (if present)
46// extra_arg1 any missing optional argument default value
47// FP -> cur_func calling function
48// current previous instruction pointer
49// frame_ptr previous Frame Pointer
50// var1 space for local variable
51// var2 space for local variable
52// .... fixed space for max. number of local variables
53// temp temporary values
54// .... flexible space for temporary values (can grow big)
55
56/*
57 * Execution context.
58 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010059struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020061 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010062
Bram Moolenaar0186e582021-01-10 18:33:11 +010063 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010065 garray_T ec_trystack; // stack of trycmd_T values
66 int ec_in_catch; // when TRUE in catch or finally block
67
68 int ec_dfunc_idx; // current function index
69 isn_T *ec_instr; // array with instructions
70 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020071
72 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010073};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
Bram Moolenaar12d26532021-02-19 19:13:21 +010075#ifdef FEAT_PROFILE
76// stack of profinfo_T used when profiling.
77static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
78#endif
79
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020081#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaar418f1df2020-08-12 21:34:49 +020083 void
84to_string_error(vartype_T vartype)
85{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020086 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020087}
88
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010089/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010090 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010091 */
92 static int
93ufunc_argcount(ufunc_T *ufunc)
94{
95 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
96}
97
98/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010099 * Set the instruction index, depending on omitted arguments, where the default
100 * values are to be computed. If all optional arguments are present, start
101 * with the function body.
102 * The expression evaluation is at the start of the instructions:
103 * 0 -> EVAL default1
104 * STORE arg[-2]
105 * 1 -> EVAL default2
106 * STORE arg[-1]
107 * 2 -> function body
108 */
109 static void
110init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
111{
112 if (ufunc->uf_def_args.ga_len == 0)
113 ectx->ec_iidx = 0;
114 else
115 {
116 int defcount = ufunc->uf_args.ga_len - argcount;
117
118 // If there is a varargs argument defcount can be negative, no defaults
119 // to evaluate then.
120 if (defcount < 0)
121 defcount = 0;
122 ectx->ec_iidx = ufunc->uf_def_arg_idx[
123 ufunc->uf_def_args.ga_len - defcount];
124 }
125}
126
127/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200128 * Create a new list from "count" items at the bottom of the stack.
129 * When "count" is zero an empty list is added to the stack.
130 */
131 static int
132exe_newlist(int count, ectx_T *ectx)
133{
134 list_T *list = list_alloc_with_items(count);
135 int idx;
136 typval_T *tv;
137
138 if (list == NULL)
139 return FAIL;
140 for (idx = 0; idx < count; ++idx)
141 list_set_item(list, idx, STACK_TV_BOT(idx - count));
142
143 if (count > 0)
144 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200145 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200146 return FAIL;
147 else
148 ++ectx->ec_stack.ga_len;
149 tv = STACK_TV_BOT(-1);
150 tv->v_type = VAR_LIST;
151 tv->vval.v_list = list;
152 ++list->lv_refcount;
153 return OK;
154}
155
156/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100158 * This adds a stack frame and sets the instruction pointer to the start of the
159 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100160 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161 *
162 * Stack has:
163 * - current arguments (already there)
164 * - omitted optional argument (default values) added here
165 * - stack frame:
166 * - pointer to calling function
167 * - Index of next instruction in calling function
168 * - previous frame pointer
169 * - reserved space for local variables
170 */
171 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100172call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200174 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
176 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200177 int arg_to_add;
178 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200179 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100180 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200181 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182
183 if (dfunc->df_deleted)
184 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100185 // don't use ufunc->uf_name, it may have been freed
186 emsg_funcname(e_func_deleted,
187 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188 return FAIL;
189 }
190
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100191#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100192 if (do_profiling == PROF_YES)
193 {
194 if (ga_grow(&profile_info_ga, 1) == OK)
195 {
196 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
197 + profile_info_ga.ga_len;
198 ++profile_info_ga.ga_len;
199 CLEAR_POINTER(info);
200 profile_may_start_func(info, ufunc,
201 (((dfunc_T *)def_functions.ga_data)
202 + ectx->ec_dfunc_idx)->df_ufunc);
203 }
204
205 // Profiling might be enabled/disabled along the way. This should not
206 // fail, since the function was compiled before and toggling profiling
207 // doesn't change any errors.
208 if (func_needs_compiling(ufunc, PROFILING(ufunc))
209 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100210 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100211 return FAIL;
212 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100213#endif
214
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200215 if (ufunc->uf_va_name != NULL)
216 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200217 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200218 // Stack at time of call with 2 varargs:
219 // normal_arg
220 // optional_arg
221 // vararg_1
222 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200223 // After creating the list:
224 // normal_arg
225 // optional_arg
226 // vararg-list
227 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200228 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200229 // After creating the list
230 // normal_arg
231 // (space for optional_arg)
232 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200233 vararg_count = argcount - ufunc->uf_args.ga_len;
234 if (vararg_count < 0)
235 vararg_count = 0;
236 else
237 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200238 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200239 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200240
241 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200242 }
243
Bram Moolenaarfe270812020-04-11 22:31:27 +0200244 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200245 if (arg_to_add < 0)
246 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200247 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200248 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200249 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200250 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200251 return FAIL;
252 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200253
254 // Reserve space for:
255 // - missing arguments
256 // - stack frame
257 // - local variables
258 // - if needed: a counter for number of closures created in
259 // ectx->ec_funcrefs.
260 varcount = dfunc->df_varcount + dfunc->df_has_closure;
261 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
262 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100263 return FAIL;
264
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100265 // If depth of calling is getting too high, don't execute the function.
266 if (funcdepth_increment() == FAIL)
267 return FAIL;
268
Bram Moolenaarfe270812020-04-11 22:31:27 +0200269 // Move the vararg-list to below the missing optional arguments.
270 if (vararg_count > 0 && arg_to_add > 0)
271 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100272
273 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200274 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200275 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200276 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277
278 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100279 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
280 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100281 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
282 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200283 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284
285 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200286 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200288 if (dfunc->df_has_closure)
289 {
290 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
291
292 tv->v_type = VAR_NUMBER;
293 tv->vval.v_number = 0;
294 }
295 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100297 if (pt != NULL || ufunc->uf_partial != NULL
298 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100299 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100300 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
301
302 if (outer == NULL)
303 return FAIL;
304 if (pt != NULL)
305 {
306 *outer = pt->pt_outer;
307 outer->out_up_is_copy = TRUE;
308 }
309 else if (ufunc->uf_partial != NULL)
310 {
311 *outer = ufunc->uf_partial->pt_outer;
312 outer->out_up_is_copy = TRUE;
313 }
314 else
315 {
316 outer->out_stack = &ectx->ec_stack;
317 outer->out_frame_idx = ectx->ec_frame_idx;
318 outer->out_up = ectx->ec_outer;
319 }
320 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100321 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100322 else
323 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325 // Set execution state to the start of the called function.
326 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100327 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100328 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200329 if (entry != NULL)
330 {
331 // Set the script context to the script where the function was defined.
332 // TODO: save more than the SID?
333 entry->es_save_sid = current_sctx.sc_sid;
334 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
335 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100336
337 // Decide where to start execution, handles optional arguments.
338 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100339
340 return OK;
341}
342
343// Get pointer to item in the stack.
344#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
345
346/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200347 * Used when returning from a function: Check if any closure is still
348 * referenced. If so then move the arguments and variables to a separate piece
349 * of stack to be used when the closure is called.
350 * When "free_arguments" is TRUE the arguments are to be freed.
351 * Returns FAIL when out of memory.
352 */
353 static int
354handle_closure_in_use(ectx_T *ectx, int free_arguments)
355{
356 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
357 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200358 int argcount;
359 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200360 int idx;
361 typval_T *tv;
362 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200363 garray_T *gap = &ectx->ec_funcrefs;
364 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200365
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200366 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200367 return OK; // function was freed
368 if (dfunc->df_has_closure == 0)
369 return OK; // no closures
370 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
371 closure_count = tv->vval.v_number;
372 if (closure_count == 0)
373 return OK; // no funcrefs created
374
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200375 argcount = ufunc_argcount(dfunc->df_ufunc);
376 top = ectx->ec_frame_idx - argcount;
377
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200378 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200379 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200380 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200381 partial_T *pt;
382 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200383
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200384 if (off < 0)
385 continue; // count is off or already done
386 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200387 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200388 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200389 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200390 int i;
391
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200392 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200393 // unreferenced on return.
394 for (i = 0; i < dfunc->df_varcount; ++i)
395 {
396 typval_T *stv = STACK_TV(ectx->ec_frame_idx
397 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200398 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200399 --refcount;
400 }
401 if (refcount > 1)
402 {
403 closure_in_use = TRUE;
404 break;
405 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200406 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200407 }
408
409 if (closure_in_use)
410 {
411 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
412 typval_T *stack;
413
414 // A closure is using the arguments and/or local variables.
415 // Move them to the called function.
416 if (funcstack == NULL)
417 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200418 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
419 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200420 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
421 funcstack->fs_ga.ga_data = stack;
422 if (stack == NULL)
423 {
424 vim_free(funcstack);
425 return FAIL;
426 }
427
428 // Move or copy the arguments.
429 for (idx = 0; idx < argcount; ++idx)
430 {
431 tv = STACK_TV(top + idx);
432 if (free_arguments)
433 {
434 *(stack + idx) = *tv;
435 tv->v_type = VAR_UNKNOWN;
436 }
437 else
438 copy_tv(tv, stack + idx);
439 }
440 // Move the local variables.
441 for (idx = 0; idx < dfunc->df_varcount; ++idx)
442 {
443 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200444
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200445 // A partial created for a local function, that is also used as a
446 // local variable, has a reference count for the variable, thus
447 // will never go down to zero. When all these refcounts are one
448 // then the funcstack is unused. We need to count how many we have
449 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200450 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
451 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200452 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200453
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200454 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200455 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
456 gap->ga_len - closure_count + i])
457 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200458 }
459
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200460 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200461 tv->v_type = VAR_UNKNOWN;
462 }
463
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200464 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200465 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200466 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
467 - closure_count + idx];
468 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200469 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200470 ++funcstack->fs_refcount;
471 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100472 pt->pt_outer.out_stack = &funcstack->fs_ga;
473 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
474 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200475 }
476 }
477 }
478
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200479 for (idx = 0; idx < closure_count; ++idx)
480 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
481 - closure_count + idx]);
482 gap->ga_len -= closure_count;
483 if (gap->ga_len == 0)
484 ga_clear(gap);
485
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200486 return OK;
487}
488
489/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200490 * Called when a partial is freed or its reference count goes down to one. The
491 * funcstack may be the only reference to the partials in the local variables.
492 * Go over all of them, the funcref and can be freed if all partials
493 * referencing the funcstack have a reference count of one.
494 */
495 void
496funcstack_check_refcount(funcstack_T *funcstack)
497{
498 int i;
499 garray_T *gap = &funcstack->fs_ga;
500 int done = 0;
501
502 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
503 return;
504 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
505 {
506 typval_T *tv = ((typval_T *)gap->ga_data) + i;
507
508 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
509 && tv->vval.v_partial->pt_funcstack == funcstack
510 && tv->vval.v_partial->pt_refcount == 1)
511 ++done;
512 }
513 if (done == funcstack->fs_min_refcount)
514 {
515 typval_T *stack = gap->ga_data;
516
517 // All partials referencing the funcstack have a reference count of
518 // one, thus the funcstack is no longer of use.
519 for (i = 0; i < gap->ga_len; ++i)
520 clear_tv(stack + i);
521 vim_free(stack);
522 vim_free(funcstack);
523 }
524}
525
526/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 * Return from the current function.
528 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200529 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530func_return(ectx_T *ectx)
531{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100533 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200534 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
535 + ectx->ec_dfunc_idx;
536 int argcount = ufunc_argcount(dfunc->df_ufunc);
537 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200538 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100539 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
540 + STACK_FRAME_FUNC_OFF)->vval.v_number;
541 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
542 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543
Bram Moolenaar12d26532021-02-19 19:13:21 +0100544#ifdef FEAT_PROFILE
545 if (do_profiling == PROF_YES)
546 {
547 ufunc_T *caller = prev_dfunc->df_ufunc;
548
549 if (dfunc->df_ufunc->uf_profiling
550 || (caller != NULL && caller->uf_profiling))
551 {
552 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
553 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
554 --profile_info_ga.ga_len;
555 }
556 }
557#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200559 entry = estack_pop();
560 if (entry != NULL)
561 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200563 if (handle_closure_in_use(ectx, TRUE) == FAIL)
564 return FAIL;
565
566 // Clear the arguments.
567 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
568 clear_tv(STACK_TV(idx));
569
570 // Clear local variables and temp values, but not the return value.
571 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100572 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100574
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100575 // The return value should be on top of the stack. However, when aborting
576 // it may not be there and ec_frame_idx is the top of the stack.
577 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100578 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100579 ret_idx = 0;
580
Bram Moolenaar0186e582021-01-10 18:33:11 +0100581 vim_free(ectx->ec_outer);
582
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100583 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100584 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100585 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
586 + STACK_FRAME_IIDX_OFF)->vval.v_number;
587 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
588 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200589 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100590 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
591 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100592 ectx->ec_instr = INSTRUCTIONS(prev_dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100593
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100594 if (ret_idx > 0)
595 {
596 // Reset the stack to the position before the call, with a spot for the
597 // return value, moved there from above the frame.
598 ectx->ec_stack.ga_len = top + 1;
599 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
600 }
601 else
602 // Reset the stack to the position before the call.
603 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200604
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100605 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200606 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607}
608
609#undef STACK_TV
610
611/*
612 * Prepare arguments and rettv for calling a builtin or user function.
613 */
614 static int
615call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
616{
617 int idx;
618 typval_T *tv;
619
620 // Move arguments from bottom of the stack to argvars[] and add terminator.
621 for (idx = 0; idx < argcount; ++idx)
622 argvars[idx] = *STACK_TV_BOT(idx - argcount);
623 argvars[argcount].v_type = VAR_UNKNOWN;
624
625 // Result replaces the arguments on the stack.
626 if (argcount > 0)
627 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200628 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 return FAIL;
630 else
631 ++ectx->ec_stack.ga_len;
632
633 // Default return value is zero.
634 tv = STACK_TV_BOT(-1);
635 tv->v_type = VAR_NUMBER;
636 tv->vval.v_number = 0;
637
638 return OK;
639}
640
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200641// Ugly global to avoid passing the execution context around through many
642// layers.
643static ectx_T *current_ectx = NULL;
644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645/*
646 * Call a builtin function by index.
647 */
648 static int
649call_bfunc(int func_idx, int argcount, ectx_T *ectx)
650{
651 typval_T argvars[MAX_FUNC_ARGS];
652 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100653 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200654 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655
656 if (call_prepare(argcount, argvars, ectx) == FAIL)
657 return FAIL;
658
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200659 // Call the builtin function. Set "current_ectx" so that when it
660 // recursively invokes call_def_function() a closure context can be set.
661 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100662 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200663 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664
665 // Clear the arguments.
666 for (idx = 0; idx < argcount; ++idx)
667 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200668
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100669 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200670 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 return OK;
672}
673
674/*
675 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100676 * If the function is compiled this will add a stack frame and set the
677 * instruction pointer at the start of the function.
678 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100679 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100680 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 */
682 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100683call_ufunc(
684 ufunc_T *ufunc,
685 partial_T *pt,
686 int argcount,
687 ectx_T *ectx,
688 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689{
690 typval_T argvars[MAX_FUNC_ARGS];
691 funcexe_T funcexe;
692 int error;
693 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100694 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100695#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100696 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100697#else
698# define profiling FALSE
699#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700
Bram Moolenaarb2049902021-01-24 12:53:53 +0100701 if (func_needs_compiling(ufunc, profiling)
702 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200703 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200704 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100705 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100706 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100707 if (error != FCERR_UNKNOWN)
708 {
709 if (error == FCERR_TOOMANY)
710 semsg(_(e_toomanyarg), ufunc->uf_name);
711 else
712 semsg(_(e_toofewarg), ufunc->uf_name);
713 return FAIL;
714 }
715
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100716 // The function has been compiled, can call it quickly. For a function
717 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100718 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100719 if (iptr != NULL)
720 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100721 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100722 iptr->isn_type = ISN_DCALL;
723 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
724 iptr->isn_arg.dfunc.cdf_argcount = argcount;
725 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100726 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728
729 if (call_prepare(argcount, argvars, ectx) == FAIL)
730 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200731 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732 funcexe.evaluate = TRUE;
733
734 // Call the user function. Result goes in last position on the stack.
735 // TODO: add selfdict if there is one
736 error = call_user_func_check(ufunc, argcount, argvars,
737 STACK_TV_BOT(-1), &funcexe, NULL);
738
739 // Clear the arguments.
740 for (idx = 0; idx < argcount; ++idx)
741 clear_tv(&argvars[idx]);
742
743 if (error != FCERR_NONE)
744 {
745 user_func_error(error, ufunc->uf_name);
746 return FAIL;
747 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100748 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200749 // Error other than from calling the function itself.
750 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751 return OK;
752}
753
754/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200755 * Return TRUE if an error was given or CTRL-C was pressed.
756 */
757 static int
758vim9_aborting(int prev_called_emsg)
759{
760 return called_emsg > prev_called_emsg || got_int || did_throw;
761}
762
763/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 * Execute a function by "name".
765 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100766 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 * Returns FAIL if not found without an error message.
768 */
769 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100770call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771{
772 ufunc_T *ufunc;
773
774 if (builtin_function(name, -1))
775 {
776 int func_idx = find_internal_func(name);
777
778 if (func_idx < 0)
779 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200780 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 return FAIL;
782 return call_bfunc(func_idx, argcount, ectx);
783 }
784
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200785 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200786
787 if (ufunc == NULL)
788 {
789 int called_emsg_before = called_emsg;
790
791 if (script_autoload(name, TRUE))
792 // loaded a package, search for the function again
793 ufunc = find_func(name, FALSE, NULL);
794 if (vim9_aborting(called_emsg_before))
795 return FAIL; // bail out if loading the script caused an error
796 }
797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100799 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800
801 return FAIL;
802}
803
804 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200805call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200807 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200808 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100810 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811
812 if (tv->v_type == VAR_PARTIAL)
813 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200814 partial_T *pt = tv->vval.v_partial;
815 int i;
816
817 if (pt->pt_argc > 0)
818 {
819 // Make space for arguments from the partial, shift the "argcount"
820 // arguments up.
821 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
822 return FAIL;
823 for (i = 1; i <= argcount; ++i)
824 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
825 ectx->ec_stack.ga_len += pt->pt_argc;
826 argcount += pt->pt_argc;
827
828 // copy the arguments from the partial onto the stack
829 for (i = 0; i < pt->pt_argc; ++i)
830 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
831 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832
833 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100834 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 name = pt->pt_name;
837 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200838 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200840 if (name != NULL)
841 {
842 char_u fname_buf[FLEN_FIXED + 1];
843 char_u *tofree = NULL;
844 int error = FCERR_NONE;
845 char_u *fname;
846
847 // May need to translate <SNR>123_ to K_SNR.
848 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
849 if (error != FCERR_NONE)
850 res = FAIL;
851 else
852 res = call_by_name(fname, argcount, ectx, NULL);
853 vim_free(tofree);
854 }
855
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100856 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 {
858 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200859 semsg(_(e_unknownfunc),
860 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 return FAIL;
862 }
863 return OK;
864}
865
866/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200867 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
868 * TRUE.
869 */
870 static int
871error_if_locked(int lock, char *error)
872{
873 if (lock & (VAR_LOCKED | VAR_FIXED))
874 {
875 emsg(_(error));
876 return TRUE;
877 }
878 return FALSE;
879}
880
881/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100882 * Store "tv" in variable "name".
883 * This is for s: and g: variables.
884 */
885 static void
886store_var(char_u *name, typval_T *tv)
887{
888 funccal_entry_T entry;
889
890 save_funccal(&entry);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100891 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100892 restore_funccal();
893}
894
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100895/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100896 * Convert "tv" to a string.
897 * Return FAIL if not allowed.
898 */
899 static int
900do_2string(typval_T *tv, int is_2string_any)
901{
902 if (tv->v_type != VAR_STRING)
903 {
904 char_u *str;
905
906 if (is_2string_any)
907 {
908 switch (tv->v_type)
909 {
910 case VAR_SPECIAL:
911 case VAR_BOOL:
912 case VAR_NUMBER:
913 case VAR_FLOAT:
914 case VAR_BLOB: break;
915 default: to_string_error(tv->v_type);
916 return FAIL;
917 }
918 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100919 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100920 clear_tv(tv);
921 tv->v_type = VAR_STRING;
922 tv->vval.v_string = str;
923 }
924 return OK;
925}
926
927/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100928 * When the value of "sv" is a null list of dict, allocate it.
929 */
930 static void
931allocate_if_null(typval_T *tv)
932{
933 switch (tv->v_type)
934 {
935 case VAR_LIST:
936 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100937 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100938 break;
939 case VAR_DICT:
940 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100941 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100942 break;
943 default:
944 break;
945 }
946}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200947
Bram Moolenaare7525c52021-01-09 13:20:37 +0100948/*
949 * Return the character "str[index]" where "index" is the character index. If
950 * "index" is out of range NULL is returned.
951 */
952 char_u *
953char_from_string(char_u *str, varnumber_T index)
954{
955 size_t nbyte = 0;
956 varnumber_T nchar = index;
957 size_t slen;
958
959 if (str == NULL)
960 return NULL;
961 slen = STRLEN(str);
962
963 // do the same as for a list: a negative index counts from the end
964 if (index < 0)
965 {
966 int clen = 0;
967
968 for (nbyte = 0; nbyte < slen; ++clen)
969 nbyte += MB_CPTR2LEN(str + nbyte);
970 nchar = clen + index;
971 if (nchar < 0)
972 // unlike list: index out of range results in empty string
973 return NULL;
974 }
975
976 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
977 nbyte += MB_CPTR2LEN(str + nbyte);
978 if (nbyte >= slen)
979 return NULL;
980 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
981}
982
983/*
984 * Get the byte index for character index "idx" in string "str" with length
985 * "str_len".
986 * If going over the end return "str_len".
987 * If "idx" is negative count from the end, -1 is the last character.
988 * When going over the start return -1.
989 */
990 static long
991char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
992{
993 varnumber_T nchar = idx;
994 size_t nbyte = 0;
995
996 if (nchar >= 0)
997 {
998 while (nchar > 0 && nbyte < str_len)
999 {
1000 nbyte += MB_CPTR2LEN(str + nbyte);
1001 --nchar;
1002 }
1003 }
1004 else
1005 {
1006 nbyte = str_len;
1007 while (nchar < 0 && nbyte > 0)
1008 {
1009 --nbyte;
1010 nbyte -= mb_head_off(str, str + nbyte);
1011 ++nchar;
1012 }
1013 if (nchar < 0)
1014 return -1;
1015 }
1016 return (long)nbyte;
1017}
1018
1019/*
1020 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001021 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001022 * Return NULL when the result is empty.
1023 */
1024 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001025string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001026{
1027 long start_byte, end_byte;
1028 size_t slen;
1029
1030 if (str == NULL)
1031 return NULL;
1032 slen = STRLEN(str);
1033 start_byte = char_idx2byte(str, slen, first);
1034 if (start_byte < 0)
1035 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001036 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001037 end_byte = (long)slen;
1038 else
1039 {
1040 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001041 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001042 // end index is inclusive
1043 end_byte += MB_CPTR2LEN(str + end_byte);
1044 }
1045
1046 if (start_byte >= (long)slen || end_byte <= start_byte)
1047 return NULL;
1048 return vim_strnsave(str + start_byte, end_byte - start_byte);
1049}
1050
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001051 static svar_T *
1052get_script_svar(scriptref_T *sref, ectx_T *ectx)
1053{
1054 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1055 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1056 + ectx->ec_dfunc_idx;
1057 svar_T *sv;
1058
1059 if (sref->sref_seq != si->sn_script_seq)
1060 {
1061 // The script was reloaded after the function was
1062 // compiled, the script_idx may not be valid.
1063 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1064 dfunc->df_ufunc->uf_name_exp);
1065 return NULL;
1066 }
1067 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1068 if (!equal_type(sv->sv_type, sref->sref_type))
1069 {
1070 emsg(_(e_script_variable_type_changed));
1071 return NULL;
1072 }
1073 return sv;
1074}
1075
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001076/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 * Execute a function by "name".
1078 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001079 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 */
1081 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001082call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083{
Bram Moolenaared677f52020-08-12 16:38:10 +02001084 int called_emsg_before = called_emsg;
1085 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086
Bram Moolenaared677f52020-08-12 16:38:10 +02001087 res = call_by_name(name, argcount, ectx, iptr);
1088 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001090 dictitem_T *v;
1091
1092 v = find_var(name, NULL, FALSE);
1093 if (v == NULL)
1094 {
1095 semsg(_(e_unknownfunc), name);
1096 return FAIL;
1097 }
1098 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1099 {
1100 semsg(_(e_unknownfunc), name);
1101 return FAIL;
1102 }
1103 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001105 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106}
1107
1108/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001109 * When a function reference is used, fill a partial with the information
1110 * needed, especially when it is used as a closure.
1111 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001112 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001113fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1114{
1115 pt->pt_func = ufunc;
1116 pt->pt_refcount = 1;
1117
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001118 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001119 {
1120 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1121 + ectx->ec_dfunc_idx;
1122
1123 // The closure needs to find arguments and local
1124 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001125 pt->pt_outer.out_stack = &ectx->ec_stack;
1126 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1127 pt->pt_outer.out_up = ectx->ec_outer;
1128 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001129
1130 // If this function returns and the closure is still
1131 // being used, we need to make a copy of the context
1132 // (arguments and local variables). Store a reference
1133 // to the partial so we can handle that.
1134 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1135 {
1136 vim_free(pt);
1137 return FAIL;
1138 }
1139 // Extra variable keeps the count of closures created
1140 // in the current function call.
1141 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1142 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1143
1144 ((partial_T **)ectx->ec_funcrefs.ga_data)
1145 [ectx->ec_funcrefs.ga_len] = pt;
1146 ++pt->pt_refcount;
1147 ++ectx->ec_funcrefs.ga_len;
1148 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001149 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001150 return OK;
1151}
1152
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001153
Bram Moolenaarf112f302020-12-20 17:47:52 +01001154/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 * Call a "def" function from old Vim script.
1156 * Return OK or FAIL.
1157 */
1158 int
1159call_def_function(
1160 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001161 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001163 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164 typval_T *rettv) // return value
1165{
1166 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001167 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001168 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169 typval_T *tv;
1170 int idx;
1171 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001172 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001173 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001174 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001175 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001176 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001177 msglist_T **saved_msg_list = NULL;
1178 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001179 cmdmod_T save_cmdmod;
1180 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001181 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001182 int save_emsg_silent_def = emsg_silent_def;
1183 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001184 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001185 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001186 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187
1188// Get pointer to item in the stack.
1189#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1190
1191// Get pointer to item at the bottom of the stack, -1 is the bottom.
1192#undef STACK_TV_BOT
1193#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1194
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001195// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001196#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 +01001197
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001198 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001199 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1200 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001201 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001202 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001203 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001204 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001205 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001206 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001207 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001208
Bram Moolenaar09689a02020-05-09 22:50:08 +02001209 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001210 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001211 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1212 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001213 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001214 {
1215 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001216 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001217 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001220 // If depth of calling is getting too high, don't execute the function.
1221 orig_funcdepth = funcdepth_get();
1222 if (funcdepth_increment() == FAIL)
1223 return FAIL;
1224
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001225 CLEAR_FIELD(ectx);
1226 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1227 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1228 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001229 {
1230 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001231 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001234 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001236 // Put arguments on the stack, but no more than what the function expects.
1237 // A lambda can be called with more arguments than it uses.
1238 for (idx = 0; idx < argc
1239 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1240 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001242 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001243 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001244 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001245 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 copy_tv(&argv[idx], STACK_TV_BOT(0));
1247 ++ectx.ec_stack.ga_len;
1248 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001249
1250 // Turn varargs into a list. Empty list if no args.
1251 if (ufunc->uf_va_name != NULL)
1252 {
1253 int vararg_count = argc - ufunc->uf_args.ga_len;
1254
1255 if (vararg_count < 0)
1256 vararg_count = 0;
1257 else
1258 argc -= vararg_count;
1259 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001260 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001261
1262 // Check the type of the list items.
1263 tv = STACK_TV_BOT(-1);
1264 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001265 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001266 && ufunc->uf_va_type->tt_member != &t_any
1267 && tv->vval.v_list != NULL)
1268 {
1269 type_T *expected = ufunc->uf_va_type->tt_member;
1270 listitem_T *li = tv->vval.v_list->lv_first;
1271
1272 for (idx = 0; idx < vararg_count; ++idx)
1273 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001274 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001275 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001276 goto failed_early;
1277 li = li->li_next;
1278 }
1279 }
1280
Bram Moolenaar23e03252020-04-12 22:22:31 +02001281 if (defcount > 0)
1282 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001283 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001284 --ectx.ec_stack.ga_len;
1285 }
1286
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001287 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001288 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001289 if (defcount > 0)
1290 for (idx = 0; idx < defcount; ++idx)
1291 {
1292 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1293 ++ectx.ec_stack.ga_len;
1294 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001295 if (ufunc->uf_va_name != NULL)
1296 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297
1298 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001299 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1300 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001302 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001303 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1304 + ufunc->uf_dfunc_idx;
1305 ufunc_T *base_ufunc = dfunc->df_ufunc;
1306
1307 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1308 // by copy_func().
1309 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001310 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001311 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1312 if (ectx.ec_outer == NULL)
1313 goto failed_early;
1314 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001315 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001316 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1317 {
1318 if (current_ectx->ec_outer != NULL)
1319 *ectx.ec_outer = *current_ectx->ec_outer;
1320 }
1321 else
1322 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001323 }
1324 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001325 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1326 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001327 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001328 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 // dummy frame entries
1331 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1332 {
1333 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1334 ++ectx.ec_stack.ga_len;
1335 }
1336
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001337 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001338 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001339 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1340 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001342 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001343 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001344 ectx.ec_stack.ga_len += dfunc->df_varcount;
1345 if (dfunc->df_has_closure)
1346 {
1347 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1348 STACK_TV_VAR(idx)->vval.v_number = 0;
1349 ++ectx.ec_stack.ga_len;
1350 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001351
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001352 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001353 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001354
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001355 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001356 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001357 estack_push_ufunc(ufunc, 1);
1358 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001359 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1360
Bram Moolenaar352134b2020-10-17 22:04:08 +02001361 // Use a specific location for storing error messages to be converted to an
1362 // exception.
1363 saved_msg_list = msg_list;
1364 msg_list = &private_msg_list;
1365
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001366 // Do turn errors into exceptions.
1367 suppress_errthrow = FALSE;
1368
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001369 // When ":silent!" was used before calling then we still abort the
1370 // function. If ":silent!" is used in the function then we don't.
1371 emsg_silent_def = emsg_silent;
1372 did_emsg_def = 0;
1373
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001374 where.wt_index = 0;
1375 where.wt_variable = FALSE;
1376
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001377 // Decide where to start execution, handles optional arguments.
1378 init_instr_idx(ufunc, argc, &ectx);
1379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 for (;;)
1381 {
1382 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001383
Bram Moolenaar270d0382020-05-15 21:42:53 +02001384 if (++breakcheck_count >= 100)
1385 {
1386 line_breakcheck();
1387 breakcheck_count = 0;
1388 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001389 if (got_int)
1390 {
1391 // Turn CTRL-C into an exception.
1392 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001393 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001394 goto failed;
1395 did_throw = TRUE;
1396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397
Bram Moolenaara26b9702020-04-18 19:53:28 +02001398 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1399 {
1400 // Turn an error message into an exception.
1401 did_emsg = FALSE;
1402 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1403 goto failed;
1404 did_throw = TRUE;
1405 *msg_list = NULL;
1406 }
1407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 if (did_throw && !ectx.ec_in_catch)
1409 {
1410 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001411 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412
1413 // An exception jumps to the first catch, finally, or returns from
1414 // the current function.
1415 if (trystack->ga_len > 0)
1416 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001417 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 {
1419 // jump to ":catch" or ":finally"
1420 ectx.ec_in_catch = TRUE;
1421 ectx.ec_iidx = trycmd->tcd_catch_idx;
1422 }
1423 else
1424 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001425 // Not inside try or need to return from current functions.
1426 // Push a dummy return value.
1427 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1428 goto failed;
1429 tv = STACK_TV_BOT(0);
1430 tv->v_type = VAR_NUMBER;
1431 tv->vval.v_number = 0;
1432 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001433 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001435 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001436 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001437 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1438 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 goto done;
1440 }
1441
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001442 if (func_return(&ectx) == FAIL)
1443 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 }
1445 continue;
1446 }
1447
1448 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1449 switch (iptr->isn_type)
1450 {
1451 // execute Ex command line
1452 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001453 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001454 source_cookie_T cookie;
1455
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001456 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001457 // Pass getsourceline to get an error for a missing ":end"
1458 // command.
1459 CLEAR_FIELD(cookie);
1460 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1461 if (do_cmdline(iptr->isn_arg.string,
1462 getsourceline, &cookie,
1463 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1464 == FAIL
1465 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001466 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001467 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 break;
1469
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001470 // execute Ex command from pieces on the stack
1471 case ISN_EXECCONCAT:
1472 {
1473 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001474 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001475 int pass;
1476 int i;
1477 char_u *cmd = NULL;
1478 char_u *str;
1479
1480 for (pass = 1; pass <= 2; ++pass)
1481 {
1482 for (i = 0; i < count; ++i)
1483 {
1484 tv = STACK_TV_BOT(i - count);
1485 str = tv->vval.v_string;
1486 if (str != NULL && *str != NUL)
1487 {
1488 if (pass == 2)
1489 STRCPY(cmd + len, str);
1490 len += STRLEN(str);
1491 }
1492 if (pass == 2)
1493 clear_tv(tv);
1494 }
1495 if (pass == 1)
1496 {
1497 cmd = alloc(len + 1);
1498 if (cmd == NULL)
1499 goto failed;
1500 len = 0;
1501 }
1502 }
1503
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001504 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001505 do_cmdline_cmd(cmd);
1506 vim_free(cmd);
1507 }
1508 break;
1509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 // execute :echo {string} ...
1511 case ISN_ECHO:
1512 {
1513 int count = iptr->isn_arg.echo.echo_count;
1514 int atstart = TRUE;
1515 int needclr = TRUE;
1516
1517 for (idx = 0; idx < count; ++idx)
1518 {
1519 tv = STACK_TV_BOT(idx - count);
1520 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1521 &atstart, &needclr);
1522 clear_tv(tv);
1523 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001524 if (needclr)
1525 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 ectx.ec_stack.ga_len -= count;
1527 }
1528 break;
1529
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001530 // :execute {string} ...
1531 // :echomsg {string} ...
1532 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001533 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001534 case ISN_ECHOMSG:
1535 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001536 {
1537 int count = iptr->isn_arg.number;
1538 garray_T ga;
1539 char_u buf[NUMBUFLEN];
1540 char_u *p;
1541 int len;
1542 int failed = FALSE;
1543
1544 ga_init2(&ga, 1, 80);
1545 for (idx = 0; idx < count; ++idx)
1546 {
1547 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001548 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001549 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001550 if (tv->v_type == VAR_CHANNEL
1551 || tv->v_type == VAR_JOB)
1552 {
1553 SOURCING_LNUM = iptr->isn_lnum;
1554 emsg(_(e_inval_string));
1555 break;
1556 }
1557 else
1558 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001559 }
1560 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001561 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001562
1563 len = (int)STRLEN(p);
1564 if (ga_grow(&ga, len + 2) == FAIL)
1565 failed = TRUE;
1566 else
1567 {
1568 if (ga.ga_len > 0)
1569 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1570 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1571 ga.ga_len += len;
1572 }
1573 clear_tv(tv);
1574 }
1575 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001576 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001577 {
1578 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001579 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001580 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001581
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001582 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001583 {
1584 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001585 {
1586 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001587 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001588 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001589 {
1590 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001591 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001592 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001593 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001594 else
1595 {
1596 msg_sb_eol();
1597 if (iptr->isn_type == ISN_ECHOMSG)
1598 {
1599 msg_attr(ga.ga_data, echo_attr);
1600 out_flush();
1601 }
1602 else
1603 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001604 SOURCING_LNUM = iptr->isn_lnum;
1605 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001606 }
1607 }
1608 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001609 ga_clear(&ga);
1610 }
1611 break;
1612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 // load local variable or argument
1614 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001615 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 goto failed;
1617 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1618 ++ectx.ec_stack.ga_len;
1619 break;
1620
1621 // load v: variable
1622 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001623 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 goto failed;
1625 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1626 ++ectx.ec_stack.ga_len;
1627 break;
1628
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001629 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 case ISN_LOADSCRIPT:
1631 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001632 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 svar_T *sv;
1634
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001635 sv = get_script_svar(sref, &ectx);
1636 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001637 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001638 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001639 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 goto failed;
1641 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1642 ++ectx.ec_stack.ga_len;
1643 }
1644 break;
1645
1646 // load s: variable in old script
1647 case ISN_LOADS:
1648 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001649 hashtab_T *ht = &SCRIPT_VARS(
1650 iptr->isn_arg.loadstore.ls_sid);
1651 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 if (di == NULL)
1655 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001656 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001657 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001658 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 }
1660 else
1661 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001662 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 goto failed;
1664 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1665 ++ectx.ec_stack.ga_len;
1666 }
1667 }
1668 break;
1669
Bram Moolenaard3aac292020-04-19 14:32:17 +02001670 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001672 case ISN_LOADB:
1673 case ISN_LOADW:
1674 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001676 dictitem_T *di = NULL;
1677 hashtab_T *ht = NULL;
1678 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001679
Bram Moolenaard3aac292020-04-19 14:32:17 +02001680 switch (iptr->isn_type)
1681 {
1682 case ISN_LOADG:
1683 ht = get_globvar_ht();
1684 namespace = 'g';
1685 break;
1686 case ISN_LOADB:
1687 ht = &curbuf->b_vars->dv_hashtab;
1688 namespace = 'b';
1689 break;
1690 case ISN_LOADW:
1691 ht = &curwin->w_vars->dv_hashtab;
1692 namespace = 'w';
1693 break;
1694 case ISN_LOADT:
1695 ht = &curtab->tp_vars->dv_hashtab;
1696 namespace = 't';
1697 break;
1698 default: // Cannot reach here
1699 goto failed;
1700 }
1701 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 if (di == NULL)
1704 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001705 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001706 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001707 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001708 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 }
1710 else
1711 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001712 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 goto failed;
1714 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1715 ++ectx.ec_stack.ga_len;
1716 }
1717 }
1718 break;
1719
Bram Moolenaar03290b82020-12-19 16:30:44 +01001720 // load autoload variable
1721 case ISN_LOADAUTO:
1722 {
1723 char_u *name = iptr->isn_arg.string;
1724
1725 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1726 goto failed;
1727 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001728 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001729 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1730 goto on_error;
1731 ++ectx.ec_stack.ga_len;
1732 }
1733 break;
1734
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001735 // load g:/b:/w:/t: namespace
1736 case ISN_LOADGDICT:
1737 case ISN_LOADBDICT:
1738 case ISN_LOADWDICT:
1739 case ISN_LOADTDICT:
1740 {
1741 dict_T *d = NULL;
1742
1743 switch (iptr->isn_type)
1744 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001745 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1746 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1747 case ISN_LOADWDICT: d = curwin->w_vars; break;
1748 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001749 default: // Cannot reach here
1750 goto failed;
1751 }
1752 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1753 goto failed;
1754 tv = STACK_TV_BOT(0);
1755 tv->v_type = VAR_DICT;
1756 tv->v_lock = 0;
1757 tv->vval.v_dict = d;
1758 ++ectx.ec_stack.ga_len;
1759 }
1760 break;
1761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 // load &option
1763 case ISN_LOADOPT:
1764 {
1765 typval_T optval;
1766 char_u *name = iptr->isn_arg.string;
1767
Bram Moolenaara8c17702020-04-01 21:17:24 +02001768 // This is not expected to fail, name is checked during
1769 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001770 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001772 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001773 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 *STACK_TV_BOT(0) = optval;
1775 ++ectx.ec_stack.ga_len;
1776 }
1777 break;
1778
1779 // load $ENV
1780 case ISN_LOADENV:
1781 {
1782 typval_T optval;
1783 char_u *name = iptr->isn_arg.string;
1784
Bram Moolenaar270d0382020-05-15 21:42:53 +02001785 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001787 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001788 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 *STACK_TV_BOT(0) = optval;
1790 ++ectx.ec_stack.ga_len;
1791 }
1792 break;
1793
1794 // load @register
1795 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001796 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 goto failed;
1798 tv = STACK_TV_BOT(0);
1799 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001800 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001801 // This may result in NULL, which should be equivalent to an
1802 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 tv->vval.v_string = get_reg_contents(
1804 iptr->isn_arg.number, GREG_EXPR_SRC);
1805 ++ectx.ec_stack.ga_len;
1806 break;
1807
1808 // store local variable
1809 case ISN_STORE:
1810 --ectx.ec_stack.ga_len;
1811 tv = STACK_TV_VAR(iptr->isn_arg.number);
1812 clear_tv(tv);
1813 *tv = *STACK_TV_BOT(0);
1814 break;
1815
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001816 // store s: variable in old script
1817 case ISN_STORES:
1818 {
1819 hashtab_T *ht = &SCRIPT_VARS(
1820 iptr->isn_arg.loadstore.ls_sid);
1821 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001822 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001823
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001824 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001825 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001826 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001827 else
1828 {
1829 clear_tv(&di->di_tv);
1830 di->di_tv = *STACK_TV_BOT(0);
1831 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001832 }
1833 break;
1834
1835 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 case ISN_STORESCRIPT:
1837 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001838 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1839 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001841 sv = get_script_svar(sref, &ectx);
1842 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001843 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 --ectx.ec_stack.ga_len;
1845 clear_tv(sv->sv_tv);
1846 *sv->sv_tv = *STACK_TV_BOT(0);
1847 }
1848 break;
1849
1850 // store option
1851 case ISN_STOREOPT:
1852 {
1853 long n = 0;
1854 char_u *s = NULL;
1855 char *msg;
1856
1857 --ectx.ec_stack.ga_len;
1858 tv = STACK_TV_BOT(0);
1859 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001860 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001862 if (s == NULL)
1863 s = (char_u *)"";
1864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001866 // must be VAR_NUMBER, CHECKTYPE makes sure
1867 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1869 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001870 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 if (msg != NULL)
1872 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001873 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001875 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 }
1878 break;
1879
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001880 // store $ENV
1881 case ISN_STOREENV:
1882 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001883 tv = STACK_TV_BOT(0);
1884 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1885 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001886 break;
1887
1888 // store @r
1889 case ISN_STOREREG:
1890 {
1891 int reg = iptr->isn_arg.number;
1892
1893 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001894 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001895 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001896 tv_get_string(tv), -1, FALSE);
1897 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001898 }
1899 break;
1900
1901 // store v: variable
1902 case ISN_STOREV:
1903 --ectx.ec_stack.ga_len;
1904 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1905 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001906 // should not happen, type is checked when compiling
1907 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001908 break;
1909
Bram Moolenaard3aac292020-04-19 14:32:17 +02001910 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001911 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001912 case ISN_STOREB:
1913 case ISN_STOREW:
1914 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001916 dictitem_T *di;
1917 hashtab_T *ht;
1918 char_u *name = iptr->isn_arg.string + 2;
1919
Bram Moolenaard3aac292020-04-19 14:32:17 +02001920 switch (iptr->isn_type)
1921 {
1922 case ISN_STOREG:
1923 ht = get_globvar_ht();
1924 break;
1925 case ISN_STOREB:
1926 ht = &curbuf->b_vars->dv_hashtab;
1927 break;
1928 case ISN_STOREW:
1929 ht = &curwin->w_vars->dv_hashtab;
1930 break;
1931 case ISN_STORET:
1932 ht = &curtab->tp_vars->dv_hashtab;
1933 break;
1934 default: // Cannot reach here
1935 goto failed;
1936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937
1938 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001939 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001941 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 else
1943 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001944 SOURCING_LNUM = iptr->isn_lnum;
1945 if (var_check_permission(di, name) == FAIL)
1946 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 clear_tv(&di->di_tv);
1948 di->di_tv = *STACK_TV_BOT(0);
1949 }
1950 }
1951 break;
1952
Bram Moolenaar03290b82020-12-19 16:30:44 +01001953 // store an autoload variable
1954 case ISN_STOREAUTO:
1955 SOURCING_LNUM = iptr->isn_lnum;
1956 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1957 clear_tv(STACK_TV_BOT(-1));
1958 --ectx.ec_stack.ga_len;
1959 break;
1960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 // store number in local variable
1962 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001963 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 clear_tv(tv);
1965 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001966 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 break;
1968
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001969 // store value in list or dict variable
1970 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001971 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001972 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001973 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001974 typval_T *tv_dest = STACK_TV_BOT(-1);
1975 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001976
Bram Moolenaar752fc692021-01-04 21:57:11 +01001977 // Stack contains:
1978 // -3 value to be stored
1979 // -2 index
1980 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001981 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001982 SOURCING_LNUM = iptr->isn_lnum;
1983 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001984 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001985 dest_type = tv_dest->v_type;
1986 if (dest_type == VAR_DICT)
1987 status = do_2string(tv_idx, TRUE);
1988 else if (dest_type == VAR_LIST
1989 && tv_idx->v_type != VAR_NUMBER)
1990 {
1991 emsg(_(e_number_exp));
1992 status = FAIL;
1993 }
1994 }
1995 else if (dest_type != tv_dest->v_type)
1996 {
1997 // just in case, should be OK
1998 semsg(_(e_expected_str_but_got_str),
1999 vartype_name(dest_type),
2000 vartype_name(tv_dest->v_type));
2001 status = FAIL;
2002 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002003
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002004 if (status == OK && dest_type == VAR_LIST)
2005 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002006 long lidx = (long)tv_idx->vval.v_number;
2007 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002008
2009 if (list == NULL)
2010 {
2011 emsg(_(e_list_not_set));
2012 goto on_error;
2013 }
2014 if (lidx < 0 && list->lv_len + lidx >= 0)
2015 // negative index is relative to the end
2016 lidx = list->lv_len + lidx;
2017 if (lidx < 0 || lidx > list->lv_len)
2018 {
2019 semsg(_(e_listidx), lidx);
2020 goto on_error;
2021 }
2022 if (lidx < list->lv_len)
2023 {
2024 listitem_T *li = list_find(list, lidx);
2025
2026 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002027 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002028 goto on_error;
2029 // overwrite existing list item
2030 clear_tv(&li->li_tv);
2031 li->li_tv = *tv;
2032 }
2033 else
2034 {
2035 if (error_if_locked(list->lv_lock,
2036 e_cannot_change_list))
2037 goto on_error;
2038 // append to list, only fails when out of memory
2039 if (list_append_tv(list, tv) == FAIL)
2040 goto failed;
2041 clear_tv(tv);
2042 }
2043 }
2044 else if (status == OK && dest_type == VAR_DICT)
2045 {
2046 char_u *key = tv_idx->vval.v_string;
2047 dict_T *dict = tv_dest->vval.v_dict;
2048 dictitem_T *di;
2049
2050 SOURCING_LNUM = iptr->isn_lnum;
2051 if (dict == NULL)
2052 {
2053 emsg(_(e_dictionary_not_set));
2054 goto on_error;
2055 }
2056 if (key == NULL)
2057 key = (char_u *)"";
2058 di = dict_find(dict, key, -1);
2059 if (di != NULL)
2060 {
2061 if (error_if_locked(di->di_tv.v_lock,
2062 e_cannot_change_dict_item))
2063 goto on_error;
2064 // overwrite existing value
2065 clear_tv(&di->di_tv);
2066 di->di_tv = *tv;
2067 }
2068 else
2069 {
2070 if (error_if_locked(dict->dv_lock,
2071 e_cannot_change_dict))
2072 goto on_error;
2073 // add to dict, only fails when out of memory
2074 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2075 goto failed;
2076 clear_tv(tv);
2077 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002078 }
2079 else
2080 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002081 status = FAIL;
2082 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002083 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002084
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002085 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002086 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002087 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002088 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002089 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002090 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002091 goto on_error;
2092 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002093 }
2094 break;
2095
Bram Moolenaar0186e582021-01-10 18:33:11 +01002096 // load or store variable or argument from outer scope
2097 case ISN_LOADOUTER:
2098 case ISN_STOREOUTER:
2099 {
2100 int depth = iptr->isn_arg.outer.outer_depth;
2101 outer_T *outer = ectx.ec_outer;
2102
2103 while (depth > 1 && outer != NULL)
2104 {
2105 outer = outer->out_up;
2106 --depth;
2107 }
2108 if (outer == NULL)
2109 {
2110 SOURCING_LNUM = iptr->isn_lnum;
2111 iemsg("LOADOUTER depth more than scope levels");
2112 goto failed;
2113 }
2114 tv = ((typval_T *)outer->out_stack->ga_data)
2115 + outer->out_frame_idx + STACK_FRAME_SIZE
2116 + iptr->isn_arg.outer.outer_idx;
2117 if (iptr->isn_type == ISN_LOADOUTER)
2118 {
2119 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2120 goto failed;
2121 copy_tv(tv, STACK_TV_BOT(0));
2122 ++ectx.ec_stack.ga_len;
2123 }
2124 else
2125 {
2126 --ectx.ec_stack.ga_len;
2127 clear_tv(tv);
2128 *tv = *STACK_TV_BOT(0);
2129 }
2130 }
2131 break;
2132
Bram Moolenaar752fc692021-01-04 21:57:11 +01002133 // unlet item in list or dict variable
2134 case ISN_UNLETINDEX:
2135 {
2136 typval_T *tv_idx = STACK_TV_BOT(-2);
2137 typval_T *tv_dest = STACK_TV_BOT(-1);
2138 int status = OK;
2139
2140 // Stack contains:
2141 // -2 index
2142 // -1 dict or list
2143 if (tv_dest->v_type == VAR_DICT)
2144 {
2145 // unlet a dict item, index must be a string
2146 if (tv_idx->v_type != VAR_STRING)
2147 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002148 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002149 semsg(_(e_expected_str_but_got_str),
2150 vartype_name(VAR_STRING),
2151 vartype_name(tv_idx->v_type));
2152 status = FAIL;
2153 }
2154 else
2155 {
2156 dict_T *d = tv_dest->vval.v_dict;
2157 char_u *key = tv_idx->vval.v_string;
2158 dictitem_T *di = NULL;
2159
2160 if (key == NULL)
2161 key = (char_u *)"";
2162 if (d != NULL)
2163 di = dict_find(d, key, (int)STRLEN(key));
2164 if (di == NULL)
2165 {
2166 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002167 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002168 semsg(_(e_dictkey), key);
2169 status = FAIL;
2170 }
2171 else
2172 {
2173 // TODO: check for dict or item locked
2174 dictitem_remove(d, di);
2175 }
2176 }
2177 }
2178 else if (tv_dest->v_type == VAR_LIST)
2179 {
2180 // unlet a List item, index must be a number
2181 if (tv_idx->v_type != VAR_NUMBER)
2182 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002183 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002184 semsg(_(e_expected_str_but_got_str),
2185 vartype_name(VAR_NUMBER),
2186 vartype_name(tv_idx->v_type));
2187 status = FAIL;
2188 }
2189 else
2190 {
2191 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002192 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002193 listitem_T *li = NULL;
2194
2195 li = list_find(l, n);
2196 if (li == NULL)
2197 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002198 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002199 semsg(_(e_listidx), n);
2200 status = FAIL;
2201 }
2202 else
2203 // TODO: check for list or item locked
2204 listitem_remove(l, li);
2205 }
2206 }
2207 else
2208 {
2209 status = FAIL;
2210 semsg(_(e_cannot_index_str),
2211 vartype_name(tv_dest->v_type));
2212 }
2213
2214 clear_tv(tv_idx);
2215 clear_tv(tv_dest);
2216 ectx.ec_stack.ga_len -= 2;
2217 if (status == FAIL)
2218 goto on_error;
2219 }
2220 break;
2221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 // push constant
2223 case ISN_PUSHNR:
2224 case ISN_PUSHBOOL:
2225 case ISN_PUSHSPEC:
2226 case ISN_PUSHF:
2227 case ISN_PUSHS:
2228 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002229 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002230 case ISN_PUSHCHANNEL:
2231 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002232 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 goto failed;
2234 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002235 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 ++ectx.ec_stack.ga_len;
2237 switch (iptr->isn_type)
2238 {
2239 case ISN_PUSHNR:
2240 tv->v_type = VAR_NUMBER;
2241 tv->vval.v_number = iptr->isn_arg.number;
2242 break;
2243 case ISN_PUSHBOOL:
2244 tv->v_type = VAR_BOOL;
2245 tv->vval.v_number = iptr->isn_arg.number;
2246 break;
2247 case ISN_PUSHSPEC:
2248 tv->v_type = VAR_SPECIAL;
2249 tv->vval.v_number = iptr->isn_arg.number;
2250 break;
2251#ifdef FEAT_FLOAT
2252 case ISN_PUSHF:
2253 tv->v_type = VAR_FLOAT;
2254 tv->vval.v_float = iptr->isn_arg.fnumber;
2255 break;
2256#endif
2257 case ISN_PUSHBLOB:
2258 blob_copy(iptr->isn_arg.blob, tv);
2259 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002260 case ISN_PUSHFUNC:
2261 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002262 if (iptr->isn_arg.string == NULL)
2263 tv->vval.v_string = NULL;
2264 else
2265 tv->vval.v_string =
2266 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002267 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002268 case ISN_PUSHCHANNEL:
2269#ifdef FEAT_JOB_CHANNEL
2270 tv->v_type = VAR_CHANNEL;
2271 tv->vval.v_channel = iptr->isn_arg.channel;
2272 if (tv->vval.v_channel != NULL)
2273 ++tv->vval.v_channel->ch_refcount;
2274#endif
2275 break;
2276 case ISN_PUSHJOB:
2277#ifdef FEAT_JOB_CHANNEL
2278 tv->v_type = VAR_JOB;
2279 tv->vval.v_job = iptr->isn_arg.job;
2280 if (tv->vval.v_job != NULL)
2281 ++tv->vval.v_job->jv_refcount;
2282#endif
2283 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 default:
2285 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002286 tv->vval.v_string = vim_strsave(
2287 iptr->isn_arg.string == NULL
2288 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 }
2290 break;
2291
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002292 case ISN_UNLET:
2293 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2294 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002295 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002296 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002297 case ISN_UNLETENV:
2298 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2299 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002300
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002301 case ISN_LOCKCONST:
2302 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2303 break;
2304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 // create a list from items on the stack; uses a single allocation
2306 // for the list header and the items
2307 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002308 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2309 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 break;
2311
2312 // create a dict from items on the stack
2313 case ISN_NEWDICT:
2314 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002315 int count = iptr->isn_arg.number;
2316 dict_T *dict = dict_alloc();
2317 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002318 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319
2320 if (dict == NULL)
2321 goto failed;
2322 for (idx = 0; idx < count; ++idx)
2323 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002324 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002326 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002327 key = tv->vval.v_string == NULL
2328 ? (char_u *)"" : tv->vval.v_string;
2329 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002330 if (item != NULL)
2331 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002333 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002334 dict_unref(dict);
2335 goto on_error;
2336 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002337 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338 clear_tv(tv);
2339 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002340 {
2341 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002343 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2345 item->di_tv.v_lock = 0;
2346 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002347 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002348 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002349 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002351 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 }
2353
2354 if (count > 0)
2355 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002356 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 goto failed;
2358 else
2359 ++ectx.ec_stack.ga_len;
2360 tv = STACK_TV_BOT(-1);
2361 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002362 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 tv->vval.v_dict = dict;
2364 ++dict->dv_refcount;
2365 }
2366 break;
2367
2368 // call a :def function
2369 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002370 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002371 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 iptr->isn_arg.dfunc.cdf_argcount,
2373 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002374 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 break;
2376
2377 // call a builtin function
2378 case ISN_BCALL:
2379 SOURCING_LNUM = iptr->isn_lnum;
2380 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2381 iptr->isn_arg.bfunc.cbf_argcount,
2382 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002383 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 break;
2385
2386 // call a funcref or partial
2387 case ISN_PCALL:
2388 {
2389 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2390 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002391 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392
2393 SOURCING_LNUM = iptr->isn_lnum;
2394 if (pfunc->cpf_top)
2395 {
2396 // funcref is above the arguments
2397 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2398 }
2399 else
2400 {
2401 // Get the funcref from the stack.
2402 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002403 partial_tv = *STACK_TV_BOT(0);
2404 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 }
2406 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002407 if (tv == &partial_tv)
2408 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002410 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 }
2412 break;
2413
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002414 case ISN_PCALL_END:
2415 // PCALL finished, arguments have been consumed and replaced by
2416 // the return value. Now clear the funcref from the stack,
2417 // and move the return value in its place.
2418 --ectx.ec_stack.ga_len;
2419 clear_tv(STACK_TV_BOT(-1));
2420 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2421 break;
2422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 // call a user defined function or funcref/partial
2424 case ISN_UCALL:
2425 {
2426 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2427
2428 SOURCING_LNUM = iptr->isn_lnum;
2429 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002430 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002431 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 }
2433 break;
2434
2435 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002436 case ISN_RETURN_ZERO:
2437 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2438 goto failed;
2439 tv = STACK_TV_BOT(0);
2440 ++ectx.ec_stack.ga_len;
2441 tv->v_type = VAR_NUMBER;
2442 tv->vval.v_number = 0;
2443 tv->v_lock = 0;
2444 // FALLTHROUGH
2445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 case ISN_RETURN:
2447 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002448 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002449 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002450
2451 if (trystack->ga_len > 0)
2452 trycmd = ((trycmd_T *)trystack->ga_data)
2453 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002454 if (trycmd != NULL
2455 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaarc150c092021-02-13 15:02:46 +01002456 && ectx.ec_instr[trycmd->tcd_finally_idx]
2457 .isn_type != ISN_ENDTRY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 {
2459 // jump to ":finally"
2460 ectx.ec_iidx = trycmd->tcd_finally_idx;
2461 trycmd->tcd_return = TRUE;
2462 }
2463 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002464 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 }
2466 break;
2467
2468 // push a function reference to a compiled function
2469 case ISN_FUNCREF:
2470 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002471 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2472 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2473 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 if (pt == NULL)
2476 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002477 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002478 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002479 vim_free(pt);
2480 goto failed;
2481 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002482 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2483 &ectx) == FAIL)
2484 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 tv = STACK_TV_BOT(0);
2487 ++ectx.ec_stack.ga_len;
2488 tv->vval.v_partial = pt;
2489 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002490 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 }
2492 break;
2493
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002494 // Create a global function from a lambda.
2495 case ISN_NEWFUNC:
2496 {
2497 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2498
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002499 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002500 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002501 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002502 }
2503 break;
2504
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002505 // List functions
2506 case ISN_DEF:
2507 if (iptr->isn_arg.string == NULL)
2508 list_functions(NULL);
2509 else
2510 {
2511 exarg_T ea;
2512
2513 CLEAR_FIELD(ea);
2514 ea.cmd = ea.arg = iptr->isn_arg.string;
2515 define_function(&ea, NULL);
2516 }
2517 break;
2518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 // jump if a condition is met
2520 case ISN_JUMP:
2521 {
2522 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002523 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 int jump = TRUE;
2525
2526 if (when != JUMP_ALWAYS)
2527 {
2528 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002529 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002530 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002531 || when == JUMP_IF_COND_TRUE)
2532 {
2533 SOURCING_LNUM = iptr->isn_lnum;
2534 jump = tv_get_bool_chk(tv, &error);
2535 if (error)
2536 goto on_error;
2537 }
2538 else
2539 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002541 || when == JUMP_AND_KEEP_IF_FALSE
2542 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002544 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 {
2546 // drop the value from the stack
2547 clear_tv(tv);
2548 --ectx.ec_stack.ga_len;
2549 }
2550 }
2551 if (jump)
2552 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2553 }
2554 break;
2555
2556 // top of a for loop
2557 case ISN_FOR:
2558 {
2559 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2560 typval_T *idxtv =
2561 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2562
2563 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002564 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002566 ++idxtv->vval.v_number;
2567 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 // past the end of the list, jump to "endfor"
2569 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2570 else if (list->lv_first == &range_list_item)
2571 {
2572 // non-materialized range() list
2573 tv = STACK_TV_BOT(0);
2574 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002575 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 tv->vval.v_number = list_find_nr(
2577 list, idxtv->vval.v_number, NULL);
2578 ++ectx.ec_stack.ga_len;
2579 }
2580 else
2581 {
2582 listitem_T *li = list_find(list, idxtv->vval.v_number);
2583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2585 ++ectx.ec_stack.ga_len;
2586 }
2587 }
2588 break;
2589
2590 // start of ":try" block
2591 case ISN_TRY:
2592 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002593 trycmd_T *trycmd = NULL;
2594
Bram Moolenaar270d0382020-05-15 21:42:53 +02002595 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 goto failed;
2597 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2598 + ectx.ec_trystack.ga_len;
2599 ++ectx.ec_trystack.ga_len;
2600 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002601 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002602 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002603 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2605 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
2606 }
2607 break;
2608
2609 case ISN_PUSHEXC:
2610 if (current_exception == NULL)
2611 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002612 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 iemsg("Evaluating catch while current_exception is NULL");
2614 goto failed;
2615 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002616 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 goto failed;
2618 tv = STACK_TV_BOT(0);
2619 ++ectx.ec_stack.ga_len;
2620 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002621 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 tv->vval.v_string = vim_strsave(
2623 (char_u *)current_exception->value);
2624 break;
2625
2626 case ISN_CATCH:
2627 {
2628 garray_T *trystack = &ectx.ec_trystack;
2629
Bram Moolenaar20a76292020-12-25 19:47:24 +01002630 if (restore_cmdmod)
2631 {
2632 cmdmod.cmod_filter_regmatch.regprog = NULL;
2633 undo_cmdmod(&cmdmod);
2634 cmdmod = save_cmdmod;
2635 restore_cmdmod = FALSE;
2636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 if (trystack->ga_len > 0)
2638 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002639 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 + trystack->ga_len - 1;
2641 trycmd->tcd_caught = TRUE;
2642 }
2643 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002644 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 catch_exception(current_exception);
2646 }
2647 break;
2648
Bram Moolenaarc150c092021-02-13 15:02:46 +01002649 case ISN_TRYCONT:
2650 {
2651 garray_T *trystack = &ectx.ec_trystack;
2652 trycont_T *trycont = &iptr->isn_arg.trycont;
2653 int i;
2654 trycmd_T *trycmd;
2655 int iidx = trycont->tct_where;
2656
2657 if (trystack->ga_len < trycont->tct_levels)
2658 {
2659 siemsg("TRYCONT: expected %d levels, found %d",
2660 trycont->tct_levels, trystack->ga_len);
2661 goto failed;
2662 }
2663 // Make :endtry jump to any outer try block and the last
2664 // :endtry inside the loop to the loop start.
2665 for (i = trycont->tct_levels; i > 0; --i)
2666 {
2667 trycmd = ((trycmd_T *)trystack->ga_data)
2668 + trystack->ga_len - i;
2669 trycmd->tcd_cont = iidx;
2670 iidx = trycmd->tcd_finally_idx;
2671 }
2672 // jump to :finally or :endtry of current try statement
2673 ectx.ec_iidx = iidx;
2674 }
2675 break;
2676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 // end of ":try" block
2678 case ISN_ENDTRY:
2679 {
2680 garray_T *trystack = &ectx.ec_trystack;
2681
2682 if (trystack->ga_len > 0)
2683 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002684 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 --trystack->ga_len;
2687 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002688 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 trycmd = ((trycmd_T *)trystack->ga_data)
2690 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002691 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 {
2693 // discard the exception
2694 if (caught_stack == current_exception)
2695 caught_stack = caught_stack->caught;
2696 discard_current_exception();
2697 }
2698
2699 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002700 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002701
2702 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2703 {
2704 --ectx.ec_stack.ga_len;
2705 clear_tv(STACK_TV_BOT(0));
2706 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002707 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002708 // handling :continue: jump to outer try block or
2709 // start of the loop
2710 ectx.ec_iidx = trycmd->tcd_cont;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 }
2712 }
2713 break;
2714
2715 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002716 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002717 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002718
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002719 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2720 {
2721 // throwing an exception while using "silent!" causes
2722 // the function to abort but not display an error.
2723 tv = STACK_TV_BOT(-1);
2724 clear_tv(tv);
2725 tv->v_type = VAR_NUMBER;
2726 tv->vval.v_number = 0;
2727 goto done;
2728 }
2729 --ectx.ec_stack.ga_len;
2730 tv = STACK_TV_BOT(0);
2731 if (tv->vval.v_string == NULL
2732 || *skipwhite(tv->vval.v_string) == NUL)
2733 {
2734 vim_free(tv->vval.v_string);
2735 SOURCING_LNUM = iptr->isn_lnum;
2736 emsg(_(e_throw_with_empty_string));
2737 goto failed;
2738 }
2739
2740 // Inside a "catch" we need to first discard the caught
2741 // exception.
2742 if (trystack->ga_len > 0)
2743 {
2744 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2745 + trystack->ga_len - 1;
2746 if (trycmd->tcd_caught && current_exception != NULL)
2747 {
2748 // discard the exception
2749 if (caught_stack == current_exception)
2750 caught_stack = caught_stack->caught;
2751 discard_current_exception();
2752 trycmd->tcd_caught = FALSE;
2753 }
2754 }
2755
2756 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2757 == FAIL)
2758 {
2759 vim_free(tv->vval.v_string);
2760 goto failed;
2761 }
2762 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 break;
2765
2766 // compare with special values
2767 case ISN_COMPAREBOOL:
2768 case ISN_COMPARESPECIAL:
2769 {
2770 typval_T *tv1 = STACK_TV_BOT(-2);
2771 typval_T *tv2 = STACK_TV_BOT(-1);
2772 varnumber_T arg1 = tv1->vval.v_number;
2773 varnumber_T arg2 = tv2->vval.v_number;
2774 int res;
2775
2776 switch (iptr->isn_arg.op.op_type)
2777 {
2778 case EXPR_EQUAL: res = arg1 == arg2; break;
2779 case EXPR_NEQUAL: res = arg1 != arg2; break;
2780 default: res = 0; break;
2781 }
2782
2783 --ectx.ec_stack.ga_len;
2784 tv1->v_type = VAR_BOOL;
2785 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2786 }
2787 break;
2788
2789 // Operation with two number arguments
2790 case ISN_OPNR:
2791 case ISN_COMPARENR:
2792 {
2793 typval_T *tv1 = STACK_TV_BOT(-2);
2794 typval_T *tv2 = STACK_TV_BOT(-1);
2795 varnumber_T arg1 = tv1->vval.v_number;
2796 varnumber_T arg2 = tv2->vval.v_number;
2797 varnumber_T res;
2798
2799 switch (iptr->isn_arg.op.op_type)
2800 {
2801 case EXPR_MULT: res = arg1 * arg2; break;
2802 case EXPR_DIV: res = arg1 / arg2; break;
2803 case EXPR_REM: res = arg1 % arg2; break;
2804 case EXPR_SUB: res = arg1 - arg2; break;
2805 case EXPR_ADD: res = arg1 + arg2; break;
2806
2807 case EXPR_EQUAL: res = arg1 == arg2; break;
2808 case EXPR_NEQUAL: res = arg1 != arg2; break;
2809 case EXPR_GREATER: res = arg1 > arg2; break;
2810 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2811 case EXPR_SMALLER: res = arg1 < arg2; break;
2812 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2813 default: res = 0; break;
2814 }
2815
2816 --ectx.ec_stack.ga_len;
2817 if (iptr->isn_type == ISN_COMPARENR)
2818 {
2819 tv1->v_type = VAR_BOOL;
2820 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2821 }
2822 else
2823 tv1->vval.v_number = res;
2824 }
2825 break;
2826
2827 // Computation with two float arguments
2828 case ISN_OPFLOAT:
2829 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002830#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 {
2832 typval_T *tv1 = STACK_TV_BOT(-2);
2833 typval_T *tv2 = STACK_TV_BOT(-1);
2834 float_T arg1 = tv1->vval.v_float;
2835 float_T arg2 = tv2->vval.v_float;
2836 float_T res = 0;
2837 int cmp = FALSE;
2838
2839 switch (iptr->isn_arg.op.op_type)
2840 {
2841 case EXPR_MULT: res = arg1 * arg2; break;
2842 case EXPR_DIV: res = arg1 / arg2; break;
2843 case EXPR_SUB: res = arg1 - arg2; break;
2844 case EXPR_ADD: res = arg1 + arg2; break;
2845
2846 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2847 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2848 case EXPR_GREATER: cmp = arg1 > arg2; break;
2849 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2850 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2851 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2852 default: cmp = 0; break;
2853 }
2854 --ectx.ec_stack.ga_len;
2855 if (iptr->isn_type == ISN_COMPAREFLOAT)
2856 {
2857 tv1->v_type = VAR_BOOL;
2858 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2859 }
2860 else
2861 tv1->vval.v_float = res;
2862 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002863#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 break;
2865
2866 case ISN_COMPARELIST:
2867 {
2868 typval_T *tv1 = STACK_TV_BOT(-2);
2869 typval_T *tv2 = STACK_TV_BOT(-1);
2870 list_T *arg1 = tv1->vval.v_list;
2871 list_T *arg2 = tv2->vval.v_list;
2872 int cmp = FALSE;
2873 int ic = iptr->isn_arg.op.op_ic;
2874
2875 switch (iptr->isn_arg.op.op_type)
2876 {
2877 case EXPR_EQUAL: cmp =
2878 list_equal(arg1, arg2, ic, FALSE); break;
2879 case EXPR_NEQUAL: cmp =
2880 !list_equal(arg1, arg2, ic, FALSE); break;
2881 case EXPR_IS: cmp = arg1 == arg2; break;
2882 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2883 default: cmp = 0; break;
2884 }
2885 --ectx.ec_stack.ga_len;
2886 clear_tv(tv1);
2887 clear_tv(tv2);
2888 tv1->v_type = VAR_BOOL;
2889 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2890 }
2891 break;
2892
2893 case ISN_COMPAREBLOB:
2894 {
2895 typval_T *tv1 = STACK_TV_BOT(-2);
2896 typval_T *tv2 = STACK_TV_BOT(-1);
2897 blob_T *arg1 = tv1->vval.v_blob;
2898 blob_T *arg2 = tv2->vval.v_blob;
2899 int cmp = FALSE;
2900
2901 switch (iptr->isn_arg.op.op_type)
2902 {
2903 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2904 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2905 case EXPR_IS: cmp = arg1 == arg2; break;
2906 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2907 default: cmp = 0; break;
2908 }
2909 --ectx.ec_stack.ga_len;
2910 clear_tv(tv1);
2911 clear_tv(tv2);
2912 tv1->v_type = VAR_BOOL;
2913 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2914 }
2915 break;
2916
2917 // TODO: handle separately
2918 case ISN_COMPARESTRING:
2919 case ISN_COMPAREDICT:
2920 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 case ISN_COMPAREANY:
2922 {
2923 typval_T *tv1 = STACK_TV_BOT(-2);
2924 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002925 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 int ic = iptr->isn_arg.op.op_ic;
2927
Bram Moolenaareb26f432020-09-14 16:50:05 +02002928 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002929 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 --ectx.ec_stack.ga_len;
2932 }
2933 break;
2934
2935 case ISN_ADDLIST:
2936 case ISN_ADDBLOB:
2937 {
2938 typval_T *tv1 = STACK_TV_BOT(-2);
2939 typval_T *tv2 = STACK_TV_BOT(-1);
2940
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002941 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 if (iptr->isn_type == ISN_ADDLIST)
2943 eval_addlist(tv1, tv2);
2944 else
2945 eval_addblob(tv1, tv2);
2946 clear_tv(tv2);
2947 --ectx.ec_stack.ga_len;
2948 }
2949 break;
2950
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002951 case ISN_LISTAPPEND:
2952 {
2953 typval_T *tv1 = STACK_TV_BOT(-2);
2954 typval_T *tv2 = STACK_TV_BOT(-1);
2955 list_T *l = tv1->vval.v_list;
2956
2957 // add an item to a list
2958 if (l == NULL)
2959 {
2960 SOURCING_LNUM = iptr->isn_lnum;
2961 emsg(_(e_cannot_add_to_null_list));
2962 goto on_error;
2963 }
2964 if (list_append_tv(l, tv2) == FAIL)
2965 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002966 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002967 --ectx.ec_stack.ga_len;
2968 }
2969 break;
2970
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002971 case ISN_BLOBAPPEND:
2972 {
2973 typval_T *tv1 = STACK_TV_BOT(-2);
2974 typval_T *tv2 = STACK_TV_BOT(-1);
2975 blob_T *b = tv1->vval.v_blob;
2976 int error = FALSE;
2977 varnumber_T n;
2978
2979 // add a number to a blob
2980 if (b == NULL)
2981 {
2982 SOURCING_LNUM = iptr->isn_lnum;
2983 emsg(_(e_cannot_add_to_null_blob));
2984 goto on_error;
2985 }
2986 n = tv_get_number_chk(tv2, &error);
2987 if (error)
2988 goto on_error;
2989 ga_append(&b->bv_ga, (int)n);
2990 --ectx.ec_stack.ga_len;
2991 }
2992 break;
2993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 // Computation with two arguments of unknown type
2995 case ISN_OPANY:
2996 {
2997 typval_T *tv1 = STACK_TV_BOT(-2);
2998 typval_T *tv2 = STACK_TV_BOT(-1);
2999 varnumber_T n1, n2;
3000#ifdef FEAT_FLOAT
3001 float_T f1 = 0, f2 = 0;
3002#endif
3003 int error = FALSE;
3004
3005 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3006 {
3007 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3008 {
3009 eval_addlist(tv1, tv2);
3010 clear_tv(tv2);
3011 --ectx.ec_stack.ga_len;
3012 break;
3013 }
3014 else if (tv1->v_type == VAR_BLOB
3015 && tv2->v_type == VAR_BLOB)
3016 {
3017 eval_addblob(tv1, tv2);
3018 clear_tv(tv2);
3019 --ectx.ec_stack.ga_len;
3020 break;
3021 }
3022 }
3023#ifdef FEAT_FLOAT
3024 if (tv1->v_type == VAR_FLOAT)
3025 {
3026 f1 = tv1->vval.v_float;
3027 n1 = 0;
3028 }
3029 else
3030#endif
3031 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003032 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 n1 = tv_get_number_chk(tv1, &error);
3034 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003035 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036#ifdef FEAT_FLOAT
3037 if (tv2->v_type == VAR_FLOAT)
3038 f1 = n1;
3039#endif
3040 }
3041#ifdef FEAT_FLOAT
3042 if (tv2->v_type == VAR_FLOAT)
3043 {
3044 f2 = tv2->vval.v_float;
3045 n2 = 0;
3046 }
3047 else
3048#endif
3049 {
3050 n2 = tv_get_number_chk(tv2, &error);
3051 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003052 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053#ifdef FEAT_FLOAT
3054 if (tv1->v_type == VAR_FLOAT)
3055 f2 = n2;
3056#endif
3057 }
3058#ifdef FEAT_FLOAT
3059 // if there is a float on either side the result is a float
3060 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3061 {
3062 switch (iptr->isn_arg.op.op_type)
3063 {
3064 case EXPR_MULT: f1 = f1 * f2; break;
3065 case EXPR_DIV: f1 = f1 / f2; break;
3066 case EXPR_SUB: f1 = f1 - f2; break;
3067 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003068 default: SOURCING_LNUM = iptr->isn_lnum;
3069 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003070 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 }
3072 clear_tv(tv1);
3073 clear_tv(tv2);
3074 tv1->v_type = VAR_FLOAT;
3075 tv1->vval.v_float = f1;
3076 --ectx.ec_stack.ga_len;
3077 }
3078 else
3079#endif
3080 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003081 int failed = FALSE;
3082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 switch (iptr->isn_arg.op.op_type)
3084 {
3085 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003086 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3087 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003088 goto on_error;
3089 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 case EXPR_SUB: n1 = n1 - n2; break;
3091 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003092 default: n1 = num_modulus(n1, n2, &failed);
3093 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003094 goto on_error;
3095 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 }
3097 clear_tv(tv1);
3098 clear_tv(tv2);
3099 tv1->v_type = VAR_NUMBER;
3100 tv1->vval.v_number = n1;
3101 --ectx.ec_stack.ga_len;
3102 }
3103 }
3104 break;
3105
3106 case ISN_CONCAT:
3107 {
3108 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3109 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3110 char_u *res;
3111
3112 res = concat_str(str1, str2);
3113 clear_tv(STACK_TV_BOT(-2));
3114 clear_tv(STACK_TV_BOT(-1));
3115 --ectx.ec_stack.ga_len;
3116 STACK_TV_BOT(-1)->vval.v_string = res;
3117 }
3118 break;
3119
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003120 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003121 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003122 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003123 int is_slice = iptr->isn_type == ISN_STRSLICE;
3124 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003125 char_u *res;
3126
3127 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003128 // string slice: string is at stack-3, first index at
3129 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003130 if (is_slice)
3131 {
3132 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003133 n1 = tv->vval.v_number;
3134 }
3135
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003136 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003137 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003138
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003139 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003140 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003141 if (is_slice)
3142 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003143 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003144 else
3145 // Index: The resulting variable is a string of a
3146 // single character. If the index is too big or
3147 // negative the result is empty.
3148 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003149 vim_free(tv->vval.v_string);
3150 tv->vval.v_string = res;
3151 }
3152 break;
3153
3154 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003155 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 {
Bram Moolenaared591872020-08-15 22:14:53 +02003157 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003159 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160
3161 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003162 // list slice: list is at stack-3, indexes at stack-2 and
3163 // stack-1
3164 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 list = tv->vval.v_list;
3166
3167 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003168 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003170
3171 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 {
Bram Moolenaared591872020-08-15 22:14:53 +02003173 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003174 n1 = tv->vval.v_number;
3175 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 }
Bram Moolenaared591872020-08-15 22:14:53 +02003177
3178 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003179 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003180 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003181 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3182 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003183 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 }
3185 break;
3186
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003187 case ISN_ANYINDEX:
3188 case ISN_ANYSLICE:
3189 {
3190 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3191 typval_T *var1, *var2;
3192 int res;
3193
3194 // index: composite is at stack-2, index at stack-1
3195 // slice: composite is at stack-3, indexes at stack-2 and
3196 // stack-1
3197 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003198 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003199 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3200 goto on_error;
3201 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3202 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003203 res = eval_index_inner(tv, is_slice, var1, var2,
3204 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003205 clear_tv(var1);
3206 if (is_slice)
3207 clear_tv(var2);
3208 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3209 if (res == FAIL)
3210 goto on_error;
3211 }
3212 break;
3213
Bram Moolenaar9af78762020-06-16 11:34:42 +02003214 case ISN_SLICE:
3215 {
3216 list_T *list;
3217 int count = iptr->isn_arg.number;
3218
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003219 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003220 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003221 list = tv->vval.v_list;
3222
3223 // no error for short list, expect it to be checked earlier
3224 if (list != NULL && list->lv_len >= count)
3225 {
3226 list_T *newlist = list_slice(list,
3227 count, list->lv_len - 1);
3228
3229 if (newlist != NULL)
3230 {
3231 list_unref(list);
3232 tv->vval.v_list = newlist;
3233 ++newlist->lv_refcount;
3234 }
3235 }
3236 }
3237 break;
3238
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003239 case ISN_GETITEM:
3240 {
3241 listitem_T *li;
3242 int index = iptr->isn_arg.number;
3243
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003244 // Get list item: list is at stack-1, push item.
3245 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003246 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003247 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003248
3249 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3250 goto failed;
3251 ++ectx.ec_stack.ga_len;
3252 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003253
3254 // Useful when used in unpack assignment. Reset at
3255 // ISN_DROP.
3256 where.wt_index = index + 1;
3257 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003258 }
3259 break;
3260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 case ISN_MEMBER:
3262 {
3263 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003264 char_u *key;
3265 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003266 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003267
3268 // dict member: dict is at stack-2, key at stack-1
3269 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003270 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003271 dict = tv->vval.v_dict;
3272
3273 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003274 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003275 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003276 if (key == NULL)
3277 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003278
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003279 if ((di = dict_find(dict, key, -1)) == NULL)
3280 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003281 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003282 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003283
3284 // If :silent! is used we will continue, make sure the
3285 // stack contents makes sense.
3286 clear_tv(tv);
3287 --ectx.ec_stack.ga_len;
3288 tv = STACK_TV_BOT(-1);
3289 clear_tv(tv);
3290 tv->v_type = VAR_NUMBER;
3291 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003292 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003293 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003294 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003295 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003296 // Clear the dict only after getting the item, to avoid
3297 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003298 tv = STACK_TV_BOT(-1);
3299 temp_tv = *tv;
3300 copy_tv(&di->di_tv, tv);
3301 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003302 }
3303 break;
3304
3305 // dict member with string key
3306 case ISN_STRINGMEMBER:
3307 {
3308 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003310 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311
3312 tv = STACK_TV_BOT(-1);
3313 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3314 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003315 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003317 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 }
3319 dict = tv->vval.v_dict;
3320
3321 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3322 == NULL)
3323 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003324 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003326 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003328 // Clear the dict after getting the item, to avoid that it
3329 // make the item invalid.
3330 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003332 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 }
3334 break;
3335
3336 case ISN_NEGATENR:
3337 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003338 if (tv->v_type != VAR_NUMBER
3339#ifdef FEAT_FLOAT
3340 && tv->v_type != VAR_FLOAT
3341#endif
3342 )
3343 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003344 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003345 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003346 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003347 }
3348#ifdef FEAT_FLOAT
3349 if (tv->v_type == VAR_FLOAT)
3350 tv->vval.v_float = -tv->vval.v_float;
3351 else
3352#endif
3353 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 break;
3355
3356 case ISN_CHECKNR:
3357 {
3358 int error = FALSE;
3359
3360 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003361 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003363 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 (void)tv_get_number_chk(tv, &error);
3365 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003366 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 }
3368 break;
3369
3370 case ISN_CHECKTYPE:
3371 {
3372 checktype_T *ct = &iptr->isn_arg.type;
3373
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003374 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003375 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003376 if (!where.wt_variable)
3377 where.wt_index = ct->ct_arg_idx;
3378 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003379 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003380 if (!where.wt_variable)
3381 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003382
3383 // number 0 is FALSE, number 1 is TRUE
3384 if (tv->v_type == VAR_NUMBER
3385 && ct->ct_type->tt_type == VAR_BOOL
3386 && (tv->vval.v_number == 0
3387 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003389 tv->v_type = VAR_BOOL;
3390 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003391 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 }
3393 }
3394 break;
3395
Bram Moolenaar9af78762020-06-16 11:34:42 +02003396 case ISN_CHECKLEN:
3397 {
3398 int min_len = iptr->isn_arg.checklen.cl_min_len;
3399 list_T *list = NULL;
3400
3401 tv = STACK_TV_BOT(-1);
3402 if (tv->v_type == VAR_LIST)
3403 list = tv->vval.v_list;
3404 if (list == NULL || list->lv_len < min_len
3405 || (list->lv_len > min_len
3406 && !iptr->isn_arg.checklen.cl_more_OK))
3407 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003408 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003409 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003410 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003411 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003412 }
3413 }
3414 break;
3415
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003416 case ISN_SETTYPE:
3417 {
3418 checktype_T *ct = &iptr->isn_arg.type;
3419
3420 tv = STACK_TV_BOT(-1);
3421 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3422 {
3423 free_type(tv->vval.v_dict->dv_type);
3424 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3425 }
3426 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3427 {
3428 free_type(tv->vval.v_list->lv_type);
3429 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3430 }
3431 }
3432 break;
3433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003435 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 {
3437 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003438 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439
3440 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003441 if (iptr->isn_type == ISN_2BOOL)
3442 {
3443 n = tv2bool(tv);
3444 if (iptr->isn_arg.number) // invert
3445 n = !n;
3446 }
3447 else
3448 {
3449 SOURCING_LNUM = iptr->isn_lnum;
3450 n = tv_get_bool_chk(tv, &error);
3451 if (error)
3452 goto on_error;
3453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 clear_tv(tv);
3455 tv->v_type = VAR_BOOL;
3456 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3457 }
3458 break;
3459
3460 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003461 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003462 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003463 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3464 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3465 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 break;
3467
Bram Moolenaar08597872020-12-10 19:43:40 +01003468 case ISN_RANGE:
3469 {
3470 exarg_T ea;
3471 char *errormsg;
3472
Bram Moolenaarece0b872021-01-08 20:40:45 +01003473 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003474 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003475 ea.addr_type = ADDR_LINES;
3476 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003477 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003478 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003479 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003480
3481 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3482 goto failed;
3483 ++ectx.ec_stack.ga_len;
3484 tv = STACK_TV_BOT(-1);
3485 tv->v_type = VAR_NUMBER;
3486 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003487 if (ea.addr_count == 0)
3488 tv->vval.v_number = curwin->w_cursor.lnum;
3489 else
3490 tv->vval.v_number = ea.line2;
3491 }
3492 break;
3493
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003494 case ISN_PUT:
3495 {
3496 int regname = iptr->isn_arg.put.put_regname;
3497 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3498 char_u *expr = NULL;
3499 int dir = FORWARD;
3500
Bram Moolenaar08597872020-12-10 19:43:40 +01003501 if (lnum < -2)
3502 {
3503 // line number was put on the stack by ISN_RANGE
3504 tv = STACK_TV_BOT(-1);
3505 curwin->w_cursor.lnum = tv->vval.v_number;
3506 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3507 dir = BACKWARD;
3508 --ectx.ec_stack.ga_len;
3509 }
3510 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003511 // :put! above cursor
3512 dir = BACKWARD;
3513 else if (lnum >= 0)
3514 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003515
3516 if (regname == '=')
3517 {
3518 tv = STACK_TV_BOT(-1);
3519 if (tv->v_type == VAR_STRING)
3520 expr = tv->vval.v_string;
3521 else
3522 {
3523 expr = typval2string(tv, TRUE); // allocates value
3524 clear_tv(tv);
3525 }
3526 --ectx.ec_stack.ga_len;
3527 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003528 check_cursor();
3529 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3530 vim_free(expr);
3531 }
3532 break;
3533
Bram Moolenaar02194d22020-10-24 23:08:38 +02003534 case ISN_CMDMOD:
3535 save_cmdmod = cmdmod;
3536 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003537 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003538 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3539 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003540 break;
3541
Bram Moolenaar02194d22020-10-24 23:08:38 +02003542 case ISN_CMDMOD_REV:
3543 // filter regprog is owned by the instruction, don't free it
3544 cmdmod.cmod_filter_regmatch.regprog = NULL;
3545 undo_cmdmod(&cmdmod);
3546 cmdmod = save_cmdmod;
3547 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003548 break;
3549
Bram Moolenaar792f7862020-11-23 08:31:18 +01003550 case ISN_UNPACK:
3551 {
3552 int count = iptr->isn_arg.unpack.unp_count;
3553 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3554 list_T *l;
3555 listitem_T *li;
3556 int i;
3557
3558 // Check there is a valid list to unpack.
3559 tv = STACK_TV_BOT(-1);
3560 if (tv->v_type != VAR_LIST)
3561 {
3562 SOURCING_LNUM = iptr->isn_lnum;
3563 emsg(_(e_for_argument_must_be_sequence_of_lists));
3564 goto on_error;
3565 }
3566 l = tv->vval.v_list;
3567 if (l == NULL
3568 || l->lv_len < (semicolon ? count - 1 : count))
3569 {
3570 SOURCING_LNUM = iptr->isn_lnum;
3571 emsg(_(e_list_value_does_not_have_enough_items));
3572 goto on_error;
3573 }
3574 else if (!semicolon && l->lv_len > count)
3575 {
3576 SOURCING_LNUM = iptr->isn_lnum;
3577 emsg(_(e_list_value_has_more_items_than_targets));
3578 goto on_error;
3579 }
3580
3581 CHECK_LIST_MATERIALIZE(l);
3582 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3583 goto failed;
3584 ectx.ec_stack.ga_len += count - 1;
3585
3586 // Variable after semicolon gets a list with the remaining
3587 // items.
3588 if (semicolon)
3589 {
3590 list_T *rem_list =
3591 list_alloc_with_items(l->lv_len - count + 1);
3592
3593 if (rem_list == NULL)
3594 goto failed;
3595 tv = STACK_TV_BOT(-count);
3596 tv->vval.v_list = rem_list;
3597 ++rem_list->lv_refcount;
3598 tv->v_lock = 0;
3599 li = l->lv_first;
3600 for (i = 0; i < count - 1; ++i)
3601 li = li->li_next;
3602 for (i = 0; li != NULL; ++i)
3603 {
3604 list_set_item(rem_list, i, &li->li_tv);
3605 li = li->li_next;
3606 }
3607 --count;
3608 }
3609
3610 // Produce the values in reverse order, first item last.
3611 li = l->lv_first;
3612 for (i = 0; i < count; ++i)
3613 {
3614 tv = STACK_TV_BOT(-i - 1);
3615 copy_tv(&li->li_tv, tv);
3616 li = li->li_next;
3617 }
3618
3619 list_unref(l);
3620 }
3621 break;
3622
Bram Moolenaarb2049902021-01-24 12:53:53 +01003623 case ISN_PROF_START:
3624 case ISN_PROF_END:
3625 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003626#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003627 funccall_T cookie;
3628 ufunc_T *cur_ufunc =
3629 (((dfunc_T *)def_functions.ga_data)
3630 + ectx.ec_dfunc_idx)->df_ufunc;
3631
3632 cookie.func = cur_ufunc;
3633 if (iptr->isn_type == ISN_PROF_START)
3634 {
3635 func_line_start(&cookie, iptr->isn_lnum);
3636 // if we get here the instruction is executed
3637 func_line_exec(&cookie);
3638 }
3639 else
3640 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003641#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003642 }
3643 break;
3644
Bram Moolenaar389df252020-07-09 21:20:47 +02003645 case ISN_SHUFFLE:
3646 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003647 typval_T tmp_tv;
3648 int item = iptr->isn_arg.shuffle.shfl_item;
3649 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003650
3651 tmp_tv = *STACK_TV_BOT(-item);
3652 for ( ; up > 0 && item > 1; --up)
3653 {
3654 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3655 --item;
3656 }
3657 *STACK_TV_BOT(-item) = tmp_tv;
3658 }
3659 break;
3660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 case ISN_DROP:
3662 --ectx.ec_stack.ga_len;
3663 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003664 where.wt_index = 0;
3665 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 break;
3667 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003668 continue;
3669
Bram Moolenaard032f342020-07-18 18:13:02 +02003670func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003671 // Restore previous function. If the frame pointer is where we started
3672 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003673 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003674 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003675
Bram Moolenaard032f342020-07-18 18:13:02 +02003676 if (func_return(&ectx) == FAIL)
3677 // only fails when out of memory
3678 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003679 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003680
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003681on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003682 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003683 // If "emsg_silent" is set then ignore the error, unless it was set
3684 // when calling the function.
3685 if (did_emsg_cumul + did_emsg == did_emsg_before
3686 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003687 {
3688 // If a sequence of instructions causes an error while ":silent!"
3689 // was used, restore the stack length and jump ahead to restoring
3690 // the cmdmod.
3691 if (restore_cmdmod)
3692 {
3693 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3694 {
3695 --ectx.ec_stack.ga_len;
3696 clear_tv(STACK_TV_BOT(0));
3697 }
3698 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3699 ++ectx.ec_iidx;
3700 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003701 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003702 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003703on_fatal_error:
3704 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003705 // If we are not inside a try-catch started here, abort execution.
3706 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003707 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 }
3709
3710done:
3711 // function finished, get result from the stack.
3712 tv = STACK_TV_BOT(-1);
3713 *rettv = *tv;
3714 tv->v_type = VAR_UNKNOWN;
3715 ret = OK;
3716
3717failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003718 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003719 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003720 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003721
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003722 // Deal with any remaining closures, they may be in use somewhere.
3723 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003724 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003725 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003726 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3727 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003728
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003729 estack_pop();
3730 current_sctx = save_current_sctx;
3731
Bram Moolenaar352134b2020-10-17 22:04:08 +02003732 if (*msg_list != NULL && saved_msg_list != NULL)
3733 {
3734 msglist_T **plist = saved_msg_list;
3735
3736 // Append entries from the current msg_list (uncaught exceptions) to
3737 // the saved msg_list.
3738 while (*plist != NULL)
3739 plist = &(*plist)->next;
3740
3741 *plist = *msg_list;
3742 }
3743 msg_list = saved_msg_list;
3744
Bram Moolenaar02194d22020-10-24 23:08:38 +02003745 if (restore_cmdmod)
3746 {
3747 cmdmod.cmod_filter_regmatch.regprog = NULL;
3748 undo_cmdmod(&cmdmod);
3749 cmdmod = save_cmdmod;
3750 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003751 emsg_silent_def = save_emsg_silent_def;
3752 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003753
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003754failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003755 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3757 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003760 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003761
Bram Moolenaar0186e582021-01-10 18:33:11 +01003762 while (ectx.ec_outer != NULL)
3763 {
3764 outer_T *up = ectx.ec_outer->out_up_is_copy
3765 ? NULL : ectx.ec_outer->out_up;
3766
3767 vim_free(ectx.ec_outer);
3768 ectx.ec_outer = up;
3769 }
3770
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003771 // Not sure if this is necessary.
3772 suppress_errthrow = save_suppress_errthrow;
3773
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003774 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003775 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003776 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003777 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 return ret;
3779}
3780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003782 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003783 * We don't really need this at runtime, but we do have tests that require it,
3784 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 */
3786 void
3787ex_disassemble(exarg_T *eap)
3788{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003789 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003790 char_u *fname;
3791 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 dfunc_T *dfunc;
3793 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003794 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 int current;
3796 int line_idx = 0;
3797 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003798 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003800 if (STRNCMP(arg, "<lambda>", 8) == 0)
3801 {
3802 arg += 8;
3803 (void)getdigits(&arg);
3804 fname = vim_strnsave(eap->arg, arg - eap->arg);
3805 }
3806 else
3807 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003808 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003809 if (fname == NULL)
3810 {
3811 semsg(_(e_invarg2), eap->arg);
3812 return;
3813 }
3814
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003815 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003816 if (ufunc == NULL)
3817 {
3818 char_u *p = untrans_function_name(fname);
3819
3820 if (p != NULL)
3821 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003822 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003823 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003824 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 if (ufunc == NULL)
3826 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003827 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 return;
3829 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003830 if (func_needs_compiling(ufunc, eap->forceit)
3831 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003832 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003833 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003835 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 return;
3837 }
3838 if (ufunc->uf_name_exp != NULL)
3839 msg((char *)ufunc->uf_name_exp);
3840 else
3841 msg((char *)ufunc->uf_name);
3842
3843 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003844#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003845 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3846 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3847 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003848#else
3849 instr = dfunc->df_instr;
3850 instr_count = dfunc->df_instr_count;
3851#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003852 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 {
3854 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003855 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856
3857 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3858 {
3859 if (current > prev_current)
3860 {
3861 msg_puts("\n\n");
3862 prev_current = current;
3863 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003864 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3865 if (line != NULL)
3866 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 }
3868
3869 switch (iptr->isn_type)
3870 {
3871 case ISN_EXEC:
3872 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3873 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003874 case ISN_EXECCONCAT:
3875 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003876 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003877 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 case ISN_ECHO:
3879 {
3880 echo_T *echo = &iptr->isn_arg.echo;
3881
3882 smsg("%4d %s %d", current,
3883 echo->echo_with_white ? "ECHO" : "ECHON",
3884 echo->echo_count);
3885 }
3886 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003887 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003888 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003889 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003890 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003891 case ISN_ECHOMSG:
3892 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003893 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003894 break;
3895 case ISN_ECHOERR:
3896 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003897 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003898 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003900 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003901 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003902 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003903 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003904 + STACK_FRAME_SIZE));
3905 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003906 smsg("%4d LOAD $%lld", current,
3907 (varnumber_T)(iptr->isn_arg.number));
3908 }
3909 break;
3910 case ISN_LOADOUTER:
3911 {
3912 if (iptr->isn_arg.number < 0)
3913 smsg("%4d LOADOUTER level %d arg[%d]", current,
3914 iptr->isn_arg.outer.outer_depth,
3915 iptr->isn_arg.outer.outer_idx
3916 + STACK_FRAME_SIZE);
3917 else
3918 smsg("%4d LOADOUTER level %d $%d", current,
3919 iptr->isn_arg.outer.outer_depth,
3920 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 break;
3923 case ISN_LOADV:
3924 smsg("%4d LOADV v:%s", current,
3925 get_vim_var_name(iptr->isn_arg.number));
3926 break;
3927 case ISN_LOADSCRIPT:
3928 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003929 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3930 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003932 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003934 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3935 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003936 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003937 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 }
3939 break;
3940 case ISN_LOADS:
3941 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003942 scriptitem_T *si = SCRIPT_ITEM(
3943 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944
3945 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003946 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 }
3948 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003949 case ISN_LOADAUTO:
3950 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3951 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 case ISN_LOADG:
3953 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3954 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003955 case ISN_LOADB:
3956 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3957 break;
3958 case ISN_LOADW:
3959 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3960 break;
3961 case ISN_LOADT:
3962 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3963 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003964 case ISN_LOADGDICT:
3965 smsg("%4d LOAD g:", current);
3966 break;
3967 case ISN_LOADBDICT:
3968 smsg("%4d LOAD b:", current);
3969 break;
3970 case ISN_LOADWDICT:
3971 smsg("%4d LOAD w:", current);
3972 break;
3973 case ISN_LOADTDICT:
3974 smsg("%4d LOAD t:", current);
3975 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 case ISN_LOADOPT:
3977 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3978 break;
3979 case ISN_LOADENV:
3980 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3981 break;
3982 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003983 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 break;
3985
3986 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003987 if (iptr->isn_arg.number < 0)
3988 smsg("%4d STORE arg[%lld]", current,
3989 iptr->isn_arg.number + STACK_FRAME_SIZE);
3990 else
3991 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3992 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003993 case ISN_STOREOUTER:
3994 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003995 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003996 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3997 iptr->isn_arg.outer.outer_depth,
3998 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003999 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004000 smsg("%4d STOREOUTER level %d $%d", current,
4001 iptr->isn_arg.outer.outer_depth,
4002 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004005 case ISN_STOREV:
4006 smsg("%4d STOREV v:%s", current,
4007 get_vim_var_name(iptr->isn_arg.number));
4008 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004009 case ISN_STOREAUTO:
4010 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4011 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004013 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4014 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004015 case ISN_STOREB:
4016 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4017 break;
4018 case ISN_STOREW:
4019 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4020 break;
4021 case ISN_STORET:
4022 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4023 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004024 case ISN_STORES:
4025 {
4026 scriptitem_T *si = SCRIPT_ITEM(
4027 iptr->isn_arg.loadstore.ls_sid);
4028
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004029 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004030 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 break;
4033 case ISN_STORESCRIPT:
4034 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004035 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4036 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004038 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004040 smsg("%4d STORESCRIPT %s-%d in %s", current,
4041 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004042 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004043 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 }
4045 break;
4046 case ISN_STOREOPT:
4047 smsg("%4d STOREOPT &%s", current,
4048 iptr->isn_arg.storeopt.so_name);
4049 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004050 case ISN_STOREENV:
4051 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4052 break;
4053 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004054 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004055 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 case ISN_STORENR:
4057 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004058 iptr->isn_arg.storenr.stnr_val,
4059 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 break;
4061
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004062 case ISN_STOREINDEX:
4063 switch (iptr->isn_arg.vartype)
4064 {
4065 case VAR_LIST:
4066 smsg("%4d STORELIST", current);
4067 break;
4068 case VAR_DICT:
4069 smsg("%4d STOREDICT", current);
4070 break;
4071 case VAR_ANY:
4072 smsg("%4d STOREINDEX", current);
4073 break;
4074 default: break;
4075 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004076 break;
4077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 // constants
4079 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004080 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004081 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 break;
4083 case ISN_PUSHBOOL:
4084 case ISN_PUSHSPEC:
4085 smsg("%4d PUSH %s", current,
4086 get_var_special_name(iptr->isn_arg.number));
4087 break;
4088 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004089#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004091#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 break;
4093 case ISN_PUSHS:
4094 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4095 break;
4096 case ISN_PUSHBLOB:
4097 {
4098 char_u *r;
4099 char_u numbuf[NUMBUFLEN];
4100 char_u *tofree;
4101
4102 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004103 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 vim_free(tofree);
4105 }
4106 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004107 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004108 {
4109 char *name = (char *)iptr->isn_arg.string;
4110
4111 smsg("%4d PUSHFUNC \"%s\"", current,
4112 name == NULL ? "[none]" : name);
4113 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004114 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004115 case ISN_PUSHCHANNEL:
4116#ifdef FEAT_JOB_CHANNEL
4117 {
4118 channel_T *channel = iptr->isn_arg.channel;
4119
4120 smsg("%4d PUSHCHANNEL %d", current,
4121 channel == NULL ? 0 : channel->ch_id);
4122 }
4123#endif
4124 break;
4125 case ISN_PUSHJOB:
4126#ifdef FEAT_JOB_CHANNEL
4127 {
4128 typval_T tv;
4129 char_u *name;
4130
4131 tv.v_type = VAR_JOB;
4132 tv.vval.v_job = iptr->isn_arg.job;
4133 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004134 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004135 }
4136#endif
4137 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 case ISN_PUSHEXC:
4139 smsg("%4d PUSH v:exception", current);
4140 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004141 case ISN_UNLET:
4142 smsg("%4d UNLET%s %s", current,
4143 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4144 iptr->isn_arg.unlet.ul_name);
4145 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004146 case ISN_UNLETENV:
4147 smsg("%4d UNLETENV%s $%s", current,
4148 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4149 iptr->isn_arg.unlet.ul_name);
4150 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004151 case ISN_UNLETINDEX:
4152 smsg("%4d UNLETINDEX", current);
4153 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004154 case ISN_LOCKCONST:
4155 smsg("%4d LOCKCONST", current);
4156 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004158 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004159 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 break;
4161 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004162 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004163 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 break;
4165
4166 // function call
4167 case ISN_BCALL:
4168 {
4169 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4170
4171 smsg("%4d BCALL %s(argc %d)", current,
4172 internal_func_name(cbfunc->cbf_idx),
4173 cbfunc->cbf_argcount);
4174 }
4175 break;
4176 case ISN_DCALL:
4177 {
4178 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4179 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4180 + cdfunc->cdf_idx;
4181
4182 smsg("%4d DCALL %s(argc %d)", current,
4183 df->df_ufunc->uf_name_exp != NULL
4184 ? df->df_ufunc->uf_name_exp
4185 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4186 }
4187 break;
4188 case ISN_UCALL:
4189 {
4190 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4191
4192 smsg("%4d UCALL %s(argc %d)", current,
4193 cufunc->cuf_name, cufunc->cuf_argcount);
4194 }
4195 break;
4196 case ISN_PCALL:
4197 {
4198 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4199
4200 smsg("%4d PCALL%s (argc %d)", current,
4201 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4202 }
4203 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004204 case ISN_PCALL_END:
4205 smsg("%4d PCALL end", current);
4206 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 case ISN_RETURN:
4208 smsg("%4d RETURN", current);
4209 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004210 case ISN_RETURN_ZERO:
4211 smsg("%4d RETURN 0", current);
4212 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 case ISN_FUNCREF:
4214 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004215 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004217 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004219 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 }
4221 break;
4222
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004223 case ISN_NEWFUNC:
4224 {
4225 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4226
4227 smsg("%4d NEWFUNC %s %s", current,
4228 newfunc->nf_lambda, newfunc->nf_global);
4229 }
4230 break;
4231
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004232 case ISN_DEF:
4233 {
4234 char_u *name = iptr->isn_arg.string;
4235
4236 smsg("%4d DEF %s", current,
4237 name == NULL ? (char_u *)"" : name);
4238 }
4239 break;
4240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 case ISN_JUMP:
4242 {
4243 char *when = "?";
4244
4245 switch (iptr->isn_arg.jump.jump_when)
4246 {
4247 case JUMP_ALWAYS:
4248 when = "JUMP";
4249 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 case JUMP_AND_KEEP_IF_TRUE:
4251 when = "JUMP_AND_KEEP_IF_TRUE";
4252 break;
4253 case JUMP_IF_FALSE:
4254 when = "JUMP_IF_FALSE";
4255 break;
4256 case JUMP_AND_KEEP_IF_FALSE:
4257 when = "JUMP_AND_KEEP_IF_FALSE";
4258 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004259 case JUMP_IF_COND_FALSE:
4260 when = "JUMP_IF_COND_FALSE";
4261 break;
4262 case JUMP_IF_COND_TRUE:
4263 when = "JUMP_IF_COND_TRUE";
4264 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004266 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 iptr->isn_arg.jump.jump_where);
4268 }
4269 break;
4270
4271 case ISN_FOR:
4272 {
4273 forloop_T *forloop = &iptr->isn_arg.forloop;
4274
4275 smsg("%4d FOR $%d -> %d", current,
4276 forloop->for_idx, forloop->for_end);
4277 }
4278 break;
4279
4280 case ISN_TRY:
4281 {
4282 try_T *try = &iptr->isn_arg.try;
4283
Bram Moolenaarc150c092021-02-13 15:02:46 +01004284 smsg("%4d TRY catch -> %d, %s -> %d", current,
4285 try->try_catch,
4286 instr[try->try_finally].isn_type == ISN_ENDTRY
4287 ? "end" : "finally",
4288 try->try_finally);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 }
4290 break;
4291 case ISN_CATCH:
4292 // TODO
4293 smsg("%4d CATCH", current);
4294 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004295 case ISN_TRYCONT:
4296 {
4297 trycont_T *trycont = &iptr->isn_arg.trycont;
4298
4299 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4300 trycont->tct_levels,
4301 trycont->tct_levels == 1 ? "" : "s",
4302 trycont->tct_where);
4303 }
4304 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 case ISN_ENDTRY:
4306 smsg("%4d ENDTRY", current);
4307 break;
4308 case ISN_THROW:
4309 smsg("%4d THROW", current);
4310 break;
4311
4312 // expression operations on number
4313 case ISN_OPNR:
4314 case ISN_OPFLOAT:
4315 case ISN_OPANY:
4316 {
4317 char *what;
4318 char *ins;
4319
4320 switch (iptr->isn_arg.op.op_type)
4321 {
4322 case EXPR_MULT: what = "*"; break;
4323 case EXPR_DIV: what = "/"; break;
4324 case EXPR_REM: what = "%"; break;
4325 case EXPR_SUB: what = "-"; break;
4326 case EXPR_ADD: what = "+"; break;
4327 default: what = "???"; break;
4328 }
4329 switch (iptr->isn_type)
4330 {
4331 case ISN_OPNR: ins = "OPNR"; break;
4332 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4333 case ISN_OPANY: ins = "OPANY"; break;
4334 default: ins = "???"; break;
4335 }
4336 smsg("%4d %s %s", current, ins, what);
4337 }
4338 break;
4339
4340 case ISN_COMPAREBOOL:
4341 case ISN_COMPARESPECIAL:
4342 case ISN_COMPARENR:
4343 case ISN_COMPAREFLOAT:
4344 case ISN_COMPARESTRING:
4345 case ISN_COMPAREBLOB:
4346 case ISN_COMPARELIST:
4347 case ISN_COMPAREDICT:
4348 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 case ISN_COMPAREANY:
4350 {
4351 char *p;
4352 char buf[10];
4353 char *type;
4354
4355 switch (iptr->isn_arg.op.op_type)
4356 {
4357 case EXPR_EQUAL: p = "=="; break;
4358 case EXPR_NEQUAL: p = "!="; break;
4359 case EXPR_GREATER: p = ">"; break;
4360 case EXPR_GEQUAL: p = ">="; break;
4361 case EXPR_SMALLER: p = "<"; break;
4362 case EXPR_SEQUAL: p = "<="; break;
4363 case EXPR_MATCH: p = "=~"; break;
4364 case EXPR_IS: p = "is"; break;
4365 case EXPR_ISNOT: p = "isnot"; break;
4366 case EXPR_NOMATCH: p = "!~"; break;
4367 default: p = "???"; break;
4368 }
4369 STRCPY(buf, p);
4370 if (iptr->isn_arg.op.op_ic == TRUE)
4371 strcat(buf, "?");
4372 switch(iptr->isn_type)
4373 {
4374 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4375 case ISN_COMPARESPECIAL:
4376 type = "COMPARESPECIAL"; break;
4377 case ISN_COMPARENR: type = "COMPARENR"; break;
4378 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4379 case ISN_COMPARESTRING:
4380 type = "COMPARESTRING"; break;
4381 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4382 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4383 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4384 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4386 default: type = "???"; break;
4387 }
4388
4389 smsg("%4d %s %s", current, type, buf);
4390 }
4391 break;
4392
4393 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4394 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4395
4396 // expression operations
4397 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004398 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004399 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004400 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004401 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004402 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004403 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004404 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4405 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004406 case ISN_SLICE: smsg("%4d SLICE %lld",
4407 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004408 case ISN_GETITEM: smsg("%4d ITEM %lld",
4409 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004410 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4411 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 iptr->isn_arg.string); break;
4413 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4414
4415 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004416 case ISN_CHECKTYPE:
4417 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004418 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004419 char *tofree;
4420
Bram Moolenaare32e5162021-01-21 20:21:29 +01004421 if (ct->ct_arg_idx == 0)
4422 smsg("%4d CHECKTYPE %s stack[%d]", current,
4423 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004424 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004425 else
4426 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4427 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004428 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004429 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004430 vim_free(tofree);
4431 break;
4432 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004433 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4434 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4435 iptr->isn_arg.checklen.cl_min_len);
4436 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004437 case ISN_SETTYPE:
4438 {
4439 char *tofree;
4440
4441 smsg("%4d SETTYPE %s", current,
4442 type_name(iptr->isn_arg.type.ct_type, &tofree));
4443 vim_free(tofree);
4444 break;
4445 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004446 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 case ISN_2BOOL: if (iptr->isn_arg.number)
4448 smsg("%4d INVERT (!val)", current);
4449 else
4450 smsg("%4d 2BOOL (!!val)", current);
4451 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004452 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004453 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004454 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004455 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004456 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004457 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004458 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4459 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004460 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004461 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4462 smsg("%4d PUT %c above range",
4463 current, iptr->isn_arg.put.put_regname);
4464 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4465 smsg("%4d PUT %c range",
4466 current, iptr->isn_arg.put.put_regname);
4467 else
4468 smsg("%4d PUT %c %ld", current,
4469 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004470 (long)iptr->isn_arg.put.put_lnum);
4471 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472
Bram Moolenaar02194d22020-10-24 23:08:38 +02004473 // TODO: summarize modifiers
4474 case ISN_CMDMOD:
4475 {
4476 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004477 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004478 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4479
4480 buf = alloc(len + 1);
4481 if (buf != NULL)
4482 {
4483 (void)produce_cmdmods(
4484 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4485 smsg("%4d CMDMOD %s", current, buf);
4486 vim_free(buf);
4487 }
4488 break;
4489 }
4490 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004491
Bram Moolenaarb2049902021-01-24 12:53:53 +01004492 case ISN_PROF_START:
4493 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4494 break;
4495
4496 case ISN_PROF_END:
4497 smsg("%4d PROFILE END", current);
4498 break;
4499
Bram Moolenaar792f7862020-11-23 08:31:18 +01004500 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4501 iptr->isn_arg.unpack.unp_count,
4502 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4503 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004504 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4505 iptr->isn_arg.shuffle.shfl_item,
4506 iptr->isn_arg.shuffle.shfl_up);
4507 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 case ISN_DROP: smsg("%4d DROP", current); break;
4509 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004510
4511 out_flush(); // output one line at a time
4512 ui_breakcheck();
4513 if (got_int)
4514 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516}
4517
4518/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004519 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 * list, etc. Mostly like what JavaScript does, except that empty list and
4521 * empty dictionary are FALSE.
4522 */
4523 int
4524tv2bool(typval_T *tv)
4525{
4526 switch (tv->v_type)
4527 {
4528 case VAR_NUMBER:
4529 return tv->vval.v_number != 0;
4530 case VAR_FLOAT:
4531#ifdef FEAT_FLOAT
4532 return tv->vval.v_float != 0.0;
4533#else
4534 break;
4535#endif
4536 case VAR_PARTIAL:
4537 return tv->vval.v_partial != NULL;
4538 case VAR_FUNC:
4539 case VAR_STRING:
4540 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4541 case VAR_LIST:
4542 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4543 case VAR_DICT:
4544 return tv->vval.v_dict != NULL
4545 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4546 case VAR_BOOL:
4547 case VAR_SPECIAL:
4548 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4549 case VAR_JOB:
4550#ifdef FEAT_JOB_CHANNEL
4551 return tv->vval.v_job != NULL;
4552#else
4553 break;
4554#endif
4555 case VAR_CHANNEL:
4556#ifdef FEAT_JOB_CHANNEL
4557 return tv->vval.v_channel != NULL;
4558#else
4559 break;
4560#endif
4561 case VAR_BLOB:
4562 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4563 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004564 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 case VAR_VOID:
4566 break;
4567 }
4568 return FALSE;
4569}
4570
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004571 void
4572emsg_using_string_as(typval_T *tv, int as_number)
4573{
4574 semsg(_(as_number ? e_using_string_as_number_str
4575 : e_using_string_as_bool_str),
4576 tv->vval.v_string == NULL
4577 ? (char_u *)"" : tv->vval.v_string);
4578}
4579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580/*
4581 * If "tv" is a string give an error and return FAIL.
4582 */
4583 int
4584check_not_string(typval_T *tv)
4585{
4586 if (tv->v_type == VAR_STRING)
4587 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004588 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 clear_tv(tv);
4590 return FAIL;
4591 }
4592 return OK;
4593}
4594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595
4596#endif // FEAT_EVAL