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