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