Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9execute.c: execute Vim9 script instructions |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | #ifdef VMS |
| 20 | # include <float.h> |
| 21 | #endif |
| 22 | |
| 23 | #include "vim9.h" |
| 24 | |
| 25 | // Structure put on ec_trystack when ISN_TRY is encountered. |
| 26 | typedef struct { |
Bram Moolenaar | 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 |
| 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) |
| 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 | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 104 | // 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] | 105 | #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] | 106 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 107 | void |
| 108 | to_string_error(vartype_T vartype) |
| 109 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 110 | semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype)); |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 113 | /* |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 114 | * Return the number of arguments, including optional arguments and any vararg. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 115 | */ |
| 116 | static int |
| 117 | ufunc_argcount(ufunc_T *ufunc) |
| 118 | { |
| 119 | return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 120 | } |
| 121 | |
| 122 | /* |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 123 | * Create a new list from "count" items at the bottom of the stack. |
| 124 | * When "count" is zero an empty list is added to the stack. |
| 125 | */ |
| 126 | static int |
| 127 | exe_newlist(int count, ectx_T *ectx) |
| 128 | { |
| 129 | list_T *list = list_alloc_with_items(count); |
| 130 | int idx; |
| 131 | typval_T *tv; |
| 132 | |
| 133 | if (list == NULL) |
| 134 | return FAIL; |
| 135 | for (idx = 0; idx < count; ++idx) |
| 136 | list_set_item(list, idx, STACK_TV_BOT(idx - count)); |
| 137 | |
| 138 | if (count > 0) |
| 139 | ectx->ec_stack.ga_len -= count - 1; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 140 | else if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 141 | return FAIL; |
| 142 | else |
| 143 | ++ectx->ec_stack.ga_len; |
| 144 | tv = STACK_TV_BOT(-1); |
| 145 | tv->v_type = VAR_LIST; |
| 146 | tv->vval.v_list = list; |
| 147 | ++list->lv_refcount; |
| 148 | return OK; |
| 149 | } |
| 150 | |
| 151 | /* |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 152 | * If debug_tick changed check if "ufunc" has a breakpoint and update |
| 153 | * "uf_has_breakpoint". |
| 154 | */ |
| 155 | static void |
| 156 | update_has_breakpoint(ufunc_T *ufunc) |
| 157 | { |
| 158 | if (ufunc->uf_debug_tick != debug_tick) |
| 159 | { |
| 160 | linenr_T breakpoint; |
| 161 | |
| 162 | ufunc->uf_debug_tick = debug_tick; |
| 163 | breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0); |
| 164 | ufunc->uf_has_breakpoint = breakpoint > 0; |
| 165 | } |
| 166 | } |
| 167 | |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 168 | static garray_T dict_stack = GA_EMPTY; |
| 169 | |
| 170 | /* |
| 171 | * Put a value on the dict stack. This consumes "tv". |
| 172 | */ |
| 173 | static int |
| 174 | dict_stack_save(typval_T *tv) |
| 175 | { |
| 176 | if (dict_stack.ga_growsize == 0) |
| 177 | ga_init2(&dict_stack, (int)sizeof(typval_T), 10); |
| 178 | if (ga_grow(&dict_stack, 1) == FAIL) |
| 179 | return FAIL; |
| 180 | ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv; |
| 181 | ++dict_stack.ga_len; |
| 182 | return OK; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Get the typval at top of the dict stack. |
| 187 | */ |
| 188 | static typval_T * |
| 189 | dict_stack_get_tv(void) |
| 190 | { |
| 191 | if (dict_stack.ga_len == 0) |
| 192 | return NULL; |
| 193 | return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Get the dict at top of the dict stack. |
| 198 | */ |
| 199 | static dict_T * |
| 200 | dict_stack_get_dict(void) |
| 201 | { |
| 202 | typval_T *tv; |
| 203 | |
| 204 | if (dict_stack.ga_len == 0) |
| 205 | return NULL; |
| 206 | tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1; |
| 207 | if (tv->v_type == VAR_DICT) |
| 208 | return tv->vval.v_dict; |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Drop an item from the dict stack. |
| 214 | */ |
| 215 | static void |
| 216 | dict_stack_drop(void) |
| 217 | { |
| 218 | if (dict_stack.ga_len == 0) |
| 219 | { |
| 220 | iemsg("Dict stack underflow"); |
| 221 | return; |
| 222 | } |
| 223 | --dict_stack.ga_len; |
| 224 | clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Drop items from the dict stack until the length is equal to "len". |
| 229 | */ |
| 230 | static void |
| 231 | dict_stack_clear(int len) |
| 232 | { |
| 233 | while (dict_stack.ga_len > len) |
| 234 | dict_stack_drop(); |
| 235 | } |
| 236 | |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 237 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 238 | * Call compiled function "cdf_idx" from compiled code. |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 239 | * This adds a stack frame and sets the instruction pointer to the start of the |
| 240 | * called function. |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 241 | * 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] | 242 | * |
| 243 | * Stack has: |
| 244 | * - current arguments (already there) |
| 245 | * - omitted optional argument (default values) added here |
| 246 | * - stack frame: |
| 247 | * - pointer to calling function |
| 248 | * - Index of next instruction in calling function |
| 249 | * - previous frame pointer |
| 250 | * - reserved space for local variables |
| 251 | */ |
| 252 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 253 | call_dfunc( |
| 254 | int cdf_idx, |
| 255 | partial_T *pt, |
| 256 | int argcount_arg, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 257 | ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 258 | { |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 259 | int argcount = argcount_arg; |
| 260 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx; |
| 261 | ufunc_T *ufunc = dfunc->df_ufunc; |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 262 | int did_emsg_before = did_emsg_cumul + did_emsg; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 263 | int arg_to_add; |
| 264 | int vararg_count = 0; |
| 265 | int varcount; |
| 266 | int idx; |
| 267 | estack_T *entry; |
| 268 | funclocal_T *floc = NULL; |
Bram Moolenaar | 648594e | 2021-07-11 17:55:01 +0200 | [diff] [blame] | 269 | int res = OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 270 | |
| 271 | if (dfunc->df_deleted) |
| 272 | { |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 273 | // don't use ufunc->uf_name, it may have been freed |
| 274 | emsg_funcname(e_func_deleted, |
| 275 | dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 276 | return FAIL; |
| 277 | } |
| 278 | |
Bram Moolenaar | e5ea346 | 2021-01-25 21:01:48 +0100 | [diff] [blame] | 279 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 280 | if (do_profiling == PROF_YES) |
| 281 | { |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 282 | if (GA_GROW_OK(&profile_info_ga, 1)) |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 283 | { |
| 284 | profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data) |
| 285 | + profile_info_ga.ga_len; |
| 286 | ++profile_info_ga.ga_len; |
| 287 | CLEAR_POINTER(info); |
| 288 | profile_may_start_func(info, ufunc, |
| 289 | (((dfunc_T *)def_functions.ga_data) |
| 290 | + ectx->ec_dfunc_idx)->df_ufunc); |
| 291 | } |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 292 | } |
Bram Moolenaar | e5ea346 | 2021-01-25 21:01:48 +0100 | [diff] [blame] | 293 | #endif |
| 294 | |
Bram Moolenaar | 2ac4b25 | 2021-06-20 20:09:42 +0200 | [diff] [blame] | 295 | // Update uf_has_breakpoint if needed. |
| 296 | update_has_breakpoint(ufunc); |
| 297 | |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 298 | // When debugging and using "cont" switches to the not-debugged |
| 299 | // instructions, may need to still compile them. |
Bram Moolenaar | 648594e | 2021-07-11 17:55:01 +0200 | [diff] [blame] | 300 | if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))) |
| 301 | { |
| 302 | res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL); |
| 303 | |
| 304 | // compile_def_function() may cause def_functions.ga_data to change |
| 305 | dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx; |
| 306 | } |
| 307 | if (res == FAIL || INSTRUCTIONS(dfunc) == NULL) |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 308 | { |
| 309 | if (did_emsg_cumul + did_emsg == did_emsg_before) |
| 310 | semsg(_(e_function_is_not_compiled_str), |
| 311 | printable_func_name(ufunc)); |
| 312 | return FAIL; |
| 313 | } |
| 314 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 315 | if (ufunc->uf_va_name != NULL) |
| 316 | { |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 317 | // Need to make a list out of the vararg arguments. |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 318 | // Stack at time of call with 2 varargs: |
| 319 | // normal_arg |
| 320 | // optional_arg |
| 321 | // vararg_1 |
| 322 | // vararg_2 |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 323 | // After creating the list: |
| 324 | // normal_arg |
| 325 | // optional_arg |
| 326 | // vararg-list |
| 327 | // With missing optional arguments we get: |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 328 | // normal_arg |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 329 | // After creating the list |
| 330 | // normal_arg |
| 331 | // (space for optional_arg) |
| 332 | // vararg-list |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 333 | vararg_count = argcount - ufunc->uf_args.ga_len; |
| 334 | if (vararg_count < 0) |
| 335 | vararg_count = 0; |
| 336 | else |
| 337 | argcount -= vararg_count; |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 338 | if (exe_newlist(vararg_count, ectx) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 339 | return FAIL; |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 340 | |
| 341 | vararg_count = 1; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 344 | arg_to_add = ufunc->uf_args.ga_len - argcount; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 345 | if (arg_to_add < 0) |
| 346 | { |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 347 | if (arg_to_add == -1) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 348 | emsg(_(e_one_argument_too_many)); |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 349 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 350 | semsg(_(e_nr_arguments_too_many), -arg_to_add); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 351 | return FAIL; |
| 352 | } |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 353 | |
| 354 | // Reserve space for: |
| 355 | // - missing arguments |
| 356 | // - stack frame |
| 357 | // - local variables |
| 358 | // - if needed: a counter for number of closures created in |
| 359 | // ectx->ec_funcrefs. |
| 360 | varcount = dfunc->df_varcount + dfunc->df_has_closure; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 361 | if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 362 | return FAIL; |
| 363 | |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 364 | // If depth of calling is getting too high, don't execute the function. |
| 365 | if (funcdepth_increment() == FAIL) |
| 366 | return FAIL; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 367 | ++ex_nesting_level; |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 368 | |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 369 | // Only make a copy of funclocal if it contains something to restore. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 370 | if (ectx->ec_funclocal.floc_restore_cmdmod) |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 371 | { |
| 372 | floc = ALLOC_ONE(funclocal_T); |
| 373 | if (floc == NULL) |
| 374 | return FAIL; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 375 | *floc = ectx->ec_funclocal; |
| 376 | ectx->ec_funclocal.floc_restore_cmdmod = FALSE; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 379 | // Move the vararg-list to below the missing optional arguments. |
| 380 | if (vararg_count > 0 && arg_to_add > 0) |
| 381 | *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 382 | |
| 383 | // Reserve space for omitted optional arguments, filled in soon. |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 384 | for (idx = 0; idx < arg_to_add; ++idx) |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 385 | STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 386 | ectx->ec_stack.ga_len += arg_to_add; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 387 | |
| 388 | // Store current execution state in stack frame for ISN_RETURN. |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 389 | STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx; |
| 390 | 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] | 391 | 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] | 392 | STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = |
| 393 | (void *)ectx->ec_outer_ref; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 394 | STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 395 | 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] | 396 | ectx->ec_frame_idx = ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 397 | |
| 398 | // Initialize local variables |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 399 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 400 | STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 401 | if (dfunc->df_has_closure) |
| 402 | { |
| 403 | typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount); |
| 404 | |
| 405 | tv->v_type = VAR_NUMBER; |
| 406 | tv->vval.v_number = 0; |
| 407 | } |
| 408 | ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 409 | |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 410 | if (pt != NULL || ufunc->uf_partial != NULL |
| 411 | || (ufunc->uf_flags & FC_CLOSURE)) |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 412 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 413 | outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T); |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 414 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 415 | if (ref == NULL) |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 416 | return FAIL; |
| 417 | if (pt != NULL) |
| 418 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 419 | ref->or_outer = &pt->pt_outer; |
| 420 | ++pt->pt_refcount; |
| 421 | ref->or_partial = pt; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 422 | } |
| 423 | else if (ufunc->uf_partial != NULL) |
| 424 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 425 | ref->or_outer = &ufunc->uf_partial->pt_outer; |
| 426 | ++ufunc->uf_partial->pt_refcount; |
| 427 | ref->or_partial = ufunc->uf_partial; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 428 | } |
| 429 | else |
| 430 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 431 | ref->or_outer = ALLOC_CLEAR_ONE(outer_T); |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 432 | if (unlikely(ref->or_outer == NULL)) |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 433 | { |
| 434 | vim_free(ref); |
| 435 | return FAIL; |
| 436 | } |
| 437 | ref->or_outer_allocated = TRUE; |
| 438 | ref->or_outer->out_stack = &ectx->ec_stack; |
| 439 | ref->or_outer->out_frame_idx = ectx->ec_frame_idx; |
| 440 | if (ectx->ec_outer_ref != NULL) |
| 441 | ref->or_outer->out_up = ectx->ec_outer_ref->or_outer; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 442 | } |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 443 | ectx->ec_outer_ref = ref; |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 444 | } |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 445 | else |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 446 | ectx->ec_outer_ref = NULL; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 447 | |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 448 | ++ufunc->uf_calls; |
| 449 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 450 | // Set execution state to the start of the called function. |
| 451 | ectx->ec_dfunc_idx = cdf_idx; |
Bram Moolenaar | e5ea346 | 2021-01-25 21:01:48 +0100 | [diff] [blame] | 452 | ectx->ec_instr = INSTRUCTIONS(dfunc); |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 453 | entry = estack_push_ufunc(ufunc, 1); |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 454 | if (entry != NULL) |
| 455 | { |
| 456 | // Set the script context to the script where the function was defined. |
Bram Moolenaar | c70fe46 | 2021-04-17 17:59:19 +0200 | [diff] [blame] | 457 | // Save the current context so it can be restored on return. |
| 458 | entry->es_save_sctx = current_sctx; |
| 459 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 460 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 461 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 462 | // Start execution at the first instruction. |
| 463 | ectx->ec_iidx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 464 | |
| 465 | return OK; |
| 466 | } |
| 467 | |
| 468 | // Get pointer to item in the stack. |
| 469 | #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx) |
| 470 | |
| 471 | /* |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 472 | * Used when returning from a function: Check if any closure is still |
| 473 | * referenced. If so then move the arguments and variables to a separate piece |
| 474 | * of stack to be used when the closure is called. |
| 475 | * When "free_arguments" is TRUE the arguments are to be freed. |
| 476 | * Returns FAIL when out of memory. |
| 477 | */ |
| 478 | static int |
| 479 | handle_closure_in_use(ectx_T *ectx, int free_arguments) |
| 480 | { |
| 481 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 482 | + ectx->ec_dfunc_idx; |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 483 | int argcount; |
| 484 | int top; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 485 | int idx; |
| 486 | typval_T *tv; |
| 487 | int closure_in_use = FALSE; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 488 | garray_T *gap = &ectx->ec_funcrefs; |
| 489 | varnumber_T closure_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 490 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 491 | if (dfunc->df_ufunc == NULL) |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 492 | return OK; // function was freed |
| 493 | if (dfunc->df_has_closure == 0) |
| 494 | return OK; // no closures |
| 495 | tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount); |
| 496 | closure_count = tv->vval.v_number; |
| 497 | if (closure_count == 0) |
| 498 | return OK; // no funcrefs created |
| 499 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 500 | argcount = ufunc_argcount(dfunc->df_ufunc); |
| 501 | top = ectx->ec_frame_idx - argcount; |
| 502 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 503 | // Check if any created closure is still in use. |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 504 | for (idx = 0; idx < closure_count; ++idx) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 505 | { |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 506 | partial_T *pt; |
| 507 | int off = gap->ga_len - closure_count + idx; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 508 | |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 509 | if (off < 0) |
| 510 | continue; // count is off or already done |
| 511 | pt = ((partial_T **)gap->ga_data)[off]; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 512 | if (pt->pt_refcount > 1) |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 513 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 514 | int refcount = pt->pt_refcount; |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 515 | int i; |
| 516 | |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 517 | // A Reference in a local variables doesn't count, it gets |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 518 | // unreferenced on return. |
| 519 | for (i = 0; i < dfunc->df_varcount; ++i) |
| 520 | { |
| 521 | typval_T *stv = STACK_TV(ectx->ec_frame_idx |
| 522 | + STACK_FRAME_SIZE + i); |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 523 | if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial) |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 524 | --refcount; |
| 525 | } |
| 526 | if (refcount > 1) |
| 527 | { |
| 528 | closure_in_use = TRUE; |
| 529 | break; |
| 530 | } |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 531 | } |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | if (closure_in_use) |
| 535 | { |
| 536 | funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T); |
| 537 | typval_T *stack; |
| 538 | |
| 539 | // A closure is using the arguments and/or local variables. |
| 540 | // Move them to the called function. |
| 541 | if (funcstack == NULL) |
| 542 | return FAIL; |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 543 | funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE; |
| 544 | funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 545 | stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len); |
| 546 | funcstack->fs_ga.ga_data = stack; |
| 547 | if (stack == NULL) |
| 548 | { |
| 549 | vim_free(funcstack); |
| 550 | return FAIL; |
| 551 | } |
| 552 | |
| 553 | // Move or copy the arguments. |
| 554 | for (idx = 0; idx < argcount; ++idx) |
| 555 | { |
| 556 | tv = STACK_TV(top + idx); |
| 557 | if (free_arguments) |
| 558 | { |
| 559 | *(stack + idx) = *tv; |
| 560 | tv->v_type = VAR_UNKNOWN; |
| 561 | } |
| 562 | else |
| 563 | copy_tv(tv, stack + idx); |
| 564 | } |
| 565 | // Move the local variables. |
| 566 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
| 567 | { |
| 568 | tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx); |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 569 | |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 570 | // A partial created for a local function, that is also used as a |
| 571 | // local variable, has a reference count for the variable, thus |
| 572 | // will never go down to zero. When all these refcounts are one |
| 573 | // then the funcstack is unused. We need to count how many we have |
| 574 | // so we need when to check. |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 575 | if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL) |
| 576 | { |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 577 | int i; |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 578 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 579 | for (i = 0; i < closure_count; ++i) |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 580 | if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[ |
| 581 | gap->ga_len - closure_count + i]) |
| 582 | ++funcstack->fs_min_refcount; |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 585 | *(stack + funcstack->fs_var_offset + idx) = *tv; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 586 | tv->v_type = VAR_UNKNOWN; |
| 587 | } |
| 588 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 589 | for (idx = 0; idx < closure_count; ++idx) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 590 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 591 | partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len |
| 592 | - closure_count + idx]; |
| 593 | if (pt->pt_refcount > 1) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 594 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 595 | ++funcstack->fs_refcount; |
| 596 | pt->pt_funcstack = funcstack; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 597 | pt->pt_outer.out_stack = &funcstack->fs_ga; |
| 598 | pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 603 | for (idx = 0; idx < closure_count; ++idx) |
| 604 | partial_unref(((partial_T **)gap->ga_data)[gap->ga_len |
| 605 | - closure_count + idx]); |
| 606 | gap->ga_len -= closure_count; |
| 607 | if (gap->ga_len == 0) |
| 608 | ga_clear(gap); |
| 609 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 610 | return OK; |
| 611 | } |
| 612 | |
| 613 | /* |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 614 | * Called when a partial is freed or its reference count goes down to one. The |
| 615 | * funcstack may be the only reference to the partials in the local variables. |
| 616 | * Go over all of them, the funcref and can be freed if all partials |
| 617 | * referencing the funcstack have a reference count of one. |
| 618 | */ |
| 619 | void |
| 620 | funcstack_check_refcount(funcstack_T *funcstack) |
| 621 | { |
| 622 | int i; |
| 623 | garray_T *gap = &funcstack->fs_ga; |
| 624 | int done = 0; |
| 625 | |
| 626 | if (funcstack->fs_refcount > funcstack->fs_min_refcount) |
| 627 | return; |
| 628 | for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i) |
| 629 | { |
| 630 | typval_T *tv = ((typval_T *)gap->ga_data) + i; |
| 631 | |
| 632 | if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL |
| 633 | && tv->vval.v_partial->pt_funcstack == funcstack |
| 634 | && tv->vval.v_partial->pt_refcount == 1) |
| 635 | ++done; |
| 636 | } |
| 637 | if (done == funcstack->fs_min_refcount) |
| 638 | { |
| 639 | typval_T *stack = gap->ga_data; |
| 640 | |
| 641 | // All partials referencing the funcstack have a reference count of |
| 642 | // one, thus the funcstack is no longer of use. |
| 643 | for (i = 0; i < gap->ga_len; ++i) |
| 644 | clear_tv(stack + i); |
| 645 | vim_free(stack); |
| 646 | vim_free(funcstack); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 651 | * Return from the current function. |
| 652 | */ |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 653 | static int |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 654 | func_return(ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 655 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 656 | int idx; |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 657 | int ret_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 658 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 659 | + ectx->ec_dfunc_idx; |
| 660 | int argcount = ufunc_argcount(dfunc->df_ufunc); |
| 661 | int top = ectx->ec_frame_idx - argcount; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 662 | estack_T *entry; |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 663 | int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx |
| 664 | + STACK_FRAME_FUNC_OFF)->vval.v_number; |
Bram Moolenaar | b06b50d | 2021-04-26 21:39:25 +0200 | [diff] [blame] | 665 | funclocal_T *floc; |
| 666 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 667 | dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data) |
| 668 | + prev_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 669 | |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 670 | if (do_profiling == PROF_YES) |
| 671 | { |
| 672 | ufunc_T *caller = prev_dfunc->df_ufunc; |
| 673 | |
| 674 | if (dfunc->df_ufunc->uf_profiling |
| 675 | || (caller != NULL && caller->uf_profiling)) |
| 676 | { |
| 677 | profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data) |
| 678 | + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller); |
| 679 | --profile_info_ga.ga_len; |
| 680 | } |
| 681 | } |
| 682 | #endif |
Bram Moolenaar | 919c12c | 2021-12-14 14:29:16 +0000 | [diff] [blame^] | 683 | |
| 684 | // No check for uf_refcount being zero, cannot think of a way that would |
| 685 | // happen. |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 686 | --dfunc->df_ufunc->uf_calls; |
| 687 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 688 | // execution context goes one level up |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 689 | entry = estack_pop(); |
| 690 | if (entry != NULL) |
Bram Moolenaar | c70fe46 | 2021-04-17 17:59:19 +0200 | [diff] [blame] | 691 | current_sctx = entry->es_save_sctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 692 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 693 | if (handle_closure_in_use(ectx, TRUE) == FAIL) |
| 694 | return FAIL; |
| 695 | |
| 696 | // Clear the arguments. |
| 697 | for (idx = top; idx < ectx->ec_frame_idx; ++idx) |
| 698 | clear_tv(STACK_TV(idx)); |
| 699 | |
| 700 | // Clear local variables and temp values, but not the return value. |
| 701 | for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 702 | idx < ectx->ec_stack.ga_len - 1; ++idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 703 | clear_tv(STACK_TV(idx)); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 704 | |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 705 | // The return value should be on top of the stack. However, when aborting |
| 706 | // it may not be there and ec_frame_idx is the top of the stack. |
| 707 | ret_idx = ectx->ec_stack.ga_len - 1; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 708 | if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF) |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 709 | ret_idx = 0; |
| 710 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 711 | if (ectx->ec_outer_ref != NULL) |
| 712 | { |
| 713 | if (ectx->ec_outer_ref->or_outer_allocated) |
| 714 | vim_free(ectx->ec_outer_ref->or_outer); |
| 715 | partial_unref(ectx->ec_outer_ref->or_partial); |
| 716 | vim_free(ectx->ec_outer_ref); |
| 717 | } |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 718 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 719 | // Restore the previous frame. |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 720 | ectx->ec_dfunc_idx = prev_dfunc_idx; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 721 | ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx |
| 722 | + STACK_FRAME_IIDX_OFF)->vval.v_number; |
Bram Moolenaar | 5930ddc | 2021-04-26 20:32:59 +0200 | [diff] [blame] | 723 | ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx |
| 724 | + STACK_FRAME_INSTR_OFF)->vval.v_string; |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 725 | ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 726 | + STACK_FRAME_OUTER_OFF)->vval.v_string; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 727 | floc = (void *)STACK_TV(ectx->ec_frame_idx |
| 728 | + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string; |
Bram Moolenaar | 5366e1a | 2020-10-01 13:01:34 +0200 | [diff] [blame] | 729 | // restoring ec_frame_idx must be last |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 730 | ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx |
| 731 | + STACK_FRAME_IDX_OFF)->vval.v_number; |
Bram Moolenaar | d386e92 | 2021-04-25 14:48:49 +0200 | [diff] [blame] | 732 | |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 733 | if (floc == NULL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 734 | ectx->ec_funclocal.floc_restore_cmdmod = FALSE; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 735 | else |
| 736 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 737 | ectx->ec_funclocal = *floc; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 738 | vim_free(floc); |
| 739 | } |
| 740 | |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 741 | if (ret_idx > 0) |
| 742 | { |
| 743 | // Reset the stack to the position before the call, with a spot for the |
| 744 | // return value, moved there from above the frame. |
| 745 | ectx->ec_stack.ga_len = top + 1; |
| 746 | *STACK_TV_BOT(-1) = *STACK_TV(ret_idx); |
| 747 | } |
| 748 | else |
| 749 | // Reset the stack to the position before the call. |
| 750 | ectx->ec_stack.ga_len = top; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 751 | |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 752 | funcdepth_decrement(); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 753 | --ex_nesting_level; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 754 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | #undef STACK_TV |
| 758 | |
| 759 | /* |
| 760 | * Prepare arguments and rettv for calling a builtin or user function. |
| 761 | */ |
| 762 | static int |
| 763 | call_prepare(int argcount, typval_T *argvars, ectx_T *ectx) |
| 764 | { |
| 765 | int idx; |
| 766 | typval_T *tv; |
| 767 | |
| 768 | // Move arguments from bottom of the stack to argvars[] and add terminator. |
| 769 | for (idx = 0; idx < argcount; ++idx) |
| 770 | argvars[idx] = *STACK_TV_BOT(idx - argcount); |
| 771 | argvars[argcount].v_type = VAR_UNKNOWN; |
| 772 | |
| 773 | // Result replaces the arguments on the stack. |
| 774 | if (argcount > 0) |
| 775 | ectx->ec_stack.ga_len -= argcount - 1; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 776 | else if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 777 | return FAIL; |
| 778 | else |
| 779 | ++ectx->ec_stack.ga_len; |
| 780 | |
| 781 | // Default return value is zero. |
| 782 | tv = STACK_TV_BOT(-1); |
| 783 | tv->v_type = VAR_NUMBER; |
| 784 | tv->vval.v_number = 0; |
| 785 | |
| 786 | return OK; |
| 787 | } |
| 788 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 789 | // Ugly global to avoid passing the execution context around through many |
| 790 | // layers. |
| 791 | static ectx_T *current_ectx = NULL; |
| 792 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 793 | /* |
| 794 | * Call a builtin function by index. |
| 795 | */ |
| 796 | static int |
| 797 | call_bfunc(int func_idx, int argcount, ectx_T *ectx) |
| 798 | { |
| 799 | typval_T argvars[MAX_FUNC_ARGS]; |
| 800 | int idx; |
Bram Moolenaar | 171fb92 | 2020-10-28 16:54:47 +0100 | [diff] [blame] | 801 | int did_emsg_before = did_emsg; |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 802 | ectx_T *prev_ectx = current_ectx; |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 803 | char *save_func_name = ectx->ec_where.wt_func_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 804 | |
| 805 | if (call_prepare(argcount, argvars, ectx) == FAIL) |
| 806 | return FAIL; |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 807 | ectx->ec_where.wt_func_name = internal_func_name(func_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 808 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 809 | // Call the builtin function. Set "current_ectx" so that when it |
| 810 | // recursively invokes call_def_function() a closure context can be set. |
| 811 | current_ectx = ectx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 812 | call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1)); |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 813 | current_ectx = prev_ectx; |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 814 | ectx->ec_where.wt_func_name = save_func_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 815 | |
| 816 | // Clear the arguments. |
| 817 | for (idx = 0; idx < argcount; ++idx) |
| 818 | clear_tv(&argvars[idx]); |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 819 | |
Bram Moolenaar | 57f799e | 2020-12-12 20:42:19 +0100 | [diff] [blame] | 820 | if (did_emsg > did_emsg_before) |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 821 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 822 | return OK; |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | * Execute a user defined function. |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 827 | * If the function is compiled this will add a stack frame and set the |
| 828 | * instruction pointer at the start of the function. |
| 829 | * Otherwise the function is called here. |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 830 | * 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] | 831 | * "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] | 832 | */ |
| 833 | static int |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 834 | call_ufunc( |
| 835 | ufunc_T *ufunc, |
| 836 | partial_T *pt, |
| 837 | int argcount, |
| 838 | ectx_T *ectx, |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 839 | isn_T *iptr, |
| 840 | dict_T *selfdict) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 841 | { |
| 842 | typval_T argvars[MAX_FUNC_ARGS]; |
| 843 | funcexe_T funcexe; |
| 844 | int error; |
| 845 | int idx; |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 846 | int did_emsg_before = did_emsg; |
Bram Moolenaar | 968a5b6 | 2021-06-15 19:32:40 +0200 | [diff] [blame] | 847 | compiletype_T compile_type = COMPILE_TYPE(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 848 | |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 849 | if (func_needs_compiling(ufunc, compile_type) |
| 850 | && compile_def_function(ufunc, FALSE, compile_type, NULL) |
| 851 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 852 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 853 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 854 | { |
Bram Moolenaar | 52c124d | 2020-12-20 21:43:35 +0100 | [diff] [blame] | 855 | error = check_user_func_argcount(ufunc, argcount); |
Bram Moolenaar | 5082471 | 2020-12-20 21:10:17 +0100 | [diff] [blame] | 856 | if (error != FCERR_UNKNOWN) |
| 857 | { |
| 858 | if (error == FCERR_TOOMANY) |
| 859 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 860 | else |
| 861 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 862 | return FAIL; |
| 863 | } |
| 864 | |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 865 | // The function has been compiled, can call it quickly. For a function |
| 866 | // that was defined later: we can call it directly next time. |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 867 | // TODO: what if the function was deleted and then defined again? |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 868 | if (iptr != NULL) |
| 869 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 870 | delete_instr(iptr); |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 871 | iptr->isn_type = ISN_DCALL; |
| 872 | iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 873 | iptr->isn_arg.dfunc.cdf_argcount = argcount; |
| 874 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 875 | return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx); |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 876 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 877 | |
| 878 | if (call_prepare(argcount, argvars, ectx) == FAIL) |
| 879 | return FAIL; |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 880 | CLEAR_FIELD(funcexe); |
Bram Moolenaar | 851f86b | 2021-12-13 14:26:44 +0000 | [diff] [blame] | 881 | funcexe.fe_evaluate = TRUE; |
| 882 | funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict(); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 883 | |
| 884 | // Call the user function. Result goes in last position on the stack. |
| 885 | // TODO: add selfdict if there is one |
| 886 | error = call_user_func_check(ufunc, argcount, argvars, |
Bram Moolenaar | 851f86b | 2021-12-13 14:26:44 +0000 | [diff] [blame] | 887 | STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 888 | |
| 889 | // Clear the arguments. |
| 890 | for (idx = 0; idx < argcount; ++idx) |
| 891 | clear_tv(&argvars[idx]); |
| 892 | |
| 893 | if (error != FCERR_NONE) |
| 894 | { |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 895 | user_func_error(error, ufunc->uf_name, &funcexe); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 896 | return FAIL; |
| 897 | } |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 898 | if (did_emsg > did_emsg_before) |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 899 | // Error other than from calling the function itself. |
| 900 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 901 | return OK; |
| 902 | } |
| 903 | |
| 904 | /* |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 905 | * If command modifiers were applied restore them. |
| 906 | */ |
| 907 | static void |
| 908 | may_restore_cmdmod(funclocal_T *funclocal) |
| 909 | { |
| 910 | if (funclocal->floc_restore_cmdmod) |
| 911 | { |
| 912 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 913 | undo_cmdmod(&cmdmod); |
| 914 | cmdmod = funclocal->floc_save_cmdmod; |
| 915 | funclocal->floc_restore_cmdmod = FALSE; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | /* |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 920 | * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was |
| 921 | * pressed. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 922 | */ |
| 923 | static int |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 924 | vim9_aborting(int prev_uncaught_emsg) |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 925 | { |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 926 | return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 930 | * Execute a function by "name". |
| 931 | * This can be a builtin function or a user function. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 932 | * "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] | 933 | * Returns FAIL if not found without an error message. |
| 934 | */ |
| 935 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 936 | call_by_name( |
| 937 | char_u *name, |
| 938 | int argcount, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 939 | ectx_T *ectx, |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 940 | isn_T *iptr, |
| 941 | dict_T *selfdict) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 942 | { |
| 943 | ufunc_T *ufunc; |
| 944 | |
| 945 | if (builtin_function(name, -1)) |
| 946 | { |
| 947 | int func_idx = find_internal_func(name); |
| 948 | |
| 949 | if (func_idx < 0) |
| 950 | return FAIL; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 951 | if (check_internal_func(func_idx, argcount) < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 952 | return FAIL; |
| 953 | return call_bfunc(func_idx, argcount, ectx); |
| 954 | } |
| 955 | |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 956 | ufunc = find_func(name, FALSE, NULL); |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 957 | |
| 958 | if (ufunc == NULL) |
| 959 | { |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 960 | int prev_uncaught_emsg = uncaught_emsg; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 961 | |
| 962 | if (script_autoload(name, TRUE)) |
| 963 | // loaded a package, search for the function again |
| 964 | ufunc = find_func(name, FALSE, NULL); |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 965 | |
| 966 | if (vim9_aborting(prev_uncaught_emsg)) |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 967 | return FAIL; // bail out if loading the script caused an error |
| 968 | } |
| 969 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 970 | if (ufunc != NULL) |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 971 | { |
Bram Moolenaar | 6ce46b9 | 2021-08-07 15:35:36 +0200 | [diff] [blame] | 972 | if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL) |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 973 | { |
| 974 | int i; |
| 975 | typval_T *argv = STACK_TV_BOT(0) - argcount; |
| 976 | |
| 977 | // The function can change at runtime, check that the argument |
| 978 | // types are correct. |
| 979 | for (i = 0; i < argcount; ++i) |
| 980 | { |
Bram Moolenaar | e3ffcd9 | 2021-03-08 21:47:13 +0100 | [diff] [blame] | 981 | type_T *type = NULL; |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 982 | |
Bram Moolenaar | 6ce46b9 | 2021-08-07 15:35:36 +0200 | [diff] [blame] | 983 | if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL) |
Bram Moolenaar | e3ffcd9 | 2021-03-08 21:47:13 +0100 | [diff] [blame] | 984 | type = ufunc->uf_arg_types[i]; |
| 985 | else if (ufunc->uf_va_type != NULL) |
| 986 | type = ufunc->uf_va_type->tt_member; |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 987 | if (type != NULL && check_typval_arg_type(type, |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 988 | &argv[i], NULL, i + 1) == FAIL) |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 989 | return FAIL; |
| 990 | } |
| 991 | } |
| 992 | |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 993 | return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict); |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 994 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 995 | |
| 996 | return FAIL; |
| 997 | } |
| 998 | |
| 999 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1000 | call_partial( |
| 1001 | typval_T *tv, |
| 1002 | int argcount_arg, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1003 | ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1004 | { |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 1005 | int argcount = argcount_arg; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1006 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1007 | int called_emsg_before = called_emsg; |
Bram Moolenaar | cb6cbf2 | 2021-01-12 17:17:01 +0100 | [diff] [blame] | 1008 | int res = FAIL; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1009 | dict_T *selfdict = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1010 | |
| 1011 | if (tv->v_type == VAR_PARTIAL) |
| 1012 | { |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 1013 | partial_T *pt = tv->vval.v_partial; |
| 1014 | int i; |
| 1015 | |
| 1016 | if (pt->pt_argc > 0) |
| 1017 | { |
| 1018 | // Make space for arguments from the partial, shift the "argcount" |
| 1019 | // arguments up. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1020 | if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc)) |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 1021 | return FAIL; |
| 1022 | for (i = 1; i <= argcount; ++i) |
| 1023 | *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i); |
| 1024 | ectx->ec_stack.ga_len += pt->pt_argc; |
| 1025 | argcount += pt->pt_argc; |
| 1026 | |
| 1027 | // copy the arguments from the partial onto the stack |
| 1028 | for (i = 0; i < pt->pt_argc; ++i) |
| 1029 | copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i)); |
| 1030 | } |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1031 | selfdict = pt->pt_dict; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1032 | |
| 1033 | if (pt->pt_func != NULL) |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1034 | return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict); |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 1035 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1036 | name = pt->pt_name; |
| 1037 | } |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1038 | else if (tv->v_type == VAR_FUNC) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1039 | name = tv->vval.v_string; |
Bram Moolenaar | 95006e3 | 2020-08-29 17:47:08 +0200 | [diff] [blame] | 1040 | if (name != NULL) |
| 1041 | { |
| 1042 | char_u fname_buf[FLEN_FIXED + 1]; |
| 1043 | char_u *tofree = NULL; |
| 1044 | int error = FCERR_NONE; |
| 1045 | char_u *fname; |
| 1046 | |
| 1047 | // May need to translate <SNR>123_ to K_SNR. |
| 1048 | fname = fname_trans_sid(name, fname_buf, &tofree, &error); |
| 1049 | if (error != FCERR_NONE) |
| 1050 | res = FAIL; |
| 1051 | else |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1052 | res = call_by_name(fname, argcount, ectx, NULL, selfdict); |
Bram Moolenaar | 95006e3 | 2020-08-29 17:47:08 +0200 | [diff] [blame] | 1053 | vim_free(tofree); |
| 1054 | } |
| 1055 | |
Bram Moolenaar | cb6cbf2 | 2021-01-12 17:17:01 +0100 | [diff] [blame] | 1056 | if (res == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1057 | { |
| 1058 | if (called_emsg == called_emsg_before) |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1059 | semsg(_(e_unknownfunc), |
| 1060 | name == NULL ? (char_u *)"[unknown]" : name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1061 | return FAIL; |
| 1062 | } |
| 1063 | return OK; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 1067 | * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return |
| 1068 | * TRUE. |
| 1069 | */ |
| 1070 | static int |
| 1071 | error_if_locked(int lock, char *error) |
| 1072 | { |
| 1073 | if (lock & (VAR_LOCKED | VAR_FIXED)) |
| 1074 | { |
| 1075 | emsg(_(error)); |
| 1076 | return TRUE; |
| 1077 | } |
| 1078 | return FALSE; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 1082 | * Give an error if "tv" is not a number and return FAIL. |
| 1083 | */ |
| 1084 | static int |
| 1085 | check_for_number(typval_T *tv) |
| 1086 | { |
| 1087 | if (tv->v_type != VAR_NUMBER) |
| 1088 | { |
| 1089 | semsg(_(e_expected_str_but_got_str), |
| 1090 | vartype_name(VAR_NUMBER), vartype_name(tv->v_type)); |
| 1091 | return FAIL; |
| 1092 | } |
| 1093 | return OK; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1097 | * Store "tv" in variable "name". |
| 1098 | * This is for s: and g: variables. |
| 1099 | */ |
| 1100 | static void |
| 1101 | store_var(char_u *name, typval_T *tv) |
| 1102 | { |
| 1103 | funccal_entry_T entry; |
Bram Moolenaar | d877a57 | 2021-04-01 19:42:48 +0200 | [diff] [blame] | 1104 | int flags = ASSIGN_DECL; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1105 | |
Bram Moolenaar | d877a57 | 2021-04-01 19:42:48 +0200 | [diff] [blame] | 1106 | if (tv->v_lock) |
| 1107 | flags |= ASSIGN_CONST; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1108 | save_funccal(&entry); |
Bram Moolenaar | d877a57 | 2021-04-01 19:42:48 +0200 | [diff] [blame] | 1109 | set_var_const(name, NULL, tv, FALSE, flags, 0); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1110 | restore_funccal(); |
| 1111 | } |
| 1112 | |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1113 | /* |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1114 | * Convert "tv" to a string. |
| 1115 | * Return FAIL if not allowed. |
| 1116 | */ |
| 1117 | static int |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1118 | do_2string(typval_T *tv, int is_2string_any, int tolerant) |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1119 | { |
| 1120 | if (tv->v_type != VAR_STRING) |
| 1121 | { |
| 1122 | char_u *str; |
| 1123 | |
| 1124 | if (is_2string_any) |
| 1125 | { |
| 1126 | switch (tv->v_type) |
| 1127 | { |
| 1128 | case VAR_SPECIAL: |
| 1129 | case VAR_BOOL: |
| 1130 | case VAR_NUMBER: |
| 1131 | case VAR_FLOAT: |
| 1132 | case VAR_BLOB: break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1133 | |
| 1134 | case VAR_LIST: |
| 1135 | if (tolerant) |
| 1136 | { |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1137 | char_u *s, *e, *p; |
| 1138 | garray_T ga; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1139 | |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1140 | ga_init2(&ga, sizeof(char_u *), 1); |
| 1141 | |
| 1142 | // Convert to NL separated items, then |
| 1143 | // escape the items and replace the NL with |
| 1144 | // a space. |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1145 | str = typval2string(tv, TRUE); |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1146 | if (str == NULL) |
| 1147 | return FAIL; |
| 1148 | s = str; |
| 1149 | while ((e = vim_strchr(s, '\n')) != NULL) |
| 1150 | { |
| 1151 | *e = NUL; |
Bram Moolenaar | 21c1a0c | 2021-10-17 17:20:23 +0100 | [diff] [blame] | 1152 | p = vim_strsave_fnameescape(s, |
| 1153 | VSE_NONE); |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1154 | if (p != NULL) |
| 1155 | { |
| 1156 | ga_concat(&ga, p); |
| 1157 | ga_concat(&ga, (char_u *)" "); |
| 1158 | vim_free(p); |
| 1159 | } |
| 1160 | s = e + 1; |
| 1161 | } |
| 1162 | vim_free(str); |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1163 | clear_tv(tv); |
| 1164 | tv->v_type = VAR_STRING; |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1165 | tv->vval.v_string = ga.ga_data; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1166 | return OK; |
| 1167 | } |
| 1168 | // FALLTHROUGH |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1169 | default: to_string_error(tv->v_type); |
| 1170 | return FAIL; |
| 1171 | } |
| 1172 | } |
Bram Moolenaar | 3445320 | 2021-01-31 13:08:38 +0100 | [diff] [blame] | 1173 | str = typval_tostring(tv, TRUE); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1174 | clear_tv(tv); |
| 1175 | tv->v_type = VAR_STRING; |
| 1176 | tv->vval.v_string = str; |
| 1177 | } |
| 1178 | return OK; |
| 1179 | } |
| 1180 | |
| 1181 | /* |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1182 | * When the value of "sv" is a null list of dict, allocate it. |
| 1183 | */ |
| 1184 | static void |
| 1185 | allocate_if_null(typval_T *tv) |
| 1186 | { |
| 1187 | switch (tv->v_type) |
| 1188 | { |
| 1189 | case VAR_LIST: |
| 1190 | if (tv->vval.v_list == NULL) |
Bram Moolenaar | fef8064 | 2021-02-03 20:01:19 +0100 | [diff] [blame] | 1191 | (void)rettv_list_alloc(tv); |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1192 | break; |
| 1193 | case VAR_DICT: |
| 1194 | if (tv->vval.v_dict == NULL) |
Bram Moolenaar | fef8064 | 2021-02-03 20:01:19 +0100 | [diff] [blame] | 1195 | (void)rettv_dict_alloc(tv); |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1196 | break; |
Bram Moolenaar | b7c21af | 2021-04-18 14:12:31 +0200 | [diff] [blame] | 1197 | case VAR_BLOB: |
| 1198 | if (tv->vval.v_blob == NULL) |
| 1199 | (void)rettv_blob_alloc(tv); |
| 1200 | break; |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1201 | default: |
| 1202 | break; |
| 1203 | } |
| 1204 | } |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1205 | |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1206 | /* |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1207 | * Return the character "str[index]" where "index" is the character index, |
| 1208 | * including composing characters. |
| 1209 | * If "index" is out of range NULL is returned. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1210 | */ |
| 1211 | char_u * |
| 1212 | char_from_string(char_u *str, varnumber_T index) |
| 1213 | { |
| 1214 | size_t nbyte = 0; |
| 1215 | varnumber_T nchar = index; |
| 1216 | size_t slen; |
| 1217 | |
| 1218 | if (str == NULL) |
| 1219 | return NULL; |
| 1220 | slen = STRLEN(str); |
| 1221 | |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1222 | // Do the same as for a list: a negative index counts from the end. |
| 1223 | // Optimization to check the first byte to be below 0x80 (and no composing |
| 1224 | // character follows) makes this a lot faster. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1225 | if (index < 0) |
| 1226 | { |
| 1227 | int clen = 0; |
| 1228 | |
| 1229 | for (nbyte = 0; nbyte < slen; ++clen) |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1230 | { |
| 1231 | if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80) |
| 1232 | ++nbyte; |
| 1233 | else if (enc_utf8) |
| 1234 | nbyte += utfc_ptr2len(str + nbyte); |
| 1235 | else |
| 1236 | nbyte += mb_ptr2len(str + nbyte); |
| 1237 | } |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1238 | nchar = clen + index; |
| 1239 | if (nchar < 0) |
| 1240 | // unlike list: index out of range results in empty string |
| 1241 | return NULL; |
| 1242 | } |
| 1243 | |
| 1244 | for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar) |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1245 | { |
| 1246 | if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80) |
| 1247 | ++nbyte; |
| 1248 | else if (enc_utf8) |
| 1249 | nbyte += utfc_ptr2len(str + nbyte); |
| 1250 | else |
| 1251 | nbyte += mb_ptr2len(str + nbyte); |
| 1252 | } |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1253 | if (nbyte >= slen) |
| 1254 | return NULL; |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1255 | return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte)); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * 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] | 1260 | * "str_len". Composing characters are included. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1261 | * If going over the end return "str_len". |
| 1262 | * If "idx" is negative count from the end, -1 is the last character. |
| 1263 | * When going over the start return -1. |
| 1264 | */ |
| 1265 | static long |
| 1266 | char_idx2byte(char_u *str, size_t str_len, varnumber_T idx) |
| 1267 | { |
| 1268 | varnumber_T nchar = idx; |
| 1269 | size_t nbyte = 0; |
| 1270 | |
| 1271 | if (nchar >= 0) |
| 1272 | { |
| 1273 | while (nchar > 0 && nbyte < str_len) |
| 1274 | { |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1275 | nbyte += mb_ptr2len(str + nbyte); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1276 | --nchar; |
| 1277 | } |
| 1278 | } |
| 1279 | else |
| 1280 | { |
| 1281 | nbyte = str_len; |
| 1282 | while (nchar < 0 && nbyte > 0) |
| 1283 | { |
| 1284 | --nbyte; |
| 1285 | nbyte -= mb_head_off(str, str + nbyte); |
| 1286 | ++nchar; |
| 1287 | } |
| 1288 | if (nchar < 0) |
| 1289 | return -1; |
| 1290 | } |
| 1291 | return (long)nbyte; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1295 | * Return the slice "str[first : last]" using character indexes. Composing |
| 1296 | * characters are included. |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1297 | * "exclusive" is TRUE for slice(). |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1298 | * Return NULL when the result is empty. |
| 1299 | */ |
| 1300 | char_u * |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1301 | 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] | 1302 | { |
| 1303 | long start_byte, end_byte; |
| 1304 | size_t slen; |
| 1305 | |
| 1306 | if (str == NULL) |
| 1307 | return NULL; |
| 1308 | slen = STRLEN(str); |
| 1309 | start_byte = char_idx2byte(str, slen, first); |
| 1310 | if (start_byte < 0) |
| 1311 | start_byte = 0; // first index very negative: use zero |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1312 | if ((last == -1 && !exclusive) || last == VARNUM_MAX) |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1313 | end_byte = (long)slen; |
| 1314 | else |
| 1315 | { |
| 1316 | end_byte = char_idx2byte(str, slen, last); |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1317 | if (!exclusive && end_byte >= 0 && end_byte < (long)slen) |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1318 | // end index is inclusive |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1319 | end_byte += mb_ptr2len(str + end_byte); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | if (start_byte >= (long)slen || end_byte <= start_byte) |
| 1323 | return NULL; |
| 1324 | return vim_strnsave(str + start_byte, end_byte - start_byte); |
| 1325 | } |
| 1326 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1327 | /* |
| 1328 | * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT. |
| 1329 | * When "dfunc_idx" is negative don't give an error. |
| 1330 | * Returns NULL for an error. |
| 1331 | */ |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1332 | static svar_T * |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1333 | get_script_svar(scriptref_T *sref, int dfunc_idx) |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1334 | { |
| 1335 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1336 | dfunc_T *dfunc = dfunc_idx < 0 ? NULL |
| 1337 | : ((dfunc_T *)def_functions.ga_data) + dfunc_idx; |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1338 | svar_T *sv; |
| 1339 | |
| 1340 | if (sref->sref_seq != si->sn_script_seq) |
| 1341 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1342 | // The script was reloaded after the function was compiled, the |
| 1343 | // script_idx may not be valid. |
| 1344 | if (dfunc != NULL) |
| 1345 | semsg(_(e_script_variable_invalid_after_reload_in_function_str), |
| 1346 | printable_func_name(dfunc->df_ufunc)); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1347 | return NULL; |
| 1348 | } |
| 1349 | sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx; |
Bram Moolenaar | 60dc827 | 2021-07-29 22:48:54 +0200 | [diff] [blame] | 1350 | if (!equal_type(sv->sv_type, sref->sref_type, 0)) |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1351 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1352 | if (dfunc != NULL) |
| 1353 | emsg(_(e_script_variable_type_changed)); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1354 | return NULL; |
| 1355 | } |
| 1356 | return sv; |
| 1357 | } |
| 1358 | |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1359 | /* |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1360 | * Function passed to do_cmdline() for splitting a script joined by NL |
| 1361 | * characters. |
| 1362 | */ |
| 1363 | static char_u * |
| 1364 | get_split_sourceline( |
| 1365 | int c UNUSED, |
| 1366 | void *cookie, |
| 1367 | int indent UNUSED, |
| 1368 | getline_opt_T options UNUSED) |
| 1369 | { |
| 1370 | source_cookie_T *sp = (source_cookie_T *)cookie; |
| 1371 | char_u *p; |
| 1372 | char_u *line; |
| 1373 | |
| 1374 | if (*sp->nextline == NUL) |
| 1375 | return NULL; |
| 1376 | p = vim_strchr(sp->nextline, '\n'); |
| 1377 | if (p == NULL) |
| 1378 | { |
| 1379 | line = vim_strsave(sp->nextline); |
| 1380 | sp->nextline += STRLEN(sp->nextline); |
| 1381 | } |
| 1382 | else |
| 1383 | { |
| 1384 | line = vim_strnsave(sp->nextline, p - sp->nextline); |
| 1385 | sp->nextline = p + 1; |
| 1386 | } |
| 1387 | return line; |
| 1388 | } |
| 1389 | |
| 1390 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1391 | * Execute a function by "name". |
| 1392 | * This can be a builtin function, user function or a funcref. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 1393 | * "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] | 1394 | */ |
| 1395 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1396 | call_eval_func( |
| 1397 | char_u *name, |
| 1398 | int argcount, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1399 | ectx_T *ectx, |
| 1400 | isn_T *iptr) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1401 | { |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1402 | int called_emsg_before = called_emsg; |
| 1403 | int res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1404 | |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1405 | res = call_by_name(name, argcount, ectx, iptr, NULL); |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1406 | if (res == FAIL && called_emsg == called_emsg_before) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1407 | { |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 1408 | dictitem_T *v; |
| 1409 | |
| 1410 | v = find_var(name, NULL, FALSE); |
| 1411 | if (v == NULL) |
| 1412 | { |
| 1413 | semsg(_(e_unknownfunc), name); |
| 1414 | return FAIL; |
| 1415 | } |
| 1416 | if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC) |
| 1417 | { |
| 1418 | semsg(_(e_unknownfunc), name); |
| 1419 | return FAIL; |
| 1420 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1421 | return call_partial(&v->di_tv, argcount, ectx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1422 | } |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1423 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | /* |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1427 | * When a function reference is used, fill a partial with the information |
| 1428 | * needed, especially when it is used as a closure. |
| 1429 | */ |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 1430 | int |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1431 | fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx) |
| 1432 | { |
| 1433 | pt->pt_func = ufunc; |
| 1434 | pt->pt_refcount = 1; |
| 1435 | |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 1436 | if (ufunc->uf_flags & FC_CLOSURE) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1437 | { |
| 1438 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 1439 | + ectx->ec_dfunc_idx; |
| 1440 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1441 | // The closure may need to find arguments and local variables in the |
| 1442 | // current stack. |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 1443 | pt->pt_outer.out_stack = &ectx->ec_stack; |
| 1444 | pt->pt_outer.out_frame_idx = ectx->ec_frame_idx; |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1445 | if (ectx->ec_outer_ref != NULL) |
| 1446 | { |
| 1447 | // The current context already has a context, link to that one. |
| 1448 | pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer; |
| 1449 | if (ectx->ec_outer_ref->or_partial != NULL) |
| 1450 | { |
| 1451 | pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial; |
| 1452 | ++pt->pt_outer.out_up_partial->pt_refcount; |
| 1453 | } |
| 1454 | } |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1455 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1456 | // If this function returns and the closure is still being used, we |
| 1457 | // need to make a copy of the context (arguments and local variables). |
| 1458 | // Store a reference to the partial so we can handle that. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1459 | if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1)) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1460 | { |
| 1461 | vim_free(pt); |
| 1462 | return FAIL; |
| 1463 | } |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1464 | // Extra variable keeps the count of closures created in the current |
| 1465 | // function call. |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1466 | ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx |
| 1467 | + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number; |
| 1468 | |
| 1469 | ((partial_T **)ectx->ec_funcrefs.ga_data) |
| 1470 | [ectx->ec_funcrefs.ga_len] = pt; |
| 1471 | ++pt->pt_refcount; |
| 1472 | ++ectx->ec_funcrefs.ga_len; |
| 1473 | } |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 1474 | ++ufunc->uf_refcount; |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1475 | return OK; |
| 1476 | } |
| 1477 | |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 1478 | /* |
| 1479 | * Execute iptr->isn_arg.string as an Ex command. |
| 1480 | */ |
| 1481 | static int |
| 1482 | exec_command(isn_T *iptr) |
| 1483 | { |
| 1484 | source_cookie_T cookie; |
| 1485 | |
| 1486 | SOURCING_LNUM = iptr->isn_lnum; |
| 1487 | // Pass getsourceline to get an error for a missing ":end" |
| 1488 | // command. |
| 1489 | CLEAR_FIELD(cookie); |
| 1490 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 1491 | if (do_cmdline(iptr->isn_arg.string, |
| 1492 | getsourceline, &cookie, |
| 1493 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL |
| 1494 | || did_emsg) |
| 1495 | return FAIL; |
| 1496 | return OK; |
| 1497 | } |
| 1498 | |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 1499 | // used for v_instr of typval of VAR_INSTR |
| 1500 | struct instr_S { |
| 1501 | ectx_T *instr_ectx; |
| 1502 | isn_T *instr_instr; |
| 1503 | }; |
| 1504 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1505 | // used for substitute_instr |
| 1506 | typedef struct subs_expr_S { |
| 1507 | ectx_T *subs_ectx; |
| 1508 | isn_T *subs_instr; |
| 1509 | int subs_status; |
| 1510 | } subs_expr_T; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1511 | |
| 1512 | // Get pointer to item in the stack. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1513 | #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1514 | |
| 1515 | // Get pointer to item at the bottom of the stack, -1 is the bottom. |
| 1516 | #undef STACK_TV_BOT |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1517 | #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] | 1518 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1519 | // Get pointer to a local variable on the stack. Negative for arguments. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1520 | #define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1521 | |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1522 | // Set when calling do_debug(). |
| 1523 | static ectx_T *debug_context = NULL; |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1524 | static int debug_var_count; |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1525 | |
| 1526 | /* |
| 1527 | * When debugging lookup "name" and return the typeval. |
| 1528 | * When not found return NULL. |
| 1529 | */ |
| 1530 | typval_T * |
| 1531 | lookup_debug_var(char_u *name) |
| 1532 | { |
| 1533 | int idx; |
| 1534 | dfunc_T *dfunc; |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1535 | ufunc_T *ufunc; |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1536 | ectx_T *ectx = debug_context; |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1537 | int varargs_off; |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1538 | |
| 1539 | if (ectx == NULL) |
| 1540 | return NULL; |
| 1541 | dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx; |
| 1542 | |
| 1543 | // Go through the local variable names, from last to first. |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1544 | for (idx = debug_var_count - 1; idx >= 0; --idx) |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1545 | { |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1546 | if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0) |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1547 | return STACK_TV_VAR(idx); |
| 1548 | } |
| 1549 | |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1550 | // Go through argument names. |
| 1551 | ufunc = dfunc->df_ufunc; |
| 1552 | varargs_off = ufunc->uf_va_name == NULL ? 0 : 1; |
| 1553 | for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx) |
| 1554 | if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0) |
| 1555 | return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len |
| 1556 | - varargs_off + idx); |
| 1557 | if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0) |
| 1558 | return STACK_TV(ectx->ec_frame_idx - 1); |
| 1559 | |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1560 | return NULL; |
| 1561 | } |
| 1562 | |
Bram Moolenaar | 26a4484 | 2021-09-02 18:49:06 +0200 | [diff] [blame] | 1563 | /* |
| 1564 | * Return TRUE if there might be a breakpoint in "ufunc", which is when a |
| 1565 | * breakpoint was set in that function or when there is any expression. |
| 1566 | */ |
| 1567 | int |
| 1568 | may_break_in_function(ufunc_T *ufunc) |
| 1569 | { |
| 1570 | return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint(); |
| 1571 | } |
| 1572 | |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1573 | static void |
| 1574 | handle_debug(isn_T *iptr, ectx_T *ectx) |
| 1575 | { |
| 1576 | char_u *line; |
| 1577 | ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data) |
| 1578 | + ectx->ec_dfunc_idx)->df_ufunc; |
| 1579 | isn_T *ni; |
| 1580 | int end_lnum = iptr->isn_lnum; |
| 1581 | garray_T ga; |
| 1582 | int lnum; |
| 1583 | |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 1584 | if (ex_nesting_level > debug_break_level) |
| 1585 | { |
| 1586 | linenr_T breakpoint; |
| 1587 | |
Bram Moolenaar | 26a4484 | 2021-09-02 18:49:06 +0200 | [diff] [blame] | 1588 | if (!may_break_in_function(ufunc)) |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 1589 | return; |
| 1590 | |
| 1591 | // check for the next breakpoint if needed |
| 1592 | breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, |
Bram Moolenaar | 8cec927 | 2021-06-23 20:20:53 +0200 | [diff] [blame] | 1593 | iptr->isn_arg.debug.dbg_break_lnum); |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 1594 | if (breakpoint <= 0 || breakpoint > iptr->isn_lnum) |
| 1595 | return; |
| 1596 | } |
| 1597 | |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1598 | SOURCING_LNUM = iptr->isn_lnum; |
| 1599 | debug_context = ectx; |
Bram Moolenaar | 8cec927 | 2021-06-23 20:20:53 +0200 | [diff] [blame] | 1600 | debug_var_count = iptr->isn_arg.debug.dbg_var_names_len; |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1601 | |
| 1602 | for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni) |
| 1603 | if (ni->isn_type == ISN_DEBUG |
| 1604 | || ni->isn_type == ISN_RETURN |
| 1605 | || ni->isn_type == ISN_RETURN_VOID) |
| 1606 | { |
Bram Moolenaar | 112bed0 | 2021-11-23 22:16:34 +0000 | [diff] [blame] | 1607 | end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1); |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1608 | break; |
| 1609 | } |
| 1610 | |
| 1611 | if (end_lnum > iptr->isn_lnum) |
| 1612 | { |
| 1613 | ga_init2(&ga, sizeof(char_u *), 10); |
| 1614 | for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum) |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 1615 | { |
Bram Moolenaar | 303215d | 2021-07-07 20:10:43 +0200 | [diff] [blame] | 1616 | char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1]; |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 1617 | |
Bram Moolenaar | 303215d | 2021-07-07 20:10:43 +0200 | [diff] [blame] | 1618 | if (p == NULL) |
| 1619 | continue; // left over from continuation line |
| 1620 | p = skipwhite(p); |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 1621 | if (*p == '#') |
| 1622 | break; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1623 | if (GA_GROW_OK(&ga, 1)) |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 1624 | ((char_u **)(ga.ga_data))[ga.ga_len++] = p; |
| 1625 | if (STRNCMP(p, "def ", 4) == 0) |
| 1626 | break; |
| 1627 | } |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1628 | line = ga_concat_strings(&ga, " "); |
| 1629 | vim_free(ga.ga_data); |
| 1630 | } |
| 1631 | else |
| 1632 | line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1]; |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1633 | |
Dominique Pelle | 6e96955 | 2021-06-17 13:53:41 +0200 | [diff] [blame] | 1634 | do_debug(line == NULL ? (char_u *)"[empty]" : line); |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1635 | debug_context = NULL; |
| 1636 | |
| 1637 | if (end_lnum > iptr->isn_lnum) |
| 1638 | vim_free(line); |
| 1639 | } |
| 1640 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1641 | /* |
| 1642 | * Execute instructions in execution context "ectx". |
| 1643 | * Return OK or FAIL; |
| 1644 | */ |
| 1645 | static int |
| 1646 | exec_instructions(ectx_T *ectx) |
| 1647 | { |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1648 | int ret = FAIL; |
| 1649 | int save_trylevel_at_start = ectx->ec_trylevel_at_start; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1650 | int dict_stack_len_at_start = dict_stack.ga_len; |
Bram Moolenaar | f785aa1 | 2021-02-11 21:19:34 +0100 | [diff] [blame] | 1651 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 1652 | // Start execution at the first instruction. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1653 | ectx->ec_iidx = 0; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1654 | |
Bram Moolenaar | ff65288 | 2021-05-16 15:24:49 +0200 | [diff] [blame] | 1655 | // Only catch exceptions in this instruction list. |
| 1656 | ectx->ec_trylevel_at_start = trylevel; |
| 1657 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1658 | for (;;) |
| 1659 | { |
Bram Moolenaar | a749019 | 2021-07-22 12:26:14 +0200 | [diff] [blame] | 1660 | static int breakcheck_count = 0; // using "static" makes it faster |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1661 | isn_T *iptr; |
Bram Moolenaar | a749019 | 2021-07-22 12:26:14 +0200 | [diff] [blame] | 1662 | typval_T *tv; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1663 | |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 1664 | if (unlikely(++breakcheck_count >= 100)) |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1665 | { |
| 1666 | line_breakcheck(); |
| 1667 | breakcheck_count = 0; |
| 1668 | } |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 1669 | if (unlikely(got_int)) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1670 | { |
| 1671 | // Turn CTRL-C into an exception. |
| 1672 | got_int = FALSE; |
Bram Moolenaar | 97acfc7 | 2020-03-22 13:44:28 +0100 | [diff] [blame] | 1673 | if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1674 | goto theend; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1675 | did_throw = TRUE; |
| 1676 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1677 | |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 1678 | if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL)) |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 1679 | { |
| 1680 | // Turn an error message into an exception. |
| 1681 | did_emsg = FALSE; |
| 1682 | if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1683 | goto theend; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 1684 | did_throw = TRUE; |
| 1685 | *msg_list = NULL; |
| 1686 | } |
| 1687 | |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 1688 | if (unlikely(did_throw)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1689 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1690 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1691 | trycmd_T *trycmd = NULL; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 1692 | int index = trystack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1693 | |
| 1694 | // An exception jumps to the first catch, finally, or returns from |
| 1695 | // the current function. |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 1696 | while (index > 0) |
| 1697 | { |
| 1698 | trycmd = ((trycmd_T *)trystack->ga_data) + index - 1; |
Bram Moolenaar | 834193a | 2021-06-30 20:39:15 +0200 | [diff] [blame] | 1699 | if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0) |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 1700 | break; |
| 1701 | // In the catch and finally block of this try we have to go up |
| 1702 | // one level. |
| 1703 | --index; |
| 1704 | trycmd = NULL; |
| 1705 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1706 | if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1707 | { |
Bram Moolenaar | 834193a | 2021-06-30 20:39:15 +0200 | [diff] [blame] | 1708 | if (trycmd->tcd_in_catch) |
| 1709 | { |
| 1710 | // exception inside ":catch", jump to ":finally" once |
| 1711 | ectx->ec_iidx = trycmd->tcd_finally_idx; |
| 1712 | trycmd->tcd_finally_idx = 0; |
| 1713 | } |
| 1714 | else |
| 1715 | // jump to first ":catch" |
| 1716 | ectx->ec_iidx = trycmd->tcd_catch_idx; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 1717 | trycmd->tcd_in_catch = TRUE; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 1718 | did_throw = FALSE; // don't come back here until :endtry |
| 1719 | trycmd->tcd_did_throw = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1720 | } |
| 1721 | else |
| 1722 | { |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 1723 | // Not inside try or need to return from current functions. |
| 1724 | // Push a dummy return value. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1725 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1726 | goto theend; |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 1727 | tv = STACK_TV_BOT(0); |
| 1728 | tv->v_type = VAR_NUMBER; |
| 1729 | tv->vval.v_number = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1730 | ++ectx->ec_stack.ga_len; |
| 1731 | if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1732 | { |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 1733 | // At the toplevel we are done. |
Bram Moolenaar | 257cc5e | 2020-02-19 17:06:11 +0100 | [diff] [blame] | 1734 | need_rethrow = TRUE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1735 | if (handle_closure_in_use(ectx, FALSE) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1736 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1737 | goto done; |
| 1738 | } |
| 1739 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1740 | if (func_return(ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1741 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1742 | } |
| 1743 | continue; |
| 1744 | } |
| 1745 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1746 | iptr = &ectx->ec_instr[ectx->ec_iidx++]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1747 | switch (iptr->isn_type) |
| 1748 | { |
| 1749 | // execute Ex command line |
| 1750 | case ISN_EXEC: |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 1751 | if (exec_command(iptr) == FAIL) |
| 1752 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1753 | break; |
| 1754 | |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1755 | // execute Ex command line split at NL characters. |
| 1756 | case ISN_EXEC_SPLIT: |
| 1757 | { |
| 1758 | source_cookie_T cookie; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 1759 | char_u *line; |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1760 | |
| 1761 | SOURCING_LNUM = iptr->isn_lnum; |
| 1762 | CLEAR_FIELD(cookie); |
| 1763 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 1764 | cookie.nextline = iptr->isn_arg.string; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 1765 | line = get_split_sourceline(0, &cookie, 0, 0); |
| 1766 | if (do_cmdline(line, |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1767 | get_split_sourceline, &cookie, |
| 1768 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) |
| 1769 | == FAIL |
| 1770 | || did_emsg) |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 1771 | { |
| 1772 | vim_free(line); |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1773 | goto on_error; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 1774 | } |
| 1775 | vim_free(line); |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1776 | } |
| 1777 | break; |
| 1778 | |
Bram Moolenaar | e4eed8c | 2021-12-01 15:22:56 +0000 | [diff] [blame] | 1779 | // execute Ex command line that is only a range |
| 1780 | case ISN_EXECRANGE: |
| 1781 | { |
| 1782 | exarg_T ea; |
| 1783 | char *error = NULL; |
| 1784 | |
| 1785 | CLEAR_FIELD(ea); |
| 1786 | ea.cmdidx = CMD_SIZE; |
| 1787 | ea.addr_type = ADDR_LINES; |
| 1788 | ea.cmd = iptr->isn_arg.string; |
| 1789 | parse_cmd_address(&ea, &error, FALSE); |
Bram Moolenaar | 01a4dcb | 2021-12-04 13:15:10 +0000 | [diff] [blame] | 1790 | if (ea.cmd == NULL) |
| 1791 | goto on_error; |
Bram Moolenaar | e4eed8c | 2021-12-01 15:22:56 +0000 | [diff] [blame] | 1792 | if (error == NULL) |
| 1793 | error = ex_range_without_command(&ea); |
| 1794 | if (error != NULL) |
| 1795 | { |
| 1796 | SOURCING_LNUM = iptr->isn_lnum; |
| 1797 | emsg(error); |
| 1798 | goto on_error; |
| 1799 | } |
| 1800 | } |
| 1801 | break; |
| 1802 | |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 1803 | // Evaluate an expression with legacy syntax, push it onto the |
| 1804 | // stack. |
| 1805 | case ISN_LEGACY_EVAL: |
| 1806 | { |
| 1807 | char_u *arg = iptr->isn_arg.string; |
| 1808 | int res; |
| 1809 | int save_flags = cmdmod.cmod_flags; |
| 1810 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1811 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1812 | goto theend; |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 1813 | tv = STACK_TV_BOT(0); |
| 1814 | init_tv(tv); |
| 1815 | cmdmod.cmod_flags |= CMOD_LEGACY; |
| 1816 | res = eval0(arg, tv, NULL, &EVALARG_EVALUATE); |
| 1817 | cmdmod.cmod_flags = save_flags; |
| 1818 | if (res == FAIL) |
| 1819 | goto on_error; |
| 1820 | ++ectx->ec_stack.ga_len; |
| 1821 | } |
| 1822 | break; |
| 1823 | |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 1824 | // push typeval VAR_INSTR with instructions to be executed |
| 1825 | case ISN_INSTR: |
| 1826 | { |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1827 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1828 | goto theend; |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 1829 | tv = STACK_TV_BOT(0); |
| 1830 | tv->vval.v_instr = ALLOC_ONE(instr_T); |
| 1831 | if (tv->vval.v_instr == NULL) |
| 1832 | goto on_error; |
| 1833 | ++ectx->ec_stack.ga_len; |
| 1834 | |
| 1835 | tv->v_type = VAR_INSTR; |
| 1836 | tv->vval.v_instr->instr_ectx = ectx; |
| 1837 | tv->vval.v_instr->instr_instr = iptr->isn_arg.instr; |
| 1838 | } |
| 1839 | break; |
| 1840 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1841 | // execute :substitute with an expression |
| 1842 | case ISN_SUBSTITUTE: |
| 1843 | { |
| 1844 | subs_T *subs = &iptr->isn_arg.subs; |
| 1845 | source_cookie_T cookie; |
| 1846 | struct subs_expr_S *save_instr = substitute_instr; |
| 1847 | struct subs_expr_S subs_instr; |
| 1848 | int res; |
| 1849 | |
| 1850 | subs_instr.subs_ectx = ectx; |
| 1851 | subs_instr.subs_instr = subs->subs_instr; |
| 1852 | subs_instr.subs_status = OK; |
| 1853 | substitute_instr = &subs_instr; |
| 1854 | |
| 1855 | SOURCING_LNUM = iptr->isn_lnum; |
| 1856 | // This is very much like ISN_EXEC |
| 1857 | CLEAR_FIELD(cookie); |
| 1858 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 1859 | res = do_cmdline(subs->subs_cmd, |
| 1860 | getsourceline, &cookie, |
| 1861 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); |
| 1862 | substitute_instr = save_instr; |
| 1863 | |
| 1864 | if (res == FAIL || did_emsg |
| 1865 | || subs_instr.subs_status == FAIL) |
| 1866 | goto on_error; |
| 1867 | } |
| 1868 | break; |
| 1869 | |
| 1870 | case ISN_FINISH: |
| 1871 | goto done; |
| 1872 | |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 1873 | case ISN_REDIRSTART: |
| 1874 | // create a dummy entry for var_redir_str() |
| 1875 | if (alloc_redir_lval() == FAIL) |
| 1876 | goto on_error; |
| 1877 | |
| 1878 | // The output is stored in growarray "redir_ga" until |
| 1879 | // redirection ends. |
| 1880 | init_redir_ga(); |
| 1881 | redir_vname = 1; |
| 1882 | break; |
| 1883 | |
| 1884 | case ISN_REDIREND: |
| 1885 | { |
| 1886 | char_u *res = get_clear_redir_ga(); |
| 1887 | |
| 1888 | // End redirection, put redirected text on the stack. |
| 1889 | clear_redir_lval(); |
| 1890 | redir_vname = 0; |
| 1891 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1892 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 1893 | { |
| 1894 | vim_free(res); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1895 | goto theend; |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 1896 | } |
| 1897 | tv = STACK_TV_BOT(0); |
| 1898 | tv->v_type = VAR_STRING; |
| 1899 | tv->vval.v_string = res; |
| 1900 | ++ectx->ec_stack.ga_len; |
| 1901 | } |
| 1902 | break; |
| 1903 | |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 1904 | case ISN_CEXPR_AUCMD: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 1905 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 1906 | if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL) |
| 1907 | goto on_error; |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 1908 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 1909 | break; |
| 1910 | |
| 1911 | case ISN_CEXPR_CORE: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 1912 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 1913 | { |
| 1914 | exarg_T ea; |
| 1915 | int res; |
| 1916 | |
| 1917 | CLEAR_FIELD(ea); |
| 1918 | ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx; |
| 1919 | ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit; |
| 1920 | ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline; |
| 1921 | --ectx->ec_stack.ga_len; |
| 1922 | tv = STACK_TV_BOT(0); |
| 1923 | res = cexpr_core(&ea, tv); |
| 1924 | clear_tv(tv); |
| 1925 | if (res == FAIL) |
| 1926 | goto on_error; |
| 1927 | } |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 1928 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 1929 | break; |
| 1930 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1931 | // execute Ex command from pieces on the stack |
| 1932 | case ISN_EXECCONCAT: |
| 1933 | { |
| 1934 | int count = iptr->isn_arg.number; |
Bram Moolenaar | 7f6f56f | 2020-04-30 20:21:43 +0200 | [diff] [blame] | 1935 | size_t len = 0; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1936 | int pass; |
| 1937 | int i; |
| 1938 | char_u *cmd = NULL; |
| 1939 | char_u *str; |
| 1940 | |
| 1941 | for (pass = 1; pass <= 2; ++pass) |
| 1942 | { |
| 1943 | for (i = 0; i < count; ++i) |
| 1944 | { |
| 1945 | tv = STACK_TV_BOT(i - count); |
| 1946 | str = tv->vval.v_string; |
| 1947 | if (str != NULL && *str != NUL) |
| 1948 | { |
| 1949 | if (pass == 2) |
| 1950 | STRCPY(cmd + len, str); |
| 1951 | len += STRLEN(str); |
| 1952 | } |
| 1953 | if (pass == 2) |
| 1954 | clear_tv(tv); |
| 1955 | } |
| 1956 | if (pass == 1) |
| 1957 | { |
| 1958 | cmd = alloc(len + 1); |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 1959 | if (unlikely(cmd == NULL)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1960 | goto theend; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1961 | len = 0; |
| 1962 | } |
| 1963 | } |
| 1964 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 1965 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1966 | do_cmdline_cmd(cmd); |
| 1967 | vim_free(cmd); |
| 1968 | } |
| 1969 | break; |
| 1970 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1971 | // execute :echo {string} ... |
| 1972 | case ISN_ECHO: |
| 1973 | { |
| 1974 | int count = iptr->isn_arg.echo.echo_count; |
| 1975 | int atstart = TRUE; |
| 1976 | int needclr = TRUE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1977 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1978 | |
| 1979 | for (idx = 0; idx < count; ++idx) |
| 1980 | { |
| 1981 | tv = STACK_TV_BOT(idx - count); |
| 1982 | echo_one(tv, iptr->isn_arg.echo.echo_with_white, |
| 1983 | &atstart, &needclr); |
| 1984 | clear_tv(tv); |
| 1985 | } |
Bram Moolenaar | e0807ea | 2020-02-20 22:18:06 +0100 | [diff] [blame] | 1986 | if (needclr) |
| 1987 | msg_clr_eos(); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1988 | ectx->ec_stack.ga_len -= count; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1989 | } |
| 1990 | break; |
| 1991 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1992 | // :execute {string} ... |
| 1993 | // :echomsg {string} ... |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 1994 | // :echoconsole {string} ... |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1995 | // :echoerr {string} ... |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1996 | case ISN_EXECUTE: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1997 | case ISN_ECHOMSG: |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 1998 | case ISN_ECHOCONSOLE: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1999 | case ISN_ECHOERR: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2000 | { |
| 2001 | int count = iptr->isn_arg.number; |
| 2002 | garray_T ga; |
| 2003 | char_u buf[NUMBUFLEN]; |
| 2004 | char_u *p; |
| 2005 | int len; |
| 2006 | int failed = FALSE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2007 | int idx; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2008 | |
| 2009 | ga_init2(&ga, 1, 80); |
| 2010 | for (idx = 0; idx < count; ++idx) |
| 2011 | { |
| 2012 | tv = STACK_TV_BOT(idx - count); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2013 | if (iptr->isn_type == ISN_EXECUTE) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2014 | { |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2015 | if (tv->v_type == VAR_CHANNEL |
| 2016 | || tv->v_type == VAR_JOB) |
| 2017 | { |
| 2018 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 68db996 | 2021-05-09 23:19:22 +0200 | [diff] [blame] | 2019 | semsg(_(e_using_invalid_value_as_string_str), |
| 2020 | vartype_name(tv->v_type)); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2021 | break; |
| 2022 | } |
| 2023 | else |
| 2024 | p = tv_get_string_buf(tv, buf); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2025 | } |
| 2026 | else |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2027 | p = tv_stringify(tv, buf); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2028 | |
| 2029 | len = (int)STRLEN(p); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2030 | if (GA_GROW_FAILS(&ga, len + 2)) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2031 | failed = TRUE; |
| 2032 | else |
| 2033 | { |
| 2034 | if (ga.ga_len > 0) |
| 2035 | ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; |
| 2036 | STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); |
| 2037 | ga.ga_len += len; |
| 2038 | } |
| 2039 | clear_tv(tv); |
| 2040 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2041 | ectx->ec_stack.ga_len -= count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2042 | if (failed) |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 2043 | { |
| 2044 | ga_clear(&ga); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2045 | goto on_error; |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 2046 | } |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2047 | |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2048 | if (ga.ga_data != NULL) |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2049 | { |
| 2050 | if (iptr->isn_type == ISN_EXECUTE) |
Bram Moolenaar | 430deb1 | 2020-08-23 16:29:11 +0200 | [diff] [blame] | 2051 | { |
| 2052 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2053 | do_cmdline_cmd((char_u *)ga.ga_data); |
Bram Moolenaar | eeece9e | 2020-11-20 19:26:48 +0100 | [diff] [blame] | 2054 | if (did_emsg) |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 2055 | { |
| 2056 | ga_clear(&ga); |
Bram Moolenaar | eeece9e | 2020-11-20 19:26:48 +0100 | [diff] [blame] | 2057 | goto on_error; |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 2058 | } |
Bram Moolenaar | 430deb1 | 2020-08-23 16:29:11 +0200 | [diff] [blame] | 2059 | } |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2060 | else |
| 2061 | { |
| 2062 | msg_sb_eol(); |
| 2063 | if (iptr->isn_type == ISN_ECHOMSG) |
| 2064 | { |
| 2065 | msg_attr(ga.ga_data, echo_attr); |
| 2066 | out_flush(); |
| 2067 | } |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 2068 | else if (iptr->isn_type == ISN_ECHOCONSOLE) |
| 2069 | { |
| 2070 | ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), |
| 2071 | TRUE); |
| 2072 | ui_write((char_u *)"\r\n", 2, TRUE); |
| 2073 | } |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2074 | else |
| 2075 | { |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2076 | SOURCING_LNUM = iptr->isn_lnum; |
| 2077 | emsg(ga.ga_data); |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2078 | } |
| 2079 | } |
| 2080 | } |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2081 | ga_clear(&ga); |
| 2082 | } |
| 2083 | break; |
| 2084 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2085 | // load local variable or argument |
| 2086 | case ISN_LOAD: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2087 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2088 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2089 | 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] | 2090 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2091 | break; |
| 2092 | |
| 2093 | // load v: variable |
| 2094 | case ISN_LOADV: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2095 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2096 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2097 | 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] | 2098 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2099 | break; |
| 2100 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2101 | // load s: variable in Vim9 script |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2102 | case ISN_LOADSCRIPT: |
| 2103 | { |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 2104 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2105 | svar_T *sv; |
| 2106 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 2107 | sv = get_script_svar(sref, ectx->ec_dfunc_idx); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 2108 | if (sv == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2109 | goto theend; |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 2110 | allocate_if_null(sv->sv_tv); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2111 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2112 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2113 | copy_tv(sv->sv_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2114 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2115 | } |
| 2116 | break; |
| 2117 | |
| 2118 | // load s: variable in old script |
| 2119 | case ISN_LOADS: |
| 2120 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2121 | hashtab_T *ht = &SCRIPT_VARS( |
| 2122 | iptr->isn_arg.loadstore.ls_sid); |
| 2123 | char_u *name = iptr->isn_arg.loadstore.ls_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2124 | dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2125 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2126 | if (di == NULL) |
| 2127 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 2128 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2129 | semsg(_(e_undefined_variable_str), name); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2130 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2131 | } |
| 2132 | else |
| 2133 | { |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2134 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2135 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2136 | copy_tv(&di->di_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2137 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2138 | } |
| 2139 | } |
| 2140 | break; |
| 2141 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2142 | // load g:/b:/w:/t: variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2143 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2144 | case ISN_LOADB: |
| 2145 | case ISN_LOADW: |
| 2146 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2147 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2148 | dictitem_T *di = NULL; |
| 2149 | hashtab_T *ht = NULL; |
| 2150 | char namespace; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2151 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2152 | switch (iptr->isn_type) |
| 2153 | { |
| 2154 | case ISN_LOADG: |
| 2155 | ht = get_globvar_ht(); |
| 2156 | namespace = 'g'; |
| 2157 | break; |
| 2158 | case ISN_LOADB: |
| 2159 | ht = &curbuf->b_vars->dv_hashtab; |
| 2160 | namespace = 'b'; |
| 2161 | break; |
| 2162 | case ISN_LOADW: |
| 2163 | ht = &curwin->w_vars->dv_hashtab; |
| 2164 | namespace = 'w'; |
| 2165 | break; |
| 2166 | case ISN_LOADT: |
| 2167 | ht = &curtab->tp_vars->dv_hashtab; |
| 2168 | namespace = 't'; |
| 2169 | break; |
| 2170 | default: // Cannot reach here |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2171 | goto theend; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2172 | } |
| 2173 | di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2174 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2175 | if (di == NULL) |
| 2176 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 2177 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2178 | semsg(_(e_undefined_variable_char_str), |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2179 | namespace, iptr->isn_arg.string); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2180 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2181 | } |
| 2182 | else |
| 2183 | { |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2184 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2185 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2186 | copy_tv(&di->di_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2187 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | break; |
| 2191 | |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2192 | // load autoload variable |
| 2193 | case ISN_LOADAUTO: |
| 2194 | { |
| 2195 | char_u *name = iptr->isn_arg.string; |
| 2196 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2197 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2198 | goto theend; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2199 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 38a434f | 2021-01-02 12:45:45 +0100 | [diff] [blame] | 2200 | if (eval_variable(name, (int)STRLEN(name), |
Bram Moolenaar | cb4e80f | 2021-03-13 20:57:19 +0100 | [diff] [blame] | 2201 | STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL) |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2202 | goto on_error; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2203 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2204 | } |
| 2205 | break; |
| 2206 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2207 | // load g:/b:/w:/t: namespace |
| 2208 | case ISN_LOADGDICT: |
| 2209 | case ISN_LOADBDICT: |
| 2210 | case ISN_LOADWDICT: |
| 2211 | case ISN_LOADTDICT: |
| 2212 | { |
| 2213 | dict_T *d = NULL; |
| 2214 | |
| 2215 | switch (iptr->isn_type) |
| 2216 | { |
Bram Moolenaar | 682d0a1 | 2020-07-19 20:48:59 +0200 | [diff] [blame] | 2217 | case ISN_LOADGDICT: d = get_globvar_dict(); break; |
| 2218 | case ISN_LOADBDICT: d = curbuf->b_vars; break; |
| 2219 | case ISN_LOADWDICT: d = curwin->w_vars; break; |
| 2220 | case ISN_LOADTDICT: d = curtab->tp_vars; break; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2221 | default: // Cannot reach here |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2222 | goto theend; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2223 | } |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2224 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2225 | goto theend; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2226 | tv = STACK_TV_BOT(0); |
| 2227 | tv->v_type = VAR_DICT; |
| 2228 | tv->v_lock = 0; |
| 2229 | tv->vval.v_dict = d; |
Bram Moolenaar | 1bd3cb2 | 2021-02-24 12:27:31 +0100 | [diff] [blame] | 2230 | ++d->dv_refcount; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2231 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2232 | } |
| 2233 | break; |
| 2234 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2235 | // load &option |
| 2236 | case ISN_LOADOPT: |
| 2237 | { |
| 2238 | typval_T optval; |
| 2239 | char_u *name = iptr->isn_arg.string; |
| 2240 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 2241 | // This is not expected to fail, name is checked during |
| 2242 | // compilation: don't set SOURCING_LNUM. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2243 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2244 | goto theend; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2245 | if (eval_option(&name, &optval, TRUE) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2246 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2247 | *STACK_TV_BOT(0) = optval; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2248 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2249 | } |
| 2250 | break; |
| 2251 | |
| 2252 | // load $ENV |
| 2253 | case ISN_LOADENV: |
| 2254 | { |
| 2255 | typval_T optval; |
| 2256 | char_u *name = iptr->isn_arg.string; |
| 2257 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2258 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2259 | goto theend; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2260 | // name is always valid, checked when compiling |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2261 | (void)eval_env_var(&name, &optval, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2262 | *STACK_TV_BOT(0) = optval; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2263 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2264 | } |
| 2265 | break; |
| 2266 | |
| 2267 | // load @register |
| 2268 | case ISN_LOADREG: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2269 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2270 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2271 | tv = STACK_TV_BOT(0); |
| 2272 | tv->v_type = VAR_STRING; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2273 | tv->v_lock = 0; |
Bram Moolenaar | 1e021e6 | 2020-10-16 20:25:23 +0200 | [diff] [blame] | 2274 | // This may result in NULL, which should be equivalent to an |
| 2275 | // empty string. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2276 | tv->vval.v_string = get_reg_contents( |
| 2277 | iptr->isn_arg.number, GREG_EXPR_SRC); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2278 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2279 | break; |
| 2280 | |
| 2281 | // store local variable |
| 2282 | case ISN_STORE: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2283 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2284 | tv = STACK_TV_VAR(iptr->isn_arg.number); |
| 2285 | clear_tv(tv); |
| 2286 | *tv = *STACK_TV_BOT(0); |
| 2287 | break; |
| 2288 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2289 | // store s: variable in old script |
| 2290 | case ISN_STORES: |
| 2291 | { |
| 2292 | hashtab_T *ht = &SCRIPT_VARS( |
| 2293 | iptr->isn_arg.loadstore.ls_sid); |
| 2294 | char_u *name = iptr->isn_arg.loadstore.ls_name; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2295 | dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2296 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2297 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2298 | if (di == NULL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2299 | store_var(name, STACK_TV_BOT(0)); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2300 | else |
| 2301 | { |
Bram Moolenaar | dcf29ac | 2021-04-02 14:44:02 +0200 | [diff] [blame] | 2302 | SOURCING_LNUM = iptr->isn_lnum; |
| 2303 | if (var_check_permission(di, name) == FAIL) |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 2304 | { |
| 2305 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | dcf29ac | 2021-04-02 14:44:02 +0200 | [diff] [blame] | 2306 | goto on_error; |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 2307 | } |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2308 | clear_tv(&di->di_tv); |
| 2309 | di->di_tv = *STACK_TV_BOT(0); |
| 2310 | } |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2311 | } |
| 2312 | break; |
| 2313 | |
| 2314 | // store script-local variable in Vim9 script |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2315 | case ISN_STORESCRIPT: |
| 2316 | { |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 2317 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 2318 | svar_T *sv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2319 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 2320 | sv = get_script_svar(sref, ectx->ec_dfunc_idx); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 2321 | if (sv == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2322 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2323 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 2324 | |
| 2325 | // "const" and "final" are checked at compile time, locking |
| 2326 | // the value needs to be checked here. |
| 2327 | SOURCING_LNUM = iptr->isn_lnum; |
| 2328 | 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] | 2329 | { |
| 2330 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 2331 | goto on_error; |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 2332 | } |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 2333 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2334 | clear_tv(sv->sv_tv); |
| 2335 | *sv->sv_tv = *STACK_TV_BOT(0); |
| 2336 | } |
| 2337 | break; |
| 2338 | |
| 2339 | // store option |
| 2340 | case ISN_STOREOPT: |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 2341 | case ISN_STOREFUNCOPT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2342 | { |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 2343 | char_u *opt_name = iptr->isn_arg.storeopt.so_name; |
| 2344 | int opt_flags = iptr->isn_arg.storeopt.so_flags; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2345 | long n = 0; |
| 2346 | char_u *s = NULL; |
| 2347 | char *msg; |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 2348 | char_u numbuf[NUMBUFLEN]; |
| 2349 | char_u *tofree = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2350 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2351 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2352 | tv = STACK_TV_BOT(0); |
| 2353 | if (tv->v_type == VAR_STRING) |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 2354 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2355 | s = tv->vval.v_string; |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 2356 | if (s == NULL) |
| 2357 | s = (char_u *)""; |
| 2358 | } |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 2359 | else if (iptr->isn_type == ISN_STOREFUNCOPT) |
| 2360 | { |
| 2361 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 2362 | // If the option can be set to a function reference or |
| 2363 | // a lambda and the passed value is a function |
| 2364 | // reference, then convert it to the name (string) of |
| 2365 | // the function reference. |
| 2366 | s = tv2string(tv, &tofree, numbuf, 0); |
| 2367 | if (s == NULL || *s == NUL) |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 2368 | { |
| 2369 | clear_tv(tv); |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 2370 | goto on_error; |
| 2371 | } |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 2372 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2373 | else |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 2374 | // must be VAR_NUMBER, CHECKTYPE makes sure |
| 2375 | n = tv->vval.v_number; |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 2376 | msg = set_option_value(opt_name, n, s, opt_flags); |
Bram Moolenaar | e75ba26 | 2020-05-16 15:43:31 +0200 | [diff] [blame] | 2377 | clear_tv(tv); |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 2378 | vim_free(tofree); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2379 | if (msg != NULL) |
| 2380 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 2381 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2382 | emsg(_(msg)); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2383 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2384 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2385 | } |
| 2386 | break; |
| 2387 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2388 | // store $ENV |
| 2389 | case ISN_STOREENV: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2390 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2391 | tv = STACK_TV_BOT(0); |
| 2392 | vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); |
| 2393 | clear_tv(tv); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2394 | break; |
| 2395 | |
| 2396 | // store @r |
| 2397 | case ISN_STOREREG: |
| 2398 | { |
| 2399 | int reg = iptr->isn_arg.number; |
| 2400 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2401 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 2402 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 74f4a96 | 2021-06-17 21:03:07 +0200 | [diff] [blame] | 2403 | write_reg_contents(reg, tv_get_string(tv), -1, FALSE); |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 2404 | clear_tv(tv); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2405 | } |
| 2406 | break; |
| 2407 | |
| 2408 | // store v: variable |
| 2409 | case ISN_STOREV: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2410 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2411 | if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) |
| 2412 | == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2413 | // should not happen, type is checked when compiling |
| 2414 | goto on_error; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2415 | break; |
| 2416 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2417 | // store g:/b:/w:/t: variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2418 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2419 | case ISN_STOREB: |
| 2420 | case ISN_STOREW: |
| 2421 | case ISN_STORET: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2422 | { |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 2423 | dictitem_T *di; |
| 2424 | hashtab_T *ht; |
| 2425 | char_u *name = iptr->isn_arg.string + 2; |
| 2426 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2427 | switch (iptr->isn_type) |
| 2428 | { |
| 2429 | case ISN_STOREG: |
| 2430 | ht = get_globvar_ht(); |
| 2431 | break; |
| 2432 | case ISN_STOREB: |
| 2433 | ht = &curbuf->b_vars->dv_hashtab; |
| 2434 | break; |
| 2435 | case ISN_STOREW: |
| 2436 | ht = &curwin->w_vars->dv_hashtab; |
| 2437 | break; |
| 2438 | case ISN_STORET: |
| 2439 | ht = &curtab->tp_vars->dv_hashtab; |
| 2440 | break; |
| 2441 | default: // Cannot reach here |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2442 | goto theend; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2443 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2444 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2445 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 2446 | di = find_var_in_ht(ht, 0, name, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2447 | if (di == NULL) |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2448 | store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2449 | else |
| 2450 | { |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 2451 | SOURCING_LNUM = iptr->isn_lnum; |
| 2452 | if (var_check_permission(di, name) == FAIL) |
| 2453 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2454 | clear_tv(&di->di_tv); |
| 2455 | di->di_tv = *STACK_TV_BOT(0); |
| 2456 | } |
| 2457 | } |
| 2458 | break; |
| 2459 | |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2460 | // store an autoload variable |
| 2461 | case ISN_STOREAUTO: |
| 2462 | SOURCING_LNUM = iptr->isn_lnum; |
| 2463 | set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE); |
| 2464 | clear_tv(STACK_TV_BOT(-1)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2465 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2466 | break; |
| 2467 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2468 | // store number in local variable |
| 2469 | case ISN_STORENR: |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 2470 | tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2471 | clear_tv(tv); |
| 2472 | tv->v_type = VAR_NUMBER; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 2473 | tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2474 | break; |
| 2475 | |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2476 | // store value in list or dict variable |
| 2477 | case ISN_STOREINDEX: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2478 | { |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2479 | vartype_T dest_type = iptr->isn_arg.vartype; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2480 | typval_T *tv_idx = STACK_TV_BOT(-2); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2481 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2482 | int status = OK; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2483 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2484 | // Stack contains: |
| 2485 | // -3 value to be stored |
| 2486 | // -2 index |
| 2487 | // -1 dict or list |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2488 | tv = STACK_TV_BOT(-3); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2489 | SOURCING_LNUM = iptr->isn_lnum; |
| 2490 | if (dest_type == VAR_ANY) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2491 | { |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2492 | dest_type = tv_dest->v_type; |
| 2493 | if (dest_type == VAR_DICT) |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 2494 | status = do_2string(tv_idx, TRUE, FALSE); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2495 | else if (dest_type == VAR_LIST |
| 2496 | && tv_idx->v_type != VAR_NUMBER) |
| 2497 | { |
Bram Moolenaar | e29a27f | 2021-07-20 21:07:36 +0200 | [diff] [blame] | 2498 | emsg(_(e_number_expected)); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2499 | status = FAIL; |
| 2500 | } |
| 2501 | } |
| 2502 | else if (dest_type != tv_dest->v_type) |
| 2503 | { |
| 2504 | // just in case, should be OK |
| 2505 | semsg(_(e_expected_str_but_got_str), |
| 2506 | vartype_name(dest_type), |
| 2507 | vartype_name(tv_dest->v_type)); |
| 2508 | status = FAIL; |
| 2509 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2510 | |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2511 | if (status == OK && dest_type == VAR_LIST) |
| 2512 | { |
Bram Moolenaar | 239f8d9 | 2021-01-17 13:21:20 +0100 | [diff] [blame] | 2513 | long lidx = (long)tv_idx->vval.v_number; |
| 2514 | list_T *list = tv_dest->vval.v_list; |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2515 | |
| 2516 | if (list == NULL) |
| 2517 | { |
| 2518 | emsg(_(e_list_not_set)); |
| 2519 | goto on_error; |
| 2520 | } |
| 2521 | if (lidx < 0 && list->lv_len + lidx >= 0) |
| 2522 | // negative index is relative to the end |
| 2523 | lidx = list->lv_len + lidx; |
| 2524 | if (lidx < 0 || lidx > list->lv_len) |
| 2525 | { |
| 2526 | semsg(_(e_listidx), lidx); |
| 2527 | goto on_error; |
| 2528 | } |
| 2529 | if (lidx < list->lv_len) |
| 2530 | { |
| 2531 | listitem_T *li = list_find(list, lidx); |
| 2532 | |
| 2533 | if (error_if_locked(li->li_tv.v_lock, |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 2534 | e_cannot_change_list_item)) |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2535 | goto on_error; |
| 2536 | // overwrite existing list item |
| 2537 | clear_tv(&li->li_tv); |
| 2538 | li->li_tv = *tv; |
| 2539 | } |
| 2540 | else |
| 2541 | { |
| 2542 | if (error_if_locked(list->lv_lock, |
| 2543 | e_cannot_change_list)) |
| 2544 | goto on_error; |
| 2545 | // append to list, only fails when out of memory |
| 2546 | if (list_append_tv(list, tv) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2547 | goto theend; |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2548 | clear_tv(tv); |
| 2549 | } |
| 2550 | } |
| 2551 | else if (status == OK && dest_type == VAR_DICT) |
| 2552 | { |
| 2553 | char_u *key = tv_idx->vval.v_string; |
| 2554 | dict_T *dict = tv_dest->vval.v_dict; |
| 2555 | dictitem_T *di; |
| 2556 | |
| 2557 | SOURCING_LNUM = iptr->isn_lnum; |
| 2558 | if (dict == NULL) |
| 2559 | { |
| 2560 | emsg(_(e_dictionary_not_set)); |
| 2561 | goto on_error; |
| 2562 | } |
| 2563 | if (key == NULL) |
| 2564 | key = (char_u *)""; |
| 2565 | di = dict_find(dict, key, -1); |
| 2566 | if (di != NULL) |
| 2567 | { |
| 2568 | if (error_if_locked(di->di_tv.v_lock, |
| 2569 | e_cannot_change_dict_item)) |
| 2570 | goto on_error; |
| 2571 | // overwrite existing value |
| 2572 | clear_tv(&di->di_tv); |
| 2573 | di->di_tv = *tv; |
| 2574 | } |
| 2575 | else |
| 2576 | { |
| 2577 | if (error_if_locked(dict->dv_lock, |
| 2578 | e_cannot_change_dict)) |
| 2579 | goto on_error; |
| 2580 | // add to dict, only fails when out of memory |
| 2581 | if (dict_add_tv(dict, (char *)key, tv) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2582 | goto theend; |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2583 | clear_tv(tv); |
| 2584 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2585 | } |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2586 | else if (status == OK && dest_type == VAR_BLOB) |
| 2587 | { |
Bram Moolenaar | 51e9332 | 2021-04-17 20:44:56 +0200 | [diff] [blame] | 2588 | long lidx = (long)tv_idx->vval.v_number; |
| 2589 | blob_T *blob = tv_dest->vval.v_blob; |
| 2590 | varnumber_T nr; |
| 2591 | int error = FALSE; |
| 2592 | int len; |
| 2593 | |
| 2594 | if (blob == NULL) |
| 2595 | { |
| 2596 | emsg(_(e_blob_not_set)); |
| 2597 | goto on_error; |
| 2598 | } |
| 2599 | len = blob_len(blob); |
| 2600 | if (lidx < 0 && len + lidx >= 0) |
| 2601 | // negative index is relative to the end |
| 2602 | lidx = len + lidx; |
| 2603 | |
| 2604 | // Can add one byte at the end. |
| 2605 | if (lidx < 0 || lidx > len) |
| 2606 | { |
| 2607 | semsg(_(e_blobidx), lidx); |
| 2608 | goto on_error; |
| 2609 | } |
| 2610 | if (value_check_lock(blob->bv_lock, |
| 2611 | (char_u *)"blob", FALSE)) |
| 2612 | goto on_error; |
| 2613 | nr = tv_get_number_chk(tv, &error); |
| 2614 | if (error) |
| 2615 | goto on_error; |
| 2616 | blob_set_append(blob, lidx, nr); |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2617 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2618 | else |
| 2619 | { |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2620 | status = FAIL; |
| 2621 | semsg(_(e_cannot_index_str), vartype_name(dest_type)); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2622 | } |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2623 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2624 | clear_tv(tv_idx); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2625 | clear_tv(tv_dest); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2626 | ectx->ec_stack.ga_len -= 3; |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2627 | if (status == FAIL) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 2628 | { |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2629 | clear_tv(tv); |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 2630 | goto on_error; |
| 2631 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2632 | } |
| 2633 | break; |
| 2634 | |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2635 | // store value in blob range |
| 2636 | case ISN_STORERANGE: |
| 2637 | { |
| 2638 | typval_T *tv_idx1 = STACK_TV_BOT(-3); |
| 2639 | typval_T *tv_idx2 = STACK_TV_BOT(-2); |
| 2640 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2641 | int status = OK; |
| 2642 | |
| 2643 | // Stack contains: |
| 2644 | // -4 value to be stored |
| 2645 | // -3 first index or "none" |
| 2646 | // -2 second index or "none" |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 2647 | // -1 destination list or blob |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2648 | tv = STACK_TV_BOT(-4); |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 2649 | if (tv_dest->v_type == VAR_LIST) |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2650 | { |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 2651 | long n1; |
| 2652 | long n2; |
| 2653 | int error = FALSE; |
| 2654 | |
| 2655 | SOURCING_LNUM = iptr->isn_lnum; |
| 2656 | n1 = (long)tv_get_number_chk(tv_idx1, &error); |
| 2657 | if (error) |
| 2658 | status = FAIL; |
| 2659 | else |
| 2660 | { |
| 2661 | if (tv_idx2->v_type == VAR_SPECIAL |
| 2662 | && tv_idx2->vval.v_number == VVAL_NONE) |
| 2663 | n2 = list_len(tv_dest->vval.v_list) - 1; |
| 2664 | else |
| 2665 | n2 = (long)tv_get_number_chk(tv_idx2, &error); |
| 2666 | if (error) |
| 2667 | status = FAIL; |
| 2668 | else |
| 2669 | { |
| 2670 | listitem_T *li1 = check_range_index_one( |
| 2671 | tv_dest->vval.v_list, &n1, FALSE); |
| 2672 | |
| 2673 | if (li1 == NULL) |
| 2674 | status = FAIL; |
| 2675 | else |
| 2676 | { |
| 2677 | status = check_range_index_two( |
| 2678 | tv_dest->vval.v_list, |
| 2679 | &n1, li1, &n2, FALSE); |
| 2680 | if (status != FAIL) |
| 2681 | status = list_assign_range( |
| 2682 | tv_dest->vval.v_list, |
| 2683 | tv->vval.v_list, |
| 2684 | n1, |
| 2685 | n2, |
| 2686 | tv_idx2->v_type == VAR_SPECIAL, |
| 2687 | (char_u *)"=", |
| 2688 | (char_u *)"[unknown]"); |
| 2689 | } |
| 2690 | } |
| 2691 | } |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2692 | } |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 2693 | else if (tv_dest->v_type == VAR_BLOB) |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2694 | { |
| 2695 | varnumber_T n1; |
| 2696 | varnumber_T n2; |
| 2697 | int error = FALSE; |
| 2698 | |
| 2699 | n1 = tv_get_number_chk(tv_idx1, &error); |
| 2700 | if (error) |
| 2701 | status = FAIL; |
| 2702 | else |
| 2703 | { |
| 2704 | if (tv_idx2->v_type == VAR_SPECIAL |
| 2705 | && tv_idx2->vval.v_number == VVAL_NONE) |
| 2706 | n2 = blob_len(tv_dest->vval.v_blob) - 1; |
| 2707 | else |
| 2708 | n2 = tv_get_number_chk(tv_idx2, &error); |
| 2709 | if (error) |
| 2710 | status = FAIL; |
| 2711 | else |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 2712 | { |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 2713 | long bloblen = blob_len(tv_dest->vval.v_blob); |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 2714 | |
| 2715 | if (check_blob_index(bloblen, |
Bram Moolenaar | bd6406f | 2021-04-14 21:30:06 +0200 | [diff] [blame] | 2716 | n1, FALSE) == FAIL |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 2717 | || check_blob_range(bloblen, |
| 2718 | n1, n2, FALSE) == FAIL) |
| 2719 | status = FAIL; |
| 2720 | else |
| 2721 | status = blob_set_range( |
| 2722 | tv_dest->vval.v_blob, n1, n2, tv); |
| 2723 | } |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2724 | } |
| 2725 | } |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 2726 | else |
| 2727 | { |
| 2728 | status = FAIL; |
| 2729 | emsg(_(e_blob_required)); |
| 2730 | } |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2731 | |
| 2732 | clear_tv(tv_idx1); |
| 2733 | clear_tv(tv_idx2); |
| 2734 | clear_tv(tv_dest); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2735 | ectx->ec_stack.ga_len -= 4; |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2736 | clear_tv(tv); |
| 2737 | |
| 2738 | if (status == FAIL) |
| 2739 | goto on_error; |
| 2740 | } |
| 2741 | break; |
| 2742 | |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2743 | // load or store variable or argument from outer scope |
| 2744 | case ISN_LOADOUTER: |
| 2745 | case ISN_STOREOUTER: |
| 2746 | { |
| 2747 | int depth = iptr->isn_arg.outer.outer_depth; |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 2748 | outer_T *outer = ectx->ec_outer_ref == NULL ? NULL |
| 2749 | : ectx->ec_outer_ref->or_outer; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2750 | |
| 2751 | while (depth > 1 && outer != NULL) |
| 2752 | { |
| 2753 | outer = outer->out_up; |
| 2754 | --depth; |
| 2755 | } |
| 2756 | if (outer == NULL) |
| 2757 | { |
| 2758 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 69c7617 | 2021-12-02 16:38:52 +0000 | [diff] [blame] | 2759 | if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx |
| 2760 | || ectx->ec_outer_ref == NULL) |
| 2761 | // Possibly :def function called from legacy |
| 2762 | // context. |
| 2763 | emsg(_(e_closure_called_from_invalid_context)); |
| 2764 | else |
| 2765 | iemsg("LOADOUTER depth more than scope levels"); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2766 | goto theend; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2767 | } |
| 2768 | tv = ((typval_T *)outer->out_stack->ga_data) |
| 2769 | + outer->out_frame_idx + STACK_FRAME_SIZE |
| 2770 | + iptr->isn_arg.outer.outer_idx; |
| 2771 | if (iptr->isn_type == ISN_LOADOUTER) |
| 2772 | { |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2773 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2774 | goto theend; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2775 | copy_tv(tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2776 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2777 | } |
| 2778 | else |
| 2779 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2780 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2781 | clear_tv(tv); |
| 2782 | *tv = *STACK_TV_BOT(0); |
| 2783 | } |
| 2784 | } |
| 2785 | break; |
| 2786 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2787 | // unlet item in list or dict variable |
| 2788 | case ISN_UNLETINDEX: |
| 2789 | { |
| 2790 | typval_T *tv_idx = STACK_TV_BOT(-2); |
| 2791 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2792 | int status = OK; |
| 2793 | |
| 2794 | // Stack contains: |
| 2795 | // -2 index |
| 2796 | // -1 dict or list |
| 2797 | if (tv_dest->v_type == VAR_DICT) |
| 2798 | { |
| 2799 | // unlet a dict item, index must be a string |
| 2800 | if (tv_idx->v_type != VAR_STRING) |
| 2801 | { |
Bram Moolenaar | 0acbf5a | 2021-01-05 20:58:25 +0100 | [diff] [blame] | 2802 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2803 | semsg(_(e_expected_str_but_got_str), |
| 2804 | vartype_name(VAR_STRING), |
| 2805 | vartype_name(tv_idx->v_type)); |
| 2806 | status = FAIL; |
| 2807 | } |
| 2808 | else |
| 2809 | { |
| 2810 | dict_T *d = tv_dest->vval.v_dict; |
| 2811 | char_u *key = tv_idx->vval.v_string; |
| 2812 | dictitem_T *di = NULL; |
| 2813 | |
| 2814 | if (key == NULL) |
| 2815 | key = (char_u *)""; |
| 2816 | if (d != NULL) |
| 2817 | di = dict_find(d, key, (int)STRLEN(key)); |
| 2818 | if (di == NULL) |
| 2819 | { |
| 2820 | // NULL dict is equivalent to empty dict |
Bram Moolenaar | 0acbf5a | 2021-01-05 20:58:25 +0100 | [diff] [blame] | 2821 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2822 | semsg(_(e_dictkey), key); |
| 2823 | status = FAIL; |
| 2824 | } |
| 2825 | else |
| 2826 | { |
| 2827 | // TODO: check for dict or item locked |
| 2828 | dictitem_remove(d, di); |
| 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | else if (tv_dest->v_type == VAR_LIST) |
| 2833 | { |
| 2834 | // unlet a List item, index must be a number |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 2835 | SOURCING_LNUM = iptr->isn_lnum; |
| 2836 | if (check_for_number(tv_idx) == FAIL) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2837 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2838 | status = FAIL; |
| 2839 | } |
| 2840 | else |
| 2841 | { |
| 2842 | list_T *l = tv_dest->vval.v_list; |
Bram Moolenaar | 239f8d9 | 2021-01-17 13:21:20 +0100 | [diff] [blame] | 2843 | long n = (long)tv_idx->vval.v_number; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2844 | listitem_T *li = NULL; |
| 2845 | |
| 2846 | li = list_find(l, n); |
| 2847 | if (li == NULL) |
| 2848 | { |
Bram Moolenaar | 0acbf5a | 2021-01-05 20:58:25 +0100 | [diff] [blame] | 2849 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2850 | semsg(_(e_listidx), n); |
| 2851 | status = FAIL; |
| 2852 | } |
| 2853 | else |
| 2854 | // TODO: check for list or item locked |
| 2855 | listitem_remove(l, li); |
| 2856 | } |
| 2857 | } |
| 2858 | else |
| 2859 | { |
| 2860 | status = FAIL; |
| 2861 | semsg(_(e_cannot_index_str), |
| 2862 | vartype_name(tv_dest->v_type)); |
| 2863 | } |
| 2864 | |
| 2865 | clear_tv(tv_idx); |
| 2866 | clear_tv(tv_dest); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2867 | ectx->ec_stack.ga_len -= 2; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2868 | if (status == FAIL) |
| 2869 | goto on_error; |
| 2870 | } |
| 2871 | break; |
| 2872 | |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 2873 | // unlet range of items in list variable |
| 2874 | case ISN_UNLETRANGE: |
| 2875 | { |
| 2876 | // Stack contains: |
| 2877 | // -3 index1 |
| 2878 | // -2 index2 |
| 2879 | // -1 dict or list |
| 2880 | typval_T *tv_idx1 = STACK_TV_BOT(-3); |
| 2881 | typval_T *tv_idx2 = STACK_TV_BOT(-2); |
| 2882 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2883 | int status = OK; |
| 2884 | |
| 2885 | if (tv_dest->v_type == VAR_LIST) |
| 2886 | { |
| 2887 | // indexes must be a number |
| 2888 | SOURCING_LNUM = iptr->isn_lnum; |
| 2889 | if (check_for_number(tv_idx1) == FAIL |
Bram Moolenaar | 63cb656 | 2021-07-20 22:21:59 +0200 | [diff] [blame] | 2890 | || (tv_idx2->v_type != VAR_SPECIAL |
| 2891 | && check_for_number(tv_idx2) == FAIL)) |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 2892 | { |
| 2893 | status = FAIL; |
| 2894 | } |
| 2895 | else |
| 2896 | { |
| 2897 | list_T *l = tv_dest->vval.v_list; |
| 2898 | long n1 = (long)tv_idx1->vval.v_number; |
Bram Moolenaar | 63cb656 | 2021-07-20 22:21:59 +0200 | [diff] [blame] | 2899 | long n2 = tv_idx2->v_type == VAR_SPECIAL |
| 2900 | ? 0 : (long)tv_idx2->vval.v_number; |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 2901 | listitem_T *li; |
| 2902 | |
| 2903 | li = list_find_index(l, &n1); |
Bram Moolenaar | 63cb656 | 2021-07-20 22:21:59 +0200 | [diff] [blame] | 2904 | if (li == NULL) |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 2905 | status = FAIL; |
Bram Moolenaar | 63cb656 | 2021-07-20 22:21:59 +0200 | [diff] [blame] | 2906 | else |
| 2907 | { |
| 2908 | if (n1 < 0) |
| 2909 | n1 = list_idx_of_item(l, li); |
| 2910 | if (n2 < 0) |
| 2911 | { |
| 2912 | listitem_T *li2 = list_find(l, n2); |
| 2913 | |
| 2914 | if (li2 == NULL) |
| 2915 | status = FAIL; |
| 2916 | else |
| 2917 | n2 = list_idx_of_item(l, li2); |
| 2918 | } |
| 2919 | if (status != FAIL |
Bram Moolenaar | 5dd839c | 2021-07-22 18:48:53 +0200 | [diff] [blame] | 2920 | && tv_idx2->v_type != VAR_SPECIAL |
| 2921 | && n2 < n1) |
| 2922 | { |
| 2923 | semsg(_(e_listidx), n2); |
| 2924 | status = FAIL; |
| 2925 | } |
| 2926 | if (status != FAIL |
Bram Moolenaar | 63cb656 | 2021-07-20 22:21:59 +0200 | [diff] [blame] | 2927 | && list_unlet_range(l, li, NULL, n1, |
| 2928 | tv_idx2->v_type != VAR_SPECIAL, n2) |
| 2929 | == FAIL) |
| 2930 | status = FAIL; |
| 2931 | } |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 2932 | } |
| 2933 | } |
| 2934 | else |
| 2935 | { |
| 2936 | status = FAIL; |
| 2937 | SOURCING_LNUM = iptr->isn_lnum; |
| 2938 | semsg(_(e_cannot_index_str), |
| 2939 | vartype_name(tv_dest->v_type)); |
| 2940 | } |
| 2941 | |
| 2942 | clear_tv(tv_idx1); |
| 2943 | clear_tv(tv_idx2); |
| 2944 | clear_tv(tv_dest); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2945 | ectx->ec_stack.ga_len -= 3; |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 2946 | if (status == FAIL) |
| 2947 | goto on_error; |
| 2948 | } |
| 2949 | break; |
| 2950 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2951 | // push constant |
| 2952 | case ISN_PUSHNR: |
| 2953 | case ISN_PUSHBOOL: |
| 2954 | case ISN_PUSHSPEC: |
| 2955 | case ISN_PUSHF: |
| 2956 | case ISN_PUSHS: |
| 2957 | case ISN_PUSHBLOB: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2958 | case ISN_PUSHFUNC: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2959 | case ISN_PUSHCHANNEL: |
| 2960 | case ISN_PUSHJOB: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2961 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2962 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2963 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2964 | tv->v_lock = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2965 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2966 | switch (iptr->isn_type) |
| 2967 | { |
| 2968 | case ISN_PUSHNR: |
| 2969 | tv->v_type = VAR_NUMBER; |
| 2970 | tv->vval.v_number = iptr->isn_arg.number; |
| 2971 | break; |
| 2972 | case ISN_PUSHBOOL: |
| 2973 | tv->v_type = VAR_BOOL; |
| 2974 | tv->vval.v_number = iptr->isn_arg.number; |
| 2975 | break; |
| 2976 | case ISN_PUSHSPEC: |
| 2977 | tv->v_type = VAR_SPECIAL; |
| 2978 | tv->vval.v_number = iptr->isn_arg.number; |
| 2979 | break; |
| 2980 | #ifdef FEAT_FLOAT |
| 2981 | case ISN_PUSHF: |
| 2982 | tv->v_type = VAR_FLOAT; |
| 2983 | tv->vval.v_float = iptr->isn_arg.fnumber; |
| 2984 | break; |
| 2985 | #endif |
| 2986 | case ISN_PUSHBLOB: |
| 2987 | blob_copy(iptr->isn_arg.blob, tv); |
| 2988 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2989 | case ISN_PUSHFUNC: |
| 2990 | tv->v_type = VAR_FUNC; |
Bram Moolenaar | 087d2e1 | 2020-03-01 15:36:42 +0100 | [diff] [blame] | 2991 | if (iptr->isn_arg.string == NULL) |
| 2992 | tv->vval.v_string = NULL; |
| 2993 | else |
| 2994 | tv->vval.v_string = |
| 2995 | vim_strsave(iptr->isn_arg.string); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2996 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2997 | case ISN_PUSHCHANNEL: |
| 2998 | #ifdef FEAT_JOB_CHANNEL |
| 2999 | tv->v_type = VAR_CHANNEL; |
| 3000 | tv->vval.v_channel = iptr->isn_arg.channel; |
| 3001 | if (tv->vval.v_channel != NULL) |
| 3002 | ++tv->vval.v_channel->ch_refcount; |
| 3003 | #endif |
| 3004 | break; |
| 3005 | case ISN_PUSHJOB: |
| 3006 | #ifdef FEAT_JOB_CHANNEL |
| 3007 | tv->v_type = VAR_JOB; |
| 3008 | tv->vval.v_job = iptr->isn_arg.job; |
| 3009 | if (tv->vval.v_job != NULL) |
| 3010 | ++tv->vval.v_job->jv_refcount; |
| 3011 | #endif |
| 3012 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3013 | default: |
| 3014 | tv->v_type = VAR_STRING; |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 3015 | tv->vval.v_string = vim_strsave( |
| 3016 | iptr->isn_arg.string == NULL |
| 3017 | ? (char_u *)"" : iptr->isn_arg.string); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3018 | } |
| 3019 | break; |
| 3020 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 3021 | case ISN_UNLET: |
| 3022 | if (do_unlet(iptr->isn_arg.unlet.ul_name, |
| 3023 | iptr->isn_arg.unlet.ul_forceit) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3024 | goto on_error; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 3025 | break; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 3026 | case ISN_UNLETENV: |
| 3027 | vim_unsetenv(iptr->isn_arg.unlet.ul_name); |
| 3028 | break; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 3029 | |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 3030 | case ISN_LOCKUNLOCK: |
| 3031 | { |
| 3032 | typval_T *lval_root_save = lval_root; |
| 3033 | int res; |
| 3034 | |
| 3035 | // Stack has the local variable, argument the whole :lock |
| 3036 | // or :unlock command, like ISN_EXEC. |
| 3037 | --ectx->ec_stack.ga_len; |
| 3038 | lval_root = STACK_TV_BOT(0); |
| 3039 | res = exec_command(iptr); |
| 3040 | clear_tv(lval_root); |
| 3041 | lval_root = lval_root_save; |
| 3042 | if (res == FAIL) |
| 3043 | goto on_error; |
| 3044 | } |
| 3045 | break; |
| 3046 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 3047 | case ISN_LOCKCONST: |
| 3048 | item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE); |
| 3049 | break; |
| 3050 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3051 | // create a list from items on the stack; uses a single allocation |
| 3052 | // for the list header and the items |
| 3053 | case ISN_NEWLIST: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3054 | if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3055 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3056 | break; |
| 3057 | |
| 3058 | // create a dict from items on the stack |
| 3059 | case ISN_NEWDICT: |
| 3060 | { |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3061 | int count = iptr->isn_arg.number; |
| 3062 | dict_T *dict = dict_alloc(); |
| 3063 | dictitem_T *item; |
Bram Moolenaar | c7f7f6d | 2020-11-04 13:38:28 +0100 | [diff] [blame] | 3064 | char_u *key; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3065 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3066 | |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 3067 | if (unlikely(dict == NULL)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3068 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3069 | for (idx = 0; idx < count; ++idx) |
| 3070 | { |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3071 | // have already checked key type is VAR_STRING |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3072 | tv = STACK_TV_BOT(2 * (idx - count)); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3073 | // check key is unique |
Bram Moolenaar | c7f7f6d | 2020-11-04 13:38:28 +0100 | [diff] [blame] | 3074 | key = tv->vval.v_string == NULL |
| 3075 | ? (char_u *)"" : tv->vval.v_string; |
| 3076 | item = dict_find(dict, key, -1); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3077 | if (item != NULL) |
| 3078 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3079 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | c7f7f6d | 2020-11-04 13:38:28 +0100 | [diff] [blame] | 3080 | semsg(_(e_duplicate_key), key); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3081 | dict_unref(dict); |
| 3082 | goto on_error; |
| 3083 | } |
Bram Moolenaar | c7f7f6d | 2020-11-04 13:38:28 +0100 | [diff] [blame] | 3084 | item = dictitem_alloc(key); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3085 | clear_tv(tv); |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 3086 | if (unlikely(item == NULL)) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3087 | { |
| 3088 | dict_unref(dict); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3089 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3090 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3091 | item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1); |
| 3092 | item->di_tv.v_lock = 0; |
| 3093 | if (dict_add(dict, item) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3094 | { |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3095 | // can this ever happen? |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3096 | dict_unref(dict); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3097 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3098 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | if (count > 0) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3102 | ectx->ec_stack.ga_len -= 2 * count - 1; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3103 | else if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3104 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3105 | else |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3106 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3107 | tv = STACK_TV_BOT(-1); |
| 3108 | tv->v_type = VAR_DICT; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3109 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3110 | tv->vval.v_dict = dict; |
| 3111 | ++dict->dv_refcount; |
| 3112 | } |
| 3113 | break; |
| 3114 | |
| 3115 | // call a :def function |
| 3116 | case ISN_DCALL: |
Bram Moolenaar | dfa3d55 | 2020-09-10 22:05:08 +0200 | [diff] [blame] | 3117 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 3118 | if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, |
| 3119 | NULL, |
| 3120 | iptr->isn_arg.dfunc.cdf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3121 | ectx) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3122 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3123 | break; |
| 3124 | |
| 3125 | // call a builtin function |
| 3126 | case ISN_BCALL: |
| 3127 | SOURCING_LNUM = iptr->isn_lnum; |
| 3128 | if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, |
| 3129 | iptr->isn_arg.bfunc.cbf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3130 | ectx) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3131 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3132 | break; |
| 3133 | |
| 3134 | // call a funcref or partial |
| 3135 | case ISN_PCALL: |
| 3136 | { |
| 3137 | cpfunc_T *pfunc = &iptr->isn_arg.pfunc; |
| 3138 | int r; |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 3139 | typval_T partial_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3140 | |
| 3141 | SOURCING_LNUM = iptr->isn_lnum; |
| 3142 | if (pfunc->cpf_top) |
| 3143 | { |
| 3144 | // funcref is above the arguments |
| 3145 | tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); |
| 3146 | } |
| 3147 | else |
| 3148 | { |
| 3149 | // Get the funcref from the stack. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3150 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 3151 | partial_tv = *STACK_TV_BOT(0); |
| 3152 | tv = &partial_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3153 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3154 | r = call_partial(tv, pfunc->cpf_argcount, ectx); |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 3155 | if (tv == &partial_tv) |
| 3156 | clear_tv(&partial_tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3157 | if (r == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3158 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3159 | } |
| 3160 | break; |
| 3161 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 3162 | case ISN_PCALL_END: |
| 3163 | // PCALL finished, arguments have been consumed and replaced by |
| 3164 | // the return value. Now clear the funcref from the stack, |
| 3165 | // and move the return value in its place. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3166 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 3167 | clear_tv(STACK_TV_BOT(-1)); |
| 3168 | *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); |
| 3169 | break; |
| 3170 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3171 | // call a user defined function or funcref/partial |
| 3172 | case ISN_UCALL: |
| 3173 | { |
| 3174 | cufunc_T *cufunc = &iptr->isn_arg.ufunc; |
| 3175 | |
| 3176 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 3177 | if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3178 | ectx, iptr) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3179 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3180 | } |
| 3181 | break; |
| 3182 | |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 3183 | // return from a :def function call without a value |
| 3184 | case ISN_RETURN_VOID: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3185 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3186 | goto theend; |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 3187 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3188 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 3189 | tv->v_type = VAR_VOID; |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 3190 | tv->vval.v_number = 0; |
| 3191 | tv->v_lock = 0; |
| 3192 | // FALLTHROUGH |
| 3193 | |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 3194 | // return from a :def function call with what is on the stack |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3195 | case ISN_RETURN: |
| 3196 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3197 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3198 | trycmd_T *trycmd = NULL; |
Bram Moolenaar | 8cbd6df | 2020-01-28 22:59:45 +0100 | [diff] [blame] | 3199 | |
| 3200 | if (trystack->ga_len > 0) |
| 3201 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 3202 | + trystack->ga_len - 1; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 3203 | if (trycmd != NULL |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3204 | && trycmd->tcd_frame_idx == ectx->ec_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3205 | { |
Bram Moolenaar | 9cb577a | 2021-02-22 22:45:10 +0100 | [diff] [blame] | 3206 | // jump to ":finally" or ":endtry" |
| 3207 | if (trycmd->tcd_finally_idx != 0) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3208 | ectx->ec_iidx = trycmd->tcd_finally_idx; |
Bram Moolenaar | 9cb577a | 2021-02-22 22:45:10 +0100 | [diff] [blame] | 3209 | else |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3210 | ectx->ec_iidx = trycmd->tcd_endtry_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3211 | trycmd->tcd_return = TRUE; |
| 3212 | } |
| 3213 | else |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3214 | goto func_return; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3215 | } |
| 3216 | break; |
| 3217 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 3218 | // push a partial, a reference to a compiled function |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3219 | case ISN_FUNCREF: |
| 3220 | { |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 3221 | partial_T *pt = ALLOC_CLEAR_ONE(partial_T); |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 3222 | ufunc_T *ufunc; |
| 3223 | funcref_T *funcref = &iptr->isn_arg.funcref; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3224 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3225 | if (pt == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3226 | goto theend; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3227 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3228 | { |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 3229 | vim_free(pt); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3230 | goto theend; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 3231 | } |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 3232 | if (funcref->fr_func_name == NULL) |
| 3233 | { |
| 3234 | dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data) |
| 3235 | + funcref->fr_dfunc_idx; |
| 3236 | |
| 3237 | ufunc = pt_dfunc->df_ufunc; |
| 3238 | } |
| 3239 | else |
| 3240 | { |
| 3241 | ufunc = find_func(funcref->fr_func_name, FALSE, NULL); |
| 3242 | } |
Bram Moolenaar | 293eb9b | 2021-11-29 10:36:19 +0000 | [diff] [blame] | 3243 | if (ufunc == NULL) |
| 3244 | { |
| 3245 | SOURCING_LNUM = iptr->isn_lnum; |
| 3246 | emsg(_(e_function_reference_invalid)); |
| 3247 | goto theend; |
| 3248 | } |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 3249 | if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3250 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3251 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3252 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3253 | tv->vval.v_partial = pt; |
| 3254 | tv->v_type = VAR_PARTIAL; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3255 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3256 | } |
| 3257 | break; |
| 3258 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 3259 | // Create a global function from a lambda. |
| 3260 | case ISN_NEWFUNC: |
| 3261 | { |
| 3262 | newfunc_T *newfunc = &iptr->isn_arg.newfunc; |
| 3263 | |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 3264 | if (copy_func(newfunc->nf_lambda, newfunc->nf_global, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3265 | ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3266 | goto theend; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 3267 | } |
| 3268 | break; |
| 3269 | |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 3270 | // List functions |
| 3271 | case ISN_DEF: |
| 3272 | if (iptr->isn_arg.string == NULL) |
| 3273 | list_functions(NULL); |
| 3274 | else |
| 3275 | { |
| 3276 | exarg_T ea; |
| 3277 | |
| 3278 | CLEAR_FIELD(ea); |
| 3279 | ea.cmd = ea.arg = iptr->isn_arg.string; |
| 3280 | define_function(&ea, NULL); |
| 3281 | } |
| 3282 | break; |
| 3283 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3284 | // jump if a condition is met |
| 3285 | case ISN_JUMP: |
| 3286 | { |
| 3287 | jumpwhen_T when = iptr->isn_arg.jump.jump_when; |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3288 | int error = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3289 | int jump = TRUE; |
| 3290 | |
| 3291 | if (when != JUMP_ALWAYS) |
| 3292 | { |
| 3293 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3294 | if (when == JUMP_IF_COND_FALSE |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 3295 | || when == JUMP_IF_FALSE |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3296 | || when == JUMP_IF_COND_TRUE) |
| 3297 | { |
| 3298 | SOURCING_LNUM = iptr->isn_lnum; |
| 3299 | jump = tv_get_bool_chk(tv, &error); |
| 3300 | if (error) |
| 3301 | goto on_error; |
| 3302 | } |
| 3303 | else |
| 3304 | jump = tv2bool(tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3305 | if (when == JUMP_IF_FALSE |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3306 | || when == JUMP_AND_KEEP_IF_FALSE |
| 3307 | || when == JUMP_IF_COND_FALSE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3308 | jump = !jump; |
Bram Moolenaar | 777770f | 2020-02-06 21:27:08 +0100 | [diff] [blame] | 3309 | if (when == JUMP_IF_FALSE || !jump) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3310 | { |
| 3311 | // drop the value from the stack |
| 3312 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3313 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3314 | } |
| 3315 | } |
| 3316 | if (jump) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3317 | ectx->ec_iidx = iptr->isn_arg.jump.jump_where; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3318 | } |
| 3319 | break; |
| 3320 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 3321 | // Jump if an argument with a default value was already set and not |
| 3322 | // v:none. |
| 3323 | case ISN_JUMP_IF_ARG_SET: |
| 3324 | tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off); |
| 3325 | if (tv->v_type != VAR_UNKNOWN |
| 3326 | && !(tv->v_type == VAR_SPECIAL |
| 3327 | && tv->vval.v_number == VVAL_NONE)) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3328 | ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where; |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 3329 | break; |
| 3330 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3331 | // top of a for loop |
| 3332 | case ISN_FOR: |
| 3333 | { |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3334 | typval_T *ltv = STACK_TV_BOT(-1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3335 | typval_T *idxtv = |
| 3336 | STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); |
| 3337 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3338 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3339 | goto theend; |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3340 | if (ltv->v_type == VAR_LIST) |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 3341 | { |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3342 | list_T *list = ltv->vval.v_list; |
| 3343 | |
| 3344 | // push the next item from the list |
| 3345 | ++idxtv->vval.v_number; |
| 3346 | if (list == NULL |
| 3347 | || idxtv->vval.v_number >= list->lv_len) |
| 3348 | { |
| 3349 | // past the end of the list, jump to "endfor" |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3350 | ectx->ec_iidx = iptr->isn_arg.forloop.for_end; |
| 3351 | may_restore_cmdmod(&ectx->ec_funclocal); |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3352 | } |
| 3353 | else if (list->lv_first == &range_list_item) |
| 3354 | { |
| 3355 | // non-materialized range() list |
| 3356 | tv = STACK_TV_BOT(0); |
| 3357 | tv->v_type = VAR_NUMBER; |
| 3358 | tv->v_lock = 0; |
| 3359 | tv->vval.v_number = list_find_nr( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3360 | list, idxtv->vval.v_number, NULL); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3361 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3362 | } |
| 3363 | else |
| 3364 | { |
| 3365 | listitem_T *li = list_find(list, |
| 3366 | idxtv->vval.v_number); |
| 3367 | |
| 3368 | copy_tv(&li->li_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3369 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3370 | } |
| 3371 | } |
| 3372 | else if (ltv->v_type == VAR_STRING) |
| 3373 | { |
| 3374 | char_u *str = ltv->vval.v_string; |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3375 | |
Bram Moolenaar | d551d6c | 2021-04-18 13:15:58 +0200 | [diff] [blame] | 3376 | // The index is for the last byte of the previous |
| 3377 | // character. |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3378 | ++idxtv->vval.v_number; |
Bram Moolenaar | 175a41c | 2021-04-08 18:05:03 +0200 | [diff] [blame] | 3379 | if (str == NULL || str[idxtv->vval.v_number] == NUL) |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3380 | { |
| 3381 | // past the end of the string, jump to "endfor" |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3382 | ectx->ec_iidx = iptr->isn_arg.forloop.for_end; |
| 3383 | may_restore_cmdmod(&ectx->ec_funclocal); |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3384 | } |
| 3385 | else |
| 3386 | { |
| 3387 | int clen = mb_ptr2len(str + idxtv->vval.v_number); |
| 3388 | |
Bram Moolenaar | d551d6c | 2021-04-18 13:15:58 +0200 | [diff] [blame] | 3389 | // Push the next character from the string. |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3390 | tv = STACK_TV_BOT(0); |
| 3391 | tv->v_type = VAR_STRING; |
| 3392 | tv->vval.v_string = vim_strnsave( |
| 3393 | str + idxtv->vval.v_number, clen); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3394 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3395 | idxtv->vval.v_number += clen - 1; |
| 3396 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3397 | } |
Bram Moolenaar | d551d6c | 2021-04-18 13:15:58 +0200 | [diff] [blame] | 3398 | else if (ltv->v_type == VAR_BLOB) |
| 3399 | { |
| 3400 | blob_T *blob = ltv->vval.v_blob; |
| 3401 | |
| 3402 | // When we get here the first time make a copy of the |
| 3403 | // blob, so that the iteration still works when it is |
| 3404 | // changed. |
| 3405 | if (idxtv->vval.v_number == -1 && blob != NULL) |
| 3406 | { |
| 3407 | blob_copy(blob, ltv); |
| 3408 | blob_unref(blob); |
| 3409 | blob = ltv->vval.v_blob; |
| 3410 | } |
| 3411 | |
| 3412 | // The index is for the previous byte. |
| 3413 | ++idxtv->vval.v_number; |
| 3414 | if (blob == NULL |
| 3415 | || idxtv->vval.v_number >= blob_len(blob)) |
| 3416 | { |
| 3417 | // past the end of the blob, jump to "endfor" |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3418 | ectx->ec_iidx = iptr->isn_arg.forloop.for_end; |
| 3419 | may_restore_cmdmod(&ectx->ec_funclocal); |
Bram Moolenaar | d551d6c | 2021-04-18 13:15:58 +0200 | [diff] [blame] | 3420 | } |
| 3421 | else |
| 3422 | { |
| 3423 | // Push the next byte from the blob. |
| 3424 | tv = STACK_TV_BOT(0); |
| 3425 | tv->v_type = VAR_NUMBER; |
| 3426 | tv->vval.v_number = blob_get(blob, |
| 3427 | idxtv->vval.v_number); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3428 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | d551d6c | 2021-04-18 13:15:58 +0200 | [diff] [blame] | 3429 | } |
| 3430 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3431 | else |
| 3432 | { |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 3433 | semsg(_(e_for_loop_on_str_not_supported), |
| 3434 | vartype_name(ltv->v_type)); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3435 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3436 | } |
| 3437 | } |
| 3438 | break; |
| 3439 | |
| 3440 | // start of ":try" block |
| 3441 | case ISN_TRY: |
| 3442 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3443 | trycmd_T *trycmd = NULL; |
| 3444 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3445 | if (GA_GROW_FAILS(&ectx->ec_trystack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3446 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3447 | trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data) |
| 3448 | + ectx->ec_trystack.ga_len; |
| 3449 | ++ectx->ec_trystack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3450 | ++trylevel; |
Bram Moolenaar | 8d4be89 | 2021-02-13 18:33:02 +0100 | [diff] [blame] | 3451 | CLEAR_POINTER(trycmd); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3452 | trycmd->tcd_frame_idx = ectx->ec_frame_idx; |
| 3453 | trycmd->tcd_stack_len = ectx->ec_stack.ga_len; |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 3454 | trycmd->tcd_catch_idx = |
| 3455 | iptr->isn_arg.try.try_ref->try_catch; |
| 3456 | trycmd->tcd_finally_idx = |
| 3457 | iptr->isn_arg.try.try_ref->try_finally; |
| 3458 | trycmd->tcd_endtry_idx = |
| 3459 | iptr->isn_arg.try.try_ref->try_endtry; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3460 | } |
| 3461 | break; |
| 3462 | |
| 3463 | case ISN_PUSHEXC: |
| 3464 | if (current_exception == NULL) |
| 3465 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3466 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3467 | iemsg("Evaluating catch while current_exception is NULL"); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3468 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3469 | } |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3470 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3471 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3472 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3473 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3474 | tv->v_type = VAR_STRING; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3475 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3476 | tv->vval.v_string = vim_strsave( |
| 3477 | (char_u *)current_exception->value); |
| 3478 | break; |
| 3479 | |
| 3480 | case ISN_CATCH: |
| 3481 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3482 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3483 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3484 | may_restore_cmdmod(&ectx->ec_funclocal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3485 | if (trystack->ga_len > 0) |
| 3486 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3487 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3488 | + trystack->ga_len - 1; |
| 3489 | trycmd->tcd_caught = TRUE; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 3490 | trycmd->tcd_did_throw = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3491 | } |
| 3492 | did_emsg = got_int = did_throw = FALSE; |
Bram Moolenaar | 1430cee | 2021-01-17 19:20:32 +0100 | [diff] [blame] | 3493 | force_abort = need_rethrow = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3494 | catch_exception(current_exception); |
| 3495 | } |
| 3496 | break; |
| 3497 | |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3498 | case ISN_TRYCONT: |
| 3499 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3500 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3501 | trycont_T *trycont = &iptr->isn_arg.trycont; |
| 3502 | int i; |
| 3503 | trycmd_T *trycmd; |
| 3504 | int iidx = trycont->tct_where; |
| 3505 | |
| 3506 | if (trystack->ga_len < trycont->tct_levels) |
| 3507 | { |
| 3508 | siemsg("TRYCONT: expected %d levels, found %d", |
| 3509 | trycont->tct_levels, trystack->ga_len); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3510 | goto theend; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3511 | } |
| 3512 | // Make :endtry jump to any outer try block and the last |
| 3513 | // :endtry inside the loop to the loop start. |
| 3514 | for (i = trycont->tct_levels; i > 0; --i) |
| 3515 | { |
| 3516 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 3517 | + trystack->ga_len - i; |
Bram Moolenaar | 2e34c34 | 2021-03-14 12:13:33 +0100 | [diff] [blame] | 3518 | // Add one to tcd_cont to be able to jump to |
| 3519 | // instruction with index zero. |
| 3520 | trycmd->tcd_cont = iidx + 1; |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 3521 | iidx = trycmd->tcd_finally_idx == 0 |
| 3522 | ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3523 | } |
| 3524 | // jump to :finally or :endtry of current try statement |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3525 | ectx->ec_iidx = iidx; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3526 | } |
| 3527 | break; |
| 3528 | |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 3529 | case ISN_FINALLY: |
| 3530 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3531 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 3532 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
| 3533 | + trystack->ga_len - 1; |
| 3534 | |
| 3535 | // Reset the index to avoid a return statement jumps here |
| 3536 | // again. |
| 3537 | trycmd->tcd_finally_idx = 0; |
| 3538 | break; |
| 3539 | } |
| 3540 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3541 | // end of ":try" block |
| 3542 | case ISN_ENDTRY: |
| 3543 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3544 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3545 | |
| 3546 | if (trystack->ga_len > 0) |
| 3547 | { |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3548 | trycmd_T *trycmd; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3549 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3550 | --trystack->ga_len; |
| 3551 | --trylevel; |
| 3552 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 3553 | + trystack->ga_len; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 3554 | if (trycmd->tcd_did_throw) |
| 3555 | did_throw = TRUE; |
Bram Moolenaar | f575adf | 2020-02-20 20:41:06 +0100 | [diff] [blame] | 3556 | if (trycmd->tcd_caught && current_exception != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3557 | { |
| 3558 | // discard the exception |
| 3559 | if (caught_stack == current_exception) |
| 3560 | caught_stack = caught_stack->caught; |
| 3561 | discard_current_exception(); |
| 3562 | } |
| 3563 | |
| 3564 | if (trycmd->tcd_return) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3565 | goto func_return; |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 3566 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3567 | while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len) |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 3568 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3569 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 3570 | clear_tv(STACK_TV_BOT(0)); |
| 3571 | } |
Bram Moolenaar | 8d4be89 | 2021-02-13 18:33:02 +0100 | [diff] [blame] | 3572 | if (trycmd->tcd_cont != 0) |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3573 | // handling :continue: jump to outer try block or |
| 3574 | // start of the loop |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3575 | ectx->ec_iidx = trycmd->tcd_cont - 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3576 | } |
| 3577 | } |
| 3578 | break; |
| 3579 | |
| 3580 | case ISN_THROW: |
Bram Moolenaar | 8f81b22 | 2021-01-14 21:47:06 +0100 | [diff] [blame] | 3581 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3582 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 1e021e6 | 2020-10-16 20:25:23 +0200 | [diff] [blame] | 3583 | |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3584 | if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent) |
| 3585 | { |
| 3586 | // throwing an exception while using "silent!" causes |
| 3587 | // the function to abort but not display an error. |
| 3588 | tv = STACK_TV_BOT(-1); |
| 3589 | clear_tv(tv); |
| 3590 | tv->v_type = VAR_NUMBER; |
| 3591 | tv->vval.v_number = 0; |
| 3592 | goto done; |
| 3593 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3594 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3595 | tv = STACK_TV_BOT(0); |
| 3596 | if (tv->vval.v_string == NULL |
| 3597 | || *skipwhite(tv->vval.v_string) == NUL) |
| 3598 | { |
| 3599 | vim_free(tv->vval.v_string); |
| 3600 | SOURCING_LNUM = iptr->isn_lnum; |
| 3601 | emsg(_(e_throw_with_empty_string)); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3602 | goto theend; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | // Inside a "catch" we need to first discard the caught |
| 3606 | // exception. |
| 3607 | if (trystack->ga_len > 0) |
| 3608 | { |
| 3609 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
| 3610 | + trystack->ga_len - 1; |
| 3611 | if (trycmd->tcd_caught && current_exception != NULL) |
| 3612 | { |
| 3613 | // discard the exception |
| 3614 | if (caught_stack == current_exception) |
| 3615 | caught_stack = caught_stack->caught; |
| 3616 | discard_current_exception(); |
| 3617 | trycmd->tcd_caught = FALSE; |
| 3618 | } |
| 3619 | } |
| 3620 | |
| 3621 | if (throw_exception(tv->vval.v_string, ET_USER, NULL) |
| 3622 | == FAIL) |
| 3623 | { |
| 3624 | vim_free(tv->vval.v_string); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3625 | goto theend; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3626 | } |
| 3627 | did_throw = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3628 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3629 | break; |
| 3630 | |
| 3631 | // compare with special values |
| 3632 | case ISN_COMPAREBOOL: |
| 3633 | case ISN_COMPARESPECIAL: |
| 3634 | { |
| 3635 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3636 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3637 | varnumber_T arg1 = tv1->vval.v_number; |
| 3638 | varnumber_T arg2 = tv2->vval.v_number; |
| 3639 | int res; |
| 3640 | |
| 3641 | switch (iptr->isn_arg.op.op_type) |
| 3642 | { |
| 3643 | case EXPR_EQUAL: res = arg1 == arg2; break; |
| 3644 | case EXPR_NEQUAL: res = arg1 != arg2; break; |
| 3645 | default: res = 0; break; |
| 3646 | } |
| 3647 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3648 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3649 | tv1->v_type = VAR_BOOL; |
| 3650 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 3651 | } |
| 3652 | break; |
| 3653 | |
| 3654 | // Operation with two number arguments |
| 3655 | case ISN_OPNR: |
| 3656 | case ISN_COMPARENR: |
| 3657 | { |
| 3658 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3659 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3660 | varnumber_T arg1 = tv1->vval.v_number; |
| 3661 | varnumber_T arg2 = tv2->vval.v_number; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 3662 | varnumber_T res = 0; |
| 3663 | int div_zero = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3664 | |
| 3665 | switch (iptr->isn_arg.op.op_type) |
| 3666 | { |
| 3667 | case EXPR_MULT: res = arg1 * arg2; break; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 3668 | case EXPR_DIV: if (arg2 == 0) |
| 3669 | div_zero = TRUE; |
| 3670 | else |
| 3671 | res = arg1 / arg2; |
| 3672 | break; |
| 3673 | case EXPR_REM: if (arg2 == 0) |
| 3674 | div_zero = TRUE; |
| 3675 | else |
| 3676 | res = arg1 % arg2; |
| 3677 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3678 | case EXPR_SUB: res = arg1 - arg2; break; |
| 3679 | case EXPR_ADD: res = arg1 + arg2; break; |
| 3680 | |
| 3681 | case EXPR_EQUAL: res = arg1 == arg2; break; |
| 3682 | case EXPR_NEQUAL: res = arg1 != arg2; break; |
| 3683 | case EXPR_GREATER: res = arg1 > arg2; break; |
| 3684 | case EXPR_GEQUAL: res = arg1 >= arg2; break; |
| 3685 | case EXPR_SMALLER: res = arg1 < arg2; break; |
| 3686 | case EXPR_SEQUAL: res = arg1 <= arg2; break; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 3687 | default: break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3688 | } |
| 3689 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3690 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3691 | if (iptr->isn_type == ISN_COMPARENR) |
| 3692 | { |
| 3693 | tv1->v_type = VAR_BOOL; |
| 3694 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 3695 | } |
| 3696 | else |
| 3697 | tv1->vval.v_number = res; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 3698 | if (div_zero) |
| 3699 | { |
| 3700 | SOURCING_LNUM = iptr->isn_lnum; |
| 3701 | emsg(_(e_divide_by_zero)); |
| 3702 | goto on_error; |
| 3703 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3704 | } |
| 3705 | break; |
| 3706 | |
| 3707 | // Computation with two float arguments |
| 3708 | case ISN_OPFLOAT: |
| 3709 | case ISN_COMPAREFLOAT: |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 3710 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3711 | { |
| 3712 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3713 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3714 | float_T arg1 = tv1->vval.v_float; |
| 3715 | float_T arg2 = tv2->vval.v_float; |
| 3716 | float_T res = 0; |
| 3717 | int cmp = FALSE; |
| 3718 | |
| 3719 | switch (iptr->isn_arg.op.op_type) |
| 3720 | { |
| 3721 | case EXPR_MULT: res = arg1 * arg2; break; |
| 3722 | case EXPR_DIV: res = arg1 / arg2; break; |
| 3723 | case EXPR_SUB: res = arg1 - arg2; break; |
| 3724 | case EXPR_ADD: res = arg1 + arg2; break; |
| 3725 | |
| 3726 | case EXPR_EQUAL: cmp = arg1 == arg2; break; |
| 3727 | case EXPR_NEQUAL: cmp = arg1 != arg2; break; |
| 3728 | case EXPR_GREATER: cmp = arg1 > arg2; break; |
| 3729 | case EXPR_GEQUAL: cmp = arg1 >= arg2; break; |
| 3730 | case EXPR_SMALLER: cmp = arg1 < arg2; break; |
| 3731 | case EXPR_SEQUAL: cmp = arg1 <= arg2; break; |
| 3732 | default: cmp = 0; break; |
| 3733 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3734 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3735 | if (iptr->isn_type == ISN_COMPAREFLOAT) |
| 3736 | { |
| 3737 | tv1->v_type = VAR_BOOL; |
| 3738 | tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; |
| 3739 | } |
| 3740 | else |
| 3741 | tv1->vval.v_float = res; |
| 3742 | } |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 3743 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3744 | break; |
| 3745 | |
| 3746 | case ISN_COMPARELIST: |
| 3747 | { |
| 3748 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3749 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3750 | list_T *arg1 = tv1->vval.v_list; |
| 3751 | list_T *arg2 = tv2->vval.v_list; |
| 3752 | int cmp = FALSE; |
| 3753 | int ic = iptr->isn_arg.op.op_ic; |
| 3754 | |
| 3755 | switch (iptr->isn_arg.op.op_type) |
| 3756 | { |
| 3757 | case EXPR_EQUAL: cmp = |
| 3758 | list_equal(arg1, arg2, ic, FALSE); break; |
| 3759 | case EXPR_NEQUAL: cmp = |
| 3760 | !list_equal(arg1, arg2, ic, FALSE); break; |
| 3761 | case EXPR_IS: cmp = arg1 == arg2; break; |
| 3762 | case EXPR_ISNOT: cmp = arg1 != arg2; break; |
| 3763 | default: cmp = 0; break; |
| 3764 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3765 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3766 | clear_tv(tv1); |
| 3767 | clear_tv(tv2); |
| 3768 | tv1->v_type = VAR_BOOL; |
| 3769 | tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; |
| 3770 | } |
| 3771 | break; |
| 3772 | |
| 3773 | case ISN_COMPAREBLOB: |
| 3774 | { |
| 3775 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3776 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3777 | blob_T *arg1 = tv1->vval.v_blob; |
| 3778 | blob_T *arg2 = tv2->vval.v_blob; |
| 3779 | int cmp = FALSE; |
| 3780 | |
| 3781 | switch (iptr->isn_arg.op.op_type) |
| 3782 | { |
| 3783 | case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break; |
| 3784 | case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break; |
| 3785 | case EXPR_IS: cmp = arg1 == arg2; break; |
| 3786 | case EXPR_ISNOT: cmp = arg1 != arg2; break; |
| 3787 | default: cmp = 0; break; |
| 3788 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3789 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3790 | clear_tv(tv1); |
| 3791 | clear_tv(tv2); |
| 3792 | tv1->v_type = VAR_BOOL; |
| 3793 | tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; |
| 3794 | } |
| 3795 | break; |
| 3796 | |
| 3797 | // TODO: handle separately |
| 3798 | case ISN_COMPARESTRING: |
| 3799 | case ISN_COMPAREDICT: |
| 3800 | case ISN_COMPAREFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3801 | case ISN_COMPAREANY: |
| 3802 | { |
| 3803 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3804 | typval_T *tv2 = STACK_TV_BOT(-1); |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 3805 | exprtype_T exprtype = iptr->isn_arg.op.op_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3806 | int ic = iptr->isn_arg.op.op_ic; |
| 3807 | |
Bram Moolenaar | eb26f43 | 2020-09-14 16:50:05 +0200 | [diff] [blame] | 3808 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 3809 | typval_compare(tv1, tv2, exprtype, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3810 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3811 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3812 | } |
| 3813 | break; |
| 3814 | |
| 3815 | case ISN_ADDLIST: |
| 3816 | case ISN_ADDBLOB: |
| 3817 | { |
| 3818 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3819 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3820 | |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 3821 | // add two lists or blobs |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3822 | if (iptr->isn_type == ISN_ADDLIST) |
Bram Moolenaar | 0780204 | 2021-09-09 23:01:14 +0200 | [diff] [blame] | 3823 | { |
| 3824 | if (iptr->isn_arg.op.op_type == EXPR_APPEND |
| 3825 | && tv1->vval.v_list != NULL) |
| 3826 | list_extend(tv1->vval.v_list, tv2->vval.v_list, |
| 3827 | NULL); |
| 3828 | else |
| 3829 | eval_addlist(tv1, tv2); |
| 3830 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3831 | else |
| 3832 | eval_addblob(tv1, tv2); |
| 3833 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3834 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3835 | } |
| 3836 | break; |
| 3837 | |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 3838 | case ISN_LISTAPPEND: |
| 3839 | { |
| 3840 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3841 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3842 | list_T *l = tv1->vval.v_list; |
| 3843 | |
| 3844 | // add an item to a list |
| 3845 | if (l == NULL) |
| 3846 | { |
| 3847 | SOURCING_LNUM = iptr->isn_lnum; |
| 3848 | emsg(_(e_cannot_add_to_null_list)); |
| 3849 | goto on_error; |
| 3850 | } |
| 3851 | if (list_append_tv(l, tv2) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3852 | goto theend; |
Bram Moolenaar | 955347c | 2020-10-19 23:01:46 +0200 | [diff] [blame] | 3853 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3854 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 3855 | } |
| 3856 | break; |
| 3857 | |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 3858 | case ISN_BLOBAPPEND: |
| 3859 | { |
| 3860 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3861 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3862 | blob_T *b = tv1->vval.v_blob; |
| 3863 | int error = FALSE; |
| 3864 | varnumber_T n; |
| 3865 | |
| 3866 | // add a number to a blob |
| 3867 | if (b == NULL) |
| 3868 | { |
| 3869 | SOURCING_LNUM = iptr->isn_lnum; |
| 3870 | emsg(_(e_cannot_add_to_null_blob)); |
| 3871 | goto on_error; |
| 3872 | } |
| 3873 | n = tv_get_number_chk(tv2, &error); |
| 3874 | if (error) |
| 3875 | goto on_error; |
| 3876 | ga_append(&b->bv_ga, (int)n); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3877 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 3878 | } |
| 3879 | break; |
| 3880 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3881 | // Computation with two arguments of unknown type |
| 3882 | case ISN_OPANY: |
| 3883 | { |
| 3884 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3885 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3886 | varnumber_T n1, n2; |
| 3887 | #ifdef FEAT_FLOAT |
| 3888 | float_T f1 = 0, f2 = 0; |
| 3889 | #endif |
| 3890 | int error = FALSE; |
| 3891 | |
| 3892 | if (iptr->isn_arg.op.op_type == EXPR_ADD) |
| 3893 | { |
| 3894 | if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) |
| 3895 | { |
| 3896 | eval_addlist(tv1, tv2); |
| 3897 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3898 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3899 | break; |
| 3900 | } |
| 3901 | else if (tv1->v_type == VAR_BLOB |
| 3902 | && tv2->v_type == VAR_BLOB) |
| 3903 | { |
| 3904 | eval_addblob(tv1, tv2); |
| 3905 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3906 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3907 | break; |
| 3908 | } |
| 3909 | } |
| 3910 | #ifdef FEAT_FLOAT |
| 3911 | if (tv1->v_type == VAR_FLOAT) |
| 3912 | { |
| 3913 | f1 = tv1->vval.v_float; |
| 3914 | n1 = 0; |
| 3915 | } |
| 3916 | else |
| 3917 | #endif |
| 3918 | { |
Bram Moolenaar | f665e97 | 2020-12-05 19:17:16 +0100 | [diff] [blame] | 3919 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3920 | n1 = tv_get_number_chk(tv1, &error); |
| 3921 | if (error) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3922 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3923 | #ifdef FEAT_FLOAT |
| 3924 | if (tv2->v_type == VAR_FLOAT) |
| 3925 | f1 = n1; |
| 3926 | #endif |
| 3927 | } |
| 3928 | #ifdef FEAT_FLOAT |
| 3929 | if (tv2->v_type == VAR_FLOAT) |
| 3930 | { |
| 3931 | f2 = tv2->vval.v_float; |
| 3932 | n2 = 0; |
| 3933 | } |
| 3934 | else |
| 3935 | #endif |
| 3936 | { |
| 3937 | n2 = tv_get_number_chk(tv2, &error); |
| 3938 | if (error) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3939 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3940 | #ifdef FEAT_FLOAT |
| 3941 | if (tv1->v_type == VAR_FLOAT) |
| 3942 | f2 = n2; |
| 3943 | #endif |
| 3944 | } |
| 3945 | #ifdef FEAT_FLOAT |
| 3946 | // if there is a float on either side the result is a float |
| 3947 | if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) |
| 3948 | { |
| 3949 | switch (iptr->isn_arg.op.op_type) |
| 3950 | { |
| 3951 | case EXPR_MULT: f1 = f1 * f2; break; |
| 3952 | case EXPR_DIV: f1 = f1 / f2; break; |
| 3953 | case EXPR_SUB: f1 = f1 - f2; break; |
| 3954 | case EXPR_ADD: f1 = f1 + f2; break; |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3955 | default: SOURCING_LNUM = iptr->isn_lnum; |
| 3956 | emsg(_(e_modulus)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 3957 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3958 | } |
| 3959 | clear_tv(tv1); |
| 3960 | clear_tv(tv2); |
| 3961 | tv1->v_type = VAR_FLOAT; |
| 3962 | tv1->vval.v_float = f1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3963 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3964 | } |
| 3965 | else |
| 3966 | #endif |
| 3967 | { |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 3968 | int failed = FALSE; |
| 3969 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3970 | switch (iptr->isn_arg.op.op_type) |
| 3971 | { |
| 3972 | case EXPR_MULT: n1 = n1 * n2; break; |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 3973 | case EXPR_DIV: n1 = num_divide(n1, n2, &failed); |
| 3974 | if (failed) |
Bram Moolenaar | 99880f9 | 2021-01-20 21:23:14 +0100 | [diff] [blame] | 3975 | goto on_error; |
| 3976 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3977 | case EXPR_SUB: n1 = n1 - n2; break; |
| 3978 | case EXPR_ADD: n1 = n1 + n2; break; |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 3979 | default: n1 = num_modulus(n1, n2, &failed); |
| 3980 | if (failed) |
Bram Moolenaar | 99880f9 | 2021-01-20 21:23:14 +0100 | [diff] [blame] | 3981 | goto on_error; |
| 3982 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3983 | } |
| 3984 | clear_tv(tv1); |
| 3985 | clear_tv(tv2); |
| 3986 | tv1->v_type = VAR_NUMBER; |
| 3987 | tv1->vval.v_number = n1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3988 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3989 | } |
| 3990 | } |
| 3991 | break; |
| 3992 | |
| 3993 | case ISN_CONCAT: |
| 3994 | { |
| 3995 | char_u *str1 = STACK_TV_BOT(-2)->vval.v_string; |
| 3996 | char_u *str2 = STACK_TV_BOT(-1)->vval.v_string; |
| 3997 | char_u *res; |
| 3998 | |
| 3999 | res = concat_str(str1, str2); |
| 4000 | clear_tv(STACK_TV_BOT(-2)); |
| 4001 | clear_tv(STACK_TV_BOT(-1)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4002 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4003 | STACK_TV_BOT(-1)->vval.v_string = res; |
| 4004 | } |
| 4005 | break; |
| 4006 | |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4007 | case ISN_STRINDEX: |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4008 | case ISN_STRSLICE: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4009 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4010 | int is_slice = iptr->isn_type == ISN_STRSLICE; |
| 4011 | varnumber_T n1 = 0, n2; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4012 | char_u *res; |
| 4013 | |
| 4014 | // string index: string is at stack-2, index at stack-1 |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4015 | // string slice: string is at stack-3, first index at |
| 4016 | // stack-2, second index at stack-1 |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4017 | if (is_slice) |
| 4018 | { |
| 4019 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4020 | n1 = tv->vval.v_number; |
| 4021 | } |
| 4022 | |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4023 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4024 | n2 = tv->vval.v_number; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4025 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4026 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4027 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4028 | if (is_slice) |
| 4029 | // Slice: Select the characters from the string |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 4030 | res = string_slice(tv->vval.v_string, n1, n2, FALSE); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4031 | else |
| 4032 | // Index: The resulting variable is a string of a |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 4033 | // single character (including composing characters). |
| 4034 | // If the index is too big or negative the result is |
| 4035 | // empty. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4036 | res = char_from_string(tv->vval.v_string, n2); |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4037 | vim_free(tv->vval.v_string); |
| 4038 | tv->vval.v_string = res; |
| 4039 | } |
| 4040 | break; |
| 4041 | |
| 4042 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4043 | case ISN_LISTSLICE: |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4044 | case ISN_BLOBINDEX: |
| 4045 | case ISN_BLOBSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4046 | { |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4047 | int is_slice = iptr->isn_type == ISN_LISTSLICE |
| 4048 | || iptr->isn_type == ISN_BLOBSLICE; |
| 4049 | int is_blob = iptr->isn_type == ISN_BLOBINDEX |
| 4050 | || iptr->isn_type == ISN_BLOBSLICE; |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4051 | varnumber_T n1, n2; |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4052 | typval_T *val_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4053 | |
| 4054 | // list index: list is at stack-2, index at stack-1 |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4055 | // list slice: list is at stack-3, indexes at stack-2 and |
| 4056 | // stack-1 |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4057 | // Same for blob. |
| 4058 | val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4059 | |
| 4060 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4061 | n1 = n2 = tv->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4062 | clear_tv(tv); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4063 | |
| 4064 | if (is_slice) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4065 | { |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4066 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4067 | n1 = tv->vval.v_number; |
| 4068 | clear_tv(tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4069 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4070 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4071 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | 435d897 | 2020-07-05 16:42:13 +0200 | [diff] [blame] | 4072 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 1d63454 | 2020-08-18 13:41:50 +0200 | [diff] [blame] | 4073 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4074 | if (is_blob) |
| 4075 | { |
| 4076 | if (blob_slice_or_index(val_tv->vval.v_blob, is_slice, |
| 4077 | n1, n2, FALSE, tv) == FAIL) |
| 4078 | goto on_error; |
| 4079 | } |
| 4080 | else |
| 4081 | { |
| 4082 | if (list_slice_or_index(val_tv->vval.v_list, is_slice, |
| 4083 | n1, n2, FALSE, tv, TRUE) == FAIL) |
| 4084 | goto on_error; |
| 4085 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4086 | } |
| 4087 | break; |
| 4088 | |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4089 | case ISN_ANYINDEX: |
| 4090 | case ISN_ANYSLICE: |
| 4091 | { |
| 4092 | int is_slice = iptr->isn_type == ISN_ANYSLICE; |
| 4093 | typval_T *var1, *var2; |
| 4094 | int res; |
| 4095 | |
| 4096 | // index: composite is at stack-2, index at stack-1 |
| 4097 | // slice: composite is at stack-3, indexes at stack-2 and |
| 4098 | // stack-1 |
| 4099 | tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); |
Bram Moolenaar | 3affe7a | 2020-08-18 20:34:13 +0200 | [diff] [blame] | 4100 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4101 | if (check_can_index(tv, TRUE, TRUE) == FAIL) |
| 4102 | goto on_error; |
| 4103 | var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1); |
| 4104 | var2 = is_slice ? STACK_TV_BOT(-1) : NULL; |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 4105 | res = eval_index_inner(tv, is_slice, var1, var2, |
| 4106 | FALSE, NULL, -1, TRUE); |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4107 | clear_tv(var1); |
| 4108 | if (is_slice) |
| 4109 | clear_tv(var2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4110 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4111 | if (res == FAIL) |
| 4112 | goto on_error; |
| 4113 | } |
| 4114 | break; |
| 4115 | |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4116 | case ISN_SLICE: |
| 4117 | { |
| 4118 | list_T *list; |
| 4119 | int count = iptr->isn_arg.number; |
| 4120 | |
Bram Moolenaar | c5b1c20 | 2020-06-18 22:43:27 +0200 | [diff] [blame] | 4121 | // type will have been checked to be a list |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4122 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4123 | list = tv->vval.v_list; |
| 4124 | |
| 4125 | // no error for short list, expect it to be checked earlier |
| 4126 | if (list != NULL && list->lv_len >= count) |
| 4127 | { |
| 4128 | list_T *newlist = list_slice(list, |
| 4129 | count, list->lv_len - 1); |
| 4130 | |
| 4131 | if (newlist != NULL) |
| 4132 | { |
| 4133 | list_unref(list); |
| 4134 | tv->vval.v_list = newlist; |
| 4135 | ++newlist->lv_refcount; |
| 4136 | } |
| 4137 | } |
| 4138 | } |
| 4139 | break; |
| 4140 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4141 | case ISN_GETITEM: |
| 4142 | { |
| 4143 | listitem_T *li; |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 4144 | getitem_T *gi = &iptr->isn_arg.getitem; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4145 | |
Bram Moolenaar | c785b9a | 2020-06-19 18:34:15 +0200 | [diff] [blame] | 4146 | // Get list item: list is at stack-1, push item. |
| 4147 | // List type and length is checked for when compiling. |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 4148 | tv = STACK_TV_BOT(-1 - gi->gi_with_op); |
| 4149 | li = list_find(tv->vval.v_list, gi->gi_index); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4150 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4151 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4152 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4153 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4154 | copy_tv(&li->li_tv, STACK_TV_BOT(-1)); |
Bram Moolenaar | f785aa1 | 2021-02-11 21:19:34 +0100 | [diff] [blame] | 4155 | |
| 4156 | // Useful when used in unpack assignment. Reset at |
| 4157 | // ISN_DROP. |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 4158 | ectx->ec_where.wt_index = gi->gi_index + 1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4159 | ectx->ec_where.wt_variable = TRUE; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4160 | } |
| 4161 | break; |
| 4162 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4163 | case ISN_MEMBER: |
| 4164 | { |
| 4165 | dict_T *dict; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4166 | char_u *key; |
| 4167 | dictitem_T *di; |
| 4168 | |
| 4169 | // dict member: dict is at stack-2, key at stack-1 |
| 4170 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 4171 | // no need to check for VAR_DICT, CHECKTYPE will check. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4172 | dict = tv->vval.v_dict; |
| 4173 | |
| 4174 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 4175 | // no need to check for VAR_STRING, 2STRING will check. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4176 | key = tv->vval.v_string; |
Bram Moolenaar | 086fc9a | 2020-10-30 18:33:02 +0100 | [diff] [blame] | 4177 | if (key == NULL) |
| 4178 | key = (char_u *)""; |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 4179 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4180 | if ((di = dict_find(dict, key, -1)) == NULL) |
| 4181 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4182 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4183 | semsg(_(e_dictkey), key); |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 4184 | |
| 4185 | // If :silent! is used we will continue, make sure the |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4186 | // stack contents makes sense and the dict stack is |
| 4187 | // updated. |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 4188 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4189 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 4190 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4191 | (void) dict_stack_save(tv); |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 4192 | tv->v_type = VAR_NUMBER; |
| 4193 | tv->vval.v_number = 0; |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 4194 | goto on_fatal_error; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4195 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4196 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4197 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4198 | // Put the dict used on the dict stack, it might be used by |
| 4199 | // a dict function later. |
Bram Moolenaar | 50788ef | 2020-07-05 16:51:26 +0200 | [diff] [blame] | 4200 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4201 | if (dict_stack_save(tv) == FAIL) |
| 4202 | goto on_fatal_error; |
Bram Moolenaar | 50788ef | 2020-07-05 16:51:26 +0200 | [diff] [blame] | 4203 | copy_tv(&di->di_tv, tv); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4204 | } |
| 4205 | break; |
| 4206 | |
| 4207 | // dict member with string key |
| 4208 | case ISN_STRINGMEMBER: |
| 4209 | { |
| 4210 | dict_T *dict; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4211 | dictitem_T *di; |
| 4212 | |
| 4213 | tv = STACK_TV_BOT(-1); |
| 4214 | if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) |
| 4215 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4216 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4217 | emsg(_(e_dictreq)); |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4218 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4219 | } |
| 4220 | dict = tv->vval.v_dict; |
| 4221 | |
| 4222 | if ((di = dict_find(dict, iptr->isn_arg.string, -1)) |
| 4223 | == NULL) |
| 4224 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4225 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4226 | semsg(_(e_dictkey), iptr->isn_arg.string); |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4227 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4228 | } |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4229 | // Put the dict used on the dict stack, it might be used by |
| 4230 | // a dict function later. |
| 4231 | if (dict_stack_save(tv) == FAIL) |
| 4232 | goto on_fatal_error; |
| 4233 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4234 | copy_tv(&di->di_tv, tv); |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4235 | } |
| 4236 | break; |
| 4237 | |
| 4238 | case ISN_CLEARDICT: |
| 4239 | dict_stack_drop(); |
| 4240 | break; |
| 4241 | |
| 4242 | case ISN_USEDICT: |
| 4243 | { |
| 4244 | typval_T *dict_tv = dict_stack_get_tv(); |
| 4245 | |
| 4246 | // Turn "dict.Func" into a partial for "Func" bound to |
| 4247 | // "dict". Don't do this when "Func" is already a partial |
| 4248 | // that was bound explicitly (pt_auto is FALSE). |
| 4249 | tv = STACK_TV_BOT(-1); |
| 4250 | if (dict_tv != NULL |
| 4251 | && dict_tv->v_type == VAR_DICT |
| 4252 | && dict_tv->vval.v_dict != NULL |
| 4253 | && (tv->v_type == VAR_FUNC |
| 4254 | || (tv->v_type == VAR_PARTIAL |
| 4255 | && (tv->vval.v_partial->pt_auto |
| 4256 | || tv->vval.v_partial->pt_dict == NULL)))) |
| 4257 | dict_tv->vval.v_dict = |
| 4258 | make_partial(dict_tv->vval.v_dict, tv); |
| 4259 | dict_stack_drop(); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4260 | } |
| 4261 | break; |
| 4262 | |
| 4263 | case ISN_NEGATENR: |
| 4264 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 4265 | if (tv->v_type != VAR_NUMBER |
| 4266 | #ifdef FEAT_FLOAT |
| 4267 | && tv->v_type != VAR_FLOAT |
| 4268 | #endif |
| 4269 | ) |
| 4270 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4271 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | e29a27f | 2021-07-20 21:07:36 +0200 | [diff] [blame] | 4272 | emsg(_(e_number_expected)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4273 | goto on_error; |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 4274 | } |
| 4275 | #ifdef FEAT_FLOAT |
| 4276 | if (tv->v_type == VAR_FLOAT) |
| 4277 | tv->vval.v_float = -tv->vval.v_float; |
| 4278 | else |
| 4279 | #endif |
| 4280 | tv->vval.v_number = -tv->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4281 | break; |
| 4282 | |
| 4283 | case ISN_CHECKNR: |
| 4284 | { |
| 4285 | int error = FALSE; |
| 4286 | |
| 4287 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 3affe7a | 2020-08-18 20:34:13 +0200 | [diff] [blame] | 4288 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4289 | if (check_not_string(tv) == FAIL) |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4290 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4291 | (void)tv_get_number_chk(tv, &error); |
| 4292 | if (error) |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4293 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4294 | } |
| 4295 | break; |
| 4296 | |
| 4297 | case ISN_CHECKTYPE: |
| 4298 | { |
| 4299 | checktype_T *ct = &iptr->isn_arg.type; |
| 4300 | |
Bram Moolenaar | b3005ce | 2021-01-22 17:51:06 +0100 | [diff] [blame] | 4301 | tv = STACK_TV_BOT((int)ct->ct_off); |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 4302 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4303 | if (!ectx->ec_where.wt_variable) |
| 4304 | ectx->ec_where.wt_index = ct->ct_arg_idx; |
| 4305 | if (check_typval_type(ct->ct_type, tv, ectx->ec_where) |
| 4306 | == FAIL) |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 4307 | goto on_error; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4308 | if (!ectx->ec_where.wt_variable) |
| 4309 | ectx->ec_where.wt_index = 0; |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 4310 | |
| 4311 | // number 0 is FALSE, number 1 is TRUE |
| 4312 | if (tv->v_type == VAR_NUMBER |
| 4313 | && ct->ct_type->tt_type == VAR_BOOL |
| 4314 | && (tv->vval.v_number == 0 |
| 4315 | || tv->vval.v_number == 1)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4316 | { |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 4317 | tv->v_type = VAR_BOOL; |
| 4318 | tv->vval.v_number = tv->vval.v_number |
Bram Moolenaar | dadaddd | 2020-09-12 19:11:23 +0200 | [diff] [blame] | 4319 | ? VVAL_TRUE : VVAL_FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4320 | } |
| 4321 | } |
| 4322 | break; |
| 4323 | |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4324 | case ISN_CHECKLEN: |
| 4325 | { |
| 4326 | int min_len = iptr->isn_arg.checklen.cl_min_len; |
| 4327 | list_T *list = NULL; |
| 4328 | |
| 4329 | tv = STACK_TV_BOT(-1); |
| 4330 | if (tv->v_type == VAR_LIST) |
| 4331 | list = tv->vval.v_list; |
| 4332 | if (list == NULL || list->lv_len < min_len |
| 4333 | || (list->lv_len > min_len |
| 4334 | && !iptr->isn_arg.checklen.cl_more_OK)) |
| 4335 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4336 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4337 | semsg(_(e_expected_nr_items_but_got_nr), |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4338 | min_len, list == NULL ? 0 : list->lv_len); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4339 | goto on_error; |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4340 | } |
| 4341 | } |
| 4342 | break; |
| 4343 | |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 4344 | case ISN_SETTYPE: |
| 4345 | { |
| 4346 | checktype_T *ct = &iptr->isn_arg.type; |
| 4347 | |
| 4348 | tv = STACK_TV_BOT(-1); |
| 4349 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 4350 | { |
| 4351 | free_type(tv->vval.v_dict->dv_type); |
| 4352 | tv->vval.v_dict->dv_type = alloc_type(ct->ct_type); |
| 4353 | } |
| 4354 | else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL) |
| 4355 | { |
| 4356 | free_type(tv->vval.v_list->lv_type); |
| 4357 | tv->vval.v_list->lv_type = alloc_type(ct->ct_type); |
| 4358 | } |
| 4359 | } |
| 4360 | break; |
| 4361 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4362 | case ISN_2BOOL: |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4363 | case ISN_COND2BOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4364 | { |
| 4365 | int n; |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4366 | int error = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4367 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4368 | if (iptr->isn_type == ISN_2BOOL) |
| 4369 | { |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 4370 | tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4371 | n = tv2bool(tv); |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 4372 | if (iptr->isn_arg.tobool.invert) |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4373 | n = !n; |
| 4374 | } |
| 4375 | else |
| 4376 | { |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 4377 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4378 | SOURCING_LNUM = iptr->isn_lnum; |
| 4379 | n = tv_get_bool_chk(tv, &error); |
| 4380 | if (error) |
| 4381 | goto on_error; |
| 4382 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4383 | clear_tv(tv); |
| 4384 | tv->v_type = VAR_BOOL; |
| 4385 | tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; |
| 4386 | } |
| 4387 | break; |
| 4388 | |
| 4389 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 4390 | case ISN_2STRING_ANY: |
Bram Moolenaar | 0acbf5a | 2021-01-05 20:58:25 +0100 | [diff] [blame] | 4391 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 4392 | if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset), |
| 4393 | iptr->isn_type == ISN_2STRING_ANY, |
| 4394 | iptr->isn_arg.tostring.tolerant) == FAIL) |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 4395 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4396 | break; |
| 4397 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4398 | case ISN_RANGE: |
| 4399 | { |
| 4400 | exarg_T ea; |
| 4401 | char *errormsg; |
| 4402 | |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 4403 | ea.line2 = 0; |
Bram Moolenaar | d1510ee | 2021-01-04 16:15:58 +0100 | [diff] [blame] | 4404 | ea.addr_count = 0; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4405 | ea.addr_type = ADDR_LINES; |
| 4406 | ea.cmd = iptr->isn_arg.string; |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 4407 | ea.skip = FALSE; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4408 | if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL) |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 4409 | goto on_error; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 4410 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4411 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4412 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4413 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 4414 | tv = STACK_TV_BOT(-1); |
| 4415 | tv->v_type = VAR_NUMBER; |
| 4416 | tv->v_lock = 0; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4417 | if (ea.addr_count == 0) |
| 4418 | tv->vval.v_number = curwin->w_cursor.lnum; |
| 4419 | else |
| 4420 | tv->vval.v_number = ea.line2; |
| 4421 | } |
| 4422 | break; |
| 4423 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 4424 | case ISN_PUT: |
| 4425 | { |
| 4426 | int regname = iptr->isn_arg.put.put_regname; |
| 4427 | linenr_T lnum = iptr->isn_arg.put.put_lnum; |
| 4428 | char_u *expr = NULL; |
| 4429 | int dir = FORWARD; |
| 4430 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4431 | if (lnum < -2) |
| 4432 | { |
| 4433 | // line number was put on the stack by ISN_RANGE |
| 4434 | tv = STACK_TV_BOT(-1); |
| 4435 | curwin->w_cursor.lnum = tv->vval.v_number; |
| 4436 | if (lnum == LNUM_VARIABLE_RANGE_ABOVE) |
| 4437 | dir = BACKWARD; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4438 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4439 | } |
| 4440 | else if (lnum == -2) |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 4441 | // :put! above cursor |
| 4442 | dir = BACKWARD; |
| 4443 | else if (lnum >= 0) |
| 4444 | curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 4445 | |
| 4446 | if (regname == '=') |
| 4447 | { |
| 4448 | tv = STACK_TV_BOT(-1); |
| 4449 | if (tv->v_type == VAR_STRING) |
| 4450 | expr = tv->vval.v_string; |
| 4451 | else |
| 4452 | { |
| 4453 | expr = typval2string(tv, TRUE); // allocates value |
| 4454 | clear_tv(tv); |
| 4455 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4456 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 4457 | } |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 4458 | check_cursor(); |
| 4459 | do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE); |
| 4460 | vim_free(expr); |
| 4461 | } |
| 4462 | break; |
| 4463 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 4464 | case ISN_CMDMOD: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4465 | ectx->ec_funclocal.floc_save_cmdmod = cmdmod; |
| 4466 | ectx->ec_funclocal.floc_restore_cmdmod = TRUE; |
| 4467 | ectx->ec_funclocal.floc_restore_cmdmod_stacklen = |
| 4468 | ectx->ec_stack.ga_len; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 4469 | cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod; |
| 4470 | apply_cmdmod(&cmdmod); |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 4471 | break; |
| 4472 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 4473 | case ISN_CMDMOD_REV: |
| 4474 | // filter regprog is owned by the instruction, don't free it |
| 4475 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 4476 | undo_cmdmod(&cmdmod); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4477 | cmdmod = ectx->ec_funclocal.floc_save_cmdmod; |
| 4478 | ectx->ec_funclocal.floc_restore_cmdmod = FALSE; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 4479 | break; |
| 4480 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 4481 | case ISN_UNPACK: |
| 4482 | { |
| 4483 | int count = iptr->isn_arg.unpack.unp_count; |
| 4484 | int semicolon = iptr->isn_arg.unpack.unp_semicolon; |
| 4485 | list_T *l; |
| 4486 | listitem_T *li; |
| 4487 | int i; |
| 4488 | |
| 4489 | // Check there is a valid list to unpack. |
| 4490 | tv = STACK_TV_BOT(-1); |
| 4491 | if (tv->v_type != VAR_LIST) |
| 4492 | { |
| 4493 | SOURCING_LNUM = iptr->isn_lnum; |
| 4494 | emsg(_(e_for_argument_must_be_sequence_of_lists)); |
| 4495 | goto on_error; |
| 4496 | } |
| 4497 | l = tv->vval.v_list; |
| 4498 | if (l == NULL |
| 4499 | || l->lv_len < (semicolon ? count - 1 : count)) |
| 4500 | { |
| 4501 | SOURCING_LNUM = iptr->isn_lnum; |
| 4502 | emsg(_(e_list_value_does_not_have_enough_items)); |
| 4503 | goto on_error; |
| 4504 | } |
| 4505 | else if (!semicolon && l->lv_len > count) |
| 4506 | { |
| 4507 | SOURCING_LNUM = iptr->isn_lnum; |
| 4508 | emsg(_(e_list_value_has_more_items_than_targets)); |
| 4509 | goto on_error; |
| 4510 | } |
| 4511 | |
| 4512 | CHECK_LIST_MATERIALIZE(l); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4513 | if (GA_GROW_FAILS(&ectx->ec_stack, count - 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4514 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4515 | ectx->ec_stack.ga_len += count - 1; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 4516 | |
| 4517 | // Variable after semicolon gets a list with the remaining |
| 4518 | // items. |
| 4519 | if (semicolon) |
| 4520 | { |
| 4521 | list_T *rem_list = |
| 4522 | list_alloc_with_items(l->lv_len - count + 1); |
| 4523 | |
| 4524 | if (rem_list == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4525 | goto theend; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 4526 | tv = STACK_TV_BOT(-count); |
| 4527 | tv->vval.v_list = rem_list; |
| 4528 | ++rem_list->lv_refcount; |
| 4529 | tv->v_lock = 0; |
| 4530 | li = l->lv_first; |
| 4531 | for (i = 0; i < count - 1; ++i) |
| 4532 | li = li->li_next; |
| 4533 | for (i = 0; li != NULL; ++i) |
| 4534 | { |
| 4535 | list_set_item(rem_list, i, &li->li_tv); |
| 4536 | li = li->li_next; |
| 4537 | } |
| 4538 | --count; |
| 4539 | } |
| 4540 | |
| 4541 | // Produce the values in reverse order, first item last. |
| 4542 | li = l->lv_first; |
| 4543 | for (i = 0; i < count; ++i) |
| 4544 | { |
| 4545 | tv = STACK_TV_BOT(-i - 1); |
| 4546 | copy_tv(&li->li_tv, tv); |
| 4547 | li = li->li_next; |
| 4548 | } |
| 4549 | |
| 4550 | list_unref(l); |
| 4551 | } |
| 4552 | break; |
| 4553 | |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4554 | case ISN_PROF_START: |
| 4555 | case ISN_PROF_END: |
| 4556 | { |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 4557 | #ifdef FEAT_PROFILE |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4558 | funccall_T cookie; |
| 4559 | ufunc_T *cur_ufunc = |
| 4560 | (((dfunc_T *)def_functions.ga_data) |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 4561 | + ectx->ec_dfunc_idx)->df_ufunc; |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4562 | |
| 4563 | cookie.func = cur_ufunc; |
| 4564 | if (iptr->isn_type == ISN_PROF_START) |
| 4565 | { |
| 4566 | func_line_start(&cookie, iptr->isn_lnum); |
| 4567 | // if we get here the instruction is executed |
| 4568 | func_line_exec(&cookie); |
| 4569 | } |
| 4570 | else |
| 4571 | func_line_end(&cookie); |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 4572 | #endif |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4573 | } |
| 4574 | break; |
| 4575 | |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 4576 | case ISN_DEBUG: |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 4577 | handle_debug(iptr, ectx); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 4578 | break; |
| 4579 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 4580 | case ISN_SHUFFLE: |
| 4581 | { |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 4582 | typval_T tmp_tv; |
| 4583 | int item = iptr->isn_arg.shuffle.shfl_item; |
| 4584 | int up = iptr->isn_arg.shuffle.shfl_up; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 4585 | |
| 4586 | tmp_tv = *STACK_TV_BOT(-item); |
| 4587 | for ( ; up > 0 && item > 1; --up) |
| 4588 | { |
| 4589 | *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1); |
| 4590 | --item; |
| 4591 | } |
| 4592 | *STACK_TV_BOT(-item) = tmp_tv; |
| 4593 | } |
| 4594 | break; |
| 4595 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4596 | case ISN_DROP: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4597 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4598 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4599 | ectx->ec_where.wt_index = 0; |
| 4600 | ectx->ec_where.wt_variable = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4601 | break; |
| 4602 | } |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4603 | continue; |
| 4604 | |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4605 | func_return: |
Bram Moolenaar | 7cbfaa5 | 2020-09-18 21:25:32 +0200 | [diff] [blame] | 4606 | // Restore previous function. If the frame pointer is where we started |
| 4607 | // then there is none and we are done. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4608 | if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4609 | goto done; |
Bram Moolenaar | 7cbfaa5 | 2020-09-18 21:25:32 +0200 | [diff] [blame] | 4610 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4611 | if (func_return(ectx) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4612 | // only fails when out of memory |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4613 | goto theend; |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4614 | continue; |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4615 | |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4616 | on_error: |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 4617 | // Jump here for an error that does not require aborting execution. |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 4618 | // If "emsg_silent" is set then ignore the error, unless it was set |
| 4619 | // when calling the function. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4620 | if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 4621 | && emsg_silent && did_emsg_def == 0) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4622 | { |
| 4623 | // If a sequence of instructions causes an error while ":silent!" |
| 4624 | // was used, restore the stack length and jump ahead to restoring |
| 4625 | // the cmdmod. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4626 | if (ectx->ec_funclocal.floc_restore_cmdmod) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4627 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4628 | while (ectx->ec_stack.ga_len |
| 4629 | > ectx->ec_funclocal.floc_restore_cmdmod_stacklen) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4630 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4631 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4632 | clear_tv(STACK_TV_BOT(0)); |
| 4633 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4634 | while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV) |
| 4635 | ++ectx->ec_iidx; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4636 | } |
Bram Moolenaar | cd030c4 | 2020-10-30 21:49:40 +0100 | [diff] [blame] | 4637 | continue; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4638 | } |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 4639 | on_fatal_error: |
| 4640 | // Jump here for an error that messes up the stack. |
Bram Moolenaar | 171fb92 | 2020-10-28 16:54:47 +0100 | [diff] [blame] | 4641 | // If we are not inside a try-catch started here, abort execution. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4642 | if (trylevel <= ectx->ec_trylevel_at_start) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4643 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | done: |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4647 | ret = OK; |
| 4648 | theend: |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4649 | dict_stack_clear(dict_stack_len_at_start); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4650 | ectx->ec_trylevel_at_start = save_trylevel_at_start; |
| 4651 | return ret; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | /* |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 4655 | * Execute the instructions from a VAR_INSTR typeval and put the result in |
| 4656 | * "rettv". |
| 4657 | * Return OK or FAIL. |
| 4658 | */ |
| 4659 | int |
| 4660 | exe_typval_instr(typval_T *tv, typval_T *rettv) |
| 4661 | { |
| 4662 | ectx_T *ectx = tv->vval.v_instr->instr_ectx; |
| 4663 | isn_T *save_instr = ectx->ec_instr; |
| 4664 | int save_iidx = ectx->ec_iidx; |
| 4665 | int res; |
| 4666 | |
| 4667 | ectx->ec_instr = tv->vval.v_instr->instr_instr; |
| 4668 | res = exec_instructions(ectx); |
| 4669 | if (res == OK) |
| 4670 | { |
| 4671 | *rettv = *STACK_TV_BOT(-1); |
| 4672 | --ectx->ec_stack.ga_len; |
| 4673 | } |
| 4674 | |
| 4675 | ectx->ec_instr = save_instr; |
| 4676 | ectx->ec_iidx = save_iidx; |
| 4677 | |
| 4678 | return res; |
| 4679 | } |
| 4680 | |
| 4681 | /* |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4682 | * Execute the instructions from an ISN_SUBSTITUTE command, which are in |
| 4683 | * "substitute_instr". |
| 4684 | */ |
| 4685 | char_u * |
| 4686 | exe_substitute_instr(void) |
| 4687 | { |
| 4688 | ectx_T *ectx = substitute_instr->subs_ectx; |
| 4689 | isn_T *save_instr = ectx->ec_instr; |
| 4690 | int save_iidx = ectx->ec_iidx; |
| 4691 | char_u *res; |
| 4692 | |
| 4693 | ectx->ec_instr = substitute_instr->subs_instr; |
| 4694 | if (exec_instructions(ectx) == OK) |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 4695 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4696 | typval_T *tv = STACK_TV_BOT(-1); |
| 4697 | |
Bram Moolenaar | 2752360 | 2021-06-05 21:36:19 +0200 | [diff] [blame] | 4698 | res = typval2string(tv, TRUE); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4699 | --ectx->ec_stack.ga_len; |
| 4700 | clear_tv(tv); |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 4701 | } |
| 4702 | else |
| 4703 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4704 | substitute_instr->subs_status = FAIL; |
| 4705 | res = vim_strsave((char_u *)""); |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 4706 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4707 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4708 | ectx->ec_instr = save_instr; |
| 4709 | ectx->ec_iidx = save_iidx; |
| 4710 | |
| 4711 | return res; |
| 4712 | } |
| 4713 | |
| 4714 | /* |
| 4715 | * Call a "def" function from old Vim script. |
| 4716 | * Return OK or FAIL. |
| 4717 | */ |
| 4718 | int |
| 4719 | call_def_function( |
| 4720 | ufunc_T *ufunc, |
| 4721 | int argc_arg, // nr of arguments |
| 4722 | typval_T *argv, // arguments |
| 4723 | partial_T *partial, // optional partial for context |
| 4724 | typval_T *rettv) // return value |
| 4725 | { |
| 4726 | ectx_T ectx; // execution context |
| 4727 | int argc = argc_arg; |
| 4728 | typval_T *tv; |
| 4729 | int idx; |
| 4730 | int ret = FAIL; |
| 4731 | int defcount = ufunc->uf_args.ga_len - argc; |
| 4732 | sctx_T save_current_sctx = current_sctx; |
| 4733 | int did_emsg_before = did_emsg_cumul + did_emsg; |
| 4734 | int save_suppress_errthrow = suppress_errthrow; |
| 4735 | msglist_T **saved_msg_list = NULL; |
| 4736 | msglist_T *private_msg_list = NULL; |
| 4737 | int save_emsg_silent_def = emsg_silent_def; |
| 4738 | int save_did_emsg_def = did_emsg_def; |
| 4739 | int orig_funcdepth; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 4740 | int orig_nesting_level = ex_nesting_level; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4741 | |
| 4742 | // Get pointer to item in the stack. |
| 4743 | #undef STACK_TV |
| 4744 | #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx) |
| 4745 | |
| 4746 | // Get pointer to item at the bottom of the stack, -1 is the bottom. |
| 4747 | #undef STACK_TV_BOT |
| 4748 | #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx) |
| 4749 | |
| 4750 | // Get pointer to a local variable on the stack. Negative for arguments. |
| 4751 | #undef STACK_TV_VAR |
| 4752 | #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx) |
| 4753 | |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 4754 | // Update uf_has_breakpoint if needed. |
| 4755 | update_has_breakpoint(ufunc); |
| 4756 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4757 | if (ufunc->uf_def_status == UF_NOT_COMPILED |
| 4758 | || ufunc->uf_def_status == UF_COMPILE_ERROR |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 4759 | || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)) |
| 4760 | && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4761 | == FAIL)) |
| 4762 | { |
| 4763 | if (did_emsg_cumul + did_emsg == did_emsg_before) |
| 4764 | semsg(_(e_function_is_not_compiled_str), |
| 4765 | printable_func_name(ufunc)); |
| 4766 | return FAIL; |
| 4767 | } |
| 4768 | |
| 4769 | { |
| 4770 | // Check the function was really compiled. |
| 4771 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 4772 | + ufunc->uf_dfunc_idx; |
| 4773 | if (INSTRUCTIONS(dfunc) == NULL) |
| 4774 | { |
| 4775 | iemsg("using call_def_function() on not compiled function"); |
| 4776 | return FAIL; |
| 4777 | } |
| 4778 | } |
| 4779 | |
| 4780 | // If depth of calling is getting too high, don't execute the function. |
| 4781 | orig_funcdepth = funcdepth_get(); |
| 4782 | if (funcdepth_increment() == FAIL) |
| 4783 | return FAIL; |
| 4784 | |
| 4785 | CLEAR_FIELD(ectx); |
| 4786 | ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx; |
| 4787 | ga_init2(&ectx.ec_stack, sizeof(typval_T), 500); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4788 | if (GA_GROW_FAILS(&ectx.ec_stack, 20)) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4789 | { |
| 4790 | funcdepth_decrement(); |
| 4791 | return FAIL; |
| 4792 | } |
| 4793 | ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10); |
| 4794 | ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10); |
| 4795 | ectx.ec_did_emsg_before = did_emsg_before; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 4796 | ++ex_nesting_level; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4797 | |
| 4798 | idx = argc - ufunc->uf_args.ga_len; |
| 4799 | if (idx > 0 && ufunc->uf_va_name == NULL) |
| 4800 | { |
| 4801 | if (idx == 1) |
| 4802 | emsg(_(e_one_argument_too_many)); |
| 4803 | else |
| 4804 | semsg(_(e_nr_arguments_too_many), idx); |
| 4805 | goto failed_early; |
| 4806 | } |
Bram Moolenaar | c6d7153 | 2021-06-05 18:49:38 +0200 | [diff] [blame] | 4807 | idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len; |
| 4808 | if (idx < 0) |
Bram Moolenaar | 8da6d6d | 2021-06-05 18:15:09 +0200 | [diff] [blame] | 4809 | { |
| 4810 | if (idx == -1) |
| 4811 | emsg(_(e_one_argument_too_few)); |
| 4812 | else |
| 4813 | semsg(_(e_nr_arguments_too_few), -idx); |
| 4814 | goto failed_early; |
| 4815 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4816 | |
| 4817 | // Put arguments on the stack, but no more than what the function expects. |
| 4818 | // A lambda can be called with more arguments than it uses. |
| 4819 | for (idx = 0; idx < argc |
| 4820 | && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len); |
| 4821 | ++idx) |
| 4822 | { |
| 4823 | if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len |
| 4824 | && argv[idx].v_type == VAR_SPECIAL |
| 4825 | && argv[idx].vval.v_number == VVAL_NONE) |
| 4826 | { |
| 4827 | // Use the default value. |
| 4828 | STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; |
| 4829 | } |
| 4830 | else |
| 4831 | { |
| 4832 | if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len |
| 4833 | && check_typval_arg_type( |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 4834 | ufunc->uf_arg_types[idx], &argv[idx], |
| 4835 | NULL, idx + 1) == FAIL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4836 | goto failed_early; |
| 4837 | copy_tv(&argv[idx], STACK_TV_BOT(0)); |
| 4838 | } |
| 4839 | ++ectx.ec_stack.ga_len; |
| 4840 | } |
| 4841 | |
| 4842 | // Turn varargs into a list. Empty list if no args. |
| 4843 | if (ufunc->uf_va_name != NULL) |
| 4844 | { |
| 4845 | int vararg_count = argc - ufunc->uf_args.ga_len; |
| 4846 | |
| 4847 | if (vararg_count < 0) |
| 4848 | vararg_count = 0; |
| 4849 | else |
| 4850 | argc -= vararg_count; |
| 4851 | if (exe_newlist(vararg_count, &ectx) == FAIL) |
| 4852 | goto failed_early; |
| 4853 | |
| 4854 | // Check the type of the list items. |
| 4855 | tv = STACK_TV_BOT(-1); |
| 4856 | if (ufunc->uf_va_type != NULL |
| 4857 | && ufunc->uf_va_type != &t_list_any |
| 4858 | && ufunc->uf_va_type->tt_member != &t_any |
| 4859 | && tv->vval.v_list != NULL) |
| 4860 | { |
| 4861 | type_T *expected = ufunc->uf_va_type->tt_member; |
| 4862 | listitem_T *li = tv->vval.v_list->lv_first; |
| 4863 | |
| 4864 | for (idx = 0; idx < vararg_count; ++idx) |
| 4865 | { |
| 4866 | if (check_typval_arg_type(expected, &li->li_tv, |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 4867 | NULL, argc + idx + 1) == FAIL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4868 | goto failed_early; |
| 4869 | li = li->li_next; |
| 4870 | } |
| 4871 | } |
| 4872 | |
| 4873 | if (defcount > 0) |
| 4874 | // Move varargs list to below missing default arguments. |
| 4875 | *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1); |
| 4876 | --ectx.ec_stack.ga_len; |
| 4877 | } |
| 4878 | |
| 4879 | // Make space for omitted arguments, will store default value below. |
| 4880 | // Any varargs list goes after them. |
| 4881 | if (defcount > 0) |
| 4882 | for (idx = 0; idx < defcount; ++idx) |
| 4883 | { |
| 4884 | STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; |
| 4885 | ++ectx.ec_stack.ga_len; |
| 4886 | } |
| 4887 | if (ufunc->uf_va_name != NULL) |
| 4888 | ++ectx.ec_stack.ga_len; |
| 4889 | |
| 4890 | // Frame pointer points to just after arguments. |
| 4891 | ectx.ec_frame_idx = ectx.ec_stack.ga_len; |
| 4892 | ectx.ec_initial_frame_idx = ectx.ec_frame_idx; |
| 4893 | |
| 4894 | { |
| 4895 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 4896 | + ufunc->uf_dfunc_idx; |
| 4897 | ufunc_T *base_ufunc = dfunc->df_ufunc; |
| 4898 | |
| 4899 | // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done |
| 4900 | // by copy_func(). |
| 4901 | if (partial != NULL || base_ufunc->uf_partial != NULL) |
| 4902 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 4903 | ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T); |
| 4904 | if (ectx.ec_outer_ref == NULL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4905 | goto failed_early; |
| 4906 | if (partial != NULL) |
| 4907 | { |
| 4908 | if (partial->pt_outer.out_stack == NULL && current_ectx != NULL) |
| 4909 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 4910 | if (current_ectx->ec_outer_ref != NULL |
| 4911 | && current_ectx->ec_outer_ref->or_outer != NULL) |
| 4912 | ectx.ec_outer_ref->or_outer = |
| 4913 | current_ectx->ec_outer_ref->or_outer; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4914 | } |
| 4915 | else |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 4916 | { |
| 4917 | ectx.ec_outer_ref->or_outer = &partial->pt_outer; |
| 4918 | ++partial->pt_refcount; |
| 4919 | ectx.ec_outer_ref->or_partial = partial; |
| 4920 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4921 | } |
| 4922 | else |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 4923 | { |
| 4924 | ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer; |
| 4925 | ++base_ufunc->uf_partial->pt_refcount; |
| 4926 | ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial; |
| 4927 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4928 | } |
| 4929 | } |
| 4930 | |
| 4931 | // dummy frame entries |
| 4932 | for (idx = 0; idx < STACK_FRAME_SIZE; ++idx) |
| 4933 | { |
| 4934 | STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN; |
| 4935 | ++ectx.ec_stack.ga_len; |
| 4936 | } |
| 4937 | |
| 4938 | { |
| 4939 | // Reserve space for local variables and any closure reference count. |
| 4940 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 4941 | + ufunc->uf_dfunc_idx; |
| 4942 | |
| 4943 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
| 4944 | STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN; |
| 4945 | ectx.ec_stack.ga_len += dfunc->df_varcount; |
| 4946 | if (dfunc->df_has_closure) |
| 4947 | { |
| 4948 | STACK_TV_VAR(idx)->v_type = VAR_NUMBER; |
| 4949 | STACK_TV_VAR(idx)->vval.v_number = 0; |
| 4950 | ++ectx.ec_stack.ga_len; |
| 4951 | } |
| 4952 | |
| 4953 | ectx.ec_instr = INSTRUCTIONS(dfunc); |
| 4954 | } |
| 4955 | |
| 4956 | // Following errors are in the function, not the caller. |
| 4957 | // Commands behave like vim9script. |
| 4958 | estack_push_ufunc(ufunc, 1); |
| 4959 | current_sctx = ufunc->uf_script_ctx; |
| 4960 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 4961 | |
| 4962 | // Use a specific location for storing error messages to be converted to an |
| 4963 | // exception. |
| 4964 | saved_msg_list = msg_list; |
| 4965 | msg_list = &private_msg_list; |
| 4966 | |
| 4967 | // Do turn errors into exceptions. |
| 4968 | suppress_errthrow = FALSE; |
| 4969 | |
| 4970 | // Do not delete the function while executing it. |
| 4971 | ++ufunc->uf_calls; |
| 4972 | |
| 4973 | // When ":silent!" was used before calling then we still abort the |
| 4974 | // function. If ":silent!" is used in the function then we don't. |
| 4975 | emsg_silent_def = emsg_silent; |
| 4976 | did_emsg_def = 0; |
| 4977 | |
| 4978 | ectx.ec_where.wt_index = 0; |
| 4979 | ectx.ec_where.wt_variable = FALSE; |
| 4980 | |
| 4981 | // Execute the instructions until done. |
| 4982 | ret = exec_instructions(&ectx); |
| 4983 | if (ret == OK) |
| 4984 | { |
| 4985 | // function finished, get result from the stack. |
| 4986 | if (ufunc->uf_ret_type == &t_void) |
| 4987 | { |
| 4988 | rettv->v_type = VAR_VOID; |
| 4989 | } |
| 4990 | else |
| 4991 | { |
| 4992 | tv = STACK_TV_BOT(-1); |
| 4993 | *rettv = *tv; |
| 4994 | tv->v_type = VAR_UNKNOWN; |
| 4995 | } |
| 4996 | } |
| 4997 | |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 4998 | // When failed need to unwind the call stack. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4999 | while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx) |
| 5000 | func_return(&ectx); |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 5001 | |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 5002 | // Deal with any remaining closures, they may be in use somewhere. |
| 5003 | if (ectx.ec_funcrefs.ga_len > 0) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 5004 | { |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 5005 | handle_closure_in_use(&ectx, FALSE); |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 5006 | ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed? |
| 5007 | } |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 5008 | |
Bram Moolenaar | ee8580e | 2020-08-28 17:19:07 +0200 | [diff] [blame] | 5009 | estack_pop(); |
| 5010 | current_sctx = save_current_sctx; |
| 5011 | |
Bram Moolenaar | 2336c37 | 2021-12-06 15:06:54 +0000 | [diff] [blame] | 5012 | if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0) |
| 5013 | // Function was unreferenced while being used, free it now. |
| 5014 | func_clear_free(ufunc, FALSE); |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 5015 | |
Bram Moolenaar | 352134b | 2020-10-17 22:04:08 +0200 | [diff] [blame] | 5016 | if (*msg_list != NULL && saved_msg_list != NULL) |
| 5017 | { |
| 5018 | msglist_T **plist = saved_msg_list; |
| 5019 | |
| 5020 | // Append entries from the current msg_list (uncaught exceptions) to |
| 5021 | // the saved msg_list. |
| 5022 | while (*plist != NULL) |
| 5023 | plist = &(*plist)->next; |
| 5024 | |
| 5025 | *plist = *msg_list; |
| 5026 | } |
| 5027 | msg_list = saved_msg_list; |
| 5028 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5029 | if (ectx.ec_funclocal.floc_restore_cmdmod) |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 5030 | { |
| 5031 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 5032 | undo_cmdmod(&cmdmod); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5033 | cmdmod = ectx.ec_funclocal.floc_save_cmdmod; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 5034 | } |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 5035 | emsg_silent_def = save_emsg_silent_def; |
| 5036 | did_emsg_def += save_did_emsg_def; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 5037 | |
Bram Moolenaar | ee8580e | 2020-08-28 17:19:07 +0200 | [diff] [blame] | 5038 | failed_early: |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 5039 | // Free all local variables, but not arguments. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5040 | for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) |
| 5041 | clear_tv(STACK_TV(idx)); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5042 | ex_nesting_level = orig_nesting_level; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 5043 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5044 | vim_free(ectx.ec_stack.ga_data); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5045 | vim_free(ectx.ec_trystack.ga_data); |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5046 | if (ectx.ec_outer_ref != NULL) |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 5047 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5048 | if (ectx.ec_outer_ref->or_outer_allocated) |
| 5049 | vim_free(ectx.ec_outer_ref->or_outer); |
| 5050 | partial_unref(ectx.ec_outer_ref->or_partial); |
| 5051 | vim_free(ectx.ec_outer_ref); |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 5052 | } |
| 5053 | |
Bram Moolenaar | 77e5dcc | 2020-09-17 21:29:03 +0200 | [diff] [blame] | 5054 | // Not sure if this is necessary. |
| 5055 | suppress_errthrow = save_suppress_errthrow; |
| 5056 | |
Bram Moolenaar | b5841b9 | 2021-07-15 18:09:53 +0200 | [diff] [blame] | 5057 | if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before |
| 5058 | && !need_rethrow) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5059 | semsg(_(e_unknown_error_while_executing_str), |
Bram Moolenaar | 682d0a1 | 2020-07-19 20:48:59 +0200 | [diff] [blame] | 5060 | printable_func_name(ufunc)); |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 5061 | funcdepth_restore(orig_funcdepth); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5062 | return ret; |
| 5063 | } |
| 5064 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5065 | /* |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5066 | * List instructions "instr" up to "instr_count" or until ISN_FINISH. |
| 5067 | * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE. |
| 5068 | * "pfx" is prefixed to every line. |
| 5069 | */ |
| 5070 | static void |
| 5071 | list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc) |
| 5072 | { |
| 5073 | int line_idx = 0; |
| 5074 | int prev_current = 0; |
| 5075 | int current; |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 5076 | int def_arg_idx = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5077 | |
| 5078 | for (current = 0; current < instr_count; ++current) |
| 5079 | { |
| 5080 | isn_T *iptr = &instr[current]; |
| 5081 | char *line; |
| 5082 | |
| 5083 | if (ufunc != NULL) |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 5084 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5085 | while (line_idx < iptr->isn_lnum |
| 5086 | && line_idx < ufunc->uf_lines.ga_len) |
| 5087 | { |
| 5088 | if (current > prev_current) |
| 5089 | { |
| 5090 | msg_puts("\n\n"); |
| 5091 | prev_current = current; |
| 5092 | } |
| 5093 | line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; |
| 5094 | if (line != NULL) |
| 5095 | msg(line); |
| 5096 | } |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 5097 | if (iptr->isn_type == ISN_JUMP_IF_ARG_SET) |
| 5098 | { |
| 5099 | int first_def_arg = ufunc->uf_args.ga_len |
| 5100 | - ufunc->uf_def_args.ga_len; |
| 5101 | |
| 5102 | if (def_arg_idx > 0) |
| 5103 | msg_puts("\n\n"); |
| 5104 | msg_start(); |
| 5105 | msg_puts(" "); |
| 5106 | msg_puts(((char **)(ufunc->uf_args.ga_data))[ |
| 5107 | first_def_arg + def_arg_idx]); |
| 5108 | msg_puts(" = "); |
| 5109 | msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]); |
| 5110 | msg_clr_eos(); |
| 5111 | msg_end(); |
| 5112 | } |
| 5113 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5114 | |
| 5115 | switch (iptr->isn_type) |
| 5116 | { |
| 5117 | case ISN_EXEC: |
| 5118 | smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string); |
| 5119 | break; |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 5120 | case ISN_EXEC_SPLIT: |
| 5121 | smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string); |
| 5122 | break; |
Bram Moolenaar | e4eed8c | 2021-12-01 15:22:56 +0000 | [diff] [blame] | 5123 | case ISN_EXECRANGE: |
| 5124 | smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string); |
| 5125 | break; |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 5126 | case ISN_LEGACY_EVAL: |
| 5127 | smsg("%s%4d EVAL legacy %s", pfx, current, |
| 5128 | iptr->isn_arg.string); |
| 5129 | break; |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 5130 | case ISN_REDIRSTART: |
| 5131 | smsg("%s%4d REDIR", pfx, current); |
| 5132 | break; |
| 5133 | case ISN_REDIREND: |
| 5134 | smsg("%s%4d REDIR END%s", pfx, current, |
| 5135 | iptr->isn_arg.number ? " append" : ""); |
| 5136 | break; |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5137 | case ISN_CEXPR_AUCMD: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 5138 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5139 | smsg("%s%4d CEXPR pre %s", pfx, current, |
| 5140 | cexpr_get_auname(iptr->isn_arg.number)); |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 5141 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5142 | break; |
| 5143 | case ISN_CEXPR_CORE: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 5144 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5145 | { |
| 5146 | cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref; |
| 5147 | |
| 5148 | smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current, |
| 5149 | cexpr_get_auname(cer->cer_cmdidx), |
| 5150 | cer->cer_forceit ? "!" : "", |
| 5151 | cer->cer_cmdline); |
| 5152 | } |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 5153 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5154 | break; |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 5155 | case ISN_INSTR: |
| 5156 | { |
| 5157 | smsg("%s%4d INSTR", pfx, current); |
| 5158 | list_instructions(" ", iptr->isn_arg.instr, |
| 5159 | INT_MAX, NULL); |
| 5160 | msg(" -------------"); |
| 5161 | } |
| 5162 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5163 | case ISN_SUBSTITUTE: |
| 5164 | { |
| 5165 | subs_T *subs = &iptr->isn_arg.subs; |
| 5166 | |
| 5167 | smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd); |
| 5168 | list_instructions(" ", subs->subs_instr, INT_MAX, NULL); |
| 5169 | msg(" -------------"); |
| 5170 | } |
| 5171 | break; |
| 5172 | case ISN_EXECCONCAT: |
| 5173 | smsg("%s%4d EXECCONCAT %lld", pfx, current, |
| 5174 | (varnumber_T)iptr->isn_arg.number); |
| 5175 | break; |
| 5176 | case ISN_ECHO: |
| 5177 | { |
| 5178 | echo_T *echo = &iptr->isn_arg.echo; |
| 5179 | |
| 5180 | smsg("%s%4d %s %d", pfx, current, |
| 5181 | echo->echo_with_white ? "ECHO" : "ECHON", |
| 5182 | echo->echo_count); |
| 5183 | } |
| 5184 | break; |
| 5185 | case ISN_EXECUTE: |
| 5186 | smsg("%s%4d EXECUTE %lld", pfx, current, |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 5187 | (varnumber_T)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5188 | break; |
| 5189 | case ISN_ECHOMSG: |
| 5190 | smsg("%s%4d ECHOMSG %lld", pfx, current, |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 5191 | (varnumber_T)(iptr->isn_arg.number)); |
| 5192 | break; |
| 5193 | case ISN_ECHOCONSOLE: |
| 5194 | smsg("%s%4d ECHOCONSOLE %lld", pfx, current, |
| 5195 | (varnumber_T)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5196 | break; |
| 5197 | case ISN_ECHOERR: |
| 5198 | smsg("%s%4d ECHOERR %lld", pfx, current, |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 5199 | (varnumber_T)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5200 | break; |
| 5201 | case ISN_LOAD: |
| 5202 | { |
| 5203 | if (iptr->isn_arg.number < 0) |
| 5204 | smsg("%s%4d LOAD arg[%lld]", pfx, current, |
| 5205 | (varnumber_T)(iptr->isn_arg.number |
| 5206 | + STACK_FRAME_SIZE)); |
| 5207 | else |
| 5208 | smsg("%s%4d LOAD $%lld", pfx, current, |
| 5209 | (varnumber_T)(iptr->isn_arg.number)); |
| 5210 | } |
| 5211 | break; |
| 5212 | case ISN_LOADOUTER: |
| 5213 | { |
| 5214 | if (iptr->isn_arg.number < 0) |
| 5215 | smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current, |
| 5216 | iptr->isn_arg.outer.outer_depth, |
| 5217 | iptr->isn_arg.outer.outer_idx |
| 5218 | + STACK_FRAME_SIZE); |
| 5219 | else |
| 5220 | smsg("%s%4d LOADOUTER level %d $%d", pfx, current, |
| 5221 | iptr->isn_arg.outer.outer_depth, |
| 5222 | iptr->isn_arg.outer.outer_idx); |
| 5223 | } |
| 5224 | break; |
| 5225 | case ISN_LOADV: |
| 5226 | smsg("%s%4d LOADV v:%s", pfx, current, |
| 5227 | get_vim_var_name(iptr->isn_arg.number)); |
| 5228 | break; |
| 5229 | case ISN_LOADSCRIPT: |
| 5230 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5231 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 5232 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
| 5233 | svar_T *sv; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5234 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5235 | sv = get_script_svar(sref, -1); |
| 5236 | if (sv == NULL) |
| 5237 | smsg("%s%4d LOADSCRIPT [deleted] from %s", |
| 5238 | pfx, current, si->sn_name); |
| 5239 | else |
| 5240 | smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5241 | sv->sv_name, |
| 5242 | sref->sref_idx, |
| 5243 | si->sn_name); |
| 5244 | } |
| 5245 | break; |
| 5246 | case ISN_LOADS: |
| 5247 | { |
| 5248 | scriptitem_T *si = SCRIPT_ITEM( |
| 5249 | iptr->isn_arg.loadstore.ls_sid); |
| 5250 | |
| 5251 | smsg("%s%4d LOADS s:%s from %s", pfx, current, |
| 5252 | iptr->isn_arg.loadstore.ls_name, si->sn_name); |
| 5253 | } |
| 5254 | break; |
| 5255 | case ISN_LOADAUTO: |
| 5256 | smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string); |
| 5257 | break; |
| 5258 | case ISN_LOADG: |
| 5259 | smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string); |
| 5260 | break; |
| 5261 | case ISN_LOADB: |
| 5262 | smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string); |
| 5263 | break; |
| 5264 | case ISN_LOADW: |
| 5265 | smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string); |
| 5266 | break; |
| 5267 | case ISN_LOADT: |
| 5268 | smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string); |
| 5269 | break; |
| 5270 | case ISN_LOADGDICT: |
| 5271 | smsg("%s%4d LOAD g:", pfx, current); |
| 5272 | break; |
| 5273 | case ISN_LOADBDICT: |
| 5274 | smsg("%s%4d LOAD b:", pfx, current); |
| 5275 | break; |
| 5276 | case ISN_LOADWDICT: |
| 5277 | smsg("%s%4d LOAD w:", pfx, current); |
| 5278 | break; |
| 5279 | case ISN_LOADTDICT: |
| 5280 | smsg("%s%4d LOAD t:", pfx, current); |
| 5281 | break; |
| 5282 | case ISN_LOADOPT: |
| 5283 | smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string); |
| 5284 | break; |
| 5285 | case ISN_LOADENV: |
| 5286 | smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string); |
| 5287 | break; |
| 5288 | case ISN_LOADREG: |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5289 | smsg("%s%4d LOADREG @%c", pfx, current, |
| 5290 | (int)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5291 | break; |
| 5292 | |
| 5293 | case ISN_STORE: |
| 5294 | if (iptr->isn_arg.number < 0) |
| 5295 | smsg("%s%4d STORE arg[%lld]", pfx, current, |
| 5296 | iptr->isn_arg.number + STACK_FRAME_SIZE); |
| 5297 | else |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5298 | smsg("%s%4d STORE $%lld", pfx, current, |
| 5299 | iptr->isn_arg.number); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5300 | break; |
| 5301 | case ISN_STOREOUTER: |
| 5302 | { |
| 5303 | if (iptr->isn_arg.number < 0) |
| 5304 | smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current, |
| 5305 | iptr->isn_arg.outer.outer_depth, |
| 5306 | iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE); |
| 5307 | else |
| 5308 | smsg("%s%4d STOREOUTER level %d $%d", pfx, current, |
| 5309 | iptr->isn_arg.outer.outer_depth, |
| 5310 | iptr->isn_arg.outer.outer_idx); |
| 5311 | } |
| 5312 | break; |
| 5313 | case ISN_STOREV: |
| 5314 | smsg("%s%4d STOREV v:%s", pfx, current, |
| 5315 | get_vim_var_name(iptr->isn_arg.number)); |
| 5316 | break; |
| 5317 | case ISN_STOREAUTO: |
| 5318 | smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string); |
| 5319 | break; |
| 5320 | case ISN_STOREG: |
| 5321 | smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string); |
| 5322 | break; |
| 5323 | case ISN_STOREB: |
| 5324 | smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string); |
| 5325 | break; |
| 5326 | case ISN_STOREW: |
| 5327 | smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string); |
| 5328 | break; |
| 5329 | case ISN_STORET: |
| 5330 | smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string); |
| 5331 | break; |
| 5332 | case ISN_STORES: |
| 5333 | { |
| 5334 | scriptitem_T *si = SCRIPT_ITEM( |
| 5335 | iptr->isn_arg.loadstore.ls_sid); |
| 5336 | |
| 5337 | smsg("%s%4d STORES %s in %s", pfx, current, |
| 5338 | iptr->isn_arg.loadstore.ls_name, si->sn_name); |
| 5339 | } |
| 5340 | break; |
| 5341 | case ISN_STORESCRIPT: |
| 5342 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5343 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 5344 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
| 5345 | svar_T *sv; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5346 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5347 | sv = get_script_svar(sref, -1); |
| 5348 | if (sv == NULL) |
| 5349 | smsg("%s%4d STORESCRIPT [deleted] in %s", |
| 5350 | pfx, current, si->sn_name); |
| 5351 | else |
| 5352 | smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5353 | sv->sv_name, |
| 5354 | sref->sref_idx, |
| 5355 | si->sn_name); |
| 5356 | } |
| 5357 | break; |
| 5358 | case ISN_STOREOPT: |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 5359 | case ISN_STOREFUNCOPT: |
| 5360 | smsg("%s%4d %s &%s", pfx, current, |
| 5361 | iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT", |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5362 | iptr->isn_arg.storeopt.so_name); |
| 5363 | break; |
| 5364 | case ISN_STOREENV: |
| 5365 | smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string); |
| 5366 | break; |
| 5367 | case ISN_STOREREG: |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5368 | smsg("%s%4d STOREREG @%c", pfx, current, |
| 5369 | (int)iptr->isn_arg.number); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5370 | break; |
| 5371 | case ISN_STORENR: |
| 5372 | smsg("%s%4d STORE %lld in $%d", pfx, current, |
| 5373 | iptr->isn_arg.storenr.stnr_val, |
| 5374 | iptr->isn_arg.storenr.stnr_idx); |
| 5375 | break; |
| 5376 | |
| 5377 | case ISN_STOREINDEX: |
| 5378 | smsg("%s%4d STOREINDEX %s", pfx, current, |
| 5379 | vartype_name(iptr->isn_arg.vartype)); |
| 5380 | break; |
| 5381 | |
| 5382 | case ISN_STORERANGE: |
| 5383 | smsg("%s%4d STORERANGE", pfx, current); |
| 5384 | break; |
| 5385 | |
| 5386 | // constants |
| 5387 | case ISN_PUSHNR: |
| 5388 | smsg("%s%4d PUSHNR %lld", pfx, current, |
| 5389 | (varnumber_T)(iptr->isn_arg.number)); |
| 5390 | break; |
| 5391 | case ISN_PUSHBOOL: |
| 5392 | case ISN_PUSHSPEC: |
| 5393 | smsg("%s%4d PUSH %s", pfx, current, |
| 5394 | get_var_special_name(iptr->isn_arg.number)); |
| 5395 | break; |
| 5396 | case ISN_PUSHF: |
| 5397 | #ifdef FEAT_FLOAT |
| 5398 | smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber); |
| 5399 | #endif |
| 5400 | break; |
| 5401 | case ISN_PUSHS: |
| 5402 | smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string); |
| 5403 | break; |
| 5404 | case ISN_PUSHBLOB: |
| 5405 | { |
| 5406 | char_u *r; |
| 5407 | char_u numbuf[NUMBUFLEN]; |
| 5408 | char_u *tofree; |
| 5409 | |
| 5410 | r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); |
| 5411 | smsg("%s%4d PUSHBLOB %s", pfx, current, r); |
| 5412 | vim_free(tofree); |
| 5413 | } |
| 5414 | break; |
| 5415 | case ISN_PUSHFUNC: |
| 5416 | { |
| 5417 | char *name = (char *)iptr->isn_arg.string; |
| 5418 | |
| 5419 | smsg("%s%4d PUSHFUNC \"%s\"", pfx, current, |
| 5420 | name == NULL ? "[none]" : name); |
| 5421 | } |
| 5422 | break; |
| 5423 | case ISN_PUSHCHANNEL: |
| 5424 | #ifdef FEAT_JOB_CHANNEL |
| 5425 | { |
| 5426 | channel_T *channel = iptr->isn_arg.channel; |
| 5427 | |
| 5428 | smsg("%s%4d PUSHCHANNEL %d", pfx, current, |
| 5429 | channel == NULL ? 0 : channel->ch_id); |
| 5430 | } |
| 5431 | #endif |
| 5432 | break; |
| 5433 | case ISN_PUSHJOB: |
| 5434 | #ifdef FEAT_JOB_CHANNEL |
| 5435 | { |
| 5436 | typval_T tv; |
| 5437 | char_u *name; |
Bram Moolenaar | 1328bde | 2021-06-05 20:51:38 +0200 | [diff] [blame] | 5438 | char_u buf[NUMBUFLEN]; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5439 | |
| 5440 | tv.v_type = VAR_JOB; |
| 5441 | tv.vval.v_job = iptr->isn_arg.job; |
Bram Moolenaar | 1328bde | 2021-06-05 20:51:38 +0200 | [diff] [blame] | 5442 | name = job_to_string_buf(&tv, buf); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5443 | smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name); |
| 5444 | } |
| 5445 | #endif |
| 5446 | break; |
| 5447 | case ISN_PUSHEXC: |
| 5448 | smsg("%s%4d PUSH v:exception", pfx, current); |
| 5449 | break; |
| 5450 | case ISN_UNLET: |
| 5451 | smsg("%s%4d UNLET%s %s", pfx, current, |
| 5452 | iptr->isn_arg.unlet.ul_forceit ? "!" : "", |
| 5453 | iptr->isn_arg.unlet.ul_name); |
| 5454 | break; |
| 5455 | case ISN_UNLETENV: |
| 5456 | smsg("%s%4d UNLETENV%s $%s", pfx, current, |
| 5457 | iptr->isn_arg.unlet.ul_forceit ? "!" : "", |
| 5458 | iptr->isn_arg.unlet.ul_name); |
| 5459 | break; |
| 5460 | case ISN_UNLETINDEX: |
| 5461 | smsg("%s%4d UNLETINDEX", pfx, current); |
| 5462 | break; |
| 5463 | case ISN_UNLETRANGE: |
| 5464 | smsg("%s%4d UNLETRANGE", pfx, current); |
| 5465 | break; |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 5466 | case ISN_LOCKUNLOCK: |
| 5467 | smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string); |
| 5468 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5469 | case ISN_LOCKCONST: |
| 5470 | smsg("%s%4d LOCKCONST", pfx, current); |
| 5471 | break; |
| 5472 | case ISN_NEWLIST: |
| 5473 | smsg("%s%4d NEWLIST size %lld", pfx, current, |
| 5474 | (varnumber_T)(iptr->isn_arg.number)); |
| 5475 | break; |
| 5476 | case ISN_NEWDICT: |
| 5477 | smsg("%s%4d NEWDICT size %lld", pfx, current, |
| 5478 | (varnumber_T)(iptr->isn_arg.number)); |
| 5479 | break; |
| 5480 | |
| 5481 | // function call |
| 5482 | case ISN_BCALL: |
| 5483 | { |
| 5484 | cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; |
| 5485 | |
| 5486 | smsg("%s%4d BCALL %s(argc %d)", pfx, current, |
| 5487 | internal_func_name(cbfunc->cbf_idx), |
| 5488 | cbfunc->cbf_argcount); |
| 5489 | } |
| 5490 | break; |
| 5491 | case ISN_DCALL: |
| 5492 | { |
| 5493 | cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; |
| 5494 | dfunc_T *df = ((dfunc_T *)def_functions.ga_data) |
| 5495 | + cdfunc->cdf_idx; |
| 5496 | |
| 5497 | smsg("%s%4d DCALL %s(argc %d)", pfx, current, |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5498 | printable_func_name(df->df_ufunc), |
| 5499 | cdfunc->cdf_argcount); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5500 | } |
| 5501 | break; |
| 5502 | case ISN_UCALL: |
| 5503 | { |
| 5504 | cufunc_T *cufunc = &iptr->isn_arg.ufunc; |
| 5505 | |
| 5506 | smsg("%s%4d UCALL %s(argc %d)", pfx, current, |
| 5507 | cufunc->cuf_name, cufunc->cuf_argcount); |
| 5508 | } |
| 5509 | break; |
| 5510 | case ISN_PCALL: |
| 5511 | { |
| 5512 | cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; |
| 5513 | |
| 5514 | smsg("%s%4d PCALL%s (argc %d)", pfx, current, |
| 5515 | cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); |
| 5516 | } |
| 5517 | break; |
| 5518 | case ISN_PCALL_END: |
| 5519 | smsg("%s%4d PCALL end", pfx, current); |
| 5520 | break; |
| 5521 | case ISN_RETURN: |
| 5522 | smsg("%s%4d RETURN", pfx, current); |
| 5523 | break; |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 5524 | case ISN_RETURN_VOID: |
| 5525 | smsg("%s%4d RETURN void", pfx, current); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5526 | break; |
| 5527 | case ISN_FUNCREF: |
| 5528 | { |
| 5529 | funcref_T *funcref = &iptr->isn_arg.funcref; |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 5530 | char_u *name; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5531 | |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 5532 | if (funcref->fr_func_name == NULL) |
| 5533 | { |
| 5534 | dfunc_T *df = ((dfunc_T *)def_functions.ga_data) |
| 5535 | + funcref->fr_dfunc_idx; |
| 5536 | name = df->df_ufunc->uf_name; |
| 5537 | } |
| 5538 | else |
| 5539 | name = funcref->fr_func_name; |
| 5540 | smsg("%s%4d FUNCREF %s", pfx, current, name); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5541 | } |
| 5542 | break; |
| 5543 | |
| 5544 | case ISN_NEWFUNC: |
| 5545 | { |
| 5546 | newfunc_T *newfunc = &iptr->isn_arg.newfunc; |
| 5547 | |
| 5548 | smsg("%s%4d NEWFUNC %s %s", pfx, current, |
| 5549 | newfunc->nf_lambda, newfunc->nf_global); |
| 5550 | } |
| 5551 | break; |
| 5552 | |
| 5553 | case ISN_DEF: |
| 5554 | { |
| 5555 | char_u *name = iptr->isn_arg.string; |
| 5556 | |
| 5557 | smsg("%s%4d DEF %s", pfx, current, |
| 5558 | name == NULL ? (char_u *)"" : name); |
| 5559 | } |
| 5560 | break; |
| 5561 | |
| 5562 | case ISN_JUMP: |
| 5563 | { |
| 5564 | char *when = "?"; |
| 5565 | |
| 5566 | switch (iptr->isn_arg.jump.jump_when) |
| 5567 | { |
| 5568 | case JUMP_ALWAYS: |
| 5569 | when = "JUMP"; |
| 5570 | break; |
Bram Moolenaar | 1a7ee4d | 2021-09-16 16:15:07 +0200 | [diff] [blame] | 5571 | case JUMP_NEVER: |
| 5572 | iemsg("JUMP_NEVER should not be used"); |
| 5573 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5574 | case JUMP_AND_KEEP_IF_TRUE: |
| 5575 | when = "JUMP_AND_KEEP_IF_TRUE"; |
| 5576 | break; |
| 5577 | case JUMP_IF_FALSE: |
| 5578 | when = "JUMP_IF_FALSE"; |
| 5579 | break; |
| 5580 | case JUMP_AND_KEEP_IF_FALSE: |
| 5581 | when = "JUMP_AND_KEEP_IF_FALSE"; |
| 5582 | break; |
| 5583 | case JUMP_IF_COND_FALSE: |
| 5584 | when = "JUMP_IF_COND_FALSE"; |
| 5585 | break; |
| 5586 | case JUMP_IF_COND_TRUE: |
| 5587 | when = "JUMP_IF_COND_TRUE"; |
| 5588 | break; |
| 5589 | } |
| 5590 | smsg("%s%4d %s -> %d", pfx, current, when, |
| 5591 | iptr->isn_arg.jump.jump_where); |
| 5592 | } |
| 5593 | break; |
| 5594 | |
| 5595 | case ISN_JUMP_IF_ARG_SET: |
| 5596 | smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current, |
| 5597 | iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE, |
| 5598 | iptr->isn_arg.jump.jump_where); |
| 5599 | break; |
| 5600 | |
| 5601 | case ISN_FOR: |
| 5602 | { |
| 5603 | forloop_T *forloop = &iptr->isn_arg.forloop; |
| 5604 | |
| 5605 | smsg("%s%4d FOR $%d -> %d", pfx, current, |
| 5606 | forloop->for_idx, forloop->for_end); |
| 5607 | } |
| 5608 | break; |
| 5609 | |
| 5610 | case ISN_TRY: |
| 5611 | { |
| 5612 | try_T *try = &iptr->isn_arg.try; |
| 5613 | |
| 5614 | if (try->try_ref->try_finally == 0) |
| 5615 | smsg("%s%4d TRY catch -> %d, endtry -> %d", |
| 5616 | pfx, current, |
| 5617 | try->try_ref->try_catch, |
| 5618 | try->try_ref->try_endtry); |
| 5619 | else |
| 5620 | smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d", |
| 5621 | pfx, current, |
| 5622 | try->try_ref->try_catch, |
| 5623 | try->try_ref->try_finally, |
| 5624 | try->try_ref->try_endtry); |
| 5625 | } |
| 5626 | break; |
| 5627 | case ISN_CATCH: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5628 | smsg("%s%4d CATCH", pfx, current); |
| 5629 | break; |
| 5630 | case ISN_TRYCONT: |
| 5631 | { |
| 5632 | trycont_T *trycont = &iptr->isn_arg.trycont; |
| 5633 | |
| 5634 | smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current, |
| 5635 | trycont->tct_levels, |
| 5636 | trycont->tct_levels == 1 ? "" : "s", |
| 5637 | trycont->tct_where); |
| 5638 | } |
| 5639 | break; |
| 5640 | case ISN_FINALLY: |
| 5641 | smsg("%s%4d FINALLY", pfx, current); |
| 5642 | break; |
| 5643 | case ISN_ENDTRY: |
| 5644 | smsg("%s%4d ENDTRY", pfx, current); |
| 5645 | break; |
| 5646 | case ISN_THROW: |
| 5647 | smsg("%s%4d THROW", pfx, current); |
| 5648 | break; |
| 5649 | |
| 5650 | // expression operations on number |
| 5651 | case ISN_OPNR: |
| 5652 | case ISN_OPFLOAT: |
| 5653 | case ISN_OPANY: |
| 5654 | { |
| 5655 | char *what; |
| 5656 | char *ins; |
| 5657 | |
| 5658 | switch (iptr->isn_arg.op.op_type) |
| 5659 | { |
| 5660 | case EXPR_MULT: what = "*"; break; |
| 5661 | case EXPR_DIV: what = "/"; break; |
| 5662 | case EXPR_REM: what = "%"; break; |
| 5663 | case EXPR_SUB: what = "-"; break; |
| 5664 | case EXPR_ADD: what = "+"; break; |
| 5665 | default: what = "???"; break; |
| 5666 | } |
| 5667 | switch (iptr->isn_type) |
| 5668 | { |
| 5669 | case ISN_OPNR: ins = "OPNR"; break; |
| 5670 | case ISN_OPFLOAT: ins = "OPFLOAT"; break; |
| 5671 | case ISN_OPANY: ins = "OPANY"; break; |
| 5672 | default: ins = "???"; break; |
| 5673 | } |
| 5674 | smsg("%s%4d %s %s", pfx, current, ins, what); |
| 5675 | } |
| 5676 | break; |
| 5677 | |
| 5678 | case ISN_COMPAREBOOL: |
| 5679 | case ISN_COMPARESPECIAL: |
| 5680 | case ISN_COMPARENR: |
| 5681 | case ISN_COMPAREFLOAT: |
| 5682 | case ISN_COMPARESTRING: |
| 5683 | case ISN_COMPAREBLOB: |
| 5684 | case ISN_COMPARELIST: |
| 5685 | case ISN_COMPAREDICT: |
| 5686 | case ISN_COMPAREFUNC: |
| 5687 | case ISN_COMPAREANY: |
| 5688 | { |
| 5689 | char *p; |
| 5690 | char buf[10]; |
| 5691 | char *type; |
| 5692 | |
| 5693 | switch (iptr->isn_arg.op.op_type) |
| 5694 | { |
| 5695 | case EXPR_EQUAL: p = "=="; break; |
| 5696 | case EXPR_NEQUAL: p = "!="; break; |
| 5697 | case EXPR_GREATER: p = ">"; break; |
| 5698 | case EXPR_GEQUAL: p = ">="; break; |
| 5699 | case EXPR_SMALLER: p = "<"; break; |
| 5700 | case EXPR_SEQUAL: p = "<="; break; |
| 5701 | case EXPR_MATCH: p = "=~"; break; |
| 5702 | case EXPR_IS: p = "is"; break; |
| 5703 | case EXPR_ISNOT: p = "isnot"; break; |
| 5704 | case EXPR_NOMATCH: p = "!~"; break; |
| 5705 | default: p = "???"; break; |
| 5706 | } |
| 5707 | STRCPY(buf, p); |
| 5708 | if (iptr->isn_arg.op.op_ic == TRUE) |
| 5709 | strcat(buf, "?"); |
| 5710 | switch(iptr->isn_type) |
| 5711 | { |
| 5712 | case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; |
| 5713 | case ISN_COMPARESPECIAL: |
| 5714 | type = "COMPARESPECIAL"; break; |
| 5715 | case ISN_COMPARENR: type = "COMPARENR"; break; |
| 5716 | case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; |
| 5717 | case ISN_COMPARESTRING: |
| 5718 | type = "COMPARESTRING"; break; |
| 5719 | case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; |
| 5720 | case ISN_COMPARELIST: type = "COMPARELIST"; break; |
| 5721 | case ISN_COMPAREDICT: type = "COMPAREDICT"; break; |
| 5722 | case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; |
| 5723 | case ISN_COMPAREANY: type = "COMPAREANY"; break; |
| 5724 | default: type = "???"; break; |
| 5725 | } |
| 5726 | |
| 5727 | smsg("%s%4d %s %s", pfx, current, type, buf); |
| 5728 | } |
| 5729 | break; |
| 5730 | |
| 5731 | case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break; |
| 5732 | case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break; |
| 5733 | |
| 5734 | // expression operations |
| 5735 | case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break; |
| 5736 | case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break; |
| 5737 | case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break; |
| 5738 | case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break; |
| 5739 | case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break; |
| 5740 | case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break; |
| 5741 | case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break; |
| 5742 | case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break; |
| 5743 | case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break; |
| 5744 | case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break; |
| 5745 | case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break; |
| 5746 | case ISN_SLICE: smsg("%s%4d SLICE %lld", |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 5747 | pfx, current, iptr->isn_arg.number); break; |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 5748 | case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current, |
| 5749 | iptr->isn_arg.getitem.gi_index, |
| 5750 | iptr->isn_arg.getitem.gi_with_op ? |
| 5751 | " with op" : ""); break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5752 | case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break; |
| 5753 | case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current, |
| 5754 | iptr->isn_arg.string); break; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 5755 | case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break; |
| 5756 | case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break; |
| 5757 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5758 | case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break; |
| 5759 | |
| 5760 | case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break; |
| 5761 | case ISN_CHECKTYPE: |
| 5762 | { |
| 5763 | checktype_T *ct = &iptr->isn_arg.type; |
| 5764 | char *tofree; |
| 5765 | |
| 5766 | if (ct->ct_arg_idx == 0) |
| 5767 | smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current, |
| 5768 | type_name(ct->ct_type, &tofree), |
| 5769 | (int)ct->ct_off); |
| 5770 | else |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 5771 | smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", |
| 5772 | pfx, current, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5773 | type_name(ct->ct_type, &tofree), |
| 5774 | (int)ct->ct_off, |
| 5775 | (int)ct->ct_arg_idx); |
| 5776 | vim_free(tofree); |
| 5777 | break; |
| 5778 | } |
| 5779 | case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current, |
| 5780 | iptr->isn_arg.checklen.cl_more_OK ? ">= " : "", |
| 5781 | iptr->isn_arg.checklen.cl_min_len); |
| 5782 | break; |
| 5783 | case ISN_SETTYPE: |
| 5784 | { |
| 5785 | char *tofree; |
| 5786 | |
| 5787 | smsg("%s%4d SETTYPE %s", pfx, current, |
| 5788 | type_name(iptr->isn_arg.type.ct_type, &tofree)); |
| 5789 | vim_free(tofree); |
| 5790 | break; |
| 5791 | } |
| 5792 | case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5793 | case ISN_2BOOL: if (iptr->isn_arg.tobool.invert) |
| 5794 | smsg("%s%4d INVERT %d (!val)", pfx, current, |
| 5795 | iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5796 | else |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5797 | smsg("%s%4d 2BOOL %d (!!val)", pfx, current, |
| 5798 | iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5799 | break; |
| 5800 | case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current, |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5801 | (varnumber_T)(iptr->isn_arg.tostring.offset)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5802 | break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5803 | case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]", |
| 5804 | pfx, current, |
| 5805 | (varnumber_T)(iptr->isn_arg.tostring.offset)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5806 | break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5807 | case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current, |
| 5808 | iptr->isn_arg.string); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5809 | break; |
| 5810 | case ISN_PUT: |
| 5811 | if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE) |
| 5812 | smsg("%s%4d PUT %c above range", |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5813 | pfx, current, iptr->isn_arg.put.put_regname); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5814 | else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE) |
| 5815 | smsg("%s%4d PUT %c range", |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5816 | pfx, current, iptr->isn_arg.put.put_regname); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5817 | else |
| 5818 | smsg("%s%4d PUT %c %ld", pfx, current, |
| 5819 | iptr->isn_arg.put.put_regname, |
| 5820 | (long)iptr->isn_arg.put.put_lnum); |
| 5821 | break; |
| 5822 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5823 | case ISN_CMDMOD: |
| 5824 | { |
| 5825 | char_u *buf; |
| 5826 | size_t len = produce_cmdmods( |
| 5827 | NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); |
| 5828 | |
| 5829 | buf = alloc(len + 1); |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 5830 | if (likely(buf != NULL)) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5831 | { |
| 5832 | (void)produce_cmdmods( |
| 5833 | buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); |
| 5834 | smsg("%s%4d CMDMOD %s", pfx, current, buf); |
| 5835 | vim_free(buf); |
| 5836 | } |
| 5837 | break; |
| 5838 | } |
| 5839 | case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break; |
| 5840 | |
| 5841 | case ISN_PROF_START: |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5842 | smsg("%s%4d PROFILE START line %d", pfx, current, |
| 5843 | iptr->isn_lnum); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5844 | break; |
| 5845 | |
| 5846 | case ISN_PROF_END: |
| 5847 | smsg("%s%4d PROFILE END", pfx, current); |
| 5848 | break; |
| 5849 | |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5850 | case ISN_DEBUG: |
Bram Moolenaar | 8cec927 | 2021-06-23 20:20:53 +0200 | [diff] [blame] | 5851 | smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current, |
| 5852 | iptr->isn_arg.debug.dbg_break_lnum + 1, |
| 5853 | iptr->isn_lnum, |
| 5854 | iptr->isn_arg.debug.dbg_var_names_len); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5855 | break; |
| 5856 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5857 | case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current, |
| 5858 | iptr->isn_arg.unpack.unp_count, |
| 5859 | iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : ""); |
| 5860 | break; |
| 5861 | case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current, |
| 5862 | iptr->isn_arg.shuffle.shfl_item, |
| 5863 | iptr->isn_arg.shuffle.shfl_up); |
| 5864 | break; |
| 5865 | case ISN_DROP: smsg("%s%4d DROP", pfx, current); break; |
| 5866 | |
| 5867 | case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE. |
| 5868 | return; |
| 5869 | } |
| 5870 | |
| 5871 | out_flush(); // output one line at a time |
| 5872 | ui_breakcheck(); |
| 5873 | if (got_int) |
| 5874 | break; |
| 5875 | } |
| 5876 | } |
| 5877 | |
| 5878 | /* |
Bram Moolenaar | 4ee9d8e | 2021-06-13 18:38:48 +0200 | [diff] [blame] | 5879 | * Handle command line completion for the :disassemble command. |
| 5880 | */ |
| 5881 | void |
| 5882 | set_context_in_disassemble_cmd(expand_T *xp, char_u *arg) |
| 5883 | { |
| 5884 | char_u *p; |
| 5885 | |
| 5886 | // Default: expand user functions, "debug" and "profile" |
| 5887 | xp->xp_context = EXPAND_DISASSEMBLE; |
| 5888 | xp->xp_pattern = arg; |
| 5889 | |
| 5890 | // first argument already typed: only user function names |
| 5891 | if (*arg != NUL && *(p = skiptowhite(arg)) != NUL) |
| 5892 | { |
| 5893 | xp->xp_context = EXPAND_USER_FUNC; |
| 5894 | xp->xp_pattern = skipwhite(p); |
| 5895 | } |
| 5896 | } |
| 5897 | |
| 5898 | /* |
| 5899 | * Function given to ExpandGeneric() to obtain the list of :disassemble |
| 5900 | * arguments. |
| 5901 | */ |
| 5902 | char_u * |
| 5903 | get_disassemble_argument(expand_T *xp, int idx) |
| 5904 | { |
| 5905 | if (idx == 0) |
| 5906 | return (char_u *)"debug"; |
| 5907 | if (idx == 1) |
| 5908 | return (char_u *)"profile"; |
| 5909 | return get_user_func_name(xp, idx - 2); |
| 5910 | } |
| 5911 | |
| 5912 | /* |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 5913 | * ":disassemble". |
Bram Moolenaar | 777770f | 2020-02-06 21:27:08 +0100 | [diff] [blame] | 5914 | * We don't really need this at runtime, but we do have tests that require it, |
| 5915 | * so always include this. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5916 | */ |
| 5917 | void |
| 5918 | ex_disassemble(exarg_T *eap) |
| 5919 | { |
Bram Moolenaar | 21456cd | 2020-02-13 21:29:32 +0100 | [diff] [blame] | 5920 | char_u *arg = eap->arg; |
Bram Moolenaar | 0f18b6d | 2020-02-02 17:22:27 +0100 | [diff] [blame] | 5921 | char_u *fname; |
| 5922 | ufunc_T *ufunc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5923 | dfunc_T *dfunc; |
| 5924 | isn_T *instr; |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 5925 | int instr_count; |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 5926 | int is_global = FALSE; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5927 | compiletype_T compile_type = CT_NONE; |
| 5928 | |
Bram Moolenaar | c7d5fc8 | 2021-12-05 17:20:24 +0000 | [diff] [blame] | 5929 | if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7])) |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5930 | { |
| 5931 | compile_type = CT_PROFILE; |
| 5932 | arg = skipwhite(arg + 7); |
| 5933 | } |
Bram Moolenaar | c7d5fc8 | 2021-12-05 17:20:24 +0000 | [diff] [blame] | 5934 | else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5])) |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5935 | { |
| 5936 | compile_type = CT_DEBUG; |
| 5937 | arg = skipwhite(arg + 5); |
| 5938 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5939 | |
Bram Moolenaar | bfd6558 | 2020-07-13 18:18:00 +0200 | [diff] [blame] | 5940 | if (STRNCMP(arg, "<lambda>", 8) == 0) |
| 5941 | { |
| 5942 | arg += 8; |
| 5943 | (void)getdigits(&arg); |
| 5944 | fname = vim_strnsave(eap->arg, arg - eap->arg); |
| 5945 | } |
| 5946 | else |
| 5947 | fname = trans_function_name(&arg, &is_global, FALSE, |
Bram Moolenaar | 32b3f82 | 2021-01-06 21:59:39 +0100 | [diff] [blame] | 5948 | TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL); |
Bram Moolenaar | 21456cd | 2020-02-13 21:29:32 +0100 | [diff] [blame] | 5949 | if (fname == NULL) |
| 5950 | { |
| 5951 | semsg(_(e_invarg2), eap->arg); |
| 5952 | return; |
| 5953 | } |
| 5954 | |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 5955 | ufunc = find_func(fname, is_global, NULL); |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 5956 | if (ufunc == NULL) |
| 5957 | { |
| 5958 | char_u *p = untrans_function_name(fname); |
| 5959 | |
| 5960 | if (p != NULL) |
| 5961 | // Try again without making it script-local. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 5962 | ufunc = find_func(p, FALSE, NULL); |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 5963 | } |
Bram Moolenaar | 0f18b6d | 2020-02-02 17:22:27 +0100 | [diff] [blame] | 5964 | vim_free(fname); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5965 | if (ufunc == NULL) |
| 5966 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5967 | semsg(_(e_cannot_find_function_str), eap->arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5968 | return; |
| 5969 | } |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5970 | if (func_needs_compiling(ufunc, compile_type) |
| 5971 | && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 5972 | return; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 5973 | if (ufunc->uf_def_status != UF_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5974 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5975 | semsg(_(e_function_is_not_compiled_str), eap->arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5976 | return; |
| 5977 | } |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5978 | msg((char *)printable_func_name(ufunc)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5979 | |
| 5980 | dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5981 | switch (compile_type) |
| 5982 | { |
| 5983 | case CT_PROFILE: |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 5984 | #ifdef FEAT_PROFILE |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5985 | instr = dfunc->df_instr_prof; |
| 5986 | instr_count = dfunc->df_instr_prof_count; |
| 5987 | break; |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 5988 | #endif |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5989 | // FALLTHROUGH |
| 5990 | case CT_NONE: |
| 5991 | instr = dfunc->df_instr; |
| 5992 | instr_count = dfunc->df_instr_count; |
| 5993 | break; |
| 5994 | case CT_DEBUG: |
| 5995 | instr = dfunc->df_instr_debug; |
| 5996 | instr_count = dfunc->df_instr_debug_count; |
| 5997 | break; |
| 5998 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5999 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6000 | list_instructions("", instr, instr_count, ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6001 | } |
| 6002 | |
| 6003 | /* |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6004 | * 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] | 6005 | * list, etc. Mostly like what JavaScript does, except that empty list and |
| 6006 | * empty dictionary are FALSE. |
| 6007 | */ |
| 6008 | int |
| 6009 | tv2bool(typval_T *tv) |
| 6010 | { |
| 6011 | switch (tv->v_type) |
| 6012 | { |
| 6013 | case VAR_NUMBER: |
| 6014 | return tv->vval.v_number != 0; |
| 6015 | case VAR_FLOAT: |
| 6016 | #ifdef FEAT_FLOAT |
| 6017 | return tv->vval.v_float != 0.0; |
| 6018 | #else |
| 6019 | break; |
| 6020 | #endif |
| 6021 | case VAR_PARTIAL: |
| 6022 | return tv->vval.v_partial != NULL; |
| 6023 | case VAR_FUNC: |
| 6024 | case VAR_STRING: |
| 6025 | return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; |
| 6026 | case VAR_LIST: |
| 6027 | return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; |
| 6028 | case VAR_DICT: |
| 6029 | return tv->vval.v_dict != NULL |
| 6030 | && tv->vval.v_dict->dv_hashtab.ht_used > 0; |
| 6031 | case VAR_BOOL: |
| 6032 | case VAR_SPECIAL: |
| 6033 | return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; |
| 6034 | case VAR_JOB: |
| 6035 | #ifdef FEAT_JOB_CHANNEL |
| 6036 | return tv->vval.v_job != NULL; |
| 6037 | #else |
| 6038 | break; |
| 6039 | #endif |
| 6040 | case VAR_CHANNEL: |
| 6041 | #ifdef FEAT_JOB_CHANNEL |
| 6042 | return tv->vval.v_channel != NULL; |
| 6043 | #else |
| 6044 | break; |
| 6045 | #endif |
| 6046 | case VAR_BLOB: |
| 6047 | return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; |
| 6048 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 6049 | case VAR_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6050 | case VAR_VOID: |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 6051 | case VAR_INSTR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6052 | break; |
| 6053 | } |
| 6054 | return FALSE; |
| 6055 | } |
| 6056 | |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 6057 | void |
| 6058 | emsg_using_string_as(typval_T *tv, int as_number) |
| 6059 | { |
| 6060 | semsg(_(as_number ? e_using_string_as_number_str |
| 6061 | : e_using_string_as_bool_str), |
| 6062 | tv->vval.v_string == NULL |
| 6063 | ? (char_u *)"" : tv->vval.v_string); |
| 6064 | } |
| 6065 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6066 | /* |
| 6067 | * If "tv" is a string give an error and return FAIL. |
| 6068 | */ |
| 6069 | int |
| 6070 | check_not_string(typval_T *tv) |
| 6071 | { |
| 6072 | if (tv->v_type == VAR_STRING) |
| 6073 | { |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 6074 | emsg_using_string_as(tv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6075 | clear_tv(tv); |
| 6076 | return FAIL; |
| 6077 | } |
| 6078 | return OK; |
| 6079 | } |
| 6080 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6081 | |
| 6082 | #endif // FEAT_EVAL |