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