blob: 4349489e54dd77ea40dd6b9aaeb53a720491bd53 [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/*
359 * Execute a function by "name".
360 * This can be a builtin function, user function or a funcref.
361 */
362 static int
363call_eval_func(char_u *name, int argcount, ectx_T *ectx)
364{
365 int called_emsg_before = called_emsg;
366
367 if (call_by_name(name, argcount, ectx) == FAIL
368 && called_emsg == called_emsg_before)
369 {
370 // "name" may be a variable that is a funcref or partial
371 // if find variable
372 // call_partial()
373 // else
374 // semsg(_(e_unknownfunc), name);
375 emsg("call_eval_func(partial) not implemented yet");
376 return FAIL;
377 }
378 return OK;
379}
380
381/*
382 * Call a "def" function from old Vim script.
383 * Return OK or FAIL.
384 */
385 int
386call_def_function(
387 ufunc_T *ufunc,
388 int argc, // nr of arguments
389 typval_T *argv, // arguments
390 typval_T *rettv) // return value
391{
392 ectx_T ectx; // execution context
393 int initial_frame_ptr;
394 typval_T *tv;
395 int idx;
396 int ret = FAIL;
397 dfunc_T *dfunc;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100398 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399
400// Get pointer to item in the stack.
401#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
402
403// Get pointer to item at the bottom of the stack, -1 is the bottom.
404#undef STACK_TV_BOT
405#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
406
407// Get pointer to local variable on the stack.
408#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame + STACK_FRAME_SIZE + idx)
409
410 vim_memset(&ectx, 0, sizeof(ectx));
411 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
412 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
413 goto failed;
414 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
415
416 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
417
418 // Put arguments on the stack.
419 for (idx = 0; idx < argc; ++idx)
420 {
421 copy_tv(&argv[idx], STACK_TV_BOT(0));
422 ++ectx.ec_stack.ga_len;
423 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100424 // Make space for omitted arguments, will store default value below.
425 if (defcount > 0)
426 for (idx = 0; idx < defcount; ++idx)
427 {
428 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
429 ++ectx.ec_stack.ga_len;
430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431
432 // Frame pointer points to just after arguments.
433 ectx.ec_frame = ectx.ec_stack.ga_len;
434 initial_frame_ptr = ectx.ec_frame;
435
436 // dummy frame entries
437 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
438 {
439 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
440 ++ectx.ec_stack.ga_len;
441 }
442
443 // Reserve space for local variables.
444 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
445 for (idx = 0; idx < dfunc->df_varcount; ++idx)
446 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
447 ectx.ec_stack.ga_len += dfunc->df_varcount;
448
449 ectx.ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100450
451 // Decide where to start execution, handles optional arguments.
452 init_instr_idx(ufunc, argc, &ectx);
453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 for (;;)
455 {
456 isn_T *iptr;
457 trycmd_T *trycmd = NULL;
458
459 if (did_throw && !ectx.ec_in_catch)
460 {
461 garray_T *trystack = &ectx.ec_trystack;
462
463 // An exception jumps to the first catch, finally, or returns from
464 // the current function.
465 if (trystack->ga_len > 0)
466 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
467 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame)
468 {
469 // jump to ":catch" or ":finally"
470 ectx.ec_in_catch = TRUE;
471 ectx.ec_iidx = trycmd->tcd_catch_idx;
472 }
473 else
474 {
475 // not inside try or need to return from current functions.
476 if (ectx.ec_frame == initial_frame_ptr)
477 {
478 // At the toplevel we are done. Push a dummy return value.
479 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
480 goto failed;
481 tv = STACK_TV_BOT(0);
482 tv->v_type = VAR_NUMBER;
483 tv->vval.v_number = 0;
484 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100485 need_rethrow = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486 goto done;
487 }
488
489 func_return(&ectx);
490 }
491 continue;
492 }
493
494 iptr = &ectx.ec_instr[ectx.ec_iidx++];
495 switch (iptr->isn_type)
496 {
497 // execute Ex command line
498 case ISN_EXEC:
499 do_cmdline_cmd(iptr->isn_arg.string);
500 break;
501
502 // execute :echo {string} ...
503 case ISN_ECHO:
504 {
505 int count = iptr->isn_arg.echo.echo_count;
506 int atstart = TRUE;
507 int needclr = TRUE;
508
509 for (idx = 0; idx < count; ++idx)
510 {
511 tv = STACK_TV_BOT(idx - count);
512 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
513 &atstart, &needclr);
514 clear_tv(tv);
515 }
516 ectx.ec_stack.ga_len -= count;
517 }
518 break;
519
520 // load local variable or argument
521 case ISN_LOAD:
522 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
523 goto failed;
524 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
525 ++ectx.ec_stack.ga_len;
526 break;
527
528 // load v: variable
529 case ISN_LOADV:
530 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
531 goto failed;
532 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
533 ++ectx.ec_stack.ga_len;
534 break;
535
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100536 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537 case ISN_LOADSCRIPT:
538 {
539 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100540 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541 svar_T *sv;
542
543 sv = ((svar_T *)si->sn_var_vals.ga_data)
544 + iptr->isn_arg.script.script_idx;
545 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
546 goto failed;
547 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
548 ++ectx.ec_stack.ga_len;
549 }
550 break;
551
552 // load s: variable in old script
553 case ISN_LOADS:
554 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100555 hashtab_T *ht = &SCRIPT_VARS(
556 iptr->isn_arg.loadstore.ls_sid);
557 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
559 if (di == NULL)
560 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100561 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 goto failed;
563 }
564 else
565 {
566 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
567 goto failed;
568 copy_tv(&di->di_tv, STACK_TV_BOT(0));
569 ++ectx.ec_stack.ga_len;
570 }
571 }
572 break;
573
574 // load g: variable
575 case ISN_LOADG:
576 {
577 dictitem_T *di;
578
579 di = find_var_in_ht(get_globvar_ht(), 0,
580 iptr->isn_arg.string, TRUE);
581 if (di == NULL)
582 {
583 semsg(_("E121: Undefined variable: g:%s"),
584 iptr->isn_arg.string);
585 goto failed;
586 }
587 else
588 {
589 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
590 goto failed;
591 copy_tv(&di->di_tv, STACK_TV_BOT(0));
592 ++ectx.ec_stack.ga_len;
593 }
594 }
595 break;
596
597 // load &option
598 case ISN_LOADOPT:
599 {
600 typval_T optval;
601 char_u *name = iptr->isn_arg.string;
602
603 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
604 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100605 if (get_option_tv(&name, &optval, TRUE) == FAIL)
606 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607 *STACK_TV_BOT(0) = optval;
608 ++ectx.ec_stack.ga_len;
609 }
610 break;
611
612 // load $ENV
613 case ISN_LOADENV:
614 {
615 typval_T optval;
616 char_u *name = iptr->isn_arg.string;
617
618 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
619 goto failed;
Bram Moolenaar07da94b2020-01-28 22:39:19 +0100620 if (get_env_tv(&name, &optval, TRUE) == FAIL)
621 {
622 semsg(_("E1060: Invalid environment variable name: %s"),
623 iptr->isn_arg.string);
624 goto failed;
625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 *STACK_TV_BOT(0) = optval;
627 ++ectx.ec_stack.ga_len;
628 }
629 break;
630
631 // load @register
632 case ISN_LOADREG:
633 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
634 goto failed;
635 tv = STACK_TV_BOT(0);
636 tv->v_type = VAR_STRING;
637 tv->vval.v_string = get_reg_contents(
638 iptr->isn_arg.number, GREG_EXPR_SRC);
639 ++ectx.ec_stack.ga_len;
640 break;
641
642 // store local variable
643 case ISN_STORE:
644 --ectx.ec_stack.ga_len;
645 tv = STACK_TV_VAR(iptr->isn_arg.number);
646 clear_tv(tv);
647 *tv = *STACK_TV_BOT(0);
648 break;
649
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100650 // store s: variable in old script
651 case ISN_STORES:
652 {
653 hashtab_T *ht = &SCRIPT_VARS(
654 iptr->isn_arg.loadstore.ls_sid);
655 char_u *name = iptr->isn_arg.loadstore.ls_name;
656 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
657
658 if (di == NULL)
659 {
660 semsg(_(e_undefvar), name);
661 goto failed;
662 }
663 --ectx.ec_stack.ga_len;
664 clear_tv(&di->di_tv);
665 di->di_tv = *STACK_TV_BOT(0);
666 }
667 break;
668
669 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 case ISN_STORESCRIPT:
671 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100672 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 iptr->isn_arg.script.script_sid);
674 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
675 + iptr->isn_arg.script.script_idx;
676
677 --ectx.ec_stack.ga_len;
678 clear_tv(sv->sv_tv);
679 *sv->sv_tv = *STACK_TV_BOT(0);
680 }
681 break;
682
683 // store option
684 case ISN_STOREOPT:
685 {
686 long n = 0;
687 char_u *s = NULL;
688 char *msg;
689
690 --ectx.ec_stack.ga_len;
691 tv = STACK_TV_BOT(0);
692 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100693 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100695 if (s == NULL)
696 s = (char_u *)"";
697 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 else if (tv->v_type == VAR_NUMBER)
699 n = tv->vval.v_number;
700 else
701 {
702 emsg(_("E1051: Expected string or number"));
703 goto failed;
704 }
705 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
706 n, s, iptr->isn_arg.storeopt.so_flags);
707 if (msg != NULL)
708 {
709 emsg(_(msg));
710 goto failed;
711 }
712 clear_tv(tv);
713 }
714 break;
715
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100716 // store $ENV
717 case ISN_STOREENV:
718 --ectx.ec_stack.ga_len;
719 vim_setenv_ext(iptr->isn_arg.string,
720 tv_get_string(STACK_TV_BOT(0)));
721 break;
722
723 // store @r
724 case ISN_STOREREG:
725 {
726 int reg = iptr->isn_arg.number;
727
728 --ectx.ec_stack.ga_len;
729 write_reg_contents(reg == '@' ? '"' : reg,
730 tv_get_string(STACK_TV_BOT(0)), -1, FALSE);
731 }
732 break;
733
734 // store v: variable
735 case ISN_STOREV:
736 --ectx.ec_stack.ga_len;
737 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
738 == FAIL)
739 goto failed;
740 break;
741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742 // store g: variable
743 case ISN_STOREG:
744 {
745 dictitem_T *di;
746
747 --ectx.ec_stack.ga_len;
748 di = find_var_in_ht(get_globvar_ht(), 0,
749 iptr->isn_arg.string, TRUE);
750 if (di == NULL)
751 {
752 funccal_entry_T entry;
753
754 save_funccal(&entry);
755 set_var_const(iptr->isn_arg.string, NULL,
756 STACK_TV_BOT(0), FALSE, 0);
757 restore_funccal();
758 }
759 else
760 {
761 clear_tv(&di->di_tv);
762 di->di_tv = *STACK_TV_BOT(0);
763 }
764 }
765 break;
766
767 // store number in local variable
768 case ISN_STORENR:
769 tv = STACK_TV_VAR(iptr->isn_arg.storenr.str_idx);
770 clear_tv(tv);
771 tv->v_type = VAR_NUMBER;
772 tv->vval.v_number = iptr->isn_arg.storenr.str_val;
773 break;
774
775 // push constant
776 case ISN_PUSHNR:
777 case ISN_PUSHBOOL:
778 case ISN_PUSHSPEC:
779 case ISN_PUSHF:
780 case ISN_PUSHS:
781 case ISN_PUSHBLOB:
782 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
783 goto failed;
784 tv = STACK_TV_BOT(0);
785 ++ectx.ec_stack.ga_len;
786 switch (iptr->isn_type)
787 {
788 case ISN_PUSHNR:
789 tv->v_type = VAR_NUMBER;
790 tv->vval.v_number = iptr->isn_arg.number;
791 break;
792 case ISN_PUSHBOOL:
793 tv->v_type = VAR_BOOL;
794 tv->vval.v_number = iptr->isn_arg.number;
795 break;
796 case ISN_PUSHSPEC:
797 tv->v_type = VAR_SPECIAL;
798 tv->vval.v_number = iptr->isn_arg.number;
799 break;
800#ifdef FEAT_FLOAT
801 case ISN_PUSHF:
802 tv->v_type = VAR_FLOAT;
803 tv->vval.v_float = iptr->isn_arg.fnumber;
804 break;
805#endif
806 case ISN_PUSHBLOB:
807 blob_copy(iptr->isn_arg.blob, tv);
808 break;
809 default:
810 tv->v_type = VAR_STRING;
811 tv->vval.v_string = vim_strsave(iptr->isn_arg.string);
812 }
813 break;
814
815 // create a list from items on the stack; uses a single allocation
816 // for the list header and the items
817 case ISN_NEWLIST:
818 {
819 int count = iptr->isn_arg.number;
820 list_T *list = list_alloc_with_items(count);
821
822 if (list == NULL)
823 goto failed;
824 for (idx = 0; idx < count; ++idx)
825 list_set_item(list, idx, STACK_TV_BOT(idx - count));
826
827 if (count > 0)
828 ectx.ec_stack.ga_len -= count - 1;
829 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
830 goto failed;
831 else
832 ++ectx.ec_stack.ga_len;
833 tv = STACK_TV_BOT(-1);
834 tv->v_type = VAR_LIST;
835 tv->vval.v_list = list;
836 ++list->lv_refcount;
837 }
838 break;
839
840 // create a dict from items on the stack
841 case ISN_NEWDICT:
842 {
843 int count = iptr->isn_arg.number;
844 dict_T *dict = dict_alloc();
845 dictitem_T *item;
846
847 if (dict == NULL)
848 goto failed;
849 for (idx = 0; idx < count; ++idx)
850 {
851 // check key type is VAR_STRING
852 tv = STACK_TV_BOT(2 * (idx - count));
853 item = dictitem_alloc(tv->vval.v_string);
854 clear_tv(tv);
855 if (item == NULL)
856 goto failed;
857 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
858 item->di_tv.v_lock = 0;
859 if (dict_add(dict, item) == FAIL)
860 goto failed;
861 }
862
863 if (count > 0)
864 ectx.ec_stack.ga_len -= 2 * count - 1;
865 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
866 goto failed;
867 else
868 ++ectx.ec_stack.ga_len;
869 tv = STACK_TV_BOT(-1);
870 tv->v_type = VAR_DICT;
871 tv->vval.v_dict = dict;
872 ++dict->dv_refcount;
873 }
874 break;
875
876 // call a :def function
877 case ISN_DCALL:
878 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
879 iptr->isn_arg.dfunc.cdf_argcount,
880 &ectx) == FAIL)
881 goto failed;
882 break;
883
884 // call a builtin function
885 case ISN_BCALL:
886 SOURCING_LNUM = iptr->isn_lnum;
887 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
888 iptr->isn_arg.bfunc.cbf_argcount,
889 &ectx) == FAIL)
890 goto failed;
891 break;
892
893 // call a funcref or partial
894 case ISN_PCALL:
895 {
896 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
897 int r;
898 typval_T partial;
899
900 SOURCING_LNUM = iptr->isn_lnum;
901 if (pfunc->cpf_top)
902 {
903 // funcref is above the arguments
904 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
905 }
906 else
907 {
908 // Get the funcref from the stack.
909 --ectx.ec_stack.ga_len;
910 partial = *STACK_TV_BOT(0);
911 tv = &partial;
912 }
913 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
914 if (tv == &partial)
915 clear_tv(&partial);
916 if (r == FAIL)
917 goto failed;
918
919 if (pfunc->cpf_top)
920 {
921 // Get the funcref from the stack, overwrite with the
922 // return value.
923 clear_tv(tv);
924 --ectx.ec_stack.ga_len;
925 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
926 }
927 }
928 break;
929
930 // call a user defined function or funcref/partial
931 case ISN_UCALL:
932 {
933 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
934
935 SOURCING_LNUM = iptr->isn_lnum;
936 if (call_eval_func(cufunc->cuf_name,
937 cufunc->cuf_argcount, &ectx) == FAIL)
938 goto failed;
939 }
940 break;
941
942 // return from a :def function call
943 case ISN_RETURN:
944 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +0100945 garray_T *trystack = &ectx.ec_trystack;
946
947 if (trystack->ga_len > 0)
948 trycmd = ((trycmd_T *)trystack->ga_data)
949 + trystack->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame
951 && trycmd->tcd_finally_idx != 0)
952 {
953 // jump to ":finally"
954 ectx.ec_iidx = trycmd->tcd_finally_idx;
955 trycmd->tcd_return = TRUE;
956 }
957 else
958 {
959 // Restore previous function. If the frame pointer
960 // is zero then there is none and we are done.
961 if (ectx.ec_frame == initial_frame_ptr)
962 goto done;
963
964 func_return(&ectx);
965 }
966 }
967 break;
968
969 // push a function reference to a compiled function
970 case ISN_FUNCREF:
971 {
972 partial_T *pt = NULL;
973
974 pt = ALLOC_CLEAR_ONE(partial_T);
975 if (pt == NULL)
976 goto failed;
977 dfunc = ((dfunc_T *)def_functions.ga_data)
978 + iptr->isn_arg.number;
979 pt->pt_func = dfunc->df_ufunc;
980 pt->pt_refcount = 1;
981 ++dfunc->df_ufunc->uf_refcount;
982
983 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
984 goto failed;
985 tv = STACK_TV_BOT(0);
986 ++ectx.ec_stack.ga_len;
987 tv->vval.v_partial = pt;
988 tv->v_type = VAR_PARTIAL;
989 }
990 break;
991
992 // jump if a condition is met
993 case ISN_JUMP:
994 {
995 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
996 int jump = TRUE;
997
998 if (when != JUMP_ALWAYS)
999 {
1000 tv = STACK_TV_BOT(-1);
1001 jump = tv2bool(tv);
1002 if (when == JUMP_IF_FALSE
1003 || when == JUMP_AND_KEEP_IF_FALSE)
1004 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001005 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 {
1007 // drop the value from the stack
1008 clear_tv(tv);
1009 --ectx.ec_stack.ga_len;
1010 }
1011 }
1012 if (jump)
1013 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1014 }
1015 break;
1016
1017 // top of a for loop
1018 case ISN_FOR:
1019 {
1020 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1021 typval_T *idxtv =
1022 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1023
1024 // push the next item from the list
1025 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1026 goto failed;
1027 if (++idxtv->vval.v_number >= list->lv_len)
1028 // past the end of the list, jump to "endfor"
1029 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1030 else if (list->lv_first == &range_list_item)
1031 {
1032 // non-materialized range() list
1033 tv = STACK_TV_BOT(0);
1034 tv->v_type = VAR_NUMBER;
1035 tv->vval.v_number = list_find_nr(
1036 list, idxtv->vval.v_number, NULL);
1037 ++ectx.ec_stack.ga_len;
1038 }
1039 else
1040 {
1041 listitem_T *li = list_find(list, idxtv->vval.v_number);
1042
1043 if (li == NULL)
1044 goto failed;
1045 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1046 ++ectx.ec_stack.ga_len;
1047 }
1048 }
1049 break;
1050
1051 // start of ":try" block
1052 case ISN_TRY:
1053 {
1054 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1055 goto failed;
1056 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1057 + ectx.ec_trystack.ga_len;
1058 ++ectx.ec_trystack.ga_len;
1059 ++trylevel;
1060 trycmd->tcd_frame = ectx.ec_frame;
1061 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1062 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
1063 }
1064 break;
1065
1066 case ISN_PUSHEXC:
1067 if (current_exception == NULL)
1068 {
1069 iemsg("Evaluating catch while current_exception is NULL");
1070 goto failed;
1071 }
1072 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1073 goto failed;
1074 tv = STACK_TV_BOT(0);
1075 ++ectx.ec_stack.ga_len;
1076 tv->v_type = VAR_STRING;
1077 tv->vval.v_string = vim_strsave(
1078 (char_u *)current_exception->value);
1079 break;
1080
1081 case ISN_CATCH:
1082 {
1083 garray_T *trystack = &ectx.ec_trystack;
1084
1085 if (trystack->ga_len > 0)
1086 {
1087 trycmd = ((trycmd_T *)trystack->ga_data)
1088 + trystack->ga_len - 1;
1089 trycmd->tcd_caught = TRUE;
1090 }
1091 did_emsg = got_int = did_throw = FALSE;
1092 catch_exception(current_exception);
1093 }
1094 break;
1095
1096 // end of ":try" block
1097 case ISN_ENDTRY:
1098 {
1099 garray_T *trystack = &ectx.ec_trystack;
1100
1101 if (trystack->ga_len > 0)
1102 {
1103 --trystack->ga_len;
1104 --trylevel;
1105 trycmd = ((trycmd_T *)trystack->ga_data)
1106 + trystack->ga_len;
1107 if (trycmd->tcd_caught)
1108 {
1109 // discard the exception
1110 if (caught_stack == current_exception)
1111 caught_stack = caught_stack->caught;
1112 discard_current_exception();
1113 }
1114
1115 if (trycmd->tcd_return)
1116 {
1117 // Restore previous function. If the frame pointer
1118 // is zero then there is none and we are done.
1119 if (ectx.ec_frame == initial_frame_ptr)
1120 goto done;
1121
1122 func_return(&ectx);
1123 }
1124 }
1125 }
1126 break;
1127
1128 case ISN_THROW:
1129 --ectx.ec_stack.ga_len;
1130 tv = STACK_TV_BOT(0);
1131 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1132 {
1133 vim_free(tv->vval.v_string);
1134 goto failed;
1135 }
1136 did_throw = TRUE;
1137 break;
1138
1139 // compare with special values
1140 case ISN_COMPAREBOOL:
1141 case ISN_COMPARESPECIAL:
1142 {
1143 typval_T *tv1 = STACK_TV_BOT(-2);
1144 typval_T *tv2 = STACK_TV_BOT(-1);
1145 varnumber_T arg1 = tv1->vval.v_number;
1146 varnumber_T arg2 = tv2->vval.v_number;
1147 int res;
1148
1149 switch (iptr->isn_arg.op.op_type)
1150 {
1151 case EXPR_EQUAL: res = arg1 == arg2; break;
1152 case EXPR_NEQUAL: res = arg1 != arg2; break;
1153 default: res = 0; break;
1154 }
1155
1156 --ectx.ec_stack.ga_len;
1157 tv1->v_type = VAR_BOOL;
1158 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1159 }
1160 break;
1161
1162 // Operation with two number arguments
1163 case ISN_OPNR:
1164 case ISN_COMPARENR:
1165 {
1166 typval_T *tv1 = STACK_TV_BOT(-2);
1167 typval_T *tv2 = STACK_TV_BOT(-1);
1168 varnumber_T arg1 = tv1->vval.v_number;
1169 varnumber_T arg2 = tv2->vval.v_number;
1170 varnumber_T res;
1171
1172 switch (iptr->isn_arg.op.op_type)
1173 {
1174 case EXPR_MULT: res = arg1 * arg2; break;
1175 case EXPR_DIV: res = arg1 / arg2; break;
1176 case EXPR_REM: res = arg1 % arg2; break;
1177 case EXPR_SUB: res = arg1 - arg2; break;
1178 case EXPR_ADD: res = arg1 + arg2; break;
1179
1180 case EXPR_EQUAL: res = arg1 == arg2; break;
1181 case EXPR_NEQUAL: res = arg1 != arg2; break;
1182 case EXPR_GREATER: res = arg1 > arg2; break;
1183 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1184 case EXPR_SMALLER: res = arg1 < arg2; break;
1185 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1186 default: res = 0; break;
1187 }
1188
1189 --ectx.ec_stack.ga_len;
1190 if (iptr->isn_type == ISN_COMPARENR)
1191 {
1192 tv1->v_type = VAR_BOOL;
1193 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1194 }
1195 else
1196 tv1->vval.v_number = res;
1197 }
1198 break;
1199
1200 // Computation with two float arguments
1201 case ISN_OPFLOAT:
1202 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001203#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 {
1205 typval_T *tv1 = STACK_TV_BOT(-2);
1206 typval_T *tv2 = STACK_TV_BOT(-1);
1207 float_T arg1 = tv1->vval.v_float;
1208 float_T arg2 = tv2->vval.v_float;
1209 float_T res = 0;
1210 int cmp = FALSE;
1211
1212 switch (iptr->isn_arg.op.op_type)
1213 {
1214 case EXPR_MULT: res = arg1 * arg2; break;
1215 case EXPR_DIV: res = arg1 / arg2; break;
1216 case EXPR_SUB: res = arg1 - arg2; break;
1217 case EXPR_ADD: res = arg1 + arg2; break;
1218
1219 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1220 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1221 case EXPR_GREATER: cmp = arg1 > arg2; break;
1222 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1223 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1224 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1225 default: cmp = 0; break;
1226 }
1227 --ectx.ec_stack.ga_len;
1228 if (iptr->isn_type == ISN_COMPAREFLOAT)
1229 {
1230 tv1->v_type = VAR_BOOL;
1231 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1232 }
1233 else
1234 tv1->vval.v_float = res;
1235 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001236#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 break;
1238
1239 case ISN_COMPARELIST:
1240 {
1241 typval_T *tv1 = STACK_TV_BOT(-2);
1242 typval_T *tv2 = STACK_TV_BOT(-1);
1243 list_T *arg1 = tv1->vval.v_list;
1244 list_T *arg2 = tv2->vval.v_list;
1245 int cmp = FALSE;
1246 int ic = iptr->isn_arg.op.op_ic;
1247
1248 switch (iptr->isn_arg.op.op_type)
1249 {
1250 case EXPR_EQUAL: cmp =
1251 list_equal(arg1, arg2, ic, FALSE); break;
1252 case EXPR_NEQUAL: cmp =
1253 !list_equal(arg1, arg2, ic, FALSE); break;
1254 case EXPR_IS: cmp = arg1 == arg2; break;
1255 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1256 default: cmp = 0; break;
1257 }
1258 --ectx.ec_stack.ga_len;
1259 clear_tv(tv1);
1260 clear_tv(tv2);
1261 tv1->v_type = VAR_BOOL;
1262 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1263 }
1264 break;
1265
1266 case ISN_COMPAREBLOB:
1267 {
1268 typval_T *tv1 = STACK_TV_BOT(-2);
1269 typval_T *tv2 = STACK_TV_BOT(-1);
1270 blob_T *arg1 = tv1->vval.v_blob;
1271 blob_T *arg2 = tv2->vval.v_blob;
1272 int cmp = FALSE;
1273
1274 switch (iptr->isn_arg.op.op_type)
1275 {
1276 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1277 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1278 case EXPR_IS: cmp = arg1 == arg2; break;
1279 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1280 default: cmp = 0; break;
1281 }
1282 --ectx.ec_stack.ga_len;
1283 clear_tv(tv1);
1284 clear_tv(tv2);
1285 tv1->v_type = VAR_BOOL;
1286 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1287 }
1288 break;
1289
1290 // TODO: handle separately
1291 case ISN_COMPARESTRING:
1292 case ISN_COMPAREDICT:
1293 case ISN_COMPAREFUNC:
1294 case ISN_COMPAREPARTIAL:
1295 case ISN_COMPAREANY:
1296 {
1297 typval_T *tv1 = STACK_TV_BOT(-2);
1298 typval_T *tv2 = STACK_TV_BOT(-1);
1299 exptype_T exptype = iptr->isn_arg.op.op_type;
1300 int ic = iptr->isn_arg.op.op_ic;
1301
1302 typval_compare(tv1, tv2, exptype, ic);
1303 clear_tv(tv2);
1304 tv1->v_type = VAR_BOOL;
1305 tv1->vval.v_number = tv1->vval.v_number
1306 ? VVAL_TRUE : VVAL_FALSE;
1307 --ectx.ec_stack.ga_len;
1308 }
1309 break;
1310
1311 case ISN_ADDLIST:
1312 case ISN_ADDBLOB:
1313 {
1314 typval_T *tv1 = STACK_TV_BOT(-2);
1315 typval_T *tv2 = STACK_TV_BOT(-1);
1316
1317 if (iptr->isn_type == ISN_ADDLIST)
1318 eval_addlist(tv1, tv2);
1319 else
1320 eval_addblob(tv1, tv2);
1321 clear_tv(tv2);
1322 --ectx.ec_stack.ga_len;
1323 }
1324 break;
1325
1326 // Computation with two arguments of unknown type
1327 case ISN_OPANY:
1328 {
1329 typval_T *tv1 = STACK_TV_BOT(-2);
1330 typval_T *tv2 = STACK_TV_BOT(-1);
1331 varnumber_T n1, n2;
1332#ifdef FEAT_FLOAT
1333 float_T f1 = 0, f2 = 0;
1334#endif
1335 int error = FALSE;
1336
1337 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1338 {
1339 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1340 {
1341 eval_addlist(tv1, tv2);
1342 clear_tv(tv2);
1343 --ectx.ec_stack.ga_len;
1344 break;
1345 }
1346 else if (tv1->v_type == VAR_BLOB
1347 && tv2->v_type == VAR_BLOB)
1348 {
1349 eval_addblob(tv1, tv2);
1350 clear_tv(tv2);
1351 --ectx.ec_stack.ga_len;
1352 break;
1353 }
1354 }
1355#ifdef FEAT_FLOAT
1356 if (tv1->v_type == VAR_FLOAT)
1357 {
1358 f1 = tv1->vval.v_float;
1359 n1 = 0;
1360 }
1361 else
1362#endif
1363 {
1364 n1 = tv_get_number_chk(tv1, &error);
1365 if (error)
1366 goto failed;
1367#ifdef FEAT_FLOAT
1368 if (tv2->v_type == VAR_FLOAT)
1369 f1 = n1;
1370#endif
1371 }
1372#ifdef FEAT_FLOAT
1373 if (tv2->v_type == VAR_FLOAT)
1374 {
1375 f2 = tv2->vval.v_float;
1376 n2 = 0;
1377 }
1378 else
1379#endif
1380 {
1381 n2 = tv_get_number_chk(tv2, &error);
1382 if (error)
1383 goto failed;
1384#ifdef FEAT_FLOAT
1385 if (tv1->v_type == VAR_FLOAT)
1386 f2 = n2;
1387#endif
1388 }
1389#ifdef FEAT_FLOAT
1390 // if there is a float on either side the result is a float
1391 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1392 {
1393 switch (iptr->isn_arg.op.op_type)
1394 {
1395 case EXPR_MULT: f1 = f1 * f2; break;
1396 case EXPR_DIV: f1 = f1 / f2; break;
1397 case EXPR_SUB: f1 = f1 - f2; break;
1398 case EXPR_ADD: f1 = f1 + f2; break;
1399 default: emsg(_(e_modulus)); goto failed;
1400 }
1401 clear_tv(tv1);
1402 clear_tv(tv2);
1403 tv1->v_type = VAR_FLOAT;
1404 tv1->vval.v_float = f1;
1405 --ectx.ec_stack.ga_len;
1406 }
1407 else
1408#endif
1409 {
1410 switch (iptr->isn_arg.op.op_type)
1411 {
1412 case EXPR_MULT: n1 = n1 * n2; break;
1413 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1414 case EXPR_SUB: n1 = n1 - n2; break;
1415 case EXPR_ADD: n1 = n1 + n2; break;
1416 default: n1 = num_modulus(n1, n2); break;
1417 }
1418 clear_tv(tv1);
1419 clear_tv(tv2);
1420 tv1->v_type = VAR_NUMBER;
1421 tv1->vval.v_number = n1;
1422 --ectx.ec_stack.ga_len;
1423 }
1424 }
1425 break;
1426
1427 case ISN_CONCAT:
1428 {
1429 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1430 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1431 char_u *res;
1432
1433 res = concat_str(str1, str2);
1434 clear_tv(STACK_TV_BOT(-2));
1435 clear_tv(STACK_TV_BOT(-1));
1436 --ectx.ec_stack.ga_len;
1437 STACK_TV_BOT(-1)->vval.v_string = res;
1438 }
1439 break;
1440
1441 case ISN_INDEX:
1442 {
1443 list_T *list;
1444 varnumber_T n;
1445 listitem_T *li;
1446
1447 // list index: list is at stack-2, index at stack-1
1448 tv = STACK_TV_BOT(-2);
1449 if (tv->v_type != VAR_LIST)
1450 {
1451 emsg(_(e_listreq));
1452 goto failed;
1453 }
1454 list = tv->vval.v_list;
1455
1456 tv = STACK_TV_BOT(-1);
1457 if (tv->v_type != VAR_NUMBER)
1458 {
1459 emsg(_(e_number_exp));
1460 goto failed;
1461 }
1462 n = tv->vval.v_number;
1463 clear_tv(tv);
1464 if ((li = list_find(list, n)) == NULL)
1465 {
1466 semsg(_(e_listidx), n);
1467 goto failed;
1468 }
1469 --ectx.ec_stack.ga_len;
1470 clear_tv(STACK_TV_BOT(-1));
1471 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1472 }
1473 break;
1474
1475 // dict member with string key
1476 case ISN_MEMBER:
1477 {
1478 dict_T *dict;
1479 dictitem_T *di;
1480
1481 tv = STACK_TV_BOT(-1);
1482 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1483 {
1484 emsg(_(e_dictreq));
1485 goto failed;
1486 }
1487 dict = tv->vval.v_dict;
1488
1489 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1490 == NULL)
1491 {
1492 semsg(_(e_dictkey), iptr->isn_arg.string);
1493 goto failed;
1494 }
1495 clear_tv(tv);
1496 copy_tv(&di->di_tv, tv);
1497 }
1498 break;
1499
1500 case ISN_NEGATENR:
1501 tv = STACK_TV_BOT(-1);
1502 tv->vval.v_number = -tv->vval.v_number;
1503 break;
1504
1505 case ISN_CHECKNR:
1506 {
1507 int error = FALSE;
1508
1509 tv = STACK_TV_BOT(-1);
1510 if (check_not_string(tv) == FAIL)
1511 {
1512 --ectx.ec_stack.ga_len;
1513 goto failed;
1514 }
1515 (void)tv_get_number_chk(tv, &error);
1516 if (error)
1517 goto failed;
1518 }
1519 break;
1520
1521 case ISN_CHECKTYPE:
1522 {
1523 checktype_T *ct = &iptr->isn_arg.type;
1524
1525 tv = STACK_TV_BOT(ct->ct_off);
1526 if (tv->v_type != ct->ct_type)
1527 {
1528 semsg(_("E1029: Expected %s but got %s"),
1529 vartype_name(ct->ct_type),
1530 vartype_name(tv->v_type));
1531 goto failed;
1532 }
1533 }
1534 break;
1535
1536 case ISN_2BOOL:
1537 {
1538 int n;
1539
1540 tv = STACK_TV_BOT(-1);
1541 n = tv2bool(tv);
1542 if (iptr->isn_arg.number) // invert
1543 n = !n;
1544 clear_tv(tv);
1545 tv->v_type = VAR_BOOL;
1546 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
1547 }
1548 break;
1549
1550 case ISN_2STRING:
1551 {
1552 char_u *str;
1553
1554 tv = STACK_TV_BOT(iptr->isn_arg.number);
1555 if (tv->v_type != VAR_STRING)
1556 {
1557 str = typval_tostring(tv);
1558 clear_tv(tv);
1559 tv->v_type = VAR_STRING;
1560 tv->vval.v_string = str;
1561 }
1562 }
1563 break;
1564
1565 case ISN_DROP:
1566 --ectx.ec_stack.ga_len;
1567 clear_tv(STACK_TV_BOT(0));
1568 break;
1569 }
1570 }
1571
1572done:
1573 // function finished, get result from the stack.
1574 tv = STACK_TV_BOT(-1);
1575 *rettv = *tv;
1576 tv->v_type = VAR_UNKNOWN;
1577 ret = OK;
1578
1579failed:
1580 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
1581 clear_tv(STACK_TV(idx));
1582 vim_free(ectx.ec_stack.ga_data);
1583 return ret;
1584}
1585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586/*
1587 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01001588 * We don't really need this at runtime, but we do have tests that require it,
1589 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 */
1591 void
1592ex_disassemble(exarg_T *eap)
1593{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001594 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001595 char_u *fname;
1596 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 dfunc_T *dfunc;
1598 isn_T *instr;
1599 int current;
1600 int line_idx = 0;
1601 int prev_current = 0;
1602
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001603 fname = trans_function_name(&arg, FALSE,
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001604 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001605 if (fname == NULL)
1606 {
1607 semsg(_(e_invarg2), eap->arg);
1608 return;
1609 }
1610
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001611 ufunc = find_func(fname, NULL);
1612 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 if (ufunc == NULL)
1614 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001615 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 return;
1617 }
1618 if (ufunc->uf_dfunc_idx < 0)
1619 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001620 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 return;
1622 }
1623 if (ufunc->uf_name_exp != NULL)
1624 msg((char *)ufunc->uf_name_exp);
1625 else
1626 msg((char *)ufunc->uf_name);
1627
1628 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
1629 instr = dfunc->df_instr;
1630 for (current = 0; current < dfunc->df_instr_count; ++current)
1631 {
1632 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001633 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634
1635 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
1636 {
1637 if (current > prev_current)
1638 {
1639 msg_puts("\n\n");
1640 prev_current = current;
1641 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001642 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
1643 if (line != NULL)
1644 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 }
1646
1647 switch (iptr->isn_type)
1648 {
1649 case ISN_EXEC:
1650 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
1651 break;
1652 case ISN_ECHO:
1653 {
1654 echo_T *echo = &iptr->isn_arg.echo;
1655
1656 smsg("%4d %s %d", current,
1657 echo->echo_with_white ? "ECHO" : "ECHON",
1658 echo->echo_count);
1659 }
1660 break;
1661 case ISN_LOAD:
1662 if (iptr->isn_arg.number < 0)
1663 smsg("%4d LOAD arg[%lld]", current,
1664 iptr->isn_arg.number + STACK_FRAME_SIZE);
1665 else
1666 smsg("%4d LOAD $%lld", current, iptr->isn_arg.number);
1667 break;
1668 case ISN_LOADV:
1669 smsg("%4d LOADV v:%s", current,
1670 get_vim_var_name(iptr->isn_arg.number));
1671 break;
1672 case ISN_LOADSCRIPT:
1673 {
1674 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001675 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1677 + iptr->isn_arg.script.script_idx;
1678
1679 smsg("%4d LOADSCRIPT %s from %s", current,
1680 sv->sv_name, si->sn_name);
1681 }
1682 break;
1683 case ISN_LOADS:
1684 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001685 scriptitem_T *si = SCRIPT_ITEM(
1686 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687
1688 smsg("%4d LOADS s:%s from %s", current,
1689 iptr->isn_arg.string, si->sn_name);
1690 }
1691 break;
1692 case ISN_LOADG:
1693 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
1694 break;
1695 case ISN_LOADOPT:
1696 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
1697 break;
1698 case ISN_LOADENV:
1699 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
1700 break;
1701 case ISN_LOADREG:
1702 smsg("%4d LOADREG @%c", current, iptr->isn_arg.number);
1703 break;
1704
1705 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001706 if (iptr->isn_arg.number < 0)
1707 smsg("%4d STORE arg[%lld]", current,
1708 iptr->isn_arg.number + STACK_FRAME_SIZE);
1709 else
1710 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001712 case ISN_STOREV:
1713 smsg("%4d STOREV v:%s", current,
1714 get_vim_var_name(iptr->isn_arg.number));
1715 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001717 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
1718 break;
1719 case ISN_STORES:
1720 {
1721 scriptitem_T *si = SCRIPT_ITEM(
1722 iptr->isn_arg.loadstore.ls_sid);
1723
1724 smsg("%4d STORES s:%s in %s", current,
1725 iptr->isn_arg.string, si->sn_name);
1726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 break;
1728 case ISN_STORESCRIPT:
1729 {
1730 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001731 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1733 + iptr->isn_arg.script.script_idx;
1734
1735 smsg("%4d STORESCRIPT %s in %s", current,
1736 sv->sv_name, si->sn_name);
1737 }
1738 break;
1739 case ISN_STOREOPT:
1740 smsg("%4d STOREOPT &%s", current,
1741 iptr->isn_arg.storeopt.so_name);
1742 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001743 case ISN_STOREENV:
1744 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
1745 break;
1746 case ISN_STOREREG:
1747 smsg("%4d STOREREG @%c", current, iptr->isn_arg.number);
1748 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749 case ISN_STORENR:
1750 smsg("%4d STORE %lld in $%d", current,
1751 iptr->isn_arg.storenr.str_val,
1752 iptr->isn_arg.storenr.str_idx);
1753 break;
1754
1755 // constants
1756 case ISN_PUSHNR:
1757 smsg("%4d PUSHNR %lld", current, iptr->isn_arg.number);
1758 break;
1759 case ISN_PUSHBOOL:
1760 case ISN_PUSHSPEC:
1761 smsg("%4d PUSH %s", current,
1762 get_var_special_name(iptr->isn_arg.number));
1763 break;
1764 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001765#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01001767#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 break;
1769 case ISN_PUSHS:
1770 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
1771 break;
1772 case ISN_PUSHBLOB:
1773 {
1774 char_u *r;
1775 char_u numbuf[NUMBUFLEN];
1776 char_u *tofree;
1777
1778 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01001779 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 vim_free(tofree);
1781 }
1782 break;
1783 case ISN_PUSHEXC:
1784 smsg("%4d PUSH v:exception", current);
1785 break;
1786 case ISN_NEWLIST:
1787 smsg("%4d NEWLIST size %lld", current, iptr->isn_arg.number);
1788 break;
1789 case ISN_NEWDICT:
1790 smsg("%4d NEWDICT size %lld", current, iptr->isn_arg.number);
1791 break;
1792
1793 // function call
1794 case ISN_BCALL:
1795 {
1796 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
1797
1798 smsg("%4d BCALL %s(argc %d)", current,
1799 internal_func_name(cbfunc->cbf_idx),
1800 cbfunc->cbf_argcount);
1801 }
1802 break;
1803 case ISN_DCALL:
1804 {
1805 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
1806 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1807 + cdfunc->cdf_idx;
1808
1809 smsg("%4d DCALL %s(argc %d)", current,
1810 df->df_ufunc->uf_name_exp != NULL
1811 ? df->df_ufunc->uf_name_exp
1812 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
1813 }
1814 break;
1815 case ISN_UCALL:
1816 {
1817 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1818
1819 smsg("%4d UCALL %s(argc %d)", current,
1820 cufunc->cuf_name, cufunc->cuf_argcount);
1821 }
1822 break;
1823 case ISN_PCALL:
1824 {
1825 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
1826
1827 smsg("%4d PCALL%s (argc %d)", current,
1828 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
1829 }
1830 break;
1831 case ISN_RETURN:
1832 smsg("%4d RETURN", current);
1833 break;
1834 case ISN_FUNCREF:
1835 {
1836 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1837 + iptr->isn_arg.number;
1838
1839 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
1840 }
1841 break;
1842
1843 case ISN_JUMP:
1844 {
1845 char *when = "?";
1846
1847 switch (iptr->isn_arg.jump.jump_when)
1848 {
1849 case JUMP_ALWAYS:
1850 when = "JUMP";
1851 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 case JUMP_AND_KEEP_IF_TRUE:
1853 when = "JUMP_AND_KEEP_IF_TRUE";
1854 break;
1855 case JUMP_IF_FALSE:
1856 when = "JUMP_IF_FALSE";
1857 break;
1858 case JUMP_AND_KEEP_IF_FALSE:
1859 when = "JUMP_AND_KEEP_IF_FALSE";
1860 break;
1861 }
1862 smsg("%4d %s -> %lld", current, when,
1863 iptr->isn_arg.jump.jump_where);
1864 }
1865 break;
1866
1867 case ISN_FOR:
1868 {
1869 forloop_T *forloop = &iptr->isn_arg.forloop;
1870
1871 smsg("%4d FOR $%d -> %d", current,
1872 forloop->for_idx, forloop->for_end);
1873 }
1874 break;
1875
1876 case ISN_TRY:
1877 {
1878 try_T *try = &iptr->isn_arg.try;
1879
1880 smsg("%4d TRY catch -> %d, finally -> %d", current,
1881 try->try_catch, try->try_finally);
1882 }
1883 break;
1884 case ISN_CATCH:
1885 // TODO
1886 smsg("%4d CATCH", current);
1887 break;
1888 case ISN_ENDTRY:
1889 smsg("%4d ENDTRY", current);
1890 break;
1891 case ISN_THROW:
1892 smsg("%4d THROW", current);
1893 break;
1894
1895 // expression operations on number
1896 case ISN_OPNR:
1897 case ISN_OPFLOAT:
1898 case ISN_OPANY:
1899 {
1900 char *what;
1901 char *ins;
1902
1903 switch (iptr->isn_arg.op.op_type)
1904 {
1905 case EXPR_MULT: what = "*"; break;
1906 case EXPR_DIV: what = "/"; break;
1907 case EXPR_REM: what = "%"; break;
1908 case EXPR_SUB: what = "-"; break;
1909 case EXPR_ADD: what = "+"; break;
1910 default: what = "???"; break;
1911 }
1912 switch (iptr->isn_type)
1913 {
1914 case ISN_OPNR: ins = "OPNR"; break;
1915 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
1916 case ISN_OPANY: ins = "OPANY"; break;
1917 default: ins = "???"; break;
1918 }
1919 smsg("%4d %s %s", current, ins, what);
1920 }
1921 break;
1922
1923 case ISN_COMPAREBOOL:
1924 case ISN_COMPARESPECIAL:
1925 case ISN_COMPARENR:
1926 case ISN_COMPAREFLOAT:
1927 case ISN_COMPARESTRING:
1928 case ISN_COMPAREBLOB:
1929 case ISN_COMPARELIST:
1930 case ISN_COMPAREDICT:
1931 case ISN_COMPAREFUNC:
1932 case ISN_COMPAREPARTIAL:
1933 case ISN_COMPAREANY:
1934 {
1935 char *p;
1936 char buf[10];
1937 char *type;
1938
1939 switch (iptr->isn_arg.op.op_type)
1940 {
1941 case EXPR_EQUAL: p = "=="; break;
1942 case EXPR_NEQUAL: p = "!="; break;
1943 case EXPR_GREATER: p = ">"; break;
1944 case EXPR_GEQUAL: p = ">="; break;
1945 case EXPR_SMALLER: p = "<"; break;
1946 case EXPR_SEQUAL: p = "<="; break;
1947 case EXPR_MATCH: p = "=~"; break;
1948 case EXPR_IS: p = "is"; break;
1949 case EXPR_ISNOT: p = "isnot"; break;
1950 case EXPR_NOMATCH: p = "!~"; break;
1951 default: p = "???"; break;
1952 }
1953 STRCPY(buf, p);
1954 if (iptr->isn_arg.op.op_ic == TRUE)
1955 strcat(buf, "?");
1956 switch(iptr->isn_type)
1957 {
1958 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
1959 case ISN_COMPARESPECIAL:
1960 type = "COMPARESPECIAL"; break;
1961 case ISN_COMPARENR: type = "COMPARENR"; break;
1962 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
1963 case ISN_COMPARESTRING:
1964 type = "COMPARESTRING"; break;
1965 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
1966 case ISN_COMPARELIST: type = "COMPARELIST"; break;
1967 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
1968 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
1969 case ISN_COMPAREPARTIAL:
1970 type = "COMPAREPARTIAL"; break;
1971 case ISN_COMPAREANY: type = "COMPAREANY"; break;
1972 default: type = "???"; break;
1973 }
1974
1975 smsg("%4d %s %s", current, type, buf);
1976 }
1977 break;
1978
1979 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
1980 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
1981
1982 // expression operations
1983 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
1984 case ISN_INDEX: smsg("%4d INDEX", current); break;
1985 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
1986 iptr->isn_arg.string); break;
1987 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
1988
1989 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
1990 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
1991 vartype_name(iptr->isn_arg.type.ct_type),
1992 iptr->isn_arg.type.ct_off);
1993 break;
1994 case ISN_2BOOL: if (iptr->isn_arg.number)
1995 smsg("%4d INVERT (!val)", current);
1996 else
1997 smsg("%4d 2BOOL (!!val)", current);
1998 break;
1999 case ISN_2STRING: smsg("%4d 2STRING stack[%d]", current,
2000 iptr->isn_arg.number);
2001 break;
2002
2003 case ISN_DROP: smsg("%4d DROP", current); break;
2004 }
2005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006}
2007
2008/*
2009 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2010 * list, etc. Mostly like what JavaScript does, except that empty list and
2011 * empty dictionary are FALSE.
2012 */
2013 int
2014tv2bool(typval_T *tv)
2015{
2016 switch (tv->v_type)
2017 {
2018 case VAR_NUMBER:
2019 return tv->vval.v_number != 0;
2020 case VAR_FLOAT:
2021#ifdef FEAT_FLOAT
2022 return tv->vval.v_float != 0.0;
2023#else
2024 break;
2025#endif
2026 case VAR_PARTIAL:
2027 return tv->vval.v_partial != NULL;
2028 case VAR_FUNC:
2029 case VAR_STRING:
2030 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2031 case VAR_LIST:
2032 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2033 case VAR_DICT:
2034 return tv->vval.v_dict != NULL
2035 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2036 case VAR_BOOL:
2037 case VAR_SPECIAL:
2038 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2039 case VAR_JOB:
2040#ifdef FEAT_JOB_CHANNEL
2041 return tv->vval.v_job != NULL;
2042#else
2043 break;
2044#endif
2045 case VAR_CHANNEL:
2046#ifdef FEAT_JOB_CHANNEL
2047 return tv->vval.v_channel != NULL;
2048#else
2049 break;
2050#endif
2051 case VAR_BLOB:
2052 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2053 case VAR_UNKNOWN:
2054 case VAR_VOID:
2055 break;
2056 }
2057 return FALSE;
2058}
2059
2060/*
2061 * If "tv" is a string give an error and return FAIL.
2062 */
2063 int
2064check_not_string(typval_T *tv)
2065{
2066 if (tv->v_type == VAR_STRING)
2067 {
2068 emsg(_("E1030: Using a String as a Number"));
2069 clear_tv(tv);
2070 return FAIL;
2071 }
2072 return OK;
2073}
2074
2075
2076#endif // FEAT_EVAL