blob: e93817a62bb1237a4b2067650610c6645d805b53 [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 Moolenaar8b565c22020-08-30 23:24:20 +0200795 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
796 idx + 1) == 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 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200825 if (check_typval_type(expected, &li->li_tv,
826 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200827 goto failed_early;
828 li = li->li_next;
829 }
830 }
831
Bram Moolenaar23e03252020-04-12 22:22:31 +0200832 if (defcount > 0)
833 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200834 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200835 --ectx.ec_stack.ga_len;
836 }
837
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100838 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200839 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100840 if (defcount > 0)
841 for (idx = 0; idx < defcount; ++idx)
842 {
843 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
844 ++ectx.ec_stack.ga_len;
845 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200846 if (ufunc->uf_va_name != NULL)
847 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848
849 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200850 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
851 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200853 if (partial != NULL)
854 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200855 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
856 {
857 // TODO: is this always the right way?
858 ectx.ec_outer_stack = &current_ectx->ec_stack;
859 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
860 }
861 else
862 {
863 ectx.ec_outer_stack = partial->pt_ectx_stack;
864 ectx.ec_outer_frame = partial->pt_ectx_frame;
865 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200866 }
867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 // dummy frame entries
869 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
870 {
871 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
872 ++ectx.ec_stack.ga_len;
873 }
874
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200875 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200876 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200877 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
878 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200879 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200881 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200882 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200883 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200884
885 ectx.ec_instr = dfunc->df_instr;
886 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100887
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200888 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200889 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200890 estack_push_ufunc(ufunc, 1);
891 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200892 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
893
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100894 // Decide where to start execution, handles optional arguments.
895 init_instr_idx(ufunc, argc, &ectx);
896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897 for (;;)
898 {
899 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100900
Bram Moolenaar270d0382020-05-15 21:42:53 +0200901 if (++breakcheck_count >= 100)
902 {
903 line_breakcheck();
904 breakcheck_count = 0;
905 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100906 if (got_int)
907 {
908 // Turn CTRL-C into an exception.
909 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100910 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100911 goto failed;
912 did_throw = TRUE;
913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914
Bram Moolenaara26b9702020-04-18 19:53:28 +0200915 if (did_emsg && msg_list != NULL && *msg_list != NULL)
916 {
917 // Turn an error message into an exception.
918 did_emsg = FALSE;
919 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
920 goto failed;
921 did_throw = TRUE;
922 *msg_list = NULL;
923 }
924
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 if (did_throw && !ectx.ec_in_catch)
926 {
927 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100928 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929
930 // An exception jumps to the first catch, finally, or returns from
931 // the current function.
932 if (trystack->ga_len > 0)
933 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200934 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935 {
936 // jump to ":catch" or ":finally"
937 ectx.ec_in_catch = TRUE;
938 ectx.ec_iidx = trycmd->tcd_catch_idx;
939 }
940 else
941 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200942 // Not inside try or need to return from current functions.
943 // Push a dummy return value.
944 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
945 goto failed;
946 tv = STACK_TV_BOT(0);
947 tv->v_type = VAR_NUMBER;
948 tv->vval.v_number = 0;
949 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200950 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200952 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100953 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200954 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
955 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 goto done;
957 }
958
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200959 if (func_return(&ectx) == FAIL)
960 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961 }
962 continue;
963 }
964
965 iptr = &ectx.ec_instr[ectx.ec_iidx++];
966 switch (iptr->isn_type)
967 {
968 // execute Ex command line
969 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200970 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 do_cmdline_cmd(iptr->isn_arg.string);
972 break;
973
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200974 // execute Ex command from pieces on the stack
975 case ISN_EXECCONCAT:
976 {
977 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200978 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200979 int pass;
980 int i;
981 char_u *cmd = NULL;
982 char_u *str;
983
984 for (pass = 1; pass <= 2; ++pass)
985 {
986 for (i = 0; i < count; ++i)
987 {
988 tv = STACK_TV_BOT(i - count);
989 str = tv->vval.v_string;
990 if (str != NULL && *str != NUL)
991 {
992 if (pass == 2)
993 STRCPY(cmd + len, str);
994 len += STRLEN(str);
995 }
996 if (pass == 2)
997 clear_tv(tv);
998 }
999 if (pass == 1)
1000 {
1001 cmd = alloc(len + 1);
1002 if (cmd == NULL)
1003 goto failed;
1004 len = 0;
1005 }
1006 }
1007
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001008 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001009 do_cmdline_cmd(cmd);
1010 vim_free(cmd);
1011 }
1012 break;
1013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 // execute :echo {string} ...
1015 case ISN_ECHO:
1016 {
1017 int count = iptr->isn_arg.echo.echo_count;
1018 int atstart = TRUE;
1019 int needclr = TRUE;
1020
1021 for (idx = 0; idx < count; ++idx)
1022 {
1023 tv = STACK_TV_BOT(idx - count);
1024 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1025 &atstart, &needclr);
1026 clear_tv(tv);
1027 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001028 if (needclr)
1029 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 ectx.ec_stack.ga_len -= count;
1031 }
1032 break;
1033
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001034 // :execute {string} ...
1035 // :echomsg {string} ...
1036 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001037 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001038 case ISN_ECHOMSG:
1039 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001040 {
1041 int count = iptr->isn_arg.number;
1042 garray_T ga;
1043 char_u buf[NUMBUFLEN];
1044 char_u *p;
1045 int len;
1046 int failed = FALSE;
1047
1048 ga_init2(&ga, 1, 80);
1049 for (idx = 0; idx < count; ++idx)
1050 {
1051 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001052 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001053 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001054 if (tv->v_type == VAR_CHANNEL
1055 || tv->v_type == VAR_JOB)
1056 {
1057 SOURCING_LNUM = iptr->isn_lnum;
1058 emsg(_(e_inval_string));
1059 break;
1060 }
1061 else
1062 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001063 }
1064 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001065 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001066
1067 len = (int)STRLEN(p);
1068 if (ga_grow(&ga, len + 2) == FAIL)
1069 failed = TRUE;
1070 else
1071 {
1072 if (ga.ga_len > 0)
1073 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1074 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1075 ga.ga_len += len;
1076 }
1077 clear_tv(tv);
1078 }
1079 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001080 if (failed)
1081 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001082
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001083 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001084 {
1085 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001086 {
1087 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001088 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001089 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001090 else
1091 {
1092 msg_sb_eol();
1093 if (iptr->isn_type == ISN_ECHOMSG)
1094 {
1095 msg_attr(ga.ga_data, echo_attr);
1096 out_flush();
1097 }
1098 else
1099 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001100 SOURCING_LNUM = iptr->isn_lnum;
1101 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001102 }
1103 }
1104 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001105 ga_clear(&ga);
1106 }
1107 break;
1108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 // load local variable or argument
1110 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001111 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 goto failed;
1113 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1114 ++ectx.ec_stack.ga_len;
1115 break;
1116
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001117 // load variable or argument from outer scope
1118 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001119 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001120 goto failed;
1121 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1122 STACK_TV_BOT(0));
1123 ++ectx.ec_stack.ga_len;
1124 break;
1125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 // load v: variable
1127 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001128 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 goto failed;
1130 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1131 ++ectx.ec_stack.ga_len;
1132 break;
1133
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001134 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 case ISN_LOADSCRIPT:
1136 {
1137 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001138 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 svar_T *sv;
1140
1141 sv = ((svar_T *)si->sn_var_vals.ga_data)
1142 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001143 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 goto failed;
1145 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1146 ++ectx.ec_stack.ga_len;
1147 }
1148 break;
1149
1150 // load s: variable in old script
1151 case ISN_LOADS:
1152 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001153 hashtab_T *ht = &SCRIPT_VARS(
1154 iptr->isn_arg.loadstore.ls_sid);
1155 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 if (di == NULL)
1159 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001160 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001161 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001162 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 }
1164 else
1165 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001166 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 goto failed;
1168 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1169 ++ectx.ec_stack.ga_len;
1170 }
1171 }
1172 break;
1173
Bram Moolenaard3aac292020-04-19 14:32:17 +02001174 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001176 case ISN_LOADB:
1177 case ISN_LOADW:
1178 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001180 dictitem_T *di = NULL;
1181 hashtab_T *ht = NULL;
1182 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001183
Bram Moolenaard3aac292020-04-19 14:32:17 +02001184 switch (iptr->isn_type)
1185 {
1186 case ISN_LOADG:
1187 ht = get_globvar_ht();
1188 namespace = 'g';
1189 break;
1190 case ISN_LOADB:
1191 ht = &curbuf->b_vars->dv_hashtab;
1192 namespace = 'b';
1193 break;
1194 case ISN_LOADW:
1195 ht = &curwin->w_vars->dv_hashtab;
1196 namespace = 'w';
1197 break;
1198 case ISN_LOADT:
1199 ht = &curtab->tp_vars->dv_hashtab;
1200 namespace = 't';
1201 break;
1202 default: // Cannot reach here
1203 goto failed;
1204 }
1205 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 if (di == NULL)
1208 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001209 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001210 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001211 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001212 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 }
1214 else
1215 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001216 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 goto failed;
1218 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1219 ++ectx.ec_stack.ga_len;
1220 }
1221 }
1222 break;
1223
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001224 // load g:/b:/w:/t: namespace
1225 case ISN_LOADGDICT:
1226 case ISN_LOADBDICT:
1227 case ISN_LOADWDICT:
1228 case ISN_LOADTDICT:
1229 {
1230 dict_T *d = NULL;
1231
1232 switch (iptr->isn_type)
1233 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001234 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1235 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1236 case ISN_LOADWDICT: d = curwin->w_vars; break;
1237 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001238 default: // Cannot reach here
1239 goto failed;
1240 }
1241 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1242 goto failed;
1243 tv = STACK_TV_BOT(0);
1244 tv->v_type = VAR_DICT;
1245 tv->v_lock = 0;
1246 tv->vval.v_dict = d;
1247 ++ectx.ec_stack.ga_len;
1248 }
1249 break;
1250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 // load &option
1252 case ISN_LOADOPT:
1253 {
1254 typval_T optval;
1255 char_u *name = iptr->isn_arg.string;
1256
Bram Moolenaara8c17702020-04-01 21:17:24 +02001257 // This is not expected to fail, name is checked during
1258 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001259 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001261 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001262 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 *STACK_TV_BOT(0) = optval;
1264 ++ectx.ec_stack.ga_len;
1265 }
1266 break;
1267
1268 // load $ENV
1269 case ISN_LOADENV:
1270 {
1271 typval_T optval;
1272 char_u *name = iptr->isn_arg.string;
1273
Bram Moolenaar270d0382020-05-15 21:42:53 +02001274 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001276 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001277 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 *STACK_TV_BOT(0) = optval;
1279 ++ectx.ec_stack.ga_len;
1280 }
1281 break;
1282
1283 // load @register
1284 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001285 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286 goto failed;
1287 tv = STACK_TV_BOT(0);
1288 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001289 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 tv->vval.v_string = get_reg_contents(
1291 iptr->isn_arg.number, GREG_EXPR_SRC);
1292 ++ectx.ec_stack.ga_len;
1293 break;
1294
1295 // store local variable
1296 case ISN_STORE:
1297 --ectx.ec_stack.ga_len;
1298 tv = STACK_TV_VAR(iptr->isn_arg.number);
1299 clear_tv(tv);
1300 *tv = *STACK_TV_BOT(0);
1301 break;
1302
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001303 // store variable or argument in outer scope
1304 case ISN_STOREOUTER:
1305 --ectx.ec_stack.ga_len;
1306 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1307 clear_tv(tv);
1308 *tv = *STACK_TV_BOT(0);
1309 break;
1310
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001311 // store s: variable in old script
1312 case ISN_STORES:
1313 {
1314 hashtab_T *ht = &SCRIPT_VARS(
1315 iptr->isn_arg.loadstore.ls_sid);
1316 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001317 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001318
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001319 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001320 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001321 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001322 else
1323 {
1324 clear_tv(&di->di_tv);
1325 di->di_tv = *STACK_TV_BOT(0);
1326 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001327 }
1328 break;
1329
1330 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 case ISN_STORESCRIPT:
1332 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001333 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 iptr->isn_arg.script.script_sid);
1335 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1336 + iptr->isn_arg.script.script_idx;
1337
1338 --ectx.ec_stack.ga_len;
1339 clear_tv(sv->sv_tv);
1340 *sv->sv_tv = *STACK_TV_BOT(0);
1341 }
1342 break;
1343
1344 // store option
1345 case ISN_STOREOPT:
1346 {
1347 long n = 0;
1348 char_u *s = NULL;
1349 char *msg;
1350
1351 --ectx.ec_stack.ga_len;
1352 tv = STACK_TV_BOT(0);
1353 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001354 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001356 if (s == NULL)
1357 s = (char_u *)"";
1358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001360 // must be VAR_NUMBER, CHECKTYPE makes sure
1361 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1363 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001364 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365 if (msg != NULL)
1366 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001367 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001369 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 }
1372 break;
1373
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001374 // store $ENV
1375 case ISN_STOREENV:
1376 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001377 tv = STACK_TV_BOT(0);
1378 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1379 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001380 break;
1381
1382 // store @r
1383 case ISN_STOREREG:
1384 {
1385 int reg = iptr->isn_arg.number;
1386
1387 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001388 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001389 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001390 tv_get_string(tv), -1, FALSE);
1391 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001392 }
1393 break;
1394
1395 // store v: variable
1396 case ISN_STOREV:
1397 --ectx.ec_stack.ga_len;
1398 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1399 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001400 // should not happen, type is checked when compiling
1401 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001402 break;
1403
Bram Moolenaard3aac292020-04-19 14:32:17 +02001404 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001406 case ISN_STOREB:
1407 case ISN_STOREW:
1408 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 {
1410 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001411 hashtab_T *ht;
1412 switch (iptr->isn_type)
1413 {
1414 case ISN_STOREG:
1415 ht = get_globvar_ht();
1416 break;
1417 case ISN_STOREB:
1418 ht = &curbuf->b_vars->dv_hashtab;
1419 break;
1420 case ISN_STOREW:
1421 ht = &curwin->w_vars->dv_hashtab;
1422 break;
1423 case ISN_STORET:
1424 ht = &curtab->tp_vars->dv_hashtab;
1425 break;
1426 default: // Cannot reach here
1427 goto failed;
1428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429
1430 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001431 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001433 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 else
1435 {
1436 clear_tv(&di->di_tv);
1437 di->di_tv = *STACK_TV_BOT(0);
1438 }
1439 }
1440 break;
1441
1442 // store number in local variable
1443 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001444 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 clear_tv(tv);
1446 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001447 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 break;
1449
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001450 // store value in list variable
1451 case ISN_STORELIST:
1452 {
1453 typval_T *tv_idx = STACK_TV_BOT(-2);
1454 varnumber_T lidx = tv_idx->vval.v_number;
1455 typval_T *tv_list = STACK_TV_BOT(-1);
1456 list_T *list = tv_list->vval.v_list;
1457
1458 if (lidx < 0 && list->lv_len + lidx >= 0)
1459 // negative index is relative to the end
1460 lidx = list->lv_len + lidx;
1461 if (lidx < 0 || lidx > list->lv_len)
1462 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001463 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001464 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001465 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001466 }
1467 tv = STACK_TV_BOT(-3);
1468 if (lidx < list->lv_len)
1469 {
1470 listitem_T *li = list_find(list, lidx);
1471
1472 // overwrite existing list item
1473 clear_tv(&li->li_tv);
1474 li->li_tv = *tv;
1475 }
1476 else
1477 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001478 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001479 if (list_append_tv(list, tv) == FAIL)
1480 goto failed;
1481 clear_tv(tv);
1482 }
1483 clear_tv(tv_idx);
1484 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001485 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001486 }
1487 break;
1488
1489 // store value in dict variable
1490 case ISN_STOREDICT:
1491 {
1492 typval_T *tv_key = STACK_TV_BOT(-2);
1493 char_u *key = tv_key->vval.v_string;
1494 typval_T *tv_dict = STACK_TV_BOT(-1);
1495 dict_T *dict = tv_dict->vval.v_dict;
1496 dictitem_T *di;
1497
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001498 if (dict == NULL)
1499 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001500 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001501 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001502 goto on_error;
1503 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001504 if (key == NULL)
1505 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001506 tv = STACK_TV_BOT(-3);
1507 di = dict_find(dict, key, -1);
1508 if (di != NULL)
1509 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001510 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001511 clear_tv(&di->di_tv);
1512 di->di_tv = *tv;
1513 }
1514 else
1515 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001516 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001517 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1518 goto failed;
1519 clear_tv(tv);
1520 }
1521 clear_tv(tv_key);
1522 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001523 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001524 }
1525 break;
1526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 // push constant
1528 case ISN_PUSHNR:
1529 case ISN_PUSHBOOL:
1530 case ISN_PUSHSPEC:
1531 case ISN_PUSHF:
1532 case ISN_PUSHS:
1533 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001534 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001535 case ISN_PUSHCHANNEL:
1536 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001537 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 goto failed;
1539 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001540 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 ++ectx.ec_stack.ga_len;
1542 switch (iptr->isn_type)
1543 {
1544 case ISN_PUSHNR:
1545 tv->v_type = VAR_NUMBER;
1546 tv->vval.v_number = iptr->isn_arg.number;
1547 break;
1548 case ISN_PUSHBOOL:
1549 tv->v_type = VAR_BOOL;
1550 tv->vval.v_number = iptr->isn_arg.number;
1551 break;
1552 case ISN_PUSHSPEC:
1553 tv->v_type = VAR_SPECIAL;
1554 tv->vval.v_number = iptr->isn_arg.number;
1555 break;
1556#ifdef FEAT_FLOAT
1557 case ISN_PUSHF:
1558 tv->v_type = VAR_FLOAT;
1559 tv->vval.v_float = iptr->isn_arg.fnumber;
1560 break;
1561#endif
1562 case ISN_PUSHBLOB:
1563 blob_copy(iptr->isn_arg.blob, tv);
1564 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001565 case ISN_PUSHFUNC:
1566 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001567 if (iptr->isn_arg.string == NULL)
1568 tv->vval.v_string = NULL;
1569 else
1570 tv->vval.v_string =
1571 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001572 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001573 case ISN_PUSHCHANNEL:
1574#ifdef FEAT_JOB_CHANNEL
1575 tv->v_type = VAR_CHANNEL;
1576 tv->vval.v_channel = iptr->isn_arg.channel;
1577 if (tv->vval.v_channel != NULL)
1578 ++tv->vval.v_channel->ch_refcount;
1579#endif
1580 break;
1581 case ISN_PUSHJOB:
1582#ifdef FEAT_JOB_CHANNEL
1583 tv->v_type = VAR_JOB;
1584 tv->vval.v_job = iptr->isn_arg.job;
1585 if (tv->vval.v_job != NULL)
1586 ++tv->vval.v_job->jv_refcount;
1587#endif
1588 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 default:
1590 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001591 tv->vval.v_string = vim_strsave(
1592 iptr->isn_arg.string == NULL
1593 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 }
1595 break;
1596
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001597 case ISN_UNLET:
1598 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1599 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001600 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001601 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001602 case ISN_UNLETENV:
1603 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1604 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 // create a list from items on the stack; uses a single allocation
1607 // for the list header and the items
1608 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001609 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1610 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 break;
1612
1613 // create a dict from items on the stack
1614 case ISN_NEWDICT:
1615 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001616 int count = iptr->isn_arg.number;
1617 dict_T *dict = dict_alloc();
1618 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619
1620 if (dict == NULL)
1621 goto failed;
1622 for (idx = 0; idx < count; ++idx)
1623 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001624 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001626 // check key is unique
1627 item = dict_find(dict, tv->vval.v_string, -1);
1628 if (item != NULL)
1629 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001630 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001631 semsg(_(e_duplicate_key), tv->vval.v_string);
1632 dict_unref(dict);
1633 goto on_error;
1634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 item = dictitem_alloc(tv->vval.v_string);
1636 clear_tv(tv);
1637 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001638 {
1639 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1643 item->di_tv.v_lock = 0;
1644 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001645 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001646 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001647 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 }
1651
1652 if (count > 0)
1653 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001654 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 goto failed;
1656 else
1657 ++ectx.ec_stack.ga_len;
1658 tv = STACK_TV_BOT(-1);
1659 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001660 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 tv->vval.v_dict = dict;
1662 ++dict->dv_refcount;
1663 }
1664 break;
1665
1666 // call a :def function
1667 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001668 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1670 iptr->isn_arg.dfunc.cdf_argcount,
1671 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001672 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673 break;
1674
1675 // call a builtin function
1676 case ISN_BCALL:
1677 SOURCING_LNUM = iptr->isn_lnum;
1678 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1679 iptr->isn_arg.bfunc.cbf_argcount,
1680 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001681 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 break;
1683
1684 // call a funcref or partial
1685 case ISN_PCALL:
1686 {
1687 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1688 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001689 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690
1691 SOURCING_LNUM = iptr->isn_lnum;
1692 if (pfunc->cpf_top)
1693 {
1694 // funcref is above the arguments
1695 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1696 }
1697 else
1698 {
1699 // Get the funcref from the stack.
1700 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001701 partial_tv = *STACK_TV_BOT(0);
1702 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 }
1704 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001705 if (tv == &partial_tv)
1706 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001708 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 }
1710 break;
1711
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001712 case ISN_PCALL_END:
1713 // PCALL finished, arguments have been consumed and replaced by
1714 // the return value. Now clear the funcref from the stack,
1715 // and move the return value in its place.
1716 --ectx.ec_stack.ga_len;
1717 clear_tv(STACK_TV_BOT(-1));
1718 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1719 break;
1720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 // call a user defined function or funcref/partial
1722 case ISN_UCALL:
1723 {
1724 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1725
1726 SOURCING_LNUM = iptr->isn_lnum;
1727 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001728 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001729 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 }
1731 break;
1732
1733 // return from a :def function call
1734 case ISN_RETURN:
1735 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001736 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001737 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001738
1739 if (trystack->ga_len > 0)
1740 trycmd = ((trycmd_T *)trystack->ga_data)
1741 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001742 if (trycmd != NULL
1743 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 && trycmd->tcd_finally_idx != 0)
1745 {
1746 // jump to ":finally"
1747 ectx.ec_iidx = trycmd->tcd_finally_idx;
1748 trycmd->tcd_return = TRUE;
1749 }
1750 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001751 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 }
1753 break;
1754
1755 // push a function reference to a compiled function
1756 case ISN_FUNCREF:
1757 {
1758 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001759 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760
1761 pt = ALLOC_CLEAR_ONE(partial_T);
1762 if (pt == NULL)
1763 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001764 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001765 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001766 vim_free(pt);
1767 goto failed;
1768 }
1769 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1770 + iptr->isn_arg.funcref.fr_func;
1771 pt->pt_func = pt_dfunc->df_ufunc;
1772 pt->pt_refcount = 1;
1773 ++pt_dfunc->df_ufunc->uf_refcount;
1774
1775 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1776 {
1777 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1778 + ectx.ec_dfunc_idx;
1779
1780 // The closure needs to find arguments and local
1781 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001782 pt->pt_ectx_stack = &ectx.ec_stack;
1783 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001784
1785 // If this function returns and the closure is still
1786 // used, we need to make a copy of the context
1787 // (arguments and local variables). Store a reference
1788 // to the partial so we can handle that.
1789 ++pt->pt_refcount;
1790 tv = STACK_TV_VAR(dfunc->df_varcount
1791 + iptr->isn_arg.funcref.fr_var_idx);
1792 if (tv->v_type == VAR_PARTIAL)
1793 {
1794 // TODO: use a garray_T on ectx.
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001795 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001796 emsg("Multiple closures not supported yet");
1797 goto failed;
1798 }
1799 tv->v_type = VAR_PARTIAL;
1800 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001801 }
1802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 tv = STACK_TV_BOT(0);
1804 ++ectx.ec_stack.ga_len;
1805 tv->vval.v_partial = pt;
1806 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001807 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 }
1809 break;
1810
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001811 // Create a global function from a lambda.
1812 case ISN_NEWFUNC:
1813 {
1814 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1815
1816 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1817 }
1818 break;
1819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 // jump if a condition is met
1821 case ISN_JUMP:
1822 {
1823 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1824 int jump = TRUE;
1825
1826 if (when != JUMP_ALWAYS)
1827 {
1828 tv = STACK_TV_BOT(-1);
1829 jump = tv2bool(tv);
1830 if (when == JUMP_IF_FALSE
1831 || when == JUMP_AND_KEEP_IF_FALSE)
1832 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001833 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 {
1835 // drop the value from the stack
1836 clear_tv(tv);
1837 --ectx.ec_stack.ga_len;
1838 }
1839 }
1840 if (jump)
1841 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1842 }
1843 break;
1844
1845 // top of a for loop
1846 case ISN_FOR:
1847 {
1848 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1849 typval_T *idxtv =
1850 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1851
1852 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001853 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 goto failed;
1855 if (++idxtv->vval.v_number >= list->lv_len)
1856 // past the end of the list, jump to "endfor"
1857 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1858 else if (list->lv_first == &range_list_item)
1859 {
1860 // non-materialized range() list
1861 tv = STACK_TV_BOT(0);
1862 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001863 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 tv->vval.v_number = list_find_nr(
1865 list, idxtv->vval.v_number, NULL);
1866 ++ectx.ec_stack.ga_len;
1867 }
1868 else
1869 {
1870 listitem_T *li = list_find(list, idxtv->vval.v_number);
1871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1873 ++ectx.ec_stack.ga_len;
1874 }
1875 }
1876 break;
1877
1878 // start of ":try" block
1879 case ISN_TRY:
1880 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001881 trycmd_T *trycmd = NULL;
1882
Bram Moolenaar270d0382020-05-15 21:42:53 +02001883 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 goto failed;
1885 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1886 + ectx.ec_trystack.ga_len;
1887 ++ectx.ec_trystack.ga_len;
1888 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001889 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1891 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001892 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 }
1894 break;
1895
1896 case ISN_PUSHEXC:
1897 if (current_exception == NULL)
1898 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001899 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 iemsg("Evaluating catch while current_exception is NULL");
1901 goto failed;
1902 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001903 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 goto failed;
1905 tv = STACK_TV_BOT(0);
1906 ++ectx.ec_stack.ga_len;
1907 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001908 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 tv->vval.v_string = vim_strsave(
1910 (char_u *)current_exception->value);
1911 break;
1912
1913 case ISN_CATCH:
1914 {
1915 garray_T *trystack = &ectx.ec_trystack;
1916
1917 if (trystack->ga_len > 0)
1918 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001919 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 + trystack->ga_len - 1;
1921 trycmd->tcd_caught = TRUE;
1922 }
1923 did_emsg = got_int = did_throw = FALSE;
1924 catch_exception(current_exception);
1925 }
1926 break;
1927
1928 // end of ":try" block
1929 case ISN_ENDTRY:
1930 {
1931 garray_T *trystack = &ectx.ec_trystack;
1932
1933 if (trystack->ga_len > 0)
1934 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001935 trycmd_T *trycmd = NULL;
1936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 --trystack->ga_len;
1938 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001939 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 trycmd = ((trycmd_T *)trystack->ga_data)
1941 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001942 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 {
1944 // discard the exception
1945 if (caught_stack == current_exception)
1946 caught_stack = caught_stack->caught;
1947 discard_current_exception();
1948 }
1949
1950 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001951 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 }
1953 }
1954 break;
1955
1956 case ISN_THROW:
1957 --ectx.ec_stack.ga_len;
1958 tv = STACK_TV_BOT(0);
1959 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1960 {
1961 vim_free(tv->vval.v_string);
1962 goto failed;
1963 }
1964 did_throw = TRUE;
1965 break;
1966
1967 // compare with special values
1968 case ISN_COMPAREBOOL:
1969 case ISN_COMPARESPECIAL:
1970 {
1971 typval_T *tv1 = STACK_TV_BOT(-2);
1972 typval_T *tv2 = STACK_TV_BOT(-1);
1973 varnumber_T arg1 = tv1->vval.v_number;
1974 varnumber_T arg2 = tv2->vval.v_number;
1975 int res;
1976
1977 switch (iptr->isn_arg.op.op_type)
1978 {
1979 case EXPR_EQUAL: res = arg1 == arg2; break;
1980 case EXPR_NEQUAL: res = arg1 != arg2; break;
1981 default: res = 0; break;
1982 }
1983
1984 --ectx.ec_stack.ga_len;
1985 tv1->v_type = VAR_BOOL;
1986 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1987 }
1988 break;
1989
1990 // Operation with two number arguments
1991 case ISN_OPNR:
1992 case ISN_COMPARENR:
1993 {
1994 typval_T *tv1 = STACK_TV_BOT(-2);
1995 typval_T *tv2 = STACK_TV_BOT(-1);
1996 varnumber_T arg1 = tv1->vval.v_number;
1997 varnumber_T arg2 = tv2->vval.v_number;
1998 varnumber_T res;
1999
2000 switch (iptr->isn_arg.op.op_type)
2001 {
2002 case EXPR_MULT: res = arg1 * arg2; break;
2003 case EXPR_DIV: res = arg1 / arg2; break;
2004 case EXPR_REM: res = arg1 % arg2; break;
2005 case EXPR_SUB: res = arg1 - arg2; break;
2006 case EXPR_ADD: res = arg1 + arg2; break;
2007
2008 case EXPR_EQUAL: res = arg1 == arg2; break;
2009 case EXPR_NEQUAL: res = arg1 != arg2; break;
2010 case EXPR_GREATER: res = arg1 > arg2; break;
2011 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2012 case EXPR_SMALLER: res = arg1 < arg2; break;
2013 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2014 default: res = 0; break;
2015 }
2016
2017 --ectx.ec_stack.ga_len;
2018 if (iptr->isn_type == ISN_COMPARENR)
2019 {
2020 tv1->v_type = VAR_BOOL;
2021 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2022 }
2023 else
2024 tv1->vval.v_number = res;
2025 }
2026 break;
2027
2028 // Computation with two float arguments
2029 case ISN_OPFLOAT:
2030 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002031#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 {
2033 typval_T *tv1 = STACK_TV_BOT(-2);
2034 typval_T *tv2 = STACK_TV_BOT(-1);
2035 float_T arg1 = tv1->vval.v_float;
2036 float_T arg2 = tv2->vval.v_float;
2037 float_T res = 0;
2038 int cmp = FALSE;
2039
2040 switch (iptr->isn_arg.op.op_type)
2041 {
2042 case EXPR_MULT: res = arg1 * arg2; break;
2043 case EXPR_DIV: res = arg1 / arg2; break;
2044 case EXPR_SUB: res = arg1 - arg2; break;
2045 case EXPR_ADD: res = arg1 + arg2; break;
2046
2047 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2048 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2049 case EXPR_GREATER: cmp = arg1 > arg2; break;
2050 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2051 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2052 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2053 default: cmp = 0; break;
2054 }
2055 --ectx.ec_stack.ga_len;
2056 if (iptr->isn_type == ISN_COMPAREFLOAT)
2057 {
2058 tv1->v_type = VAR_BOOL;
2059 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2060 }
2061 else
2062 tv1->vval.v_float = res;
2063 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002064#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 break;
2066
2067 case ISN_COMPARELIST:
2068 {
2069 typval_T *tv1 = STACK_TV_BOT(-2);
2070 typval_T *tv2 = STACK_TV_BOT(-1);
2071 list_T *arg1 = tv1->vval.v_list;
2072 list_T *arg2 = tv2->vval.v_list;
2073 int cmp = FALSE;
2074 int ic = iptr->isn_arg.op.op_ic;
2075
2076 switch (iptr->isn_arg.op.op_type)
2077 {
2078 case EXPR_EQUAL: cmp =
2079 list_equal(arg1, arg2, ic, FALSE); break;
2080 case EXPR_NEQUAL: cmp =
2081 !list_equal(arg1, arg2, ic, FALSE); break;
2082 case EXPR_IS: cmp = arg1 == arg2; break;
2083 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2084 default: cmp = 0; break;
2085 }
2086 --ectx.ec_stack.ga_len;
2087 clear_tv(tv1);
2088 clear_tv(tv2);
2089 tv1->v_type = VAR_BOOL;
2090 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2091 }
2092 break;
2093
2094 case ISN_COMPAREBLOB:
2095 {
2096 typval_T *tv1 = STACK_TV_BOT(-2);
2097 typval_T *tv2 = STACK_TV_BOT(-1);
2098 blob_T *arg1 = tv1->vval.v_blob;
2099 blob_T *arg2 = tv2->vval.v_blob;
2100 int cmp = FALSE;
2101
2102 switch (iptr->isn_arg.op.op_type)
2103 {
2104 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2105 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2106 case EXPR_IS: cmp = arg1 == arg2; break;
2107 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2108 default: cmp = 0; break;
2109 }
2110 --ectx.ec_stack.ga_len;
2111 clear_tv(tv1);
2112 clear_tv(tv2);
2113 tv1->v_type = VAR_BOOL;
2114 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2115 }
2116 break;
2117
2118 // TODO: handle separately
2119 case ISN_COMPARESTRING:
2120 case ISN_COMPAREDICT:
2121 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 case ISN_COMPAREANY:
2123 {
2124 typval_T *tv1 = STACK_TV_BOT(-2);
2125 typval_T *tv2 = STACK_TV_BOT(-1);
2126 exptype_T exptype = iptr->isn_arg.op.op_type;
2127 int ic = iptr->isn_arg.op.op_ic;
2128
2129 typval_compare(tv1, tv2, exptype, ic);
2130 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 --ectx.ec_stack.ga_len;
2132 }
2133 break;
2134
2135 case ISN_ADDLIST:
2136 case ISN_ADDBLOB:
2137 {
2138 typval_T *tv1 = STACK_TV_BOT(-2);
2139 typval_T *tv2 = STACK_TV_BOT(-1);
2140
2141 if (iptr->isn_type == ISN_ADDLIST)
2142 eval_addlist(tv1, tv2);
2143 else
2144 eval_addblob(tv1, tv2);
2145 clear_tv(tv2);
2146 --ectx.ec_stack.ga_len;
2147 }
2148 break;
2149
2150 // Computation with two arguments of unknown type
2151 case ISN_OPANY:
2152 {
2153 typval_T *tv1 = STACK_TV_BOT(-2);
2154 typval_T *tv2 = STACK_TV_BOT(-1);
2155 varnumber_T n1, n2;
2156#ifdef FEAT_FLOAT
2157 float_T f1 = 0, f2 = 0;
2158#endif
2159 int error = FALSE;
2160
2161 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2162 {
2163 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2164 {
2165 eval_addlist(tv1, tv2);
2166 clear_tv(tv2);
2167 --ectx.ec_stack.ga_len;
2168 break;
2169 }
2170 else if (tv1->v_type == VAR_BLOB
2171 && tv2->v_type == VAR_BLOB)
2172 {
2173 eval_addblob(tv1, tv2);
2174 clear_tv(tv2);
2175 --ectx.ec_stack.ga_len;
2176 break;
2177 }
2178 }
2179#ifdef FEAT_FLOAT
2180 if (tv1->v_type == VAR_FLOAT)
2181 {
2182 f1 = tv1->vval.v_float;
2183 n1 = 0;
2184 }
2185 else
2186#endif
2187 {
2188 n1 = tv_get_number_chk(tv1, &error);
2189 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002190 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191#ifdef FEAT_FLOAT
2192 if (tv2->v_type == VAR_FLOAT)
2193 f1 = n1;
2194#endif
2195 }
2196#ifdef FEAT_FLOAT
2197 if (tv2->v_type == VAR_FLOAT)
2198 {
2199 f2 = tv2->vval.v_float;
2200 n2 = 0;
2201 }
2202 else
2203#endif
2204 {
2205 n2 = tv_get_number_chk(tv2, &error);
2206 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002207 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208#ifdef FEAT_FLOAT
2209 if (tv1->v_type == VAR_FLOAT)
2210 f2 = n2;
2211#endif
2212 }
2213#ifdef FEAT_FLOAT
2214 // if there is a float on either side the result is a float
2215 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2216 {
2217 switch (iptr->isn_arg.op.op_type)
2218 {
2219 case EXPR_MULT: f1 = f1 * f2; break;
2220 case EXPR_DIV: f1 = f1 / f2; break;
2221 case EXPR_SUB: f1 = f1 - f2; break;
2222 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002223 default: SOURCING_LNUM = iptr->isn_lnum;
2224 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002225 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 }
2227 clear_tv(tv1);
2228 clear_tv(tv2);
2229 tv1->v_type = VAR_FLOAT;
2230 tv1->vval.v_float = f1;
2231 --ectx.ec_stack.ga_len;
2232 }
2233 else
2234#endif
2235 {
2236 switch (iptr->isn_arg.op.op_type)
2237 {
2238 case EXPR_MULT: n1 = n1 * n2; break;
2239 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2240 case EXPR_SUB: n1 = n1 - n2; break;
2241 case EXPR_ADD: n1 = n1 + n2; break;
2242 default: n1 = num_modulus(n1, n2); break;
2243 }
2244 clear_tv(tv1);
2245 clear_tv(tv2);
2246 tv1->v_type = VAR_NUMBER;
2247 tv1->vval.v_number = n1;
2248 --ectx.ec_stack.ga_len;
2249 }
2250 }
2251 break;
2252
2253 case ISN_CONCAT:
2254 {
2255 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2256 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2257 char_u *res;
2258
2259 res = concat_str(str1, str2);
2260 clear_tv(STACK_TV_BOT(-2));
2261 clear_tv(STACK_TV_BOT(-1));
2262 --ectx.ec_stack.ga_len;
2263 STACK_TV_BOT(-1)->vval.v_string = res;
2264 }
2265 break;
2266
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002267 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002268 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002269 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002270 int is_slice = iptr->isn_type == ISN_STRSLICE;
2271 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002272 char_u *res;
2273
2274 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002275 // string slice: string is at stack-3, first index at
2276 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002277 if (is_slice)
2278 {
2279 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002280 n1 = tv->vval.v_number;
2281 }
2282
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002283 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002284 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002285
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002286 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002287 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002288 if (is_slice)
2289 // Slice: Select the characters from the string
2290 res = string_slice(tv->vval.v_string, n1, n2);
2291 else
2292 // Index: The resulting variable is a string of a
2293 // single character. If the index is too big or
2294 // negative the result is empty.
2295 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002296 vim_free(tv->vval.v_string);
2297 tv->vval.v_string = res;
2298 }
2299 break;
2300
2301 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002302 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 {
Bram Moolenaared591872020-08-15 22:14:53 +02002304 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002306 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307
2308 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002309 // list slice: list is at stack-3, indexes at stack-2 and
2310 // stack-1
2311 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 list = tv->vval.v_list;
2313
2314 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002315 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002317
2318 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 {
Bram Moolenaared591872020-08-15 22:14:53 +02002320 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002321 n1 = tv->vval.v_number;
2322 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 }
Bram Moolenaared591872020-08-15 22:14:53 +02002324
2325 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002326 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002327 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002328 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2329 == FAIL)
2330 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331 }
2332 break;
2333
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002334 case ISN_ANYINDEX:
2335 case ISN_ANYSLICE:
2336 {
2337 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2338 typval_T *var1, *var2;
2339 int res;
2340
2341 // index: composite is at stack-2, index at stack-1
2342 // slice: composite is at stack-3, indexes at stack-2 and
2343 // stack-1
2344 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002345 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002346 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2347 goto on_error;
2348 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2349 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2350 res = eval_index_inner(tv, is_slice,
2351 var1, var2, NULL, -1, TRUE);
2352 clear_tv(var1);
2353 if (is_slice)
2354 clear_tv(var2);
2355 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2356 if (res == FAIL)
2357 goto on_error;
2358 }
2359 break;
2360
Bram Moolenaar9af78762020-06-16 11:34:42 +02002361 case ISN_SLICE:
2362 {
2363 list_T *list;
2364 int count = iptr->isn_arg.number;
2365
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002366 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002367 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002368 list = tv->vval.v_list;
2369
2370 // no error for short list, expect it to be checked earlier
2371 if (list != NULL && list->lv_len >= count)
2372 {
2373 list_T *newlist = list_slice(list,
2374 count, list->lv_len - 1);
2375
2376 if (newlist != NULL)
2377 {
2378 list_unref(list);
2379 tv->vval.v_list = newlist;
2380 ++newlist->lv_refcount;
2381 }
2382 }
2383 }
2384 break;
2385
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002386 case ISN_GETITEM:
2387 {
2388 listitem_T *li;
2389 int index = iptr->isn_arg.number;
2390
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002391 // Get list item: list is at stack-1, push item.
2392 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002393 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002394 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002395
2396 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2397 goto failed;
2398 ++ectx.ec_stack.ga_len;
2399 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2400 }
2401 break;
2402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 case ISN_MEMBER:
2404 {
2405 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002406 char_u *key;
2407 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002408 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002409
2410 // dict member: dict is at stack-2, key at stack-1
2411 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002412 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002413 dict = tv->vval.v_dict;
2414
2415 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002416 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002417 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002418
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002419 if ((di = dict_find(dict, key, -1)) == NULL)
2420 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002421 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002422 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002423 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002424 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002425 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002426 --ectx.ec_stack.ga_len;
2427 // Clear the dict after getting the item, to avoid that it
2428 // make the item invalid.
2429 tv = STACK_TV_BOT(-1);
2430 temp_tv = *tv;
2431 copy_tv(&di->di_tv, tv);
2432 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002433 }
2434 break;
2435
2436 // dict member with string key
2437 case ISN_STRINGMEMBER:
2438 {
2439 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002441 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442
2443 tv = STACK_TV_BOT(-1);
2444 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2445 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002446 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002448 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 }
2450 dict = tv->vval.v_dict;
2451
2452 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2453 == NULL)
2454 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002455 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002457 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002459 // Clear the dict after getting the item, to avoid that it
2460 // make the item invalid.
2461 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002463 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 }
2465 break;
2466
2467 case ISN_NEGATENR:
2468 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002469 if (tv->v_type != VAR_NUMBER
2470#ifdef FEAT_FLOAT
2471 && tv->v_type != VAR_FLOAT
2472#endif
2473 )
2474 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002475 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002476 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002477 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002478 }
2479#ifdef FEAT_FLOAT
2480 if (tv->v_type == VAR_FLOAT)
2481 tv->vval.v_float = -tv->vval.v_float;
2482 else
2483#endif
2484 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 break;
2486
2487 case ISN_CHECKNR:
2488 {
2489 int error = FALSE;
2490
2491 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002492 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002494 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 (void)tv_get_number_chk(tv, &error);
2496 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002497 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 }
2499 break;
2500
2501 case ISN_CHECKTYPE:
2502 {
2503 checktype_T *ct = &iptr->isn_arg.type;
2504
2505 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002506 // TODO: better type comparison
2507 if (tv->v_type != ct->ct_type
2508 && !((tv->v_type == VAR_PARTIAL
2509 && ct->ct_type == VAR_FUNC)
2510 || (tv->v_type == VAR_FUNC
2511 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 {
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002513 if (tv->v_type == VAR_NUMBER && ct->ct_type == VAR_BOOL
2514 && (tv->vval.v_number == 0
2515 || tv->vval.v_number == 1))
2516 {
2517 // number 0 is FALSE, number 1 is TRUE
2518 tv->v_type = VAR_BOOL;
2519 tv->vval.v_number = tv->vval.v_number
2520 ? VVAL_TRUE : VVAL_FALSE;
2521 }
2522 else
2523 {
2524 SOURCING_LNUM = iptr->isn_lnum;
2525 semsg(_(e_expected_str_but_got_str),
2526 vartype_name(ct->ct_type),
2527 vartype_name(tv->v_type));
2528 goto on_error;
2529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 }
2531 }
2532 break;
2533
Bram Moolenaar9af78762020-06-16 11:34:42 +02002534 case ISN_CHECKLEN:
2535 {
2536 int min_len = iptr->isn_arg.checklen.cl_min_len;
2537 list_T *list = NULL;
2538
2539 tv = STACK_TV_BOT(-1);
2540 if (tv->v_type == VAR_LIST)
2541 list = tv->vval.v_list;
2542 if (list == NULL || list->lv_len < min_len
2543 || (list->lv_len > min_len
2544 && !iptr->isn_arg.checklen.cl_more_OK))
2545 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002546 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002547 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002548 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002549 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002550 }
2551 }
2552 break;
2553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 case ISN_2BOOL:
2555 {
2556 int n;
2557
2558 tv = STACK_TV_BOT(-1);
2559 n = tv2bool(tv);
2560 if (iptr->isn_arg.number) // invert
2561 n = !n;
2562 clear_tv(tv);
2563 tv->v_type = VAR_BOOL;
2564 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2565 }
2566 break;
2567
2568 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002569 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 {
2571 char_u *str;
2572
2573 tv = STACK_TV_BOT(iptr->isn_arg.number);
2574 if (tv->v_type != VAR_STRING)
2575 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002576 if (iptr->isn_type == ISN_2STRING_ANY)
2577 {
2578 switch (tv->v_type)
2579 {
2580 case VAR_SPECIAL:
2581 case VAR_BOOL:
2582 case VAR_NUMBER:
2583 case VAR_FLOAT:
2584 case VAR_BLOB: break;
2585 default: to_string_error(tv->v_type);
2586 goto on_error;
2587 }
2588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 str = typval_tostring(tv);
2590 clear_tv(tv);
2591 tv->v_type = VAR_STRING;
2592 tv->vval.v_string = str;
2593 }
2594 }
2595 break;
2596
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002597 case ISN_PUT:
2598 {
2599 int regname = iptr->isn_arg.put.put_regname;
2600 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2601 char_u *expr = NULL;
2602 int dir = FORWARD;
2603
2604 if (regname == '=')
2605 {
2606 tv = STACK_TV_BOT(-1);
2607 if (tv->v_type == VAR_STRING)
2608 expr = tv->vval.v_string;
2609 else
2610 {
2611 expr = typval_tostring(tv); // allocates value
2612 clear_tv(tv);
2613 }
2614 --ectx.ec_stack.ga_len;
2615 }
2616 if (lnum == -2)
2617 // :put! above cursor
2618 dir = BACKWARD;
2619 else if (lnum >= 0)
2620 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2621 check_cursor();
2622 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2623 vim_free(expr);
2624 }
2625 break;
2626
Bram Moolenaar389df252020-07-09 21:20:47 +02002627 case ISN_SHUFFLE:
2628 {
2629 typval_T tmp_tv;
2630 int item = iptr->isn_arg.shuffle.shfl_item;
2631 int up = iptr->isn_arg.shuffle.shfl_up;
2632
2633 tmp_tv = *STACK_TV_BOT(-item);
2634 for ( ; up > 0 && item > 1; --up)
2635 {
2636 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2637 --item;
2638 }
2639 *STACK_TV_BOT(-item) = tmp_tv;
2640 }
2641 break;
2642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 case ISN_DROP:
2644 --ectx.ec_stack.ga_len;
2645 clear_tv(STACK_TV_BOT(0));
2646 break;
2647 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002648 continue;
2649
Bram Moolenaard032f342020-07-18 18:13:02 +02002650func_return:
2651 // Restore previous function. If the frame pointer is zero then there
2652 // is none and we are done.
2653 if (ectx.ec_frame_idx == initial_frame_idx)
2654 {
2655 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2656 // only fails when out of memory
2657 goto failed;
2658 goto done;
2659 }
2660 if (func_return(&ectx) == FAIL)
2661 // only fails when out of memory
2662 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002663 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002664
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002665on_error:
2666 if (trylevel == 0)
2667 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 }
2669
2670done:
2671 // function finished, get result from the stack.
2672 tv = STACK_TV_BOT(-1);
2673 *rettv = *tv;
2674 tv->v_type = VAR_UNKNOWN;
2675 ret = OK;
2676
2677failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002678 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002679 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002680 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002681
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002682 estack_pop();
2683 current_sctx = save_current_sctx;
2684
2685failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002686 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2688 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002691 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002692
2693 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002694 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002695 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 return ret;
2697}
2698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699/*
2700 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002701 * We don't really need this at runtime, but we do have tests that require it,
2702 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 */
2704 void
2705ex_disassemble(exarg_T *eap)
2706{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002707 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002708 char_u *fname;
2709 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 dfunc_T *dfunc;
2711 isn_T *instr;
2712 int current;
2713 int line_idx = 0;
2714 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002715 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002717 if (STRNCMP(arg, "<lambda>", 8) == 0)
2718 {
2719 arg += 8;
2720 (void)getdigits(&arg);
2721 fname = vim_strnsave(eap->arg, arg - eap->arg);
2722 }
2723 else
2724 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002725 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002726 if (fname == NULL)
2727 {
2728 semsg(_(e_invarg2), eap->arg);
2729 return;
2730 }
2731
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002732 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002733 if (ufunc == NULL)
2734 {
2735 char_u *p = untrans_function_name(fname);
2736
2737 if (p != NULL)
2738 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002739 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002740 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002741 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 if (ufunc == NULL)
2743 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002744 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 return;
2746 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002747 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002748 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2749 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002750 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002752 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 return;
2754 }
2755 if (ufunc->uf_name_exp != NULL)
2756 msg((char *)ufunc->uf_name_exp);
2757 else
2758 msg((char *)ufunc->uf_name);
2759
2760 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2761 instr = dfunc->df_instr;
2762 for (current = 0; current < dfunc->df_instr_count; ++current)
2763 {
2764 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002765 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766
2767 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2768 {
2769 if (current > prev_current)
2770 {
2771 msg_puts("\n\n");
2772 prev_current = current;
2773 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002774 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2775 if (line != NULL)
2776 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 }
2778
2779 switch (iptr->isn_type)
2780 {
2781 case ISN_EXEC:
2782 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2783 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002784 case ISN_EXECCONCAT:
2785 smsg("%4d EXECCONCAT %lld", current,
2786 (long long)iptr->isn_arg.number);
2787 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 case ISN_ECHO:
2789 {
2790 echo_T *echo = &iptr->isn_arg.echo;
2791
2792 smsg("%4d %s %d", current,
2793 echo->echo_with_white ? "ECHO" : "ECHON",
2794 echo->echo_count);
2795 }
2796 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002797 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002798 smsg("%4d EXECUTE %lld", current,
2799 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002800 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002801 case ISN_ECHOMSG:
2802 smsg("%4d ECHOMSG %lld", current,
2803 (long long)(iptr->isn_arg.number));
2804 break;
2805 case ISN_ECHOERR:
2806 smsg("%4d ECHOERR %lld", current,
2807 (long long)(iptr->isn_arg.number));
2808 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002810 case ISN_LOADOUTER:
2811 {
2812 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2813
2814 if (iptr->isn_arg.number < 0)
2815 smsg("%4d LOAD%s arg[%lld]", current, add,
2816 (long long)(iptr->isn_arg.number
2817 + STACK_FRAME_SIZE));
2818 else
2819 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002820 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002821 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 break;
2823 case ISN_LOADV:
2824 smsg("%4d LOADV v:%s", current,
2825 get_vim_var_name(iptr->isn_arg.number));
2826 break;
2827 case ISN_LOADSCRIPT:
2828 {
2829 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002830 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2832 + iptr->isn_arg.script.script_idx;
2833
2834 smsg("%4d LOADSCRIPT %s from %s", current,
2835 sv->sv_name, si->sn_name);
2836 }
2837 break;
2838 case ISN_LOADS:
2839 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002840 scriptitem_T *si = SCRIPT_ITEM(
2841 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842
2843 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002844 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 }
2846 break;
2847 case ISN_LOADG:
2848 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2849 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002850 case ISN_LOADB:
2851 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2852 break;
2853 case ISN_LOADW:
2854 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2855 break;
2856 case ISN_LOADT:
2857 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2858 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002859 case ISN_LOADGDICT:
2860 smsg("%4d LOAD g:", current);
2861 break;
2862 case ISN_LOADBDICT:
2863 smsg("%4d LOAD b:", current);
2864 break;
2865 case ISN_LOADWDICT:
2866 smsg("%4d LOAD w:", current);
2867 break;
2868 case ISN_LOADTDICT:
2869 smsg("%4d LOAD t:", current);
2870 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 case ISN_LOADOPT:
2872 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2873 break;
2874 case ISN_LOADENV:
2875 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2876 break;
2877 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002878 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 break;
2880
2881 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002882 case ISN_STOREOUTER:
2883 {
2884 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2885
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002886 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002887 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002888 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002889 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002890 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002891 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002894 case ISN_STOREV:
2895 smsg("%4d STOREV v:%s", current,
2896 get_vim_var_name(iptr->isn_arg.number));
2897 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002899 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2900 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002901 case ISN_STOREB:
2902 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2903 break;
2904 case ISN_STOREW:
2905 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2906 break;
2907 case ISN_STORET:
2908 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2909 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002910 case ISN_STORES:
2911 {
2912 scriptitem_T *si = SCRIPT_ITEM(
2913 iptr->isn_arg.loadstore.ls_sid);
2914
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002915 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002916 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 break;
2919 case ISN_STORESCRIPT:
2920 {
2921 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002922 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2924 + iptr->isn_arg.script.script_idx;
2925
2926 smsg("%4d STORESCRIPT %s in %s", current,
2927 sv->sv_name, si->sn_name);
2928 }
2929 break;
2930 case ISN_STOREOPT:
2931 smsg("%4d STOREOPT &%s", current,
2932 iptr->isn_arg.storeopt.so_name);
2933 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002934 case ISN_STOREENV:
2935 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2936 break;
2937 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002938 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002939 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 case ISN_STORENR:
2941 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002942 iptr->isn_arg.storenr.stnr_val,
2943 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 break;
2945
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002946 case ISN_STORELIST:
2947 smsg("%4d STORELIST", current);
2948 break;
2949
2950 case ISN_STOREDICT:
2951 smsg("%4d STOREDICT", current);
2952 break;
2953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 // constants
2955 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002956 smsg("%4d PUSHNR %lld", current,
2957 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 break;
2959 case ISN_PUSHBOOL:
2960 case ISN_PUSHSPEC:
2961 smsg("%4d PUSH %s", current,
2962 get_var_special_name(iptr->isn_arg.number));
2963 break;
2964 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002965#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002967#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 break;
2969 case ISN_PUSHS:
2970 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2971 break;
2972 case ISN_PUSHBLOB:
2973 {
2974 char_u *r;
2975 char_u numbuf[NUMBUFLEN];
2976 char_u *tofree;
2977
2978 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002979 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 vim_free(tofree);
2981 }
2982 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002983 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002984 {
2985 char *name = (char *)iptr->isn_arg.string;
2986
2987 smsg("%4d PUSHFUNC \"%s\"", current,
2988 name == NULL ? "[none]" : name);
2989 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002990 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002991 case ISN_PUSHCHANNEL:
2992#ifdef FEAT_JOB_CHANNEL
2993 {
2994 channel_T *channel = iptr->isn_arg.channel;
2995
2996 smsg("%4d PUSHCHANNEL %d", current,
2997 channel == NULL ? 0 : channel->ch_id);
2998 }
2999#endif
3000 break;
3001 case ISN_PUSHJOB:
3002#ifdef FEAT_JOB_CHANNEL
3003 {
3004 typval_T tv;
3005 char_u *name;
3006
3007 tv.v_type = VAR_JOB;
3008 tv.vval.v_job = iptr->isn_arg.job;
3009 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003010 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003011 }
3012#endif
3013 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 case ISN_PUSHEXC:
3015 smsg("%4d PUSH v:exception", current);
3016 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003017 case ISN_UNLET:
3018 smsg("%4d UNLET%s %s", current,
3019 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3020 iptr->isn_arg.unlet.ul_name);
3021 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003022 case ISN_UNLETENV:
3023 smsg("%4d UNLETENV%s $%s", current,
3024 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3025 iptr->isn_arg.unlet.ul_name);
3026 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003028 smsg("%4d NEWLIST size %lld", current,
3029 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 break;
3031 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003032 smsg("%4d NEWDICT size %lld", current,
3033 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 break;
3035
3036 // function call
3037 case ISN_BCALL:
3038 {
3039 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3040
3041 smsg("%4d BCALL %s(argc %d)", current,
3042 internal_func_name(cbfunc->cbf_idx),
3043 cbfunc->cbf_argcount);
3044 }
3045 break;
3046 case ISN_DCALL:
3047 {
3048 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3049 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3050 + cdfunc->cdf_idx;
3051
3052 smsg("%4d DCALL %s(argc %d)", current,
3053 df->df_ufunc->uf_name_exp != NULL
3054 ? df->df_ufunc->uf_name_exp
3055 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3056 }
3057 break;
3058 case ISN_UCALL:
3059 {
3060 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3061
3062 smsg("%4d UCALL %s(argc %d)", current,
3063 cufunc->cuf_name, cufunc->cuf_argcount);
3064 }
3065 break;
3066 case ISN_PCALL:
3067 {
3068 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3069
3070 smsg("%4d PCALL%s (argc %d)", current,
3071 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3072 }
3073 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003074 case ISN_PCALL_END:
3075 smsg("%4d PCALL end", current);
3076 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 case ISN_RETURN:
3078 smsg("%4d RETURN", current);
3079 break;
3080 case ISN_FUNCREF:
3081 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003082 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003084 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003086 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003087 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 }
3089 break;
3090
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003091 case ISN_NEWFUNC:
3092 {
3093 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3094
3095 smsg("%4d NEWFUNC %s %s", current,
3096 newfunc->nf_lambda, newfunc->nf_global);
3097 }
3098 break;
3099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 case ISN_JUMP:
3101 {
3102 char *when = "?";
3103
3104 switch (iptr->isn_arg.jump.jump_when)
3105 {
3106 case JUMP_ALWAYS:
3107 when = "JUMP";
3108 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 case JUMP_AND_KEEP_IF_TRUE:
3110 when = "JUMP_AND_KEEP_IF_TRUE";
3111 break;
3112 case JUMP_IF_FALSE:
3113 when = "JUMP_IF_FALSE";
3114 break;
3115 case JUMP_AND_KEEP_IF_FALSE:
3116 when = "JUMP_AND_KEEP_IF_FALSE";
3117 break;
3118 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003119 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 iptr->isn_arg.jump.jump_where);
3121 }
3122 break;
3123
3124 case ISN_FOR:
3125 {
3126 forloop_T *forloop = &iptr->isn_arg.forloop;
3127
3128 smsg("%4d FOR $%d -> %d", current,
3129 forloop->for_idx, forloop->for_end);
3130 }
3131 break;
3132
3133 case ISN_TRY:
3134 {
3135 try_T *try = &iptr->isn_arg.try;
3136
3137 smsg("%4d TRY catch -> %d, finally -> %d", current,
3138 try->try_catch, try->try_finally);
3139 }
3140 break;
3141 case ISN_CATCH:
3142 // TODO
3143 smsg("%4d CATCH", current);
3144 break;
3145 case ISN_ENDTRY:
3146 smsg("%4d ENDTRY", current);
3147 break;
3148 case ISN_THROW:
3149 smsg("%4d THROW", current);
3150 break;
3151
3152 // expression operations on number
3153 case ISN_OPNR:
3154 case ISN_OPFLOAT:
3155 case ISN_OPANY:
3156 {
3157 char *what;
3158 char *ins;
3159
3160 switch (iptr->isn_arg.op.op_type)
3161 {
3162 case EXPR_MULT: what = "*"; break;
3163 case EXPR_DIV: what = "/"; break;
3164 case EXPR_REM: what = "%"; break;
3165 case EXPR_SUB: what = "-"; break;
3166 case EXPR_ADD: what = "+"; break;
3167 default: what = "???"; break;
3168 }
3169 switch (iptr->isn_type)
3170 {
3171 case ISN_OPNR: ins = "OPNR"; break;
3172 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3173 case ISN_OPANY: ins = "OPANY"; break;
3174 default: ins = "???"; break;
3175 }
3176 smsg("%4d %s %s", current, ins, what);
3177 }
3178 break;
3179
3180 case ISN_COMPAREBOOL:
3181 case ISN_COMPARESPECIAL:
3182 case ISN_COMPARENR:
3183 case ISN_COMPAREFLOAT:
3184 case ISN_COMPARESTRING:
3185 case ISN_COMPAREBLOB:
3186 case ISN_COMPARELIST:
3187 case ISN_COMPAREDICT:
3188 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 case ISN_COMPAREANY:
3190 {
3191 char *p;
3192 char buf[10];
3193 char *type;
3194
3195 switch (iptr->isn_arg.op.op_type)
3196 {
3197 case EXPR_EQUAL: p = "=="; break;
3198 case EXPR_NEQUAL: p = "!="; break;
3199 case EXPR_GREATER: p = ">"; break;
3200 case EXPR_GEQUAL: p = ">="; break;
3201 case EXPR_SMALLER: p = "<"; break;
3202 case EXPR_SEQUAL: p = "<="; break;
3203 case EXPR_MATCH: p = "=~"; break;
3204 case EXPR_IS: p = "is"; break;
3205 case EXPR_ISNOT: p = "isnot"; break;
3206 case EXPR_NOMATCH: p = "!~"; break;
3207 default: p = "???"; break;
3208 }
3209 STRCPY(buf, p);
3210 if (iptr->isn_arg.op.op_ic == TRUE)
3211 strcat(buf, "?");
3212 switch(iptr->isn_type)
3213 {
3214 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3215 case ISN_COMPARESPECIAL:
3216 type = "COMPARESPECIAL"; break;
3217 case ISN_COMPARENR: type = "COMPARENR"; break;
3218 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3219 case ISN_COMPARESTRING:
3220 type = "COMPARESTRING"; break;
3221 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3222 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3223 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3224 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3226 default: type = "???"; break;
3227 }
3228
3229 smsg("%4d %s %s", current, type, buf);
3230 }
3231 break;
3232
3233 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3234 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3235
3236 // expression operations
3237 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003238 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003239 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003240 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003241 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003242 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3243 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003244 case ISN_SLICE: smsg("%4d SLICE %lld",
3245 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003246 case ISN_GETITEM: smsg("%4d ITEM %lld",
3247 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003248 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3249 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 iptr->isn_arg.string); break;
3251 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3252
3253 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3254 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3255 vartype_name(iptr->isn_arg.type.ct_type),
3256 iptr->isn_arg.type.ct_off);
3257 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003258 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3259 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3260 iptr->isn_arg.checklen.cl_min_len);
3261 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 case ISN_2BOOL: if (iptr->isn_arg.number)
3263 smsg("%4d INVERT (!val)", current);
3264 else
3265 smsg("%4d 2BOOL (!!val)", current);
3266 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003267 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3268 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003269 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003270 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3271 (long long)(iptr->isn_arg.number));
3272 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003273 case ISN_PUT:
3274 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3275 (long)iptr->isn_arg.put.put_lnum);
3276 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277
Bram Moolenaar389df252020-07-09 21:20:47 +02003278 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3279 iptr->isn_arg.shuffle.shfl_item,
3280 iptr->isn_arg.shuffle.shfl_up);
3281 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 case ISN_DROP: smsg("%4d DROP", current); break;
3283 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003284
3285 out_flush(); // output one line at a time
3286 ui_breakcheck();
3287 if (got_int)
3288 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290}
3291
3292/*
3293 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3294 * list, etc. Mostly like what JavaScript does, except that empty list and
3295 * empty dictionary are FALSE.
3296 */
3297 int
3298tv2bool(typval_T *tv)
3299{
3300 switch (tv->v_type)
3301 {
3302 case VAR_NUMBER:
3303 return tv->vval.v_number != 0;
3304 case VAR_FLOAT:
3305#ifdef FEAT_FLOAT
3306 return tv->vval.v_float != 0.0;
3307#else
3308 break;
3309#endif
3310 case VAR_PARTIAL:
3311 return tv->vval.v_partial != NULL;
3312 case VAR_FUNC:
3313 case VAR_STRING:
3314 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3315 case VAR_LIST:
3316 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3317 case VAR_DICT:
3318 return tv->vval.v_dict != NULL
3319 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3320 case VAR_BOOL:
3321 case VAR_SPECIAL:
3322 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3323 case VAR_JOB:
3324#ifdef FEAT_JOB_CHANNEL
3325 return tv->vval.v_job != NULL;
3326#else
3327 break;
3328#endif
3329 case VAR_CHANNEL:
3330#ifdef FEAT_JOB_CHANNEL
3331 return tv->vval.v_channel != NULL;
3332#else
3333 break;
3334#endif
3335 case VAR_BLOB:
3336 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3337 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003338 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 case VAR_VOID:
3340 break;
3341 }
3342 return FALSE;
3343}
3344
3345/*
3346 * If "tv" is a string give an error and return FAIL.
3347 */
3348 int
3349check_not_string(typval_T *tv)
3350{
3351 if (tv->v_type == VAR_STRING)
3352 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003353 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 clear_tv(tv);
3355 return FAIL;
3356 }
3357 return OK;
3358}
3359
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360
3361#endif // FEAT_EVAL