blob: 9bc0d45d34400d3a33682cfb94ba5daab2e060ea [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 Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
70} ectx_T;
71
72// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020073#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 +010074
Bram Moolenaar418f1df2020-08-12 21:34:49 +020075 void
76to_string_error(vartype_T vartype)
77{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020078 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020079}
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010082 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 */
84 static int
85ufunc_argcount(ufunc_T *ufunc)
86{
87 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
88}
89
90/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Set the instruction index, depending on omitted arguments, where the default
92 * values are to be computed. If all optional arguments are present, start
93 * with the function body.
94 * The expression evaluation is at the start of the instructions:
95 * 0 -> EVAL default1
96 * STORE arg[-2]
97 * 1 -> EVAL default2
98 * STORE arg[-1]
99 * 2 -> function body
100 */
101 static void
102init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
103{
104 if (ufunc->uf_def_args.ga_len == 0)
105 ectx->ec_iidx = 0;
106 else
107 {
108 int defcount = ufunc->uf_args.ga_len - argcount;
109
110 // If there is a varargs argument defcount can be negative, no defaults
111 // to evaluate then.
112 if (defcount < 0)
113 defcount = 0;
114 ectx->ec_iidx = ufunc->uf_def_arg_idx[
115 ufunc->uf_def_args.ga_len - defcount];
116 }
117}
118
119/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200120 * Create a new list from "count" items at the bottom of the stack.
121 * When "count" is zero an empty list is added to the stack.
122 */
123 static int
124exe_newlist(int count, ectx_T *ectx)
125{
126 list_T *list = list_alloc_with_items(count);
127 int idx;
128 typval_T *tv;
129
130 if (list == NULL)
131 return FAIL;
132 for (idx = 0; idx < count; ++idx)
133 list_set_item(list, idx, STACK_TV_BOT(idx - count));
134
135 if (count > 0)
136 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200137 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200138 return FAIL;
139 else
140 ++ectx->ec_stack.ga_len;
141 tv = STACK_TV_BOT(-1);
142 tv->v_type = VAR_LIST;
143 tv->vval.v_list = list;
144 ++list->lv_refcount;
145 return OK;
146}
147
148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 * Call compiled function "cdf_idx" from compiled code.
150 *
151 * Stack has:
152 * - current arguments (already there)
153 * - omitted optional argument (default values) added here
154 * - stack frame:
155 * - pointer to calling function
156 * - Index of next instruction in calling function
157 * - previous frame pointer
158 * - reserved space for local variables
159 */
160 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200161call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
165 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200166 int arg_to_add;
167 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200169 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170
171 if (dfunc->df_deleted)
172 {
173 emsg_funcname(e_func_deleted, ufunc->uf_name);
174 return FAIL;
175 }
176
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200177 if (ufunc->uf_va_name != NULL)
178 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 // Stack at time of call with 2 varargs:
181 // normal_arg
182 // optional_arg
183 // vararg_1
184 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200185 // After creating the list:
186 // normal_arg
187 // optional_arg
188 // vararg-list
189 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200190 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200191 // After creating the list
192 // normal_arg
193 // (space for optional_arg)
194 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 vararg_count = argcount - ufunc->uf_args.ga_len;
196 if (vararg_count < 0)
197 vararg_count = 0;
198 else
199 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200201 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200202
203 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 }
205
Bram Moolenaarfe270812020-04-11 22:31:27 +0200206 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 if (arg_to_add < 0)
208 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200209 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200210 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200211 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200212 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200213 return FAIL;
214 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200215 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
216 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 return FAIL;
218
Bram Moolenaarfe270812020-04-11 22:31:27 +0200219 // Move the vararg-list to below the missing optional arguments.
220 if (vararg_count > 0 && arg_to_add > 0)
221 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100222
223 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200224 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200225 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200226 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227
228 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
230 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200231 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
232 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
234 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200235 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200237 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
238 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239
240 // Set execution state to the start of the called function.
241 ectx->ec_dfunc_idx = cdf_idx;
242 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200243 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
244 if (entry != NULL)
245 {
246 // Set the script context to the script where the function was defined.
247 // TODO: save more than the SID?
248 entry->es_save_sid = current_sctx.sc_sid;
249 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
250 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100251
252 // Decide where to start execution, handles optional arguments.
253 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254
255 return OK;
256}
257
258// Get pointer to item in the stack.
259#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
260
261/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200262 * Used when returning from a function: Check if any closure is still
263 * referenced. If so then move the arguments and variables to a separate piece
264 * of stack to be used when the closure is called.
265 * When "free_arguments" is TRUE the arguments are to be freed.
266 * Returns FAIL when out of memory.
267 */
268 static int
269handle_closure_in_use(ectx_T *ectx, int free_arguments)
270{
271 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
272 + ectx->ec_dfunc_idx;
273 int argcount = ufunc_argcount(dfunc->df_ufunc);
274 int top = ectx->ec_frame_idx - argcount;
275 int idx;
276 typval_T *tv;
277 int closure_in_use = FALSE;
278
279 // Check if any created closure is still in use.
280 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
281 {
282 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
283 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200284 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
285 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200286 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200287 int refcount = tv->vval.v_partial->pt_refcount;
288 int i;
289
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200290 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200291 // unreferenced on return.
292 for (i = 0; i < dfunc->df_varcount; ++i)
293 {
294 typval_T *stv = STACK_TV(ectx->ec_frame_idx
295 + STACK_FRAME_SIZE + i);
296 if (stv->v_type == VAR_PARTIAL
297 && tv->vval.v_partial == stv->vval.v_partial)
298 --refcount;
299 }
300 if (refcount > 1)
301 {
302 closure_in_use = TRUE;
303 break;
304 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200305 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200306 }
307
308 if (closure_in_use)
309 {
310 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
311 typval_T *stack;
312
313 // A closure is using the arguments and/or local variables.
314 // Move them to the called function.
315 if (funcstack == NULL)
316 return FAIL;
317 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
318 + dfunc->df_varcount;
319 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
320 funcstack->fs_ga.ga_data = stack;
321 if (stack == NULL)
322 {
323 vim_free(funcstack);
324 return FAIL;
325 }
326
327 // Move or copy the arguments.
328 for (idx = 0; idx < argcount; ++idx)
329 {
330 tv = STACK_TV(top + idx);
331 if (free_arguments)
332 {
333 *(stack + idx) = *tv;
334 tv->v_type = VAR_UNKNOWN;
335 }
336 else
337 copy_tv(tv, stack + idx);
338 }
339 // Move the local variables.
340 for (idx = 0; idx < dfunc->df_varcount; ++idx)
341 {
342 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200343
344 // Do not copy a partial created for a local function.
345 // TODO: this won't work if the closure actually uses it. But when
346 // keeping it it gets complicated: it will create a reference cycle
347 // inside the partial, thus needs special handling for garbage
348 // collection.
349 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
350 {
351 int i;
352 typval_T *ctv;
353
354 for (i = 0; i < dfunc->df_closure_count; ++i)
355 {
356 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
357 + dfunc->df_varcount + i);
358 if (tv->vval.v_partial == ctv->vval.v_partial)
359 break;
360 }
361 if (i < dfunc->df_closure_count)
362 {
363 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
364 VAR_UNKNOWN;
365 continue;
366 }
367 }
368
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200369 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
370 tv->v_type = VAR_UNKNOWN;
371 }
372
373 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
374 {
375 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
376 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200377 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200378 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200379 partial_T *partial = tv->vval.v_partial;
380
381 if (partial->pt_refcount > 1)
382 {
383 ++funcstack->fs_refcount;
384 partial->pt_funcstack = funcstack;
385 partial->pt_ectx_stack = &funcstack->fs_ga;
386 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
387 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200388 }
389 }
390 }
391
392 return OK;
393}
394
395/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396 * Return from the current function.
397 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200398 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399func_return(ectx_T *ectx)
400{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100401 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200402 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
403 + ectx->ec_dfunc_idx;
404 int argcount = ufunc_argcount(dfunc->df_ufunc);
405 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200406 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407
408 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200409 entry = estack_pop();
410 if (entry != NULL)
411 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200413 if (handle_closure_in_use(ectx, TRUE) == FAIL)
414 return FAIL;
415
416 // Clear the arguments.
417 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
418 clear_tv(STACK_TV(idx));
419
420 // Clear local variables and temp values, but not the return value.
421 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100422 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100423 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100424
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100425 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200426 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
427 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
428 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100429 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
430 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100431
432 // Reset the stack to the position before the call, move the return value
433 // to the top of the stack.
434 idx = ectx->ec_stack.ga_len - 1;
435 ectx->ec_stack.ga_len = top + 1;
436 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200437
438 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439}
440
441#undef STACK_TV
442
443/*
444 * Prepare arguments and rettv for calling a builtin or user function.
445 */
446 static int
447call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
448{
449 int idx;
450 typval_T *tv;
451
452 // Move arguments from bottom of the stack to argvars[] and add terminator.
453 for (idx = 0; idx < argcount; ++idx)
454 argvars[idx] = *STACK_TV_BOT(idx - argcount);
455 argvars[argcount].v_type = VAR_UNKNOWN;
456
457 // Result replaces the arguments on the stack.
458 if (argcount > 0)
459 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200460 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100461 return FAIL;
462 else
463 ++ectx->ec_stack.ga_len;
464
465 // Default return value is zero.
466 tv = STACK_TV_BOT(-1);
467 tv->v_type = VAR_NUMBER;
468 tv->vval.v_number = 0;
469
470 return OK;
471}
472
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200473// Ugly global to avoid passing the execution context around through many
474// layers.
475static ectx_T *current_ectx = NULL;
476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477/*
478 * Call a builtin function by index.
479 */
480 static int
481call_bfunc(int func_idx, int argcount, ectx_T *ectx)
482{
483 typval_T argvars[MAX_FUNC_ARGS];
484 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200485 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200486 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
488 if (call_prepare(argcount, argvars, ectx) == FAIL)
489 return FAIL;
490
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200491 // Call the builtin function. Set "current_ectx" so that when it
492 // recursively invokes call_def_function() a closure context can be set.
493 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200495 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496
497 // Clear the arguments.
498 for (idx = 0; idx < argcount; ++idx)
499 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200500
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200501 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200502 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 return OK;
504}
505
506/*
507 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100508 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509 */
510 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100511call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512{
513 typval_T argvars[MAX_FUNC_ARGS];
514 funcexe_T funcexe;
515 int error;
516 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200517 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200519 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200520 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
521 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200522 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100523 {
524 // The function has been compiled, can call it quickly. For a function
525 // that was defined later: we can call it directly next time.
526 if (iptr != NULL)
527 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100528 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100529 iptr->isn_type = ISN_DCALL;
530 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
531 iptr->isn_arg.dfunc.cdf_argcount = argcount;
532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
536 if (call_prepare(argcount, argvars, ectx) == FAIL)
537 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200538 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539 funcexe.evaluate = TRUE;
540
541 // Call the user function. Result goes in last position on the stack.
542 // TODO: add selfdict if there is one
543 error = call_user_func_check(ufunc, argcount, argvars,
544 STACK_TV_BOT(-1), &funcexe, NULL);
545
546 // Clear the arguments.
547 for (idx = 0; idx < argcount; ++idx)
548 clear_tv(&argvars[idx]);
549
550 if (error != FCERR_NONE)
551 {
552 user_func_error(error, ufunc->uf_name);
553 return FAIL;
554 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200555 if (called_emsg > called_emsg_before)
556 // Error other than from calling the function itself.
557 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 return OK;
559}
560
561/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200562 * Return TRUE if an error was given or CTRL-C was pressed.
563 */
564 static int
565vim9_aborting(int prev_called_emsg)
566{
567 return called_emsg > prev_called_emsg || got_int || did_throw;
568}
569
570/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 * Execute a function by "name".
572 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100573 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 * Returns FAIL if not found without an error message.
575 */
576 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100577call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578{
579 ufunc_T *ufunc;
580
581 if (builtin_function(name, -1))
582 {
583 int func_idx = find_internal_func(name);
584
585 if (func_idx < 0)
586 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200587 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return FAIL;
589 return call_bfunc(func_idx, argcount, ectx);
590 }
591
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200592 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200593
594 if (ufunc == NULL)
595 {
596 int called_emsg_before = called_emsg;
597
598 if (script_autoload(name, TRUE))
599 // loaded a package, search for the function again
600 ufunc = find_func(name, FALSE, NULL);
601 if (vim9_aborting(called_emsg_before))
602 return FAIL; // bail out if loading the script caused an error
603 }
604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100606 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607
608 return FAIL;
609}
610
611 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200612call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200614 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200615 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200617 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618
619 if (tv->v_type == VAR_PARTIAL)
620 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200621 partial_T *pt = tv->vval.v_partial;
622 int i;
623
624 if (pt->pt_argc > 0)
625 {
626 // Make space for arguments from the partial, shift the "argcount"
627 // arguments up.
628 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
629 return FAIL;
630 for (i = 1; i <= argcount; ++i)
631 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
632 ectx->ec_stack.ga_len += pt->pt_argc;
633 argcount += pt->pt_argc;
634
635 // copy the arguments from the partial onto the stack
636 for (i = 0; i < pt->pt_argc; ++i)
637 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639
640 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200641 {
642 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
643
644 // closure may need the function context where it was defined
645 ectx->ec_outer_stack = pt->pt_ectx_stack;
646 ectx->ec_outer_frame = pt->pt_ectx_frame;
647
648 return ret;
649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650 name = pt->pt_name;
651 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200652 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200654 if (name != NULL)
655 {
656 char_u fname_buf[FLEN_FIXED + 1];
657 char_u *tofree = NULL;
658 int error = FCERR_NONE;
659 char_u *fname;
660
661 // May need to translate <SNR>123_ to K_SNR.
662 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
663 if (error != FCERR_NONE)
664 res = FAIL;
665 else
666 res = call_by_name(fname, argcount, ectx, NULL);
667 vim_free(tofree);
668 }
669
670 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 {
672 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200673 semsg(_(e_unknownfunc),
674 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 return FAIL;
676 }
677 return OK;
678}
679
680/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100681 * Store "tv" in variable "name".
682 * This is for s: and g: variables.
683 */
684 static void
685store_var(char_u *name, typval_T *tv)
686{
687 funccal_entry_T entry;
688
689 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200690 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100691 restore_funccal();
692}
693
Bram Moolenaard3aac292020-04-19 14:32:17 +0200694
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100695/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 * Execute a function by "name".
697 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100698 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 */
700 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100701call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702{
Bram Moolenaared677f52020-08-12 16:38:10 +0200703 int called_emsg_before = called_emsg;
704 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705
Bram Moolenaared677f52020-08-12 16:38:10 +0200706 res = call_by_name(name, argcount, ectx, iptr);
707 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200709 dictitem_T *v;
710
711 v = find_var(name, NULL, FALSE);
712 if (v == NULL)
713 {
714 semsg(_(e_unknownfunc), name);
715 return FAIL;
716 }
717 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
718 {
719 semsg(_(e_unknownfunc), name);
720 return FAIL;
721 }
722 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200724 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725}
726
727/*
728 * Call a "def" function from old Vim script.
729 * Return OK or FAIL.
730 */
731 int
732call_def_function(
733 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200734 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200736 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737 typval_T *rettv) // return value
738{
739 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200740 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200741 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742 typval_T *tv;
743 int idx;
744 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100745 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200746 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200747 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200748 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749
750// Get pointer to item in the stack.
751#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
752
753// Get pointer to item at the bottom of the stack, -1 is the bottom.
754#undef STACK_TV_BOT
755#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
756
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200757// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200758#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 +0100759
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200760// Like STACK_TV_VAR but use the outer scope
761#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
762
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200763 if (ufunc->uf_def_status == UF_NOT_COMPILED
764 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200765 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200766 {
767 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200768 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200769 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200770 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200771 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200772
Bram Moolenaar09689a02020-05-09 22:50:08 +0200773 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200774 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200775 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
776 + ufunc->uf_dfunc_idx;
777 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200778 {
779 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200780 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200781 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200782 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200784 CLEAR_FIELD(ectx);
785 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
786 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
787 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
788 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
790
791 // Put arguments on the stack.
792 for (idx = 0; idx < argc; ++idx)
793 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200794 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200795 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
796 == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200797 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 copy_tv(&argv[idx], STACK_TV_BOT(0));
799 ++ectx.ec_stack.ga_len;
800 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200801
802 // Turn varargs into a list. Empty list if no args.
803 if (ufunc->uf_va_name != NULL)
804 {
805 int vararg_count = argc - ufunc->uf_args.ga_len;
806
807 if (vararg_count < 0)
808 vararg_count = 0;
809 else
810 argc -= vararg_count;
811 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200812 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200813
814 // Check the type of the list items.
815 tv = STACK_TV_BOT(-1);
816 if (ufunc->uf_va_type != NULL
817 && ufunc->uf_va_type->tt_member != &t_any
818 && tv->vval.v_list != NULL)
819 {
820 type_T *expected = ufunc->uf_va_type->tt_member;
821 listitem_T *li = tv->vval.v_list->lv_first;
822
823 for (idx = 0; idx < vararg_count; ++idx)
824 {
825 if (check_typval_type(expected, &li->li_tv) == FAIL)
826 goto failed_early;
827 li = li->li_next;
828 }
829 }
830
Bram Moolenaar23e03252020-04-12 22:22:31 +0200831 if (defcount > 0)
832 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200833 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200834 --ectx.ec_stack.ga_len;
835 }
836
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100837 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200838 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100839 if (defcount > 0)
840 for (idx = 0; idx < defcount; ++idx)
841 {
842 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
843 ++ectx.ec_stack.ga_len;
844 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200845 if (ufunc->uf_va_name != NULL)
846 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847
848 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200849 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
850 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200852 if (partial != NULL)
853 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200854 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
855 {
856 // TODO: is this always the right way?
857 ectx.ec_outer_stack = &current_ectx->ec_stack;
858 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
859 }
860 else
861 {
862 ectx.ec_outer_stack = partial->pt_ectx_stack;
863 ectx.ec_outer_frame = partial->pt_ectx_frame;
864 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200865 }
866
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 // dummy frame entries
868 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
869 {
870 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
871 ++ectx.ec_stack.ga_len;
872 }
873
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200874 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200875 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200876 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
877 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200878 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200880 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200881 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200882 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200883
884 ectx.ec_instr = dfunc->df_instr;
885 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100886
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200887 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200888 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200889 estack_push_ufunc(ufunc, 1);
890 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200891 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
892
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100893 // Decide where to start execution, handles optional arguments.
894 init_instr_idx(ufunc, argc, &ectx);
895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 for (;;)
897 {
898 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100899
Bram Moolenaar270d0382020-05-15 21:42:53 +0200900 if (++breakcheck_count >= 100)
901 {
902 line_breakcheck();
903 breakcheck_count = 0;
904 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100905 if (got_int)
906 {
907 // Turn CTRL-C into an exception.
908 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100909 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100910 goto failed;
911 did_throw = TRUE;
912 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913
Bram Moolenaara26b9702020-04-18 19:53:28 +0200914 if (did_emsg && msg_list != NULL && *msg_list != NULL)
915 {
916 // Turn an error message into an exception.
917 did_emsg = FALSE;
918 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
919 goto failed;
920 did_throw = TRUE;
921 *msg_list = NULL;
922 }
923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 if (did_throw && !ectx.ec_in_catch)
925 {
926 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100927 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928
929 // An exception jumps to the first catch, finally, or returns from
930 // the current function.
931 if (trystack->ga_len > 0)
932 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200933 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 {
935 // jump to ":catch" or ":finally"
936 ectx.ec_in_catch = TRUE;
937 ectx.ec_iidx = trycmd->tcd_catch_idx;
938 }
939 else
940 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200941 // Not inside try or need to return from current functions.
942 // Push a dummy return value.
943 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
944 goto failed;
945 tv = STACK_TV_BOT(0);
946 tv->v_type = VAR_NUMBER;
947 tv->vval.v_number = 0;
948 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200949 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200951 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100952 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200953 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
954 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 goto done;
956 }
957
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200958 if (func_return(&ectx) == FAIL)
959 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960 }
961 continue;
962 }
963
964 iptr = &ectx.ec_instr[ectx.ec_iidx++];
965 switch (iptr->isn_type)
966 {
967 // execute Ex command line
968 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200969 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 do_cmdline_cmd(iptr->isn_arg.string);
971 break;
972
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200973 // execute Ex command from pieces on the stack
974 case ISN_EXECCONCAT:
975 {
976 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200977 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200978 int pass;
979 int i;
980 char_u *cmd = NULL;
981 char_u *str;
982
983 for (pass = 1; pass <= 2; ++pass)
984 {
985 for (i = 0; i < count; ++i)
986 {
987 tv = STACK_TV_BOT(i - count);
988 str = tv->vval.v_string;
989 if (str != NULL && *str != NUL)
990 {
991 if (pass == 2)
992 STRCPY(cmd + len, str);
993 len += STRLEN(str);
994 }
995 if (pass == 2)
996 clear_tv(tv);
997 }
998 if (pass == 1)
999 {
1000 cmd = alloc(len + 1);
1001 if (cmd == NULL)
1002 goto failed;
1003 len = 0;
1004 }
1005 }
1006
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001007 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001008 do_cmdline_cmd(cmd);
1009 vim_free(cmd);
1010 }
1011 break;
1012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 // execute :echo {string} ...
1014 case ISN_ECHO:
1015 {
1016 int count = iptr->isn_arg.echo.echo_count;
1017 int atstart = TRUE;
1018 int needclr = TRUE;
1019
1020 for (idx = 0; idx < count; ++idx)
1021 {
1022 tv = STACK_TV_BOT(idx - count);
1023 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1024 &atstart, &needclr);
1025 clear_tv(tv);
1026 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001027 if (needclr)
1028 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 ectx.ec_stack.ga_len -= count;
1030 }
1031 break;
1032
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001033 // :execute {string} ...
1034 // :echomsg {string} ...
1035 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001036 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001037 case ISN_ECHOMSG:
1038 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001039 {
1040 int count = iptr->isn_arg.number;
1041 garray_T ga;
1042 char_u buf[NUMBUFLEN];
1043 char_u *p;
1044 int len;
1045 int failed = FALSE;
1046
1047 ga_init2(&ga, 1, 80);
1048 for (idx = 0; idx < count; ++idx)
1049 {
1050 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001051 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001052 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001053 if (tv->v_type == VAR_CHANNEL
1054 || tv->v_type == VAR_JOB)
1055 {
1056 SOURCING_LNUM = iptr->isn_lnum;
1057 emsg(_(e_inval_string));
1058 break;
1059 }
1060 else
1061 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001062 }
1063 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001064 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001065
1066 len = (int)STRLEN(p);
1067 if (ga_grow(&ga, len + 2) == FAIL)
1068 failed = TRUE;
1069 else
1070 {
1071 if (ga.ga_len > 0)
1072 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1073 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1074 ga.ga_len += len;
1075 }
1076 clear_tv(tv);
1077 }
1078 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001079 if (failed)
1080 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001081
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001082 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001083 {
1084 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001085 {
1086 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001087 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001088 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001089 else
1090 {
1091 msg_sb_eol();
1092 if (iptr->isn_type == ISN_ECHOMSG)
1093 {
1094 msg_attr(ga.ga_data, echo_attr);
1095 out_flush();
1096 }
1097 else
1098 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001099 SOURCING_LNUM = iptr->isn_lnum;
1100 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001101 }
1102 }
1103 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001104 ga_clear(&ga);
1105 }
1106 break;
1107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 // load local variable or argument
1109 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001110 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 goto failed;
1112 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1113 ++ectx.ec_stack.ga_len;
1114 break;
1115
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001116 // load variable or argument from outer scope
1117 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001118 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001119 goto failed;
1120 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1121 STACK_TV_BOT(0));
1122 ++ectx.ec_stack.ga_len;
1123 break;
1124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 // load v: variable
1126 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001127 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 goto failed;
1129 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1130 ++ectx.ec_stack.ga_len;
1131 break;
1132
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001133 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 case ISN_LOADSCRIPT:
1135 {
1136 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001137 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 svar_T *sv;
1139
1140 sv = ((svar_T *)si->sn_var_vals.ga_data)
1141 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001142 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143 goto failed;
1144 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1145 ++ectx.ec_stack.ga_len;
1146 }
1147 break;
1148
1149 // load s: variable in old script
1150 case ISN_LOADS:
1151 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001152 hashtab_T *ht = &SCRIPT_VARS(
1153 iptr->isn_arg.loadstore.ls_sid);
1154 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 if (di == NULL)
1158 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001159 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001160 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001161 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 }
1163 else
1164 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001165 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 goto failed;
1167 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1168 ++ectx.ec_stack.ga_len;
1169 }
1170 }
1171 break;
1172
Bram Moolenaard3aac292020-04-19 14:32:17 +02001173 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001175 case ISN_LOADB:
1176 case ISN_LOADW:
1177 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001179 dictitem_T *di = NULL;
1180 hashtab_T *ht = NULL;
1181 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001182
Bram Moolenaard3aac292020-04-19 14:32:17 +02001183 switch (iptr->isn_type)
1184 {
1185 case ISN_LOADG:
1186 ht = get_globvar_ht();
1187 namespace = 'g';
1188 break;
1189 case ISN_LOADB:
1190 ht = &curbuf->b_vars->dv_hashtab;
1191 namespace = 'b';
1192 break;
1193 case ISN_LOADW:
1194 ht = &curwin->w_vars->dv_hashtab;
1195 namespace = 'w';
1196 break;
1197 case ISN_LOADT:
1198 ht = &curtab->tp_vars->dv_hashtab;
1199 namespace = 't';
1200 break;
1201 default: // Cannot reach here
1202 goto failed;
1203 }
1204 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 if (di == NULL)
1207 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001208 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001209 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001210 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001211 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 }
1213 else
1214 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001215 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 goto failed;
1217 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1218 ++ectx.ec_stack.ga_len;
1219 }
1220 }
1221 break;
1222
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001223 // load g:/b:/w:/t: namespace
1224 case ISN_LOADGDICT:
1225 case ISN_LOADBDICT:
1226 case ISN_LOADWDICT:
1227 case ISN_LOADTDICT:
1228 {
1229 dict_T *d = NULL;
1230
1231 switch (iptr->isn_type)
1232 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001233 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1234 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1235 case ISN_LOADWDICT: d = curwin->w_vars; break;
1236 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001237 default: // Cannot reach here
1238 goto failed;
1239 }
1240 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1241 goto failed;
1242 tv = STACK_TV_BOT(0);
1243 tv->v_type = VAR_DICT;
1244 tv->v_lock = 0;
1245 tv->vval.v_dict = d;
1246 ++ectx.ec_stack.ga_len;
1247 }
1248 break;
1249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 // load &option
1251 case ISN_LOADOPT:
1252 {
1253 typval_T optval;
1254 char_u *name = iptr->isn_arg.string;
1255
Bram Moolenaara8c17702020-04-01 21:17:24 +02001256 // This is not expected to fail, name is checked during
1257 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001258 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001260 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001261 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 *STACK_TV_BOT(0) = optval;
1263 ++ectx.ec_stack.ga_len;
1264 }
1265 break;
1266
1267 // load $ENV
1268 case ISN_LOADENV:
1269 {
1270 typval_T optval;
1271 char_u *name = iptr->isn_arg.string;
1272
Bram Moolenaar270d0382020-05-15 21:42:53 +02001273 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001275 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001276 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 *STACK_TV_BOT(0) = optval;
1278 ++ectx.ec_stack.ga_len;
1279 }
1280 break;
1281
1282 // load @register
1283 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001284 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 goto failed;
1286 tv = STACK_TV_BOT(0);
1287 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001288 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 tv->vval.v_string = get_reg_contents(
1290 iptr->isn_arg.number, GREG_EXPR_SRC);
1291 ++ectx.ec_stack.ga_len;
1292 break;
1293
1294 // store local variable
1295 case ISN_STORE:
1296 --ectx.ec_stack.ga_len;
1297 tv = STACK_TV_VAR(iptr->isn_arg.number);
1298 clear_tv(tv);
1299 *tv = *STACK_TV_BOT(0);
1300 break;
1301
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001302 // store variable or argument in outer scope
1303 case ISN_STOREOUTER:
1304 --ectx.ec_stack.ga_len;
1305 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1306 clear_tv(tv);
1307 *tv = *STACK_TV_BOT(0);
1308 break;
1309
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001310 // store s: variable in old script
1311 case ISN_STORES:
1312 {
1313 hashtab_T *ht = &SCRIPT_VARS(
1314 iptr->isn_arg.loadstore.ls_sid);
1315 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001316 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001317
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001318 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001319 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001320 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001321 else
1322 {
1323 clear_tv(&di->di_tv);
1324 di->di_tv = *STACK_TV_BOT(0);
1325 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001326 }
1327 break;
1328
1329 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 case ISN_STORESCRIPT:
1331 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001332 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 iptr->isn_arg.script.script_sid);
1334 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1335 + iptr->isn_arg.script.script_idx;
1336
1337 --ectx.ec_stack.ga_len;
1338 clear_tv(sv->sv_tv);
1339 *sv->sv_tv = *STACK_TV_BOT(0);
1340 }
1341 break;
1342
1343 // store option
1344 case ISN_STOREOPT:
1345 {
1346 long n = 0;
1347 char_u *s = NULL;
1348 char *msg;
1349
1350 --ectx.ec_stack.ga_len;
1351 tv = STACK_TV_BOT(0);
1352 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001353 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001355 if (s == NULL)
1356 s = (char_u *)"";
1357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001359 // must be VAR_NUMBER, CHECKTYPE makes sure
1360 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1362 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001363 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 if (msg != NULL)
1365 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001366 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001368 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 }
1371 break;
1372
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001373 // store $ENV
1374 case ISN_STOREENV:
1375 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001376 tv = STACK_TV_BOT(0);
1377 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1378 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001379 break;
1380
1381 // store @r
1382 case ISN_STOREREG:
1383 {
1384 int reg = iptr->isn_arg.number;
1385
1386 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001387 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001388 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001389 tv_get_string(tv), -1, FALSE);
1390 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001391 }
1392 break;
1393
1394 // store v: variable
1395 case ISN_STOREV:
1396 --ectx.ec_stack.ga_len;
1397 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1398 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001399 // should not happen, type is checked when compiling
1400 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001401 break;
1402
Bram Moolenaard3aac292020-04-19 14:32:17 +02001403 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001405 case ISN_STOREB:
1406 case ISN_STOREW:
1407 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 {
1409 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001410 hashtab_T *ht;
1411 switch (iptr->isn_type)
1412 {
1413 case ISN_STOREG:
1414 ht = get_globvar_ht();
1415 break;
1416 case ISN_STOREB:
1417 ht = &curbuf->b_vars->dv_hashtab;
1418 break;
1419 case ISN_STOREW:
1420 ht = &curwin->w_vars->dv_hashtab;
1421 break;
1422 case ISN_STORET:
1423 ht = &curtab->tp_vars->dv_hashtab;
1424 break;
1425 default: // Cannot reach here
1426 goto failed;
1427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428
1429 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001430 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001432 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 else
1434 {
1435 clear_tv(&di->di_tv);
1436 di->di_tv = *STACK_TV_BOT(0);
1437 }
1438 }
1439 break;
1440
1441 // store number in local variable
1442 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001443 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 clear_tv(tv);
1445 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001446 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 break;
1448
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001449 // store value in list variable
1450 case ISN_STORELIST:
1451 {
1452 typval_T *tv_idx = STACK_TV_BOT(-2);
1453 varnumber_T lidx = tv_idx->vval.v_number;
1454 typval_T *tv_list = STACK_TV_BOT(-1);
1455 list_T *list = tv_list->vval.v_list;
1456
1457 if (lidx < 0 && list->lv_len + lidx >= 0)
1458 // negative index is relative to the end
1459 lidx = list->lv_len + lidx;
1460 if (lidx < 0 || lidx > list->lv_len)
1461 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001462 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001463 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001464 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001465 }
1466 tv = STACK_TV_BOT(-3);
1467 if (lidx < list->lv_len)
1468 {
1469 listitem_T *li = list_find(list, lidx);
1470
1471 // overwrite existing list item
1472 clear_tv(&li->li_tv);
1473 li->li_tv = *tv;
1474 }
1475 else
1476 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001477 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001478 if (list_append_tv(list, tv) == FAIL)
1479 goto failed;
1480 clear_tv(tv);
1481 }
1482 clear_tv(tv_idx);
1483 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001484 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001485 }
1486 break;
1487
1488 // store value in dict variable
1489 case ISN_STOREDICT:
1490 {
1491 typval_T *tv_key = STACK_TV_BOT(-2);
1492 char_u *key = tv_key->vval.v_string;
1493 typval_T *tv_dict = STACK_TV_BOT(-1);
1494 dict_T *dict = tv_dict->vval.v_dict;
1495 dictitem_T *di;
1496
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001497 if (dict == NULL)
1498 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001499 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001500 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001501 goto on_error;
1502 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001503 if (key == NULL)
1504 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001505 tv = STACK_TV_BOT(-3);
1506 di = dict_find(dict, key, -1);
1507 if (di != NULL)
1508 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001509 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001510 clear_tv(&di->di_tv);
1511 di->di_tv = *tv;
1512 }
1513 else
1514 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001515 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001516 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1517 goto failed;
1518 clear_tv(tv);
1519 }
1520 clear_tv(tv_key);
1521 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001522 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001523 }
1524 break;
1525
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 // push constant
1527 case ISN_PUSHNR:
1528 case ISN_PUSHBOOL:
1529 case ISN_PUSHSPEC:
1530 case ISN_PUSHF:
1531 case ISN_PUSHS:
1532 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001533 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001534 case ISN_PUSHCHANNEL:
1535 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001536 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 goto failed;
1538 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001539 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 ++ectx.ec_stack.ga_len;
1541 switch (iptr->isn_type)
1542 {
1543 case ISN_PUSHNR:
1544 tv->v_type = VAR_NUMBER;
1545 tv->vval.v_number = iptr->isn_arg.number;
1546 break;
1547 case ISN_PUSHBOOL:
1548 tv->v_type = VAR_BOOL;
1549 tv->vval.v_number = iptr->isn_arg.number;
1550 break;
1551 case ISN_PUSHSPEC:
1552 tv->v_type = VAR_SPECIAL;
1553 tv->vval.v_number = iptr->isn_arg.number;
1554 break;
1555#ifdef FEAT_FLOAT
1556 case ISN_PUSHF:
1557 tv->v_type = VAR_FLOAT;
1558 tv->vval.v_float = iptr->isn_arg.fnumber;
1559 break;
1560#endif
1561 case ISN_PUSHBLOB:
1562 blob_copy(iptr->isn_arg.blob, tv);
1563 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001564 case ISN_PUSHFUNC:
1565 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001566 if (iptr->isn_arg.string == NULL)
1567 tv->vval.v_string = NULL;
1568 else
1569 tv->vval.v_string =
1570 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001571 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001572 case ISN_PUSHCHANNEL:
1573#ifdef FEAT_JOB_CHANNEL
1574 tv->v_type = VAR_CHANNEL;
1575 tv->vval.v_channel = iptr->isn_arg.channel;
1576 if (tv->vval.v_channel != NULL)
1577 ++tv->vval.v_channel->ch_refcount;
1578#endif
1579 break;
1580 case ISN_PUSHJOB:
1581#ifdef FEAT_JOB_CHANNEL
1582 tv->v_type = VAR_JOB;
1583 tv->vval.v_job = iptr->isn_arg.job;
1584 if (tv->vval.v_job != NULL)
1585 ++tv->vval.v_job->jv_refcount;
1586#endif
1587 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 default:
1589 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001590 tv->vval.v_string = vim_strsave(
1591 iptr->isn_arg.string == NULL
1592 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 }
1594 break;
1595
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001596 case ISN_UNLET:
1597 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1598 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001599 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001600 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001601 case ISN_UNLETENV:
1602 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1603 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 // create a list from items on the stack; uses a single allocation
1606 // for the list header and the items
1607 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001608 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1609 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 break;
1611
1612 // create a dict from items on the stack
1613 case ISN_NEWDICT:
1614 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001615 int count = iptr->isn_arg.number;
1616 dict_T *dict = dict_alloc();
1617 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618
1619 if (dict == NULL)
1620 goto failed;
1621 for (idx = 0; idx < count; ++idx)
1622 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001623 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001625 // check key is unique
1626 item = dict_find(dict, tv->vval.v_string, -1);
1627 if (item != NULL)
1628 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001629 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001630 semsg(_(e_duplicate_key), tv->vval.v_string);
1631 dict_unref(dict);
1632 goto on_error;
1633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 item = dictitem_alloc(tv->vval.v_string);
1635 clear_tv(tv);
1636 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001637 {
1638 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001640 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1642 item->di_tv.v_lock = 0;
1643 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001644 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001645 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001646 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 }
1650
1651 if (count > 0)
1652 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001653 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 goto failed;
1655 else
1656 ++ectx.ec_stack.ga_len;
1657 tv = STACK_TV_BOT(-1);
1658 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001659 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 tv->vval.v_dict = dict;
1661 ++dict->dv_refcount;
1662 }
1663 break;
1664
1665 // call a :def function
1666 case ISN_DCALL:
1667 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1668 iptr->isn_arg.dfunc.cdf_argcount,
1669 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001670 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 break;
1672
1673 // call a builtin function
1674 case ISN_BCALL:
1675 SOURCING_LNUM = iptr->isn_lnum;
1676 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1677 iptr->isn_arg.bfunc.cbf_argcount,
1678 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001679 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 break;
1681
1682 // call a funcref or partial
1683 case ISN_PCALL:
1684 {
1685 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1686 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001687 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688
1689 SOURCING_LNUM = iptr->isn_lnum;
1690 if (pfunc->cpf_top)
1691 {
1692 // funcref is above the arguments
1693 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1694 }
1695 else
1696 {
1697 // Get the funcref from the stack.
1698 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001699 partial_tv = *STACK_TV_BOT(0);
1700 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 }
1702 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001703 if (tv == &partial_tv)
1704 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001706 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 }
1708 break;
1709
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001710 case ISN_PCALL_END:
1711 // PCALL finished, arguments have been consumed and replaced by
1712 // the return value. Now clear the funcref from the stack,
1713 // and move the return value in its place.
1714 --ectx.ec_stack.ga_len;
1715 clear_tv(STACK_TV_BOT(-1));
1716 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1717 break;
1718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 // call a user defined function or funcref/partial
1720 case ISN_UCALL:
1721 {
1722 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1723
1724 SOURCING_LNUM = iptr->isn_lnum;
1725 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001726 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001727 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 }
1729 break;
1730
1731 // return from a :def function call
1732 case ISN_RETURN:
1733 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001734 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001735 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001736
1737 if (trystack->ga_len > 0)
1738 trycmd = ((trycmd_T *)trystack->ga_data)
1739 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001740 if (trycmd != NULL
1741 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 && trycmd->tcd_finally_idx != 0)
1743 {
1744 // jump to ":finally"
1745 ectx.ec_iidx = trycmd->tcd_finally_idx;
1746 trycmd->tcd_return = TRUE;
1747 }
1748 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001749 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 }
1751 break;
1752
1753 // push a function reference to a compiled function
1754 case ISN_FUNCREF:
1755 {
1756 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001757 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758
1759 pt = ALLOC_CLEAR_ONE(partial_T);
1760 if (pt == NULL)
1761 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001762 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001763 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001764 vim_free(pt);
1765 goto failed;
1766 }
1767 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1768 + iptr->isn_arg.funcref.fr_func;
1769 pt->pt_func = pt_dfunc->df_ufunc;
1770 pt->pt_refcount = 1;
1771 ++pt_dfunc->df_ufunc->uf_refcount;
1772
1773 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1774 {
1775 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1776 + ectx.ec_dfunc_idx;
1777
1778 // The closure needs to find arguments and local
1779 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001780 pt->pt_ectx_stack = &ectx.ec_stack;
1781 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001782
1783 // If this function returns and the closure is still
1784 // used, we need to make a copy of the context
1785 // (arguments and local variables). Store a reference
1786 // to the partial so we can handle that.
1787 ++pt->pt_refcount;
1788 tv = STACK_TV_VAR(dfunc->df_varcount
1789 + iptr->isn_arg.funcref.fr_var_idx);
1790 if (tv->v_type == VAR_PARTIAL)
1791 {
1792 // TODO: use a garray_T on ectx.
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001793 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001794 emsg("Multiple closures not supported yet");
1795 goto failed;
1796 }
1797 tv->v_type = VAR_PARTIAL;
1798 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001799 }
1800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 tv = STACK_TV_BOT(0);
1802 ++ectx.ec_stack.ga_len;
1803 tv->vval.v_partial = pt;
1804 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001805 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 }
1807 break;
1808
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001809 // Create a global function from a lambda.
1810 case ISN_NEWFUNC:
1811 {
1812 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1813
1814 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1815 }
1816 break;
1817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 // jump if a condition is met
1819 case ISN_JUMP:
1820 {
1821 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1822 int jump = TRUE;
1823
1824 if (when != JUMP_ALWAYS)
1825 {
1826 tv = STACK_TV_BOT(-1);
1827 jump = tv2bool(tv);
1828 if (when == JUMP_IF_FALSE
1829 || when == JUMP_AND_KEEP_IF_FALSE)
1830 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001831 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 {
1833 // drop the value from the stack
1834 clear_tv(tv);
1835 --ectx.ec_stack.ga_len;
1836 }
1837 }
1838 if (jump)
1839 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1840 }
1841 break;
1842
1843 // top of a for loop
1844 case ISN_FOR:
1845 {
1846 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1847 typval_T *idxtv =
1848 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1849
1850 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001851 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 goto failed;
1853 if (++idxtv->vval.v_number >= list->lv_len)
1854 // past the end of the list, jump to "endfor"
1855 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1856 else if (list->lv_first == &range_list_item)
1857 {
1858 // non-materialized range() list
1859 tv = STACK_TV_BOT(0);
1860 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001861 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 tv->vval.v_number = list_find_nr(
1863 list, idxtv->vval.v_number, NULL);
1864 ++ectx.ec_stack.ga_len;
1865 }
1866 else
1867 {
1868 listitem_T *li = list_find(list, idxtv->vval.v_number);
1869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1871 ++ectx.ec_stack.ga_len;
1872 }
1873 }
1874 break;
1875
1876 // start of ":try" block
1877 case ISN_TRY:
1878 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001879 trycmd_T *trycmd = NULL;
1880
Bram Moolenaar270d0382020-05-15 21:42:53 +02001881 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 goto failed;
1883 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1884 + ectx.ec_trystack.ga_len;
1885 ++ectx.ec_trystack.ga_len;
1886 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001887 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1889 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001890 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 }
1892 break;
1893
1894 case ISN_PUSHEXC:
1895 if (current_exception == NULL)
1896 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001897 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 iemsg("Evaluating catch while current_exception is NULL");
1899 goto failed;
1900 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001901 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 goto failed;
1903 tv = STACK_TV_BOT(0);
1904 ++ectx.ec_stack.ga_len;
1905 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001906 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 tv->vval.v_string = vim_strsave(
1908 (char_u *)current_exception->value);
1909 break;
1910
1911 case ISN_CATCH:
1912 {
1913 garray_T *trystack = &ectx.ec_trystack;
1914
1915 if (trystack->ga_len > 0)
1916 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001917 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 + trystack->ga_len - 1;
1919 trycmd->tcd_caught = TRUE;
1920 }
1921 did_emsg = got_int = did_throw = FALSE;
1922 catch_exception(current_exception);
1923 }
1924 break;
1925
1926 // end of ":try" block
1927 case ISN_ENDTRY:
1928 {
1929 garray_T *trystack = &ectx.ec_trystack;
1930
1931 if (trystack->ga_len > 0)
1932 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001933 trycmd_T *trycmd = NULL;
1934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 --trystack->ga_len;
1936 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001937 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 trycmd = ((trycmd_T *)trystack->ga_data)
1939 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001940 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 {
1942 // discard the exception
1943 if (caught_stack == current_exception)
1944 caught_stack = caught_stack->caught;
1945 discard_current_exception();
1946 }
1947
1948 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001949 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 }
1951 }
1952 break;
1953
1954 case ISN_THROW:
1955 --ectx.ec_stack.ga_len;
1956 tv = STACK_TV_BOT(0);
1957 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1958 {
1959 vim_free(tv->vval.v_string);
1960 goto failed;
1961 }
1962 did_throw = TRUE;
1963 break;
1964
1965 // compare with special values
1966 case ISN_COMPAREBOOL:
1967 case ISN_COMPARESPECIAL:
1968 {
1969 typval_T *tv1 = STACK_TV_BOT(-2);
1970 typval_T *tv2 = STACK_TV_BOT(-1);
1971 varnumber_T arg1 = tv1->vval.v_number;
1972 varnumber_T arg2 = tv2->vval.v_number;
1973 int res;
1974
1975 switch (iptr->isn_arg.op.op_type)
1976 {
1977 case EXPR_EQUAL: res = arg1 == arg2; break;
1978 case EXPR_NEQUAL: res = arg1 != arg2; break;
1979 default: res = 0; break;
1980 }
1981
1982 --ectx.ec_stack.ga_len;
1983 tv1->v_type = VAR_BOOL;
1984 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1985 }
1986 break;
1987
1988 // Operation with two number arguments
1989 case ISN_OPNR:
1990 case ISN_COMPARENR:
1991 {
1992 typval_T *tv1 = STACK_TV_BOT(-2);
1993 typval_T *tv2 = STACK_TV_BOT(-1);
1994 varnumber_T arg1 = tv1->vval.v_number;
1995 varnumber_T arg2 = tv2->vval.v_number;
1996 varnumber_T res;
1997
1998 switch (iptr->isn_arg.op.op_type)
1999 {
2000 case EXPR_MULT: res = arg1 * arg2; break;
2001 case EXPR_DIV: res = arg1 / arg2; break;
2002 case EXPR_REM: res = arg1 % arg2; break;
2003 case EXPR_SUB: res = arg1 - arg2; break;
2004 case EXPR_ADD: res = arg1 + arg2; break;
2005
2006 case EXPR_EQUAL: res = arg1 == arg2; break;
2007 case EXPR_NEQUAL: res = arg1 != arg2; break;
2008 case EXPR_GREATER: res = arg1 > arg2; break;
2009 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2010 case EXPR_SMALLER: res = arg1 < arg2; break;
2011 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2012 default: res = 0; break;
2013 }
2014
2015 --ectx.ec_stack.ga_len;
2016 if (iptr->isn_type == ISN_COMPARENR)
2017 {
2018 tv1->v_type = VAR_BOOL;
2019 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2020 }
2021 else
2022 tv1->vval.v_number = res;
2023 }
2024 break;
2025
2026 // Computation with two float arguments
2027 case ISN_OPFLOAT:
2028 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002029#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 {
2031 typval_T *tv1 = STACK_TV_BOT(-2);
2032 typval_T *tv2 = STACK_TV_BOT(-1);
2033 float_T arg1 = tv1->vval.v_float;
2034 float_T arg2 = tv2->vval.v_float;
2035 float_T res = 0;
2036 int cmp = FALSE;
2037
2038 switch (iptr->isn_arg.op.op_type)
2039 {
2040 case EXPR_MULT: res = arg1 * arg2; break;
2041 case EXPR_DIV: res = arg1 / arg2; break;
2042 case EXPR_SUB: res = arg1 - arg2; break;
2043 case EXPR_ADD: res = arg1 + arg2; break;
2044
2045 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2046 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2047 case EXPR_GREATER: cmp = arg1 > arg2; break;
2048 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2049 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2050 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2051 default: cmp = 0; break;
2052 }
2053 --ectx.ec_stack.ga_len;
2054 if (iptr->isn_type == ISN_COMPAREFLOAT)
2055 {
2056 tv1->v_type = VAR_BOOL;
2057 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2058 }
2059 else
2060 tv1->vval.v_float = res;
2061 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002062#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 break;
2064
2065 case ISN_COMPARELIST:
2066 {
2067 typval_T *tv1 = STACK_TV_BOT(-2);
2068 typval_T *tv2 = STACK_TV_BOT(-1);
2069 list_T *arg1 = tv1->vval.v_list;
2070 list_T *arg2 = tv2->vval.v_list;
2071 int cmp = FALSE;
2072 int ic = iptr->isn_arg.op.op_ic;
2073
2074 switch (iptr->isn_arg.op.op_type)
2075 {
2076 case EXPR_EQUAL: cmp =
2077 list_equal(arg1, arg2, ic, FALSE); break;
2078 case EXPR_NEQUAL: cmp =
2079 !list_equal(arg1, arg2, ic, FALSE); break;
2080 case EXPR_IS: cmp = arg1 == arg2; break;
2081 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2082 default: cmp = 0; break;
2083 }
2084 --ectx.ec_stack.ga_len;
2085 clear_tv(tv1);
2086 clear_tv(tv2);
2087 tv1->v_type = VAR_BOOL;
2088 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2089 }
2090 break;
2091
2092 case ISN_COMPAREBLOB:
2093 {
2094 typval_T *tv1 = STACK_TV_BOT(-2);
2095 typval_T *tv2 = STACK_TV_BOT(-1);
2096 blob_T *arg1 = tv1->vval.v_blob;
2097 blob_T *arg2 = tv2->vval.v_blob;
2098 int cmp = FALSE;
2099
2100 switch (iptr->isn_arg.op.op_type)
2101 {
2102 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2103 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2104 case EXPR_IS: cmp = arg1 == arg2; break;
2105 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2106 default: cmp = 0; break;
2107 }
2108 --ectx.ec_stack.ga_len;
2109 clear_tv(tv1);
2110 clear_tv(tv2);
2111 tv1->v_type = VAR_BOOL;
2112 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2113 }
2114 break;
2115
2116 // TODO: handle separately
2117 case ISN_COMPARESTRING:
2118 case ISN_COMPAREDICT:
2119 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 case ISN_COMPAREANY:
2121 {
2122 typval_T *tv1 = STACK_TV_BOT(-2);
2123 typval_T *tv2 = STACK_TV_BOT(-1);
2124 exptype_T exptype = iptr->isn_arg.op.op_type;
2125 int ic = iptr->isn_arg.op.op_ic;
2126
2127 typval_compare(tv1, tv2, exptype, ic);
2128 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 --ectx.ec_stack.ga_len;
2130 }
2131 break;
2132
2133 case ISN_ADDLIST:
2134 case ISN_ADDBLOB:
2135 {
2136 typval_T *tv1 = STACK_TV_BOT(-2);
2137 typval_T *tv2 = STACK_TV_BOT(-1);
2138
2139 if (iptr->isn_type == ISN_ADDLIST)
2140 eval_addlist(tv1, tv2);
2141 else
2142 eval_addblob(tv1, tv2);
2143 clear_tv(tv2);
2144 --ectx.ec_stack.ga_len;
2145 }
2146 break;
2147
2148 // Computation with two arguments of unknown type
2149 case ISN_OPANY:
2150 {
2151 typval_T *tv1 = STACK_TV_BOT(-2);
2152 typval_T *tv2 = STACK_TV_BOT(-1);
2153 varnumber_T n1, n2;
2154#ifdef FEAT_FLOAT
2155 float_T f1 = 0, f2 = 0;
2156#endif
2157 int error = FALSE;
2158
2159 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2160 {
2161 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2162 {
2163 eval_addlist(tv1, tv2);
2164 clear_tv(tv2);
2165 --ectx.ec_stack.ga_len;
2166 break;
2167 }
2168 else if (tv1->v_type == VAR_BLOB
2169 && tv2->v_type == VAR_BLOB)
2170 {
2171 eval_addblob(tv1, tv2);
2172 clear_tv(tv2);
2173 --ectx.ec_stack.ga_len;
2174 break;
2175 }
2176 }
2177#ifdef FEAT_FLOAT
2178 if (tv1->v_type == VAR_FLOAT)
2179 {
2180 f1 = tv1->vval.v_float;
2181 n1 = 0;
2182 }
2183 else
2184#endif
2185 {
2186 n1 = tv_get_number_chk(tv1, &error);
2187 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002188 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189#ifdef FEAT_FLOAT
2190 if (tv2->v_type == VAR_FLOAT)
2191 f1 = n1;
2192#endif
2193 }
2194#ifdef FEAT_FLOAT
2195 if (tv2->v_type == VAR_FLOAT)
2196 {
2197 f2 = tv2->vval.v_float;
2198 n2 = 0;
2199 }
2200 else
2201#endif
2202 {
2203 n2 = tv_get_number_chk(tv2, &error);
2204 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002205 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206#ifdef FEAT_FLOAT
2207 if (tv1->v_type == VAR_FLOAT)
2208 f2 = n2;
2209#endif
2210 }
2211#ifdef FEAT_FLOAT
2212 // if there is a float on either side the result is a float
2213 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2214 {
2215 switch (iptr->isn_arg.op.op_type)
2216 {
2217 case EXPR_MULT: f1 = f1 * f2; break;
2218 case EXPR_DIV: f1 = f1 / f2; break;
2219 case EXPR_SUB: f1 = f1 - f2; break;
2220 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002221 default: SOURCING_LNUM = iptr->isn_lnum;
2222 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002223 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 }
2225 clear_tv(tv1);
2226 clear_tv(tv2);
2227 tv1->v_type = VAR_FLOAT;
2228 tv1->vval.v_float = f1;
2229 --ectx.ec_stack.ga_len;
2230 }
2231 else
2232#endif
2233 {
2234 switch (iptr->isn_arg.op.op_type)
2235 {
2236 case EXPR_MULT: n1 = n1 * n2; break;
2237 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2238 case EXPR_SUB: n1 = n1 - n2; break;
2239 case EXPR_ADD: n1 = n1 + n2; break;
2240 default: n1 = num_modulus(n1, n2); break;
2241 }
2242 clear_tv(tv1);
2243 clear_tv(tv2);
2244 tv1->v_type = VAR_NUMBER;
2245 tv1->vval.v_number = n1;
2246 --ectx.ec_stack.ga_len;
2247 }
2248 }
2249 break;
2250
2251 case ISN_CONCAT:
2252 {
2253 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2254 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2255 char_u *res;
2256
2257 res = concat_str(str1, str2);
2258 clear_tv(STACK_TV_BOT(-2));
2259 clear_tv(STACK_TV_BOT(-1));
2260 --ectx.ec_stack.ga_len;
2261 STACK_TV_BOT(-1)->vval.v_string = res;
2262 }
2263 break;
2264
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002265 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002266 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002267 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002268 int is_slice = iptr->isn_type == ISN_STRSLICE;
2269 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002270 char_u *res;
2271
2272 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002273 // string slice: string is at stack-3, first index at
2274 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002275 if (is_slice)
2276 {
2277 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002278 n1 = tv->vval.v_number;
2279 }
2280
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002281 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002282 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002283
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002284 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002285 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002286 if (is_slice)
2287 // Slice: Select the characters from the string
2288 res = string_slice(tv->vval.v_string, n1, n2);
2289 else
2290 // Index: The resulting variable is a string of a
2291 // single character. If the index is too big or
2292 // negative the result is empty.
2293 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002294 vim_free(tv->vval.v_string);
2295 tv->vval.v_string = res;
2296 }
2297 break;
2298
2299 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002300 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 {
Bram Moolenaared591872020-08-15 22:14:53 +02002302 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002304 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305
2306 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002307 // list slice: list is at stack-3, indexes at stack-2 and
2308 // stack-1
2309 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 list = tv->vval.v_list;
2311
2312 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002313 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002315
2316 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 {
Bram Moolenaared591872020-08-15 22:14:53 +02002318 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002319 n1 = tv->vval.v_number;
2320 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 }
Bram Moolenaared591872020-08-15 22:14:53 +02002322
2323 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002324 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002325 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002326 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2327 == FAIL)
2328 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 }
2330 break;
2331
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002332 case ISN_ANYINDEX:
2333 case ISN_ANYSLICE:
2334 {
2335 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2336 typval_T *var1, *var2;
2337 int res;
2338
2339 // index: composite is at stack-2, index at stack-1
2340 // slice: composite is at stack-3, indexes at stack-2 and
2341 // stack-1
2342 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002343 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002344 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2345 goto on_error;
2346 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2347 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2348 res = eval_index_inner(tv, is_slice,
2349 var1, var2, NULL, -1, TRUE);
2350 clear_tv(var1);
2351 if (is_slice)
2352 clear_tv(var2);
2353 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2354 if (res == FAIL)
2355 goto on_error;
2356 }
2357 break;
2358
Bram Moolenaar9af78762020-06-16 11:34:42 +02002359 case ISN_SLICE:
2360 {
2361 list_T *list;
2362 int count = iptr->isn_arg.number;
2363
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002364 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002365 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002366 list = tv->vval.v_list;
2367
2368 // no error for short list, expect it to be checked earlier
2369 if (list != NULL && list->lv_len >= count)
2370 {
2371 list_T *newlist = list_slice(list,
2372 count, list->lv_len - 1);
2373
2374 if (newlist != NULL)
2375 {
2376 list_unref(list);
2377 tv->vval.v_list = newlist;
2378 ++newlist->lv_refcount;
2379 }
2380 }
2381 }
2382 break;
2383
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002384 case ISN_GETITEM:
2385 {
2386 listitem_T *li;
2387 int index = iptr->isn_arg.number;
2388
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002389 // Get list item: list is at stack-1, push item.
2390 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002391 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002392 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002393
2394 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2395 goto failed;
2396 ++ectx.ec_stack.ga_len;
2397 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2398 }
2399 break;
2400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 case ISN_MEMBER:
2402 {
2403 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002404 char_u *key;
2405 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002406 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002407
2408 // dict member: dict is at stack-2, key at stack-1
2409 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002410 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002411 dict = tv->vval.v_dict;
2412
2413 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002414 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002415 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002416
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002417 if ((di = dict_find(dict, key, -1)) == NULL)
2418 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002419 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002420 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002421 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002422 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002423 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002424 --ectx.ec_stack.ga_len;
2425 // Clear the dict after getting the item, to avoid that it
2426 // make the item invalid.
2427 tv = STACK_TV_BOT(-1);
2428 temp_tv = *tv;
2429 copy_tv(&di->di_tv, tv);
2430 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002431 }
2432 break;
2433
2434 // dict member with string key
2435 case ISN_STRINGMEMBER:
2436 {
2437 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002439 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440
2441 tv = STACK_TV_BOT(-1);
2442 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2443 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002444 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002446 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 }
2448 dict = tv->vval.v_dict;
2449
2450 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2451 == NULL)
2452 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002453 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002455 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002457 // Clear the dict after getting the item, to avoid that it
2458 // make the item invalid.
2459 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002461 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 }
2463 break;
2464
2465 case ISN_NEGATENR:
2466 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002467 if (tv->v_type != VAR_NUMBER
2468#ifdef FEAT_FLOAT
2469 && tv->v_type != VAR_FLOAT
2470#endif
2471 )
2472 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002473 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002474 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002475 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002476 }
2477#ifdef FEAT_FLOAT
2478 if (tv->v_type == VAR_FLOAT)
2479 tv->vval.v_float = -tv->vval.v_float;
2480 else
2481#endif
2482 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 break;
2484
2485 case ISN_CHECKNR:
2486 {
2487 int error = FALSE;
2488
2489 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002490 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002492 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 (void)tv_get_number_chk(tv, &error);
2494 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002495 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 }
2497 break;
2498
2499 case ISN_CHECKTYPE:
2500 {
2501 checktype_T *ct = &iptr->isn_arg.type;
2502
2503 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002504 // TODO: better type comparison
2505 if (tv->v_type != ct->ct_type
2506 && !((tv->v_type == VAR_PARTIAL
2507 && ct->ct_type == VAR_FUNC)
2508 || (tv->v_type == VAR_FUNC
2509 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002511 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002512 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 vartype_name(ct->ct_type),
2514 vartype_name(tv->v_type));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002515 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 }
2517 }
2518 break;
2519
Bram Moolenaar9af78762020-06-16 11:34:42 +02002520 case ISN_CHECKLEN:
2521 {
2522 int min_len = iptr->isn_arg.checklen.cl_min_len;
2523 list_T *list = NULL;
2524
2525 tv = STACK_TV_BOT(-1);
2526 if (tv->v_type == VAR_LIST)
2527 list = tv->vval.v_list;
2528 if (list == NULL || list->lv_len < min_len
2529 || (list->lv_len > min_len
2530 && !iptr->isn_arg.checklen.cl_more_OK))
2531 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002532 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002533 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002534 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002535 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002536 }
2537 }
2538 break;
2539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 case ISN_2BOOL:
2541 {
2542 int n;
2543
2544 tv = STACK_TV_BOT(-1);
2545 n = tv2bool(tv);
2546 if (iptr->isn_arg.number) // invert
2547 n = !n;
2548 clear_tv(tv);
2549 tv->v_type = VAR_BOOL;
2550 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2551 }
2552 break;
2553
2554 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002555 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 {
2557 char_u *str;
2558
2559 tv = STACK_TV_BOT(iptr->isn_arg.number);
2560 if (tv->v_type != VAR_STRING)
2561 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002562 if (iptr->isn_type == ISN_2STRING_ANY)
2563 {
2564 switch (tv->v_type)
2565 {
2566 case VAR_SPECIAL:
2567 case VAR_BOOL:
2568 case VAR_NUMBER:
2569 case VAR_FLOAT:
2570 case VAR_BLOB: break;
2571 default: to_string_error(tv->v_type);
2572 goto on_error;
2573 }
2574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 str = typval_tostring(tv);
2576 clear_tv(tv);
2577 tv->v_type = VAR_STRING;
2578 tv->vval.v_string = str;
2579 }
2580 }
2581 break;
2582
Bram Moolenaar389df252020-07-09 21:20:47 +02002583 case ISN_SHUFFLE:
2584 {
2585 typval_T tmp_tv;
2586 int item = iptr->isn_arg.shuffle.shfl_item;
2587 int up = iptr->isn_arg.shuffle.shfl_up;
2588
2589 tmp_tv = *STACK_TV_BOT(-item);
2590 for ( ; up > 0 && item > 1; --up)
2591 {
2592 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2593 --item;
2594 }
2595 *STACK_TV_BOT(-item) = tmp_tv;
2596 }
2597 break;
2598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 case ISN_DROP:
2600 --ectx.ec_stack.ga_len;
2601 clear_tv(STACK_TV_BOT(0));
2602 break;
2603 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002604 continue;
2605
Bram Moolenaard032f342020-07-18 18:13:02 +02002606func_return:
2607 // Restore previous function. If the frame pointer is zero then there
2608 // is none and we are done.
2609 if (ectx.ec_frame_idx == initial_frame_idx)
2610 {
2611 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2612 // only fails when out of memory
2613 goto failed;
2614 goto done;
2615 }
2616 if (func_return(&ectx) == FAIL)
2617 // only fails when out of memory
2618 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002619 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002620
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002621on_error:
2622 if (trylevel == 0)
2623 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 }
2625
2626done:
2627 // function finished, get result from the stack.
2628 tv = STACK_TV_BOT(-1);
2629 *rettv = *tv;
2630 tv->v_type = VAR_UNKNOWN;
2631 ret = OK;
2632
2633failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002634 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002635 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002636 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002637
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002638 estack_pop();
2639 current_sctx = save_current_sctx;
2640
2641failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002642 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2644 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002647 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002648
2649 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002650 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002651 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 return ret;
2653}
2654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655/*
2656 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002657 * We don't really need this at runtime, but we do have tests that require it,
2658 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 */
2660 void
2661ex_disassemble(exarg_T *eap)
2662{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002663 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002664 char_u *fname;
2665 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 dfunc_T *dfunc;
2667 isn_T *instr;
2668 int current;
2669 int line_idx = 0;
2670 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002671 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002673 if (STRNCMP(arg, "<lambda>", 8) == 0)
2674 {
2675 arg += 8;
2676 (void)getdigits(&arg);
2677 fname = vim_strnsave(eap->arg, arg - eap->arg);
2678 }
2679 else
2680 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002681 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002682 if (fname == NULL)
2683 {
2684 semsg(_(e_invarg2), eap->arg);
2685 return;
2686 }
2687
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002688 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002689 if (ufunc == NULL)
2690 {
2691 char_u *p = untrans_function_name(fname);
2692
2693 if (p != NULL)
2694 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002695 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002696 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002697 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 if (ufunc == NULL)
2699 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002700 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 return;
2702 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002703 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002704 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2705 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002706 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002708 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 return;
2710 }
2711 if (ufunc->uf_name_exp != NULL)
2712 msg((char *)ufunc->uf_name_exp);
2713 else
2714 msg((char *)ufunc->uf_name);
2715
2716 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2717 instr = dfunc->df_instr;
2718 for (current = 0; current < dfunc->df_instr_count; ++current)
2719 {
2720 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002721 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722
2723 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2724 {
2725 if (current > prev_current)
2726 {
2727 msg_puts("\n\n");
2728 prev_current = current;
2729 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002730 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2731 if (line != NULL)
2732 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 }
2734
2735 switch (iptr->isn_type)
2736 {
2737 case ISN_EXEC:
2738 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2739 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002740 case ISN_EXECCONCAT:
2741 smsg("%4d EXECCONCAT %lld", current,
2742 (long long)iptr->isn_arg.number);
2743 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744 case ISN_ECHO:
2745 {
2746 echo_T *echo = &iptr->isn_arg.echo;
2747
2748 smsg("%4d %s %d", current,
2749 echo->echo_with_white ? "ECHO" : "ECHON",
2750 echo->echo_count);
2751 }
2752 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002753 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002754 smsg("%4d EXECUTE %lld", current,
2755 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002756 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002757 case ISN_ECHOMSG:
2758 smsg("%4d ECHOMSG %lld", current,
2759 (long long)(iptr->isn_arg.number));
2760 break;
2761 case ISN_ECHOERR:
2762 smsg("%4d ECHOERR %lld", current,
2763 (long long)(iptr->isn_arg.number));
2764 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002766 case ISN_LOADOUTER:
2767 {
2768 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2769
2770 if (iptr->isn_arg.number < 0)
2771 smsg("%4d LOAD%s arg[%lld]", current, add,
2772 (long long)(iptr->isn_arg.number
2773 + STACK_FRAME_SIZE));
2774 else
2775 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002776 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 break;
2779 case ISN_LOADV:
2780 smsg("%4d LOADV v:%s", current,
2781 get_vim_var_name(iptr->isn_arg.number));
2782 break;
2783 case ISN_LOADSCRIPT:
2784 {
2785 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002786 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2788 + iptr->isn_arg.script.script_idx;
2789
2790 smsg("%4d LOADSCRIPT %s from %s", current,
2791 sv->sv_name, si->sn_name);
2792 }
2793 break;
2794 case ISN_LOADS:
2795 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002796 scriptitem_T *si = SCRIPT_ITEM(
2797 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798
2799 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002800 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 }
2802 break;
2803 case ISN_LOADG:
2804 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2805 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002806 case ISN_LOADB:
2807 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2808 break;
2809 case ISN_LOADW:
2810 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2811 break;
2812 case ISN_LOADT:
2813 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2814 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002815 case ISN_LOADGDICT:
2816 smsg("%4d LOAD g:", current);
2817 break;
2818 case ISN_LOADBDICT:
2819 smsg("%4d LOAD b:", current);
2820 break;
2821 case ISN_LOADWDICT:
2822 smsg("%4d LOAD w:", current);
2823 break;
2824 case ISN_LOADTDICT:
2825 smsg("%4d LOAD t:", current);
2826 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 case ISN_LOADOPT:
2828 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2829 break;
2830 case ISN_LOADENV:
2831 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2832 break;
2833 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002834 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 break;
2836
2837 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002838 case ISN_STOREOUTER:
2839 {
2840 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2841
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002842 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002843 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002844 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002845 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002846 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002847 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002850 case ISN_STOREV:
2851 smsg("%4d STOREV v:%s", current,
2852 get_vim_var_name(iptr->isn_arg.number));
2853 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002855 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2856 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002857 case ISN_STOREB:
2858 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2859 break;
2860 case ISN_STOREW:
2861 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2862 break;
2863 case ISN_STORET:
2864 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2865 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002866 case ISN_STORES:
2867 {
2868 scriptitem_T *si = SCRIPT_ITEM(
2869 iptr->isn_arg.loadstore.ls_sid);
2870
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002871 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002872 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002873 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 break;
2875 case ISN_STORESCRIPT:
2876 {
2877 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002878 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2880 + iptr->isn_arg.script.script_idx;
2881
2882 smsg("%4d STORESCRIPT %s in %s", current,
2883 sv->sv_name, si->sn_name);
2884 }
2885 break;
2886 case ISN_STOREOPT:
2887 smsg("%4d STOREOPT &%s", current,
2888 iptr->isn_arg.storeopt.so_name);
2889 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002890 case ISN_STOREENV:
2891 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2892 break;
2893 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002894 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002895 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 case ISN_STORENR:
2897 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002898 iptr->isn_arg.storenr.stnr_val,
2899 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 break;
2901
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002902 case ISN_STORELIST:
2903 smsg("%4d STORELIST", current);
2904 break;
2905
2906 case ISN_STOREDICT:
2907 smsg("%4d STOREDICT", current);
2908 break;
2909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 // constants
2911 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002912 smsg("%4d PUSHNR %lld", current,
2913 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 break;
2915 case ISN_PUSHBOOL:
2916 case ISN_PUSHSPEC:
2917 smsg("%4d PUSH %s", current,
2918 get_var_special_name(iptr->isn_arg.number));
2919 break;
2920 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002921#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002923#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 break;
2925 case ISN_PUSHS:
2926 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2927 break;
2928 case ISN_PUSHBLOB:
2929 {
2930 char_u *r;
2931 char_u numbuf[NUMBUFLEN];
2932 char_u *tofree;
2933
2934 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002935 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 vim_free(tofree);
2937 }
2938 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002939 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002940 {
2941 char *name = (char *)iptr->isn_arg.string;
2942
2943 smsg("%4d PUSHFUNC \"%s\"", current,
2944 name == NULL ? "[none]" : name);
2945 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002946 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002947 case ISN_PUSHCHANNEL:
2948#ifdef FEAT_JOB_CHANNEL
2949 {
2950 channel_T *channel = iptr->isn_arg.channel;
2951
2952 smsg("%4d PUSHCHANNEL %d", current,
2953 channel == NULL ? 0 : channel->ch_id);
2954 }
2955#endif
2956 break;
2957 case ISN_PUSHJOB:
2958#ifdef FEAT_JOB_CHANNEL
2959 {
2960 typval_T tv;
2961 char_u *name;
2962
2963 tv.v_type = VAR_JOB;
2964 tv.vval.v_job = iptr->isn_arg.job;
2965 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002966 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002967 }
2968#endif
2969 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 case ISN_PUSHEXC:
2971 smsg("%4d PUSH v:exception", current);
2972 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002973 case ISN_UNLET:
2974 smsg("%4d UNLET%s %s", current,
2975 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2976 iptr->isn_arg.unlet.ul_name);
2977 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002978 case ISN_UNLETENV:
2979 smsg("%4d UNLETENV%s $%s", current,
2980 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2981 iptr->isn_arg.unlet.ul_name);
2982 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002984 smsg("%4d NEWLIST size %lld", current,
2985 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 break;
2987 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002988 smsg("%4d NEWDICT size %lld", current,
2989 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 break;
2991
2992 // function call
2993 case ISN_BCALL:
2994 {
2995 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2996
2997 smsg("%4d BCALL %s(argc %d)", current,
2998 internal_func_name(cbfunc->cbf_idx),
2999 cbfunc->cbf_argcount);
3000 }
3001 break;
3002 case ISN_DCALL:
3003 {
3004 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3005 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3006 + cdfunc->cdf_idx;
3007
3008 smsg("%4d DCALL %s(argc %d)", current,
3009 df->df_ufunc->uf_name_exp != NULL
3010 ? df->df_ufunc->uf_name_exp
3011 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3012 }
3013 break;
3014 case ISN_UCALL:
3015 {
3016 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3017
3018 smsg("%4d UCALL %s(argc %d)", current,
3019 cufunc->cuf_name, cufunc->cuf_argcount);
3020 }
3021 break;
3022 case ISN_PCALL:
3023 {
3024 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3025
3026 smsg("%4d PCALL%s (argc %d)", current,
3027 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3028 }
3029 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003030 case ISN_PCALL_END:
3031 smsg("%4d PCALL end", current);
3032 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 case ISN_RETURN:
3034 smsg("%4d RETURN", current);
3035 break;
3036 case ISN_FUNCREF:
3037 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003038 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003040 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003042 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003043 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 }
3045 break;
3046
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003047 case ISN_NEWFUNC:
3048 {
3049 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3050
3051 smsg("%4d NEWFUNC %s %s", current,
3052 newfunc->nf_lambda, newfunc->nf_global);
3053 }
3054 break;
3055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 case ISN_JUMP:
3057 {
3058 char *when = "?";
3059
3060 switch (iptr->isn_arg.jump.jump_when)
3061 {
3062 case JUMP_ALWAYS:
3063 when = "JUMP";
3064 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 case JUMP_AND_KEEP_IF_TRUE:
3066 when = "JUMP_AND_KEEP_IF_TRUE";
3067 break;
3068 case JUMP_IF_FALSE:
3069 when = "JUMP_IF_FALSE";
3070 break;
3071 case JUMP_AND_KEEP_IF_FALSE:
3072 when = "JUMP_AND_KEEP_IF_FALSE";
3073 break;
3074 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003075 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 iptr->isn_arg.jump.jump_where);
3077 }
3078 break;
3079
3080 case ISN_FOR:
3081 {
3082 forloop_T *forloop = &iptr->isn_arg.forloop;
3083
3084 smsg("%4d FOR $%d -> %d", current,
3085 forloop->for_idx, forloop->for_end);
3086 }
3087 break;
3088
3089 case ISN_TRY:
3090 {
3091 try_T *try = &iptr->isn_arg.try;
3092
3093 smsg("%4d TRY catch -> %d, finally -> %d", current,
3094 try->try_catch, try->try_finally);
3095 }
3096 break;
3097 case ISN_CATCH:
3098 // TODO
3099 smsg("%4d CATCH", current);
3100 break;
3101 case ISN_ENDTRY:
3102 smsg("%4d ENDTRY", current);
3103 break;
3104 case ISN_THROW:
3105 smsg("%4d THROW", current);
3106 break;
3107
3108 // expression operations on number
3109 case ISN_OPNR:
3110 case ISN_OPFLOAT:
3111 case ISN_OPANY:
3112 {
3113 char *what;
3114 char *ins;
3115
3116 switch (iptr->isn_arg.op.op_type)
3117 {
3118 case EXPR_MULT: what = "*"; break;
3119 case EXPR_DIV: what = "/"; break;
3120 case EXPR_REM: what = "%"; break;
3121 case EXPR_SUB: what = "-"; break;
3122 case EXPR_ADD: what = "+"; break;
3123 default: what = "???"; break;
3124 }
3125 switch (iptr->isn_type)
3126 {
3127 case ISN_OPNR: ins = "OPNR"; break;
3128 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3129 case ISN_OPANY: ins = "OPANY"; break;
3130 default: ins = "???"; break;
3131 }
3132 smsg("%4d %s %s", current, ins, what);
3133 }
3134 break;
3135
3136 case ISN_COMPAREBOOL:
3137 case ISN_COMPARESPECIAL:
3138 case ISN_COMPARENR:
3139 case ISN_COMPAREFLOAT:
3140 case ISN_COMPARESTRING:
3141 case ISN_COMPAREBLOB:
3142 case ISN_COMPARELIST:
3143 case ISN_COMPAREDICT:
3144 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 case ISN_COMPAREANY:
3146 {
3147 char *p;
3148 char buf[10];
3149 char *type;
3150
3151 switch (iptr->isn_arg.op.op_type)
3152 {
3153 case EXPR_EQUAL: p = "=="; break;
3154 case EXPR_NEQUAL: p = "!="; break;
3155 case EXPR_GREATER: p = ">"; break;
3156 case EXPR_GEQUAL: p = ">="; break;
3157 case EXPR_SMALLER: p = "<"; break;
3158 case EXPR_SEQUAL: p = "<="; break;
3159 case EXPR_MATCH: p = "=~"; break;
3160 case EXPR_IS: p = "is"; break;
3161 case EXPR_ISNOT: p = "isnot"; break;
3162 case EXPR_NOMATCH: p = "!~"; break;
3163 default: p = "???"; break;
3164 }
3165 STRCPY(buf, p);
3166 if (iptr->isn_arg.op.op_ic == TRUE)
3167 strcat(buf, "?");
3168 switch(iptr->isn_type)
3169 {
3170 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3171 case ISN_COMPARESPECIAL:
3172 type = "COMPARESPECIAL"; break;
3173 case ISN_COMPARENR: type = "COMPARENR"; break;
3174 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3175 case ISN_COMPARESTRING:
3176 type = "COMPARESTRING"; break;
3177 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3178 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3179 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3180 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3182 default: type = "???"; break;
3183 }
3184
3185 smsg("%4d %s %s", current, type, buf);
3186 }
3187 break;
3188
3189 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3190 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3191
3192 // expression operations
3193 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003194 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003195 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003196 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003197 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003198 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3199 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003200 case ISN_SLICE: smsg("%4d SLICE %lld",
3201 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003202 case ISN_GETITEM: smsg("%4d ITEM %lld",
3203 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003204 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3205 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 iptr->isn_arg.string); break;
3207 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3208
3209 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3210 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3211 vartype_name(iptr->isn_arg.type.ct_type),
3212 iptr->isn_arg.type.ct_off);
3213 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003214 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3215 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3216 iptr->isn_arg.checklen.cl_min_len);
3217 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 case ISN_2BOOL: if (iptr->isn_arg.number)
3219 smsg("%4d INVERT (!val)", current);
3220 else
3221 smsg("%4d 2BOOL (!!val)", current);
3222 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003223 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3224 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003225 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003226 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3227 (long long)(iptr->isn_arg.number));
3228 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229
Bram Moolenaar389df252020-07-09 21:20:47 +02003230 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3231 iptr->isn_arg.shuffle.shfl_item,
3232 iptr->isn_arg.shuffle.shfl_up);
3233 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 case ISN_DROP: smsg("%4d DROP", current); break;
3235 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003236
3237 out_flush(); // output one line at a time
3238 ui_breakcheck();
3239 if (got_int)
3240 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242}
3243
3244/*
3245 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3246 * list, etc. Mostly like what JavaScript does, except that empty list and
3247 * empty dictionary are FALSE.
3248 */
3249 int
3250tv2bool(typval_T *tv)
3251{
3252 switch (tv->v_type)
3253 {
3254 case VAR_NUMBER:
3255 return tv->vval.v_number != 0;
3256 case VAR_FLOAT:
3257#ifdef FEAT_FLOAT
3258 return tv->vval.v_float != 0.0;
3259#else
3260 break;
3261#endif
3262 case VAR_PARTIAL:
3263 return tv->vval.v_partial != NULL;
3264 case VAR_FUNC:
3265 case VAR_STRING:
3266 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3267 case VAR_LIST:
3268 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3269 case VAR_DICT:
3270 return tv->vval.v_dict != NULL
3271 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3272 case VAR_BOOL:
3273 case VAR_SPECIAL:
3274 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3275 case VAR_JOB:
3276#ifdef FEAT_JOB_CHANNEL
3277 return tv->vval.v_job != NULL;
3278#else
3279 break;
3280#endif
3281 case VAR_CHANNEL:
3282#ifdef FEAT_JOB_CHANNEL
3283 return tv->vval.v_channel != NULL;
3284#else
3285 break;
3286#endif
3287 case VAR_BLOB:
3288 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3289 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003290 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 case VAR_VOID:
3292 break;
3293 }
3294 return FALSE;
3295}
3296
3297/*
3298 * If "tv" is a string give an error and return FAIL.
3299 */
3300 int
3301check_not_string(typval_T *tv)
3302{
3303 if (tv->v_type == VAR_STRING)
3304 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003305 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 clear_tv(tv);
3307 return FAIL;
3308 }
3309 return OK;
3310}
3311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312
3313#endif // FEAT_EVAL