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 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 19 | // When not generating protos this is included in proto.h |
| 20 | #ifdef PROTO |
| 21 | # include "vim9.h" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 24 | |
| 25 | // Structure put on ec_trystack when ISN_TRY is encountered. |
| 26 | typedef struct { |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 27 | int tcd_frame_idx; // ec_frame_idx at ISN_TRY |
| 28 | int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 29 | int tcd_in_catch; // in catch or finally block |
| 30 | int tcd_did_throw; // set did_throw in :endtry |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 31 | int tcd_catch_idx; // instruction of the first :catch or :finally |
| 32 | int tcd_finally_idx; // instruction of the :finally block or zero |
| 33 | int tcd_endtry_idx; // instruction of the :endtry |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 34 | int tcd_caught; // catch block entered |
Bram Moolenaar | 2e34c34 | 2021-03-14 12:13:33 +0100 | [diff] [blame] | 35 | int tcd_cont; // :continue encountered, jump here (minus one) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 36 | int tcd_return; // when TRUE return from end of :finally |
| 37 | } trycmd_T; |
| 38 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 39 | // Data local to a function. |
| 40 | // On a function call, if not empty, is saved on the stack and restored when |
| 41 | // returning. |
| 42 | typedef struct { |
| 43 | int floc_restore_cmdmod; |
| 44 | cmdmod_T floc_save_cmdmod; |
| 45 | int floc_restore_cmdmod_stacklen; |
| 46 | } funclocal_T; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 47 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 48 | // Structure to hold a reference to an outer_T, with information of whether it |
| 49 | // was allocated. |
| 50 | typedef struct { |
| 51 | outer_T *or_outer; |
| 52 | partial_T *or_partial; // decrement "or_partial->pt_refcount" later |
| 53 | int or_outer_allocated; // free "or_outer" later |
| 54 | } outer_ref_T; |
| 55 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 56 | // A stack is used to store: |
| 57 | // - arguments passed to a :def function |
| 58 | // - info about the calling function, to use when returning |
| 59 | // - local variables |
| 60 | // - temporary values |
| 61 | // |
| 62 | // In detail (FP == Frame Pointer): |
| 63 | // arg1 first argument from caller (if present) |
| 64 | // arg2 second argument from caller (if present) |
| 65 | // extra_arg1 any missing optional argument default value |
| 66 | // FP -> cur_func calling function |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 67 | // current previous instruction pointer |
| 68 | // frame_ptr previous Frame Pointer |
| 69 | // var1 space for local variable |
| 70 | // var2 space for local variable |
| 71 | // .... fixed space for max. number of local variables |
| 72 | // temp temporary values |
| 73 | // .... flexible space for temporary values (can grow big) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Execution context. |
| 77 | */ |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 78 | struct ectx_S { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 79 | garray_T ec_stack; // stack of typval_T values |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 80 | int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 81 | int ec_initial_frame_idx; // frame index when called |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 82 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 83 | outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 84 | funclocal_T ec_funclocal; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 85 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 86 | garray_T ec_trystack; // stack of trycmd_T values |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 87 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 88 | isn_T *ec_instr; // array with instructions |
Dominique Pelle | 3276f58 | 2021-08-07 12:44:41 +0200 | [diff] [blame] | 89 | int ec_dfunc_idx; // current function index |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 90 | int ec_iidx; // index in ec_instr: instruction to execute |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 91 | |
| 92 | garray_T ec_funcrefs; // partials that might be a closure |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 93 | |
| 94 | int ec_did_emsg_before; |
| 95 | int ec_trylevel_at_start; |
| 96 | where_T ec_where; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 97 | }; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 98 | |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 99 | #ifdef FEAT_PROFILE |
| 100 | // stack of profinfo_T used when profiling. |
| 101 | static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL}; |
| 102 | #endif |
| 103 | |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 104 | // Get pointer to item in the stack. |
| 105 | #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx) |
| 106 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 107 | // Get pointer to item relative to the bottom of the stack, -1 is the last one. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 108 | #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 109 | |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 110 | // Get pointer to a local variable on the stack. Negative for arguments. |
| 111 | #define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx) |
| 112 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 113 | void |
| 114 | to_string_error(vartype_T vartype) |
| 115 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 116 | semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype)); |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 119 | /* |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 120 | * Return the number of arguments, including optional arguments and any vararg. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 121 | */ |
| 122 | static int |
| 123 | ufunc_argcount(ufunc_T *ufunc) |
| 124 | { |
| 125 | return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 126 | } |
| 127 | |
| 128 | /* |
LemonBoy | 372bcce | 2022-04-25 12:43:20 +0100 | [diff] [blame] | 129 | * Create a new string from "count" items at the bottom of the stack. |
| 130 | * A trailing NUL is appended. |
| 131 | * When "count" is zero an empty string is added to the stack. |
| 132 | */ |
| 133 | static int |
| 134 | exe_concat(int count, ectx_T *ectx) |
| 135 | { |
| 136 | int idx; |
| 137 | int len = 0; |
| 138 | typval_T *tv; |
| 139 | garray_T ga; |
| 140 | |
| 141 | ga_init2(&ga, sizeof(char), 1); |
| 142 | // Preallocate enough space for the whole string to avoid having to grow |
| 143 | // and copy. |
| 144 | for (idx = 0; idx < count; ++idx) |
| 145 | { |
| 146 | tv = STACK_TV_BOT(idx - count); |
| 147 | if (tv->vval.v_string != NULL) |
| 148 | len += (int)STRLEN(tv->vval.v_string); |
| 149 | } |
| 150 | |
| 151 | if (ga_grow(&ga, len + 1) == FAIL) |
| 152 | return FAIL; |
| 153 | |
| 154 | for (idx = 0; idx < count; ++idx) |
| 155 | { |
| 156 | tv = STACK_TV_BOT(idx - count); |
| 157 | ga_concat(&ga, tv->vval.v_string); |
| 158 | clear_tv(tv); |
| 159 | } |
| 160 | |
| 161 | // add a terminating NUL |
| 162 | ga_append(&ga, NUL); |
| 163 | |
| 164 | ectx->ec_stack.ga_len -= count - 1; |
| 165 | STACK_TV_BOT(-1)->vval.v_string = ga.ga_data; |
| 166 | |
| 167 | return OK; |
| 168 | } |
| 169 | |
| 170 | /* |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 171 | * Create a new list from "count" items at the bottom of the stack. |
| 172 | * When "count" is zero an empty list is added to the stack. |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 173 | * When "count" is -1 a NULL list is added to the stack. |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 174 | */ |
| 175 | static int |
| 176 | exe_newlist(int count, ectx_T *ectx) |
| 177 | { |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 178 | list_T *list = NULL; |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 179 | int idx; |
| 180 | typval_T *tv; |
| 181 | |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 182 | if (count >= 0) |
| 183 | { |
| 184 | list = list_alloc_with_items(count); |
| 185 | if (list == NULL) |
| 186 | return FAIL; |
| 187 | for (idx = 0; idx < count; ++idx) |
| 188 | list_set_item(list, idx, STACK_TV_BOT(idx - count)); |
| 189 | } |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 190 | |
| 191 | if (count > 0) |
| 192 | ectx->ec_stack.ga_len -= count - 1; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 193 | else if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 194 | { |
| 195 | list_unref(list); |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 196 | return FAIL; |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 197 | } |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 198 | else |
| 199 | ++ectx->ec_stack.ga_len; |
| 200 | tv = STACK_TV_BOT(-1); |
| 201 | tv->v_type = VAR_LIST; |
| 202 | tv->vval.v_list = list; |
Bram Moolenaar | 566badc | 2022-09-18 13:46:08 +0100 | [diff] [blame] | 203 | tv->v_lock = 0; |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 204 | if (list != NULL) |
| 205 | ++list->lv_refcount; |
| 206 | return OK; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Implementation of ISN_NEWDICT. |
| 211 | * Returns FAIL on total failure, MAYBE on error. |
| 212 | */ |
| 213 | static int |
| 214 | exe_newdict(int count, ectx_T *ectx) |
| 215 | { |
| 216 | dict_T *dict = NULL; |
| 217 | dictitem_T *item; |
| 218 | char_u *key; |
| 219 | int idx; |
| 220 | typval_T *tv; |
| 221 | |
| 222 | if (count >= 0) |
| 223 | { |
| 224 | dict = dict_alloc(); |
| 225 | if (unlikely(dict == NULL)) |
| 226 | return FAIL; |
| 227 | for (idx = 0; idx < count; ++idx) |
| 228 | { |
| 229 | // have already checked key type is VAR_STRING |
| 230 | tv = STACK_TV_BOT(2 * (idx - count)); |
| 231 | // check key is unique |
| 232 | key = tv->vval.v_string == NULL |
| 233 | ? (char_u *)"" : tv->vval.v_string; |
| 234 | item = dict_find(dict, key, -1); |
| 235 | if (item != NULL) |
| 236 | { |
| 237 | semsg(_(e_duplicate_key_in_dicitonary), key); |
| 238 | dict_unref(dict); |
| 239 | return MAYBE; |
| 240 | } |
| 241 | item = dictitem_alloc(key); |
| 242 | clear_tv(tv); |
| 243 | if (unlikely(item == NULL)) |
| 244 | { |
| 245 | dict_unref(dict); |
| 246 | return FAIL; |
| 247 | } |
Bram Moolenaar | 0d1f55c | 2022-04-05 17:30:29 +0100 | [diff] [blame] | 248 | tv = STACK_TV_BOT(2 * (idx - count) + 1); |
| 249 | item->di_tv = *tv; |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 250 | item->di_tv.v_lock = 0; |
Bram Moolenaar | 0d1f55c | 2022-04-05 17:30:29 +0100 | [diff] [blame] | 251 | tv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 252 | if (dict_add(dict, item) == FAIL) |
| 253 | { |
| 254 | // can this ever happen? |
| 255 | dict_unref(dict); |
| 256 | return FAIL; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (count > 0) |
| 262 | ectx->ec_stack.ga_len -= 2 * count - 1; |
| 263 | else if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
| 264 | return FAIL; |
| 265 | else |
| 266 | ++ectx->ec_stack.ga_len; |
| 267 | tv = STACK_TV_BOT(-1); |
| 268 | tv->v_type = VAR_DICT; |
| 269 | tv->v_lock = 0; |
| 270 | tv->vval.v_dict = dict; |
| 271 | if (dict != NULL) |
| 272 | ++dict->dv_refcount; |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 273 | return OK; |
| 274 | } |
| 275 | |
| 276 | /* |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 277 | * If debug_tick changed check if "ufunc" has a breakpoint and update |
| 278 | * "uf_has_breakpoint". |
| 279 | */ |
Bram Moolenaar | 96923b7 | 2022-03-15 15:57:04 +0000 | [diff] [blame] | 280 | void |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 281 | update_has_breakpoint(ufunc_T *ufunc) |
| 282 | { |
| 283 | if (ufunc->uf_debug_tick != debug_tick) |
| 284 | { |
| 285 | linenr_T breakpoint; |
| 286 | |
| 287 | ufunc->uf_debug_tick = debug_tick; |
| 288 | breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0); |
| 289 | ufunc->uf_has_breakpoint = breakpoint > 0; |
| 290 | } |
| 291 | } |
| 292 | |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 293 | static garray_T dict_stack = GA_EMPTY; |
| 294 | |
| 295 | /* |
| 296 | * Put a value on the dict stack. This consumes "tv". |
| 297 | */ |
| 298 | static int |
| 299 | dict_stack_save(typval_T *tv) |
| 300 | { |
| 301 | if (dict_stack.ga_growsize == 0) |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 302 | ga_init2(&dict_stack, sizeof(typval_T), 10); |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 303 | if (ga_grow(&dict_stack, 1) == FAIL) |
| 304 | return FAIL; |
| 305 | ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv; |
| 306 | ++dict_stack.ga_len; |
| 307 | return OK; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Get the typval at top of the dict stack. |
| 312 | */ |
| 313 | static typval_T * |
| 314 | dict_stack_get_tv(void) |
| 315 | { |
| 316 | if (dict_stack.ga_len == 0) |
| 317 | return NULL; |
| 318 | return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Get the dict at top of the dict stack. |
| 323 | */ |
| 324 | static dict_T * |
| 325 | dict_stack_get_dict(void) |
| 326 | { |
| 327 | typval_T *tv; |
| 328 | |
| 329 | if (dict_stack.ga_len == 0) |
| 330 | return NULL; |
| 331 | tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1; |
| 332 | if (tv->v_type == VAR_DICT) |
| 333 | return tv->vval.v_dict; |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Drop an item from the dict stack. |
| 339 | */ |
| 340 | static void |
| 341 | dict_stack_drop(void) |
| 342 | { |
| 343 | if (dict_stack.ga_len == 0) |
| 344 | { |
| 345 | iemsg("Dict stack underflow"); |
| 346 | return; |
| 347 | } |
| 348 | --dict_stack.ga_len; |
| 349 | clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Drop items from the dict stack until the length is equal to "len". |
| 354 | */ |
| 355 | static void |
| 356 | dict_stack_clear(int len) |
| 357 | { |
| 358 | while (dict_stack.ga_len > len) |
| 359 | dict_stack_drop(); |
| 360 | } |
| 361 | |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 362 | /* |
Bram Moolenaar | 7aca5ca | 2022-02-07 19:56:43 +0000 | [diff] [blame] | 363 | * Get a pointer to useful "pt_outer" of "pt". |
| 364 | */ |
| 365 | static outer_T * |
| 366 | get_pt_outer(partial_T *pt) |
| 367 | { |
| 368 | partial_T *ptref = pt->pt_outer_partial; |
| 369 | |
| 370 | if (ptref == NULL) |
| 371 | return &pt->pt_outer; |
| 372 | |
| 373 | // partial using partial (recursively) |
| 374 | while (ptref->pt_outer_partial != NULL) |
| 375 | ptref = ptref->pt_outer_partial; |
| 376 | return &ptref->pt_outer; |
| 377 | } |
| 378 | |
| 379 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 380 | * Call compiled function "cdf_idx" from compiled code. |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 381 | * This adds a stack frame and sets the instruction pointer to the start of the |
| 382 | * called function. |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 383 | * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 384 | * |
| 385 | * Stack has: |
| 386 | * - current arguments (already there) |
| 387 | * - omitted optional argument (default values) added here |
| 388 | * - stack frame: |
| 389 | * - pointer to calling function |
| 390 | * - Index of next instruction in calling function |
| 391 | * - previous frame pointer |
| 392 | * - reserved space for local variables |
| 393 | */ |
| 394 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 395 | call_dfunc( |
| 396 | int cdf_idx, |
| 397 | partial_T *pt, |
| 398 | int argcount_arg, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 399 | ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 400 | { |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 401 | int argcount = argcount_arg; |
| 402 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx; |
| 403 | ufunc_T *ufunc = dfunc->df_ufunc; |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 404 | int did_emsg_before = did_emsg_cumul + did_emsg; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 405 | int arg_to_add; |
| 406 | int vararg_count = 0; |
| 407 | int varcount; |
| 408 | int idx; |
| 409 | estack_T *entry; |
| 410 | funclocal_T *floc = NULL; |
Bram Moolenaar | 648594e | 2021-07-11 17:55:01 +0200 | [diff] [blame] | 411 | int res = OK; |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 412 | compiletype_T compile_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 413 | |
| 414 | if (dfunc->df_deleted) |
| 415 | { |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 416 | // don't use ufunc->uf_name, it may have been freed |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 417 | emsg_funcname(e_function_was_deleted_str, |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 418 | dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 419 | return FAIL; |
| 420 | } |
| 421 | |
Bram Moolenaar | e5ea346 | 2021-01-25 21:01:48 +0100 | [diff] [blame] | 422 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 423 | if (do_profiling == PROF_YES) |
| 424 | { |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 425 | if (GA_GROW_OK(&profile_info_ga, 1)) |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 426 | { |
| 427 | profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data) |
| 428 | + profile_info_ga.ga_len; |
| 429 | ++profile_info_ga.ga_len; |
| 430 | CLEAR_POINTER(info); |
| 431 | profile_may_start_func(info, ufunc, |
| 432 | (((dfunc_T *)def_functions.ga_data) |
| 433 | + ectx->ec_dfunc_idx)->df_ufunc); |
| 434 | } |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 435 | } |
Bram Moolenaar | e5ea346 | 2021-01-25 21:01:48 +0100 | [diff] [blame] | 436 | #endif |
| 437 | |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 438 | // When debugging and using "cont" switches to the not-debugged |
| 439 | // instructions, may need to still compile them. |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 440 | compile_type = get_compile_type(ufunc); |
| 441 | if (func_needs_compiling(ufunc, compile_type)) |
Bram Moolenaar | 648594e | 2021-07-11 17:55:01 +0200 | [diff] [blame] | 442 | { |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 443 | res = compile_def_function(ufunc, FALSE, compile_type, NULL); |
Bram Moolenaar | 648594e | 2021-07-11 17:55:01 +0200 | [diff] [blame] | 444 | |
| 445 | // compile_def_function() may cause def_functions.ga_data to change |
| 446 | dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx; |
| 447 | } |
| 448 | if (res == FAIL || INSTRUCTIONS(dfunc) == NULL) |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 449 | { |
| 450 | if (did_emsg_cumul + did_emsg == did_emsg_before) |
| 451 | semsg(_(e_function_is_not_compiled_str), |
| 452 | printable_func_name(ufunc)); |
| 453 | return FAIL; |
| 454 | } |
| 455 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 456 | if (ufunc->uf_va_name != NULL) |
| 457 | { |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 458 | // Need to make a list out of the vararg arguments. |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 459 | // Stack at time of call with 2 varargs: |
| 460 | // normal_arg |
| 461 | // optional_arg |
| 462 | // vararg_1 |
| 463 | // vararg_2 |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 464 | // After creating the list: |
| 465 | // normal_arg |
| 466 | // optional_arg |
| 467 | // vararg-list |
| 468 | // With missing optional arguments we get: |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 469 | // normal_arg |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 470 | // After creating the list |
| 471 | // normal_arg |
| 472 | // (space for optional_arg) |
| 473 | // vararg-list |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 474 | vararg_count = argcount - ufunc->uf_args.ga_len; |
| 475 | if (vararg_count < 0) |
| 476 | vararg_count = 0; |
| 477 | else |
| 478 | argcount -= vararg_count; |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 479 | if (exe_newlist(vararg_count, ectx) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 480 | return FAIL; |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 481 | |
| 482 | vararg_count = 1; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 485 | arg_to_add = ufunc->uf_args.ga_len - argcount; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 486 | if (arg_to_add < 0) |
| 487 | { |
Matvey Tarasov | d14bb1a | 2022-06-29 13:18:27 +0100 | [diff] [blame] | 488 | semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many, |
| 489 | -arg_to_add), -arg_to_add); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 490 | return FAIL; |
| 491 | } |
Bram Moolenaar | cd1cda2 | 2022-02-16 21:48:25 +0000 | [diff] [blame] | 492 | else if (arg_to_add > ufunc->uf_def_args.ga_len) |
| 493 | { |
| 494 | int missing = arg_to_add - ufunc->uf_def_args.ga_len; |
| 495 | |
Matvey Tarasov | d14bb1a | 2022-06-29 13:18:27 +0100 | [diff] [blame] | 496 | semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few, |
| 497 | missing), missing); |
Bram Moolenaar | cd1cda2 | 2022-02-16 21:48:25 +0000 | [diff] [blame] | 498 | return FAIL; |
| 499 | } |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 500 | |
| 501 | // Reserve space for: |
| 502 | // - missing arguments |
| 503 | // - stack frame |
| 504 | // - local variables |
| 505 | // - if needed: a counter for number of closures created in |
| 506 | // ectx->ec_funcrefs. |
| 507 | varcount = dfunc->df_varcount + dfunc->df_has_closure; |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 508 | if (GA_GROW_FAILS(&ectx->ec_stack, |
| 509 | arg_to_add + STACK_FRAME_SIZE + varcount)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 510 | return FAIL; |
| 511 | |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 512 | // If depth of calling is getting too high, don't execute the function. |
| 513 | if (funcdepth_increment() == FAIL) |
| 514 | return FAIL; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 515 | ++ex_nesting_level; |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 516 | |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 517 | // Only make a copy of funclocal if it contains something to restore. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 518 | if (ectx->ec_funclocal.floc_restore_cmdmod) |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 519 | { |
| 520 | floc = ALLOC_ONE(funclocal_T); |
| 521 | if (floc == NULL) |
| 522 | return FAIL; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 523 | *floc = ectx->ec_funclocal; |
| 524 | ectx->ec_funclocal.floc_restore_cmdmod = FALSE; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 527 | // Move the vararg-list to below the missing optional arguments. |
| 528 | if (vararg_count > 0 && arg_to_add > 0) |
| 529 | *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 530 | |
| 531 | // Reserve space for omitted optional arguments, filled in soon. |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 532 | for (idx = 0; idx < arg_to_add; ++idx) |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 533 | STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 534 | ectx->ec_stack.ga_len += arg_to_add; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 535 | |
| 536 | // Store current execution state in stack frame for ISN_RETURN. |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 537 | STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx; |
| 538 | STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx; |
Bram Moolenaar | 5930ddc | 2021-04-26 20:32:59 +0200 | [diff] [blame] | 539 | STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr; |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 540 | STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = |
| 541 | (void *)ectx->ec_outer_ref; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 542 | STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 543 | STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 544 | ectx->ec_frame_idx = ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 545 | |
| 546 | // Initialize local variables |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 547 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
Bram Moolenaar | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 548 | { |
| 549 | typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx); |
| 550 | |
| 551 | tv->v_type = VAR_NUMBER; |
| 552 | tv->vval.v_number = 0; |
| 553 | } |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 554 | if (dfunc->df_has_closure) |
| 555 | { |
| 556 | typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount); |
| 557 | |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 558 | // Initialize the variable that counts how many closures were created. |
| 559 | // This is used in handle_closure_in_use(). |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 560 | tv->v_type = VAR_NUMBER; |
| 561 | tv->vval.v_number = 0; |
| 562 | } |
| 563 | ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 564 | |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 565 | if (pt != NULL || ufunc->uf_partial != NULL |
| 566 | || (ufunc->uf_flags & FC_CLOSURE)) |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 567 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 568 | outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T); |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 569 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 570 | if (ref == NULL) |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 571 | return FAIL; |
| 572 | if (pt != NULL) |
| 573 | { |
Bram Moolenaar | 7aca5ca | 2022-02-07 19:56:43 +0000 | [diff] [blame] | 574 | ref->or_outer = get_pt_outer(pt); |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 575 | ++pt->pt_refcount; |
| 576 | ref->or_partial = pt; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 577 | } |
| 578 | else if (ufunc->uf_partial != NULL) |
| 579 | { |
Bram Moolenaar | 7aca5ca | 2022-02-07 19:56:43 +0000 | [diff] [blame] | 580 | ref->or_outer = get_pt_outer(ufunc->uf_partial); |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 581 | ++ufunc->uf_partial->pt_refcount; |
| 582 | ref->or_partial = ufunc->uf_partial; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 583 | } |
| 584 | else |
| 585 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 586 | ref->or_outer = ALLOC_CLEAR_ONE(outer_T); |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 587 | if (unlikely(ref->or_outer == NULL)) |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 588 | { |
| 589 | vim_free(ref); |
| 590 | return FAIL; |
| 591 | } |
| 592 | ref->or_outer_allocated = TRUE; |
| 593 | ref->or_outer->out_stack = &ectx->ec_stack; |
| 594 | ref->or_outer->out_frame_idx = ectx->ec_frame_idx; |
| 595 | if (ectx->ec_outer_ref != NULL) |
| 596 | ref->or_outer->out_up = ectx->ec_outer_ref->or_outer; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 597 | } |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 598 | ectx->ec_outer_ref = ref; |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 599 | } |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 600 | else |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 601 | ectx->ec_outer_ref = NULL; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 602 | |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 603 | ++ufunc->uf_calls; |
| 604 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 605 | // Set execution state to the start of the called function. |
| 606 | ectx->ec_dfunc_idx = cdf_idx; |
Bram Moolenaar | e5ea346 | 2021-01-25 21:01:48 +0100 | [diff] [blame] | 607 | ectx->ec_instr = INSTRUCTIONS(dfunc); |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 608 | entry = estack_push_ufunc(ufunc, 1); |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 609 | if (entry != NULL) |
| 610 | { |
| 611 | // Set the script context to the script where the function was defined. |
Bram Moolenaar | c70fe46 | 2021-04-17 17:59:19 +0200 | [diff] [blame] | 612 | // Save the current context so it can be restored on return. |
| 613 | entry->es_save_sctx = current_sctx; |
| 614 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 615 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 616 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 617 | // Start execution at the first instruction. |
| 618 | ectx->ec_iidx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 619 | |
| 620 | return OK; |
| 621 | } |
| 622 | |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 623 | // Double linked list of funcstack_T in use. |
| 624 | static funcstack_T *first_funcstack = NULL; |
| 625 | |
| 626 | static void |
| 627 | add_funcstack_to_list(funcstack_T *funcstack) |
| 628 | { |
| 629 | // Link in list of funcstacks. |
| 630 | if (first_funcstack != NULL) |
| 631 | first_funcstack->fs_prev = funcstack; |
| 632 | funcstack->fs_next = first_funcstack; |
| 633 | funcstack->fs_prev = NULL; |
| 634 | first_funcstack = funcstack; |
| 635 | } |
| 636 | |
| 637 | static void |
| 638 | remove_funcstack_from_list(funcstack_T *funcstack) |
| 639 | { |
| 640 | if (funcstack->fs_prev == NULL) |
| 641 | first_funcstack = funcstack->fs_next; |
| 642 | else |
| 643 | funcstack->fs_prev->fs_next = funcstack->fs_next; |
| 644 | if (funcstack->fs_next != NULL) |
| 645 | funcstack->fs_next->fs_prev = funcstack->fs_prev; |
| 646 | } |
| 647 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 648 | /* |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 649 | * Used when returning from a function: Check if any closure is still |
| 650 | * referenced. If so then move the arguments and variables to a separate piece |
| 651 | * of stack to be used when the closure is called. |
| 652 | * When "free_arguments" is TRUE the arguments are to be freed. |
| 653 | * Returns FAIL when out of memory. |
| 654 | */ |
| 655 | static int |
| 656 | handle_closure_in_use(ectx_T *ectx, int free_arguments) |
| 657 | { |
| 658 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 659 | + ectx->ec_dfunc_idx; |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 660 | int argcount; |
| 661 | int top; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 662 | int idx; |
| 663 | typval_T *tv; |
| 664 | int closure_in_use = FALSE; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 665 | garray_T *gap = &ectx->ec_funcrefs; |
| 666 | varnumber_T closure_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 667 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 668 | if (dfunc->df_ufunc == NULL) |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 669 | return OK; // function was freed |
| 670 | if (dfunc->df_has_closure == 0) |
| 671 | return OK; // no closures |
| 672 | tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount); |
| 673 | closure_count = tv->vval.v_number; |
| 674 | if (closure_count == 0) |
| 675 | return OK; // no funcrefs created |
| 676 | |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 677 | // Compute "top": the first entry in the stack used by the function. |
| 678 | // This is the first argument (after that comes the stack frame and then |
| 679 | // the local variables). |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 680 | argcount = ufunc_argcount(dfunc->df_ufunc); |
| 681 | top = ectx->ec_frame_idx - argcount; |
| 682 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 683 | // Check if any created closure is still in use. |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 684 | for (idx = 0; idx < closure_count; ++idx) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 685 | { |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 686 | partial_T *pt; |
| 687 | int off = gap->ga_len - closure_count + idx; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 688 | |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 689 | if (off < 0) |
| 690 | continue; // count is off or already done |
| 691 | pt = ((partial_T **)gap->ga_data)[off]; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 692 | if (pt->pt_refcount > 1) |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 693 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 694 | int refcount = pt->pt_refcount; |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 695 | int i; |
| 696 | |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 697 | // A Reference in a local variable doesn't count, it gets |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 698 | // unreferenced on return. |
| 699 | for (i = 0; i < dfunc->df_varcount; ++i) |
| 700 | { |
| 701 | typval_T *stv = STACK_TV(ectx->ec_frame_idx |
| 702 | + STACK_FRAME_SIZE + i); |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 703 | if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial) |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 704 | --refcount; |
| 705 | } |
| 706 | if (refcount > 1) |
| 707 | { |
| 708 | closure_in_use = TRUE; |
| 709 | break; |
| 710 | } |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 711 | } |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | if (closure_in_use) |
| 715 | { |
| 716 | funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T); |
| 717 | typval_T *stack; |
| 718 | |
| 719 | // A closure is using the arguments and/or local variables. |
| 720 | // Move them to the called function. |
| 721 | if (funcstack == NULL) |
| 722 | return FAIL; |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 723 | |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 724 | funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE; |
| 725 | funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 726 | stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len); |
| 727 | funcstack->fs_ga.ga_data = stack; |
| 728 | if (stack == NULL) |
| 729 | { |
| 730 | vim_free(funcstack); |
| 731 | return FAIL; |
| 732 | } |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 733 | add_funcstack_to_list(funcstack); |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 734 | |
| 735 | // Move or copy the arguments. |
| 736 | for (idx = 0; idx < argcount; ++idx) |
| 737 | { |
| 738 | tv = STACK_TV(top + idx); |
| 739 | if (free_arguments) |
| 740 | { |
| 741 | *(stack + idx) = *tv; |
| 742 | tv->v_type = VAR_UNKNOWN; |
| 743 | } |
| 744 | else |
| 745 | copy_tv(tv, stack + idx); |
| 746 | } |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 747 | // Skip the stack frame. |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 748 | // Move the local variables. |
| 749 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
| 750 | { |
| 751 | tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx); |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 752 | |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 753 | // A partial created for a local function, that is also used as a |
| 754 | // local variable, has a reference count for the variable, thus |
| 755 | // will never go down to zero. When all these refcounts are one |
| 756 | // then the funcstack is unused. We need to count how many we have |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 757 | // so we know when to check. |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 758 | if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL) |
| 759 | { |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 760 | int i; |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 761 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 762 | for (i = 0; i < closure_count; ++i) |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 763 | if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[ |
| 764 | gap->ga_len - closure_count + i]) |
| 765 | ++funcstack->fs_min_refcount; |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 768 | *(stack + funcstack->fs_var_offset + idx) = *tv; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 769 | tv->v_type = VAR_UNKNOWN; |
| 770 | } |
| 771 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 772 | for (idx = 0; idx < closure_count; ++idx) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 773 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 774 | partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len |
| 775 | - closure_count + idx]; |
| 776 | if (pt->pt_refcount > 1) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 777 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 778 | ++funcstack->fs_refcount; |
| 779 | pt->pt_funcstack = funcstack; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 780 | pt->pt_outer.out_stack = &funcstack->fs_ga; |
| 781 | pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 786 | for (idx = 0; idx < closure_count; ++idx) |
| 787 | partial_unref(((partial_T **)gap->ga_data)[gap->ga_len |
| 788 | - closure_count + idx]); |
| 789 | gap->ga_len -= closure_count; |
| 790 | if (gap->ga_len == 0) |
| 791 | ga_clear(gap); |
| 792 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 793 | return OK; |
| 794 | } |
| 795 | |
| 796 | /* |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 797 | * Called when a partial is freed or its reference count goes down to one. The |
| 798 | * funcstack may be the only reference to the partials in the local variables. |
| 799 | * Go over all of them, the funcref and can be freed if all partials |
| 800 | * referencing the funcstack have a reference count of one. |
Bram Moolenaar | acd6b99 | 2022-09-17 16:27:39 +0100 | [diff] [blame] | 801 | * Returns TRUE if the funcstack is freed, the partial referencing it will then |
| 802 | * also have been freed. |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 803 | */ |
Bram Moolenaar | acd6b99 | 2022-09-17 16:27:39 +0100 | [diff] [blame] | 804 | int |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 805 | funcstack_check_refcount(funcstack_T *funcstack) |
| 806 | { |
Bram Moolenaar | acd6b99 | 2022-09-17 16:27:39 +0100 | [diff] [blame] | 807 | int i; |
| 808 | garray_T *gap = &funcstack->fs_ga; |
| 809 | int done = 0; |
| 810 | typval_T *stack; |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 811 | |
| 812 | if (funcstack->fs_refcount > funcstack->fs_min_refcount) |
Bram Moolenaar | acd6b99 | 2022-09-17 16:27:39 +0100 | [diff] [blame] | 813 | return FALSE; |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 814 | for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i) |
| 815 | { |
| 816 | typval_T *tv = ((typval_T *)gap->ga_data) + i; |
| 817 | |
| 818 | if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL |
| 819 | && tv->vval.v_partial->pt_funcstack == funcstack |
| 820 | && tv->vval.v_partial->pt_refcount == 1) |
| 821 | ++done; |
| 822 | } |
Bram Moolenaar | acd6b99 | 2022-09-17 16:27:39 +0100 | [diff] [blame] | 823 | if (done != funcstack->fs_min_refcount) |
| 824 | return FALSE; |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 825 | |
Bram Moolenaar | acd6b99 | 2022-09-17 16:27:39 +0100 | [diff] [blame] | 826 | stack = gap->ga_data; |
| 827 | |
| 828 | // All partials referencing the funcstack have a reference count of |
| 829 | // one, thus the funcstack is no longer of use. |
| 830 | for (i = 0; i < gap->ga_len; ++i) |
| 831 | clear_tv(stack + i); |
| 832 | vim_free(stack); |
| 833 | remove_funcstack_from_list(funcstack); |
| 834 | vim_free(funcstack); |
| 835 | |
| 836 | return TRUE; |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /* |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 840 | * For garbage collecting: set references in all variables referenced by |
| 841 | * all funcstacks. |
| 842 | */ |
| 843 | int |
| 844 | set_ref_in_funcstacks(int copyID) |
| 845 | { |
| 846 | funcstack_T *funcstack; |
| 847 | |
| 848 | for (funcstack = first_funcstack; funcstack != NULL; |
| 849 | funcstack = funcstack->fs_next) |
| 850 | { |
| 851 | typval_T *stack = funcstack->fs_ga.ga_data; |
| 852 | int i; |
| 853 | |
| 854 | for (i = 0; i < funcstack->fs_ga.ga_len; ++i) |
| 855 | if (set_ref_in_item(stack + i, copyID, NULL, NULL)) |
| 856 | return TRUE; // abort |
| 857 | } |
| 858 | return FALSE; |
| 859 | } |
| 860 | |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 861 | // Ugly static to avoid passing the execution context around through many |
| 862 | // layers. |
| 863 | static ectx_T *current_ectx = NULL; |
| 864 | |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 865 | /* |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 866 | * Return TRUE if currently executing a :def function. |
| 867 | * Can be used by builtin functions only. |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 868 | */ |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 869 | int |
| 870 | in_def_function(void) |
| 871 | { |
| 872 | return current_ectx != NULL; |
| 873 | } |
| 874 | |
| 875 | /* |
Bram Moolenaar | 98aff65 | 2022-09-06 21:02:35 +0100 | [diff] [blame] | 876 | * Clear "current_ectx" and return the previous value. To be used when calling |
| 877 | * a user function. |
| 878 | */ |
| 879 | ectx_T * |
| 880 | clear_currrent_ectx(void) |
| 881 | { |
| 882 | ectx_T *r = current_ectx; |
| 883 | |
| 884 | current_ectx = NULL; |
| 885 | return r; |
| 886 | } |
| 887 | |
| 888 | void |
| 889 | restore_current_ectx(ectx_T *ectx) |
| 890 | { |
| 891 | if (current_ectx != NULL) |
| 892 | iemsg("Restoring current_ectx while it is not NULL"); |
| 893 | current_ectx = ectx; |
| 894 | } |
| 895 | |
| 896 | /* |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 897 | * Add an entry for a deferred function call to the currently executing |
| 898 | * function. |
| 899 | * Return the list or NULL when failed. |
| 900 | */ |
| 901 | static list_T * |
| 902 | add_defer_item(int var_idx, int argcount, ectx_T *ectx) |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 903 | { |
| 904 | typval_T *defer_tv = STACK_TV_VAR(var_idx); |
| 905 | list_T *defer_l; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 906 | list_T *l; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 907 | typval_T listval; |
| 908 | |
| 909 | if (defer_tv->v_type != VAR_LIST) |
| 910 | { |
Bram Moolenaar | a5348f2 | 2022-09-04 11:42:22 +0100 | [diff] [blame] | 911 | // first time, allocate the list |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 912 | if (rettv_list_alloc(defer_tv) == FAIL) |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 913 | return NULL; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 914 | } |
| 915 | defer_l = defer_tv->vval.v_list; |
| 916 | |
| 917 | l = list_alloc_with_items(argcount + 1); |
| 918 | if (l == NULL) |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 919 | return NULL; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 920 | listval.v_type = VAR_LIST; |
| 921 | listval.vval.v_list = l; |
| 922 | listval.v_lock = 0; |
Bram Moolenaar | a5348f2 | 2022-09-04 11:42:22 +0100 | [diff] [blame] | 923 | if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL) |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 924 | { |
| 925 | vim_free(l); |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 926 | return NULL; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 927 | } |
| 928 | |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 929 | return l; |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments. |
| 934 | * The local variable that lists deferred functions is "var_idx". |
| 935 | * Returns OK or FAIL. |
| 936 | */ |
| 937 | static int |
| 938 | defer_command(int var_idx, int argcount, ectx_T *ectx) |
| 939 | { |
| 940 | list_T *l = add_defer_item(var_idx, argcount, ectx); |
| 941 | int i; |
| 942 | typval_T *func_tv; |
| 943 | |
| 944 | if (l == NULL) |
| 945 | return FAIL; |
| 946 | |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 947 | func_tv = STACK_TV_BOT(-argcount - 1); |
Bram Moolenaar | f5fec05 | 2022-09-11 11:49:22 +0100 | [diff] [blame] | 948 | if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL) |
| 949 | { |
| 950 | semsg(_(e_expected_str_but_got_str), |
| 951 | "function or partial", |
| 952 | vartype_name(func_tv->v_type)); |
| 953 | return FAIL; |
| 954 | } |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 955 | list_set_item(l, 0, func_tv); |
| 956 | |
Bram Moolenaar | f5fec05 | 2022-09-11 11:49:22 +0100 | [diff] [blame] | 957 | for (i = 0; i < argcount; ++i) |
| 958 | list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i)); |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 959 | ectx->ec_stack.ga_len -= argcount + 1; |
| 960 | return OK; |
| 961 | } |
| 962 | |
| 963 | /* |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 964 | * Add a deferred function "name" with one argument "arg_tv". |
| 965 | * Consumes "name", also on failure. |
| 966 | * Only to be called when in_def_function() returns TRUE. |
| 967 | */ |
| 968 | int |
| 969 | add_defer_function(char_u *name, int argcount, typval_T *argvars) |
| 970 | { |
| 971 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 972 | + current_ectx->ec_dfunc_idx; |
| 973 | list_T *l; |
| 974 | typval_T func_tv; |
| 975 | int i; |
| 976 | |
| 977 | if (dfunc->df_defer_var_idx == 0) |
| 978 | { |
| 979 | iemsg("df_defer_var_idx is zero"); |
Bram Moolenaar | 31ea6bf | 2022-09-05 10:47:13 +0100 | [diff] [blame] | 980 | vim_free(name); |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 981 | return FAIL; |
| 982 | } |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 983 | |
Bram Moolenaar | f5fec05 | 2022-09-11 11:49:22 +0100 | [diff] [blame] | 984 | l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx); |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 985 | if (l == NULL) |
| 986 | { |
Bram Moolenaar | 31ea6bf | 2022-09-05 10:47:13 +0100 | [diff] [blame] | 987 | vim_free(name); |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 988 | return FAIL; |
| 989 | } |
| 990 | |
Bram Moolenaar | 31ea6bf | 2022-09-05 10:47:13 +0100 | [diff] [blame] | 991 | func_tv.v_type = VAR_FUNC; |
| 992 | func_tv.v_lock = 0; |
| 993 | func_tv.vval.v_string = name; |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 994 | list_set_item(l, 0, &func_tv); |
Bram Moolenaar | 31ea6bf | 2022-09-05 10:47:13 +0100 | [diff] [blame] | 995 | |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 996 | for (i = 0; i < argcount; ++i) |
| 997 | list_set_item(l, i + 1, argvars + i); |
| 998 | return OK; |
| 999 | } |
| 1000 | |
| 1001 | /* |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1002 | * Invoked when returning from a function: Invoke any deferred calls. |
| 1003 | */ |
| 1004 | static void |
| 1005 | invoke_defer_funcs(ectx_T *ectx) |
| 1006 | { |
| 1007 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 1008 | + ectx->ec_dfunc_idx; |
| 1009 | typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1); |
| 1010 | listitem_T *li; |
| 1011 | |
| 1012 | if (defer_tv->v_type != VAR_LIST) |
| 1013 | return; // no function added |
| 1014 | for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next) |
| 1015 | { |
| 1016 | list_T *l = li->li_tv.vval.v_list; |
| 1017 | typval_T rettv; |
| 1018 | typval_T argvars[MAX_FUNC_ARGS]; |
| 1019 | int i; |
| 1020 | listitem_T *arg_li = l->lv_first; |
| 1021 | funcexe_T funcexe; |
| 1022 | |
| 1023 | for (i = 0; i < l->lv_len - 1; ++i) |
| 1024 | { |
| 1025 | arg_li = arg_li->li_next; |
| 1026 | argvars[i] = arg_li->li_tv; |
| 1027 | } |
| 1028 | |
| 1029 | CLEAR_FIELD(funcexe); |
| 1030 | funcexe.fe_evaluate = TRUE; |
| 1031 | rettv.v_type = VAR_UNKNOWN; |
| 1032 | (void)call_func(l->lv_first->li_tv.vval.v_string, -1, |
| 1033 | &rettv, l->lv_len - 1, argvars, &funcexe); |
| 1034 | clear_tv(&rettv); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1039 | * Return from the current function. |
| 1040 | */ |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1041 | static int |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1042 | func_return(ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1043 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1044 | int idx; |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 1045 | int ret_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1046 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 1047 | + ectx->ec_dfunc_idx; |
| 1048 | int argcount = ufunc_argcount(dfunc->df_ufunc); |
| 1049 | int top = ectx->ec_frame_idx - argcount; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 1050 | estack_T *entry; |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 1051 | int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx |
| 1052 | + STACK_FRAME_FUNC_OFF)->vval.v_number; |
Bram Moolenaar | b06b50d | 2021-04-26 21:39:25 +0200 | [diff] [blame] | 1053 | funclocal_T *floc; |
| 1054 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 1055 | dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data) |
| 1056 | + prev_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1057 | |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 1058 | if (do_profiling == PROF_YES) |
| 1059 | { |
| 1060 | ufunc_T *caller = prev_dfunc->df_ufunc; |
| 1061 | |
| 1062 | if (dfunc->df_ufunc->uf_profiling |
| 1063 | || (caller != NULL && caller->uf_profiling)) |
| 1064 | { |
| 1065 | profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data) |
| 1066 | + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller); |
| 1067 | --profile_info_ga.ga_len; |
| 1068 | } |
| 1069 | } |
| 1070 | #endif |
Bram Moolenaar | 919c12c | 2021-12-14 14:29:16 +0000 | [diff] [blame] | 1071 | |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1072 | if (dfunc->df_defer_var_idx > 0) |
| 1073 | invoke_defer_funcs(ectx); |
| 1074 | |
Bram Moolenaar | 919c12c | 2021-12-14 14:29:16 +0000 | [diff] [blame] | 1075 | // No check for uf_refcount being zero, cannot think of a way that would |
| 1076 | // happen. |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 1077 | --dfunc->df_ufunc->uf_calls; |
| 1078 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1079 | // execution context goes one level up |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 1080 | entry = estack_pop(); |
| 1081 | if (entry != NULL) |
Bram Moolenaar | c70fe46 | 2021-04-17 17:59:19 +0200 | [diff] [blame] | 1082 | current_sctx = entry->es_save_sctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1083 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1084 | if (handle_closure_in_use(ectx, TRUE) == FAIL) |
| 1085 | return FAIL; |
| 1086 | |
| 1087 | // Clear the arguments. |
| 1088 | for (idx = top; idx < ectx->ec_frame_idx; ++idx) |
| 1089 | clear_tv(STACK_TV(idx)); |
| 1090 | |
| 1091 | // Clear local variables and temp values, but not the return value. |
| 1092 | for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1093 | idx < ectx->ec_stack.ga_len - 1; ++idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1094 | clear_tv(STACK_TV(idx)); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1095 | |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 1096 | // The return value should be on top of the stack. However, when aborting |
| 1097 | // it may not be there and ec_frame_idx is the top of the stack. |
| 1098 | ret_idx = ectx->ec_stack.ga_len - 1; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 1099 | if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF) |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 1100 | ret_idx = 0; |
| 1101 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1102 | if (ectx->ec_outer_ref != NULL) |
| 1103 | { |
| 1104 | if (ectx->ec_outer_ref->or_outer_allocated) |
| 1105 | vim_free(ectx->ec_outer_ref->or_outer); |
| 1106 | partial_unref(ectx->ec_outer_ref->or_partial); |
| 1107 | vim_free(ectx->ec_outer_ref); |
| 1108 | } |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 1109 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1110 | // Restore the previous frame. |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 1111 | ectx->ec_dfunc_idx = prev_dfunc_idx; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 1112 | ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx |
| 1113 | + STACK_FRAME_IIDX_OFF)->vval.v_number; |
Bram Moolenaar | 5930ddc | 2021-04-26 20:32:59 +0200 | [diff] [blame] | 1114 | ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx |
| 1115 | + STACK_FRAME_INSTR_OFF)->vval.v_string; |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1116 | ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 1117 | + STACK_FRAME_OUTER_OFF)->vval.v_string; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1118 | floc = (void *)STACK_TV(ectx->ec_frame_idx |
| 1119 | + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string; |
Bram Moolenaar | 5366e1a | 2020-10-01 13:01:34 +0200 | [diff] [blame] | 1120 | // restoring ec_frame_idx must be last |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 1121 | ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx |
| 1122 | + STACK_FRAME_IDX_OFF)->vval.v_number; |
Bram Moolenaar | d386e92 | 2021-04-25 14:48:49 +0200 | [diff] [blame] | 1123 | |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1124 | if (floc == NULL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1125 | ectx->ec_funclocal.floc_restore_cmdmod = FALSE; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1126 | else |
| 1127 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1128 | ectx->ec_funclocal = *floc; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1129 | vim_free(floc); |
| 1130 | } |
| 1131 | |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 1132 | if (ret_idx > 0) |
| 1133 | { |
| 1134 | // Reset the stack to the position before the call, with a spot for the |
| 1135 | // return value, moved there from above the frame. |
| 1136 | ectx->ec_stack.ga_len = top + 1; |
| 1137 | *STACK_TV_BOT(-1) = *STACK_TV(ret_idx); |
| 1138 | } |
| 1139 | else |
| 1140 | // Reset the stack to the position before the call. |
| 1141 | ectx->ec_stack.ga_len = top; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1142 | |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 1143 | funcdepth_decrement(); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 1144 | --ex_nesting_level; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1145 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1148 | /* |
| 1149 | * Prepare arguments and rettv for calling a builtin or user function. |
| 1150 | */ |
| 1151 | static int |
| 1152 | call_prepare(int argcount, typval_T *argvars, ectx_T *ectx) |
| 1153 | { |
| 1154 | int idx; |
| 1155 | typval_T *tv; |
| 1156 | |
| 1157 | // Move arguments from bottom of the stack to argvars[] and add terminator. |
| 1158 | for (idx = 0; idx < argcount; ++idx) |
| 1159 | argvars[idx] = *STACK_TV_BOT(idx - argcount); |
| 1160 | argvars[argcount].v_type = VAR_UNKNOWN; |
| 1161 | |
| 1162 | // Result replaces the arguments on the stack. |
| 1163 | if (argcount > 0) |
| 1164 | ectx->ec_stack.ga_len -= argcount - 1; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1165 | else if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1166 | return FAIL; |
| 1167 | else |
| 1168 | ++ectx->ec_stack.ga_len; |
| 1169 | |
| 1170 | // Default return value is zero. |
| 1171 | tv = STACK_TV_BOT(-1); |
| 1172 | tv->v_type = VAR_NUMBER; |
| 1173 | tv->vval.v_number = 0; |
Bram Moolenaar | 24565cf | 2022-03-28 18:16:52 +0100 | [diff] [blame] | 1174 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1175 | |
| 1176 | return OK; |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * Call a builtin function by index. |
| 1181 | */ |
| 1182 | static int |
| 1183 | call_bfunc(int func_idx, int argcount, ectx_T *ectx) |
| 1184 | { |
| 1185 | typval_T argvars[MAX_FUNC_ARGS]; |
| 1186 | int idx; |
Bram Moolenaar | 171fb92 | 2020-10-28 16:54:47 +0100 | [diff] [blame] | 1187 | int did_emsg_before = did_emsg; |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 1188 | ectx_T *prev_ectx = current_ectx; |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 1189 | char *save_func_name = ectx->ec_where.wt_func_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1190 | |
| 1191 | if (call_prepare(argcount, argvars, ectx) == FAIL) |
| 1192 | return FAIL; |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 1193 | ectx->ec_where.wt_func_name = internal_func_name(func_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1194 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 1195 | // Call the builtin function. Set "current_ectx" so that when it |
| 1196 | // recursively invokes call_def_function() a closure context can be set. |
| 1197 | current_ectx = ectx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1198 | call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1)); |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 1199 | current_ectx = prev_ectx; |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 1200 | ectx->ec_where.wt_func_name = save_func_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1201 | |
| 1202 | // Clear the arguments. |
| 1203 | for (idx = 0; idx < argcount; ++idx) |
| 1204 | clear_tv(&argvars[idx]); |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1205 | |
Bram Moolenaar | 57f799e | 2020-12-12 20:42:19 +0100 | [diff] [blame] | 1206 | if (did_emsg > did_emsg_before) |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1207 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1208 | return OK; |
| 1209 | } |
| 1210 | |
| 1211 | /* |
| 1212 | * Execute a user defined function. |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 1213 | * If the function is compiled this will add a stack frame and set the |
| 1214 | * instruction pointer at the start of the function. |
| 1215 | * Otherwise the function is called here. |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1216 | * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 1217 | * "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] | 1218 | */ |
| 1219 | static int |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 1220 | call_ufunc( |
| 1221 | ufunc_T *ufunc, |
| 1222 | partial_T *pt, |
| 1223 | int argcount, |
| 1224 | ectx_T *ectx, |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1225 | isn_T *iptr, |
| 1226 | dict_T *selfdict) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1227 | { |
| 1228 | typval_T argvars[MAX_FUNC_ARGS]; |
| 1229 | funcexe_T funcexe; |
| 1230 | int error; |
| 1231 | int idx; |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 1232 | int did_emsg_before = did_emsg; |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 1233 | compiletype_T compile_type = get_compile_type(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1234 | |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 1235 | if (func_needs_compiling(ufunc, compile_type) |
| 1236 | && compile_def_function(ufunc, FALSE, compile_type, NULL) |
| 1237 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1238 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1239 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 1240 | { |
Bram Moolenaar | 52c124d | 2020-12-20 21:43:35 +0100 | [diff] [blame] | 1241 | error = check_user_func_argcount(ufunc, argcount); |
Bram Moolenaar | 5082471 | 2020-12-20 21:10:17 +0100 | [diff] [blame] | 1242 | if (error != FCERR_UNKNOWN) |
| 1243 | { |
| 1244 | if (error == FCERR_TOOMANY) |
Bram Moolenaar | a6c18d3 | 2022-03-31 20:02:56 +0100 | [diff] [blame] | 1245 | semsg(_(e_too_many_arguments_for_function_str), |
| 1246 | printable_func_name(ufunc)); |
Bram Moolenaar | 5082471 | 2020-12-20 21:10:17 +0100 | [diff] [blame] | 1247 | else |
Bram Moolenaar | e124204 | 2021-12-16 20:56:57 +0000 | [diff] [blame] | 1248 | semsg(_(e_not_enough_arguments_for_function_str), |
Bram Moolenaar | a6c18d3 | 2022-03-31 20:02:56 +0100 | [diff] [blame] | 1249 | printable_func_name(ufunc)); |
Bram Moolenaar | 5082471 | 2020-12-20 21:10:17 +0100 | [diff] [blame] | 1250 | return FAIL; |
| 1251 | } |
| 1252 | |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 1253 | // The function has been compiled, can call it quickly. For a function |
| 1254 | // that was defined later: we can call it directly next time. |
| 1255 | if (iptr != NULL) |
| 1256 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1257 | delete_instr(iptr); |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 1258 | iptr->isn_type = ISN_DCALL; |
| 1259 | iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1260 | iptr->isn_arg.dfunc.cdf_argcount = argcount; |
| 1261 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1262 | return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx); |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 1263 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1264 | |
| 1265 | if (call_prepare(argcount, argvars, ectx) == FAIL) |
| 1266 | return FAIL; |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 1267 | CLEAR_FIELD(funcexe); |
Bram Moolenaar | 851f86b | 2021-12-13 14:26:44 +0000 | [diff] [blame] | 1268 | funcexe.fe_evaluate = TRUE; |
| 1269 | funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict(); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1270 | |
| 1271 | // Call the user function. Result goes in last position on the stack. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1272 | error = call_user_func_check(ufunc, argcount, argvars, |
Bram Moolenaar | 851f86b | 2021-12-13 14:26:44 +0000 | [diff] [blame] | 1273 | STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1274 | |
| 1275 | // Clear the arguments. |
| 1276 | for (idx = 0; idx < argcount; ++idx) |
| 1277 | clear_tv(&argvars[idx]); |
| 1278 | |
| 1279 | if (error != FCERR_NONE) |
| 1280 | { |
Bram Moolenaar | a6c18d3 | 2022-03-31 20:02:56 +0100 | [diff] [blame] | 1281 | user_func_error(error, printable_func_name(ufunc), &funcexe); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1282 | return FAIL; |
| 1283 | } |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 1284 | if (did_emsg > did_emsg_before) |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1285 | // Error other than from calling the function itself. |
| 1286 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1287 | return OK; |
| 1288 | } |
| 1289 | |
| 1290 | /* |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 1291 | * If command modifiers were applied restore them. |
| 1292 | */ |
| 1293 | static void |
| 1294 | may_restore_cmdmod(funclocal_T *funclocal) |
| 1295 | { |
| 1296 | if (funclocal->floc_restore_cmdmod) |
| 1297 | { |
| 1298 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 1299 | undo_cmdmod(&cmdmod); |
| 1300 | cmdmod = funclocal->floc_save_cmdmod; |
| 1301 | funclocal->floc_restore_cmdmod = FALSE; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | /* |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 1306 | * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was |
| 1307 | * pressed. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 1308 | */ |
| 1309 | static int |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 1310 | vim9_aborting(int prev_uncaught_emsg) |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 1311 | { |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 1312 | return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1316 | * Execute a function by "name". |
| 1317 | * This can be a builtin function or a user function. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 1318 | * "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] | 1319 | * Returns FAIL if not found without an error message. |
| 1320 | */ |
| 1321 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1322 | call_by_name( |
| 1323 | char_u *name, |
| 1324 | int argcount, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1325 | ectx_T *ectx, |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1326 | isn_T *iptr, |
| 1327 | dict_T *selfdict) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1328 | { |
| 1329 | ufunc_T *ufunc; |
| 1330 | |
| 1331 | if (builtin_function(name, -1)) |
| 1332 | { |
| 1333 | int func_idx = find_internal_func(name); |
| 1334 | |
Bram Moolenaar | 1983f1a | 2022-02-28 20:55:02 +0000 | [diff] [blame] | 1335 | if (func_idx < 0) // Impossible? |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1336 | return FAIL; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1337 | if (check_internal_func(func_idx, argcount) < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1338 | return FAIL; |
| 1339 | return call_bfunc(func_idx, argcount, ectx); |
| 1340 | } |
| 1341 | |
Bram Moolenaar | d9d2fd0 | 2022-01-13 21:15:21 +0000 | [diff] [blame] | 1342 | ufunc = find_func(name, FALSE); |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 1343 | |
| 1344 | if (ufunc == NULL) |
| 1345 | { |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 1346 | int prev_uncaught_emsg = uncaught_emsg; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 1347 | |
| 1348 | if (script_autoload(name, TRUE)) |
| 1349 | // loaded a package, search for the function again |
Bram Moolenaar | d9d2fd0 | 2022-01-13 21:15:21 +0000 | [diff] [blame] | 1350 | ufunc = find_func(name, FALSE); |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 1351 | |
| 1352 | if (vim9_aborting(prev_uncaught_emsg)) |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 1353 | return FAIL; // bail out if loading the script caused an error |
| 1354 | } |
| 1355 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1356 | if (ufunc != NULL) |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1357 | { |
Bram Moolenaar | 6ce46b9 | 2021-08-07 15:35:36 +0200 | [diff] [blame] | 1358 | if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL) |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1359 | { |
| 1360 | int i; |
| 1361 | typval_T *argv = STACK_TV_BOT(0) - argcount; |
| 1362 | |
| 1363 | // The function can change at runtime, check that the argument |
| 1364 | // types are correct. |
| 1365 | for (i = 0; i < argcount; ++i) |
| 1366 | { |
Bram Moolenaar | e3ffcd9 | 2021-03-08 21:47:13 +0100 | [diff] [blame] | 1367 | type_T *type = NULL; |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1368 | |
Bram Moolenaar | 6ce46b9 | 2021-08-07 15:35:36 +0200 | [diff] [blame] | 1369 | if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL) |
Bram Moolenaar | e3ffcd9 | 2021-03-08 21:47:13 +0100 | [diff] [blame] | 1370 | type = ufunc->uf_arg_types[i]; |
| 1371 | else if (ufunc->uf_va_type != NULL) |
| 1372 | type = ufunc->uf_va_type->tt_member; |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1373 | if (type != NULL && check_typval_arg_type(type, |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 1374 | &argv[i], NULL, i + 1) == FAIL) |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1375 | return FAIL; |
| 1376 | } |
| 1377 | } |
| 1378 | |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1379 | return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict); |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1380 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1381 | |
| 1382 | return FAIL; |
| 1383 | } |
| 1384 | |
| 1385 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1386 | call_partial( |
| 1387 | typval_T *tv, |
| 1388 | int argcount_arg, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1389 | ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1390 | { |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 1391 | int argcount = argcount_arg; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1392 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1393 | int called_emsg_before = called_emsg; |
Bram Moolenaar | cb6cbf2 | 2021-01-12 17:17:01 +0100 | [diff] [blame] | 1394 | int res = FAIL; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1395 | dict_T *selfdict = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1396 | |
| 1397 | if (tv->v_type == VAR_PARTIAL) |
| 1398 | { |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 1399 | partial_T *pt = tv->vval.v_partial; |
| 1400 | int i; |
| 1401 | |
| 1402 | if (pt->pt_argc > 0) |
| 1403 | { |
| 1404 | // Make space for arguments from the partial, shift the "argcount" |
| 1405 | // arguments up. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1406 | if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc)) |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 1407 | return FAIL; |
| 1408 | for (i = 1; i <= argcount; ++i) |
| 1409 | *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i); |
| 1410 | ectx->ec_stack.ga_len += pt->pt_argc; |
| 1411 | argcount += pt->pt_argc; |
| 1412 | |
| 1413 | // copy the arguments from the partial onto the stack |
| 1414 | for (i = 0; i < pt->pt_argc; ++i) |
| 1415 | copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i)); |
| 1416 | } |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1417 | selfdict = pt->pt_dict; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1418 | |
| 1419 | if (pt->pt_func != NULL) |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1420 | return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict); |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 1421 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1422 | name = pt->pt_name; |
| 1423 | } |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1424 | else if (tv->v_type == VAR_FUNC) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1425 | name = tv->vval.v_string; |
Bram Moolenaar | 95006e3 | 2020-08-29 17:47:08 +0200 | [diff] [blame] | 1426 | if (name != NULL) |
| 1427 | { |
| 1428 | char_u fname_buf[FLEN_FIXED + 1]; |
| 1429 | char_u *tofree = NULL; |
| 1430 | int error = FCERR_NONE; |
| 1431 | char_u *fname; |
| 1432 | |
| 1433 | // May need to translate <SNR>123_ to K_SNR. |
| 1434 | fname = fname_trans_sid(name, fname_buf, &tofree, &error); |
| 1435 | if (error != FCERR_NONE) |
| 1436 | res = FAIL; |
| 1437 | else |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1438 | res = call_by_name(fname, argcount, ectx, NULL, selfdict); |
Bram Moolenaar | 95006e3 | 2020-08-29 17:47:08 +0200 | [diff] [blame] | 1439 | vim_free(tofree); |
| 1440 | } |
| 1441 | |
Bram Moolenaar | cb6cbf2 | 2021-01-12 17:17:01 +0100 | [diff] [blame] | 1442 | if (res == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1443 | { |
| 1444 | if (called_emsg == called_emsg_before) |
Bram Moolenaar | a6c18d3 | 2022-03-31 20:02:56 +0100 | [diff] [blame] | 1445 | emsg_funcname(e_unknown_function_str, |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1446 | name == NULL ? (char_u *)"[unknown]" : name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1447 | return FAIL; |
| 1448 | } |
| 1449 | return OK; |
| 1450 | } |
| 1451 | |
| 1452 | /* |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 1453 | * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return |
| 1454 | * TRUE. |
| 1455 | */ |
| 1456 | static int |
| 1457 | error_if_locked(int lock, char *error) |
| 1458 | { |
| 1459 | if (lock & (VAR_LOCKED | VAR_FIXED)) |
| 1460 | { |
| 1461 | emsg(_(error)); |
| 1462 | return TRUE; |
| 1463 | } |
| 1464 | return FALSE; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 1468 | * Give an error if "tv" is not a number and return FAIL. |
| 1469 | */ |
| 1470 | static int |
| 1471 | check_for_number(typval_T *tv) |
| 1472 | { |
| 1473 | if (tv->v_type != VAR_NUMBER) |
| 1474 | { |
| 1475 | semsg(_(e_expected_str_but_got_str), |
| 1476 | vartype_name(VAR_NUMBER), vartype_name(tv->v_type)); |
| 1477 | return FAIL; |
| 1478 | } |
| 1479 | return OK; |
| 1480 | } |
| 1481 | |
| 1482 | /* |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1483 | * Store "tv" in variable "name". |
| 1484 | * This is for s: and g: variables. |
| 1485 | */ |
| 1486 | static void |
| 1487 | store_var(char_u *name, typval_T *tv) |
| 1488 | { |
| 1489 | funccal_entry_T entry; |
Bram Moolenaar | d877a57 | 2021-04-01 19:42:48 +0200 | [diff] [blame] | 1490 | int flags = ASSIGN_DECL; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1491 | |
Bram Moolenaar | d877a57 | 2021-04-01 19:42:48 +0200 | [diff] [blame] | 1492 | if (tv->v_lock) |
| 1493 | flags |= ASSIGN_CONST; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1494 | save_funccal(&entry); |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 1495 | set_var_const(name, 0, NULL, tv, FALSE, flags, 0); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1496 | restore_funccal(); |
| 1497 | } |
| 1498 | |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1499 | /* |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1500 | * Convert "tv" to a string. |
| 1501 | * Return FAIL if not allowed. |
| 1502 | */ |
| 1503 | static int |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1504 | do_2string(typval_T *tv, int is_2string_any, int tolerant) |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1505 | { |
| 1506 | if (tv->v_type != VAR_STRING) |
| 1507 | { |
| 1508 | char_u *str; |
| 1509 | |
| 1510 | if (is_2string_any) |
| 1511 | { |
| 1512 | switch (tv->v_type) |
| 1513 | { |
| 1514 | case VAR_SPECIAL: |
| 1515 | case VAR_BOOL: |
| 1516 | case VAR_NUMBER: |
| 1517 | case VAR_FLOAT: |
| 1518 | case VAR_BLOB: break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1519 | |
| 1520 | case VAR_LIST: |
| 1521 | if (tolerant) |
| 1522 | { |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1523 | char_u *s, *e, *p; |
| 1524 | garray_T ga; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1525 | |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1526 | ga_init2(&ga, sizeof(char_u *), 1); |
| 1527 | |
| 1528 | // Convert to NL separated items, then |
| 1529 | // escape the items and replace the NL with |
| 1530 | // a space. |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1531 | str = typval2string(tv, TRUE); |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1532 | if (str == NULL) |
| 1533 | return FAIL; |
| 1534 | s = str; |
| 1535 | while ((e = vim_strchr(s, '\n')) != NULL) |
| 1536 | { |
| 1537 | *e = NUL; |
Bram Moolenaar | 21c1a0c | 2021-10-17 17:20:23 +0100 | [diff] [blame] | 1538 | p = vim_strsave_fnameescape(s, |
| 1539 | VSE_NONE); |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1540 | if (p != NULL) |
| 1541 | { |
| 1542 | ga_concat(&ga, p); |
| 1543 | ga_concat(&ga, (char_u *)" "); |
| 1544 | vim_free(p); |
| 1545 | } |
| 1546 | s = e + 1; |
| 1547 | } |
| 1548 | vim_free(str); |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1549 | clear_tv(tv); |
| 1550 | tv->v_type = VAR_STRING; |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1551 | tv->vval.v_string = ga.ga_data; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1552 | return OK; |
| 1553 | } |
| 1554 | // FALLTHROUGH |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1555 | default: to_string_error(tv->v_type); |
| 1556 | return FAIL; |
| 1557 | } |
| 1558 | } |
Bram Moolenaar | 3445320 | 2021-01-31 13:08:38 +0100 | [diff] [blame] | 1559 | str = typval_tostring(tv, TRUE); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1560 | clear_tv(tv); |
| 1561 | tv->v_type = VAR_STRING; |
| 1562 | tv->vval.v_string = str; |
| 1563 | } |
| 1564 | return OK; |
| 1565 | } |
| 1566 | |
| 1567 | /* |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1568 | * When the value of "sv" is a null list of dict, allocate it. |
| 1569 | */ |
| 1570 | static void |
Bram Moolenaar | 859cc21 | 2022-03-28 15:22:35 +0100 | [diff] [blame] | 1571 | allocate_if_null(svar_T *sv) |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1572 | { |
Bram Moolenaar | 859cc21 | 2022-03-28 15:22:35 +0100 | [diff] [blame] | 1573 | typval_T *tv = sv->sv_tv; |
| 1574 | |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1575 | switch (tv->v_type) |
| 1576 | { |
| 1577 | case VAR_LIST: |
Bram Moolenaar | 859cc21 | 2022-03-28 15:22:35 +0100 | [diff] [blame] | 1578 | if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty) |
Bram Moolenaar | fef8064 | 2021-02-03 20:01:19 +0100 | [diff] [blame] | 1579 | (void)rettv_list_alloc(tv); |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1580 | break; |
| 1581 | case VAR_DICT: |
Bram Moolenaar | 859cc21 | 2022-03-28 15:22:35 +0100 | [diff] [blame] | 1582 | if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty) |
Bram Moolenaar | fef8064 | 2021-02-03 20:01:19 +0100 | [diff] [blame] | 1583 | (void)rettv_dict_alloc(tv); |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1584 | break; |
Bram Moolenaar | b7c21af | 2021-04-18 14:12:31 +0200 | [diff] [blame] | 1585 | case VAR_BLOB: |
Bram Moolenaar | 859cc21 | 2022-03-28 15:22:35 +0100 | [diff] [blame] | 1586 | if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null) |
Bram Moolenaar | b7c21af | 2021-04-18 14:12:31 +0200 | [diff] [blame] | 1587 | (void)rettv_blob_alloc(tv); |
| 1588 | break; |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1589 | default: |
| 1590 | break; |
| 1591 | } |
| 1592 | } |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1593 | |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1594 | /* |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1595 | * Return the character "str[index]" where "index" is the character index, |
| 1596 | * including composing characters. |
| 1597 | * If "index" is out of range NULL is returned. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1598 | */ |
| 1599 | char_u * |
| 1600 | char_from_string(char_u *str, varnumber_T index) |
| 1601 | { |
| 1602 | size_t nbyte = 0; |
| 1603 | varnumber_T nchar = index; |
| 1604 | size_t slen; |
| 1605 | |
| 1606 | if (str == NULL) |
| 1607 | return NULL; |
| 1608 | slen = STRLEN(str); |
| 1609 | |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1610 | // Do the same as for a list: a negative index counts from the end. |
| 1611 | // Optimization to check the first byte to be below 0x80 (and no composing |
| 1612 | // character follows) makes this a lot faster. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1613 | if (index < 0) |
| 1614 | { |
| 1615 | int clen = 0; |
| 1616 | |
| 1617 | for (nbyte = 0; nbyte < slen; ++clen) |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1618 | { |
| 1619 | if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80) |
| 1620 | ++nbyte; |
| 1621 | else if (enc_utf8) |
| 1622 | nbyte += utfc_ptr2len(str + nbyte); |
| 1623 | else |
| 1624 | nbyte += mb_ptr2len(str + nbyte); |
| 1625 | } |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1626 | nchar = clen + index; |
| 1627 | if (nchar < 0) |
| 1628 | // unlike list: index out of range results in empty string |
| 1629 | return NULL; |
| 1630 | } |
| 1631 | |
| 1632 | for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar) |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1633 | { |
| 1634 | if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80) |
| 1635 | ++nbyte; |
| 1636 | else if (enc_utf8) |
| 1637 | nbyte += utfc_ptr2len(str + nbyte); |
| 1638 | else |
| 1639 | nbyte += mb_ptr2len(str + nbyte); |
| 1640 | } |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1641 | if (nbyte >= slen) |
| 1642 | return NULL; |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1643 | return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte)); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | /* |
| 1647 | * Get the byte index for character index "idx" in string "str" with length |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1648 | * "str_len". Composing characters are included. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1649 | * If going over the end return "str_len". |
| 1650 | * If "idx" is negative count from the end, -1 is the last character. |
| 1651 | * When going over the start return -1. |
| 1652 | */ |
| 1653 | static long |
| 1654 | char_idx2byte(char_u *str, size_t str_len, varnumber_T idx) |
| 1655 | { |
| 1656 | varnumber_T nchar = idx; |
| 1657 | size_t nbyte = 0; |
| 1658 | |
| 1659 | if (nchar >= 0) |
| 1660 | { |
| 1661 | while (nchar > 0 && nbyte < str_len) |
| 1662 | { |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1663 | nbyte += mb_ptr2len(str + nbyte); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1664 | --nchar; |
| 1665 | } |
| 1666 | } |
| 1667 | else |
| 1668 | { |
| 1669 | nbyte = str_len; |
| 1670 | while (nchar < 0 && nbyte > 0) |
| 1671 | { |
| 1672 | --nbyte; |
| 1673 | nbyte -= mb_head_off(str, str + nbyte); |
| 1674 | ++nchar; |
| 1675 | } |
| 1676 | if (nchar < 0) |
| 1677 | return -1; |
| 1678 | } |
| 1679 | return (long)nbyte; |
| 1680 | } |
| 1681 | |
| 1682 | /* |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1683 | * Return the slice "str[first : last]" using character indexes. Composing |
| 1684 | * characters are included. |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1685 | * "exclusive" is TRUE for slice(). |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1686 | * Return NULL when the result is empty. |
| 1687 | */ |
| 1688 | char_u * |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1689 | string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive) |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1690 | { |
| 1691 | long start_byte, end_byte; |
| 1692 | size_t slen; |
| 1693 | |
| 1694 | if (str == NULL) |
| 1695 | return NULL; |
| 1696 | slen = STRLEN(str); |
| 1697 | start_byte = char_idx2byte(str, slen, first); |
| 1698 | if (start_byte < 0) |
| 1699 | start_byte = 0; // first index very negative: use zero |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1700 | if ((last == -1 && !exclusive) || last == VARNUM_MAX) |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1701 | end_byte = (long)slen; |
| 1702 | else |
| 1703 | { |
| 1704 | end_byte = char_idx2byte(str, slen, last); |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1705 | if (!exclusive && end_byte >= 0 && end_byte < (long)slen) |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1706 | // end index is inclusive |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1707 | end_byte += mb_ptr2len(str + end_byte); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | if (start_byte >= (long)slen || end_byte <= start_byte) |
| 1711 | return NULL; |
| 1712 | return vim_strnsave(str + start_byte, end_byte - start_byte); |
| 1713 | } |
| 1714 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1715 | /* |
| 1716 | * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT. |
| 1717 | * When "dfunc_idx" is negative don't give an error. |
| 1718 | * Returns NULL for an error. |
| 1719 | */ |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1720 | static svar_T * |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1721 | get_script_svar(scriptref_T *sref, int dfunc_idx) |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1722 | { |
| 1723 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1724 | dfunc_T *dfunc = dfunc_idx < 0 ? NULL |
| 1725 | : ((dfunc_T *)def_functions.ga_data) + dfunc_idx; |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1726 | svar_T *sv; |
| 1727 | |
| 1728 | if (sref->sref_seq != si->sn_script_seq) |
| 1729 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1730 | // The script was reloaded after the function was compiled, the |
| 1731 | // script_idx may not be valid. |
| 1732 | if (dfunc != NULL) |
| 1733 | semsg(_(e_script_variable_invalid_after_reload_in_function_str), |
| 1734 | printable_func_name(dfunc->df_ufunc)); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1735 | return NULL; |
| 1736 | } |
| 1737 | sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx; |
Bram Moolenaar | 6de2296 | 2022-09-09 21:35:36 +0100 | [diff] [blame] | 1738 | if (sv->sv_name == NULL) |
| 1739 | { |
| 1740 | if (dfunc != NULL) |
| 1741 | emsg(_(e_script_variable_was_deleted)); |
| 1742 | return NULL; |
| 1743 | } |
Bram Moolenaar | 60dc827 | 2021-07-29 22:48:54 +0200 | [diff] [blame] | 1744 | if (!equal_type(sv->sv_type, sref->sref_type, 0)) |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1745 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1746 | if (dfunc != NULL) |
| 1747 | emsg(_(e_script_variable_type_changed)); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1748 | return NULL; |
| 1749 | } |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 1750 | |
Bram Moolenaar | aa7d0c2 | 2022-04-05 21:40:38 +0100 | [diff] [blame] | 1751 | if ((sv->sv_flags & SVFLAG_EXPORTED) == 0 |
| 1752 | && sref->sref_sid != current_sctx.sc_sid) |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 1753 | { |
| 1754 | if (dfunc != NULL) |
| 1755 | semsg(_(e_item_not_exported_in_script_str), sv->sv_name); |
| 1756 | return NULL; |
| 1757 | } |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1758 | return sv; |
| 1759 | } |
| 1760 | |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1761 | /* |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1762 | * Function passed to do_cmdline() for splitting a script joined by NL |
| 1763 | * characters. |
| 1764 | */ |
| 1765 | static char_u * |
| 1766 | get_split_sourceline( |
| 1767 | int c UNUSED, |
| 1768 | void *cookie, |
| 1769 | int indent UNUSED, |
| 1770 | getline_opt_T options UNUSED) |
| 1771 | { |
| 1772 | source_cookie_T *sp = (source_cookie_T *)cookie; |
| 1773 | char_u *p; |
| 1774 | char_u *line; |
| 1775 | |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1776 | p = vim_strchr(sp->nextline, '\n'); |
| 1777 | if (p == NULL) |
| 1778 | { |
| 1779 | line = vim_strsave(sp->nextline); |
| 1780 | sp->nextline += STRLEN(sp->nextline); |
| 1781 | } |
| 1782 | else |
| 1783 | { |
| 1784 | line = vim_strnsave(sp->nextline, p - sp->nextline); |
| 1785 | sp->nextline = p + 1; |
| 1786 | } |
| 1787 | return line; |
| 1788 | } |
| 1789 | |
| 1790 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1791 | * Execute a function by "name". |
| 1792 | * This can be a builtin function, user function or a funcref. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 1793 | * "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] | 1794 | */ |
| 1795 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1796 | call_eval_func( |
| 1797 | char_u *name, |
| 1798 | int argcount, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1799 | ectx_T *ectx, |
| 1800 | isn_T *iptr) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1801 | { |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1802 | int called_emsg_before = called_emsg; |
| 1803 | int res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1804 | |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1805 | res = call_by_name(name, argcount, ectx, iptr, NULL); |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1806 | if (res == FAIL && called_emsg == called_emsg_before) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1807 | { |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 1808 | dictitem_T *v; |
| 1809 | |
| 1810 | v = find_var(name, NULL, FALSE); |
Bram Moolenaar | a6c18d3 | 2022-03-31 20:02:56 +0100 | [diff] [blame] | 1811 | if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL |
| 1812 | && v->di_tv.v_type != VAR_FUNC)) |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 1813 | { |
Bram Moolenaar | a6c18d3 | 2022-03-31 20:02:56 +0100 | [diff] [blame] | 1814 | emsg_funcname(e_unknown_function_str, name); |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 1815 | return FAIL; |
| 1816 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1817 | return call_partial(&v->di_tv, argcount, ectx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1818 | } |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1819 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /* |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1823 | * When a function reference is used, fill a partial with the information |
| 1824 | * needed, especially when it is used as a closure. |
| 1825 | */ |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 1826 | int |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1827 | fill_partial_and_closure( |
| 1828 | partial_T *pt, |
| 1829 | ufunc_T *ufunc, |
| 1830 | short loop_var_idx, |
| 1831 | short loop_var_count, |
| 1832 | ectx_T *ectx) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1833 | { |
| 1834 | pt->pt_func = ufunc; |
| 1835 | pt->pt_refcount = 1; |
| 1836 | |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 1837 | if (ufunc->uf_flags & FC_CLOSURE) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1838 | { |
| 1839 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 1840 | + ectx->ec_dfunc_idx; |
| 1841 | |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1842 | // The closure may need to find arguments and local variables of the |
| 1843 | // current function in the stack. |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 1844 | pt->pt_outer.out_stack = &ectx->ec_stack; |
| 1845 | pt->pt_outer.out_frame_idx = ectx->ec_frame_idx; |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1846 | if (ectx->ec_outer_ref != NULL) |
| 1847 | { |
| 1848 | // The current context already has a context, link to that one. |
| 1849 | pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer; |
| 1850 | if (ectx->ec_outer_ref->or_partial != NULL) |
| 1851 | { |
| 1852 | pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial; |
| 1853 | ++pt->pt_outer.out_up_partial->pt_refcount; |
| 1854 | } |
| 1855 | } |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1856 | |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1857 | // The closure may need to find variables defined inside a loop. A |
| 1858 | // new reference is made every time, ISN_ENDLOOP will check if they |
| 1859 | // are actually used. |
| 1860 | pt->pt_outer.out_loop_stack = &ectx->ec_stack; |
| 1861 | pt->pt_outer.out_loop_var_idx = ectx->ec_frame_idx + STACK_FRAME_SIZE |
| 1862 | + loop_var_idx; |
| 1863 | pt->pt_outer.out_loop_var_count = loop_var_count; |
| 1864 | |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1865 | // If the function currently executing returns and the closure is still |
| 1866 | // being referenced, we need to make a copy of the context (arguments |
| 1867 | // and local variables) so that the closure can use it later. |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1868 | // Store a reference to the partial so we can handle that. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1869 | if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1)) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1870 | { |
| 1871 | vim_free(pt); |
| 1872 | return FAIL; |
| 1873 | } |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1874 | // Extra variable keeps the count of closures created in the current |
| 1875 | // function call. |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1876 | ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx |
| 1877 | + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number; |
| 1878 | |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1879 | ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len] |
| 1880 | = pt; |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1881 | ++pt->pt_refcount; |
| 1882 | ++ectx->ec_funcrefs.ga_len; |
| 1883 | } |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 1884 | ++ufunc->uf_refcount; |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1885 | return OK; |
| 1886 | } |
| 1887 | |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 1888 | /* |
| 1889 | * Execute iptr->isn_arg.string as an Ex command. |
| 1890 | */ |
| 1891 | static int |
| 1892 | exec_command(isn_T *iptr) |
| 1893 | { |
| 1894 | source_cookie_T cookie; |
| 1895 | |
| 1896 | SOURCING_LNUM = iptr->isn_lnum; |
| 1897 | // Pass getsourceline to get an error for a missing ":end" |
| 1898 | // command. |
| 1899 | CLEAR_FIELD(cookie); |
| 1900 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 1901 | if (do_cmdline(iptr->isn_arg.string, |
| 1902 | getsourceline, &cookie, |
| 1903 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL |
| 1904 | || did_emsg) |
| 1905 | return FAIL; |
| 1906 | return OK; |
| 1907 | } |
| 1908 | |
Bram Moolenaar | 8944551 | 2022-04-14 12:58:23 +0100 | [diff] [blame] | 1909 | /* |
| 1910 | * If script "sid" is not loaded yet then load it now. |
| 1911 | * Caller must make sure "sid" is a valid script ID. |
| 1912 | * "loaded" is set to TRUE if the script had to be loaded. |
| 1913 | * Returns FAIL if loading fails, OK if already loaded or loaded now. |
| 1914 | */ |
| 1915 | int |
| 1916 | may_load_script(int sid, int *loaded) |
| 1917 | { |
| 1918 | scriptitem_T *si = SCRIPT_ITEM(sid); |
| 1919 | |
| 1920 | if (si->sn_state == SN_STATE_NOT_LOADED) |
| 1921 | { |
| 1922 | *loaded = TRUE; |
| 1923 | if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL) |
| 1924 | { |
| 1925 | semsg(_(e_cant_open_file_str), si->sn_name); |
| 1926 | return FAIL; |
| 1927 | } |
| 1928 | } |
| 1929 | return OK; |
| 1930 | } |
| 1931 | |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 1932 | // used for v_instr of typval of VAR_INSTR |
| 1933 | struct instr_S { |
| 1934 | ectx_T *instr_ectx; |
| 1935 | isn_T *instr_instr; |
| 1936 | }; |
| 1937 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1938 | // used for substitute_instr |
| 1939 | typedef struct subs_expr_S { |
| 1940 | ectx_T *subs_ectx; |
| 1941 | isn_T *subs_instr; |
| 1942 | int subs_status; |
| 1943 | } subs_expr_T; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1944 | |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1945 | // Set when calling do_debug(). |
| 1946 | static ectx_T *debug_context = NULL; |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1947 | static int debug_var_count; |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1948 | |
| 1949 | /* |
| 1950 | * When debugging lookup "name" and return the typeval. |
| 1951 | * When not found return NULL. |
| 1952 | */ |
| 1953 | typval_T * |
| 1954 | lookup_debug_var(char_u *name) |
| 1955 | { |
| 1956 | int idx; |
| 1957 | dfunc_T *dfunc; |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1958 | ufunc_T *ufunc; |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1959 | ectx_T *ectx = debug_context; |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1960 | int varargs_off; |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1961 | |
| 1962 | if (ectx == NULL) |
| 1963 | return NULL; |
| 1964 | dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx; |
| 1965 | |
| 1966 | // Go through the local variable names, from last to first. |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1967 | for (idx = debug_var_count - 1; idx >= 0; --idx) |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1968 | { |
Bram Moolenaar | e406ff8 | 2022-03-10 20:47:43 +0000 | [diff] [blame] | 1969 | char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx]; |
| 1970 | |
| 1971 | // the variable name may be NULL when not available in this block |
| 1972 | if (varname != NULL && STRCMP(varname, name) == 0) |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1973 | return STACK_TV_VAR(idx); |
| 1974 | } |
| 1975 | |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1976 | // Go through argument names. |
| 1977 | ufunc = dfunc->df_ufunc; |
| 1978 | varargs_off = ufunc->uf_va_name == NULL ? 0 : 1; |
| 1979 | for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx) |
| 1980 | if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0) |
| 1981 | return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len |
| 1982 | - varargs_off + idx); |
| 1983 | if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0) |
| 1984 | return STACK_TV(ectx->ec_frame_idx - 1); |
| 1985 | |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1986 | return NULL; |
| 1987 | } |
| 1988 | |
Bram Moolenaar | 26a4484 | 2021-09-02 18:49:06 +0200 | [diff] [blame] | 1989 | /* |
| 1990 | * Return TRUE if there might be a breakpoint in "ufunc", which is when a |
| 1991 | * breakpoint was set in that function or when there is any expression. |
| 1992 | */ |
| 1993 | int |
| 1994 | may_break_in_function(ufunc_T *ufunc) |
| 1995 | { |
| 1996 | return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint(); |
| 1997 | } |
| 1998 | |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1999 | static void |
| 2000 | handle_debug(isn_T *iptr, ectx_T *ectx) |
| 2001 | { |
| 2002 | char_u *line; |
| 2003 | ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data) |
| 2004 | + ectx->ec_dfunc_idx)->df_ufunc; |
| 2005 | isn_T *ni; |
| 2006 | int end_lnum = iptr->isn_lnum; |
| 2007 | garray_T ga; |
| 2008 | int lnum; |
| 2009 | |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 2010 | if (ex_nesting_level > debug_break_level) |
| 2011 | { |
| 2012 | linenr_T breakpoint; |
| 2013 | |
Bram Moolenaar | 26a4484 | 2021-09-02 18:49:06 +0200 | [diff] [blame] | 2014 | if (!may_break_in_function(ufunc)) |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 2015 | return; |
| 2016 | |
| 2017 | // check for the next breakpoint if needed |
| 2018 | breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, |
Bram Moolenaar | 8cec927 | 2021-06-23 20:20:53 +0200 | [diff] [blame] | 2019 | iptr->isn_arg.debug.dbg_break_lnum); |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 2020 | if (breakpoint <= 0 || breakpoint > iptr->isn_lnum) |
| 2021 | return; |
| 2022 | } |
| 2023 | |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 2024 | SOURCING_LNUM = iptr->isn_lnum; |
| 2025 | debug_context = ectx; |
Bram Moolenaar | 8cec927 | 2021-06-23 20:20:53 +0200 | [diff] [blame] | 2026 | debug_var_count = iptr->isn_arg.debug.dbg_var_names_len; |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 2027 | |
| 2028 | for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni) |
| 2029 | if (ni->isn_type == ISN_DEBUG |
| 2030 | || ni->isn_type == ISN_RETURN |
| 2031 | || ni->isn_type == ISN_RETURN_VOID) |
| 2032 | { |
Bram Moolenaar | 112bed0 | 2021-11-23 22:16:34 +0000 | [diff] [blame] | 2033 | end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1); |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 2034 | break; |
| 2035 | } |
| 2036 | |
| 2037 | if (end_lnum > iptr->isn_lnum) |
| 2038 | { |
| 2039 | ga_init2(&ga, sizeof(char_u *), 10); |
Bram Moolenaar | 310091d | 2021-12-23 21:14:37 +0000 | [diff] [blame] | 2040 | for (lnum = iptr->isn_lnum; lnum < end_lnum |
| 2041 | && lnum <= ufunc->uf_lines.ga_len; ++lnum) |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 2042 | { |
Bram Moolenaar | 303215d | 2021-07-07 20:10:43 +0200 | [diff] [blame] | 2043 | char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1]; |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 2044 | |
Bram Moolenaar | 303215d | 2021-07-07 20:10:43 +0200 | [diff] [blame] | 2045 | if (p == NULL) |
| 2046 | continue; // left over from continuation line |
| 2047 | p = skipwhite(p); |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 2048 | if (*p == '#') |
| 2049 | break; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2050 | if (GA_GROW_OK(&ga, 1)) |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 2051 | ((char_u **)(ga.ga_data))[ga.ga_len++] = p; |
| 2052 | if (STRNCMP(p, "def ", 4) == 0) |
| 2053 | break; |
| 2054 | } |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 2055 | line = ga_concat_strings(&ga, " "); |
| 2056 | vim_free(ga.ga_data); |
| 2057 | } |
| 2058 | else |
| 2059 | line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1]; |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 2060 | |
Dominique Pelle | 6e96955 | 2021-06-17 13:53:41 +0200 | [diff] [blame] | 2061 | do_debug(line == NULL ? (char_u *)"[empty]" : line); |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 2062 | debug_context = NULL; |
| 2063 | |
| 2064 | if (end_lnum > iptr->isn_lnum) |
| 2065 | vim_free(line); |
| 2066 | } |
| 2067 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2068 | /* |
Bram Moolenaar | fe1bfc9 | 2022-02-06 13:55:03 +0000 | [diff] [blame] | 2069 | * Store a value in a list, dict or blob variable. |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2070 | * Returns OK, FAIL or NOTDONE (uncatchable error). |
| 2071 | */ |
| 2072 | static int |
| 2073 | execute_storeindex(isn_T *iptr, ectx_T *ectx) |
| 2074 | { |
| 2075 | vartype_T dest_type = iptr->isn_arg.vartype; |
| 2076 | typval_T *tv; |
| 2077 | typval_T *tv_idx = STACK_TV_BOT(-2); |
| 2078 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2079 | int status = OK; |
| 2080 | |
| 2081 | // Stack contains: |
| 2082 | // -3 value to be stored |
| 2083 | // -2 index |
| 2084 | // -1 dict or list |
| 2085 | tv = STACK_TV_BOT(-3); |
| 2086 | SOURCING_LNUM = iptr->isn_lnum; |
| 2087 | if (dest_type == VAR_ANY) |
| 2088 | { |
| 2089 | dest_type = tv_dest->v_type; |
| 2090 | if (dest_type == VAR_DICT) |
| 2091 | status = do_2string(tv_idx, TRUE, FALSE); |
| 2092 | else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER) |
| 2093 | { |
| 2094 | emsg(_(e_number_expected)); |
| 2095 | status = FAIL; |
| 2096 | } |
| 2097 | } |
Bram Moolenaar | e08be09 | 2022-02-17 13:08:26 +0000 | [diff] [blame] | 2098 | |
| 2099 | if (status == OK) |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2100 | { |
Bram Moolenaar | e08be09 | 2022-02-17 13:08:26 +0000 | [diff] [blame] | 2101 | if (dest_type == VAR_LIST) |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2102 | { |
Bram Moolenaar | e08be09 | 2022-02-17 13:08:26 +0000 | [diff] [blame] | 2103 | long lidx = (long)tv_idx->vval.v_number; |
| 2104 | list_T *list = tv_dest->vval.v_list; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2105 | |
Bram Moolenaar | e08be09 | 2022-02-17 13:08:26 +0000 | [diff] [blame] | 2106 | if (list == NULL) |
| 2107 | { |
| 2108 | emsg(_(e_list_not_set)); |
| 2109 | return FAIL; |
| 2110 | } |
| 2111 | if (lidx < 0 && list->lv_len + lidx >= 0) |
| 2112 | // negative index is relative to the end |
| 2113 | lidx = list->lv_len + lidx; |
| 2114 | if (lidx < 0 || lidx > list->lv_len) |
| 2115 | { |
| 2116 | semsg(_(e_list_index_out_of_range_nr), lidx); |
| 2117 | return FAIL; |
| 2118 | } |
| 2119 | if (lidx < list->lv_len) |
| 2120 | { |
| 2121 | listitem_T *li = list_find(list, lidx); |
| 2122 | |
| 2123 | if (error_if_locked(li->li_tv.v_lock, |
Bram Moolenaar | 70c43d8 | 2022-01-26 21:01:15 +0000 | [diff] [blame] | 2124 | e_cannot_change_locked_list_item)) |
Bram Moolenaar | e08be09 | 2022-02-17 13:08:26 +0000 | [diff] [blame] | 2125 | return FAIL; |
| 2126 | // overwrite existing list item |
| 2127 | clear_tv(&li->li_tv); |
| 2128 | li->li_tv = *tv; |
| 2129 | } |
| 2130 | else |
| 2131 | { |
| 2132 | if (error_if_locked(list->lv_lock, e_cannot_change_locked_list)) |
| 2133 | return FAIL; |
| 2134 | // append to list, only fails when out of memory |
| 2135 | if (list_append_tv(list, tv) == FAIL) |
| 2136 | return NOTDONE; |
| 2137 | clear_tv(tv); |
| 2138 | } |
| 2139 | } |
| 2140 | else if (dest_type == VAR_DICT) |
| 2141 | { |
| 2142 | char_u *key = tv_idx->vval.v_string; |
| 2143 | dict_T *dict = tv_dest->vval.v_dict; |
| 2144 | dictitem_T *di; |
| 2145 | |
| 2146 | SOURCING_LNUM = iptr->isn_lnum; |
| 2147 | if (dict == NULL) |
| 2148 | { |
| 2149 | emsg(_(e_dictionary_not_set)); |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2150 | return FAIL; |
Bram Moolenaar | e08be09 | 2022-02-17 13:08:26 +0000 | [diff] [blame] | 2151 | } |
| 2152 | if (key == NULL) |
| 2153 | key = (char_u *)""; |
| 2154 | di = dict_find(dict, key, -1); |
| 2155 | if (di != NULL) |
| 2156 | { |
| 2157 | if (error_if_locked(di->di_tv.v_lock, |
| 2158 | e_cannot_change_dict_item)) |
| 2159 | return FAIL; |
| 2160 | // overwrite existing value |
| 2161 | clear_tv(&di->di_tv); |
| 2162 | di->di_tv = *tv; |
| 2163 | } |
| 2164 | else |
| 2165 | { |
| 2166 | if (error_if_locked(dict->dv_lock, e_cannot_change_dict)) |
| 2167 | return FAIL; |
| 2168 | // add to dict, only fails when out of memory |
| 2169 | if (dict_add_tv(dict, (char *)key, tv) == FAIL) |
| 2170 | return NOTDONE; |
| 2171 | clear_tv(tv); |
| 2172 | } |
| 2173 | } |
| 2174 | else if (dest_type == VAR_BLOB) |
| 2175 | { |
| 2176 | long lidx = (long)tv_idx->vval.v_number; |
| 2177 | blob_T *blob = tv_dest->vval.v_blob; |
| 2178 | varnumber_T nr; |
| 2179 | int error = FALSE; |
| 2180 | int len; |
| 2181 | |
| 2182 | if (blob == NULL) |
| 2183 | { |
| 2184 | emsg(_(e_blob_not_set)); |
| 2185 | return FAIL; |
| 2186 | } |
| 2187 | len = blob_len(blob); |
| 2188 | if (lidx < 0 && len + lidx >= 0) |
| 2189 | // negative index is relative to the end |
| 2190 | lidx = len + lidx; |
| 2191 | |
| 2192 | // Can add one byte at the end. |
| 2193 | if (lidx < 0 || lidx > len) |
| 2194 | { |
| 2195 | semsg(_(e_blob_index_out_of_range_nr), lidx); |
| 2196 | return FAIL; |
| 2197 | } |
| 2198 | if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE)) |
| 2199 | return FAIL; |
| 2200 | nr = tv_get_number_chk(tv, &error); |
| 2201 | if (error) |
| 2202 | return FAIL; |
| 2203 | blob_set_append(blob, lidx, nr); |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2204 | } |
| 2205 | else |
| 2206 | { |
Bram Moolenaar | e08be09 | 2022-02-17 13:08:26 +0000 | [diff] [blame] | 2207 | status = FAIL; |
| 2208 | semsg(_(e_cannot_index_str), vartype_name(dest_type)); |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2209 | } |
| 2210 | } |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2211 | |
| 2212 | clear_tv(tv_idx); |
| 2213 | clear_tv(tv_dest); |
| 2214 | ectx->ec_stack.ga_len -= 3; |
| 2215 | if (status == FAIL) |
| 2216 | { |
| 2217 | clear_tv(tv); |
| 2218 | return FAIL; |
| 2219 | } |
| 2220 | return OK; |
| 2221 | } |
| 2222 | |
| 2223 | /* |
Bram Moolenaar | ea5c898 | 2022-02-17 14:42:02 +0000 | [diff] [blame] | 2224 | * Store a value in a list or blob range. |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2225 | */ |
| 2226 | static int |
| 2227 | execute_storerange(isn_T *iptr, ectx_T *ectx) |
| 2228 | { |
| 2229 | typval_T *tv; |
| 2230 | typval_T *tv_idx1 = STACK_TV_BOT(-3); |
| 2231 | typval_T *tv_idx2 = STACK_TV_BOT(-2); |
| 2232 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2233 | int status = OK; |
| 2234 | |
| 2235 | // Stack contains: |
| 2236 | // -4 value to be stored |
| 2237 | // -3 first index or "none" |
| 2238 | // -2 second index or "none" |
| 2239 | // -1 destination list or blob |
| 2240 | tv = STACK_TV_BOT(-4); |
Bram Moolenaar | ea5c898 | 2022-02-17 14:42:02 +0000 | [diff] [blame] | 2241 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2242 | if (tv_dest->v_type == VAR_LIST) |
| 2243 | { |
Bram Moolenaar | 2995e5c | 2022-03-18 21:41:47 +0000 | [diff] [blame] | 2244 | long n1; |
| 2245 | long n2; |
| 2246 | listitem_T *li1; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2247 | |
Bram Moolenaar | 2995e5c | 2022-03-18 21:41:47 +0000 | [diff] [blame] | 2248 | n1 = (long)tv_get_number_chk(tv_idx1, NULL); |
| 2249 | if (tv_idx2->v_type == VAR_SPECIAL |
| 2250 | && tv_idx2->vval.v_number == VVAL_NONE) |
| 2251 | n2 = list_len(tv_dest->vval.v_list) - 1; |
| 2252 | else |
| 2253 | n2 = (long)tv_get_number_chk(tv_idx2, NULL); |
| 2254 | |
Bram Moolenaar | 22ebd17 | 2022-04-01 15:26:58 +0100 | [diff] [blame] | 2255 | li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE); |
Bram Moolenaar | 2995e5c | 2022-03-18 21:41:47 +0000 | [diff] [blame] | 2256 | if (li1 == NULL) |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2257 | status = FAIL; |
| 2258 | else |
| 2259 | { |
Bram Moolenaar | 2995e5c | 2022-03-18 21:41:47 +0000 | [diff] [blame] | 2260 | status = check_range_index_two(tv_dest->vval.v_list, |
| 2261 | &n1, li1, &n2, FALSE); |
| 2262 | if (status != FAIL) |
| 2263 | status = list_assign_range( |
| 2264 | tv_dest->vval.v_list, |
| 2265 | tv->vval.v_list, |
| 2266 | n1, |
| 2267 | n2, |
| 2268 | tv_idx2->v_type == VAR_SPECIAL, |
| 2269 | (char_u *)"=", |
| 2270 | (char_u *)"[unknown]"); |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2271 | } |
| 2272 | } |
| 2273 | else if (tv_dest->v_type == VAR_BLOB) |
| 2274 | { |
| 2275 | varnumber_T n1; |
| 2276 | varnumber_T n2; |
Bram Moolenaar | 2995e5c | 2022-03-18 21:41:47 +0000 | [diff] [blame] | 2277 | long bloblen; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2278 | |
Bram Moolenaar | 2995e5c | 2022-03-18 21:41:47 +0000 | [diff] [blame] | 2279 | n1 = tv_get_number_chk(tv_idx1, NULL); |
| 2280 | if (tv_idx2->v_type == VAR_SPECIAL |
| 2281 | && tv_idx2->vval.v_number == VVAL_NONE) |
| 2282 | n2 = blob_len(tv_dest->vval.v_blob) - 1; |
| 2283 | else |
| 2284 | n2 = tv_get_number_chk(tv_idx2, NULL); |
| 2285 | bloblen = blob_len(tv_dest->vval.v_blob); |
| 2286 | |
| 2287 | if (check_blob_index(bloblen, n1, FALSE) == FAIL |
| 2288 | || check_blob_range(bloblen, n1, n2, FALSE) == FAIL) |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2289 | status = FAIL; |
| 2290 | else |
Bram Moolenaar | 2995e5c | 2022-03-18 21:41:47 +0000 | [diff] [blame] | 2291 | status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv); |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2292 | } |
| 2293 | else |
| 2294 | { |
| 2295 | status = FAIL; |
Bram Moolenaar | ea5c898 | 2022-02-17 14:42:02 +0000 | [diff] [blame] | 2296 | emsg(_(e_list_or_blob_required)); |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | clear_tv(tv_idx1); |
| 2300 | clear_tv(tv_idx2); |
| 2301 | clear_tv(tv_dest); |
| 2302 | ectx->ec_stack.ga_len -= 4; |
| 2303 | clear_tv(tv); |
| 2304 | |
| 2305 | return status; |
| 2306 | } |
| 2307 | |
| 2308 | /* |
| 2309 | * Unlet item in list or dict variable. |
| 2310 | */ |
| 2311 | static int |
| 2312 | execute_unletindex(isn_T *iptr, ectx_T *ectx) |
| 2313 | { |
| 2314 | typval_T *tv_idx = STACK_TV_BOT(-2); |
| 2315 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2316 | int status = OK; |
| 2317 | |
| 2318 | // Stack contains: |
| 2319 | // -2 index |
| 2320 | // -1 dict or list |
Bram Moolenaar | 6296d1e | 2022-02-17 16:30:11 +0000 | [diff] [blame] | 2321 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2322 | if (tv_dest->v_type == VAR_DICT) |
| 2323 | { |
| 2324 | // unlet a dict item, index must be a string |
Bram Moolenaar | ea5c898 | 2022-02-17 14:42:02 +0000 | [diff] [blame] | 2325 | if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER) |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2326 | { |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2327 | semsg(_(e_expected_str_but_got_str), |
| 2328 | vartype_name(VAR_STRING), |
| 2329 | vartype_name(tv_idx->v_type)); |
| 2330 | status = FAIL; |
| 2331 | } |
| 2332 | else |
| 2333 | { |
| 2334 | dict_T *d = tv_dest->vval.v_dict; |
Bram Moolenaar | ea5c898 | 2022-02-17 14:42:02 +0000 | [diff] [blame] | 2335 | char_u *key; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2336 | dictitem_T *di = NULL; |
| 2337 | |
| 2338 | if (d != NULL && value_check_lock( |
| 2339 | d->dv_lock, NULL, FALSE)) |
| 2340 | status = FAIL; |
| 2341 | else |
| 2342 | { |
Bram Moolenaar | ea5c898 | 2022-02-17 14:42:02 +0000 | [diff] [blame] | 2343 | if (tv_idx->v_type == VAR_STRING) |
| 2344 | { |
| 2345 | key = tv_idx->vval.v_string; |
| 2346 | if (key == NULL) |
| 2347 | key = (char_u *)""; |
| 2348 | } |
| 2349 | else |
| 2350 | { |
| 2351 | key = tv_get_string(tv_idx); |
| 2352 | } |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2353 | if (d != NULL) |
| 2354 | di = dict_find(d, key, (int)STRLEN(key)); |
| 2355 | if (di == NULL) |
| 2356 | { |
| 2357 | // NULL dict is equivalent to empty dict |
| 2358 | semsg(_(e_key_not_present_in_dictionary), |
| 2359 | key); |
| 2360 | status = FAIL; |
| 2361 | } |
| 2362 | else if (var_check_fixed(di->di_flags, |
| 2363 | NULL, FALSE) |
| 2364 | || var_check_ro(di->di_flags, |
| 2365 | NULL, FALSE)) |
| 2366 | status = FAIL; |
| 2367 | else |
| 2368 | dictitem_remove(d, di); |
| 2369 | } |
| 2370 | } |
| 2371 | } |
| 2372 | else if (tv_dest->v_type == VAR_LIST) |
| 2373 | { |
| 2374 | // unlet a List item, index must be a number |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2375 | if (check_for_number(tv_idx) == FAIL) |
| 2376 | { |
| 2377 | status = FAIL; |
| 2378 | } |
| 2379 | else |
| 2380 | { |
| 2381 | list_T *l = tv_dest->vval.v_list; |
| 2382 | long n = (long)tv_idx->vval.v_number; |
| 2383 | |
| 2384 | if (l != NULL && value_check_lock( |
| 2385 | l->lv_lock, NULL, FALSE)) |
| 2386 | status = FAIL; |
| 2387 | else |
| 2388 | { |
| 2389 | listitem_T *li = list_find(l, n); |
| 2390 | |
| 2391 | if (li == NULL) |
| 2392 | { |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2393 | semsg(_(e_list_index_out_of_range_nr), n); |
| 2394 | status = FAIL; |
| 2395 | } |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2396 | else |
| 2397 | listitem_remove(l, li); |
| 2398 | } |
| 2399 | } |
| 2400 | } |
| 2401 | else |
| 2402 | { |
| 2403 | status = FAIL; |
| 2404 | semsg(_(e_cannot_index_str), |
| 2405 | vartype_name(tv_dest->v_type)); |
| 2406 | } |
| 2407 | |
| 2408 | clear_tv(tv_idx); |
| 2409 | clear_tv(tv_dest); |
| 2410 | ectx->ec_stack.ga_len -= 2; |
| 2411 | |
| 2412 | return status; |
| 2413 | } |
| 2414 | |
| 2415 | /* |
| 2416 | * Unlet a range of items in a list variable. |
| 2417 | */ |
| 2418 | static int |
| 2419 | execute_unletrange(isn_T *iptr, ectx_T *ectx) |
| 2420 | { |
| 2421 | // Stack contains: |
| 2422 | // -3 index1 |
| 2423 | // -2 index2 |
| 2424 | // -1 dict or list |
| 2425 | typval_T *tv_idx1 = STACK_TV_BOT(-3); |
| 2426 | typval_T *tv_idx2 = STACK_TV_BOT(-2); |
| 2427 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2428 | int status = OK; |
| 2429 | |
| 2430 | if (tv_dest->v_type == VAR_LIST) |
| 2431 | { |
| 2432 | // indexes must be a number |
| 2433 | SOURCING_LNUM = iptr->isn_lnum; |
| 2434 | if (check_for_number(tv_idx1) == FAIL |
| 2435 | || (tv_idx2->v_type != VAR_SPECIAL |
| 2436 | && check_for_number(tv_idx2) == FAIL)) |
| 2437 | { |
| 2438 | status = FAIL; |
| 2439 | } |
| 2440 | else |
| 2441 | { |
| 2442 | list_T *l = tv_dest->vval.v_list; |
| 2443 | long n1 = (long)tv_idx1->vval.v_number; |
| 2444 | long n2 = tv_idx2->v_type == VAR_SPECIAL |
| 2445 | ? 0 : (long)tv_idx2->vval.v_number; |
| 2446 | listitem_T *li; |
| 2447 | |
| 2448 | li = list_find_index(l, &n1); |
| 2449 | if (li == NULL) |
Bram Moolenaar | 6296d1e | 2022-02-17 16:30:11 +0000 | [diff] [blame] | 2450 | { |
| 2451 | semsg(_(e_list_index_out_of_range_nr), |
| 2452 | (long)tv_idx1->vval.v_number); |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2453 | status = FAIL; |
Bram Moolenaar | 6296d1e | 2022-02-17 16:30:11 +0000 | [diff] [blame] | 2454 | } |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2455 | else |
| 2456 | { |
| 2457 | if (n1 < 0) |
| 2458 | n1 = list_idx_of_item(l, li); |
| 2459 | if (n2 < 0) |
| 2460 | { |
| 2461 | listitem_T *li2 = list_find(l, n2); |
| 2462 | |
| 2463 | if (li2 == NULL) |
Bram Moolenaar | 6296d1e | 2022-02-17 16:30:11 +0000 | [diff] [blame] | 2464 | { |
| 2465 | semsg(_(e_list_index_out_of_range_nr), n2); |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2466 | status = FAIL; |
Bram Moolenaar | 6296d1e | 2022-02-17 16:30:11 +0000 | [diff] [blame] | 2467 | } |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2468 | else |
| 2469 | n2 = list_idx_of_item(l, li2); |
| 2470 | } |
| 2471 | if (status != FAIL |
| 2472 | && tv_idx2->v_type != VAR_SPECIAL |
| 2473 | && n2 < n1) |
| 2474 | { |
| 2475 | semsg(_(e_list_index_out_of_range_nr), n2); |
| 2476 | status = FAIL; |
| 2477 | } |
Bram Moolenaar | 6b8c7ba | 2022-03-20 17:46:06 +0000 | [diff] [blame] | 2478 | if (status != FAIL) |
| 2479 | list_unlet_range(l, li, n1, |
| 2480 | tv_idx2->v_type != VAR_SPECIAL, n2); |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | } |
| 2484 | else |
| 2485 | { |
| 2486 | status = FAIL; |
| 2487 | SOURCING_LNUM = iptr->isn_lnum; |
| 2488 | semsg(_(e_cannot_index_str), |
| 2489 | vartype_name(tv_dest->v_type)); |
| 2490 | } |
| 2491 | |
| 2492 | clear_tv(tv_idx1); |
| 2493 | clear_tv(tv_idx2); |
| 2494 | clear_tv(tv_dest); |
| 2495 | ectx->ec_stack.ga_len -= 3; |
| 2496 | |
| 2497 | return status; |
| 2498 | } |
| 2499 | |
| 2500 | /* |
| 2501 | * Top of a for loop. |
| 2502 | */ |
| 2503 | static int |
| 2504 | execute_for(isn_T *iptr, ectx_T *ectx) |
| 2505 | { |
| 2506 | typval_T *tv; |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2507 | int jump = FALSE; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2508 | typval_T *ltv = STACK_TV_BOT(-1); |
| 2509 | typval_T *idxtv = |
| 2510 | STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); |
| 2511 | |
| 2512 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
| 2513 | return FAIL; |
| 2514 | if (ltv->v_type == VAR_LIST) |
| 2515 | { |
| 2516 | list_T *list = ltv->vval.v_list; |
| 2517 | |
| 2518 | // push the next item from the list |
| 2519 | ++idxtv->vval.v_number; |
| 2520 | if (list == NULL |
| 2521 | || idxtv->vval.v_number >= list->lv_len) |
| 2522 | { |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2523 | jump = TRUE; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2524 | } |
| 2525 | else if (list->lv_first == &range_list_item) |
| 2526 | { |
| 2527 | // non-materialized range() list |
| 2528 | tv = STACK_TV_BOT(0); |
| 2529 | tv->v_type = VAR_NUMBER; |
| 2530 | tv->v_lock = 0; |
| 2531 | tv->vval.v_number = list_find_nr( |
| 2532 | list, idxtv->vval.v_number, NULL); |
| 2533 | ++ectx->ec_stack.ga_len; |
| 2534 | } |
| 2535 | else |
| 2536 | { |
| 2537 | listitem_T *li = list_find(list, |
| 2538 | idxtv->vval.v_number); |
| 2539 | |
| 2540 | copy_tv(&li->li_tv, STACK_TV_BOT(0)); |
| 2541 | ++ectx->ec_stack.ga_len; |
| 2542 | } |
| 2543 | } |
| 2544 | else if (ltv->v_type == VAR_STRING) |
| 2545 | { |
| 2546 | char_u *str = ltv->vval.v_string; |
| 2547 | |
| 2548 | // The index is for the last byte of the previous |
| 2549 | // character. |
| 2550 | ++idxtv->vval.v_number; |
| 2551 | if (str == NULL || str[idxtv->vval.v_number] == NUL) |
| 2552 | { |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2553 | jump = TRUE; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2554 | } |
| 2555 | else |
| 2556 | { |
| 2557 | int clen = mb_ptr2len(str + idxtv->vval.v_number); |
| 2558 | |
| 2559 | // Push the next character from the string. |
| 2560 | tv = STACK_TV_BOT(0); |
| 2561 | tv->v_type = VAR_STRING; |
| 2562 | tv->vval.v_string = vim_strnsave( |
| 2563 | str + idxtv->vval.v_number, clen); |
| 2564 | ++ectx->ec_stack.ga_len; |
| 2565 | idxtv->vval.v_number += clen - 1; |
| 2566 | } |
| 2567 | } |
| 2568 | else if (ltv->v_type == VAR_BLOB) |
| 2569 | { |
| 2570 | blob_T *blob = ltv->vval.v_blob; |
| 2571 | |
| 2572 | // When we get here the first time make a copy of the |
| 2573 | // blob, so that the iteration still works when it is |
| 2574 | // changed. |
| 2575 | if (idxtv->vval.v_number == -1 && blob != NULL) |
| 2576 | { |
| 2577 | blob_copy(blob, ltv); |
| 2578 | blob_unref(blob); |
| 2579 | blob = ltv->vval.v_blob; |
| 2580 | } |
| 2581 | |
| 2582 | // The index is for the previous byte. |
| 2583 | ++idxtv->vval.v_number; |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2584 | if (blob == NULL || idxtv->vval.v_number >= blob_len(blob)) |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2585 | { |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2586 | jump = TRUE; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2587 | } |
| 2588 | else |
| 2589 | { |
| 2590 | // Push the next byte from the blob. |
| 2591 | tv = STACK_TV_BOT(0); |
| 2592 | tv->v_type = VAR_NUMBER; |
| 2593 | tv->vval.v_number = blob_get(blob, |
| 2594 | idxtv->vval.v_number); |
| 2595 | ++ectx->ec_stack.ga_len; |
| 2596 | } |
| 2597 | } |
| 2598 | else |
| 2599 | { |
| 2600 | semsg(_(e_for_loop_on_str_not_supported), |
| 2601 | vartype_name(ltv->v_type)); |
| 2602 | return FAIL; |
| 2603 | } |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2604 | |
| 2605 | if (jump) |
| 2606 | { |
| 2607 | // past the end of the list/string/blob, jump to "endfor" |
| 2608 | ectx->ec_iidx = iptr->isn_arg.forloop.for_end; |
| 2609 | may_restore_cmdmod(&ectx->ec_funclocal); |
| 2610 | } |
| 2611 | else |
| 2612 | { |
| 2613 | // Store the current number of funcrefs, this may be used in |
| 2614 | // ISN_LOOPEND. The variable index is always one more than the loop |
| 2615 | // variable index. |
| 2616 | tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_idx + 1); |
| 2617 | tv->vval.v_number = ectx->ec_funcrefs.ga_len; |
| 2618 | } |
| 2619 | |
| 2620 | return OK; |
| 2621 | } |
| 2622 | |
| 2623 | /* |
Bram Moolenaar | 0cdfb7c | 2022-09-17 15:44:52 +0100 | [diff] [blame] | 2624 | * Code for handling variables declared inside a loop and used in a closure. |
| 2625 | * This is very similar to what is done with funcstack_T. The difference is |
| 2626 | * that the funcstack_T has the scope of a function, while a loopvars_T has the |
| 2627 | * scope of the block inside a loop and each loop may have its own. |
| 2628 | */ |
| 2629 | |
| 2630 | // Double linked list of loopvars_T in use. |
| 2631 | static loopvars_T *first_loopvars = NULL; |
| 2632 | |
| 2633 | static void |
| 2634 | add_loopvars_to_list(loopvars_T *loopvars) |
| 2635 | { |
| 2636 | // Link in list of loopvarss. |
| 2637 | if (first_loopvars != NULL) |
| 2638 | first_loopvars->lvs_prev = loopvars; |
| 2639 | loopvars->lvs_next = first_loopvars; |
| 2640 | loopvars->lvs_prev = NULL; |
| 2641 | first_loopvars = loopvars; |
| 2642 | } |
| 2643 | |
| 2644 | static void |
| 2645 | remove_loopvars_from_list(loopvars_T *loopvars) |
| 2646 | { |
| 2647 | if (loopvars->lvs_prev == NULL) |
| 2648 | first_loopvars = loopvars->lvs_next; |
| 2649 | else |
| 2650 | loopvars->lvs_prev->lvs_next = loopvars->lvs_next; |
| 2651 | if (loopvars->lvs_next != NULL) |
| 2652 | loopvars->lvs_next->lvs_prev = loopvars->lvs_prev; |
| 2653 | } |
| 2654 | |
| 2655 | /* |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2656 | * End of a for or while loop: Handle any variables used by a closure. |
| 2657 | */ |
| 2658 | static int |
Bram Moolenaar | 0cdfb7c | 2022-09-17 15:44:52 +0100 | [diff] [blame] | 2659 | execute_endloop(isn_T *iptr, ectx_T *ectx) |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2660 | { |
Bram Moolenaar | 0cdfb7c | 2022-09-17 15:44:52 +0100 | [diff] [blame] | 2661 | endloop_T *endloop = &iptr->isn_arg.endloop; |
| 2662 | typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx); |
| 2663 | int prev_closure_count = tv_refcount->vval.v_number; |
| 2664 | garray_T *gap = &ectx->ec_funcrefs; |
| 2665 | int closure_in_use = FALSE; |
| 2666 | loopvars_T *loopvars; |
| 2667 | typval_T *stack; |
| 2668 | int idx; |
| 2669 | |
| 2670 | // Check if any created closure is still being referenced. |
| 2671 | for (idx = prev_closure_count; idx < gap->ga_len; ++idx) |
| 2672 | { |
| 2673 | partial_T *pt = ((partial_T **)gap->ga_data)[idx]; |
| 2674 | |
Bram Moolenaar | dbbb02b | 2022-09-18 12:00:21 +0100 | [diff] [blame] | 2675 | if (pt->pt_refcount > 1 && pt->pt_loopvars == NULL) |
Bram Moolenaar | 0cdfb7c | 2022-09-17 15:44:52 +0100 | [diff] [blame] | 2676 | { |
| 2677 | int refcount = pt->pt_refcount; |
| 2678 | int i; |
| 2679 | |
| 2680 | // A Reference in a variable inside the loop doesn't count, it gets |
| 2681 | // unreferenced at the end of the loop. |
| 2682 | for (i = 0; i < endloop->end_var_count; ++i) |
| 2683 | { |
| 2684 | typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i); |
| 2685 | |
| 2686 | if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial) |
| 2687 | --refcount; |
| 2688 | } |
| 2689 | if (refcount > 1) |
| 2690 | { |
| 2691 | closure_in_use = TRUE; |
| 2692 | break; |
| 2693 | } |
| 2694 | } |
| 2695 | } |
| 2696 | |
| 2697 | // If no function reference were created since the start of the loop block |
| 2698 | // or it is no longer referenced there is nothing to do. |
| 2699 | if (!closure_in_use) |
| 2700 | return OK; |
| 2701 | |
| 2702 | // A closure is using variables declared inside the loop. |
| 2703 | // Move them to the called function. |
| 2704 | loopvars = ALLOC_CLEAR_ONE(loopvars_T); |
| 2705 | if (loopvars == NULL) |
| 2706 | return FAIL; |
| 2707 | |
| 2708 | loopvars->lvs_ga.ga_len = endloop->end_var_count; |
| 2709 | stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len); |
| 2710 | loopvars->lvs_ga.ga_data = stack; |
| 2711 | if (stack == NULL) |
| 2712 | { |
| 2713 | vim_free(loopvars); |
| 2714 | return FAIL; |
| 2715 | } |
| 2716 | add_loopvars_to_list(loopvars); |
| 2717 | |
| 2718 | // Move the variable values. |
| 2719 | for (idx = 0; idx < endloop->end_var_count; ++idx) |
| 2720 | { |
| 2721 | typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx); |
| 2722 | |
| 2723 | *(stack + idx) = *tv; |
| 2724 | tv->v_type = VAR_UNKNOWN; |
| 2725 | } |
| 2726 | |
| 2727 | for (idx = prev_closure_count; idx < gap->ga_len; ++idx) |
| 2728 | { |
| 2729 | partial_T *pt = ((partial_T **)gap->ga_data)[idx]; |
| 2730 | |
Bram Moolenaar | dbbb02b | 2022-09-18 12:00:21 +0100 | [diff] [blame] | 2731 | if (pt->pt_refcount > 1 && pt->pt_loopvars == NULL) |
Bram Moolenaar | 0cdfb7c | 2022-09-17 15:44:52 +0100 | [diff] [blame] | 2732 | { |
| 2733 | ++loopvars->lvs_refcount; |
| 2734 | pt->pt_loopvars = loopvars; |
| 2735 | |
| 2736 | pt->pt_outer.out_loop_stack = &loopvars->lvs_ga; |
| 2737 | pt->pt_outer.out_loop_var_idx -= ectx->ec_frame_idx |
| 2738 | + STACK_FRAME_SIZE + endloop->end_var_idx; |
| 2739 | } |
| 2740 | } |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2741 | |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2742 | return OK; |
| 2743 | } |
| 2744 | |
| 2745 | /* |
Bram Moolenaar | 0cdfb7c | 2022-09-17 15:44:52 +0100 | [diff] [blame] | 2746 | * Called when a partial is freed or its reference count goes down to one. The |
| 2747 | * loopvars may be the only reference to the partials in the local variables. |
| 2748 | * Go over all of them, the funcref and can be freed if all partials |
| 2749 | * referencing the loopvars have a reference count of one. |
| 2750 | */ |
| 2751 | void |
| 2752 | loopvars_check_refcount(loopvars_T *loopvars) |
| 2753 | { |
| 2754 | int i; |
| 2755 | garray_T *gap = &loopvars->lvs_ga; |
| 2756 | int done = 0; |
| 2757 | |
| 2758 | if (loopvars->lvs_refcount > loopvars->lvs_min_refcount) |
| 2759 | return; |
| 2760 | for (i = 0; i < gap->ga_len; ++i) |
| 2761 | { |
| 2762 | typval_T *tv = ((typval_T *)gap->ga_data) + i; |
| 2763 | |
| 2764 | if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL |
| 2765 | && tv->vval.v_partial->pt_loopvars == loopvars |
| 2766 | && tv->vval.v_partial->pt_refcount == 1) |
| 2767 | ++done; |
| 2768 | } |
| 2769 | if (done == loopvars->lvs_min_refcount) |
| 2770 | { |
| 2771 | typval_T *stack = gap->ga_data; |
| 2772 | |
| 2773 | // All partials referencing the loopvars have a reference count of |
| 2774 | // one, thus the loopvars is no longer of use. |
| 2775 | for (i = 0; i < gap->ga_len; ++i) |
| 2776 | clear_tv(stack + i); |
| 2777 | vim_free(stack); |
| 2778 | remove_loopvars_from_list(loopvars); |
| 2779 | vim_free(loopvars); |
| 2780 | } |
| 2781 | } |
| 2782 | |
| 2783 | /* |
| 2784 | * For garbage collecting: set references in all variables referenced by |
| 2785 | * all loopvars. |
| 2786 | */ |
| 2787 | int |
| 2788 | set_ref_in_loopvars(int copyID) |
| 2789 | { |
| 2790 | loopvars_T *loopvars; |
| 2791 | |
| 2792 | for (loopvars = first_loopvars; loopvars != NULL; |
| 2793 | loopvars = loopvars->lvs_next) |
| 2794 | { |
| 2795 | typval_T *stack = loopvars->lvs_ga.ga_data; |
| 2796 | int i; |
| 2797 | |
| 2798 | for (i = 0; i < loopvars->lvs_ga.ga_len; ++i) |
| 2799 | if (set_ref_in_item(stack + i, copyID, NULL, NULL)) |
| 2800 | return TRUE; // abort |
| 2801 | } |
| 2802 | return FALSE; |
| 2803 | } |
| 2804 | |
| 2805 | /* |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2806 | * Load instruction for w:/b:/g:/t: variable. |
| 2807 | * "isn_type" is used instead of "iptr->isn_type". |
| 2808 | */ |
| 2809 | static int |
| 2810 | load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr) |
| 2811 | { |
| 2812 | dictitem_T *di = NULL; |
| 2813 | hashtab_T *ht = NULL; |
| 2814 | char namespace; |
| 2815 | |
| 2816 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
| 2817 | return NOTDONE; |
| 2818 | switch (isn_type) |
| 2819 | { |
| 2820 | case ISN_LOADG: |
| 2821 | ht = get_globvar_ht(); |
| 2822 | namespace = 'g'; |
| 2823 | break; |
| 2824 | case ISN_LOADB: |
| 2825 | ht = &curbuf->b_vars->dv_hashtab; |
| 2826 | namespace = 'b'; |
| 2827 | break; |
| 2828 | case ISN_LOADW: |
| 2829 | ht = &curwin->w_vars->dv_hashtab; |
| 2830 | namespace = 'w'; |
| 2831 | break; |
| 2832 | case ISN_LOADT: |
| 2833 | ht = &curtab->tp_vars->dv_hashtab; |
| 2834 | namespace = 't'; |
| 2835 | break; |
| 2836 | default: // Cannot reach here |
| 2837 | return NOTDONE; |
| 2838 | } |
| 2839 | di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); |
| 2840 | |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2841 | if (di == NULL) |
| 2842 | { |
Bram Moolenaar | fe73255 | 2022-02-22 19:39:13 +0000 | [diff] [blame] | 2843 | if (isn_type == ISN_LOADG) |
| 2844 | { |
| 2845 | ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE); |
| 2846 | |
| 2847 | // g:Something could be a function |
| 2848 | if (ufunc != NULL) |
| 2849 | { |
| 2850 | typval_T *tv = STACK_TV_BOT(0); |
| 2851 | |
| 2852 | ++ectx->ec_stack.ga_len; |
| 2853 | tv->v_type = VAR_FUNC; |
| 2854 | tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3); |
| 2855 | if (tv->vval.v_string == NULL) |
| 2856 | return FAIL; |
| 2857 | STRCPY(tv->vval.v_string, "g:"); |
| 2858 | STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string); |
| 2859 | return OK; |
| 2860 | } |
| 2861 | } |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2862 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | fe73255 | 2022-02-22 19:39:13 +0000 | [diff] [blame] | 2863 | if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL) |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2864 | // no check if the item exists in the script but |
| 2865 | // isn't exported, it is too complicated |
Bram Moolenaar | fe73255 | 2022-02-22 19:39:13 +0000 | [diff] [blame] | 2866 | semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string); |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2867 | else |
| 2868 | semsg(_(e_undefined_variable_char_str), |
Bram Moolenaar | fe73255 | 2022-02-22 19:39:13 +0000 | [diff] [blame] | 2869 | namespace, iptr->isn_arg.string); |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2870 | return FAIL; |
| 2871 | } |
| 2872 | else |
| 2873 | { |
| 2874 | copy_tv(&di->di_tv, STACK_TV_BOT(0)); |
| 2875 | ++ectx->ec_stack.ga_len; |
| 2876 | } |
| 2877 | return OK; |
| 2878 | } |
| 2879 | |
| 2880 | /* |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2881 | * Execute instructions in execution context "ectx". |
| 2882 | * Return OK or FAIL; |
| 2883 | */ |
| 2884 | static int |
| 2885 | exec_instructions(ectx_T *ectx) |
| 2886 | { |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2887 | int ret = FAIL; |
| 2888 | int save_trylevel_at_start = ectx->ec_trylevel_at_start; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 2889 | int dict_stack_len_at_start = dict_stack.ga_len; |
Bram Moolenaar | f785aa1 | 2021-02-11 21:19:34 +0100 | [diff] [blame] | 2890 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 2891 | // Start execution at the first instruction. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2892 | ectx->ec_iidx = 0; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 2893 | |
Bram Moolenaar | ff65288 | 2021-05-16 15:24:49 +0200 | [diff] [blame] | 2894 | // Only catch exceptions in this instruction list. |
| 2895 | ectx->ec_trylevel_at_start = trylevel; |
| 2896 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2897 | for (;;) |
| 2898 | { |
Bram Moolenaar | a749019 | 2021-07-22 12:26:14 +0200 | [diff] [blame] | 2899 | static int breakcheck_count = 0; // using "static" makes it faster |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2900 | isn_T *iptr; |
Bram Moolenaar | a749019 | 2021-07-22 12:26:14 +0200 | [diff] [blame] | 2901 | typval_T *tv; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2902 | |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 2903 | if (unlikely(++breakcheck_count >= 100)) |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 2904 | { |
| 2905 | line_breakcheck(); |
| 2906 | breakcheck_count = 0; |
| 2907 | } |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 2908 | if (unlikely(got_int)) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2909 | { |
| 2910 | // Turn CTRL-C into an exception. |
| 2911 | got_int = FALSE; |
Bram Moolenaar | 97acfc7 | 2020-03-22 13:44:28 +0100 | [diff] [blame] | 2912 | if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2913 | goto theend; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2914 | did_throw = TRUE; |
| 2915 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2916 | |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 2917 | if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL)) |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2918 | { |
| 2919 | // Turn an error message into an exception. |
| 2920 | did_emsg = FALSE; |
| 2921 | if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2922 | goto theend; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2923 | did_throw = TRUE; |
| 2924 | *msg_list = NULL; |
| 2925 | } |
| 2926 | |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 2927 | if (unlikely(did_throw)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2928 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2929 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2930 | trycmd_T *trycmd = NULL; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 2931 | int index = trystack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2932 | |
| 2933 | // An exception jumps to the first catch, finally, or returns from |
| 2934 | // the current function. |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 2935 | while (index > 0) |
| 2936 | { |
| 2937 | trycmd = ((trycmd_T *)trystack->ga_data) + index - 1; |
Bram Moolenaar | 834193a | 2021-06-30 20:39:15 +0200 | [diff] [blame] | 2938 | if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0) |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 2939 | break; |
| 2940 | // In the catch and finally block of this try we have to go up |
| 2941 | // one level. |
| 2942 | --index; |
| 2943 | trycmd = NULL; |
| 2944 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2945 | if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2946 | { |
Bram Moolenaar | 834193a | 2021-06-30 20:39:15 +0200 | [diff] [blame] | 2947 | if (trycmd->tcd_in_catch) |
| 2948 | { |
| 2949 | // exception inside ":catch", jump to ":finally" once |
| 2950 | ectx->ec_iidx = trycmd->tcd_finally_idx; |
| 2951 | trycmd->tcd_finally_idx = 0; |
| 2952 | } |
| 2953 | else |
| 2954 | // jump to first ":catch" |
| 2955 | ectx->ec_iidx = trycmd->tcd_catch_idx; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 2956 | trycmd->tcd_in_catch = TRUE; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 2957 | did_throw = FALSE; // don't come back here until :endtry |
| 2958 | trycmd->tcd_did_throw = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2959 | } |
| 2960 | else |
| 2961 | { |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 2962 | // Not inside try or need to return from current functions. |
| 2963 | // Push a dummy return value. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2964 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2965 | goto theend; |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 2966 | tv = STACK_TV_BOT(0); |
| 2967 | tv->v_type = VAR_NUMBER; |
| 2968 | tv->vval.v_number = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2969 | ++ectx->ec_stack.ga_len; |
| 2970 | if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2971 | { |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 2972 | // At the toplevel we are done. |
Bram Moolenaar | 257cc5e | 2020-02-19 17:06:11 +0100 | [diff] [blame] | 2973 | need_rethrow = TRUE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2974 | if (handle_closure_in_use(ectx, FALSE) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2975 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2976 | goto done; |
| 2977 | } |
| 2978 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2979 | if (func_return(ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2980 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2981 | } |
| 2982 | continue; |
| 2983 | } |
| 2984 | |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2985 | /* |
| 2986 | * Big switch on the instruction. Most compilers will be turning this |
| 2987 | * into an efficient lookup table, since the "case" values are an enum |
| 2988 | * with sequential numbers. It may look ugly, but it should be the |
| 2989 | * most efficient way. |
| 2990 | */ |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2991 | iptr = &ectx->ec_instr[ectx->ec_iidx++]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2992 | switch (iptr->isn_type) |
| 2993 | { |
| 2994 | // execute Ex command line |
| 2995 | case ISN_EXEC: |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 2996 | if (exec_command(iptr) == FAIL) |
| 2997 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2998 | break; |
| 2999 | |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 3000 | // execute Ex command line split at NL characters. |
| 3001 | case ISN_EXEC_SPLIT: |
| 3002 | { |
| 3003 | source_cookie_T cookie; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 3004 | char_u *line; |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 3005 | |
| 3006 | SOURCING_LNUM = iptr->isn_lnum; |
| 3007 | CLEAR_FIELD(cookie); |
| 3008 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 3009 | cookie.nextline = iptr->isn_arg.string; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 3010 | line = get_split_sourceline(0, &cookie, 0, 0); |
| 3011 | if (do_cmdline(line, |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 3012 | get_split_sourceline, &cookie, |
| 3013 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) |
| 3014 | == FAIL |
| 3015 | || did_emsg) |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 3016 | { |
| 3017 | vim_free(line); |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 3018 | goto on_error; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 3019 | } |
| 3020 | vim_free(line); |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 3021 | } |
| 3022 | break; |
| 3023 | |
Bram Moolenaar | e4eed8c | 2021-12-01 15:22:56 +0000 | [diff] [blame] | 3024 | // execute Ex command line that is only a range |
| 3025 | case ISN_EXECRANGE: |
| 3026 | { |
| 3027 | exarg_T ea; |
| 3028 | char *error = NULL; |
| 3029 | |
| 3030 | CLEAR_FIELD(ea); |
| 3031 | ea.cmdidx = CMD_SIZE; |
| 3032 | ea.addr_type = ADDR_LINES; |
| 3033 | ea.cmd = iptr->isn_arg.string; |
Bram Moolenaar | 0c7f261 | 2022-02-17 19:44:07 +0000 | [diff] [blame] | 3034 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | e4eed8c | 2021-12-01 15:22:56 +0000 | [diff] [blame] | 3035 | parse_cmd_address(&ea, &error, FALSE); |
Bram Moolenaar | 01a4dcb | 2021-12-04 13:15:10 +0000 | [diff] [blame] | 3036 | if (ea.cmd == NULL) |
| 3037 | goto on_error; |
Bram Moolenaar | 0c7f261 | 2022-02-17 19:44:07 +0000 | [diff] [blame] | 3038 | // error is always NULL when using ADDR_LINES |
| 3039 | error = ex_range_without_command(&ea); |
Bram Moolenaar | e4eed8c | 2021-12-01 15:22:56 +0000 | [diff] [blame] | 3040 | if (error != NULL) |
| 3041 | { |
Bram Moolenaar | e4eed8c | 2021-12-01 15:22:56 +0000 | [diff] [blame] | 3042 | emsg(error); |
| 3043 | goto on_error; |
| 3044 | } |
| 3045 | } |
| 3046 | break; |
| 3047 | |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 3048 | // Evaluate an expression with legacy syntax, push it onto the |
| 3049 | // stack. |
| 3050 | case ISN_LEGACY_EVAL: |
| 3051 | { |
| 3052 | char_u *arg = iptr->isn_arg.string; |
| 3053 | int res; |
| 3054 | int save_flags = cmdmod.cmod_flags; |
| 3055 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3056 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3057 | goto theend; |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 3058 | tv = STACK_TV_BOT(0); |
| 3059 | init_tv(tv); |
| 3060 | cmdmod.cmod_flags |= CMOD_LEGACY; |
| 3061 | res = eval0(arg, tv, NULL, &EVALARG_EVALUATE); |
| 3062 | cmdmod.cmod_flags = save_flags; |
| 3063 | if (res == FAIL) |
| 3064 | goto on_error; |
| 3065 | ++ectx->ec_stack.ga_len; |
| 3066 | } |
| 3067 | break; |
| 3068 | |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 3069 | // push typeval VAR_INSTR with instructions to be executed |
| 3070 | case ISN_INSTR: |
| 3071 | { |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3072 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3073 | goto theend; |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 3074 | tv = STACK_TV_BOT(0); |
| 3075 | tv->vval.v_instr = ALLOC_ONE(instr_T); |
| 3076 | if (tv->vval.v_instr == NULL) |
| 3077 | goto on_error; |
| 3078 | ++ectx->ec_stack.ga_len; |
| 3079 | |
| 3080 | tv->v_type = VAR_INSTR; |
| 3081 | tv->vval.v_instr->instr_ectx = ectx; |
| 3082 | tv->vval.v_instr->instr_instr = iptr->isn_arg.instr; |
| 3083 | } |
| 3084 | break; |
| 3085 | |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3086 | case ISN_SOURCE: |
| 3087 | { |
Bram Moolenaar | 8944551 | 2022-04-14 12:58:23 +0100 | [diff] [blame] | 3088 | int notused; |
LemonBoy | 7714231 | 2022-04-15 20:50:46 +0100 | [diff] [blame] | 3089 | |
Bram Moolenaar | 8944551 | 2022-04-14 12:58:23 +0100 | [diff] [blame] | 3090 | SOURCING_LNUM = iptr->isn_lnum; |
| 3091 | if (may_load_script((int)iptr->isn_arg.number, ¬used) |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3092 | == FAIL) |
Bram Moolenaar | 8944551 | 2022-04-14 12:58:23 +0100 | [diff] [blame] | 3093 | goto on_error; |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3094 | } |
| 3095 | break; |
| 3096 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3097 | // execute :substitute with an expression |
| 3098 | case ISN_SUBSTITUTE: |
| 3099 | { |
| 3100 | subs_T *subs = &iptr->isn_arg.subs; |
| 3101 | source_cookie_T cookie; |
| 3102 | struct subs_expr_S *save_instr = substitute_instr; |
| 3103 | struct subs_expr_S subs_instr; |
| 3104 | int res; |
| 3105 | |
| 3106 | subs_instr.subs_ectx = ectx; |
| 3107 | subs_instr.subs_instr = subs->subs_instr; |
| 3108 | subs_instr.subs_status = OK; |
| 3109 | substitute_instr = &subs_instr; |
| 3110 | |
| 3111 | SOURCING_LNUM = iptr->isn_lnum; |
| 3112 | // This is very much like ISN_EXEC |
| 3113 | CLEAR_FIELD(cookie); |
| 3114 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 3115 | res = do_cmdline(subs->subs_cmd, |
| 3116 | getsourceline, &cookie, |
| 3117 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); |
| 3118 | substitute_instr = save_instr; |
| 3119 | |
| 3120 | if (res == FAIL || did_emsg |
| 3121 | || subs_instr.subs_status == FAIL) |
| 3122 | goto on_error; |
| 3123 | } |
| 3124 | break; |
| 3125 | |
| 3126 | case ISN_FINISH: |
| 3127 | goto done; |
| 3128 | |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 3129 | case ISN_REDIRSTART: |
| 3130 | // create a dummy entry for var_redir_str() |
| 3131 | if (alloc_redir_lval() == FAIL) |
| 3132 | goto on_error; |
| 3133 | |
| 3134 | // The output is stored in growarray "redir_ga" until |
| 3135 | // redirection ends. |
| 3136 | init_redir_ga(); |
| 3137 | redir_vname = 1; |
| 3138 | break; |
| 3139 | |
| 3140 | case ISN_REDIREND: |
| 3141 | { |
| 3142 | char_u *res = get_clear_redir_ga(); |
| 3143 | |
| 3144 | // End redirection, put redirected text on the stack. |
| 3145 | clear_redir_lval(); |
| 3146 | redir_vname = 0; |
| 3147 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3148 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 3149 | { |
| 3150 | vim_free(res); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3151 | goto theend; |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 3152 | } |
| 3153 | tv = STACK_TV_BOT(0); |
| 3154 | tv->v_type = VAR_STRING; |
| 3155 | tv->vval.v_string = res; |
| 3156 | ++ectx->ec_stack.ga_len; |
| 3157 | } |
| 3158 | break; |
| 3159 | |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 3160 | case ISN_CEXPR_AUCMD: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 3161 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 3162 | force_abort = TRUE; |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 3163 | if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL) |
| 3164 | goto on_error; |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 3165 | force_abort = FALSE; |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 3166 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 3167 | break; |
| 3168 | |
| 3169 | case ISN_CEXPR_CORE: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 3170 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 3171 | { |
| 3172 | exarg_T ea; |
| 3173 | int res; |
| 3174 | |
| 3175 | CLEAR_FIELD(ea); |
| 3176 | ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx; |
| 3177 | ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit; |
| 3178 | ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline; |
| 3179 | --ectx->ec_stack.ga_len; |
| 3180 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | bd683e3 | 2022-07-18 17:49:03 +0100 | [diff] [blame] | 3181 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 3182 | res = cexpr_core(&ea, tv); |
| 3183 | clear_tv(tv); |
| 3184 | if (res == FAIL) |
| 3185 | goto on_error; |
| 3186 | } |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 3187 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 3188 | break; |
| 3189 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 3190 | // execute Ex command from pieces on the stack |
| 3191 | case ISN_EXECCONCAT: |
| 3192 | { |
| 3193 | int count = iptr->isn_arg.number; |
Bram Moolenaar | 7f6f56f | 2020-04-30 20:21:43 +0200 | [diff] [blame] | 3194 | size_t len = 0; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 3195 | int pass; |
| 3196 | int i; |
| 3197 | char_u *cmd = NULL; |
| 3198 | char_u *str; |
| 3199 | |
| 3200 | for (pass = 1; pass <= 2; ++pass) |
| 3201 | { |
| 3202 | for (i = 0; i < count; ++i) |
| 3203 | { |
| 3204 | tv = STACK_TV_BOT(i - count); |
| 3205 | str = tv->vval.v_string; |
| 3206 | if (str != NULL && *str != NUL) |
| 3207 | { |
| 3208 | if (pass == 2) |
| 3209 | STRCPY(cmd + len, str); |
| 3210 | len += STRLEN(str); |
| 3211 | } |
| 3212 | if (pass == 2) |
| 3213 | clear_tv(tv); |
| 3214 | } |
| 3215 | if (pass == 1) |
| 3216 | { |
| 3217 | cmd = alloc(len + 1); |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 3218 | if (unlikely(cmd == NULL)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3219 | goto theend; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 3220 | len = 0; |
| 3221 | } |
| 3222 | } |
| 3223 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 3224 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 3225 | do_cmdline_cmd(cmd); |
| 3226 | vim_free(cmd); |
| 3227 | } |
| 3228 | break; |
| 3229 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3230 | // execute :echo {string} ... |
| 3231 | case ISN_ECHO: |
| 3232 | { |
| 3233 | int count = iptr->isn_arg.echo.echo_count; |
| 3234 | int atstart = TRUE; |
| 3235 | int needclr = TRUE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3236 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3237 | |
| 3238 | for (idx = 0; idx < count; ++idx) |
| 3239 | { |
| 3240 | tv = STACK_TV_BOT(idx - count); |
| 3241 | echo_one(tv, iptr->isn_arg.echo.echo_with_white, |
| 3242 | &atstart, &needclr); |
| 3243 | clear_tv(tv); |
| 3244 | } |
Bram Moolenaar | e0807ea | 2020-02-20 22:18:06 +0100 | [diff] [blame] | 3245 | if (needclr) |
| 3246 | msg_clr_eos(); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3247 | ectx->ec_stack.ga_len -= count; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3248 | } |
| 3249 | break; |
| 3250 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 3251 | // :execute {string} ... |
| 3252 | // :echomsg {string} ... |
Bram Moolenaar | 7d7ad7b | 2022-09-01 16:00:53 +0100 | [diff] [blame] | 3253 | // :echowindow {string} ... |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 3254 | // :echoconsole {string} ... |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 3255 | // :echoerr {string} ... |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 3256 | case ISN_EXECUTE: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 3257 | case ISN_ECHOMSG: |
Bram Moolenaar | 7d7ad7b | 2022-09-01 16:00:53 +0100 | [diff] [blame] | 3258 | case ISN_ECHOWINDOW: |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 3259 | case ISN_ECHOCONSOLE: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 3260 | case ISN_ECHOERR: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 3261 | { |
| 3262 | int count = iptr->isn_arg.number; |
| 3263 | garray_T ga; |
| 3264 | char_u buf[NUMBUFLEN]; |
| 3265 | char_u *p; |
| 3266 | int len; |
| 3267 | int failed = FALSE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3268 | int idx; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 3269 | |
| 3270 | ga_init2(&ga, 1, 80); |
| 3271 | for (idx = 0; idx < count; ++idx) |
| 3272 | { |
| 3273 | tv = STACK_TV_BOT(idx - count); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 3274 | if (iptr->isn_type == ISN_EXECUTE) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 3275 | { |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 3276 | if (tv->v_type == VAR_CHANNEL |
| 3277 | || tv->v_type == VAR_JOB) |
| 3278 | { |
| 3279 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 68db996 | 2021-05-09 23:19:22 +0200 | [diff] [blame] | 3280 | semsg(_(e_using_invalid_value_as_string_str), |
| 3281 | vartype_name(tv->v_type)); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 3282 | break; |
| 3283 | } |
| 3284 | else |
| 3285 | p = tv_get_string_buf(tv, buf); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 3286 | } |
| 3287 | else |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 3288 | p = tv_stringify(tv, buf); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 3289 | |
| 3290 | len = (int)STRLEN(p); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3291 | if (GA_GROW_FAILS(&ga, len + 2)) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 3292 | failed = TRUE; |
| 3293 | else |
| 3294 | { |
| 3295 | if (ga.ga_len > 0) |
| 3296 | ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; |
| 3297 | STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); |
| 3298 | ga.ga_len += len; |
| 3299 | } |
| 3300 | clear_tv(tv); |
| 3301 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3302 | ectx->ec_stack.ga_len -= count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 3303 | if (failed) |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 3304 | { |
| 3305 | ga_clear(&ga); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 3306 | goto on_error; |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 3307 | } |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 3308 | |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 3309 | if (ga.ga_data != NULL) |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 3310 | { |
| 3311 | if (iptr->isn_type == ISN_EXECUTE) |
Bram Moolenaar | 430deb1 | 2020-08-23 16:29:11 +0200 | [diff] [blame] | 3312 | { |
| 3313 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 3314 | do_cmdline_cmd((char_u *)ga.ga_data); |
Bram Moolenaar | eeece9e | 2020-11-20 19:26:48 +0100 | [diff] [blame] | 3315 | if (did_emsg) |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 3316 | { |
| 3317 | ga_clear(&ga); |
Bram Moolenaar | eeece9e | 2020-11-20 19:26:48 +0100 | [diff] [blame] | 3318 | goto on_error; |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 3319 | } |
Bram Moolenaar | 430deb1 | 2020-08-23 16:29:11 +0200 | [diff] [blame] | 3320 | } |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 3321 | else |
| 3322 | { |
| 3323 | msg_sb_eol(); |
| 3324 | if (iptr->isn_type == ISN_ECHOMSG) |
| 3325 | { |
| 3326 | msg_attr(ga.ga_data, echo_attr); |
| 3327 | out_flush(); |
| 3328 | } |
Bram Moolenaar | 7d7ad7b | 2022-09-01 16:00:53 +0100 | [diff] [blame] | 3329 | #ifdef HAS_MESSAGE_WINDOW |
| 3330 | else if (iptr->isn_type == ISN_ECHOWINDOW) |
| 3331 | { |
| 3332 | start_echowindow(); |
| 3333 | msg_attr(ga.ga_data, echo_attr); |
| 3334 | end_echowindow(); |
| 3335 | } |
| 3336 | #endif |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 3337 | else if (iptr->isn_type == ISN_ECHOCONSOLE) |
| 3338 | { |
| 3339 | ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), |
| 3340 | TRUE); |
| 3341 | ui_write((char_u *)"\r\n", 2, TRUE); |
| 3342 | } |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 3343 | else |
| 3344 | { |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 3345 | SOURCING_LNUM = iptr->isn_lnum; |
| 3346 | emsg(ga.ga_data); |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 3347 | } |
| 3348 | } |
| 3349 | } |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 3350 | ga_clear(&ga); |
| 3351 | } |
| 3352 | break; |
| 3353 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3354 | // load local variable or argument |
| 3355 | case ISN_LOAD: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3356 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3357 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3358 | copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3359 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3360 | break; |
| 3361 | |
| 3362 | // load v: variable |
| 3363 | case ISN_LOADV: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3364 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3365 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3366 | copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3367 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3368 | break; |
| 3369 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3370 | // load s: variable in Vim9 script |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3371 | case ISN_LOADSCRIPT: |
| 3372 | { |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 3373 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3374 | svar_T *sv; |
| 3375 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 3376 | sv = get_script_svar(sref, ectx->ec_dfunc_idx); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 3377 | if (sv == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3378 | goto theend; |
Bram Moolenaar | 859cc21 | 2022-03-28 15:22:35 +0100 | [diff] [blame] | 3379 | allocate_if_null(sv); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3380 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3381 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3382 | copy_tv(sv->sv_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3383 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3384 | } |
| 3385 | break; |
| 3386 | |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3387 | // load s: variable in old script or autoload import |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3388 | case ISN_LOADS: |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3389 | case ISN_LOADEXPORT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3390 | { |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3391 | int sid = iptr->isn_arg.loadstore.ls_sid; |
| 3392 | hashtab_T *ht = &SCRIPT_VARS(sid); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3393 | char_u *name = iptr->isn_arg.loadstore.ls_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3394 | dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 3395 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3396 | if (di == NULL) |
| 3397 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3398 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3399 | semsg(_(e_undefined_variable_str), name); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 3400 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3401 | } |
| 3402 | else |
| 3403 | { |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3404 | if (iptr->isn_type == ISN_LOADEXPORT) |
| 3405 | { |
| 3406 | int idx = get_script_item_idx(sid, name, 0, |
| 3407 | NULL, NULL); |
| 3408 | svar_T *sv; |
| 3409 | |
| 3410 | if (idx >= 0) |
| 3411 | { |
| 3412 | sv = ((svar_T *)SCRIPT_ITEM(sid) |
| 3413 | ->sn_var_vals.ga_data) + idx; |
Bram Moolenaar | aa7d0c2 | 2022-04-05 21:40:38 +0100 | [diff] [blame] | 3414 | if ((sv->sv_flags & SVFLAG_EXPORTED) == 0) |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3415 | { |
| 3416 | SOURCING_LNUM = iptr->isn_lnum; |
| 3417 | semsg(_(e_item_not_exported_in_script_str), |
| 3418 | name); |
| 3419 | goto on_error; |
| 3420 | } |
| 3421 | } |
| 3422 | } |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3423 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3424 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3425 | copy_tv(&di->di_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3426 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3427 | } |
| 3428 | } |
| 3429 | break; |
| 3430 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 3431 | // load g:/b:/w:/t: variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3432 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 3433 | case ISN_LOADB: |
| 3434 | case ISN_LOADW: |
| 3435 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3436 | { |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 3437 | int res = load_namespace_var(ectx, iptr->isn_type, iptr); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3438 | |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 3439 | if (res == NOTDONE) |
Bram Moolenaar | cbbc48f | 2022-01-18 12:58:28 +0000 | [diff] [blame] | 3440 | goto theend; |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 3441 | if (res == FAIL) |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 3442 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3443 | } |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 3444 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3445 | break; |
| 3446 | |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 3447 | // load autoload variable |
| 3448 | case ISN_LOADAUTO: |
| 3449 | { |
| 3450 | char_u *name = iptr->isn_arg.string; |
| 3451 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3452 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3453 | goto theend; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 3454 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 3455 | if (eval_variable(name, (int)STRLEN(name), 0, |
Bram Moolenaar | cb4e80f | 2021-03-13 20:57:19 +0100 | [diff] [blame] | 3456 | STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL) |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 3457 | goto on_error; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3458 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 3459 | } |
| 3460 | break; |
| 3461 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3462 | // load g:/b:/w:/t: namespace |
| 3463 | case ISN_LOADGDICT: |
| 3464 | case ISN_LOADBDICT: |
| 3465 | case ISN_LOADWDICT: |
| 3466 | case ISN_LOADTDICT: |
| 3467 | { |
| 3468 | dict_T *d = NULL; |
| 3469 | |
| 3470 | switch (iptr->isn_type) |
| 3471 | { |
Bram Moolenaar | 682d0a1 | 2020-07-19 20:48:59 +0200 | [diff] [blame] | 3472 | case ISN_LOADGDICT: d = get_globvar_dict(); break; |
| 3473 | case ISN_LOADBDICT: d = curbuf->b_vars; break; |
| 3474 | case ISN_LOADWDICT: d = curwin->w_vars; break; |
| 3475 | case ISN_LOADTDICT: d = curtab->tp_vars; break; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3476 | default: // Cannot reach here |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3477 | goto theend; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3478 | } |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3479 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3480 | goto theend; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3481 | tv = STACK_TV_BOT(0); |
| 3482 | tv->v_type = VAR_DICT; |
| 3483 | tv->v_lock = 0; |
| 3484 | tv->vval.v_dict = d; |
Bram Moolenaar | 1bd3cb2 | 2021-02-24 12:27:31 +0100 | [diff] [blame] | 3485 | ++d->dv_refcount; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3486 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3487 | } |
| 3488 | break; |
| 3489 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3490 | // load &option |
| 3491 | case ISN_LOADOPT: |
| 3492 | { |
| 3493 | typval_T optval; |
| 3494 | char_u *name = iptr->isn_arg.string; |
| 3495 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 3496 | // This is not expected to fail, name is checked during |
| 3497 | // compilation: don't set SOURCING_LNUM. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3498 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3499 | goto theend; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3500 | if (eval_option(&name, &optval, TRUE) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3501 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3502 | *STACK_TV_BOT(0) = optval; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3503 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3504 | } |
| 3505 | break; |
| 3506 | |
| 3507 | // load $ENV |
| 3508 | case ISN_LOADENV: |
| 3509 | { |
| 3510 | typval_T optval; |
| 3511 | char_u *name = iptr->isn_arg.string; |
| 3512 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3513 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3514 | goto theend; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 3515 | // name is always valid, checked when compiling |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3516 | (void)eval_env_var(&name, &optval, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3517 | *STACK_TV_BOT(0) = optval; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3518 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3519 | } |
| 3520 | break; |
| 3521 | |
| 3522 | // load @register |
| 3523 | case ISN_LOADREG: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3524 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3525 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3526 | tv = STACK_TV_BOT(0); |
| 3527 | tv->v_type = VAR_STRING; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3528 | tv->v_lock = 0; |
Bram Moolenaar | 1e021e6 | 2020-10-16 20:25:23 +0200 | [diff] [blame] | 3529 | // This may result in NULL, which should be equivalent to an |
| 3530 | // empty string. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3531 | tv->vval.v_string = get_reg_contents( |
| 3532 | iptr->isn_arg.number, GREG_EXPR_SRC); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3533 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3534 | break; |
| 3535 | |
| 3536 | // store local variable |
| 3537 | case ISN_STORE: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3538 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3539 | tv = STACK_TV_VAR(iptr->isn_arg.number); |
| 3540 | clear_tv(tv); |
| 3541 | *tv = *STACK_TV_BOT(0); |
| 3542 | break; |
| 3543 | |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3544 | // store s: variable in old script or autoload import |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3545 | case ISN_STORES: |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3546 | case ISN_STOREEXPORT: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3547 | { |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3548 | int sid = iptr->isn_arg.loadstore.ls_sid; |
| 3549 | hashtab_T *ht = &SCRIPT_VARS(sid); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3550 | char_u *name = iptr->isn_arg.loadstore.ls_name; |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3551 | dictitem_T *di = find_var_in_ht(ht, 0, |
| 3552 | iptr->isn_type == ISN_STORES |
| 3553 | ? name + 2 : name, TRUE); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3554 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3555 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3556 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 3557 | if (di == NULL) |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3558 | { |
| 3559 | if (iptr->isn_type == ISN_STOREEXPORT) |
| 3560 | { |
| 3561 | semsg(_(e_undefined_variable_str), name); |
Bram Moolenaar | d1d2684 | 2022-03-31 10:13:47 +0100 | [diff] [blame] | 3562 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3563 | goto on_error; |
| 3564 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3565 | store_var(name, STACK_TV_BOT(0)); |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3566 | } |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 3567 | else |
| 3568 | { |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3569 | if (iptr->isn_type == ISN_STOREEXPORT) |
| 3570 | { |
| 3571 | int idx = get_script_item_idx(sid, name, 0, |
| 3572 | NULL, NULL); |
| 3573 | |
Bram Moolenaar | 0665163 | 2022-04-27 17:54:25 +0100 | [diff] [blame] | 3574 | // can this ever fail? |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3575 | if (idx >= 0) |
| 3576 | { |
| 3577 | svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid) |
| 3578 | ->sn_var_vals.ga_data) + idx; |
| 3579 | |
Bram Moolenaar | aa7d0c2 | 2022-04-05 21:40:38 +0100 | [diff] [blame] | 3580 | if ((sv->sv_flags & SVFLAG_EXPORTED) == 0) |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3581 | { |
| 3582 | semsg(_(e_item_not_exported_in_script_str), |
| 3583 | name); |
Bram Moolenaar | d1d2684 | 2022-03-31 10:13:47 +0100 | [diff] [blame] | 3584 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 3585 | goto on_error; |
| 3586 | } |
| 3587 | } |
| 3588 | } |
Bram Moolenaar | dcf29ac | 2021-04-02 14:44:02 +0200 | [diff] [blame] | 3589 | if (var_check_permission(di, name) == FAIL) |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 3590 | { |
| 3591 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | dcf29ac | 2021-04-02 14:44:02 +0200 | [diff] [blame] | 3592 | goto on_error; |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 3593 | } |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 3594 | clear_tv(&di->di_tv); |
| 3595 | di->di_tv = *STACK_TV_BOT(0); |
| 3596 | } |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3597 | } |
| 3598 | break; |
| 3599 | |
| 3600 | // store script-local variable in Vim9 script |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3601 | case ISN_STORESCRIPT: |
| 3602 | { |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 3603 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 3604 | svar_T *sv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3605 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 3606 | sv = get_script_svar(sref, ectx->ec_dfunc_idx); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 3607 | if (sv == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3608 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3609 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 3610 | |
| 3611 | // "const" and "final" are checked at compile time, locking |
| 3612 | // the value needs to be checked here. |
| 3613 | SOURCING_LNUM = iptr->isn_lnum; |
| 3614 | if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE)) |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 3615 | { |
| 3616 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 3617 | goto on_error; |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 3618 | } |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 3619 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3620 | clear_tv(sv->sv_tv); |
| 3621 | *sv->sv_tv = *STACK_TV_BOT(0); |
| 3622 | } |
| 3623 | break; |
| 3624 | |
| 3625 | // store option |
| 3626 | case ISN_STOREOPT: |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3627 | case ISN_STOREFUNCOPT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3628 | { |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3629 | char_u *opt_name = iptr->isn_arg.storeopt.so_name; |
| 3630 | int opt_flags = iptr->isn_arg.storeopt.so_flags; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3631 | long n = 0; |
| 3632 | char_u *s = NULL; |
| 3633 | char *msg; |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 3634 | char_u numbuf[NUMBUFLEN]; |
| 3635 | char_u *tofree = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3636 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3637 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3638 | tv = STACK_TV_BOT(0); |
| 3639 | if (tv->v_type == VAR_STRING) |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 3640 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3641 | s = tv->vval.v_string; |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 3642 | if (s == NULL) |
| 3643 | s = (char_u *)""; |
| 3644 | } |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3645 | else if (iptr->isn_type == ISN_STOREFUNCOPT) |
| 3646 | { |
| 3647 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 3648 | // If the option can be set to a function reference or |
| 3649 | // a lambda and the passed value is a function |
| 3650 | // reference, then convert it to the name (string) of |
| 3651 | // the function reference. |
| 3652 | s = tv2string(tv, &tofree, numbuf, 0); |
| 3653 | if (s == NULL || *s == NUL) |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3654 | { |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 3655 | // cannot happen? |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3656 | clear_tv(tv); |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 3657 | vim_free(tofree); |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3658 | goto on_error; |
| 3659 | } |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3660 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3661 | else |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3662 | // must be VAR_NUMBER, CHECKTYPE makes sure |
| 3663 | n = tv->vval.v_number; |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3664 | msg = set_option_value(opt_name, n, s, opt_flags); |
Bram Moolenaar | e75ba26 | 2020-05-16 15:43:31 +0200 | [diff] [blame] | 3665 | clear_tv(tv); |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 3666 | vim_free(tofree); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3667 | if (msg != NULL) |
| 3668 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3669 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3670 | emsg(_(msg)); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3671 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3672 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3673 | } |
| 3674 | break; |
| 3675 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3676 | // store $ENV |
| 3677 | case ISN_STOREENV: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3678 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3679 | tv = STACK_TV_BOT(0); |
| 3680 | vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); |
| 3681 | clear_tv(tv); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3682 | break; |
| 3683 | |
| 3684 | // store @r |
| 3685 | case ISN_STOREREG: |
| 3686 | { |
| 3687 | int reg = iptr->isn_arg.number; |
| 3688 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3689 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 3690 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 74f4a96 | 2021-06-17 21:03:07 +0200 | [diff] [blame] | 3691 | write_reg_contents(reg, tv_get_string(tv), -1, FALSE); |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 3692 | clear_tv(tv); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3693 | } |
| 3694 | break; |
| 3695 | |
| 3696 | // store v: variable |
| 3697 | case ISN_STOREV: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3698 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3699 | if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) |
| 3700 | == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3701 | // should not happen, type is checked when compiling |
| 3702 | goto on_error; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3703 | break; |
| 3704 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 3705 | // store g:/b:/w:/t: variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3706 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 3707 | case ISN_STOREB: |
| 3708 | case ISN_STOREW: |
| 3709 | case ISN_STORET: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3710 | { |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 3711 | dictitem_T *di; |
| 3712 | hashtab_T *ht; |
| 3713 | char_u *name = iptr->isn_arg.string + 2; |
| 3714 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 3715 | switch (iptr->isn_type) |
| 3716 | { |
| 3717 | case ISN_STOREG: |
| 3718 | ht = get_globvar_ht(); |
| 3719 | break; |
| 3720 | case ISN_STOREB: |
| 3721 | ht = &curbuf->b_vars->dv_hashtab; |
| 3722 | break; |
| 3723 | case ISN_STOREW: |
| 3724 | ht = &curwin->w_vars->dv_hashtab; |
| 3725 | break; |
| 3726 | case ISN_STORET: |
| 3727 | ht = &curtab->tp_vars->dv_hashtab; |
| 3728 | break; |
| 3729 | default: // Cannot reach here |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3730 | goto theend; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 3731 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3732 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3733 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 3734 | di = find_var_in_ht(ht, 0, name, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3735 | if (di == NULL) |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 3736 | store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3737 | else |
| 3738 | { |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 3739 | SOURCING_LNUM = iptr->isn_lnum; |
| 3740 | if (var_check_permission(di, name) == FAIL) |
| 3741 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3742 | clear_tv(&di->di_tv); |
| 3743 | di->di_tv = *STACK_TV_BOT(0); |
| 3744 | } |
| 3745 | } |
| 3746 | break; |
| 3747 | |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 3748 | // store an autoload variable |
| 3749 | case ISN_STOREAUTO: |
| 3750 | SOURCING_LNUM = iptr->isn_lnum; |
| 3751 | set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE); |
| 3752 | clear_tv(STACK_TV_BOT(-1)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3753 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 3754 | break; |
| 3755 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3756 | // store number in local variable |
| 3757 | case ISN_STORENR: |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 3758 | tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3759 | clear_tv(tv); |
| 3760 | tv->v_type = VAR_NUMBER; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 3761 | tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3762 | break; |
| 3763 | |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 3764 | // store value in list or dict variable |
| 3765 | case ISN_STOREINDEX: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3766 | { |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3767 | int res = execute_storeindex(iptr, ectx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3768 | |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3769 | if (res == FAIL) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 3770 | goto on_error; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3771 | if (res == NOTDONE) |
| 3772 | goto theend; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3773 | } |
| 3774 | break; |
| 3775 | |
Bram Moolenaar | ea5c898 | 2022-02-17 14:42:02 +0000 | [diff] [blame] | 3776 | // store value in list or blob range |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 3777 | case ISN_STORERANGE: |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3778 | if (execute_storerange(iptr, ectx) == FAIL) |
| 3779 | goto on_error; |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 3780 | break; |
| 3781 | |
Bram Moolenaar | 0cdfb7c | 2022-09-17 15:44:52 +0100 | [diff] [blame] | 3782 | // Load or store variable or argument from outer scope. |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3783 | case ISN_LOADOUTER: |
| 3784 | case ISN_STOREOUTER: |
| 3785 | { |
| 3786 | int depth = iptr->isn_arg.outer.outer_depth; |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 3787 | outer_T *outer = ectx->ec_outer_ref == NULL ? NULL |
| 3788 | : ectx->ec_outer_ref->or_outer; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3789 | |
| 3790 | while (depth > 1 && outer != NULL) |
| 3791 | { |
| 3792 | outer = outer->out_up; |
| 3793 | --depth; |
| 3794 | } |
| 3795 | if (outer == NULL) |
| 3796 | { |
| 3797 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 69c7617 | 2021-12-02 16:38:52 +0000 | [diff] [blame] | 3798 | if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx |
| 3799 | || ectx->ec_outer_ref == NULL) |
| 3800 | // Possibly :def function called from legacy |
| 3801 | // context. |
| 3802 | emsg(_(e_closure_called_from_invalid_context)); |
| 3803 | else |
| 3804 | iemsg("LOADOUTER depth more than scope levels"); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3805 | goto theend; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3806 | } |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 3807 | if (depth == OUTER_LOOP_DEPTH) |
Bram Moolenaar | 0cdfb7c | 2022-09-17 15:44:52 +0100 | [diff] [blame] | 3808 | // Variable declared in loop. May be copied if the |
| 3809 | // loop block has already ended. |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 3810 | tv = ((typval_T *)outer->out_loop_stack->ga_data) |
| 3811 | + outer->out_loop_var_idx |
| 3812 | + iptr->isn_arg.outer.outer_idx; |
| 3813 | else |
Bram Moolenaar | 0cdfb7c | 2022-09-17 15:44:52 +0100 | [diff] [blame] | 3814 | // Variable declared in a function. May be copied if |
| 3815 | // the function has already returned. |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 3816 | tv = ((typval_T *)outer->out_stack->ga_data) |
| 3817 | + outer->out_frame_idx + STACK_FRAME_SIZE |
| 3818 | + iptr->isn_arg.outer.outer_idx; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3819 | if (iptr->isn_type == ISN_LOADOUTER) |
| 3820 | { |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3821 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3822 | goto theend; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3823 | copy_tv(tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3824 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3825 | } |
| 3826 | else |
| 3827 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3828 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3829 | clear_tv(tv); |
| 3830 | *tv = *STACK_TV_BOT(0); |
| 3831 | } |
| 3832 | } |
| 3833 | break; |
| 3834 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 3835 | // unlet item in list or dict variable |
| 3836 | case ISN_UNLETINDEX: |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3837 | if (execute_unletindex(iptr, ectx) == FAIL) |
| 3838 | goto on_error; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 3839 | break; |
| 3840 | |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 3841 | // unlet range of items in list variable |
| 3842 | case ISN_UNLETRANGE: |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3843 | if (execute_unletrange(iptr, ectx) == FAIL) |
| 3844 | goto on_error; |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 3845 | break; |
| 3846 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3847 | // push constant |
| 3848 | case ISN_PUSHNR: |
| 3849 | case ISN_PUSHBOOL: |
| 3850 | case ISN_PUSHSPEC: |
| 3851 | case ISN_PUSHF: |
| 3852 | case ISN_PUSHS: |
| 3853 | case ISN_PUSHBLOB: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3854 | case ISN_PUSHFUNC: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3855 | case ISN_PUSHCHANNEL: |
| 3856 | case ISN_PUSHJOB: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3857 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3858 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3859 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3860 | tv->v_lock = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3861 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3862 | switch (iptr->isn_type) |
| 3863 | { |
| 3864 | case ISN_PUSHNR: |
| 3865 | tv->v_type = VAR_NUMBER; |
| 3866 | tv->vval.v_number = iptr->isn_arg.number; |
| 3867 | break; |
| 3868 | case ISN_PUSHBOOL: |
| 3869 | tv->v_type = VAR_BOOL; |
| 3870 | tv->vval.v_number = iptr->isn_arg.number; |
| 3871 | break; |
| 3872 | case ISN_PUSHSPEC: |
| 3873 | tv->v_type = VAR_SPECIAL; |
| 3874 | tv->vval.v_number = iptr->isn_arg.number; |
| 3875 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3876 | case ISN_PUSHF: |
| 3877 | tv->v_type = VAR_FLOAT; |
| 3878 | tv->vval.v_float = iptr->isn_arg.fnumber; |
| 3879 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3880 | case ISN_PUSHBLOB: |
| 3881 | blob_copy(iptr->isn_arg.blob, tv); |
| 3882 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3883 | case ISN_PUSHFUNC: |
| 3884 | tv->v_type = VAR_FUNC; |
Bram Moolenaar | 087d2e1 | 2020-03-01 15:36:42 +0100 | [diff] [blame] | 3885 | if (iptr->isn_arg.string == NULL) |
| 3886 | tv->vval.v_string = NULL; |
| 3887 | else |
| 3888 | tv->vval.v_string = |
| 3889 | vim_strsave(iptr->isn_arg.string); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3890 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3891 | case ISN_PUSHCHANNEL: |
| 3892 | #ifdef FEAT_JOB_CHANNEL |
| 3893 | tv->v_type = VAR_CHANNEL; |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 3894 | tv->vval.v_channel = NULL; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3895 | #endif |
| 3896 | break; |
| 3897 | case ISN_PUSHJOB: |
| 3898 | #ifdef FEAT_JOB_CHANNEL |
| 3899 | tv->v_type = VAR_JOB; |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 3900 | tv->vval.v_job = NULL; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3901 | #endif |
| 3902 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3903 | default: |
| 3904 | tv->v_type = VAR_STRING; |
Bram Moolenaar | f869100 | 2022-03-10 12:20:53 +0000 | [diff] [blame] | 3905 | tv->vval.v_string = iptr->isn_arg.string == NULL |
| 3906 | ? NULL : vim_strsave(iptr->isn_arg.string); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3907 | } |
| 3908 | break; |
| 3909 | |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 3910 | case ISN_AUTOLOAD: |
| 3911 | { |
| 3912 | char_u *name = iptr->isn_arg.string; |
| 3913 | |
| 3914 | (void)script_autoload(name, FALSE); |
| 3915 | if (find_func(name, TRUE)) |
| 3916 | { |
| 3917 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
| 3918 | goto theend; |
| 3919 | tv = STACK_TV_BOT(0); |
| 3920 | tv->v_lock = 0; |
| 3921 | ++ectx->ec_stack.ga_len; |
| 3922 | tv->v_type = VAR_FUNC; |
| 3923 | tv->vval.v_string = vim_strsave(name); |
| 3924 | } |
| 3925 | else |
| 3926 | { |
| 3927 | int res = load_namespace_var(ectx, ISN_LOADG, iptr); |
| 3928 | |
| 3929 | if (res == NOTDONE) |
| 3930 | goto theend; |
| 3931 | if (res == FAIL) |
| 3932 | goto on_error; |
| 3933 | } |
| 3934 | } |
| 3935 | break; |
| 3936 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 3937 | case ISN_UNLET: |
| 3938 | if (do_unlet(iptr->isn_arg.unlet.ul_name, |
| 3939 | iptr->isn_arg.unlet.ul_forceit) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3940 | goto on_error; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 3941 | break; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 3942 | case ISN_UNLETENV: |
LemonBoy | 7714231 | 2022-04-15 20:50:46 +0100 | [diff] [blame] | 3943 | vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 3944 | break; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 3945 | |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 3946 | case ISN_LOCKUNLOCK: |
| 3947 | { |
| 3948 | typval_T *lval_root_save = lval_root; |
| 3949 | int res; |
| 3950 | |
| 3951 | // Stack has the local variable, argument the whole :lock |
| 3952 | // or :unlock command, like ISN_EXEC. |
| 3953 | --ectx->ec_stack.ga_len; |
| 3954 | lval_root = STACK_TV_BOT(0); |
| 3955 | res = exec_command(iptr); |
| 3956 | clear_tv(lval_root); |
| 3957 | lval_root = lval_root_save; |
| 3958 | if (res == FAIL) |
| 3959 | goto on_error; |
| 3960 | } |
| 3961 | break; |
| 3962 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 3963 | case ISN_LOCKCONST: |
| 3964 | item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE); |
| 3965 | break; |
| 3966 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3967 | // create a list from items on the stack; uses a single allocation |
| 3968 | // for the list header and the items |
| 3969 | case ISN_NEWLIST: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3970 | if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3971 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3972 | break; |
| 3973 | |
| 3974 | // create a dict from items on the stack |
| 3975 | case ISN_NEWDICT: |
| 3976 | { |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 3977 | int res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3978 | |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 3979 | SOURCING_LNUM = iptr->isn_lnum; |
| 3980 | res = exe_newdict(iptr->isn_arg.number, ectx); |
| 3981 | if (res == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3982 | goto theend; |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 3983 | if (res == MAYBE) |
| 3984 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3985 | } |
| 3986 | break; |
| 3987 | |
LemonBoy | 372bcce | 2022-04-25 12:43:20 +0100 | [diff] [blame] | 3988 | case ISN_CONCAT: |
| 3989 | if (exe_concat(iptr->isn_arg.number, ectx) == FAIL) |
| 3990 | goto theend; |
| 3991 | break; |
| 3992 | |
Bram Moolenaar | 8acb9cc | 2022-03-08 13:18:55 +0000 | [diff] [blame] | 3993 | // create a partial with NULL value |
| 3994 | case ISN_NEWPARTIAL: |
| 3995 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
| 3996 | goto theend; |
| 3997 | ++ectx->ec_stack.ga_len; |
| 3998 | tv = STACK_TV_BOT(-1); |
| 3999 | tv->v_type = VAR_PARTIAL; |
| 4000 | tv->v_lock = 0; |
| 4001 | tv->vval.v_partial = NULL; |
| 4002 | break; |
| 4003 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4004 | // call a :def function |
| 4005 | case ISN_DCALL: |
Bram Moolenaar | dfa3d55 | 2020-09-10 22:05:08 +0200 | [diff] [blame] | 4006 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 4007 | if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, |
| 4008 | NULL, |
| 4009 | iptr->isn_arg.dfunc.cdf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4010 | ectx) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 4011 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4012 | break; |
| 4013 | |
| 4014 | // call a builtin function |
| 4015 | case ISN_BCALL: |
| 4016 | SOURCING_LNUM = iptr->isn_lnum; |
| 4017 | if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, |
| 4018 | iptr->isn_arg.bfunc.cbf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4019 | ectx) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4020 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4021 | break; |
| 4022 | |
| 4023 | // call a funcref or partial |
| 4024 | case ISN_PCALL: |
| 4025 | { |
| 4026 | cpfunc_T *pfunc = &iptr->isn_arg.pfunc; |
| 4027 | int r; |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 4028 | typval_T partial_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4029 | |
| 4030 | SOURCING_LNUM = iptr->isn_lnum; |
| 4031 | if (pfunc->cpf_top) |
| 4032 | { |
| 4033 | // funcref is above the arguments |
| 4034 | tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); |
| 4035 | } |
| 4036 | else |
| 4037 | { |
| 4038 | // Get the funcref from the stack. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4039 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 4040 | partial_tv = *STACK_TV_BOT(0); |
| 4041 | tv = &partial_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4042 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4043 | r = call_partial(tv, pfunc->cpf_argcount, ectx); |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 4044 | if (tv == &partial_tv) |
| 4045 | clear_tv(&partial_tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4046 | if (r == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4047 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4048 | } |
| 4049 | break; |
| 4050 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 4051 | case ISN_PCALL_END: |
| 4052 | // PCALL finished, arguments have been consumed and replaced by |
| 4053 | // the return value. Now clear the funcref from the stack, |
| 4054 | // and move the return value in its place. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4055 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 4056 | clear_tv(STACK_TV_BOT(-1)); |
| 4057 | *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); |
| 4058 | break; |
| 4059 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4060 | // call a user defined function or funcref/partial |
| 4061 | case ISN_UCALL: |
| 4062 | { |
| 4063 | cufunc_T *cufunc = &iptr->isn_arg.ufunc; |
| 4064 | |
| 4065 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 4066 | if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4067 | ectx, iptr) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4068 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4069 | } |
| 4070 | break; |
| 4071 | |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 4072 | // :defer func(arg) |
| 4073 | case ISN_DEFER: |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 4074 | if (defer_command(iptr->isn_arg.defer.defer_var_idx, |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 4075 | iptr->isn_arg.defer.defer_argcount, ectx) == FAIL) |
| 4076 | goto on_error; |
| 4077 | break; |
| 4078 | |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 4079 | // return from a :def function call without a value |
| 4080 | case ISN_RETURN_VOID: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4081 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4082 | goto theend; |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 4083 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4084 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 4085 | tv->v_type = VAR_VOID; |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 4086 | tv->vval.v_number = 0; |
| 4087 | tv->v_lock = 0; |
| 4088 | // FALLTHROUGH |
| 4089 | |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 4090 | // return from a :def function call with what is on the stack |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4091 | case ISN_RETURN: |
| 4092 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4093 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 4094 | trycmd_T *trycmd = NULL; |
Bram Moolenaar | 8cbd6df | 2020-01-28 22:59:45 +0100 | [diff] [blame] | 4095 | |
| 4096 | if (trystack->ga_len > 0) |
| 4097 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 4098 | + trystack->ga_len - 1; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 4099 | if (trycmd != NULL |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4100 | && trycmd->tcd_frame_idx == ectx->ec_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4101 | { |
Bram Moolenaar | 9cb577a | 2021-02-22 22:45:10 +0100 | [diff] [blame] | 4102 | // jump to ":finally" or ":endtry" |
| 4103 | if (trycmd->tcd_finally_idx != 0) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4104 | ectx->ec_iidx = trycmd->tcd_finally_idx; |
Bram Moolenaar | 9cb577a | 2021-02-22 22:45:10 +0100 | [diff] [blame] | 4105 | else |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4106 | ectx->ec_iidx = trycmd->tcd_endtry_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4107 | trycmd->tcd_return = TRUE; |
| 4108 | } |
| 4109 | else |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4110 | goto func_return; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4111 | } |
| 4112 | break; |
| 4113 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 4114 | // push a partial, a reference to a compiled function |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4115 | case ISN_FUNCREF: |
| 4116 | { |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 4117 | partial_T *pt = ALLOC_CLEAR_ONE(partial_T); |
| 4118 | ufunc_T *ufunc; |
| 4119 | funcref_T *funcref = &iptr->isn_arg.funcref; |
| 4120 | funcref_extra_T *extra = funcref->fr_extra; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4121 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4122 | if (pt == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4123 | goto theend; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4124 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 4125 | { |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 4126 | vim_free(pt); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4127 | goto theend; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 4128 | } |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 4129 | if (extra == NULL || extra->fre_func_name == NULL) |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 4130 | { |
| 4131 | dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data) |
| 4132 | + funcref->fr_dfunc_idx; |
| 4133 | |
| 4134 | ufunc = pt_dfunc->df_ufunc; |
| 4135 | } |
| 4136 | else |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 4137 | ufunc = find_func(extra->fre_func_name, FALSE); |
Bram Moolenaar | 56acd1f | 2022-02-18 13:24:52 +0000 | [diff] [blame] | 4138 | if (ufunc == NULL) |
| 4139 | { |
| 4140 | SOURCING_LNUM = iptr->isn_lnum; |
| 4141 | iemsg("ufunc unexpectedly NULL for FUNCREF"); |
| 4142 | goto theend; |
| 4143 | } |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 4144 | if (fill_partial_and_closure(pt, ufunc, |
| 4145 | extra == NULL ? 0 : extra->fre_loop_var_idx, |
| 4146 | extra == NULL ? 0 : extra->fre_loop_var_count, |
| 4147 | ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4148 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4149 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4150 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4151 | tv->vval.v_partial = pt; |
| 4152 | tv->v_type = VAR_PARTIAL; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 4153 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4154 | } |
| 4155 | break; |
| 4156 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4157 | // Create a global function from a lambda. |
| 4158 | case ISN_NEWFUNC: |
| 4159 | { |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 4160 | newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4161 | |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 4162 | if (copy_lambda_to_global_func(arg->nfa_lambda, |
| 4163 | arg->nfa_global, arg->nfa_loop_var_idx, |
| 4164 | arg->nfa_loop_var_count, ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4165 | goto theend; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4166 | } |
| 4167 | break; |
| 4168 | |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 4169 | // List functions |
| 4170 | case ISN_DEF: |
| 4171 | if (iptr->isn_arg.string == NULL) |
| 4172 | list_functions(NULL); |
| 4173 | else |
| 4174 | { |
Bram Moolenaar | 1433672 | 2022-01-08 16:02:59 +0000 | [diff] [blame] | 4175 | exarg_T ea; |
| 4176 | garray_T lines_to_free; |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 4177 | |
| 4178 | CLEAR_FIELD(ea); |
| 4179 | ea.cmd = ea.arg = iptr->isn_arg.string; |
Bram Moolenaar | 1433672 | 2022-01-08 16:02:59 +0000 | [diff] [blame] | 4180 | ga_init2(&lines_to_free, sizeof(char_u *), 50); |
| 4181 | define_function(&ea, NULL, &lines_to_free); |
| 4182 | ga_clear_strings(&lines_to_free); |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 4183 | } |
| 4184 | break; |
| 4185 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4186 | // jump if a condition is met |
| 4187 | case ISN_JUMP: |
| 4188 | { |
| 4189 | jumpwhen_T when = iptr->isn_arg.jump.jump_when; |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4190 | int error = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4191 | int jump = TRUE; |
| 4192 | |
| 4193 | if (when != JUMP_ALWAYS) |
| 4194 | { |
| 4195 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4196 | if (when == JUMP_IF_COND_FALSE |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 4197 | || when == JUMP_IF_FALSE |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4198 | || when == JUMP_IF_COND_TRUE) |
| 4199 | { |
| 4200 | SOURCING_LNUM = iptr->isn_lnum; |
| 4201 | jump = tv_get_bool_chk(tv, &error); |
| 4202 | if (error) |
| 4203 | goto on_error; |
| 4204 | } |
| 4205 | else |
| 4206 | jump = tv2bool(tv); |
Bram Moolenaar | f6ced98 | 2022-04-28 12:00:49 +0100 | [diff] [blame] | 4207 | if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4208 | jump = !jump; |
Bram Moolenaar | 777770f | 2020-02-06 21:27:08 +0100 | [diff] [blame] | 4209 | if (when == JUMP_IF_FALSE || !jump) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4210 | { |
| 4211 | // drop the value from the stack |
| 4212 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4213 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4214 | } |
| 4215 | } |
| 4216 | if (jump) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4217 | ectx->ec_iidx = iptr->isn_arg.jump.jump_where; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4218 | } |
| 4219 | break; |
| 4220 | |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 4221 | // "while": jump to end if a condition is false |
| 4222 | case ISN_WHILE: |
| 4223 | { |
| 4224 | int error = FALSE; |
| 4225 | int jump = TRUE; |
| 4226 | |
| 4227 | tv = STACK_TV_BOT(-1); |
| 4228 | SOURCING_LNUM = iptr->isn_lnum; |
| 4229 | jump = !tv_get_bool_chk(tv, &error); |
| 4230 | if (error) |
| 4231 | goto on_error; |
| 4232 | // drop the value from the stack |
| 4233 | clear_tv(tv); |
| 4234 | --ectx->ec_stack.ga_len; |
| 4235 | if (jump) |
| 4236 | ectx->ec_iidx = iptr->isn_arg.whileloop.while_end; |
| 4237 | |
| 4238 | // Store the current funccal count, may be used by |
| 4239 | // ISN_LOOPEND later |
| 4240 | tv = STACK_TV_VAR( |
| 4241 | iptr->isn_arg.whileloop.while_funcref_idx); |
| 4242 | tv->vval.v_number = ectx->ec_funcrefs.ga_len; |
| 4243 | } |
| 4244 | break; |
| 4245 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 4246 | // Jump if an argument with a default value was already set and not |
| 4247 | // v:none. |
| 4248 | case ISN_JUMP_IF_ARG_SET: |
| 4249 | tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off); |
| 4250 | if (tv->v_type != VAR_UNKNOWN |
| 4251 | && !(tv->v_type == VAR_SPECIAL |
| 4252 | && tv->vval.v_number == VVAL_NONE)) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4253 | ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where; |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 4254 | break; |
| 4255 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4256 | // top of a for loop |
| 4257 | case ISN_FOR: |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 4258 | if (execute_for(iptr, ectx) == FAIL) |
| 4259 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4260 | break; |
| 4261 | |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 4262 | // end of a for or while loop |
| 4263 | case ISN_ENDLOOP: |
| 4264 | if (execute_endloop(iptr, ectx) == FAIL) |
| 4265 | goto theend; |
| 4266 | break; |
| 4267 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4268 | // start of ":try" block |
| 4269 | case ISN_TRY: |
| 4270 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 4271 | trycmd_T *trycmd = NULL; |
| 4272 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4273 | if (GA_GROW_FAILS(&ectx->ec_trystack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4274 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4275 | trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data) |
| 4276 | + ectx->ec_trystack.ga_len; |
| 4277 | ++ectx->ec_trystack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4278 | ++trylevel; |
Bram Moolenaar | 8d4be89 | 2021-02-13 18:33:02 +0100 | [diff] [blame] | 4279 | CLEAR_POINTER(trycmd); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4280 | trycmd->tcd_frame_idx = ectx->ec_frame_idx; |
| 4281 | trycmd->tcd_stack_len = ectx->ec_stack.ga_len; |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 4282 | trycmd->tcd_catch_idx = |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 4283 | iptr->isn_arg.tryref.try_ref->try_catch; |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 4284 | trycmd->tcd_finally_idx = |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 4285 | iptr->isn_arg.tryref.try_ref->try_finally; |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 4286 | trycmd->tcd_endtry_idx = |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 4287 | iptr->isn_arg.tryref.try_ref->try_endtry; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4288 | } |
| 4289 | break; |
| 4290 | |
| 4291 | case ISN_PUSHEXC: |
| 4292 | if (current_exception == NULL) |
| 4293 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4294 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4295 | iemsg("Evaluating catch while current_exception is NULL"); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4296 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4297 | } |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4298 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4299 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4300 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4301 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4302 | tv->v_type = VAR_STRING; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 4303 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4304 | tv->vval.v_string = vim_strsave( |
| 4305 | (char_u *)current_exception->value); |
| 4306 | break; |
| 4307 | |
| 4308 | case ISN_CATCH: |
| 4309 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4310 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 0665163 | 2022-04-27 17:54:25 +0100 | [diff] [blame] | 4311 | trycmd_T *trycmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4312 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4313 | may_restore_cmdmod(&ectx->ec_funclocal); |
Bram Moolenaar | 0665163 | 2022-04-27 17:54:25 +0100 | [diff] [blame] | 4314 | trycmd = ((trycmd_T *)trystack->ga_data) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4315 | + trystack->ga_len - 1; |
Bram Moolenaar | 0665163 | 2022-04-27 17:54:25 +0100 | [diff] [blame] | 4316 | trycmd->tcd_caught = TRUE; |
| 4317 | trycmd->tcd_did_throw = FALSE; |
| 4318 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4319 | did_emsg = got_int = did_throw = FALSE; |
Bram Moolenaar | 1430cee | 2021-01-17 19:20:32 +0100 | [diff] [blame] | 4320 | force_abort = need_rethrow = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4321 | catch_exception(current_exception); |
| 4322 | } |
| 4323 | break; |
| 4324 | |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 4325 | case ISN_TRYCONT: |
| 4326 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4327 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 4328 | trycont_T *trycont = &iptr->isn_arg.trycont; |
| 4329 | int i; |
| 4330 | trycmd_T *trycmd; |
| 4331 | int iidx = trycont->tct_where; |
| 4332 | |
| 4333 | if (trystack->ga_len < trycont->tct_levels) |
| 4334 | { |
| 4335 | siemsg("TRYCONT: expected %d levels, found %d", |
| 4336 | trycont->tct_levels, trystack->ga_len); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4337 | goto theend; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 4338 | } |
| 4339 | // Make :endtry jump to any outer try block and the last |
| 4340 | // :endtry inside the loop to the loop start. |
| 4341 | for (i = trycont->tct_levels; i > 0; --i) |
| 4342 | { |
| 4343 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 4344 | + trystack->ga_len - i; |
Bram Moolenaar | 2e34c34 | 2021-03-14 12:13:33 +0100 | [diff] [blame] | 4345 | // Add one to tcd_cont to be able to jump to |
| 4346 | // instruction with index zero. |
| 4347 | trycmd->tcd_cont = iidx + 1; |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 4348 | iidx = trycmd->tcd_finally_idx == 0 |
| 4349 | ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 4350 | } |
| 4351 | // jump to :finally or :endtry of current try statement |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4352 | ectx->ec_iidx = iidx; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 4353 | } |
| 4354 | break; |
| 4355 | |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 4356 | case ISN_FINALLY: |
| 4357 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4358 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 4359 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
| 4360 | + trystack->ga_len - 1; |
| 4361 | |
| 4362 | // Reset the index to avoid a return statement jumps here |
| 4363 | // again. |
| 4364 | trycmd->tcd_finally_idx = 0; |
| 4365 | break; |
| 4366 | } |
| 4367 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4368 | // end of ":try" block |
| 4369 | case ISN_ENDTRY: |
| 4370 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4371 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 0665163 | 2022-04-27 17:54:25 +0100 | [diff] [blame] | 4372 | trycmd_T *trycmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4373 | |
Bram Moolenaar | 0665163 | 2022-04-27 17:54:25 +0100 | [diff] [blame] | 4374 | --trystack->ga_len; |
| 4375 | --trylevel; |
| 4376 | trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len; |
| 4377 | if (trycmd->tcd_did_throw) |
| 4378 | did_throw = TRUE; |
| 4379 | if (trycmd->tcd_caught && current_exception != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4380 | { |
Bram Moolenaar | 0665163 | 2022-04-27 17:54:25 +0100 | [diff] [blame] | 4381 | // discard the exception |
| 4382 | if (caught_stack == current_exception) |
| 4383 | caught_stack = caught_stack->caught; |
| 4384 | discard_current_exception(); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4385 | } |
Bram Moolenaar | 0665163 | 2022-04-27 17:54:25 +0100 | [diff] [blame] | 4386 | |
| 4387 | if (trycmd->tcd_return) |
| 4388 | goto func_return; |
| 4389 | |
| 4390 | while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len) |
| 4391 | { |
| 4392 | --ectx->ec_stack.ga_len; |
| 4393 | clear_tv(STACK_TV_BOT(0)); |
| 4394 | } |
| 4395 | if (trycmd->tcd_cont != 0) |
| 4396 | // handling :continue: jump to outer try block or |
| 4397 | // start of the loop |
| 4398 | ectx->ec_iidx = trycmd->tcd_cont - 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4399 | } |
| 4400 | break; |
| 4401 | |
| 4402 | case ISN_THROW: |
Bram Moolenaar | 8f81b22 | 2021-01-14 21:47:06 +0100 | [diff] [blame] | 4403 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4404 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 1e021e6 | 2020-10-16 20:25:23 +0200 | [diff] [blame] | 4405 | |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 4406 | if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent) |
| 4407 | { |
| 4408 | // throwing an exception while using "silent!" causes |
| 4409 | // the function to abort but not display an error. |
| 4410 | tv = STACK_TV_BOT(-1); |
| 4411 | clear_tv(tv); |
| 4412 | tv->v_type = VAR_NUMBER; |
| 4413 | tv->vval.v_number = 0; |
| 4414 | goto done; |
| 4415 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4416 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 4417 | tv = STACK_TV_BOT(0); |
| 4418 | if (tv->vval.v_string == NULL |
| 4419 | || *skipwhite(tv->vval.v_string) == NUL) |
| 4420 | { |
| 4421 | vim_free(tv->vval.v_string); |
| 4422 | SOURCING_LNUM = iptr->isn_lnum; |
| 4423 | emsg(_(e_throw_with_empty_string)); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4424 | goto theend; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | // Inside a "catch" we need to first discard the caught |
| 4428 | // exception. |
| 4429 | if (trystack->ga_len > 0) |
| 4430 | { |
| 4431 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
| 4432 | + trystack->ga_len - 1; |
| 4433 | if (trycmd->tcd_caught && current_exception != NULL) |
| 4434 | { |
| 4435 | // discard the exception |
| 4436 | if (caught_stack == current_exception) |
| 4437 | caught_stack = caught_stack->caught; |
| 4438 | discard_current_exception(); |
| 4439 | trycmd->tcd_caught = FALSE; |
| 4440 | } |
| 4441 | } |
| 4442 | |
Bram Moolenaar | 90a5716 | 2022-02-12 14:23:17 +0000 | [diff] [blame] | 4443 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 4444 | if (throw_exception(tv->vval.v_string, ET_USER, NULL) |
| 4445 | == FAIL) |
| 4446 | { |
| 4447 | vim_free(tv->vval.v_string); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4448 | goto theend; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 4449 | } |
| 4450 | did_throw = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4451 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4452 | break; |
| 4453 | |
| 4454 | // compare with special values |
| 4455 | case ISN_COMPAREBOOL: |
| 4456 | case ISN_COMPARESPECIAL: |
| 4457 | { |
| 4458 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4459 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4460 | varnumber_T arg1 = tv1->vval.v_number; |
| 4461 | varnumber_T arg2 = tv2->vval.v_number; |
| 4462 | int res; |
| 4463 | |
Bram Moolenaar | 0665163 | 2022-04-27 17:54:25 +0100 | [diff] [blame] | 4464 | if (iptr->isn_arg.op.op_type == EXPR_EQUAL) |
| 4465 | res = arg1 == arg2; |
| 4466 | else |
| 4467 | res = arg1 != arg2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4468 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4469 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4470 | tv1->v_type = VAR_BOOL; |
| 4471 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 4472 | } |
| 4473 | break; |
| 4474 | |
Bram Moolenaar | 7a22224 | 2022-03-01 19:23:24 +0000 | [diff] [blame] | 4475 | case ISN_COMPARENULL: |
| 4476 | { |
| 4477 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4478 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4479 | int res; |
| 4480 | |
| 4481 | res = typval_compare_null(tv1, tv2); |
| 4482 | if (res == MAYBE) |
| 4483 | goto on_error; |
| 4484 | if (iptr->isn_arg.op.op_type == EXPR_NEQUAL) |
| 4485 | res = !res; |
| 4486 | clear_tv(tv1); |
| 4487 | clear_tv(tv2); |
| 4488 | --ectx->ec_stack.ga_len; |
| 4489 | tv1->v_type = VAR_BOOL; |
| 4490 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 4491 | } |
| 4492 | break; |
| 4493 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4494 | // Operation with two number arguments |
| 4495 | case ISN_OPNR: |
| 4496 | case ISN_COMPARENR: |
| 4497 | { |
| 4498 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4499 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4500 | varnumber_T arg1 = tv1->vval.v_number; |
| 4501 | varnumber_T arg2 = tv2->vval.v_number; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 4502 | varnumber_T res = 0; |
| 4503 | int div_zero = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4504 | |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 4505 | if (iptr->isn_arg.op.op_type == EXPR_LSHIFT |
| 4506 | || iptr->isn_arg.op.op_type == EXPR_RSHIFT) |
| 4507 | { |
| 4508 | if (arg2 < 0) |
| 4509 | { |
| 4510 | SOURCING_LNUM = iptr->isn_lnum; |
| 4511 | emsg(_(e_bitshift_ops_must_be_postive)); |
| 4512 | goto on_error; |
| 4513 | } |
| 4514 | } |
| 4515 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4516 | switch (iptr->isn_arg.op.op_type) |
| 4517 | { |
| 4518 | case EXPR_MULT: res = arg1 * arg2; break; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 4519 | case EXPR_DIV: if (arg2 == 0) |
| 4520 | div_zero = TRUE; |
| 4521 | else |
| 4522 | res = arg1 / arg2; |
| 4523 | break; |
| 4524 | case EXPR_REM: if (arg2 == 0) |
| 4525 | div_zero = TRUE; |
| 4526 | else |
| 4527 | res = arg1 % arg2; |
| 4528 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4529 | case EXPR_SUB: res = arg1 - arg2; break; |
| 4530 | case EXPR_ADD: res = arg1 + arg2; break; |
| 4531 | |
| 4532 | case EXPR_EQUAL: res = arg1 == arg2; break; |
| 4533 | case EXPR_NEQUAL: res = arg1 != arg2; break; |
| 4534 | case EXPR_GREATER: res = arg1 > arg2; break; |
| 4535 | case EXPR_GEQUAL: res = arg1 >= arg2; break; |
| 4536 | case EXPR_SMALLER: res = arg1 < arg2; break; |
| 4537 | case EXPR_SEQUAL: res = arg1 <= arg2; break; |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 4538 | case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS) |
| 4539 | res = 0; |
| 4540 | else |
Bram Moolenaar | 68e64d2 | 2022-05-22 22:07:52 +0100 | [diff] [blame] | 4541 | res = (uvarnumber_T)arg1 << arg2; |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 4542 | break; |
| 4543 | case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS) |
| 4544 | res = 0; |
| 4545 | else |
Bram Moolenaar | 338bf58 | 2022-05-22 20:16:32 +0100 | [diff] [blame] | 4546 | res = (uvarnumber_T)arg1 >> arg2; |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 4547 | break; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 4548 | default: break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4549 | } |
| 4550 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4551 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4552 | if (iptr->isn_type == ISN_COMPARENR) |
| 4553 | { |
| 4554 | tv1->v_type = VAR_BOOL; |
| 4555 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 4556 | } |
| 4557 | else |
| 4558 | tv1->vval.v_number = res; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 4559 | if (div_zero) |
| 4560 | { |
| 4561 | SOURCING_LNUM = iptr->isn_lnum; |
| 4562 | emsg(_(e_divide_by_zero)); |
| 4563 | goto on_error; |
| 4564 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4565 | } |
| 4566 | break; |
| 4567 | |
| 4568 | // Computation with two float arguments |
| 4569 | case ISN_OPFLOAT: |
| 4570 | case ISN_COMPAREFLOAT: |
| 4571 | { |
| 4572 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4573 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4574 | float_T arg1 = tv1->vval.v_float; |
| 4575 | float_T arg2 = tv2->vval.v_float; |
| 4576 | float_T res = 0; |
| 4577 | int cmp = FALSE; |
| 4578 | |
| 4579 | switch (iptr->isn_arg.op.op_type) |
| 4580 | { |
| 4581 | case EXPR_MULT: res = arg1 * arg2; break; |
| 4582 | case EXPR_DIV: res = arg1 / arg2; break; |
| 4583 | case EXPR_SUB: res = arg1 - arg2; break; |
| 4584 | case EXPR_ADD: res = arg1 + arg2; break; |
| 4585 | |
| 4586 | case EXPR_EQUAL: cmp = arg1 == arg2; break; |
| 4587 | case EXPR_NEQUAL: cmp = arg1 != arg2; break; |
| 4588 | case EXPR_GREATER: cmp = arg1 > arg2; break; |
| 4589 | case EXPR_GEQUAL: cmp = arg1 >= arg2; break; |
| 4590 | case EXPR_SMALLER: cmp = arg1 < arg2; break; |
| 4591 | case EXPR_SEQUAL: cmp = arg1 <= arg2; break; |
| 4592 | default: cmp = 0; break; |
| 4593 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4594 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4595 | if (iptr->isn_type == ISN_COMPAREFLOAT) |
| 4596 | { |
| 4597 | tv1->v_type = VAR_BOOL; |
| 4598 | tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; |
| 4599 | } |
| 4600 | else |
| 4601 | tv1->vval.v_float = res; |
| 4602 | } |
| 4603 | break; |
| 4604 | |
| 4605 | case ISN_COMPARELIST: |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 4606 | case ISN_COMPAREDICT: |
| 4607 | case ISN_COMPAREFUNC: |
| 4608 | case ISN_COMPARESTRING: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4609 | case ISN_COMPAREBLOB: |
| 4610 | { |
| 4611 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4612 | typval_T *tv2 = STACK_TV_BOT(-1); |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 4613 | exprtype_T exprtype = iptr->isn_arg.op.op_type; |
| 4614 | int ic = iptr->isn_arg.op.op_ic; |
| 4615 | int res = FALSE; |
| 4616 | int status = OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4617 | |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 4618 | SOURCING_LNUM = iptr->isn_lnum; |
| 4619 | if (iptr->isn_type == ISN_COMPARELIST) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4620 | { |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 4621 | status = typval_compare_list(tv1, tv2, |
| 4622 | exprtype, ic, &res); |
| 4623 | } |
| 4624 | else if (iptr->isn_type == ISN_COMPAREDICT) |
| 4625 | { |
| 4626 | status = typval_compare_dict(tv1, tv2, |
| 4627 | exprtype, ic, &res); |
| 4628 | } |
| 4629 | else if (iptr->isn_type == ISN_COMPAREFUNC) |
| 4630 | { |
| 4631 | status = typval_compare_func(tv1, tv2, |
| 4632 | exprtype, ic, &res); |
| 4633 | } |
| 4634 | else if (iptr->isn_type == ISN_COMPARESTRING) |
| 4635 | { |
| 4636 | status = typval_compare_string(tv1, tv2, |
| 4637 | exprtype, ic, &res); |
| 4638 | } |
| 4639 | else |
| 4640 | { |
| 4641 | status = typval_compare_blob(tv1, tv2, exprtype, &res); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4642 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4643 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4644 | clear_tv(tv1); |
| 4645 | clear_tv(tv2); |
| 4646 | tv1->v_type = VAR_BOOL; |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 4647 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 4648 | if (status == FAIL) |
| 4649 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4650 | } |
| 4651 | break; |
| 4652 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4653 | case ISN_COMPAREANY: |
| 4654 | { |
| 4655 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4656 | typval_T *tv2 = STACK_TV_BOT(-1); |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 4657 | exprtype_T exprtype = iptr->isn_arg.op.op_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4658 | int ic = iptr->isn_arg.op.op_ic; |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 4659 | int status; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4660 | |
Bram Moolenaar | eb26f43 | 2020-09-14 16:50:05 +0200 | [diff] [blame] | 4661 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 4662 | status = typval_compare(tv1, tv2, exprtype, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4663 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4664 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 4665 | if (status == FAIL) |
| 4666 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4667 | } |
| 4668 | break; |
| 4669 | |
| 4670 | case ISN_ADDLIST: |
| 4671 | case ISN_ADDBLOB: |
| 4672 | { |
| 4673 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4674 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4675 | |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4676 | // add two lists or blobs |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4677 | if (iptr->isn_type == ISN_ADDLIST) |
Bram Moolenaar | 0780204 | 2021-09-09 23:01:14 +0200 | [diff] [blame] | 4678 | { |
| 4679 | if (iptr->isn_arg.op.op_type == EXPR_APPEND |
| 4680 | && tv1->vval.v_list != NULL) |
| 4681 | list_extend(tv1->vval.v_list, tv2->vval.v_list, |
| 4682 | NULL); |
| 4683 | else |
| 4684 | eval_addlist(tv1, tv2); |
| 4685 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4686 | else |
| 4687 | eval_addblob(tv1, tv2); |
| 4688 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4689 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4690 | } |
| 4691 | break; |
| 4692 | |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4693 | case ISN_LISTAPPEND: |
| 4694 | { |
| 4695 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4696 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4697 | list_T *l = tv1->vval.v_list; |
| 4698 | |
| 4699 | // add an item to a list |
Bram Moolenaar | 1f4a345 | 2022-01-01 18:29:21 +0000 | [diff] [blame] | 4700 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4701 | if (l == NULL) |
| 4702 | { |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4703 | emsg(_(e_cannot_add_to_null_list)); |
| 4704 | goto on_error; |
| 4705 | } |
Bram Moolenaar | 1f4a345 | 2022-01-01 18:29:21 +0000 | [diff] [blame] | 4706 | if (value_check_lock(l->lv_lock, NULL, FALSE)) |
| 4707 | goto on_error; |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4708 | if (list_append_tv(l, tv2) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4709 | goto theend; |
Bram Moolenaar | 955347c | 2020-10-19 23:01:46 +0200 | [diff] [blame] | 4710 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4711 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4712 | } |
| 4713 | break; |
| 4714 | |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 4715 | case ISN_BLOBAPPEND: |
| 4716 | { |
| 4717 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4718 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4719 | blob_T *b = tv1->vval.v_blob; |
| 4720 | int error = FALSE; |
| 4721 | varnumber_T n; |
| 4722 | |
| 4723 | // add a number to a blob |
| 4724 | if (b == NULL) |
| 4725 | { |
| 4726 | SOURCING_LNUM = iptr->isn_lnum; |
| 4727 | emsg(_(e_cannot_add_to_null_blob)); |
| 4728 | goto on_error; |
| 4729 | } |
| 4730 | n = tv_get_number_chk(tv2, &error); |
| 4731 | if (error) |
| 4732 | goto on_error; |
| 4733 | ga_append(&b->bv_ga, (int)n); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4734 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 4735 | } |
| 4736 | break; |
| 4737 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4738 | // Computation with two arguments of unknown type |
| 4739 | case ISN_OPANY: |
| 4740 | { |
| 4741 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4742 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4743 | varnumber_T n1, n2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4744 | float_T f1 = 0, f2 = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4745 | int error = FALSE; |
| 4746 | |
| 4747 | if (iptr->isn_arg.op.op_type == EXPR_ADD) |
| 4748 | { |
| 4749 | if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) |
| 4750 | { |
| 4751 | eval_addlist(tv1, tv2); |
| 4752 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4753 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4754 | break; |
| 4755 | } |
| 4756 | else if (tv1->v_type == VAR_BLOB |
| 4757 | && tv2->v_type == VAR_BLOB) |
| 4758 | { |
| 4759 | eval_addblob(tv1, tv2); |
| 4760 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4761 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4762 | break; |
| 4763 | } |
| 4764 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4765 | if (tv1->v_type == VAR_FLOAT) |
| 4766 | { |
| 4767 | f1 = tv1->vval.v_float; |
| 4768 | n1 = 0; |
| 4769 | } |
| 4770 | else |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4771 | { |
Bram Moolenaar | f665e97 | 2020-12-05 19:17:16 +0100 | [diff] [blame] | 4772 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4773 | n1 = tv_get_number_chk(tv1, &error); |
| 4774 | if (error) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4775 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4776 | if (tv2->v_type == VAR_FLOAT) |
| 4777 | f1 = n1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4778 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4779 | if (tv2->v_type == VAR_FLOAT) |
| 4780 | { |
| 4781 | f2 = tv2->vval.v_float; |
| 4782 | n2 = 0; |
| 4783 | } |
| 4784 | else |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4785 | { |
| 4786 | n2 = tv_get_number_chk(tv2, &error); |
| 4787 | if (error) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4788 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4789 | if (tv1->v_type == VAR_FLOAT) |
| 4790 | f2 = n2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4791 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4792 | // if there is a float on either side the result is a float |
| 4793 | if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) |
| 4794 | { |
| 4795 | switch (iptr->isn_arg.op.op_type) |
| 4796 | { |
| 4797 | case EXPR_MULT: f1 = f1 * f2; break; |
| 4798 | case EXPR_DIV: f1 = f1 / f2; break; |
| 4799 | case EXPR_SUB: f1 = f1 - f2; break; |
| 4800 | case EXPR_ADD: f1 = f1 + f2; break; |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4801 | default: SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4802 | emsg(_(e_cannot_use_percent_with_float)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4803 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4804 | } |
| 4805 | clear_tv(tv1); |
| 4806 | clear_tv(tv2); |
| 4807 | tv1->v_type = VAR_FLOAT; |
| 4808 | tv1->vval.v_float = f1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4809 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4810 | } |
| 4811 | else |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4812 | { |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 4813 | int failed = FALSE; |
| 4814 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4815 | switch (iptr->isn_arg.op.op_type) |
| 4816 | { |
| 4817 | case EXPR_MULT: n1 = n1 * n2; break; |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 4818 | case EXPR_DIV: n1 = num_divide(n1, n2, &failed); |
| 4819 | if (failed) |
Bram Moolenaar | 99880f9 | 2021-01-20 21:23:14 +0100 | [diff] [blame] | 4820 | goto on_error; |
| 4821 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4822 | case EXPR_SUB: n1 = n1 - n2; break; |
| 4823 | case EXPR_ADD: n1 = n1 + n2; break; |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 4824 | default: n1 = num_modulus(n1, n2, &failed); |
| 4825 | if (failed) |
Bram Moolenaar | 99880f9 | 2021-01-20 21:23:14 +0100 | [diff] [blame] | 4826 | goto on_error; |
| 4827 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4828 | } |
| 4829 | clear_tv(tv1); |
| 4830 | clear_tv(tv2); |
| 4831 | tv1->v_type = VAR_NUMBER; |
| 4832 | tv1->vval.v_number = n1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4833 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4834 | } |
| 4835 | } |
| 4836 | break; |
| 4837 | |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4838 | case ISN_STRINDEX: |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4839 | case ISN_STRSLICE: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4840 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4841 | int is_slice = iptr->isn_type == ISN_STRSLICE; |
| 4842 | varnumber_T n1 = 0, n2; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4843 | char_u *res; |
| 4844 | |
| 4845 | // string index: string is at stack-2, index at stack-1 |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4846 | // string slice: string is at stack-3, first index at |
| 4847 | // stack-2, second index at stack-1 |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4848 | if (is_slice) |
| 4849 | { |
| 4850 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4851 | n1 = tv->vval.v_number; |
| 4852 | } |
| 4853 | |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4854 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4855 | n2 = tv->vval.v_number; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4856 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4857 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4858 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4859 | if (is_slice) |
| 4860 | // Slice: Select the characters from the string |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 4861 | res = string_slice(tv->vval.v_string, n1, n2, FALSE); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4862 | else |
| 4863 | // Index: The resulting variable is a string of a |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 4864 | // single character (including composing characters). |
| 4865 | // If the index is too big or negative the result is |
| 4866 | // empty. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4867 | res = char_from_string(tv->vval.v_string, n2); |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4868 | vim_free(tv->vval.v_string); |
| 4869 | tv->vval.v_string = res; |
| 4870 | } |
| 4871 | break; |
| 4872 | |
| 4873 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4874 | case ISN_LISTSLICE: |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4875 | case ISN_BLOBINDEX: |
| 4876 | case ISN_BLOBSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4877 | { |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4878 | int is_slice = iptr->isn_type == ISN_LISTSLICE |
| 4879 | || iptr->isn_type == ISN_BLOBSLICE; |
| 4880 | int is_blob = iptr->isn_type == ISN_BLOBINDEX |
| 4881 | || iptr->isn_type == ISN_BLOBSLICE; |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4882 | varnumber_T n1, n2; |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4883 | typval_T *val_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4884 | |
| 4885 | // list index: list is at stack-2, index at stack-1 |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4886 | // list slice: list is at stack-3, indexes at stack-2 and |
| 4887 | // stack-1 |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4888 | // Same for blob. |
| 4889 | val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4890 | |
| 4891 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4892 | n1 = n2 = tv->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4893 | clear_tv(tv); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4894 | |
| 4895 | if (is_slice) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4896 | { |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4897 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4898 | n1 = tv->vval.v_number; |
| 4899 | clear_tv(tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4900 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4901 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4902 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | 435d897 | 2020-07-05 16:42:13 +0200 | [diff] [blame] | 4903 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 1d63454 | 2020-08-18 13:41:50 +0200 | [diff] [blame] | 4904 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4905 | if (is_blob) |
| 4906 | { |
| 4907 | if (blob_slice_or_index(val_tv->vval.v_blob, is_slice, |
| 4908 | n1, n2, FALSE, tv) == FAIL) |
| 4909 | goto on_error; |
| 4910 | } |
| 4911 | else |
| 4912 | { |
| 4913 | if (list_slice_or_index(val_tv->vval.v_list, is_slice, |
| 4914 | n1, n2, FALSE, tv, TRUE) == FAIL) |
| 4915 | goto on_error; |
| 4916 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4917 | } |
| 4918 | break; |
| 4919 | |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4920 | case ISN_ANYINDEX: |
| 4921 | case ISN_ANYSLICE: |
| 4922 | { |
| 4923 | int is_slice = iptr->isn_type == ISN_ANYSLICE; |
| 4924 | typval_T *var1, *var2; |
| 4925 | int res; |
| 4926 | |
| 4927 | // index: composite is at stack-2, index at stack-1 |
| 4928 | // slice: composite is at stack-3, indexes at stack-2 and |
| 4929 | // stack-1 |
| 4930 | tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); |
Bram Moolenaar | 3affe7a | 2020-08-18 20:34:13 +0200 | [diff] [blame] | 4931 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4932 | if (check_can_index(tv, TRUE, TRUE) == FAIL) |
| 4933 | goto on_error; |
| 4934 | var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1); |
| 4935 | var2 = is_slice ? STACK_TV_BOT(-1) : NULL; |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 4936 | res = eval_index_inner(tv, is_slice, var1, var2, |
| 4937 | FALSE, NULL, -1, TRUE); |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4938 | clear_tv(var1); |
| 4939 | if (is_slice) |
| 4940 | clear_tv(var2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4941 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4942 | if (res == FAIL) |
| 4943 | goto on_error; |
| 4944 | } |
| 4945 | break; |
| 4946 | |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4947 | case ISN_SLICE: |
| 4948 | { |
| 4949 | list_T *list; |
| 4950 | int count = iptr->isn_arg.number; |
| 4951 | |
Bram Moolenaar | c5b1c20 | 2020-06-18 22:43:27 +0200 | [diff] [blame] | 4952 | // type will have been checked to be a list |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4953 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4954 | list = tv->vval.v_list; |
| 4955 | |
| 4956 | // no error for short list, expect it to be checked earlier |
| 4957 | if (list != NULL && list->lv_len >= count) |
| 4958 | { |
| 4959 | list_T *newlist = list_slice(list, |
| 4960 | count, list->lv_len - 1); |
| 4961 | |
| 4962 | if (newlist != NULL) |
| 4963 | { |
| 4964 | list_unref(list); |
| 4965 | tv->vval.v_list = newlist; |
| 4966 | ++newlist->lv_refcount; |
| 4967 | } |
| 4968 | } |
| 4969 | } |
| 4970 | break; |
| 4971 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4972 | case ISN_GETITEM: |
| 4973 | { |
| 4974 | listitem_T *li; |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 4975 | getitem_T *gi = &iptr->isn_arg.getitem; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4976 | |
Bram Moolenaar | c785b9a | 2020-06-19 18:34:15 +0200 | [diff] [blame] | 4977 | // Get list item: list is at stack-1, push item. |
| 4978 | // List type and length is checked for when compiling. |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 4979 | tv = STACK_TV_BOT(-1 - gi->gi_with_op); |
| 4980 | li = list_find(tv->vval.v_list, gi->gi_index); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4981 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4982 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4983 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4984 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4985 | copy_tv(&li->li_tv, STACK_TV_BOT(-1)); |
Bram Moolenaar | f785aa1 | 2021-02-11 21:19:34 +0100 | [diff] [blame] | 4986 | |
| 4987 | // Useful when used in unpack assignment. Reset at |
| 4988 | // ISN_DROP. |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 4989 | ectx->ec_where.wt_index = gi->gi_index + 1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4990 | ectx->ec_where.wt_variable = TRUE; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4991 | } |
| 4992 | break; |
| 4993 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4994 | case ISN_MEMBER: |
| 4995 | { |
| 4996 | dict_T *dict; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4997 | char_u *key; |
| 4998 | dictitem_T *di; |
| 4999 | |
| 5000 | // dict member: dict is at stack-2, key at stack-1 |
| 5001 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 5002 | // no need to check for VAR_DICT, CHECKTYPE will check. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5003 | dict = tv->vval.v_dict; |
| 5004 | |
| 5005 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 5006 | // no need to check for VAR_STRING, 2STRING will check. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5007 | key = tv->vval.v_string; |
Bram Moolenaar | 086fc9a | 2020-10-30 18:33:02 +0100 | [diff] [blame] | 5008 | if (key == NULL) |
| 5009 | key = (char_u *)""; |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 5010 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5011 | if ((di = dict_find(dict, key, -1)) == NULL) |
| 5012 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 5013 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 5014 | semsg(_(e_key_not_present_in_dictionary), key); |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 5015 | |
| 5016 | // If :silent! is used we will continue, make sure the |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 5017 | // stack contents makes sense and the dict stack is |
| 5018 | // updated. |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 5019 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5020 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 5021 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 5022 | (void) dict_stack_save(tv); |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 5023 | tv->v_type = VAR_NUMBER; |
| 5024 | tv->vval.v_number = 0; |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 5025 | goto on_fatal_error; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5026 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5027 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5028 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 5029 | // Put the dict used on the dict stack, it might be used by |
| 5030 | // a dict function later. |
Bram Moolenaar | 50788ef | 2020-07-05 16:51:26 +0200 | [diff] [blame] | 5031 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 5032 | if (dict_stack_save(tv) == FAIL) |
| 5033 | goto on_fatal_error; |
Bram Moolenaar | 50788ef | 2020-07-05 16:51:26 +0200 | [diff] [blame] | 5034 | copy_tv(&di->di_tv, tv); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5035 | } |
| 5036 | break; |
| 5037 | |
| 5038 | // dict member with string key |
| 5039 | case ISN_STRINGMEMBER: |
| 5040 | { |
| 5041 | dict_T *dict; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5042 | dictitem_T *di; |
| 5043 | |
| 5044 | tv = STACK_TV_BOT(-1); |
| 5045 | if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) |
| 5046 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 5047 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 5048 | emsg(_(e_dictionary_required)); |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 5049 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5050 | } |
| 5051 | dict = tv->vval.v_dict; |
| 5052 | |
| 5053 | if ((di = dict_find(dict, iptr->isn_arg.string, -1)) |
| 5054 | == NULL) |
| 5055 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 5056 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 381692b | 2022-02-02 20:01:27 +0000 | [diff] [blame] | 5057 | semsg(_(e_key_not_present_in_dictionary), |
| 5058 | iptr->isn_arg.string); |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 5059 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5060 | } |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 5061 | // Put the dict used on the dict stack, it might be used by |
| 5062 | // a dict function later. |
| 5063 | if (dict_stack_save(tv) == FAIL) |
| 5064 | goto on_fatal_error; |
| 5065 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5066 | copy_tv(&di->di_tv, tv); |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 5067 | } |
| 5068 | break; |
| 5069 | |
| 5070 | case ISN_CLEARDICT: |
| 5071 | dict_stack_drop(); |
| 5072 | break; |
| 5073 | |
| 5074 | case ISN_USEDICT: |
| 5075 | { |
| 5076 | typval_T *dict_tv = dict_stack_get_tv(); |
| 5077 | |
| 5078 | // Turn "dict.Func" into a partial for "Func" bound to |
| 5079 | // "dict". Don't do this when "Func" is already a partial |
| 5080 | // that was bound explicitly (pt_auto is FALSE). |
| 5081 | tv = STACK_TV_BOT(-1); |
| 5082 | if (dict_tv != NULL |
| 5083 | && dict_tv->v_type == VAR_DICT |
| 5084 | && dict_tv->vval.v_dict != NULL |
| 5085 | && (tv->v_type == VAR_FUNC |
| 5086 | || (tv->v_type == VAR_PARTIAL |
| 5087 | && (tv->vval.v_partial->pt_auto |
| 5088 | || tv->vval.v_partial->pt_dict == NULL)))) |
| 5089 | dict_tv->vval.v_dict = |
| 5090 | make_partial(dict_tv->vval.v_dict, tv); |
| 5091 | dict_stack_drop(); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5092 | } |
| 5093 | break; |
| 5094 | |
| 5095 | case ISN_NEGATENR: |
| 5096 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 0c7f261 | 2022-02-17 19:44:07 +0000 | [diff] [blame] | 5097 | // CHECKTYPE should have checked the variable type |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 5098 | if (tv->v_type == VAR_FLOAT) |
| 5099 | tv->vval.v_float = -tv->vval.v_float; |
| 5100 | else |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 5101 | tv->vval.v_number = -tv->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5102 | break; |
| 5103 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5104 | case ISN_CHECKTYPE: |
| 5105 | { |
| 5106 | checktype_T *ct = &iptr->isn_arg.type; |
Bram Moolenaar | bd3a9d2 | 2022-05-17 16:12:39 +0100 | [diff] [blame] | 5107 | int save_wt_variable = ectx->ec_where.wt_variable; |
Bram Moolenaar | b1040dc | 2022-05-18 11:00:48 +0100 | [diff] [blame] | 5108 | int r; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5109 | |
Bram Moolenaar | b3005ce | 2021-01-22 17:51:06 +0100 | [diff] [blame] | 5110 | tv = STACK_TV_BOT((int)ct->ct_off); |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 5111 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5112 | if (!ectx->ec_where.wt_variable) |
| 5113 | ectx->ec_where.wt_index = ct->ct_arg_idx; |
Bram Moolenaar | bd3a9d2 | 2022-05-17 16:12:39 +0100 | [diff] [blame] | 5114 | ectx->ec_where.wt_variable = ct->ct_is_var; |
Bram Moolenaar | b1040dc | 2022-05-18 11:00:48 +0100 | [diff] [blame] | 5115 | r = check_typval_type(ct->ct_type, tv, ectx->ec_where); |
Bram Moolenaar | bd3a9d2 | 2022-05-17 16:12:39 +0100 | [diff] [blame] | 5116 | ectx->ec_where.wt_variable = save_wt_variable; |
Bram Moolenaar | b1040dc | 2022-05-18 11:00:48 +0100 | [diff] [blame] | 5117 | if (r == FAIL) |
| 5118 | goto on_error; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5119 | if (!ectx->ec_where.wt_variable) |
| 5120 | ectx->ec_where.wt_index = 0; |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 5121 | |
| 5122 | // number 0 is FALSE, number 1 is TRUE |
| 5123 | if (tv->v_type == VAR_NUMBER |
| 5124 | && ct->ct_type->tt_type == VAR_BOOL |
| 5125 | && (tv->vval.v_number == 0 |
| 5126 | || tv->vval.v_number == 1)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5127 | { |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 5128 | tv->v_type = VAR_BOOL; |
| 5129 | tv->vval.v_number = tv->vval.v_number |
Bram Moolenaar | dadaddd | 2020-09-12 19:11:23 +0200 | [diff] [blame] | 5130 | ? VVAL_TRUE : VVAL_FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5131 | } |
| 5132 | } |
| 5133 | break; |
| 5134 | |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5135 | case ISN_CHECKLEN: |
| 5136 | { |
| 5137 | int min_len = iptr->isn_arg.checklen.cl_min_len; |
| 5138 | list_T *list = NULL; |
| 5139 | |
| 5140 | tv = STACK_TV_BOT(-1); |
| 5141 | if (tv->v_type == VAR_LIST) |
| 5142 | list = tv->vval.v_list; |
| 5143 | if (list == NULL || list->lv_len < min_len |
| 5144 | || (list->lv_len > min_len |
| 5145 | && !iptr->isn_arg.checklen.cl_more_OK)) |
| 5146 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 5147 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5148 | semsg(_(e_expected_nr_items_but_got_nr), |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5149 | min_len, list == NULL ? 0 : list->lv_len); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 5150 | goto on_error; |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5151 | } |
| 5152 | } |
| 5153 | break; |
| 5154 | |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 5155 | case ISN_SETTYPE: |
Bram Moolenaar | 381692b | 2022-02-02 20:01:27 +0000 | [diff] [blame] | 5156 | set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type); |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 5157 | break; |
| 5158 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5159 | case ISN_2BOOL: |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 5160 | case ISN_COND2BOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5161 | { |
| 5162 | int n; |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 5163 | int error = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5164 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 5165 | if (iptr->isn_type == ISN_2BOOL) |
| 5166 | { |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5167 | tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 5168 | n = tv2bool(tv); |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5169 | if (iptr->isn_arg.tobool.invert) |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 5170 | n = !n; |
| 5171 | } |
| 5172 | else |
| 5173 | { |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5174 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 5175 | SOURCING_LNUM = iptr->isn_lnum; |
| 5176 | n = tv_get_bool_chk(tv, &error); |
| 5177 | if (error) |
| 5178 | goto on_error; |
| 5179 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5180 | clear_tv(tv); |
| 5181 | tv->v_type = VAR_BOOL; |
| 5182 | tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; |
| 5183 | } |
| 5184 | break; |
| 5185 | |
| 5186 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 5187 | case ISN_2STRING_ANY: |
Bram Moolenaar | 0acbf5a | 2021-01-05 20:58:25 +0100 | [diff] [blame] | 5188 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5189 | if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset), |
| 5190 | iptr->isn_type == ISN_2STRING_ANY, |
| 5191 | iptr->isn_arg.tostring.tolerant) == FAIL) |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 5192 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5193 | break; |
| 5194 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 5195 | case ISN_RANGE: |
| 5196 | { |
| 5197 | exarg_T ea; |
| 5198 | char *errormsg; |
| 5199 | |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 5200 | ea.line2 = 0; |
Bram Moolenaar | d1510ee | 2021-01-04 16:15:58 +0100 | [diff] [blame] | 5201 | ea.addr_count = 0; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 5202 | ea.addr_type = ADDR_LINES; |
| 5203 | ea.cmd = iptr->isn_arg.string; |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 5204 | ea.skip = FALSE; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 5205 | if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL) |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 5206 | goto on_error; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 5207 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 5208 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 5209 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5210 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 5211 | tv = STACK_TV_BOT(-1); |
| 5212 | tv->v_type = VAR_NUMBER; |
| 5213 | tv->v_lock = 0; |
Bram Moolenaar | 0665163 | 2022-04-27 17:54:25 +0100 | [diff] [blame] | 5214 | tv->vval.v_number = ea.line2; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 5215 | } |
| 5216 | break; |
| 5217 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 5218 | case ISN_PUT: |
| 5219 | { |
| 5220 | int regname = iptr->isn_arg.put.put_regname; |
| 5221 | linenr_T lnum = iptr->isn_arg.put.put_lnum; |
| 5222 | char_u *expr = NULL; |
| 5223 | int dir = FORWARD; |
| 5224 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 5225 | if (lnum < -2) |
| 5226 | { |
| 5227 | // line number was put on the stack by ISN_RANGE |
| 5228 | tv = STACK_TV_BOT(-1); |
| 5229 | curwin->w_cursor.lnum = tv->vval.v_number; |
| 5230 | if (lnum == LNUM_VARIABLE_RANGE_ABOVE) |
| 5231 | dir = BACKWARD; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5232 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 5233 | } |
| 5234 | else if (lnum == -2) |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 5235 | // :put! above cursor |
| 5236 | dir = BACKWARD; |
| 5237 | else if (lnum >= 0) |
Bram Moolenaar | 4e713ba | 2022-02-07 15:31:37 +0000 | [diff] [blame] | 5238 | { |
| 5239 | curwin->w_cursor.lnum = lnum; |
| 5240 | if (lnum == 0) |
| 5241 | // check_cursor() below will move to line 1 |
| 5242 | dir = BACKWARD; |
| 5243 | } |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 5244 | |
| 5245 | if (regname == '=') |
| 5246 | { |
| 5247 | tv = STACK_TV_BOT(-1); |
| 5248 | if (tv->v_type == VAR_STRING) |
| 5249 | expr = tv->vval.v_string; |
| 5250 | else |
| 5251 | { |
| 5252 | expr = typval2string(tv, TRUE); // allocates value |
| 5253 | clear_tv(tv); |
| 5254 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5255 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 5256 | } |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 5257 | check_cursor(); |
| 5258 | do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE); |
| 5259 | vim_free(expr); |
| 5260 | } |
| 5261 | break; |
| 5262 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 5263 | case ISN_CMDMOD: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5264 | ectx->ec_funclocal.floc_save_cmdmod = cmdmod; |
| 5265 | ectx->ec_funclocal.floc_restore_cmdmod = TRUE; |
| 5266 | ectx->ec_funclocal.floc_restore_cmdmod_stacklen = |
| 5267 | ectx->ec_stack.ga_len; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 5268 | cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod; |
| 5269 | apply_cmdmod(&cmdmod); |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 5270 | break; |
| 5271 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 5272 | case ISN_CMDMOD_REV: |
| 5273 | // filter regprog is owned by the instruction, don't free it |
| 5274 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 5275 | undo_cmdmod(&cmdmod); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5276 | cmdmod = ectx->ec_funclocal.floc_save_cmdmod; |
| 5277 | ectx->ec_funclocal.floc_restore_cmdmod = FALSE; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 5278 | break; |
| 5279 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 5280 | case ISN_UNPACK: |
| 5281 | { |
| 5282 | int count = iptr->isn_arg.unpack.unp_count; |
| 5283 | int semicolon = iptr->isn_arg.unpack.unp_semicolon; |
| 5284 | list_T *l; |
| 5285 | listitem_T *li; |
| 5286 | int i; |
| 5287 | |
| 5288 | // Check there is a valid list to unpack. |
| 5289 | tv = STACK_TV_BOT(-1); |
| 5290 | if (tv->v_type != VAR_LIST) |
| 5291 | { |
| 5292 | SOURCING_LNUM = iptr->isn_lnum; |
| 5293 | emsg(_(e_for_argument_must_be_sequence_of_lists)); |
| 5294 | goto on_error; |
| 5295 | } |
| 5296 | l = tv->vval.v_list; |
| 5297 | if (l == NULL |
| 5298 | || l->lv_len < (semicolon ? count - 1 : count)) |
| 5299 | { |
| 5300 | SOURCING_LNUM = iptr->isn_lnum; |
| 5301 | emsg(_(e_list_value_does_not_have_enough_items)); |
| 5302 | goto on_error; |
| 5303 | } |
| 5304 | else if (!semicolon && l->lv_len > count) |
| 5305 | { |
| 5306 | SOURCING_LNUM = iptr->isn_lnum; |
| 5307 | emsg(_(e_list_value_has_more_items_than_targets)); |
| 5308 | goto on_error; |
| 5309 | } |
| 5310 | |
| 5311 | CHECK_LIST_MATERIALIZE(l); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 5312 | if (GA_GROW_FAILS(&ectx->ec_stack, count - 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 5313 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5314 | ectx->ec_stack.ga_len += count - 1; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 5315 | |
| 5316 | // Variable after semicolon gets a list with the remaining |
| 5317 | // items. |
| 5318 | if (semicolon) |
| 5319 | { |
| 5320 | list_T *rem_list = |
| 5321 | list_alloc_with_items(l->lv_len - count + 1); |
| 5322 | |
| 5323 | if (rem_list == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 5324 | goto theend; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 5325 | tv = STACK_TV_BOT(-count); |
| 5326 | tv->vval.v_list = rem_list; |
| 5327 | ++rem_list->lv_refcount; |
| 5328 | tv->v_lock = 0; |
| 5329 | li = l->lv_first; |
| 5330 | for (i = 0; i < count - 1; ++i) |
| 5331 | li = li->li_next; |
| 5332 | for (i = 0; li != NULL; ++i) |
| 5333 | { |
Bram Moolenaar | 61efa16 | 2022-03-18 13:10:48 +0000 | [diff] [blame] | 5334 | typval_T tvcopy; |
| 5335 | |
| 5336 | copy_tv(&li->li_tv, &tvcopy); |
| 5337 | list_set_item(rem_list, i, &tvcopy); |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 5338 | li = li->li_next; |
| 5339 | } |
| 5340 | --count; |
| 5341 | } |
| 5342 | |
| 5343 | // Produce the values in reverse order, first item last. |
| 5344 | li = l->lv_first; |
| 5345 | for (i = 0; i < count; ++i) |
| 5346 | { |
| 5347 | tv = STACK_TV_BOT(-i - 1); |
| 5348 | copy_tv(&li->li_tv, tv); |
| 5349 | li = li->li_next; |
| 5350 | } |
| 5351 | |
| 5352 | list_unref(l); |
| 5353 | } |
| 5354 | break; |
| 5355 | |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 5356 | case ISN_PROF_START: |
| 5357 | case ISN_PROF_END: |
| 5358 | { |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 5359 | #ifdef FEAT_PROFILE |
Bram Moolenaar | ca16c60 | 2022-09-06 18:57:08 +0100 | [diff] [blame] | 5360 | funccall_T cookie; |
| 5361 | ufunc_T *cur_ufunc = |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 5362 | (((dfunc_T *)def_functions.ga_data) |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 5363 | + ectx->ec_dfunc_idx)->df_ufunc; |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 5364 | |
Bram Moolenaar | ca16c60 | 2022-09-06 18:57:08 +0100 | [diff] [blame] | 5365 | cookie.fc_func = cur_ufunc; |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 5366 | if (iptr->isn_type == ISN_PROF_START) |
| 5367 | { |
| 5368 | func_line_start(&cookie, iptr->isn_lnum); |
| 5369 | // if we get here the instruction is executed |
| 5370 | func_line_exec(&cookie); |
| 5371 | } |
| 5372 | else |
| 5373 | func_line_end(&cookie); |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 5374 | #endif |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 5375 | } |
| 5376 | break; |
| 5377 | |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5378 | case ISN_DEBUG: |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 5379 | handle_debug(iptr, ectx); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5380 | break; |
| 5381 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 5382 | case ISN_SHUFFLE: |
| 5383 | { |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 5384 | typval_T tmp_tv; |
| 5385 | int item = iptr->isn_arg.shuffle.shfl_item; |
| 5386 | int up = iptr->isn_arg.shuffle.shfl_up; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 5387 | |
| 5388 | tmp_tv = *STACK_TV_BOT(-item); |
| 5389 | for ( ; up > 0 && item > 1; --up) |
| 5390 | { |
| 5391 | *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1); |
| 5392 | --item; |
| 5393 | } |
| 5394 | *STACK_TV_BOT(-item) = tmp_tv; |
| 5395 | } |
| 5396 | break; |
| 5397 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5398 | case ISN_DROP: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5399 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5400 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5401 | ectx->ec_where.wt_index = 0; |
| 5402 | ectx->ec_where.wt_variable = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5403 | break; |
| 5404 | } |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 5405 | continue; |
| 5406 | |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 5407 | func_return: |
Bram Moolenaar | 7cbfaa5 | 2020-09-18 21:25:32 +0200 | [diff] [blame] | 5408 | // Restore previous function. If the frame pointer is where we started |
| 5409 | // then there is none and we are done. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5410 | if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 5411 | goto done; |
Bram Moolenaar | 7cbfaa5 | 2020-09-18 21:25:32 +0200 | [diff] [blame] | 5412 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5413 | if (func_return(ectx) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 5414 | // only fails when out of memory |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 5415 | goto theend; |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 5416 | continue; |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 5417 | |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 5418 | on_error: |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 5419 | // Jump here for an error that does not require aborting execution. |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 5420 | // If "emsg_silent" is set then ignore the error, unless it was set |
| 5421 | // when calling the function. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5422 | if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 5423 | && emsg_silent && did_emsg_def == 0) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 5424 | { |
| 5425 | // If a sequence of instructions causes an error while ":silent!" |
| 5426 | // was used, restore the stack length and jump ahead to restoring |
| 5427 | // the cmdmod. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5428 | if (ectx->ec_funclocal.floc_restore_cmdmod) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 5429 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5430 | while (ectx->ec_stack.ga_len |
| 5431 | > ectx->ec_funclocal.floc_restore_cmdmod_stacklen) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 5432 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5433 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 5434 | clear_tv(STACK_TV_BOT(0)); |
| 5435 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5436 | while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV) |
| 5437 | ++ectx->ec_iidx; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 5438 | } |
Bram Moolenaar | cd030c4 | 2020-10-30 21:49:40 +0100 | [diff] [blame] | 5439 | continue; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 5440 | } |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 5441 | on_fatal_error: |
| 5442 | // Jump here for an error that messes up the stack. |
Bram Moolenaar | 171fb92 | 2020-10-28 16:54:47 +0100 | [diff] [blame] | 5443 | // If we are not inside a try-catch started here, abort execution. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5444 | if (trylevel <= ectx->ec_trylevel_at_start) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 5445 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | done: |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 5449 | ret = OK; |
| 5450 | theend: |
Bram Moolenaar | 5877985 | 2022-09-06 18:31:14 +0100 | [diff] [blame] | 5451 | may_invoke_defer_funcs(ectx); |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 5452 | |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 5453 | dict_stack_clear(dict_stack_len_at_start); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 5454 | ectx->ec_trylevel_at_start = save_trylevel_at_start; |
| 5455 | return ret; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | /* |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 5459 | * Execute the instructions from a VAR_INSTR typval and put the result in |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 5460 | * "rettv". |
| 5461 | * Return OK or FAIL. |
| 5462 | */ |
| 5463 | int |
| 5464 | exe_typval_instr(typval_T *tv, typval_T *rettv) |
| 5465 | { |
| 5466 | ectx_T *ectx = tv->vval.v_instr->instr_ectx; |
| 5467 | isn_T *save_instr = ectx->ec_instr; |
| 5468 | int save_iidx = ectx->ec_iidx; |
| 5469 | int res; |
| 5470 | |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 5471 | // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv) |
| 5472 | // even when the compilation fails. |
| 5473 | rettv->v_type = VAR_UNKNOWN; |
| 5474 | |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 5475 | ectx->ec_instr = tv->vval.v_instr->instr_instr; |
| 5476 | res = exec_instructions(ectx); |
| 5477 | if (res == OK) |
| 5478 | { |
| 5479 | *rettv = *STACK_TV_BOT(-1); |
| 5480 | --ectx->ec_stack.ga_len; |
| 5481 | } |
| 5482 | |
| 5483 | ectx->ec_instr = save_instr; |
| 5484 | ectx->ec_iidx = save_iidx; |
| 5485 | |
| 5486 | return res; |
| 5487 | } |
| 5488 | |
| 5489 | /* |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5490 | * Execute the instructions from an ISN_SUBSTITUTE command, which are in |
| 5491 | * "substitute_instr". |
| 5492 | */ |
| 5493 | char_u * |
| 5494 | exe_substitute_instr(void) |
| 5495 | { |
| 5496 | ectx_T *ectx = substitute_instr->subs_ectx; |
| 5497 | isn_T *save_instr = ectx->ec_instr; |
| 5498 | int save_iidx = ectx->ec_iidx; |
| 5499 | char_u *res; |
| 5500 | |
| 5501 | ectx->ec_instr = substitute_instr->subs_instr; |
| 5502 | if (exec_instructions(ectx) == OK) |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 5503 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5504 | typval_T *tv = STACK_TV_BOT(-1); |
| 5505 | |
Bram Moolenaar | 2752360 | 2021-06-05 21:36:19 +0200 | [diff] [blame] | 5506 | res = typval2string(tv, TRUE); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5507 | --ectx->ec_stack.ga_len; |
| 5508 | clear_tv(tv); |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 5509 | } |
| 5510 | else |
| 5511 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5512 | substitute_instr->subs_status = FAIL; |
| 5513 | res = vim_strsave((char_u *)""); |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 5514 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5515 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5516 | ectx->ec_instr = save_instr; |
| 5517 | ectx->ec_iidx = save_iidx; |
| 5518 | |
| 5519 | return res; |
| 5520 | } |
| 5521 | |
| 5522 | /* |
| 5523 | * Call a "def" function from old Vim script. |
| 5524 | * Return OK or FAIL. |
| 5525 | */ |
| 5526 | int |
| 5527 | call_def_function( |
| 5528 | ufunc_T *ufunc, |
| 5529 | int argc_arg, // nr of arguments |
| 5530 | typval_T *argv, // arguments |
Bram Moolenaar | c9c967d | 2022-09-07 16:48:46 +0100 | [diff] [blame] | 5531 | int flags, // DEF_ flags |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5532 | partial_T *partial, // optional partial for context |
Bram Moolenaar | 5877985 | 2022-09-06 18:31:14 +0100 | [diff] [blame] | 5533 | funccall_T *funccal, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5534 | typval_T *rettv) // return value |
| 5535 | { |
| 5536 | ectx_T ectx; // execution context |
| 5537 | int argc = argc_arg; |
Bram Moolenaar | c9c967d | 2022-09-07 16:48:46 +0100 | [diff] [blame] | 5538 | int partial_argc = partial == NULL |
| 5539 | || (flags & DEF_USE_PT_ARGV) == 0 |
| 5540 | ? 0 : partial->pt_argc; |
| 5541 | int total_argc = argc + partial_argc; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5542 | typval_T *tv; |
| 5543 | int idx; |
| 5544 | int ret = FAIL; |
Bram Moolenaar | c9c967d | 2022-09-07 16:48:46 +0100 | [diff] [blame] | 5545 | int defcount = ufunc->uf_args.ga_len - total_argc; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5546 | sctx_T save_current_sctx = current_sctx; |
| 5547 | int did_emsg_before = did_emsg_cumul + did_emsg; |
| 5548 | int save_suppress_errthrow = suppress_errthrow; |
| 5549 | msglist_T **saved_msg_list = NULL; |
| 5550 | msglist_T *private_msg_list = NULL; |
| 5551 | int save_emsg_silent_def = emsg_silent_def; |
| 5552 | int save_did_emsg_def = did_emsg_def; |
| 5553 | int orig_funcdepth; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5554 | int orig_nesting_level = ex_nesting_level; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5555 | |
| 5556 | // Get pointer to item in the stack. |
| 5557 | #undef STACK_TV |
| 5558 | #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx) |
| 5559 | |
| 5560 | // Get pointer to item at the bottom of the stack, -1 is the bottom. |
| 5561 | #undef STACK_TV_BOT |
| 5562 | #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx) |
| 5563 | |
| 5564 | // Get pointer to a local variable on the stack. Negative for arguments. |
| 5565 | #undef STACK_TV_VAR |
| 5566 | #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx) |
| 5567 | |
| 5568 | if (ufunc->uf_def_status == UF_NOT_COMPILED |
| 5569 | || ufunc->uf_def_status == UF_COMPILE_ERROR |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 5570 | || (func_needs_compiling(ufunc, get_compile_type(ufunc)) |
| 5571 | && compile_def_function(ufunc, FALSE, |
| 5572 | get_compile_type(ufunc), NULL) == FAIL)) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5573 | { |
| 5574 | if (did_emsg_cumul + did_emsg == did_emsg_before) |
| 5575 | semsg(_(e_function_is_not_compiled_str), |
| 5576 | printable_func_name(ufunc)); |
| 5577 | return FAIL; |
| 5578 | } |
| 5579 | |
| 5580 | { |
| 5581 | // Check the function was really compiled. |
| 5582 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 5583 | + ufunc->uf_dfunc_idx; |
| 5584 | if (INSTRUCTIONS(dfunc) == NULL) |
| 5585 | { |
| 5586 | iemsg("using call_def_function() on not compiled function"); |
| 5587 | return FAIL; |
| 5588 | } |
| 5589 | } |
| 5590 | |
| 5591 | // If depth of calling is getting too high, don't execute the function. |
| 5592 | orig_funcdepth = funcdepth_get(); |
| 5593 | if (funcdepth_increment() == FAIL) |
| 5594 | return FAIL; |
| 5595 | |
| 5596 | CLEAR_FIELD(ectx); |
| 5597 | ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx; |
| 5598 | ga_init2(&ectx.ec_stack, sizeof(typval_T), 500); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 5599 | if (GA_GROW_FAILS(&ectx.ec_stack, 20)) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5600 | { |
| 5601 | funcdepth_decrement(); |
| 5602 | return FAIL; |
| 5603 | } |
| 5604 | ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10); |
| 5605 | ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10); |
| 5606 | ectx.ec_did_emsg_before = did_emsg_before; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5607 | ++ex_nesting_level; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5608 | |
Bram Moolenaar | c9c967d | 2022-09-07 16:48:46 +0100 | [diff] [blame] | 5609 | idx = total_argc - ufunc->uf_args.ga_len; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5610 | if (idx > 0 && ufunc->uf_va_name == NULL) |
| 5611 | { |
Matvey Tarasov | d14bb1a | 2022-06-29 13:18:27 +0100 | [diff] [blame] | 5612 | semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many, |
Bram Moolenaar | c9c967d | 2022-09-07 16:48:46 +0100 | [diff] [blame] | 5613 | idx), idx); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5614 | goto failed_early; |
| 5615 | } |
Bram Moolenaar | c9c967d | 2022-09-07 16:48:46 +0100 | [diff] [blame] | 5616 | idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len; |
Bram Moolenaar | c6d7153 | 2021-06-05 18:49:38 +0200 | [diff] [blame] | 5617 | if (idx < 0) |
Bram Moolenaar | 8da6d6d | 2021-06-05 18:15:09 +0200 | [diff] [blame] | 5618 | { |
Matvey Tarasov | d14bb1a | 2022-06-29 13:18:27 +0100 | [diff] [blame] | 5619 | semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few, |
Bram Moolenaar | 98aff65 | 2022-09-06 21:02:35 +0100 | [diff] [blame] | 5620 | -idx), -idx); |
Bram Moolenaar | 8da6d6d | 2021-06-05 18:15:09 +0200 | [diff] [blame] | 5621 | goto failed_early; |
| 5622 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5623 | |
Bram Moolenaar | c9c967d | 2022-09-07 16:48:46 +0100 | [diff] [blame] | 5624 | // Put values from the partial and arguments on the stack, but no more than |
| 5625 | // what the function expects. A lambda can be called with more arguments |
| 5626 | // than it uses. |
| 5627 | for (idx = 0; idx < total_argc |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5628 | && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len); |
| 5629 | ++idx) |
| 5630 | { |
Bram Moolenaar | c9c967d | 2022-09-07 16:48:46 +0100 | [diff] [blame] | 5631 | int argv_idx = idx - partial_argc; |
| 5632 | |
| 5633 | tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5634 | if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len |
Bram Moolenaar | c9c967d | 2022-09-07 16:48:46 +0100 | [diff] [blame] | 5635 | && tv->v_type == VAR_SPECIAL |
| 5636 | && tv->vval.v_number == VVAL_NONE) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5637 | { |
| 5638 | // Use the default value. |
| 5639 | STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; |
| 5640 | } |
| 5641 | else |
| 5642 | { |
| 5643 | if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len |
| 5644 | && check_typval_arg_type( |
Bram Moolenaar | c9c967d | 2022-09-07 16:48:46 +0100 | [diff] [blame] | 5645 | ufunc->uf_arg_types[idx], tv, |
| 5646 | NULL, argv_idx + 1) == FAIL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5647 | goto failed_early; |
Bram Moolenaar | c9c967d | 2022-09-07 16:48:46 +0100 | [diff] [blame] | 5648 | copy_tv(tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5649 | } |
| 5650 | ++ectx.ec_stack.ga_len; |
| 5651 | } |
| 5652 | |
| 5653 | // Turn varargs into a list. Empty list if no args. |
| 5654 | if (ufunc->uf_va_name != NULL) |
| 5655 | { |
| 5656 | int vararg_count = argc - ufunc->uf_args.ga_len; |
| 5657 | |
| 5658 | if (vararg_count < 0) |
| 5659 | vararg_count = 0; |
| 5660 | else |
| 5661 | argc -= vararg_count; |
| 5662 | if (exe_newlist(vararg_count, &ectx) == FAIL) |
| 5663 | goto failed_early; |
| 5664 | |
| 5665 | // Check the type of the list items. |
| 5666 | tv = STACK_TV_BOT(-1); |
| 5667 | if (ufunc->uf_va_type != NULL |
| 5668 | && ufunc->uf_va_type != &t_list_any |
| 5669 | && ufunc->uf_va_type->tt_member != &t_any |
| 5670 | && tv->vval.v_list != NULL) |
| 5671 | { |
| 5672 | type_T *expected = ufunc->uf_va_type->tt_member; |
| 5673 | listitem_T *li = tv->vval.v_list->lv_first; |
| 5674 | |
| 5675 | for (idx = 0; idx < vararg_count; ++idx) |
| 5676 | { |
| 5677 | if (check_typval_arg_type(expected, &li->li_tv, |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 5678 | NULL, argc + idx + 1) == FAIL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5679 | goto failed_early; |
| 5680 | li = li->li_next; |
| 5681 | } |
| 5682 | } |
| 5683 | |
| 5684 | if (defcount > 0) |
| 5685 | // Move varargs list to below missing default arguments. |
| 5686 | *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1); |
| 5687 | --ectx.ec_stack.ga_len; |
| 5688 | } |
| 5689 | |
| 5690 | // Make space for omitted arguments, will store default value below. |
| 5691 | // Any varargs list goes after them. |
| 5692 | if (defcount > 0) |
| 5693 | for (idx = 0; idx < defcount; ++idx) |
| 5694 | { |
| 5695 | STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; |
| 5696 | ++ectx.ec_stack.ga_len; |
| 5697 | } |
| 5698 | if (ufunc->uf_va_name != NULL) |
| 5699 | ++ectx.ec_stack.ga_len; |
| 5700 | |
| 5701 | // Frame pointer points to just after arguments. |
| 5702 | ectx.ec_frame_idx = ectx.ec_stack.ga_len; |
| 5703 | ectx.ec_initial_frame_idx = ectx.ec_frame_idx; |
| 5704 | |
| 5705 | { |
| 5706 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 5707 | + ufunc->uf_dfunc_idx; |
| 5708 | ufunc_T *base_ufunc = dfunc->df_ufunc; |
| 5709 | |
| 5710 | // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 5711 | // by copy_lambda_to_global_func(). |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5712 | if (partial != NULL || base_ufunc->uf_partial != NULL) |
| 5713 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5714 | ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T); |
| 5715 | if (ectx.ec_outer_ref == NULL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5716 | goto failed_early; |
| 5717 | if (partial != NULL) |
| 5718 | { |
Bram Moolenaar | 7aca5ca | 2022-02-07 19:56:43 +0000 | [diff] [blame] | 5719 | outer_T *outer = get_pt_outer(partial); |
| 5720 | |
Bram Moolenaar | 0cdfb7c | 2022-09-17 15:44:52 +0100 | [diff] [blame] | 5721 | if (outer->out_stack == NULL && outer->out_loop_stack == NULL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5722 | { |
Bram Moolenaar | fe1bfc9 | 2022-02-06 13:55:03 +0000 | [diff] [blame] | 5723 | if (current_ectx != NULL) |
| 5724 | { |
| 5725 | if (current_ectx->ec_outer_ref != NULL |
| 5726 | && current_ectx->ec_outer_ref->or_outer != NULL) |
| 5727 | ectx.ec_outer_ref->or_outer = |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5728 | current_ectx->ec_outer_ref->or_outer; |
Bram Moolenaar | fe1bfc9 | 2022-02-06 13:55:03 +0000 | [diff] [blame] | 5729 | } |
Bram Moolenaar | 0cdfb7c | 2022-09-17 15:44:52 +0100 | [diff] [blame] | 5730 | // else: should there be an error here? |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5731 | } |
| 5732 | else |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5733 | { |
Bram Moolenaar | 7aca5ca | 2022-02-07 19:56:43 +0000 | [diff] [blame] | 5734 | ectx.ec_outer_ref->or_outer = outer; |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5735 | ++partial->pt_refcount; |
| 5736 | ectx.ec_outer_ref->or_partial = partial; |
| 5737 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5738 | } |
| 5739 | else |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5740 | { |
| 5741 | ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer; |
| 5742 | ++base_ufunc->uf_partial->pt_refcount; |
| 5743 | ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial; |
| 5744 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5745 | } |
| 5746 | } |
| 5747 | |
| 5748 | // dummy frame entries |
| 5749 | for (idx = 0; idx < STACK_FRAME_SIZE; ++idx) |
| 5750 | { |
| 5751 | STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN; |
| 5752 | ++ectx.ec_stack.ga_len; |
| 5753 | } |
| 5754 | |
| 5755 | { |
| 5756 | // Reserve space for local variables and any closure reference count. |
| 5757 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 5758 | + ufunc->uf_dfunc_idx; |
| 5759 | |
Bram Moolenaar | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 5760 | // Initialize variables to zero. That avoids having to generate |
| 5761 | // initializing instructions for "var nr: number", "var x: any", etc. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5762 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
Bram Moolenaar | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 5763 | { |
| 5764 | STACK_TV_VAR(idx)->v_type = VAR_NUMBER; |
| 5765 | STACK_TV_VAR(idx)->vval.v_number = 0; |
| 5766 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5767 | ectx.ec_stack.ga_len += dfunc->df_varcount; |
| 5768 | if (dfunc->df_has_closure) |
| 5769 | { |
| 5770 | STACK_TV_VAR(idx)->v_type = VAR_NUMBER; |
| 5771 | STACK_TV_VAR(idx)->vval.v_number = 0; |
| 5772 | ++ectx.ec_stack.ga_len; |
| 5773 | } |
| 5774 | |
| 5775 | ectx.ec_instr = INSTRUCTIONS(dfunc); |
| 5776 | } |
| 5777 | |
Bram Moolenaar | 5877985 | 2022-09-06 18:31:14 +0100 | [diff] [blame] | 5778 | // Store the execution context in funccal, used by invoke_all_defer(). |
| 5779 | if (funccal != NULL) |
| 5780 | funccal->fc_ectx = &ectx; |
| 5781 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5782 | // Following errors are in the function, not the caller. |
| 5783 | // Commands behave like vim9script. |
| 5784 | estack_push_ufunc(ufunc, 1); |
| 5785 | current_sctx = ufunc->uf_script_ctx; |
| 5786 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 5787 | |
| 5788 | // Use a specific location for storing error messages to be converted to an |
| 5789 | // exception. |
| 5790 | saved_msg_list = msg_list; |
| 5791 | msg_list = &private_msg_list; |
| 5792 | |
| 5793 | // Do turn errors into exceptions. |
| 5794 | suppress_errthrow = FALSE; |
| 5795 | |
| 5796 | // Do not delete the function while executing it. |
| 5797 | ++ufunc->uf_calls; |
| 5798 | |
| 5799 | // When ":silent!" was used before calling then we still abort the |
| 5800 | // function. If ":silent!" is used in the function then we don't. |
| 5801 | emsg_silent_def = emsg_silent; |
| 5802 | did_emsg_def = 0; |
| 5803 | |
| 5804 | ectx.ec_where.wt_index = 0; |
| 5805 | ectx.ec_where.wt_variable = FALSE; |
| 5806 | |
| 5807 | // Execute the instructions until done. |
| 5808 | ret = exec_instructions(&ectx); |
| 5809 | if (ret == OK) |
| 5810 | { |
| 5811 | // function finished, get result from the stack. |
| 5812 | if (ufunc->uf_ret_type == &t_void) |
| 5813 | { |
| 5814 | rettv->v_type = VAR_VOID; |
| 5815 | } |
| 5816 | else |
| 5817 | { |
| 5818 | tv = STACK_TV_BOT(-1); |
| 5819 | *rettv = *tv; |
| 5820 | tv->v_type = VAR_UNKNOWN; |
| 5821 | } |
| 5822 | } |
| 5823 | |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 5824 | // When failed need to unwind the call stack. |
Bram Moolenaar | 5877985 | 2022-09-06 18:31:14 +0100 | [diff] [blame] | 5825 | unwind_def_callstack(&ectx); |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 5826 | |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 5827 | // Deal with any remaining closures, they may be in use somewhere. |
| 5828 | if (ectx.ec_funcrefs.ga_len > 0) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 5829 | { |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 5830 | handle_closure_in_use(&ectx, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 5831 | ga_clear(&ectx.ec_funcrefs); |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 5832 | } |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 5833 | |
Bram Moolenaar | ee8580e | 2020-08-28 17:19:07 +0200 | [diff] [blame] | 5834 | estack_pop(); |
| 5835 | current_sctx = save_current_sctx; |
| 5836 | |
Bram Moolenaar | 2336c37 | 2021-12-06 15:06:54 +0000 | [diff] [blame] | 5837 | if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0) |
| 5838 | // Function was unreferenced while being used, free it now. |
| 5839 | func_clear_free(ufunc, FALSE); |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 5840 | |
Bram Moolenaar | 352134b | 2020-10-17 22:04:08 +0200 | [diff] [blame] | 5841 | if (*msg_list != NULL && saved_msg_list != NULL) |
| 5842 | { |
| 5843 | msglist_T **plist = saved_msg_list; |
| 5844 | |
| 5845 | // Append entries from the current msg_list (uncaught exceptions) to |
| 5846 | // the saved msg_list. |
| 5847 | while (*plist != NULL) |
| 5848 | plist = &(*plist)->next; |
| 5849 | |
| 5850 | *plist = *msg_list; |
| 5851 | } |
| 5852 | msg_list = saved_msg_list; |
| 5853 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5854 | if (ectx.ec_funclocal.floc_restore_cmdmod) |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 5855 | { |
| 5856 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 5857 | undo_cmdmod(&cmdmod); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5858 | cmdmod = ectx.ec_funclocal.floc_save_cmdmod; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 5859 | } |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 5860 | emsg_silent_def = save_emsg_silent_def; |
| 5861 | did_emsg_def += save_did_emsg_def; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 5862 | |
Bram Moolenaar | ee8580e | 2020-08-28 17:19:07 +0200 | [diff] [blame] | 5863 | failed_early: |
Bram Moolenaar | 0d1f55c | 2022-04-05 17:30:29 +0100 | [diff] [blame] | 5864 | // Free all arguments and local variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5865 | for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) |
| 5866 | clear_tv(STACK_TV(idx)); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5867 | ex_nesting_level = orig_nesting_level; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 5868 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5869 | vim_free(ectx.ec_stack.ga_data); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5870 | vim_free(ectx.ec_trystack.ga_data); |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5871 | if (ectx.ec_outer_ref != NULL) |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 5872 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5873 | if (ectx.ec_outer_ref->or_outer_allocated) |
| 5874 | vim_free(ectx.ec_outer_ref->or_outer); |
| 5875 | partial_unref(ectx.ec_outer_ref->or_partial); |
| 5876 | vim_free(ectx.ec_outer_ref); |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 5877 | } |
| 5878 | |
Bram Moolenaar | 77e5dcc | 2020-09-17 21:29:03 +0200 | [diff] [blame] | 5879 | // Not sure if this is necessary. |
| 5880 | suppress_errthrow = save_suppress_errthrow; |
| 5881 | |
Bram Moolenaar | b5841b9 | 2021-07-15 18:09:53 +0200 | [diff] [blame] | 5882 | if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before |
| 5883 | && !need_rethrow) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5884 | semsg(_(e_unknown_error_while_executing_str), |
Bram Moolenaar | 682d0a1 | 2020-07-19 20:48:59 +0200 | [diff] [blame] | 5885 | printable_func_name(ufunc)); |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 5886 | funcdepth_restore(orig_funcdepth); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5887 | return ret; |
| 5888 | } |
| 5889 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5890 | /* |
Bram Moolenaar | 5877985 | 2022-09-06 18:31:14 +0100 | [diff] [blame] | 5891 | * Called when a def function has finished (possibly failed). |
| 5892 | * Invoke all the function returns to clean up and invoke deferred functions, |
| 5893 | * except the toplevel one. |
| 5894 | */ |
| 5895 | void |
| 5896 | unwind_def_callstack(ectx_T *ectx) |
| 5897 | { |
| 5898 | while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx) |
| 5899 | func_return(ectx); |
| 5900 | } |
| 5901 | |
| 5902 | /* |
| 5903 | * Invoke any deffered functions for the top function in "ectx". |
| 5904 | */ |
| 5905 | void |
| 5906 | may_invoke_defer_funcs(ectx_T *ectx) |
| 5907 | { |
| 5908 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx; |
| 5909 | |
| 5910 | if (dfunc->df_defer_var_idx > 0) |
| 5911 | invoke_defer_funcs(ectx); |
| 5912 | } |
| 5913 | |
| 5914 | /* |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5915 | * List instructions "instr" up to "instr_count" or until ISN_FINISH. |
| 5916 | * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE. |
| 5917 | * "pfx" is prefixed to every line. |
| 5918 | */ |
| 5919 | static void |
| 5920 | list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc) |
| 5921 | { |
| 5922 | int line_idx = 0; |
| 5923 | int prev_current = 0; |
| 5924 | int current; |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 5925 | int def_arg_idx = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5926 | |
| 5927 | for (current = 0; current < instr_count; ++current) |
| 5928 | { |
| 5929 | isn_T *iptr = &instr[current]; |
| 5930 | char *line; |
| 5931 | |
| 5932 | if (ufunc != NULL) |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 5933 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5934 | while (line_idx < iptr->isn_lnum |
| 5935 | && line_idx < ufunc->uf_lines.ga_len) |
| 5936 | { |
| 5937 | if (current > prev_current) |
| 5938 | { |
| 5939 | msg_puts("\n\n"); |
| 5940 | prev_current = current; |
| 5941 | } |
| 5942 | line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; |
| 5943 | if (line != NULL) |
| 5944 | msg(line); |
| 5945 | } |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 5946 | if (iptr->isn_type == ISN_JUMP_IF_ARG_SET) |
| 5947 | { |
| 5948 | int first_def_arg = ufunc->uf_args.ga_len |
| 5949 | - ufunc->uf_def_args.ga_len; |
| 5950 | |
| 5951 | if (def_arg_idx > 0) |
| 5952 | msg_puts("\n\n"); |
| 5953 | msg_start(); |
| 5954 | msg_puts(" "); |
| 5955 | msg_puts(((char **)(ufunc->uf_args.ga_data))[ |
| 5956 | first_def_arg + def_arg_idx]); |
| 5957 | msg_puts(" = "); |
| 5958 | msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]); |
| 5959 | msg_clr_eos(); |
| 5960 | msg_end(); |
| 5961 | } |
| 5962 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5963 | |
| 5964 | switch (iptr->isn_type) |
| 5965 | { |
| 5966 | case ISN_EXEC: |
| 5967 | smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string); |
| 5968 | break; |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 5969 | case ISN_EXEC_SPLIT: |
| 5970 | smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string); |
| 5971 | break; |
Bram Moolenaar | e4eed8c | 2021-12-01 15:22:56 +0000 | [diff] [blame] | 5972 | case ISN_EXECRANGE: |
| 5973 | smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string); |
| 5974 | break; |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 5975 | case ISN_LEGACY_EVAL: |
| 5976 | smsg("%s%4d EVAL legacy %s", pfx, current, |
| 5977 | iptr->isn_arg.string); |
| 5978 | break; |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 5979 | case ISN_REDIRSTART: |
| 5980 | smsg("%s%4d REDIR", pfx, current); |
| 5981 | break; |
| 5982 | case ISN_REDIREND: |
| 5983 | smsg("%s%4d REDIR END%s", pfx, current, |
| 5984 | iptr->isn_arg.number ? " append" : ""); |
| 5985 | break; |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5986 | case ISN_CEXPR_AUCMD: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 5987 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5988 | smsg("%s%4d CEXPR pre %s", pfx, current, |
| 5989 | cexpr_get_auname(iptr->isn_arg.number)); |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 5990 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5991 | break; |
| 5992 | case ISN_CEXPR_CORE: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 5993 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5994 | { |
| 5995 | cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref; |
| 5996 | |
| 5997 | smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current, |
| 5998 | cexpr_get_auname(cer->cer_cmdidx), |
| 5999 | cer->cer_forceit ? "!" : "", |
| 6000 | cer->cer_cmdline); |
| 6001 | } |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 6002 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 6003 | break; |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 6004 | case ISN_INSTR: |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 6005 | smsg("%s%4d INSTR", pfx, current); |
| 6006 | list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL); |
| 6007 | msg(" -------------"); |
| 6008 | break; |
| 6009 | case ISN_SOURCE: |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 6010 | { |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 6011 | scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number); |
| 6012 | |
| 6013 | smsg("%s%4d SOURCE %s", pfx, current, si->sn_name); |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 6014 | } |
| 6015 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6016 | case ISN_SUBSTITUTE: |
| 6017 | { |
| 6018 | subs_T *subs = &iptr->isn_arg.subs; |
| 6019 | |
| 6020 | smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd); |
| 6021 | list_instructions(" ", subs->subs_instr, INT_MAX, NULL); |
| 6022 | msg(" -------------"); |
| 6023 | } |
| 6024 | break; |
| 6025 | case ISN_EXECCONCAT: |
| 6026 | smsg("%s%4d EXECCONCAT %lld", pfx, current, |
| 6027 | (varnumber_T)iptr->isn_arg.number); |
| 6028 | break; |
| 6029 | case ISN_ECHO: |
| 6030 | { |
| 6031 | echo_T *echo = &iptr->isn_arg.echo; |
| 6032 | |
| 6033 | smsg("%s%4d %s %d", pfx, current, |
| 6034 | echo->echo_with_white ? "ECHO" : "ECHON", |
| 6035 | echo->echo_count); |
| 6036 | } |
| 6037 | break; |
| 6038 | case ISN_EXECUTE: |
| 6039 | smsg("%s%4d EXECUTE %lld", pfx, current, |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 6040 | (varnumber_T)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6041 | break; |
| 6042 | case ISN_ECHOMSG: |
| 6043 | smsg("%s%4d ECHOMSG %lld", pfx, current, |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 6044 | (varnumber_T)(iptr->isn_arg.number)); |
| 6045 | break; |
Bram Moolenaar | 7d7ad7b | 2022-09-01 16:00:53 +0100 | [diff] [blame] | 6046 | case ISN_ECHOWINDOW: |
| 6047 | smsg("%s%4d ECHOWINDOW %lld", pfx, current, |
| 6048 | (varnumber_T)(iptr->isn_arg.number)); |
| 6049 | break; |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 6050 | case ISN_ECHOCONSOLE: |
| 6051 | smsg("%s%4d ECHOCONSOLE %lld", pfx, current, |
| 6052 | (varnumber_T)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6053 | break; |
| 6054 | case ISN_ECHOERR: |
| 6055 | smsg("%s%4d ECHOERR %lld", pfx, current, |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 6056 | (varnumber_T)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6057 | break; |
| 6058 | case ISN_LOAD: |
| 6059 | { |
| 6060 | if (iptr->isn_arg.number < 0) |
| 6061 | smsg("%s%4d LOAD arg[%lld]", pfx, current, |
| 6062 | (varnumber_T)(iptr->isn_arg.number |
| 6063 | + STACK_FRAME_SIZE)); |
| 6064 | else |
| 6065 | smsg("%s%4d LOAD $%lld", pfx, current, |
| 6066 | (varnumber_T)(iptr->isn_arg.number)); |
| 6067 | } |
| 6068 | break; |
| 6069 | case ISN_LOADOUTER: |
| 6070 | { |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 6071 | isn_outer_T *outer = &iptr->isn_arg.outer; |
| 6072 | |
| 6073 | if (outer->outer_idx < 0) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6074 | smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current, |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 6075 | outer->outer_depth, |
| 6076 | outer->outer_idx |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6077 | + STACK_FRAME_SIZE); |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 6078 | else if (outer->outer_depth == OUTER_LOOP_DEPTH) |
| 6079 | smsg("%s%4d LOADOUTER level 1 $%d in loop", |
| 6080 | pfx, current, outer->outer_idx); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6081 | else |
| 6082 | smsg("%s%4d LOADOUTER level %d $%d", pfx, current, |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 6083 | outer->outer_depth, |
| 6084 | outer->outer_idx); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6085 | } |
| 6086 | break; |
| 6087 | case ISN_LOADV: |
| 6088 | smsg("%s%4d LOADV v:%s", pfx, current, |
| 6089 | get_vim_var_name(iptr->isn_arg.number)); |
| 6090 | break; |
| 6091 | case ISN_LOADSCRIPT: |
| 6092 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 6093 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 6094 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
| 6095 | svar_T *sv; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6096 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 6097 | sv = get_script_svar(sref, -1); |
| 6098 | if (sv == NULL) |
| 6099 | smsg("%s%4d LOADSCRIPT [deleted] from %s", |
| 6100 | pfx, current, si->sn_name); |
| 6101 | else |
| 6102 | smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6103 | sv->sv_name, |
| 6104 | sref->sref_idx, |
| 6105 | si->sn_name); |
| 6106 | } |
| 6107 | break; |
| 6108 | case ISN_LOADS: |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 6109 | case ISN_LOADEXPORT: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6110 | { |
| 6111 | scriptitem_T *si = SCRIPT_ITEM( |
| 6112 | iptr->isn_arg.loadstore.ls_sid); |
| 6113 | |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 6114 | smsg("%s%4d %s s:%s from %s", pfx, current, |
| 6115 | iptr->isn_type == ISN_LOADS ? "LOADS" |
| 6116 | : "LOADEXPORT", |
| 6117 | iptr->isn_arg.loadstore.ls_name, si->sn_name); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6118 | } |
| 6119 | break; |
| 6120 | case ISN_LOADAUTO: |
| 6121 | smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string); |
| 6122 | break; |
| 6123 | case ISN_LOADG: |
| 6124 | smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string); |
| 6125 | break; |
| 6126 | case ISN_LOADB: |
| 6127 | smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string); |
| 6128 | break; |
| 6129 | case ISN_LOADW: |
| 6130 | smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string); |
| 6131 | break; |
| 6132 | case ISN_LOADT: |
| 6133 | smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string); |
| 6134 | break; |
| 6135 | case ISN_LOADGDICT: |
| 6136 | smsg("%s%4d LOAD g:", pfx, current); |
| 6137 | break; |
| 6138 | case ISN_LOADBDICT: |
| 6139 | smsg("%s%4d LOAD b:", pfx, current); |
| 6140 | break; |
| 6141 | case ISN_LOADWDICT: |
| 6142 | smsg("%s%4d LOAD w:", pfx, current); |
| 6143 | break; |
| 6144 | case ISN_LOADTDICT: |
| 6145 | smsg("%s%4d LOAD t:", pfx, current); |
| 6146 | break; |
| 6147 | case ISN_LOADOPT: |
| 6148 | smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string); |
| 6149 | break; |
| 6150 | case ISN_LOADENV: |
| 6151 | smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string); |
| 6152 | break; |
| 6153 | case ISN_LOADREG: |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 6154 | smsg("%s%4d LOADREG @%c", pfx, current, |
| 6155 | (int)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6156 | break; |
| 6157 | |
| 6158 | case ISN_STORE: |
| 6159 | if (iptr->isn_arg.number < 0) |
| 6160 | smsg("%s%4d STORE arg[%lld]", pfx, current, |
| 6161 | iptr->isn_arg.number + STACK_FRAME_SIZE); |
| 6162 | else |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 6163 | smsg("%s%4d STORE $%lld", pfx, current, |
| 6164 | iptr->isn_arg.number); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6165 | break; |
| 6166 | case ISN_STOREOUTER: |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 6167 | { |
| 6168 | isn_outer_T *outer = &iptr->isn_arg.outer; |
| 6169 | |
| 6170 | if (outer->outer_depth == OUTER_LOOP_DEPTH) |
| 6171 | smsg("%s%4d STOREOUTER level 1 $%d in loop", |
| 6172 | pfx, current, outer->outer_idx); |
| 6173 | else |
| 6174 | smsg("%s%4d STOREOUTER level %d $%d", pfx, current, |
| 6175 | outer->outer_depth, outer->outer_idx); |
| 6176 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6177 | break; |
| 6178 | case ISN_STOREV: |
| 6179 | smsg("%s%4d STOREV v:%s", pfx, current, |
| 6180 | get_vim_var_name(iptr->isn_arg.number)); |
| 6181 | break; |
| 6182 | case ISN_STOREAUTO: |
| 6183 | smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string); |
| 6184 | break; |
| 6185 | case ISN_STOREG: |
| 6186 | smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string); |
| 6187 | break; |
| 6188 | case ISN_STOREB: |
| 6189 | smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string); |
| 6190 | break; |
| 6191 | case ISN_STOREW: |
| 6192 | smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string); |
| 6193 | break; |
| 6194 | case ISN_STORET: |
| 6195 | smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string); |
| 6196 | break; |
| 6197 | case ISN_STORES: |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 6198 | case ISN_STOREEXPORT: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6199 | { |
| 6200 | scriptitem_T *si = SCRIPT_ITEM( |
| 6201 | iptr->isn_arg.loadstore.ls_sid); |
| 6202 | |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 6203 | smsg("%s%4d %s %s in %s", pfx, current, |
| 6204 | iptr->isn_type == ISN_STORES |
| 6205 | ? "STORES" : "STOREEXPORT", |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6206 | iptr->isn_arg.loadstore.ls_name, si->sn_name); |
| 6207 | } |
| 6208 | break; |
| 6209 | case ISN_STORESCRIPT: |
| 6210 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 6211 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 6212 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
| 6213 | svar_T *sv; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6214 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 6215 | sv = get_script_svar(sref, -1); |
| 6216 | if (sv == NULL) |
| 6217 | smsg("%s%4d STORESCRIPT [deleted] in %s", |
| 6218 | pfx, current, si->sn_name); |
| 6219 | else |
| 6220 | smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6221 | sv->sv_name, |
| 6222 | sref->sref_idx, |
| 6223 | si->sn_name); |
| 6224 | } |
| 6225 | break; |
| 6226 | case ISN_STOREOPT: |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 6227 | case ISN_STOREFUNCOPT: |
| 6228 | smsg("%s%4d %s &%s", pfx, current, |
| 6229 | iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT", |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6230 | iptr->isn_arg.storeopt.so_name); |
| 6231 | break; |
| 6232 | case ISN_STOREENV: |
| 6233 | smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string); |
| 6234 | break; |
| 6235 | case ISN_STOREREG: |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 6236 | smsg("%s%4d STOREREG @%c", pfx, current, |
| 6237 | (int)iptr->isn_arg.number); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6238 | break; |
| 6239 | case ISN_STORENR: |
| 6240 | smsg("%s%4d STORE %lld in $%d", pfx, current, |
| 6241 | iptr->isn_arg.storenr.stnr_val, |
| 6242 | iptr->isn_arg.storenr.stnr_idx); |
| 6243 | break; |
| 6244 | |
| 6245 | case ISN_STOREINDEX: |
| 6246 | smsg("%s%4d STOREINDEX %s", pfx, current, |
| 6247 | vartype_name(iptr->isn_arg.vartype)); |
| 6248 | break; |
| 6249 | |
| 6250 | case ISN_STORERANGE: |
| 6251 | smsg("%s%4d STORERANGE", pfx, current); |
| 6252 | break; |
| 6253 | |
| 6254 | // constants |
| 6255 | case ISN_PUSHNR: |
| 6256 | smsg("%s%4d PUSHNR %lld", pfx, current, |
| 6257 | (varnumber_T)(iptr->isn_arg.number)); |
| 6258 | break; |
| 6259 | case ISN_PUSHBOOL: |
| 6260 | case ISN_PUSHSPEC: |
| 6261 | smsg("%s%4d PUSH %s", pfx, current, |
| 6262 | get_var_special_name(iptr->isn_arg.number)); |
| 6263 | break; |
| 6264 | case ISN_PUSHF: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6265 | smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6266 | break; |
| 6267 | case ISN_PUSHS: |
| 6268 | smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string); |
| 6269 | break; |
| 6270 | case ISN_PUSHBLOB: |
| 6271 | { |
| 6272 | char_u *r; |
| 6273 | char_u numbuf[NUMBUFLEN]; |
| 6274 | char_u *tofree; |
| 6275 | |
| 6276 | r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); |
| 6277 | smsg("%s%4d PUSHBLOB %s", pfx, current, r); |
| 6278 | vim_free(tofree); |
| 6279 | } |
| 6280 | break; |
| 6281 | case ISN_PUSHFUNC: |
| 6282 | { |
| 6283 | char *name = (char *)iptr->isn_arg.string; |
| 6284 | |
| 6285 | smsg("%s%4d PUSHFUNC \"%s\"", pfx, current, |
| 6286 | name == NULL ? "[none]" : name); |
| 6287 | } |
| 6288 | break; |
| 6289 | case ISN_PUSHCHANNEL: |
| 6290 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 6291 | smsg("%s%4d PUSHCHANNEL 0", pfx, current); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6292 | #endif |
| 6293 | break; |
| 6294 | case ISN_PUSHJOB: |
| 6295 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 6296 | smsg("%s%4d PUSHJOB \"no process\"", pfx, current); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6297 | #endif |
| 6298 | break; |
| 6299 | case ISN_PUSHEXC: |
| 6300 | smsg("%s%4d PUSH v:exception", pfx, current); |
| 6301 | break; |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 6302 | case ISN_AUTOLOAD: |
| 6303 | smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string); |
| 6304 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6305 | case ISN_UNLET: |
| 6306 | smsg("%s%4d UNLET%s %s", pfx, current, |
| 6307 | iptr->isn_arg.unlet.ul_forceit ? "!" : "", |
| 6308 | iptr->isn_arg.unlet.ul_name); |
| 6309 | break; |
| 6310 | case ISN_UNLETENV: |
| 6311 | smsg("%s%4d UNLETENV%s $%s", pfx, current, |
| 6312 | iptr->isn_arg.unlet.ul_forceit ? "!" : "", |
| 6313 | iptr->isn_arg.unlet.ul_name); |
| 6314 | break; |
| 6315 | case ISN_UNLETINDEX: |
| 6316 | smsg("%s%4d UNLETINDEX", pfx, current); |
| 6317 | break; |
| 6318 | case ISN_UNLETRANGE: |
| 6319 | smsg("%s%4d UNLETRANGE", pfx, current); |
| 6320 | break; |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 6321 | case ISN_LOCKUNLOCK: |
| 6322 | smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string); |
| 6323 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6324 | case ISN_LOCKCONST: |
| 6325 | smsg("%s%4d LOCKCONST", pfx, current); |
| 6326 | break; |
| 6327 | case ISN_NEWLIST: |
| 6328 | smsg("%s%4d NEWLIST size %lld", pfx, current, |
| 6329 | (varnumber_T)(iptr->isn_arg.number)); |
| 6330 | break; |
| 6331 | case ISN_NEWDICT: |
| 6332 | smsg("%s%4d NEWDICT size %lld", pfx, current, |
| 6333 | (varnumber_T)(iptr->isn_arg.number)); |
| 6334 | break; |
Bram Moolenaar | 8acb9cc | 2022-03-08 13:18:55 +0000 | [diff] [blame] | 6335 | case ISN_NEWPARTIAL: |
| 6336 | smsg("%s%4d NEWPARTIAL", pfx, current); |
| 6337 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6338 | |
| 6339 | // function call |
| 6340 | case ISN_BCALL: |
| 6341 | { |
| 6342 | cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; |
| 6343 | |
| 6344 | smsg("%s%4d BCALL %s(argc %d)", pfx, current, |
| 6345 | internal_func_name(cbfunc->cbf_idx), |
| 6346 | cbfunc->cbf_argcount); |
| 6347 | } |
| 6348 | break; |
| 6349 | case ISN_DCALL: |
| 6350 | { |
| 6351 | cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; |
| 6352 | dfunc_T *df = ((dfunc_T *)def_functions.ga_data) |
| 6353 | + cdfunc->cdf_idx; |
| 6354 | |
| 6355 | smsg("%s%4d DCALL %s(argc %d)", pfx, current, |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 6356 | printable_func_name(df->df_ufunc), |
| 6357 | cdfunc->cdf_argcount); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6358 | } |
| 6359 | break; |
| 6360 | case ISN_UCALL: |
| 6361 | { |
| 6362 | cufunc_T *cufunc = &iptr->isn_arg.ufunc; |
| 6363 | |
| 6364 | smsg("%s%4d UCALL %s(argc %d)", pfx, current, |
| 6365 | cufunc->cuf_name, cufunc->cuf_argcount); |
| 6366 | } |
| 6367 | break; |
| 6368 | case ISN_PCALL: |
| 6369 | { |
| 6370 | cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; |
| 6371 | |
| 6372 | smsg("%s%4d PCALL%s (argc %d)", pfx, current, |
| 6373 | cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); |
| 6374 | } |
| 6375 | break; |
| 6376 | case ISN_PCALL_END: |
| 6377 | smsg("%s%4d PCALL end", pfx, current); |
| 6378 | break; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 6379 | case ISN_DEFER: |
| 6380 | smsg("%s%4d DEFER %d args", pfx, current, |
| 6381 | (int)iptr->isn_arg.defer.defer_argcount); |
| 6382 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6383 | case ISN_RETURN: |
| 6384 | smsg("%s%4d RETURN", pfx, current); |
| 6385 | break; |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 6386 | case ISN_RETURN_VOID: |
| 6387 | smsg("%s%4d RETURN void", pfx, current); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6388 | break; |
| 6389 | case ISN_FUNCREF: |
| 6390 | { |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 6391 | funcref_T *funcref = &iptr->isn_arg.funcref; |
| 6392 | funcref_extra_T *extra = funcref->fr_extra; |
| 6393 | char_u *name; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6394 | |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 6395 | if (extra == NULL || extra->fre_func_name == NULL) |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 6396 | { |
| 6397 | dfunc_T *df = ((dfunc_T *)def_functions.ga_data) |
| 6398 | + funcref->fr_dfunc_idx; |
| 6399 | name = df->df_ufunc->uf_name; |
| 6400 | } |
| 6401 | else |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 6402 | name = extra->fre_func_name; |
| 6403 | if (extra == NULL || extra->fre_loop_var_count == 0) |
| 6404 | smsg("%s%4d FUNCREF %s", pfx, current, name); |
| 6405 | else |
| 6406 | smsg("%s%4d FUNCREF %s var $%d - $%d", pfx, current, |
| 6407 | name, |
| 6408 | extra->fre_loop_var_idx, |
| 6409 | extra->fre_loop_var_idx |
| 6410 | + extra->fre_loop_var_count - 1); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6411 | } |
| 6412 | break; |
| 6413 | |
| 6414 | case ISN_NEWFUNC: |
| 6415 | { |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 6416 | newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6417 | |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 6418 | if (arg->nfa_loop_var_count == 0) |
| 6419 | smsg("%s%4d NEWFUNC %s %s", pfx, current, |
| 6420 | arg->nfa_lambda, arg->nfa_global); |
| 6421 | else |
| 6422 | smsg("%s%4d NEWFUNC %s %s var $%d - $%d", pfx, current, |
| 6423 | arg->nfa_lambda, arg->nfa_global, |
| 6424 | arg->nfa_loop_var_idx, |
| 6425 | arg->nfa_loop_var_idx + arg->nfa_loop_var_count - 1); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6426 | } |
| 6427 | break; |
| 6428 | |
| 6429 | case ISN_DEF: |
| 6430 | { |
| 6431 | char_u *name = iptr->isn_arg.string; |
| 6432 | |
| 6433 | smsg("%s%4d DEF %s", pfx, current, |
| 6434 | name == NULL ? (char_u *)"" : name); |
| 6435 | } |
| 6436 | break; |
| 6437 | |
| 6438 | case ISN_JUMP: |
| 6439 | { |
| 6440 | char *when = "?"; |
| 6441 | |
| 6442 | switch (iptr->isn_arg.jump.jump_when) |
| 6443 | { |
| 6444 | case JUMP_ALWAYS: |
| 6445 | when = "JUMP"; |
| 6446 | break; |
Bram Moolenaar | 1a7ee4d | 2021-09-16 16:15:07 +0200 | [diff] [blame] | 6447 | case JUMP_NEVER: |
| 6448 | iemsg("JUMP_NEVER should not be used"); |
| 6449 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6450 | case JUMP_AND_KEEP_IF_TRUE: |
| 6451 | when = "JUMP_AND_KEEP_IF_TRUE"; |
| 6452 | break; |
| 6453 | case JUMP_IF_FALSE: |
| 6454 | when = "JUMP_IF_FALSE"; |
| 6455 | break; |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 6456 | case JUMP_WHILE_FALSE: |
| 6457 | when = "JUMP_WHILE_FALSE"; // unused |
| 6458 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6459 | case JUMP_IF_COND_FALSE: |
| 6460 | when = "JUMP_IF_COND_FALSE"; |
| 6461 | break; |
| 6462 | case JUMP_IF_COND_TRUE: |
| 6463 | when = "JUMP_IF_COND_TRUE"; |
| 6464 | break; |
| 6465 | } |
| 6466 | smsg("%s%4d %s -> %d", pfx, current, when, |
| 6467 | iptr->isn_arg.jump.jump_where); |
| 6468 | } |
| 6469 | break; |
| 6470 | |
| 6471 | case ISN_JUMP_IF_ARG_SET: |
| 6472 | smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current, |
| 6473 | iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE, |
| 6474 | iptr->isn_arg.jump.jump_where); |
| 6475 | break; |
| 6476 | |
| 6477 | case ISN_FOR: |
| 6478 | { |
| 6479 | forloop_T *forloop = &iptr->isn_arg.forloop; |
| 6480 | |
| 6481 | smsg("%s%4d FOR $%d -> %d", pfx, current, |
| 6482 | forloop->for_idx, forloop->for_end); |
| 6483 | } |
| 6484 | break; |
| 6485 | |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 6486 | case ISN_ENDLOOP: |
| 6487 | { |
| 6488 | endloop_T *endloop = &iptr->isn_arg.endloop; |
| 6489 | |
| 6490 | smsg("%s%4d ENDLOOP $%d save $%d - $%d", pfx, current, |
| 6491 | endloop->end_funcref_idx, |
| 6492 | endloop->end_var_idx, |
| 6493 | endloop->end_var_idx + endloop->end_var_count - 1); |
| 6494 | } |
| 6495 | break; |
| 6496 | |
| 6497 | case ISN_WHILE: |
| 6498 | { |
| 6499 | whileloop_T *whileloop = &iptr->isn_arg.whileloop; |
| 6500 | |
| 6501 | smsg("%s%4d WHILE $%d -> %d", pfx, current, |
| 6502 | whileloop->while_funcref_idx, |
| 6503 | whileloop->while_end); |
| 6504 | } |
| 6505 | break; |
| 6506 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6507 | case ISN_TRY: |
| 6508 | { |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 6509 | try_T *try = &iptr->isn_arg.tryref; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6510 | |
| 6511 | if (try->try_ref->try_finally == 0) |
| 6512 | smsg("%s%4d TRY catch -> %d, endtry -> %d", |
| 6513 | pfx, current, |
| 6514 | try->try_ref->try_catch, |
| 6515 | try->try_ref->try_endtry); |
| 6516 | else |
| 6517 | smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d", |
| 6518 | pfx, current, |
| 6519 | try->try_ref->try_catch, |
| 6520 | try->try_ref->try_finally, |
| 6521 | try->try_ref->try_endtry); |
| 6522 | } |
| 6523 | break; |
| 6524 | case ISN_CATCH: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6525 | smsg("%s%4d CATCH", pfx, current); |
| 6526 | break; |
| 6527 | case ISN_TRYCONT: |
| 6528 | { |
| 6529 | trycont_T *trycont = &iptr->isn_arg.trycont; |
| 6530 | |
| 6531 | smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current, |
| 6532 | trycont->tct_levels, |
| 6533 | trycont->tct_levels == 1 ? "" : "s", |
| 6534 | trycont->tct_where); |
| 6535 | } |
| 6536 | break; |
| 6537 | case ISN_FINALLY: |
| 6538 | smsg("%s%4d FINALLY", pfx, current); |
| 6539 | break; |
| 6540 | case ISN_ENDTRY: |
| 6541 | smsg("%s%4d ENDTRY", pfx, current); |
| 6542 | break; |
| 6543 | case ISN_THROW: |
| 6544 | smsg("%s%4d THROW", pfx, current); |
| 6545 | break; |
| 6546 | |
| 6547 | // expression operations on number |
| 6548 | case ISN_OPNR: |
| 6549 | case ISN_OPFLOAT: |
| 6550 | case ISN_OPANY: |
| 6551 | { |
| 6552 | char *what; |
| 6553 | char *ins; |
| 6554 | |
| 6555 | switch (iptr->isn_arg.op.op_type) |
| 6556 | { |
| 6557 | case EXPR_MULT: what = "*"; break; |
| 6558 | case EXPR_DIV: what = "/"; break; |
| 6559 | case EXPR_REM: what = "%"; break; |
| 6560 | case EXPR_SUB: what = "-"; break; |
| 6561 | case EXPR_ADD: what = "+"; break; |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 6562 | case EXPR_LSHIFT: what = "<<"; break; |
| 6563 | case EXPR_RSHIFT: what = ">>"; break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6564 | default: what = "???"; break; |
| 6565 | } |
| 6566 | switch (iptr->isn_type) |
| 6567 | { |
| 6568 | case ISN_OPNR: ins = "OPNR"; break; |
| 6569 | case ISN_OPFLOAT: ins = "OPFLOAT"; break; |
| 6570 | case ISN_OPANY: ins = "OPANY"; break; |
| 6571 | default: ins = "???"; break; |
| 6572 | } |
| 6573 | smsg("%s%4d %s %s", pfx, current, ins, what); |
| 6574 | } |
| 6575 | break; |
| 6576 | |
| 6577 | case ISN_COMPAREBOOL: |
| 6578 | case ISN_COMPARESPECIAL: |
Bram Moolenaar | 7a22224 | 2022-03-01 19:23:24 +0000 | [diff] [blame] | 6579 | case ISN_COMPARENULL: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6580 | case ISN_COMPARENR: |
| 6581 | case ISN_COMPAREFLOAT: |
| 6582 | case ISN_COMPARESTRING: |
| 6583 | case ISN_COMPAREBLOB: |
| 6584 | case ISN_COMPARELIST: |
| 6585 | case ISN_COMPAREDICT: |
| 6586 | case ISN_COMPAREFUNC: |
| 6587 | case ISN_COMPAREANY: |
| 6588 | { |
| 6589 | char *p; |
| 6590 | char buf[10]; |
| 6591 | char *type; |
| 6592 | |
| 6593 | switch (iptr->isn_arg.op.op_type) |
| 6594 | { |
| 6595 | case EXPR_EQUAL: p = "=="; break; |
| 6596 | case EXPR_NEQUAL: p = "!="; break; |
| 6597 | case EXPR_GREATER: p = ">"; break; |
| 6598 | case EXPR_GEQUAL: p = ">="; break; |
| 6599 | case EXPR_SMALLER: p = "<"; break; |
| 6600 | case EXPR_SEQUAL: p = "<="; break; |
| 6601 | case EXPR_MATCH: p = "=~"; break; |
| 6602 | case EXPR_IS: p = "is"; break; |
| 6603 | case EXPR_ISNOT: p = "isnot"; break; |
| 6604 | case EXPR_NOMATCH: p = "!~"; break; |
| 6605 | default: p = "???"; break; |
| 6606 | } |
| 6607 | STRCPY(buf, p); |
| 6608 | if (iptr->isn_arg.op.op_ic == TRUE) |
| 6609 | strcat(buf, "?"); |
| 6610 | switch(iptr->isn_type) |
| 6611 | { |
| 6612 | case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; |
| 6613 | case ISN_COMPARESPECIAL: |
| 6614 | type = "COMPARESPECIAL"; break; |
Bram Moolenaar | 7a22224 | 2022-03-01 19:23:24 +0000 | [diff] [blame] | 6615 | case ISN_COMPARENULL: type = "COMPARENULL"; break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6616 | case ISN_COMPARENR: type = "COMPARENR"; break; |
| 6617 | case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; |
| 6618 | case ISN_COMPARESTRING: |
| 6619 | type = "COMPARESTRING"; break; |
| 6620 | case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; |
| 6621 | case ISN_COMPARELIST: type = "COMPARELIST"; break; |
| 6622 | case ISN_COMPAREDICT: type = "COMPAREDICT"; break; |
| 6623 | case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; |
| 6624 | case ISN_COMPAREANY: type = "COMPAREANY"; break; |
| 6625 | default: type = "???"; break; |
| 6626 | } |
| 6627 | |
| 6628 | smsg("%s%4d %s %s", pfx, current, type, buf); |
| 6629 | } |
| 6630 | break; |
| 6631 | |
| 6632 | case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break; |
| 6633 | case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break; |
| 6634 | |
| 6635 | // expression operations |
LemonBoy | 372bcce | 2022-04-25 12:43:20 +0100 | [diff] [blame] | 6636 | case ISN_CONCAT: |
| 6637 | smsg("%s%4d CONCAT size %lld", pfx, current, |
| 6638 | (varnumber_T)(iptr->isn_arg.number)); |
| 6639 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6640 | case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break; |
| 6641 | case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break; |
| 6642 | case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break; |
| 6643 | case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break; |
| 6644 | case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break; |
| 6645 | case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break; |
| 6646 | case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break; |
| 6647 | case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break; |
| 6648 | case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break; |
| 6649 | case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break; |
| 6650 | case ISN_SLICE: smsg("%s%4d SLICE %lld", |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 6651 | pfx, current, iptr->isn_arg.number); break; |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 6652 | case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current, |
| 6653 | iptr->isn_arg.getitem.gi_index, |
| 6654 | iptr->isn_arg.getitem.gi_with_op ? |
| 6655 | " with op" : ""); break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6656 | case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break; |
| 6657 | case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current, |
| 6658 | iptr->isn_arg.string); break; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 6659 | case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break; |
| 6660 | case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break; |
| 6661 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6662 | case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break; |
| 6663 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6664 | case ISN_CHECKTYPE: |
| 6665 | { |
Bram Moolenaar | bd3a9d2 | 2022-05-17 16:12:39 +0100 | [diff] [blame] | 6666 | checktype_T *ct = &iptr->isn_arg.type; |
| 6667 | char *tofree; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6668 | |
| 6669 | if (ct->ct_arg_idx == 0) |
| 6670 | smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current, |
| 6671 | type_name(ct->ct_type, &tofree), |
| 6672 | (int)ct->ct_off); |
| 6673 | else |
Bram Moolenaar | bd3a9d2 | 2022-05-17 16:12:39 +0100 | [diff] [blame] | 6674 | smsg("%s%4d CHECKTYPE %s stack[%d] %s %d", |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 6675 | pfx, current, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6676 | type_name(ct->ct_type, &tofree), |
| 6677 | (int)ct->ct_off, |
Bram Moolenaar | bd3a9d2 | 2022-05-17 16:12:39 +0100 | [diff] [blame] | 6678 | ct->ct_is_var ? "var": "arg", |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6679 | (int)ct->ct_arg_idx); |
| 6680 | vim_free(tofree); |
| 6681 | break; |
| 6682 | } |
| 6683 | case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current, |
| 6684 | iptr->isn_arg.checklen.cl_more_OK ? ">= " : "", |
| 6685 | iptr->isn_arg.checklen.cl_min_len); |
| 6686 | break; |
| 6687 | case ISN_SETTYPE: |
| 6688 | { |
| 6689 | char *tofree; |
| 6690 | |
| 6691 | smsg("%s%4d SETTYPE %s", pfx, current, |
| 6692 | type_name(iptr->isn_arg.type.ct_type, &tofree)); |
| 6693 | vim_free(tofree); |
| 6694 | break; |
| 6695 | } |
| 6696 | case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 6697 | case ISN_2BOOL: if (iptr->isn_arg.tobool.invert) |
| 6698 | smsg("%s%4d INVERT %d (!val)", pfx, current, |
| 6699 | iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6700 | else |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 6701 | smsg("%s%4d 2BOOL %d (!!val)", pfx, current, |
| 6702 | iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6703 | break; |
| 6704 | case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current, |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 6705 | (varnumber_T)(iptr->isn_arg.tostring.offset)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6706 | break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 6707 | case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]", |
| 6708 | pfx, current, |
| 6709 | (varnumber_T)(iptr->isn_arg.tostring.offset)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6710 | break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 6711 | case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current, |
| 6712 | iptr->isn_arg.string); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6713 | break; |
| 6714 | case ISN_PUT: |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 6715 | if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6716 | smsg("%s%4d PUT %c above range", |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 6717 | pfx, current, iptr->isn_arg.put.put_regname); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6718 | else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE) |
| 6719 | smsg("%s%4d PUT %c range", |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 6720 | pfx, current, iptr->isn_arg.put.put_regname); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6721 | else |
| 6722 | smsg("%s%4d PUT %c %ld", pfx, current, |
| 6723 | iptr->isn_arg.put.put_regname, |
| 6724 | (long)iptr->isn_arg.put.put_lnum); |
| 6725 | break; |
| 6726 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6727 | case ISN_CMDMOD: |
| 6728 | { |
| 6729 | char_u *buf; |
| 6730 | size_t len = produce_cmdmods( |
| 6731 | NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); |
| 6732 | |
| 6733 | buf = alloc(len + 1); |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 6734 | if (likely(buf != NULL)) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6735 | { |
| 6736 | (void)produce_cmdmods( |
| 6737 | buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); |
| 6738 | smsg("%s%4d CMDMOD %s", pfx, current, buf); |
| 6739 | vim_free(buf); |
| 6740 | } |
| 6741 | break; |
| 6742 | } |
| 6743 | case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break; |
| 6744 | |
| 6745 | case ISN_PROF_START: |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6746 | smsg("%s%4d PROFILE START line %d", pfx, current, |
| 6747 | iptr->isn_lnum); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6748 | break; |
| 6749 | |
| 6750 | case ISN_PROF_END: |
| 6751 | smsg("%s%4d PROFILE END", pfx, current); |
| 6752 | break; |
| 6753 | |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6754 | case ISN_DEBUG: |
Bram Moolenaar | 8cec927 | 2021-06-23 20:20:53 +0200 | [diff] [blame] | 6755 | smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current, |
| 6756 | iptr->isn_arg.debug.dbg_break_lnum + 1, |
| 6757 | iptr->isn_lnum, |
| 6758 | iptr->isn_arg.debug.dbg_var_names_len); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6759 | break; |
| 6760 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6761 | case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current, |
| 6762 | iptr->isn_arg.unpack.unp_count, |
| 6763 | iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : ""); |
| 6764 | break; |
| 6765 | case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current, |
| 6766 | iptr->isn_arg.shuffle.shfl_item, |
| 6767 | iptr->isn_arg.shuffle.shfl_up); |
| 6768 | break; |
| 6769 | case ISN_DROP: smsg("%s%4d DROP", pfx, current); break; |
| 6770 | |
| 6771 | case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE. |
| 6772 | return; |
| 6773 | } |
| 6774 | |
| 6775 | out_flush(); // output one line at a time |
| 6776 | ui_breakcheck(); |
| 6777 | if (got_int) |
| 6778 | break; |
| 6779 | } |
| 6780 | } |
| 6781 | |
| 6782 | /* |
Bram Moolenaar | 4ee9d8e | 2021-06-13 18:38:48 +0200 | [diff] [blame] | 6783 | * Handle command line completion for the :disassemble command. |
| 6784 | */ |
| 6785 | void |
| 6786 | set_context_in_disassemble_cmd(expand_T *xp, char_u *arg) |
| 6787 | { |
| 6788 | char_u *p; |
| 6789 | |
| 6790 | // Default: expand user functions, "debug" and "profile" |
| 6791 | xp->xp_context = EXPAND_DISASSEMBLE; |
| 6792 | xp->xp_pattern = arg; |
| 6793 | |
| 6794 | // first argument already typed: only user function names |
| 6795 | if (*arg != NUL && *(p = skiptowhite(arg)) != NUL) |
| 6796 | { |
| 6797 | xp->xp_context = EXPAND_USER_FUNC; |
| 6798 | xp->xp_pattern = skipwhite(p); |
| 6799 | } |
| 6800 | } |
| 6801 | |
| 6802 | /* |
| 6803 | * Function given to ExpandGeneric() to obtain the list of :disassemble |
| 6804 | * arguments. |
| 6805 | */ |
| 6806 | char_u * |
| 6807 | get_disassemble_argument(expand_T *xp, int idx) |
| 6808 | { |
| 6809 | if (idx == 0) |
| 6810 | return (char_u *)"debug"; |
| 6811 | if (idx == 1) |
| 6812 | return (char_u *)"profile"; |
| 6813 | return get_user_func_name(xp, idx - 2); |
| 6814 | } |
| 6815 | |
| 6816 | /* |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 6817 | * ":disassemble". |
Bram Moolenaar | 777770f | 2020-02-06 21:27:08 +0100 | [diff] [blame] | 6818 | * We don't really need this at runtime, but we do have tests that require it, |
| 6819 | * so always include this. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6820 | */ |
| 6821 | void |
| 6822 | ex_disassemble(exarg_T *eap) |
| 6823 | { |
Bram Moolenaar | 21456cd | 2020-02-13 21:29:32 +0100 | [diff] [blame] | 6824 | char_u *arg = eap->arg; |
Bram Moolenaar | 0f18b6d | 2020-02-02 17:22:27 +0100 | [diff] [blame] | 6825 | ufunc_T *ufunc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6826 | dfunc_T *dfunc; |
Bram Moolenaar | dafef51 | 2022-05-21 21:55:55 +0100 | [diff] [blame] | 6827 | isn_T *instr = NULL; // init to shut up gcc warning |
| 6828 | int instr_count = 0; // init to shut up gcc warning |
Bram Moolenaar | 5a01caa | 2022-05-21 18:56:58 +0100 | [diff] [blame] | 6829 | compiletype_T compile_type = CT_NONE; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6830 | |
Bram Moolenaar | f79d9dd | 2022-05-21 15:39:02 +0100 | [diff] [blame] | 6831 | ufunc = find_func_by_name(arg, &compile_type); |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 6832 | if (ufunc == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6833 | return; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6834 | if (func_needs_compiling(ufunc, compile_type) |
| 6835 | && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6836 | return; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6837 | if (ufunc->uf_def_status != UF_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6838 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6839 | semsg(_(e_function_is_not_compiled_str), eap->arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6840 | return; |
| 6841 | } |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 6842 | msg((char *)printable_func_name(ufunc)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6843 | |
| 6844 | dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6845 | switch (compile_type) |
| 6846 | { |
| 6847 | case CT_PROFILE: |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 6848 | #ifdef FEAT_PROFILE |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6849 | instr = dfunc->df_instr_prof; |
| 6850 | instr_count = dfunc->df_instr_prof_count; |
| 6851 | break; |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 6852 | #endif |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6853 | // FALLTHROUGH |
| 6854 | case CT_NONE: |
| 6855 | instr = dfunc->df_instr; |
| 6856 | instr_count = dfunc->df_instr_count; |
| 6857 | break; |
| 6858 | case CT_DEBUG: |
| 6859 | instr = dfunc->df_instr_debug; |
| 6860 | instr_count = dfunc->df_instr_debug_count; |
| 6861 | break; |
| 6862 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6863 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6864 | list_instructions("", instr, instr_count, ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6865 | } |
| 6866 | |
| 6867 | /* |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6868 | * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6869 | * list, etc. Mostly like what JavaScript does, except that empty list and |
| 6870 | * empty dictionary are FALSE. |
| 6871 | */ |
| 6872 | int |
| 6873 | tv2bool(typval_T *tv) |
| 6874 | { |
| 6875 | switch (tv->v_type) |
| 6876 | { |
| 6877 | case VAR_NUMBER: |
| 6878 | return tv->vval.v_number != 0; |
| 6879 | case VAR_FLOAT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6880 | return tv->vval.v_float != 0.0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6881 | case VAR_PARTIAL: |
| 6882 | return tv->vval.v_partial != NULL; |
| 6883 | case VAR_FUNC: |
| 6884 | case VAR_STRING: |
| 6885 | return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; |
| 6886 | case VAR_LIST: |
| 6887 | return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; |
| 6888 | case VAR_DICT: |
| 6889 | return tv->vval.v_dict != NULL |
| 6890 | && tv->vval.v_dict->dv_hashtab.ht_used > 0; |
| 6891 | case VAR_BOOL: |
| 6892 | case VAR_SPECIAL: |
| 6893 | return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; |
| 6894 | case VAR_JOB: |
| 6895 | #ifdef FEAT_JOB_CHANNEL |
| 6896 | return tv->vval.v_job != NULL; |
| 6897 | #else |
| 6898 | break; |
| 6899 | #endif |
| 6900 | case VAR_CHANNEL: |
| 6901 | #ifdef FEAT_JOB_CHANNEL |
| 6902 | return tv->vval.v_channel != NULL; |
| 6903 | #else |
| 6904 | break; |
| 6905 | #endif |
| 6906 | case VAR_BLOB: |
| 6907 | return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; |
| 6908 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 6909 | case VAR_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6910 | case VAR_VOID: |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 6911 | case VAR_INSTR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6912 | break; |
| 6913 | } |
| 6914 | return FALSE; |
| 6915 | } |
| 6916 | |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 6917 | void |
| 6918 | emsg_using_string_as(typval_T *tv, int as_number) |
| 6919 | { |
| 6920 | semsg(_(as_number ? e_using_string_as_number_str |
| 6921 | : e_using_string_as_bool_str), |
| 6922 | tv->vval.v_string == NULL |
| 6923 | ? (char_u *)"" : tv->vval.v_string); |
| 6924 | } |
| 6925 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6926 | /* |
| 6927 | * If "tv" is a string give an error and return FAIL. |
| 6928 | */ |
| 6929 | int |
| 6930 | check_not_string(typval_T *tv) |
| 6931 | { |
| 6932 | if (tv->v_type == VAR_STRING) |
| 6933 | { |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 6934 | emsg_using_string_as(tv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6935 | clear_tv(tv); |
| 6936 | return FAIL; |
| 6937 | } |
| 6938 | return OK; |
| 6939 | } |
| 6940 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6941 | |
| 6942 | #endif // FEAT_EVAL |