blob: eb900c876ff6c054f616f9aa23ba205f18179b1c [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.
73#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
74
75/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010076 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 */
78 static int
79ufunc_argcount(ufunc_T *ufunc)
80{
81 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
82}
83
84/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010085 * Set the instruction index, depending on omitted arguments, where the default
86 * values are to be computed. If all optional arguments are present, start
87 * with the function body.
88 * The expression evaluation is at the start of the instructions:
89 * 0 -> EVAL default1
90 * STORE arg[-2]
91 * 1 -> EVAL default2
92 * STORE arg[-1]
93 * 2 -> function body
94 */
95 static void
96init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
97{
98 if (ufunc->uf_def_args.ga_len == 0)
99 ectx->ec_iidx = 0;
100 else
101 {
102 int defcount = ufunc->uf_args.ga_len - argcount;
103
104 // If there is a varargs argument defcount can be negative, no defaults
105 // to evaluate then.
106 if (defcount < 0)
107 defcount = 0;
108 ectx->ec_iidx = ufunc->uf_def_arg_idx[
109 ufunc->uf_def_args.ga_len - defcount];
110 }
111}
112
113/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200114 * Create a new list from "count" items at the bottom of the stack.
115 * When "count" is zero an empty list is added to the stack.
116 */
117 static int
118exe_newlist(int count, ectx_T *ectx)
119{
120 list_T *list = list_alloc_with_items(count);
121 int idx;
122 typval_T *tv;
123
124 if (list == NULL)
125 return FAIL;
126 for (idx = 0; idx < count; ++idx)
127 list_set_item(list, idx, STACK_TV_BOT(idx - count));
128
129 if (count > 0)
130 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200131 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200132 return FAIL;
133 else
134 ++ectx->ec_stack.ga_len;
135 tv = STACK_TV_BOT(-1);
136 tv->v_type = VAR_LIST;
137 tv->vval.v_list = list;
138 ++list->lv_refcount;
139 return OK;
140}
141
142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 * Call compiled function "cdf_idx" from compiled code.
144 *
145 * Stack has:
146 * - current arguments (already there)
147 * - omitted optional argument (default values) added here
148 * - stack frame:
149 * - pointer to calling function
150 * - Index of next instruction in calling function
151 * - previous frame pointer
152 * - reserved space for local variables
153 */
154 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200155call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100156{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200157 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
159 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200160 int arg_to_add;
161 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200163 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
165 if (dfunc->df_deleted)
166 {
167 emsg_funcname(e_func_deleted, ufunc->uf_name);
168 return FAIL;
169 }
170
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200171 if (ufunc->uf_va_name != NULL)
172 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200173 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200174 // Stack at time of call with 2 varargs:
175 // normal_arg
176 // optional_arg
177 // vararg_1
178 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 // After creating the list:
180 // normal_arg
181 // optional_arg
182 // vararg-list
183 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200184 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200185 // After creating the list
186 // normal_arg
187 // (space for optional_arg)
188 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200189 vararg_count = argcount - ufunc->uf_args.ga_len;
190 if (vararg_count < 0)
191 vararg_count = 0;
192 else
193 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196
197 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 }
199
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200201 if (arg_to_add < 0)
202 {
203 iemsg("Argument count wrong?");
204 return FAIL;
205 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200206 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
207 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208 return FAIL;
209
Bram Moolenaarfe270812020-04-11 22:31:27 +0200210 // Move the vararg-list to below the missing optional arguments.
211 if (vararg_count > 0 && arg_to_add > 0)
212 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100213
214 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200215 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200216 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200217 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218
219 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100220 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
221 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200222 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
223 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224
225 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200226 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200228 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
229 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230
231 // Set execution state to the start of the called function.
232 ectx->ec_dfunc_idx = cdf_idx;
233 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200234 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
235 if (entry != NULL)
236 {
237 // Set the script context to the script where the function was defined.
238 // TODO: save more than the SID?
239 entry->es_save_sid = current_sctx.sc_sid;
240 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
241 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100242
243 // Decide where to start execution, handles optional arguments.
244 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245
246 return OK;
247}
248
249// Get pointer to item in the stack.
250#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
251
252/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200253 * Used when returning from a function: Check if any closure is still
254 * referenced. If so then move the arguments and variables to a separate piece
255 * of stack to be used when the closure is called.
256 * When "free_arguments" is TRUE the arguments are to be freed.
257 * Returns FAIL when out of memory.
258 */
259 static int
260handle_closure_in_use(ectx_T *ectx, int free_arguments)
261{
262 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
263 + ectx->ec_dfunc_idx;
264 int argcount = ufunc_argcount(dfunc->df_ufunc);
265 int top = ectx->ec_frame_idx - argcount;
266 int idx;
267 typval_T *tv;
268 int closure_in_use = FALSE;
269
270 // Check if any created closure is still in use.
271 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
272 {
273 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
274 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200275 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
276 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200277 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200278 int refcount = tv->vval.v_partial->pt_refcount;
279 int i;
280
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200281 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200282 // unreferenced on return.
283 for (i = 0; i < dfunc->df_varcount; ++i)
284 {
285 typval_T *stv = STACK_TV(ectx->ec_frame_idx
286 + STACK_FRAME_SIZE + i);
287 if (stv->v_type == VAR_PARTIAL
288 && tv->vval.v_partial == stv->vval.v_partial)
289 --refcount;
290 }
291 if (refcount > 1)
292 {
293 closure_in_use = TRUE;
294 break;
295 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200296 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200297 }
298
299 if (closure_in_use)
300 {
301 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
302 typval_T *stack;
303
304 // A closure is using the arguments and/or local variables.
305 // Move them to the called function.
306 if (funcstack == NULL)
307 return FAIL;
308 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
309 + dfunc->df_varcount;
310 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
311 funcstack->fs_ga.ga_data = stack;
312 if (stack == NULL)
313 {
314 vim_free(funcstack);
315 return FAIL;
316 }
317
318 // Move or copy the arguments.
319 for (idx = 0; idx < argcount; ++idx)
320 {
321 tv = STACK_TV(top + idx);
322 if (free_arguments)
323 {
324 *(stack + idx) = *tv;
325 tv->v_type = VAR_UNKNOWN;
326 }
327 else
328 copy_tv(tv, stack + idx);
329 }
330 // Move the local variables.
331 for (idx = 0; idx < dfunc->df_varcount; ++idx)
332 {
333 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200334
335 // Do not copy a partial created for a local function.
336 // TODO: this won't work if the closure actually uses it. But when
337 // keeping it it gets complicated: it will create a reference cycle
338 // inside the partial, thus needs special handling for garbage
339 // collection.
340 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
341 {
342 int i;
343 typval_T *ctv;
344
345 for (i = 0; i < dfunc->df_closure_count; ++i)
346 {
347 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
348 + dfunc->df_varcount + i);
349 if (tv->vval.v_partial == ctv->vval.v_partial)
350 break;
351 }
352 if (i < dfunc->df_closure_count)
353 {
354 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
355 VAR_UNKNOWN;
356 continue;
357 }
358 }
359
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200360 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
361 tv->v_type = VAR_UNKNOWN;
362 }
363
364 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
365 {
366 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
367 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200368 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200369 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200370 partial_T *partial = tv->vval.v_partial;
371
372 if (partial->pt_refcount > 1)
373 {
374 ++funcstack->fs_refcount;
375 partial->pt_funcstack = funcstack;
376 partial->pt_ectx_stack = &funcstack->fs_ga;
377 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
378 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200379 }
380 }
381 }
382
383 return OK;
384}
385
386/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387 * Return from the current function.
388 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200389 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390func_return(ectx_T *ectx)
391{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200393 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
394 + ectx->ec_dfunc_idx;
395 int argcount = ufunc_argcount(dfunc->df_ufunc);
396 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200397 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398
399 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200400 entry = estack_pop();
401 if (entry != NULL)
402 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404 if (handle_closure_in_use(ectx, TRUE) == FAIL)
405 return FAIL;
406
407 // Clear the arguments.
408 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
409 clear_tv(STACK_TV(idx));
410
411 // Clear local variables and temp values, but not the return value.
412 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100413 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100415
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100416 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
418 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
419 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
421 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100422
423 // Reset the stack to the position before the call, move the return value
424 // to the top of the stack.
425 idx = ectx->ec_stack.ga_len - 1;
426 ectx->ec_stack.ga_len = top + 1;
427 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200428
429 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430}
431
432#undef STACK_TV
433
434/*
435 * Prepare arguments and rettv for calling a builtin or user function.
436 */
437 static int
438call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
439{
440 int idx;
441 typval_T *tv;
442
443 // Move arguments from bottom of the stack to argvars[] and add terminator.
444 for (idx = 0; idx < argcount; ++idx)
445 argvars[idx] = *STACK_TV_BOT(idx - argcount);
446 argvars[argcount].v_type = VAR_UNKNOWN;
447
448 // Result replaces the arguments on the stack.
449 if (argcount > 0)
450 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200451 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452 return FAIL;
453 else
454 ++ectx->ec_stack.ga_len;
455
456 // Default return value is zero.
457 tv = STACK_TV_BOT(-1);
458 tv->v_type = VAR_NUMBER;
459 tv->vval.v_number = 0;
460
461 return OK;
462}
463
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200464// Ugly global to avoid passing the execution context around through many
465// layers.
466static ectx_T *current_ectx = NULL;
467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468/*
469 * Call a builtin function by index.
470 */
471 static int
472call_bfunc(int func_idx, int argcount, ectx_T *ectx)
473{
474 typval_T argvars[MAX_FUNC_ARGS];
475 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200476 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200477 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478
479 if (call_prepare(argcount, argvars, ectx) == FAIL)
480 return FAIL;
481
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200482 // Call the builtin function. Set "current_ectx" so that when it
483 // recursively invokes call_def_function() a closure context can be set.
484 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200486 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
488 // Clear the arguments.
489 for (idx = 0; idx < argcount; ++idx)
490 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200491
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200492 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200493 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 return OK;
495}
496
497/*
498 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100499 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 */
501 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100502call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503{
504 typval_T argvars[MAX_FUNC_ARGS];
505 funcexe_T funcexe;
506 int error;
507 int idx;
508
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200509 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200510 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
511 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200512 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100513 {
514 // The function has been compiled, can call it quickly. For a function
515 // that was defined later: we can call it directly next time.
516 if (iptr != NULL)
517 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100518 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100519 iptr->isn_type = ISN_DCALL;
520 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
521 iptr->isn_arg.dfunc.cdf_argcount = argcount;
522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100525
526 if (call_prepare(argcount, argvars, ectx) == FAIL)
527 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200528 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 funcexe.evaluate = TRUE;
530
531 // Call the user function. Result goes in last position on the stack.
532 // TODO: add selfdict if there is one
533 error = call_user_func_check(ufunc, argcount, argvars,
534 STACK_TV_BOT(-1), &funcexe, NULL);
535
536 // Clear the arguments.
537 for (idx = 0; idx < argcount; ++idx)
538 clear_tv(&argvars[idx]);
539
540 if (error != FCERR_NONE)
541 {
542 user_func_error(error, ufunc->uf_name);
543 return FAIL;
544 }
545 return OK;
546}
547
548/*
549 * Execute a function by "name".
550 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100551 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 * Returns FAIL if not found without an error message.
553 */
554 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100555call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556{
557 ufunc_T *ufunc;
558
559 if (builtin_function(name, -1))
560 {
561 int func_idx = find_internal_func(name);
562
563 if (func_idx < 0)
564 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200565 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 return FAIL;
567 return call_bfunc(func_idx, argcount, ectx);
568 }
569
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200570 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100572 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573
574 return FAIL;
575}
576
577 static int
578call_partial(typval_T *tv, int argcount, ectx_T *ectx)
579{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200580 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 int called_emsg_before = called_emsg;
582
583 if (tv->v_type == VAR_PARTIAL)
584 {
585 partial_T *pt = tv->vval.v_partial;
586
587 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200588 {
589 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
590
591 // closure may need the function context where it was defined
592 ectx->ec_outer_stack = pt->pt_ectx_stack;
593 ectx->ec_outer_frame = pt->pt_ectx_frame;
594
595 return ret;
596 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 name = pt->pt_name;
598 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200599 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200601 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 {
603 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200604 semsg(_(e_unknownfunc),
605 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 return FAIL;
607 }
608 return OK;
609}
610
611/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100612 * Store "tv" in variable "name".
613 * This is for s: and g: variables.
614 */
615 static void
616store_var(char_u *name, typval_T *tv)
617{
618 funccal_entry_T entry;
619
620 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200621 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100622 restore_funccal();
623}
624
Bram Moolenaard3aac292020-04-19 14:32:17 +0200625
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100626/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 * Execute a function by "name".
628 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100629 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630 */
631 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100632call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633{
634 int called_emsg_before = called_emsg;
635
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100636 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 && called_emsg == called_emsg_before)
638 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200639 dictitem_T *v;
640
641 v = find_var(name, NULL, FALSE);
642 if (v == NULL)
643 {
644 semsg(_(e_unknownfunc), name);
645 return FAIL;
646 }
647 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
648 {
649 semsg(_(e_unknownfunc), name);
650 return FAIL;
651 }
652 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 }
654 return OK;
655}
656
657/*
658 * Call a "def" function from old Vim script.
659 * Return OK or FAIL.
660 */
661 int
662call_def_function(
663 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200664 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200666 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667 typval_T *rettv) // return value
668{
669 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200670 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200671 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 typval_T *tv;
673 int idx;
674 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100675 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200676 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200677 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200678 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679
680// Get pointer to item in the stack.
681#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
682
683// Get pointer to item at the bottom of the stack, -1 is the bottom.
684#undef STACK_TV_BOT
685#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
686
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200687// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200688#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 +0100689
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200690// Like STACK_TV_VAR but use the outer scope
691#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
692
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200693 if (ufunc->uf_def_status == UF_NOT_COMPILED
694 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200695 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200696 {
697 if (called_emsg == called_emsg_before)
698 semsg(_("E1091: Function is not compiled: %s"),
699 ufunc->uf_name_exp == NULL
700 ? ufunc->uf_name : ufunc->uf_name_exp);
Bram Moolenaar822ba242020-05-24 23:00:18 +0200701 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200702 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200703
Bram Moolenaar09689a02020-05-09 22:50:08 +0200704 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200705 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200706 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
707 + ufunc->uf_dfunc_idx;
708 if (dfunc->df_instr == NULL)
709 return FAIL;
710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200712 CLEAR_FIELD(ectx);
713 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
714 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
715 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
716 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
718
719 // Put arguments on the stack.
720 for (idx = 0; idx < argc; ++idx)
721 {
722 copy_tv(&argv[idx], STACK_TV_BOT(0));
723 ++ectx.ec_stack.ga_len;
724 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200725
726 // Turn varargs into a list. Empty list if no args.
727 if (ufunc->uf_va_name != NULL)
728 {
729 int vararg_count = argc - ufunc->uf_args.ga_len;
730
731 if (vararg_count < 0)
732 vararg_count = 0;
733 else
734 argc -= vararg_count;
735 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200736 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200737 if (defcount > 0)
738 // Move varargs list to below missing default arguments.
739 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
740 --ectx.ec_stack.ga_len;
741 }
742
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100743 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200744 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100745 if (defcount > 0)
746 for (idx = 0; idx < defcount; ++idx)
747 {
748 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
749 ++ectx.ec_stack.ga_len;
750 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200751 if (ufunc->uf_va_name != NULL)
752 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753
754 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200755 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
756 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200758 if (partial != NULL)
759 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200760 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
761 {
762 // TODO: is this always the right way?
763 ectx.ec_outer_stack = &current_ectx->ec_stack;
764 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
765 }
766 else
767 {
768 ectx.ec_outer_stack = partial->pt_ectx_stack;
769 ectx.ec_outer_frame = partial->pt_ectx_frame;
770 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200771 }
772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 // dummy frame entries
774 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
775 {
776 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
777 ++ectx.ec_stack.ga_len;
778 }
779
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200780 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200781 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200782 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
783 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200784 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200786 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200787 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200788 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200789
790 ectx.ec_instr = dfunc->df_instr;
791 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100792
Bram Moolenaara26b9702020-04-18 19:53:28 +0200793 // Commands behave like vim9script.
794 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
795
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100796 // Decide where to start execution, handles optional arguments.
797 init_instr_idx(ufunc, argc, &ectx);
798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 for (;;)
800 {
801 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100802
Bram Moolenaar270d0382020-05-15 21:42:53 +0200803 if (++breakcheck_count >= 100)
804 {
805 line_breakcheck();
806 breakcheck_count = 0;
807 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100808 if (got_int)
809 {
810 // Turn CTRL-C into an exception.
811 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100812 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100813 goto failed;
814 did_throw = TRUE;
815 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816
Bram Moolenaara26b9702020-04-18 19:53:28 +0200817 if (did_emsg && msg_list != NULL && *msg_list != NULL)
818 {
819 // Turn an error message into an exception.
820 did_emsg = FALSE;
821 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
822 goto failed;
823 did_throw = TRUE;
824 *msg_list = NULL;
825 }
826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 if (did_throw && !ectx.ec_in_catch)
828 {
829 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100830 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831
832 // An exception jumps to the first catch, finally, or returns from
833 // the current function.
834 if (trystack->ga_len > 0)
835 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200836 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 {
838 // jump to ":catch" or ":finally"
839 ectx.ec_in_catch = TRUE;
840 ectx.ec_iidx = trycmd->tcd_catch_idx;
841 }
842 else
843 {
844 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200845 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 {
847 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200848 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849 goto failed;
850 tv = STACK_TV_BOT(0);
851 tv->v_type = VAR_NUMBER;
852 tv->vval.v_number = 0;
853 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100854 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200855 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
856 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 goto done;
858 }
859
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200860 if (func_return(&ectx) == FAIL)
861 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 }
863 continue;
864 }
865
866 iptr = &ectx.ec_instr[ectx.ec_iidx++];
867 switch (iptr->isn_type)
868 {
869 // execute Ex command line
870 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200871 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 do_cmdline_cmd(iptr->isn_arg.string);
873 break;
874
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200875 // execute Ex command from pieces on the stack
876 case ISN_EXECCONCAT:
877 {
878 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200879 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200880 int pass;
881 int i;
882 char_u *cmd = NULL;
883 char_u *str;
884
885 for (pass = 1; pass <= 2; ++pass)
886 {
887 for (i = 0; i < count; ++i)
888 {
889 tv = STACK_TV_BOT(i - count);
890 str = tv->vval.v_string;
891 if (str != NULL && *str != NUL)
892 {
893 if (pass == 2)
894 STRCPY(cmd + len, str);
895 len += STRLEN(str);
896 }
897 if (pass == 2)
898 clear_tv(tv);
899 }
900 if (pass == 1)
901 {
902 cmd = alloc(len + 1);
903 if (cmd == NULL)
904 goto failed;
905 len = 0;
906 }
907 }
908
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200909 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200910 do_cmdline_cmd(cmd);
911 vim_free(cmd);
912 }
913 break;
914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 // execute :echo {string} ...
916 case ISN_ECHO:
917 {
918 int count = iptr->isn_arg.echo.echo_count;
919 int atstart = TRUE;
920 int needclr = TRUE;
921
922 for (idx = 0; idx < count; ++idx)
923 {
924 tv = STACK_TV_BOT(idx - count);
925 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
926 &atstart, &needclr);
927 clear_tv(tv);
928 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100929 if (needclr)
930 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 ectx.ec_stack.ga_len -= count;
932 }
933 break;
934
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200935 // :execute {string} ...
936 // :echomsg {string} ...
937 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100938 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200939 case ISN_ECHOMSG:
940 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100941 {
942 int count = iptr->isn_arg.number;
943 garray_T ga;
944 char_u buf[NUMBUFLEN];
945 char_u *p;
946 int len;
947 int failed = FALSE;
948
949 ga_init2(&ga, 1, 80);
950 for (idx = 0; idx < count; ++idx)
951 {
952 tv = STACK_TV_BOT(idx - count);
953 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
954 {
955 emsg(_(e_inval_string));
956 break;
957 }
958 else
959 p = tv_get_string_buf(tv, buf);
960
961 len = (int)STRLEN(p);
962 if (ga_grow(&ga, len + 2) == FAIL)
963 failed = TRUE;
964 else
965 {
966 if (ga.ga_len > 0)
967 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
968 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
969 ga.ga_len += len;
970 }
971 clear_tv(tv);
972 }
973 ectx.ec_stack.ga_len -= count;
974
975 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200976 {
977 if (iptr->isn_type == ISN_EXECUTE)
978 do_cmdline_cmd((char_u *)ga.ga_data);
979 else
980 {
981 msg_sb_eol();
982 if (iptr->isn_type == ISN_ECHOMSG)
983 {
984 msg_attr(ga.ga_data, echo_attr);
985 out_flush();
986 }
987 else
988 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200989 SOURCING_LNUM = iptr->isn_lnum;
990 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200991 }
992 }
993 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100994 ga_clear(&ga);
995 }
996 break;
997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 // load local variable or argument
999 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001000 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 goto failed;
1002 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1003 ++ectx.ec_stack.ga_len;
1004 break;
1005
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001006 // load variable or argument from outer scope
1007 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001008 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001009 goto failed;
1010 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1011 STACK_TV_BOT(0));
1012 ++ectx.ec_stack.ga_len;
1013 break;
1014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015 // load v: variable
1016 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001017 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 goto failed;
1019 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1020 ++ectx.ec_stack.ga_len;
1021 break;
1022
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001023 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 case ISN_LOADSCRIPT:
1025 {
1026 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001027 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 svar_T *sv;
1029
1030 sv = ((svar_T *)si->sn_var_vals.ga_data)
1031 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001032 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 goto failed;
1034 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1035 ++ectx.ec_stack.ga_len;
1036 }
1037 break;
1038
1039 // load s: variable in old script
1040 case ISN_LOADS:
1041 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001042 hashtab_T *ht = &SCRIPT_VARS(
1043 iptr->isn_arg.loadstore.ls_sid);
1044 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047 if (di == NULL)
1048 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001049 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 goto failed;
1051 }
1052 else
1053 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001054 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 goto failed;
1056 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1057 ++ectx.ec_stack.ga_len;
1058 }
1059 }
1060 break;
1061
Bram Moolenaard3aac292020-04-19 14:32:17 +02001062 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001064 case ISN_LOADB:
1065 case ISN_LOADW:
1066 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001068 dictitem_T *di = NULL;
1069 hashtab_T *ht = NULL;
1070 char namespace;
1071 switch (iptr->isn_type)
1072 {
1073 case ISN_LOADG:
1074 ht = get_globvar_ht();
1075 namespace = 'g';
1076 break;
1077 case ISN_LOADB:
1078 ht = &curbuf->b_vars->dv_hashtab;
1079 namespace = 'b';
1080 break;
1081 case ISN_LOADW:
1082 ht = &curwin->w_vars->dv_hashtab;
1083 namespace = 'w';
1084 break;
1085 case ISN_LOADT:
1086 ht = &curtab->tp_vars->dv_hashtab;
1087 namespace = 't';
1088 break;
1089 default: // Cannot reach here
1090 goto failed;
1091 }
1092 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 if (di == NULL)
1095 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001096 semsg(_("E121: Undefined variable: %c:%s"),
1097 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098 goto failed;
1099 }
1100 else
1101 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001102 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 goto failed;
1104 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1105 ++ectx.ec_stack.ga_len;
1106 }
1107 }
1108 break;
1109
1110 // load &option
1111 case ISN_LOADOPT:
1112 {
1113 typval_T optval;
1114 char_u *name = iptr->isn_arg.string;
1115
Bram Moolenaara8c17702020-04-01 21:17:24 +02001116 // This is not expected to fail, name is checked during
1117 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001118 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001120 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001121 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 *STACK_TV_BOT(0) = optval;
1123 ++ectx.ec_stack.ga_len;
1124 }
1125 break;
1126
1127 // load $ENV
1128 case ISN_LOADENV:
1129 {
1130 typval_T optval;
1131 char_u *name = iptr->isn_arg.string;
1132
Bram Moolenaar270d0382020-05-15 21:42:53 +02001133 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001135 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001136 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 *STACK_TV_BOT(0) = optval;
1138 ++ectx.ec_stack.ga_len;
1139 }
1140 break;
1141
1142 // load @register
1143 case ISN_LOADREG:
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 tv = STACK_TV_BOT(0);
1147 tv->v_type = VAR_STRING;
1148 tv->vval.v_string = get_reg_contents(
1149 iptr->isn_arg.number, GREG_EXPR_SRC);
1150 ++ectx.ec_stack.ga_len;
1151 break;
1152
1153 // store local variable
1154 case ISN_STORE:
1155 --ectx.ec_stack.ga_len;
1156 tv = STACK_TV_VAR(iptr->isn_arg.number);
1157 clear_tv(tv);
1158 *tv = *STACK_TV_BOT(0);
1159 break;
1160
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001161 // store variable or argument in outer scope
1162 case ISN_STOREOUTER:
1163 --ectx.ec_stack.ga_len;
1164 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1165 clear_tv(tv);
1166 *tv = *STACK_TV_BOT(0);
1167 break;
1168
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001169 // store s: variable in old script
1170 case ISN_STORES:
1171 {
1172 hashtab_T *ht = &SCRIPT_VARS(
1173 iptr->isn_arg.loadstore.ls_sid);
1174 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001175 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001176
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001177 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001178 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001179 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001180 else
1181 {
1182 clear_tv(&di->di_tv);
1183 di->di_tv = *STACK_TV_BOT(0);
1184 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001185 }
1186 break;
1187
1188 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 case ISN_STORESCRIPT:
1190 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001191 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 iptr->isn_arg.script.script_sid);
1193 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1194 + iptr->isn_arg.script.script_idx;
1195
1196 --ectx.ec_stack.ga_len;
1197 clear_tv(sv->sv_tv);
1198 *sv->sv_tv = *STACK_TV_BOT(0);
1199 }
1200 break;
1201
1202 // store option
1203 case ISN_STOREOPT:
1204 {
1205 long n = 0;
1206 char_u *s = NULL;
1207 char *msg;
1208
1209 --ectx.ec_stack.ga_len;
1210 tv = STACK_TV_BOT(0);
1211 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001212 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001214 if (s == NULL)
1215 s = (char_u *)"";
1216 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001218 // must be VAR_NUMBER, CHECKTYPE makes sure
1219 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1221 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001222 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 if (msg != NULL)
1224 {
1225 emsg(_(msg));
1226 goto failed;
1227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228 }
1229 break;
1230
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001231 // store $ENV
1232 case ISN_STOREENV:
1233 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001234 tv = STACK_TV_BOT(0);
1235 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1236 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001237 break;
1238
1239 // store @r
1240 case ISN_STOREREG:
1241 {
1242 int reg = iptr->isn_arg.number;
1243
1244 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001245 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001246 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001247 tv_get_string(tv), -1, FALSE);
1248 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001249 }
1250 break;
1251
1252 // store v: variable
1253 case ISN_STOREV:
1254 --ectx.ec_stack.ga_len;
1255 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1256 == FAIL)
1257 goto failed;
1258 break;
1259
Bram Moolenaard3aac292020-04-19 14:32:17 +02001260 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001262 case ISN_STOREB:
1263 case ISN_STOREW:
1264 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 {
1266 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001267 hashtab_T *ht;
1268 switch (iptr->isn_type)
1269 {
1270 case ISN_STOREG:
1271 ht = get_globvar_ht();
1272 break;
1273 case ISN_STOREB:
1274 ht = &curbuf->b_vars->dv_hashtab;
1275 break;
1276 case ISN_STOREW:
1277 ht = &curwin->w_vars->dv_hashtab;
1278 break;
1279 case ISN_STORET:
1280 ht = &curtab->tp_vars->dv_hashtab;
1281 break;
1282 default: // Cannot reach here
1283 goto failed;
1284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285
1286 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001287 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001289 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 else
1291 {
1292 clear_tv(&di->di_tv);
1293 di->di_tv = *STACK_TV_BOT(0);
1294 }
1295 }
1296 break;
1297
1298 // store number in local variable
1299 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001300 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 clear_tv(tv);
1302 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001303 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 break;
1305
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001306 // store value in list variable
1307 case ISN_STORELIST:
1308 {
1309 typval_T *tv_idx = STACK_TV_BOT(-2);
1310 varnumber_T lidx = tv_idx->vval.v_number;
1311 typval_T *tv_list = STACK_TV_BOT(-1);
1312 list_T *list = tv_list->vval.v_list;
1313
1314 if (lidx < 0 && list->lv_len + lidx >= 0)
1315 // negative index is relative to the end
1316 lidx = list->lv_len + lidx;
1317 if (lidx < 0 || lidx > list->lv_len)
1318 {
1319 semsg(_(e_listidx), lidx);
1320 goto failed;
1321 }
1322 tv = STACK_TV_BOT(-3);
1323 if (lidx < list->lv_len)
1324 {
1325 listitem_T *li = list_find(list, lidx);
1326
1327 // overwrite existing list item
1328 clear_tv(&li->li_tv);
1329 li->li_tv = *tv;
1330 }
1331 else
1332 {
1333 // append to list
1334 if (list_append_tv(list, tv) == FAIL)
1335 goto failed;
1336 clear_tv(tv);
1337 }
1338 clear_tv(tv_idx);
1339 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001340 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001341 }
1342 break;
1343
1344 // store value in dict variable
1345 case ISN_STOREDICT:
1346 {
1347 typval_T *tv_key = STACK_TV_BOT(-2);
1348 char_u *key = tv_key->vval.v_string;
1349 typval_T *tv_dict = STACK_TV_BOT(-1);
1350 dict_T *dict = tv_dict->vval.v_dict;
1351 dictitem_T *di;
1352
1353 if (key == NULL || *key == NUL)
1354 {
1355 emsg(_(e_emptykey));
1356 goto failed;
1357 }
1358 tv = STACK_TV_BOT(-3);
1359 di = dict_find(dict, key, -1);
1360 if (di != NULL)
1361 {
1362 clear_tv(&di->di_tv);
1363 di->di_tv = *tv;
1364 }
1365 else
1366 {
1367 // add to dict
1368 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1369 goto failed;
1370 clear_tv(tv);
1371 }
1372 clear_tv(tv_key);
1373 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001374 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001375 }
1376 break;
1377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 // push constant
1379 case ISN_PUSHNR:
1380 case ISN_PUSHBOOL:
1381 case ISN_PUSHSPEC:
1382 case ISN_PUSHF:
1383 case ISN_PUSHS:
1384 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001385 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001386 case ISN_PUSHCHANNEL:
1387 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001388 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 goto failed;
1390 tv = STACK_TV_BOT(0);
1391 ++ectx.ec_stack.ga_len;
1392 switch (iptr->isn_type)
1393 {
1394 case ISN_PUSHNR:
1395 tv->v_type = VAR_NUMBER;
1396 tv->vval.v_number = iptr->isn_arg.number;
1397 break;
1398 case ISN_PUSHBOOL:
1399 tv->v_type = VAR_BOOL;
1400 tv->vval.v_number = iptr->isn_arg.number;
1401 break;
1402 case ISN_PUSHSPEC:
1403 tv->v_type = VAR_SPECIAL;
1404 tv->vval.v_number = iptr->isn_arg.number;
1405 break;
1406#ifdef FEAT_FLOAT
1407 case ISN_PUSHF:
1408 tv->v_type = VAR_FLOAT;
1409 tv->vval.v_float = iptr->isn_arg.fnumber;
1410 break;
1411#endif
1412 case ISN_PUSHBLOB:
1413 blob_copy(iptr->isn_arg.blob, tv);
1414 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001415 case ISN_PUSHFUNC:
1416 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001417 if (iptr->isn_arg.string == NULL)
1418 tv->vval.v_string = NULL;
1419 else
1420 tv->vval.v_string =
1421 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001422 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001423 case ISN_PUSHCHANNEL:
1424#ifdef FEAT_JOB_CHANNEL
1425 tv->v_type = VAR_CHANNEL;
1426 tv->vval.v_channel = iptr->isn_arg.channel;
1427 if (tv->vval.v_channel != NULL)
1428 ++tv->vval.v_channel->ch_refcount;
1429#endif
1430 break;
1431 case ISN_PUSHJOB:
1432#ifdef FEAT_JOB_CHANNEL
1433 tv->v_type = VAR_JOB;
1434 tv->vval.v_job = iptr->isn_arg.job;
1435 if (tv->vval.v_job != NULL)
1436 ++tv->vval.v_job->jv_refcount;
1437#endif
1438 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 default:
1440 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001441 tv->vval.v_string = vim_strsave(
1442 iptr->isn_arg.string == NULL
1443 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 }
1445 break;
1446
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001447 case ISN_UNLET:
1448 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1449 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1450 goto failed;
1451 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001452 case ISN_UNLETENV:
1453 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1454 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 // create a list from items on the stack; uses a single allocation
1457 // for the list header and the items
1458 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001459 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1460 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 break;
1462
1463 // create a dict from items on the stack
1464 case ISN_NEWDICT:
1465 {
1466 int count = iptr->isn_arg.number;
1467 dict_T *dict = dict_alloc();
1468 dictitem_T *item;
1469
1470 if (dict == NULL)
1471 goto failed;
1472 for (idx = 0; idx < count; ++idx)
1473 {
1474 // check key type is VAR_STRING
1475 tv = STACK_TV_BOT(2 * (idx - count));
1476 item = dictitem_alloc(tv->vval.v_string);
1477 clear_tv(tv);
1478 if (item == NULL)
1479 goto failed;
1480 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1481 item->di_tv.v_lock = 0;
1482 if (dict_add(dict, item) == FAIL)
1483 goto failed;
1484 }
1485
1486 if (count > 0)
1487 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001488 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 goto failed;
1490 else
1491 ++ectx.ec_stack.ga_len;
1492 tv = STACK_TV_BOT(-1);
1493 tv->v_type = VAR_DICT;
1494 tv->vval.v_dict = dict;
1495 ++dict->dv_refcount;
1496 }
1497 break;
1498
1499 // call a :def function
1500 case ISN_DCALL:
1501 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1502 iptr->isn_arg.dfunc.cdf_argcount,
1503 &ectx) == FAIL)
1504 goto failed;
1505 break;
1506
1507 // call a builtin function
1508 case ISN_BCALL:
1509 SOURCING_LNUM = iptr->isn_lnum;
1510 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1511 iptr->isn_arg.bfunc.cbf_argcount,
1512 &ectx) == FAIL)
1513 goto failed;
1514 break;
1515
1516 // call a funcref or partial
1517 case ISN_PCALL:
1518 {
1519 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1520 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001521 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522
1523 SOURCING_LNUM = iptr->isn_lnum;
1524 if (pfunc->cpf_top)
1525 {
1526 // funcref is above the arguments
1527 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1528 }
1529 else
1530 {
1531 // Get the funcref from the stack.
1532 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001533 partial_tv = *STACK_TV_BOT(0);
1534 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 }
1536 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001537 if (tv == &partial_tv)
1538 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 if (r == FAIL)
1540 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 }
1542 break;
1543
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001544 case ISN_PCALL_END:
1545 // PCALL finished, arguments have been consumed and replaced by
1546 // the return value. Now clear the funcref from the stack,
1547 // and move the return value in its place.
1548 --ectx.ec_stack.ga_len;
1549 clear_tv(STACK_TV_BOT(-1));
1550 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1551 break;
1552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 // call a user defined function or funcref/partial
1554 case ISN_UCALL:
1555 {
1556 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1557
1558 SOURCING_LNUM = iptr->isn_lnum;
1559 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001560 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 goto failed;
1562 }
1563 break;
1564
1565 // return from a :def function call
1566 case ISN_RETURN:
1567 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001568 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001569 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001570
1571 if (trystack->ga_len > 0)
1572 trycmd = ((trycmd_T *)trystack->ga_data)
1573 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001574 if (trycmd != NULL
1575 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 && trycmd->tcd_finally_idx != 0)
1577 {
1578 // jump to ":finally"
1579 ectx.ec_iidx = trycmd->tcd_finally_idx;
1580 trycmd->tcd_return = TRUE;
1581 }
1582 else
1583 {
1584 // Restore previous function. If the frame pointer
1585 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001586 if (ectx.ec_frame_idx == initial_frame_idx)
1587 {
1588 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1589 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001591 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001593 if (func_return(&ectx) == FAIL)
1594 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 }
1596 }
1597 break;
1598
1599 // push a function reference to a compiled function
1600 case ISN_FUNCREF:
1601 {
1602 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001603 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604
1605 pt = ALLOC_CLEAR_ONE(partial_T);
1606 if (pt == NULL)
1607 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001608 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001609 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001610 vim_free(pt);
1611 goto failed;
1612 }
1613 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1614 + iptr->isn_arg.funcref.fr_func;
1615 pt->pt_func = pt_dfunc->df_ufunc;
1616 pt->pt_refcount = 1;
1617 ++pt_dfunc->df_ufunc->uf_refcount;
1618
1619 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1620 {
1621 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1622 + ectx.ec_dfunc_idx;
1623
1624 // The closure needs to find arguments and local
1625 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001626 pt->pt_ectx_stack = &ectx.ec_stack;
1627 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001628
1629 // If this function returns and the closure is still
1630 // used, we need to make a copy of the context
1631 // (arguments and local variables). Store a reference
1632 // to the partial so we can handle that.
1633 ++pt->pt_refcount;
1634 tv = STACK_TV_VAR(dfunc->df_varcount
1635 + iptr->isn_arg.funcref.fr_var_idx);
1636 if (tv->v_type == VAR_PARTIAL)
1637 {
1638 // TODO: use a garray_T on ectx.
1639 emsg("Multiple closures not supported yet");
1640 goto failed;
1641 }
1642 tv->v_type = VAR_PARTIAL;
1643 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001644 }
1645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 tv = STACK_TV_BOT(0);
1647 ++ectx.ec_stack.ga_len;
1648 tv->vval.v_partial = pt;
1649 tv->v_type = VAR_PARTIAL;
1650 }
1651 break;
1652
1653 // jump if a condition is met
1654 case ISN_JUMP:
1655 {
1656 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1657 int jump = TRUE;
1658
1659 if (when != JUMP_ALWAYS)
1660 {
1661 tv = STACK_TV_BOT(-1);
1662 jump = tv2bool(tv);
1663 if (when == JUMP_IF_FALSE
1664 || when == JUMP_AND_KEEP_IF_FALSE)
1665 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001666 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 {
1668 // drop the value from the stack
1669 clear_tv(tv);
1670 --ectx.ec_stack.ga_len;
1671 }
1672 }
1673 if (jump)
1674 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1675 }
1676 break;
1677
1678 // top of a for loop
1679 case ISN_FOR:
1680 {
1681 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1682 typval_T *idxtv =
1683 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1684
1685 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001686 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 goto failed;
1688 if (++idxtv->vval.v_number >= list->lv_len)
1689 // past the end of the list, jump to "endfor"
1690 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1691 else if (list->lv_first == &range_list_item)
1692 {
1693 // non-materialized range() list
1694 tv = STACK_TV_BOT(0);
1695 tv->v_type = VAR_NUMBER;
1696 tv->vval.v_number = list_find_nr(
1697 list, idxtv->vval.v_number, NULL);
1698 ++ectx.ec_stack.ga_len;
1699 }
1700 else
1701 {
1702 listitem_T *li = list_find(list, idxtv->vval.v_number);
1703
1704 if (li == NULL)
1705 goto failed;
1706 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1707 ++ectx.ec_stack.ga_len;
1708 }
1709 }
1710 break;
1711
1712 // start of ":try" block
1713 case ISN_TRY:
1714 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001715 trycmd_T *trycmd = NULL;
1716
Bram Moolenaar270d0382020-05-15 21:42:53 +02001717 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 goto failed;
1719 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1720 + ectx.ec_trystack.ga_len;
1721 ++ectx.ec_trystack.ga_len;
1722 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001723 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1725 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001726 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 }
1728 break;
1729
1730 case ISN_PUSHEXC:
1731 if (current_exception == NULL)
1732 {
1733 iemsg("Evaluating catch while current_exception is NULL");
1734 goto failed;
1735 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001736 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 goto failed;
1738 tv = STACK_TV_BOT(0);
1739 ++ectx.ec_stack.ga_len;
1740 tv->v_type = VAR_STRING;
1741 tv->vval.v_string = vim_strsave(
1742 (char_u *)current_exception->value);
1743 break;
1744
1745 case ISN_CATCH:
1746 {
1747 garray_T *trystack = &ectx.ec_trystack;
1748
1749 if (trystack->ga_len > 0)
1750 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001751 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 + trystack->ga_len - 1;
1753 trycmd->tcd_caught = TRUE;
1754 }
1755 did_emsg = got_int = did_throw = FALSE;
1756 catch_exception(current_exception);
1757 }
1758 break;
1759
1760 // end of ":try" block
1761 case ISN_ENDTRY:
1762 {
1763 garray_T *trystack = &ectx.ec_trystack;
1764
1765 if (trystack->ga_len > 0)
1766 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001767 trycmd_T *trycmd = NULL;
1768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 --trystack->ga_len;
1770 --trylevel;
1771 trycmd = ((trycmd_T *)trystack->ga_data)
1772 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001773 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 {
1775 // discard the exception
1776 if (caught_stack == current_exception)
1777 caught_stack = caught_stack->caught;
1778 discard_current_exception();
1779 }
1780
1781 if (trycmd->tcd_return)
1782 {
1783 // Restore previous function. If the frame pointer
1784 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001785 if (ectx.ec_frame_idx == initial_frame_idx)
1786 {
1787 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1788 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001792 if (func_return(&ectx) == FAIL)
1793 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 }
1795 }
1796 }
1797 break;
1798
1799 case ISN_THROW:
1800 --ectx.ec_stack.ga_len;
1801 tv = STACK_TV_BOT(0);
1802 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1803 {
1804 vim_free(tv->vval.v_string);
1805 goto failed;
1806 }
1807 did_throw = TRUE;
1808 break;
1809
1810 // compare with special values
1811 case ISN_COMPAREBOOL:
1812 case ISN_COMPARESPECIAL:
1813 {
1814 typval_T *tv1 = STACK_TV_BOT(-2);
1815 typval_T *tv2 = STACK_TV_BOT(-1);
1816 varnumber_T arg1 = tv1->vval.v_number;
1817 varnumber_T arg2 = tv2->vval.v_number;
1818 int res;
1819
1820 switch (iptr->isn_arg.op.op_type)
1821 {
1822 case EXPR_EQUAL: res = arg1 == arg2; break;
1823 case EXPR_NEQUAL: res = arg1 != arg2; break;
1824 default: res = 0; break;
1825 }
1826
1827 --ectx.ec_stack.ga_len;
1828 tv1->v_type = VAR_BOOL;
1829 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1830 }
1831 break;
1832
1833 // Operation with two number arguments
1834 case ISN_OPNR:
1835 case ISN_COMPARENR:
1836 {
1837 typval_T *tv1 = STACK_TV_BOT(-2);
1838 typval_T *tv2 = STACK_TV_BOT(-1);
1839 varnumber_T arg1 = tv1->vval.v_number;
1840 varnumber_T arg2 = tv2->vval.v_number;
1841 varnumber_T res;
1842
1843 switch (iptr->isn_arg.op.op_type)
1844 {
1845 case EXPR_MULT: res = arg1 * arg2; break;
1846 case EXPR_DIV: res = arg1 / arg2; break;
1847 case EXPR_REM: res = arg1 % arg2; break;
1848 case EXPR_SUB: res = arg1 - arg2; break;
1849 case EXPR_ADD: res = arg1 + arg2; break;
1850
1851 case EXPR_EQUAL: res = arg1 == arg2; break;
1852 case EXPR_NEQUAL: res = arg1 != arg2; break;
1853 case EXPR_GREATER: res = arg1 > arg2; break;
1854 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1855 case EXPR_SMALLER: res = arg1 < arg2; break;
1856 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1857 default: res = 0; break;
1858 }
1859
1860 --ectx.ec_stack.ga_len;
1861 if (iptr->isn_type == ISN_COMPARENR)
1862 {
1863 tv1->v_type = VAR_BOOL;
1864 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1865 }
1866 else
1867 tv1->vval.v_number = res;
1868 }
1869 break;
1870
1871 // Computation with two float arguments
1872 case ISN_OPFLOAT:
1873 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001874#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 {
1876 typval_T *tv1 = STACK_TV_BOT(-2);
1877 typval_T *tv2 = STACK_TV_BOT(-1);
1878 float_T arg1 = tv1->vval.v_float;
1879 float_T arg2 = tv2->vval.v_float;
1880 float_T res = 0;
1881 int cmp = FALSE;
1882
1883 switch (iptr->isn_arg.op.op_type)
1884 {
1885 case EXPR_MULT: res = arg1 * arg2; break;
1886 case EXPR_DIV: res = arg1 / arg2; break;
1887 case EXPR_SUB: res = arg1 - arg2; break;
1888 case EXPR_ADD: res = arg1 + arg2; break;
1889
1890 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1891 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1892 case EXPR_GREATER: cmp = arg1 > arg2; break;
1893 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1894 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1895 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1896 default: cmp = 0; break;
1897 }
1898 --ectx.ec_stack.ga_len;
1899 if (iptr->isn_type == ISN_COMPAREFLOAT)
1900 {
1901 tv1->v_type = VAR_BOOL;
1902 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1903 }
1904 else
1905 tv1->vval.v_float = res;
1906 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001907#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 break;
1909
1910 case ISN_COMPARELIST:
1911 {
1912 typval_T *tv1 = STACK_TV_BOT(-2);
1913 typval_T *tv2 = STACK_TV_BOT(-1);
1914 list_T *arg1 = tv1->vval.v_list;
1915 list_T *arg2 = tv2->vval.v_list;
1916 int cmp = FALSE;
1917 int ic = iptr->isn_arg.op.op_ic;
1918
1919 switch (iptr->isn_arg.op.op_type)
1920 {
1921 case EXPR_EQUAL: cmp =
1922 list_equal(arg1, arg2, ic, FALSE); break;
1923 case EXPR_NEQUAL: cmp =
1924 !list_equal(arg1, arg2, ic, FALSE); break;
1925 case EXPR_IS: cmp = arg1 == arg2; break;
1926 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1927 default: cmp = 0; break;
1928 }
1929 --ectx.ec_stack.ga_len;
1930 clear_tv(tv1);
1931 clear_tv(tv2);
1932 tv1->v_type = VAR_BOOL;
1933 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1934 }
1935 break;
1936
1937 case ISN_COMPAREBLOB:
1938 {
1939 typval_T *tv1 = STACK_TV_BOT(-2);
1940 typval_T *tv2 = STACK_TV_BOT(-1);
1941 blob_T *arg1 = tv1->vval.v_blob;
1942 blob_T *arg2 = tv2->vval.v_blob;
1943 int cmp = FALSE;
1944
1945 switch (iptr->isn_arg.op.op_type)
1946 {
1947 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1948 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1949 case EXPR_IS: cmp = arg1 == arg2; break;
1950 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1951 default: cmp = 0; break;
1952 }
1953 --ectx.ec_stack.ga_len;
1954 clear_tv(tv1);
1955 clear_tv(tv2);
1956 tv1->v_type = VAR_BOOL;
1957 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1958 }
1959 break;
1960
1961 // TODO: handle separately
1962 case ISN_COMPARESTRING:
1963 case ISN_COMPAREDICT:
1964 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 case ISN_COMPAREANY:
1966 {
1967 typval_T *tv1 = STACK_TV_BOT(-2);
1968 typval_T *tv2 = STACK_TV_BOT(-1);
1969 exptype_T exptype = iptr->isn_arg.op.op_type;
1970 int ic = iptr->isn_arg.op.op_ic;
1971
1972 typval_compare(tv1, tv2, exptype, ic);
1973 clear_tv(tv2);
1974 tv1->v_type = VAR_BOOL;
1975 tv1->vval.v_number = tv1->vval.v_number
1976 ? VVAL_TRUE : VVAL_FALSE;
1977 --ectx.ec_stack.ga_len;
1978 }
1979 break;
1980
1981 case ISN_ADDLIST:
1982 case ISN_ADDBLOB:
1983 {
1984 typval_T *tv1 = STACK_TV_BOT(-2);
1985 typval_T *tv2 = STACK_TV_BOT(-1);
1986
1987 if (iptr->isn_type == ISN_ADDLIST)
1988 eval_addlist(tv1, tv2);
1989 else
1990 eval_addblob(tv1, tv2);
1991 clear_tv(tv2);
1992 --ectx.ec_stack.ga_len;
1993 }
1994 break;
1995
1996 // Computation with two arguments of unknown type
1997 case ISN_OPANY:
1998 {
1999 typval_T *tv1 = STACK_TV_BOT(-2);
2000 typval_T *tv2 = STACK_TV_BOT(-1);
2001 varnumber_T n1, n2;
2002#ifdef FEAT_FLOAT
2003 float_T f1 = 0, f2 = 0;
2004#endif
2005 int error = FALSE;
2006
2007 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2008 {
2009 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2010 {
2011 eval_addlist(tv1, tv2);
2012 clear_tv(tv2);
2013 --ectx.ec_stack.ga_len;
2014 break;
2015 }
2016 else if (tv1->v_type == VAR_BLOB
2017 && tv2->v_type == VAR_BLOB)
2018 {
2019 eval_addblob(tv1, tv2);
2020 clear_tv(tv2);
2021 --ectx.ec_stack.ga_len;
2022 break;
2023 }
2024 }
2025#ifdef FEAT_FLOAT
2026 if (tv1->v_type == VAR_FLOAT)
2027 {
2028 f1 = tv1->vval.v_float;
2029 n1 = 0;
2030 }
2031 else
2032#endif
2033 {
2034 n1 = tv_get_number_chk(tv1, &error);
2035 if (error)
2036 goto failed;
2037#ifdef FEAT_FLOAT
2038 if (tv2->v_type == VAR_FLOAT)
2039 f1 = n1;
2040#endif
2041 }
2042#ifdef FEAT_FLOAT
2043 if (tv2->v_type == VAR_FLOAT)
2044 {
2045 f2 = tv2->vval.v_float;
2046 n2 = 0;
2047 }
2048 else
2049#endif
2050 {
2051 n2 = tv_get_number_chk(tv2, &error);
2052 if (error)
2053 goto failed;
2054#ifdef FEAT_FLOAT
2055 if (tv1->v_type == VAR_FLOAT)
2056 f2 = n2;
2057#endif
2058 }
2059#ifdef FEAT_FLOAT
2060 // if there is a float on either side the result is a float
2061 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2062 {
2063 switch (iptr->isn_arg.op.op_type)
2064 {
2065 case EXPR_MULT: f1 = f1 * f2; break;
2066 case EXPR_DIV: f1 = f1 / f2; break;
2067 case EXPR_SUB: f1 = f1 - f2; break;
2068 case EXPR_ADD: f1 = f1 + f2; break;
2069 default: emsg(_(e_modulus)); goto failed;
2070 }
2071 clear_tv(tv1);
2072 clear_tv(tv2);
2073 tv1->v_type = VAR_FLOAT;
2074 tv1->vval.v_float = f1;
2075 --ectx.ec_stack.ga_len;
2076 }
2077 else
2078#endif
2079 {
2080 switch (iptr->isn_arg.op.op_type)
2081 {
2082 case EXPR_MULT: n1 = n1 * n2; break;
2083 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2084 case EXPR_SUB: n1 = n1 - n2; break;
2085 case EXPR_ADD: n1 = n1 + n2; break;
2086 default: n1 = num_modulus(n1, n2); break;
2087 }
2088 clear_tv(tv1);
2089 clear_tv(tv2);
2090 tv1->v_type = VAR_NUMBER;
2091 tv1->vval.v_number = n1;
2092 --ectx.ec_stack.ga_len;
2093 }
2094 }
2095 break;
2096
2097 case ISN_CONCAT:
2098 {
2099 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2100 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2101 char_u *res;
2102
2103 res = concat_str(str1, str2);
2104 clear_tv(STACK_TV_BOT(-2));
2105 clear_tv(STACK_TV_BOT(-1));
2106 --ectx.ec_stack.ga_len;
2107 STACK_TV_BOT(-1)->vval.v_string = res;
2108 }
2109 break;
2110
2111 case ISN_INDEX:
2112 {
2113 list_T *list;
2114 varnumber_T n;
2115 listitem_T *li;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002116 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117
2118 // list index: list is at stack-2, index at stack-1
2119 tv = STACK_TV_BOT(-2);
2120 if (tv->v_type != VAR_LIST)
2121 {
2122 emsg(_(e_listreq));
2123 goto failed;
2124 }
2125 list = tv->vval.v_list;
2126
2127 tv = STACK_TV_BOT(-1);
2128 if (tv->v_type != VAR_NUMBER)
2129 {
2130 emsg(_(e_number_exp));
2131 goto failed;
2132 }
2133 n = tv->vval.v_number;
2134 clear_tv(tv);
2135 if ((li = list_find(list, n)) == NULL)
2136 {
2137 semsg(_(e_listidx), n);
2138 goto failed;
2139 }
2140 --ectx.ec_stack.ga_len;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002141 // Clear the list after getting the item, to avoid that it
2142 // make the item invalid.
2143 tv = STACK_TV_BOT(-1);
2144 temp_tv = *tv;
2145 copy_tv(&li->li_tv, tv);
2146 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 }
2148 break;
2149
Bram Moolenaar9af78762020-06-16 11:34:42 +02002150 case ISN_SLICE:
2151 {
2152 list_T *list;
2153 int count = iptr->isn_arg.number;
2154
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002155 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002156 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002157 list = tv->vval.v_list;
2158
2159 // no error for short list, expect it to be checked earlier
2160 if (list != NULL && list->lv_len >= count)
2161 {
2162 list_T *newlist = list_slice(list,
2163 count, list->lv_len - 1);
2164
2165 if (newlist != NULL)
2166 {
2167 list_unref(list);
2168 tv->vval.v_list = newlist;
2169 ++newlist->lv_refcount;
2170 }
2171 }
2172 }
2173 break;
2174
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002175 case ISN_GETITEM:
2176 {
2177 listitem_T *li;
2178 int index = iptr->isn_arg.number;
2179
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002180 // Get list item: list is at stack-1, push item.
2181 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002182 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002183 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002184
2185 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2186 goto failed;
2187 ++ectx.ec_stack.ga_len;
2188 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2189 }
2190 break;
2191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 case ISN_MEMBER:
2193 {
2194 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002195 char_u *key;
2196 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002197 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002198
2199 // dict member: dict is at stack-2, key at stack-1
2200 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002201 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002202 dict = tv->vval.v_dict;
2203
2204 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002205 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002206 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002207
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002208 if ((di = dict_find(dict, key, -1)) == NULL)
2209 {
2210 semsg(_(e_dictkey), key);
2211 goto failed;
2212 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002213 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002214 --ectx.ec_stack.ga_len;
2215 // Clear the dict after getting the item, to avoid that it
2216 // make the item invalid.
2217 tv = STACK_TV_BOT(-1);
2218 temp_tv = *tv;
2219 copy_tv(&di->di_tv, tv);
2220 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002221 }
2222 break;
2223
2224 // dict member with string key
2225 case ISN_STRINGMEMBER:
2226 {
2227 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002229 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230
2231 tv = STACK_TV_BOT(-1);
2232 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2233 {
2234 emsg(_(e_dictreq));
2235 goto failed;
2236 }
2237 dict = tv->vval.v_dict;
2238
2239 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2240 == NULL)
2241 {
2242 semsg(_(e_dictkey), iptr->isn_arg.string);
2243 goto failed;
2244 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002245 // Clear the dict after getting the item, to avoid that it
2246 // make the item invalid.
2247 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002249 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 }
2251 break;
2252
2253 case ISN_NEGATENR:
2254 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002255 if (tv->v_type != VAR_NUMBER
2256#ifdef FEAT_FLOAT
2257 && tv->v_type != VAR_FLOAT
2258#endif
2259 )
2260 {
2261 emsg(_(e_number_exp));
2262 goto failed;
2263 }
2264#ifdef FEAT_FLOAT
2265 if (tv->v_type == VAR_FLOAT)
2266 tv->vval.v_float = -tv->vval.v_float;
2267 else
2268#endif
2269 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 break;
2271
2272 case ISN_CHECKNR:
2273 {
2274 int error = FALSE;
2275
2276 tv = STACK_TV_BOT(-1);
2277 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 (void)tv_get_number_chk(tv, &error);
2280 if (error)
2281 goto failed;
2282 }
2283 break;
2284
2285 case ISN_CHECKTYPE:
2286 {
2287 checktype_T *ct = &iptr->isn_arg.type;
2288
2289 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002290 // TODO: better type comparison
2291 if (tv->v_type != ct->ct_type
2292 && !((tv->v_type == VAR_PARTIAL
2293 && ct->ct_type == VAR_FUNC)
2294 || (tv->v_type == VAR_FUNC
2295 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 {
2297 semsg(_("E1029: Expected %s but got %s"),
2298 vartype_name(ct->ct_type),
2299 vartype_name(tv->v_type));
2300 goto failed;
2301 }
2302 }
2303 break;
2304
Bram Moolenaar9af78762020-06-16 11:34:42 +02002305 case ISN_CHECKLEN:
2306 {
2307 int min_len = iptr->isn_arg.checklen.cl_min_len;
2308 list_T *list = NULL;
2309
2310 tv = STACK_TV_BOT(-1);
2311 if (tv->v_type == VAR_LIST)
2312 list = tv->vval.v_list;
2313 if (list == NULL || list->lv_len < min_len
2314 || (list->lv_len > min_len
2315 && !iptr->isn_arg.checklen.cl_more_OK))
2316 {
2317 semsg(_("E1093: Expected %d items but got %d"),
2318 min_len, list == NULL ? 0 : list->lv_len);
2319 goto failed;
2320 }
2321 }
2322 break;
2323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 case ISN_2BOOL:
2325 {
2326 int n;
2327
2328 tv = STACK_TV_BOT(-1);
2329 n = tv2bool(tv);
2330 if (iptr->isn_arg.number) // invert
2331 n = !n;
2332 clear_tv(tv);
2333 tv->v_type = VAR_BOOL;
2334 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2335 }
2336 break;
2337
2338 case ISN_2STRING:
2339 {
2340 char_u *str;
2341
2342 tv = STACK_TV_BOT(iptr->isn_arg.number);
2343 if (tv->v_type != VAR_STRING)
2344 {
2345 str = typval_tostring(tv);
2346 clear_tv(tv);
2347 tv->v_type = VAR_STRING;
2348 tv->vval.v_string = str;
2349 }
2350 }
2351 break;
2352
Bram Moolenaar389df252020-07-09 21:20:47 +02002353 case ISN_SHUFFLE:
2354 {
2355 typval_T tmp_tv;
2356 int item = iptr->isn_arg.shuffle.shfl_item;
2357 int up = iptr->isn_arg.shuffle.shfl_up;
2358
2359 tmp_tv = *STACK_TV_BOT(-item);
2360 for ( ; up > 0 && item > 1; --up)
2361 {
2362 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2363 --item;
2364 }
2365 *STACK_TV_BOT(-item) = tmp_tv;
2366 }
2367 break;
2368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369 case ISN_DROP:
2370 --ectx.ec_stack.ga_len;
2371 clear_tv(STACK_TV_BOT(0));
2372 break;
2373 }
2374 }
2375
2376done:
2377 // function finished, get result from the stack.
2378 tv = STACK_TV_BOT(-1);
2379 *rettv = *tv;
2380 tv->v_type = VAR_UNKNOWN;
2381 ret = OK;
2382
2383failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002384 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002385 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002386 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002387failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002388 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002389
2390 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2392 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002395 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 return ret;
2397}
2398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399/*
2400 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002401 * We don't really need this at runtime, but we do have tests that require it,
2402 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 */
2404 void
2405ex_disassemble(exarg_T *eap)
2406{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002407 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002408 char_u *fname;
2409 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 dfunc_T *dfunc;
2411 isn_T *instr;
2412 int current;
2413 int line_idx = 0;
2414 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002415 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002417 if (STRNCMP(arg, "<lambda>", 8) == 0)
2418 {
2419 arg += 8;
2420 (void)getdigits(&arg);
2421 fname = vim_strnsave(eap->arg, arg - eap->arg);
2422 }
2423 else
2424 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002425 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002426 if (fname == NULL)
2427 {
2428 semsg(_(e_invarg2), eap->arg);
2429 return;
2430 }
2431
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002432 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002433 if (ufunc == NULL)
2434 {
2435 char_u *p = untrans_function_name(fname);
2436
2437 if (p != NULL)
2438 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002439 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002440 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002441 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 if (ufunc == NULL)
2443 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002444 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 return;
2446 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002447 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002448 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2449 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002450 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002452 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 return;
2454 }
2455 if (ufunc->uf_name_exp != NULL)
2456 msg((char *)ufunc->uf_name_exp);
2457 else
2458 msg((char *)ufunc->uf_name);
2459
2460 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2461 instr = dfunc->df_instr;
2462 for (current = 0; current < dfunc->df_instr_count; ++current)
2463 {
2464 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002465 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466
2467 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2468 {
2469 if (current > prev_current)
2470 {
2471 msg_puts("\n\n");
2472 prev_current = current;
2473 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002474 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2475 if (line != NULL)
2476 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 }
2478
2479 switch (iptr->isn_type)
2480 {
2481 case ISN_EXEC:
2482 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2483 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002484 case ISN_EXECCONCAT:
2485 smsg("%4d EXECCONCAT %lld", current,
2486 (long long)iptr->isn_arg.number);
2487 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 case ISN_ECHO:
2489 {
2490 echo_T *echo = &iptr->isn_arg.echo;
2491
2492 smsg("%4d %s %d", current,
2493 echo->echo_with_white ? "ECHO" : "ECHON",
2494 echo->echo_count);
2495 }
2496 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002497 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002498 smsg("%4d EXECUTE %lld", current,
2499 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002500 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002501 case ISN_ECHOMSG:
2502 smsg("%4d ECHOMSG %lld", current,
2503 (long long)(iptr->isn_arg.number));
2504 break;
2505 case ISN_ECHOERR:
2506 smsg("%4d ECHOERR %lld", current,
2507 (long long)(iptr->isn_arg.number));
2508 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002510 case ISN_LOADOUTER:
2511 {
2512 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2513
2514 if (iptr->isn_arg.number < 0)
2515 smsg("%4d LOAD%s arg[%lld]", current, add,
2516 (long long)(iptr->isn_arg.number
2517 + STACK_FRAME_SIZE));
2518 else
2519 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002520 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002521 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 break;
2523 case ISN_LOADV:
2524 smsg("%4d LOADV v:%s", current,
2525 get_vim_var_name(iptr->isn_arg.number));
2526 break;
2527 case ISN_LOADSCRIPT:
2528 {
2529 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002530 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2532 + iptr->isn_arg.script.script_idx;
2533
2534 smsg("%4d LOADSCRIPT %s from %s", current,
2535 sv->sv_name, si->sn_name);
2536 }
2537 break;
2538 case ISN_LOADS:
2539 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002540 scriptitem_T *si = SCRIPT_ITEM(
2541 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542
2543 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002544 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 }
2546 break;
2547 case ISN_LOADG:
2548 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2549 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002550 case ISN_LOADB:
2551 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2552 break;
2553 case ISN_LOADW:
2554 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2555 break;
2556 case ISN_LOADT:
2557 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2558 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 case ISN_LOADOPT:
2560 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2561 break;
2562 case ISN_LOADENV:
2563 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2564 break;
2565 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002566 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 break;
2568
2569 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002570 case ISN_STOREOUTER:
2571 {
2572 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2573
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002574 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002575 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002576 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002577 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002578 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002579 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002580 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002582 case ISN_STOREV:
2583 smsg("%4d STOREV v:%s", current,
2584 get_vim_var_name(iptr->isn_arg.number));
2585 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002587 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2588 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002589 case ISN_STOREB:
2590 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2591 break;
2592 case ISN_STOREW:
2593 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2594 break;
2595 case ISN_STORET:
2596 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2597 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002598 case ISN_STORES:
2599 {
2600 scriptitem_T *si = SCRIPT_ITEM(
2601 iptr->isn_arg.loadstore.ls_sid);
2602
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002603 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002604 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 break;
2607 case ISN_STORESCRIPT:
2608 {
2609 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002610 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2612 + iptr->isn_arg.script.script_idx;
2613
2614 smsg("%4d STORESCRIPT %s in %s", current,
2615 sv->sv_name, si->sn_name);
2616 }
2617 break;
2618 case ISN_STOREOPT:
2619 smsg("%4d STOREOPT &%s", current,
2620 iptr->isn_arg.storeopt.so_name);
2621 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002622 case ISN_STOREENV:
2623 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2624 break;
2625 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002626 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002627 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 case ISN_STORENR:
2629 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002630 iptr->isn_arg.storenr.stnr_val,
2631 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 break;
2633
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002634 case ISN_STORELIST:
2635 smsg("%4d STORELIST", current);
2636 break;
2637
2638 case ISN_STOREDICT:
2639 smsg("%4d STOREDICT", current);
2640 break;
2641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 // constants
2643 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002644 smsg("%4d PUSHNR %lld", current,
2645 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 break;
2647 case ISN_PUSHBOOL:
2648 case ISN_PUSHSPEC:
2649 smsg("%4d PUSH %s", current,
2650 get_var_special_name(iptr->isn_arg.number));
2651 break;
2652 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002653#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002655#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 break;
2657 case ISN_PUSHS:
2658 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2659 break;
2660 case ISN_PUSHBLOB:
2661 {
2662 char_u *r;
2663 char_u numbuf[NUMBUFLEN];
2664 char_u *tofree;
2665
2666 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002667 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 vim_free(tofree);
2669 }
2670 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002671 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002672 {
2673 char *name = (char *)iptr->isn_arg.string;
2674
2675 smsg("%4d PUSHFUNC \"%s\"", current,
2676 name == NULL ? "[none]" : name);
2677 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002678 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002679 case ISN_PUSHCHANNEL:
2680#ifdef FEAT_JOB_CHANNEL
2681 {
2682 channel_T *channel = iptr->isn_arg.channel;
2683
2684 smsg("%4d PUSHCHANNEL %d", current,
2685 channel == NULL ? 0 : channel->ch_id);
2686 }
2687#endif
2688 break;
2689 case ISN_PUSHJOB:
2690#ifdef FEAT_JOB_CHANNEL
2691 {
2692 typval_T tv;
2693 char_u *name;
2694
2695 tv.v_type = VAR_JOB;
2696 tv.vval.v_job = iptr->isn_arg.job;
2697 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002698 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002699 }
2700#endif
2701 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 case ISN_PUSHEXC:
2703 smsg("%4d PUSH v:exception", current);
2704 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002705 case ISN_UNLET:
2706 smsg("%4d UNLET%s %s", current,
2707 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2708 iptr->isn_arg.unlet.ul_name);
2709 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002710 case ISN_UNLETENV:
2711 smsg("%4d UNLETENV%s $%s", current,
2712 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2713 iptr->isn_arg.unlet.ul_name);
2714 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002716 smsg("%4d NEWLIST size %lld", current,
2717 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 break;
2719 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002720 smsg("%4d NEWDICT size %lld", current,
2721 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 break;
2723
2724 // function call
2725 case ISN_BCALL:
2726 {
2727 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2728
2729 smsg("%4d BCALL %s(argc %d)", current,
2730 internal_func_name(cbfunc->cbf_idx),
2731 cbfunc->cbf_argcount);
2732 }
2733 break;
2734 case ISN_DCALL:
2735 {
2736 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2737 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2738 + cdfunc->cdf_idx;
2739
2740 smsg("%4d DCALL %s(argc %d)", current,
2741 df->df_ufunc->uf_name_exp != NULL
2742 ? df->df_ufunc->uf_name_exp
2743 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2744 }
2745 break;
2746 case ISN_UCALL:
2747 {
2748 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2749
2750 smsg("%4d UCALL %s(argc %d)", current,
2751 cufunc->cuf_name, cufunc->cuf_argcount);
2752 }
2753 break;
2754 case ISN_PCALL:
2755 {
2756 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2757
2758 smsg("%4d PCALL%s (argc %d)", current,
2759 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2760 }
2761 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002762 case ISN_PCALL_END:
2763 smsg("%4d PCALL end", current);
2764 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 case ISN_RETURN:
2766 smsg("%4d RETURN", current);
2767 break;
2768 case ISN_FUNCREF:
2769 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002770 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002772 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002774 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002775 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 }
2777 break;
2778
2779 case ISN_JUMP:
2780 {
2781 char *when = "?";
2782
2783 switch (iptr->isn_arg.jump.jump_when)
2784 {
2785 case JUMP_ALWAYS:
2786 when = "JUMP";
2787 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 case JUMP_AND_KEEP_IF_TRUE:
2789 when = "JUMP_AND_KEEP_IF_TRUE";
2790 break;
2791 case JUMP_IF_FALSE:
2792 when = "JUMP_IF_FALSE";
2793 break;
2794 case JUMP_AND_KEEP_IF_FALSE:
2795 when = "JUMP_AND_KEEP_IF_FALSE";
2796 break;
2797 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002798 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 iptr->isn_arg.jump.jump_where);
2800 }
2801 break;
2802
2803 case ISN_FOR:
2804 {
2805 forloop_T *forloop = &iptr->isn_arg.forloop;
2806
2807 smsg("%4d FOR $%d -> %d", current,
2808 forloop->for_idx, forloop->for_end);
2809 }
2810 break;
2811
2812 case ISN_TRY:
2813 {
2814 try_T *try = &iptr->isn_arg.try;
2815
2816 smsg("%4d TRY catch -> %d, finally -> %d", current,
2817 try->try_catch, try->try_finally);
2818 }
2819 break;
2820 case ISN_CATCH:
2821 // TODO
2822 smsg("%4d CATCH", current);
2823 break;
2824 case ISN_ENDTRY:
2825 smsg("%4d ENDTRY", current);
2826 break;
2827 case ISN_THROW:
2828 smsg("%4d THROW", current);
2829 break;
2830
2831 // expression operations on number
2832 case ISN_OPNR:
2833 case ISN_OPFLOAT:
2834 case ISN_OPANY:
2835 {
2836 char *what;
2837 char *ins;
2838
2839 switch (iptr->isn_arg.op.op_type)
2840 {
2841 case EXPR_MULT: what = "*"; break;
2842 case EXPR_DIV: what = "/"; break;
2843 case EXPR_REM: what = "%"; break;
2844 case EXPR_SUB: what = "-"; break;
2845 case EXPR_ADD: what = "+"; break;
2846 default: what = "???"; break;
2847 }
2848 switch (iptr->isn_type)
2849 {
2850 case ISN_OPNR: ins = "OPNR"; break;
2851 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2852 case ISN_OPANY: ins = "OPANY"; break;
2853 default: ins = "???"; break;
2854 }
2855 smsg("%4d %s %s", current, ins, what);
2856 }
2857 break;
2858
2859 case ISN_COMPAREBOOL:
2860 case ISN_COMPARESPECIAL:
2861 case ISN_COMPARENR:
2862 case ISN_COMPAREFLOAT:
2863 case ISN_COMPARESTRING:
2864 case ISN_COMPAREBLOB:
2865 case ISN_COMPARELIST:
2866 case ISN_COMPAREDICT:
2867 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 case ISN_COMPAREANY:
2869 {
2870 char *p;
2871 char buf[10];
2872 char *type;
2873
2874 switch (iptr->isn_arg.op.op_type)
2875 {
2876 case EXPR_EQUAL: p = "=="; break;
2877 case EXPR_NEQUAL: p = "!="; break;
2878 case EXPR_GREATER: p = ">"; break;
2879 case EXPR_GEQUAL: p = ">="; break;
2880 case EXPR_SMALLER: p = "<"; break;
2881 case EXPR_SEQUAL: p = "<="; break;
2882 case EXPR_MATCH: p = "=~"; break;
2883 case EXPR_IS: p = "is"; break;
2884 case EXPR_ISNOT: p = "isnot"; break;
2885 case EXPR_NOMATCH: p = "!~"; break;
2886 default: p = "???"; break;
2887 }
2888 STRCPY(buf, p);
2889 if (iptr->isn_arg.op.op_ic == TRUE)
2890 strcat(buf, "?");
2891 switch(iptr->isn_type)
2892 {
2893 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2894 case ISN_COMPARESPECIAL:
2895 type = "COMPARESPECIAL"; break;
2896 case ISN_COMPARENR: type = "COMPARENR"; break;
2897 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2898 case ISN_COMPARESTRING:
2899 type = "COMPARESTRING"; break;
2900 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2901 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2902 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2903 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2905 default: type = "???"; break;
2906 }
2907
2908 smsg("%4d %s %s", current, type, buf);
2909 }
2910 break;
2911
2912 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2913 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2914
2915 // expression operations
2916 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2917 case ISN_INDEX: smsg("%4d INDEX", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002918 case ISN_SLICE: smsg("%4d SLICE %lld",
2919 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002920 case ISN_GETITEM: smsg("%4d ITEM %lld",
2921 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002922 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
2923 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 iptr->isn_arg.string); break;
2925 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2926
2927 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2928 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2929 vartype_name(iptr->isn_arg.type.ct_type),
2930 iptr->isn_arg.type.ct_off);
2931 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002932 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
2933 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
2934 iptr->isn_arg.checklen.cl_min_len);
2935 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 case ISN_2BOOL: if (iptr->isn_arg.number)
2937 smsg("%4d INVERT (!val)", current);
2938 else
2939 smsg("%4d 2BOOL (!!val)", current);
2940 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002941 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2942 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02002943 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944
Bram Moolenaar389df252020-07-09 21:20:47 +02002945 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
2946 iptr->isn_arg.shuffle.shfl_item,
2947 iptr->isn_arg.shuffle.shfl_up);
2948 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 case ISN_DROP: smsg("%4d DROP", current); break;
2950 }
2951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952}
2953
2954/*
2955 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2956 * list, etc. Mostly like what JavaScript does, except that empty list and
2957 * empty dictionary are FALSE.
2958 */
2959 int
2960tv2bool(typval_T *tv)
2961{
2962 switch (tv->v_type)
2963 {
2964 case VAR_NUMBER:
2965 return tv->vval.v_number != 0;
2966 case VAR_FLOAT:
2967#ifdef FEAT_FLOAT
2968 return tv->vval.v_float != 0.0;
2969#else
2970 break;
2971#endif
2972 case VAR_PARTIAL:
2973 return tv->vval.v_partial != NULL;
2974 case VAR_FUNC:
2975 case VAR_STRING:
2976 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2977 case VAR_LIST:
2978 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2979 case VAR_DICT:
2980 return tv->vval.v_dict != NULL
2981 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2982 case VAR_BOOL:
2983 case VAR_SPECIAL:
2984 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2985 case VAR_JOB:
2986#ifdef FEAT_JOB_CHANNEL
2987 return tv->vval.v_job != NULL;
2988#else
2989 break;
2990#endif
2991 case VAR_CHANNEL:
2992#ifdef FEAT_JOB_CHANNEL
2993 return tv->vval.v_channel != NULL;
2994#else
2995 break;
2996#endif
2997 case VAR_BLOB:
2998 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2999 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003000 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 case VAR_VOID:
3002 break;
3003 }
3004 return FALSE;
3005}
3006
3007/*
3008 * If "tv" is a string give an error and return FAIL.
3009 */
3010 int
3011check_not_string(typval_T *tv)
3012{
3013 if (tv->v_type == VAR_STRING)
3014 {
3015 emsg(_("E1030: Using a String as a Number"));
3016 clear_tv(tv);
3017 return FAIL;
3018 }
3019 return OK;
3020}
3021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022
3023#endif // FEAT_EVAL