blob: 40d6193ca58e36b97d7ca83260075d8affa9aa74 [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
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200832 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200833 && ufunc->uf_va_type->tt_member != &t_any
834 && tv->vval.v_list != NULL)
835 {
836 type_T *expected = ufunc->uf_va_type->tt_member;
837 listitem_T *li = tv->vval.v_list->lv_first;
838
839 for (idx = 0; idx < vararg_count; ++idx)
840 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200841 if (check_typval_type(expected, &li->li_tv,
842 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200843 goto failed_early;
844 li = li->li_next;
845 }
846 }
847
Bram Moolenaar23e03252020-04-12 22:22:31 +0200848 if (defcount > 0)
849 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200850 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200851 --ectx.ec_stack.ga_len;
852 }
853
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100854 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200855 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100856 if (defcount > 0)
857 for (idx = 0; idx < defcount; ++idx)
858 {
859 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
860 ++ectx.ec_stack.ga_len;
861 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200862 if (ufunc->uf_va_name != NULL)
863 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864
865 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200866 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
867 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200869 if (partial != NULL)
870 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200871 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
872 {
873 // TODO: is this always the right way?
874 ectx.ec_outer_stack = &current_ectx->ec_stack;
875 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
876 }
877 else
878 {
879 ectx.ec_outer_stack = partial->pt_ectx_stack;
880 ectx.ec_outer_frame = partial->pt_ectx_frame;
881 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200882 }
883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884 // dummy frame entries
885 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
886 {
887 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
888 ++ectx.ec_stack.ga_len;
889 }
890
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200891 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200892 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200893 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
894 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200895 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200897 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200898 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200899 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200900
901 ectx.ec_instr = dfunc->df_instr;
902 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100903
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200904 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200905 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200906 estack_push_ufunc(ufunc, 1);
907 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200908 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
909
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100910 // Decide where to start execution, handles optional arguments.
911 init_instr_idx(ufunc, argc, &ectx);
912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913 for (;;)
914 {
915 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100916
Bram Moolenaar270d0382020-05-15 21:42:53 +0200917 if (++breakcheck_count >= 100)
918 {
919 line_breakcheck();
920 breakcheck_count = 0;
921 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100922 if (got_int)
923 {
924 // Turn CTRL-C into an exception.
925 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100926 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100927 goto failed;
928 did_throw = TRUE;
929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930
Bram Moolenaara26b9702020-04-18 19:53:28 +0200931 if (did_emsg && msg_list != NULL && *msg_list != NULL)
932 {
933 // Turn an error message into an exception.
934 did_emsg = FALSE;
935 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
936 goto failed;
937 did_throw = TRUE;
938 *msg_list = NULL;
939 }
940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941 if (did_throw && !ectx.ec_in_catch)
942 {
943 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100944 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945
946 // An exception jumps to the first catch, finally, or returns from
947 // the current function.
948 if (trystack->ga_len > 0)
949 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200950 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 {
952 // jump to ":catch" or ":finally"
953 ectx.ec_in_catch = TRUE;
954 ectx.ec_iidx = trycmd->tcd_catch_idx;
955 }
956 else
957 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200958 // Not inside try or need to return from current functions.
959 // Push a dummy return value.
960 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
961 goto failed;
962 tv = STACK_TV_BOT(0);
963 tv->v_type = VAR_NUMBER;
964 tv->vval.v_number = 0;
965 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200966 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200968 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100969 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200970 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
971 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 goto done;
973 }
974
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200975 if (func_return(&ectx) == FAIL)
976 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 }
978 continue;
979 }
980
981 iptr = &ectx.ec_instr[ectx.ec_iidx++];
982 switch (iptr->isn_type)
983 {
984 // execute Ex command line
985 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200986 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 do_cmdline_cmd(iptr->isn_arg.string);
988 break;
989
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200990 // execute Ex command from pieces on the stack
991 case ISN_EXECCONCAT:
992 {
993 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200994 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200995 int pass;
996 int i;
997 char_u *cmd = NULL;
998 char_u *str;
999
1000 for (pass = 1; pass <= 2; ++pass)
1001 {
1002 for (i = 0; i < count; ++i)
1003 {
1004 tv = STACK_TV_BOT(i - count);
1005 str = tv->vval.v_string;
1006 if (str != NULL && *str != NUL)
1007 {
1008 if (pass == 2)
1009 STRCPY(cmd + len, str);
1010 len += STRLEN(str);
1011 }
1012 if (pass == 2)
1013 clear_tv(tv);
1014 }
1015 if (pass == 1)
1016 {
1017 cmd = alloc(len + 1);
1018 if (cmd == NULL)
1019 goto failed;
1020 len = 0;
1021 }
1022 }
1023
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001024 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001025 do_cmdline_cmd(cmd);
1026 vim_free(cmd);
1027 }
1028 break;
1029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 // execute :echo {string} ...
1031 case ISN_ECHO:
1032 {
1033 int count = iptr->isn_arg.echo.echo_count;
1034 int atstart = TRUE;
1035 int needclr = TRUE;
1036
1037 for (idx = 0; idx < count; ++idx)
1038 {
1039 tv = STACK_TV_BOT(idx - count);
1040 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1041 &atstart, &needclr);
1042 clear_tv(tv);
1043 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001044 if (needclr)
1045 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046 ectx.ec_stack.ga_len -= count;
1047 }
1048 break;
1049
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001050 // :execute {string} ...
1051 // :echomsg {string} ...
1052 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001053 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001054 case ISN_ECHOMSG:
1055 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001056 {
1057 int count = iptr->isn_arg.number;
1058 garray_T ga;
1059 char_u buf[NUMBUFLEN];
1060 char_u *p;
1061 int len;
1062 int failed = FALSE;
1063
1064 ga_init2(&ga, 1, 80);
1065 for (idx = 0; idx < count; ++idx)
1066 {
1067 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001068 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001069 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001070 if (tv->v_type == VAR_CHANNEL
1071 || tv->v_type == VAR_JOB)
1072 {
1073 SOURCING_LNUM = iptr->isn_lnum;
1074 emsg(_(e_inval_string));
1075 break;
1076 }
1077 else
1078 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001079 }
1080 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001081 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001082
1083 len = (int)STRLEN(p);
1084 if (ga_grow(&ga, len + 2) == FAIL)
1085 failed = TRUE;
1086 else
1087 {
1088 if (ga.ga_len > 0)
1089 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1090 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1091 ga.ga_len += len;
1092 }
1093 clear_tv(tv);
1094 }
1095 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001096 if (failed)
1097 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001098
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001099 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001100 {
1101 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001102 {
1103 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001104 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001105 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001106 else
1107 {
1108 msg_sb_eol();
1109 if (iptr->isn_type == ISN_ECHOMSG)
1110 {
1111 msg_attr(ga.ga_data, echo_attr);
1112 out_flush();
1113 }
1114 else
1115 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001116 SOURCING_LNUM = iptr->isn_lnum;
1117 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001118 }
1119 }
1120 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001121 ga_clear(&ga);
1122 }
1123 break;
1124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 // load local variable or argument
1126 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001127 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 goto failed;
1129 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1130 ++ectx.ec_stack.ga_len;
1131 break;
1132
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001133 // load variable or argument from outer scope
1134 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001135 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001136 goto failed;
1137 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1138 STACK_TV_BOT(0));
1139 ++ectx.ec_stack.ga_len;
1140 break;
1141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 // load v: variable
1143 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001144 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 goto failed;
1146 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1147 ++ectx.ec_stack.ga_len;
1148 break;
1149
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001150 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 case ISN_LOADSCRIPT:
1152 {
1153 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001154 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 svar_T *sv;
1156
1157 sv = ((svar_T *)si->sn_var_vals.ga_data)
1158 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001159 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160 goto failed;
1161 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1162 ++ectx.ec_stack.ga_len;
1163 }
1164 break;
1165
1166 // load s: variable in old script
1167 case ISN_LOADS:
1168 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001169 hashtab_T *ht = &SCRIPT_VARS(
1170 iptr->isn_arg.loadstore.ls_sid);
1171 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 if (di == NULL)
1175 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001176 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001177 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001178 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 }
1180 else
1181 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001182 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 goto failed;
1184 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1185 ++ectx.ec_stack.ga_len;
1186 }
1187 }
1188 break;
1189
Bram Moolenaard3aac292020-04-19 14:32:17 +02001190 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001192 case ISN_LOADB:
1193 case ISN_LOADW:
1194 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001196 dictitem_T *di = NULL;
1197 hashtab_T *ht = NULL;
1198 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001199
Bram Moolenaard3aac292020-04-19 14:32:17 +02001200 switch (iptr->isn_type)
1201 {
1202 case ISN_LOADG:
1203 ht = get_globvar_ht();
1204 namespace = 'g';
1205 break;
1206 case ISN_LOADB:
1207 ht = &curbuf->b_vars->dv_hashtab;
1208 namespace = 'b';
1209 break;
1210 case ISN_LOADW:
1211 ht = &curwin->w_vars->dv_hashtab;
1212 namespace = 'w';
1213 break;
1214 case ISN_LOADT:
1215 ht = &curtab->tp_vars->dv_hashtab;
1216 namespace = 't';
1217 break;
1218 default: // Cannot reach here
1219 goto failed;
1220 }
1221 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 if (di == NULL)
1224 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001225 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001226 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001227 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001228 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 }
1230 else
1231 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001232 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 goto failed;
1234 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1235 ++ectx.ec_stack.ga_len;
1236 }
1237 }
1238 break;
1239
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001240 // load g:/b:/w:/t: namespace
1241 case ISN_LOADGDICT:
1242 case ISN_LOADBDICT:
1243 case ISN_LOADWDICT:
1244 case ISN_LOADTDICT:
1245 {
1246 dict_T *d = NULL;
1247
1248 switch (iptr->isn_type)
1249 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001250 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1251 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1252 case ISN_LOADWDICT: d = curwin->w_vars; break;
1253 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001254 default: // Cannot reach here
1255 goto failed;
1256 }
1257 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1258 goto failed;
1259 tv = STACK_TV_BOT(0);
1260 tv->v_type = VAR_DICT;
1261 tv->v_lock = 0;
1262 tv->vval.v_dict = d;
1263 ++ectx.ec_stack.ga_len;
1264 }
1265 break;
1266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 // load &option
1268 case ISN_LOADOPT:
1269 {
1270 typval_T optval;
1271 char_u *name = iptr->isn_arg.string;
1272
Bram Moolenaara8c17702020-04-01 21:17:24 +02001273 // This is not expected to fail, name is checked during
1274 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001275 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001277 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001278 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 *STACK_TV_BOT(0) = optval;
1280 ++ectx.ec_stack.ga_len;
1281 }
1282 break;
1283
1284 // load $ENV
1285 case ISN_LOADENV:
1286 {
1287 typval_T optval;
1288 char_u *name = iptr->isn_arg.string;
1289
Bram Moolenaar270d0382020-05-15 21:42:53 +02001290 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001292 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001293 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294 *STACK_TV_BOT(0) = optval;
1295 ++ectx.ec_stack.ga_len;
1296 }
1297 break;
1298
1299 // load @register
1300 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001301 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302 goto failed;
1303 tv = STACK_TV_BOT(0);
1304 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001305 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 tv->vval.v_string = get_reg_contents(
1307 iptr->isn_arg.number, GREG_EXPR_SRC);
1308 ++ectx.ec_stack.ga_len;
1309 break;
1310
1311 // store local variable
1312 case ISN_STORE:
1313 --ectx.ec_stack.ga_len;
1314 tv = STACK_TV_VAR(iptr->isn_arg.number);
1315 clear_tv(tv);
1316 *tv = *STACK_TV_BOT(0);
1317 break;
1318
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001319 // store variable or argument in outer scope
1320 case ISN_STOREOUTER:
1321 --ectx.ec_stack.ga_len;
1322 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1323 clear_tv(tv);
1324 *tv = *STACK_TV_BOT(0);
1325 break;
1326
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001327 // store s: variable in old script
1328 case ISN_STORES:
1329 {
1330 hashtab_T *ht = &SCRIPT_VARS(
1331 iptr->isn_arg.loadstore.ls_sid);
1332 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001333 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001334
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001335 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001336 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001337 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001338 else
1339 {
1340 clear_tv(&di->di_tv);
1341 di->di_tv = *STACK_TV_BOT(0);
1342 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001343 }
1344 break;
1345
1346 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 case ISN_STORESCRIPT:
1348 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001349 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 iptr->isn_arg.script.script_sid);
1351 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1352 + iptr->isn_arg.script.script_idx;
1353
1354 --ectx.ec_stack.ga_len;
1355 clear_tv(sv->sv_tv);
1356 *sv->sv_tv = *STACK_TV_BOT(0);
1357 }
1358 break;
1359
1360 // store option
1361 case ISN_STOREOPT:
1362 {
1363 long n = 0;
1364 char_u *s = NULL;
1365 char *msg;
1366
1367 --ectx.ec_stack.ga_len;
1368 tv = STACK_TV_BOT(0);
1369 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001370 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001372 if (s == NULL)
1373 s = (char_u *)"";
1374 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001376 // must be VAR_NUMBER, CHECKTYPE makes sure
1377 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1379 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001380 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 if (msg != NULL)
1382 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001383 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001385 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 }
1388 break;
1389
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001390 // store $ENV
1391 case ISN_STOREENV:
1392 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001393 tv = STACK_TV_BOT(0);
1394 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1395 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001396 break;
1397
1398 // store @r
1399 case ISN_STOREREG:
1400 {
1401 int reg = iptr->isn_arg.number;
1402
1403 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001404 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001405 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001406 tv_get_string(tv), -1, FALSE);
1407 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001408 }
1409 break;
1410
1411 // store v: variable
1412 case ISN_STOREV:
1413 --ectx.ec_stack.ga_len;
1414 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1415 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001416 // should not happen, type is checked when compiling
1417 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001418 break;
1419
Bram Moolenaard3aac292020-04-19 14:32:17 +02001420 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001422 case ISN_STOREB:
1423 case ISN_STOREW:
1424 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 {
1426 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001427 hashtab_T *ht;
1428 switch (iptr->isn_type)
1429 {
1430 case ISN_STOREG:
1431 ht = get_globvar_ht();
1432 break;
1433 case ISN_STOREB:
1434 ht = &curbuf->b_vars->dv_hashtab;
1435 break;
1436 case ISN_STOREW:
1437 ht = &curwin->w_vars->dv_hashtab;
1438 break;
1439 case ISN_STORET:
1440 ht = &curtab->tp_vars->dv_hashtab;
1441 break;
1442 default: // Cannot reach here
1443 goto failed;
1444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445
1446 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001447 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001449 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 else
1451 {
1452 clear_tv(&di->di_tv);
1453 di->di_tv = *STACK_TV_BOT(0);
1454 }
1455 }
1456 break;
1457
1458 // store number in local variable
1459 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001460 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 clear_tv(tv);
1462 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001463 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 break;
1465
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001466 // store value in list variable
1467 case ISN_STORELIST:
1468 {
1469 typval_T *tv_idx = STACK_TV_BOT(-2);
1470 varnumber_T lidx = tv_idx->vval.v_number;
1471 typval_T *tv_list = STACK_TV_BOT(-1);
1472 list_T *list = tv_list->vval.v_list;
1473
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001474 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001475 if (lidx < 0 && list->lv_len + lidx >= 0)
1476 // negative index is relative to the end
1477 lidx = list->lv_len + lidx;
1478 if (lidx < 0 || lidx > list->lv_len)
1479 {
1480 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001481 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001482 }
1483 tv = STACK_TV_BOT(-3);
1484 if (lidx < list->lv_len)
1485 {
1486 listitem_T *li = list_find(list, lidx);
1487
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001488 if (error_if_locked(li->li_tv.v_lock,
1489 e_cannot_change_list_item))
1490 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001491 // overwrite existing list item
1492 clear_tv(&li->li_tv);
1493 li->li_tv = *tv;
1494 }
1495 else
1496 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001497 if (error_if_locked(list->lv_lock,
1498 e_cannot_change_list))
1499 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001500 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001501 if (list_append_tv(list, tv) == FAIL)
1502 goto failed;
1503 clear_tv(tv);
1504 }
1505 clear_tv(tv_idx);
1506 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001507 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001508 }
1509 break;
1510
1511 // store value in dict variable
1512 case ISN_STOREDICT:
1513 {
1514 typval_T *tv_key = STACK_TV_BOT(-2);
1515 char_u *key = tv_key->vval.v_string;
1516 typval_T *tv_dict = STACK_TV_BOT(-1);
1517 dict_T *dict = tv_dict->vval.v_dict;
1518 dictitem_T *di;
1519
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001520 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001521 if (dict == NULL)
1522 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001523 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001524 goto on_error;
1525 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001526 if (key == NULL)
1527 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001528 tv = STACK_TV_BOT(-3);
1529 di = dict_find(dict, key, -1);
1530 if (di != NULL)
1531 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001532 if (error_if_locked(di->di_tv.v_lock,
1533 e_cannot_change_dict_item))
1534 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001535 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001536 clear_tv(&di->di_tv);
1537 di->di_tv = *tv;
1538 }
1539 else
1540 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001541 if (error_if_locked(dict->dv_lock,
1542 e_cannot_change_dict))
1543 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001544 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001545 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1546 goto failed;
1547 clear_tv(tv);
1548 }
1549 clear_tv(tv_key);
1550 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001551 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001552 }
1553 break;
1554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 // push constant
1556 case ISN_PUSHNR:
1557 case ISN_PUSHBOOL:
1558 case ISN_PUSHSPEC:
1559 case ISN_PUSHF:
1560 case ISN_PUSHS:
1561 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001562 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001563 case ISN_PUSHCHANNEL:
1564 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001565 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 goto failed;
1567 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001568 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 ++ectx.ec_stack.ga_len;
1570 switch (iptr->isn_type)
1571 {
1572 case ISN_PUSHNR:
1573 tv->v_type = VAR_NUMBER;
1574 tv->vval.v_number = iptr->isn_arg.number;
1575 break;
1576 case ISN_PUSHBOOL:
1577 tv->v_type = VAR_BOOL;
1578 tv->vval.v_number = iptr->isn_arg.number;
1579 break;
1580 case ISN_PUSHSPEC:
1581 tv->v_type = VAR_SPECIAL;
1582 tv->vval.v_number = iptr->isn_arg.number;
1583 break;
1584#ifdef FEAT_FLOAT
1585 case ISN_PUSHF:
1586 tv->v_type = VAR_FLOAT;
1587 tv->vval.v_float = iptr->isn_arg.fnumber;
1588 break;
1589#endif
1590 case ISN_PUSHBLOB:
1591 blob_copy(iptr->isn_arg.blob, tv);
1592 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001593 case ISN_PUSHFUNC:
1594 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001595 if (iptr->isn_arg.string == NULL)
1596 tv->vval.v_string = NULL;
1597 else
1598 tv->vval.v_string =
1599 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001600 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001601 case ISN_PUSHCHANNEL:
1602#ifdef FEAT_JOB_CHANNEL
1603 tv->v_type = VAR_CHANNEL;
1604 tv->vval.v_channel = iptr->isn_arg.channel;
1605 if (tv->vval.v_channel != NULL)
1606 ++tv->vval.v_channel->ch_refcount;
1607#endif
1608 break;
1609 case ISN_PUSHJOB:
1610#ifdef FEAT_JOB_CHANNEL
1611 tv->v_type = VAR_JOB;
1612 tv->vval.v_job = iptr->isn_arg.job;
1613 if (tv->vval.v_job != NULL)
1614 ++tv->vval.v_job->jv_refcount;
1615#endif
1616 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 default:
1618 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001619 tv->vval.v_string = vim_strsave(
1620 iptr->isn_arg.string == NULL
1621 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 }
1623 break;
1624
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001625 case ISN_UNLET:
1626 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1627 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001628 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001629 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001630 case ISN_UNLETENV:
1631 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1632 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001633
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001634 case ISN_LOCKCONST:
1635 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1636 break;
1637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 // create a list from items on the stack; uses a single allocation
1639 // for the list header and the items
1640 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001641 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1642 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643 break;
1644
1645 // create a dict from items on the stack
1646 case ISN_NEWDICT:
1647 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001648 int count = iptr->isn_arg.number;
1649 dict_T *dict = dict_alloc();
1650 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651
1652 if (dict == NULL)
1653 goto failed;
1654 for (idx = 0; idx < count; ++idx)
1655 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001656 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001658 // check key is unique
1659 item = dict_find(dict, tv->vval.v_string, -1);
1660 if (item != NULL)
1661 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001662 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001663 semsg(_(e_duplicate_key), tv->vval.v_string);
1664 dict_unref(dict);
1665 goto on_error;
1666 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 item = dictitem_alloc(tv->vval.v_string);
1668 clear_tv(tv);
1669 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001670 {
1671 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001673 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1675 item->di_tv.v_lock = 0;
1676 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001677 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001678 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001679 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 }
1683
1684 if (count > 0)
1685 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001686 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 goto failed;
1688 else
1689 ++ectx.ec_stack.ga_len;
1690 tv = STACK_TV_BOT(-1);
1691 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001692 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 tv->vval.v_dict = dict;
1694 ++dict->dv_refcount;
1695 }
1696 break;
1697
1698 // call a :def function
1699 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001700 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1702 iptr->isn_arg.dfunc.cdf_argcount,
1703 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001704 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 break;
1706
1707 // call a builtin function
1708 case ISN_BCALL:
1709 SOURCING_LNUM = iptr->isn_lnum;
1710 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1711 iptr->isn_arg.bfunc.cbf_argcount,
1712 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001713 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 break;
1715
1716 // call a funcref or partial
1717 case ISN_PCALL:
1718 {
1719 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1720 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001721 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722
1723 SOURCING_LNUM = iptr->isn_lnum;
1724 if (pfunc->cpf_top)
1725 {
1726 // funcref is above the arguments
1727 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1728 }
1729 else
1730 {
1731 // Get the funcref from the stack.
1732 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001733 partial_tv = *STACK_TV_BOT(0);
1734 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 }
1736 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001737 if (tv == &partial_tv)
1738 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001740 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 }
1742 break;
1743
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001744 case ISN_PCALL_END:
1745 // PCALL finished, arguments have been consumed and replaced by
1746 // the return value. Now clear the funcref from the stack,
1747 // and move the return value in its place.
1748 --ectx.ec_stack.ga_len;
1749 clear_tv(STACK_TV_BOT(-1));
1750 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1751 break;
1752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 // call a user defined function or funcref/partial
1754 case ISN_UCALL:
1755 {
1756 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1757
1758 SOURCING_LNUM = iptr->isn_lnum;
1759 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001760 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001761 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 }
1763 break;
1764
1765 // return from a :def function call
1766 case ISN_RETURN:
1767 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001768 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001769 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001770
1771 if (trystack->ga_len > 0)
1772 trycmd = ((trycmd_T *)trystack->ga_data)
1773 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001774 if (trycmd != NULL
1775 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 && trycmd->tcd_finally_idx != 0)
1777 {
1778 // jump to ":finally"
1779 ectx.ec_iidx = trycmd->tcd_finally_idx;
1780 trycmd->tcd_return = TRUE;
1781 }
1782 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001783 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 }
1785 break;
1786
1787 // push a function reference to a compiled function
1788 case ISN_FUNCREF:
1789 {
1790 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001791 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792
1793 pt = ALLOC_CLEAR_ONE(partial_T);
1794 if (pt == NULL)
1795 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001796 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001797 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001798 vim_free(pt);
1799 goto failed;
1800 }
1801 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1802 + iptr->isn_arg.funcref.fr_func;
1803 pt->pt_func = pt_dfunc->df_ufunc;
1804 pt->pt_refcount = 1;
1805 ++pt_dfunc->df_ufunc->uf_refcount;
1806
1807 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1808 {
1809 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1810 + ectx.ec_dfunc_idx;
1811
1812 // The closure needs to find arguments and local
1813 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001814 pt->pt_ectx_stack = &ectx.ec_stack;
1815 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001816
1817 // If this function returns and the closure is still
1818 // used, we need to make a copy of the context
1819 // (arguments and local variables). Store a reference
1820 // to the partial so we can handle that.
1821 ++pt->pt_refcount;
1822 tv = STACK_TV_VAR(dfunc->df_varcount
1823 + iptr->isn_arg.funcref.fr_var_idx);
1824 if (tv->v_type == VAR_PARTIAL)
1825 {
1826 // TODO: use a garray_T on ectx.
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001827 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001828 emsg("Multiple closures not supported yet");
1829 goto failed;
1830 }
1831 tv->v_type = VAR_PARTIAL;
1832 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001833 }
1834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 tv = STACK_TV_BOT(0);
1836 ++ectx.ec_stack.ga_len;
1837 tv->vval.v_partial = pt;
1838 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001839 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 }
1841 break;
1842
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001843 // Create a global function from a lambda.
1844 case ISN_NEWFUNC:
1845 {
1846 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1847
1848 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1849 }
1850 break;
1851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 // jump if a condition is met
1853 case ISN_JUMP:
1854 {
1855 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1856 int jump = TRUE;
1857
1858 if (when != JUMP_ALWAYS)
1859 {
1860 tv = STACK_TV_BOT(-1);
1861 jump = tv2bool(tv);
1862 if (when == JUMP_IF_FALSE
1863 || when == JUMP_AND_KEEP_IF_FALSE)
1864 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001865 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 {
1867 // drop the value from the stack
1868 clear_tv(tv);
1869 --ectx.ec_stack.ga_len;
1870 }
1871 }
1872 if (jump)
1873 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1874 }
1875 break;
1876
1877 // top of a for loop
1878 case ISN_FOR:
1879 {
1880 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1881 typval_T *idxtv =
1882 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1883
1884 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001885 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 goto failed;
1887 if (++idxtv->vval.v_number >= list->lv_len)
1888 // past the end of the list, jump to "endfor"
1889 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1890 else if (list->lv_first == &range_list_item)
1891 {
1892 // non-materialized range() list
1893 tv = STACK_TV_BOT(0);
1894 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001895 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 tv->vval.v_number = list_find_nr(
1897 list, idxtv->vval.v_number, NULL);
1898 ++ectx.ec_stack.ga_len;
1899 }
1900 else
1901 {
1902 listitem_T *li = list_find(list, idxtv->vval.v_number);
1903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1905 ++ectx.ec_stack.ga_len;
1906 }
1907 }
1908 break;
1909
1910 // start of ":try" block
1911 case ISN_TRY:
1912 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001913 trycmd_T *trycmd = NULL;
1914
Bram Moolenaar270d0382020-05-15 21:42:53 +02001915 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 goto failed;
1917 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1918 + ectx.ec_trystack.ga_len;
1919 ++ectx.ec_trystack.ga_len;
1920 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001921 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1923 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001924 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02001925 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 }
1927 break;
1928
1929 case ISN_PUSHEXC:
1930 if (current_exception == NULL)
1931 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001932 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 iemsg("Evaluating catch while current_exception is NULL");
1934 goto failed;
1935 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001936 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 goto failed;
1938 tv = STACK_TV_BOT(0);
1939 ++ectx.ec_stack.ga_len;
1940 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001941 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 tv->vval.v_string = vim_strsave(
1943 (char_u *)current_exception->value);
1944 break;
1945
1946 case ISN_CATCH:
1947 {
1948 garray_T *trystack = &ectx.ec_trystack;
1949
1950 if (trystack->ga_len > 0)
1951 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001952 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 + trystack->ga_len - 1;
1954 trycmd->tcd_caught = TRUE;
1955 }
1956 did_emsg = got_int = did_throw = FALSE;
1957 catch_exception(current_exception);
1958 }
1959 break;
1960
1961 // end of ":try" block
1962 case ISN_ENDTRY:
1963 {
1964 garray_T *trystack = &ectx.ec_trystack;
1965
1966 if (trystack->ga_len > 0)
1967 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001968 trycmd_T *trycmd = NULL;
1969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 --trystack->ga_len;
1971 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001972 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 trycmd = ((trycmd_T *)trystack->ga_data)
1974 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001975 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 {
1977 // discard the exception
1978 if (caught_stack == current_exception)
1979 caught_stack = caught_stack->caught;
1980 discard_current_exception();
1981 }
1982
1983 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001984 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 }
1986 }
1987 break;
1988
1989 case ISN_THROW:
1990 --ectx.ec_stack.ga_len;
1991 tv = STACK_TV_BOT(0);
1992 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1993 {
1994 vim_free(tv->vval.v_string);
1995 goto failed;
1996 }
1997 did_throw = TRUE;
1998 break;
1999
2000 // compare with special values
2001 case ISN_COMPAREBOOL:
2002 case ISN_COMPARESPECIAL:
2003 {
2004 typval_T *tv1 = STACK_TV_BOT(-2);
2005 typval_T *tv2 = STACK_TV_BOT(-1);
2006 varnumber_T arg1 = tv1->vval.v_number;
2007 varnumber_T arg2 = tv2->vval.v_number;
2008 int res;
2009
2010 switch (iptr->isn_arg.op.op_type)
2011 {
2012 case EXPR_EQUAL: res = arg1 == arg2; break;
2013 case EXPR_NEQUAL: res = arg1 != arg2; break;
2014 default: res = 0; break;
2015 }
2016
2017 --ectx.ec_stack.ga_len;
2018 tv1->v_type = VAR_BOOL;
2019 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2020 }
2021 break;
2022
2023 // Operation with two number arguments
2024 case ISN_OPNR:
2025 case ISN_COMPARENR:
2026 {
2027 typval_T *tv1 = STACK_TV_BOT(-2);
2028 typval_T *tv2 = STACK_TV_BOT(-1);
2029 varnumber_T arg1 = tv1->vval.v_number;
2030 varnumber_T arg2 = tv2->vval.v_number;
2031 varnumber_T res;
2032
2033 switch (iptr->isn_arg.op.op_type)
2034 {
2035 case EXPR_MULT: res = arg1 * arg2; break;
2036 case EXPR_DIV: res = arg1 / arg2; break;
2037 case EXPR_REM: res = arg1 % arg2; break;
2038 case EXPR_SUB: res = arg1 - arg2; break;
2039 case EXPR_ADD: res = arg1 + arg2; break;
2040
2041 case EXPR_EQUAL: res = arg1 == arg2; break;
2042 case EXPR_NEQUAL: res = arg1 != arg2; break;
2043 case EXPR_GREATER: res = arg1 > arg2; break;
2044 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2045 case EXPR_SMALLER: res = arg1 < arg2; break;
2046 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2047 default: res = 0; break;
2048 }
2049
2050 --ectx.ec_stack.ga_len;
2051 if (iptr->isn_type == ISN_COMPARENR)
2052 {
2053 tv1->v_type = VAR_BOOL;
2054 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2055 }
2056 else
2057 tv1->vval.v_number = res;
2058 }
2059 break;
2060
2061 // Computation with two float arguments
2062 case ISN_OPFLOAT:
2063 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002064#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 {
2066 typval_T *tv1 = STACK_TV_BOT(-2);
2067 typval_T *tv2 = STACK_TV_BOT(-1);
2068 float_T arg1 = tv1->vval.v_float;
2069 float_T arg2 = tv2->vval.v_float;
2070 float_T res = 0;
2071 int cmp = FALSE;
2072
2073 switch (iptr->isn_arg.op.op_type)
2074 {
2075 case EXPR_MULT: res = arg1 * arg2; break;
2076 case EXPR_DIV: res = arg1 / arg2; break;
2077 case EXPR_SUB: res = arg1 - arg2; break;
2078 case EXPR_ADD: res = arg1 + arg2; break;
2079
2080 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2081 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2082 case EXPR_GREATER: cmp = arg1 > arg2; break;
2083 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2084 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2085 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2086 default: cmp = 0; break;
2087 }
2088 --ectx.ec_stack.ga_len;
2089 if (iptr->isn_type == ISN_COMPAREFLOAT)
2090 {
2091 tv1->v_type = VAR_BOOL;
2092 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2093 }
2094 else
2095 tv1->vval.v_float = res;
2096 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002097#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 break;
2099
2100 case ISN_COMPARELIST:
2101 {
2102 typval_T *tv1 = STACK_TV_BOT(-2);
2103 typval_T *tv2 = STACK_TV_BOT(-1);
2104 list_T *arg1 = tv1->vval.v_list;
2105 list_T *arg2 = tv2->vval.v_list;
2106 int cmp = FALSE;
2107 int ic = iptr->isn_arg.op.op_ic;
2108
2109 switch (iptr->isn_arg.op.op_type)
2110 {
2111 case EXPR_EQUAL: cmp =
2112 list_equal(arg1, arg2, ic, FALSE); break;
2113 case EXPR_NEQUAL: cmp =
2114 !list_equal(arg1, arg2, ic, FALSE); break;
2115 case EXPR_IS: cmp = arg1 == arg2; break;
2116 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2117 default: cmp = 0; break;
2118 }
2119 --ectx.ec_stack.ga_len;
2120 clear_tv(tv1);
2121 clear_tv(tv2);
2122 tv1->v_type = VAR_BOOL;
2123 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2124 }
2125 break;
2126
2127 case ISN_COMPAREBLOB:
2128 {
2129 typval_T *tv1 = STACK_TV_BOT(-2);
2130 typval_T *tv2 = STACK_TV_BOT(-1);
2131 blob_T *arg1 = tv1->vval.v_blob;
2132 blob_T *arg2 = tv2->vval.v_blob;
2133 int cmp = FALSE;
2134
2135 switch (iptr->isn_arg.op.op_type)
2136 {
2137 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2138 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2139 case EXPR_IS: cmp = arg1 == arg2; break;
2140 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2141 default: cmp = 0; break;
2142 }
2143 --ectx.ec_stack.ga_len;
2144 clear_tv(tv1);
2145 clear_tv(tv2);
2146 tv1->v_type = VAR_BOOL;
2147 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2148 }
2149 break;
2150
2151 // TODO: handle separately
2152 case ISN_COMPARESTRING:
2153 case ISN_COMPAREDICT:
2154 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 case ISN_COMPAREANY:
2156 {
2157 typval_T *tv1 = STACK_TV_BOT(-2);
2158 typval_T *tv2 = STACK_TV_BOT(-1);
2159 exptype_T exptype = iptr->isn_arg.op.op_type;
2160 int ic = iptr->isn_arg.op.op_ic;
2161
Bram Moolenaareb26f432020-09-14 16:50:05 +02002162 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 typval_compare(tv1, tv2, exptype, ic);
2164 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 --ectx.ec_stack.ga_len;
2166 }
2167 break;
2168
2169 case ISN_ADDLIST:
2170 case ISN_ADDBLOB:
2171 {
2172 typval_T *tv1 = STACK_TV_BOT(-2);
2173 typval_T *tv2 = STACK_TV_BOT(-1);
2174
2175 if (iptr->isn_type == ISN_ADDLIST)
2176 eval_addlist(tv1, tv2);
2177 else
2178 eval_addblob(tv1, tv2);
2179 clear_tv(tv2);
2180 --ectx.ec_stack.ga_len;
2181 }
2182 break;
2183
2184 // Computation with two arguments of unknown type
2185 case ISN_OPANY:
2186 {
2187 typval_T *tv1 = STACK_TV_BOT(-2);
2188 typval_T *tv2 = STACK_TV_BOT(-1);
2189 varnumber_T n1, n2;
2190#ifdef FEAT_FLOAT
2191 float_T f1 = 0, f2 = 0;
2192#endif
2193 int error = FALSE;
2194
2195 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2196 {
2197 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2198 {
2199 eval_addlist(tv1, tv2);
2200 clear_tv(tv2);
2201 --ectx.ec_stack.ga_len;
2202 break;
2203 }
2204 else if (tv1->v_type == VAR_BLOB
2205 && tv2->v_type == VAR_BLOB)
2206 {
2207 eval_addblob(tv1, tv2);
2208 clear_tv(tv2);
2209 --ectx.ec_stack.ga_len;
2210 break;
2211 }
2212 }
2213#ifdef FEAT_FLOAT
2214 if (tv1->v_type == VAR_FLOAT)
2215 {
2216 f1 = tv1->vval.v_float;
2217 n1 = 0;
2218 }
2219 else
2220#endif
2221 {
2222 n1 = tv_get_number_chk(tv1, &error);
2223 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002224 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225#ifdef FEAT_FLOAT
2226 if (tv2->v_type == VAR_FLOAT)
2227 f1 = n1;
2228#endif
2229 }
2230#ifdef FEAT_FLOAT
2231 if (tv2->v_type == VAR_FLOAT)
2232 {
2233 f2 = tv2->vval.v_float;
2234 n2 = 0;
2235 }
2236 else
2237#endif
2238 {
2239 n2 = tv_get_number_chk(tv2, &error);
2240 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002241 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242#ifdef FEAT_FLOAT
2243 if (tv1->v_type == VAR_FLOAT)
2244 f2 = n2;
2245#endif
2246 }
2247#ifdef FEAT_FLOAT
2248 // if there is a float on either side the result is a float
2249 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2250 {
2251 switch (iptr->isn_arg.op.op_type)
2252 {
2253 case EXPR_MULT: f1 = f1 * f2; break;
2254 case EXPR_DIV: f1 = f1 / f2; break;
2255 case EXPR_SUB: f1 = f1 - f2; break;
2256 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002257 default: SOURCING_LNUM = iptr->isn_lnum;
2258 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002259 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 }
2261 clear_tv(tv1);
2262 clear_tv(tv2);
2263 tv1->v_type = VAR_FLOAT;
2264 tv1->vval.v_float = f1;
2265 --ectx.ec_stack.ga_len;
2266 }
2267 else
2268#endif
2269 {
2270 switch (iptr->isn_arg.op.op_type)
2271 {
2272 case EXPR_MULT: n1 = n1 * n2; break;
2273 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2274 case EXPR_SUB: n1 = n1 - n2; break;
2275 case EXPR_ADD: n1 = n1 + n2; break;
2276 default: n1 = num_modulus(n1, n2); break;
2277 }
2278 clear_tv(tv1);
2279 clear_tv(tv2);
2280 tv1->v_type = VAR_NUMBER;
2281 tv1->vval.v_number = n1;
2282 --ectx.ec_stack.ga_len;
2283 }
2284 }
2285 break;
2286
2287 case ISN_CONCAT:
2288 {
2289 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2290 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2291 char_u *res;
2292
2293 res = concat_str(str1, str2);
2294 clear_tv(STACK_TV_BOT(-2));
2295 clear_tv(STACK_TV_BOT(-1));
2296 --ectx.ec_stack.ga_len;
2297 STACK_TV_BOT(-1)->vval.v_string = res;
2298 }
2299 break;
2300
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002301 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002302 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002303 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002304 int is_slice = iptr->isn_type == ISN_STRSLICE;
2305 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002306 char_u *res;
2307
2308 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002309 // string slice: string is at stack-3, first index at
2310 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002311 if (is_slice)
2312 {
2313 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002314 n1 = tv->vval.v_number;
2315 }
2316
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002317 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002318 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002319
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002320 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002321 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002322 if (is_slice)
2323 // Slice: Select the characters from the string
2324 res = string_slice(tv->vval.v_string, n1, n2);
2325 else
2326 // Index: The resulting variable is a string of a
2327 // single character. If the index is too big or
2328 // negative the result is empty.
2329 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002330 vim_free(tv->vval.v_string);
2331 tv->vval.v_string = res;
2332 }
2333 break;
2334
2335 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002336 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 {
Bram Moolenaared591872020-08-15 22:14:53 +02002338 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002340 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341
2342 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002343 // list slice: list is at stack-3, indexes at stack-2 and
2344 // stack-1
2345 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346 list = tv->vval.v_list;
2347
2348 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002349 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002351
2352 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 {
Bram Moolenaared591872020-08-15 22:14:53 +02002354 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002355 n1 = tv->vval.v_number;
2356 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 }
Bram Moolenaared591872020-08-15 22:14:53 +02002358
2359 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002360 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002361 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002362 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2363 == FAIL)
2364 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 }
2366 break;
2367
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002368 case ISN_ANYINDEX:
2369 case ISN_ANYSLICE:
2370 {
2371 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2372 typval_T *var1, *var2;
2373 int res;
2374
2375 // index: composite is at stack-2, index at stack-1
2376 // slice: composite is at stack-3, indexes at stack-2 and
2377 // stack-1
2378 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002379 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002380 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2381 goto on_error;
2382 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2383 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2384 res = eval_index_inner(tv, is_slice,
2385 var1, var2, NULL, -1, TRUE);
2386 clear_tv(var1);
2387 if (is_slice)
2388 clear_tv(var2);
2389 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2390 if (res == FAIL)
2391 goto on_error;
2392 }
2393 break;
2394
Bram Moolenaar9af78762020-06-16 11:34:42 +02002395 case ISN_SLICE:
2396 {
2397 list_T *list;
2398 int count = iptr->isn_arg.number;
2399
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002400 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002401 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002402 list = tv->vval.v_list;
2403
2404 // no error for short list, expect it to be checked earlier
2405 if (list != NULL && list->lv_len >= count)
2406 {
2407 list_T *newlist = list_slice(list,
2408 count, list->lv_len - 1);
2409
2410 if (newlist != NULL)
2411 {
2412 list_unref(list);
2413 tv->vval.v_list = newlist;
2414 ++newlist->lv_refcount;
2415 }
2416 }
2417 }
2418 break;
2419
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002420 case ISN_GETITEM:
2421 {
2422 listitem_T *li;
2423 int index = iptr->isn_arg.number;
2424
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002425 // Get list item: list is at stack-1, push item.
2426 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002427 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002428 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002429
2430 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2431 goto failed;
2432 ++ectx.ec_stack.ga_len;
2433 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2434 }
2435 break;
2436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 case ISN_MEMBER:
2438 {
2439 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002440 char_u *key;
2441 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002442 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002443
2444 // dict member: dict is at stack-2, key at stack-1
2445 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002446 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002447 dict = tv->vval.v_dict;
2448
2449 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002450 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002451 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002452
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002453 if ((di = dict_find(dict, key, -1)) == NULL)
2454 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002455 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002456 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002457 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002458 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002459 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002460 --ectx.ec_stack.ga_len;
2461 // Clear the dict after getting the item, to avoid that it
2462 // make the item invalid.
2463 tv = STACK_TV_BOT(-1);
2464 temp_tv = *tv;
2465 copy_tv(&di->di_tv, tv);
2466 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002467 }
2468 break;
2469
2470 // dict member with string key
2471 case ISN_STRINGMEMBER:
2472 {
2473 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002475 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476
2477 tv = STACK_TV_BOT(-1);
2478 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2479 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002480 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002482 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 }
2484 dict = tv->vval.v_dict;
2485
2486 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2487 == NULL)
2488 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002489 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002491 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002493 // Clear the dict after getting the item, to avoid that it
2494 // make the item invalid.
2495 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002497 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 }
2499 break;
2500
2501 case ISN_NEGATENR:
2502 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002503 if (tv->v_type != VAR_NUMBER
2504#ifdef FEAT_FLOAT
2505 && tv->v_type != VAR_FLOAT
2506#endif
2507 )
2508 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002509 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002510 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002511 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002512 }
2513#ifdef FEAT_FLOAT
2514 if (tv->v_type == VAR_FLOAT)
2515 tv->vval.v_float = -tv->vval.v_float;
2516 else
2517#endif
2518 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 break;
2520
2521 case ISN_CHECKNR:
2522 {
2523 int error = FALSE;
2524
2525 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002526 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002528 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 (void)tv_get_number_chk(tv, &error);
2530 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002531 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 }
2533 break;
2534
2535 case ISN_CHECKTYPE:
2536 {
2537 checktype_T *ct = &iptr->isn_arg.type;
2538
2539 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002540 SOURCING_LNUM = iptr->isn_lnum;
2541 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2542 goto on_error;
2543
2544 // number 0 is FALSE, number 1 is TRUE
2545 if (tv->v_type == VAR_NUMBER
2546 && ct->ct_type->tt_type == VAR_BOOL
2547 && (tv->vval.v_number == 0
2548 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002550 tv->v_type = VAR_BOOL;
2551 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002552 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 }
2554 }
2555 break;
2556
Bram Moolenaar9af78762020-06-16 11:34:42 +02002557 case ISN_CHECKLEN:
2558 {
2559 int min_len = iptr->isn_arg.checklen.cl_min_len;
2560 list_T *list = NULL;
2561
2562 tv = STACK_TV_BOT(-1);
2563 if (tv->v_type == VAR_LIST)
2564 list = tv->vval.v_list;
2565 if (list == NULL || list->lv_len < min_len
2566 || (list->lv_len > min_len
2567 && !iptr->isn_arg.checklen.cl_more_OK))
2568 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002569 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002570 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002571 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002572 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002573 }
2574 }
2575 break;
2576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 case ISN_2BOOL:
2578 {
2579 int n;
2580
2581 tv = STACK_TV_BOT(-1);
2582 n = tv2bool(tv);
2583 if (iptr->isn_arg.number) // invert
2584 n = !n;
2585 clear_tv(tv);
2586 tv->v_type = VAR_BOOL;
2587 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2588 }
2589 break;
2590
2591 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002592 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 {
2594 char_u *str;
2595
2596 tv = STACK_TV_BOT(iptr->isn_arg.number);
2597 if (tv->v_type != VAR_STRING)
2598 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002599 if (iptr->isn_type == ISN_2STRING_ANY)
2600 {
2601 switch (tv->v_type)
2602 {
2603 case VAR_SPECIAL:
2604 case VAR_BOOL:
2605 case VAR_NUMBER:
2606 case VAR_FLOAT:
2607 case VAR_BLOB: break;
2608 default: to_string_error(tv->v_type);
2609 goto on_error;
2610 }
2611 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 str = typval_tostring(tv);
2613 clear_tv(tv);
2614 tv->v_type = VAR_STRING;
2615 tv->vval.v_string = str;
2616 }
2617 }
2618 break;
2619
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002620 case ISN_PUT:
2621 {
2622 int regname = iptr->isn_arg.put.put_regname;
2623 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2624 char_u *expr = NULL;
2625 int dir = FORWARD;
2626
2627 if (regname == '=')
2628 {
2629 tv = STACK_TV_BOT(-1);
2630 if (tv->v_type == VAR_STRING)
2631 expr = tv->vval.v_string;
2632 else
2633 {
2634 expr = typval_tostring(tv); // allocates value
2635 clear_tv(tv);
2636 }
2637 --ectx.ec_stack.ga_len;
2638 }
2639 if (lnum == -2)
2640 // :put! above cursor
2641 dir = BACKWARD;
2642 else if (lnum >= 0)
2643 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2644 check_cursor();
2645 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2646 vim_free(expr);
2647 }
2648 break;
2649
Bram Moolenaar389df252020-07-09 21:20:47 +02002650 case ISN_SHUFFLE:
2651 {
2652 typval_T tmp_tv;
2653 int item = iptr->isn_arg.shuffle.shfl_item;
2654 int up = iptr->isn_arg.shuffle.shfl_up;
2655
2656 tmp_tv = *STACK_TV_BOT(-item);
2657 for ( ; up > 0 && item > 1; --up)
2658 {
2659 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2660 --item;
2661 }
2662 *STACK_TV_BOT(-item) = tmp_tv;
2663 }
2664 break;
2665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 case ISN_DROP:
2667 --ectx.ec_stack.ga_len;
2668 clear_tv(STACK_TV_BOT(0));
2669 break;
2670 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002671 continue;
2672
Bram Moolenaard032f342020-07-18 18:13:02 +02002673func_return:
2674 // Restore previous function. If the frame pointer is zero then there
2675 // is none and we are done.
2676 if (ectx.ec_frame_idx == initial_frame_idx)
2677 {
2678 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2679 // only fails when out of memory
2680 goto failed;
2681 goto done;
2682 }
2683 if (func_return(&ectx) == FAIL)
2684 // only fails when out of memory
2685 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002686 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002687
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002688on_error:
2689 if (trylevel == 0)
2690 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 }
2692
2693done:
2694 // function finished, get result from the stack.
2695 tv = STACK_TV_BOT(-1);
2696 *rettv = *tv;
2697 tv->v_type = VAR_UNKNOWN;
2698 ret = OK;
2699
2700failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002701 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002702 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002703 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002704
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002705 estack_pop();
2706 current_sctx = save_current_sctx;
2707
2708failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002709 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2711 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002714 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002715
2716 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002717 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002718 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 return ret;
2720}
2721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722/*
2723 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002724 * We don't really need this at runtime, but we do have tests that require it,
2725 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 */
2727 void
2728ex_disassemble(exarg_T *eap)
2729{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002730 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002731 char_u *fname;
2732 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 dfunc_T *dfunc;
2734 isn_T *instr;
2735 int current;
2736 int line_idx = 0;
2737 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002738 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002740 if (STRNCMP(arg, "<lambda>", 8) == 0)
2741 {
2742 arg += 8;
2743 (void)getdigits(&arg);
2744 fname = vim_strnsave(eap->arg, arg - eap->arg);
2745 }
2746 else
2747 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002748 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002749 if (fname == NULL)
2750 {
2751 semsg(_(e_invarg2), eap->arg);
2752 return;
2753 }
2754
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002755 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002756 if (ufunc == NULL)
2757 {
2758 char_u *p = untrans_function_name(fname);
2759
2760 if (p != NULL)
2761 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002762 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002763 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002764 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 if (ufunc == NULL)
2766 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002767 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 return;
2769 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002770 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002771 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2772 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002773 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002775 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 return;
2777 }
2778 if (ufunc->uf_name_exp != NULL)
2779 msg((char *)ufunc->uf_name_exp);
2780 else
2781 msg((char *)ufunc->uf_name);
2782
2783 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2784 instr = dfunc->df_instr;
2785 for (current = 0; current < dfunc->df_instr_count; ++current)
2786 {
2787 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002788 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789
2790 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2791 {
2792 if (current > prev_current)
2793 {
2794 msg_puts("\n\n");
2795 prev_current = current;
2796 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002797 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2798 if (line != NULL)
2799 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 }
2801
2802 switch (iptr->isn_type)
2803 {
2804 case ISN_EXEC:
2805 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2806 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002807 case ISN_EXECCONCAT:
2808 smsg("%4d EXECCONCAT %lld", current,
2809 (long long)iptr->isn_arg.number);
2810 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 case ISN_ECHO:
2812 {
2813 echo_T *echo = &iptr->isn_arg.echo;
2814
2815 smsg("%4d %s %d", current,
2816 echo->echo_with_white ? "ECHO" : "ECHON",
2817 echo->echo_count);
2818 }
2819 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002820 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002821 smsg("%4d EXECUTE %lld", current,
2822 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002823 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002824 case ISN_ECHOMSG:
2825 smsg("%4d ECHOMSG %lld", current,
2826 (long long)(iptr->isn_arg.number));
2827 break;
2828 case ISN_ECHOERR:
2829 smsg("%4d ECHOERR %lld", current,
2830 (long long)(iptr->isn_arg.number));
2831 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002833 case ISN_LOADOUTER:
2834 {
2835 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2836
2837 if (iptr->isn_arg.number < 0)
2838 smsg("%4d LOAD%s arg[%lld]", current, add,
2839 (long long)(iptr->isn_arg.number
2840 + STACK_FRAME_SIZE));
2841 else
2842 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002843 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 break;
2846 case ISN_LOADV:
2847 smsg("%4d LOADV v:%s", current,
2848 get_vim_var_name(iptr->isn_arg.number));
2849 break;
2850 case ISN_LOADSCRIPT:
2851 {
2852 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002853 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2855 + iptr->isn_arg.script.script_idx;
2856
2857 smsg("%4d LOADSCRIPT %s from %s", current,
2858 sv->sv_name, si->sn_name);
2859 }
2860 break;
2861 case ISN_LOADS:
2862 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002863 scriptitem_T *si = SCRIPT_ITEM(
2864 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865
2866 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002867 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 }
2869 break;
2870 case ISN_LOADG:
2871 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2872 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002873 case ISN_LOADB:
2874 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2875 break;
2876 case ISN_LOADW:
2877 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2878 break;
2879 case ISN_LOADT:
2880 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2881 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002882 case ISN_LOADGDICT:
2883 smsg("%4d LOAD g:", current);
2884 break;
2885 case ISN_LOADBDICT:
2886 smsg("%4d LOAD b:", current);
2887 break;
2888 case ISN_LOADWDICT:
2889 smsg("%4d LOAD w:", current);
2890 break;
2891 case ISN_LOADTDICT:
2892 smsg("%4d LOAD t:", current);
2893 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 case ISN_LOADOPT:
2895 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2896 break;
2897 case ISN_LOADENV:
2898 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2899 break;
2900 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002901 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 break;
2903
2904 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002905 case ISN_STOREOUTER:
2906 {
2907 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2908
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002909 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002910 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002911 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002912 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002913 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002914 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002917 case ISN_STOREV:
2918 smsg("%4d STOREV v:%s", current,
2919 get_vim_var_name(iptr->isn_arg.number));
2920 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002922 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2923 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002924 case ISN_STOREB:
2925 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2926 break;
2927 case ISN_STOREW:
2928 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2929 break;
2930 case ISN_STORET:
2931 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2932 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002933 case ISN_STORES:
2934 {
2935 scriptitem_T *si = SCRIPT_ITEM(
2936 iptr->isn_arg.loadstore.ls_sid);
2937
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002938 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002939 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002940 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 break;
2942 case ISN_STORESCRIPT:
2943 {
2944 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002945 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2947 + iptr->isn_arg.script.script_idx;
2948
2949 smsg("%4d STORESCRIPT %s in %s", current,
2950 sv->sv_name, si->sn_name);
2951 }
2952 break;
2953 case ISN_STOREOPT:
2954 smsg("%4d STOREOPT &%s", current,
2955 iptr->isn_arg.storeopt.so_name);
2956 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002957 case ISN_STOREENV:
2958 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2959 break;
2960 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002961 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002962 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 case ISN_STORENR:
2964 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002965 iptr->isn_arg.storenr.stnr_val,
2966 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 break;
2968
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002969 case ISN_STORELIST:
2970 smsg("%4d STORELIST", current);
2971 break;
2972
2973 case ISN_STOREDICT:
2974 smsg("%4d STOREDICT", current);
2975 break;
2976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 // constants
2978 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002979 smsg("%4d PUSHNR %lld", current,
2980 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 break;
2982 case ISN_PUSHBOOL:
2983 case ISN_PUSHSPEC:
2984 smsg("%4d PUSH %s", current,
2985 get_var_special_name(iptr->isn_arg.number));
2986 break;
2987 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002988#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002990#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 break;
2992 case ISN_PUSHS:
2993 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2994 break;
2995 case ISN_PUSHBLOB:
2996 {
2997 char_u *r;
2998 char_u numbuf[NUMBUFLEN];
2999 char_u *tofree;
3000
3001 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003002 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 vim_free(tofree);
3004 }
3005 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003006 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003007 {
3008 char *name = (char *)iptr->isn_arg.string;
3009
3010 smsg("%4d PUSHFUNC \"%s\"", current,
3011 name == NULL ? "[none]" : name);
3012 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003013 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003014 case ISN_PUSHCHANNEL:
3015#ifdef FEAT_JOB_CHANNEL
3016 {
3017 channel_T *channel = iptr->isn_arg.channel;
3018
3019 smsg("%4d PUSHCHANNEL %d", current,
3020 channel == NULL ? 0 : channel->ch_id);
3021 }
3022#endif
3023 break;
3024 case ISN_PUSHJOB:
3025#ifdef FEAT_JOB_CHANNEL
3026 {
3027 typval_T tv;
3028 char_u *name;
3029
3030 tv.v_type = VAR_JOB;
3031 tv.vval.v_job = iptr->isn_arg.job;
3032 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003033 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003034 }
3035#endif
3036 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 case ISN_PUSHEXC:
3038 smsg("%4d PUSH v:exception", current);
3039 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003040 case ISN_UNLET:
3041 smsg("%4d UNLET%s %s", current,
3042 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3043 iptr->isn_arg.unlet.ul_name);
3044 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003045 case ISN_UNLETENV:
3046 smsg("%4d UNLETENV%s $%s", current,
3047 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3048 iptr->isn_arg.unlet.ul_name);
3049 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003050 case ISN_LOCKCONST:
3051 smsg("%4d LOCKCONST", current);
3052 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003054 smsg("%4d NEWLIST size %lld", current,
3055 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 break;
3057 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003058 smsg("%4d NEWDICT size %lld", current,
3059 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 break;
3061
3062 // function call
3063 case ISN_BCALL:
3064 {
3065 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3066
3067 smsg("%4d BCALL %s(argc %d)", current,
3068 internal_func_name(cbfunc->cbf_idx),
3069 cbfunc->cbf_argcount);
3070 }
3071 break;
3072 case ISN_DCALL:
3073 {
3074 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3075 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3076 + cdfunc->cdf_idx;
3077
3078 smsg("%4d DCALL %s(argc %d)", current,
3079 df->df_ufunc->uf_name_exp != NULL
3080 ? df->df_ufunc->uf_name_exp
3081 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3082 }
3083 break;
3084 case ISN_UCALL:
3085 {
3086 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3087
3088 smsg("%4d UCALL %s(argc %d)", current,
3089 cufunc->cuf_name, cufunc->cuf_argcount);
3090 }
3091 break;
3092 case ISN_PCALL:
3093 {
3094 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3095
3096 smsg("%4d PCALL%s (argc %d)", current,
3097 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3098 }
3099 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003100 case ISN_PCALL_END:
3101 smsg("%4d PCALL end", current);
3102 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 case ISN_RETURN:
3104 smsg("%4d RETURN", current);
3105 break;
3106 case ISN_FUNCREF:
3107 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003108 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003110 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003112 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003113 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 }
3115 break;
3116
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003117 case ISN_NEWFUNC:
3118 {
3119 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3120
3121 smsg("%4d NEWFUNC %s %s", current,
3122 newfunc->nf_lambda, newfunc->nf_global);
3123 }
3124 break;
3125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 case ISN_JUMP:
3127 {
3128 char *when = "?";
3129
3130 switch (iptr->isn_arg.jump.jump_when)
3131 {
3132 case JUMP_ALWAYS:
3133 when = "JUMP";
3134 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 case JUMP_AND_KEEP_IF_TRUE:
3136 when = "JUMP_AND_KEEP_IF_TRUE";
3137 break;
3138 case JUMP_IF_FALSE:
3139 when = "JUMP_IF_FALSE";
3140 break;
3141 case JUMP_AND_KEEP_IF_FALSE:
3142 when = "JUMP_AND_KEEP_IF_FALSE";
3143 break;
3144 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003145 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 iptr->isn_arg.jump.jump_where);
3147 }
3148 break;
3149
3150 case ISN_FOR:
3151 {
3152 forloop_T *forloop = &iptr->isn_arg.forloop;
3153
3154 smsg("%4d FOR $%d -> %d", current,
3155 forloop->for_idx, forloop->for_end);
3156 }
3157 break;
3158
3159 case ISN_TRY:
3160 {
3161 try_T *try = &iptr->isn_arg.try;
3162
3163 smsg("%4d TRY catch -> %d, finally -> %d", current,
3164 try->try_catch, try->try_finally);
3165 }
3166 break;
3167 case ISN_CATCH:
3168 // TODO
3169 smsg("%4d CATCH", current);
3170 break;
3171 case ISN_ENDTRY:
3172 smsg("%4d ENDTRY", current);
3173 break;
3174 case ISN_THROW:
3175 smsg("%4d THROW", current);
3176 break;
3177
3178 // expression operations on number
3179 case ISN_OPNR:
3180 case ISN_OPFLOAT:
3181 case ISN_OPANY:
3182 {
3183 char *what;
3184 char *ins;
3185
3186 switch (iptr->isn_arg.op.op_type)
3187 {
3188 case EXPR_MULT: what = "*"; break;
3189 case EXPR_DIV: what = "/"; break;
3190 case EXPR_REM: what = "%"; break;
3191 case EXPR_SUB: what = "-"; break;
3192 case EXPR_ADD: what = "+"; break;
3193 default: what = "???"; break;
3194 }
3195 switch (iptr->isn_type)
3196 {
3197 case ISN_OPNR: ins = "OPNR"; break;
3198 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3199 case ISN_OPANY: ins = "OPANY"; break;
3200 default: ins = "???"; break;
3201 }
3202 smsg("%4d %s %s", current, ins, what);
3203 }
3204 break;
3205
3206 case ISN_COMPAREBOOL:
3207 case ISN_COMPARESPECIAL:
3208 case ISN_COMPARENR:
3209 case ISN_COMPAREFLOAT:
3210 case ISN_COMPARESTRING:
3211 case ISN_COMPAREBLOB:
3212 case ISN_COMPARELIST:
3213 case ISN_COMPAREDICT:
3214 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 case ISN_COMPAREANY:
3216 {
3217 char *p;
3218 char buf[10];
3219 char *type;
3220
3221 switch (iptr->isn_arg.op.op_type)
3222 {
3223 case EXPR_EQUAL: p = "=="; break;
3224 case EXPR_NEQUAL: p = "!="; break;
3225 case EXPR_GREATER: p = ">"; break;
3226 case EXPR_GEQUAL: p = ">="; break;
3227 case EXPR_SMALLER: p = "<"; break;
3228 case EXPR_SEQUAL: p = "<="; break;
3229 case EXPR_MATCH: p = "=~"; break;
3230 case EXPR_IS: p = "is"; break;
3231 case EXPR_ISNOT: p = "isnot"; break;
3232 case EXPR_NOMATCH: p = "!~"; break;
3233 default: p = "???"; break;
3234 }
3235 STRCPY(buf, p);
3236 if (iptr->isn_arg.op.op_ic == TRUE)
3237 strcat(buf, "?");
3238 switch(iptr->isn_type)
3239 {
3240 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3241 case ISN_COMPARESPECIAL:
3242 type = "COMPARESPECIAL"; break;
3243 case ISN_COMPARENR: type = "COMPARENR"; break;
3244 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3245 case ISN_COMPARESTRING:
3246 type = "COMPARESTRING"; break;
3247 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3248 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3249 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3250 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3252 default: type = "???"; break;
3253 }
3254
3255 smsg("%4d %s %s", current, type, buf);
3256 }
3257 break;
3258
3259 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3260 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3261
3262 // expression operations
3263 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003264 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003265 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003266 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003267 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003268 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3269 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003270 case ISN_SLICE: smsg("%4d SLICE %lld",
3271 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003272 case ISN_GETITEM: smsg("%4d ITEM %lld",
3273 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003274 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3275 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 iptr->isn_arg.string); break;
3277 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3278
3279 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003280 case ISN_CHECKTYPE:
3281 {
3282 char *tofree;
3283
3284 smsg("%4d CHECKTYPE %s stack[%d]", current,
3285 type_name(iptr->isn_arg.type.ct_type, &tofree),
3286 iptr->isn_arg.type.ct_off);
3287 vim_free(tofree);
3288 break;
3289 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003290 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3291 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3292 iptr->isn_arg.checklen.cl_min_len);
3293 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 case ISN_2BOOL: if (iptr->isn_arg.number)
3295 smsg("%4d INVERT (!val)", current);
3296 else
3297 smsg("%4d 2BOOL (!!val)", current);
3298 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003299 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3300 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003301 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003302 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3303 (long long)(iptr->isn_arg.number));
3304 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003305 case ISN_PUT:
3306 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3307 (long)iptr->isn_arg.put.put_lnum);
3308 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309
Bram Moolenaar389df252020-07-09 21:20:47 +02003310 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3311 iptr->isn_arg.shuffle.shfl_item,
3312 iptr->isn_arg.shuffle.shfl_up);
3313 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 case ISN_DROP: smsg("%4d DROP", current); break;
3315 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003316
3317 out_flush(); // output one line at a time
3318 ui_breakcheck();
3319 if (got_int)
3320 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322}
3323
3324/*
3325 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3326 * list, etc. Mostly like what JavaScript does, except that empty list and
3327 * empty dictionary are FALSE.
3328 */
3329 int
3330tv2bool(typval_T *tv)
3331{
3332 switch (tv->v_type)
3333 {
3334 case VAR_NUMBER:
3335 return tv->vval.v_number != 0;
3336 case VAR_FLOAT:
3337#ifdef FEAT_FLOAT
3338 return tv->vval.v_float != 0.0;
3339#else
3340 break;
3341#endif
3342 case VAR_PARTIAL:
3343 return tv->vval.v_partial != NULL;
3344 case VAR_FUNC:
3345 case VAR_STRING:
3346 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3347 case VAR_LIST:
3348 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3349 case VAR_DICT:
3350 return tv->vval.v_dict != NULL
3351 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3352 case VAR_BOOL:
3353 case VAR_SPECIAL:
3354 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3355 case VAR_JOB:
3356#ifdef FEAT_JOB_CHANNEL
3357 return tv->vval.v_job != NULL;
3358#else
3359 break;
3360#endif
3361 case VAR_CHANNEL:
3362#ifdef FEAT_JOB_CHANNEL
3363 return tv->vval.v_channel != NULL;
3364#else
3365 break;
3366#endif
3367 case VAR_BLOB:
3368 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3369 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003370 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 case VAR_VOID:
3372 break;
3373 }
3374 return FALSE;
3375}
3376
3377/*
3378 * If "tv" is a string give an error and return FAIL.
3379 */
3380 int
3381check_not_string(typval_T *tv)
3382{
3383 if (tv->v_type == VAR_STRING)
3384 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003385 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 clear_tv(tv);
3387 return FAIL;
3388 }
3389 return OK;
3390}
3391
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392
3393#endif // FEAT_EVAL