blob: 3bbf94ff676415fb402c302af45085f7dcbd6550 [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 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100530 if (needclr)
531 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 ectx.ec_stack.ga_len -= count;
533 }
534 break;
535
536 // load local variable or argument
537 case ISN_LOAD:
538 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
539 goto failed;
540 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
541 ++ectx.ec_stack.ga_len;
542 break;
543
544 // load v: variable
545 case ISN_LOADV:
546 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
547 goto failed;
548 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
549 ++ectx.ec_stack.ga_len;
550 break;
551
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100552 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 case ISN_LOADSCRIPT:
554 {
555 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100556 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557 svar_T *sv;
558
559 sv = ((svar_T *)si->sn_var_vals.ga_data)
560 + iptr->isn_arg.script.script_idx;
561 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
562 goto failed;
563 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
564 ++ectx.ec_stack.ga_len;
565 }
566 break;
567
568 // load s: variable in old script
569 case ISN_LOADS:
570 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100571 hashtab_T *ht = &SCRIPT_VARS(
572 iptr->isn_arg.loadstore.ls_sid);
573 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576 if (di == NULL)
577 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100578 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 goto failed;
580 }
581 else
582 {
583 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
584 goto failed;
585 copy_tv(&di->di_tv, STACK_TV_BOT(0));
586 ++ectx.ec_stack.ga_len;
587 }
588 }
589 break;
590
591 // load g: variable
592 case ISN_LOADG:
593 {
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100594 dictitem_T *di = find_var_in_ht(get_globvar_ht(), 0,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595 iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 if (di == NULL)
598 {
599 semsg(_("E121: Undefined variable: g:%s"),
600 iptr->isn_arg.string);
601 goto failed;
602 }
603 else
604 {
605 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
606 goto failed;
607 copy_tv(&di->di_tv, STACK_TV_BOT(0));
608 ++ectx.ec_stack.ga_len;
609 }
610 }
611 break;
612
613 // load &option
614 case ISN_LOADOPT:
615 {
616 typval_T optval;
617 char_u *name = iptr->isn_arg.string;
618
619 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
620 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100621 if (get_option_tv(&name, &optval, TRUE) == FAIL)
622 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 *STACK_TV_BOT(0) = optval;
624 ++ectx.ec_stack.ga_len;
625 }
626 break;
627
628 // load $ENV
629 case ISN_LOADENV:
630 {
631 typval_T optval;
632 char_u *name = iptr->isn_arg.string;
633
634 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
635 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100636 // name is always valid, checked when compiling
637 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 *STACK_TV_BOT(0) = optval;
639 ++ectx.ec_stack.ga_len;
640 }
641 break;
642
643 // load @register
644 case ISN_LOADREG:
645 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
646 goto failed;
647 tv = STACK_TV_BOT(0);
648 tv->v_type = VAR_STRING;
649 tv->vval.v_string = get_reg_contents(
650 iptr->isn_arg.number, GREG_EXPR_SRC);
651 ++ectx.ec_stack.ga_len;
652 break;
653
654 // store local variable
655 case ISN_STORE:
656 --ectx.ec_stack.ga_len;
657 tv = STACK_TV_VAR(iptr->isn_arg.number);
658 clear_tv(tv);
659 *tv = *STACK_TV_BOT(0);
660 break;
661
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100662 // store s: variable in old script
663 case ISN_STORES:
664 {
665 hashtab_T *ht = &SCRIPT_VARS(
666 iptr->isn_arg.loadstore.ls_sid);
667 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100668 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100669
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100670 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100671 if (di == NULL)
672 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
673 else
674 {
675 clear_tv(&di->di_tv);
676 di->di_tv = *STACK_TV_BOT(0);
677 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100678 }
679 break;
680
681 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 case ISN_STORESCRIPT:
683 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100684 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 iptr->isn_arg.script.script_sid);
686 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
687 + iptr->isn_arg.script.script_idx;
688
689 --ectx.ec_stack.ga_len;
690 clear_tv(sv->sv_tv);
691 *sv->sv_tv = *STACK_TV_BOT(0);
692 }
693 break;
694
695 // store option
696 case ISN_STOREOPT:
697 {
698 long n = 0;
699 char_u *s = NULL;
700 char *msg;
701
702 --ectx.ec_stack.ga_len;
703 tv = STACK_TV_BOT(0);
704 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100705 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100707 if (s == NULL)
708 s = (char_u *)"";
709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710 else if (tv->v_type == VAR_NUMBER)
711 n = tv->vval.v_number;
712 else
713 {
714 emsg(_("E1051: Expected string or number"));
715 goto failed;
716 }
717 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
718 n, s, iptr->isn_arg.storeopt.so_flags);
719 if (msg != NULL)
720 {
721 emsg(_(msg));
722 goto failed;
723 }
724 clear_tv(tv);
725 }
726 break;
727
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100728 // store $ENV
729 case ISN_STOREENV:
730 --ectx.ec_stack.ga_len;
731 vim_setenv_ext(iptr->isn_arg.string,
732 tv_get_string(STACK_TV_BOT(0)));
733 break;
734
735 // store @r
736 case ISN_STOREREG:
737 {
738 int reg = iptr->isn_arg.number;
739
740 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100741 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100742 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100743 tv_get_string(tv), -1, FALSE);
744 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100745 }
746 break;
747
748 // store v: variable
749 case ISN_STOREV:
750 --ectx.ec_stack.ga_len;
751 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
752 == FAIL)
753 goto failed;
754 break;
755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 // store g: variable
757 case ISN_STOREG:
758 {
759 dictitem_T *di;
760
761 --ectx.ec_stack.ga_len;
762 di = find_var_in_ht(get_globvar_ht(), 0,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100763 iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100765 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 else
767 {
768 clear_tv(&di->di_tv);
769 di->di_tv = *STACK_TV_BOT(0);
770 }
771 }
772 break;
773
774 // store number in local variable
775 case ISN_STORENR:
776 tv = STACK_TV_VAR(iptr->isn_arg.storenr.str_idx);
777 clear_tv(tv);
778 tv->v_type = VAR_NUMBER;
779 tv->vval.v_number = iptr->isn_arg.storenr.str_val;
780 break;
781
782 // push constant
783 case ISN_PUSHNR:
784 case ISN_PUSHBOOL:
785 case ISN_PUSHSPEC:
786 case ISN_PUSHF:
787 case ISN_PUSHS:
788 case ISN_PUSHBLOB:
789 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
790 goto failed;
791 tv = STACK_TV_BOT(0);
792 ++ectx.ec_stack.ga_len;
793 switch (iptr->isn_type)
794 {
795 case ISN_PUSHNR:
796 tv->v_type = VAR_NUMBER;
797 tv->vval.v_number = iptr->isn_arg.number;
798 break;
799 case ISN_PUSHBOOL:
800 tv->v_type = VAR_BOOL;
801 tv->vval.v_number = iptr->isn_arg.number;
802 break;
803 case ISN_PUSHSPEC:
804 tv->v_type = VAR_SPECIAL;
805 tv->vval.v_number = iptr->isn_arg.number;
806 break;
807#ifdef FEAT_FLOAT
808 case ISN_PUSHF:
809 tv->v_type = VAR_FLOAT;
810 tv->vval.v_float = iptr->isn_arg.fnumber;
811 break;
812#endif
813 case ISN_PUSHBLOB:
814 blob_copy(iptr->isn_arg.blob, tv);
815 break;
816 default:
817 tv->v_type = VAR_STRING;
818 tv->vval.v_string = vim_strsave(iptr->isn_arg.string);
819 }
820 break;
821
822 // create a list from items on the stack; uses a single allocation
823 // for the list header and the items
824 case ISN_NEWLIST:
825 {
826 int count = iptr->isn_arg.number;
827 list_T *list = list_alloc_with_items(count);
828
829 if (list == NULL)
830 goto failed;
831 for (idx = 0; idx < count; ++idx)
832 list_set_item(list, idx, STACK_TV_BOT(idx - count));
833
834 if (count > 0)
835 ectx.ec_stack.ga_len -= count - 1;
836 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
837 goto failed;
838 else
839 ++ectx.ec_stack.ga_len;
840 tv = STACK_TV_BOT(-1);
841 tv->v_type = VAR_LIST;
842 tv->vval.v_list = list;
843 ++list->lv_refcount;
844 }
845 break;
846
847 // create a dict from items on the stack
848 case ISN_NEWDICT:
849 {
850 int count = iptr->isn_arg.number;
851 dict_T *dict = dict_alloc();
852 dictitem_T *item;
853
854 if (dict == NULL)
855 goto failed;
856 for (idx = 0; idx < count; ++idx)
857 {
858 // check key type is VAR_STRING
859 tv = STACK_TV_BOT(2 * (idx - count));
860 item = dictitem_alloc(tv->vval.v_string);
861 clear_tv(tv);
862 if (item == NULL)
863 goto failed;
864 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
865 item->di_tv.v_lock = 0;
866 if (dict_add(dict, item) == FAIL)
867 goto failed;
868 }
869
870 if (count > 0)
871 ectx.ec_stack.ga_len -= 2 * count - 1;
872 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
873 goto failed;
874 else
875 ++ectx.ec_stack.ga_len;
876 tv = STACK_TV_BOT(-1);
877 tv->v_type = VAR_DICT;
878 tv->vval.v_dict = dict;
879 ++dict->dv_refcount;
880 }
881 break;
882
883 // call a :def function
884 case ISN_DCALL:
885 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
886 iptr->isn_arg.dfunc.cdf_argcount,
887 &ectx) == FAIL)
888 goto failed;
889 break;
890
891 // call a builtin function
892 case ISN_BCALL:
893 SOURCING_LNUM = iptr->isn_lnum;
894 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
895 iptr->isn_arg.bfunc.cbf_argcount,
896 &ectx) == FAIL)
897 goto failed;
898 break;
899
900 // call a funcref or partial
901 case ISN_PCALL:
902 {
903 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
904 int r;
905 typval_T partial;
906
907 SOURCING_LNUM = iptr->isn_lnum;
908 if (pfunc->cpf_top)
909 {
910 // funcref is above the arguments
911 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
912 }
913 else
914 {
915 // Get the funcref from the stack.
916 --ectx.ec_stack.ga_len;
917 partial = *STACK_TV_BOT(0);
918 tv = &partial;
919 }
920 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
921 if (tv == &partial)
922 clear_tv(&partial);
923 if (r == FAIL)
924 goto failed;
925
926 if (pfunc->cpf_top)
927 {
928 // Get the funcref from the stack, overwrite with the
929 // return value.
930 clear_tv(tv);
931 --ectx.ec_stack.ga_len;
932 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
933 }
934 }
935 break;
936
937 // call a user defined function or funcref/partial
938 case ISN_UCALL:
939 {
940 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
941
942 SOURCING_LNUM = iptr->isn_lnum;
943 if (call_eval_func(cufunc->cuf_name,
944 cufunc->cuf_argcount, &ectx) == FAIL)
945 goto failed;
946 }
947 break;
948
949 // return from a :def function call
950 case ISN_RETURN:
951 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +0100952 garray_T *trystack = &ectx.ec_trystack;
953
954 if (trystack->ga_len > 0)
955 trycmd = ((trycmd_T *)trystack->ga_data)
956 + trystack->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame
958 && trycmd->tcd_finally_idx != 0)
959 {
960 // jump to ":finally"
961 ectx.ec_iidx = trycmd->tcd_finally_idx;
962 trycmd->tcd_return = TRUE;
963 }
964 else
965 {
966 // Restore previous function. If the frame pointer
967 // is zero then there is none and we are done.
968 if (ectx.ec_frame == initial_frame_ptr)
969 goto done;
970
971 func_return(&ectx);
972 }
973 }
974 break;
975
976 // push a function reference to a compiled function
977 case ISN_FUNCREF:
978 {
979 partial_T *pt = NULL;
980
981 pt = ALLOC_CLEAR_ONE(partial_T);
982 if (pt == NULL)
983 goto failed;
984 dfunc = ((dfunc_T *)def_functions.ga_data)
985 + iptr->isn_arg.number;
986 pt->pt_func = dfunc->df_ufunc;
987 pt->pt_refcount = 1;
988 ++dfunc->df_ufunc->uf_refcount;
989
990 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
991 goto failed;
992 tv = STACK_TV_BOT(0);
993 ++ectx.ec_stack.ga_len;
994 tv->vval.v_partial = pt;
995 tv->v_type = VAR_PARTIAL;
996 }
997 break;
998
999 // jump if a condition is met
1000 case ISN_JUMP:
1001 {
1002 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1003 int jump = TRUE;
1004
1005 if (when != JUMP_ALWAYS)
1006 {
1007 tv = STACK_TV_BOT(-1);
1008 jump = tv2bool(tv);
1009 if (when == JUMP_IF_FALSE
1010 || when == JUMP_AND_KEEP_IF_FALSE)
1011 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001012 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 {
1014 // drop the value from the stack
1015 clear_tv(tv);
1016 --ectx.ec_stack.ga_len;
1017 }
1018 }
1019 if (jump)
1020 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1021 }
1022 break;
1023
1024 // top of a for loop
1025 case ISN_FOR:
1026 {
1027 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1028 typval_T *idxtv =
1029 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1030
1031 // push the next item from the list
1032 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1033 goto failed;
1034 if (++idxtv->vval.v_number >= list->lv_len)
1035 // past the end of the list, jump to "endfor"
1036 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1037 else if (list->lv_first == &range_list_item)
1038 {
1039 // non-materialized range() list
1040 tv = STACK_TV_BOT(0);
1041 tv->v_type = VAR_NUMBER;
1042 tv->vval.v_number = list_find_nr(
1043 list, idxtv->vval.v_number, NULL);
1044 ++ectx.ec_stack.ga_len;
1045 }
1046 else
1047 {
1048 listitem_T *li = list_find(list, idxtv->vval.v_number);
1049
1050 if (li == NULL)
1051 goto failed;
1052 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1053 ++ectx.ec_stack.ga_len;
1054 }
1055 }
1056 break;
1057
1058 // start of ":try" block
1059 case ISN_TRY:
1060 {
1061 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1062 goto failed;
1063 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1064 + ectx.ec_trystack.ga_len;
1065 ++ectx.ec_trystack.ga_len;
1066 ++trylevel;
1067 trycmd->tcd_frame = ectx.ec_frame;
1068 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1069 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001070 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 }
1072 break;
1073
1074 case ISN_PUSHEXC:
1075 if (current_exception == NULL)
1076 {
1077 iemsg("Evaluating catch while current_exception is NULL");
1078 goto failed;
1079 }
1080 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1081 goto failed;
1082 tv = STACK_TV_BOT(0);
1083 ++ectx.ec_stack.ga_len;
1084 tv->v_type = VAR_STRING;
1085 tv->vval.v_string = vim_strsave(
1086 (char_u *)current_exception->value);
1087 break;
1088
1089 case ISN_CATCH:
1090 {
1091 garray_T *trystack = &ectx.ec_trystack;
1092
1093 if (trystack->ga_len > 0)
1094 {
1095 trycmd = ((trycmd_T *)trystack->ga_data)
1096 + trystack->ga_len - 1;
1097 trycmd->tcd_caught = TRUE;
1098 }
1099 did_emsg = got_int = did_throw = FALSE;
1100 catch_exception(current_exception);
1101 }
1102 break;
1103
1104 // end of ":try" block
1105 case ISN_ENDTRY:
1106 {
1107 garray_T *trystack = &ectx.ec_trystack;
1108
1109 if (trystack->ga_len > 0)
1110 {
1111 --trystack->ga_len;
1112 --trylevel;
1113 trycmd = ((trycmd_T *)trystack->ga_data)
1114 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001115 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 {
1117 // discard the exception
1118 if (caught_stack == current_exception)
1119 caught_stack = caught_stack->caught;
1120 discard_current_exception();
1121 }
1122
1123 if (trycmd->tcd_return)
1124 {
1125 // Restore previous function. If the frame pointer
1126 // is zero then there is none and we are done.
1127 if (ectx.ec_frame == initial_frame_ptr)
1128 goto done;
1129
1130 func_return(&ectx);
1131 }
1132 }
1133 }
1134 break;
1135
1136 case ISN_THROW:
1137 --ectx.ec_stack.ga_len;
1138 tv = STACK_TV_BOT(0);
1139 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1140 {
1141 vim_free(tv->vval.v_string);
1142 goto failed;
1143 }
1144 did_throw = TRUE;
1145 break;
1146
1147 // compare with special values
1148 case ISN_COMPAREBOOL:
1149 case ISN_COMPARESPECIAL:
1150 {
1151 typval_T *tv1 = STACK_TV_BOT(-2);
1152 typval_T *tv2 = STACK_TV_BOT(-1);
1153 varnumber_T arg1 = tv1->vval.v_number;
1154 varnumber_T arg2 = tv2->vval.v_number;
1155 int res;
1156
1157 switch (iptr->isn_arg.op.op_type)
1158 {
1159 case EXPR_EQUAL: res = arg1 == arg2; break;
1160 case EXPR_NEQUAL: res = arg1 != arg2; break;
1161 default: res = 0; break;
1162 }
1163
1164 --ectx.ec_stack.ga_len;
1165 tv1->v_type = VAR_BOOL;
1166 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1167 }
1168 break;
1169
1170 // Operation with two number arguments
1171 case ISN_OPNR:
1172 case ISN_COMPARENR:
1173 {
1174 typval_T *tv1 = STACK_TV_BOT(-2);
1175 typval_T *tv2 = STACK_TV_BOT(-1);
1176 varnumber_T arg1 = tv1->vval.v_number;
1177 varnumber_T arg2 = tv2->vval.v_number;
1178 varnumber_T res;
1179
1180 switch (iptr->isn_arg.op.op_type)
1181 {
1182 case EXPR_MULT: res = arg1 * arg2; break;
1183 case EXPR_DIV: res = arg1 / arg2; break;
1184 case EXPR_REM: res = arg1 % arg2; break;
1185 case EXPR_SUB: res = arg1 - arg2; break;
1186 case EXPR_ADD: res = arg1 + arg2; break;
1187
1188 case EXPR_EQUAL: res = arg1 == arg2; break;
1189 case EXPR_NEQUAL: res = arg1 != arg2; break;
1190 case EXPR_GREATER: res = arg1 > arg2; break;
1191 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1192 case EXPR_SMALLER: res = arg1 < arg2; break;
1193 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1194 default: res = 0; break;
1195 }
1196
1197 --ectx.ec_stack.ga_len;
1198 if (iptr->isn_type == ISN_COMPARENR)
1199 {
1200 tv1->v_type = VAR_BOOL;
1201 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1202 }
1203 else
1204 tv1->vval.v_number = res;
1205 }
1206 break;
1207
1208 // Computation with two float arguments
1209 case ISN_OPFLOAT:
1210 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001211#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 {
1213 typval_T *tv1 = STACK_TV_BOT(-2);
1214 typval_T *tv2 = STACK_TV_BOT(-1);
1215 float_T arg1 = tv1->vval.v_float;
1216 float_T arg2 = tv2->vval.v_float;
1217 float_T res = 0;
1218 int cmp = FALSE;
1219
1220 switch (iptr->isn_arg.op.op_type)
1221 {
1222 case EXPR_MULT: res = arg1 * arg2; break;
1223 case EXPR_DIV: res = arg1 / arg2; break;
1224 case EXPR_SUB: res = arg1 - arg2; break;
1225 case EXPR_ADD: res = arg1 + arg2; break;
1226
1227 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1228 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1229 case EXPR_GREATER: cmp = arg1 > arg2; break;
1230 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1231 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1232 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1233 default: cmp = 0; break;
1234 }
1235 --ectx.ec_stack.ga_len;
1236 if (iptr->isn_type == ISN_COMPAREFLOAT)
1237 {
1238 tv1->v_type = VAR_BOOL;
1239 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1240 }
1241 else
1242 tv1->vval.v_float = res;
1243 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001244#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 break;
1246
1247 case ISN_COMPARELIST:
1248 {
1249 typval_T *tv1 = STACK_TV_BOT(-2);
1250 typval_T *tv2 = STACK_TV_BOT(-1);
1251 list_T *arg1 = tv1->vval.v_list;
1252 list_T *arg2 = tv2->vval.v_list;
1253 int cmp = FALSE;
1254 int ic = iptr->isn_arg.op.op_ic;
1255
1256 switch (iptr->isn_arg.op.op_type)
1257 {
1258 case EXPR_EQUAL: cmp =
1259 list_equal(arg1, arg2, ic, FALSE); break;
1260 case EXPR_NEQUAL: cmp =
1261 !list_equal(arg1, arg2, ic, FALSE); break;
1262 case EXPR_IS: cmp = arg1 == arg2; break;
1263 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1264 default: cmp = 0; break;
1265 }
1266 --ectx.ec_stack.ga_len;
1267 clear_tv(tv1);
1268 clear_tv(tv2);
1269 tv1->v_type = VAR_BOOL;
1270 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1271 }
1272 break;
1273
1274 case ISN_COMPAREBLOB:
1275 {
1276 typval_T *tv1 = STACK_TV_BOT(-2);
1277 typval_T *tv2 = STACK_TV_BOT(-1);
1278 blob_T *arg1 = tv1->vval.v_blob;
1279 blob_T *arg2 = tv2->vval.v_blob;
1280 int cmp = FALSE;
1281
1282 switch (iptr->isn_arg.op.op_type)
1283 {
1284 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1285 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1286 case EXPR_IS: cmp = arg1 == arg2; break;
1287 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1288 default: cmp = 0; break;
1289 }
1290 --ectx.ec_stack.ga_len;
1291 clear_tv(tv1);
1292 clear_tv(tv2);
1293 tv1->v_type = VAR_BOOL;
1294 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1295 }
1296 break;
1297
1298 // TODO: handle separately
1299 case ISN_COMPARESTRING:
1300 case ISN_COMPAREDICT:
1301 case ISN_COMPAREFUNC:
1302 case ISN_COMPAREPARTIAL:
1303 case ISN_COMPAREANY:
1304 {
1305 typval_T *tv1 = STACK_TV_BOT(-2);
1306 typval_T *tv2 = STACK_TV_BOT(-1);
1307 exptype_T exptype = iptr->isn_arg.op.op_type;
1308 int ic = iptr->isn_arg.op.op_ic;
1309
1310 typval_compare(tv1, tv2, exptype, ic);
1311 clear_tv(tv2);
1312 tv1->v_type = VAR_BOOL;
1313 tv1->vval.v_number = tv1->vval.v_number
1314 ? VVAL_TRUE : VVAL_FALSE;
1315 --ectx.ec_stack.ga_len;
1316 }
1317 break;
1318
1319 case ISN_ADDLIST:
1320 case ISN_ADDBLOB:
1321 {
1322 typval_T *tv1 = STACK_TV_BOT(-2);
1323 typval_T *tv2 = STACK_TV_BOT(-1);
1324
1325 if (iptr->isn_type == ISN_ADDLIST)
1326 eval_addlist(tv1, tv2);
1327 else
1328 eval_addblob(tv1, tv2);
1329 clear_tv(tv2);
1330 --ectx.ec_stack.ga_len;
1331 }
1332 break;
1333
1334 // Computation with two arguments of unknown type
1335 case ISN_OPANY:
1336 {
1337 typval_T *tv1 = STACK_TV_BOT(-2);
1338 typval_T *tv2 = STACK_TV_BOT(-1);
1339 varnumber_T n1, n2;
1340#ifdef FEAT_FLOAT
1341 float_T f1 = 0, f2 = 0;
1342#endif
1343 int error = FALSE;
1344
1345 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1346 {
1347 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1348 {
1349 eval_addlist(tv1, tv2);
1350 clear_tv(tv2);
1351 --ectx.ec_stack.ga_len;
1352 break;
1353 }
1354 else if (tv1->v_type == VAR_BLOB
1355 && tv2->v_type == VAR_BLOB)
1356 {
1357 eval_addblob(tv1, tv2);
1358 clear_tv(tv2);
1359 --ectx.ec_stack.ga_len;
1360 break;
1361 }
1362 }
1363#ifdef FEAT_FLOAT
1364 if (tv1->v_type == VAR_FLOAT)
1365 {
1366 f1 = tv1->vval.v_float;
1367 n1 = 0;
1368 }
1369 else
1370#endif
1371 {
1372 n1 = tv_get_number_chk(tv1, &error);
1373 if (error)
1374 goto failed;
1375#ifdef FEAT_FLOAT
1376 if (tv2->v_type == VAR_FLOAT)
1377 f1 = n1;
1378#endif
1379 }
1380#ifdef FEAT_FLOAT
1381 if (tv2->v_type == VAR_FLOAT)
1382 {
1383 f2 = tv2->vval.v_float;
1384 n2 = 0;
1385 }
1386 else
1387#endif
1388 {
1389 n2 = tv_get_number_chk(tv2, &error);
1390 if (error)
1391 goto failed;
1392#ifdef FEAT_FLOAT
1393 if (tv1->v_type == VAR_FLOAT)
1394 f2 = n2;
1395#endif
1396 }
1397#ifdef FEAT_FLOAT
1398 // if there is a float on either side the result is a float
1399 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1400 {
1401 switch (iptr->isn_arg.op.op_type)
1402 {
1403 case EXPR_MULT: f1 = f1 * f2; break;
1404 case EXPR_DIV: f1 = f1 / f2; break;
1405 case EXPR_SUB: f1 = f1 - f2; break;
1406 case EXPR_ADD: f1 = f1 + f2; break;
1407 default: emsg(_(e_modulus)); goto failed;
1408 }
1409 clear_tv(tv1);
1410 clear_tv(tv2);
1411 tv1->v_type = VAR_FLOAT;
1412 tv1->vval.v_float = f1;
1413 --ectx.ec_stack.ga_len;
1414 }
1415 else
1416#endif
1417 {
1418 switch (iptr->isn_arg.op.op_type)
1419 {
1420 case EXPR_MULT: n1 = n1 * n2; break;
1421 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1422 case EXPR_SUB: n1 = n1 - n2; break;
1423 case EXPR_ADD: n1 = n1 + n2; break;
1424 default: n1 = num_modulus(n1, n2); break;
1425 }
1426 clear_tv(tv1);
1427 clear_tv(tv2);
1428 tv1->v_type = VAR_NUMBER;
1429 tv1->vval.v_number = n1;
1430 --ectx.ec_stack.ga_len;
1431 }
1432 }
1433 break;
1434
1435 case ISN_CONCAT:
1436 {
1437 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1438 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1439 char_u *res;
1440
1441 res = concat_str(str1, str2);
1442 clear_tv(STACK_TV_BOT(-2));
1443 clear_tv(STACK_TV_BOT(-1));
1444 --ectx.ec_stack.ga_len;
1445 STACK_TV_BOT(-1)->vval.v_string = res;
1446 }
1447 break;
1448
1449 case ISN_INDEX:
1450 {
1451 list_T *list;
1452 varnumber_T n;
1453 listitem_T *li;
1454
1455 // list index: list is at stack-2, index at stack-1
1456 tv = STACK_TV_BOT(-2);
1457 if (tv->v_type != VAR_LIST)
1458 {
1459 emsg(_(e_listreq));
1460 goto failed;
1461 }
1462 list = tv->vval.v_list;
1463
1464 tv = STACK_TV_BOT(-1);
1465 if (tv->v_type != VAR_NUMBER)
1466 {
1467 emsg(_(e_number_exp));
1468 goto failed;
1469 }
1470 n = tv->vval.v_number;
1471 clear_tv(tv);
1472 if ((li = list_find(list, n)) == NULL)
1473 {
1474 semsg(_(e_listidx), n);
1475 goto failed;
1476 }
1477 --ectx.ec_stack.ga_len;
1478 clear_tv(STACK_TV_BOT(-1));
1479 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1480 }
1481 break;
1482
1483 // dict member with string key
1484 case ISN_MEMBER:
1485 {
1486 dict_T *dict;
1487 dictitem_T *di;
1488
1489 tv = STACK_TV_BOT(-1);
1490 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1491 {
1492 emsg(_(e_dictreq));
1493 goto failed;
1494 }
1495 dict = tv->vval.v_dict;
1496
1497 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1498 == NULL)
1499 {
1500 semsg(_(e_dictkey), iptr->isn_arg.string);
1501 goto failed;
1502 }
1503 clear_tv(tv);
1504 copy_tv(&di->di_tv, tv);
1505 }
1506 break;
1507
1508 case ISN_NEGATENR:
1509 tv = STACK_TV_BOT(-1);
1510 tv->vval.v_number = -tv->vval.v_number;
1511 break;
1512
1513 case ISN_CHECKNR:
1514 {
1515 int error = FALSE;
1516
1517 tv = STACK_TV_BOT(-1);
1518 if (check_not_string(tv) == FAIL)
1519 {
1520 --ectx.ec_stack.ga_len;
1521 goto failed;
1522 }
1523 (void)tv_get_number_chk(tv, &error);
1524 if (error)
1525 goto failed;
1526 }
1527 break;
1528
1529 case ISN_CHECKTYPE:
1530 {
1531 checktype_T *ct = &iptr->isn_arg.type;
1532
1533 tv = STACK_TV_BOT(ct->ct_off);
1534 if (tv->v_type != ct->ct_type)
1535 {
1536 semsg(_("E1029: Expected %s but got %s"),
1537 vartype_name(ct->ct_type),
1538 vartype_name(tv->v_type));
1539 goto failed;
1540 }
1541 }
1542 break;
1543
1544 case ISN_2BOOL:
1545 {
1546 int n;
1547
1548 tv = STACK_TV_BOT(-1);
1549 n = tv2bool(tv);
1550 if (iptr->isn_arg.number) // invert
1551 n = !n;
1552 clear_tv(tv);
1553 tv->v_type = VAR_BOOL;
1554 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
1555 }
1556 break;
1557
1558 case ISN_2STRING:
1559 {
1560 char_u *str;
1561
1562 tv = STACK_TV_BOT(iptr->isn_arg.number);
1563 if (tv->v_type != VAR_STRING)
1564 {
1565 str = typval_tostring(tv);
1566 clear_tv(tv);
1567 tv->v_type = VAR_STRING;
1568 tv->vval.v_string = str;
1569 }
1570 }
1571 break;
1572
1573 case ISN_DROP:
1574 --ectx.ec_stack.ga_len;
1575 clear_tv(STACK_TV_BOT(0));
1576 break;
1577 }
1578 }
1579
1580done:
1581 // function finished, get result from the stack.
1582 tv = STACK_TV_BOT(-1);
1583 *rettv = *tv;
1584 tv->v_type = VAR_UNKNOWN;
1585 ret = OK;
1586
1587failed:
1588 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
1589 clear_tv(STACK_TV(idx));
1590 vim_free(ectx.ec_stack.ga_data);
1591 return ret;
1592}
1593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594/*
1595 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01001596 * We don't really need this at runtime, but we do have tests that require it,
1597 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 */
1599 void
1600ex_disassemble(exarg_T *eap)
1601{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001602 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001603 char_u *fname;
1604 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 dfunc_T *dfunc;
1606 isn_T *instr;
1607 int current;
1608 int line_idx = 0;
1609 int prev_current = 0;
1610
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001611 fname = trans_function_name(&arg, FALSE,
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001612 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001613 if (fname == NULL)
1614 {
1615 semsg(_(e_invarg2), eap->arg);
1616 return;
1617 }
1618
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001619 ufunc = find_func(fname, NULL);
1620 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 if (ufunc == NULL)
1622 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001623 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 return;
1625 }
1626 if (ufunc->uf_dfunc_idx < 0)
1627 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001628 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 return;
1630 }
1631 if (ufunc->uf_name_exp != NULL)
1632 msg((char *)ufunc->uf_name_exp);
1633 else
1634 msg((char *)ufunc->uf_name);
1635
1636 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
1637 instr = dfunc->df_instr;
1638 for (current = 0; current < dfunc->df_instr_count; ++current)
1639 {
1640 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001641 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642
1643 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
1644 {
1645 if (current > prev_current)
1646 {
1647 msg_puts("\n\n");
1648 prev_current = current;
1649 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001650 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
1651 if (line != NULL)
1652 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 }
1654
1655 switch (iptr->isn_type)
1656 {
1657 case ISN_EXEC:
1658 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
1659 break;
1660 case ISN_ECHO:
1661 {
1662 echo_T *echo = &iptr->isn_arg.echo;
1663
1664 smsg("%4d %s %d", current,
1665 echo->echo_with_white ? "ECHO" : "ECHON",
1666 echo->echo_count);
1667 }
1668 break;
1669 case ISN_LOAD:
1670 if (iptr->isn_arg.number < 0)
1671 smsg("%4d LOAD arg[%lld]", current,
1672 iptr->isn_arg.number + STACK_FRAME_SIZE);
1673 else
1674 smsg("%4d LOAD $%lld", current, iptr->isn_arg.number);
1675 break;
1676 case ISN_LOADV:
1677 smsg("%4d LOADV v:%s", current,
1678 get_vim_var_name(iptr->isn_arg.number));
1679 break;
1680 case ISN_LOADSCRIPT:
1681 {
1682 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001683 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1685 + iptr->isn_arg.script.script_idx;
1686
1687 smsg("%4d LOADSCRIPT %s from %s", current,
1688 sv->sv_name, si->sn_name);
1689 }
1690 break;
1691 case ISN_LOADS:
1692 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001693 scriptitem_T *si = SCRIPT_ITEM(
1694 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695
1696 smsg("%4d LOADS s:%s from %s", current,
1697 iptr->isn_arg.string, si->sn_name);
1698 }
1699 break;
1700 case ISN_LOADG:
1701 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
1702 break;
1703 case ISN_LOADOPT:
1704 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
1705 break;
1706 case ISN_LOADENV:
1707 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
1708 break;
1709 case ISN_LOADREG:
1710 smsg("%4d LOADREG @%c", current, iptr->isn_arg.number);
1711 break;
1712
1713 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001714 if (iptr->isn_arg.number < 0)
1715 smsg("%4d STORE arg[%lld]", current,
1716 iptr->isn_arg.number + STACK_FRAME_SIZE);
1717 else
1718 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001720 case ISN_STOREV:
1721 smsg("%4d STOREV v:%s", current,
1722 get_vim_var_name(iptr->isn_arg.number));
1723 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001725 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
1726 break;
1727 case ISN_STORES:
1728 {
1729 scriptitem_T *si = SCRIPT_ITEM(
1730 iptr->isn_arg.loadstore.ls_sid);
1731
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001732 smsg("%4d STORES %s in %s", current,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001733 iptr->isn_arg.string, si->sn_name);
1734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 break;
1736 case ISN_STORESCRIPT:
1737 {
1738 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001739 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1741 + iptr->isn_arg.script.script_idx;
1742
1743 smsg("%4d STORESCRIPT %s in %s", current,
1744 sv->sv_name, si->sn_name);
1745 }
1746 break;
1747 case ISN_STOREOPT:
1748 smsg("%4d STOREOPT &%s", current,
1749 iptr->isn_arg.storeopt.so_name);
1750 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001751 case ISN_STOREENV:
1752 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
1753 break;
1754 case ISN_STOREREG:
1755 smsg("%4d STOREREG @%c", current, iptr->isn_arg.number);
1756 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 case ISN_STORENR:
1758 smsg("%4d STORE %lld in $%d", current,
1759 iptr->isn_arg.storenr.str_val,
1760 iptr->isn_arg.storenr.str_idx);
1761 break;
1762
1763 // constants
1764 case ISN_PUSHNR:
1765 smsg("%4d PUSHNR %lld", current, iptr->isn_arg.number);
1766 break;
1767 case ISN_PUSHBOOL:
1768 case ISN_PUSHSPEC:
1769 smsg("%4d PUSH %s", current,
1770 get_var_special_name(iptr->isn_arg.number));
1771 break;
1772 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001773#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01001775#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 break;
1777 case ISN_PUSHS:
1778 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
1779 break;
1780 case ISN_PUSHBLOB:
1781 {
1782 char_u *r;
1783 char_u numbuf[NUMBUFLEN];
1784 char_u *tofree;
1785
1786 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01001787 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 vim_free(tofree);
1789 }
1790 break;
1791 case ISN_PUSHEXC:
1792 smsg("%4d PUSH v:exception", current);
1793 break;
1794 case ISN_NEWLIST:
1795 smsg("%4d NEWLIST size %lld", current, iptr->isn_arg.number);
1796 break;
1797 case ISN_NEWDICT:
1798 smsg("%4d NEWDICT size %lld", current, iptr->isn_arg.number);
1799 break;
1800
1801 // function call
1802 case ISN_BCALL:
1803 {
1804 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
1805
1806 smsg("%4d BCALL %s(argc %d)", current,
1807 internal_func_name(cbfunc->cbf_idx),
1808 cbfunc->cbf_argcount);
1809 }
1810 break;
1811 case ISN_DCALL:
1812 {
1813 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
1814 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1815 + cdfunc->cdf_idx;
1816
1817 smsg("%4d DCALL %s(argc %d)", current,
1818 df->df_ufunc->uf_name_exp != NULL
1819 ? df->df_ufunc->uf_name_exp
1820 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
1821 }
1822 break;
1823 case ISN_UCALL:
1824 {
1825 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1826
1827 smsg("%4d UCALL %s(argc %d)", current,
1828 cufunc->cuf_name, cufunc->cuf_argcount);
1829 }
1830 break;
1831 case ISN_PCALL:
1832 {
1833 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
1834
1835 smsg("%4d PCALL%s (argc %d)", current,
1836 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
1837 }
1838 break;
1839 case ISN_RETURN:
1840 smsg("%4d RETURN", current);
1841 break;
1842 case ISN_FUNCREF:
1843 {
1844 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1845 + iptr->isn_arg.number;
1846
1847 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
1848 }
1849 break;
1850
1851 case ISN_JUMP:
1852 {
1853 char *when = "?";
1854
1855 switch (iptr->isn_arg.jump.jump_when)
1856 {
1857 case JUMP_ALWAYS:
1858 when = "JUMP";
1859 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 case JUMP_AND_KEEP_IF_TRUE:
1861 when = "JUMP_AND_KEEP_IF_TRUE";
1862 break;
1863 case JUMP_IF_FALSE:
1864 when = "JUMP_IF_FALSE";
1865 break;
1866 case JUMP_AND_KEEP_IF_FALSE:
1867 when = "JUMP_AND_KEEP_IF_FALSE";
1868 break;
1869 }
1870 smsg("%4d %s -> %lld", current, when,
1871 iptr->isn_arg.jump.jump_where);
1872 }
1873 break;
1874
1875 case ISN_FOR:
1876 {
1877 forloop_T *forloop = &iptr->isn_arg.forloop;
1878
1879 smsg("%4d FOR $%d -> %d", current,
1880 forloop->for_idx, forloop->for_end);
1881 }
1882 break;
1883
1884 case ISN_TRY:
1885 {
1886 try_T *try = &iptr->isn_arg.try;
1887
1888 smsg("%4d TRY catch -> %d, finally -> %d", current,
1889 try->try_catch, try->try_finally);
1890 }
1891 break;
1892 case ISN_CATCH:
1893 // TODO
1894 smsg("%4d CATCH", current);
1895 break;
1896 case ISN_ENDTRY:
1897 smsg("%4d ENDTRY", current);
1898 break;
1899 case ISN_THROW:
1900 smsg("%4d THROW", current);
1901 break;
1902
1903 // expression operations on number
1904 case ISN_OPNR:
1905 case ISN_OPFLOAT:
1906 case ISN_OPANY:
1907 {
1908 char *what;
1909 char *ins;
1910
1911 switch (iptr->isn_arg.op.op_type)
1912 {
1913 case EXPR_MULT: what = "*"; break;
1914 case EXPR_DIV: what = "/"; break;
1915 case EXPR_REM: what = "%"; break;
1916 case EXPR_SUB: what = "-"; break;
1917 case EXPR_ADD: what = "+"; break;
1918 default: what = "???"; break;
1919 }
1920 switch (iptr->isn_type)
1921 {
1922 case ISN_OPNR: ins = "OPNR"; break;
1923 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
1924 case ISN_OPANY: ins = "OPANY"; break;
1925 default: ins = "???"; break;
1926 }
1927 smsg("%4d %s %s", current, ins, what);
1928 }
1929 break;
1930
1931 case ISN_COMPAREBOOL:
1932 case ISN_COMPARESPECIAL:
1933 case ISN_COMPARENR:
1934 case ISN_COMPAREFLOAT:
1935 case ISN_COMPARESTRING:
1936 case ISN_COMPAREBLOB:
1937 case ISN_COMPARELIST:
1938 case ISN_COMPAREDICT:
1939 case ISN_COMPAREFUNC:
1940 case ISN_COMPAREPARTIAL:
1941 case ISN_COMPAREANY:
1942 {
1943 char *p;
1944 char buf[10];
1945 char *type;
1946
1947 switch (iptr->isn_arg.op.op_type)
1948 {
1949 case EXPR_EQUAL: p = "=="; break;
1950 case EXPR_NEQUAL: p = "!="; break;
1951 case EXPR_GREATER: p = ">"; break;
1952 case EXPR_GEQUAL: p = ">="; break;
1953 case EXPR_SMALLER: p = "<"; break;
1954 case EXPR_SEQUAL: p = "<="; break;
1955 case EXPR_MATCH: p = "=~"; break;
1956 case EXPR_IS: p = "is"; break;
1957 case EXPR_ISNOT: p = "isnot"; break;
1958 case EXPR_NOMATCH: p = "!~"; break;
1959 default: p = "???"; break;
1960 }
1961 STRCPY(buf, p);
1962 if (iptr->isn_arg.op.op_ic == TRUE)
1963 strcat(buf, "?");
1964 switch(iptr->isn_type)
1965 {
1966 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
1967 case ISN_COMPARESPECIAL:
1968 type = "COMPARESPECIAL"; break;
1969 case ISN_COMPARENR: type = "COMPARENR"; break;
1970 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
1971 case ISN_COMPARESTRING:
1972 type = "COMPARESTRING"; break;
1973 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
1974 case ISN_COMPARELIST: type = "COMPARELIST"; break;
1975 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
1976 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
1977 case ISN_COMPAREPARTIAL:
1978 type = "COMPAREPARTIAL"; break;
1979 case ISN_COMPAREANY: type = "COMPAREANY"; break;
1980 default: type = "???"; break;
1981 }
1982
1983 smsg("%4d %s %s", current, type, buf);
1984 }
1985 break;
1986
1987 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
1988 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
1989
1990 // expression operations
1991 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
1992 case ISN_INDEX: smsg("%4d INDEX", current); break;
1993 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
1994 iptr->isn_arg.string); break;
1995 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
1996
1997 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
1998 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
1999 vartype_name(iptr->isn_arg.type.ct_type),
2000 iptr->isn_arg.type.ct_off);
2001 break;
2002 case ISN_2BOOL: if (iptr->isn_arg.number)
2003 smsg("%4d INVERT (!val)", current);
2004 else
2005 smsg("%4d 2BOOL (!!val)", current);
2006 break;
2007 case ISN_2STRING: smsg("%4d 2STRING stack[%d]", current,
2008 iptr->isn_arg.number);
2009 break;
2010
2011 case ISN_DROP: smsg("%4d DROP", current); break;
2012 }
2013 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014}
2015
2016/*
2017 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2018 * list, etc. Mostly like what JavaScript does, except that empty list and
2019 * empty dictionary are FALSE.
2020 */
2021 int
2022tv2bool(typval_T *tv)
2023{
2024 switch (tv->v_type)
2025 {
2026 case VAR_NUMBER:
2027 return tv->vval.v_number != 0;
2028 case VAR_FLOAT:
2029#ifdef FEAT_FLOAT
2030 return tv->vval.v_float != 0.0;
2031#else
2032 break;
2033#endif
2034 case VAR_PARTIAL:
2035 return tv->vval.v_partial != NULL;
2036 case VAR_FUNC:
2037 case VAR_STRING:
2038 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2039 case VAR_LIST:
2040 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2041 case VAR_DICT:
2042 return tv->vval.v_dict != NULL
2043 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2044 case VAR_BOOL:
2045 case VAR_SPECIAL:
2046 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2047 case VAR_JOB:
2048#ifdef FEAT_JOB_CHANNEL
2049 return tv->vval.v_job != NULL;
2050#else
2051 break;
2052#endif
2053 case VAR_CHANNEL:
2054#ifdef FEAT_JOB_CHANNEL
2055 return tv->vval.v_channel != NULL;
2056#else
2057 break;
2058#endif
2059 case VAR_BLOB:
2060 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2061 case VAR_UNKNOWN:
2062 case VAR_VOID:
2063 break;
2064 }
2065 return FALSE;
2066}
2067
2068/*
2069 * If "tv" is a string give an error and return FAIL.
2070 */
2071 int
2072check_not_string(typval_T *tv)
2073{
2074 if (tv->v_type == VAR_STRING)
2075 {
2076 emsg(_("E1030: Using a String as a Number"));
2077 clear_tv(tv);
2078 return FAIL;
2079 }
2080 return OK;
2081}
2082
2083
2084#endif // FEAT_EVAL