blob: fe6adda601c6f6afad31aa974188e3beb0ebfa5f [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
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200644 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 typval_T *rettv) // return value
646{
647 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200648 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200649 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650 typval_T *tv;
651 int idx;
652 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100653 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200654 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200655 int breakcheck_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656
657// Get pointer to item in the stack.
658#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
659
660// Get pointer to item at the bottom of the stack, -1 is the bottom.
661#undef STACK_TV_BOT
662#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
663
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200664// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200665#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 +0100666
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200667// Like STACK_TV_VAR but use the outer scope
668#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
669
Bram Moolenaar09689a02020-05-09 22:50:08 +0200670 {
671 // Check the function was compiled, it is postponed in ex_vim9script().
672 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
673 + ufunc->uf_dfunc_idx;
674 if (dfunc->df_instr == NULL)
675 return FAIL;
676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200678 CLEAR_FIELD(ectx);
679 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
680 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
681 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
682 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
684
685 // Put arguments on the stack.
686 for (idx = 0; idx < argc; ++idx)
687 {
688 copy_tv(&argv[idx], STACK_TV_BOT(0));
689 ++ectx.ec_stack.ga_len;
690 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200691
692 // Turn varargs into a list. Empty list if no args.
693 if (ufunc->uf_va_name != NULL)
694 {
695 int vararg_count = argc - ufunc->uf_args.ga_len;
696
697 if (vararg_count < 0)
698 vararg_count = 0;
699 else
700 argc -= vararg_count;
701 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200702 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200703 if (defcount > 0)
704 // Move varargs list to below missing default arguments.
705 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
706 --ectx.ec_stack.ga_len;
707 }
708
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100709 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200710 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100711 if (defcount > 0)
712 for (idx = 0; idx < defcount; ++idx)
713 {
714 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
715 ++ectx.ec_stack.ga_len;
716 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200717 if (ufunc->uf_va_name != NULL)
718 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719
720 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200721 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
722 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200724 if (partial != NULL)
725 {
726 ectx.ec_outer_stack = partial->pt_ectx_stack;
727 ectx.ec_outer_frame = partial->pt_ectx_frame;
728 }
729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730 // dummy frame entries
731 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
732 {
733 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
734 ++ectx.ec_stack.ga_len;
735 }
736
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200737 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200738 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200739 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
740 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200741 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200743 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200744 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200745 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200746
747 ectx.ec_instr = dfunc->df_instr;
748 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100749
Bram Moolenaara26b9702020-04-18 19:53:28 +0200750 // Commands behave like vim9script.
751 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
752
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100753 // Decide where to start execution, handles optional arguments.
754 init_instr_idx(ufunc, argc, &ectx);
755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 for (;;)
757 {
758 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100759
Bram Moolenaar270d0382020-05-15 21:42:53 +0200760 if (++breakcheck_count >= 100)
761 {
762 line_breakcheck();
763 breakcheck_count = 0;
764 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100765 if (got_int)
766 {
767 // Turn CTRL-C into an exception.
768 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100769 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100770 goto failed;
771 did_throw = TRUE;
772 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773
Bram Moolenaara26b9702020-04-18 19:53:28 +0200774 if (did_emsg && msg_list != NULL && *msg_list != NULL)
775 {
776 // Turn an error message into an exception.
777 did_emsg = FALSE;
778 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
779 goto failed;
780 did_throw = TRUE;
781 *msg_list = NULL;
782 }
783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 if (did_throw && !ectx.ec_in_catch)
785 {
786 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100787 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788
789 // An exception jumps to the first catch, finally, or returns from
790 // the current function.
791 if (trystack->ga_len > 0)
792 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200793 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 {
795 // jump to ":catch" or ":finally"
796 ectx.ec_in_catch = TRUE;
797 ectx.ec_iidx = trycmd->tcd_catch_idx;
798 }
799 else
800 {
801 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200802 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 {
804 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200805 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 goto failed;
807 tv = STACK_TV_BOT(0);
808 tv->v_type = VAR_NUMBER;
809 tv->vval.v_number = 0;
810 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100811 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200812 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
813 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814 goto done;
815 }
816
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200817 if (func_return(&ectx) == FAIL)
818 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 }
820 continue;
821 }
822
823 iptr = &ectx.ec_instr[ectx.ec_iidx++];
824 switch (iptr->isn_type)
825 {
826 // execute Ex command line
827 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200828 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 do_cmdline_cmd(iptr->isn_arg.string);
830 break;
831
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200832 // execute Ex command from pieces on the stack
833 case ISN_EXECCONCAT:
834 {
835 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200836 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200837 int pass;
838 int i;
839 char_u *cmd = NULL;
840 char_u *str;
841
842 for (pass = 1; pass <= 2; ++pass)
843 {
844 for (i = 0; i < count; ++i)
845 {
846 tv = STACK_TV_BOT(i - count);
847 str = tv->vval.v_string;
848 if (str != NULL && *str != NUL)
849 {
850 if (pass == 2)
851 STRCPY(cmd + len, str);
852 len += STRLEN(str);
853 }
854 if (pass == 2)
855 clear_tv(tv);
856 }
857 if (pass == 1)
858 {
859 cmd = alloc(len + 1);
860 if (cmd == NULL)
861 goto failed;
862 len = 0;
863 }
864 }
865
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200866 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200867 do_cmdline_cmd(cmd);
868 vim_free(cmd);
869 }
870 break;
871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 // execute :echo {string} ...
873 case ISN_ECHO:
874 {
875 int count = iptr->isn_arg.echo.echo_count;
876 int atstart = TRUE;
877 int needclr = TRUE;
878
879 for (idx = 0; idx < count; ++idx)
880 {
881 tv = STACK_TV_BOT(idx - count);
882 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
883 &atstart, &needclr);
884 clear_tv(tv);
885 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100886 if (needclr)
887 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 ectx.ec_stack.ga_len -= count;
889 }
890 break;
891
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200892 // :execute {string} ...
893 // :echomsg {string} ...
894 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100895 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200896 case ISN_ECHOMSG:
897 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100898 {
899 int count = iptr->isn_arg.number;
900 garray_T ga;
901 char_u buf[NUMBUFLEN];
902 char_u *p;
903 int len;
904 int failed = FALSE;
905
906 ga_init2(&ga, 1, 80);
907 for (idx = 0; idx < count; ++idx)
908 {
909 tv = STACK_TV_BOT(idx - count);
910 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
911 {
912 emsg(_(e_inval_string));
913 break;
914 }
915 else
916 p = tv_get_string_buf(tv, buf);
917
918 len = (int)STRLEN(p);
919 if (ga_grow(&ga, len + 2) == FAIL)
920 failed = TRUE;
921 else
922 {
923 if (ga.ga_len > 0)
924 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
925 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
926 ga.ga_len += len;
927 }
928 clear_tv(tv);
929 }
930 ectx.ec_stack.ga_len -= count;
931
932 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200933 {
934 if (iptr->isn_type == ISN_EXECUTE)
935 do_cmdline_cmd((char_u *)ga.ga_data);
936 else
937 {
938 msg_sb_eol();
939 if (iptr->isn_type == ISN_ECHOMSG)
940 {
941 msg_attr(ga.ga_data, echo_attr);
942 out_flush();
943 }
944 else
945 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200946 SOURCING_LNUM = iptr->isn_lnum;
947 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200948 }
949 }
950 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100951 ga_clear(&ga);
952 }
953 break;
954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 // load local variable or argument
956 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200957 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 goto failed;
959 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
960 ++ectx.ec_stack.ga_len;
961 break;
962
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200963 // load variable or argument from outer scope
964 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200965 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200966 goto failed;
967 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
968 STACK_TV_BOT(0));
969 ++ectx.ec_stack.ga_len;
970 break;
971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 // load v: variable
973 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200974 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 goto failed;
976 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
977 ++ectx.ec_stack.ga_len;
978 break;
979
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100980 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 case ISN_LOADSCRIPT:
982 {
983 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100984 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 svar_T *sv;
986
987 sv = ((svar_T *)si->sn_var_vals.ga_data)
988 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200989 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 goto failed;
991 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
992 ++ectx.ec_stack.ga_len;
993 }
994 break;
995
996 // load s: variable in old script
997 case ISN_LOADS:
998 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100999 hashtab_T *ht = &SCRIPT_VARS(
1000 iptr->isn_arg.loadstore.ls_sid);
1001 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 if (di == NULL)
1005 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001006 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 goto failed;
1008 }
1009 else
1010 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001011 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 goto failed;
1013 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1014 ++ectx.ec_stack.ga_len;
1015 }
1016 }
1017 break;
1018
Bram Moolenaard3aac292020-04-19 14:32:17 +02001019 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001021 case ISN_LOADB:
1022 case ISN_LOADW:
1023 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001025 dictitem_T *di = NULL;
1026 hashtab_T *ht = NULL;
1027 char namespace;
1028 switch (iptr->isn_type)
1029 {
1030 case ISN_LOADG:
1031 ht = get_globvar_ht();
1032 namespace = 'g';
1033 break;
1034 case ISN_LOADB:
1035 ht = &curbuf->b_vars->dv_hashtab;
1036 namespace = 'b';
1037 break;
1038 case ISN_LOADW:
1039 ht = &curwin->w_vars->dv_hashtab;
1040 namespace = 'w';
1041 break;
1042 case ISN_LOADT:
1043 ht = &curtab->tp_vars->dv_hashtab;
1044 namespace = 't';
1045 break;
1046 default: // Cannot reach here
1047 goto failed;
1048 }
1049 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 if (di == NULL)
1052 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001053 semsg(_("E121: Undefined variable: %c:%s"),
1054 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 goto failed;
1056 }
1057 else
1058 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001059 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 goto failed;
1061 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1062 ++ectx.ec_stack.ga_len;
1063 }
1064 }
1065 break;
1066
1067 // load &option
1068 case ISN_LOADOPT:
1069 {
1070 typval_T optval;
1071 char_u *name = iptr->isn_arg.string;
1072
Bram Moolenaara8c17702020-04-01 21:17:24 +02001073 // This is not expected to fail, name is checked during
1074 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001075 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001077 if (get_option_tv(&name, &optval, TRUE) == FAIL)
1078 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079 *STACK_TV_BOT(0) = optval;
1080 ++ectx.ec_stack.ga_len;
1081 }
1082 break;
1083
1084 // load $ENV
1085 case ISN_LOADENV:
1086 {
1087 typval_T optval;
1088 char_u *name = iptr->isn_arg.string;
1089
Bram Moolenaar270d0382020-05-15 21:42:53 +02001090 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001092 // name is always valid, checked when compiling
1093 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 *STACK_TV_BOT(0) = optval;
1095 ++ectx.ec_stack.ga_len;
1096 }
1097 break;
1098
1099 // load @register
1100 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001101 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102 goto failed;
1103 tv = STACK_TV_BOT(0);
1104 tv->v_type = VAR_STRING;
1105 tv->vval.v_string = get_reg_contents(
1106 iptr->isn_arg.number, GREG_EXPR_SRC);
1107 ++ectx.ec_stack.ga_len;
1108 break;
1109
1110 // store local variable
1111 case ISN_STORE:
1112 --ectx.ec_stack.ga_len;
1113 tv = STACK_TV_VAR(iptr->isn_arg.number);
1114 clear_tv(tv);
1115 *tv = *STACK_TV_BOT(0);
1116 break;
1117
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001118 // store variable or argument in outer scope
1119 case ISN_STOREOUTER:
1120 --ectx.ec_stack.ga_len;
1121 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1122 clear_tv(tv);
1123 *tv = *STACK_TV_BOT(0);
1124 break;
1125
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001126 // store s: variable in old script
1127 case ISN_STORES:
1128 {
1129 hashtab_T *ht = &SCRIPT_VARS(
1130 iptr->isn_arg.loadstore.ls_sid);
1131 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001132 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001133
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001134 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001135 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001136 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001137 else
1138 {
1139 clear_tv(&di->di_tv);
1140 di->di_tv = *STACK_TV_BOT(0);
1141 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001142 }
1143 break;
1144
1145 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 case ISN_STORESCRIPT:
1147 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001148 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 iptr->isn_arg.script.script_sid);
1150 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1151 + iptr->isn_arg.script.script_idx;
1152
1153 --ectx.ec_stack.ga_len;
1154 clear_tv(sv->sv_tv);
1155 *sv->sv_tv = *STACK_TV_BOT(0);
1156 }
1157 break;
1158
1159 // store option
1160 case ISN_STOREOPT:
1161 {
1162 long n = 0;
1163 char_u *s = NULL;
1164 char *msg;
1165
1166 --ectx.ec_stack.ga_len;
1167 tv = STACK_TV_BOT(0);
1168 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001169 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001171 if (s == NULL)
1172 s = (char_u *)"";
1173 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001175 // must be VAR_NUMBER, CHECKTYPE makes sure
1176 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1178 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001179 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 if (msg != NULL)
1181 {
1182 emsg(_(msg));
1183 goto failed;
1184 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 }
1186 break;
1187
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001188 // store $ENV
1189 case ISN_STOREENV:
1190 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001191 tv = STACK_TV_BOT(0);
1192 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1193 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001194 break;
1195
1196 // store @r
1197 case ISN_STOREREG:
1198 {
1199 int reg = iptr->isn_arg.number;
1200
1201 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001202 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001203 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001204 tv_get_string(tv), -1, FALSE);
1205 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001206 }
1207 break;
1208
1209 // store v: variable
1210 case ISN_STOREV:
1211 --ectx.ec_stack.ga_len;
1212 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1213 == FAIL)
1214 goto failed;
1215 break;
1216
Bram Moolenaard3aac292020-04-19 14:32:17 +02001217 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001219 case ISN_STOREB:
1220 case ISN_STOREW:
1221 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 {
1223 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001224 hashtab_T *ht;
1225 switch (iptr->isn_type)
1226 {
1227 case ISN_STOREG:
1228 ht = get_globvar_ht();
1229 break;
1230 case ISN_STOREB:
1231 ht = &curbuf->b_vars->dv_hashtab;
1232 break;
1233 case ISN_STOREW:
1234 ht = &curwin->w_vars->dv_hashtab;
1235 break;
1236 case ISN_STORET:
1237 ht = &curtab->tp_vars->dv_hashtab;
1238 break;
1239 default: // Cannot reach here
1240 goto failed;
1241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242
1243 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001244 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001246 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 else
1248 {
1249 clear_tv(&di->di_tv);
1250 di->di_tv = *STACK_TV_BOT(0);
1251 }
1252 }
1253 break;
1254
1255 // store number in local variable
1256 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001257 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 clear_tv(tv);
1259 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001260 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 break;
1262
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001263 // store value in list variable
1264 case ISN_STORELIST:
1265 {
1266 typval_T *tv_idx = STACK_TV_BOT(-2);
1267 varnumber_T lidx = tv_idx->vval.v_number;
1268 typval_T *tv_list = STACK_TV_BOT(-1);
1269 list_T *list = tv_list->vval.v_list;
1270
1271 if (lidx < 0 && list->lv_len + lidx >= 0)
1272 // negative index is relative to the end
1273 lidx = list->lv_len + lidx;
1274 if (lidx < 0 || lidx > list->lv_len)
1275 {
1276 semsg(_(e_listidx), lidx);
1277 goto failed;
1278 }
1279 tv = STACK_TV_BOT(-3);
1280 if (lidx < list->lv_len)
1281 {
1282 listitem_T *li = list_find(list, lidx);
1283
1284 // overwrite existing list item
1285 clear_tv(&li->li_tv);
1286 li->li_tv = *tv;
1287 }
1288 else
1289 {
1290 // append to list
1291 if (list_append_tv(list, tv) == FAIL)
1292 goto failed;
1293 clear_tv(tv);
1294 }
1295 clear_tv(tv_idx);
1296 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001297 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001298 }
1299 break;
1300
1301 // store value in dict variable
1302 case ISN_STOREDICT:
1303 {
1304 typval_T *tv_key = STACK_TV_BOT(-2);
1305 char_u *key = tv_key->vval.v_string;
1306 typval_T *tv_dict = STACK_TV_BOT(-1);
1307 dict_T *dict = tv_dict->vval.v_dict;
1308 dictitem_T *di;
1309
1310 if (key == NULL || *key == NUL)
1311 {
1312 emsg(_(e_emptykey));
1313 goto failed;
1314 }
1315 tv = STACK_TV_BOT(-3);
1316 di = dict_find(dict, key, -1);
1317 if (di != NULL)
1318 {
1319 clear_tv(&di->di_tv);
1320 di->di_tv = *tv;
1321 }
1322 else
1323 {
1324 // add to dict
1325 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1326 goto failed;
1327 clear_tv(tv);
1328 }
1329 clear_tv(tv_key);
1330 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001331 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001332 }
1333 break;
1334
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 // push constant
1336 case ISN_PUSHNR:
1337 case ISN_PUSHBOOL:
1338 case ISN_PUSHSPEC:
1339 case ISN_PUSHF:
1340 case ISN_PUSHS:
1341 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001342 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001343 case ISN_PUSHCHANNEL:
1344 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001345 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 goto failed;
1347 tv = STACK_TV_BOT(0);
1348 ++ectx.ec_stack.ga_len;
1349 switch (iptr->isn_type)
1350 {
1351 case ISN_PUSHNR:
1352 tv->v_type = VAR_NUMBER;
1353 tv->vval.v_number = iptr->isn_arg.number;
1354 break;
1355 case ISN_PUSHBOOL:
1356 tv->v_type = VAR_BOOL;
1357 tv->vval.v_number = iptr->isn_arg.number;
1358 break;
1359 case ISN_PUSHSPEC:
1360 tv->v_type = VAR_SPECIAL;
1361 tv->vval.v_number = iptr->isn_arg.number;
1362 break;
1363#ifdef FEAT_FLOAT
1364 case ISN_PUSHF:
1365 tv->v_type = VAR_FLOAT;
1366 tv->vval.v_float = iptr->isn_arg.fnumber;
1367 break;
1368#endif
1369 case ISN_PUSHBLOB:
1370 blob_copy(iptr->isn_arg.blob, tv);
1371 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001372 case ISN_PUSHFUNC:
1373 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001374 if (iptr->isn_arg.string == NULL)
1375 tv->vval.v_string = NULL;
1376 else
1377 tv->vval.v_string =
1378 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001379 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001380 case ISN_PUSHCHANNEL:
1381#ifdef FEAT_JOB_CHANNEL
1382 tv->v_type = VAR_CHANNEL;
1383 tv->vval.v_channel = iptr->isn_arg.channel;
1384 if (tv->vval.v_channel != NULL)
1385 ++tv->vval.v_channel->ch_refcount;
1386#endif
1387 break;
1388 case ISN_PUSHJOB:
1389#ifdef FEAT_JOB_CHANNEL
1390 tv->v_type = VAR_JOB;
1391 tv->vval.v_job = iptr->isn_arg.job;
1392 if (tv->vval.v_job != NULL)
1393 ++tv->vval.v_job->jv_refcount;
1394#endif
1395 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 default:
1397 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001398 tv->vval.v_string = vim_strsave(
1399 iptr->isn_arg.string == NULL
1400 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 }
1402 break;
1403
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001404 case ISN_UNLET:
1405 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1406 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1407 goto failed;
1408 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001409 case ISN_UNLETENV:
1410 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1411 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 // create a list from items on the stack; uses a single allocation
1414 // for the list header and the items
1415 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001416 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1417 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 break;
1419
1420 // create a dict from items on the stack
1421 case ISN_NEWDICT:
1422 {
1423 int count = iptr->isn_arg.number;
1424 dict_T *dict = dict_alloc();
1425 dictitem_T *item;
1426
1427 if (dict == NULL)
1428 goto failed;
1429 for (idx = 0; idx < count; ++idx)
1430 {
1431 // check key type is VAR_STRING
1432 tv = STACK_TV_BOT(2 * (idx - count));
1433 item = dictitem_alloc(tv->vval.v_string);
1434 clear_tv(tv);
1435 if (item == NULL)
1436 goto failed;
1437 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1438 item->di_tv.v_lock = 0;
1439 if (dict_add(dict, item) == FAIL)
1440 goto failed;
1441 }
1442
1443 if (count > 0)
1444 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001445 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 goto failed;
1447 else
1448 ++ectx.ec_stack.ga_len;
1449 tv = STACK_TV_BOT(-1);
1450 tv->v_type = VAR_DICT;
1451 tv->vval.v_dict = dict;
1452 ++dict->dv_refcount;
1453 }
1454 break;
1455
1456 // call a :def function
1457 case ISN_DCALL:
1458 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1459 iptr->isn_arg.dfunc.cdf_argcount,
1460 &ectx) == FAIL)
1461 goto failed;
1462 break;
1463
1464 // call a builtin function
1465 case ISN_BCALL:
1466 SOURCING_LNUM = iptr->isn_lnum;
1467 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1468 iptr->isn_arg.bfunc.cbf_argcount,
1469 &ectx) == FAIL)
1470 goto failed;
1471 break;
1472
1473 // call a funcref or partial
1474 case ISN_PCALL:
1475 {
1476 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1477 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001478 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479
1480 SOURCING_LNUM = iptr->isn_lnum;
1481 if (pfunc->cpf_top)
1482 {
1483 // funcref is above the arguments
1484 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1485 }
1486 else
1487 {
1488 // Get the funcref from the stack.
1489 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001490 partial_tv = *STACK_TV_BOT(0);
1491 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 }
1493 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001494 if (tv == &partial_tv)
1495 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 if (r == FAIL)
1497 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498 }
1499 break;
1500
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001501 case ISN_PCALL_END:
1502 // PCALL finished, arguments have been consumed and replaced by
1503 // the return value. Now clear the funcref from the stack,
1504 // and move the return value in its place.
1505 --ectx.ec_stack.ga_len;
1506 clear_tv(STACK_TV_BOT(-1));
1507 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1508 break;
1509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 // call a user defined function or funcref/partial
1511 case ISN_UCALL:
1512 {
1513 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1514
1515 SOURCING_LNUM = iptr->isn_lnum;
1516 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001517 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 goto failed;
1519 }
1520 break;
1521
1522 // return from a :def function call
1523 case ISN_RETURN:
1524 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001525 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001526 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001527
1528 if (trystack->ga_len > 0)
1529 trycmd = ((trycmd_T *)trystack->ga_data)
1530 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001531 if (trycmd != NULL
1532 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 && trycmd->tcd_finally_idx != 0)
1534 {
1535 // jump to ":finally"
1536 ectx.ec_iidx = trycmd->tcd_finally_idx;
1537 trycmd->tcd_return = TRUE;
1538 }
1539 else
1540 {
1541 // Restore previous function. If the frame pointer
1542 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001543 if (ectx.ec_frame_idx == initial_frame_idx)
1544 {
1545 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1546 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001548 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001550 if (func_return(&ectx) == FAIL)
1551 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 }
1553 }
1554 break;
1555
1556 // push a function reference to a compiled function
1557 case ISN_FUNCREF:
1558 {
1559 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001560 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561
1562 pt = ALLOC_CLEAR_ONE(partial_T);
1563 if (pt == NULL)
1564 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001565 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001566 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001567 vim_free(pt);
1568 goto failed;
1569 }
1570 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1571 + iptr->isn_arg.funcref.fr_func;
1572 pt->pt_func = pt_dfunc->df_ufunc;
1573 pt->pt_refcount = 1;
1574 ++pt_dfunc->df_ufunc->uf_refcount;
1575
1576 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1577 {
1578 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1579 + ectx.ec_dfunc_idx;
1580
1581 // The closure needs to find arguments and local
1582 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001583 pt->pt_ectx_stack = &ectx.ec_stack;
1584 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001585
1586 // If this function returns and the closure is still
1587 // used, we need to make a copy of the context
1588 // (arguments and local variables). Store a reference
1589 // to the partial so we can handle that.
1590 ++pt->pt_refcount;
1591 tv = STACK_TV_VAR(dfunc->df_varcount
1592 + iptr->isn_arg.funcref.fr_var_idx);
1593 if (tv->v_type == VAR_PARTIAL)
1594 {
1595 // TODO: use a garray_T on ectx.
1596 emsg("Multiple closures not supported yet");
1597 goto failed;
1598 }
1599 tv->v_type = VAR_PARTIAL;
1600 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001601 }
1602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 tv = STACK_TV_BOT(0);
1604 ++ectx.ec_stack.ga_len;
1605 tv->vval.v_partial = pt;
1606 tv->v_type = VAR_PARTIAL;
1607 }
1608 break;
1609
1610 // jump if a condition is met
1611 case ISN_JUMP:
1612 {
1613 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1614 int jump = TRUE;
1615
1616 if (when != JUMP_ALWAYS)
1617 {
1618 tv = STACK_TV_BOT(-1);
1619 jump = tv2bool(tv);
1620 if (when == JUMP_IF_FALSE
1621 || when == JUMP_AND_KEEP_IF_FALSE)
1622 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001623 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 {
1625 // drop the value from the stack
1626 clear_tv(tv);
1627 --ectx.ec_stack.ga_len;
1628 }
1629 }
1630 if (jump)
1631 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1632 }
1633 break;
1634
1635 // top of a for loop
1636 case ISN_FOR:
1637 {
1638 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1639 typval_T *idxtv =
1640 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1641
1642 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001643 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 goto failed;
1645 if (++idxtv->vval.v_number >= list->lv_len)
1646 // past the end of the list, jump to "endfor"
1647 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1648 else if (list->lv_first == &range_list_item)
1649 {
1650 // non-materialized range() list
1651 tv = STACK_TV_BOT(0);
1652 tv->v_type = VAR_NUMBER;
1653 tv->vval.v_number = list_find_nr(
1654 list, idxtv->vval.v_number, NULL);
1655 ++ectx.ec_stack.ga_len;
1656 }
1657 else
1658 {
1659 listitem_T *li = list_find(list, idxtv->vval.v_number);
1660
1661 if (li == NULL)
1662 goto failed;
1663 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1664 ++ectx.ec_stack.ga_len;
1665 }
1666 }
1667 break;
1668
1669 // start of ":try" block
1670 case ISN_TRY:
1671 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001672 trycmd_T *trycmd = NULL;
1673
Bram Moolenaar270d0382020-05-15 21:42:53 +02001674 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 goto failed;
1676 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1677 + ectx.ec_trystack.ga_len;
1678 ++ectx.ec_trystack.ga_len;
1679 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001680 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1682 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001683 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 }
1685 break;
1686
1687 case ISN_PUSHEXC:
1688 if (current_exception == NULL)
1689 {
1690 iemsg("Evaluating catch while current_exception is NULL");
1691 goto failed;
1692 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001693 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 goto failed;
1695 tv = STACK_TV_BOT(0);
1696 ++ectx.ec_stack.ga_len;
1697 tv->v_type = VAR_STRING;
1698 tv->vval.v_string = vim_strsave(
1699 (char_u *)current_exception->value);
1700 break;
1701
1702 case ISN_CATCH:
1703 {
1704 garray_T *trystack = &ectx.ec_trystack;
1705
1706 if (trystack->ga_len > 0)
1707 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001708 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 + trystack->ga_len - 1;
1710 trycmd->tcd_caught = TRUE;
1711 }
1712 did_emsg = got_int = did_throw = FALSE;
1713 catch_exception(current_exception);
1714 }
1715 break;
1716
1717 // end of ":try" block
1718 case ISN_ENDTRY:
1719 {
1720 garray_T *trystack = &ectx.ec_trystack;
1721
1722 if (trystack->ga_len > 0)
1723 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001724 trycmd_T *trycmd = NULL;
1725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 --trystack->ga_len;
1727 --trylevel;
1728 trycmd = ((trycmd_T *)trystack->ga_data)
1729 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001730 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 {
1732 // discard the exception
1733 if (caught_stack == current_exception)
1734 caught_stack = caught_stack->caught;
1735 discard_current_exception();
1736 }
1737
1738 if (trycmd->tcd_return)
1739 {
1740 // Restore previous function. If the frame pointer
1741 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001742 if (ectx.ec_frame_idx == initial_frame_idx)
1743 {
1744 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1745 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001749 if (func_return(&ectx) == FAIL)
1750 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 }
1752 }
1753 }
1754 break;
1755
1756 case ISN_THROW:
1757 --ectx.ec_stack.ga_len;
1758 tv = STACK_TV_BOT(0);
1759 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1760 {
1761 vim_free(tv->vval.v_string);
1762 goto failed;
1763 }
1764 did_throw = TRUE;
1765 break;
1766
1767 // compare with special values
1768 case ISN_COMPAREBOOL:
1769 case ISN_COMPARESPECIAL:
1770 {
1771 typval_T *tv1 = STACK_TV_BOT(-2);
1772 typval_T *tv2 = STACK_TV_BOT(-1);
1773 varnumber_T arg1 = tv1->vval.v_number;
1774 varnumber_T arg2 = tv2->vval.v_number;
1775 int res;
1776
1777 switch (iptr->isn_arg.op.op_type)
1778 {
1779 case EXPR_EQUAL: res = arg1 == arg2; break;
1780 case EXPR_NEQUAL: res = arg1 != arg2; break;
1781 default: res = 0; break;
1782 }
1783
1784 --ectx.ec_stack.ga_len;
1785 tv1->v_type = VAR_BOOL;
1786 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1787 }
1788 break;
1789
1790 // Operation with two number arguments
1791 case ISN_OPNR:
1792 case ISN_COMPARENR:
1793 {
1794 typval_T *tv1 = STACK_TV_BOT(-2);
1795 typval_T *tv2 = STACK_TV_BOT(-1);
1796 varnumber_T arg1 = tv1->vval.v_number;
1797 varnumber_T arg2 = tv2->vval.v_number;
1798 varnumber_T res;
1799
1800 switch (iptr->isn_arg.op.op_type)
1801 {
1802 case EXPR_MULT: res = arg1 * arg2; break;
1803 case EXPR_DIV: res = arg1 / arg2; break;
1804 case EXPR_REM: res = arg1 % arg2; break;
1805 case EXPR_SUB: res = arg1 - arg2; break;
1806 case EXPR_ADD: res = arg1 + arg2; break;
1807
1808 case EXPR_EQUAL: res = arg1 == arg2; break;
1809 case EXPR_NEQUAL: res = arg1 != arg2; break;
1810 case EXPR_GREATER: res = arg1 > arg2; break;
1811 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1812 case EXPR_SMALLER: res = arg1 < arg2; break;
1813 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1814 default: res = 0; break;
1815 }
1816
1817 --ectx.ec_stack.ga_len;
1818 if (iptr->isn_type == ISN_COMPARENR)
1819 {
1820 tv1->v_type = VAR_BOOL;
1821 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1822 }
1823 else
1824 tv1->vval.v_number = res;
1825 }
1826 break;
1827
1828 // Computation with two float arguments
1829 case ISN_OPFLOAT:
1830 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001831#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 {
1833 typval_T *tv1 = STACK_TV_BOT(-2);
1834 typval_T *tv2 = STACK_TV_BOT(-1);
1835 float_T arg1 = tv1->vval.v_float;
1836 float_T arg2 = tv2->vval.v_float;
1837 float_T res = 0;
1838 int cmp = FALSE;
1839
1840 switch (iptr->isn_arg.op.op_type)
1841 {
1842 case EXPR_MULT: res = arg1 * arg2; break;
1843 case EXPR_DIV: res = arg1 / arg2; break;
1844 case EXPR_SUB: res = arg1 - arg2; break;
1845 case EXPR_ADD: res = arg1 + arg2; break;
1846
1847 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1848 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1849 case EXPR_GREATER: cmp = arg1 > arg2; break;
1850 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1851 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1852 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1853 default: cmp = 0; break;
1854 }
1855 --ectx.ec_stack.ga_len;
1856 if (iptr->isn_type == ISN_COMPAREFLOAT)
1857 {
1858 tv1->v_type = VAR_BOOL;
1859 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1860 }
1861 else
1862 tv1->vval.v_float = res;
1863 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001864#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 break;
1866
1867 case ISN_COMPARELIST:
1868 {
1869 typval_T *tv1 = STACK_TV_BOT(-2);
1870 typval_T *tv2 = STACK_TV_BOT(-1);
1871 list_T *arg1 = tv1->vval.v_list;
1872 list_T *arg2 = tv2->vval.v_list;
1873 int cmp = FALSE;
1874 int ic = iptr->isn_arg.op.op_ic;
1875
1876 switch (iptr->isn_arg.op.op_type)
1877 {
1878 case EXPR_EQUAL: cmp =
1879 list_equal(arg1, arg2, ic, FALSE); break;
1880 case EXPR_NEQUAL: cmp =
1881 !list_equal(arg1, arg2, ic, FALSE); break;
1882 case EXPR_IS: cmp = arg1 == arg2; break;
1883 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1884 default: cmp = 0; break;
1885 }
1886 --ectx.ec_stack.ga_len;
1887 clear_tv(tv1);
1888 clear_tv(tv2);
1889 tv1->v_type = VAR_BOOL;
1890 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1891 }
1892 break;
1893
1894 case ISN_COMPAREBLOB:
1895 {
1896 typval_T *tv1 = STACK_TV_BOT(-2);
1897 typval_T *tv2 = STACK_TV_BOT(-1);
1898 blob_T *arg1 = tv1->vval.v_blob;
1899 blob_T *arg2 = tv2->vval.v_blob;
1900 int cmp = FALSE;
1901
1902 switch (iptr->isn_arg.op.op_type)
1903 {
1904 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1905 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1906 case EXPR_IS: cmp = arg1 == arg2; break;
1907 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1908 default: cmp = 0; break;
1909 }
1910 --ectx.ec_stack.ga_len;
1911 clear_tv(tv1);
1912 clear_tv(tv2);
1913 tv1->v_type = VAR_BOOL;
1914 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1915 }
1916 break;
1917
1918 // TODO: handle separately
1919 case ISN_COMPARESTRING:
1920 case ISN_COMPAREDICT:
1921 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 case ISN_COMPAREANY:
1923 {
1924 typval_T *tv1 = STACK_TV_BOT(-2);
1925 typval_T *tv2 = STACK_TV_BOT(-1);
1926 exptype_T exptype = iptr->isn_arg.op.op_type;
1927 int ic = iptr->isn_arg.op.op_ic;
1928
1929 typval_compare(tv1, tv2, exptype, ic);
1930 clear_tv(tv2);
1931 tv1->v_type = VAR_BOOL;
1932 tv1->vval.v_number = tv1->vval.v_number
1933 ? VVAL_TRUE : VVAL_FALSE;
1934 --ectx.ec_stack.ga_len;
1935 }
1936 break;
1937
1938 case ISN_ADDLIST:
1939 case ISN_ADDBLOB:
1940 {
1941 typval_T *tv1 = STACK_TV_BOT(-2);
1942 typval_T *tv2 = STACK_TV_BOT(-1);
1943
1944 if (iptr->isn_type == ISN_ADDLIST)
1945 eval_addlist(tv1, tv2);
1946 else
1947 eval_addblob(tv1, tv2);
1948 clear_tv(tv2);
1949 --ectx.ec_stack.ga_len;
1950 }
1951 break;
1952
1953 // Computation with two arguments of unknown type
1954 case ISN_OPANY:
1955 {
1956 typval_T *tv1 = STACK_TV_BOT(-2);
1957 typval_T *tv2 = STACK_TV_BOT(-1);
1958 varnumber_T n1, n2;
1959#ifdef FEAT_FLOAT
1960 float_T f1 = 0, f2 = 0;
1961#endif
1962 int error = FALSE;
1963
1964 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1965 {
1966 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1967 {
1968 eval_addlist(tv1, tv2);
1969 clear_tv(tv2);
1970 --ectx.ec_stack.ga_len;
1971 break;
1972 }
1973 else if (tv1->v_type == VAR_BLOB
1974 && tv2->v_type == VAR_BLOB)
1975 {
1976 eval_addblob(tv1, tv2);
1977 clear_tv(tv2);
1978 --ectx.ec_stack.ga_len;
1979 break;
1980 }
1981 }
1982#ifdef FEAT_FLOAT
1983 if (tv1->v_type == VAR_FLOAT)
1984 {
1985 f1 = tv1->vval.v_float;
1986 n1 = 0;
1987 }
1988 else
1989#endif
1990 {
1991 n1 = tv_get_number_chk(tv1, &error);
1992 if (error)
1993 goto failed;
1994#ifdef FEAT_FLOAT
1995 if (tv2->v_type == VAR_FLOAT)
1996 f1 = n1;
1997#endif
1998 }
1999#ifdef FEAT_FLOAT
2000 if (tv2->v_type == VAR_FLOAT)
2001 {
2002 f2 = tv2->vval.v_float;
2003 n2 = 0;
2004 }
2005 else
2006#endif
2007 {
2008 n2 = tv_get_number_chk(tv2, &error);
2009 if (error)
2010 goto failed;
2011#ifdef FEAT_FLOAT
2012 if (tv1->v_type == VAR_FLOAT)
2013 f2 = n2;
2014#endif
2015 }
2016#ifdef FEAT_FLOAT
2017 // if there is a float on either side the result is a float
2018 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2019 {
2020 switch (iptr->isn_arg.op.op_type)
2021 {
2022 case EXPR_MULT: f1 = f1 * f2; break;
2023 case EXPR_DIV: f1 = f1 / f2; break;
2024 case EXPR_SUB: f1 = f1 - f2; break;
2025 case EXPR_ADD: f1 = f1 + f2; break;
2026 default: emsg(_(e_modulus)); goto failed;
2027 }
2028 clear_tv(tv1);
2029 clear_tv(tv2);
2030 tv1->v_type = VAR_FLOAT;
2031 tv1->vval.v_float = f1;
2032 --ectx.ec_stack.ga_len;
2033 }
2034 else
2035#endif
2036 {
2037 switch (iptr->isn_arg.op.op_type)
2038 {
2039 case EXPR_MULT: n1 = n1 * n2; break;
2040 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2041 case EXPR_SUB: n1 = n1 - n2; break;
2042 case EXPR_ADD: n1 = n1 + n2; break;
2043 default: n1 = num_modulus(n1, n2); break;
2044 }
2045 clear_tv(tv1);
2046 clear_tv(tv2);
2047 tv1->v_type = VAR_NUMBER;
2048 tv1->vval.v_number = n1;
2049 --ectx.ec_stack.ga_len;
2050 }
2051 }
2052 break;
2053
2054 case ISN_CONCAT:
2055 {
2056 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2057 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2058 char_u *res;
2059
2060 res = concat_str(str1, str2);
2061 clear_tv(STACK_TV_BOT(-2));
2062 clear_tv(STACK_TV_BOT(-1));
2063 --ectx.ec_stack.ga_len;
2064 STACK_TV_BOT(-1)->vval.v_string = res;
2065 }
2066 break;
2067
2068 case ISN_INDEX:
2069 {
2070 list_T *list;
2071 varnumber_T n;
2072 listitem_T *li;
2073
2074 // list index: list is at stack-2, index at stack-1
2075 tv = STACK_TV_BOT(-2);
2076 if (tv->v_type != VAR_LIST)
2077 {
2078 emsg(_(e_listreq));
2079 goto failed;
2080 }
2081 list = tv->vval.v_list;
2082
2083 tv = STACK_TV_BOT(-1);
2084 if (tv->v_type != VAR_NUMBER)
2085 {
2086 emsg(_(e_number_exp));
2087 goto failed;
2088 }
2089 n = tv->vval.v_number;
2090 clear_tv(tv);
2091 if ((li = list_find(list, n)) == NULL)
2092 {
2093 semsg(_(e_listidx), n);
2094 goto failed;
2095 }
2096 --ectx.ec_stack.ga_len;
2097 clear_tv(STACK_TV_BOT(-1));
2098 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2099 }
2100 break;
2101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 case ISN_MEMBER:
2103 {
2104 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002105 char_u *key;
2106 dictitem_T *di;
2107
2108 // dict member: dict is at stack-2, key at stack-1
2109 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002110 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002111 dict = tv->vval.v_dict;
2112
2113 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002114 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002115 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002116
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002117 if ((di = dict_find(dict, key, -1)) == NULL)
2118 {
2119 semsg(_(e_dictkey), key);
2120 goto failed;
2121 }
2122 --ectx.ec_stack.ga_len;
2123 clear_tv(tv);
2124 clear_tv(STACK_TV_BOT(-1));
2125 copy_tv(&di->di_tv, STACK_TV_BOT(-1));
2126 }
2127 break;
2128
2129 // dict member with string key
2130 case ISN_STRINGMEMBER:
2131 {
2132 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 dictitem_T *di;
2134
2135 tv = STACK_TV_BOT(-1);
2136 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2137 {
2138 emsg(_(e_dictreq));
2139 goto failed;
2140 }
2141 dict = tv->vval.v_dict;
2142
2143 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2144 == NULL)
2145 {
2146 semsg(_(e_dictkey), iptr->isn_arg.string);
2147 goto failed;
2148 }
2149 clear_tv(tv);
2150 copy_tv(&di->di_tv, tv);
2151 }
2152 break;
2153
2154 case ISN_NEGATENR:
2155 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002156 if (tv->v_type != VAR_NUMBER
2157#ifdef FEAT_FLOAT
2158 && tv->v_type != VAR_FLOAT
2159#endif
2160 )
2161 {
2162 emsg(_(e_number_exp));
2163 goto failed;
2164 }
2165#ifdef FEAT_FLOAT
2166 if (tv->v_type == VAR_FLOAT)
2167 tv->vval.v_float = -tv->vval.v_float;
2168 else
2169#endif
2170 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 break;
2172
2173 case ISN_CHECKNR:
2174 {
2175 int error = FALSE;
2176
2177 tv = STACK_TV_BOT(-1);
2178 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 (void)tv_get_number_chk(tv, &error);
2181 if (error)
2182 goto failed;
2183 }
2184 break;
2185
2186 case ISN_CHECKTYPE:
2187 {
2188 checktype_T *ct = &iptr->isn_arg.type;
2189
2190 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002191 // TODO: better type comparison
2192 if (tv->v_type != ct->ct_type
2193 && !((tv->v_type == VAR_PARTIAL
2194 && ct->ct_type == VAR_FUNC)
2195 || (tv->v_type == VAR_FUNC
2196 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 {
2198 semsg(_("E1029: Expected %s but got %s"),
2199 vartype_name(ct->ct_type),
2200 vartype_name(tv->v_type));
2201 goto failed;
2202 }
2203 }
2204 break;
2205
2206 case ISN_2BOOL:
2207 {
2208 int n;
2209
2210 tv = STACK_TV_BOT(-1);
2211 n = tv2bool(tv);
2212 if (iptr->isn_arg.number) // invert
2213 n = !n;
2214 clear_tv(tv);
2215 tv->v_type = VAR_BOOL;
2216 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2217 }
2218 break;
2219
2220 case ISN_2STRING:
2221 {
2222 char_u *str;
2223
2224 tv = STACK_TV_BOT(iptr->isn_arg.number);
2225 if (tv->v_type != VAR_STRING)
2226 {
2227 str = typval_tostring(tv);
2228 clear_tv(tv);
2229 tv->v_type = VAR_STRING;
2230 tv->vval.v_string = str;
2231 }
2232 }
2233 break;
2234
2235 case ISN_DROP:
2236 --ectx.ec_stack.ga_len;
2237 clear_tv(STACK_TV_BOT(0));
2238 break;
2239 }
2240 }
2241
2242done:
2243 // function finished, get result from the stack.
2244 tv = STACK_TV_BOT(-1);
2245 *rettv = *tv;
2246 tv->v_type = VAR_UNKNOWN;
2247 ret = OK;
2248
2249failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002250 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002251 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002252 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002253failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002254 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002255
2256 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2258 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002261 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 return ret;
2263}
2264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265/*
2266 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002267 * We don't really need this at runtime, but we do have tests that require it,
2268 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 */
2270 void
2271ex_disassemble(exarg_T *eap)
2272{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002273 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002274 char_u *fname;
2275 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 dfunc_T *dfunc;
2277 isn_T *instr;
2278 int current;
2279 int line_idx = 0;
2280 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002281 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002283 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002284 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002285 if (fname == NULL)
2286 {
2287 semsg(_(e_invarg2), eap->arg);
2288 return;
2289 }
2290
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002291 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002292 if (ufunc == NULL)
2293 {
2294 char_u *p = untrans_function_name(fname);
2295
2296 if (p != NULL)
2297 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002298 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002299 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002300 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 if (ufunc == NULL)
2302 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002303 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 return;
2305 }
2306 if (ufunc->uf_dfunc_idx < 0)
2307 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002308 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 return;
2310 }
2311 if (ufunc->uf_name_exp != NULL)
2312 msg((char *)ufunc->uf_name_exp);
2313 else
2314 msg((char *)ufunc->uf_name);
2315
2316 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2317 instr = dfunc->df_instr;
2318 for (current = 0; current < dfunc->df_instr_count; ++current)
2319 {
2320 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002321 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322
2323 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2324 {
2325 if (current > prev_current)
2326 {
2327 msg_puts("\n\n");
2328 prev_current = current;
2329 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002330 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2331 if (line != NULL)
2332 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 }
2334
2335 switch (iptr->isn_type)
2336 {
2337 case ISN_EXEC:
2338 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2339 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002340 case ISN_EXECCONCAT:
2341 smsg("%4d EXECCONCAT %lld", current,
2342 (long long)iptr->isn_arg.number);
2343 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 case ISN_ECHO:
2345 {
2346 echo_T *echo = &iptr->isn_arg.echo;
2347
2348 smsg("%4d %s %d", current,
2349 echo->echo_with_white ? "ECHO" : "ECHON",
2350 echo->echo_count);
2351 }
2352 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002353 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002354 smsg("%4d EXECUTE %lld", current,
2355 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002356 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002357 case ISN_ECHOMSG:
2358 smsg("%4d ECHOMSG %lld", current,
2359 (long long)(iptr->isn_arg.number));
2360 break;
2361 case ISN_ECHOERR:
2362 smsg("%4d ECHOERR %lld", current,
2363 (long long)(iptr->isn_arg.number));
2364 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002366 case ISN_LOADOUTER:
2367 {
2368 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2369
2370 if (iptr->isn_arg.number < 0)
2371 smsg("%4d LOAD%s arg[%lld]", current, add,
2372 (long long)(iptr->isn_arg.number
2373 + STACK_FRAME_SIZE));
2374 else
2375 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002376 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 break;
2379 case ISN_LOADV:
2380 smsg("%4d LOADV v:%s", current,
2381 get_vim_var_name(iptr->isn_arg.number));
2382 break;
2383 case ISN_LOADSCRIPT:
2384 {
2385 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002386 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2388 + iptr->isn_arg.script.script_idx;
2389
2390 smsg("%4d LOADSCRIPT %s from %s", current,
2391 sv->sv_name, si->sn_name);
2392 }
2393 break;
2394 case ISN_LOADS:
2395 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002396 scriptitem_T *si = SCRIPT_ITEM(
2397 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398
2399 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002400 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 }
2402 break;
2403 case ISN_LOADG:
2404 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2405 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002406 case ISN_LOADB:
2407 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2408 break;
2409 case ISN_LOADW:
2410 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2411 break;
2412 case ISN_LOADT:
2413 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2414 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 case ISN_LOADOPT:
2416 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2417 break;
2418 case ISN_LOADENV:
2419 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2420 break;
2421 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002422 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 break;
2424
2425 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002426 case ISN_STOREOUTER:
2427 {
2428 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2429
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002430 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002431 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002432 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002433 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002434 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002435 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002436 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002438 case ISN_STOREV:
2439 smsg("%4d STOREV v:%s", current,
2440 get_vim_var_name(iptr->isn_arg.number));
2441 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002443 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2444 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002445 case ISN_STOREB:
2446 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2447 break;
2448 case ISN_STOREW:
2449 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2450 break;
2451 case ISN_STORET:
2452 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2453 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002454 case ISN_STORES:
2455 {
2456 scriptitem_T *si = SCRIPT_ITEM(
2457 iptr->isn_arg.loadstore.ls_sid);
2458
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002459 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002460 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 break;
2463 case ISN_STORESCRIPT:
2464 {
2465 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002466 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2468 + iptr->isn_arg.script.script_idx;
2469
2470 smsg("%4d STORESCRIPT %s in %s", current,
2471 sv->sv_name, si->sn_name);
2472 }
2473 break;
2474 case ISN_STOREOPT:
2475 smsg("%4d STOREOPT &%s", current,
2476 iptr->isn_arg.storeopt.so_name);
2477 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002478 case ISN_STOREENV:
2479 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2480 break;
2481 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002482 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002483 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 case ISN_STORENR:
2485 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002486 iptr->isn_arg.storenr.stnr_val,
2487 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 break;
2489
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002490 case ISN_STORELIST:
2491 smsg("%4d STORELIST", current);
2492 break;
2493
2494 case ISN_STOREDICT:
2495 smsg("%4d STOREDICT", current);
2496 break;
2497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 // constants
2499 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002500 smsg("%4d PUSHNR %lld", current,
2501 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 break;
2503 case ISN_PUSHBOOL:
2504 case ISN_PUSHSPEC:
2505 smsg("%4d PUSH %s", current,
2506 get_var_special_name(iptr->isn_arg.number));
2507 break;
2508 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002509#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002511#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 break;
2513 case ISN_PUSHS:
2514 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2515 break;
2516 case ISN_PUSHBLOB:
2517 {
2518 char_u *r;
2519 char_u numbuf[NUMBUFLEN];
2520 char_u *tofree;
2521
2522 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002523 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 vim_free(tofree);
2525 }
2526 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002527 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002528 {
2529 char *name = (char *)iptr->isn_arg.string;
2530
2531 smsg("%4d PUSHFUNC \"%s\"", current,
2532 name == NULL ? "[none]" : name);
2533 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002534 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002535 case ISN_PUSHCHANNEL:
2536#ifdef FEAT_JOB_CHANNEL
2537 {
2538 channel_T *channel = iptr->isn_arg.channel;
2539
2540 smsg("%4d PUSHCHANNEL %d", current,
2541 channel == NULL ? 0 : channel->ch_id);
2542 }
2543#endif
2544 break;
2545 case ISN_PUSHJOB:
2546#ifdef FEAT_JOB_CHANNEL
2547 {
2548 typval_T tv;
2549 char_u *name;
2550
2551 tv.v_type = VAR_JOB;
2552 tv.vval.v_job = iptr->isn_arg.job;
2553 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002554 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002555 }
2556#endif
2557 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 case ISN_PUSHEXC:
2559 smsg("%4d PUSH v:exception", current);
2560 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002561 case ISN_UNLET:
2562 smsg("%4d UNLET%s %s", current,
2563 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2564 iptr->isn_arg.unlet.ul_name);
2565 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002566 case ISN_UNLETENV:
2567 smsg("%4d UNLETENV%s $%s", current,
2568 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2569 iptr->isn_arg.unlet.ul_name);
2570 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002572 smsg("%4d NEWLIST size %lld", current,
2573 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 break;
2575 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002576 smsg("%4d NEWDICT size %lld", current,
2577 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 break;
2579
2580 // function call
2581 case ISN_BCALL:
2582 {
2583 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2584
2585 smsg("%4d BCALL %s(argc %d)", current,
2586 internal_func_name(cbfunc->cbf_idx),
2587 cbfunc->cbf_argcount);
2588 }
2589 break;
2590 case ISN_DCALL:
2591 {
2592 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2593 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2594 + cdfunc->cdf_idx;
2595
2596 smsg("%4d DCALL %s(argc %d)", current,
2597 df->df_ufunc->uf_name_exp != NULL
2598 ? df->df_ufunc->uf_name_exp
2599 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2600 }
2601 break;
2602 case ISN_UCALL:
2603 {
2604 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2605
2606 smsg("%4d UCALL %s(argc %d)", current,
2607 cufunc->cuf_name, cufunc->cuf_argcount);
2608 }
2609 break;
2610 case ISN_PCALL:
2611 {
2612 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2613
2614 smsg("%4d PCALL%s (argc %d)", current,
2615 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2616 }
2617 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002618 case ISN_PCALL_END:
2619 smsg("%4d PCALL end", current);
2620 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 case ISN_RETURN:
2622 smsg("%4d RETURN", current);
2623 break;
2624 case ISN_FUNCREF:
2625 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002626 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002628 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002630 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002631 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 }
2633 break;
2634
2635 case ISN_JUMP:
2636 {
2637 char *when = "?";
2638
2639 switch (iptr->isn_arg.jump.jump_when)
2640 {
2641 case JUMP_ALWAYS:
2642 when = "JUMP";
2643 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 case JUMP_AND_KEEP_IF_TRUE:
2645 when = "JUMP_AND_KEEP_IF_TRUE";
2646 break;
2647 case JUMP_IF_FALSE:
2648 when = "JUMP_IF_FALSE";
2649 break;
2650 case JUMP_AND_KEEP_IF_FALSE:
2651 when = "JUMP_AND_KEEP_IF_FALSE";
2652 break;
2653 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002654 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 iptr->isn_arg.jump.jump_where);
2656 }
2657 break;
2658
2659 case ISN_FOR:
2660 {
2661 forloop_T *forloop = &iptr->isn_arg.forloop;
2662
2663 smsg("%4d FOR $%d -> %d", current,
2664 forloop->for_idx, forloop->for_end);
2665 }
2666 break;
2667
2668 case ISN_TRY:
2669 {
2670 try_T *try = &iptr->isn_arg.try;
2671
2672 smsg("%4d TRY catch -> %d, finally -> %d", current,
2673 try->try_catch, try->try_finally);
2674 }
2675 break;
2676 case ISN_CATCH:
2677 // TODO
2678 smsg("%4d CATCH", current);
2679 break;
2680 case ISN_ENDTRY:
2681 smsg("%4d ENDTRY", current);
2682 break;
2683 case ISN_THROW:
2684 smsg("%4d THROW", current);
2685 break;
2686
2687 // expression operations on number
2688 case ISN_OPNR:
2689 case ISN_OPFLOAT:
2690 case ISN_OPANY:
2691 {
2692 char *what;
2693 char *ins;
2694
2695 switch (iptr->isn_arg.op.op_type)
2696 {
2697 case EXPR_MULT: what = "*"; break;
2698 case EXPR_DIV: what = "/"; break;
2699 case EXPR_REM: what = "%"; break;
2700 case EXPR_SUB: what = "-"; break;
2701 case EXPR_ADD: what = "+"; break;
2702 default: what = "???"; break;
2703 }
2704 switch (iptr->isn_type)
2705 {
2706 case ISN_OPNR: ins = "OPNR"; break;
2707 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2708 case ISN_OPANY: ins = "OPANY"; break;
2709 default: ins = "???"; break;
2710 }
2711 smsg("%4d %s %s", current, ins, what);
2712 }
2713 break;
2714
2715 case ISN_COMPAREBOOL:
2716 case ISN_COMPARESPECIAL:
2717 case ISN_COMPARENR:
2718 case ISN_COMPAREFLOAT:
2719 case ISN_COMPARESTRING:
2720 case ISN_COMPAREBLOB:
2721 case ISN_COMPARELIST:
2722 case ISN_COMPAREDICT:
2723 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 case ISN_COMPAREANY:
2725 {
2726 char *p;
2727 char buf[10];
2728 char *type;
2729
2730 switch (iptr->isn_arg.op.op_type)
2731 {
2732 case EXPR_EQUAL: p = "=="; break;
2733 case EXPR_NEQUAL: p = "!="; break;
2734 case EXPR_GREATER: p = ">"; break;
2735 case EXPR_GEQUAL: p = ">="; break;
2736 case EXPR_SMALLER: p = "<"; break;
2737 case EXPR_SEQUAL: p = "<="; break;
2738 case EXPR_MATCH: p = "=~"; break;
2739 case EXPR_IS: p = "is"; break;
2740 case EXPR_ISNOT: p = "isnot"; break;
2741 case EXPR_NOMATCH: p = "!~"; break;
2742 default: p = "???"; break;
2743 }
2744 STRCPY(buf, p);
2745 if (iptr->isn_arg.op.op_ic == TRUE)
2746 strcat(buf, "?");
2747 switch(iptr->isn_type)
2748 {
2749 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2750 case ISN_COMPARESPECIAL:
2751 type = "COMPARESPECIAL"; break;
2752 case ISN_COMPARENR: type = "COMPARENR"; break;
2753 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2754 case ISN_COMPARESTRING:
2755 type = "COMPARESTRING"; break;
2756 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2757 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2758 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2759 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2761 default: type = "???"; break;
2762 }
2763
2764 smsg("%4d %s %s", current, type, buf);
2765 }
2766 break;
2767
2768 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2769 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2770
2771 // expression operations
2772 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2773 case ISN_INDEX: smsg("%4d INDEX", current); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002774 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
2775 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 iptr->isn_arg.string); break;
2777 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2778
2779 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2780 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2781 vartype_name(iptr->isn_arg.type.ct_type),
2782 iptr->isn_arg.type.ct_off);
2783 break;
2784 case ISN_2BOOL: if (iptr->isn_arg.number)
2785 smsg("%4d INVERT (!val)", current);
2786 else
2787 smsg("%4d 2BOOL (!!val)", current);
2788 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002789 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2790 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 break;
2792
2793 case ISN_DROP: smsg("%4d DROP", current); break;
2794 }
2795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796}
2797
2798/*
2799 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2800 * list, etc. Mostly like what JavaScript does, except that empty list and
2801 * empty dictionary are FALSE.
2802 */
2803 int
2804tv2bool(typval_T *tv)
2805{
2806 switch (tv->v_type)
2807 {
2808 case VAR_NUMBER:
2809 return tv->vval.v_number != 0;
2810 case VAR_FLOAT:
2811#ifdef FEAT_FLOAT
2812 return tv->vval.v_float != 0.0;
2813#else
2814 break;
2815#endif
2816 case VAR_PARTIAL:
2817 return tv->vval.v_partial != NULL;
2818 case VAR_FUNC:
2819 case VAR_STRING:
2820 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2821 case VAR_LIST:
2822 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2823 case VAR_DICT:
2824 return tv->vval.v_dict != NULL
2825 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2826 case VAR_BOOL:
2827 case VAR_SPECIAL:
2828 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2829 case VAR_JOB:
2830#ifdef FEAT_JOB_CHANNEL
2831 return tv->vval.v_job != NULL;
2832#else
2833 break;
2834#endif
2835 case VAR_CHANNEL:
2836#ifdef FEAT_JOB_CHANNEL
2837 return tv->vval.v_channel != NULL;
2838#else
2839 break;
2840#endif
2841 case VAR_BLOB:
2842 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2843 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002844 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 case VAR_VOID:
2846 break;
2847 }
2848 return FALSE;
2849}
2850
2851/*
2852 * If "tv" is a string give an error and return FAIL.
2853 */
2854 int
2855check_not_string(typval_T *tv)
2856{
2857 if (tv->v_type == VAR_STRING)
2858 {
2859 emsg(_("E1030: Using a String as a Number"));
2860 clear_tv(tv);
2861 return FAIL;
2862 }
2863 return OK;
2864}
2865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866
2867#endif // FEAT_EVAL