blob: e4ccaaa8a11124822425dd70a2ba3ca13c618173 [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 Moolenaar0b4c66c2020-09-14 21:39:44 +0200681 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
682 * TRUE.
683 */
684 static int
685error_if_locked(int lock, char *error)
686{
687 if (lock & (VAR_LOCKED | VAR_FIXED))
688 {
689 emsg(_(error));
690 return TRUE;
691 }
692 return FALSE;
693}
694
695/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100696 * Store "tv" in variable "name".
697 * This is for s: and g: variables.
698 */
699 static void
700store_var(char_u *name, typval_T *tv)
701{
702 funccal_entry_T entry;
703
704 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200705 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100706 restore_funccal();
707}
708
Bram Moolenaard3aac292020-04-19 14:32:17 +0200709
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100710/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 * Execute a function by "name".
712 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100713 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 */
715 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100716call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717{
Bram Moolenaared677f52020-08-12 16:38:10 +0200718 int called_emsg_before = called_emsg;
719 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720
Bram Moolenaared677f52020-08-12 16:38:10 +0200721 res = call_by_name(name, argcount, ectx, iptr);
722 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200724 dictitem_T *v;
725
726 v = find_var(name, NULL, FALSE);
727 if (v == NULL)
728 {
729 semsg(_(e_unknownfunc), name);
730 return FAIL;
731 }
732 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
733 {
734 semsg(_(e_unknownfunc), name);
735 return FAIL;
736 }
737 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200739 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100740}
741
742/*
743 * Call a "def" function from old Vim script.
744 * Return OK or FAIL.
745 */
746 int
747call_def_function(
748 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200749 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200751 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 typval_T *rettv) // return value
753{
754 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200755 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200756 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 typval_T *tv;
758 int idx;
759 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100760 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200761 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200762 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200763 int called_emsg_before = called_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200764 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765
766// Get pointer to item in the stack.
767#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
768
769// Get pointer to item at the bottom of the stack, -1 is the bottom.
770#undef STACK_TV_BOT
771#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
772
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200773// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200774#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 +0100775
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200776// Like STACK_TV_VAR but use the outer scope
777#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
778
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200779 if (ufunc->uf_def_status == UF_NOT_COMPILED
780 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200781 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200782 {
783 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200784 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200785 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200786 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200787 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200788
Bram Moolenaar09689a02020-05-09 22:50:08 +0200789 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200790 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200791 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
792 + ufunc->uf_dfunc_idx;
793 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200794 {
795 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200796 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200797 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200798 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200800 CLEAR_FIELD(ectx);
801 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
802 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
803 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
804 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
806
807 // Put arguments on the stack.
808 for (idx = 0; idx < argc; ++idx)
809 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200810 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200811 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
812 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200813 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814 copy_tv(&argv[idx], STACK_TV_BOT(0));
815 ++ectx.ec_stack.ga_len;
816 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200817
818 // Turn varargs into a list. Empty list if no args.
819 if (ufunc->uf_va_name != NULL)
820 {
821 int vararg_count = argc - ufunc->uf_args.ga_len;
822
823 if (vararg_count < 0)
824 vararg_count = 0;
825 else
826 argc -= vararg_count;
827 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200828 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200829
830 // Check the type of the list items.
831 tv = STACK_TV_BOT(-1);
832 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200833 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200834 && ufunc->uf_va_type->tt_member != &t_any
835 && tv->vval.v_list != NULL)
836 {
837 type_T *expected = ufunc->uf_va_type->tt_member;
838 listitem_T *li = tv->vval.v_list->lv_first;
839
840 for (idx = 0; idx < vararg_count; ++idx)
841 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200842 if (check_typval_type(expected, &li->li_tv,
843 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200844 goto failed_early;
845 li = li->li_next;
846 }
847 }
848
Bram Moolenaar23e03252020-04-12 22:22:31 +0200849 if (defcount > 0)
850 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200851 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200852 --ectx.ec_stack.ga_len;
853 }
854
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100855 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200856 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100857 if (defcount > 0)
858 for (idx = 0; idx < defcount; ++idx)
859 {
860 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
861 ++ectx.ec_stack.ga_len;
862 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200863 if (ufunc->uf_va_name != NULL)
864 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865
866 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200867 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
868 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200870 if (partial != NULL)
871 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200872 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
873 {
874 // TODO: is this always the right way?
875 ectx.ec_outer_stack = &current_ectx->ec_stack;
876 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
877 }
878 else
879 {
880 ectx.ec_outer_stack = partial->pt_ectx_stack;
881 ectx.ec_outer_frame = partial->pt_ectx_frame;
882 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200883 }
884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 // dummy frame entries
886 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
887 {
888 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
889 ++ectx.ec_stack.ga_len;
890 }
891
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200892 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200893 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200894 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
895 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200896 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200898 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200899 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200900 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200901
902 ectx.ec_instr = dfunc->df_instr;
903 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100904
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200905 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200906 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200907 estack_push_ufunc(ufunc, 1);
908 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200909 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
910
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200911 // Do turn errors into exceptions.
912 suppress_errthrow = FALSE;
913
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100914 // Decide where to start execution, handles optional arguments.
915 init_instr_idx(ufunc, argc, &ectx);
916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 for (;;)
918 {
919 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100920
Bram Moolenaar270d0382020-05-15 21:42:53 +0200921 if (++breakcheck_count >= 100)
922 {
923 line_breakcheck();
924 breakcheck_count = 0;
925 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100926 if (got_int)
927 {
928 // Turn CTRL-C into an exception.
929 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100930 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100931 goto failed;
932 did_throw = TRUE;
933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934
Bram Moolenaara26b9702020-04-18 19:53:28 +0200935 if (did_emsg && msg_list != NULL && *msg_list != NULL)
936 {
937 // Turn an error message into an exception.
938 did_emsg = FALSE;
939 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
940 goto failed;
941 did_throw = TRUE;
942 *msg_list = NULL;
943 }
944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 if (did_throw && !ectx.ec_in_catch)
946 {
947 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100948 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949
950 // An exception jumps to the first catch, finally, or returns from
951 // the current function.
952 if (trystack->ga_len > 0)
953 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200954 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 {
956 // jump to ":catch" or ":finally"
957 ectx.ec_in_catch = TRUE;
958 ectx.ec_iidx = trycmd->tcd_catch_idx;
959 }
960 else
961 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200962 // Not inside try or need to return from current functions.
963 // Push a dummy return value.
964 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
965 goto failed;
966 tv = STACK_TV_BOT(0);
967 tv->v_type = VAR_NUMBER;
968 tv->vval.v_number = 0;
969 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200970 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200972 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100973 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200974 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
975 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 goto done;
977 }
978
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200979 if (func_return(&ectx) == FAIL)
980 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 }
982 continue;
983 }
984
985 iptr = &ectx.ec_instr[ectx.ec_iidx++];
986 switch (iptr->isn_type)
987 {
988 // execute Ex command line
989 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200990 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991 do_cmdline_cmd(iptr->isn_arg.string);
992 break;
993
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200994 // execute Ex command from pieces on the stack
995 case ISN_EXECCONCAT:
996 {
997 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200998 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200999 int pass;
1000 int i;
1001 char_u *cmd = NULL;
1002 char_u *str;
1003
1004 for (pass = 1; pass <= 2; ++pass)
1005 {
1006 for (i = 0; i < count; ++i)
1007 {
1008 tv = STACK_TV_BOT(i - count);
1009 str = tv->vval.v_string;
1010 if (str != NULL && *str != NUL)
1011 {
1012 if (pass == 2)
1013 STRCPY(cmd + len, str);
1014 len += STRLEN(str);
1015 }
1016 if (pass == 2)
1017 clear_tv(tv);
1018 }
1019 if (pass == 1)
1020 {
1021 cmd = alloc(len + 1);
1022 if (cmd == NULL)
1023 goto failed;
1024 len = 0;
1025 }
1026 }
1027
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001028 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001029 do_cmdline_cmd(cmd);
1030 vim_free(cmd);
1031 }
1032 break;
1033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034 // execute :echo {string} ...
1035 case ISN_ECHO:
1036 {
1037 int count = iptr->isn_arg.echo.echo_count;
1038 int atstart = TRUE;
1039 int needclr = TRUE;
1040
1041 for (idx = 0; idx < count; ++idx)
1042 {
1043 tv = STACK_TV_BOT(idx - count);
1044 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1045 &atstart, &needclr);
1046 clear_tv(tv);
1047 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001048 if (needclr)
1049 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 ectx.ec_stack.ga_len -= count;
1051 }
1052 break;
1053
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001054 // :execute {string} ...
1055 // :echomsg {string} ...
1056 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001057 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001058 case ISN_ECHOMSG:
1059 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001060 {
1061 int count = iptr->isn_arg.number;
1062 garray_T ga;
1063 char_u buf[NUMBUFLEN];
1064 char_u *p;
1065 int len;
1066 int failed = FALSE;
1067
1068 ga_init2(&ga, 1, 80);
1069 for (idx = 0; idx < count; ++idx)
1070 {
1071 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001072 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001073 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001074 if (tv->v_type == VAR_CHANNEL
1075 || tv->v_type == VAR_JOB)
1076 {
1077 SOURCING_LNUM = iptr->isn_lnum;
1078 emsg(_(e_inval_string));
1079 break;
1080 }
1081 else
1082 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001083 }
1084 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001085 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001086
1087 len = (int)STRLEN(p);
1088 if (ga_grow(&ga, len + 2) == FAIL)
1089 failed = TRUE;
1090 else
1091 {
1092 if (ga.ga_len > 0)
1093 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1094 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1095 ga.ga_len += len;
1096 }
1097 clear_tv(tv);
1098 }
1099 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001100 if (failed)
1101 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001102
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001103 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001104 {
1105 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001106 {
1107 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001108 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001109 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001110 else
1111 {
1112 msg_sb_eol();
1113 if (iptr->isn_type == ISN_ECHOMSG)
1114 {
1115 msg_attr(ga.ga_data, echo_attr);
1116 out_flush();
1117 }
1118 else
1119 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001120 SOURCING_LNUM = iptr->isn_lnum;
1121 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001122 }
1123 }
1124 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001125 ga_clear(&ga);
1126 }
1127 break;
1128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 // load local variable or argument
1130 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001131 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 goto failed;
1133 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1134 ++ectx.ec_stack.ga_len;
1135 break;
1136
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001137 // load variable or argument from outer scope
1138 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001139 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001140 goto failed;
1141 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1142 STACK_TV_BOT(0));
1143 ++ectx.ec_stack.ga_len;
1144 break;
1145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 // load v: variable
1147 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001148 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 goto failed;
1150 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1151 ++ectx.ec_stack.ga_len;
1152 break;
1153
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001154 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 case ISN_LOADSCRIPT:
1156 {
1157 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001158 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159 svar_T *sv;
1160
1161 sv = ((svar_T *)si->sn_var_vals.ga_data)
1162 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001163 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164 goto failed;
1165 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1166 ++ectx.ec_stack.ga_len;
1167 }
1168 break;
1169
1170 // load s: variable in old script
1171 case ISN_LOADS:
1172 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001173 hashtab_T *ht = &SCRIPT_VARS(
1174 iptr->isn_arg.loadstore.ls_sid);
1175 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 if (di == NULL)
1179 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001180 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001181 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001182 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 }
1184 else
1185 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001186 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 goto failed;
1188 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1189 ++ectx.ec_stack.ga_len;
1190 }
1191 }
1192 break;
1193
Bram Moolenaard3aac292020-04-19 14:32:17 +02001194 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001196 case ISN_LOADB:
1197 case ISN_LOADW:
1198 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001200 dictitem_T *di = NULL;
1201 hashtab_T *ht = NULL;
1202 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001203
Bram Moolenaard3aac292020-04-19 14:32:17 +02001204 switch (iptr->isn_type)
1205 {
1206 case ISN_LOADG:
1207 ht = get_globvar_ht();
1208 namespace = 'g';
1209 break;
1210 case ISN_LOADB:
1211 ht = &curbuf->b_vars->dv_hashtab;
1212 namespace = 'b';
1213 break;
1214 case ISN_LOADW:
1215 ht = &curwin->w_vars->dv_hashtab;
1216 namespace = 'w';
1217 break;
1218 case ISN_LOADT:
1219 ht = &curtab->tp_vars->dv_hashtab;
1220 namespace = 't';
1221 break;
1222 default: // Cannot reach here
1223 goto failed;
1224 }
1225 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227 if (di == NULL)
1228 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001229 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001230 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001231 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001232 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 }
1234 else
1235 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001236 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 goto failed;
1238 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1239 ++ectx.ec_stack.ga_len;
1240 }
1241 }
1242 break;
1243
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001244 // load g:/b:/w:/t: namespace
1245 case ISN_LOADGDICT:
1246 case ISN_LOADBDICT:
1247 case ISN_LOADWDICT:
1248 case ISN_LOADTDICT:
1249 {
1250 dict_T *d = NULL;
1251
1252 switch (iptr->isn_type)
1253 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001254 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1255 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1256 case ISN_LOADWDICT: d = curwin->w_vars; break;
1257 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001258 default: // Cannot reach here
1259 goto failed;
1260 }
1261 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1262 goto failed;
1263 tv = STACK_TV_BOT(0);
1264 tv->v_type = VAR_DICT;
1265 tv->v_lock = 0;
1266 tv->vval.v_dict = d;
1267 ++ectx.ec_stack.ga_len;
1268 }
1269 break;
1270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 // load &option
1272 case ISN_LOADOPT:
1273 {
1274 typval_T optval;
1275 char_u *name = iptr->isn_arg.string;
1276
Bram Moolenaara8c17702020-04-01 21:17:24 +02001277 // This is not expected to fail, name is checked during
1278 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001279 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001281 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001282 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 *STACK_TV_BOT(0) = optval;
1284 ++ectx.ec_stack.ga_len;
1285 }
1286 break;
1287
1288 // load $ENV
1289 case ISN_LOADENV:
1290 {
1291 typval_T optval;
1292 char_u *name = iptr->isn_arg.string;
1293
Bram Moolenaar270d0382020-05-15 21:42:53 +02001294 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001296 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001297 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 *STACK_TV_BOT(0) = optval;
1299 ++ectx.ec_stack.ga_len;
1300 }
1301 break;
1302
1303 // load @register
1304 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001305 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 goto failed;
1307 tv = STACK_TV_BOT(0);
1308 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001309 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 tv->vval.v_string = get_reg_contents(
1311 iptr->isn_arg.number, GREG_EXPR_SRC);
1312 ++ectx.ec_stack.ga_len;
1313 break;
1314
1315 // store local variable
1316 case ISN_STORE:
1317 --ectx.ec_stack.ga_len;
1318 tv = STACK_TV_VAR(iptr->isn_arg.number);
1319 clear_tv(tv);
1320 *tv = *STACK_TV_BOT(0);
1321 break;
1322
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001323 // store variable or argument in outer scope
1324 case ISN_STOREOUTER:
1325 --ectx.ec_stack.ga_len;
1326 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1327 clear_tv(tv);
1328 *tv = *STACK_TV_BOT(0);
1329 break;
1330
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001331 // store s: variable in old script
1332 case ISN_STORES:
1333 {
1334 hashtab_T *ht = &SCRIPT_VARS(
1335 iptr->isn_arg.loadstore.ls_sid);
1336 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001337 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001338
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001339 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001340 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001341 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001342 else
1343 {
1344 clear_tv(&di->di_tv);
1345 di->di_tv = *STACK_TV_BOT(0);
1346 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001347 }
1348 break;
1349
1350 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 case ISN_STORESCRIPT:
1352 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001353 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 iptr->isn_arg.script.script_sid);
1355 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1356 + iptr->isn_arg.script.script_idx;
1357
1358 --ectx.ec_stack.ga_len;
1359 clear_tv(sv->sv_tv);
1360 *sv->sv_tv = *STACK_TV_BOT(0);
1361 }
1362 break;
1363
1364 // store option
1365 case ISN_STOREOPT:
1366 {
1367 long n = 0;
1368 char_u *s = NULL;
1369 char *msg;
1370
1371 --ectx.ec_stack.ga_len;
1372 tv = STACK_TV_BOT(0);
1373 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001374 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001376 if (s == NULL)
1377 s = (char_u *)"";
1378 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001380 // must be VAR_NUMBER, CHECKTYPE makes sure
1381 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1383 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001384 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 if (msg != NULL)
1386 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001387 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001389 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 }
1392 break;
1393
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001394 // store $ENV
1395 case ISN_STOREENV:
1396 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001397 tv = STACK_TV_BOT(0);
1398 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1399 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001400 break;
1401
1402 // store @r
1403 case ISN_STOREREG:
1404 {
1405 int reg = iptr->isn_arg.number;
1406
1407 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001408 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001409 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001410 tv_get_string(tv), -1, FALSE);
1411 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001412 }
1413 break;
1414
1415 // store v: variable
1416 case ISN_STOREV:
1417 --ectx.ec_stack.ga_len;
1418 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1419 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001420 // should not happen, type is checked when compiling
1421 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001422 break;
1423
Bram Moolenaard3aac292020-04-19 14:32:17 +02001424 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001426 case ISN_STOREB:
1427 case ISN_STOREW:
1428 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 {
1430 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001431 hashtab_T *ht;
1432 switch (iptr->isn_type)
1433 {
1434 case ISN_STOREG:
1435 ht = get_globvar_ht();
1436 break;
1437 case ISN_STOREB:
1438 ht = &curbuf->b_vars->dv_hashtab;
1439 break;
1440 case ISN_STOREW:
1441 ht = &curwin->w_vars->dv_hashtab;
1442 break;
1443 case ISN_STORET:
1444 ht = &curtab->tp_vars->dv_hashtab;
1445 break;
1446 default: // Cannot reach here
1447 goto failed;
1448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449
1450 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001451 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001453 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 else
1455 {
1456 clear_tv(&di->di_tv);
1457 di->di_tv = *STACK_TV_BOT(0);
1458 }
1459 }
1460 break;
1461
1462 // store number in local variable
1463 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001464 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 clear_tv(tv);
1466 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001467 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 break;
1469
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001470 // store value in list variable
1471 case ISN_STORELIST:
1472 {
1473 typval_T *tv_idx = STACK_TV_BOT(-2);
1474 varnumber_T lidx = tv_idx->vval.v_number;
1475 typval_T *tv_list = STACK_TV_BOT(-1);
1476 list_T *list = tv_list->vval.v_list;
1477
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001478 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001479 if (lidx < 0 && list->lv_len + lidx >= 0)
1480 // negative index is relative to the end
1481 lidx = list->lv_len + lidx;
1482 if (lidx < 0 || lidx > list->lv_len)
1483 {
1484 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001485 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001486 }
1487 tv = STACK_TV_BOT(-3);
1488 if (lidx < list->lv_len)
1489 {
1490 listitem_T *li = list_find(list, lidx);
1491
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001492 if (error_if_locked(li->li_tv.v_lock,
1493 e_cannot_change_list_item))
1494 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001495 // overwrite existing list item
1496 clear_tv(&li->li_tv);
1497 li->li_tv = *tv;
1498 }
1499 else
1500 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001501 if (error_if_locked(list->lv_lock,
1502 e_cannot_change_list))
1503 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001504 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001505 if (list_append_tv(list, tv) == FAIL)
1506 goto failed;
1507 clear_tv(tv);
1508 }
1509 clear_tv(tv_idx);
1510 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001511 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001512 }
1513 break;
1514
1515 // store value in dict variable
1516 case ISN_STOREDICT:
1517 {
1518 typval_T *tv_key = STACK_TV_BOT(-2);
1519 char_u *key = tv_key->vval.v_string;
1520 typval_T *tv_dict = STACK_TV_BOT(-1);
1521 dict_T *dict = tv_dict->vval.v_dict;
1522 dictitem_T *di;
1523
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001524 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001525 if (dict == NULL)
1526 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001527 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001528 goto on_error;
1529 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001530 if (key == NULL)
1531 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001532 tv = STACK_TV_BOT(-3);
1533 di = dict_find(dict, key, -1);
1534 if (di != NULL)
1535 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001536 if (error_if_locked(di->di_tv.v_lock,
1537 e_cannot_change_dict_item))
1538 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001539 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001540 clear_tv(&di->di_tv);
1541 di->di_tv = *tv;
1542 }
1543 else
1544 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001545 if (error_if_locked(dict->dv_lock,
1546 e_cannot_change_dict))
1547 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001548 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001549 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1550 goto failed;
1551 clear_tv(tv);
1552 }
1553 clear_tv(tv_key);
1554 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001555 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001556 }
1557 break;
1558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 // push constant
1560 case ISN_PUSHNR:
1561 case ISN_PUSHBOOL:
1562 case ISN_PUSHSPEC:
1563 case ISN_PUSHF:
1564 case ISN_PUSHS:
1565 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001566 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001567 case ISN_PUSHCHANNEL:
1568 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001569 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 goto failed;
1571 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001572 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 ++ectx.ec_stack.ga_len;
1574 switch (iptr->isn_type)
1575 {
1576 case ISN_PUSHNR:
1577 tv->v_type = VAR_NUMBER;
1578 tv->vval.v_number = iptr->isn_arg.number;
1579 break;
1580 case ISN_PUSHBOOL:
1581 tv->v_type = VAR_BOOL;
1582 tv->vval.v_number = iptr->isn_arg.number;
1583 break;
1584 case ISN_PUSHSPEC:
1585 tv->v_type = VAR_SPECIAL;
1586 tv->vval.v_number = iptr->isn_arg.number;
1587 break;
1588#ifdef FEAT_FLOAT
1589 case ISN_PUSHF:
1590 tv->v_type = VAR_FLOAT;
1591 tv->vval.v_float = iptr->isn_arg.fnumber;
1592 break;
1593#endif
1594 case ISN_PUSHBLOB:
1595 blob_copy(iptr->isn_arg.blob, tv);
1596 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001597 case ISN_PUSHFUNC:
1598 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001599 if (iptr->isn_arg.string == NULL)
1600 tv->vval.v_string = NULL;
1601 else
1602 tv->vval.v_string =
1603 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001604 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001605 case ISN_PUSHCHANNEL:
1606#ifdef FEAT_JOB_CHANNEL
1607 tv->v_type = VAR_CHANNEL;
1608 tv->vval.v_channel = iptr->isn_arg.channel;
1609 if (tv->vval.v_channel != NULL)
1610 ++tv->vval.v_channel->ch_refcount;
1611#endif
1612 break;
1613 case ISN_PUSHJOB:
1614#ifdef FEAT_JOB_CHANNEL
1615 tv->v_type = VAR_JOB;
1616 tv->vval.v_job = iptr->isn_arg.job;
1617 if (tv->vval.v_job != NULL)
1618 ++tv->vval.v_job->jv_refcount;
1619#endif
1620 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 default:
1622 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001623 tv->vval.v_string = vim_strsave(
1624 iptr->isn_arg.string == NULL
1625 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 }
1627 break;
1628
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001629 case ISN_UNLET:
1630 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1631 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001632 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001633 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001634 case ISN_UNLETENV:
1635 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1636 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001637
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001638 case ISN_LOCKCONST:
1639 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1640 break;
1641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 // create a list from items on the stack; uses a single allocation
1643 // for the list header and the items
1644 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001645 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1646 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 break;
1648
1649 // create a dict from items on the stack
1650 case ISN_NEWDICT:
1651 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001652 int count = iptr->isn_arg.number;
1653 dict_T *dict = dict_alloc();
1654 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655
1656 if (dict == NULL)
1657 goto failed;
1658 for (idx = 0; idx < count; ++idx)
1659 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001660 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001662 // check key is unique
1663 item = dict_find(dict, tv->vval.v_string, -1);
1664 if (item != NULL)
1665 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001666 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001667 semsg(_(e_duplicate_key), tv->vval.v_string);
1668 dict_unref(dict);
1669 goto on_error;
1670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 item = dictitem_alloc(tv->vval.v_string);
1672 clear_tv(tv);
1673 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001674 {
1675 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001677 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1679 item->di_tv.v_lock = 0;
1680 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001681 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001682 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001683 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 }
1687
1688 if (count > 0)
1689 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001690 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 goto failed;
1692 else
1693 ++ectx.ec_stack.ga_len;
1694 tv = STACK_TV_BOT(-1);
1695 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001696 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 tv->vval.v_dict = dict;
1698 ++dict->dv_refcount;
1699 }
1700 break;
1701
1702 // call a :def function
1703 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001704 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1706 iptr->isn_arg.dfunc.cdf_argcount,
1707 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001708 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 break;
1710
1711 // call a builtin function
1712 case ISN_BCALL:
1713 SOURCING_LNUM = iptr->isn_lnum;
1714 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1715 iptr->isn_arg.bfunc.cbf_argcount,
1716 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001717 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 break;
1719
1720 // call a funcref or partial
1721 case ISN_PCALL:
1722 {
1723 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1724 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001725 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726
1727 SOURCING_LNUM = iptr->isn_lnum;
1728 if (pfunc->cpf_top)
1729 {
1730 // funcref is above the arguments
1731 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1732 }
1733 else
1734 {
1735 // Get the funcref from the stack.
1736 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001737 partial_tv = *STACK_TV_BOT(0);
1738 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 }
1740 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001741 if (tv == &partial_tv)
1742 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001744 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 }
1746 break;
1747
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001748 case ISN_PCALL_END:
1749 // PCALL finished, arguments have been consumed and replaced by
1750 // the return value. Now clear the funcref from the stack,
1751 // and move the return value in its place.
1752 --ectx.ec_stack.ga_len;
1753 clear_tv(STACK_TV_BOT(-1));
1754 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1755 break;
1756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 // call a user defined function or funcref/partial
1758 case ISN_UCALL:
1759 {
1760 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1761
1762 SOURCING_LNUM = iptr->isn_lnum;
1763 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001764 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001765 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766 }
1767 break;
1768
1769 // return from a :def function call
1770 case ISN_RETURN:
1771 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001772 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001773 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001774
1775 if (trystack->ga_len > 0)
1776 trycmd = ((trycmd_T *)trystack->ga_data)
1777 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001778 if (trycmd != NULL
1779 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 && trycmd->tcd_finally_idx != 0)
1781 {
1782 // jump to ":finally"
1783 ectx.ec_iidx = trycmd->tcd_finally_idx;
1784 trycmd->tcd_return = TRUE;
1785 }
1786 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001787 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 }
1789 break;
1790
1791 // push a function reference to a compiled function
1792 case ISN_FUNCREF:
1793 {
1794 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001795 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796
1797 pt = ALLOC_CLEAR_ONE(partial_T);
1798 if (pt == NULL)
1799 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001800 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001801 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001802 vim_free(pt);
1803 goto failed;
1804 }
1805 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1806 + iptr->isn_arg.funcref.fr_func;
1807 pt->pt_func = pt_dfunc->df_ufunc;
1808 pt->pt_refcount = 1;
1809 ++pt_dfunc->df_ufunc->uf_refcount;
1810
1811 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1812 {
1813 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1814 + ectx.ec_dfunc_idx;
1815
1816 // The closure needs to find arguments and local
1817 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001818 pt->pt_ectx_stack = &ectx.ec_stack;
1819 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001820
1821 // If this function returns and the closure is still
1822 // used, we need to make a copy of the context
1823 // (arguments and local variables). Store a reference
1824 // to the partial so we can handle that.
1825 ++pt->pt_refcount;
1826 tv = STACK_TV_VAR(dfunc->df_varcount
1827 + iptr->isn_arg.funcref.fr_var_idx);
1828 if (tv->v_type == VAR_PARTIAL)
1829 {
1830 // TODO: use a garray_T on ectx.
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001831 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001832 emsg("Multiple closures not supported yet");
Bram Moolenaardec07512020-09-18 23:11:10 +02001833 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001834 goto failed;
1835 }
1836 tv->v_type = VAR_PARTIAL;
1837 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001838 }
1839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 tv = STACK_TV_BOT(0);
1841 ++ectx.ec_stack.ga_len;
1842 tv->vval.v_partial = pt;
1843 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001844 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 }
1846 break;
1847
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001848 // Create a global function from a lambda.
1849 case ISN_NEWFUNC:
1850 {
1851 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1852
1853 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1854 }
1855 break;
1856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 // jump if a condition is met
1858 case ISN_JUMP:
1859 {
1860 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1861 int jump = TRUE;
1862
1863 if (when != JUMP_ALWAYS)
1864 {
1865 tv = STACK_TV_BOT(-1);
1866 jump = tv2bool(tv);
1867 if (when == JUMP_IF_FALSE
1868 || when == JUMP_AND_KEEP_IF_FALSE)
1869 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001870 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 {
1872 // drop the value from the stack
1873 clear_tv(tv);
1874 --ectx.ec_stack.ga_len;
1875 }
1876 }
1877 if (jump)
1878 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1879 }
1880 break;
1881
1882 // top of a for loop
1883 case ISN_FOR:
1884 {
1885 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1886 typval_T *idxtv =
1887 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1888
1889 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001890 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001892 ++idxtv->vval.v_number;
1893 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 // past the end of the list, jump to "endfor"
1895 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1896 else if (list->lv_first == &range_list_item)
1897 {
1898 // non-materialized range() list
1899 tv = STACK_TV_BOT(0);
1900 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001901 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 tv->vval.v_number = list_find_nr(
1903 list, idxtv->vval.v_number, NULL);
1904 ++ectx.ec_stack.ga_len;
1905 }
1906 else
1907 {
1908 listitem_T *li = list_find(list, idxtv->vval.v_number);
1909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1911 ++ectx.ec_stack.ga_len;
1912 }
1913 }
1914 break;
1915
1916 // start of ":try" block
1917 case ISN_TRY:
1918 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001919 trycmd_T *trycmd = NULL;
1920
Bram Moolenaar270d0382020-05-15 21:42:53 +02001921 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 goto failed;
1923 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1924 + ectx.ec_trystack.ga_len;
1925 ++ectx.ec_trystack.ga_len;
1926 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001927 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1929 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001930 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02001931 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 }
1933 break;
1934
1935 case ISN_PUSHEXC:
1936 if (current_exception == NULL)
1937 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001938 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 iemsg("Evaluating catch while current_exception is NULL");
1940 goto failed;
1941 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001942 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 goto failed;
1944 tv = STACK_TV_BOT(0);
1945 ++ectx.ec_stack.ga_len;
1946 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001947 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 tv->vval.v_string = vim_strsave(
1949 (char_u *)current_exception->value);
1950 break;
1951
1952 case ISN_CATCH:
1953 {
1954 garray_T *trystack = &ectx.ec_trystack;
1955
1956 if (trystack->ga_len > 0)
1957 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001958 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 + trystack->ga_len - 1;
1960 trycmd->tcd_caught = TRUE;
1961 }
1962 did_emsg = got_int = did_throw = FALSE;
1963 catch_exception(current_exception);
1964 }
1965 break;
1966
1967 // end of ":try" block
1968 case ISN_ENDTRY:
1969 {
1970 garray_T *trystack = &ectx.ec_trystack;
1971
1972 if (trystack->ga_len > 0)
1973 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001974 trycmd_T *trycmd = NULL;
1975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 --trystack->ga_len;
1977 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001978 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 trycmd = ((trycmd_T *)trystack->ga_data)
1980 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001981 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 {
1983 // discard the exception
1984 if (caught_stack == current_exception)
1985 caught_stack = caught_stack->caught;
1986 discard_current_exception();
1987 }
1988
1989 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001990 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 }
1992 }
1993 break;
1994
1995 case ISN_THROW:
1996 --ectx.ec_stack.ga_len;
1997 tv = STACK_TV_BOT(0);
1998 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1999 {
2000 vim_free(tv->vval.v_string);
2001 goto failed;
2002 }
2003 did_throw = TRUE;
2004 break;
2005
2006 // compare with special values
2007 case ISN_COMPAREBOOL:
2008 case ISN_COMPARESPECIAL:
2009 {
2010 typval_T *tv1 = STACK_TV_BOT(-2);
2011 typval_T *tv2 = STACK_TV_BOT(-1);
2012 varnumber_T arg1 = tv1->vval.v_number;
2013 varnumber_T arg2 = tv2->vval.v_number;
2014 int res;
2015
2016 switch (iptr->isn_arg.op.op_type)
2017 {
2018 case EXPR_EQUAL: res = arg1 == arg2; break;
2019 case EXPR_NEQUAL: res = arg1 != arg2; break;
2020 default: res = 0; break;
2021 }
2022
2023 --ectx.ec_stack.ga_len;
2024 tv1->v_type = VAR_BOOL;
2025 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2026 }
2027 break;
2028
2029 // Operation with two number arguments
2030 case ISN_OPNR:
2031 case ISN_COMPARENR:
2032 {
2033 typval_T *tv1 = STACK_TV_BOT(-2);
2034 typval_T *tv2 = STACK_TV_BOT(-1);
2035 varnumber_T arg1 = tv1->vval.v_number;
2036 varnumber_T arg2 = tv2->vval.v_number;
2037 varnumber_T res;
2038
2039 switch (iptr->isn_arg.op.op_type)
2040 {
2041 case EXPR_MULT: res = arg1 * arg2; break;
2042 case EXPR_DIV: res = arg1 / arg2; break;
2043 case EXPR_REM: 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: res = arg1 == arg2; break;
2048 case EXPR_NEQUAL: res = arg1 != arg2; break;
2049 case EXPR_GREATER: res = arg1 > arg2; break;
2050 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2051 case EXPR_SMALLER: res = arg1 < arg2; break;
2052 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2053 default: res = 0; break;
2054 }
2055
2056 --ectx.ec_stack.ga_len;
2057 if (iptr->isn_type == ISN_COMPARENR)
2058 {
2059 tv1->v_type = VAR_BOOL;
2060 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2061 }
2062 else
2063 tv1->vval.v_number = res;
2064 }
2065 break;
2066
2067 // Computation with two float arguments
2068 case ISN_OPFLOAT:
2069 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002070#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 {
2072 typval_T *tv1 = STACK_TV_BOT(-2);
2073 typval_T *tv2 = STACK_TV_BOT(-1);
2074 float_T arg1 = tv1->vval.v_float;
2075 float_T arg2 = tv2->vval.v_float;
2076 float_T res = 0;
2077 int cmp = FALSE;
2078
2079 switch (iptr->isn_arg.op.op_type)
2080 {
2081 case EXPR_MULT: res = arg1 * arg2; break;
2082 case EXPR_DIV: res = arg1 / arg2; break;
2083 case EXPR_SUB: res = arg1 - arg2; break;
2084 case EXPR_ADD: res = arg1 + arg2; break;
2085
2086 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2087 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2088 case EXPR_GREATER: cmp = arg1 > arg2; break;
2089 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2090 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2091 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2092 default: cmp = 0; break;
2093 }
2094 --ectx.ec_stack.ga_len;
2095 if (iptr->isn_type == ISN_COMPAREFLOAT)
2096 {
2097 tv1->v_type = VAR_BOOL;
2098 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2099 }
2100 else
2101 tv1->vval.v_float = res;
2102 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002103#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 break;
2105
2106 case ISN_COMPARELIST:
2107 {
2108 typval_T *tv1 = STACK_TV_BOT(-2);
2109 typval_T *tv2 = STACK_TV_BOT(-1);
2110 list_T *arg1 = tv1->vval.v_list;
2111 list_T *arg2 = tv2->vval.v_list;
2112 int cmp = FALSE;
2113 int ic = iptr->isn_arg.op.op_ic;
2114
2115 switch (iptr->isn_arg.op.op_type)
2116 {
2117 case EXPR_EQUAL: cmp =
2118 list_equal(arg1, arg2, ic, FALSE); break;
2119 case EXPR_NEQUAL: cmp =
2120 !list_equal(arg1, arg2, ic, FALSE); break;
2121 case EXPR_IS: cmp = arg1 == arg2; break;
2122 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2123 default: cmp = 0; break;
2124 }
2125 --ectx.ec_stack.ga_len;
2126 clear_tv(tv1);
2127 clear_tv(tv2);
2128 tv1->v_type = VAR_BOOL;
2129 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2130 }
2131 break;
2132
2133 case ISN_COMPAREBLOB:
2134 {
2135 typval_T *tv1 = STACK_TV_BOT(-2);
2136 typval_T *tv2 = STACK_TV_BOT(-1);
2137 blob_T *arg1 = tv1->vval.v_blob;
2138 blob_T *arg2 = tv2->vval.v_blob;
2139 int cmp = FALSE;
2140
2141 switch (iptr->isn_arg.op.op_type)
2142 {
2143 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2144 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2145 case EXPR_IS: cmp = arg1 == arg2; break;
2146 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2147 default: cmp = 0; break;
2148 }
2149 --ectx.ec_stack.ga_len;
2150 clear_tv(tv1);
2151 clear_tv(tv2);
2152 tv1->v_type = VAR_BOOL;
2153 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2154 }
2155 break;
2156
2157 // TODO: handle separately
2158 case ISN_COMPARESTRING:
2159 case ISN_COMPAREDICT:
2160 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 case ISN_COMPAREANY:
2162 {
2163 typval_T *tv1 = STACK_TV_BOT(-2);
2164 typval_T *tv2 = STACK_TV_BOT(-1);
2165 exptype_T exptype = iptr->isn_arg.op.op_type;
2166 int ic = iptr->isn_arg.op.op_ic;
2167
Bram Moolenaareb26f432020-09-14 16:50:05 +02002168 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 typval_compare(tv1, tv2, exptype, ic);
2170 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 --ectx.ec_stack.ga_len;
2172 }
2173 break;
2174
2175 case ISN_ADDLIST:
2176 case ISN_ADDBLOB:
2177 {
2178 typval_T *tv1 = STACK_TV_BOT(-2);
2179 typval_T *tv2 = STACK_TV_BOT(-1);
2180
2181 if (iptr->isn_type == ISN_ADDLIST)
2182 eval_addlist(tv1, tv2);
2183 else
2184 eval_addblob(tv1, tv2);
2185 clear_tv(tv2);
2186 --ectx.ec_stack.ga_len;
2187 }
2188 break;
2189
2190 // Computation with two arguments of unknown type
2191 case ISN_OPANY:
2192 {
2193 typval_T *tv1 = STACK_TV_BOT(-2);
2194 typval_T *tv2 = STACK_TV_BOT(-1);
2195 varnumber_T n1, n2;
2196#ifdef FEAT_FLOAT
2197 float_T f1 = 0, f2 = 0;
2198#endif
2199 int error = FALSE;
2200
2201 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2202 {
2203 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2204 {
2205 eval_addlist(tv1, tv2);
2206 clear_tv(tv2);
2207 --ectx.ec_stack.ga_len;
2208 break;
2209 }
2210 else if (tv1->v_type == VAR_BLOB
2211 && tv2->v_type == VAR_BLOB)
2212 {
2213 eval_addblob(tv1, tv2);
2214 clear_tv(tv2);
2215 --ectx.ec_stack.ga_len;
2216 break;
2217 }
2218 }
2219#ifdef FEAT_FLOAT
2220 if (tv1->v_type == VAR_FLOAT)
2221 {
2222 f1 = tv1->vval.v_float;
2223 n1 = 0;
2224 }
2225 else
2226#endif
2227 {
2228 n1 = tv_get_number_chk(tv1, &error);
2229 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002230 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231#ifdef FEAT_FLOAT
2232 if (tv2->v_type == VAR_FLOAT)
2233 f1 = n1;
2234#endif
2235 }
2236#ifdef FEAT_FLOAT
2237 if (tv2->v_type == VAR_FLOAT)
2238 {
2239 f2 = tv2->vval.v_float;
2240 n2 = 0;
2241 }
2242 else
2243#endif
2244 {
2245 n2 = tv_get_number_chk(tv2, &error);
2246 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002247 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248#ifdef FEAT_FLOAT
2249 if (tv1->v_type == VAR_FLOAT)
2250 f2 = n2;
2251#endif
2252 }
2253#ifdef FEAT_FLOAT
2254 // if there is a float on either side the result is a float
2255 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2256 {
2257 switch (iptr->isn_arg.op.op_type)
2258 {
2259 case EXPR_MULT: f1 = f1 * f2; break;
2260 case EXPR_DIV: f1 = f1 / f2; break;
2261 case EXPR_SUB: f1 = f1 - f2; break;
2262 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002263 default: SOURCING_LNUM = iptr->isn_lnum;
2264 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002265 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 }
2267 clear_tv(tv1);
2268 clear_tv(tv2);
2269 tv1->v_type = VAR_FLOAT;
2270 tv1->vval.v_float = f1;
2271 --ectx.ec_stack.ga_len;
2272 }
2273 else
2274#endif
2275 {
2276 switch (iptr->isn_arg.op.op_type)
2277 {
2278 case EXPR_MULT: n1 = n1 * n2; break;
2279 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2280 case EXPR_SUB: n1 = n1 - n2; break;
2281 case EXPR_ADD: n1 = n1 + n2; break;
2282 default: n1 = num_modulus(n1, n2); break;
2283 }
2284 clear_tv(tv1);
2285 clear_tv(tv2);
2286 tv1->v_type = VAR_NUMBER;
2287 tv1->vval.v_number = n1;
2288 --ectx.ec_stack.ga_len;
2289 }
2290 }
2291 break;
2292
2293 case ISN_CONCAT:
2294 {
2295 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2296 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2297 char_u *res;
2298
2299 res = concat_str(str1, str2);
2300 clear_tv(STACK_TV_BOT(-2));
2301 clear_tv(STACK_TV_BOT(-1));
2302 --ectx.ec_stack.ga_len;
2303 STACK_TV_BOT(-1)->vval.v_string = res;
2304 }
2305 break;
2306
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002307 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002308 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002309 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002310 int is_slice = iptr->isn_type == ISN_STRSLICE;
2311 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002312 char_u *res;
2313
2314 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002315 // string slice: string is at stack-3, first index at
2316 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002317 if (is_slice)
2318 {
2319 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002320 n1 = tv->vval.v_number;
2321 }
2322
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002323 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002324 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002325
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002326 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002327 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002328 if (is_slice)
2329 // Slice: Select the characters from the string
2330 res = string_slice(tv->vval.v_string, n1, n2);
2331 else
2332 // Index: The resulting variable is a string of a
2333 // single character. If the index is too big or
2334 // negative the result is empty.
2335 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002336 vim_free(tv->vval.v_string);
2337 tv->vval.v_string = res;
2338 }
2339 break;
2340
2341 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002342 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 {
Bram Moolenaared591872020-08-15 22:14:53 +02002344 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002346 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347
2348 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002349 // list slice: list is at stack-3, indexes at stack-2 and
2350 // stack-1
2351 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 list = tv->vval.v_list;
2353
2354 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002355 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002357
2358 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 {
Bram Moolenaared591872020-08-15 22:14:53 +02002360 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002361 n1 = tv->vval.v_number;
2362 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 }
Bram Moolenaared591872020-08-15 22:14:53 +02002364
2365 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002366 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002367 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002368 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2369 == FAIL)
2370 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 }
2372 break;
2373
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002374 case ISN_ANYINDEX:
2375 case ISN_ANYSLICE:
2376 {
2377 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2378 typval_T *var1, *var2;
2379 int res;
2380
2381 // index: composite is at stack-2, index at stack-1
2382 // slice: composite is at stack-3, indexes at stack-2 and
2383 // stack-1
2384 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002385 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002386 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2387 goto on_error;
2388 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2389 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2390 res = eval_index_inner(tv, is_slice,
2391 var1, var2, NULL, -1, TRUE);
2392 clear_tv(var1);
2393 if (is_slice)
2394 clear_tv(var2);
2395 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2396 if (res == FAIL)
2397 goto on_error;
2398 }
2399 break;
2400
Bram Moolenaar9af78762020-06-16 11:34:42 +02002401 case ISN_SLICE:
2402 {
2403 list_T *list;
2404 int count = iptr->isn_arg.number;
2405
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002406 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002407 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002408 list = tv->vval.v_list;
2409
2410 // no error for short list, expect it to be checked earlier
2411 if (list != NULL && list->lv_len >= count)
2412 {
2413 list_T *newlist = list_slice(list,
2414 count, list->lv_len - 1);
2415
2416 if (newlist != NULL)
2417 {
2418 list_unref(list);
2419 tv->vval.v_list = newlist;
2420 ++newlist->lv_refcount;
2421 }
2422 }
2423 }
2424 break;
2425
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002426 case ISN_GETITEM:
2427 {
2428 listitem_T *li;
2429 int index = iptr->isn_arg.number;
2430
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002431 // Get list item: list is at stack-1, push item.
2432 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002433 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002434 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002435
2436 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2437 goto failed;
2438 ++ectx.ec_stack.ga_len;
2439 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2440 }
2441 break;
2442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 case ISN_MEMBER:
2444 {
2445 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002446 char_u *key;
2447 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002448 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002449
2450 // dict member: dict is at stack-2, key at stack-1
2451 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002452 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002453 dict = tv->vval.v_dict;
2454
2455 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002456 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002457 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002458
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002459 if ((di = dict_find(dict, key, -1)) == NULL)
2460 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002461 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002462 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002463 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002464 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002465 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002466 --ectx.ec_stack.ga_len;
2467 // Clear the dict after getting the item, to avoid that it
2468 // make the item invalid.
2469 tv = STACK_TV_BOT(-1);
2470 temp_tv = *tv;
2471 copy_tv(&di->di_tv, tv);
2472 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002473 }
2474 break;
2475
2476 // dict member with string key
2477 case ISN_STRINGMEMBER:
2478 {
2479 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002481 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482
2483 tv = STACK_TV_BOT(-1);
2484 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2485 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002486 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002488 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 }
2490 dict = tv->vval.v_dict;
2491
2492 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2493 == NULL)
2494 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002495 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002497 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002499 // Clear the dict after getting the item, to avoid that it
2500 // make the item invalid.
2501 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002503 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 }
2505 break;
2506
2507 case ISN_NEGATENR:
2508 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002509 if (tv->v_type != VAR_NUMBER
2510#ifdef FEAT_FLOAT
2511 && tv->v_type != VAR_FLOAT
2512#endif
2513 )
2514 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002515 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002516 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002517 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002518 }
2519#ifdef FEAT_FLOAT
2520 if (tv->v_type == VAR_FLOAT)
2521 tv->vval.v_float = -tv->vval.v_float;
2522 else
2523#endif
2524 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 break;
2526
2527 case ISN_CHECKNR:
2528 {
2529 int error = FALSE;
2530
2531 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002532 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002534 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 (void)tv_get_number_chk(tv, &error);
2536 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002537 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 }
2539 break;
2540
2541 case ISN_CHECKTYPE:
2542 {
2543 checktype_T *ct = &iptr->isn_arg.type;
2544
2545 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002546 SOURCING_LNUM = iptr->isn_lnum;
2547 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2548 goto on_error;
2549
2550 // number 0 is FALSE, number 1 is TRUE
2551 if (tv->v_type == VAR_NUMBER
2552 && ct->ct_type->tt_type == VAR_BOOL
2553 && (tv->vval.v_number == 0
2554 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002556 tv->v_type = VAR_BOOL;
2557 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002558 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 }
2560 }
2561 break;
2562
Bram Moolenaar9af78762020-06-16 11:34:42 +02002563 case ISN_CHECKLEN:
2564 {
2565 int min_len = iptr->isn_arg.checklen.cl_min_len;
2566 list_T *list = NULL;
2567
2568 tv = STACK_TV_BOT(-1);
2569 if (tv->v_type == VAR_LIST)
2570 list = tv->vval.v_list;
2571 if (list == NULL || list->lv_len < min_len
2572 || (list->lv_len > min_len
2573 && !iptr->isn_arg.checklen.cl_more_OK))
2574 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002575 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002576 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002577 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002578 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002579 }
2580 }
2581 break;
2582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 case ISN_2BOOL:
2584 {
2585 int n;
2586
2587 tv = STACK_TV_BOT(-1);
2588 n = tv2bool(tv);
2589 if (iptr->isn_arg.number) // invert
2590 n = !n;
2591 clear_tv(tv);
2592 tv->v_type = VAR_BOOL;
2593 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2594 }
2595 break;
2596
2597 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002598 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 {
2600 char_u *str;
2601
2602 tv = STACK_TV_BOT(iptr->isn_arg.number);
2603 if (tv->v_type != VAR_STRING)
2604 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002605 if (iptr->isn_type == ISN_2STRING_ANY)
2606 {
2607 switch (tv->v_type)
2608 {
2609 case VAR_SPECIAL:
2610 case VAR_BOOL:
2611 case VAR_NUMBER:
2612 case VAR_FLOAT:
2613 case VAR_BLOB: break;
2614 default: to_string_error(tv->v_type);
2615 goto on_error;
2616 }
2617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 str = typval_tostring(tv);
2619 clear_tv(tv);
2620 tv->v_type = VAR_STRING;
2621 tv->vval.v_string = str;
2622 }
2623 }
2624 break;
2625
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002626 case ISN_PUT:
2627 {
2628 int regname = iptr->isn_arg.put.put_regname;
2629 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2630 char_u *expr = NULL;
2631 int dir = FORWARD;
2632
2633 if (regname == '=')
2634 {
2635 tv = STACK_TV_BOT(-1);
2636 if (tv->v_type == VAR_STRING)
2637 expr = tv->vval.v_string;
2638 else
2639 {
2640 expr = typval_tostring(tv); // allocates value
2641 clear_tv(tv);
2642 }
2643 --ectx.ec_stack.ga_len;
2644 }
2645 if (lnum == -2)
2646 // :put! above cursor
2647 dir = BACKWARD;
2648 else if (lnum >= 0)
2649 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2650 check_cursor();
2651 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2652 vim_free(expr);
2653 }
2654 break;
2655
Bram Moolenaar389df252020-07-09 21:20:47 +02002656 case ISN_SHUFFLE:
2657 {
2658 typval_T tmp_tv;
2659 int item = iptr->isn_arg.shuffle.shfl_item;
2660 int up = iptr->isn_arg.shuffle.shfl_up;
2661
2662 tmp_tv = *STACK_TV_BOT(-item);
2663 for ( ; up > 0 && item > 1; --up)
2664 {
2665 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2666 --item;
2667 }
2668 *STACK_TV_BOT(-item) = tmp_tv;
2669 }
2670 break;
2671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 case ISN_DROP:
2673 --ectx.ec_stack.ga_len;
2674 clear_tv(STACK_TV_BOT(0));
2675 break;
2676 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002677 continue;
2678
Bram Moolenaard032f342020-07-18 18:13:02 +02002679func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002680 // Restore previous function. If the frame pointer is where we started
2681 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02002682 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02002683 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002684
Bram Moolenaard032f342020-07-18 18:13:02 +02002685 if (func_return(&ectx) == FAIL)
2686 // only fails when out of memory
2687 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002688 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002689
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002690on_error:
2691 if (trylevel == 0)
2692 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 }
2694
2695done:
2696 // function finished, get result from the stack.
2697 tv = STACK_TV_BOT(-1);
2698 *rettv = *tv;
2699 tv->v_type = VAR_UNKNOWN;
2700 ret = OK;
2701
2702failed:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002703 // Also deal with closures when failed, they may already be in use
2704 // somewhere.
2705 handle_closure_in_use(&ectx, FALSE);
2706
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002707 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002708 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002709 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002710
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002711 estack_pop();
2712 current_sctx = save_current_sctx;
2713
2714failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002715 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2717 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002720 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002721
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002722 // Not sure if this is necessary.
2723 suppress_errthrow = save_suppress_errthrow;
2724
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002725 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002726 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002727 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 return ret;
2729}
2730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731/*
2732 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002733 * We don't really need this at runtime, but we do have tests that require it,
2734 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 */
2736 void
2737ex_disassemble(exarg_T *eap)
2738{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002739 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002740 char_u *fname;
2741 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 dfunc_T *dfunc;
2743 isn_T *instr;
2744 int current;
2745 int line_idx = 0;
2746 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002747 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002749 if (STRNCMP(arg, "<lambda>", 8) == 0)
2750 {
2751 arg += 8;
2752 (void)getdigits(&arg);
2753 fname = vim_strnsave(eap->arg, arg - eap->arg);
2754 }
2755 else
2756 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002757 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002758 if (fname == NULL)
2759 {
2760 semsg(_(e_invarg2), eap->arg);
2761 return;
2762 }
2763
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002764 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002765 if (ufunc == NULL)
2766 {
2767 char_u *p = untrans_function_name(fname);
2768
2769 if (p != NULL)
2770 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002771 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002772 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002773 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 if (ufunc == NULL)
2775 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002776 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 return;
2778 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002779 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002780 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2781 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002782 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002784 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 return;
2786 }
2787 if (ufunc->uf_name_exp != NULL)
2788 msg((char *)ufunc->uf_name_exp);
2789 else
2790 msg((char *)ufunc->uf_name);
2791
2792 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2793 instr = dfunc->df_instr;
2794 for (current = 0; current < dfunc->df_instr_count; ++current)
2795 {
2796 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002797 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798
2799 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2800 {
2801 if (current > prev_current)
2802 {
2803 msg_puts("\n\n");
2804 prev_current = current;
2805 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002806 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2807 if (line != NULL)
2808 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 }
2810
2811 switch (iptr->isn_type)
2812 {
2813 case ISN_EXEC:
2814 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2815 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002816 case ISN_EXECCONCAT:
2817 smsg("%4d EXECCONCAT %lld", current,
2818 (long long)iptr->isn_arg.number);
2819 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 case ISN_ECHO:
2821 {
2822 echo_T *echo = &iptr->isn_arg.echo;
2823
2824 smsg("%4d %s %d", current,
2825 echo->echo_with_white ? "ECHO" : "ECHON",
2826 echo->echo_count);
2827 }
2828 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002829 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002830 smsg("%4d EXECUTE %lld", current,
2831 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002832 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002833 case ISN_ECHOMSG:
2834 smsg("%4d ECHOMSG %lld", current,
2835 (long long)(iptr->isn_arg.number));
2836 break;
2837 case ISN_ECHOERR:
2838 smsg("%4d ECHOERR %lld", current,
2839 (long long)(iptr->isn_arg.number));
2840 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002842 case ISN_LOADOUTER:
2843 {
2844 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2845
2846 if (iptr->isn_arg.number < 0)
2847 smsg("%4d LOAD%s arg[%lld]", current, add,
2848 (long long)(iptr->isn_arg.number
2849 + STACK_FRAME_SIZE));
2850 else
2851 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002852 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 break;
2855 case ISN_LOADV:
2856 smsg("%4d LOADV v:%s", current,
2857 get_vim_var_name(iptr->isn_arg.number));
2858 break;
2859 case ISN_LOADSCRIPT:
2860 {
2861 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002862 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2864 + iptr->isn_arg.script.script_idx;
2865
2866 smsg("%4d LOADSCRIPT %s from %s", current,
2867 sv->sv_name, si->sn_name);
2868 }
2869 break;
2870 case ISN_LOADS:
2871 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002872 scriptitem_T *si = SCRIPT_ITEM(
2873 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874
2875 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002876 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 }
2878 break;
2879 case ISN_LOADG:
2880 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2881 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002882 case ISN_LOADB:
2883 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2884 break;
2885 case ISN_LOADW:
2886 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2887 break;
2888 case ISN_LOADT:
2889 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2890 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002891 case ISN_LOADGDICT:
2892 smsg("%4d LOAD g:", current);
2893 break;
2894 case ISN_LOADBDICT:
2895 smsg("%4d LOAD b:", current);
2896 break;
2897 case ISN_LOADWDICT:
2898 smsg("%4d LOAD w:", current);
2899 break;
2900 case ISN_LOADTDICT:
2901 smsg("%4d LOAD t:", current);
2902 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 case ISN_LOADOPT:
2904 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2905 break;
2906 case ISN_LOADENV:
2907 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2908 break;
2909 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002910 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 break;
2912
2913 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002914 case ISN_STOREOUTER:
2915 {
2916 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2917
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002918 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002919 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002920 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002921 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002922 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002923 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002926 case ISN_STOREV:
2927 smsg("%4d STOREV v:%s", current,
2928 get_vim_var_name(iptr->isn_arg.number));
2929 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002931 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2932 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002933 case ISN_STOREB:
2934 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2935 break;
2936 case ISN_STOREW:
2937 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2938 break;
2939 case ISN_STORET:
2940 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2941 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002942 case ISN_STORES:
2943 {
2944 scriptitem_T *si = SCRIPT_ITEM(
2945 iptr->isn_arg.loadstore.ls_sid);
2946
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002947 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002948 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002949 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 break;
2951 case ISN_STORESCRIPT:
2952 {
2953 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002954 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2956 + iptr->isn_arg.script.script_idx;
2957
2958 smsg("%4d STORESCRIPT %s in %s", current,
2959 sv->sv_name, si->sn_name);
2960 }
2961 break;
2962 case ISN_STOREOPT:
2963 smsg("%4d STOREOPT &%s", current,
2964 iptr->isn_arg.storeopt.so_name);
2965 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002966 case ISN_STOREENV:
2967 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2968 break;
2969 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002970 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002971 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 case ISN_STORENR:
2973 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002974 iptr->isn_arg.storenr.stnr_val,
2975 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 break;
2977
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002978 case ISN_STORELIST:
2979 smsg("%4d STORELIST", current);
2980 break;
2981
2982 case ISN_STOREDICT:
2983 smsg("%4d STOREDICT", current);
2984 break;
2985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 // constants
2987 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002988 smsg("%4d PUSHNR %lld", current,
2989 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 break;
2991 case ISN_PUSHBOOL:
2992 case ISN_PUSHSPEC:
2993 smsg("%4d PUSH %s", current,
2994 get_var_special_name(iptr->isn_arg.number));
2995 break;
2996 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002997#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002999#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 break;
3001 case ISN_PUSHS:
3002 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3003 break;
3004 case ISN_PUSHBLOB:
3005 {
3006 char_u *r;
3007 char_u numbuf[NUMBUFLEN];
3008 char_u *tofree;
3009
3010 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003011 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 vim_free(tofree);
3013 }
3014 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003015 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003016 {
3017 char *name = (char *)iptr->isn_arg.string;
3018
3019 smsg("%4d PUSHFUNC \"%s\"", current,
3020 name == NULL ? "[none]" : name);
3021 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003022 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003023 case ISN_PUSHCHANNEL:
3024#ifdef FEAT_JOB_CHANNEL
3025 {
3026 channel_T *channel = iptr->isn_arg.channel;
3027
3028 smsg("%4d PUSHCHANNEL %d", current,
3029 channel == NULL ? 0 : channel->ch_id);
3030 }
3031#endif
3032 break;
3033 case ISN_PUSHJOB:
3034#ifdef FEAT_JOB_CHANNEL
3035 {
3036 typval_T tv;
3037 char_u *name;
3038
3039 tv.v_type = VAR_JOB;
3040 tv.vval.v_job = iptr->isn_arg.job;
3041 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003042 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003043 }
3044#endif
3045 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 case ISN_PUSHEXC:
3047 smsg("%4d PUSH v:exception", current);
3048 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003049 case ISN_UNLET:
3050 smsg("%4d UNLET%s %s", current,
3051 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3052 iptr->isn_arg.unlet.ul_name);
3053 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003054 case ISN_UNLETENV:
3055 smsg("%4d UNLETENV%s $%s", current,
3056 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3057 iptr->isn_arg.unlet.ul_name);
3058 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003059 case ISN_LOCKCONST:
3060 smsg("%4d LOCKCONST", current);
3061 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003063 smsg("%4d NEWLIST size %lld", current,
3064 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 break;
3066 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003067 smsg("%4d NEWDICT size %lld", current,
3068 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 break;
3070
3071 // function call
3072 case ISN_BCALL:
3073 {
3074 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3075
3076 smsg("%4d BCALL %s(argc %d)", current,
3077 internal_func_name(cbfunc->cbf_idx),
3078 cbfunc->cbf_argcount);
3079 }
3080 break;
3081 case ISN_DCALL:
3082 {
3083 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3084 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3085 + cdfunc->cdf_idx;
3086
3087 smsg("%4d DCALL %s(argc %d)", current,
3088 df->df_ufunc->uf_name_exp != NULL
3089 ? df->df_ufunc->uf_name_exp
3090 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3091 }
3092 break;
3093 case ISN_UCALL:
3094 {
3095 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3096
3097 smsg("%4d UCALL %s(argc %d)", current,
3098 cufunc->cuf_name, cufunc->cuf_argcount);
3099 }
3100 break;
3101 case ISN_PCALL:
3102 {
3103 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3104
3105 smsg("%4d PCALL%s (argc %d)", current,
3106 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3107 }
3108 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003109 case ISN_PCALL_END:
3110 smsg("%4d PCALL end", current);
3111 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 case ISN_RETURN:
3113 smsg("%4d RETURN", current);
3114 break;
3115 case ISN_FUNCREF:
3116 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003117 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003119 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003121 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003122 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 }
3124 break;
3125
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003126 case ISN_NEWFUNC:
3127 {
3128 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3129
3130 smsg("%4d NEWFUNC %s %s", current,
3131 newfunc->nf_lambda, newfunc->nf_global);
3132 }
3133 break;
3134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 case ISN_JUMP:
3136 {
3137 char *when = "?";
3138
3139 switch (iptr->isn_arg.jump.jump_when)
3140 {
3141 case JUMP_ALWAYS:
3142 when = "JUMP";
3143 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 case JUMP_AND_KEEP_IF_TRUE:
3145 when = "JUMP_AND_KEEP_IF_TRUE";
3146 break;
3147 case JUMP_IF_FALSE:
3148 when = "JUMP_IF_FALSE";
3149 break;
3150 case JUMP_AND_KEEP_IF_FALSE:
3151 when = "JUMP_AND_KEEP_IF_FALSE";
3152 break;
3153 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003154 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 iptr->isn_arg.jump.jump_where);
3156 }
3157 break;
3158
3159 case ISN_FOR:
3160 {
3161 forloop_T *forloop = &iptr->isn_arg.forloop;
3162
3163 smsg("%4d FOR $%d -> %d", current,
3164 forloop->for_idx, forloop->for_end);
3165 }
3166 break;
3167
3168 case ISN_TRY:
3169 {
3170 try_T *try = &iptr->isn_arg.try;
3171
3172 smsg("%4d TRY catch -> %d, finally -> %d", current,
3173 try->try_catch, try->try_finally);
3174 }
3175 break;
3176 case ISN_CATCH:
3177 // TODO
3178 smsg("%4d CATCH", current);
3179 break;
3180 case ISN_ENDTRY:
3181 smsg("%4d ENDTRY", current);
3182 break;
3183 case ISN_THROW:
3184 smsg("%4d THROW", current);
3185 break;
3186
3187 // expression operations on number
3188 case ISN_OPNR:
3189 case ISN_OPFLOAT:
3190 case ISN_OPANY:
3191 {
3192 char *what;
3193 char *ins;
3194
3195 switch (iptr->isn_arg.op.op_type)
3196 {
3197 case EXPR_MULT: what = "*"; break;
3198 case EXPR_DIV: what = "/"; break;
3199 case EXPR_REM: what = "%"; break;
3200 case EXPR_SUB: what = "-"; break;
3201 case EXPR_ADD: what = "+"; break;
3202 default: what = "???"; break;
3203 }
3204 switch (iptr->isn_type)
3205 {
3206 case ISN_OPNR: ins = "OPNR"; break;
3207 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3208 case ISN_OPANY: ins = "OPANY"; break;
3209 default: ins = "???"; break;
3210 }
3211 smsg("%4d %s %s", current, ins, what);
3212 }
3213 break;
3214
3215 case ISN_COMPAREBOOL:
3216 case ISN_COMPARESPECIAL:
3217 case ISN_COMPARENR:
3218 case ISN_COMPAREFLOAT:
3219 case ISN_COMPARESTRING:
3220 case ISN_COMPAREBLOB:
3221 case ISN_COMPARELIST:
3222 case ISN_COMPAREDICT:
3223 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 case ISN_COMPAREANY:
3225 {
3226 char *p;
3227 char buf[10];
3228 char *type;
3229
3230 switch (iptr->isn_arg.op.op_type)
3231 {
3232 case EXPR_EQUAL: p = "=="; break;
3233 case EXPR_NEQUAL: p = "!="; break;
3234 case EXPR_GREATER: p = ">"; break;
3235 case EXPR_GEQUAL: p = ">="; break;
3236 case EXPR_SMALLER: p = "<"; break;
3237 case EXPR_SEQUAL: p = "<="; break;
3238 case EXPR_MATCH: p = "=~"; break;
3239 case EXPR_IS: p = "is"; break;
3240 case EXPR_ISNOT: p = "isnot"; break;
3241 case EXPR_NOMATCH: p = "!~"; break;
3242 default: p = "???"; break;
3243 }
3244 STRCPY(buf, p);
3245 if (iptr->isn_arg.op.op_ic == TRUE)
3246 strcat(buf, "?");
3247 switch(iptr->isn_type)
3248 {
3249 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3250 case ISN_COMPARESPECIAL:
3251 type = "COMPARESPECIAL"; break;
3252 case ISN_COMPARENR: type = "COMPARENR"; break;
3253 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3254 case ISN_COMPARESTRING:
3255 type = "COMPARESTRING"; break;
3256 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3257 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3258 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3259 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3261 default: type = "???"; break;
3262 }
3263
3264 smsg("%4d %s %s", current, type, buf);
3265 }
3266 break;
3267
3268 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3269 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3270
3271 // expression operations
3272 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003273 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003274 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003275 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003276 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003277 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3278 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003279 case ISN_SLICE: smsg("%4d SLICE %lld",
3280 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003281 case ISN_GETITEM: smsg("%4d ITEM %lld",
3282 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003283 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3284 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 iptr->isn_arg.string); break;
3286 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3287
3288 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003289 case ISN_CHECKTYPE:
3290 {
3291 char *tofree;
3292
3293 smsg("%4d CHECKTYPE %s stack[%d]", current,
3294 type_name(iptr->isn_arg.type.ct_type, &tofree),
3295 iptr->isn_arg.type.ct_off);
3296 vim_free(tofree);
3297 break;
3298 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003299 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3300 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3301 iptr->isn_arg.checklen.cl_min_len);
3302 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 case ISN_2BOOL: if (iptr->isn_arg.number)
3304 smsg("%4d INVERT (!val)", current);
3305 else
3306 smsg("%4d 2BOOL (!!val)", current);
3307 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003308 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3309 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003310 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003311 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3312 (long long)(iptr->isn_arg.number));
3313 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003314 case ISN_PUT:
3315 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3316 (long)iptr->isn_arg.put.put_lnum);
3317 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318
Bram Moolenaar389df252020-07-09 21:20:47 +02003319 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3320 iptr->isn_arg.shuffle.shfl_item,
3321 iptr->isn_arg.shuffle.shfl_up);
3322 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 case ISN_DROP: smsg("%4d DROP", current); break;
3324 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003325
3326 out_flush(); // output one line at a time
3327 ui_breakcheck();
3328 if (got_int)
3329 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331}
3332
3333/*
3334 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3335 * list, etc. Mostly like what JavaScript does, except that empty list and
3336 * empty dictionary are FALSE.
3337 */
3338 int
3339tv2bool(typval_T *tv)
3340{
3341 switch (tv->v_type)
3342 {
3343 case VAR_NUMBER:
3344 return tv->vval.v_number != 0;
3345 case VAR_FLOAT:
3346#ifdef FEAT_FLOAT
3347 return tv->vval.v_float != 0.0;
3348#else
3349 break;
3350#endif
3351 case VAR_PARTIAL:
3352 return tv->vval.v_partial != NULL;
3353 case VAR_FUNC:
3354 case VAR_STRING:
3355 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3356 case VAR_LIST:
3357 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3358 case VAR_DICT:
3359 return tv->vval.v_dict != NULL
3360 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3361 case VAR_BOOL:
3362 case VAR_SPECIAL:
3363 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3364 case VAR_JOB:
3365#ifdef FEAT_JOB_CHANNEL
3366 return tv->vval.v_job != NULL;
3367#else
3368 break;
3369#endif
3370 case VAR_CHANNEL:
3371#ifdef FEAT_JOB_CHANNEL
3372 return tv->vval.v_channel != NULL;
3373#else
3374 break;
3375#endif
3376 case VAR_BLOB:
3377 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3378 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003379 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 case VAR_VOID:
3381 break;
3382 }
3383 return FALSE;
3384}
3385
3386/*
3387 * If "tv" is a string give an error and return FAIL.
3388 */
3389 int
3390check_not_string(typval_T *tv)
3391{
3392 if (tv->v_type == VAR_STRING)
3393 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003394 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 clear_tv(tv);
3396 return FAIL;
3397 }
3398 return OK;
3399}
3400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401
3402#endif // FEAT_EVAL