blob: 6fad391d1f281525f2218087bdb04536664b01d6 [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 {
27 int tcd_frame; // ec_frame when ISN_TRY was encountered
28 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
59 int ec_frame; // index in ec_stack: context of ec_dfunc_idx
60
61 garray_T ec_trystack; // stack of trycmd_T values
62 int ec_in_catch; // when TRUE in catch or finally block
63
64 int ec_dfunc_idx; // current function index
65 isn_T *ec_instr; // array with instructions
66 int ec_iidx; // index in ec_instr: instruction to execute
67} ectx_T;
68
69// Get pointer to item relative to the bottom of the stack, -1 is the last one.
70#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
71
72/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010073 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074 */
75 static int
76ufunc_argcount(ufunc_T *ufunc)
77{
78 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
79}
80
81/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010082 * Set the instruction index, depending on omitted arguments, where the default
83 * values are to be computed. If all optional arguments are present, start
84 * with the function body.
85 * The expression evaluation is at the start of the instructions:
86 * 0 -> EVAL default1
87 * STORE arg[-2]
88 * 1 -> EVAL default2
89 * STORE arg[-1]
90 * 2 -> function body
91 */
92 static void
93init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
94{
95 if (ufunc->uf_def_args.ga_len == 0)
96 ectx->ec_iidx = 0;
97 else
98 {
99 int defcount = ufunc->uf_args.ga_len - argcount;
100
101 // If there is a varargs argument defcount can be negative, no defaults
102 // to evaluate then.
103 if (defcount < 0)
104 defcount = 0;
105 ectx->ec_iidx = ufunc->uf_def_arg_idx[
106 ufunc->uf_def_args.ga_len - defcount];
107 }
108}
109
110/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100111 * Call compiled function "cdf_idx" from compiled code.
112 *
113 * Stack has:
114 * - current arguments (already there)
115 * - omitted optional argument (default values) added here
116 * - stack frame:
117 * - pointer to calling function
118 * - Index of next instruction in calling function
119 * - previous frame pointer
120 * - reserved space for local variables
121 */
122 static int
123call_dfunc(int cdf_idx, int argcount, ectx_T *ectx)
124{
125 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
126 ufunc_T *ufunc = dfunc->df_ufunc;
127 int optcount = ufunc_argcount(ufunc) - argcount;
128 int idx;
129
130 if (dfunc->df_deleted)
131 {
132 emsg_funcname(e_func_deleted, ufunc->uf_name);
133 return FAIL;
134 }
135
136 if (ga_grow(&ectx->ec_stack, optcount + 3 + dfunc->df_varcount) == FAIL)
137 return FAIL;
138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 if (optcount < 0)
140 {
141 emsg("argument count wrong?");
142 return FAIL;
143 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100144
145 // Reserve space for omitted optional arguments, filled in soon.
146 // Also any empty varargs argument.
147 ectx->ec_stack.ga_len += optcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148
149 // Store current execution state in stack frame for ISN_RETURN.
150 // TODO: If the actual number of arguments doesn't match what the called
151 // function expects things go bad.
152 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
153 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
154 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame;
155 ectx->ec_frame = ectx->ec_stack.ga_len;
156
157 // Initialize local variables
158 for (idx = 0; idx < dfunc->df_varcount; ++idx)
159 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
160 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + dfunc->df_varcount;
161
162 // Set execution state to the start of the called function.
163 ectx->ec_dfunc_idx = cdf_idx;
164 ectx->ec_instr = dfunc->df_instr;
165 estack_push_ufunc(ETYPE_UFUNC, dfunc->df_ufunc, 1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100166
167 // Decide where to start execution, handles optional arguments.
168 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169
170 return OK;
171}
172
173// Get pointer to item in the stack.
174#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
175
176/*
177 * Return from the current function.
178 */
179 static void
180func_return(ectx_T *ectx)
181{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 int idx;
183 dfunc_T *dfunc;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100184 int top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100185
186 // execution context goes one level up
187 estack_pop();
188
189 // Clear the local variables and temporary values, but not
190 // the return value.
191 for (idx = ectx->ec_frame + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100192 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100193 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100194
195 // Clear the arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100197 top = ectx->ec_frame - ufunc_argcount(dfunc->df_ufunc);
198 for (idx = top; idx < ectx->ec_frame; ++idx)
199 clear_tv(STACK_TV(idx));
200
201 // Restore the previous frame.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame)->vval.v_number;
203 ectx->ec_iidx = STACK_TV(ectx->ec_frame + 1)->vval.v_number;
204 ectx->ec_frame = STACK_TV(ectx->ec_frame + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
206 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100207
208 // Reset the stack to the position before the call, move the return value
209 // to the top of the stack.
210 idx = ectx->ec_stack.ga_len - 1;
211 ectx->ec_stack.ga_len = top + 1;
212 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213}
214
215#undef STACK_TV
216
217/*
218 * Prepare arguments and rettv for calling a builtin or user function.
219 */
220 static int
221call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
222{
223 int idx;
224 typval_T *tv;
225
226 // Move arguments from bottom of the stack to argvars[] and add terminator.
227 for (idx = 0; idx < argcount; ++idx)
228 argvars[idx] = *STACK_TV_BOT(idx - argcount);
229 argvars[argcount].v_type = VAR_UNKNOWN;
230
231 // Result replaces the arguments on the stack.
232 if (argcount > 0)
233 ectx->ec_stack.ga_len -= argcount - 1;
234 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
235 return FAIL;
236 else
237 ++ectx->ec_stack.ga_len;
238
239 // Default return value is zero.
240 tv = STACK_TV_BOT(-1);
241 tv->v_type = VAR_NUMBER;
242 tv->vval.v_number = 0;
243
244 return OK;
245}
246
247/*
248 * Call a builtin function by index.
249 */
250 static int
251call_bfunc(int func_idx, int argcount, ectx_T *ectx)
252{
253 typval_T argvars[MAX_FUNC_ARGS];
254 int idx;
255
256 if (call_prepare(argcount, argvars, ectx) == FAIL)
257 return FAIL;
258
259 // Call the builtin function.
260 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
261
262 // Clear the arguments.
263 for (idx = 0; idx < argcount; ++idx)
264 clear_tv(&argvars[idx]);
265 return OK;
266}
267
268/*
269 * Execute a user defined function.
270 */
271 static int
272call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx)
273{
274 typval_T argvars[MAX_FUNC_ARGS];
275 funcexe_T funcexe;
276 int error;
277 int idx;
278
279 if (ufunc->uf_dfunc_idx >= 0)
280 // The function has been compiled, can call it quickly.
281 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
282
283 if (call_prepare(argcount, argvars, ectx) == FAIL)
284 return FAIL;
285 vim_memset(&funcexe, 0, sizeof(funcexe));
286 funcexe.evaluate = TRUE;
287
288 // Call the user function. Result goes in last position on the stack.
289 // TODO: add selfdict if there is one
290 error = call_user_func_check(ufunc, argcount, argvars,
291 STACK_TV_BOT(-1), &funcexe, NULL);
292
293 // Clear the arguments.
294 for (idx = 0; idx < argcount; ++idx)
295 clear_tv(&argvars[idx]);
296
297 if (error != FCERR_NONE)
298 {
299 user_func_error(error, ufunc->uf_name);
300 return FAIL;
301 }
302 return OK;
303}
304
305/*
306 * Execute a function by "name".
307 * This can be a builtin function or a user function.
308 * Returns FAIL if not found without an error message.
309 */
310 static int
311call_by_name(char_u *name, int argcount, ectx_T *ectx)
312{
313 ufunc_T *ufunc;
314
315 if (builtin_function(name, -1))
316 {
317 int func_idx = find_internal_func(name);
318
319 if (func_idx < 0)
320 return FAIL;
321 if (check_internal_func(func_idx, argcount) == FAIL)
322 return FAIL;
323 return call_bfunc(func_idx, argcount, ectx);
324 }
325
326 ufunc = find_func(name, NULL);
327 if (ufunc != NULL)
328 return call_ufunc(ufunc, argcount, ectx);
329
330 return FAIL;
331}
332
333 static int
334call_partial(typval_T *tv, int argcount, ectx_T *ectx)
335{
336 char_u *name;
337 int called_emsg_before = called_emsg;
338
339 if (tv->v_type == VAR_PARTIAL)
340 {
341 partial_T *pt = tv->vval.v_partial;
342
343 if (pt->pt_func != NULL)
344 return call_ufunc(pt->pt_func, argcount, ectx);
345 name = pt->pt_name;
346 }
347 else
348 name = tv->vval.v_string;
349 if (call_by_name(name, argcount, ectx) == FAIL)
350 {
351 if (called_emsg == called_emsg_before)
352 semsg(_(e_unknownfunc), name);
353 return FAIL;
354 }
355 return OK;
356}
357
358/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100359 * Store "tv" in variable "name".
360 * This is for s: and g: variables.
361 */
362 static void
363store_var(char_u *name, typval_T *tv)
364{
365 funccal_entry_T entry;
366
367 save_funccal(&entry);
368 set_var_const(name, NULL, tv, FALSE, 0);
369 restore_funccal();
370}
371
372/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373 * Execute a function by "name".
374 * This can be a builtin function, user function or a funcref.
375 */
376 static int
377call_eval_func(char_u *name, int argcount, ectx_T *ectx)
378{
379 int called_emsg_before = called_emsg;
380
381 if (call_by_name(name, argcount, ectx) == FAIL
382 && called_emsg == called_emsg_before)
383 {
384 // "name" may be a variable that is a funcref or partial
385 // if find variable
386 // call_partial()
387 // else
388 // semsg(_(e_unknownfunc), name);
389 emsg("call_eval_func(partial) not implemented yet");
390 return FAIL;
391 }
392 return OK;
393}
394
395/*
396 * Call a "def" function from old Vim script.
397 * Return OK or FAIL.
398 */
399 int
400call_def_function(
401 ufunc_T *ufunc,
402 int argc, // nr of arguments
403 typval_T *argv, // arguments
404 typval_T *rettv) // return value
405{
406 ectx_T ectx; // execution context
407 int initial_frame_ptr;
408 typval_T *tv;
409 int idx;
410 int ret = FAIL;
411 dfunc_T *dfunc;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100412 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413
414// Get pointer to item in the stack.
415#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
416
417// Get pointer to item at the bottom of the stack, -1 is the bottom.
418#undef STACK_TV_BOT
419#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
420
421// Get pointer to local variable on the stack.
422#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame + STACK_FRAME_SIZE + idx)
423
424 vim_memset(&ectx, 0, sizeof(ectx));
425 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
426 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
427 goto failed;
428 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
429
430 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
431
432 // Put arguments on the stack.
433 for (idx = 0; idx < argc; ++idx)
434 {
435 copy_tv(&argv[idx], STACK_TV_BOT(0));
436 ++ectx.ec_stack.ga_len;
437 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100438 // Make space for omitted arguments, will store default value below.
439 if (defcount > 0)
440 for (idx = 0; idx < defcount; ++idx)
441 {
442 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
443 ++ectx.ec_stack.ga_len;
444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100445
446 // Frame pointer points to just after arguments.
447 ectx.ec_frame = ectx.ec_stack.ga_len;
448 initial_frame_ptr = ectx.ec_frame;
449
450 // dummy frame entries
451 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
452 {
453 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
454 ++ectx.ec_stack.ga_len;
455 }
456
457 // Reserve space for local variables.
458 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
459 for (idx = 0; idx < dfunc->df_varcount; ++idx)
460 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
461 ectx.ec_stack.ga_len += dfunc->df_varcount;
462
463 ectx.ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100464
465 // Decide where to start execution, handles optional arguments.
466 init_instr_idx(ufunc, argc, &ectx);
467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468 for (;;)
469 {
470 isn_T *iptr;
471 trycmd_T *trycmd = NULL;
472
473 if (did_throw && !ectx.ec_in_catch)
474 {
475 garray_T *trystack = &ectx.ec_trystack;
476
477 // An exception jumps to the first catch, finally, or returns from
478 // the current function.
479 if (trystack->ga_len > 0)
480 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
481 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame)
482 {
483 // jump to ":catch" or ":finally"
484 ectx.ec_in_catch = TRUE;
485 ectx.ec_iidx = trycmd->tcd_catch_idx;
486 }
487 else
488 {
489 // not inside try or need to return from current functions.
490 if (ectx.ec_frame == initial_frame_ptr)
491 {
492 // At the toplevel we are done. Push a dummy return value.
493 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
494 goto failed;
495 tv = STACK_TV_BOT(0);
496 tv->v_type = VAR_NUMBER;
497 tv->vval.v_number = 0;
498 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100499 need_rethrow = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 goto done;
501 }
502
503 func_return(&ectx);
504 }
505 continue;
506 }
507
508 iptr = &ectx.ec_instr[ectx.ec_iidx++];
509 switch (iptr->isn_type)
510 {
511 // execute Ex command line
512 case ISN_EXEC:
513 do_cmdline_cmd(iptr->isn_arg.string);
514 break;
515
516 // execute :echo {string} ...
517 case ISN_ECHO:
518 {
519 int count = iptr->isn_arg.echo.echo_count;
520 int atstart = TRUE;
521 int needclr = TRUE;
522
523 for (idx = 0; idx < count; ++idx)
524 {
525 tv = STACK_TV_BOT(idx - count);
526 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
527 &atstart, &needclr);
528 clear_tv(tv);
529 }
530 ectx.ec_stack.ga_len -= count;
531 }
532 break;
533
534 // load local variable or argument
535 case ISN_LOAD:
536 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
537 goto failed;
538 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
539 ++ectx.ec_stack.ga_len;
540 break;
541
542 // load v: variable
543 case ISN_LOADV:
544 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
545 goto failed;
546 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
547 ++ectx.ec_stack.ga_len;
548 break;
549
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100550 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 case ISN_LOADSCRIPT:
552 {
553 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100554 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 svar_T *sv;
556
557 sv = ((svar_T *)si->sn_var_vals.ga_data)
558 + iptr->isn_arg.script.script_idx;
559 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
560 goto failed;
561 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
562 ++ectx.ec_stack.ga_len;
563 }
564 break;
565
566 // load s: variable in old script
567 case ISN_LOADS:
568 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100569 hashtab_T *ht = &SCRIPT_VARS(
570 iptr->isn_arg.loadstore.ls_sid);
571 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 if (di == NULL)
575 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100576 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 goto failed;
578 }
579 else
580 {
581 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
582 goto failed;
583 copy_tv(&di->di_tv, STACK_TV_BOT(0));
584 ++ectx.ec_stack.ga_len;
585 }
586 }
587 break;
588
589 // load g: variable
590 case ISN_LOADG:
591 {
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100592 dictitem_T *di = find_var_in_ht(get_globvar_ht(), 0,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595 if (di == NULL)
596 {
597 semsg(_("E121: Undefined variable: g:%s"),
598 iptr->isn_arg.string);
599 goto failed;
600 }
601 else
602 {
603 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
604 goto failed;
605 copy_tv(&di->di_tv, STACK_TV_BOT(0));
606 ++ectx.ec_stack.ga_len;
607 }
608 }
609 break;
610
611 // load &option
612 case ISN_LOADOPT:
613 {
614 typval_T optval;
615 char_u *name = iptr->isn_arg.string;
616
617 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
618 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100619 if (get_option_tv(&name, &optval, TRUE) == FAIL)
620 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 *STACK_TV_BOT(0) = optval;
622 ++ectx.ec_stack.ga_len;
623 }
624 break;
625
626 // load $ENV
627 case ISN_LOADENV:
628 {
629 typval_T optval;
630 char_u *name = iptr->isn_arg.string;
631
632 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
633 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100634 // name is always valid, checked when compiling
635 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 *STACK_TV_BOT(0) = optval;
637 ++ectx.ec_stack.ga_len;
638 }
639 break;
640
641 // load @register
642 case ISN_LOADREG:
643 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
644 goto failed;
645 tv = STACK_TV_BOT(0);
646 tv->v_type = VAR_STRING;
647 tv->vval.v_string = get_reg_contents(
648 iptr->isn_arg.number, GREG_EXPR_SRC);
649 ++ectx.ec_stack.ga_len;
650 break;
651
652 // store local variable
653 case ISN_STORE:
654 --ectx.ec_stack.ga_len;
655 tv = STACK_TV_VAR(iptr->isn_arg.number);
656 clear_tv(tv);
657 *tv = *STACK_TV_BOT(0);
658 break;
659
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100660 // store s: variable in old script
661 case ISN_STORES:
662 {
663 hashtab_T *ht = &SCRIPT_VARS(
664 iptr->isn_arg.loadstore.ls_sid);
665 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100666 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100667
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100668 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100669 if (di == NULL)
670 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
671 else
672 {
673 clear_tv(&di->di_tv);
674 di->di_tv = *STACK_TV_BOT(0);
675 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100676 }
677 break;
678
679 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680 case ISN_STORESCRIPT:
681 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100682 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 iptr->isn_arg.script.script_sid);
684 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
685 + iptr->isn_arg.script.script_idx;
686
687 --ectx.ec_stack.ga_len;
688 clear_tv(sv->sv_tv);
689 *sv->sv_tv = *STACK_TV_BOT(0);
690 }
691 break;
692
693 // store option
694 case ISN_STOREOPT:
695 {
696 long n = 0;
697 char_u *s = NULL;
698 char *msg;
699
700 --ectx.ec_stack.ga_len;
701 tv = STACK_TV_BOT(0);
702 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100703 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100705 if (s == NULL)
706 s = (char_u *)"";
707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 else if (tv->v_type == VAR_NUMBER)
709 n = tv->vval.v_number;
710 else
711 {
712 emsg(_("E1051: Expected string or number"));
713 goto failed;
714 }
715 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
716 n, s, iptr->isn_arg.storeopt.so_flags);
717 if (msg != NULL)
718 {
719 emsg(_(msg));
720 goto failed;
721 }
722 clear_tv(tv);
723 }
724 break;
725
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100726 // store $ENV
727 case ISN_STOREENV:
728 --ectx.ec_stack.ga_len;
729 vim_setenv_ext(iptr->isn_arg.string,
730 tv_get_string(STACK_TV_BOT(0)));
731 break;
732
733 // store @r
734 case ISN_STOREREG:
735 {
736 int reg = iptr->isn_arg.number;
737
738 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100739 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100740 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100741 tv_get_string(tv), -1, FALSE);
742 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100743 }
744 break;
745
746 // store v: variable
747 case ISN_STOREV:
748 --ectx.ec_stack.ga_len;
749 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
750 == FAIL)
751 goto failed;
752 break;
753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 // store g: variable
755 case ISN_STOREG:
756 {
757 dictitem_T *di;
758
759 --ectx.ec_stack.ga_len;
760 di = find_var_in_ht(get_globvar_ht(), 0,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100761 iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100763 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 else
765 {
766 clear_tv(&di->di_tv);
767 di->di_tv = *STACK_TV_BOT(0);
768 }
769 }
770 break;
771
772 // store number in local variable
773 case ISN_STORENR:
774 tv = STACK_TV_VAR(iptr->isn_arg.storenr.str_idx);
775 clear_tv(tv);
776 tv->v_type = VAR_NUMBER;
777 tv->vval.v_number = iptr->isn_arg.storenr.str_val;
778 break;
779
780 // push constant
781 case ISN_PUSHNR:
782 case ISN_PUSHBOOL:
783 case ISN_PUSHSPEC:
784 case ISN_PUSHF:
785 case ISN_PUSHS:
786 case ISN_PUSHBLOB:
787 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
788 goto failed;
789 tv = STACK_TV_BOT(0);
790 ++ectx.ec_stack.ga_len;
791 switch (iptr->isn_type)
792 {
793 case ISN_PUSHNR:
794 tv->v_type = VAR_NUMBER;
795 tv->vval.v_number = iptr->isn_arg.number;
796 break;
797 case ISN_PUSHBOOL:
798 tv->v_type = VAR_BOOL;
799 tv->vval.v_number = iptr->isn_arg.number;
800 break;
801 case ISN_PUSHSPEC:
802 tv->v_type = VAR_SPECIAL;
803 tv->vval.v_number = iptr->isn_arg.number;
804 break;
805#ifdef FEAT_FLOAT
806 case ISN_PUSHF:
807 tv->v_type = VAR_FLOAT;
808 tv->vval.v_float = iptr->isn_arg.fnumber;
809 break;
810#endif
811 case ISN_PUSHBLOB:
812 blob_copy(iptr->isn_arg.blob, tv);
813 break;
814 default:
815 tv->v_type = VAR_STRING;
816 tv->vval.v_string = vim_strsave(iptr->isn_arg.string);
817 }
818 break;
819
820 // create a list from items on the stack; uses a single allocation
821 // for the list header and the items
822 case ISN_NEWLIST:
823 {
824 int count = iptr->isn_arg.number;
825 list_T *list = list_alloc_with_items(count);
826
827 if (list == NULL)
828 goto failed;
829 for (idx = 0; idx < count; ++idx)
830 list_set_item(list, idx, STACK_TV_BOT(idx - count));
831
832 if (count > 0)
833 ectx.ec_stack.ga_len -= count - 1;
834 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
835 goto failed;
836 else
837 ++ectx.ec_stack.ga_len;
838 tv = STACK_TV_BOT(-1);
839 tv->v_type = VAR_LIST;
840 tv->vval.v_list = list;
841 ++list->lv_refcount;
842 }
843 break;
844
845 // create a dict from items on the stack
846 case ISN_NEWDICT:
847 {
848 int count = iptr->isn_arg.number;
849 dict_T *dict = dict_alloc();
850 dictitem_T *item;
851
852 if (dict == NULL)
853 goto failed;
854 for (idx = 0; idx < count; ++idx)
855 {
856 // check key type is VAR_STRING
857 tv = STACK_TV_BOT(2 * (idx - count));
858 item = dictitem_alloc(tv->vval.v_string);
859 clear_tv(tv);
860 if (item == NULL)
861 goto failed;
862 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
863 item->di_tv.v_lock = 0;
864 if (dict_add(dict, item) == FAIL)
865 goto failed;
866 }
867
868 if (count > 0)
869 ectx.ec_stack.ga_len -= 2 * count - 1;
870 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
871 goto failed;
872 else
873 ++ectx.ec_stack.ga_len;
874 tv = STACK_TV_BOT(-1);
875 tv->v_type = VAR_DICT;
876 tv->vval.v_dict = dict;
877 ++dict->dv_refcount;
878 }
879 break;
880
881 // call a :def function
882 case ISN_DCALL:
883 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
884 iptr->isn_arg.dfunc.cdf_argcount,
885 &ectx) == FAIL)
886 goto failed;
887 break;
888
889 // call a builtin function
890 case ISN_BCALL:
891 SOURCING_LNUM = iptr->isn_lnum;
892 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
893 iptr->isn_arg.bfunc.cbf_argcount,
894 &ectx) == FAIL)
895 goto failed;
896 break;
897
898 // call a funcref or partial
899 case ISN_PCALL:
900 {
901 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
902 int r;
903 typval_T partial;
904
905 SOURCING_LNUM = iptr->isn_lnum;
906 if (pfunc->cpf_top)
907 {
908 // funcref is above the arguments
909 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
910 }
911 else
912 {
913 // Get the funcref from the stack.
914 --ectx.ec_stack.ga_len;
915 partial = *STACK_TV_BOT(0);
916 tv = &partial;
917 }
918 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
919 if (tv == &partial)
920 clear_tv(&partial);
921 if (r == FAIL)
922 goto failed;
923
924 if (pfunc->cpf_top)
925 {
926 // Get the funcref from the stack, overwrite with the
927 // return value.
928 clear_tv(tv);
929 --ectx.ec_stack.ga_len;
930 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
931 }
932 }
933 break;
934
935 // call a user defined function or funcref/partial
936 case ISN_UCALL:
937 {
938 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
939
940 SOURCING_LNUM = iptr->isn_lnum;
941 if (call_eval_func(cufunc->cuf_name,
942 cufunc->cuf_argcount, &ectx) == FAIL)
943 goto failed;
944 }
945 break;
946
947 // return from a :def function call
948 case ISN_RETURN:
949 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +0100950 garray_T *trystack = &ectx.ec_trystack;
951
952 if (trystack->ga_len > 0)
953 trycmd = ((trycmd_T *)trystack->ga_data)
954 + trystack->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame
956 && trycmd->tcd_finally_idx != 0)
957 {
958 // jump to ":finally"
959 ectx.ec_iidx = trycmd->tcd_finally_idx;
960 trycmd->tcd_return = TRUE;
961 }
962 else
963 {
964 // Restore previous function. If the frame pointer
965 // is zero then there is none and we are done.
966 if (ectx.ec_frame == initial_frame_ptr)
967 goto done;
968
969 func_return(&ectx);
970 }
971 }
972 break;
973
974 // push a function reference to a compiled function
975 case ISN_FUNCREF:
976 {
977 partial_T *pt = NULL;
978
979 pt = ALLOC_CLEAR_ONE(partial_T);
980 if (pt == NULL)
981 goto failed;
982 dfunc = ((dfunc_T *)def_functions.ga_data)
983 + iptr->isn_arg.number;
984 pt->pt_func = dfunc->df_ufunc;
985 pt->pt_refcount = 1;
986 ++dfunc->df_ufunc->uf_refcount;
987
988 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
989 goto failed;
990 tv = STACK_TV_BOT(0);
991 ++ectx.ec_stack.ga_len;
992 tv->vval.v_partial = pt;
993 tv->v_type = VAR_PARTIAL;
994 }
995 break;
996
997 // jump if a condition is met
998 case ISN_JUMP:
999 {
1000 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1001 int jump = TRUE;
1002
1003 if (when != JUMP_ALWAYS)
1004 {
1005 tv = STACK_TV_BOT(-1);
1006 jump = tv2bool(tv);
1007 if (when == JUMP_IF_FALSE
1008 || when == JUMP_AND_KEEP_IF_FALSE)
1009 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001010 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 {
1012 // drop the value from the stack
1013 clear_tv(tv);
1014 --ectx.ec_stack.ga_len;
1015 }
1016 }
1017 if (jump)
1018 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1019 }
1020 break;
1021
1022 // top of a for loop
1023 case ISN_FOR:
1024 {
1025 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1026 typval_T *idxtv =
1027 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1028
1029 // push the next item from the list
1030 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1031 goto failed;
1032 if (++idxtv->vval.v_number >= list->lv_len)
1033 // past the end of the list, jump to "endfor"
1034 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1035 else if (list->lv_first == &range_list_item)
1036 {
1037 // non-materialized range() list
1038 tv = STACK_TV_BOT(0);
1039 tv->v_type = VAR_NUMBER;
1040 tv->vval.v_number = list_find_nr(
1041 list, idxtv->vval.v_number, NULL);
1042 ++ectx.ec_stack.ga_len;
1043 }
1044 else
1045 {
1046 listitem_T *li = list_find(list, idxtv->vval.v_number);
1047
1048 if (li == NULL)
1049 goto failed;
1050 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1051 ++ectx.ec_stack.ga_len;
1052 }
1053 }
1054 break;
1055
1056 // start of ":try" block
1057 case ISN_TRY:
1058 {
1059 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1060 goto failed;
1061 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1062 + ectx.ec_trystack.ga_len;
1063 ++ectx.ec_trystack.ga_len;
1064 ++trylevel;
1065 trycmd->tcd_frame = ectx.ec_frame;
1066 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1067 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
1068 }
1069 break;
1070
1071 case ISN_PUSHEXC:
1072 if (current_exception == NULL)
1073 {
1074 iemsg("Evaluating catch while current_exception is NULL");
1075 goto failed;
1076 }
1077 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1078 goto failed;
1079 tv = STACK_TV_BOT(0);
1080 ++ectx.ec_stack.ga_len;
1081 tv->v_type = VAR_STRING;
1082 tv->vval.v_string = vim_strsave(
1083 (char_u *)current_exception->value);
1084 break;
1085
1086 case ISN_CATCH:
1087 {
1088 garray_T *trystack = &ectx.ec_trystack;
1089
1090 if (trystack->ga_len > 0)
1091 {
1092 trycmd = ((trycmd_T *)trystack->ga_data)
1093 + trystack->ga_len - 1;
1094 trycmd->tcd_caught = TRUE;
1095 }
1096 did_emsg = got_int = did_throw = FALSE;
1097 catch_exception(current_exception);
1098 }
1099 break;
1100
1101 // end of ":try" block
1102 case ISN_ENDTRY:
1103 {
1104 garray_T *trystack = &ectx.ec_trystack;
1105
1106 if (trystack->ga_len > 0)
1107 {
1108 --trystack->ga_len;
1109 --trylevel;
1110 trycmd = ((trycmd_T *)trystack->ga_data)
1111 + trystack->ga_len;
1112 if (trycmd->tcd_caught)
1113 {
1114 // discard the exception
1115 if (caught_stack == current_exception)
1116 caught_stack = caught_stack->caught;
1117 discard_current_exception();
1118 }
1119
1120 if (trycmd->tcd_return)
1121 {
1122 // Restore previous function. If the frame pointer
1123 // is zero then there is none and we are done.
1124 if (ectx.ec_frame == initial_frame_ptr)
1125 goto done;
1126
1127 func_return(&ectx);
1128 }
1129 }
1130 }
1131 break;
1132
1133 case ISN_THROW:
1134 --ectx.ec_stack.ga_len;
1135 tv = STACK_TV_BOT(0);
1136 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1137 {
1138 vim_free(tv->vval.v_string);
1139 goto failed;
1140 }
1141 did_throw = TRUE;
1142 break;
1143
1144 // compare with special values
1145 case ISN_COMPAREBOOL:
1146 case ISN_COMPARESPECIAL:
1147 {
1148 typval_T *tv1 = STACK_TV_BOT(-2);
1149 typval_T *tv2 = STACK_TV_BOT(-1);
1150 varnumber_T arg1 = tv1->vval.v_number;
1151 varnumber_T arg2 = tv2->vval.v_number;
1152 int res;
1153
1154 switch (iptr->isn_arg.op.op_type)
1155 {
1156 case EXPR_EQUAL: res = arg1 == arg2; break;
1157 case EXPR_NEQUAL: res = arg1 != arg2; break;
1158 default: res = 0; break;
1159 }
1160
1161 --ectx.ec_stack.ga_len;
1162 tv1->v_type = VAR_BOOL;
1163 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1164 }
1165 break;
1166
1167 // Operation with two number arguments
1168 case ISN_OPNR:
1169 case ISN_COMPARENR:
1170 {
1171 typval_T *tv1 = STACK_TV_BOT(-2);
1172 typval_T *tv2 = STACK_TV_BOT(-1);
1173 varnumber_T arg1 = tv1->vval.v_number;
1174 varnumber_T arg2 = tv2->vval.v_number;
1175 varnumber_T res;
1176
1177 switch (iptr->isn_arg.op.op_type)
1178 {
1179 case EXPR_MULT: res = arg1 * arg2; break;
1180 case EXPR_DIV: res = arg1 / arg2; break;
1181 case EXPR_REM: res = arg1 % arg2; break;
1182 case EXPR_SUB: res = arg1 - arg2; break;
1183 case EXPR_ADD: res = arg1 + arg2; break;
1184
1185 case EXPR_EQUAL: res = arg1 == arg2; break;
1186 case EXPR_NEQUAL: res = arg1 != arg2; break;
1187 case EXPR_GREATER: res = arg1 > arg2; break;
1188 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1189 case EXPR_SMALLER: res = arg1 < arg2; break;
1190 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1191 default: res = 0; break;
1192 }
1193
1194 --ectx.ec_stack.ga_len;
1195 if (iptr->isn_type == ISN_COMPARENR)
1196 {
1197 tv1->v_type = VAR_BOOL;
1198 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1199 }
1200 else
1201 tv1->vval.v_number = res;
1202 }
1203 break;
1204
1205 // Computation with two float arguments
1206 case ISN_OPFLOAT:
1207 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001208#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 {
1210 typval_T *tv1 = STACK_TV_BOT(-2);
1211 typval_T *tv2 = STACK_TV_BOT(-1);
1212 float_T arg1 = tv1->vval.v_float;
1213 float_T arg2 = tv2->vval.v_float;
1214 float_T res = 0;
1215 int cmp = FALSE;
1216
1217 switch (iptr->isn_arg.op.op_type)
1218 {
1219 case EXPR_MULT: res = arg1 * arg2; break;
1220 case EXPR_DIV: res = arg1 / arg2; break;
1221 case EXPR_SUB: res = arg1 - arg2; break;
1222 case EXPR_ADD: res = arg1 + arg2; break;
1223
1224 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1225 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1226 case EXPR_GREATER: cmp = arg1 > arg2; break;
1227 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1228 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1229 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1230 default: cmp = 0; break;
1231 }
1232 --ectx.ec_stack.ga_len;
1233 if (iptr->isn_type == ISN_COMPAREFLOAT)
1234 {
1235 tv1->v_type = VAR_BOOL;
1236 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1237 }
1238 else
1239 tv1->vval.v_float = res;
1240 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001241#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 break;
1243
1244 case ISN_COMPARELIST:
1245 {
1246 typval_T *tv1 = STACK_TV_BOT(-2);
1247 typval_T *tv2 = STACK_TV_BOT(-1);
1248 list_T *arg1 = tv1->vval.v_list;
1249 list_T *arg2 = tv2->vval.v_list;
1250 int cmp = FALSE;
1251 int ic = iptr->isn_arg.op.op_ic;
1252
1253 switch (iptr->isn_arg.op.op_type)
1254 {
1255 case EXPR_EQUAL: cmp =
1256 list_equal(arg1, arg2, ic, FALSE); break;
1257 case EXPR_NEQUAL: cmp =
1258 !list_equal(arg1, arg2, ic, FALSE); break;
1259 case EXPR_IS: cmp = arg1 == arg2; break;
1260 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1261 default: cmp = 0; break;
1262 }
1263 --ectx.ec_stack.ga_len;
1264 clear_tv(tv1);
1265 clear_tv(tv2);
1266 tv1->v_type = VAR_BOOL;
1267 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1268 }
1269 break;
1270
1271 case ISN_COMPAREBLOB:
1272 {
1273 typval_T *tv1 = STACK_TV_BOT(-2);
1274 typval_T *tv2 = STACK_TV_BOT(-1);
1275 blob_T *arg1 = tv1->vval.v_blob;
1276 blob_T *arg2 = tv2->vval.v_blob;
1277 int cmp = FALSE;
1278
1279 switch (iptr->isn_arg.op.op_type)
1280 {
1281 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1282 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1283 case EXPR_IS: cmp = arg1 == arg2; break;
1284 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1285 default: cmp = 0; break;
1286 }
1287 --ectx.ec_stack.ga_len;
1288 clear_tv(tv1);
1289 clear_tv(tv2);
1290 tv1->v_type = VAR_BOOL;
1291 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1292 }
1293 break;
1294
1295 // TODO: handle separately
1296 case ISN_COMPARESTRING:
1297 case ISN_COMPAREDICT:
1298 case ISN_COMPAREFUNC:
1299 case ISN_COMPAREPARTIAL:
1300 case ISN_COMPAREANY:
1301 {
1302 typval_T *tv1 = STACK_TV_BOT(-2);
1303 typval_T *tv2 = STACK_TV_BOT(-1);
1304 exptype_T exptype = iptr->isn_arg.op.op_type;
1305 int ic = iptr->isn_arg.op.op_ic;
1306
1307 typval_compare(tv1, tv2, exptype, ic);
1308 clear_tv(tv2);
1309 tv1->v_type = VAR_BOOL;
1310 tv1->vval.v_number = tv1->vval.v_number
1311 ? VVAL_TRUE : VVAL_FALSE;
1312 --ectx.ec_stack.ga_len;
1313 }
1314 break;
1315
1316 case ISN_ADDLIST:
1317 case ISN_ADDBLOB:
1318 {
1319 typval_T *tv1 = STACK_TV_BOT(-2);
1320 typval_T *tv2 = STACK_TV_BOT(-1);
1321
1322 if (iptr->isn_type == ISN_ADDLIST)
1323 eval_addlist(tv1, tv2);
1324 else
1325 eval_addblob(tv1, tv2);
1326 clear_tv(tv2);
1327 --ectx.ec_stack.ga_len;
1328 }
1329 break;
1330
1331 // Computation with two arguments of unknown type
1332 case ISN_OPANY:
1333 {
1334 typval_T *tv1 = STACK_TV_BOT(-2);
1335 typval_T *tv2 = STACK_TV_BOT(-1);
1336 varnumber_T n1, n2;
1337#ifdef FEAT_FLOAT
1338 float_T f1 = 0, f2 = 0;
1339#endif
1340 int error = FALSE;
1341
1342 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1343 {
1344 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1345 {
1346 eval_addlist(tv1, tv2);
1347 clear_tv(tv2);
1348 --ectx.ec_stack.ga_len;
1349 break;
1350 }
1351 else if (tv1->v_type == VAR_BLOB
1352 && tv2->v_type == VAR_BLOB)
1353 {
1354 eval_addblob(tv1, tv2);
1355 clear_tv(tv2);
1356 --ectx.ec_stack.ga_len;
1357 break;
1358 }
1359 }
1360#ifdef FEAT_FLOAT
1361 if (tv1->v_type == VAR_FLOAT)
1362 {
1363 f1 = tv1->vval.v_float;
1364 n1 = 0;
1365 }
1366 else
1367#endif
1368 {
1369 n1 = tv_get_number_chk(tv1, &error);
1370 if (error)
1371 goto failed;
1372#ifdef FEAT_FLOAT
1373 if (tv2->v_type == VAR_FLOAT)
1374 f1 = n1;
1375#endif
1376 }
1377#ifdef FEAT_FLOAT
1378 if (tv2->v_type == VAR_FLOAT)
1379 {
1380 f2 = tv2->vval.v_float;
1381 n2 = 0;
1382 }
1383 else
1384#endif
1385 {
1386 n2 = tv_get_number_chk(tv2, &error);
1387 if (error)
1388 goto failed;
1389#ifdef FEAT_FLOAT
1390 if (tv1->v_type == VAR_FLOAT)
1391 f2 = n2;
1392#endif
1393 }
1394#ifdef FEAT_FLOAT
1395 // if there is a float on either side the result is a float
1396 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1397 {
1398 switch (iptr->isn_arg.op.op_type)
1399 {
1400 case EXPR_MULT: f1 = f1 * f2; break;
1401 case EXPR_DIV: f1 = f1 / f2; break;
1402 case EXPR_SUB: f1 = f1 - f2; break;
1403 case EXPR_ADD: f1 = f1 + f2; break;
1404 default: emsg(_(e_modulus)); goto failed;
1405 }
1406 clear_tv(tv1);
1407 clear_tv(tv2);
1408 tv1->v_type = VAR_FLOAT;
1409 tv1->vval.v_float = f1;
1410 --ectx.ec_stack.ga_len;
1411 }
1412 else
1413#endif
1414 {
1415 switch (iptr->isn_arg.op.op_type)
1416 {
1417 case EXPR_MULT: n1 = n1 * n2; break;
1418 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1419 case EXPR_SUB: n1 = n1 - n2; break;
1420 case EXPR_ADD: n1 = n1 + n2; break;
1421 default: n1 = num_modulus(n1, n2); break;
1422 }
1423 clear_tv(tv1);
1424 clear_tv(tv2);
1425 tv1->v_type = VAR_NUMBER;
1426 tv1->vval.v_number = n1;
1427 --ectx.ec_stack.ga_len;
1428 }
1429 }
1430 break;
1431
1432 case ISN_CONCAT:
1433 {
1434 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1435 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1436 char_u *res;
1437
1438 res = concat_str(str1, str2);
1439 clear_tv(STACK_TV_BOT(-2));
1440 clear_tv(STACK_TV_BOT(-1));
1441 --ectx.ec_stack.ga_len;
1442 STACK_TV_BOT(-1)->vval.v_string = res;
1443 }
1444 break;
1445
1446 case ISN_INDEX:
1447 {
1448 list_T *list;
1449 varnumber_T n;
1450 listitem_T *li;
1451
1452 // list index: list is at stack-2, index at stack-1
1453 tv = STACK_TV_BOT(-2);
1454 if (tv->v_type != VAR_LIST)
1455 {
1456 emsg(_(e_listreq));
1457 goto failed;
1458 }
1459 list = tv->vval.v_list;
1460
1461 tv = STACK_TV_BOT(-1);
1462 if (tv->v_type != VAR_NUMBER)
1463 {
1464 emsg(_(e_number_exp));
1465 goto failed;
1466 }
1467 n = tv->vval.v_number;
1468 clear_tv(tv);
1469 if ((li = list_find(list, n)) == NULL)
1470 {
1471 semsg(_(e_listidx), n);
1472 goto failed;
1473 }
1474 --ectx.ec_stack.ga_len;
1475 clear_tv(STACK_TV_BOT(-1));
1476 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1477 }
1478 break;
1479
1480 // dict member with string key
1481 case ISN_MEMBER:
1482 {
1483 dict_T *dict;
1484 dictitem_T *di;
1485
1486 tv = STACK_TV_BOT(-1);
1487 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1488 {
1489 emsg(_(e_dictreq));
1490 goto failed;
1491 }
1492 dict = tv->vval.v_dict;
1493
1494 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1495 == NULL)
1496 {
1497 semsg(_(e_dictkey), iptr->isn_arg.string);
1498 goto failed;
1499 }
1500 clear_tv(tv);
1501 copy_tv(&di->di_tv, tv);
1502 }
1503 break;
1504
1505 case ISN_NEGATENR:
1506 tv = STACK_TV_BOT(-1);
1507 tv->vval.v_number = -tv->vval.v_number;
1508 break;
1509
1510 case ISN_CHECKNR:
1511 {
1512 int error = FALSE;
1513
1514 tv = STACK_TV_BOT(-1);
1515 if (check_not_string(tv) == FAIL)
1516 {
1517 --ectx.ec_stack.ga_len;
1518 goto failed;
1519 }
1520 (void)tv_get_number_chk(tv, &error);
1521 if (error)
1522 goto failed;
1523 }
1524 break;
1525
1526 case ISN_CHECKTYPE:
1527 {
1528 checktype_T *ct = &iptr->isn_arg.type;
1529
1530 tv = STACK_TV_BOT(ct->ct_off);
1531 if (tv->v_type != ct->ct_type)
1532 {
1533 semsg(_("E1029: Expected %s but got %s"),
1534 vartype_name(ct->ct_type),
1535 vartype_name(tv->v_type));
1536 goto failed;
1537 }
1538 }
1539 break;
1540
1541 case ISN_2BOOL:
1542 {
1543 int n;
1544
1545 tv = STACK_TV_BOT(-1);
1546 n = tv2bool(tv);
1547 if (iptr->isn_arg.number) // invert
1548 n = !n;
1549 clear_tv(tv);
1550 tv->v_type = VAR_BOOL;
1551 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
1552 }
1553 break;
1554
1555 case ISN_2STRING:
1556 {
1557 char_u *str;
1558
1559 tv = STACK_TV_BOT(iptr->isn_arg.number);
1560 if (tv->v_type != VAR_STRING)
1561 {
1562 str = typval_tostring(tv);
1563 clear_tv(tv);
1564 tv->v_type = VAR_STRING;
1565 tv->vval.v_string = str;
1566 }
1567 }
1568 break;
1569
1570 case ISN_DROP:
1571 --ectx.ec_stack.ga_len;
1572 clear_tv(STACK_TV_BOT(0));
1573 break;
1574 }
1575 }
1576
1577done:
1578 // function finished, get result from the stack.
1579 tv = STACK_TV_BOT(-1);
1580 *rettv = *tv;
1581 tv->v_type = VAR_UNKNOWN;
1582 ret = OK;
1583
1584failed:
1585 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
1586 clear_tv(STACK_TV(idx));
1587 vim_free(ectx.ec_stack.ga_data);
1588 return ret;
1589}
1590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591/*
1592 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01001593 * We don't really need this at runtime, but we do have tests that require it,
1594 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 */
1596 void
1597ex_disassemble(exarg_T *eap)
1598{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001599 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001600 char_u *fname;
1601 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 dfunc_T *dfunc;
1603 isn_T *instr;
1604 int current;
1605 int line_idx = 0;
1606 int prev_current = 0;
1607
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001608 fname = trans_function_name(&arg, FALSE,
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001609 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001610 if (fname == NULL)
1611 {
1612 semsg(_(e_invarg2), eap->arg);
1613 return;
1614 }
1615
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001616 ufunc = find_func(fname, NULL);
1617 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 if (ufunc == NULL)
1619 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001620 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 return;
1622 }
1623 if (ufunc->uf_dfunc_idx < 0)
1624 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001625 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 return;
1627 }
1628 if (ufunc->uf_name_exp != NULL)
1629 msg((char *)ufunc->uf_name_exp);
1630 else
1631 msg((char *)ufunc->uf_name);
1632
1633 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
1634 instr = dfunc->df_instr;
1635 for (current = 0; current < dfunc->df_instr_count; ++current)
1636 {
1637 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001638 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639
1640 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
1641 {
1642 if (current > prev_current)
1643 {
1644 msg_puts("\n\n");
1645 prev_current = current;
1646 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001647 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
1648 if (line != NULL)
1649 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 }
1651
1652 switch (iptr->isn_type)
1653 {
1654 case ISN_EXEC:
1655 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
1656 break;
1657 case ISN_ECHO:
1658 {
1659 echo_T *echo = &iptr->isn_arg.echo;
1660
1661 smsg("%4d %s %d", current,
1662 echo->echo_with_white ? "ECHO" : "ECHON",
1663 echo->echo_count);
1664 }
1665 break;
1666 case ISN_LOAD:
1667 if (iptr->isn_arg.number < 0)
1668 smsg("%4d LOAD arg[%lld]", current,
1669 iptr->isn_arg.number + STACK_FRAME_SIZE);
1670 else
1671 smsg("%4d LOAD $%lld", current, iptr->isn_arg.number);
1672 break;
1673 case ISN_LOADV:
1674 smsg("%4d LOADV v:%s", current,
1675 get_vim_var_name(iptr->isn_arg.number));
1676 break;
1677 case ISN_LOADSCRIPT:
1678 {
1679 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001680 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1682 + iptr->isn_arg.script.script_idx;
1683
1684 smsg("%4d LOADSCRIPT %s from %s", current,
1685 sv->sv_name, si->sn_name);
1686 }
1687 break;
1688 case ISN_LOADS:
1689 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001690 scriptitem_T *si = SCRIPT_ITEM(
1691 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692
1693 smsg("%4d LOADS s:%s from %s", current,
1694 iptr->isn_arg.string, si->sn_name);
1695 }
1696 break;
1697 case ISN_LOADG:
1698 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
1699 break;
1700 case ISN_LOADOPT:
1701 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
1702 break;
1703 case ISN_LOADENV:
1704 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
1705 break;
1706 case ISN_LOADREG:
1707 smsg("%4d LOADREG @%c", current, iptr->isn_arg.number);
1708 break;
1709
1710 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001711 if (iptr->isn_arg.number < 0)
1712 smsg("%4d STORE arg[%lld]", current,
1713 iptr->isn_arg.number + STACK_FRAME_SIZE);
1714 else
1715 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001717 case ISN_STOREV:
1718 smsg("%4d STOREV v:%s", current,
1719 get_vim_var_name(iptr->isn_arg.number));
1720 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001722 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
1723 break;
1724 case ISN_STORES:
1725 {
1726 scriptitem_T *si = SCRIPT_ITEM(
1727 iptr->isn_arg.loadstore.ls_sid);
1728
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001729 smsg("%4d STORES %s in %s", current,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001730 iptr->isn_arg.string, si->sn_name);
1731 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732 break;
1733 case ISN_STORESCRIPT:
1734 {
1735 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001736 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1738 + iptr->isn_arg.script.script_idx;
1739
1740 smsg("%4d STORESCRIPT %s in %s", current,
1741 sv->sv_name, si->sn_name);
1742 }
1743 break;
1744 case ISN_STOREOPT:
1745 smsg("%4d STOREOPT &%s", current,
1746 iptr->isn_arg.storeopt.so_name);
1747 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001748 case ISN_STOREENV:
1749 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
1750 break;
1751 case ISN_STOREREG:
1752 smsg("%4d STOREREG @%c", current, iptr->isn_arg.number);
1753 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 case ISN_STORENR:
1755 smsg("%4d STORE %lld in $%d", current,
1756 iptr->isn_arg.storenr.str_val,
1757 iptr->isn_arg.storenr.str_idx);
1758 break;
1759
1760 // constants
1761 case ISN_PUSHNR:
1762 smsg("%4d PUSHNR %lld", current, iptr->isn_arg.number);
1763 break;
1764 case ISN_PUSHBOOL:
1765 case ISN_PUSHSPEC:
1766 smsg("%4d PUSH %s", current,
1767 get_var_special_name(iptr->isn_arg.number));
1768 break;
1769 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001770#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01001772#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 break;
1774 case ISN_PUSHS:
1775 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
1776 break;
1777 case ISN_PUSHBLOB:
1778 {
1779 char_u *r;
1780 char_u numbuf[NUMBUFLEN];
1781 char_u *tofree;
1782
1783 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01001784 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 vim_free(tofree);
1786 }
1787 break;
1788 case ISN_PUSHEXC:
1789 smsg("%4d PUSH v:exception", current);
1790 break;
1791 case ISN_NEWLIST:
1792 smsg("%4d NEWLIST size %lld", current, iptr->isn_arg.number);
1793 break;
1794 case ISN_NEWDICT:
1795 smsg("%4d NEWDICT size %lld", current, iptr->isn_arg.number);
1796 break;
1797
1798 // function call
1799 case ISN_BCALL:
1800 {
1801 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
1802
1803 smsg("%4d BCALL %s(argc %d)", current,
1804 internal_func_name(cbfunc->cbf_idx),
1805 cbfunc->cbf_argcount);
1806 }
1807 break;
1808 case ISN_DCALL:
1809 {
1810 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
1811 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1812 + cdfunc->cdf_idx;
1813
1814 smsg("%4d DCALL %s(argc %d)", current,
1815 df->df_ufunc->uf_name_exp != NULL
1816 ? df->df_ufunc->uf_name_exp
1817 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
1818 }
1819 break;
1820 case ISN_UCALL:
1821 {
1822 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1823
1824 smsg("%4d UCALL %s(argc %d)", current,
1825 cufunc->cuf_name, cufunc->cuf_argcount);
1826 }
1827 break;
1828 case ISN_PCALL:
1829 {
1830 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
1831
1832 smsg("%4d PCALL%s (argc %d)", current,
1833 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
1834 }
1835 break;
1836 case ISN_RETURN:
1837 smsg("%4d RETURN", current);
1838 break;
1839 case ISN_FUNCREF:
1840 {
1841 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1842 + iptr->isn_arg.number;
1843
1844 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
1845 }
1846 break;
1847
1848 case ISN_JUMP:
1849 {
1850 char *when = "?";
1851
1852 switch (iptr->isn_arg.jump.jump_when)
1853 {
1854 case JUMP_ALWAYS:
1855 when = "JUMP";
1856 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 case JUMP_AND_KEEP_IF_TRUE:
1858 when = "JUMP_AND_KEEP_IF_TRUE";
1859 break;
1860 case JUMP_IF_FALSE:
1861 when = "JUMP_IF_FALSE";
1862 break;
1863 case JUMP_AND_KEEP_IF_FALSE:
1864 when = "JUMP_AND_KEEP_IF_FALSE";
1865 break;
1866 }
1867 smsg("%4d %s -> %lld", current, when,
1868 iptr->isn_arg.jump.jump_where);
1869 }
1870 break;
1871
1872 case ISN_FOR:
1873 {
1874 forloop_T *forloop = &iptr->isn_arg.forloop;
1875
1876 smsg("%4d FOR $%d -> %d", current,
1877 forloop->for_idx, forloop->for_end);
1878 }
1879 break;
1880
1881 case ISN_TRY:
1882 {
1883 try_T *try = &iptr->isn_arg.try;
1884
1885 smsg("%4d TRY catch -> %d, finally -> %d", current,
1886 try->try_catch, try->try_finally);
1887 }
1888 break;
1889 case ISN_CATCH:
1890 // TODO
1891 smsg("%4d CATCH", current);
1892 break;
1893 case ISN_ENDTRY:
1894 smsg("%4d ENDTRY", current);
1895 break;
1896 case ISN_THROW:
1897 smsg("%4d THROW", current);
1898 break;
1899
1900 // expression operations on number
1901 case ISN_OPNR:
1902 case ISN_OPFLOAT:
1903 case ISN_OPANY:
1904 {
1905 char *what;
1906 char *ins;
1907
1908 switch (iptr->isn_arg.op.op_type)
1909 {
1910 case EXPR_MULT: what = "*"; break;
1911 case EXPR_DIV: what = "/"; break;
1912 case EXPR_REM: what = "%"; break;
1913 case EXPR_SUB: what = "-"; break;
1914 case EXPR_ADD: what = "+"; break;
1915 default: what = "???"; break;
1916 }
1917 switch (iptr->isn_type)
1918 {
1919 case ISN_OPNR: ins = "OPNR"; break;
1920 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
1921 case ISN_OPANY: ins = "OPANY"; break;
1922 default: ins = "???"; break;
1923 }
1924 smsg("%4d %s %s", current, ins, what);
1925 }
1926 break;
1927
1928 case ISN_COMPAREBOOL:
1929 case ISN_COMPARESPECIAL:
1930 case ISN_COMPARENR:
1931 case ISN_COMPAREFLOAT:
1932 case ISN_COMPARESTRING:
1933 case ISN_COMPAREBLOB:
1934 case ISN_COMPARELIST:
1935 case ISN_COMPAREDICT:
1936 case ISN_COMPAREFUNC:
1937 case ISN_COMPAREPARTIAL:
1938 case ISN_COMPAREANY:
1939 {
1940 char *p;
1941 char buf[10];
1942 char *type;
1943
1944 switch (iptr->isn_arg.op.op_type)
1945 {
1946 case EXPR_EQUAL: p = "=="; break;
1947 case EXPR_NEQUAL: p = "!="; break;
1948 case EXPR_GREATER: p = ">"; break;
1949 case EXPR_GEQUAL: p = ">="; break;
1950 case EXPR_SMALLER: p = "<"; break;
1951 case EXPR_SEQUAL: p = "<="; break;
1952 case EXPR_MATCH: p = "=~"; break;
1953 case EXPR_IS: p = "is"; break;
1954 case EXPR_ISNOT: p = "isnot"; break;
1955 case EXPR_NOMATCH: p = "!~"; break;
1956 default: p = "???"; break;
1957 }
1958 STRCPY(buf, p);
1959 if (iptr->isn_arg.op.op_ic == TRUE)
1960 strcat(buf, "?");
1961 switch(iptr->isn_type)
1962 {
1963 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
1964 case ISN_COMPARESPECIAL:
1965 type = "COMPARESPECIAL"; break;
1966 case ISN_COMPARENR: type = "COMPARENR"; break;
1967 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
1968 case ISN_COMPARESTRING:
1969 type = "COMPARESTRING"; break;
1970 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
1971 case ISN_COMPARELIST: type = "COMPARELIST"; break;
1972 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
1973 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
1974 case ISN_COMPAREPARTIAL:
1975 type = "COMPAREPARTIAL"; break;
1976 case ISN_COMPAREANY: type = "COMPAREANY"; break;
1977 default: type = "???"; break;
1978 }
1979
1980 smsg("%4d %s %s", current, type, buf);
1981 }
1982 break;
1983
1984 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
1985 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
1986
1987 // expression operations
1988 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
1989 case ISN_INDEX: smsg("%4d INDEX", current); break;
1990 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
1991 iptr->isn_arg.string); break;
1992 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
1993
1994 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
1995 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
1996 vartype_name(iptr->isn_arg.type.ct_type),
1997 iptr->isn_arg.type.ct_off);
1998 break;
1999 case ISN_2BOOL: if (iptr->isn_arg.number)
2000 smsg("%4d INVERT (!val)", current);
2001 else
2002 smsg("%4d 2BOOL (!!val)", current);
2003 break;
2004 case ISN_2STRING: smsg("%4d 2STRING stack[%d]", current,
2005 iptr->isn_arg.number);
2006 break;
2007
2008 case ISN_DROP: smsg("%4d DROP", current); break;
2009 }
2010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011}
2012
2013/*
2014 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2015 * list, etc. Mostly like what JavaScript does, except that empty list and
2016 * empty dictionary are FALSE.
2017 */
2018 int
2019tv2bool(typval_T *tv)
2020{
2021 switch (tv->v_type)
2022 {
2023 case VAR_NUMBER:
2024 return tv->vval.v_number != 0;
2025 case VAR_FLOAT:
2026#ifdef FEAT_FLOAT
2027 return tv->vval.v_float != 0.0;
2028#else
2029 break;
2030#endif
2031 case VAR_PARTIAL:
2032 return tv->vval.v_partial != NULL;
2033 case VAR_FUNC:
2034 case VAR_STRING:
2035 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2036 case VAR_LIST:
2037 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2038 case VAR_DICT:
2039 return tv->vval.v_dict != NULL
2040 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2041 case VAR_BOOL:
2042 case VAR_SPECIAL:
2043 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2044 case VAR_JOB:
2045#ifdef FEAT_JOB_CHANNEL
2046 return tv->vval.v_job != NULL;
2047#else
2048 break;
2049#endif
2050 case VAR_CHANNEL:
2051#ifdef FEAT_JOB_CHANNEL
2052 return tv->vval.v_channel != NULL;
2053#else
2054 break;
2055#endif
2056 case VAR_BLOB:
2057 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2058 case VAR_UNKNOWN:
2059 case VAR_VOID:
2060 break;
2061 }
2062 return FALSE;
2063}
2064
2065/*
2066 * If "tv" is a string give an error and return FAIL.
2067 */
2068 int
2069check_not_string(typval_T *tv)
2070{
2071 if (tv->v_type == VAR_STRING)
2072 {
2073 emsg(_("E1030: Using a String as a Number"));
2074 clear_tv(tv);
2075 return FAIL;
2076 }
2077 return OK;
2078}
2079
2080
2081#endif // FEAT_EVAL