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