blob: dcc982805ecb8ec9e0aa6b1dcfd67f61b0ce1a4b [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;
163
164 if (dfunc->df_deleted)
165 {
166 emsg_funcname(e_func_deleted, ufunc->uf_name);
167 return FAIL;
168 }
169
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200170 if (ufunc->uf_va_name != NULL)
171 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200172 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200173 // Stack at time of call with 2 varargs:
174 // normal_arg
175 // optional_arg
176 // vararg_1
177 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200178 // After creating the list:
179 // normal_arg
180 // optional_arg
181 // vararg-list
182 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200183 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200184 // After creating the list
185 // normal_arg
186 // (space for optional_arg)
187 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200188 vararg_count = argcount - ufunc->uf_args.ga_len;
189 if (vararg_count < 0)
190 vararg_count = 0;
191 else
192 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200193 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200194 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200195
196 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200197 }
198
Bram Moolenaarfe270812020-04-11 22:31:27 +0200199 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200200 if (arg_to_add < 0)
201 {
202 iemsg("Argument count wrong?");
203 return FAIL;
204 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200205 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
206 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 return FAIL;
208
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209 // Move the vararg-list to below the missing optional arguments.
210 if (vararg_count > 0 && arg_to_add > 0)
211 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100212
213 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200214 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200215 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217
218 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
220 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200221 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
222 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100223
224 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200225 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200227 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
228 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229
230 // Set execution state to the start of the called function.
231 ectx->ec_dfunc_idx = cdf_idx;
232 ectx->ec_instr = dfunc->df_instr;
233 estack_push_ufunc(ETYPE_UFUNC, dfunc->df_ufunc, 1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100234
235 // Decide where to start execution, handles optional arguments.
236 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237
238 return OK;
239}
240
241// Get pointer to item in the stack.
242#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
243
244/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200245 * Used when returning from a function: Check if any closure is still
246 * referenced. If so then move the arguments and variables to a separate piece
247 * of stack to be used when the closure is called.
248 * When "free_arguments" is TRUE the arguments are to be freed.
249 * Returns FAIL when out of memory.
250 */
251 static int
252handle_closure_in_use(ectx_T *ectx, int free_arguments)
253{
254 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
255 + ectx->ec_dfunc_idx;
256 int argcount = ufunc_argcount(dfunc->df_ufunc);
257 int top = ectx->ec_frame_idx - argcount;
258 int idx;
259 typval_T *tv;
260 int closure_in_use = FALSE;
261
262 // Check if any created closure is still in use.
263 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
264 {
265 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
266 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200267 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
268 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200269 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200270 int refcount = tv->vval.v_partial->pt_refcount;
271 int i;
272
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200273 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200274 // unreferenced on return.
275 for (i = 0; i < dfunc->df_varcount; ++i)
276 {
277 typval_T *stv = STACK_TV(ectx->ec_frame_idx
278 + STACK_FRAME_SIZE + i);
279 if (stv->v_type == VAR_PARTIAL
280 && tv->vval.v_partial == stv->vval.v_partial)
281 --refcount;
282 }
283 if (refcount > 1)
284 {
285 closure_in_use = TRUE;
286 break;
287 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200288 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200289 }
290
291 if (closure_in_use)
292 {
293 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
294 typval_T *stack;
295
296 // A closure is using the arguments and/or local variables.
297 // Move them to the called function.
298 if (funcstack == NULL)
299 return FAIL;
300 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
301 + dfunc->df_varcount;
302 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
303 funcstack->fs_ga.ga_data = stack;
304 if (stack == NULL)
305 {
306 vim_free(funcstack);
307 return FAIL;
308 }
309
310 // Move or copy the arguments.
311 for (idx = 0; idx < argcount; ++idx)
312 {
313 tv = STACK_TV(top + idx);
314 if (free_arguments)
315 {
316 *(stack + idx) = *tv;
317 tv->v_type = VAR_UNKNOWN;
318 }
319 else
320 copy_tv(tv, stack + idx);
321 }
322 // Move the local variables.
323 for (idx = 0; idx < dfunc->df_varcount; ++idx)
324 {
325 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200326
327 // Do not copy a partial created for a local function.
328 // TODO: this won't work if the closure actually uses it. But when
329 // keeping it it gets complicated: it will create a reference cycle
330 // inside the partial, thus needs special handling for garbage
331 // collection.
332 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
333 {
334 int i;
335 typval_T *ctv;
336
337 for (i = 0; i < dfunc->df_closure_count; ++i)
338 {
339 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
340 + dfunc->df_varcount + i);
341 if (tv->vval.v_partial == ctv->vval.v_partial)
342 break;
343 }
344 if (i < dfunc->df_closure_count)
345 {
346 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
347 VAR_UNKNOWN;
348 continue;
349 }
350 }
351
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200352 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
353 tv->v_type = VAR_UNKNOWN;
354 }
355
356 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
357 {
358 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
359 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200360 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200361 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200362 partial_T *partial = tv->vval.v_partial;
363
364 if (partial->pt_refcount > 1)
365 {
366 ++funcstack->fs_refcount;
367 partial->pt_funcstack = funcstack;
368 partial->pt_ectx_stack = &funcstack->fs_ga;
369 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
370 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200371 }
372 }
373 }
374
375 return OK;
376}
377
378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 * Return from the current function.
380 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200381 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382func_return(ectx_T *ectx)
383{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200385 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
386 + ectx->ec_dfunc_idx;
387 int argcount = ufunc_argcount(dfunc->df_ufunc);
388 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389
390 // execution context goes one level up
391 estack_pop();
392
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200393 if (handle_closure_in_use(ectx, TRUE) == FAIL)
394 return FAIL;
395
396 // Clear the arguments.
397 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
398 clear_tv(STACK_TV(idx));
399
400 // Clear local variables and temp values, but not the return value.
401 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100402 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100404
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100405 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200406 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
407 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
408 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
410 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100411
412 // Reset the stack to the position before the call, move the return value
413 // to the top of the stack.
414 idx = ectx->ec_stack.ga_len - 1;
415 ectx->ec_stack.ga_len = top + 1;
416 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417
418 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419}
420
421#undef STACK_TV
422
423/*
424 * Prepare arguments and rettv for calling a builtin or user function.
425 */
426 static int
427call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
428{
429 int idx;
430 typval_T *tv;
431
432 // Move arguments from bottom of the stack to argvars[] and add terminator.
433 for (idx = 0; idx < argcount; ++idx)
434 argvars[idx] = *STACK_TV_BOT(idx - argcount);
435 argvars[argcount].v_type = VAR_UNKNOWN;
436
437 // Result replaces the arguments on the stack.
438 if (argcount > 0)
439 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200440 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441 return FAIL;
442 else
443 ++ectx->ec_stack.ga_len;
444
445 // Default return value is zero.
446 tv = STACK_TV_BOT(-1);
447 tv->v_type = VAR_NUMBER;
448 tv->vval.v_number = 0;
449
450 return OK;
451}
452
453/*
454 * Call a builtin function by index.
455 */
456 static int
457call_bfunc(int func_idx, int argcount, ectx_T *ectx)
458{
459 typval_T argvars[MAX_FUNC_ARGS];
460 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200461 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462
463 if (call_prepare(argcount, argvars, ectx) == FAIL)
464 return FAIL;
465
466 // Call the builtin function.
467 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
468
469 // Clear the arguments.
470 for (idx = 0; idx < argcount; ++idx)
471 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200472
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200473 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200474 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 return OK;
476}
477
478/*
479 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100480 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 */
482 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100483call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484{
485 typval_T argvars[MAX_FUNC_ARGS];
486 funcexe_T funcexe;
487 int error;
488 int idx;
489
490 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100491 {
492 // The function has been compiled, can call it quickly. For a function
493 // that was defined later: we can call it directly next time.
494 if (iptr != NULL)
495 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100496 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100497 iptr->isn_type = ISN_DCALL;
498 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
499 iptr->isn_arg.dfunc.cdf_argcount = argcount;
500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503
504 if (call_prepare(argcount, argvars, ectx) == FAIL)
505 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200506 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507 funcexe.evaluate = TRUE;
508
509 // Call the user function. Result goes in last position on the stack.
510 // TODO: add selfdict if there is one
511 error = call_user_func_check(ufunc, argcount, argvars,
512 STACK_TV_BOT(-1), &funcexe, NULL);
513
514 // Clear the arguments.
515 for (idx = 0; idx < argcount; ++idx)
516 clear_tv(&argvars[idx]);
517
518 if (error != FCERR_NONE)
519 {
520 user_func_error(error, ufunc->uf_name);
521 return FAIL;
522 }
523 return OK;
524}
525
526/*
527 * Execute a function by "name".
528 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100529 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 * Returns FAIL if not found without an error message.
531 */
532 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100533call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534{
535 ufunc_T *ufunc;
536
537 if (builtin_function(name, -1))
538 {
539 int func_idx = find_internal_func(name);
540
541 if (func_idx < 0)
542 return FAIL;
543 if (check_internal_func(func_idx, argcount) == FAIL)
544 return FAIL;
545 return call_bfunc(func_idx, argcount, ectx);
546 }
547
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200548 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100550 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551
552 return FAIL;
553}
554
555 static int
556call_partial(typval_T *tv, int argcount, ectx_T *ectx)
557{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200558 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 int called_emsg_before = called_emsg;
560
561 if (tv->v_type == VAR_PARTIAL)
562 {
563 partial_T *pt = tv->vval.v_partial;
564
565 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200566 {
567 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
568
569 // closure may need the function context where it was defined
570 ectx->ec_outer_stack = pt->pt_ectx_stack;
571 ectx->ec_outer_frame = pt->pt_ectx_frame;
572
573 return ret;
574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 name = pt->pt_name;
576 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200577 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200579 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 {
581 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200582 semsg(_(e_unknownfunc),
583 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 return FAIL;
585 }
586 return OK;
587}
588
589/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100590 * Store "tv" in variable "name".
591 * This is for s: and g: variables.
592 */
593 static void
594store_var(char_u *name, typval_T *tv)
595{
596 funccal_entry_T entry;
597
598 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200599 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100600 restore_funccal();
601}
602
Bram Moolenaard3aac292020-04-19 14:32:17 +0200603
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100604/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 * Execute a function by "name".
606 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100607 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608 */
609 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100610call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611{
612 int called_emsg_before = called_emsg;
613
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100614 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 && called_emsg == called_emsg_before)
616 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200617 dictitem_T *v;
618
619 v = find_var(name, NULL, FALSE);
620 if (v == NULL)
621 {
622 semsg(_(e_unknownfunc), name);
623 return FAIL;
624 }
625 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
626 {
627 semsg(_(e_unknownfunc), name);
628 return FAIL;
629 }
630 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100631 }
632 return OK;
633}
634
635/*
636 * Call a "def" function from old Vim script.
637 * Return OK or FAIL.
638 */
639 int
640call_def_function(
641 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200642 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 typval_T *argv, // arguments
644 typval_T *rettv) // return value
645{
646 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200647 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200648 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 typval_T *tv;
650 int idx;
651 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100652 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200653 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200654 int breakcheck_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655
656// Get pointer to item in the stack.
657#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
658
659// Get pointer to item at the bottom of the stack, -1 is the bottom.
660#undef STACK_TV_BOT
661#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
662
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200663// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200664#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 +0100665
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200666// Like STACK_TV_VAR but use the outer scope
667#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
668
Bram Moolenaar09689a02020-05-09 22:50:08 +0200669 {
670 // Check the function was compiled, it is postponed in ex_vim9script().
671 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
672 + ufunc->uf_dfunc_idx;
673 if (dfunc->df_instr == NULL)
674 return FAIL;
675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200677 CLEAR_FIELD(ectx);
678 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
679 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
680 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
681 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
683
684 // Put arguments on the stack.
685 for (idx = 0; idx < argc; ++idx)
686 {
687 copy_tv(&argv[idx], STACK_TV_BOT(0));
688 ++ectx.ec_stack.ga_len;
689 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200690
691 // Turn varargs into a list. Empty list if no args.
692 if (ufunc->uf_va_name != NULL)
693 {
694 int vararg_count = argc - ufunc->uf_args.ga_len;
695
696 if (vararg_count < 0)
697 vararg_count = 0;
698 else
699 argc -= vararg_count;
700 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200701 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200702 if (defcount > 0)
703 // Move varargs list to below missing default arguments.
704 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
705 --ectx.ec_stack.ga_len;
706 }
707
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100708 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200709 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100710 if (defcount > 0)
711 for (idx = 0; idx < defcount; ++idx)
712 {
713 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
714 ++ectx.ec_stack.ga_len;
715 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200716 if (ufunc->uf_va_name != NULL)
717 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718
719 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
721 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722
723 // dummy frame entries
724 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
725 {
726 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
727 ++ectx.ec_stack.ga_len;
728 }
729
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200730 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200731 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200732 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
733 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200734 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200736 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200737 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200738 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200739
740 ectx.ec_instr = dfunc->df_instr;
741 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100742
Bram Moolenaara26b9702020-04-18 19:53:28 +0200743 // Commands behave like vim9script.
744 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
745
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100746 // Decide where to start execution, handles optional arguments.
747 init_instr_idx(ufunc, argc, &ectx);
748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 for (;;)
750 {
751 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100752
Bram Moolenaar270d0382020-05-15 21:42:53 +0200753 if (++breakcheck_count >= 100)
754 {
755 line_breakcheck();
756 breakcheck_count = 0;
757 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100758 if (got_int)
759 {
760 // Turn CTRL-C into an exception.
761 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100762 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100763 goto failed;
764 did_throw = TRUE;
765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766
Bram Moolenaara26b9702020-04-18 19:53:28 +0200767 if (did_emsg && msg_list != NULL && *msg_list != NULL)
768 {
769 // Turn an error message into an exception.
770 did_emsg = FALSE;
771 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
772 goto failed;
773 did_throw = TRUE;
774 *msg_list = NULL;
775 }
776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 if (did_throw && !ectx.ec_in_catch)
778 {
779 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100780 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781
782 // An exception jumps to the first catch, finally, or returns from
783 // the current function.
784 if (trystack->ga_len > 0)
785 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200786 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 {
788 // jump to ":catch" or ":finally"
789 ectx.ec_in_catch = TRUE;
790 ectx.ec_iidx = trycmd->tcd_catch_idx;
791 }
792 else
793 {
794 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200795 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 {
797 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200798 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 goto failed;
800 tv = STACK_TV_BOT(0);
801 tv->v_type = VAR_NUMBER;
802 tv->vval.v_number = 0;
803 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100804 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200805 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
806 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 goto done;
808 }
809
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200810 if (func_return(&ectx) == FAIL)
811 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 }
813 continue;
814 }
815
816 iptr = &ectx.ec_instr[ectx.ec_iidx++];
817 switch (iptr->isn_type)
818 {
819 // execute Ex command line
820 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200821 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100822 do_cmdline_cmd(iptr->isn_arg.string);
823 break;
824
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200825 // execute Ex command from pieces on the stack
826 case ISN_EXECCONCAT:
827 {
828 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200829 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200830 int pass;
831 int i;
832 char_u *cmd = NULL;
833 char_u *str;
834
835 for (pass = 1; pass <= 2; ++pass)
836 {
837 for (i = 0; i < count; ++i)
838 {
839 tv = STACK_TV_BOT(i - count);
840 str = tv->vval.v_string;
841 if (str != NULL && *str != NUL)
842 {
843 if (pass == 2)
844 STRCPY(cmd + len, str);
845 len += STRLEN(str);
846 }
847 if (pass == 2)
848 clear_tv(tv);
849 }
850 if (pass == 1)
851 {
852 cmd = alloc(len + 1);
853 if (cmd == NULL)
854 goto failed;
855 len = 0;
856 }
857 }
858
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200859 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200860 do_cmdline_cmd(cmd);
861 vim_free(cmd);
862 }
863 break;
864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 // execute :echo {string} ...
866 case ISN_ECHO:
867 {
868 int count = iptr->isn_arg.echo.echo_count;
869 int atstart = TRUE;
870 int needclr = TRUE;
871
872 for (idx = 0; idx < count; ++idx)
873 {
874 tv = STACK_TV_BOT(idx - count);
875 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
876 &atstart, &needclr);
877 clear_tv(tv);
878 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100879 if (needclr)
880 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 ectx.ec_stack.ga_len -= count;
882 }
883 break;
884
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200885 // :execute {string} ...
886 // :echomsg {string} ...
887 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100888 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200889 case ISN_ECHOMSG:
890 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100891 {
892 int count = iptr->isn_arg.number;
893 garray_T ga;
894 char_u buf[NUMBUFLEN];
895 char_u *p;
896 int len;
897 int failed = FALSE;
898
899 ga_init2(&ga, 1, 80);
900 for (idx = 0; idx < count; ++idx)
901 {
902 tv = STACK_TV_BOT(idx - count);
903 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
904 {
905 emsg(_(e_inval_string));
906 break;
907 }
908 else
909 p = tv_get_string_buf(tv, buf);
910
911 len = (int)STRLEN(p);
912 if (ga_grow(&ga, len + 2) == FAIL)
913 failed = TRUE;
914 else
915 {
916 if (ga.ga_len > 0)
917 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
918 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
919 ga.ga_len += len;
920 }
921 clear_tv(tv);
922 }
923 ectx.ec_stack.ga_len -= count;
924
925 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200926 {
927 if (iptr->isn_type == ISN_EXECUTE)
928 do_cmdline_cmd((char_u *)ga.ga_data);
929 else
930 {
931 msg_sb_eol();
932 if (iptr->isn_type == ISN_ECHOMSG)
933 {
934 msg_attr(ga.ga_data, echo_attr);
935 out_flush();
936 }
937 else
938 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200939 SOURCING_LNUM = iptr->isn_lnum;
940 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200941 }
942 }
943 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100944 ga_clear(&ga);
945 }
946 break;
947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 // load local variable or argument
949 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200950 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 goto failed;
952 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
953 ++ectx.ec_stack.ga_len;
954 break;
955
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200956 // load variable or argument from outer scope
957 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200958 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200959 goto failed;
960 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
961 STACK_TV_BOT(0));
962 ++ectx.ec_stack.ga_len;
963 break;
964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965 // load v: variable
966 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200967 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 goto failed;
969 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
970 ++ectx.ec_stack.ga_len;
971 break;
972
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100973 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974 case ISN_LOADSCRIPT:
975 {
976 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100977 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 svar_T *sv;
979
980 sv = ((svar_T *)si->sn_var_vals.ga_data)
981 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200982 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 goto failed;
984 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
985 ++ectx.ec_stack.ga_len;
986 }
987 break;
988
989 // load s: variable in old script
990 case ISN_LOADS:
991 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100992 hashtab_T *ht = &SCRIPT_VARS(
993 iptr->isn_arg.loadstore.ls_sid);
994 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 if (di == NULL)
998 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100999 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000 goto failed;
1001 }
1002 else
1003 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001004 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 goto failed;
1006 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1007 ++ectx.ec_stack.ga_len;
1008 }
1009 }
1010 break;
1011
Bram Moolenaard3aac292020-04-19 14:32:17 +02001012 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001014 case ISN_LOADB:
1015 case ISN_LOADW:
1016 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001018 dictitem_T *di = NULL;
1019 hashtab_T *ht = NULL;
1020 char namespace;
1021 switch (iptr->isn_type)
1022 {
1023 case ISN_LOADG:
1024 ht = get_globvar_ht();
1025 namespace = 'g';
1026 break;
1027 case ISN_LOADB:
1028 ht = &curbuf->b_vars->dv_hashtab;
1029 namespace = 'b';
1030 break;
1031 case ISN_LOADW:
1032 ht = &curwin->w_vars->dv_hashtab;
1033 namespace = 'w';
1034 break;
1035 case ISN_LOADT:
1036 ht = &curtab->tp_vars->dv_hashtab;
1037 namespace = 't';
1038 break;
1039 default: // Cannot reach here
1040 goto failed;
1041 }
1042 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 if (di == NULL)
1045 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001046 semsg(_("E121: Undefined variable: %c:%s"),
1047 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048 goto failed;
1049 }
1050 else
1051 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001052 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 goto failed;
1054 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1055 ++ectx.ec_stack.ga_len;
1056 }
1057 }
1058 break;
1059
1060 // load &option
1061 case ISN_LOADOPT:
1062 {
1063 typval_T optval;
1064 char_u *name = iptr->isn_arg.string;
1065
Bram Moolenaara8c17702020-04-01 21:17:24 +02001066 // This is not expected to fail, name is checked during
1067 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001068 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001070 if (get_option_tv(&name, &optval, TRUE) == FAIL)
1071 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 *STACK_TV_BOT(0) = optval;
1073 ++ectx.ec_stack.ga_len;
1074 }
1075 break;
1076
1077 // load $ENV
1078 case ISN_LOADENV:
1079 {
1080 typval_T optval;
1081 char_u *name = iptr->isn_arg.string;
1082
Bram Moolenaar270d0382020-05-15 21:42:53 +02001083 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001085 // name is always valid, checked when compiling
1086 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 *STACK_TV_BOT(0) = optval;
1088 ++ectx.ec_stack.ga_len;
1089 }
1090 break;
1091
1092 // load @register
1093 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001094 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 goto failed;
1096 tv = STACK_TV_BOT(0);
1097 tv->v_type = VAR_STRING;
1098 tv->vval.v_string = get_reg_contents(
1099 iptr->isn_arg.number, GREG_EXPR_SRC);
1100 ++ectx.ec_stack.ga_len;
1101 break;
1102
1103 // store local variable
1104 case ISN_STORE:
1105 --ectx.ec_stack.ga_len;
1106 tv = STACK_TV_VAR(iptr->isn_arg.number);
1107 clear_tv(tv);
1108 *tv = *STACK_TV_BOT(0);
1109 break;
1110
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001111 // store variable or argument in outer scope
1112 case ISN_STOREOUTER:
1113 --ectx.ec_stack.ga_len;
1114 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1115 clear_tv(tv);
1116 *tv = *STACK_TV_BOT(0);
1117 break;
1118
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001119 // store s: variable in old script
1120 case ISN_STORES:
1121 {
1122 hashtab_T *ht = &SCRIPT_VARS(
1123 iptr->isn_arg.loadstore.ls_sid);
1124 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001125 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001126
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001127 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001128 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001129 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001130 else
1131 {
1132 clear_tv(&di->di_tv);
1133 di->di_tv = *STACK_TV_BOT(0);
1134 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001135 }
1136 break;
1137
1138 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 case ISN_STORESCRIPT:
1140 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001141 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 iptr->isn_arg.script.script_sid);
1143 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1144 + iptr->isn_arg.script.script_idx;
1145
1146 --ectx.ec_stack.ga_len;
1147 clear_tv(sv->sv_tv);
1148 *sv->sv_tv = *STACK_TV_BOT(0);
1149 }
1150 break;
1151
1152 // store option
1153 case ISN_STOREOPT:
1154 {
1155 long n = 0;
1156 char_u *s = NULL;
1157 char *msg;
1158
1159 --ectx.ec_stack.ga_len;
1160 tv = STACK_TV_BOT(0);
1161 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001162 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001164 if (s == NULL)
1165 s = (char_u *)"";
1166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 else if (tv->v_type == VAR_NUMBER)
1168 n = tv->vval.v_number;
1169 else
1170 {
1171 emsg(_("E1051: Expected string or number"));
1172 goto failed;
1173 }
1174 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1175 n, s, iptr->isn_arg.storeopt.so_flags);
1176 if (msg != NULL)
1177 {
1178 emsg(_(msg));
1179 goto failed;
1180 }
1181 clear_tv(tv);
1182 }
1183 break;
1184
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001185 // store $ENV
1186 case ISN_STOREENV:
1187 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001188 tv = STACK_TV_BOT(0);
1189 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1190 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001191 break;
1192
1193 // store @r
1194 case ISN_STOREREG:
1195 {
1196 int reg = iptr->isn_arg.number;
1197
1198 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001199 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001200 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001201 tv_get_string(tv), -1, FALSE);
1202 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001203 }
1204 break;
1205
1206 // store v: variable
1207 case ISN_STOREV:
1208 --ectx.ec_stack.ga_len;
1209 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1210 == FAIL)
1211 goto failed;
1212 break;
1213
Bram Moolenaard3aac292020-04-19 14:32:17 +02001214 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001216 case ISN_STOREB:
1217 case ISN_STOREW:
1218 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 {
1220 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001221 hashtab_T *ht;
1222 switch (iptr->isn_type)
1223 {
1224 case ISN_STOREG:
1225 ht = get_globvar_ht();
1226 break;
1227 case ISN_STOREB:
1228 ht = &curbuf->b_vars->dv_hashtab;
1229 break;
1230 case ISN_STOREW:
1231 ht = &curwin->w_vars->dv_hashtab;
1232 break;
1233 case ISN_STORET:
1234 ht = &curtab->tp_vars->dv_hashtab;
1235 break;
1236 default: // Cannot reach here
1237 goto failed;
1238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239
1240 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001241 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001243 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001244 else
1245 {
1246 clear_tv(&di->di_tv);
1247 di->di_tv = *STACK_TV_BOT(0);
1248 }
1249 }
1250 break;
1251
1252 // store number in local variable
1253 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001254 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 clear_tv(tv);
1256 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001257 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 break;
1259
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001260 // store value in list variable
1261 case ISN_STORELIST:
1262 {
1263 typval_T *tv_idx = STACK_TV_BOT(-2);
1264 varnumber_T lidx = tv_idx->vval.v_number;
1265 typval_T *tv_list = STACK_TV_BOT(-1);
1266 list_T *list = tv_list->vval.v_list;
1267
1268 if (lidx < 0 && list->lv_len + lidx >= 0)
1269 // negative index is relative to the end
1270 lidx = list->lv_len + lidx;
1271 if (lidx < 0 || lidx > list->lv_len)
1272 {
1273 semsg(_(e_listidx), lidx);
1274 goto failed;
1275 }
1276 tv = STACK_TV_BOT(-3);
1277 if (lidx < list->lv_len)
1278 {
1279 listitem_T *li = list_find(list, lidx);
1280
1281 // overwrite existing list item
1282 clear_tv(&li->li_tv);
1283 li->li_tv = *tv;
1284 }
1285 else
1286 {
1287 // append to list
1288 if (list_append_tv(list, tv) == FAIL)
1289 goto failed;
1290 clear_tv(tv);
1291 }
1292 clear_tv(tv_idx);
1293 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001294 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001295 }
1296 break;
1297
1298 // store value in dict variable
1299 case ISN_STOREDICT:
1300 {
1301 typval_T *tv_key = STACK_TV_BOT(-2);
1302 char_u *key = tv_key->vval.v_string;
1303 typval_T *tv_dict = STACK_TV_BOT(-1);
1304 dict_T *dict = tv_dict->vval.v_dict;
1305 dictitem_T *di;
1306
1307 if (key == NULL || *key == NUL)
1308 {
1309 emsg(_(e_emptykey));
1310 goto failed;
1311 }
1312 tv = STACK_TV_BOT(-3);
1313 di = dict_find(dict, key, -1);
1314 if (di != NULL)
1315 {
1316 clear_tv(&di->di_tv);
1317 di->di_tv = *tv;
1318 }
1319 else
1320 {
1321 // add to dict
1322 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1323 goto failed;
1324 clear_tv(tv);
1325 }
1326 clear_tv(tv_key);
1327 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001328 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001329 }
1330 break;
1331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 // push constant
1333 case ISN_PUSHNR:
1334 case ISN_PUSHBOOL:
1335 case ISN_PUSHSPEC:
1336 case ISN_PUSHF:
1337 case ISN_PUSHS:
1338 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001339 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001340 case ISN_PUSHCHANNEL:
1341 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001342 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 goto failed;
1344 tv = STACK_TV_BOT(0);
1345 ++ectx.ec_stack.ga_len;
1346 switch (iptr->isn_type)
1347 {
1348 case ISN_PUSHNR:
1349 tv->v_type = VAR_NUMBER;
1350 tv->vval.v_number = iptr->isn_arg.number;
1351 break;
1352 case ISN_PUSHBOOL:
1353 tv->v_type = VAR_BOOL;
1354 tv->vval.v_number = iptr->isn_arg.number;
1355 break;
1356 case ISN_PUSHSPEC:
1357 tv->v_type = VAR_SPECIAL;
1358 tv->vval.v_number = iptr->isn_arg.number;
1359 break;
1360#ifdef FEAT_FLOAT
1361 case ISN_PUSHF:
1362 tv->v_type = VAR_FLOAT;
1363 tv->vval.v_float = iptr->isn_arg.fnumber;
1364 break;
1365#endif
1366 case ISN_PUSHBLOB:
1367 blob_copy(iptr->isn_arg.blob, tv);
1368 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001369 case ISN_PUSHFUNC:
1370 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001371 if (iptr->isn_arg.string == NULL)
1372 tv->vval.v_string = NULL;
1373 else
1374 tv->vval.v_string =
1375 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001376 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001377 case ISN_PUSHCHANNEL:
1378#ifdef FEAT_JOB_CHANNEL
1379 tv->v_type = VAR_CHANNEL;
1380 tv->vval.v_channel = iptr->isn_arg.channel;
1381 if (tv->vval.v_channel != NULL)
1382 ++tv->vval.v_channel->ch_refcount;
1383#endif
1384 break;
1385 case ISN_PUSHJOB:
1386#ifdef FEAT_JOB_CHANNEL
1387 tv->v_type = VAR_JOB;
1388 tv->vval.v_job = iptr->isn_arg.job;
1389 if (tv->vval.v_job != NULL)
1390 ++tv->vval.v_job->jv_refcount;
1391#endif
1392 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 default:
1394 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001395 tv->vval.v_string = vim_strsave(
1396 iptr->isn_arg.string == NULL
1397 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 }
1399 break;
1400
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001401 case ISN_UNLET:
1402 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1403 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1404 goto failed;
1405 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001406 case ISN_UNLETENV:
1407 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1408 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 // create a list from items on the stack; uses a single allocation
1411 // for the list header and the items
1412 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001413 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1414 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 break;
1416
1417 // create a dict from items on the stack
1418 case ISN_NEWDICT:
1419 {
1420 int count = iptr->isn_arg.number;
1421 dict_T *dict = dict_alloc();
1422 dictitem_T *item;
1423
1424 if (dict == NULL)
1425 goto failed;
1426 for (idx = 0; idx < count; ++idx)
1427 {
1428 // check key type is VAR_STRING
1429 tv = STACK_TV_BOT(2 * (idx - count));
1430 item = dictitem_alloc(tv->vval.v_string);
1431 clear_tv(tv);
1432 if (item == NULL)
1433 goto failed;
1434 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1435 item->di_tv.v_lock = 0;
1436 if (dict_add(dict, item) == FAIL)
1437 goto failed;
1438 }
1439
1440 if (count > 0)
1441 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001442 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 goto failed;
1444 else
1445 ++ectx.ec_stack.ga_len;
1446 tv = STACK_TV_BOT(-1);
1447 tv->v_type = VAR_DICT;
1448 tv->vval.v_dict = dict;
1449 ++dict->dv_refcount;
1450 }
1451 break;
1452
1453 // call a :def function
1454 case ISN_DCALL:
1455 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1456 iptr->isn_arg.dfunc.cdf_argcount,
1457 &ectx) == FAIL)
1458 goto failed;
1459 break;
1460
1461 // call a builtin function
1462 case ISN_BCALL:
1463 SOURCING_LNUM = iptr->isn_lnum;
1464 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1465 iptr->isn_arg.bfunc.cbf_argcount,
1466 &ectx) == FAIL)
1467 goto failed;
1468 break;
1469
1470 // call a funcref or partial
1471 case ISN_PCALL:
1472 {
1473 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1474 int r;
1475 typval_T partial;
1476
1477 SOURCING_LNUM = iptr->isn_lnum;
1478 if (pfunc->cpf_top)
1479 {
1480 // funcref is above the arguments
1481 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1482 }
1483 else
1484 {
1485 // Get the funcref from the stack.
1486 --ectx.ec_stack.ga_len;
1487 partial = *STACK_TV_BOT(0);
1488 tv = &partial;
1489 }
1490 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1491 if (tv == &partial)
1492 clear_tv(&partial);
1493 if (r == FAIL)
1494 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 }
1496 break;
1497
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001498 case ISN_PCALL_END:
1499 // PCALL finished, arguments have been consumed and replaced by
1500 // the return value. Now clear the funcref from the stack,
1501 // and move the return value in its place.
1502 --ectx.ec_stack.ga_len;
1503 clear_tv(STACK_TV_BOT(-1));
1504 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1505 break;
1506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 // call a user defined function or funcref/partial
1508 case ISN_UCALL:
1509 {
1510 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1511
1512 SOURCING_LNUM = iptr->isn_lnum;
1513 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001514 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 goto failed;
1516 }
1517 break;
1518
1519 // return from a :def function call
1520 case ISN_RETURN:
1521 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001522 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001523 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001524
1525 if (trystack->ga_len > 0)
1526 trycmd = ((trycmd_T *)trystack->ga_data)
1527 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001528 if (trycmd != NULL
1529 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 && trycmd->tcd_finally_idx != 0)
1531 {
1532 // jump to ":finally"
1533 ectx.ec_iidx = trycmd->tcd_finally_idx;
1534 trycmd->tcd_return = TRUE;
1535 }
1536 else
1537 {
1538 // Restore previous function. If the frame pointer
1539 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001540 if (ectx.ec_frame_idx == initial_frame_idx)
1541 {
1542 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1543 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001545 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001547 if (func_return(&ectx) == FAIL)
1548 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 }
1550 }
1551 break;
1552
1553 // push a function reference to a compiled function
1554 case ISN_FUNCREF:
1555 {
1556 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001557 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558
1559 pt = ALLOC_CLEAR_ONE(partial_T);
1560 if (pt == NULL)
1561 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001562 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001563 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001564 vim_free(pt);
1565 goto failed;
1566 }
1567 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1568 + iptr->isn_arg.funcref.fr_func;
1569 pt->pt_func = pt_dfunc->df_ufunc;
1570 pt->pt_refcount = 1;
1571 ++pt_dfunc->df_ufunc->uf_refcount;
1572
1573 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1574 {
1575 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1576 + ectx.ec_dfunc_idx;
1577
1578 // The closure needs to find arguments and local
1579 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001580 pt->pt_ectx_stack = &ectx.ec_stack;
1581 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001582
1583 // If this function returns and the closure is still
1584 // used, we need to make a copy of the context
1585 // (arguments and local variables). Store a reference
1586 // to the partial so we can handle that.
1587 ++pt->pt_refcount;
1588 tv = STACK_TV_VAR(dfunc->df_varcount
1589 + iptr->isn_arg.funcref.fr_var_idx);
1590 if (tv->v_type == VAR_PARTIAL)
1591 {
1592 // TODO: use a garray_T on ectx.
1593 emsg("Multiple closures not supported yet");
1594 goto failed;
1595 }
1596 tv->v_type = VAR_PARTIAL;
1597 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001598 }
1599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 tv = STACK_TV_BOT(0);
1601 ++ectx.ec_stack.ga_len;
1602 tv->vval.v_partial = pt;
1603 tv->v_type = VAR_PARTIAL;
1604 }
1605 break;
1606
1607 // jump if a condition is met
1608 case ISN_JUMP:
1609 {
1610 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1611 int jump = TRUE;
1612
1613 if (when != JUMP_ALWAYS)
1614 {
1615 tv = STACK_TV_BOT(-1);
1616 jump = tv2bool(tv);
1617 if (when == JUMP_IF_FALSE
1618 || when == JUMP_AND_KEEP_IF_FALSE)
1619 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001620 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 {
1622 // drop the value from the stack
1623 clear_tv(tv);
1624 --ectx.ec_stack.ga_len;
1625 }
1626 }
1627 if (jump)
1628 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1629 }
1630 break;
1631
1632 // top of a for loop
1633 case ISN_FOR:
1634 {
1635 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1636 typval_T *idxtv =
1637 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1638
1639 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001640 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 goto failed;
1642 if (++idxtv->vval.v_number >= list->lv_len)
1643 // past the end of the list, jump to "endfor"
1644 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1645 else if (list->lv_first == &range_list_item)
1646 {
1647 // non-materialized range() list
1648 tv = STACK_TV_BOT(0);
1649 tv->v_type = VAR_NUMBER;
1650 tv->vval.v_number = list_find_nr(
1651 list, idxtv->vval.v_number, NULL);
1652 ++ectx.ec_stack.ga_len;
1653 }
1654 else
1655 {
1656 listitem_T *li = list_find(list, idxtv->vval.v_number);
1657
1658 if (li == NULL)
1659 goto failed;
1660 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1661 ++ectx.ec_stack.ga_len;
1662 }
1663 }
1664 break;
1665
1666 // start of ":try" block
1667 case ISN_TRY:
1668 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001669 trycmd_T *trycmd = NULL;
1670
Bram Moolenaar270d0382020-05-15 21:42:53 +02001671 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 goto failed;
1673 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1674 + ectx.ec_trystack.ga_len;
1675 ++ectx.ec_trystack.ga_len;
1676 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001677 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1679 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001680 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 }
1682 break;
1683
1684 case ISN_PUSHEXC:
1685 if (current_exception == NULL)
1686 {
1687 iemsg("Evaluating catch while current_exception is NULL");
1688 goto failed;
1689 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001690 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 goto failed;
1692 tv = STACK_TV_BOT(0);
1693 ++ectx.ec_stack.ga_len;
1694 tv->v_type = VAR_STRING;
1695 tv->vval.v_string = vim_strsave(
1696 (char_u *)current_exception->value);
1697 break;
1698
1699 case ISN_CATCH:
1700 {
1701 garray_T *trystack = &ectx.ec_trystack;
1702
1703 if (trystack->ga_len > 0)
1704 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001705 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 + trystack->ga_len - 1;
1707 trycmd->tcd_caught = TRUE;
1708 }
1709 did_emsg = got_int = did_throw = FALSE;
1710 catch_exception(current_exception);
1711 }
1712 break;
1713
1714 // end of ":try" block
1715 case ISN_ENDTRY:
1716 {
1717 garray_T *trystack = &ectx.ec_trystack;
1718
1719 if (trystack->ga_len > 0)
1720 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001721 trycmd_T *trycmd = NULL;
1722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 --trystack->ga_len;
1724 --trylevel;
1725 trycmd = ((trycmd_T *)trystack->ga_data)
1726 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001727 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 {
1729 // discard the exception
1730 if (caught_stack == current_exception)
1731 caught_stack = caught_stack->caught;
1732 discard_current_exception();
1733 }
1734
1735 if (trycmd->tcd_return)
1736 {
1737 // Restore previous function. If the frame pointer
1738 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001739 if (ectx.ec_frame_idx == initial_frame_idx)
1740 {
1741 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1742 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001746 if (func_return(&ectx) == FAIL)
1747 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 }
1749 }
1750 }
1751 break;
1752
1753 case ISN_THROW:
1754 --ectx.ec_stack.ga_len;
1755 tv = STACK_TV_BOT(0);
1756 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1757 {
1758 vim_free(tv->vval.v_string);
1759 goto failed;
1760 }
1761 did_throw = TRUE;
1762 break;
1763
1764 // compare with special values
1765 case ISN_COMPAREBOOL:
1766 case ISN_COMPARESPECIAL:
1767 {
1768 typval_T *tv1 = STACK_TV_BOT(-2);
1769 typval_T *tv2 = STACK_TV_BOT(-1);
1770 varnumber_T arg1 = tv1->vval.v_number;
1771 varnumber_T arg2 = tv2->vval.v_number;
1772 int res;
1773
1774 switch (iptr->isn_arg.op.op_type)
1775 {
1776 case EXPR_EQUAL: res = arg1 == arg2; break;
1777 case EXPR_NEQUAL: res = arg1 != arg2; break;
1778 default: res = 0; break;
1779 }
1780
1781 --ectx.ec_stack.ga_len;
1782 tv1->v_type = VAR_BOOL;
1783 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1784 }
1785 break;
1786
1787 // Operation with two number arguments
1788 case ISN_OPNR:
1789 case ISN_COMPARENR:
1790 {
1791 typval_T *tv1 = STACK_TV_BOT(-2);
1792 typval_T *tv2 = STACK_TV_BOT(-1);
1793 varnumber_T arg1 = tv1->vval.v_number;
1794 varnumber_T arg2 = tv2->vval.v_number;
1795 varnumber_T res;
1796
1797 switch (iptr->isn_arg.op.op_type)
1798 {
1799 case EXPR_MULT: res = arg1 * arg2; break;
1800 case EXPR_DIV: res = arg1 / arg2; break;
1801 case EXPR_REM: res = arg1 % arg2; break;
1802 case EXPR_SUB: res = arg1 - arg2; break;
1803 case EXPR_ADD: res = arg1 + arg2; break;
1804
1805 case EXPR_EQUAL: res = arg1 == arg2; break;
1806 case EXPR_NEQUAL: res = arg1 != arg2; break;
1807 case EXPR_GREATER: res = arg1 > arg2; break;
1808 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1809 case EXPR_SMALLER: res = arg1 < arg2; break;
1810 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1811 default: res = 0; break;
1812 }
1813
1814 --ectx.ec_stack.ga_len;
1815 if (iptr->isn_type == ISN_COMPARENR)
1816 {
1817 tv1->v_type = VAR_BOOL;
1818 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1819 }
1820 else
1821 tv1->vval.v_number = res;
1822 }
1823 break;
1824
1825 // Computation with two float arguments
1826 case ISN_OPFLOAT:
1827 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001828#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 {
1830 typval_T *tv1 = STACK_TV_BOT(-2);
1831 typval_T *tv2 = STACK_TV_BOT(-1);
1832 float_T arg1 = tv1->vval.v_float;
1833 float_T arg2 = tv2->vval.v_float;
1834 float_T res = 0;
1835 int cmp = FALSE;
1836
1837 switch (iptr->isn_arg.op.op_type)
1838 {
1839 case EXPR_MULT: res = arg1 * arg2; break;
1840 case EXPR_DIV: res = arg1 / arg2; break;
1841 case EXPR_SUB: res = arg1 - arg2; break;
1842 case EXPR_ADD: res = arg1 + arg2; break;
1843
1844 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1845 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1846 case EXPR_GREATER: cmp = arg1 > arg2; break;
1847 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1848 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1849 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1850 default: cmp = 0; break;
1851 }
1852 --ectx.ec_stack.ga_len;
1853 if (iptr->isn_type == ISN_COMPAREFLOAT)
1854 {
1855 tv1->v_type = VAR_BOOL;
1856 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1857 }
1858 else
1859 tv1->vval.v_float = res;
1860 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001861#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 break;
1863
1864 case ISN_COMPARELIST:
1865 {
1866 typval_T *tv1 = STACK_TV_BOT(-2);
1867 typval_T *tv2 = STACK_TV_BOT(-1);
1868 list_T *arg1 = tv1->vval.v_list;
1869 list_T *arg2 = tv2->vval.v_list;
1870 int cmp = FALSE;
1871 int ic = iptr->isn_arg.op.op_ic;
1872
1873 switch (iptr->isn_arg.op.op_type)
1874 {
1875 case EXPR_EQUAL: cmp =
1876 list_equal(arg1, arg2, ic, FALSE); break;
1877 case EXPR_NEQUAL: cmp =
1878 !list_equal(arg1, arg2, ic, FALSE); break;
1879 case EXPR_IS: cmp = arg1 == arg2; break;
1880 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1881 default: cmp = 0; break;
1882 }
1883 --ectx.ec_stack.ga_len;
1884 clear_tv(tv1);
1885 clear_tv(tv2);
1886 tv1->v_type = VAR_BOOL;
1887 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1888 }
1889 break;
1890
1891 case ISN_COMPAREBLOB:
1892 {
1893 typval_T *tv1 = STACK_TV_BOT(-2);
1894 typval_T *tv2 = STACK_TV_BOT(-1);
1895 blob_T *arg1 = tv1->vval.v_blob;
1896 blob_T *arg2 = tv2->vval.v_blob;
1897 int cmp = FALSE;
1898
1899 switch (iptr->isn_arg.op.op_type)
1900 {
1901 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1902 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1903 case EXPR_IS: cmp = arg1 == arg2; break;
1904 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1905 default: cmp = 0; break;
1906 }
1907 --ectx.ec_stack.ga_len;
1908 clear_tv(tv1);
1909 clear_tv(tv2);
1910 tv1->v_type = VAR_BOOL;
1911 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1912 }
1913 break;
1914
1915 // TODO: handle separately
1916 case ISN_COMPARESTRING:
1917 case ISN_COMPAREDICT:
1918 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 case ISN_COMPAREANY:
1920 {
1921 typval_T *tv1 = STACK_TV_BOT(-2);
1922 typval_T *tv2 = STACK_TV_BOT(-1);
1923 exptype_T exptype = iptr->isn_arg.op.op_type;
1924 int ic = iptr->isn_arg.op.op_ic;
1925
1926 typval_compare(tv1, tv2, exptype, ic);
1927 clear_tv(tv2);
1928 tv1->v_type = VAR_BOOL;
1929 tv1->vval.v_number = tv1->vval.v_number
1930 ? VVAL_TRUE : VVAL_FALSE;
1931 --ectx.ec_stack.ga_len;
1932 }
1933 break;
1934
1935 case ISN_ADDLIST:
1936 case ISN_ADDBLOB:
1937 {
1938 typval_T *tv1 = STACK_TV_BOT(-2);
1939 typval_T *tv2 = STACK_TV_BOT(-1);
1940
1941 if (iptr->isn_type == ISN_ADDLIST)
1942 eval_addlist(tv1, tv2);
1943 else
1944 eval_addblob(tv1, tv2);
1945 clear_tv(tv2);
1946 --ectx.ec_stack.ga_len;
1947 }
1948 break;
1949
1950 // Computation with two arguments of unknown type
1951 case ISN_OPANY:
1952 {
1953 typval_T *tv1 = STACK_TV_BOT(-2);
1954 typval_T *tv2 = STACK_TV_BOT(-1);
1955 varnumber_T n1, n2;
1956#ifdef FEAT_FLOAT
1957 float_T f1 = 0, f2 = 0;
1958#endif
1959 int error = FALSE;
1960
1961 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1962 {
1963 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1964 {
1965 eval_addlist(tv1, tv2);
1966 clear_tv(tv2);
1967 --ectx.ec_stack.ga_len;
1968 break;
1969 }
1970 else if (tv1->v_type == VAR_BLOB
1971 && tv2->v_type == VAR_BLOB)
1972 {
1973 eval_addblob(tv1, tv2);
1974 clear_tv(tv2);
1975 --ectx.ec_stack.ga_len;
1976 break;
1977 }
1978 }
1979#ifdef FEAT_FLOAT
1980 if (tv1->v_type == VAR_FLOAT)
1981 {
1982 f1 = tv1->vval.v_float;
1983 n1 = 0;
1984 }
1985 else
1986#endif
1987 {
1988 n1 = tv_get_number_chk(tv1, &error);
1989 if (error)
1990 goto failed;
1991#ifdef FEAT_FLOAT
1992 if (tv2->v_type == VAR_FLOAT)
1993 f1 = n1;
1994#endif
1995 }
1996#ifdef FEAT_FLOAT
1997 if (tv2->v_type == VAR_FLOAT)
1998 {
1999 f2 = tv2->vval.v_float;
2000 n2 = 0;
2001 }
2002 else
2003#endif
2004 {
2005 n2 = tv_get_number_chk(tv2, &error);
2006 if (error)
2007 goto failed;
2008#ifdef FEAT_FLOAT
2009 if (tv1->v_type == VAR_FLOAT)
2010 f2 = n2;
2011#endif
2012 }
2013#ifdef FEAT_FLOAT
2014 // if there is a float on either side the result is a float
2015 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2016 {
2017 switch (iptr->isn_arg.op.op_type)
2018 {
2019 case EXPR_MULT: f1 = f1 * f2; break;
2020 case EXPR_DIV: f1 = f1 / f2; break;
2021 case EXPR_SUB: f1 = f1 - f2; break;
2022 case EXPR_ADD: f1 = f1 + f2; break;
2023 default: emsg(_(e_modulus)); goto failed;
2024 }
2025 clear_tv(tv1);
2026 clear_tv(tv2);
2027 tv1->v_type = VAR_FLOAT;
2028 tv1->vval.v_float = f1;
2029 --ectx.ec_stack.ga_len;
2030 }
2031 else
2032#endif
2033 {
2034 switch (iptr->isn_arg.op.op_type)
2035 {
2036 case EXPR_MULT: n1 = n1 * n2; break;
2037 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2038 case EXPR_SUB: n1 = n1 - n2; break;
2039 case EXPR_ADD: n1 = n1 + n2; break;
2040 default: n1 = num_modulus(n1, n2); break;
2041 }
2042 clear_tv(tv1);
2043 clear_tv(tv2);
2044 tv1->v_type = VAR_NUMBER;
2045 tv1->vval.v_number = n1;
2046 --ectx.ec_stack.ga_len;
2047 }
2048 }
2049 break;
2050
2051 case ISN_CONCAT:
2052 {
2053 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2054 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2055 char_u *res;
2056
2057 res = concat_str(str1, str2);
2058 clear_tv(STACK_TV_BOT(-2));
2059 clear_tv(STACK_TV_BOT(-1));
2060 --ectx.ec_stack.ga_len;
2061 STACK_TV_BOT(-1)->vval.v_string = res;
2062 }
2063 break;
2064
2065 case ISN_INDEX:
2066 {
2067 list_T *list;
2068 varnumber_T n;
2069 listitem_T *li;
2070
2071 // list index: list is at stack-2, index at stack-1
2072 tv = STACK_TV_BOT(-2);
2073 if (tv->v_type != VAR_LIST)
2074 {
2075 emsg(_(e_listreq));
2076 goto failed;
2077 }
2078 list = tv->vval.v_list;
2079
2080 tv = STACK_TV_BOT(-1);
2081 if (tv->v_type != VAR_NUMBER)
2082 {
2083 emsg(_(e_number_exp));
2084 goto failed;
2085 }
2086 n = tv->vval.v_number;
2087 clear_tv(tv);
2088 if ((li = list_find(list, n)) == NULL)
2089 {
2090 semsg(_(e_listidx), n);
2091 goto failed;
2092 }
2093 --ectx.ec_stack.ga_len;
2094 clear_tv(STACK_TV_BOT(-1));
2095 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2096 }
2097 break;
2098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 case ISN_MEMBER:
2100 {
2101 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002102 char_u *key;
2103 dictitem_T *di;
2104
2105 // dict member: dict is at stack-2, key at stack-1
2106 tv = STACK_TV_BOT(-2);
2107 if (tv->v_type != VAR_DICT)
2108 {
2109 emsg(_(e_dictreq));
2110 goto failed;
2111 }
2112 dict = tv->vval.v_dict;
2113
2114 tv = STACK_TV_BOT(-1);
2115 if (tv->v_type != VAR_STRING)
2116 {
2117 emsg(_(e_stringreq));
2118 goto failed;
2119 }
2120 key = tv->vval.v_string;
2121 if ((di = dict_find(dict, key, -1)) == NULL)
2122 {
2123 semsg(_(e_dictkey), key);
2124 goto failed;
2125 }
2126 --ectx.ec_stack.ga_len;
2127 clear_tv(tv);
2128 clear_tv(STACK_TV_BOT(-1));
2129 copy_tv(&di->di_tv, STACK_TV_BOT(-1));
2130 }
2131 break;
2132
2133 // dict member with string key
2134 case ISN_STRINGMEMBER:
2135 {
2136 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 dictitem_T *di;
2138
2139 tv = STACK_TV_BOT(-1);
2140 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2141 {
2142 emsg(_(e_dictreq));
2143 goto failed;
2144 }
2145 dict = tv->vval.v_dict;
2146
2147 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2148 == NULL)
2149 {
2150 semsg(_(e_dictkey), iptr->isn_arg.string);
2151 goto failed;
2152 }
2153 clear_tv(tv);
2154 copy_tv(&di->di_tv, tv);
2155 }
2156 break;
2157
2158 case ISN_NEGATENR:
2159 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002160 if (tv->v_type != VAR_NUMBER
2161#ifdef FEAT_FLOAT
2162 && tv->v_type != VAR_FLOAT
2163#endif
2164 )
2165 {
2166 emsg(_(e_number_exp));
2167 goto failed;
2168 }
2169#ifdef FEAT_FLOAT
2170 if (tv->v_type == VAR_FLOAT)
2171 tv->vval.v_float = -tv->vval.v_float;
2172 else
2173#endif
2174 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 break;
2176
2177 case ISN_CHECKNR:
2178 {
2179 int error = FALSE;
2180
2181 tv = STACK_TV_BOT(-1);
2182 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 (void)tv_get_number_chk(tv, &error);
2185 if (error)
2186 goto failed;
2187 }
2188 break;
2189
2190 case ISN_CHECKTYPE:
2191 {
2192 checktype_T *ct = &iptr->isn_arg.type;
2193
2194 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002195 // TODO: better type comparison
2196 if (tv->v_type != ct->ct_type
2197 && !((tv->v_type == VAR_PARTIAL
2198 && ct->ct_type == VAR_FUNC)
2199 || (tv->v_type == VAR_FUNC
2200 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 {
2202 semsg(_("E1029: Expected %s but got %s"),
2203 vartype_name(ct->ct_type),
2204 vartype_name(tv->v_type));
2205 goto failed;
2206 }
2207 }
2208 break;
2209
2210 case ISN_2BOOL:
2211 {
2212 int n;
2213
2214 tv = STACK_TV_BOT(-1);
2215 n = tv2bool(tv);
2216 if (iptr->isn_arg.number) // invert
2217 n = !n;
2218 clear_tv(tv);
2219 tv->v_type = VAR_BOOL;
2220 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2221 }
2222 break;
2223
2224 case ISN_2STRING:
2225 {
2226 char_u *str;
2227
2228 tv = STACK_TV_BOT(iptr->isn_arg.number);
2229 if (tv->v_type != VAR_STRING)
2230 {
2231 str = typval_tostring(tv);
2232 clear_tv(tv);
2233 tv->v_type = VAR_STRING;
2234 tv->vval.v_string = str;
2235 }
2236 }
2237 break;
2238
2239 case ISN_DROP:
2240 --ectx.ec_stack.ga_len;
2241 clear_tv(STACK_TV_BOT(0));
2242 break;
2243 }
2244 }
2245
2246done:
2247 // function finished, get result from the stack.
2248 tv = STACK_TV_BOT(-1);
2249 *rettv = *tv;
2250 tv->v_type = VAR_UNKNOWN;
2251 ret = OK;
2252
2253failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002254 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002255 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002256 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002257failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002258 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002259
2260 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2262 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002265 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 return ret;
2267}
2268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269/*
2270 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002271 * We don't really need this at runtime, but we do have tests that require it,
2272 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 */
2274 void
2275ex_disassemble(exarg_T *eap)
2276{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002277 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002278 char_u *fname;
2279 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 dfunc_T *dfunc;
2281 isn_T *instr;
2282 int current;
2283 int line_idx = 0;
2284 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002285 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002287 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002288 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002289 if (fname == NULL)
2290 {
2291 semsg(_(e_invarg2), eap->arg);
2292 return;
2293 }
2294
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002295 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002296 if (ufunc == NULL)
2297 {
2298 char_u *p = untrans_function_name(fname);
2299
2300 if (p != NULL)
2301 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002302 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002303 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002304 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 if (ufunc == NULL)
2306 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002307 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 return;
2309 }
2310 if (ufunc->uf_dfunc_idx < 0)
2311 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002312 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 return;
2314 }
2315 if (ufunc->uf_name_exp != NULL)
2316 msg((char *)ufunc->uf_name_exp);
2317 else
2318 msg((char *)ufunc->uf_name);
2319
2320 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2321 instr = dfunc->df_instr;
2322 for (current = 0; current < dfunc->df_instr_count; ++current)
2323 {
2324 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002325 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326
2327 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2328 {
2329 if (current > prev_current)
2330 {
2331 msg_puts("\n\n");
2332 prev_current = current;
2333 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002334 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2335 if (line != NULL)
2336 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 }
2338
2339 switch (iptr->isn_type)
2340 {
2341 case ISN_EXEC:
2342 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2343 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002344 case ISN_EXECCONCAT:
2345 smsg("%4d EXECCONCAT %lld", current,
2346 (long long)iptr->isn_arg.number);
2347 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 case ISN_ECHO:
2349 {
2350 echo_T *echo = &iptr->isn_arg.echo;
2351
2352 smsg("%4d %s %d", current,
2353 echo->echo_with_white ? "ECHO" : "ECHON",
2354 echo->echo_count);
2355 }
2356 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002357 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002358 smsg("%4d EXECUTE %lld", current,
2359 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002360 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002361 case ISN_ECHOMSG:
2362 smsg("%4d ECHOMSG %lld", current,
2363 (long long)(iptr->isn_arg.number));
2364 break;
2365 case ISN_ECHOERR:
2366 smsg("%4d ECHOERR %lld", current,
2367 (long long)(iptr->isn_arg.number));
2368 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002370 case ISN_LOADOUTER:
2371 {
2372 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2373
2374 if (iptr->isn_arg.number < 0)
2375 smsg("%4d LOAD%s arg[%lld]", current, add,
2376 (long long)(iptr->isn_arg.number
2377 + STACK_FRAME_SIZE));
2378 else
2379 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002380 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002381 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 break;
2383 case ISN_LOADV:
2384 smsg("%4d LOADV v:%s", current,
2385 get_vim_var_name(iptr->isn_arg.number));
2386 break;
2387 case ISN_LOADSCRIPT:
2388 {
2389 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002390 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2392 + iptr->isn_arg.script.script_idx;
2393
2394 smsg("%4d LOADSCRIPT %s from %s", current,
2395 sv->sv_name, si->sn_name);
2396 }
2397 break;
2398 case ISN_LOADS:
2399 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002400 scriptitem_T *si = SCRIPT_ITEM(
2401 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402
2403 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002404 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 }
2406 break;
2407 case ISN_LOADG:
2408 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2409 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002410 case ISN_LOADB:
2411 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2412 break;
2413 case ISN_LOADW:
2414 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2415 break;
2416 case ISN_LOADT:
2417 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2418 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 case ISN_LOADOPT:
2420 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2421 break;
2422 case ISN_LOADENV:
2423 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2424 break;
2425 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002426 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 break;
2428
2429 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002430 case ISN_STOREOUTER:
2431 {
2432 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2433
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002434 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002435 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002436 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002437 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002438 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002439 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002440 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002442 case ISN_STOREV:
2443 smsg("%4d STOREV v:%s", current,
2444 get_vim_var_name(iptr->isn_arg.number));
2445 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002447 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2448 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002449 case ISN_STOREB:
2450 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2451 break;
2452 case ISN_STOREW:
2453 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2454 break;
2455 case ISN_STORET:
2456 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2457 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002458 case ISN_STORES:
2459 {
2460 scriptitem_T *si = SCRIPT_ITEM(
2461 iptr->isn_arg.loadstore.ls_sid);
2462
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002463 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002464 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002465 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 break;
2467 case ISN_STORESCRIPT:
2468 {
2469 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002470 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2472 + iptr->isn_arg.script.script_idx;
2473
2474 smsg("%4d STORESCRIPT %s in %s", current,
2475 sv->sv_name, si->sn_name);
2476 }
2477 break;
2478 case ISN_STOREOPT:
2479 smsg("%4d STOREOPT &%s", current,
2480 iptr->isn_arg.storeopt.so_name);
2481 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002482 case ISN_STOREENV:
2483 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2484 break;
2485 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002486 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002487 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 case ISN_STORENR:
2489 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002490 iptr->isn_arg.storenr.stnr_val,
2491 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 break;
2493
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002494 case ISN_STORELIST:
2495 smsg("%4d STORELIST", current);
2496 break;
2497
2498 case ISN_STOREDICT:
2499 smsg("%4d STOREDICT", current);
2500 break;
2501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 // constants
2503 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002504 smsg("%4d PUSHNR %lld", current,
2505 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 break;
2507 case ISN_PUSHBOOL:
2508 case ISN_PUSHSPEC:
2509 smsg("%4d PUSH %s", current,
2510 get_var_special_name(iptr->isn_arg.number));
2511 break;
2512 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002513#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002515#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 break;
2517 case ISN_PUSHS:
2518 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2519 break;
2520 case ISN_PUSHBLOB:
2521 {
2522 char_u *r;
2523 char_u numbuf[NUMBUFLEN];
2524 char_u *tofree;
2525
2526 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002527 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 vim_free(tofree);
2529 }
2530 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002531 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002532 {
2533 char *name = (char *)iptr->isn_arg.string;
2534
2535 smsg("%4d PUSHFUNC \"%s\"", current,
2536 name == NULL ? "[none]" : name);
2537 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002538 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002539 case ISN_PUSHCHANNEL:
2540#ifdef FEAT_JOB_CHANNEL
2541 {
2542 channel_T *channel = iptr->isn_arg.channel;
2543
2544 smsg("%4d PUSHCHANNEL %d", current,
2545 channel == NULL ? 0 : channel->ch_id);
2546 }
2547#endif
2548 break;
2549 case ISN_PUSHJOB:
2550#ifdef FEAT_JOB_CHANNEL
2551 {
2552 typval_T tv;
2553 char_u *name;
2554
2555 tv.v_type = VAR_JOB;
2556 tv.vval.v_job = iptr->isn_arg.job;
2557 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002558 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002559 }
2560#endif
2561 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 case ISN_PUSHEXC:
2563 smsg("%4d PUSH v:exception", current);
2564 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002565 case ISN_UNLET:
2566 smsg("%4d UNLET%s %s", current,
2567 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2568 iptr->isn_arg.unlet.ul_name);
2569 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002570 case ISN_UNLETENV:
2571 smsg("%4d UNLETENV%s $%s", current,
2572 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2573 iptr->isn_arg.unlet.ul_name);
2574 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002576 smsg("%4d NEWLIST size %lld", current,
2577 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 break;
2579 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002580 smsg("%4d NEWDICT size %lld", current,
2581 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 break;
2583
2584 // function call
2585 case ISN_BCALL:
2586 {
2587 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2588
2589 smsg("%4d BCALL %s(argc %d)", current,
2590 internal_func_name(cbfunc->cbf_idx),
2591 cbfunc->cbf_argcount);
2592 }
2593 break;
2594 case ISN_DCALL:
2595 {
2596 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2597 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2598 + cdfunc->cdf_idx;
2599
2600 smsg("%4d DCALL %s(argc %d)", current,
2601 df->df_ufunc->uf_name_exp != NULL
2602 ? df->df_ufunc->uf_name_exp
2603 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2604 }
2605 break;
2606 case ISN_UCALL:
2607 {
2608 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2609
2610 smsg("%4d UCALL %s(argc %d)", current,
2611 cufunc->cuf_name, cufunc->cuf_argcount);
2612 }
2613 break;
2614 case ISN_PCALL:
2615 {
2616 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2617
2618 smsg("%4d PCALL%s (argc %d)", current,
2619 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2620 }
2621 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002622 case ISN_PCALL_END:
2623 smsg("%4d PCALL end", current);
2624 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 case ISN_RETURN:
2626 smsg("%4d RETURN", current);
2627 break;
2628 case ISN_FUNCREF:
2629 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002630 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002632 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002634 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002635 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 }
2637 break;
2638
2639 case ISN_JUMP:
2640 {
2641 char *when = "?";
2642
2643 switch (iptr->isn_arg.jump.jump_when)
2644 {
2645 case JUMP_ALWAYS:
2646 when = "JUMP";
2647 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 case JUMP_AND_KEEP_IF_TRUE:
2649 when = "JUMP_AND_KEEP_IF_TRUE";
2650 break;
2651 case JUMP_IF_FALSE:
2652 when = "JUMP_IF_FALSE";
2653 break;
2654 case JUMP_AND_KEEP_IF_FALSE:
2655 when = "JUMP_AND_KEEP_IF_FALSE";
2656 break;
2657 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002658 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 iptr->isn_arg.jump.jump_where);
2660 }
2661 break;
2662
2663 case ISN_FOR:
2664 {
2665 forloop_T *forloop = &iptr->isn_arg.forloop;
2666
2667 smsg("%4d FOR $%d -> %d", current,
2668 forloop->for_idx, forloop->for_end);
2669 }
2670 break;
2671
2672 case ISN_TRY:
2673 {
2674 try_T *try = &iptr->isn_arg.try;
2675
2676 smsg("%4d TRY catch -> %d, finally -> %d", current,
2677 try->try_catch, try->try_finally);
2678 }
2679 break;
2680 case ISN_CATCH:
2681 // TODO
2682 smsg("%4d CATCH", current);
2683 break;
2684 case ISN_ENDTRY:
2685 smsg("%4d ENDTRY", current);
2686 break;
2687 case ISN_THROW:
2688 smsg("%4d THROW", current);
2689 break;
2690
2691 // expression operations on number
2692 case ISN_OPNR:
2693 case ISN_OPFLOAT:
2694 case ISN_OPANY:
2695 {
2696 char *what;
2697 char *ins;
2698
2699 switch (iptr->isn_arg.op.op_type)
2700 {
2701 case EXPR_MULT: what = "*"; break;
2702 case EXPR_DIV: what = "/"; break;
2703 case EXPR_REM: what = "%"; break;
2704 case EXPR_SUB: what = "-"; break;
2705 case EXPR_ADD: what = "+"; break;
2706 default: what = "???"; break;
2707 }
2708 switch (iptr->isn_type)
2709 {
2710 case ISN_OPNR: ins = "OPNR"; break;
2711 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2712 case ISN_OPANY: ins = "OPANY"; break;
2713 default: ins = "???"; break;
2714 }
2715 smsg("%4d %s %s", current, ins, what);
2716 }
2717 break;
2718
2719 case ISN_COMPAREBOOL:
2720 case ISN_COMPARESPECIAL:
2721 case ISN_COMPARENR:
2722 case ISN_COMPAREFLOAT:
2723 case ISN_COMPARESTRING:
2724 case ISN_COMPAREBLOB:
2725 case ISN_COMPARELIST:
2726 case ISN_COMPAREDICT:
2727 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 case ISN_COMPAREANY:
2729 {
2730 char *p;
2731 char buf[10];
2732 char *type;
2733
2734 switch (iptr->isn_arg.op.op_type)
2735 {
2736 case EXPR_EQUAL: p = "=="; break;
2737 case EXPR_NEQUAL: p = "!="; break;
2738 case EXPR_GREATER: p = ">"; break;
2739 case EXPR_GEQUAL: p = ">="; break;
2740 case EXPR_SMALLER: p = "<"; break;
2741 case EXPR_SEQUAL: p = "<="; break;
2742 case EXPR_MATCH: p = "=~"; break;
2743 case EXPR_IS: p = "is"; break;
2744 case EXPR_ISNOT: p = "isnot"; break;
2745 case EXPR_NOMATCH: p = "!~"; break;
2746 default: p = "???"; break;
2747 }
2748 STRCPY(buf, p);
2749 if (iptr->isn_arg.op.op_ic == TRUE)
2750 strcat(buf, "?");
2751 switch(iptr->isn_type)
2752 {
2753 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2754 case ISN_COMPARESPECIAL:
2755 type = "COMPARESPECIAL"; break;
2756 case ISN_COMPARENR: type = "COMPARENR"; break;
2757 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2758 case ISN_COMPARESTRING:
2759 type = "COMPARESTRING"; break;
2760 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2761 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2762 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2763 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2765 default: type = "???"; break;
2766 }
2767
2768 smsg("%4d %s %s", current, type, buf);
2769 }
2770 break;
2771
2772 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2773 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2774
2775 // expression operations
2776 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2777 case ISN_INDEX: smsg("%4d INDEX", current); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002778 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
2779 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 iptr->isn_arg.string); break;
2781 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2782
2783 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2784 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2785 vartype_name(iptr->isn_arg.type.ct_type),
2786 iptr->isn_arg.type.ct_off);
2787 break;
2788 case ISN_2BOOL: if (iptr->isn_arg.number)
2789 smsg("%4d INVERT (!val)", current);
2790 else
2791 smsg("%4d 2BOOL (!!val)", current);
2792 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002793 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2794 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 break;
2796
2797 case ISN_DROP: smsg("%4d DROP", current); break;
2798 }
2799 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800}
2801
2802/*
2803 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2804 * list, etc. Mostly like what JavaScript does, except that empty list and
2805 * empty dictionary are FALSE.
2806 */
2807 int
2808tv2bool(typval_T *tv)
2809{
2810 switch (tv->v_type)
2811 {
2812 case VAR_NUMBER:
2813 return tv->vval.v_number != 0;
2814 case VAR_FLOAT:
2815#ifdef FEAT_FLOAT
2816 return tv->vval.v_float != 0.0;
2817#else
2818 break;
2819#endif
2820 case VAR_PARTIAL:
2821 return tv->vval.v_partial != NULL;
2822 case VAR_FUNC:
2823 case VAR_STRING:
2824 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2825 case VAR_LIST:
2826 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2827 case VAR_DICT:
2828 return tv->vval.v_dict != NULL
2829 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2830 case VAR_BOOL:
2831 case VAR_SPECIAL:
2832 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2833 case VAR_JOB:
2834#ifdef FEAT_JOB_CHANNEL
2835 return tv->vval.v_job != NULL;
2836#else
2837 break;
2838#endif
2839 case VAR_CHANNEL:
2840#ifdef FEAT_JOB_CHANNEL
2841 return tv->vval.v_channel != NULL;
2842#else
2843 break;
2844#endif
2845 case VAR_BLOB:
2846 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2847 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002848 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 case VAR_VOID:
2850 break;
2851 }
2852 return FALSE;
2853}
2854
2855/*
2856 * If "tv" is a string give an error and return FAIL.
2857 */
2858 int
2859check_not_string(typval_T *tv)
2860{
2861 if (tv->v_type == VAR_STRING)
2862 {
2863 emsg(_("E1030: Using a String as a Number"));
2864 clear_tv(tv);
2865 return FAIL;
2866 }
2867 return OK;
2868}
2869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870
2871#endif // FEAT_EVAL