blob: ddc20bce8f218774ba6deb0afd2883675d2b7aeb [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100764
765// Get pointer to item in the stack.
766#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
767
768// Get pointer to item at the bottom of the stack, -1 is the bottom.
769#undef STACK_TV_BOT
770#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
771
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200772// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200773#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 +0100774
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200775// Like STACK_TV_VAR but use the outer scope
776#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
777
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200778 if (ufunc->uf_def_status == UF_NOT_COMPILED
779 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200780 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200781 {
782 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200783 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200784 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200785 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200786 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200787
Bram Moolenaar09689a02020-05-09 22:50:08 +0200788 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200789 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200790 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
791 + ufunc->uf_dfunc_idx;
792 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200793 {
794 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200795 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200796 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200799 CLEAR_FIELD(ectx);
800 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
801 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
802 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
803 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
805
806 // Put arguments on the stack.
807 for (idx = 0; idx < argc; ++idx)
808 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200809 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200810 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
811 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200812 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 copy_tv(&argv[idx], STACK_TV_BOT(0));
814 ++ectx.ec_stack.ga_len;
815 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200816
817 // Turn varargs into a list. Empty list if no args.
818 if (ufunc->uf_va_name != NULL)
819 {
820 int vararg_count = argc - ufunc->uf_args.ga_len;
821
822 if (vararg_count < 0)
823 vararg_count = 0;
824 else
825 argc -= vararg_count;
826 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200827 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200828
829 // Check the type of the list items.
830 tv = STACK_TV_BOT(-1);
831 if (ufunc->uf_va_type != NULL
832 && ufunc->uf_va_type->tt_member != &t_any
833 && tv->vval.v_list != NULL)
834 {
835 type_T *expected = ufunc->uf_va_type->tt_member;
836 listitem_T *li = tv->vval.v_list->lv_first;
837
838 for (idx = 0; idx < vararg_count; ++idx)
839 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200840 if (check_typval_type(expected, &li->li_tv,
841 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200842 goto failed_early;
843 li = li->li_next;
844 }
845 }
846
Bram Moolenaar23e03252020-04-12 22:22:31 +0200847 if (defcount > 0)
848 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200849 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200850 --ectx.ec_stack.ga_len;
851 }
852
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100853 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200854 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100855 if (defcount > 0)
856 for (idx = 0; idx < defcount; ++idx)
857 {
858 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
859 ++ectx.ec_stack.ga_len;
860 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200861 if (ufunc->uf_va_name != NULL)
862 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863
864 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200865 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
866 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200868 if (partial != NULL)
869 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200870 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
871 {
872 // TODO: is this always the right way?
873 ectx.ec_outer_stack = &current_ectx->ec_stack;
874 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
875 }
876 else
877 {
878 ectx.ec_outer_stack = partial->pt_ectx_stack;
879 ectx.ec_outer_frame = partial->pt_ectx_frame;
880 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200881 }
882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 // dummy frame entries
884 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
885 {
886 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
887 ++ectx.ec_stack.ga_len;
888 }
889
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200890 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200891 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200892 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
893 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200894 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200896 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200897 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200898 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200899
900 ectx.ec_instr = dfunc->df_instr;
901 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100902
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200903 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200904 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200905 estack_push_ufunc(ufunc, 1);
906 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200907 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
908
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100909 // Decide where to start execution, handles optional arguments.
910 init_instr_idx(ufunc, argc, &ectx);
911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912 for (;;)
913 {
914 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100915
Bram Moolenaar270d0382020-05-15 21:42:53 +0200916 if (++breakcheck_count >= 100)
917 {
918 line_breakcheck();
919 breakcheck_count = 0;
920 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100921 if (got_int)
922 {
923 // Turn CTRL-C into an exception.
924 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100925 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100926 goto failed;
927 did_throw = TRUE;
928 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929
Bram Moolenaara26b9702020-04-18 19:53:28 +0200930 if (did_emsg && msg_list != NULL && *msg_list != NULL)
931 {
932 // Turn an error message into an exception.
933 did_emsg = FALSE;
934 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
935 goto failed;
936 did_throw = TRUE;
937 *msg_list = NULL;
938 }
939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 if (did_throw && !ectx.ec_in_catch)
941 {
942 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100943 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944
945 // An exception jumps to the first catch, finally, or returns from
946 // the current function.
947 if (trystack->ga_len > 0)
948 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200949 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 {
951 // jump to ":catch" or ":finally"
952 ectx.ec_in_catch = TRUE;
953 ectx.ec_iidx = trycmd->tcd_catch_idx;
954 }
955 else
956 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200957 // Not inside try or need to return from current functions.
958 // Push a dummy return value.
959 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
960 goto failed;
961 tv = STACK_TV_BOT(0);
962 tv->v_type = VAR_NUMBER;
963 tv->vval.v_number = 0;
964 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200965 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200967 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100968 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200969 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
970 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 goto done;
972 }
973
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200974 if (func_return(&ectx) == FAIL)
975 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 }
977 continue;
978 }
979
980 iptr = &ectx.ec_instr[ectx.ec_iidx++];
981 switch (iptr->isn_type)
982 {
983 // execute Ex command line
984 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200985 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 do_cmdline_cmd(iptr->isn_arg.string);
987 break;
988
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200989 // execute Ex command from pieces on the stack
990 case ISN_EXECCONCAT:
991 {
992 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200993 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200994 int pass;
995 int i;
996 char_u *cmd = NULL;
997 char_u *str;
998
999 for (pass = 1; pass <= 2; ++pass)
1000 {
1001 for (i = 0; i < count; ++i)
1002 {
1003 tv = STACK_TV_BOT(i - count);
1004 str = tv->vval.v_string;
1005 if (str != NULL && *str != NUL)
1006 {
1007 if (pass == 2)
1008 STRCPY(cmd + len, str);
1009 len += STRLEN(str);
1010 }
1011 if (pass == 2)
1012 clear_tv(tv);
1013 }
1014 if (pass == 1)
1015 {
1016 cmd = alloc(len + 1);
1017 if (cmd == NULL)
1018 goto failed;
1019 len = 0;
1020 }
1021 }
1022
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001023 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001024 do_cmdline_cmd(cmd);
1025 vim_free(cmd);
1026 }
1027 break;
1028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 // execute :echo {string} ...
1030 case ISN_ECHO:
1031 {
1032 int count = iptr->isn_arg.echo.echo_count;
1033 int atstart = TRUE;
1034 int needclr = TRUE;
1035
1036 for (idx = 0; idx < count; ++idx)
1037 {
1038 tv = STACK_TV_BOT(idx - count);
1039 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1040 &atstart, &needclr);
1041 clear_tv(tv);
1042 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001043 if (needclr)
1044 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045 ectx.ec_stack.ga_len -= count;
1046 }
1047 break;
1048
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001049 // :execute {string} ...
1050 // :echomsg {string} ...
1051 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001052 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001053 case ISN_ECHOMSG:
1054 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001055 {
1056 int count = iptr->isn_arg.number;
1057 garray_T ga;
1058 char_u buf[NUMBUFLEN];
1059 char_u *p;
1060 int len;
1061 int failed = FALSE;
1062
1063 ga_init2(&ga, 1, 80);
1064 for (idx = 0; idx < count; ++idx)
1065 {
1066 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001067 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001068 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001069 if (tv->v_type == VAR_CHANNEL
1070 || tv->v_type == VAR_JOB)
1071 {
1072 SOURCING_LNUM = iptr->isn_lnum;
1073 emsg(_(e_inval_string));
1074 break;
1075 }
1076 else
1077 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001078 }
1079 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001080 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001081
1082 len = (int)STRLEN(p);
1083 if (ga_grow(&ga, len + 2) == FAIL)
1084 failed = TRUE;
1085 else
1086 {
1087 if (ga.ga_len > 0)
1088 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1089 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1090 ga.ga_len += len;
1091 }
1092 clear_tv(tv);
1093 }
1094 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001095 if (failed)
1096 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001097
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001098 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001099 {
1100 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001101 {
1102 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001103 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001104 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001105 else
1106 {
1107 msg_sb_eol();
1108 if (iptr->isn_type == ISN_ECHOMSG)
1109 {
1110 msg_attr(ga.ga_data, echo_attr);
1111 out_flush();
1112 }
1113 else
1114 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001115 SOURCING_LNUM = iptr->isn_lnum;
1116 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001117 }
1118 }
1119 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001120 ga_clear(&ga);
1121 }
1122 break;
1123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124 // load local variable or argument
1125 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001126 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 goto failed;
1128 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1129 ++ectx.ec_stack.ga_len;
1130 break;
1131
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001132 // load variable or argument from outer scope
1133 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001134 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001135 goto failed;
1136 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1137 STACK_TV_BOT(0));
1138 ++ectx.ec_stack.ga_len;
1139 break;
1140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 // load v: variable
1142 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001143 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 goto failed;
1145 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1146 ++ectx.ec_stack.ga_len;
1147 break;
1148
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001149 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 case ISN_LOADSCRIPT:
1151 {
1152 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001153 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 svar_T *sv;
1155
1156 sv = ((svar_T *)si->sn_var_vals.ga_data)
1157 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001158 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159 goto failed;
1160 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1161 ++ectx.ec_stack.ga_len;
1162 }
1163 break;
1164
1165 // load s: variable in old script
1166 case ISN_LOADS:
1167 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001168 hashtab_T *ht = &SCRIPT_VARS(
1169 iptr->isn_arg.loadstore.ls_sid);
1170 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001172
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 if (di == NULL)
1174 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001175 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001176 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001177 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 }
1179 else
1180 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001181 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 goto failed;
1183 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1184 ++ectx.ec_stack.ga_len;
1185 }
1186 }
1187 break;
1188
Bram Moolenaard3aac292020-04-19 14:32:17 +02001189 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001191 case ISN_LOADB:
1192 case ISN_LOADW:
1193 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001195 dictitem_T *di = NULL;
1196 hashtab_T *ht = NULL;
1197 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001198
Bram Moolenaard3aac292020-04-19 14:32:17 +02001199 switch (iptr->isn_type)
1200 {
1201 case ISN_LOADG:
1202 ht = get_globvar_ht();
1203 namespace = 'g';
1204 break;
1205 case ISN_LOADB:
1206 ht = &curbuf->b_vars->dv_hashtab;
1207 namespace = 'b';
1208 break;
1209 case ISN_LOADW:
1210 ht = &curwin->w_vars->dv_hashtab;
1211 namespace = 'w';
1212 break;
1213 case ISN_LOADT:
1214 ht = &curtab->tp_vars->dv_hashtab;
1215 namespace = 't';
1216 break;
1217 default: // Cannot reach here
1218 goto failed;
1219 }
1220 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 if (di == NULL)
1223 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001224 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001225 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001226 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001227 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228 }
1229 else
1230 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001231 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232 goto failed;
1233 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1234 ++ectx.ec_stack.ga_len;
1235 }
1236 }
1237 break;
1238
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001239 // load g:/b:/w:/t: namespace
1240 case ISN_LOADGDICT:
1241 case ISN_LOADBDICT:
1242 case ISN_LOADWDICT:
1243 case ISN_LOADTDICT:
1244 {
1245 dict_T *d = NULL;
1246
1247 switch (iptr->isn_type)
1248 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001249 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1250 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1251 case ISN_LOADWDICT: d = curwin->w_vars; break;
1252 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001253 default: // Cannot reach here
1254 goto failed;
1255 }
1256 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1257 goto failed;
1258 tv = STACK_TV_BOT(0);
1259 tv->v_type = VAR_DICT;
1260 tv->v_lock = 0;
1261 tv->vval.v_dict = d;
1262 ++ectx.ec_stack.ga_len;
1263 }
1264 break;
1265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 // load &option
1267 case ISN_LOADOPT:
1268 {
1269 typval_T optval;
1270 char_u *name = iptr->isn_arg.string;
1271
Bram Moolenaara8c17702020-04-01 21:17:24 +02001272 // This is not expected to fail, name is checked during
1273 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001274 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001276 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001277 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 *STACK_TV_BOT(0) = optval;
1279 ++ectx.ec_stack.ga_len;
1280 }
1281 break;
1282
1283 // load $ENV
1284 case ISN_LOADENV:
1285 {
1286 typval_T optval;
1287 char_u *name = iptr->isn_arg.string;
1288
Bram Moolenaar270d0382020-05-15 21:42:53 +02001289 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001291 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001292 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 *STACK_TV_BOT(0) = optval;
1294 ++ectx.ec_stack.ga_len;
1295 }
1296 break;
1297
1298 // load @register
1299 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001300 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 goto failed;
1302 tv = STACK_TV_BOT(0);
1303 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001304 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 tv->vval.v_string = get_reg_contents(
1306 iptr->isn_arg.number, GREG_EXPR_SRC);
1307 ++ectx.ec_stack.ga_len;
1308 break;
1309
1310 // store local variable
1311 case ISN_STORE:
1312 --ectx.ec_stack.ga_len;
1313 tv = STACK_TV_VAR(iptr->isn_arg.number);
1314 clear_tv(tv);
1315 *tv = *STACK_TV_BOT(0);
1316 break;
1317
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001318 // store variable or argument in outer scope
1319 case ISN_STOREOUTER:
1320 --ectx.ec_stack.ga_len;
1321 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1322 clear_tv(tv);
1323 *tv = *STACK_TV_BOT(0);
1324 break;
1325
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001326 // store s: variable in old script
1327 case ISN_STORES:
1328 {
1329 hashtab_T *ht = &SCRIPT_VARS(
1330 iptr->isn_arg.loadstore.ls_sid);
1331 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001332 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001333
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001334 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001335 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001336 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001337 else
1338 {
1339 clear_tv(&di->di_tv);
1340 di->di_tv = *STACK_TV_BOT(0);
1341 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001342 }
1343 break;
1344
1345 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 case ISN_STORESCRIPT:
1347 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001348 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 iptr->isn_arg.script.script_sid);
1350 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1351 + iptr->isn_arg.script.script_idx;
1352
1353 --ectx.ec_stack.ga_len;
1354 clear_tv(sv->sv_tv);
1355 *sv->sv_tv = *STACK_TV_BOT(0);
1356 }
1357 break;
1358
1359 // store option
1360 case ISN_STOREOPT:
1361 {
1362 long n = 0;
1363 char_u *s = NULL;
1364 char *msg;
1365
1366 --ectx.ec_stack.ga_len;
1367 tv = STACK_TV_BOT(0);
1368 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001369 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001371 if (s == NULL)
1372 s = (char_u *)"";
1373 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001375 // must be VAR_NUMBER, CHECKTYPE makes sure
1376 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1378 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001379 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 if (msg != NULL)
1381 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001382 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001384 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 }
1387 break;
1388
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001389 // store $ENV
1390 case ISN_STOREENV:
1391 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001392 tv = STACK_TV_BOT(0);
1393 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1394 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001395 break;
1396
1397 // store @r
1398 case ISN_STOREREG:
1399 {
1400 int reg = iptr->isn_arg.number;
1401
1402 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001403 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001404 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001405 tv_get_string(tv), -1, FALSE);
1406 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001407 }
1408 break;
1409
1410 // store v: variable
1411 case ISN_STOREV:
1412 --ectx.ec_stack.ga_len;
1413 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1414 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001415 // should not happen, type is checked when compiling
1416 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001417 break;
1418
Bram Moolenaard3aac292020-04-19 14:32:17 +02001419 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001421 case ISN_STOREB:
1422 case ISN_STOREW:
1423 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 {
1425 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001426 hashtab_T *ht;
1427 switch (iptr->isn_type)
1428 {
1429 case ISN_STOREG:
1430 ht = get_globvar_ht();
1431 break;
1432 case ISN_STOREB:
1433 ht = &curbuf->b_vars->dv_hashtab;
1434 break;
1435 case ISN_STOREW:
1436 ht = &curwin->w_vars->dv_hashtab;
1437 break;
1438 case ISN_STORET:
1439 ht = &curtab->tp_vars->dv_hashtab;
1440 break;
1441 default: // Cannot reach here
1442 goto failed;
1443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444
1445 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001446 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001448 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449 else
1450 {
1451 clear_tv(&di->di_tv);
1452 di->di_tv = *STACK_TV_BOT(0);
1453 }
1454 }
1455 break;
1456
1457 // store number in local variable
1458 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001459 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 clear_tv(tv);
1461 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001462 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 break;
1464
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001465 // store value in list variable
1466 case ISN_STORELIST:
1467 {
1468 typval_T *tv_idx = STACK_TV_BOT(-2);
1469 varnumber_T lidx = tv_idx->vval.v_number;
1470 typval_T *tv_list = STACK_TV_BOT(-1);
1471 list_T *list = tv_list->vval.v_list;
1472
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001473 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001474 if (lidx < 0 && list->lv_len + lidx >= 0)
1475 // negative index is relative to the end
1476 lidx = list->lv_len + lidx;
1477 if (lidx < 0 || lidx > list->lv_len)
1478 {
1479 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001480 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001481 }
1482 tv = STACK_TV_BOT(-3);
1483 if (lidx < list->lv_len)
1484 {
1485 listitem_T *li = list_find(list, lidx);
1486
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001487 if (error_if_locked(li->li_tv.v_lock,
1488 e_cannot_change_list_item))
1489 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001490 // overwrite existing list item
1491 clear_tv(&li->li_tv);
1492 li->li_tv = *tv;
1493 }
1494 else
1495 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001496 if (error_if_locked(list->lv_lock,
1497 e_cannot_change_list))
1498 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001499 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001500 if (list_append_tv(list, tv) == FAIL)
1501 goto failed;
1502 clear_tv(tv);
1503 }
1504 clear_tv(tv_idx);
1505 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001506 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001507 }
1508 break;
1509
1510 // store value in dict variable
1511 case ISN_STOREDICT:
1512 {
1513 typval_T *tv_key = STACK_TV_BOT(-2);
1514 char_u *key = tv_key->vval.v_string;
1515 typval_T *tv_dict = STACK_TV_BOT(-1);
1516 dict_T *dict = tv_dict->vval.v_dict;
1517 dictitem_T *di;
1518
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001519 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001520 if (dict == NULL)
1521 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001522 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001523 goto on_error;
1524 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001525 if (key == NULL)
1526 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001527 tv = STACK_TV_BOT(-3);
1528 di = dict_find(dict, key, -1);
1529 if (di != NULL)
1530 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001531 if (error_if_locked(di->di_tv.v_lock,
1532 e_cannot_change_dict_item))
1533 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001534 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001535 clear_tv(&di->di_tv);
1536 di->di_tv = *tv;
1537 }
1538 else
1539 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001540 if (error_if_locked(dict->dv_lock,
1541 e_cannot_change_dict))
1542 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001543 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001544 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1545 goto failed;
1546 clear_tv(tv);
1547 }
1548 clear_tv(tv_key);
1549 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001550 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001551 }
1552 break;
1553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 // push constant
1555 case ISN_PUSHNR:
1556 case ISN_PUSHBOOL:
1557 case ISN_PUSHSPEC:
1558 case ISN_PUSHF:
1559 case ISN_PUSHS:
1560 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001561 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001562 case ISN_PUSHCHANNEL:
1563 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001564 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 goto failed;
1566 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001567 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 ++ectx.ec_stack.ga_len;
1569 switch (iptr->isn_type)
1570 {
1571 case ISN_PUSHNR:
1572 tv->v_type = VAR_NUMBER;
1573 tv->vval.v_number = iptr->isn_arg.number;
1574 break;
1575 case ISN_PUSHBOOL:
1576 tv->v_type = VAR_BOOL;
1577 tv->vval.v_number = iptr->isn_arg.number;
1578 break;
1579 case ISN_PUSHSPEC:
1580 tv->v_type = VAR_SPECIAL;
1581 tv->vval.v_number = iptr->isn_arg.number;
1582 break;
1583#ifdef FEAT_FLOAT
1584 case ISN_PUSHF:
1585 tv->v_type = VAR_FLOAT;
1586 tv->vval.v_float = iptr->isn_arg.fnumber;
1587 break;
1588#endif
1589 case ISN_PUSHBLOB:
1590 blob_copy(iptr->isn_arg.blob, tv);
1591 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001592 case ISN_PUSHFUNC:
1593 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001594 if (iptr->isn_arg.string == NULL)
1595 tv->vval.v_string = NULL;
1596 else
1597 tv->vval.v_string =
1598 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001599 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001600 case ISN_PUSHCHANNEL:
1601#ifdef FEAT_JOB_CHANNEL
1602 tv->v_type = VAR_CHANNEL;
1603 tv->vval.v_channel = iptr->isn_arg.channel;
1604 if (tv->vval.v_channel != NULL)
1605 ++tv->vval.v_channel->ch_refcount;
1606#endif
1607 break;
1608 case ISN_PUSHJOB:
1609#ifdef FEAT_JOB_CHANNEL
1610 tv->v_type = VAR_JOB;
1611 tv->vval.v_job = iptr->isn_arg.job;
1612 if (tv->vval.v_job != NULL)
1613 ++tv->vval.v_job->jv_refcount;
1614#endif
1615 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 default:
1617 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001618 tv->vval.v_string = vim_strsave(
1619 iptr->isn_arg.string == NULL
1620 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 }
1622 break;
1623
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001624 case ISN_UNLET:
1625 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1626 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001627 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001628 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001629 case ISN_UNLETENV:
1630 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1631 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001632
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001633 case ISN_LOCKCONST:
1634 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1635 break;
1636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 // create a list from items on the stack; uses a single allocation
1638 // for the list header and the items
1639 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001640 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1641 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 break;
1643
1644 // create a dict from items on the stack
1645 case ISN_NEWDICT:
1646 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001647 int count = iptr->isn_arg.number;
1648 dict_T *dict = dict_alloc();
1649 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650
1651 if (dict == NULL)
1652 goto failed;
1653 for (idx = 0; idx < count; ++idx)
1654 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001655 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001657 // check key is unique
1658 item = dict_find(dict, tv->vval.v_string, -1);
1659 if (item != NULL)
1660 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001661 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001662 semsg(_(e_duplicate_key), tv->vval.v_string);
1663 dict_unref(dict);
1664 goto on_error;
1665 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 item = dictitem_alloc(tv->vval.v_string);
1667 clear_tv(tv);
1668 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001669 {
1670 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1674 item->di_tv.v_lock = 0;
1675 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001676 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001677 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001678 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 }
1682
1683 if (count > 0)
1684 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001685 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 goto failed;
1687 else
1688 ++ectx.ec_stack.ga_len;
1689 tv = STACK_TV_BOT(-1);
1690 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001691 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 tv->vval.v_dict = dict;
1693 ++dict->dv_refcount;
1694 }
1695 break;
1696
1697 // call a :def function
1698 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001699 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1701 iptr->isn_arg.dfunc.cdf_argcount,
1702 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001703 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 break;
1705
1706 // call a builtin function
1707 case ISN_BCALL:
1708 SOURCING_LNUM = iptr->isn_lnum;
1709 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1710 iptr->isn_arg.bfunc.cbf_argcount,
1711 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001712 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 break;
1714
1715 // call a funcref or partial
1716 case ISN_PCALL:
1717 {
1718 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1719 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001720 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721
1722 SOURCING_LNUM = iptr->isn_lnum;
1723 if (pfunc->cpf_top)
1724 {
1725 // funcref is above the arguments
1726 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1727 }
1728 else
1729 {
1730 // Get the funcref from the stack.
1731 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001732 partial_tv = *STACK_TV_BOT(0);
1733 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 }
1735 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001736 if (tv == &partial_tv)
1737 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001739 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 }
1741 break;
1742
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001743 case ISN_PCALL_END:
1744 // PCALL finished, arguments have been consumed and replaced by
1745 // the return value. Now clear the funcref from the stack,
1746 // and move the return value in its place.
1747 --ectx.ec_stack.ga_len;
1748 clear_tv(STACK_TV_BOT(-1));
1749 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1750 break;
1751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 // call a user defined function or funcref/partial
1753 case ISN_UCALL:
1754 {
1755 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1756
1757 SOURCING_LNUM = iptr->isn_lnum;
1758 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001759 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001760 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 }
1762 break;
1763
1764 // return from a :def function call
1765 case ISN_RETURN:
1766 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001767 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001768 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001769
1770 if (trystack->ga_len > 0)
1771 trycmd = ((trycmd_T *)trystack->ga_data)
1772 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001773 if (trycmd != NULL
1774 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 && trycmd->tcd_finally_idx != 0)
1776 {
1777 // jump to ":finally"
1778 ectx.ec_iidx = trycmd->tcd_finally_idx;
1779 trycmd->tcd_return = TRUE;
1780 }
1781 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001782 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 }
1784 break;
1785
1786 // push a function reference to a compiled function
1787 case ISN_FUNCREF:
1788 {
1789 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001790 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791
1792 pt = ALLOC_CLEAR_ONE(partial_T);
1793 if (pt == NULL)
1794 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001795 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001796 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001797 vim_free(pt);
1798 goto failed;
1799 }
1800 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1801 + iptr->isn_arg.funcref.fr_func;
1802 pt->pt_func = pt_dfunc->df_ufunc;
1803 pt->pt_refcount = 1;
1804 ++pt_dfunc->df_ufunc->uf_refcount;
1805
1806 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1807 {
1808 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1809 + ectx.ec_dfunc_idx;
1810
1811 // The closure needs to find arguments and local
1812 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001813 pt->pt_ectx_stack = &ectx.ec_stack;
1814 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001815
1816 // If this function returns and the closure is still
1817 // used, we need to make a copy of the context
1818 // (arguments and local variables). Store a reference
1819 // to the partial so we can handle that.
1820 ++pt->pt_refcount;
1821 tv = STACK_TV_VAR(dfunc->df_varcount
1822 + iptr->isn_arg.funcref.fr_var_idx);
1823 if (tv->v_type == VAR_PARTIAL)
1824 {
1825 // TODO: use a garray_T on ectx.
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001826 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001827 emsg("Multiple closures not supported yet");
1828 goto failed;
1829 }
1830 tv->v_type = VAR_PARTIAL;
1831 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001832 }
1833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 tv = STACK_TV_BOT(0);
1835 ++ectx.ec_stack.ga_len;
1836 tv->vval.v_partial = pt;
1837 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001838 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 }
1840 break;
1841
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001842 // Create a global function from a lambda.
1843 case ISN_NEWFUNC:
1844 {
1845 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1846
1847 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1848 }
1849 break;
1850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 // jump if a condition is met
1852 case ISN_JUMP:
1853 {
1854 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1855 int jump = TRUE;
1856
1857 if (when != JUMP_ALWAYS)
1858 {
1859 tv = STACK_TV_BOT(-1);
1860 jump = tv2bool(tv);
1861 if (when == JUMP_IF_FALSE
1862 || when == JUMP_AND_KEEP_IF_FALSE)
1863 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001864 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 {
1866 // drop the value from the stack
1867 clear_tv(tv);
1868 --ectx.ec_stack.ga_len;
1869 }
1870 }
1871 if (jump)
1872 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1873 }
1874 break;
1875
1876 // top of a for loop
1877 case ISN_FOR:
1878 {
1879 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1880 typval_T *idxtv =
1881 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1882
1883 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001884 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 goto failed;
1886 if (++idxtv->vval.v_number >= list->lv_len)
1887 // past the end of the list, jump to "endfor"
1888 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1889 else if (list->lv_first == &range_list_item)
1890 {
1891 // non-materialized range() list
1892 tv = STACK_TV_BOT(0);
1893 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001894 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 tv->vval.v_number = list_find_nr(
1896 list, idxtv->vval.v_number, NULL);
1897 ++ectx.ec_stack.ga_len;
1898 }
1899 else
1900 {
1901 listitem_T *li = list_find(list, idxtv->vval.v_number);
1902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1904 ++ectx.ec_stack.ga_len;
1905 }
1906 }
1907 break;
1908
1909 // start of ":try" block
1910 case ISN_TRY:
1911 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001912 trycmd_T *trycmd = NULL;
1913
Bram Moolenaar270d0382020-05-15 21:42:53 +02001914 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 goto failed;
1916 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1917 + ectx.ec_trystack.ga_len;
1918 ++ectx.ec_trystack.ga_len;
1919 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001920 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1922 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001923 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 }
1925 break;
1926
1927 case ISN_PUSHEXC:
1928 if (current_exception == NULL)
1929 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001930 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 iemsg("Evaluating catch while current_exception is NULL");
1932 goto failed;
1933 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001934 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 goto failed;
1936 tv = STACK_TV_BOT(0);
1937 ++ectx.ec_stack.ga_len;
1938 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001939 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 tv->vval.v_string = vim_strsave(
1941 (char_u *)current_exception->value);
1942 break;
1943
1944 case ISN_CATCH:
1945 {
1946 garray_T *trystack = &ectx.ec_trystack;
1947
1948 if (trystack->ga_len > 0)
1949 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001950 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 + trystack->ga_len - 1;
1952 trycmd->tcd_caught = TRUE;
1953 }
1954 did_emsg = got_int = did_throw = FALSE;
1955 catch_exception(current_exception);
1956 }
1957 break;
1958
1959 // end of ":try" block
1960 case ISN_ENDTRY:
1961 {
1962 garray_T *trystack = &ectx.ec_trystack;
1963
1964 if (trystack->ga_len > 0)
1965 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001966 trycmd_T *trycmd = NULL;
1967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 --trystack->ga_len;
1969 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001970 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 trycmd = ((trycmd_T *)trystack->ga_data)
1972 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001973 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 {
1975 // discard the exception
1976 if (caught_stack == current_exception)
1977 caught_stack = caught_stack->caught;
1978 discard_current_exception();
1979 }
1980
1981 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001982 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 }
1984 }
1985 break;
1986
1987 case ISN_THROW:
1988 --ectx.ec_stack.ga_len;
1989 tv = STACK_TV_BOT(0);
1990 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1991 {
1992 vim_free(tv->vval.v_string);
1993 goto failed;
1994 }
1995 did_throw = TRUE;
1996 break;
1997
1998 // compare with special values
1999 case ISN_COMPAREBOOL:
2000 case ISN_COMPARESPECIAL:
2001 {
2002 typval_T *tv1 = STACK_TV_BOT(-2);
2003 typval_T *tv2 = STACK_TV_BOT(-1);
2004 varnumber_T arg1 = tv1->vval.v_number;
2005 varnumber_T arg2 = tv2->vval.v_number;
2006 int res;
2007
2008 switch (iptr->isn_arg.op.op_type)
2009 {
2010 case EXPR_EQUAL: res = arg1 == arg2; break;
2011 case EXPR_NEQUAL: res = arg1 != arg2; break;
2012 default: res = 0; break;
2013 }
2014
2015 --ectx.ec_stack.ga_len;
2016 tv1->v_type = VAR_BOOL;
2017 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2018 }
2019 break;
2020
2021 // Operation with two number arguments
2022 case ISN_OPNR:
2023 case ISN_COMPARENR:
2024 {
2025 typval_T *tv1 = STACK_TV_BOT(-2);
2026 typval_T *tv2 = STACK_TV_BOT(-1);
2027 varnumber_T arg1 = tv1->vval.v_number;
2028 varnumber_T arg2 = tv2->vval.v_number;
2029 varnumber_T res;
2030
2031 switch (iptr->isn_arg.op.op_type)
2032 {
2033 case EXPR_MULT: res = arg1 * arg2; break;
2034 case EXPR_DIV: res = arg1 / arg2; break;
2035 case EXPR_REM: res = arg1 % arg2; break;
2036 case EXPR_SUB: res = arg1 - arg2; break;
2037 case EXPR_ADD: res = arg1 + arg2; break;
2038
2039 case EXPR_EQUAL: res = arg1 == arg2; break;
2040 case EXPR_NEQUAL: res = arg1 != arg2; break;
2041 case EXPR_GREATER: res = arg1 > arg2; break;
2042 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2043 case EXPR_SMALLER: res = arg1 < arg2; break;
2044 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2045 default: res = 0; break;
2046 }
2047
2048 --ectx.ec_stack.ga_len;
2049 if (iptr->isn_type == ISN_COMPARENR)
2050 {
2051 tv1->v_type = VAR_BOOL;
2052 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2053 }
2054 else
2055 tv1->vval.v_number = res;
2056 }
2057 break;
2058
2059 // Computation with two float arguments
2060 case ISN_OPFLOAT:
2061 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002062#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 {
2064 typval_T *tv1 = STACK_TV_BOT(-2);
2065 typval_T *tv2 = STACK_TV_BOT(-1);
2066 float_T arg1 = tv1->vval.v_float;
2067 float_T arg2 = tv2->vval.v_float;
2068 float_T res = 0;
2069 int cmp = FALSE;
2070
2071 switch (iptr->isn_arg.op.op_type)
2072 {
2073 case EXPR_MULT: res = arg1 * arg2; break;
2074 case EXPR_DIV: res = arg1 / arg2; break;
2075 case EXPR_SUB: res = arg1 - arg2; break;
2076 case EXPR_ADD: res = arg1 + arg2; break;
2077
2078 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2079 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2080 case EXPR_GREATER: cmp = arg1 > arg2; break;
2081 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2082 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2083 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2084 default: cmp = 0; break;
2085 }
2086 --ectx.ec_stack.ga_len;
2087 if (iptr->isn_type == ISN_COMPAREFLOAT)
2088 {
2089 tv1->v_type = VAR_BOOL;
2090 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2091 }
2092 else
2093 tv1->vval.v_float = res;
2094 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002095#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 break;
2097
2098 case ISN_COMPARELIST:
2099 {
2100 typval_T *tv1 = STACK_TV_BOT(-2);
2101 typval_T *tv2 = STACK_TV_BOT(-1);
2102 list_T *arg1 = tv1->vval.v_list;
2103 list_T *arg2 = tv2->vval.v_list;
2104 int cmp = FALSE;
2105 int ic = iptr->isn_arg.op.op_ic;
2106
2107 switch (iptr->isn_arg.op.op_type)
2108 {
2109 case EXPR_EQUAL: cmp =
2110 list_equal(arg1, arg2, ic, FALSE); break;
2111 case EXPR_NEQUAL: cmp =
2112 !list_equal(arg1, arg2, ic, FALSE); break;
2113 case EXPR_IS: cmp = arg1 == arg2; break;
2114 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2115 default: cmp = 0; break;
2116 }
2117 --ectx.ec_stack.ga_len;
2118 clear_tv(tv1);
2119 clear_tv(tv2);
2120 tv1->v_type = VAR_BOOL;
2121 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2122 }
2123 break;
2124
2125 case ISN_COMPAREBLOB:
2126 {
2127 typval_T *tv1 = STACK_TV_BOT(-2);
2128 typval_T *tv2 = STACK_TV_BOT(-1);
2129 blob_T *arg1 = tv1->vval.v_blob;
2130 blob_T *arg2 = tv2->vval.v_blob;
2131 int cmp = FALSE;
2132
2133 switch (iptr->isn_arg.op.op_type)
2134 {
2135 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2136 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2137 case EXPR_IS: cmp = arg1 == arg2; break;
2138 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2139 default: cmp = 0; break;
2140 }
2141 --ectx.ec_stack.ga_len;
2142 clear_tv(tv1);
2143 clear_tv(tv2);
2144 tv1->v_type = VAR_BOOL;
2145 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2146 }
2147 break;
2148
2149 // TODO: handle separately
2150 case ISN_COMPARESTRING:
2151 case ISN_COMPAREDICT:
2152 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 case ISN_COMPAREANY:
2154 {
2155 typval_T *tv1 = STACK_TV_BOT(-2);
2156 typval_T *tv2 = STACK_TV_BOT(-1);
2157 exptype_T exptype = iptr->isn_arg.op.op_type;
2158 int ic = iptr->isn_arg.op.op_ic;
2159
Bram Moolenaareb26f432020-09-14 16:50:05 +02002160 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 typval_compare(tv1, tv2, exptype, ic);
2162 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 --ectx.ec_stack.ga_len;
2164 }
2165 break;
2166
2167 case ISN_ADDLIST:
2168 case ISN_ADDBLOB:
2169 {
2170 typval_T *tv1 = STACK_TV_BOT(-2);
2171 typval_T *tv2 = STACK_TV_BOT(-1);
2172
2173 if (iptr->isn_type == ISN_ADDLIST)
2174 eval_addlist(tv1, tv2);
2175 else
2176 eval_addblob(tv1, tv2);
2177 clear_tv(tv2);
2178 --ectx.ec_stack.ga_len;
2179 }
2180 break;
2181
2182 // Computation with two arguments of unknown type
2183 case ISN_OPANY:
2184 {
2185 typval_T *tv1 = STACK_TV_BOT(-2);
2186 typval_T *tv2 = STACK_TV_BOT(-1);
2187 varnumber_T n1, n2;
2188#ifdef FEAT_FLOAT
2189 float_T f1 = 0, f2 = 0;
2190#endif
2191 int error = FALSE;
2192
2193 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2194 {
2195 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2196 {
2197 eval_addlist(tv1, tv2);
2198 clear_tv(tv2);
2199 --ectx.ec_stack.ga_len;
2200 break;
2201 }
2202 else if (tv1->v_type == VAR_BLOB
2203 && tv2->v_type == VAR_BLOB)
2204 {
2205 eval_addblob(tv1, tv2);
2206 clear_tv(tv2);
2207 --ectx.ec_stack.ga_len;
2208 break;
2209 }
2210 }
2211#ifdef FEAT_FLOAT
2212 if (tv1->v_type == VAR_FLOAT)
2213 {
2214 f1 = tv1->vval.v_float;
2215 n1 = 0;
2216 }
2217 else
2218#endif
2219 {
2220 n1 = tv_get_number_chk(tv1, &error);
2221 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002222 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223#ifdef FEAT_FLOAT
2224 if (tv2->v_type == VAR_FLOAT)
2225 f1 = n1;
2226#endif
2227 }
2228#ifdef FEAT_FLOAT
2229 if (tv2->v_type == VAR_FLOAT)
2230 {
2231 f2 = tv2->vval.v_float;
2232 n2 = 0;
2233 }
2234 else
2235#endif
2236 {
2237 n2 = tv_get_number_chk(tv2, &error);
2238 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002239 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240#ifdef FEAT_FLOAT
2241 if (tv1->v_type == VAR_FLOAT)
2242 f2 = n2;
2243#endif
2244 }
2245#ifdef FEAT_FLOAT
2246 // if there is a float on either side the result is a float
2247 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2248 {
2249 switch (iptr->isn_arg.op.op_type)
2250 {
2251 case EXPR_MULT: f1 = f1 * f2; break;
2252 case EXPR_DIV: f1 = f1 / f2; break;
2253 case EXPR_SUB: f1 = f1 - f2; break;
2254 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002255 default: SOURCING_LNUM = iptr->isn_lnum;
2256 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002257 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 }
2259 clear_tv(tv1);
2260 clear_tv(tv2);
2261 tv1->v_type = VAR_FLOAT;
2262 tv1->vval.v_float = f1;
2263 --ectx.ec_stack.ga_len;
2264 }
2265 else
2266#endif
2267 {
2268 switch (iptr->isn_arg.op.op_type)
2269 {
2270 case EXPR_MULT: n1 = n1 * n2; break;
2271 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2272 case EXPR_SUB: n1 = n1 - n2; break;
2273 case EXPR_ADD: n1 = n1 + n2; break;
2274 default: n1 = num_modulus(n1, n2); break;
2275 }
2276 clear_tv(tv1);
2277 clear_tv(tv2);
2278 tv1->v_type = VAR_NUMBER;
2279 tv1->vval.v_number = n1;
2280 --ectx.ec_stack.ga_len;
2281 }
2282 }
2283 break;
2284
2285 case ISN_CONCAT:
2286 {
2287 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2288 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2289 char_u *res;
2290
2291 res = concat_str(str1, str2);
2292 clear_tv(STACK_TV_BOT(-2));
2293 clear_tv(STACK_TV_BOT(-1));
2294 --ectx.ec_stack.ga_len;
2295 STACK_TV_BOT(-1)->vval.v_string = res;
2296 }
2297 break;
2298
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002299 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002300 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002301 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002302 int is_slice = iptr->isn_type == ISN_STRSLICE;
2303 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002304 char_u *res;
2305
2306 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002307 // string slice: string is at stack-3, first index at
2308 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002309 if (is_slice)
2310 {
2311 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002312 n1 = tv->vval.v_number;
2313 }
2314
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002315 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002316 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002317
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002318 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002319 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002320 if (is_slice)
2321 // Slice: Select the characters from the string
2322 res = string_slice(tv->vval.v_string, n1, n2);
2323 else
2324 // Index: The resulting variable is a string of a
2325 // single character. If the index is too big or
2326 // negative the result is empty.
2327 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002328 vim_free(tv->vval.v_string);
2329 tv->vval.v_string = res;
2330 }
2331 break;
2332
2333 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002334 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 {
Bram Moolenaared591872020-08-15 22:14:53 +02002336 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002338 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339
2340 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002341 // list slice: list is at stack-3, indexes at stack-2 and
2342 // stack-1
2343 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 list = tv->vval.v_list;
2345
2346 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002347 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002349
2350 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 {
Bram Moolenaared591872020-08-15 22:14:53 +02002352 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002353 n1 = tv->vval.v_number;
2354 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 }
Bram Moolenaared591872020-08-15 22:14:53 +02002356
2357 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002358 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002359 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002360 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2361 == FAIL)
2362 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 }
2364 break;
2365
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002366 case ISN_ANYINDEX:
2367 case ISN_ANYSLICE:
2368 {
2369 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2370 typval_T *var1, *var2;
2371 int res;
2372
2373 // index: composite is at stack-2, index at stack-1
2374 // slice: composite is at stack-3, indexes at stack-2 and
2375 // stack-1
2376 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002377 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002378 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2379 goto on_error;
2380 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2381 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2382 res = eval_index_inner(tv, is_slice,
2383 var1, var2, NULL, -1, TRUE);
2384 clear_tv(var1);
2385 if (is_slice)
2386 clear_tv(var2);
2387 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2388 if (res == FAIL)
2389 goto on_error;
2390 }
2391 break;
2392
Bram Moolenaar9af78762020-06-16 11:34:42 +02002393 case ISN_SLICE:
2394 {
2395 list_T *list;
2396 int count = iptr->isn_arg.number;
2397
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002398 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002399 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002400 list = tv->vval.v_list;
2401
2402 // no error for short list, expect it to be checked earlier
2403 if (list != NULL && list->lv_len >= count)
2404 {
2405 list_T *newlist = list_slice(list,
2406 count, list->lv_len - 1);
2407
2408 if (newlist != NULL)
2409 {
2410 list_unref(list);
2411 tv->vval.v_list = newlist;
2412 ++newlist->lv_refcount;
2413 }
2414 }
2415 }
2416 break;
2417
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002418 case ISN_GETITEM:
2419 {
2420 listitem_T *li;
2421 int index = iptr->isn_arg.number;
2422
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002423 // Get list item: list is at stack-1, push item.
2424 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002425 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002426 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002427
2428 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2429 goto failed;
2430 ++ectx.ec_stack.ga_len;
2431 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2432 }
2433 break;
2434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 case ISN_MEMBER:
2436 {
2437 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002438 char_u *key;
2439 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002440 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002441
2442 // dict member: dict is at stack-2, key at stack-1
2443 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002444 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002445 dict = tv->vval.v_dict;
2446
2447 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002448 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002449 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002450
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002451 if ((di = dict_find(dict, key, -1)) == NULL)
2452 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002453 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002454 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002455 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002456 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002457 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002458 --ectx.ec_stack.ga_len;
2459 // Clear the dict after getting the item, to avoid that it
2460 // make the item invalid.
2461 tv = STACK_TV_BOT(-1);
2462 temp_tv = *tv;
2463 copy_tv(&di->di_tv, tv);
2464 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002465 }
2466 break;
2467
2468 // dict member with string key
2469 case ISN_STRINGMEMBER:
2470 {
2471 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002473 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474
2475 tv = STACK_TV_BOT(-1);
2476 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2477 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002478 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002480 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 }
2482 dict = tv->vval.v_dict;
2483
2484 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2485 == NULL)
2486 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002487 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002489 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002491 // Clear the dict after getting the item, to avoid that it
2492 // make the item invalid.
2493 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002495 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 }
2497 break;
2498
2499 case ISN_NEGATENR:
2500 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002501 if (tv->v_type != VAR_NUMBER
2502#ifdef FEAT_FLOAT
2503 && tv->v_type != VAR_FLOAT
2504#endif
2505 )
2506 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002507 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002508 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002509 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002510 }
2511#ifdef FEAT_FLOAT
2512 if (tv->v_type == VAR_FLOAT)
2513 tv->vval.v_float = -tv->vval.v_float;
2514 else
2515#endif
2516 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 break;
2518
2519 case ISN_CHECKNR:
2520 {
2521 int error = FALSE;
2522
2523 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002524 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002526 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 (void)tv_get_number_chk(tv, &error);
2528 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002529 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 }
2531 break;
2532
2533 case ISN_CHECKTYPE:
2534 {
2535 checktype_T *ct = &iptr->isn_arg.type;
2536
2537 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002538 SOURCING_LNUM = iptr->isn_lnum;
2539 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2540 goto on_error;
2541
2542 // number 0 is FALSE, number 1 is TRUE
2543 if (tv->v_type == VAR_NUMBER
2544 && ct->ct_type->tt_type == VAR_BOOL
2545 && (tv->vval.v_number == 0
2546 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002548 tv->v_type = VAR_BOOL;
2549 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002550 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 }
2552 }
2553 break;
2554
Bram Moolenaar9af78762020-06-16 11:34:42 +02002555 case ISN_CHECKLEN:
2556 {
2557 int min_len = iptr->isn_arg.checklen.cl_min_len;
2558 list_T *list = NULL;
2559
2560 tv = STACK_TV_BOT(-1);
2561 if (tv->v_type == VAR_LIST)
2562 list = tv->vval.v_list;
2563 if (list == NULL || list->lv_len < min_len
2564 || (list->lv_len > min_len
2565 && !iptr->isn_arg.checklen.cl_more_OK))
2566 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002567 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002568 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002569 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002570 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002571 }
2572 }
2573 break;
2574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 case ISN_2BOOL:
2576 {
2577 int n;
2578
2579 tv = STACK_TV_BOT(-1);
2580 n = tv2bool(tv);
2581 if (iptr->isn_arg.number) // invert
2582 n = !n;
2583 clear_tv(tv);
2584 tv->v_type = VAR_BOOL;
2585 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2586 }
2587 break;
2588
2589 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002590 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 {
2592 char_u *str;
2593
2594 tv = STACK_TV_BOT(iptr->isn_arg.number);
2595 if (tv->v_type != VAR_STRING)
2596 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002597 if (iptr->isn_type == ISN_2STRING_ANY)
2598 {
2599 switch (tv->v_type)
2600 {
2601 case VAR_SPECIAL:
2602 case VAR_BOOL:
2603 case VAR_NUMBER:
2604 case VAR_FLOAT:
2605 case VAR_BLOB: break;
2606 default: to_string_error(tv->v_type);
2607 goto on_error;
2608 }
2609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 str = typval_tostring(tv);
2611 clear_tv(tv);
2612 tv->v_type = VAR_STRING;
2613 tv->vval.v_string = str;
2614 }
2615 }
2616 break;
2617
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002618 case ISN_PUT:
2619 {
2620 int regname = iptr->isn_arg.put.put_regname;
2621 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2622 char_u *expr = NULL;
2623 int dir = FORWARD;
2624
2625 if (regname == '=')
2626 {
2627 tv = STACK_TV_BOT(-1);
2628 if (tv->v_type == VAR_STRING)
2629 expr = tv->vval.v_string;
2630 else
2631 {
2632 expr = typval_tostring(tv); // allocates value
2633 clear_tv(tv);
2634 }
2635 --ectx.ec_stack.ga_len;
2636 }
2637 if (lnum == -2)
2638 // :put! above cursor
2639 dir = BACKWARD;
2640 else if (lnum >= 0)
2641 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2642 check_cursor();
2643 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2644 vim_free(expr);
2645 }
2646 break;
2647
Bram Moolenaar389df252020-07-09 21:20:47 +02002648 case ISN_SHUFFLE:
2649 {
2650 typval_T tmp_tv;
2651 int item = iptr->isn_arg.shuffle.shfl_item;
2652 int up = iptr->isn_arg.shuffle.shfl_up;
2653
2654 tmp_tv = *STACK_TV_BOT(-item);
2655 for ( ; up > 0 && item > 1; --up)
2656 {
2657 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2658 --item;
2659 }
2660 *STACK_TV_BOT(-item) = tmp_tv;
2661 }
2662 break;
2663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 case ISN_DROP:
2665 --ectx.ec_stack.ga_len;
2666 clear_tv(STACK_TV_BOT(0));
2667 break;
2668 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002669 continue;
2670
Bram Moolenaard032f342020-07-18 18:13:02 +02002671func_return:
2672 // Restore previous function. If the frame pointer is zero then there
2673 // is none and we are done.
2674 if (ectx.ec_frame_idx == initial_frame_idx)
2675 {
2676 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2677 // only fails when out of memory
2678 goto failed;
2679 goto done;
2680 }
2681 if (func_return(&ectx) == FAIL)
2682 // only fails when out of memory
2683 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002684 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002685
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002686on_error:
2687 if (trylevel == 0)
2688 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 }
2690
2691done:
2692 // function finished, get result from the stack.
2693 tv = STACK_TV_BOT(-1);
2694 *rettv = *tv;
2695 tv->v_type = VAR_UNKNOWN;
2696 ret = OK;
2697
2698failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002699 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002700 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002701 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002702
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002703 estack_pop();
2704 current_sctx = save_current_sctx;
2705
2706failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002707 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2709 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002712 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002713
2714 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002715 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002716 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717 return ret;
2718}
2719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720/*
2721 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002722 * We don't really need this at runtime, but we do have tests that require it,
2723 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 */
2725 void
2726ex_disassemble(exarg_T *eap)
2727{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002728 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002729 char_u *fname;
2730 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 dfunc_T *dfunc;
2732 isn_T *instr;
2733 int current;
2734 int line_idx = 0;
2735 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002736 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002738 if (STRNCMP(arg, "<lambda>", 8) == 0)
2739 {
2740 arg += 8;
2741 (void)getdigits(&arg);
2742 fname = vim_strnsave(eap->arg, arg - eap->arg);
2743 }
2744 else
2745 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002746 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002747 if (fname == NULL)
2748 {
2749 semsg(_(e_invarg2), eap->arg);
2750 return;
2751 }
2752
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002753 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002754 if (ufunc == NULL)
2755 {
2756 char_u *p = untrans_function_name(fname);
2757
2758 if (p != NULL)
2759 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002760 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002761 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002762 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 if (ufunc == NULL)
2764 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002765 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 return;
2767 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002768 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002769 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2770 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002771 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002773 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 return;
2775 }
2776 if (ufunc->uf_name_exp != NULL)
2777 msg((char *)ufunc->uf_name_exp);
2778 else
2779 msg((char *)ufunc->uf_name);
2780
2781 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2782 instr = dfunc->df_instr;
2783 for (current = 0; current < dfunc->df_instr_count; ++current)
2784 {
2785 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002786 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787
2788 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2789 {
2790 if (current > prev_current)
2791 {
2792 msg_puts("\n\n");
2793 prev_current = current;
2794 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002795 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2796 if (line != NULL)
2797 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 }
2799
2800 switch (iptr->isn_type)
2801 {
2802 case ISN_EXEC:
2803 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2804 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002805 case ISN_EXECCONCAT:
2806 smsg("%4d EXECCONCAT %lld", current,
2807 (long long)iptr->isn_arg.number);
2808 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 case ISN_ECHO:
2810 {
2811 echo_T *echo = &iptr->isn_arg.echo;
2812
2813 smsg("%4d %s %d", current,
2814 echo->echo_with_white ? "ECHO" : "ECHON",
2815 echo->echo_count);
2816 }
2817 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002818 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002819 smsg("%4d EXECUTE %lld", current,
2820 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002821 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002822 case ISN_ECHOMSG:
2823 smsg("%4d ECHOMSG %lld", current,
2824 (long long)(iptr->isn_arg.number));
2825 break;
2826 case ISN_ECHOERR:
2827 smsg("%4d ECHOERR %lld", current,
2828 (long long)(iptr->isn_arg.number));
2829 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002831 case ISN_LOADOUTER:
2832 {
2833 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2834
2835 if (iptr->isn_arg.number < 0)
2836 smsg("%4d LOAD%s arg[%lld]", current, add,
2837 (long long)(iptr->isn_arg.number
2838 + STACK_FRAME_SIZE));
2839 else
2840 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002841 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 break;
2844 case ISN_LOADV:
2845 smsg("%4d LOADV v:%s", current,
2846 get_vim_var_name(iptr->isn_arg.number));
2847 break;
2848 case ISN_LOADSCRIPT:
2849 {
2850 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002851 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2853 + iptr->isn_arg.script.script_idx;
2854
2855 smsg("%4d LOADSCRIPT %s from %s", current,
2856 sv->sv_name, si->sn_name);
2857 }
2858 break;
2859 case ISN_LOADS:
2860 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002861 scriptitem_T *si = SCRIPT_ITEM(
2862 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863
2864 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002865 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 }
2867 break;
2868 case ISN_LOADG:
2869 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2870 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002871 case ISN_LOADB:
2872 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2873 break;
2874 case ISN_LOADW:
2875 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2876 break;
2877 case ISN_LOADT:
2878 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2879 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002880 case ISN_LOADGDICT:
2881 smsg("%4d LOAD g:", current);
2882 break;
2883 case ISN_LOADBDICT:
2884 smsg("%4d LOAD b:", current);
2885 break;
2886 case ISN_LOADWDICT:
2887 smsg("%4d LOAD w:", current);
2888 break;
2889 case ISN_LOADTDICT:
2890 smsg("%4d LOAD t:", current);
2891 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 case ISN_LOADOPT:
2893 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2894 break;
2895 case ISN_LOADENV:
2896 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2897 break;
2898 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002899 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 break;
2901
2902 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002903 case ISN_STOREOUTER:
2904 {
2905 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2906
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002907 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002908 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002909 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002910 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002911 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002912 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002915 case ISN_STOREV:
2916 smsg("%4d STOREV v:%s", current,
2917 get_vim_var_name(iptr->isn_arg.number));
2918 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002920 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2921 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002922 case ISN_STOREB:
2923 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2924 break;
2925 case ISN_STOREW:
2926 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2927 break;
2928 case ISN_STORET:
2929 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2930 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002931 case ISN_STORES:
2932 {
2933 scriptitem_T *si = SCRIPT_ITEM(
2934 iptr->isn_arg.loadstore.ls_sid);
2935
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002936 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002937 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 break;
2940 case ISN_STORESCRIPT:
2941 {
2942 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002943 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2945 + iptr->isn_arg.script.script_idx;
2946
2947 smsg("%4d STORESCRIPT %s in %s", current,
2948 sv->sv_name, si->sn_name);
2949 }
2950 break;
2951 case ISN_STOREOPT:
2952 smsg("%4d STOREOPT &%s", current,
2953 iptr->isn_arg.storeopt.so_name);
2954 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002955 case ISN_STOREENV:
2956 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2957 break;
2958 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002959 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002960 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 case ISN_STORENR:
2962 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002963 iptr->isn_arg.storenr.stnr_val,
2964 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 break;
2966
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002967 case ISN_STORELIST:
2968 smsg("%4d STORELIST", current);
2969 break;
2970
2971 case ISN_STOREDICT:
2972 smsg("%4d STOREDICT", current);
2973 break;
2974
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 // constants
2976 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002977 smsg("%4d PUSHNR %lld", current,
2978 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 break;
2980 case ISN_PUSHBOOL:
2981 case ISN_PUSHSPEC:
2982 smsg("%4d PUSH %s", current,
2983 get_var_special_name(iptr->isn_arg.number));
2984 break;
2985 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002986#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002988#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 break;
2990 case ISN_PUSHS:
2991 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2992 break;
2993 case ISN_PUSHBLOB:
2994 {
2995 char_u *r;
2996 char_u numbuf[NUMBUFLEN];
2997 char_u *tofree;
2998
2999 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003000 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 vim_free(tofree);
3002 }
3003 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003004 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003005 {
3006 char *name = (char *)iptr->isn_arg.string;
3007
3008 smsg("%4d PUSHFUNC \"%s\"", current,
3009 name == NULL ? "[none]" : name);
3010 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003011 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003012 case ISN_PUSHCHANNEL:
3013#ifdef FEAT_JOB_CHANNEL
3014 {
3015 channel_T *channel = iptr->isn_arg.channel;
3016
3017 smsg("%4d PUSHCHANNEL %d", current,
3018 channel == NULL ? 0 : channel->ch_id);
3019 }
3020#endif
3021 break;
3022 case ISN_PUSHJOB:
3023#ifdef FEAT_JOB_CHANNEL
3024 {
3025 typval_T tv;
3026 char_u *name;
3027
3028 tv.v_type = VAR_JOB;
3029 tv.vval.v_job = iptr->isn_arg.job;
3030 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003031 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003032 }
3033#endif
3034 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 case ISN_PUSHEXC:
3036 smsg("%4d PUSH v:exception", current);
3037 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003038 case ISN_UNLET:
3039 smsg("%4d UNLET%s %s", current,
3040 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3041 iptr->isn_arg.unlet.ul_name);
3042 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003043 case ISN_UNLETENV:
3044 smsg("%4d UNLETENV%s $%s", current,
3045 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3046 iptr->isn_arg.unlet.ul_name);
3047 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003048 case ISN_LOCKCONST:
3049 smsg("%4d LOCKCONST", current);
3050 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003052 smsg("%4d NEWLIST size %lld", current,
3053 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 break;
3055 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003056 smsg("%4d NEWDICT size %lld", current,
3057 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 break;
3059
3060 // function call
3061 case ISN_BCALL:
3062 {
3063 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3064
3065 smsg("%4d BCALL %s(argc %d)", current,
3066 internal_func_name(cbfunc->cbf_idx),
3067 cbfunc->cbf_argcount);
3068 }
3069 break;
3070 case ISN_DCALL:
3071 {
3072 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3073 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3074 + cdfunc->cdf_idx;
3075
3076 smsg("%4d DCALL %s(argc %d)", current,
3077 df->df_ufunc->uf_name_exp != NULL
3078 ? df->df_ufunc->uf_name_exp
3079 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3080 }
3081 break;
3082 case ISN_UCALL:
3083 {
3084 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3085
3086 smsg("%4d UCALL %s(argc %d)", current,
3087 cufunc->cuf_name, cufunc->cuf_argcount);
3088 }
3089 break;
3090 case ISN_PCALL:
3091 {
3092 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3093
3094 smsg("%4d PCALL%s (argc %d)", current,
3095 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3096 }
3097 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003098 case ISN_PCALL_END:
3099 smsg("%4d PCALL end", current);
3100 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 case ISN_RETURN:
3102 smsg("%4d RETURN", current);
3103 break;
3104 case ISN_FUNCREF:
3105 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003106 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003108 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003110 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003111 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 }
3113 break;
3114
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003115 case ISN_NEWFUNC:
3116 {
3117 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3118
3119 smsg("%4d NEWFUNC %s %s", current,
3120 newfunc->nf_lambda, newfunc->nf_global);
3121 }
3122 break;
3123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 case ISN_JUMP:
3125 {
3126 char *when = "?";
3127
3128 switch (iptr->isn_arg.jump.jump_when)
3129 {
3130 case JUMP_ALWAYS:
3131 when = "JUMP";
3132 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 case JUMP_AND_KEEP_IF_TRUE:
3134 when = "JUMP_AND_KEEP_IF_TRUE";
3135 break;
3136 case JUMP_IF_FALSE:
3137 when = "JUMP_IF_FALSE";
3138 break;
3139 case JUMP_AND_KEEP_IF_FALSE:
3140 when = "JUMP_AND_KEEP_IF_FALSE";
3141 break;
3142 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003143 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 iptr->isn_arg.jump.jump_where);
3145 }
3146 break;
3147
3148 case ISN_FOR:
3149 {
3150 forloop_T *forloop = &iptr->isn_arg.forloop;
3151
3152 smsg("%4d FOR $%d -> %d", current,
3153 forloop->for_idx, forloop->for_end);
3154 }
3155 break;
3156
3157 case ISN_TRY:
3158 {
3159 try_T *try = &iptr->isn_arg.try;
3160
3161 smsg("%4d TRY catch -> %d, finally -> %d", current,
3162 try->try_catch, try->try_finally);
3163 }
3164 break;
3165 case ISN_CATCH:
3166 // TODO
3167 smsg("%4d CATCH", current);
3168 break;
3169 case ISN_ENDTRY:
3170 smsg("%4d ENDTRY", current);
3171 break;
3172 case ISN_THROW:
3173 smsg("%4d THROW", current);
3174 break;
3175
3176 // expression operations on number
3177 case ISN_OPNR:
3178 case ISN_OPFLOAT:
3179 case ISN_OPANY:
3180 {
3181 char *what;
3182 char *ins;
3183
3184 switch (iptr->isn_arg.op.op_type)
3185 {
3186 case EXPR_MULT: what = "*"; break;
3187 case EXPR_DIV: what = "/"; break;
3188 case EXPR_REM: what = "%"; break;
3189 case EXPR_SUB: what = "-"; break;
3190 case EXPR_ADD: what = "+"; break;
3191 default: what = "???"; break;
3192 }
3193 switch (iptr->isn_type)
3194 {
3195 case ISN_OPNR: ins = "OPNR"; break;
3196 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3197 case ISN_OPANY: ins = "OPANY"; break;
3198 default: ins = "???"; break;
3199 }
3200 smsg("%4d %s %s", current, ins, what);
3201 }
3202 break;
3203
3204 case ISN_COMPAREBOOL:
3205 case ISN_COMPARESPECIAL:
3206 case ISN_COMPARENR:
3207 case ISN_COMPAREFLOAT:
3208 case ISN_COMPARESTRING:
3209 case ISN_COMPAREBLOB:
3210 case ISN_COMPARELIST:
3211 case ISN_COMPAREDICT:
3212 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 case ISN_COMPAREANY:
3214 {
3215 char *p;
3216 char buf[10];
3217 char *type;
3218
3219 switch (iptr->isn_arg.op.op_type)
3220 {
3221 case EXPR_EQUAL: p = "=="; break;
3222 case EXPR_NEQUAL: p = "!="; break;
3223 case EXPR_GREATER: p = ">"; break;
3224 case EXPR_GEQUAL: p = ">="; break;
3225 case EXPR_SMALLER: p = "<"; break;
3226 case EXPR_SEQUAL: p = "<="; break;
3227 case EXPR_MATCH: p = "=~"; break;
3228 case EXPR_IS: p = "is"; break;
3229 case EXPR_ISNOT: p = "isnot"; break;
3230 case EXPR_NOMATCH: p = "!~"; break;
3231 default: p = "???"; break;
3232 }
3233 STRCPY(buf, p);
3234 if (iptr->isn_arg.op.op_ic == TRUE)
3235 strcat(buf, "?");
3236 switch(iptr->isn_type)
3237 {
3238 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3239 case ISN_COMPARESPECIAL:
3240 type = "COMPARESPECIAL"; break;
3241 case ISN_COMPARENR: type = "COMPARENR"; break;
3242 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3243 case ISN_COMPARESTRING:
3244 type = "COMPARESTRING"; break;
3245 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3246 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3247 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3248 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3250 default: type = "???"; break;
3251 }
3252
3253 smsg("%4d %s %s", current, type, buf);
3254 }
3255 break;
3256
3257 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3258 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3259
3260 // expression operations
3261 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003262 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003263 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003264 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003265 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003266 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3267 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003268 case ISN_SLICE: smsg("%4d SLICE %lld",
3269 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003270 case ISN_GETITEM: smsg("%4d ITEM %lld",
3271 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003272 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3273 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 iptr->isn_arg.string); break;
3275 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3276
3277 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003278 case ISN_CHECKTYPE:
3279 {
3280 char *tofree;
3281
3282 smsg("%4d CHECKTYPE %s stack[%d]", current,
3283 type_name(iptr->isn_arg.type.ct_type, &tofree),
3284 iptr->isn_arg.type.ct_off);
3285 vim_free(tofree);
3286 break;
3287 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003288 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3289 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3290 iptr->isn_arg.checklen.cl_min_len);
3291 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 case ISN_2BOOL: if (iptr->isn_arg.number)
3293 smsg("%4d INVERT (!val)", current);
3294 else
3295 smsg("%4d 2BOOL (!!val)", current);
3296 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003297 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3298 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003299 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003300 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3301 (long long)(iptr->isn_arg.number));
3302 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003303 case ISN_PUT:
3304 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3305 (long)iptr->isn_arg.put.put_lnum);
3306 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307
Bram Moolenaar389df252020-07-09 21:20:47 +02003308 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3309 iptr->isn_arg.shuffle.shfl_item,
3310 iptr->isn_arg.shuffle.shfl_up);
3311 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 case ISN_DROP: smsg("%4d DROP", current); break;
3313 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003314
3315 out_flush(); // output one line at a time
3316 ui_breakcheck();
3317 if (got_int)
3318 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320}
3321
3322/*
3323 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3324 * list, etc. Mostly like what JavaScript does, except that empty list and
3325 * empty dictionary are FALSE.
3326 */
3327 int
3328tv2bool(typval_T *tv)
3329{
3330 switch (tv->v_type)
3331 {
3332 case VAR_NUMBER:
3333 return tv->vval.v_number != 0;
3334 case VAR_FLOAT:
3335#ifdef FEAT_FLOAT
3336 return tv->vval.v_float != 0.0;
3337#else
3338 break;
3339#endif
3340 case VAR_PARTIAL:
3341 return tv->vval.v_partial != NULL;
3342 case VAR_FUNC:
3343 case VAR_STRING:
3344 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3345 case VAR_LIST:
3346 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3347 case VAR_DICT:
3348 return tv->vval.v_dict != NULL
3349 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3350 case VAR_BOOL:
3351 case VAR_SPECIAL:
3352 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3353 case VAR_JOB:
3354#ifdef FEAT_JOB_CHANNEL
3355 return tv->vval.v_job != NULL;
3356#else
3357 break;
3358#endif
3359 case VAR_CHANNEL:
3360#ifdef FEAT_JOB_CHANNEL
3361 return tv->vval.v_channel != NULL;
3362#else
3363 break;
3364#endif
3365 case VAR_BLOB:
3366 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3367 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003368 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 case VAR_VOID:
3370 break;
3371 }
3372 return FALSE;
3373}
3374
3375/*
3376 * If "tv" is a string give an error and return FAIL.
3377 */
3378 int
3379check_not_string(typval_T *tv)
3380{
3381 if (tv->v_type == VAR_STRING)
3382 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003383 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 clear_tv(tv);
3385 return FAIL;
3386 }
3387 return OK;
3388}
3389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390
3391#endif // FEAT_EVAL