Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9execute.c: execute Vim9 script instructions |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 19 | // When not generating protos this is included in proto.h |
| 20 | #ifdef PROTO |
| 21 | # include "vim9.h" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 24 | |
| 25 | // Structure put on ec_trystack when ISN_TRY is encountered. |
| 26 | typedef struct { |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 27 | int tcd_frame_idx; // ec_frame_idx at ISN_TRY |
| 28 | int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 29 | int tcd_in_catch; // in catch or finally block |
| 30 | int tcd_did_throw; // set did_throw in :endtry |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 31 | int tcd_catch_idx; // instruction of the first :catch or :finally |
| 32 | int tcd_finally_idx; // instruction of the :finally block or zero |
| 33 | int tcd_endtry_idx; // instruction of the :endtry |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 34 | int tcd_caught; // catch block entered |
Bram Moolenaar | 2e34c34 | 2021-03-14 12:13:33 +0100 | [diff] [blame] | 35 | int tcd_cont; // :continue encountered, jump here (minus one) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 36 | int tcd_return; // when TRUE return from end of :finally |
| 37 | } trycmd_T; |
| 38 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 39 | // Data local to a function. |
| 40 | // On a function call, if not empty, is saved on the stack and restored when |
| 41 | // returning. |
| 42 | typedef struct { |
| 43 | int floc_restore_cmdmod; |
| 44 | cmdmod_T floc_save_cmdmod; |
| 45 | int floc_restore_cmdmod_stacklen; |
| 46 | } funclocal_T; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 47 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 48 | // Structure to hold a reference to an outer_T, with information of whether it |
| 49 | // was allocated. |
| 50 | typedef struct { |
| 51 | outer_T *or_outer; |
| 52 | partial_T *or_partial; // decrement "or_partial->pt_refcount" later |
| 53 | int or_outer_allocated; // free "or_outer" later |
| 54 | } outer_ref_T; |
| 55 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 56 | // A stack is used to store: |
| 57 | // - arguments passed to a :def function |
| 58 | // - info about the calling function, to use when returning |
| 59 | // - local variables |
| 60 | // - temporary values |
| 61 | // |
| 62 | // In detail (FP == Frame Pointer): |
| 63 | // arg1 first argument from caller (if present) |
| 64 | // arg2 second argument from caller (if present) |
| 65 | // extra_arg1 any missing optional argument default value |
| 66 | // FP -> cur_func calling function |
| 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) |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 177 | ga_init2(&dict_stack, sizeof(typval_T), 10); |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 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 |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 274 | emsg_funcname(e_function_was_deleted_str, |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 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 | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 400 | { |
| 401 | typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx); |
| 402 | |
| 403 | tv->v_type = VAR_NUMBER; |
| 404 | tv->vval.v_number = 0; |
| 405 | } |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 406 | if (dfunc->df_has_closure) |
| 407 | { |
| 408 | typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount); |
| 409 | |
| 410 | tv->v_type = VAR_NUMBER; |
| 411 | tv->vval.v_number = 0; |
| 412 | } |
| 413 | ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 414 | |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 415 | if (pt != NULL || ufunc->uf_partial != NULL |
| 416 | || (ufunc->uf_flags & FC_CLOSURE)) |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 417 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 418 | outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T); |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 419 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 420 | if (ref == NULL) |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 421 | return FAIL; |
| 422 | if (pt != NULL) |
| 423 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 424 | ref->or_outer = &pt->pt_outer; |
| 425 | ++pt->pt_refcount; |
| 426 | ref->or_partial = pt; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 427 | } |
| 428 | else if (ufunc->uf_partial != NULL) |
| 429 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 430 | ref->or_outer = &ufunc->uf_partial->pt_outer; |
| 431 | ++ufunc->uf_partial->pt_refcount; |
| 432 | ref->or_partial = ufunc->uf_partial; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 433 | } |
| 434 | else |
| 435 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 436 | ref->or_outer = ALLOC_CLEAR_ONE(outer_T); |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 437 | if (unlikely(ref->or_outer == NULL)) |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 438 | { |
| 439 | vim_free(ref); |
| 440 | return FAIL; |
| 441 | } |
| 442 | ref->or_outer_allocated = TRUE; |
| 443 | ref->or_outer->out_stack = &ectx->ec_stack; |
| 444 | ref->or_outer->out_frame_idx = ectx->ec_frame_idx; |
| 445 | if (ectx->ec_outer_ref != NULL) |
| 446 | ref->or_outer->out_up = ectx->ec_outer_ref->or_outer; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 447 | } |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 448 | ectx->ec_outer_ref = ref; |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 449 | } |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 450 | else |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 451 | ectx->ec_outer_ref = NULL; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 452 | |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 453 | ++ufunc->uf_calls; |
| 454 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 455 | // Set execution state to the start of the called function. |
| 456 | ectx->ec_dfunc_idx = cdf_idx; |
Bram Moolenaar | e5ea346 | 2021-01-25 21:01:48 +0100 | [diff] [blame] | 457 | ectx->ec_instr = INSTRUCTIONS(dfunc); |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 458 | entry = estack_push_ufunc(ufunc, 1); |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 459 | if (entry != NULL) |
| 460 | { |
| 461 | // Set the script context to the script where the function was defined. |
Bram Moolenaar | c70fe46 | 2021-04-17 17:59:19 +0200 | [diff] [blame] | 462 | // Save the current context so it can be restored on return. |
| 463 | entry->es_save_sctx = current_sctx; |
| 464 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 465 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 466 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 467 | // Start execution at the first instruction. |
| 468 | ectx->ec_iidx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 469 | |
| 470 | return OK; |
| 471 | } |
| 472 | |
| 473 | // Get pointer to item in the stack. |
| 474 | #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx) |
| 475 | |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 476 | // Double linked list of funcstack_T in use. |
| 477 | static funcstack_T *first_funcstack = NULL; |
| 478 | |
| 479 | static void |
| 480 | add_funcstack_to_list(funcstack_T *funcstack) |
| 481 | { |
| 482 | // Link in list of funcstacks. |
| 483 | if (first_funcstack != NULL) |
| 484 | first_funcstack->fs_prev = funcstack; |
| 485 | funcstack->fs_next = first_funcstack; |
| 486 | funcstack->fs_prev = NULL; |
| 487 | first_funcstack = funcstack; |
| 488 | } |
| 489 | |
| 490 | static void |
| 491 | remove_funcstack_from_list(funcstack_T *funcstack) |
| 492 | { |
| 493 | if (funcstack->fs_prev == NULL) |
| 494 | first_funcstack = funcstack->fs_next; |
| 495 | else |
| 496 | funcstack->fs_prev->fs_next = funcstack->fs_next; |
| 497 | if (funcstack->fs_next != NULL) |
| 498 | funcstack->fs_next->fs_prev = funcstack->fs_prev; |
| 499 | } |
| 500 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 501 | /* |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 502 | * Used when returning from a function: Check if any closure is still |
| 503 | * referenced. If so then move the arguments and variables to a separate piece |
| 504 | * of stack to be used when the closure is called. |
| 505 | * When "free_arguments" is TRUE the arguments are to be freed. |
| 506 | * Returns FAIL when out of memory. |
| 507 | */ |
| 508 | static int |
| 509 | handle_closure_in_use(ectx_T *ectx, int free_arguments) |
| 510 | { |
| 511 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 512 | + ectx->ec_dfunc_idx; |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 513 | int argcount; |
| 514 | int top; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 515 | int idx; |
| 516 | typval_T *tv; |
| 517 | int closure_in_use = FALSE; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 518 | garray_T *gap = &ectx->ec_funcrefs; |
| 519 | varnumber_T closure_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 520 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 521 | if (dfunc->df_ufunc == NULL) |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 522 | return OK; // function was freed |
| 523 | if (dfunc->df_has_closure == 0) |
| 524 | return OK; // no closures |
| 525 | tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount); |
| 526 | closure_count = tv->vval.v_number; |
| 527 | if (closure_count == 0) |
| 528 | return OK; // no funcrefs created |
| 529 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 530 | argcount = ufunc_argcount(dfunc->df_ufunc); |
| 531 | top = ectx->ec_frame_idx - argcount; |
| 532 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 533 | // Check if any created closure is still in use. |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 534 | for (idx = 0; idx < closure_count; ++idx) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 535 | { |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 536 | partial_T *pt; |
| 537 | int off = gap->ga_len - closure_count + idx; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 538 | |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 539 | if (off < 0) |
| 540 | continue; // count is off or already done |
| 541 | pt = ((partial_T **)gap->ga_data)[off]; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 542 | if (pt->pt_refcount > 1) |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 543 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 544 | int refcount = pt->pt_refcount; |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 545 | int i; |
| 546 | |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 547 | // A Reference in a local variable doesn't count, it gets |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 548 | // unreferenced on return. |
| 549 | for (i = 0; i < dfunc->df_varcount; ++i) |
| 550 | { |
| 551 | typval_T *stv = STACK_TV(ectx->ec_frame_idx |
| 552 | + STACK_FRAME_SIZE + i); |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 553 | if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial) |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 554 | --refcount; |
| 555 | } |
| 556 | if (refcount > 1) |
| 557 | { |
| 558 | closure_in_use = TRUE; |
| 559 | break; |
| 560 | } |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 561 | } |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | if (closure_in_use) |
| 565 | { |
| 566 | funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T); |
| 567 | typval_T *stack; |
| 568 | |
| 569 | // A closure is using the arguments and/or local variables. |
| 570 | // Move them to the called function. |
| 571 | if (funcstack == NULL) |
| 572 | return FAIL; |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 573 | |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 574 | funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE; |
| 575 | funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 576 | stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len); |
| 577 | funcstack->fs_ga.ga_data = stack; |
| 578 | if (stack == NULL) |
| 579 | { |
| 580 | vim_free(funcstack); |
| 581 | return FAIL; |
| 582 | } |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 583 | add_funcstack_to_list(funcstack); |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 584 | |
| 585 | // Move or copy the arguments. |
| 586 | for (idx = 0; idx < argcount; ++idx) |
| 587 | { |
| 588 | tv = STACK_TV(top + idx); |
| 589 | if (free_arguments) |
| 590 | { |
| 591 | *(stack + idx) = *tv; |
| 592 | tv->v_type = VAR_UNKNOWN; |
| 593 | } |
| 594 | else |
| 595 | copy_tv(tv, stack + idx); |
| 596 | } |
| 597 | // Move the local variables. |
| 598 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
| 599 | { |
| 600 | tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx); |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 601 | |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 602 | // A partial created for a local function, that is also used as a |
| 603 | // local variable, has a reference count for the variable, thus |
| 604 | // will never go down to zero. When all these refcounts are one |
| 605 | // then the funcstack is unused. We need to count how many we have |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 606 | // so we know when to check. |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 607 | if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL) |
| 608 | { |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 609 | int i; |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 610 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 611 | for (i = 0; i < closure_count; ++i) |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 612 | if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[ |
| 613 | gap->ga_len - closure_count + i]) |
| 614 | ++funcstack->fs_min_refcount; |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 617 | *(stack + funcstack->fs_var_offset + idx) = *tv; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 618 | tv->v_type = VAR_UNKNOWN; |
| 619 | } |
| 620 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 621 | for (idx = 0; idx < closure_count; ++idx) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 622 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 623 | partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len |
| 624 | - closure_count + idx]; |
| 625 | if (pt->pt_refcount > 1) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 626 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 627 | ++funcstack->fs_refcount; |
| 628 | pt->pt_funcstack = funcstack; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 629 | pt->pt_outer.out_stack = &funcstack->fs_ga; |
| 630 | pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 635 | for (idx = 0; idx < closure_count; ++idx) |
| 636 | partial_unref(((partial_T **)gap->ga_data)[gap->ga_len |
| 637 | - closure_count + idx]); |
| 638 | gap->ga_len -= closure_count; |
| 639 | if (gap->ga_len == 0) |
| 640 | ga_clear(gap); |
| 641 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 642 | return OK; |
| 643 | } |
| 644 | |
| 645 | /* |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 646 | * Called when a partial is freed or its reference count goes down to one. The |
| 647 | * funcstack may be the only reference to the partials in the local variables. |
| 648 | * Go over all of them, the funcref and can be freed if all partials |
| 649 | * referencing the funcstack have a reference count of one. |
| 650 | */ |
| 651 | void |
| 652 | funcstack_check_refcount(funcstack_T *funcstack) |
| 653 | { |
| 654 | int i; |
| 655 | garray_T *gap = &funcstack->fs_ga; |
| 656 | int done = 0; |
| 657 | |
| 658 | if (funcstack->fs_refcount > funcstack->fs_min_refcount) |
| 659 | return; |
| 660 | for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i) |
| 661 | { |
| 662 | typval_T *tv = ((typval_T *)gap->ga_data) + i; |
| 663 | |
| 664 | if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL |
| 665 | && tv->vval.v_partial->pt_funcstack == funcstack |
| 666 | && tv->vval.v_partial->pt_refcount == 1) |
| 667 | ++done; |
| 668 | } |
| 669 | if (done == funcstack->fs_min_refcount) |
| 670 | { |
| 671 | typval_T *stack = gap->ga_data; |
| 672 | |
| 673 | // All partials referencing the funcstack have a reference count of |
| 674 | // one, thus the funcstack is no longer of use. |
| 675 | for (i = 0; i < gap->ga_len; ++i) |
| 676 | clear_tv(stack + i); |
| 677 | vim_free(stack); |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 678 | remove_funcstack_from_list(funcstack); |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 679 | vim_free(funcstack); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /* |
Bram Moolenaar | 7509ad8 | 2021-12-14 18:14:37 +0000 | [diff] [blame] | 684 | * For garbage collecting: set references in all variables referenced by |
| 685 | * all funcstacks. |
| 686 | */ |
| 687 | int |
| 688 | set_ref_in_funcstacks(int copyID) |
| 689 | { |
| 690 | funcstack_T *funcstack; |
| 691 | |
| 692 | for (funcstack = first_funcstack; funcstack != NULL; |
| 693 | funcstack = funcstack->fs_next) |
| 694 | { |
| 695 | typval_T *stack = funcstack->fs_ga.ga_data; |
| 696 | int i; |
| 697 | |
| 698 | for (i = 0; i < funcstack->fs_ga.ga_len; ++i) |
| 699 | if (set_ref_in_item(stack + i, copyID, NULL, NULL)) |
| 700 | return TRUE; // abort |
| 701 | } |
| 702 | return FALSE; |
| 703 | } |
| 704 | |
| 705 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 706 | * Return from the current function. |
| 707 | */ |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 708 | static int |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 709 | func_return(ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 710 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 711 | int idx; |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 712 | int ret_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 713 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 714 | + ectx->ec_dfunc_idx; |
| 715 | int argcount = ufunc_argcount(dfunc->df_ufunc); |
| 716 | int top = ectx->ec_frame_idx - argcount; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 717 | estack_T *entry; |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 718 | int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx |
| 719 | + STACK_FRAME_FUNC_OFF)->vval.v_number; |
Bram Moolenaar | b06b50d | 2021-04-26 21:39:25 +0200 | [diff] [blame] | 720 | funclocal_T *floc; |
| 721 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 722 | dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data) |
| 723 | + prev_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 724 | |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 725 | if (do_profiling == PROF_YES) |
| 726 | { |
| 727 | ufunc_T *caller = prev_dfunc->df_ufunc; |
| 728 | |
| 729 | if (dfunc->df_ufunc->uf_profiling |
| 730 | || (caller != NULL && caller->uf_profiling)) |
| 731 | { |
| 732 | profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data) |
| 733 | + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller); |
| 734 | --profile_info_ga.ga_len; |
| 735 | } |
| 736 | } |
| 737 | #endif |
Bram Moolenaar | 919c12c | 2021-12-14 14:29:16 +0000 | [diff] [blame] | 738 | |
| 739 | // No check for uf_refcount being zero, cannot think of a way that would |
| 740 | // happen. |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 741 | --dfunc->df_ufunc->uf_calls; |
| 742 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 743 | // execution context goes one level up |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 744 | entry = estack_pop(); |
| 745 | if (entry != NULL) |
Bram Moolenaar | c70fe46 | 2021-04-17 17:59:19 +0200 | [diff] [blame] | 746 | current_sctx = entry->es_save_sctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 747 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 748 | if (handle_closure_in_use(ectx, TRUE) == FAIL) |
| 749 | return FAIL; |
| 750 | |
| 751 | // Clear the arguments. |
| 752 | for (idx = top; idx < ectx->ec_frame_idx; ++idx) |
| 753 | clear_tv(STACK_TV(idx)); |
| 754 | |
| 755 | // Clear local variables and temp values, but not the return value. |
| 756 | for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 757 | idx < ectx->ec_stack.ga_len - 1; ++idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 758 | clear_tv(STACK_TV(idx)); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 759 | |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 760 | // The return value should be on top of the stack. However, when aborting |
| 761 | // it may not be there and ec_frame_idx is the top of the stack. |
| 762 | ret_idx = ectx->ec_stack.ga_len - 1; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 763 | if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF) |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 764 | ret_idx = 0; |
| 765 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 766 | if (ectx->ec_outer_ref != NULL) |
| 767 | { |
| 768 | if (ectx->ec_outer_ref->or_outer_allocated) |
| 769 | vim_free(ectx->ec_outer_ref->or_outer); |
| 770 | partial_unref(ectx->ec_outer_ref->or_partial); |
| 771 | vim_free(ectx->ec_outer_ref); |
| 772 | } |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 773 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 774 | // Restore the previous frame. |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 775 | ectx->ec_dfunc_idx = prev_dfunc_idx; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 776 | ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx |
| 777 | + STACK_FRAME_IIDX_OFF)->vval.v_number; |
Bram Moolenaar | 5930ddc | 2021-04-26 20:32:59 +0200 | [diff] [blame] | 778 | ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx |
| 779 | + STACK_FRAME_INSTR_OFF)->vval.v_string; |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 780 | ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 781 | + STACK_FRAME_OUTER_OFF)->vval.v_string; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 782 | floc = (void *)STACK_TV(ectx->ec_frame_idx |
| 783 | + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string; |
Bram Moolenaar | 5366e1a | 2020-10-01 13:01:34 +0200 | [diff] [blame] | 784 | // restoring ec_frame_idx must be last |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 785 | ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx |
| 786 | + STACK_FRAME_IDX_OFF)->vval.v_number; |
Bram Moolenaar | d386e92 | 2021-04-25 14:48:49 +0200 | [diff] [blame] | 787 | |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 788 | if (floc == NULL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 789 | ectx->ec_funclocal.floc_restore_cmdmod = FALSE; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 790 | else |
| 791 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 792 | ectx->ec_funclocal = *floc; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 793 | vim_free(floc); |
| 794 | } |
| 795 | |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 796 | if (ret_idx > 0) |
| 797 | { |
| 798 | // Reset the stack to the position before the call, with a spot for the |
| 799 | // return value, moved there from above the frame. |
| 800 | ectx->ec_stack.ga_len = top + 1; |
| 801 | *STACK_TV_BOT(-1) = *STACK_TV(ret_idx); |
| 802 | } |
| 803 | else |
| 804 | // Reset the stack to the position before the call. |
| 805 | ectx->ec_stack.ga_len = top; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 806 | |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 807 | funcdepth_decrement(); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 808 | --ex_nesting_level; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 809 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | #undef STACK_TV |
| 813 | |
| 814 | /* |
| 815 | * Prepare arguments and rettv for calling a builtin or user function. |
| 816 | */ |
| 817 | static int |
| 818 | call_prepare(int argcount, typval_T *argvars, ectx_T *ectx) |
| 819 | { |
| 820 | int idx; |
| 821 | typval_T *tv; |
| 822 | |
| 823 | // Move arguments from bottom of the stack to argvars[] and add terminator. |
| 824 | for (idx = 0; idx < argcount; ++idx) |
| 825 | argvars[idx] = *STACK_TV_BOT(idx - argcount); |
| 826 | argvars[argcount].v_type = VAR_UNKNOWN; |
| 827 | |
| 828 | // Result replaces the arguments on the stack. |
| 829 | if (argcount > 0) |
| 830 | ectx->ec_stack.ga_len -= argcount - 1; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 831 | else if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 832 | return FAIL; |
| 833 | else |
| 834 | ++ectx->ec_stack.ga_len; |
| 835 | |
| 836 | // Default return value is zero. |
| 837 | tv = STACK_TV_BOT(-1); |
| 838 | tv->v_type = VAR_NUMBER; |
| 839 | tv->vval.v_number = 0; |
| 840 | |
| 841 | return OK; |
| 842 | } |
| 843 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 844 | // Ugly global to avoid passing the execution context around through many |
| 845 | // layers. |
| 846 | static ectx_T *current_ectx = NULL; |
| 847 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 848 | /* |
| 849 | * Call a builtin function by index. |
| 850 | */ |
| 851 | static int |
| 852 | call_bfunc(int func_idx, int argcount, ectx_T *ectx) |
| 853 | { |
| 854 | typval_T argvars[MAX_FUNC_ARGS]; |
| 855 | int idx; |
Bram Moolenaar | 171fb92 | 2020-10-28 16:54:47 +0100 | [diff] [blame] | 856 | int did_emsg_before = did_emsg; |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 857 | ectx_T *prev_ectx = current_ectx; |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 858 | char *save_func_name = ectx->ec_where.wt_func_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 859 | |
| 860 | if (call_prepare(argcount, argvars, ectx) == FAIL) |
| 861 | return FAIL; |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 862 | ectx->ec_where.wt_func_name = internal_func_name(func_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 863 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 864 | // Call the builtin function. Set "current_ectx" so that when it |
| 865 | // recursively invokes call_def_function() a closure context can be set. |
| 866 | current_ectx = ectx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 867 | call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1)); |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 868 | current_ectx = prev_ectx; |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 869 | ectx->ec_where.wt_func_name = save_func_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 870 | |
| 871 | // Clear the arguments. |
| 872 | for (idx = 0; idx < argcount; ++idx) |
| 873 | clear_tv(&argvars[idx]); |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 874 | |
Bram Moolenaar | 57f799e | 2020-12-12 20:42:19 +0100 | [diff] [blame] | 875 | if (did_emsg > did_emsg_before) |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 876 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 877 | return OK; |
| 878 | } |
| 879 | |
| 880 | /* |
| 881 | * Execute a user defined function. |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 882 | * If the function is compiled this will add a stack frame and set the |
| 883 | * instruction pointer at the start of the function. |
| 884 | * Otherwise the function is called here. |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 885 | * 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] | 886 | * "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] | 887 | */ |
| 888 | static int |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 889 | call_ufunc( |
| 890 | ufunc_T *ufunc, |
| 891 | partial_T *pt, |
| 892 | int argcount, |
| 893 | ectx_T *ectx, |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 894 | isn_T *iptr, |
| 895 | dict_T *selfdict) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 896 | { |
| 897 | typval_T argvars[MAX_FUNC_ARGS]; |
| 898 | funcexe_T funcexe; |
| 899 | int error; |
| 900 | int idx; |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 901 | int did_emsg_before = did_emsg; |
Bram Moolenaar | 968a5b6 | 2021-06-15 19:32:40 +0200 | [diff] [blame] | 902 | compiletype_T compile_type = COMPILE_TYPE(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 903 | |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 904 | if (func_needs_compiling(ufunc, compile_type) |
| 905 | && compile_def_function(ufunc, FALSE, compile_type, NULL) |
| 906 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 907 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 908 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 909 | { |
Bram Moolenaar | 52c124d | 2020-12-20 21:43:35 +0100 | [diff] [blame] | 910 | error = check_user_func_argcount(ufunc, argcount); |
Bram Moolenaar | 5082471 | 2020-12-20 21:10:17 +0100 | [diff] [blame] | 911 | if (error != FCERR_UNKNOWN) |
| 912 | { |
| 913 | if (error == FCERR_TOOMANY) |
Bram Moolenaar | e124204 | 2021-12-16 20:56:57 +0000 | [diff] [blame] | 914 | semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name); |
Bram Moolenaar | 5082471 | 2020-12-20 21:10:17 +0100 | [diff] [blame] | 915 | else |
Bram Moolenaar | e124204 | 2021-12-16 20:56:57 +0000 | [diff] [blame] | 916 | semsg(_(e_not_enough_arguments_for_function_str), |
| 917 | ufunc->uf_name); |
Bram Moolenaar | 5082471 | 2020-12-20 21:10:17 +0100 | [diff] [blame] | 918 | return FAIL; |
| 919 | } |
| 920 | |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 921 | // The function has been compiled, can call it quickly. For a function |
| 922 | // that was defined later: we can call it directly next time. |
| 923 | if (iptr != NULL) |
| 924 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 925 | delete_instr(iptr); |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 926 | iptr->isn_type = ISN_DCALL; |
| 927 | iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 928 | iptr->isn_arg.dfunc.cdf_argcount = argcount; |
| 929 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 930 | return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx); |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 931 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 932 | |
| 933 | if (call_prepare(argcount, argvars, ectx) == FAIL) |
| 934 | return FAIL; |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 935 | CLEAR_FIELD(funcexe); |
Bram Moolenaar | 851f86b | 2021-12-13 14:26:44 +0000 | [diff] [blame] | 936 | funcexe.fe_evaluate = TRUE; |
| 937 | funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict(); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 938 | |
| 939 | // Call the user function. Result goes in last position on the stack. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 940 | error = call_user_func_check(ufunc, argcount, argvars, |
Bram Moolenaar | 851f86b | 2021-12-13 14:26:44 +0000 | [diff] [blame] | 941 | STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 942 | |
| 943 | // Clear the arguments. |
| 944 | for (idx = 0; idx < argcount; ++idx) |
| 945 | clear_tv(&argvars[idx]); |
| 946 | |
| 947 | if (error != FCERR_NONE) |
| 948 | { |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 949 | user_func_error(error, ufunc->uf_name, &funcexe); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 950 | return FAIL; |
| 951 | } |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 952 | if (did_emsg > did_emsg_before) |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 953 | // Error other than from calling the function itself. |
| 954 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 955 | return OK; |
| 956 | } |
| 957 | |
| 958 | /* |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 959 | * If command modifiers were applied restore them. |
| 960 | */ |
| 961 | static void |
| 962 | may_restore_cmdmod(funclocal_T *funclocal) |
| 963 | { |
| 964 | if (funclocal->floc_restore_cmdmod) |
| 965 | { |
| 966 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 967 | undo_cmdmod(&cmdmod); |
| 968 | cmdmod = funclocal->floc_save_cmdmod; |
| 969 | funclocal->floc_restore_cmdmod = FALSE; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | /* |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 974 | * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was |
| 975 | * pressed. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 976 | */ |
| 977 | static int |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 978 | vim9_aborting(int prev_uncaught_emsg) |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 979 | { |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 980 | return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 984 | * Execute a function by "name". |
| 985 | * This can be a builtin function or a user function. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 986 | * "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] | 987 | * Returns FAIL if not found without an error message. |
| 988 | */ |
| 989 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 990 | call_by_name( |
| 991 | char_u *name, |
| 992 | int argcount, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 993 | ectx_T *ectx, |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 994 | isn_T *iptr, |
| 995 | dict_T *selfdict) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 996 | { |
| 997 | ufunc_T *ufunc; |
| 998 | |
| 999 | if (builtin_function(name, -1)) |
| 1000 | { |
| 1001 | int func_idx = find_internal_func(name); |
| 1002 | |
| 1003 | if (func_idx < 0) |
| 1004 | return FAIL; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1005 | if (check_internal_func(func_idx, argcount) < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1006 | return FAIL; |
| 1007 | return call_bfunc(func_idx, argcount, ectx); |
| 1008 | } |
| 1009 | |
Bram Moolenaar | d9d2fd0 | 2022-01-13 21:15:21 +0000 | [diff] [blame] | 1010 | ufunc = find_func(name, FALSE); |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 1011 | |
| 1012 | if (ufunc == NULL) |
| 1013 | { |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 1014 | int prev_uncaught_emsg = uncaught_emsg; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 1015 | |
| 1016 | if (script_autoload(name, TRUE)) |
| 1017 | // loaded a package, search for the function again |
Bram Moolenaar | d9d2fd0 | 2022-01-13 21:15:21 +0000 | [diff] [blame] | 1018 | ufunc = find_func(name, FALSE); |
Bram Moolenaar | 88c89c7 | 2021-08-14 14:01:05 +0200 | [diff] [blame] | 1019 | |
| 1020 | if (vim9_aborting(prev_uncaught_emsg)) |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 1021 | return FAIL; // bail out if loading the script caused an error |
| 1022 | } |
| 1023 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1024 | if (ufunc != NULL) |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1025 | { |
Bram Moolenaar | 6ce46b9 | 2021-08-07 15:35:36 +0200 | [diff] [blame] | 1026 | if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL) |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1027 | { |
| 1028 | int i; |
| 1029 | typval_T *argv = STACK_TV_BOT(0) - argcount; |
| 1030 | |
| 1031 | // The function can change at runtime, check that the argument |
| 1032 | // types are correct. |
| 1033 | for (i = 0; i < argcount; ++i) |
| 1034 | { |
Bram Moolenaar | e3ffcd9 | 2021-03-08 21:47:13 +0100 | [diff] [blame] | 1035 | type_T *type = NULL; |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1036 | |
Bram Moolenaar | 6ce46b9 | 2021-08-07 15:35:36 +0200 | [diff] [blame] | 1037 | if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL) |
Bram Moolenaar | e3ffcd9 | 2021-03-08 21:47:13 +0100 | [diff] [blame] | 1038 | type = ufunc->uf_arg_types[i]; |
| 1039 | else if (ufunc->uf_va_type != NULL) |
| 1040 | type = ufunc->uf_va_type->tt_member; |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1041 | if (type != NULL && check_typval_arg_type(type, |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 1042 | &argv[i], NULL, i + 1) == FAIL) |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1043 | return FAIL; |
| 1044 | } |
| 1045 | } |
| 1046 | |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1047 | return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict); |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 1048 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1049 | |
| 1050 | return FAIL; |
| 1051 | } |
| 1052 | |
| 1053 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1054 | call_partial( |
| 1055 | typval_T *tv, |
| 1056 | int argcount_arg, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1057 | ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1058 | { |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 1059 | int argcount = argcount_arg; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1060 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1061 | int called_emsg_before = called_emsg; |
Bram Moolenaar | cb6cbf2 | 2021-01-12 17:17:01 +0100 | [diff] [blame] | 1062 | int res = FAIL; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1063 | dict_T *selfdict = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1064 | |
| 1065 | if (tv->v_type == VAR_PARTIAL) |
| 1066 | { |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 1067 | partial_T *pt = tv->vval.v_partial; |
| 1068 | int i; |
| 1069 | |
| 1070 | if (pt->pt_argc > 0) |
| 1071 | { |
| 1072 | // Make space for arguments from the partial, shift the "argcount" |
| 1073 | // arguments up. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1074 | if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc)) |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 1075 | return FAIL; |
| 1076 | for (i = 1; i <= argcount; ++i) |
| 1077 | *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i); |
| 1078 | ectx->ec_stack.ga_len += pt->pt_argc; |
| 1079 | argcount += pt->pt_argc; |
| 1080 | |
| 1081 | // copy the arguments from the partial onto the stack |
| 1082 | for (i = 0; i < pt->pt_argc; ++i) |
| 1083 | copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i)); |
| 1084 | } |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1085 | selfdict = pt->pt_dict; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1086 | |
| 1087 | if (pt->pt_func != NULL) |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1088 | return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict); |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 1089 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1090 | name = pt->pt_name; |
| 1091 | } |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1092 | else if (tv->v_type == VAR_FUNC) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1093 | name = tv->vval.v_string; |
Bram Moolenaar | 95006e3 | 2020-08-29 17:47:08 +0200 | [diff] [blame] | 1094 | if (name != NULL) |
| 1095 | { |
| 1096 | char_u fname_buf[FLEN_FIXED + 1]; |
| 1097 | char_u *tofree = NULL; |
| 1098 | int error = FCERR_NONE; |
| 1099 | char_u *fname; |
| 1100 | |
| 1101 | // May need to translate <SNR>123_ to K_SNR. |
| 1102 | fname = fname_trans_sid(name, fname_buf, &tofree, &error); |
| 1103 | if (error != FCERR_NONE) |
| 1104 | res = FAIL; |
| 1105 | else |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1106 | res = call_by_name(fname, argcount, ectx, NULL, selfdict); |
Bram Moolenaar | 95006e3 | 2020-08-29 17:47:08 +0200 | [diff] [blame] | 1107 | vim_free(tofree); |
| 1108 | } |
| 1109 | |
Bram Moolenaar | cb6cbf2 | 2021-01-12 17:17:01 +0100 | [diff] [blame] | 1110 | if (res == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1111 | { |
| 1112 | if (called_emsg == called_emsg_before) |
Bram Moolenaar | e124204 | 2021-12-16 20:56:57 +0000 | [diff] [blame] | 1113 | semsg(_(e_unknown_function_str), |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1114 | name == NULL ? (char_u *)"[unknown]" : name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1115 | return FAIL; |
| 1116 | } |
| 1117 | return OK; |
| 1118 | } |
| 1119 | |
| 1120 | /* |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 1121 | * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return |
| 1122 | * TRUE. |
| 1123 | */ |
| 1124 | static int |
| 1125 | error_if_locked(int lock, char *error) |
| 1126 | { |
| 1127 | if (lock & (VAR_LOCKED | VAR_FIXED)) |
| 1128 | { |
| 1129 | emsg(_(error)); |
| 1130 | return TRUE; |
| 1131 | } |
| 1132 | return FALSE; |
| 1133 | } |
| 1134 | |
| 1135 | /* |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 1136 | * Give an error if "tv" is not a number and return FAIL. |
| 1137 | */ |
| 1138 | static int |
| 1139 | check_for_number(typval_T *tv) |
| 1140 | { |
| 1141 | if (tv->v_type != VAR_NUMBER) |
| 1142 | { |
| 1143 | semsg(_(e_expected_str_but_got_str), |
| 1144 | vartype_name(VAR_NUMBER), vartype_name(tv->v_type)); |
| 1145 | return FAIL; |
| 1146 | } |
| 1147 | return OK; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1151 | * Store "tv" in variable "name". |
| 1152 | * This is for s: and g: variables. |
| 1153 | */ |
| 1154 | static void |
| 1155 | store_var(char_u *name, typval_T *tv) |
| 1156 | { |
| 1157 | funccal_entry_T entry; |
Bram Moolenaar | d877a57 | 2021-04-01 19:42:48 +0200 | [diff] [blame] | 1158 | int flags = ASSIGN_DECL; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1159 | |
Bram Moolenaar | d877a57 | 2021-04-01 19:42:48 +0200 | [diff] [blame] | 1160 | if (tv->v_lock) |
| 1161 | flags |= ASSIGN_CONST; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1162 | save_funccal(&entry); |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 1163 | set_var_const(name, 0, NULL, tv, FALSE, flags, 0); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1164 | restore_funccal(); |
| 1165 | } |
| 1166 | |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1167 | /* |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1168 | * Convert "tv" to a string. |
| 1169 | * Return FAIL if not allowed. |
| 1170 | */ |
| 1171 | static int |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1172 | do_2string(typval_T *tv, int is_2string_any, int tolerant) |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1173 | { |
| 1174 | if (tv->v_type != VAR_STRING) |
| 1175 | { |
| 1176 | char_u *str; |
| 1177 | |
| 1178 | if (is_2string_any) |
| 1179 | { |
| 1180 | switch (tv->v_type) |
| 1181 | { |
| 1182 | case VAR_SPECIAL: |
| 1183 | case VAR_BOOL: |
| 1184 | case VAR_NUMBER: |
| 1185 | case VAR_FLOAT: |
| 1186 | case VAR_BLOB: break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1187 | |
| 1188 | case VAR_LIST: |
| 1189 | if (tolerant) |
| 1190 | { |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1191 | char_u *s, *e, *p; |
| 1192 | garray_T ga; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1193 | |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1194 | ga_init2(&ga, sizeof(char_u *), 1); |
| 1195 | |
| 1196 | // Convert to NL separated items, then |
| 1197 | // escape the items and replace the NL with |
| 1198 | // a space. |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1199 | str = typval2string(tv, TRUE); |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1200 | if (str == NULL) |
| 1201 | return FAIL; |
| 1202 | s = str; |
| 1203 | while ((e = vim_strchr(s, '\n')) != NULL) |
| 1204 | { |
| 1205 | *e = NUL; |
Bram Moolenaar | 21c1a0c | 2021-10-17 17:20:23 +0100 | [diff] [blame] | 1206 | p = vim_strsave_fnameescape(s, |
| 1207 | VSE_NONE); |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1208 | if (p != NULL) |
| 1209 | { |
| 1210 | ga_concat(&ga, p); |
| 1211 | ga_concat(&ga, (char_u *)" "); |
| 1212 | vim_free(p); |
| 1213 | } |
| 1214 | s = e + 1; |
| 1215 | } |
| 1216 | vim_free(str); |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1217 | clear_tv(tv); |
| 1218 | tv->v_type = VAR_STRING; |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1219 | tv->vval.v_string = ga.ga_data; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1220 | return OK; |
| 1221 | } |
| 1222 | // FALLTHROUGH |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1223 | default: to_string_error(tv->v_type); |
| 1224 | return FAIL; |
| 1225 | } |
| 1226 | } |
Bram Moolenaar | 3445320 | 2021-01-31 13:08:38 +0100 | [diff] [blame] | 1227 | str = typval_tostring(tv, TRUE); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1228 | clear_tv(tv); |
| 1229 | tv->v_type = VAR_STRING; |
| 1230 | tv->vval.v_string = str; |
| 1231 | } |
| 1232 | return OK; |
| 1233 | } |
| 1234 | |
| 1235 | /* |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1236 | * When the value of "sv" is a null list of dict, allocate it. |
| 1237 | */ |
| 1238 | static void |
| 1239 | allocate_if_null(typval_T *tv) |
| 1240 | { |
| 1241 | switch (tv->v_type) |
| 1242 | { |
| 1243 | case VAR_LIST: |
| 1244 | if (tv->vval.v_list == NULL) |
Bram Moolenaar | fef8064 | 2021-02-03 20:01:19 +0100 | [diff] [blame] | 1245 | (void)rettv_list_alloc(tv); |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1246 | break; |
| 1247 | case VAR_DICT: |
| 1248 | if (tv->vval.v_dict == NULL) |
Bram Moolenaar | fef8064 | 2021-02-03 20:01:19 +0100 | [diff] [blame] | 1249 | (void)rettv_dict_alloc(tv); |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1250 | break; |
Bram Moolenaar | b7c21af | 2021-04-18 14:12:31 +0200 | [diff] [blame] | 1251 | case VAR_BLOB: |
| 1252 | if (tv->vval.v_blob == NULL) |
| 1253 | (void)rettv_blob_alloc(tv); |
| 1254 | break; |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1255 | default: |
| 1256 | break; |
| 1257 | } |
| 1258 | } |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1259 | |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1260 | /* |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1261 | * Return the character "str[index]" where "index" is the character index, |
| 1262 | * including composing characters. |
| 1263 | * If "index" is out of range NULL is returned. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1264 | */ |
| 1265 | char_u * |
| 1266 | char_from_string(char_u *str, varnumber_T index) |
| 1267 | { |
| 1268 | size_t nbyte = 0; |
| 1269 | varnumber_T nchar = index; |
| 1270 | size_t slen; |
| 1271 | |
| 1272 | if (str == NULL) |
| 1273 | return NULL; |
| 1274 | slen = STRLEN(str); |
| 1275 | |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1276 | // Do the same as for a list: a negative index counts from the end. |
| 1277 | // Optimization to check the first byte to be below 0x80 (and no composing |
| 1278 | // character follows) makes this a lot faster. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1279 | if (index < 0) |
| 1280 | { |
| 1281 | int clen = 0; |
| 1282 | |
| 1283 | for (nbyte = 0; nbyte < slen; ++clen) |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1284 | { |
| 1285 | if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80) |
| 1286 | ++nbyte; |
| 1287 | else if (enc_utf8) |
| 1288 | nbyte += utfc_ptr2len(str + nbyte); |
| 1289 | else |
| 1290 | nbyte += mb_ptr2len(str + nbyte); |
| 1291 | } |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1292 | nchar = clen + index; |
| 1293 | if (nchar < 0) |
| 1294 | // unlike list: index out of range results in empty string |
| 1295 | return NULL; |
| 1296 | } |
| 1297 | |
| 1298 | for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar) |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1299 | { |
| 1300 | if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80) |
| 1301 | ++nbyte; |
| 1302 | else if (enc_utf8) |
| 1303 | nbyte += utfc_ptr2len(str + nbyte); |
| 1304 | else |
| 1305 | nbyte += mb_ptr2len(str + nbyte); |
| 1306 | } |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1307 | if (nbyte >= slen) |
| 1308 | return NULL; |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1309 | return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte)); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * 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] | 1314 | * "str_len". Composing characters are included. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1315 | * If going over the end return "str_len". |
| 1316 | * If "idx" is negative count from the end, -1 is the last character. |
| 1317 | * When going over the start return -1. |
| 1318 | */ |
| 1319 | static long |
| 1320 | char_idx2byte(char_u *str, size_t str_len, varnumber_T idx) |
| 1321 | { |
| 1322 | varnumber_T nchar = idx; |
| 1323 | size_t nbyte = 0; |
| 1324 | |
| 1325 | if (nchar >= 0) |
| 1326 | { |
| 1327 | while (nchar > 0 && nbyte < str_len) |
| 1328 | { |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1329 | nbyte += mb_ptr2len(str + nbyte); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1330 | --nchar; |
| 1331 | } |
| 1332 | } |
| 1333 | else |
| 1334 | { |
| 1335 | nbyte = str_len; |
| 1336 | while (nchar < 0 && nbyte > 0) |
| 1337 | { |
| 1338 | --nbyte; |
| 1339 | nbyte -= mb_head_off(str, str + nbyte); |
| 1340 | ++nchar; |
| 1341 | } |
| 1342 | if (nchar < 0) |
| 1343 | return -1; |
| 1344 | } |
| 1345 | return (long)nbyte; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1349 | * Return the slice "str[first : last]" using character indexes. Composing |
| 1350 | * characters are included. |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1351 | * "exclusive" is TRUE for slice(). |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1352 | * Return NULL when the result is empty. |
| 1353 | */ |
| 1354 | char_u * |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1355 | 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] | 1356 | { |
| 1357 | long start_byte, end_byte; |
| 1358 | size_t slen; |
| 1359 | |
| 1360 | if (str == NULL) |
| 1361 | return NULL; |
| 1362 | slen = STRLEN(str); |
| 1363 | start_byte = char_idx2byte(str, slen, first); |
| 1364 | if (start_byte < 0) |
| 1365 | start_byte = 0; // first index very negative: use zero |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1366 | if ((last == -1 && !exclusive) || last == VARNUM_MAX) |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1367 | end_byte = (long)slen; |
| 1368 | else |
| 1369 | { |
| 1370 | end_byte = char_idx2byte(str, slen, last); |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1371 | if (!exclusive && end_byte >= 0 && end_byte < (long)slen) |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1372 | // end index is inclusive |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1373 | end_byte += mb_ptr2len(str + end_byte); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | if (start_byte >= (long)slen || end_byte <= start_byte) |
| 1377 | return NULL; |
| 1378 | return vim_strnsave(str + start_byte, end_byte - start_byte); |
| 1379 | } |
| 1380 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1381 | /* |
| 1382 | * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT. |
| 1383 | * When "dfunc_idx" is negative don't give an error. |
| 1384 | * Returns NULL for an error. |
| 1385 | */ |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1386 | static svar_T * |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1387 | get_script_svar(scriptref_T *sref, int dfunc_idx) |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1388 | { |
| 1389 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1390 | dfunc_T *dfunc = dfunc_idx < 0 ? NULL |
| 1391 | : ((dfunc_T *)def_functions.ga_data) + dfunc_idx; |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1392 | svar_T *sv; |
| 1393 | |
| 1394 | if (sref->sref_seq != si->sn_script_seq) |
| 1395 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1396 | // The script was reloaded after the function was compiled, the |
| 1397 | // script_idx may not be valid. |
| 1398 | if (dfunc != NULL) |
| 1399 | semsg(_(e_script_variable_invalid_after_reload_in_function_str), |
| 1400 | printable_func_name(dfunc->df_ufunc)); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1401 | return NULL; |
| 1402 | } |
| 1403 | sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx; |
Bram Moolenaar | 60dc827 | 2021-07-29 22:48:54 +0200 | [diff] [blame] | 1404 | if (!equal_type(sv->sv_type, sref->sref_type, 0)) |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1405 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 1406 | if (dfunc != NULL) |
| 1407 | emsg(_(e_script_variable_type_changed)); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1408 | return NULL; |
| 1409 | } |
| 1410 | return sv; |
| 1411 | } |
| 1412 | |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1413 | /* |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1414 | * Function passed to do_cmdline() for splitting a script joined by NL |
| 1415 | * characters. |
| 1416 | */ |
| 1417 | static char_u * |
| 1418 | get_split_sourceline( |
| 1419 | int c UNUSED, |
| 1420 | void *cookie, |
| 1421 | int indent UNUSED, |
| 1422 | getline_opt_T options UNUSED) |
| 1423 | { |
| 1424 | source_cookie_T *sp = (source_cookie_T *)cookie; |
| 1425 | char_u *p; |
| 1426 | char_u *line; |
| 1427 | |
| 1428 | if (*sp->nextline == NUL) |
| 1429 | return NULL; |
| 1430 | p = vim_strchr(sp->nextline, '\n'); |
| 1431 | if (p == NULL) |
| 1432 | { |
| 1433 | line = vim_strsave(sp->nextline); |
| 1434 | sp->nextline += STRLEN(sp->nextline); |
| 1435 | } |
| 1436 | else |
| 1437 | { |
| 1438 | line = vim_strnsave(sp->nextline, p - sp->nextline); |
| 1439 | sp->nextline = p + 1; |
| 1440 | } |
| 1441 | return line; |
| 1442 | } |
| 1443 | |
| 1444 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1445 | * Execute a function by "name". |
| 1446 | * This can be a builtin function, user function or a funcref. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 1447 | * "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] | 1448 | */ |
| 1449 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1450 | call_eval_func( |
| 1451 | char_u *name, |
| 1452 | int argcount, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1453 | ectx_T *ectx, |
| 1454 | isn_T *iptr) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1455 | { |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1456 | int called_emsg_before = called_emsg; |
| 1457 | int res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1458 | |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 1459 | res = call_by_name(name, argcount, ectx, iptr, NULL); |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1460 | if (res == FAIL && called_emsg == called_emsg_before) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1461 | { |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 1462 | dictitem_T *v; |
| 1463 | |
| 1464 | v = find_var(name, NULL, FALSE); |
| 1465 | if (v == NULL) |
| 1466 | { |
Bram Moolenaar | e124204 | 2021-12-16 20:56:57 +0000 | [diff] [blame] | 1467 | semsg(_(e_unknown_function_str), name); |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 1468 | return FAIL; |
| 1469 | } |
| 1470 | if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC) |
| 1471 | { |
Bram Moolenaar | e124204 | 2021-12-16 20:56:57 +0000 | [diff] [blame] | 1472 | semsg(_(e_unknown_function_str), name); |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 1473 | return FAIL; |
| 1474 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1475 | return call_partial(&v->di_tv, argcount, ectx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1476 | } |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1477 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | /* |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1481 | * When a function reference is used, fill a partial with the information |
| 1482 | * needed, especially when it is used as a closure. |
| 1483 | */ |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 1484 | int |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1485 | fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx) |
| 1486 | { |
| 1487 | pt->pt_func = ufunc; |
| 1488 | pt->pt_refcount = 1; |
| 1489 | |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 1490 | if (ufunc->uf_flags & FC_CLOSURE) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1491 | { |
| 1492 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 1493 | + ectx->ec_dfunc_idx; |
| 1494 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1495 | // The closure may need to find arguments and local variables in the |
| 1496 | // current stack. |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 1497 | pt->pt_outer.out_stack = &ectx->ec_stack; |
| 1498 | pt->pt_outer.out_frame_idx = ectx->ec_frame_idx; |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1499 | if (ectx->ec_outer_ref != NULL) |
| 1500 | { |
| 1501 | // The current context already has a context, link to that one. |
| 1502 | pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer; |
| 1503 | if (ectx->ec_outer_ref->or_partial != NULL) |
| 1504 | { |
| 1505 | pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial; |
| 1506 | ++pt->pt_outer.out_up_partial->pt_refcount; |
| 1507 | } |
| 1508 | } |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1509 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1510 | // If this function returns and the closure is still being used, we |
| 1511 | // need to make a copy of the context (arguments and local variables). |
| 1512 | // Store a reference to the partial so we can handle that. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1513 | if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1)) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1514 | { |
| 1515 | vim_free(pt); |
| 1516 | return FAIL; |
| 1517 | } |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 1518 | // Extra variable keeps the count of closures created in the current |
| 1519 | // function call. |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1520 | ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx |
| 1521 | + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number; |
| 1522 | |
| 1523 | ((partial_T **)ectx->ec_funcrefs.ga_data) |
| 1524 | [ectx->ec_funcrefs.ga_len] = pt; |
| 1525 | ++pt->pt_refcount; |
| 1526 | ++ectx->ec_funcrefs.ga_len; |
| 1527 | } |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 1528 | ++ufunc->uf_refcount; |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1529 | return OK; |
| 1530 | } |
| 1531 | |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 1532 | /* |
| 1533 | * Execute iptr->isn_arg.string as an Ex command. |
| 1534 | */ |
| 1535 | static int |
| 1536 | exec_command(isn_T *iptr) |
| 1537 | { |
| 1538 | source_cookie_T cookie; |
| 1539 | |
| 1540 | SOURCING_LNUM = iptr->isn_lnum; |
| 1541 | // Pass getsourceline to get an error for a missing ":end" |
| 1542 | // command. |
| 1543 | CLEAR_FIELD(cookie); |
| 1544 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 1545 | if (do_cmdline(iptr->isn_arg.string, |
| 1546 | getsourceline, &cookie, |
| 1547 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL |
| 1548 | || did_emsg) |
| 1549 | return FAIL; |
| 1550 | return OK; |
| 1551 | } |
| 1552 | |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 1553 | // used for v_instr of typval of VAR_INSTR |
| 1554 | struct instr_S { |
| 1555 | ectx_T *instr_ectx; |
| 1556 | isn_T *instr_instr; |
| 1557 | }; |
| 1558 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1559 | // used for substitute_instr |
| 1560 | typedef struct subs_expr_S { |
| 1561 | ectx_T *subs_ectx; |
| 1562 | isn_T *subs_instr; |
| 1563 | int subs_status; |
| 1564 | } subs_expr_T; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1565 | |
| 1566 | // Get pointer to item in the stack. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1567 | #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1568 | |
| 1569 | // Get pointer to item at the bottom of the stack, -1 is the bottom. |
| 1570 | #undef STACK_TV_BOT |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1571 | #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] | 1572 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1573 | // Get pointer to a local variable on the stack. Negative for arguments. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1574 | #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] | 1575 | |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1576 | // Set when calling do_debug(). |
| 1577 | static ectx_T *debug_context = NULL; |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1578 | static int debug_var_count; |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1579 | |
| 1580 | /* |
| 1581 | * When debugging lookup "name" and return the typeval. |
| 1582 | * When not found return NULL. |
| 1583 | */ |
| 1584 | typval_T * |
| 1585 | lookup_debug_var(char_u *name) |
| 1586 | { |
| 1587 | int idx; |
| 1588 | dfunc_T *dfunc; |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1589 | ufunc_T *ufunc; |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1590 | ectx_T *ectx = debug_context; |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1591 | int varargs_off; |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1592 | |
| 1593 | if (ectx == NULL) |
| 1594 | return NULL; |
| 1595 | dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx; |
| 1596 | |
| 1597 | // Go through the local variable names, from last to first. |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1598 | for (idx = debug_var_count - 1; idx >= 0; --idx) |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1599 | { |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1600 | 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] | 1601 | return STACK_TV_VAR(idx); |
| 1602 | } |
| 1603 | |
Bram Moolenaar | 6bc30b0 | 2021-06-16 19:19:55 +0200 | [diff] [blame] | 1604 | // Go through argument names. |
| 1605 | ufunc = dfunc->df_ufunc; |
| 1606 | varargs_off = ufunc->uf_va_name == NULL ? 0 : 1; |
| 1607 | for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx) |
| 1608 | if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0) |
| 1609 | return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len |
| 1610 | - varargs_off + idx); |
| 1611 | if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0) |
| 1612 | return STACK_TV(ectx->ec_frame_idx - 1); |
| 1613 | |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 1614 | return NULL; |
| 1615 | } |
| 1616 | |
Bram Moolenaar | 26a4484 | 2021-09-02 18:49:06 +0200 | [diff] [blame] | 1617 | /* |
| 1618 | * Return TRUE if there might be a breakpoint in "ufunc", which is when a |
| 1619 | * breakpoint was set in that function or when there is any expression. |
| 1620 | */ |
| 1621 | int |
| 1622 | may_break_in_function(ufunc_T *ufunc) |
| 1623 | { |
| 1624 | return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint(); |
| 1625 | } |
| 1626 | |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1627 | static void |
| 1628 | handle_debug(isn_T *iptr, ectx_T *ectx) |
| 1629 | { |
| 1630 | char_u *line; |
| 1631 | ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data) |
| 1632 | + ectx->ec_dfunc_idx)->df_ufunc; |
| 1633 | isn_T *ni; |
| 1634 | int end_lnum = iptr->isn_lnum; |
| 1635 | garray_T ga; |
| 1636 | int lnum; |
| 1637 | |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 1638 | if (ex_nesting_level > debug_break_level) |
| 1639 | { |
| 1640 | linenr_T breakpoint; |
| 1641 | |
Bram Moolenaar | 26a4484 | 2021-09-02 18:49:06 +0200 | [diff] [blame] | 1642 | if (!may_break_in_function(ufunc)) |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 1643 | return; |
| 1644 | |
| 1645 | // check for the next breakpoint if needed |
| 1646 | breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, |
Bram Moolenaar | 8cec927 | 2021-06-23 20:20:53 +0200 | [diff] [blame] | 1647 | iptr->isn_arg.debug.dbg_break_lnum); |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 1648 | if (breakpoint <= 0 || breakpoint > iptr->isn_lnum) |
| 1649 | return; |
| 1650 | } |
| 1651 | |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1652 | SOURCING_LNUM = iptr->isn_lnum; |
| 1653 | debug_context = ectx; |
Bram Moolenaar | 8cec927 | 2021-06-23 20:20:53 +0200 | [diff] [blame] | 1654 | debug_var_count = iptr->isn_arg.debug.dbg_var_names_len; |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1655 | |
| 1656 | for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni) |
| 1657 | if (ni->isn_type == ISN_DEBUG |
| 1658 | || ni->isn_type == ISN_RETURN |
| 1659 | || ni->isn_type == ISN_RETURN_VOID) |
| 1660 | { |
Bram Moolenaar | 112bed0 | 2021-11-23 22:16:34 +0000 | [diff] [blame] | 1661 | end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1); |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1662 | break; |
| 1663 | } |
| 1664 | |
| 1665 | if (end_lnum > iptr->isn_lnum) |
| 1666 | { |
| 1667 | ga_init2(&ga, sizeof(char_u *), 10); |
Bram Moolenaar | 310091d | 2021-12-23 21:14:37 +0000 | [diff] [blame] | 1668 | for (lnum = iptr->isn_lnum; lnum < end_lnum |
| 1669 | && lnum <= ufunc->uf_lines.ga_len; ++lnum) |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 1670 | { |
Bram Moolenaar | 303215d | 2021-07-07 20:10:43 +0200 | [diff] [blame] | 1671 | char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1]; |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 1672 | |
Bram Moolenaar | 303215d | 2021-07-07 20:10:43 +0200 | [diff] [blame] | 1673 | if (p == NULL) |
| 1674 | continue; // left over from continuation line |
| 1675 | p = skipwhite(p); |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 1676 | if (*p == '#') |
| 1677 | break; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 1678 | if (GA_GROW_OK(&ga, 1)) |
Bram Moolenaar | 59b50c3 | 2021-06-17 22:27:48 +0200 | [diff] [blame] | 1679 | ((char_u **)(ga.ga_data))[ga.ga_len++] = p; |
| 1680 | if (STRNCMP(p, "def ", 4) == 0) |
| 1681 | break; |
| 1682 | } |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1683 | line = ga_concat_strings(&ga, " "); |
| 1684 | vim_free(ga.ga_data); |
| 1685 | } |
| 1686 | else |
| 1687 | line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1]; |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1688 | |
Dominique Pelle | 6e96955 | 2021-06-17 13:53:41 +0200 | [diff] [blame] | 1689 | do_debug(line == NULL ? (char_u *)"[empty]" : line); |
Bram Moolenaar | 4cea536 | 2021-06-16 22:24:40 +0200 | [diff] [blame] | 1690 | debug_context = NULL; |
| 1691 | |
| 1692 | if (end_lnum > iptr->isn_lnum) |
| 1693 | vim_free(line); |
| 1694 | } |
| 1695 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1696 | /* |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 1697 | * Store a value in a list or dict variable. |
| 1698 | * Returns OK, FAIL or NOTDONE (uncatchable error). |
| 1699 | */ |
| 1700 | static int |
| 1701 | execute_storeindex(isn_T *iptr, ectx_T *ectx) |
| 1702 | { |
| 1703 | vartype_T dest_type = iptr->isn_arg.vartype; |
| 1704 | typval_T *tv; |
| 1705 | typval_T *tv_idx = STACK_TV_BOT(-2); |
| 1706 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 1707 | int status = OK; |
| 1708 | |
| 1709 | // Stack contains: |
| 1710 | // -3 value to be stored |
| 1711 | // -2 index |
| 1712 | // -1 dict or list |
| 1713 | tv = STACK_TV_BOT(-3); |
| 1714 | SOURCING_LNUM = iptr->isn_lnum; |
| 1715 | if (dest_type == VAR_ANY) |
| 1716 | { |
| 1717 | dest_type = tv_dest->v_type; |
| 1718 | if (dest_type == VAR_DICT) |
| 1719 | status = do_2string(tv_idx, TRUE, FALSE); |
| 1720 | else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER) |
| 1721 | { |
| 1722 | emsg(_(e_number_expected)); |
| 1723 | status = FAIL; |
| 1724 | } |
| 1725 | } |
| 1726 | else if (dest_type != tv_dest->v_type) |
| 1727 | { |
| 1728 | // just in case, should be OK |
| 1729 | semsg(_(e_expected_str_but_got_str), |
| 1730 | vartype_name(dest_type), |
| 1731 | vartype_name(tv_dest->v_type)); |
| 1732 | status = FAIL; |
| 1733 | } |
| 1734 | |
| 1735 | if (status == OK && dest_type == VAR_LIST) |
| 1736 | { |
| 1737 | long lidx = (long)tv_idx->vval.v_number; |
| 1738 | list_T *list = tv_dest->vval.v_list; |
| 1739 | |
| 1740 | if (list == NULL) |
| 1741 | { |
| 1742 | emsg(_(e_list_not_set)); |
| 1743 | return FAIL; |
| 1744 | } |
| 1745 | if (lidx < 0 && list->lv_len + lidx >= 0) |
| 1746 | // negative index is relative to the end |
| 1747 | lidx = list->lv_len + lidx; |
| 1748 | if (lidx < 0 || lidx > list->lv_len) |
| 1749 | { |
| 1750 | semsg(_(e_list_index_out_of_range_nr), lidx); |
| 1751 | return FAIL; |
| 1752 | } |
| 1753 | if (lidx < list->lv_len) |
| 1754 | { |
| 1755 | listitem_T *li = list_find(list, lidx); |
| 1756 | |
Bram Moolenaar | 70c43d8 | 2022-01-26 21:01:15 +0000 | [diff] [blame] | 1757 | if (error_if_locked(li->li_tv.v_lock, |
| 1758 | e_cannot_change_locked_list_item)) |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 1759 | return FAIL; |
| 1760 | // overwrite existing list item |
| 1761 | clear_tv(&li->li_tv); |
| 1762 | li->li_tv = *tv; |
| 1763 | } |
| 1764 | else |
| 1765 | { |
Bram Moolenaar | 70c43d8 | 2022-01-26 21:01:15 +0000 | [diff] [blame] | 1766 | if (error_if_locked(list->lv_lock, e_cannot_change_locked_list)) |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 1767 | return FAIL; |
| 1768 | // append to list, only fails when out of memory |
| 1769 | if (list_append_tv(list, tv) == FAIL) |
| 1770 | return NOTDONE; |
| 1771 | clear_tv(tv); |
| 1772 | } |
| 1773 | } |
| 1774 | else if (status == OK && dest_type == VAR_DICT) |
| 1775 | { |
| 1776 | char_u *key = tv_idx->vval.v_string; |
| 1777 | dict_T *dict = tv_dest->vval.v_dict; |
| 1778 | dictitem_T *di; |
| 1779 | |
| 1780 | SOURCING_LNUM = iptr->isn_lnum; |
| 1781 | if (dict == NULL) |
| 1782 | { |
| 1783 | emsg(_(e_dictionary_not_set)); |
| 1784 | return FAIL; |
| 1785 | } |
| 1786 | if (key == NULL) |
| 1787 | key = (char_u *)""; |
| 1788 | di = dict_find(dict, key, -1); |
| 1789 | if (di != NULL) |
| 1790 | { |
| 1791 | if (error_if_locked(di->di_tv.v_lock, e_cannot_change_dict_item)) |
| 1792 | return FAIL; |
| 1793 | // overwrite existing value |
| 1794 | clear_tv(&di->di_tv); |
| 1795 | di->di_tv = *tv; |
| 1796 | } |
| 1797 | else |
| 1798 | { |
| 1799 | if (error_if_locked(dict->dv_lock, e_cannot_change_dict)) |
| 1800 | return FAIL; |
| 1801 | // add to dict, only fails when out of memory |
| 1802 | if (dict_add_tv(dict, (char *)key, tv) == FAIL) |
| 1803 | return NOTDONE; |
| 1804 | clear_tv(tv); |
| 1805 | } |
| 1806 | } |
| 1807 | else if (status == OK && dest_type == VAR_BLOB) |
| 1808 | { |
| 1809 | long lidx = (long)tv_idx->vval.v_number; |
| 1810 | blob_T *blob = tv_dest->vval.v_blob; |
| 1811 | varnumber_T nr; |
| 1812 | int error = FALSE; |
| 1813 | int len; |
| 1814 | |
| 1815 | if (blob == NULL) |
| 1816 | { |
| 1817 | emsg(_(e_blob_not_set)); |
| 1818 | return FAIL; |
| 1819 | } |
| 1820 | len = blob_len(blob); |
| 1821 | if (lidx < 0 && len + lidx >= 0) |
| 1822 | // negative index is relative to the end |
| 1823 | lidx = len + lidx; |
| 1824 | |
| 1825 | // Can add one byte at the end. |
| 1826 | if (lidx < 0 || lidx > len) |
| 1827 | { |
| 1828 | semsg(_(e_blob_index_out_of_range_nr), lidx); |
| 1829 | return FAIL; |
| 1830 | } |
| 1831 | if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE)) |
| 1832 | return FAIL; |
| 1833 | nr = tv_get_number_chk(tv, &error); |
| 1834 | if (error) |
| 1835 | return FAIL; |
| 1836 | blob_set_append(blob, lidx, nr); |
| 1837 | } |
| 1838 | else |
| 1839 | { |
| 1840 | status = FAIL; |
| 1841 | semsg(_(e_cannot_index_str), vartype_name(dest_type)); |
| 1842 | } |
| 1843 | |
| 1844 | clear_tv(tv_idx); |
| 1845 | clear_tv(tv_dest); |
| 1846 | ectx->ec_stack.ga_len -= 3; |
| 1847 | if (status == FAIL) |
| 1848 | { |
| 1849 | clear_tv(tv); |
| 1850 | return FAIL; |
| 1851 | } |
| 1852 | return OK; |
| 1853 | } |
| 1854 | |
| 1855 | /* |
| 1856 | * Store a value in a blob range. |
| 1857 | */ |
| 1858 | static int |
| 1859 | execute_storerange(isn_T *iptr, ectx_T *ectx) |
| 1860 | { |
| 1861 | typval_T *tv; |
| 1862 | typval_T *tv_idx1 = STACK_TV_BOT(-3); |
| 1863 | typval_T *tv_idx2 = STACK_TV_BOT(-2); |
| 1864 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 1865 | int status = OK; |
| 1866 | |
| 1867 | // Stack contains: |
| 1868 | // -4 value to be stored |
| 1869 | // -3 first index or "none" |
| 1870 | // -2 second index or "none" |
| 1871 | // -1 destination list or blob |
| 1872 | tv = STACK_TV_BOT(-4); |
| 1873 | if (tv_dest->v_type == VAR_LIST) |
| 1874 | { |
| 1875 | long n1; |
| 1876 | long n2; |
| 1877 | int error = FALSE; |
| 1878 | |
| 1879 | SOURCING_LNUM = iptr->isn_lnum; |
| 1880 | n1 = (long)tv_get_number_chk(tv_idx1, &error); |
| 1881 | if (error) |
| 1882 | status = FAIL; |
| 1883 | else |
| 1884 | { |
| 1885 | if (tv_idx2->v_type == VAR_SPECIAL |
| 1886 | && tv_idx2->vval.v_number == VVAL_NONE) |
| 1887 | n2 = list_len(tv_dest->vval.v_list) - 1; |
| 1888 | else |
| 1889 | n2 = (long)tv_get_number_chk(tv_idx2, &error); |
| 1890 | if (error) |
| 1891 | status = FAIL; |
| 1892 | else |
| 1893 | { |
| 1894 | listitem_T *li1 = check_range_index_one( |
| 1895 | tv_dest->vval.v_list, &n1, FALSE); |
| 1896 | |
| 1897 | if (li1 == NULL) |
| 1898 | status = FAIL; |
| 1899 | else |
| 1900 | { |
| 1901 | status = check_range_index_two( |
| 1902 | tv_dest->vval.v_list, |
| 1903 | &n1, li1, &n2, FALSE); |
| 1904 | if (status != FAIL) |
| 1905 | status = list_assign_range( |
| 1906 | tv_dest->vval.v_list, |
| 1907 | tv->vval.v_list, |
| 1908 | n1, |
| 1909 | n2, |
| 1910 | tv_idx2->v_type == VAR_SPECIAL, |
| 1911 | (char_u *)"=", |
| 1912 | (char_u *)"[unknown]"); |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | } |
| 1917 | else if (tv_dest->v_type == VAR_BLOB) |
| 1918 | { |
| 1919 | varnumber_T n1; |
| 1920 | varnumber_T n2; |
| 1921 | int error = FALSE; |
| 1922 | |
| 1923 | n1 = tv_get_number_chk(tv_idx1, &error); |
| 1924 | if (error) |
| 1925 | status = FAIL; |
| 1926 | else |
| 1927 | { |
| 1928 | if (tv_idx2->v_type == VAR_SPECIAL |
| 1929 | && tv_idx2->vval.v_number == VVAL_NONE) |
| 1930 | n2 = blob_len(tv_dest->vval.v_blob) - 1; |
| 1931 | else |
| 1932 | n2 = tv_get_number_chk(tv_idx2, &error); |
| 1933 | if (error) |
| 1934 | status = FAIL; |
| 1935 | else |
| 1936 | { |
| 1937 | long bloblen = blob_len(tv_dest->vval.v_blob); |
| 1938 | |
| 1939 | if (check_blob_index(bloblen, |
| 1940 | n1, FALSE) == FAIL |
| 1941 | || check_blob_range(bloblen, |
| 1942 | n1, n2, FALSE) == FAIL) |
| 1943 | status = FAIL; |
| 1944 | else |
| 1945 | status = blob_set_range( |
| 1946 | tv_dest->vval.v_blob, n1, n2, tv); |
| 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | else |
| 1951 | { |
| 1952 | status = FAIL; |
| 1953 | emsg(_(e_blob_required)); |
| 1954 | } |
| 1955 | |
| 1956 | clear_tv(tv_idx1); |
| 1957 | clear_tv(tv_idx2); |
| 1958 | clear_tv(tv_dest); |
| 1959 | ectx->ec_stack.ga_len -= 4; |
| 1960 | clear_tv(tv); |
| 1961 | |
| 1962 | return status; |
| 1963 | } |
| 1964 | |
| 1965 | /* |
| 1966 | * Unlet item in list or dict variable. |
| 1967 | */ |
| 1968 | static int |
| 1969 | execute_unletindex(isn_T *iptr, ectx_T *ectx) |
| 1970 | { |
| 1971 | typval_T *tv_idx = STACK_TV_BOT(-2); |
| 1972 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 1973 | int status = OK; |
| 1974 | |
| 1975 | // Stack contains: |
| 1976 | // -2 index |
| 1977 | // -1 dict or list |
| 1978 | if (tv_dest->v_type == VAR_DICT) |
| 1979 | { |
| 1980 | // unlet a dict item, index must be a string |
| 1981 | if (tv_idx->v_type != VAR_STRING) |
| 1982 | { |
| 1983 | SOURCING_LNUM = iptr->isn_lnum; |
| 1984 | semsg(_(e_expected_str_but_got_str), |
| 1985 | vartype_name(VAR_STRING), |
| 1986 | vartype_name(tv_idx->v_type)); |
| 1987 | status = FAIL; |
| 1988 | } |
| 1989 | else |
| 1990 | { |
| 1991 | dict_T *d = tv_dest->vval.v_dict; |
| 1992 | char_u *key = tv_idx->vval.v_string; |
| 1993 | dictitem_T *di = NULL; |
| 1994 | |
| 1995 | if (d != NULL && value_check_lock( |
| 1996 | d->dv_lock, NULL, FALSE)) |
| 1997 | status = FAIL; |
| 1998 | else |
| 1999 | { |
| 2000 | SOURCING_LNUM = iptr->isn_lnum; |
| 2001 | if (key == NULL) |
| 2002 | key = (char_u *)""; |
| 2003 | if (d != NULL) |
| 2004 | di = dict_find(d, key, (int)STRLEN(key)); |
| 2005 | if (di == NULL) |
| 2006 | { |
| 2007 | // NULL dict is equivalent to empty dict |
| 2008 | semsg(_(e_key_not_present_in_dictionary), |
| 2009 | key); |
| 2010 | status = FAIL; |
| 2011 | } |
| 2012 | else if (var_check_fixed(di->di_flags, |
| 2013 | NULL, FALSE) |
| 2014 | || var_check_ro(di->di_flags, |
| 2015 | NULL, FALSE)) |
| 2016 | status = FAIL; |
| 2017 | else |
| 2018 | dictitem_remove(d, di); |
| 2019 | } |
| 2020 | } |
| 2021 | } |
| 2022 | else if (tv_dest->v_type == VAR_LIST) |
| 2023 | { |
| 2024 | // unlet a List item, index must be a number |
| 2025 | SOURCING_LNUM = iptr->isn_lnum; |
| 2026 | if (check_for_number(tv_idx) == FAIL) |
| 2027 | { |
| 2028 | status = FAIL; |
| 2029 | } |
| 2030 | else |
| 2031 | { |
| 2032 | list_T *l = tv_dest->vval.v_list; |
| 2033 | long n = (long)tv_idx->vval.v_number; |
| 2034 | |
| 2035 | if (l != NULL && value_check_lock( |
| 2036 | l->lv_lock, NULL, FALSE)) |
| 2037 | status = FAIL; |
| 2038 | else |
| 2039 | { |
| 2040 | listitem_T *li = list_find(l, n); |
| 2041 | |
| 2042 | if (li == NULL) |
| 2043 | { |
| 2044 | SOURCING_LNUM = iptr->isn_lnum; |
| 2045 | semsg(_(e_list_index_out_of_range_nr), n); |
| 2046 | status = FAIL; |
| 2047 | } |
| 2048 | else if (value_check_lock(li->li_tv.v_lock, |
| 2049 | NULL, FALSE)) |
| 2050 | status = FAIL; |
| 2051 | else |
| 2052 | listitem_remove(l, li); |
| 2053 | } |
| 2054 | } |
| 2055 | } |
| 2056 | else |
| 2057 | { |
| 2058 | status = FAIL; |
| 2059 | semsg(_(e_cannot_index_str), |
| 2060 | vartype_name(tv_dest->v_type)); |
| 2061 | } |
| 2062 | |
| 2063 | clear_tv(tv_idx); |
| 2064 | clear_tv(tv_dest); |
| 2065 | ectx->ec_stack.ga_len -= 2; |
| 2066 | |
| 2067 | return status; |
| 2068 | } |
| 2069 | |
| 2070 | /* |
| 2071 | * Unlet a range of items in a list variable. |
| 2072 | */ |
| 2073 | static int |
| 2074 | execute_unletrange(isn_T *iptr, ectx_T *ectx) |
| 2075 | { |
| 2076 | // Stack contains: |
| 2077 | // -3 index1 |
| 2078 | // -2 index2 |
| 2079 | // -1 dict or list |
| 2080 | typval_T *tv_idx1 = STACK_TV_BOT(-3); |
| 2081 | typval_T *tv_idx2 = STACK_TV_BOT(-2); |
| 2082 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2083 | int status = OK; |
| 2084 | |
| 2085 | if (tv_dest->v_type == VAR_LIST) |
| 2086 | { |
| 2087 | // indexes must be a number |
| 2088 | SOURCING_LNUM = iptr->isn_lnum; |
| 2089 | if (check_for_number(tv_idx1) == FAIL |
| 2090 | || (tv_idx2->v_type != VAR_SPECIAL |
| 2091 | && check_for_number(tv_idx2) == FAIL)) |
| 2092 | { |
| 2093 | status = FAIL; |
| 2094 | } |
| 2095 | else |
| 2096 | { |
| 2097 | list_T *l = tv_dest->vval.v_list; |
| 2098 | long n1 = (long)tv_idx1->vval.v_number; |
| 2099 | long n2 = tv_idx2->v_type == VAR_SPECIAL |
| 2100 | ? 0 : (long)tv_idx2->vval.v_number; |
| 2101 | listitem_T *li; |
| 2102 | |
| 2103 | li = list_find_index(l, &n1); |
| 2104 | if (li == NULL) |
| 2105 | status = FAIL; |
| 2106 | else |
| 2107 | { |
| 2108 | if (n1 < 0) |
| 2109 | n1 = list_idx_of_item(l, li); |
| 2110 | if (n2 < 0) |
| 2111 | { |
| 2112 | listitem_T *li2 = list_find(l, n2); |
| 2113 | |
| 2114 | if (li2 == NULL) |
| 2115 | status = FAIL; |
| 2116 | else |
| 2117 | n2 = list_idx_of_item(l, li2); |
| 2118 | } |
| 2119 | if (status != FAIL |
| 2120 | && tv_idx2->v_type != VAR_SPECIAL |
| 2121 | && n2 < n1) |
| 2122 | { |
| 2123 | semsg(_(e_list_index_out_of_range_nr), n2); |
| 2124 | status = FAIL; |
| 2125 | } |
| 2126 | if (status != FAIL |
| 2127 | && list_unlet_range(l, li, NULL, n1, |
| 2128 | tv_idx2->v_type != VAR_SPECIAL, n2) |
| 2129 | == FAIL) |
| 2130 | status = FAIL; |
| 2131 | } |
| 2132 | } |
| 2133 | } |
| 2134 | else |
| 2135 | { |
| 2136 | status = FAIL; |
| 2137 | SOURCING_LNUM = iptr->isn_lnum; |
| 2138 | semsg(_(e_cannot_index_str), |
| 2139 | vartype_name(tv_dest->v_type)); |
| 2140 | } |
| 2141 | |
| 2142 | clear_tv(tv_idx1); |
| 2143 | clear_tv(tv_idx2); |
| 2144 | clear_tv(tv_dest); |
| 2145 | ectx->ec_stack.ga_len -= 3; |
| 2146 | |
| 2147 | return status; |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * Top of a for loop. |
| 2152 | */ |
| 2153 | static int |
| 2154 | execute_for(isn_T *iptr, ectx_T *ectx) |
| 2155 | { |
| 2156 | typval_T *tv; |
| 2157 | typval_T *ltv = STACK_TV_BOT(-1); |
| 2158 | typval_T *idxtv = |
| 2159 | STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); |
| 2160 | |
| 2161 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
| 2162 | return FAIL; |
| 2163 | if (ltv->v_type == VAR_LIST) |
| 2164 | { |
| 2165 | list_T *list = ltv->vval.v_list; |
| 2166 | |
| 2167 | // push the next item from the list |
| 2168 | ++idxtv->vval.v_number; |
| 2169 | if (list == NULL |
| 2170 | || idxtv->vval.v_number >= list->lv_len) |
| 2171 | { |
| 2172 | // past the end of the list, jump to "endfor" |
| 2173 | ectx->ec_iidx = iptr->isn_arg.forloop.for_end; |
| 2174 | may_restore_cmdmod(&ectx->ec_funclocal); |
| 2175 | } |
| 2176 | else if (list->lv_first == &range_list_item) |
| 2177 | { |
| 2178 | // non-materialized range() list |
| 2179 | tv = STACK_TV_BOT(0); |
| 2180 | tv->v_type = VAR_NUMBER; |
| 2181 | tv->v_lock = 0; |
| 2182 | tv->vval.v_number = list_find_nr( |
| 2183 | list, idxtv->vval.v_number, NULL); |
| 2184 | ++ectx->ec_stack.ga_len; |
| 2185 | } |
| 2186 | else |
| 2187 | { |
| 2188 | listitem_T *li = list_find(list, |
| 2189 | idxtv->vval.v_number); |
| 2190 | |
| 2191 | copy_tv(&li->li_tv, STACK_TV_BOT(0)); |
| 2192 | ++ectx->ec_stack.ga_len; |
| 2193 | } |
| 2194 | } |
| 2195 | else if (ltv->v_type == VAR_STRING) |
| 2196 | { |
| 2197 | char_u *str = ltv->vval.v_string; |
| 2198 | |
| 2199 | // The index is for the last byte of the previous |
| 2200 | // character. |
| 2201 | ++idxtv->vval.v_number; |
| 2202 | if (str == NULL || str[idxtv->vval.v_number] == NUL) |
| 2203 | { |
| 2204 | // past the end of the string, jump to "endfor" |
| 2205 | ectx->ec_iidx = iptr->isn_arg.forloop.for_end; |
| 2206 | may_restore_cmdmod(&ectx->ec_funclocal); |
| 2207 | } |
| 2208 | else |
| 2209 | { |
| 2210 | int clen = mb_ptr2len(str + idxtv->vval.v_number); |
| 2211 | |
| 2212 | // Push the next character from the string. |
| 2213 | tv = STACK_TV_BOT(0); |
| 2214 | tv->v_type = VAR_STRING; |
| 2215 | tv->vval.v_string = vim_strnsave( |
| 2216 | str + idxtv->vval.v_number, clen); |
| 2217 | ++ectx->ec_stack.ga_len; |
| 2218 | idxtv->vval.v_number += clen - 1; |
| 2219 | } |
| 2220 | } |
| 2221 | else if (ltv->v_type == VAR_BLOB) |
| 2222 | { |
| 2223 | blob_T *blob = ltv->vval.v_blob; |
| 2224 | |
| 2225 | // When we get here the first time make a copy of the |
| 2226 | // blob, so that the iteration still works when it is |
| 2227 | // changed. |
| 2228 | if (idxtv->vval.v_number == -1 && blob != NULL) |
| 2229 | { |
| 2230 | blob_copy(blob, ltv); |
| 2231 | blob_unref(blob); |
| 2232 | blob = ltv->vval.v_blob; |
| 2233 | } |
| 2234 | |
| 2235 | // The index is for the previous byte. |
| 2236 | ++idxtv->vval.v_number; |
| 2237 | if (blob == NULL |
| 2238 | || idxtv->vval.v_number >= blob_len(blob)) |
| 2239 | { |
| 2240 | // past the end of the blob, jump to "endfor" |
| 2241 | ectx->ec_iidx = iptr->isn_arg.forloop.for_end; |
| 2242 | may_restore_cmdmod(&ectx->ec_funclocal); |
| 2243 | } |
| 2244 | else |
| 2245 | { |
| 2246 | // Push the next byte from the blob. |
| 2247 | tv = STACK_TV_BOT(0); |
| 2248 | tv->v_type = VAR_NUMBER; |
| 2249 | tv->vval.v_number = blob_get(blob, |
| 2250 | idxtv->vval.v_number); |
| 2251 | ++ectx->ec_stack.ga_len; |
| 2252 | } |
| 2253 | } |
| 2254 | else |
| 2255 | { |
| 2256 | semsg(_(e_for_loop_on_str_not_supported), |
| 2257 | vartype_name(ltv->v_type)); |
| 2258 | return FAIL; |
| 2259 | } |
| 2260 | return OK; |
| 2261 | } |
| 2262 | |
| 2263 | /* |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2264 | * Load instruction for w:/b:/g:/t: variable. |
| 2265 | * "isn_type" is used instead of "iptr->isn_type". |
| 2266 | */ |
| 2267 | static int |
| 2268 | load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr) |
| 2269 | { |
| 2270 | dictitem_T *di = NULL; |
| 2271 | hashtab_T *ht = NULL; |
| 2272 | char namespace; |
| 2273 | |
| 2274 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
| 2275 | return NOTDONE; |
| 2276 | switch (isn_type) |
| 2277 | { |
| 2278 | case ISN_LOADG: |
| 2279 | ht = get_globvar_ht(); |
| 2280 | namespace = 'g'; |
| 2281 | break; |
| 2282 | case ISN_LOADB: |
| 2283 | ht = &curbuf->b_vars->dv_hashtab; |
| 2284 | namespace = 'b'; |
| 2285 | break; |
| 2286 | case ISN_LOADW: |
| 2287 | ht = &curwin->w_vars->dv_hashtab; |
| 2288 | namespace = 'w'; |
| 2289 | break; |
| 2290 | case ISN_LOADT: |
| 2291 | ht = &curtab->tp_vars->dv_hashtab; |
| 2292 | namespace = 't'; |
| 2293 | break; |
| 2294 | default: // Cannot reach here |
| 2295 | return NOTDONE; |
| 2296 | } |
| 2297 | di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); |
| 2298 | |
| 2299 | if (di == NULL && ht == get_globvar_ht() |
| 2300 | && vim_strchr(iptr->isn_arg.string, |
| 2301 | AUTOLOAD_CHAR) != NULL) |
| 2302 | { |
| 2303 | // Global variable has an autoload name, may still need |
| 2304 | // to load the script. |
| 2305 | if (script_autoload(iptr->isn_arg.string, FALSE)) |
| 2306 | di = find_var_in_ht(ht, 0, |
| 2307 | iptr->isn_arg.string, TRUE); |
| 2308 | if (did_emsg) |
| 2309 | return FAIL; |
| 2310 | } |
| 2311 | |
| 2312 | if (di == NULL) |
| 2313 | { |
| 2314 | SOURCING_LNUM = iptr->isn_lnum; |
| 2315 | if (vim_strchr(iptr->isn_arg.string, |
| 2316 | AUTOLOAD_CHAR) != NULL) |
| 2317 | // no check if the item exists in the script but |
| 2318 | // isn't exported, it is too complicated |
| 2319 | semsg(_(e_item_not_found_in_script_str), |
| 2320 | iptr->isn_arg.string); |
| 2321 | else |
| 2322 | semsg(_(e_undefined_variable_char_str), |
| 2323 | namespace, iptr->isn_arg.string); |
| 2324 | return FAIL; |
| 2325 | } |
| 2326 | else |
| 2327 | { |
| 2328 | copy_tv(&di->di_tv, STACK_TV_BOT(0)); |
| 2329 | ++ectx->ec_stack.ga_len; |
| 2330 | } |
| 2331 | return OK; |
| 2332 | } |
| 2333 | |
| 2334 | /* |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2335 | * Execute instructions in execution context "ectx". |
| 2336 | * Return OK or FAIL; |
| 2337 | */ |
| 2338 | static int |
| 2339 | exec_instructions(ectx_T *ectx) |
| 2340 | { |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2341 | int ret = FAIL; |
| 2342 | int save_trylevel_at_start = ectx->ec_trylevel_at_start; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 2343 | int dict_stack_len_at_start = dict_stack.ga_len; |
Bram Moolenaar | f785aa1 | 2021-02-11 21:19:34 +0100 | [diff] [blame] | 2344 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 2345 | // Start execution at the first instruction. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2346 | ectx->ec_iidx = 0; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 2347 | |
Bram Moolenaar | ff65288 | 2021-05-16 15:24:49 +0200 | [diff] [blame] | 2348 | // Only catch exceptions in this instruction list. |
| 2349 | ectx->ec_trylevel_at_start = trylevel; |
| 2350 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2351 | for (;;) |
| 2352 | { |
Bram Moolenaar | a749019 | 2021-07-22 12:26:14 +0200 | [diff] [blame] | 2353 | static int breakcheck_count = 0; // using "static" makes it faster |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2354 | isn_T *iptr; |
Bram Moolenaar | a749019 | 2021-07-22 12:26:14 +0200 | [diff] [blame] | 2355 | typval_T *tv; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2356 | |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 2357 | if (unlikely(++breakcheck_count >= 100)) |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 2358 | { |
| 2359 | line_breakcheck(); |
| 2360 | breakcheck_count = 0; |
| 2361 | } |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 2362 | if (unlikely(got_int)) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2363 | { |
| 2364 | // Turn CTRL-C into an exception. |
| 2365 | got_int = FALSE; |
Bram Moolenaar | 97acfc7 | 2020-03-22 13:44:28 +0100 | [diff] [blame] | 2366 | if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2367 | goto theend; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2368 | did_throw = TRUE; |
| 2369 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2370 | |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 2371 | if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL)) |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2372 | { |
| 2373 | // Turn an error message into an exception. |
| 2374 | did_emsg = FALSE; |
| 2375 | if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2376 | goto theend; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2377 | did_throw = TRUE; |
| 2378 | *msg_list = NULL; |
| 2379 | } |
| 2380 | |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 2381 | if (unlikely(did_throw)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2382 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2383 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2384 | trycmd_T *trycmd = NULL; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 2385 | int index = trystack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2386 | |
| 2387 | // An exception jumps to the first catch, finally, or returns from |
| 2388 | // the current function. |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 2389 | while (index > 0) |
| 2390 | { |
| 2391 | trycmd = ((trycmd_T *)trystack->ga_data) + index - 1; |
Bram Moolenaar | 834193a | 2021-06-30 20:39:15 +0200 | [diff] [blame] | 2392 | if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0) |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 2393 | break; |
| 2394 | // In the catch and finally block of this try we have to go up |
| 2395 | // one level. |
| 2396 | --index; |
| 2397 | trycmd = NULL; |
| 2398 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2399 | if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2400 | { |
Bram Moolenaar | 834193a | 2021-06-30 20:39:15 +0200 | [diff] [blame] | 2401 | if (trycmd->tcd_in_catch) |
| 2402 | { |
| 2403 | // exception inside ":catch", jump to ":finally" once |
| 2404 | ectx->ec_iidx = trycmd->tcd_finally_idx; |
| 2405 | trycmd->tcd_finally_idx = 0; |
| 2406 | } |
| 2407 | else |
| 2408 | // jump to first ":catch" |
| 2409 | ectx->ec_iidx = trycmd->tcd_catch_idx; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 2410 | trycmd->tcd_in_catch = TRUE; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 2411 | did_throw = FALSE; // don't come back here until :endtry |
| 2412 | trycmd->tcd_did_throw = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2413 | } |
| 2414 | else |
| 2415 | { |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 2416 | // Not inside try or need to return from current functions. |
| 2417 | // Push a dummy return value. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2418 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2419 | goto theend; |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 2420 | tv = STACK_TV_BOT(0); |
| 2421 | tv->v_type = VAR_NUMBER; |
| 2422 | tv->vval.v_number = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2423 | ++ectx->ec_stack.ga_len; |
| 2424 | if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2425 | { |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 2426 | // At the toplevel we are done. |
Bram Moolenaar | 257cc5e | 2020-02-19 17:06:11 +0100 | [diff] [blame] | 2427 | need_rethrow = TRUE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2428 | if (handle_closure_in_use(ectx, FALSE) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2429 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2430 | goto done; |
| 2431 | } |
| 2432 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2433 | if (func_return(ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2434 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2435 | } |
| 2436 | continue; |
| 2437 | } |
| 2438 | |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 2439 | /* |
| 2440 | * Big switch on the instruction. Most compilers will be turning this |
| 2441 | * into an efficient lookup table, since the "case" values are an enum |
| 2442 | * with sequential numbers. It may look ugly, but it should be the |
| 2443 | * most efficient way. |
| 2444 | */ |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2445 | iptr = &ectx->ec_instr[ectx->ec_iidx++]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2446 | switch (iptr->isn_type) |
| 2447 | { |
| 2448 | // execute Ex command line |
| 2449 | case ISN_EXEC: |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 2450 | if (exec_command(iptr) == FAIL) |
| 2451 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2452 | break; |
| 2453 | |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 2454 | // execute Ex command line split at NL characters. |
| 2455 | case ISN_EXEC_SPLIT: |
| 2456 | { |
| 2457 | source_cookie_T cookie; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 2458 | char_u *line; |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 2459 | |
| 2460 | SOURCING_LNUM = iptr->isn_lnum; |
| 2461 | CLEAR_FIELD(cookie); |
| 2462 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 2463 | cookie.nextline = iptr->isn_arg.string; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 2464 | line = get_split_sourceline(0, &cookie, 0, 0); |
| 2465 | if (do_cmdline(line, |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 2466 | get_split_sourceline, &cookie, |
| 2467 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) |
| 2468 | == FAIL |
| 2469 | || did_emsg) |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 2470 | { |
| 2471 | vim_free(line); |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 2472 | goto on_error; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 2473 | } |
| 2474 | vim_free(line); |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 2475 | } |
| 2476 | break; |
| 2477 | |
Bram Moolenaar | e4eed8c | 2021-12-01 15:22:56 +0000 | [diff] [blame] | 2478 | // execute Ex command line that is only a range |
| 2479 | case ISN_EXECRANGE: |
| 2480 | { |
| 2481 | exarg_T ea; |
| 2482 | char *error = NULL; |
| 2483 | |
| 2484 | CLEAR_FIELD(ea); |
| 2485 | ea.cmdidx = CMD_SIZE; |
| 2486 | ea.addr_type = ADDR_LINES; |
| 2487 | ea.cmd = iptr->isn_arg.string; |
| 2488 | parse_cmd_address(&ea, &error, FALSE); |
Bram Moolenaar | 01a4dcb | 2021-12-04 13:15:10 +0000 | [diff] [blame] | 2489 | if (ea.cmd == NULL) |
| 2490 | goto on_error; |
Bram Moolenaar | e4eed8c | 2021-12-01 15:22:56 +0000 | [diff] [blame] | 2491 | if (error == NULL) |
| 2492 | error = ex_range_without_command(&ea); |
| 2493 | if (error != NULL) |
| 2494 | { |
| 2495 | SOURCING_LNUM = iptr->isn_lnum; |
| 2496 | emsg(error); |
| 2497 | goto on_error; |
| 2498 | } |
| 2499 | } |
| 2500 | break; |
| 2501 | |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 2502 | // Evaluate an expression with legacy syntax, push it onto the |
| 2503 | // stack. |
| 2504 | case ISN_LEGACY_EVAL: |
| 2505 | { |
| 2506 | char_u *arg = iptr->isn_arg.string; |
| 2507 | int res; |
| 2508 | int save_flags = cmdmod.cmod_flags; |
| 2509 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2510 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2511 | goto theend; |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 2512 | tv = STACK_TV_BOT(0); |
| 2513 | init_tv(tv); |
| 2514 | cmdmod.cmod_flags |= CMOD_LEGACY; |
| 2515 | res = eval0(arg, tv, NULL, &EVALARG_EVALUATE); |
| 2516 | cmdmod.cmod_flags = save_flags; |
| 2517 | if (res == FAIL) |
| 2518 | goto on_error; |
| 2519 | ++ectx->ec_stack.ga_len; |
| 2520 | } |
| 2521 | break; |
| 2522 | |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 2523 | // push typeval VAR_INSTR with instructions to be executed |
| 2524 | case ISN_INSTR: |
| 2525 | { |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2526 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2527 | goto theend; |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 2528 | tv = STACK_TV_BOT(0); |
| 2529 | tv->vval.v_instr = ALLOC_ONE(instr_T); |
| 2530 | if (tv->vval.v_instr == NULL) |
| 2531 | goto on_error; |
| 2532 | ++ectx->ec_stack.ga_len; |
| 2533 | |
| 2534 | tv->v_type = VAR_INSTR; |
| 2535 | tv->vval.v_instr->instr_ectx = ectx; |
| 2536 | tv->vval.v_instr->instr_instr = iptr->isn_arg.instr; |
| 2537 | } |
| 2538 | break; |
| 2539 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2540 | // execute :substitute with an expression |
| 2541 | case ISN_SUBSTITUTE: |
| 2542 | { |
| 2543 | subs_T *subs = &iptr->isn_arg.subs; |
| 2544 | source_cookie_T cookie; |
| 2545 | struct subs_expr_S *save_instr = substitute_instr; |
| 2546 | struct subs_expr_S subs_instr; |
| 2547 | int res; |
| 2548 | |
| 2549 | subs_instr.subs_ectx = ectx; |
| 2550 | subs_instr.subs_instr = subs->subs_instr; |
| 2551 | subs_instr.subs_status = OK; |
| 2552 | substitute_instr = &subs_instr; |
| 2553 | |
| 2554 | SOURCING_LNUM = iptr->isn_lnum; |
| 2555 | // This is very much like ISN_EXEC |
| 2556 | CLEAR_FIELD(cookie); |
| 2557 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 2558 | res = do_cmdline(subs->subs_cmd, |
| 2559 | getsourceline, &cookie, |
| 2560 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); |
| 2561 | substitute_instr = save_instr; |
| 2562 | |
| 2563 | if (res == FAIL || did_emsg |
| 2564 | || subs_instr.subs_status == FAIL) |
| 2565 | goto on_error; |
| 2566 | } |
| 2567 | break; |
| 2568 | |
| 2569 | case ISN_FINISH: |
| 2570 | goto done; |
| 2571 | |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 2572 | case ISN_REDIRSTART: |
| 2573 | // create a dummy entry for var_redir_str() |
| 2574 | if (alloc_redir_lval() == FAIL) |
| 2575 | goto on_error; |
| 2576 | |
| 2577 | // The output is stored in growarray "redir_ga" until |
| 2578 | // redirection ends. |
| 2579 | init_redir_ga(); |
| 2580 | redir_vname = 1; |
| 2581 | break; |
| 2582 | |
| 2583 | case ISN_REDIREND: |
| 2584 | { |
| 2585 | char_u *res = get_clear_redir_ga(); |
| 2586 | |
| 2587 | // End redirection, put redirected text on the stack. |
| 2588 | clear_redir_lval(); |
| 2589 | redir_vname = 0; |
| 2590 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2591 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 2592 | { |
| 2593 | vim_free(res); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2594 | goto theend; |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 2595 | } |
| 2596 | tv = STACK_TV_BOT(0); |
| 2597 | tv->v_type = VAR_STRING; |
| 2598 | tv->vval.v_string = res; |
| 2599 | ++ectx->ec_stack.ga_len; |
| 2600 | } |
| 2601 | break; |
| 2602 | |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 2603 | case ISN_CEXPR_AUCMD: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 2604 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 2605 | if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL) |
| 2606 | goto on_error; |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 2607 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 2608 | break; |
| 2609 | |
| 2610 | case ISN_CEXPR_CORE: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 2611 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 2612 | { |
| 2613 | exarg_T ea; |
| 2614 | int res; |
| 2615 | |
| 2616 | CLEAR_FIELD(ea); |
| 2617 | ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx; |
| 2618 | ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit; |
| 2619 | ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline; |
| 2620 | --ectx->ec_stack.ga_len; |
| 2621 | tv = STACK_TV_BOT(0); |
| 2622 | res = cexpr_core(&ea, tv); |
| 2623 | clear_tv(tv); |
| 2624 | if (res == FAIL) |
| 2625 | goto on_error; |
| 2626 | } |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 2627 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 2628 | break; |
| 2629 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 2630 | // execute Ex command from pieces on the stack |
| 2631 | case ISN_EXECCONCAT: |
| 2632 | { |
| 2633 | int count = iptr->isn_arg.number; |
Bram Moolenaar | 7f6f56f | 2020-04-30 20:21:43 +0200 | [diff] [blame] | 2634 | size_t len = 0; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 2635 | int pass; |
| 2636 | int i; |
| 2637 | char_u *cmd = NULL; |
| 2638 | char_u *str; |
| 2639 | |
| 2640 | for (pass = 1; pass <= 2; ++pass) |
| 2641 | { |
| 2642 | for (i = 0; i < count; ++i) |
| 2643 | { |
| 2644 | tv = STACK_TV_BOT(i - count); |
| 2645 | str = tv->vval.v_string; |
| 2646 | if (str != NULL && *str != NUL) |
| 2647 | { |
| 2648 | if (pass == 2) |
| 2649 | STRCPY(cmd + len, str); |
| 2650 | len += STRLEN(str); |
| 2651 | } |
| 2652 | if (pass == 2) |
| 2653 | clear_tv(tv); |
| 2654 | } |
| 2655 | if (pass == 1) |
| 2656 | { |
| 2657 | cmd = alloc(len + 1); |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 2658 | if (unlikely(cmd == NULL)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2659 | goto theend; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 2660 | len = 0; |
| 2661 | } |
| 2662 | } |
| 2663 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 2664 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 2665 | do_cmdline_cmd(cmd); |
| 2666 | vim_free(cmd); |
| 2667 | } |
| 2668 | break; |
| 2669 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2670 | // execute :echo {string} ... |
| 2671 | case ISN_ECHO: |
| 2672 | { |
| 2673 | int count = iptr->isn_arg.echo.echo_count; |
| 2674 | int atstart = TRUE; |
| 2675 | int needclr = TRUE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2676 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2677 | |
| 2678 | for (idx = 0; idx < count; ++idx) |
| 2679 | { |
| 2680 | tv = STACK_TV_BOT(idx - count); |
| 2681 | echo_one(tv, iptr->isn_arg.echo.echo_with_white, |
| 2682 | &atstart, &needclr); |
| 2683 | clear_tv(tv); |
| 2684 | } |
Bram Moolenaar | e0807ea | 2020-02-20 22:18:06 +0100 | [diff] [blame] | 2685 | if (needclr) |
| 2686 | msg_clr_eos(); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2687 | ectx->ec_stack.ga_len -= count; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2688 | } |
| 2689 | break; |
| 2690 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2691 | // :execute {string} ... |
| 2692 | // :echomsg {string} ... |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 2693 | // :echoconsole {string} ... |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2694 | // :echoerr {string} ... |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2695 | case ISN_EXECUTE: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2696 | case ISN_ECHOMSG: |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 2697 | case ISN_ECHOCONSOLE: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2698 | case ISN_ECHOERR: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2699 | { |
| 2700 | int count = iptr->isn_arg.number; |
| 2701 | garray_T ga; |
| 2702 | char_u buf[NUMBUFLEN]; |
| 2703 | char_u *p; |
| 2704 | int len; |
| 2705 | int failed = FALSE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2706 | int idx; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2707 | |
| 2708 | ga_init2(&ga, 1, 80); |
| 2709 | for (idx = 0; idx < count; ++idx) |
| 2710 | { |
| 2711 | tv = STACK_TV_BOT(idx - count); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2712 | if (iptr->isn_type == ISN_EXECUTE) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2713 | { |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2714 | if (tv->v_type == VAR_CHANNEL |
| 2715 | || tv->v_type == VAR_JOB) |
| 2716 | { |
| 2717 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 68db996 | 2021-05-09 23:19:22 +0200 | [diff] [blame] | 2718 | semsg(_(e_using_invalid_value_as_string_str), |
| 2719 | vartype_name(tv->v_type)); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2720 | break; |
| 2721 | } |
| 2722 | else |
| 2723 | p = tv_get_string_buf(tv, buf); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2724 | } |
| 2725 | else |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2726 | p = tv_stringify(tv, buf); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2727 | |
| 2728 | len = (int)STRLEN(p); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2729 | if (GA_GROW_FAILS(&ga, len + 2)) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2730 | failed = TRUE; |
| 2731 | else |
| 2732 | { |
| 2733 | if (ga.ga_len > 0) |
| 2734 | ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; |
| 2735 | STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); |
| 2736 | ga.ga_len += len; |
| 2737 | } |
| 2738 | clear_tv(tv); |
| 2739 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2740 | ectx->ec_stack.ga_len -= count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2741 | if (failed) |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 2742 | { |
| 2743 | ga_clear(&ga); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2744 | goto on_error; |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 2745 | } |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2746 | |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 2747 | if (ga.ga_data != NULL) |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2748 | { |
| 2749 | if (iptr->isn_type == ISN_EXECUTE) |
Bram Moolenaar | 430deb1 | 2020-08-23 16:29:11 +0200 | [diff] [blame] | 2750 | { |
| 2751 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2752 | do_cmdline_cmd((char_u *)ga.ga_data); |
Bram Moolenaar | eeece9e | 2020-11-20 19:26:48 +0100 | [diff] [blame] | 2753 | if (did_emsg) |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 2754 | { |
| 2755 | ga_clear(&ga); |
Bram Moolenaar | eeece9e | 2020-11-20 19:26:48 +0100 | [diff] [blame] | 2756 | goto on_error; |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 2757 | } |
Bram Moolenaar | 430deb1 | 2020-08-23 16:29:11 +0200 | [diff] [blame] | 2758 | } |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2759 | else |
| 2760 | { |
| 2761 | msg_sb_eol(); |
| 2762 | if (iptr->isn_type == ISN_ECHOMSG) |
| 2763 | { |
| 2764 | msg_attr(ga.ga_data, echo_attr); |
| 2765 | out_flush(); |
| 2766 | } |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 2767 | else if (iptr->isn_type == ISN_ECHOCONSOLE) |
| 2768 | { |
| 2769 | ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), |
| 2770 | TRUE); |
| 2771 | ui_write((char_u *)"\r\n", 2, TRUE); |
| 2772 | } |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2773 | else |
| 2774 | { |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2775 | SOURCING_LNUM = iptr->isn_lnum; |
| 2776 | emsg(ga.ga_data); |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2777 | } |
| 2778 | } |
| 2779 | } |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2780 | ga_clear(&ga); |
| 2781 | } |
| 2782 | break; |
| 2783 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2784 | // load local variable or argument |
| 2785 | case ISN_LOAD: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2786 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2787 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2788 | 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] | 2789 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2790 | break; |
| 2791 | |
| 2792 | // load v: variable |
| 2793 | case ISN_LOADV: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2794 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2795 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2796 | 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] | 2797 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2798 | break; |
| 2799 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2800 | // load s: variable in Vim9 script |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2801 | case ISN_LOADSCRIPT: |
| 2802 | { |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 2803 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2804 | svar_T *sv; |
| 2805 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 2806 | sv = get_script_svar(sref, ectx->ec_dfunc_idx); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 2807 | if (sv == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2808 | goto theend; |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 2809 | allocate_if_null(sv->sv_tv); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2810 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2811 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2812 | copy_tv(sv->sv_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2813 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2814 | } |
| 2815 | break; |
| 2816 | |
| 2817 | // load s: variable in old script |
| 2818 | case ISN_LOADS: |
| 2819 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2820 | hashtab_T *ht = &SCRIPT_VARS( |
| 2821 | iptr->isn_arg.loadstore.ls_sid); |
| 2822 | char_u *name = iptr->isn_arg.loadstore.ls_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2823 | dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2824 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2825 | if (di == NULL) |
| 2826 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 2827 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2828 | semsg(_(e_undefined_variable_str), name); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2829 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2830 | } |
| 2831 | else |
| 2832 | { |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2833 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2834 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2835 | copy_tv(&di->di_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2836 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | break; |
| 2840 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2841 | // load g:/b:/w:/t: variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2842 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2843 | case ISN_LOADB: |
| 2844 | case ISN_LOADW: |
| 2845 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2846 | { |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2847 | int res = load_namespace_var(ectx, iptr->isn_type, iptr); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2848 | |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2849 | if (res == NOTDONE) |
Bram Moolenaar | cbbc48f | 2022-01-18 12:58:28 +0000 | [diff] [blame] | 2850 | goto theend; |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2851 | if (res == FAIL) |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2852 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2853 | } |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2854 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2855 | break; |
| 2856 | |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2857 | // load autoload variable |
| 2858 | case ISN_LOADAUTO: |
| 2859 | { |
| 2860 | char_u *name = iptr->isn_arg.string; |
| 2861 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2862 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2863 | goto theend; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2864 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 2865 | if (eval_variable(name, (int)STRLEN(name), 0, |
Bram Moolenaar | cb4e80f | 2021-03-13 20:57:19 +0100 | [diff] [blame] | 2866 | STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL) |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2867 | goto on_error; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2868 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2869 | } |
| 2870 | break; |
| 2871 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2872 | // load g:/b:/w:/t: namespace |
| 2873 | case ISN_LOADGDICT: |
| 2874 | case ISN_LOADBDICT: |
| 2875 | case ISN_LOADWDICT: |
| 2876 | case ISN_LOADTDICT: |
| 2877 | { |
| 2878 | dict_T *d = NULL; |
| 2879 | |
| 2880 | switch (iptr->isn_type) |
| 2881 | { |
Bram Moolenaar | 682d0a1 | 2020-07-19 20:48:59 +0200 | [diff] [blame] | 2882 | case ISN_LOADGDICT: d = get_globvar_dict(); break; |
| 2883 | case ISN_LOADBDICT: d = curbuf->b_vars; break; |
| 2884 | case ISN_LOADWDICT: d = curwin->w_vars; break; |
| 2885 | case ISN_LOADTDICT: d = curtab->tp_vars; break; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2886 | default: // Cannot reach here |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2887 | goto theend; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2888 | } |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2889 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2890 | goto theend; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2891 | tv = STACK_TV_BOT(0); |
| 2892 | tv->v_type = VAR_DICT; |
| 2893 | tv->v_lock = 0; |
| 2894 | tv->vval.v_dict = d; |
Bram Moolenaar | 1bd3cb2 | 2021-02-24 12:27:31 +0100 | [diff] [blame] | 2895 | ++d->dv_refcount; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2896 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2897 | } |
| 2898 | break; |
| 2899 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2900 | // load &option |
| 2901 | case ISN_LOADOPT: |
| 2902 | { |
| 2903 | typval_T optval; |
| 2904 | char_u *name = iptr->isn_arg.string; |
| 2905 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 2906 | // This is not expected to fail, name is checked during |
| 2907 | // compilation: don't set SOURCING_LNUM. |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2908 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2909 | goto theend; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2910 | if (eval_option(&name, &optval, TRUE) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2911 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2912 | *STACK_TV_BOT(0) = optval; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2913 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2914 | } |
| 2915 | break; |
| 2916 | |
| 2917 | // load $ENV |
| 2918 | case ISN_LOADENV: |
| 2919 | { |
| 2920 | typval_T optval; |
| 2921 | char_u *name = iptr->isn_arg.string; |
| 2922 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2923 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2924 | goto theend; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2925 | // name is always valid, checked when compiling |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2926 | (void)eval_env_var(&name, &optval, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2927 | *STACK_TV_BOT(0) = optval; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2928 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2929 | } |
| 2930 | break; |
| 2931 | |
| 2932 | // load @register |
| 2933 | case ISN_LOADREG: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 2934 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2935 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2936 | tv = STACK_TV_BOT(0); |
| 2937 | tv->v_type = VAR_STRING; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2938 | tv->v_lock = 0; |
Bram Moolenaar | 1e021e6 | 2020-10-16 20:25:23 +0200 | [diff] [blame] | 2939 | // This may result in NULL, which should be equivalent to an |
| 2940 | // empty string. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2941 | tv->vval.v_string = get_reg_contents( |
| 2942 | iptr->isn_arg.number, GREG_EXPR_SRC); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2943 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2944 | break; |
| 2945 | |
| 2946 | // store local variable |
| 2947 | case ISN_STORE: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2948 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2949 | tv = STACK_TV_VAR(iptr->isn_arg.number); |
| 2950 | clear_tv(tv); |
| 2951 | *tv = *STACK_TV_BOT(0); |
| 2952 | break; |
| 2953 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2954 | // store s: variable in old script |
| 2955 | case ISN_STORES: |
| 2956 | { |
| 2957 | hashtab_T *ht = &SCRIPT_VARS( |
| 2958 | iptr->isn_arg.loadstore.ls_sid); |
| 2959 | char_u *name = iptr->isn_arg.loadstore.ls_name; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2960 | dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2961 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2962 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2963 | if (di == NULL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2964 | store_var(name, STACK_TV_BOT(0)); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2965 | else |
| 2966 | { |
Bram Moolenaar | dcf29ac | 2021-04-02 14:44:02 +0200 | [diff] [blame] | 2967 | SOURCING_LNUM = iptr->isn_lnum; |
| 2968 | if (var_check_permission(di, name) == FAIL) |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 2969 | { |
| 2970 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | dcf29ac | 2021-04-02 14:44:02 +0200 | [diff] [blame] | 2971 | goto on_error; |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 2972 | } |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2973 | clear_tv(&di->di_tv); |
| 2974 | di->di_tv = *STACK_TV_BOT(0); |
| 2975 | } |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2976 | } |
| 2977 | break; |
| 2978 | |
| 2979 | // store script-local variable in Vim9 script |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2980 | case ISN_STORESCRIPT: |
| 2981 | { |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 2982 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 2983 | svar_T *sv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2984 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 2985 | sv = get_script_svar(sref, ectx->ec_dfunc_idx); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 2986 | if (sv == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2987 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2988 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 2989 | |
| 2990 | // "const" and "final" are checked at compile time, locking |
| 2991 | // the value needs to be checked here. |
| 2992 | SOURCING_LNUM = iptr->isn_lnum; |
| 2993 | 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] | 2994 | { |
| 2995 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 2996 | goto on_error; |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 2997 | } |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 2998 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2999 | clear_tv(sv->sv_tv); |
| 3000 | *sv->sv_tv = *STACK_TV_BOT(0); |
| 3001 | } |
| 3002 | break; |
| 3003 | |
| 3004 | // store option |
| 3005 | case ISN_STOREOPT: |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3006 | case ISN_STOREFUNCOPT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3007 | { |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3008 | char_u *opt_name = iptr->isn_arg.storeopt.so_name; |
| 3009 | int opt_flags = iptr->isn_arg.storeopt.so_flags; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3010 | long n = 0; |
| 3011 | char_u *s = NULL; |
| 3012 | char *msg; |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 3013 | char_u numbuf[NUMBUFLEN]; |
| 3014 | char_u *tofree = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3015 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3016 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3017 | tv = STACK_TV_BOT(0); |
| 3018 | if (tv->v_type == VAR_STRING) |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 3019 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3020 | s = tv->vval.v_string; |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 3021 | if (s == NULL) |
| 3022 | s = (char_u *)""; |
| 3023 | } |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3024 | else if (iptr->isn_type == ISN_STOREFUNCOPT) |
| 3025 | { |
| 3026 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 3027 | // If the option can be set to a function reference or |
| 3028 | // a lambda and the passed value is a function |
| 3029 | // reference, then convert it to the name (string) of |
| 3030 | // the function reference. |
| 3031 | s = tv2string(tv, &tofree, numbuf, 0); |
| 3032 | if (s == NULL || *s == NUL) |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3033 | { |
| 3034 | clear_tv(tv); |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3035 | goto on_error; |
| 3036 | } |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3037 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3038 | else |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3039 | // must be VAR_NUMBER, CHECKTYPE makes sure |
| 3040 | n = tv->vval.v_number; |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 3041 | msg = set_option_value(opt_name, n, s, opt_flags); |
Bram Moolenaar | e75ba26 | 2020-05-16 15:43:31 +0200 | [diff] [blame] | 3042 | clear_tv(tv); |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 3043 | vim_free(tofree); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3044 | if (msg != NULL) |
| 3045 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3046 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3047 | emsg(_(msg)); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3048 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3049 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3050 | } |
| 3051 | break; |
| 3052 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3053 | // store $ENV |
| 3054 | case ISN_STOREENV: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3055 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3056 | tv = STACK_TV_BOT(0); |
| 3057 | vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); |
| 3058 | clear_tv(tv); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3059 | break; |
| 3060 | |
| 3061 | // store @r |
| 3062 | case ISN_STOREREG: |
| 3063 | { |
| 3064 | int reg = iptr->isn_arg.number; |
| 3065 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3066 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 3067 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 74f4a96 | 2021-06-17 21:03:07 +0200 | [diff] [blame] | 3068 | write_reg_contents(reg, tv_get_string(tv), -1, FALSE); |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 3069 | clear_tv(tv); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3070 | } |
| 3071 | break; |
| 3072 | |
| 3073 | // store v: variable |
| 3074 | case ISN_STOREV: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3075 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3076 | if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) |
| 3077 | == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3078 | // should not happen, type is checked when compiling |
| 3079 | goto on_error; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3080 | break; |
| 3081 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 3082 | // store g:/b:/w:/t: variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3083 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 3084 | case ISN_STOREB: |
| 3085 | case ISN_STOREW: |
| 3086 | case ISN_STORET: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3087 | { |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 3088 | dictitem_T *di; |
| 3089 | hashtab_T *ht; |
| 3090 | char_u *name = iptr->isn_arg.string + 2; |
| 3091 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 3092 | switch (iptr->isn_type) |
| 3093 | { |
| 3094 | case ISN_STOREG: |
| 3095 | ht = get_globvar_ht(); |
| 3096 | break; |
| 3097 | case ISN_STOREB: |
| 3098 | ht = &curbuf->b_vars->dv_hashtab; |
| 3099 | break; |
| 3100 | case ISN_STOREW: |
| 3101 | ht = &curwin->w_vars->dv_hashtab; |
| 3102 | break; |
| 3103 | case ISN_STORET: |
| 3104 | ht = &curtab->tp_vars->dv_hashtab; |
| 3105 | break; |
| 3106 | default: // Cannot reach here |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3107 | goto theend; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 3108 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3109 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3110 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 3111 | di = find_var_in_ht(ht, 0, name, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3112 | if (di == NULL) |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 3113 | store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3114 | else |
| 3115 | { |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 3116 | SOURCING_LNUM = iptr->isn_lnum; |
| 3117 | if (var_check_permission(di, name) == FAIL) |
| 3118 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3119 | clear_tv(&di->di_tv); |
| 3120 | di->di_tv = *STACK_TV_BOT(0); |
| 3121 | } |
| 3122 | } |
| 3123 | break; |
| 3124 | |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 3125 | // store an autoload variable |
| 3126 | case ISN_STOREAUTO: |
| 3127 | SOURCING_LNUM = iptr->isn_lnum; |
| 3128 | set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE); |
| 3129 | clear_tv(STACK_TV_BOT(-1)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3130 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 3131 | break; |
| 3132 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3133 | // store number in local variable |
| 3134 | case ISN_STORENR: |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 3135 | tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3136 | clear_tv(tv); |
| 3137 | tv->v_type = VAR_NUMBER; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 3138 | tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3139 | break; |
| 3140 | |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 3141 | // store value in list or dict variable |
| 3142 | case ISN_STOREINDEX: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3143 | { |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3144 | int res = execute_storeindex(iptr, ectx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3145 | |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3146 | if (res == FAIL) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 3147 | goto on_error; |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3148 | if (res == NOTDONE) |
| 3149 | goto theend; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3150 | } |
| 3151 | break; |
| 3152 | |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 3153 | // store value in blob range |
| 3154 | case ISN_STORERANGE: |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3155 | if (execute_storerange(iptr, ectx) == FAIL) |
| 3156 | goto on_error; |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 3157 | break; |
| 3158 | |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3159 | // load or store variable or argument from outer scope |
| 3160 | case ISN_LOADOUTER: |
| 3161 | case ISN_STOREOUTER: |
| 3162 | { |
| 3163 | int depth = iptr->isn_arg.outer.outer_depth; |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 3164 | outer_T *outer = ectx->ec_outer_ref == NULL ? NULL |
| 3165 | : ectx->ec_outer_ref->or_outer; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3166 | |
| 3167 | while (depth > 1 && outer != NULL) |
| 3168 | { |
| 3169 | outer = outer->out_up; |
| 3170 | --depth; |
| 3171 | } |
| 3172 | if (outer == NULL) |
| 3173 | { |
| 3174 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 69c7617 | 2021-12-02 16:38:52 +0000 | [diff] [blame] | 3175 | if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx |
| 3176 | || ectx->ec_outer_ref == NULL) |
| 3177 | // Possibly :def function called from legacy |
| 3178 | // context. |
| 3179 | emsg(_(e_closure_called_from_invalid_context)); |
| 3180 | else |
| 3181 | iemsg("LOADOUTER depth more than scope levels"); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3182 | goto theend; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3183 | } |
| 3184 | tv = ((typval_T *)outer->out_stack->ga_data) |
| 3185 | + outer->out_frame_idx + STACK_FRAME_SIZE |
| 3186 | + iptr->isn_arg.outer.outer_idx; |
| 3187 | if (iptr->isn_type == ISN_LOADOUTER) |
| 3188 | { |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3189 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3190 | goto theend; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3191 | copy_tv(tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3192 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3193 | } |
| 3194 | else |
| 3195 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3196 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 3197 | clear_tv(tv); |
| 3198 | *tv = *STACK_TV_BOT(0); |
| 3199 | } |
| 3200 | } |
| 3201 | break; |
| 3202 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 3203 | // unlet item in list or dict variable |
| 3204 | case ISN_UNLETINDEX: |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3205 | if (execute_unletindex(iptr, ectx) == FAIL) |
| 3206 | goto on_error; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 3207 | break; |
| 3208 | |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 3209 | // unlet range of items in list variable |
| 3210 | case ISN_UNLETRANGE: |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3211 | if (execute_unletrange(iptr, ectx) == FAIL) |
| 3212 | goto on_error; |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 3213 | break; |
| 3214 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3215 | // push constant |
| 3216 | case ISN_PUSHNR: |
| 3217 | case ISN_PUSHBOOL: |
| 3218 | case ISN_PUSHSPEC: |
| 3219 | case ISN_PUSHF: |
| 3220 | case ISN_PUSHS: |
| 3221 | case ISN_PUSHBLOB: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3222 | case ISN_PUSHFUNC: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3223 | case ISN_PUSHCHANNEL: |
| 3224 | case ISN_PUSHJOB: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3225 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3226 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3227 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3228 | tv->v_lock = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3229 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3230 | switch (iptr->isn_type) |
| 3231 | { |
| 3232 | case ISN_PUSHNR: |
| 3233 | tv->v_type = VAR_NUMBER; |
| 3234 | tv->vval.v_number = iptr->isn_arg.number; |
| 3235 | break; |
| 3236 | case ISN_PUSHBOOL: |
| 3237 | tv->v_type = VAR_BOOL; |
| 3238 | tv->vval.v_number = iptr->isn_arg.number; |
| 3239 | break; |
| 3240 | case ISN_PUSHSPEC: |
| 3241 | tv->v_type = VAR_SPECIAL; |
| 3242 | tv->vval.v_number = iptr->isn_arg.number; |
| 3243 | break; |
| 3244 | #ifdef FEAT_FLOAT |
| 3245 | case ISN_PUSHF: |
| 3246 | tv->v_type = VAR_FLOAT; |
| 3247 | tv->vval.v_float = iptr->isn_arg.fnumber; |
| 3248 | break; |
| 3249 | #endif |
| 3250 | case ISN_PUSHBLOB: |
| 3251 | blob_copy(iptr->isn_arg.blob, tv); |
| 3252 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3253 | case ISN_PUSHFUNC: |
| 3254 | tv->v_type = VAR_FUNC; |
Bram Moolenaar | 087d2e1 | 2020-03-01 15:36:42 +0100 | [diff] [blame] | 3255 | if (iptr->isn_arg.string == NULL) |
| 3256 | tv->vval.v_string = NULL; |
| 3257 | else |
| 3258 | tv->vval.v_string = |
| 3259 | vim_strsave(iptr->isn_arg.string); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3260 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 3261 | case ISN_PUSHCHANNEL: |
| 3262 | #ifdef FEAT_JOB_CHANNEL |
| 3263 | tv->v_type = VAR_CHANNEL; |
| 3264 | tv->vval.v_channel = iptr->isn_arg.channel; |
| 3265 | if (tv->vval.v_channel != NULL) |
| 3266 | ++tv->vval.v_channel->ch_refcount; |
| 3267 | #endif |
| 3268 | break; |
| 3269 | case ISN_PUSHJOB: |
| 3270 | #ifdef FEAT_JOB_CHANNEL |
| 3271 | tv->v_type = VAR_JOB; |
| 3272 | tv->vval.v_job = iptr->isn_arg.job; |
| 3273 | if (tv->vval.v_job != NULL) |
| 3274 | ++tv->vval.v_job->jv_refcount; |
| 3275 | #endif |
| 3276 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3277 | default: |
| 3278 | tv->v_type = VAR_STRING; |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 3279 | tv->vval.v_string = vim_strsave( |
| 3280 | iptr->isn_arg.string == NULL |
| 3281 | ? (char_u *)"" : iptr->isn_arg.string); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3282 | } |
| 3283 | break; |
| 3284 | |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 3285 | case ISN_AUTOLOAD: |
| 3286 | { |
| 3287 | char_u *name = iptr->isn_arg.string; |
| 3288 | |
| 3289 | (void)script_autoload(name, FALSE); |
| 3290 | if (find_func(name, TRUE)) |
| 3291 | { |
| 3292 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
| 3293 | goto theend; |
| 3294 | tv = STACK_TV_BOT(0); |
| 3295 | tv->v_lock = 0; |
| 3296 | ++ectx->ec_stack.ga_len; |
| 3297 | tv->v_type = VAR_FUNC; |
| 3298 | tv->vval.v_string = vim_strsave(name); |
| 3299 | } |
| 3300 | else |
| 3301 | { |
| 3302 | int res = load_namespace_var(ectx, ISN_LOADG, iptr); |
| 3303 | |
| 3304 | if (res == NOTDONE) |
| 3305 | goto theend; |
| 3306 | if (res == FAIL) |
| 3307 | goto on_error; |
| 3308 | } |
| 3309 | } |
| 3310 | break; |
| 3311 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 3312 | case ISN_UNLET: |
| 3313 | if (do_unlet(iptr->isn_arg.unlet.ul_name, |
| 3314 | iptr->isn_arg.unlet.ul_forceit) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3315 | goto on_error; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 3316 | break; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 3317 | case ISN_UNLETENV: |
| 3318 | vim_unsetenv(iptr->isn_arg.unlet.ul_name); |
| 3319 | break; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 3320 | |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 3321 | case ISN_LOCKUNLOCK: |
| 3322 | { |
| 3323 | typval_T *lval_root_save = lval_root; |
| 3324 | int res; |
| 3325 | |
| 3326 | // Stack has the local variable, argument the whole :lock |
| 3327 | // or :unlock command, like ISN_EXEC. |
| 3328 | --ectx->ec_stack.ga_len; |
| 3329 | lval_root = STACK_TV_BOT(0); |
| 3330 | res = exec_command(iptr); |
| 3331 | clear_tv(lval_root); |
| 3332 | lval_root = lval_root_save; |
| 3333 | if (res == FAIL) |
| 3334 | goto on_error; |
| 3335 | } |
| 3336 | break; |
| 3337 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 3338 | case ISN_LOCKCONST: |
| 3339 | item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE); |
| 3340 | break; |
| 3341 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3342 | // create a list from items on the stack; uses a single allocation |
| 3343 | // for the list header and the items |
| 3344 | case ISN_NEWLIST: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3345 | if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3346 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3347 | break; |
| 3348 | |
| 3349 | // create a dict from items on the stack |
| 3350 | case ISN_NEWDICT: |
| 3351 | { |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3352 | int count = iptr->isn_arg.number; |
| 3353 | dict_T *dict = dict_alloc(); |
| 3354 | dictitem_T *item; |
Bram Moolenaar | c7f7f6d | 2020-11-04 13:38:28 +0100 | [diff] [blame] | 3355 | char_u *key; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3356 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3357 | |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 3358 | if (unlikely(dict == NULL)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3359 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3360 | for (idx = 0; idx < count; ++idx) |
| 3361 | { |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3362 | // have already checked key type is VAR_STRING |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3363 | tv = STACK_TV_BOT(2 * (idx - count)); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3364 | // check key is unique |
Bram Moolenaar | c7f7f6d | 2020-11-04 13:38:28 +0100 | [diff] [blame] | 3365 | key = tv->vval.v_string == NULL |
| 3366 | ? (char_u *)"" : tv->vval.v_string; |
| 3367 | item = dict_find(dict, key, -1); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3368 | if (item != NULL) |
| 3369 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3370 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 3371 | semsg(_(e_duplicate_key_in_dicitonary), key); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3372 | dict_unref(dict); |
| 3373 | goto on_error; |
| 3374 | } |
Bram Moolenaar | c7f7f6d | 2020-11-04 13:38:28 +0100 | [diff] [blame] | 3375 | item = dictitem_alloc(key); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3376 | clear_tv(tv); |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 3377 | if (unlikely(item == NULL)) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3378 | { |
| 3379 | dict_unref(dict); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3380 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3381 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3382 | item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1); |
| 3383 | item->di_tv.v_lock = 0; |
| 3384 | if (dict_add(dict, item) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3385 | { |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3386 | // can this ever happen? |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3387 | dict_unref(dict); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3388 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3389 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | if (count > 0) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3393 | ectx->ec_stack.ga_len -= 2 * count - 1; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3394 | else if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3395 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3396 | else |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3397 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3398 | tv = STACK_TV_BOT(-1); |
| 3399 | tv->v_type = VAR_DICT; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3400 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3401 | tv->vval.v_dict = dict; |
| 3402 | ++dict->dv_refcount; |
| 3403 | } |
| 3404 | break; |
| 3405 | |
| 3406 | // call a :def function |
| 3407 | case ISN_DCALL: |
Bram Moolenaar | dfa3d55 | 2020-09-10 22:05:08 +0200 | [diff] [blame] | 3408 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 3409 | if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, |
| 3410 | NULL, |
| 3411 | iptr->isn_arg.dfunc.cdf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3412 | ectx) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 3413 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3414 | break; |
| 3415 | |
| 3416 | // call a builtin function |
| 3417 | case ISN_BCALL: |
| 3418 | SOURCING_LNUM = iptr->isn_lnum; |
| 3419 | if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, |
| 3420 | iptr->isn_arg.bfunc.cbf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3421 | ectx) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3422 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3423 | break; |
| 3424 | |
| 3425 | // call a funcref or partial |
| 3426 | case ISN_PCALL: |
| 3427 | { |
| 3428 | cpfunc_T *pfunc = &iptr->isn_arg.pfunc; |
| 3429 | int r; |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 3430 | typval_T partial_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3431 | |
| 3432 | SOURCING_LNUM = iptr->isn_lnum; |
| 3433 | if (pfunc->cpf_top) |
| 3434 | { |
| 3435 | // funcref is above the arguments |
| 3436 | tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); |
| 3437 | } |
| 3438 | else |
| 3439 | { |
| 3440 | // Get the funcref from the stack. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3441 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 3442 | partial_tv = *STACK_TV_BOT(0); |
| 3443 | tv = &partial_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3444 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3445 | r = call_partial(tv, pfunc->cpf_argcount, ectx); |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 3446 | if (tv == &partial_tv) |
| 3447 | clear_tv(&partial_tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3448 | if (r == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3449 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3450 | } |
| 3451 | break; |
| 3452 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 3453 | case ISN_PCALL_END: |
| 3454 | // PCALL finished, arguments have been consumed and replaced by |
| 3455 | // the return value. Now clear the funcref from the stack, |
| 3456 | // and move the return value in its place. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3457 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 3458 | clear_tv(STACK_TV_BOT(-1)); |
| 3459 | *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); |
| 3460 | break; |
| 3461 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3462 | // call a user defined function or funcref/partial |
| 3463 | case ISN_UCALL: |
| 3464 | { |
| 3465 | cufunc_T *cufunc = &iptr->isn_arg.ufunc; |
| 3466 | |
| 3467 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 3468 | if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3469 | ectx, iptr) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3470 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3471 | } |
| 3472 | break; |
| 3473 | |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 3474 | // return from a :def function call without a value |
| 3475 | case ISN_RETURN_VOID: |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3476 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3477 | goto theend; |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 3478 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3479 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 3480 | tv->v_type = VAR_VOID; |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 3481 | tv->vval.v_number = 0; |
| 3482 | tv->v_lock = 0; |
| 3483 | // FALLTHROUGH |
| 3484 | |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 3485 | // return from a :def function call with what is on the stack |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3486 | case ISN_RETURN: |
| 3487 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3488 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3489 | trycmd_T *trycmd = NULL; |
Bram Moolenaar | 8cbd6df | 2020-01-28 22:59:45 +0100 | [diff] [blame] | 3490 | |
| 3491 | if (trystack->ga_len > 0) |
| 3492 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 3493 | + trystack->ga_len - 1; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 3494 | if (trycmd != NULL |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3495 | && trycmd->tcd_frame_idx == ectx->ec_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3496 | { |
Bram Moolenaar | 9cb577a | 2021-02-22 22:45:10 +0100 | [diff] [blame] | 3497 | // jump to ":finally" or ":endtry" |
| 3498 | if (trycmd->tcd_finally_idx != 0) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3499 | ectx->ec_iidx = trycmd->tcd_finally_idx; |
Bram Moolenaar | 9cb577a | 2021-02-22 22:45:10 +0100 | [diff] [blame] | 3500 | else |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3501 | ectx->ec_iidx = trycmd->tcd_endtry_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3502 | trycmd->tcd_return = TRUE; |
| 3503 | } |
| 3504 | else |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3505 | goto func_return; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3506 | } |
| 3507 | break; |
| 3508 | |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 3509 | // push a partial, a reference to a compiled function |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3510 | case ISN_FUNCREF: |
| 3511 | { |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 3512 | partial_T *pt = ALLOC_CLEAR_ONE(partial_T); |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 3513 | ufunc_T *ufunc; |
| 3514 | funcref_T *funcref = &iptr->isn_arg.funcref; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3515 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3516 | if (pt == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3517 | goto theend; |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3518 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3519 | { |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 3520 | vim_free(pt); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3521 | goto theend; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 3522 | } |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 3523 | if (funcref->fr_func_name == NULL) |
| 3524 | { |
| 3525 | dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data) |
| 3526 | + funcref->fr_dfunc_idx; |
| 3527 | |
| 3528 | ufunc = pt_dfunc->df_ufunc; |
| 3529 | } |
| 3530 | else |
| 3531 | { |
Bram Moolenaar | d9d2fd0 | 2022-01-13 21:15:21 +0000 | [diff] [blame] | 3532 | ufunc = find_func(funcref->fr_func_name, FALSE); |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 3533 | } |
Bram Moolenaar | 293eb9b | 2021-11-29 10:36:19 +0000 | [diff] [blame] | 3534 | if (ufunc == NULL) |
| 3535 | { |
| 3536 | SOURCING_LNUM = iptr->isn_lnum; |
| 3537 | emsg(_(e_function_reference_invalid)); |
| 3538 | goto theend; |
| 3539 | } |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 3540 | if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3541 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3542 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3543 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3544 | tv->vval.v_partial = pt; |
| 3545 | tv->v_type = VAR_PARTIAL; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3546 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3547 | } |
| 3548 | break; |
| 3549 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 3550 | // Create a global function from a lambda. |
| 3551 | case ISN_NEWFUNC: |
| 3552 | { |
| 3553 | newfunc_T *newfunc = &iptr->isn_arg.newfunc; |
| 3554 | |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 3555 | if (copy_func(newfunc->nf_lambda, newfunc->nf_global, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3556 | ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3557 | goto theend; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 3558 | } |
| 3559 | break; |
| 3560 | |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 3561 | // List functions |
| 3562 | case ISN_DEF: |
| 3563 | if (iptr->isn_arg.string == NULL) |
| 3564 | list_functions(NULL); |
| 3565 | else |
| 3566 | { |
Bram Moolenaar | 1433672 | 2022-01-08 16:02:59 +0000 | [diff] [blame] | 3567 | exarg_T ea; |
| 3568 | garray_T lines_to_free; |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 3569 | |
| 3570 | CLEAR_FIELD(ea); |
| 3571 | ea.cmd = ea.arg = iptr->isn_arg.string; |
Bram Moolenaar | 1433672 | 2022-01-08 16:02:59 +0000 | [diff] [blame] | 3572 | ga_init2(&lines_to_free, sizeof(char_u *), 50); |
| 3573 | define_function(&ea, NULL, &lines_to_free); |
| 3574 | ga_clear_strings(&lines_to_free); |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 3575 | } |
| 3576 | break; |
| 3577 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3578 | // jump if a condition is met |
| 3579 | case ISN_JUMP: |
| 3580 | { |
| 3581 | jumpwhen_T when = iptr->isn_arg.jump.jump_when; |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3582 | int error = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3583 | int jump = TRUE; |
| 3584 | |
| 3585 | if (when != JUMP_ALWAYS) |
| 3586 | { |
| 3587 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3588 | if (when == JUMP_IF_COND_FALSE |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 3589 | || when == JUMP_IF_FALSE |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3590 | || when == JUMP_IF_COND_TRUE) |
| 3591 | { |
| 3592 | SOURCING_LNUM = iptr->isn_lnum; |
| 3593 | jump = tv_get_bool_chk(tv, &error); |
| 3594 | if (error) |
| 3595 | goto on_error; |
| 3596 | } |
| 3597 | else |
| 3598 | jump = tv2bool(tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3599 | if (when == JUMP_IF_FALSE |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3600 | || when == JUMP_AND_KEEP_IF_FALSE |
| 3601 | || when == JUMP_IF_COND_FALSE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3602 | jump = !jump; |
Bram Moolenaar | 777770f | 2020-02-06 21:27:08 +0100 | [diff] [blame] | 3603 | if (when == JUMP_IF_FALSE || !jump) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3604 | { |
| 3605 | // drop the value from the stack |
| 3606 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3607 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3608 | } |
| 3609 | } |
| 3610 | if (jump) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3611 | ectx->ec_iidx = iptr->isn_arg.jump.jump_where; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3612 | } |
| 3613 | break; |
| 3614 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 3615 | // Jump if an argument with a default value was already set and not |
| 3616 | // v:none. |
| 3617 | case ISN_JUMP_IF_ARG_SET: |
| 3618 | tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off); |
| 3619 | if (tv->v_type != VAR_UNKNOWN |
| 3620 | && !(tv->v_type == VAR_SPECIAL |
| 3621 | && tv->vval.v_number == VVAL_NONE)) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3622 | ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where; |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 3623 | break; |
| 3624 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3625 | // top of a for loop |
| 3626 | case ISN_FOR: |
Bram Moolenaar | 058ee7c | 2022-01-23 20:00:42 +0000 | [diff] [blame] | 3627 | if (execute_for(iptr, ectx) == FAIL) |
| 3628 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3629 | break; |
| 3630 | |
| 3631 | // start of ":try" block |
| 3632 | case ISN_TRY: |
| 3633 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3634 | trycmd_T *trycmd = NULL; |
| 3635 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3636 | if (GA_GROW_FAILS(&ectx->ec_trystack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3637 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3638 | trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data) |
| 3639 | + ectx->ec_trystack.ga_len; |
| 3640 | ++ectx->ec_trystack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3641 | ++trylevel; |
Bram Moolenaar | 8d4be89 | 2021-02-13 18:33:02 +0100 | [diff] [blame] | 3642 | CLEAR_POINTER(trycmd); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3643 | trycmd->tcd_frame_idx = ectx->ec_frame_idx; |
| 3644 | trycmd->tcd_stack_len = ectx->ec_stack.ga_len; |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 3645 | trycmd->tcd_catch_idx = |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 3646 | iptr->isn_arg.tryref.try_ref->try_catch; |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 3647 | trycmd->tcd_finally_idx = |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 3648 | iptr->isn_arg.tryref.try_ref->try_finally; |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 3649 | trycmd->tcd_endtry_idx = |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 3650 | iptr->isn_arg.tryref.try_ref->try_endtry; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3651 | } |
| 3652 | break; |
| 3653 | |
| 3654 | case ISN_PUSHEXC: |
| 3655 | if (current_exception == NULL) |
| 3656 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3657 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3658 | iemsg("Evaluating catch while current_exception is NULL"); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3659 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3660 | } |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 3661 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3662 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3663 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3664 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3665 | tv->v_type = VAR_STRING; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3666 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3667 | tv->vval.v_string = vim_strsave( |
| 3668 | (char_u *)current_exception->value); |
| 3669 | break; |
| 3670 | |
| 3671 | case ISN_CATCH: |
| 3672 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3673 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3674 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3675 | may_restore_cmdmod(&ectx->ec_funclocal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3676 | if (trystack->ga_len > 0) |
| 3677 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3678 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3679 | + trystack->ga_len - 1; |
| 3680 | trycmd->tcd_caught = TRUE; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 3681 | trycmd->tcd_did_throw = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3682 | } |
| 3683 | did_emsg = got_int = did_throw = FALSE; |
Bram Moolenaar | 1430cee | 2021-01-17 19:20:32 +0100 | [diff] [blame] | 3684 | force_abort = need_rethrow = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3685 | catch_exception(current_exception); |
| 3686 | } |
| 3687 | break; |
| 3688 | |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3689 | case ISN_TRYCONT: |
| 3690 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3691 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3692 | trycont_T *trycont = &iptr->isn_arg.trycont; |
| 3693 | int i; |
| 3694 | trycmd_T *trycmd; |
| 3695 | int iidx = trycont->tct_where; |
| 3696 | |
| 3697 | if (trystack->ga_len < trycont->tct_levels) |
| 3698 | { |
| 3699 | siemsg("TRYCONT: expected %d levels, found %d", |
| 3700 | trycont->tct_levels, trystack->ga_len); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3701 | goto theend; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3702 | } |
| 3703 | // Make :endtry jump to any outer try block and the last |
| 3704 | // :endtry inside the loop to the loop start. |
| 3705 | for (i = trycont->tct_levels; i > 0; --i) |
| 3706 | { |
| 3707 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 3708 | + trystack->ga_len - i; |
Bram Moolenaar | 2e34c34 | 2021-03-14 12:13:33 +0100 | [diff] [blame] | 3709 | // Add one to tcd_cont to be able to jump to |
| 3710 | // instruction with index zero. |
| 3711 | trycmd->tcd_cont = iidx + 1; |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 3712 | iidx = trycmd->tcd_finally_idx == 0 |
| 3713 | ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3714 | } |
| 3715 | // jump to :finally or :endtry of current try statement |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3716 | ectx->ec_iidx = iidx; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3717 | } |
| 3718 | break; |
| 3719 | |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 3720 | case ISN_FINALLY: |
| 3721 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3722 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 3723 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
| 3724 | + trystack->ga_len - 1; |
| 3725 | |
| 3726 | // Reset the index to avoid a return statement jumps here |
| 3727 | // again. |
| 3728 | trycmd->tcd_finally_idx = 0; |
| 3729 | break; |
| 3730 | } |
| 3731 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3732 | // end of ":try" block |
| 3733 | case ISN_ENDTRY: |
| 3734 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3735 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3736 | |
| 3737 | if (trystack->ga_len > 0) |
| 3738 | { |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3739 | trycmd_T *trycmd; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3740 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3741 | --trystack->ga_len; |
| 3742 | --trylevel; |
| 3743 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 3744 | + trystack->ga_len; |
Bram Moolenaar | d3d8fee | 2021-06-30 19:54:43 +0200 | [diff] [blame] | 3745 | if (trycmd->tcd_did_throw) |
| 3746 | did_throw = TRUE; |
Bram Moolenaar | f575adf | 2020-02-20 20:41:06 +0100 | [diff] [blame] | 3747 | if (trycmd->tcd_caught && current_exception != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3748 | { |
| 3749 | // discard the exception |
| 3750 | if (caught_stack == current_exception) |
| 3751 | caught_stack = caught_stack->caught; |
| 3752 | discard_current_exception(); |
| 3753 | } |
| 3754 | |
| 3755 | if (trycmd->tcd_return) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3756 | goto func_return; |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 3757 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3758 | while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len) |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 3759 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3760 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 3761 | clear_tv(STACK_TV_BOT(0)); |
| 3762 | } |
Bram Moolenaar | 8d4be89 | 2021-02-13 18:33:02 +0100 | [diff] [blame] | 3763 | if (trycmd->tcd_cont != 0) |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3764 | // handling :continue: jump to outer try block or |
| 3765 | // start of the loop |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3766 | ectx->ec_iidx = trycmd->tcd_cont - 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3767 | } |
| 3768 | } |
| 3769 | break; |
| 3770 | |
| 3771 | case ISN_THROW: |
Bram Moolenaar | 8f81b22 | 2021-01-14 21:47:06 +0100 | [diff] [blame] | 3772 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3773 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 1e021e6 | 2020-10-16 20:25:23 +0200 | [diff] [blame] | 3774 | |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3775 | if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent) |
| 3776 | { |
| 3777 | // throwing an exception while using "silent!" causes |
| 3778 | // the function to abort but not display an error. |
| 3779 | tv = STACK_TV_BOT(-1); |
| 3780 | clear_tv(tv); |
| 3781 | tv->v_type = VAR_NUMBER; |
| 3782 | tv->vval.v_number = 0; |
| 3783 | goto done; |
| 3784 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3785 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3786 | tv = STACK_TV_BOT(0); |
| 3787 | if (tv->vval.v_string == NULL |
| 3788 | || *skipwhite(tv->vval.v_string) == NUL) |
| 3789 | { |
| 3790 | vim_free(tv->vval.v_string); |
| 3791 | SOURCING_LNUM = iptr->isn_lnum; |
| 3792 | emsg(_(e_throw_with_empty_string)); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3793 | goto theend; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | // Inside a "catch" we need to first discard the caught |
| 3797 | // exception. |
| 3798 | if (trystack->ga_len > 0) |
| 3799 | { |
| 3800 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
| 3801 | + trystack->ga_len - 1; |
| 3802 | if (trycmd->tcd_caught && current_exception != NULL) |
| 3803 | { |
| 3804 | // discard the exception |
| 3805 | if (caught_stack == current_exception) |
| 3806 | caught_stack = caught_stack->caught; |
| 3807 | discard_current_exception(); |
| 3808 | trycmd->tcd_caught = FALSE; |
| 3809 | } |
| 3810 | } |
| 3811 | |
| 3812 | if (throw_exception(tv->vval.v_string, ET_USER, NULL) |
| 3813 | == FAIL) |
| 3814 | { |
| 3815 | vim_free(tv->vval.v_string); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3816 | goto theend; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3817 | } |
| 3818 | did_throw = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3819 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3820 | break; |
| 3821 | |
| 3822 | // compare with special values |
| 3823 | case ISN_COMPAREBOOL: |
| 3824 | case ISN_COMPARESPECIAL: |
| 3825 | { |
| 3826 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3827 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3828 | varnumber_T arg1 = tv1->vval.v_number; |
| 3829 | varnumber_T arg2 = tv2->vval.v_number; |
| 3830 | int res; |
| 3831 | |
| 3832 | switch (iptr->isn_arg.op.op_type) |
| 3833 | { |
| 3834 | case EXPR_EQUAL: res = arg1 == arg2; break; |
| 3835 | case EXPR_NEQUAL: res = arg1 != arg2; break; |
| 3836 | default: res = 0; break; |
| 3837 | } |
| 3838 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3839 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3840 | tv1->v_type = VAR_BOOL; |
| 3841 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 3842 | } |
| 3843 | break; |
| 3844 | |
| 3845 | // Operation with two number arguments |
| 3846 | case ISN_OPNR: |
| 3847 | case ISN_COMPARENR: |
| 3848 | { |
| 3849 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3850 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3851 | varnumber_T arg1 = tv1->vval.v_number; |
| 3852 | varnumber_T arg2 = tv2->vval.v_number; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 3853 | varnumber_T res = 0; |
| 3854 | int div_zero = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3855 | |
| 3856 | switch (iptr->isn_arg.op.op_type) |
| 3857 | { |
| 3858 | case EXPR_MULT: res = arg1 * arg2; break; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 3859 | case EXPR_DIV: if (arg2 == 0) |
| 3860 | div_zero = TRUE; |
| 3861 | else |
| 3862 | res = arg1 / arg2; |
| 3863 | break; |
| 3864 | case EXPR_REM: if (arg2 == 0) |
| 3865 | div_zero = TRUE; |
| 3866 | else |
| 3867 | res = arg1 % arg2; |
| 3868 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3869 | case EXPR_SUB: res = arg1 - arg2; break; |
| 3870 | case EXPR_ADD: res = arg1 + arg2; break; |
| 3871 | |
| 3872 | case EXPR_EQUAL: res = arg1 == arg2; break; |
| 3873 | case EXPR_NEQUAL: res = arg1 != arg2; break; |
| 3874 | case EXPR_GREATER: res = arg1 > arg2; break; |
| 3875 | case EXPR_GEQUAL: res = arg1 >= arg2; break; |
| 3876 | case EXPR_SMALLER: res = arg1 < arg2; break; |
| 3877 | case EXPR_SEQUAL: res = arg1 <= arg2; break; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 3878 | default: break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3879 | } |
| 3880 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3881 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3882 | if (iptr->isn_type == ISN_COMPARENR) |
| 3883 | { |
| 3884 | tv1->v_type = VAR_BOOL; |
| 3885 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 3886 | } |
| 3887 | else |
| 3888 | tv1->vval.v_number = res; |
Bram Moolenaar | fbeefb1 | 2021-08-07 15:50:23 +0200 | [diff] [blame] | 3889 | if (div_zero) |
| 3890 | { |
| 3891 | SOURCING_LNUM = iptr->isn_lnum; |
| 3892 | emsg(_(e_divide_by_zero)); |
| 3893 | goto on_error; |
| 3894 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3895 | } |
| 3896 | break; |
| 3897 | |
| 3898 | // Computation with two float arguments |
| 3899 | case ISN_OPFLOAT: |
| 3900 | case ISN_COMPAREFLOAT: |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 3901 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3902 | { |
| 3903 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3904 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3905 | float_T arg1 = tv1->vval.v_float; |
| 3906 | float_T arg2 = tv2->vval.v_float; |
| 3907 | float_T res = 0; |
| 3908 | int cmp = FALSE; |
| 3909 | |
| 3910 | switch (iptr->isn_arg.op.op_type) |
| 3911 | { |
| 3912 | case EXPR_MULT: res = arg1 * arg2; break; |
| 3913 | case EXPR_DIV: res = arg1 / arg2; break; |
| 3914 | case EXPR_SUB: res = arg1 - arg2; break; |
| 3915 | case EXPR_ADD: res = arg1 + arg2; break; |
| 3916 | |
| 3917 | case EXPR_EQUAL: cmp = arg1 == arg2; break; |
| 3918 | case EXPR_NEQUAL: cmp = arg1 != arg2; break; |
| 3919 | case EXPR_GREATER: cmp = arg1 > arg2; break; |
| 3920 | case EXPR_GEQUAL: cmp = arg1 >= arg2; break; |
| 3921 | case EXPR_SMALLER: cmp = arg1 < arg2; break; |
| 3922 | case EXPR_SEQUAL: cmp = arg1 <= arg2; break; |
| 3923 | default: cmp = 0; break; |
| 3924 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3925 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3926 | if (iptr->isn_type == ISN_COMPAREFLOAT) |
| 3927 | { |
| 3928 | tv1->v_type = VAR_BOOL; |
| 3929 | tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; |
| 3930 | } |
| 3931 | else |
| 3932 | tv1->vval.v_float = res; |
| 3933 | } |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 3934 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3935 | break; |
| 3936 | |
| 3937 | case ISN_COMPARELIST: |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 3938 | case ISN_COMPAREDICT: |
| 3939 | case ISN_COMPAREFUNC: |
| 3940 | case ISN_COMPARESTRING: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3941 | case ISN_COMPAREBLOB: |
| 3942 | { |
| 3943 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3944 | typval_T *tv2 = STACK_TV_BOT(-1); |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 3945 | exprtype_T exprtype = iptr->isn_arg.op.op_type; |
| 3946 | int ic = iptr->isn_arg.op.op_ic; |
| 3947 | int res = FALSE; |
| 3948 | int status = OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3949 | |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 3950 | SOURCING_LNUM = iptr->isn_lnum; |
| 3951 | if (iptr->isn_type == ISN_COMPARELIST) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3952 | { |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 3953 | status = typval_compare_list(tv1, tv2, |
| 3954 | exprtype, ic, &res); |
| 3955 | } |
| 3956 | else if (iptr->isn_type == ISN_COMPAREDICT) |
| 3957 | { |
| 3958 | status = typval_compare_dict(tv1, tv2, |
| 3959 | exprtype, ic, &res); |
| 3960 | } |
| 3961 | else if (iptr->isn_type == ISN_COMPAREFUNC) |
| 3962 | { |
| 3963 | status = typval_compare_func(tv1, tv2, |
| 3964 | exprtype, ic, &res); |
| 3965 | } |
| 3966 | else if (iptr->isn_type == ISN_COMPARESTRING) |
| 3967 | { |
| 3968 | status = typval_compare_string(tv1, tv2, |
| 3969 | exprtype, ic, &res); |
| 3970 | } |
| 3971 | else |
| 3972 | { |
| 3973 | status = typval_compare_blob(tv1, tv2, exprtype, &res); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3974 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3975 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3976 | clear_tv(tv1); |
| 3977 | clear_tv(tv2); |
| 3978 | tv1->v_type = VAR_BOOL; |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 3979 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 3980 | if (status == FAIL) |
| 3981 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3982 | } |
| 3983 | break; |
| 3984 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3985 | case ISN_COMPAREANY: |
| 3986 | { |
| 3987 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3988 | typval_T *tv2 = STACK_TV_BOT(-1); |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 3989 | exprtype_T exprtype = iptr->isn_arg.op.op_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3990 | int ic = iptr->isn_arg.op.op_ic; |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 3991 | int status; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3992 | |
Bram Moolenaar | eb26f43 | 2020-09-14 16:50:05 +0200 | [diff] [blame] | 3993 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 3994 | status = typval_compare(tv1, tv2, exprtype, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3995 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3996 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 265f811 | 2021-12-19 12:33:05 +0000 | [diff] [blame] | 3997 | if (status == FAIL) |
| 3998 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3999 | } |
| 4000 | break; |
| 4001 | |
| 4002 | case ISN_ADDLIST: |
| 4003 | case ISN_ADDBLOB: |
| 4004 | { |
| 4005 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4006 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4007 | |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4008 | // add two lists or blobs |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4009 | if (iptr->isn_type == ISN_ADDLIST) |
Bram Moolenaar | 0780204 | 2021-09-09 23:01:14 +0200 | [diff] [blame] | 4010 | { |
| 4011 | if (iptr->isn_arg.op.op_type == EXPR_APPEND |
| 4012 | && tv1->vval.v_list != NULL) |
| 4013 | list_extend(tv1->vval.v_list, tv2->vval.v_list, |
| 4014 | NULL); |
| 4015 | else |
| 4016 | eval_addlist(tv1, tv2); |
| 4017 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4018 | else |
| 4019 | eval_addblob(tv1, tv2); |
| 4020 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4021 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4022 | } |
| 4023 | break; |
| 4024 | |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4025 | case ISN_LISTAPPEND: |
| 4026 | { |
| 4027 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4028 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4029 | list_T *l = tv1->vval.v_list; |
| 4030 | |
| 4031 | // add an item to a list |
Bram Moolenaar | 1f4a345 | 2022-01-01 18:29:21 +0000 | [diff] [blame] | 4032 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4033 | if (l == NULL) |
| 4034 | { |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4035 | emsg(_(e_cannot_add_to_null_list)); |
| 4036 | goto on_error; |
| 4037 | } |
Bram Moolenaar | 1f4a345 | 2022-01-01 18:29:21 +0000 | [diff] [blame] | 4038 | if (value_check_lock(l->lv_lock, NULL, FALSE)) |
| 4039 | goto on_error; |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4040 | if (list_append_tv(l, tv2) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4041 | goto theend; |
Bram Moolenaar | 955347c | 2020-10-19 23:01:46 +0200 | [diff] [blame] | 4042 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4043 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 4044 | } |
| 4045 | break; |
| 4046 | |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 4047 | case ISN_BLOBAPPEND: |
| 4048 | { |
| 4049 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4050 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4051 | blob_T *b = tv1->vval.v_blob; |
| 4052 | int error = FALSE; |
| 4053 | varnumber_T n; |
| 4054 | |
| 4055 | // add a number to a blob |
| 4056 | if (b == NULL) |
| 4057 | { |
| 4058 | SOURCING_LNUM = iptr->isn_lnum; |
| 4059 | emsg(_(e_cannot_add_to_null_blob)); |
| 4060 | goto on_error; |
| 4061 | } |
| 4062 | n = tv_get_number_chk(tv2, &error); |
| 4063 | if (error) |
| 4064 | goto on_error; |
| 4065 | ga_append(&b->bv_ga, (int)n); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4066 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 4067 | } |
| 4068 | break; |
| 4069 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4070 | // Computation with two arguments of unknown type |
| 4071 | case ISN_OPANY: |
| 4072 | { |
| 4073 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 4074 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 4075 | varnumber_T n1, n2; |
| 4076 | #ifdef FEAT_FLOAT |
| 4077 | float_T f1 = 0, f2 = 0; |
| 4078 | #endif |
| 4079 | int error = FALSE; |
| 4080 | |
| 4081 | if (iptr->isn_arg.op.op_type == EXPR_ADD) |
| 4082 | { |
| 4083 | if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) |
| 4084 | { |
| 4085 | eval_addlist(tv1, tv2); |
| 4086 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4087 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4088 | break; |
| 4089 | } |
| 4090 | else if (tv1->v_type == VAR_BLOB |
| 4091 | && tv2->v_type == VAR_BLOB) |
| 4092 | { |
| 4093 | eval_addblob(tv1, tv2); |
| 4094 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4095 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4096 | break; |
| 4097 | } |
| 4098 | } |
| 4099 | #ifdef FEAT_FLOAT |
| 4100 | if (tv1->v_type == VAR_FLOAT) |
| 4101 | { |
| 4102 | f1 = tv1->vval.v_float; |
| 4103 | n1 = 0; |
| 4104 | } |
| 4105 | else |
| 4106 | #endif |
| 4107 | { |
Bram Moolenaar | f665e97 | 2020-12-05 19:17:16 +0100 | [diff] [blame] | 4108 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4109 | n1 = tv_get_number_chk(tv1, &error); |
| 4110 | if (error) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4111 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4112 | #ifdef FEAT_FLOAT |
| 4113 | if (tv2->v_type == VAR_FLOAT) |
| 4114 | f1 = n1; |
| 4115 | #endif |
| 4116 | } |
| 4117 | #ifdef FEAT_FLOAT |
| 4118 | if (tv2->v_type == VAR_FLOAT) |
| 4119 | { |
| 4120 | f2 = tv2->vval.v_float; |
| 4121 | n2 = 0; |
| 4122 | } |
| 4123 | else |
| 4124 | #endif |
| 4125 | { |
| 4126 | n2 = tv_get_number_chk(tv2, &error); |
| 4127 | if (error) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4128 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4129 | #ifdef FEAT_FLOAT |
| 4130 | if (tv1->v_type == VAR_FLOAT) |
| 4131 | f2 = n2; |
| 4132 | #endif |
| 4133 | } |
| 4134 | #ifdef FEAT_FLOAT |
| 4135 | // if there is a float on either side the result is a float |
| 4136 | if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) |
| 4137 | { |
| 4138 | switch (iptr->isn_arg.op.op_type) |
| 4139 | { |
| 4140 | case EXPR_MULT: f1 = f1 * f2; break; |
| 4141 | case EXPR_DIV: f1 = f1 / f2; break; |
| 4142 | case EXPR_SUB: f1 = f1 - f2; break; |
| 4143 | case EXPR_ADD: f1 = f1 + f2; break; |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4144 | default: SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4145 | emsg(_(e_cannot_use_percent_with_float)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4146 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4147 | } |
| 4148 | clear_tv(tv1); |
| 4149 | clear_tv(tv2); |
| 4150 | tv1->v_type = VAR_FLOAT; |
| 4151 | tv1->vval.v_float = f1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4152 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4153 | } |
| 4154 | else |
| 4155 | #endif |
| 4156 | { |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 4157 | int failed = FALSE; |
| 4158 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4159 | switch (iptr->isn_arg.op.op_type) |
| 4160 | { |
| 4161 | case EXPR_MULT: n1 = n1 * n2; break; |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 4162 | case EXPR_DIV: n1 = num_divide(n1, n2, &failed); |
| 4163 | if (failed) |
Bram Moolenaar | 99880f9 | 2021-01-20 21:23:14 +0100 | [diff] [blame] | 4164 | goto on_error; |
| 4165 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4166 | case EXPR_SUB: n1 = n1 - n2; break; |
| 4167 | case EXPR_ADD: n1 = n1 + n2; break; |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 4168 | default: n1 = num_modulus(n1, n2, &failed); |
| 4169 | if (failed) |
Bram Moolenaar | 99880f9 | 2021-01-20 21:23:14 +0100 | [diff] [blame] | 4170 | goto on_error; |
| 4171 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4172 | } |
| 4173 | clear_tv(tv1); |
| 4174 | clear_tv(tv2); |
| 4175 | tv1->v_type = VAR_NUMBER; |
| 4176 | tv1->vval.v_number = n1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4177 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4178 | } |
| 4179 | } |
| 4180 | break; |
| 4181 | |
| 4182 | case ISN_CONCAT: |
| 4183 | { |
| 4184 | char_u *str1 = STACK_TV_BOT(-2)->vval.v_string; |
| 4185 | char_u *str2 = STACK_TV_BOT(-1)->vval.v_string; |
| 4186 | char_u *res; |
| 4187 | |
| 4188 | res = concat_str(str1, str2); |
| 4189 | clear_tv(STACK_TV_BOT(-2)); |
| 4190 | clear_tv(STACK_TV_BOT(-1)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4191 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4192 | STACK_TV_BOT(-1)->vval.v_string = res; |
| 4193 | } |
| 4194 | break; |
| 4195 | |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4196 | case ISN_STRINDEX: |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4197 | case ISN_STRSLICE: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4198 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4199 | int is_slice = iptr->isn_type == ISN_STRSLICE; |
| 4200 | varnumber_T n1 = 0, n2; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4201 | char_u *res; |
| 4202 | |
| 4203 | // string index: string is at stack-2, index at stack-1 |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4204 | // string slice: string is at stack-3, first index at |
| 4205 | // stack-2, second index at stack-1 |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4206 | if (is_slice) |
| 4207 | { |
| 4208 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4209 | n1 = tv->vval.v_number; |
| 4210 | } |
| 4211 | |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4212 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4213 | n2 = tv->vval.v_number; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4214 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4215 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4216 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4217 | if (is_slice) |
| 4218 | // Slice: Select the characters from the string |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 4219 | res = string_slice(tv->vval.v_string, n1, n2, FALSE); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4220 | else |
| 4221 | // Index: The resulting variable is a string of a |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 4222 | // single character (including composing characters). |
| 4223 | // If the index is too big or negative the result is |
| 4224 | // empty. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 4225 | res = char_from_string(tv->vval.v_string, n2); |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 4226 | vim_free(tv->vval.v_string); |
| 4227 | tv->vval.v_string = res; |
| 4228 | } |
| 4229 | break; |
| 4230 | |
| 4231 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4232 | case ISN_LISTSLICE: |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4233 | case ISN_BLOBINDEX: |
| 4234 | case ISN_BLOBSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4235 | { |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4236 | int is_slice = iptr->isn_type == ISN_LISTSLICE |
| 4237 | || iptr->isn_type == ISN_BLOBSLICE; |
| 4238 | int is_blob = iptr->isn_type == ISN_BLOBINDEX |
| 4239 | || iptr->isn_type == ISN_BLOBSLICE; |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4240 | varnumber_T n1, n2; |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4241 | typval_T *val_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4242 | |
| 4243 | // list index: list is at stack-2, index at stack-1 |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4244 | // list slice: list is at stack-3, indexes at stack-2 and |
| 4245 | // stack-1 |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4246 | // Same for blob. |
| 4247 | val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4248 | |
| 4249 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4250 | n1 = n2 = tv->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4251 | clear_tv(tv); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4252 | |
| 4253 | if (is_slice) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4254 | { |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4255 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4256 | n1 = tv->vval.v_number; |
| 4257 | clear_tv(tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4258 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 4259 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4260 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | 435d897 | 2020-07-05 16:42:13 +0200 | [diff] [blame] | 4261 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 1d63454 | 2020-08-18 13:41:50 +0200 | [diff] [blame] | 4262 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 4263 | if (is_blob) |
| 4264 | { |
| 4265 | if (blob_slice_or_index(val_tv->vval.v_blob, is_slice, |
| 4266 | n1, n2, FALSE, tv) == FAIL) |
| 4267 | goto on_error; |
| 4268 | } |
| 4269 | else |
| 4270 | { |
| 4271 | if (list_slice_or_index(val_tv->vval.v_list, is_slice, |
| 4272 | n1, n2, FALSE, tv, TRUE) == FAIL) |
| 4273 | goto on_error; |
| 4274 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4275 | } |
| 4276 | break; |
| 4277 | |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4278 | case ISN_ANYINDEX: |
| 4279 | case ISN_ANYSLICE: |
| 4280 | { |
| 4281 | int is_slice = iptr->isn_type == ISN_ANYSLICE; |
| 4282 | typval_T *var1, *var2; |
| 4283 | int res; |
| 4284 | |
| 4285 | // index: composite is at stack-2, index at stack-1 |
| 4286 | // slice: composite is at stack-3, indexes at stack-2 and |
| 4287 | // stack-1 |
| 4288 | tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); |
Bram Moolenaar | 3affe7a | 2020-08-18 20:34:13 +0200 | [diff] [blame] | 4289 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4290 | if (check_can_index(tv, TRUE, TRUE) == FAIL) |
| 4291 | goto on_error; |
| 4292 | var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1); |
| 4293 | var2 = is_slice ? STACK_TV_BOT(-1) : NULL; |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 4294 | res = eval_index_inner(tv, is_slice, var1, var2, |
| 4295 | FALSE, NULL, -1, TRUE); |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4296 | clear_tv(var1); |
| 4297 | if (is_slice) |
| 4298 | clear_tv(var2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4299 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 4300 | if (res == FAIL) |
| 4301 | goto on_error; |
| 4302 | } |
| 4303 | break; |
| 4304 | |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4305 | case ISN_SLICE: |
| 4306 | { |
| 4307 | list_T *list; |
| 4308 | int count = iptr->isn_arg.number; |
| 4309 | |
Bram Moolenaar | c5b1c20 | 2020-06-18 22:43:27 +0200 | [diff] [blame] | 4310 | // type will have been checked to be a list |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4311 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4312 | list = tv->vval.v_list; |
| 4313 | |
| 4314 | // no error for short list, expect it to be checked earlier |
| 4315 | if (list != NULL && list->lv_len >= count) |
| 4316 | { |
| 4317 | list_T *newlist = list_slice(list, |
| 4318 | count, list->lv_len - 1); |
| 4319 | |
| 4320 | if (newlist != NULL) |
| 4321 | { |
| 4322 | list_unref(list); |
| 4323 | tv->vval.v_list = newlist; |
| 4324 | ++newlist->lv_refcount; |
| 4325 | } |
| 4326 | } |
| 4327 | } |
| 4328 | break; |
| 4329 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4330 | case ISN_GETITEM: |
| 4331 | { |
| 4332 | listitem_T *li; |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 4333 | getitem_T *gi = &iptr->isn_arg.getitem; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4334 | |
Bram Moolenaar | c785b9a | 2020-06-19 18:34:15 +0200 | [diff] [blame] | 4335 | // Get list item: list is at stack-1, push item. |
| 4336 | // List type and length is checked for when compiling. |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 4337 | tv = STACK_TV_BOT(-1 - gi->gi_with_op); |
| 4338 | li = list_find(tv->vval.v_list, gi->gi_index); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4339 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4340 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4341 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4342 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4343 | copy_tv(&li->li_tv, STACK_TV_BOT(-1)); |
Bram Moolenaar | f785aa1 | 2021-02-11 21:19:34 +0100 | [diff] [blame] | 4344 | |
| 4345 | // Useful when used in unpack assignment. Reset at |
| 4346 | // ISN_DROP. |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 4347 | ectx->ec_where.wt_index = gi->gi_index + 1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4348 | ectx->ec_where.wt_variable = TRUE; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4349 | } |
| 4350 | break; |
| 4351 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4352 | case ISN_MEMBER: |
| 4353 | { |
| 4354 | dict_T *dict; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4355 | char_u *key; |
| 4356 | dictitem_T *di; |
| 4357 | |
| 4358 | // dict member: dict is at stack-2, key at stack-1 |
| 4359 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 4360 | // no need to check for VAR_DICT, CHECKTYPE will check. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4361 | dict = tv->vval.v_dict; |
| 4362 | |
| 4363 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 4364 | // no need to check for VAR_STRING, 2STRING will check. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4365 | key = tv->vval.v_string; |
Bram Moolenaar | 086fc9a | 2020-10-30 18:33:02 +0100 | [diff] [blame] | 4366 | if (key == NULL) |
| 4367 | key = (char_u *)""; |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 4368 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4369 | if ((di = dict_find(dict, key, -1)) == NULL) |
| 4370 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4371 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4372 | semsg(_(e_key_not_present_in_dictionary), key); |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 4373 | |
| 4374 | // If :silent! is used we will continue, make sure the |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4375 | // stack contents makes sense and the dict stack is |
| 4376 | // updated. |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 4377 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4378 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 4379 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4380 | (void) dict_stack_save(tv); |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 4381 | tv->v_type = VAR_NUMBER; |
| 4382 | tv->vval.v_number = 0; |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 4383 | goto on_fatal_error; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4384 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4385 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4386 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4387 | // Put the dict used on the dict stack, it might be used by |
| 4388 | // a dict function later. |
Bram Moolenaar | 50788ef | 2020-07-05 16:51:26 +0200 | [diff] [blame] | 4389 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4390 | if (dict_stack_save(tv) == FAIL) |
| 4391 | goto on_fatal_error; |
Bram Moolenaar | 50788ef | 2020-07-05 16:51:26 +0200 | [diff] [blame] | 4392 | copy_tv(&di->di_tv, tv); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4393 | } |
| 4394 | break; |
| 4395 | |
| 4396 | // dict member with string key |
| 4397 | case ISN_STRINGMEMBER: |
| 4398 | { |
| 4399 | dict_T *dict; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4400 | dictitem_T *di; |
| 4401 | |
| 4402 | tv = STACK_TV_BOT(-1); |
| 4403 | if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) |
| 4404 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4405 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4406 | emsg(_(e_dictionary_required)); |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4407 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4408 | } |
| 4409 | dict = tv->vval.v_dict; |
| 4410 | |
| 4411 | if ((di = dict_find(dict, iptr->isn_arg.string, -1)) |
| 4412 | == NULL) |
| 4413 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4414 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4415 | semsg(_(e_key_not_present_in_dictionary), iptr->isn_arg.string); |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4416 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4417 | } |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4418 | // Put the dict used on the dict stack, it might be used by |
| 4419 | // a dict function later. |
| 4420 | if (dict_stack_save(tv) == FAIL) |
| 4421 | goto on_fatal_error; |
| 4422 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4423 | copy_tv(&di->di_tv, tv); |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4424 | } |
| 4425 | break; |
| 4426 | |
| 4427 | case ISN_CLEARDICT: |
| 4428 | dict_stack_drop(); |
| 4429 | break; |
| 4430 | |
| 4431 | case ISN_USEDICT: |
| 4432 | { |
| 4433 | typval_T *dict_tv = dict_stack_get_tv(); |
| 4434 | |
| 4435 | // Turn "dict.Func" into a partial for "Func" bound to |
| 4436 | // "dict". Don't do this when "Func" is already a partial |
| 4437 | // that was bound explicitly (pt_auto is FALSE). |
| 4438 | tv = STACK_TV_BOT(-1); |
| 4439 | if (dict_tv != NULL |
| 4440 | && dict_tv->v_type == VAR_DICT |
| 4441 | && dict_tv->vval.v_dict != NULL |
| 4442 | && (tv->v_type == VAR_FUNC |
| 4443 | || (tv->v_type == VAR_PARTIAL |
| 4444 | && (tv->vval.v_partial->pt_auto |
| 4445 | || tv->vval.v_partial->pt_dict == NULL)))) |
| 4446 | dict_tv->vval.v_dict = |
| 4447 | make_partial(dict_tv->vval.v_dict, tv); |
| 4448 | dict_stack_drop(); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4449 | } |
| 4450 | break; |
| 4451 | |
| 4452 | case ISN_NEGATENR: |
| 4453 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 4454 | if (tv->v_type != VAR_NUMBER |
| 4455 | #ifdef FEAT_FLOAT |
| 4456 | && tv->v_type != VAR_FLOAT |
| 4457 | #endif |
| 4458 | ) |
| 4459 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4460 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | e29a27f | 2021-07-20 21:07:36 +0200 | [diff] [blame] | 4461 | emsg(_(e_number_expected)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4462 | goto on_error; |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 4463 | } |
| 4464 | #ifdef FEAT_FLOAT |
| 4465 | if (tv->v_type == VAR_FLOAT) |
| 4466 | tv->vval.v_float = -tv->vval.v_float; |
| 4467 | else |
| 4468 | #endif |
| 4469 | tv->vval.v_number = -tv->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4470 | break; |
| 4471 | |
| 4472 | case ISN_CHECKNR: |
| 4473 | { |
| 4474 | int error = FALSE; |
| 4475 | |
| 4476 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 3affe7a | 2020-08-18 20:34:13 +0200 | [diff] [blame] | 4477 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4478 | if (check_not_string(tv) == FAIL) |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4479 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4480 | (void)tv_get_number_chk(tv, &error); |
| 4481 | if (error) |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4482 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4483 | } |
| 4484 | break; |
| 4485 | |
| 4486 | case ISN_CHECKTYPE: |
| 4487 | { |
| 4488 | checktype_T *ct = &iptr->isn_arg.type; |
| 4489 | |
Bram Moolenaar | b3005ce | 2021-01-22 17:51:06 +0100 | [diff] [blame] | 4490 | tv = STACK_TV_BOT((int)ct->ct_off); |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 4491 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4492 | if (!ectx->ec_where.wt_variable) |
| 4493 | ectx->ec_where.wt_index = ct->ct_arg_idx; |
| 4494 | if (check_typval_type(ct->ct_type, tv, ectx->ec_where) |
| 4495 | == FAIL) |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 4496 | goto on_error; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4497 | if (!ectx->ec_where.wt_variable) |
| 4498 | ectx->ec_where.wt_index = 0; |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 4499 | |
| 4500 | // number 0 is FALSE, number 1 is TRUE |
| 4501 | if (tv->v_type == VAR_NUMBER |
| 4502 | && ct->ct_type->tt_type == VAR_BOOL |
| 4503 | && (tv->vval.v_number == 0 |
| 4504 | || tv->vval.v_number == 1)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4505 | { |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 4506 | tv->v_type = VAR_BOOL; |
| 4507 | tv->vval.v_number = tv->vval.v_number |
Bram Moolenaar | dadaddd | 2020-09-12 19:11:23 +0200 | [diff] [blame] | 4508 | ? VVAL_TRUE : VVAL_FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4509 | } |
| 4510 | } |
| 4511 | break; |
| 4512 | |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4513 | case ISN_CHECKLEN: |
| 4514 | { |
| 4515 | int min_len = iptr->isn_arg.checklen.cl_min_len; |
| 4516 | list_T *list = NULL; |
| 4517 | |
| 4518 | tv = STACK_TV_BOT(-1); |
| 4519 | if (tv->v_type == VAR_LIST) |
| 4520 | list = tv->vval.v_list; |
| 4521 | if (list == NULL || list->lv_len < min_len |
| 4522 | || (list->lv_len > min_len |
| 4523 | && !iptr->isn_arg.checklen.cl_more_OK)) |
| 4524 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 4525 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4526 | semsg(_(e_expected_nr_items_but_got_nr), |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4527 | min_len, list == NULL ? 0 : list->lv_len); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4528 | goto on_error; |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4529 | } |
| 4530 | } |
| 4531 | break; |
| 4532 | |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 4533 | case ISN_SETTYPE: |
| 4534 | { |
| 4535 | checktype_T *ct = &iptr->isn_arg.type; |
| 4536 | |
| 4537 | tv = STACK_TV_BOT(-1); |
| 4538 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 4539 | { |
| 4540 | free_type(tv->vval.v_dict->dv_type); |
| 4541 | tv->vval.v_dict->dv_type = alloc_type(ct->ct_type); |
| 4542 | } |
| 4543 | else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL) |
| 4544 | { |
| 4545 | free_type(tv->vval.v_list->lv_type); |
| 4546 | tv->vval.v_list->lv_type = alloc_type(ct->ct_type); |
| 4547 | } |
| 4548 | } |
| 4549 | break; |
| 4550 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4551 | case ISN_2BOOL: |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4552 | case ISN_COND2BOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4553 | { |
| 4554 | int n; |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4555 | int error = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4556 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4557 | if (iptr->isn_type == ISN_2BOOL) |
| 4558 | { |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 4559 | tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4560 | n = tv2bool(tv); |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 4561 | if (iptr->isn_arg.tobool.invert) |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4562 | n = !n; |
| 4563 | } |
| 4564 | else |
| 4565 | { |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 4566 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4567 | SOURCING_LNUM = iptr->isn_lnum; |
| 4568 | n = tv_get_bool_chk(tv, &error); |
| 4569 | if (error) |
| 4570 | goto on_error; |
| 4571 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4572 | clear_tv(tv); |
| 4573 | tv->v_type = VAR_BOOL; |
| 4574 | tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; |
| 4575 | } |
| 4576 | break; |
| 4577 | |
| 4578 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 4579 | case ISN_2STRING_ANY: |
Bram Moolenaar | 0acbf5a | 2021-01-05 20:58:25 +0100 | [diff] [blame] | 4580 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 4581 | if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset), |
| 4582 | iptr->isn_type == ISN_2STRING_ANY, |
| 4583 | iptr->isn_arg.tostring.tolerant) == FAIL) |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 4584 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4585 | break; |
| 4586 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4587 | case ISN_RANGE: |
| 4588 | { |
| 4589 | exarg_T ea; |
| 4590 | char *errormsg; |
| 4591 | |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 4592 | ea.line2 = 0; |
Bram Moolenaar | d1510ee | 2021-01-04 16:15:58 +0100 | [diff] [blame] | 4593 | ea.addr_count = 0; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4594 | ea.addr_type = ADDR_LINES; |
| 4595 | ea.cmd = iptr->isn_arg.string; |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 4596 | ea.skip = FALSE; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4597 | if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL) |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 4598 | goto on_error; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 4599 | |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4600 | if (GA_GROW_FAILS(&ectx->ec_stack, 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4601 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4602 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 4603 | tv = STACK_TV_BOT(-1); |
| 4604 | tv->v_type = VAR_NUMBER; |
| 4605 | tv->v_lock = 0; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4606 | if (ea.addr_count == 0) |
| 4607 | tv->vval.v_number = curwin->w_cursor.lnum; |
| 4608 | else |
| 4609 | tv->vval.v_number = ea.line2; |
| 4610 | } |
| 4611 | break; |
| 4612 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 4613 | case ISN_PUT: |
| 4614 | { |
| 4615 | int regname = iptr->isn_arg.put.put_regname; |
| 4616 | linenr_T lnum = iptr->isn_arg.put.put_lnum; |
| 4617 | char_u *expr = NULL; |
| 4618 | int dir = FORWARD; |
| 4619 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4620 | if (lnum < -2) |
| 4621 | { |
| 4622 | // line number was put on the stack by ISN_RANGE |
| 4623 | tv = STACK_TV_BOT(-1); |
| 4624 | curwin->w_cursor.lnum = tv->vval.v_number; |
| 4625 | if (lnum == LNUM_VARIABLE_RANGE_ABOVE) |
| 4626 | dir = BACKWARD; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4627 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 4628 | } |
| 4629 | else if (lnum == -2) |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 4630 | // :put! above cursor |
| 4631 | dir = BACKWARD; |
| 4632 | else if (lnum >= 0) |
| 4633 | curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 4634 | |
| 4635 | if (regname == '=') |
| 4636 | { |
| 4637 | tv = STACK_TV_BOT(-1); |
| 4638 | if (tv->v_type == VAR_STRING) |
| 4639 | expr = tv->vval.v_string; |
| 4640 | else |
| 4641 | { |
| 4642 | expr = typval2string(tv, TRUE); // allocates value |
| 4643 | clear_tv(tv); |
| 4644 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4645 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 4646 | } |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 4647 | check_cursor(); |
| 4648 | do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE); |
| 4649 | vim_free(expr); |
| 4650 | } |
| 4651 | break; |
| 4652 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 4653 | case ISN_CMDMOD: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4654 | ectx->ec_funclocal.floc_save_cmdmod = cmdmod; |
| 4655 | ectx->ec_funclocal.floc_restore_cmdmod = TRUE; |
| 4656 | ectx->ec_funclocal.floc_restore_cmdmod_stacklen = |
| 4657 | ectx->ec_stack.ga_len; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 4658 | cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod; |
| 4659 | apply_cmdmod(&cmdmod); |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 4660 | break; |
| 4661 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 4662 | case ISN_CMDMOD_REV: |
| 4663 | // filter regprog is owned by the instruction, don't free it |
| 4664 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 4665 | undo_cmdmod(&cmdmod); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4666 | cmdmod = ectx->ec_funclocal.floc_save_cmdmod; |
| 4667 | ectx->ec_funclocal.floc_restore_cmdmod = FALSE; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 4668 | break; |
| 4669 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 4670 | case ISN_UNPACK: |
| 4671 | { |
| 4672 | int count = iptr->isn_arg.unpack.unp_count; |
| 4673 | int semicolon = iptr->isn_arg.unpack.unp_semicolon; |
| 4674 | list_T *l; |
| 4675 | listitem_T *li; |
| 4676 | int i; |
| 4677 | |
| 4678 | // Check there is a valid list to unpack. |
| 4679 | tv = STACK_TV_BOT(-1); |
| 4680 | if (tv->v_type != VAR_LIST) |
| 4681 | { |
| 4682 | SOURCING_LNUM = iptr->isn_lnum; |
| 4683 | emsg(_(e_for_argument_must_be_sequence_of_lists)); |
| 4684 | goto on_error; |
| 4685 | } |
| 4686 | l = tv->vval.v_list; |
| 4687 | if (l == NULL |
| 4688 | || l->lv_len < (semicolon ? count - 1 : count)) |
| 4689 | { |
| 4690 | SOURCING_LNUM = iptr->isn_lnum; |
| 4691 | emsg(_(e_list_value_does_not_have_enough_items)); |
| 4692 | goto on_error; |
| 4693 | } |
| 4694 | else if (!semicolon && l->lv_len > count) |
| 4695 | { |
| 4696 | SOURCING_LNUM = iptr->isn_lnum; |
| 4697 | emsg(_(e_list_value_has_more_items_than_targets)); |
| 4698 | goto on_error; |
| 4699 | } |
| 4700 | |
| 4701 | CHECK_LIST_MATERIALIZE(l); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4702 | if (GA_GROW_FAILS(&ectx->ec_stack, count - 1)) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4703 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4704 | ectx->ec_stack.ga_len += count - 1; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 4705 | |
| 4706 | // Variable after semicolon gets a list with the remaining |
| 4707 | // items. |
| 4708 | if (semicolon) |
| 4709 | { |
| 4710 | list_T *rem_list = |
| 4711 | list_alloc_with_items(l->lv_len - count + 1); |
| 4712 | |
| 4713 | if (rem_list == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4714 | goto theend; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 4715 | tv = STACK_TV_BOT(-count); |
| 4716 | tv->vval.v_list = rem_list; |
| 4717 | ++rem_list->lv_refcount; |
| 4718 | tv->v_lock = 0; |
| 4719 | li = l->lv_first; |
| 4720 | for (i = 0; i < count - 1; ++i) |
| 4721 | li = li->li_next; |
| 4722 | for (i = 0; li != NULL; ++i) |
| 4723 | { |
| 4724 | list_set_item(rem_list, i, &li->li_tv); |
| 4725 | li = li->li_next; |
| 4726 | } |
| 4727 | --count; |
| 4728 | } |
| 4729 | |
| 4730 | // Produce the values in reverse order, first item last. |
| 4731 | li = l->lv_first; |
| 4732 | for (i = 0; i < count; ++i) |
| 4733 | { |
| 4734 | tv = STACK_TV_BOT(-i - 1); |
| 4735 | copy_tv(&li->li_tv, tv); |
| 4736 | li = li->li_next; |
| 4737 | } |
| 4738 | |
| 4739 | list_unref(l); |
| 4740 | } |
| 4741 | break; |
| 4742 | |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4743 | case ISN_PROF_START: |
| 4744 | case ISN_PROF_END: |
| 4745 | { |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 4746 | #ifdef FEAT_PROFILE |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4747 | funccall_T cookie; |
| 4748 | ufunc_T *cur_ufunc = |
| 4749 | (((dfunc_T *)def_functions.ga_data) |
Bram Moolenaar | b69c6fb | 2021-06-14 20:40:37 +0200 | [diff] [blame] | 4750 | + ectx->ec_dfunc_idx)->df_ufunc; |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4751 | |
| 4752 | cookie.func = cur_ufunc; |
| 4753 | if (iptr->isn_type == ISN_PROF_START) |
| 4754 | { |
| 4755 | func_line_start(&cookie, iptr->isn_lnum); |
| 4756 | // if we get here the instruction is executed |
| 4757 | func_line_exec(&cookie); |
| 4758 | } |
| 4759 | else |
| 4760 | func_line_end(&cookie); |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 4761 | #endif |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4762 | } |
| 4763 | break; |
| 4764 | |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 4765 | case ISN_DEBUG: |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 4766 | handle_debug(iptr, ectx); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 4767 | break; |
| 4768 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 4769 | case ISN_SHUFFLE: |
| 4770 | { |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 4771 | typval_T tmp_tv; |
| 4772 | int item = iptr->isn_arg.shuffle.shfl_item; |
| 4773 | int up = iptr->isn_arg.shuffle.shfl_up; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 4774 | |
| 4775 | tmp_tv = *STACK_TV_BOT(-item); |
| 4776 | for ( ; up > 0 && item > 1; --up) |
| 4777 | { |
| 4778 | *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1); |
| 4779 | --item; |
| 4780 | } |
| 4781 | *STACK_TV_BOT(-item) = tmp_tv; |
| 4782 | } |
| 4783 | break; |
| 4784 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4785 | case ISN_DROP: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4786 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4787 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4788 | ectx->ec_where.wt_index = 0; |
| 4789 | ectx->ec_where.wt_variable = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4790 | break; |
| 4791 | } |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4792 | continue; |
| 4793 | |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4794 | func_return: |
Bram Moolenaar | 7cbfaa5 | 2020-09-18 21:25:32 +0200 | [diff] [blame] | 4795 | // Restore previous function. If the frame pointer is where we started |
| 4796 | // then there is none and we are done. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4797 | if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4798 | goto done; |
Bram Moolenaar | 7cbfaa5 | 2020-09-18 21:25:32 +0200 | [diff] [blame] | 4799 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4800 | if (func_return(ectx) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4801 | // only fails when out of memory |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4802 | goto theend; |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4803 | continue; |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4804 | |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4805 | on_error: |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 4806 | // Jump here for an error that does not require aborting execution. |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 4807 | // If "emsg_silent" is set then ignore the error, unless it was set |
| 4808 | // when calling the function. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4809 | if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 4810 | && emsg_silent && did_emsg_def == 0) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4811 | { |
| 4812 | // If a sequence of instructions causes an error while ":silent!" |
| 4813 | // was used, restore the stack length and jump ahead to restoring |
| 4814 | // the cmdmod. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4815 | if (ectx->ec_funclocal.floc_restore_cmdmod) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4816 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4817 | while (ectx->ec_stack.ga_len |
| 4818 | > ectx->ec_funclocal.floc_restore_cmdmod_stacklen) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4819 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4820 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4821 | clear_tv(STACK_TV_BOT(0)); |
| 4822 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4823 | while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV) |
| 4824 | ++ectx->ec_iidx; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4825 | } |
Bram Moolenaar | cd030c4 | 2020-10-30 21:49:40 +0100 | [diff] [blame] | 4826 | continue; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4827 | } |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 4828 | on_fatal_error: |
| 4829 | // Jump here for an error that messes up the stack. |
Bram Moolenaar | 171fb92 | 2020-10-28 16:54:47 +0100 | [diff] [blame] | 4830 | // If we are not inside a try-catch started here, abort execution. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4831 | if (trylevel <= ectx->ec_trylevel_at_start) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4832 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | done: |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4836 | ret = OK; |
| 4837 | theend: |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 4838 | dict_stack_clear(dict_stack_len_at_start); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4839 | ectx->ec_trylevel_at_start = save_trylevel_at_start; |
| 4840 | return ret; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | /* |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 4844 | * Execute the instructions from a VAR_INSTR typeval and put the result in |
| 4845 | * "rettv". |
| 4846 | * Return OK or FAIL. |
| 4847 | */ |
| 4848 | int |
| 4849 | exe_typval_instr(typval_T *tv, typval_T *rettv) |
| 4850 | { |
| 4851 | ectx_T *ectx = tv->vval.v_instr->instr_ectx; |
| 4852 | isn_T *save_instr = ectx->ec_instr; |
| 4853 | int save_iidx = ectx->ec_iidx; |
| 4854 | int res; |
| 4855 | |
| 4856 | ectx->ec_instr = tv->vval.v_instr->instr_instr; |
| 4857 | res = exec_instructions(ectx); |
| 4858 | if (res == OK) |
| 4859 | { |
| 4860 | *rettv = *STACK_TV_BOT(-1); |
| 4861 | --ectx->ec_stack.ga_len; |
| 4862 | } |
| 4863 | |
| 4864 | ectx->ec_instr = save_instr; |
| 4865 | ectx->ec_iidx = save_iidx; |
| 4866 | |
| 4867 | return res; |
| 4868 | } |
| 4869 | |
| 4870 | /* |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4871 | * Execute the instructions from an ISN_SUBSTITUTE command, which are in |
| 4872 | * "substitute_instr". |
| 4873 | */ |
| 4874 | char_u * |
| 4875 | exe_substitute_instr(void) |
| 4876 | { |
| 4877 | ectx_T *ectx = substitute_instr->subs_ectx; |
| 4878 | isn_T *save_instr = ectx->ec_instr; |
| 4879 | int save_iidx = ectx->ec_iidx; |
| 4880 | char_u *res; |
| 4881 | |
| 4882 | ectx->ec_instr = substitute_instr->subs_instr; |
| 4883 | if (exec_instructions(ectx) == OK) |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 4884 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4885 | typval_T *tv = STACK_TV_BOT(-1); |
| 4886 | |
Bram Moolenaar | 2752360 | 2021-06-05 21:36:19 +0200 | [diff] [blame] | 4887 | res = typval2string(tv, TRUE); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4888 | --ectx->ec_stack.ga_len; |
| 4889 | clear_tv(tv); |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 4890 | } |
| 4891 | else |
| 4892 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4893 | substitute_instr->subs_status = FAIL; |
| 4894 | res = vim_strsave((char_u *)""); |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 4895 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4896 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4897 | ectx->ec_instr = save_instr; |
| 4898 | ectx->ec_iidx = save_iidx; |
| 4899 | |
| 4900 | return res; |
| 4901 | } |
| 4902 | |
| 4903 | /* |
| 4904 | * Call a "def" function from old Vim script. |
| 4905 | * Return OK or FAIL. |
| 4906 | */ |
| 4907 | int |
| 4908 | call_def_function( |
| 4909 | ufunc_T *ufunc, |
| 4910 | int argc_arg, // nr of arguments |
| 4911 | typval_T *argv, // arguments |
| 4912 | partial_T *partial, // optional partial for context |
| 4913 | typval_T *rettv) // return value |
| 4914 | { |
| 4915 | ectx_T ectx; // execution context |
| 4916 | int argc = argc_arg; |
| 4917 | typval_T *tv; |
| 4918 | int idx; |
| 4919 | int ret = FAIL; |
| 4920 | int defcount = ufunc->uf_args.ga_len - argc; |
| 4921 | sctx_T save_current_sctx = current_sctx; |
| 4922 | int did_emsg_before = did_emsg_cumul + did_emsg; |
| 4923 | int save_suppress_errthrow = suppress_errthrow; |
| 4924 | msglist_T **saved_msg_list = NULL; |
| 4925 | msglist_T *private_msg_list = NULL; |
| 4926 | int save_emsg_silent_def = emsg_silent_def; |
| 4927 | int save_did_emsg_def = did_emsg_def; |
| 4928 | int orig_funcdepth; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 4929 | int orig_nesting_level = ex_nesting_level; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4930 | |
| 4931 | // Get pointer to item in the stack. |
| 4932 | #undef STACK_TV |
| 4933 | #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx) |
| 4934 | |
| 4935 | // Get pointer to item at the bottom of the stack, -1 is the bottom. |
| 4936 | #undef STACK_TV_BOT |
| 4937 | #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx) |
| 4938 | |
| 4939 | // Get pointer to a local variable on the stack. Negative for arguments. |
| 4940 | #undef STACK_TV_VAR |
| 4941 | #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx) |
| 4942 | |
Bram Moolenaar | 4f8f542 | 2021-06-20 19:28:14 +0200 | [diff] [blame] | 4943 | // Update uf_has_breakpoint if needed. |
| 4944 | update_has_breakpoint(ufunc); |
| 4945 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4946 | if (ufunc->uf_def_status == UF_NOT_COMPILED |
| 4947 | || ufunc->uf_def_status == UF_COMPILE_ERROR |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 4948 | || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)) |
| 4949 | && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4950 | == FAIL)) |
| 4951 | { |
| 4952 | if (did_emsg_cumul + did_emsg == did_emsg_before) |
| 4953 | semsg(_(e_function_is_not_compiled_str), |
| 4954 | printable_func_name(ufunc)); |
| 4955 | return FAIL; |
| 4956 | } |
| 4957 | |
| 4958 | { |
| 4959 | // Check the function was really compiled. |
| 4960 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 4961 | + ufunc->uf_dfunc_idx; |
| 4962 | if (INSTRUCTIONS(dfunc) == NULL) |
| 4963 | { |
| 4964 | iemsg("using call_def_function() on not compiled function"); |
| 4965 | return FAIL; |
| 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | // If depth of calling is getting too high, don't execute the function. |
| 4970 | orig_funcdepth = funcdepth_get(); |
| 4971 | if (funcdepth_increment() == FAIL) |
| 4972 | return FAIL; |
| 4973 | |
| 4974 | CLEAR_FIELD(ectx); |
| 4975 | ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx; |
| 4976 | ga_init2(&ectx.ec_stack, sizeof(typval_T), 500); |
Bram Moolenaar | 3557816 | 2021-08-02 19:10:38 +0200 | [diff] [blame] | 4977 | if (GA_GROW_FAILS(&ectx.ec_stack, 20)) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4978 | { |
| 4979 | funcdepth_decrement(); |
| 4980 | return FAIL; |
| 4981 | } |
| 4982 | ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10); |
| 4983 | ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10); |
| 4984 | ectx.ec_did_emsg_before = did_emsg_before; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 4985 | ++ex_nesting_level; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4986 | |
| 4987 | idx = argc - ufunc->uf_args.ga_len; |
| 4988 | if (idx > 0 && ufunc->uf_va_name == NULL) |
| 4989 | { |
| 4990 | if (idx == 1) |
| 4991 | emsg(_(e_one_argument_too_many)); |
| 4992 | else |
| 4993 | semsg(_(e_nr_arguments_too_many), idx); |
| 4994 | goto failed_early; |
| 4995 | } |
Bram Moolenaar | c6d7153 | 2021-06-05 18:49:38 +0200 | [diff] [blame] | 4996 | idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len; |
| 4997 | if (idx < 0) |
Bram Moolenaar | 8da6d6d | 2021-06-05 18:15:09 +0200 | [diff] [blame] | 4998 | { |
| 4999 | if (idx == -1) |
| 5000 | emsg(_(e_one_argument_too_few)); |
| 5001 | else |
| 5002 | semsg(_(e_nr_arguments_too_few), -idx); |
| 5003 | goto failed_early; |
| 5004 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5005 | |
| 5006 | // Put arguments on the stack, but no more than what the function expects. |
| 5007 | // A lambda can be called with more arguments than it uses. |
| 5008 | for (idx = 0; idx < argc |
| 5009 | && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len); |
| 5010 | ++idx) |
| 5011 | { |
| 5012 | if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len |
| 5013 | && argv[idx].v_type == VAR_SPECIAL |
| 5014 | && argv[idx].vval.v_number == VVAL_NONE) |
| 5015 | { |
| 5016 | // Use the default value. |
| 5017 | STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; |
| 5018 | } |
| 5019 | else |
| 5020 | { |
| 5021 | if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len |
| 5022 | && check_typval_arg_type( |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 5023 | ufunc->uf_arg_types[idx], &argv[idx], |
| 5024 | NULL, idx + 1) == FAIL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5025 | goto failed_early; |
| 5026 | copy_tv(&argv[idx], STACK_TV_BOT(0)); |
| 5027 | } |
| 5028 | ++ectx.ec_stack.ga_len; |
| 5029 | } |
| 5030 | |
| 5031 | // Turn varargs into a list. Empty list if no args. |
| 5032 | if (ufunc->uf_va_name != NULL) |
| 5033 | { |
| 5034 | int vararg_count = argc - ufunc->uf_args.ga_len; |
| 5035 | |
| 5036 | if (vararg_count < 0) |
| 5037 | vararg_count = 0; |
| 5038 | else |
| 5039 | argc -= vararg_count; |
| 5040 | if (exe_newlist(vararg_count, &ectx) == FAIL) |
| 5041 | goto failed_early; |
| 5042 | |
| 5043 | // Check the type of the list items. |
| 5044 | tv = STACK_TV_BOT(-1); |
| 5045 | if (ufunc->uf_va_type != NULL |
| 5046 | && ufunc->uf_va_type != &t_list_any |
| 5047 | && ufunc->uf_va_type->tt_member != &t_any |
| 5048 | && tv->vval.v_list != NULL) |
| 5049 | { |
| 5050 | type_T *expected = ufunc->uf_va_type->tt_member; |
| 5051 | listitem_T *li = tv->vval.v_list->lv_first; |
| 5052 | |
| 5053 | for (idx = 0; idx < vararg_count; ++idx) |
| 5054 | { |
| 5055 | if (check_typval_arg_type(expected, &li->li_tv, |
Bram Moolenaar | 7a3fe3e | 2021-07-22 14:58:47 +0200 | [diff] [blame] | 5056 | NULL, argc + idx + 1) == FAIL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5057 | goto failed_early; |
| 5058 | li = li->li_next; |
| 5059 | } |
| 5060 | } |
| 5061 | |
| 5062 | if (defcount > 0) |
| 5063 | // Move varargs list to below missing default arguments. |
| 5064 | *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1); |
| 5065 | --ectx.ec_stack.ga_len; |
| 5066 | } |
| 5067 | |
| 5068 | // Make space for omitted arguments, will store default value below. |
| 5069 | // Any varargs list goes after them. |
| 5070 | if (defcount > 0) |
| 5071 | for (idx = 0; idx < defcount; ++idx) |
| 5072 | { |
| 5073 | STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; |
| 5074 | ++ectx.ec_stack.ga_len; |
| 5075 | } |
| 5076 | if (ufunc->uf_va_name != NULL) |
| 5077 | ++ectx.ec_stack.ga_len; |
| 5078 | |
| 5079 | // Frame pointer points to just after arguments. |
| 5080 | ectx.ec_frame_idx = ectx.ec_stack.ga_len; |
| 5081 | ectx.ec_initial_frame_idx = ectx.ec_frame_idx; |
| 5082 | |
| 5083 | { |
| 5084 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 5085 | + ufunc->uf_dfunc_idx; |
| 5086 | ufunc_T *base_ufunc = dfunc->df_ufunc; |
| 5087 | |
| 5088 | // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done |
| 5089 | // by copy_func(). |
| 5090 | if (partial != NULL || base_ufunc->uf_partial != NULL) |
| 5091 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5092 | ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T); |
| 5093 | if (ectx.ec_outer_ref == NULL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5094 | goto failed_early; |
| 5095 | if (partial != NULL) |
| 5096 | { |
| 5097 | if (partial->pt_outer.out_stack == NULL && current_ectx != NULL) |
| 5098 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5099 | if (current_ectx->ec_outer_ref != NULL |
| 5100 | && current_ectx->ec_outer_ref->or_outer != NULL) |
| 5101 | ectx.ec_outer_ref->or_outer = |
| 5102 | current_ectx->ec_outer_ref->or_outer; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5103 | } |
| 5104 | else |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5105 | { |
| 5106 | ectx.ec_outer_ref->or_outer = &partial->pt_outer; |
| 5107 | ++partial->pt_refcount; |
| 5108 | ectx.ec_outer_ref->or_partial = partial; |
| 5109 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5110 | } |
| 5111 | else |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5112 | { |
| 5113 | ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer; |
| 5114 | ++base_ufunc->uf_partial->pt_refcount; |
| 5115 | ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial; |
| 5116 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5117 | } |
| 5118 | } |
| 5119 | |
| 5120 | // dummy frame entries |
| 5121 | for (idx = 0; idx < STACK_FRAME_SIZE; ++idx) |
| 5122 | { |
| 5123 | STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN; |
| 5124 | ++ectx.ec_stack.ga_len; |
| 5125 | } |
| 5126 | |
| 5127 | { |
| 5128 | // Reserve space for local variables and any closure reference count. |
| 5129 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 5130 | + ufunc->uf_dfunc_idx; |
| 5131 | |
Bram Moolenaar | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 5132 | // Initialize variables to zero. That avoids having to generate |
| 5133 | // initializing instructions for "var nr: number", "var x: any", etc. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5134 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
Bram Moolenaar | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 5135 | { |
| 5136 | STACK_TV_VAR(idx)->v_type = VAR_NUMBER; |
| 5137 | STACK_TV_VAR(idx)->vval.v_number = 0; |
| 5138 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5139 | ectx.ec_stack.ga_len += dfunc->df_varcount; |
| 5140 | if (dfunc->df_has_closure) |
| 5141 | { |
| 5142 | STACK_TV_VAR(idx)->v_type = VAR_NUMBER; |
| 5143 | STACK_TV_VAR(idx)->vval.v_number = 0; |
| 5144 | ++ectx.ec_stack.ga_len; |
| 5145 | } |
| 5146 | |
| 5147 | ectx.ec_instr = INSTRUCTIONS(dfunc); |
| 5148 | } |
| 5149 | |
| 5150 | // Following errors are in the function, not the caller. |
| 5151 | // Commands behave like vim9script. |
| 5152 | estack_push_ufunc(ufunc, 1); |
| 5153 | current_sctx = ufunc->uf_script_ctx; |
| 5154 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 5155 | |
| 5156 | // Use a specific location for storing error messages to be converted to an |
| 5157 | // exception. |
| 5158 | saved_msg_list = msg_list; |
| 5159 | msg_list = &private_msg_list; |
| 5160 | |
| 5161 | // Do turn errors into exceptions. |
| 5162 | suppress_errthrow = FALSE; |
| 5163 | |
| 5164 | // Do not delete the function while executing it. |
| 5165 | ++ufunc->uf_calls; |
| 5166 | |
| 5167 | // When ":silent!" was used before calling then we still abort the |
| 5168 | // function. If ":silent!" is used in the function then we don't. |
| 5169 | emsg_silent_def = emsg_silent; |
| 5170 | did_emsg_def = 0; |
| 5171 | |
| 5172 | ectx.ec_where.wt_index = 0; |
| 5173 | ectx.ec_where.wt_variable = FALSE; |
| 5174 | |
| 5175 | // Execute the instructions until done. |
| 5176 | ret = exec_instructions(&ectx); |
| 5177 | if (ret == OK) |
| 5178 | { |
| 5179 | // function finished, get result from the stack. |
| 5180 | if (ufunc->uf_ret_type == &t_void) |
| 5181 | { |
| 5182 | rettv->v_type = VAR_VOID; |
| 5183 | } |
| 5184 | else |
| 5185 | { |
| 5186 | tv = STACK_TV_BOT(-1); |
| 5187 | *rettv = *tv; |
| 5188 | tv->v_type = VAR_UNKNOWN; |
| 5189 | } |
| 5190 | } |
| 5191 | |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 5192 | // When failed need to unwind the call stack. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5193 | while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx) |
| 5194 | func_return(&ectx); |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 5195 | |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 5196 | // Deal with any remaining closures, they may be in use somewhere. |
| 5197 | if (ectx.ec_funcrefs.ga_len > 0) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 5198 | { |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 5199 | handle_closure_in_use(&ectx, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 5200 | ga_clear(&ectx.ec_funcrefs); |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 5201 | } |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 5202 | |
Bram Moolenaar | ee8580e | 2020-08-28 17:19:07 +0200 | [diff] [blame] | 5203 | estack_pop(); |
| 5204 | current_sctx = save_current_sctx; |
| 5205 | |
Bram Moolenaar | 2336c37 | 2021-12-06 15:06:54 +0000 | [diff] [blame] | 5206 | if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0) |
| 5207 | // Function was unreferenced while being used, free it now. |
| 5208 | func_clear_free(ufunc, FALSE); |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 5209 | |
Bram Moolenaar | 352134b | 2020-10-17 22:04:08 +0200 | [diff] [blame] | 5210 | if (*msg_list != NULL && saved_msg_list != NULL) |
| 5211 | { |
| 5212 | msglist_T **plist = saved_msg_list; |
| 5213 | |
| 5214 | // Append entries from the current msg_list (uncaught exceptions) to |
| 5215 | // the saved msg_list. |
| 5216 | while (*plist != NULL) |
| 5217 | plist = &(*plist)->next; |
| 5218 | |
| 5219 | *plist = *msg_list; |
| 5220 | } |
| 5221 | msg_list = saved_msg_list; |
| 5222 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5223 | if (ectx.ec_funclocal.floc_restore_cmdmod) |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 5224 | { |
| 5225 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 5226 | undo_cmdmod(&cmdmod); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5227 | cmdmod = ectx.ec_funclocal.floc_save_cmdmod; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 5228 | } |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 5229 | emsg_silent_def = save_emsg_silent_def; |
| 5230 | did_emsg_def += save_did_emsg_def; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 5231 | |
Bram Moolenaar | ee8580e | 2020-08-28 17:19:07 +0200 | [diff] [blame] | 5232 | failed_early: |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 5233 | // Free all local variables, but not arguments. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5234 | for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) |
| 5235 | clear_tv(STACK_TV(idx)); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 5236 | ex_nesting_level = orig_nesting_level; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 5237 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5238 | vim_free(ectx.ec_stack.ga_data); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5239 | vim_free(ectx.ec_trystack.ga_data); |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5240 | if (ectx.ec_outer_ref != NULL) |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 5241 | { |
Bram Moolenaar | c04f2a4 | 2021-06-09 19:30:03 +0200 | [diff] [blame] | 5242 | if (ectx.ec_outer_ref->or_outer_allocated) |
| 5243 | vim_free(ectx.ec_outer_ref->or_outer); |
| 5244 | partial_unref(ectx.ec_outer_ref->or_partial); |
| 5245 | vim_free(ectx.ec_outer_ref); |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 5246 | } |
| 5247 | |
Bram Moolenaar | 77e5dcc | 2020-09-17 21:29:03 +0200 | [diff] [blame] | 5248 | // Not sure if this is necessary. |
| 5249 | suppress_errthrow = save_suppress_errthrow; |
| 5250 | |
Bram Moolenaar | b5841b9 | 2021-07-15 18:09:53 +0200 | [diff] [blame] | 5251 | if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before |
| 5252 | && !need_rethrow) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5253 | semsg(_(e_unknown_error_while_executing_str), |
Bram Moolenaar | 682d0a1 | 2020-07-19 20:48:59 +0200 | [diff] [blame] | 5254 | printable_func_name(ufunc)); |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 5255 | funcdepth_restore(orig_funcdepth); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5256 | return ret; |
| 5257 | } |
| 5258 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5259 | /* |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5260 | * List instructions "instr" up to "instr_count" or until ISN_FINISH. |
| 5261 | * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE. |
| 5262 | * "pfx" is prefixed to every line. |
| 5263 | */ |
| 5264 | static void |
| 5265 | list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc) |
| 5266 | { |
| 5267 | int line_idx = 0; |
| 5268 | int prev_current = 0; |
| 5269 | int current; |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 5270 | int def_arg_idx = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5271 | |
| 5272 | for (current = 0; current < instr_count; ++current) |
| 5273 | { |
| 5274 | isn_T *iptr = &instr[current]; |
| 5275 | char *line; |
| 5276 | |
| 5277 | if (ufunc != NULL) |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 5278 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5279 | while (line_idx < iptr->isn_lnum |
| 5280 | && line_idx < ufunc->uf_lines.ga_len) |
| 5281 | { |
| 5282 | if (current > prev_current) |
| 5283 | { |
| 5284 | msg_puts("\n\n"); |
| 5285 | prev_current = current; |
| 5286 | } |
| 5287 | line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; |
| 5288 | if (line != NULL) |
| 5289 | msg(line); |
| 5290 | } |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 5291 | if (iptr->isn_type == ISN_JUMP_IF_ARG_SET) |
| 5292 | { |
| 5293 | int first_def_arg = ufunc->uf_args.ga_len |
| 5294 | - ufunc->uf_def_args.ga_len; |
| 5295 | |
| 5296 | if (def_arg_idx > 0) |
| 5297 | msg_puts("\n\n"); |
| 5298 | msg_start(); |
| 5299 | msg_puts(" "); |
| 5300 | msg_puts(((char **)(ufunc->uf_args.ga_data))[ |
| 5301 | first_def_arg + def_arg_idx]); |
| 5302 | msg_puts(" = "); |
| 5303 | msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]); |
| 5304 | msg_clr_eos(); |
| 5305 | msg_end(); |
| 5306 | } |
| 5307 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5308 | |
| 5309 | switch (iptr->isn_type) |
| 5310 | { |
| 5311 | case ISN_EXEC: |
| 5312 | smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string); |
| 5313 | break; |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 5314 | case ISN_EXEC_SPLIT: |
| 5315 | smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string); |
| 5316 | break; |
Bram Moolenaar | e4eed8c | 2021-12-01 15:22:56 +0000 | [diff] [blame] | 5317 | case ISN_EXECRANGE: |
| 5318 | smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string); |
| 5319 | break; |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 5320 | case ISN_LEGACY_EVAL: |
| 5321 | smsg("%s%4d EVAL legacy %s", pfx, current, |
| 5322 | iptr->isn_arg.string); |
| 5323 | break; |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 5324 | case ISN_REDIRSTART: |
| 5325 | smsg("%s%4d REDIR", pfx, current); |
| 5326 | break; |
| 5327 | case ISN_REDIREND: |
| 5328 | smsg("%s%4d REDIR END%s", pfx, current, |
| 5329 | iptr->isn_arg.number ? " append" : ""); |
| 5330 | break; |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5331 | case ISN_CEXPR_AUCMD: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 5332 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5333 | smsg("%s%4d CEXPR pre %s", pfx, current, |
| 5334 | cexpr_get_auname(iptr->isn_arg.number)); |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 5335 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5336 | break; |
| 5337 | case ISN_CEXPR_CORE: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 5338 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5339 | { |
| 5340 | cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref; |
| 5341 | |
| 5342 | smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current, |
| 5343 | cexpr_get_auname(cer->cer_cmdidx), |
| 5344 | cer->cer_forceit ? "!" : "", |
| 5345 | cer->cer_cmdline); |
| 5346 | } |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 5347 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 5348 | break; |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 5349 | case ISN_INSTR: |
| 5350 | { |
| 5351 | smsg("%s%4d INSTR", pfx, current); |
| 5352 | list_instructions(" ", iptr->isn_arg.instr, |
| 5353 | INT_MAX, NULL); |
| 5354 | msg(" -------------"); |
| 5355 | } |
| 5356 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5357 | case ISN_SUBSTITUTE: |
| 5358 | { |
| 5359 | subs_T *subs = &iptr->isn_arg.subs; |
| 5360 | |
| 5361 | smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd); |
| 5362 | list_instructions(" ", subs->subs_instr, INT_MAX, NULL); |
| 5363 | msg(" -------------"); |
| 5364 | } |
| 5365 | break; |
| 5366 | case ISN_EXECCONCAT: |
| 5367 | smsg("%s%4d EXECCONCAT %lld", pfx, current, |
| 5368 | (varnumber_T)iptr->isn_arg.number); |
| 5369 | break; |
| 5370 | case ISN_ECHO: |
| 5371 | { |
| 5372 | echo_T *echo = &iptr->isn_arg.echo; |
| 5373 | |
| 5374 | smsg("%s%4d %s %d", pfx, current, |
| 5375 | echo->echo_with_white ? "ECHO" : "ECHON", |
| 5376 | echo->echo_count); |
| 5377 | } |
| 5378 | break; |
| 5379 | case ISN_EXECUTE: |
| 5380 | smsg("%s%4d EXECUTE %lld", pfx, current, |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 5381 | (varnumber_T)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5382 | break; |
| 5383 | case ISN_ECHOMSG: |
| 5384 | smsg("%s%4d ECHOMSG %lld", pfx, current, |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 5385 | (varnumber_T)(iptr->isn_arg.number)); |
| 5386 | break; |
| 5387 | case ISN_ECHOCONSOLE: |
| 5388 | smsg("%s%4d ECHOCONSOLE %lld", pfx, current, |
| 5389 | (varnumber_T)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5390 | break; |
| 5391 | case ISN_ECHOERR: |
| 5392 | smsg("%s%4d ECHOERR %lld", pfx, current, |
Bram Moolenaar | 7de6262 | 2021-08-07 15:05:47 +0200 | [diff] [blame] | 5393 | (varnumber_T)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5394 | break; |
| 5395 | case ISN_LOAD: |
| 5396 | { |
| 5397 | if (iptr->isn_arg.number < 0) |
| 5398 | smsg("%s%4d LOAD arg[%lld]", pfx, current, |
| 5399 | (varnumber_T)(iptr->isn_arg.number |
| 5400 | + STACK_FRAME_SIZE)); |
| 5401 | else |
| 5402 | smsg("%s%4d LOAD $%lld", pfx, current, |
| 5403 | (varnumber_T)(iptr->isn_arg.number)); |
| 5404 | } |
| 5405 | break; |
| 5406 | case ISN_LOADOUTER: |
| 5407 | { |
| 5408 | if (iptr->isn_arg.number < 0) |
| 5409 | smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current, |
| 5410 | iptr->isn_arg.outer.outer_depth, |
| 5411 | iptr->isn_arg.outer.outer_idx |
| 5412 | + STACK_FRAME_SIZE); |
| 5413 | else |
| 5414 | smsg("%s%4d LOADOUTER level %d $%d", pfx, current, |
| 5415 | iptr->isn_arg.outer.outer_depth, |
| 5416 | iptr->isn_arg.outer.outer_idx); |
| 5417 | } |
| 5418 | break; |
| 5419 | case ISN_LOADV: |
| 5420 | smsg("%s%4d LOADV v:%s", pfx, current, |
| 5421 | get_vim_var_name(iptr->isn_arg.number)); |
| 5422 | break; |
| 5423 | case ISN_LOADSCRIPT: |
| 5424 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5425 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 5426 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
| 5427 | svar_T *sv; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5428 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5429 | sv = get_script_svar(sref, -1); |
| 5430 | if (sv == NULL) |
| 5431 | smsg("%s%4d LOADSCRIPT [deleted] from %s", |
| 5432 | pfx, current, si->sn_name); |
| 5433 | else |
| 5434 | smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5435 | sv->sv_name, |
| 5436 | sref->sref_idx, |
| 5437 | si->sn_name); |
| 5438 | } |
| 5439 | break; |
| 5440 | case ISN_LOADS: |
| 5441 | { |
| 5442 | scriptitem_T *si = SCRIPT_ITEM( |
| 5443 | iptr->isn_arg.loadstore.ls_sid); |
| 5444 | |
| 5445 | smsg("%s%4d LOADS s:%s from %s", pfx, current, |
| 5446 | iptr->isn_arg.loadstore.ls_name, si->sn_name); |
| 5447 | } |
| 5448 | break; |
| 5449 | case ISN_LOADAUTO: |
| 5450 | smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string); |
| 5451 | break; |
| 5452 | case ISN_LOADG: |
| 5453 | smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string); |
| 5454 | break; |
| 5455 | case ISN_LOADB: |
| 5456 | smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string); |
| 5457 | break; |
| 5458 | case ISN_LOADW: |
| 5459 | smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string); |
| 5460 | break; |
| 5461 | case ISN_LOADT: |
| 5462 | smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string); |
| 5463 | break; |
| 5464 | case ISN_LOADGDICT: |
| 5465 | smsg("%s%4d LOAD g:", pfx, current); |
| 5466 | break; |
| 5467 | case ISN_LOADBDICT: |
| 5468 | smsg("%s%4d LOAD b:", pfx, current); |
| 5469 | break; |
| 5470 | case ISN_LOADWDICT: |
| 5471 | smsg("%s%4d LOAD w:", pfx, current); |
| 5472 | break; |
| 5473 | case ISN_LOADTDICT: |
| 5474 | smsg("%s%4d LOAD t:", pfx, current); |
| 5475 | break; |
| 5476 | case ISN_LOADOPT: |
| 5477 | smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string); |
| 5478 | break; |
| 5479 | case ISN_LOADENV: |
| 5480 | smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string); |
| 5481 | break; |
| 5482 | case ISN_LOADREG: |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5483 | smsg("%s%4d LOADREG @%c", pfx, current, |
| 5484 | (int)(iptr->isn_arg.number)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5485 | break; |
| 5486 | |
| 5487 | case ISN_STORE: |
| 5488 | if (iptr->isn_arg.number < 0) |
| 5489 | smsg("%s%4d STORE arg[%lld]", pfx, current, |
| 5490 | iptr->isn_arg.number + STACK_FRAME_SIZE); |
| 5491 | else |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5492 | smsg("%s%4d STORE $%lld", pfx, current, |
| 5493 | iptr->isn_arg.number); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5494 | break; |
| 5495 | case ISN_STOREOUTER: |
| 5496 | { |
| 5497 | if (iptr->isn_arg.number < 0) |
| 5498 | smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current, |
| 5499 | iptr->isn_arg.outer.outer_depth, |
| 5500 | iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE); |
| 5501 | else |
| 5502 | smsg("%s%4d STOREOUTER level %d $%d", pfx, current, |
| 5503 | iptr->isn_arg.outer.outer_depth, |
| 5504 | iptr->isn_arg.outer.outer_idx); |
| 5505 | } |
| 5506 | break; |
| 5507 | case ISN_STOREV: |
| 5508 | smsg("%s%4d STOREV v:%s", pfx, current, |
| 5509 | get_vim_var_name(iptr->isn_arg.number)); |
| 5510 | break; |
| 5511 | case ISN_STOREAUTO: |
| 5512 | smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string); |
| 5513 | break; |
| 5514 | case ISN_STOREG: |
| 5515 | smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string); |
| 5516 | break; |
| 5517 | case ISN_STOREB: |
| 5518 | smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string); |
| 5519 | break; |
| 5520 | case ISN_STOREW: |
| 5521 | smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string); |
| 5522 | break; |
| 5523 | case ISN_STORET: |
| 5524 | smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string); |
| 5525 | break; |
| 5526 | case ISN_STORES: |
| 5527 | { |
| 5528 | scriptitem_T *si = SCRIPT_ITEM( |
| 5529 | iptr->isn_arg.loadstore.ls_sid); |
| 5530 | |
| 5531 | smsg("%s%4d STORES %s in %s", pfx, current, |
| 5532 | iptr->isn_arg.loadstore.ls_name, si->sn_name); |
| 5533 | } |
| 5534 | break; |
| 5535 | case ISN_STORESCRIPT: |
| 5536 | { |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5537 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 5538 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
| 5539 | svar_T *sv; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5540 | |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5541 | sv = get_script_svar(sref, -1); |
| 5542 | if (sv == NULL) |
| 5543 | smsg("%s%4d STORESCRIPT [deleted] in %s", |
| 5544 | pfx, current, si->sn_name); |
| 5545 | else |
| 5546 | smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5547 | sv->sv_name, |
| 5548 | sref->sref_idx, |
| 5549 | si->sn_name); |
| 5550 | } |
| 5551 | break; |
| 5552 | case ISN_STOREOPT: |
Bram Moolenaar | dcb53be | 2021-12-09 14:23:43 +0000 | [diff] [blame] | 5553 | case ISN_STOREFUNCOPT: |
| 5554 | smsg("%s%4d %s &%s", pfx, current, |
| 5555 | iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT", |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5556 | iptr->isn_arg.storeopt.so_name); |
| 5557 | break; |
| 5558 | case ISN_STOREENV: |
| 5559 | smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string); |
| 5560 | break; |
| 5561 | case ISN_STOREREG: |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5562 | smsg("%s%4d STOREREG @%c", pfx, current, |
| 5563 | (int)iptr->isn_arg.number); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5564 | break; |
| 5565 | case ISN_STORENR: |
| 5566 | smsg("%s%4d STORE %lld in $%d", pfx, current, |
| 5567 | iptr->isn_arg.storenr.stnr_val, |
| 5568 | iptr->isn_arg.storenr.stnr_idx); |
| 5569 | break; |
| 5570 | |
| 5571 | case ISN_STOREINDEX: |
| 5572 | smsg("%s%4d STOREINDEX %s", pfx, current, |
| 5573 | vartype_name(iptr->isn_arg.vartype)); |
| 5574 | break; |
| 5575 | |
| 5576 | case ISN_STORERANGE: |
| 5577 | smsg("%s%4d STORERANGE", pfx, current); |
| 5578 | break; |
| 5579 | |
| 5580 | // constants |
| 5581 | case ISN_PUSHNR: |
| 5582 | smsg("%s%4d PUSHNR %lld", pfx, current, |
| 5583 | (varnumber_T)(iptr->isn_arg.number)); |
| 5584 | break; |
| 5585 | case ISN_PUSHBOOL: |
| 5586 | case ISN_PUSHSPEC: |
| 5587 | smsg("%s%4d PUSH %s", pfx, current, |
| 5588 | get_var_special_name(iptr->isn_arg.number)); |
| 5589 | break; |
| 5590 | case ISN_PUSHF: |
| 5591 | #ifdef FEAT_FLOAT |
| 5592 | smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber); |
| 5593 | #endif |
| 5594 | break; |
| 5595 | case ISN_PUSHS: |
| 5596 | smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string); |
| 5597 | break; |
| 5598 | case ISN_PUSHBLOB: |
| 5599 | { |
| 5600 | char_u *r; |
| 5601 | char_u numbuf[NUMBUFLEN]; |
| 5602 | char_u *tofree; |
| 5603 | |
| 5604 | r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); |
| 5605 | smsg("%s%4d PUSHBLOB %s", pfx, current, r); |
| 5606 | vim_free(tofree); |
| 5607 | } |
| 5608 | break; |
| 5609 | case ISN_PUSHFUNC: |
| 5610 | { |
| 5611 | char *name = (char *)iptr->isn_arg.string; |
| 5612 | |
| 5613 | smsg("%s%4d PUSHFUNC \"%s\"", pfx, current, |
| 5614 | name == NULL ? "[none]" : name); |
| 5615 | } |
| 5616 | break; |
| 5617 | case ISN_PUSHCHANNEL: |
| 5618 | #ifdef FEAT_JOB_CHANNEL |
| 5619 | { |
| 5620 | channel_T *channel = iptr->isn_arg.channel; |
| 5621 | |
| 5622 | smsg("%s%4d PUSHCHANNEL %d", pfx, current, |
| 5623 | channel == NULL ? 0 : channel->ch_id); |
| 5624 | } |
| 5625 | #endif |
| 5626 | break; |
| 5627 | case ISN_PUSHJOB: |
| 5628 | #ifdef FEAT_JOB_CHANNEL |
| 5629 | { |
| 5630 | typval_T tv; |
| 5631 | char_u *name; |
Bram Moolenaar | 1328bde | 2021-06-05 20:51:38 +0200 | [diff] [blame] | 5632 | char_u buf[NUMBUFLEN]; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5633 | |
| 5634 | tv.v_type = VAR_JOB; |
| 5635 | tv.vval.v_job = iptr->isn_arg.job; |
Bram Moolenaar | 1328bde | 2021-06-05 20:51:38 +0200 | [diff] [blame] | 5636 | name = job_to_string_buf(&tv, buf); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5637 | smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name); |
| 5638 | } |
| 5639 | #endif |
| 5640 | break; |
| 5641 | case ISN_PUSHEXC: |
| 5642 | smsg("%s%4d PUSH v:exception", pfx, current); |
| 5643 | break; |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 5644 | case ISN_AUTOLOAD: |
| 5645 | smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string); |
| 5646 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5647 | case ISN_UNLET: |
| 5648 | smsg("%s%4d UNLET%s %s", pfx, current, |
| 5649 | iptr->isn_arg.unlet.ul_forceit ? "!" : "", |
| 5650 | iptr->isn_arg.unlet.ul_name); |
| 5651 | break; |
| 5652 | case ISN_UNLETENV: |
| 5653 | smsg("%s%4d UNLETENV%s $%s", pfx, current, |
| 5654 | iptr->isn_arg.unlet.ul_forceit ? "!" : "", |
| 5655 | iptr->isn_arg.unlet.ul_name); |
| 5656 | break; |
| 5657 | case ISN_UNLETINDEX: |
| 5658 | smsg("%s%4d UNLETINDEX", pfx, current); |
| 5659 | break; |
| 5660 | case ISN_UNLETRANGE: |
| 5661 | smsg("%s%4d UNLETRANGE", pfx, current); |
| 5662 | break; |
Bram Moolenaar | aacc966 | 2021-08-13 19:40:51 +0200 | [diff] [blame] | 5663 | case ISN_LOCKUNLOCK: |
| 5664 | smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string); |
| 5665 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5666 | case ISN_LOCKCONST: |
| 5667 | smsg("%s%4d LOCKCONST", pfx, current); |
| 5668 | break; |
| 5669 | case ISN_NEWLIST: |
| 5670 | smsg("%s%4d NEWLIST size %lld", pfx, current, |
| 5671 | (varnumber_T)(iptr->isn_arg.number)); |
| 5672 | break; |
| 5673 | case ISN_NEWDICT: |
| 5674 | smsg("%s%4d NEWDICT size %lld", pfx, current, |
| 5675 | (varnumber_T)(iptr->isn_arg.number)); |
| 5676 | break; |
| 5677 | |
| 5678 | // function call |
| 5679 | case ISN_BCALL: |
| 5680 | { |
| 5681 | cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; |
| 5682 | |
| 5683 | smsg("%s%4d BCALL %s(argc %d)", pfx, current, |
| 5684 | internal_func_name(cbfunc->cbf_idx), |
| 5685 | cbfunc->cbf_argcount); |
| 5686 | } |
| 5687 | break; |
| 5688 | case ISN_DCALL: |
| 5689 | { |
| 5690 | cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; |
| 5691 | dfunc_T *df = ((dfunc_T *)def_functions.ga_data) |
| 5692 | + cdfunc->cdf_idx; |
| 5693 | |
| 5694 | smsg("%s%4d DCALL %s(argc %d)", pfx, current, |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 5695 | printable_func_name(df->df_ufunc), |
| 5696 | cdfunc->cdf_argcount); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5697 | } |
| 5698 | break; |
| 5699 | case ISN_UCALL: |
| 5700 | { |
| 5701 | cufunc_T *cufunc = &iptr->isn_arg.ufunc; |
| 5702 | |
| 5703 | smsg("%s%4d UCALL %s(argc %d)", pfx, current, |
| 5704 | cufunc->cuf_name, cufunc->cuf_argcount); |
| 5705 | } |
| 5706 | break; |
| 5707 | case ISN_PCALL: |
| 5708 | { |
| 5709 | cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; |
| 5710 | |
| 5711 | smsg("%s%4d PCALL%s (argc %d)", pfx, current, |
| 5712 | cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); |
| 5713 | } |
| 5714 | break; |
| 5715 | case ISN_PCALL_END: |
| 5716 | smsg("%s%4d PCALL end", pfx, current); |
| 5717 | break; |
| 5718 | case ISN_RETURN: |
| 5719 | smsg("%s%4d RETURN", pfx, current); |
| 5720 | break; |
Bram Moolenaar | f57b43c | 2021-06-15 22:13:27 +0200 | [diff] [blame] | 5721 | case ISN_RETURN_VOID: |
| 5722 | smsg("%s%4d RETURN void", pfx, current); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5723 | break; |
| 5724 | case ISN_FUNCREF: |
| 5725 | { |
| 5726 | funcref_T *funcref = &iptr->isn_arg.funcref; |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 5727 | char_u *name; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5728 | |
Bram Moolenaar | 3845352 | 2021-11-28 22:00:12 +0000 | [diff] [blame] | 5729 | if (funcref->fr_func_name == NULL) |
| 5730 | { |
| 5731 | dfunc_T *df = ((dfunc_T *)def_functions.ga_data) |
| 5732 | + funcref->fr_dfunc_idx; |
| 5733 | name = df->df_ufunc->uf_name; |
| 5734 | } |
| 5735 | else |
| 5736 | name = funcref->fr_func_name; |
| 5737 | smsg("%s%4d FUNCREF %s", pfx, current, name); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5738 | } |
| 5739 | break; |
| 5740 | |
| 5741 | case ISN_NEWFUNC: |
| 5742 | { |
| 5743 | newfunc_T *newfunc = &iptr->isn_arg.newfunc; |
| 5744 | |
| 5745 | smsg("%s%4d NEWFUNC %s %s", pfx, current, |
| 5746 | newfunc->nf_lambda, newfunc->nf_global); |
| 5747 | } |
| 5748 | break; |
| 5749 | |
| 5750 | case ISN_DEF: |
| 5751 | { |
| 5752 | char_u *name = iptr->isn_arg.string; |
| 5753 | |
| 5754 | smsg("%s%4d DEF %s", pfx, current, |
| 5755 | name == NULL ? (char_u *)"" : name); |
| 5756 | } |
| 5757 | break; |
| 5758 | |
| 5759 | case ISN_JUMP: |
| 5760 | { |
| 5761 | char *when = "?"; |
| 5762 | |
| 5763 | switch (iptr->isn_arg.jump.jump_when) |
| 5764 | { |
| 5765 | case JUMP_ALWAYS: |
| 5766 | when = "JUMP"; |
| 5767 | break; |
Bram Moolenaar | 1a7ee4d | 2021-09-16 16:15:07 +0200 | [diff] [blame] | 5768 | case JUMP_NEVER: |
| 5769 | iemsg("JUMP_NEVER should not be used"); |
| 5770 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5771 | case JUMP_AND_KEEP_IF_TRUE: |
| 5772 | when = "JUMP_AND_KEEP_IF_TRUE"; |
| 5773 | break; |
| 5774 | case JUMP_IF_FALSE: |
| 5775 | when = "JUMP_IF_FALSE"; |
| 5776 | break; |
| 5777 | case JUMP_AND_KEEP_IF_FALSE: |
| 5778 | when = "JUMP_AND_KEEP_IF_FALSE"; |
| 5779 | break; |
| 5780 | case JUMP_IF_COND_FALSE: |
| 5781 | when = "JUMP_IF_COND_FALSE"; |
| 5782 | break; |
| 5783 | case JUMP_IF_COND_TRUE: |
| 5784 | when = "JUMP_IF_COND_TRUE"; |
| 5785 | break; |
| 5786 | } |
| 5787 | smsg("%s%4d %s -> %d", pfx, current, when, |
| 5788 | iptr->isn_arg.jump.jump_where); |
| 5789 | } |
| 5790 | break; |
| 5791 | |
| 5792 | case ISN_JUMP_IF_ARG_SET: |
| 5793 | smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current, |
| 5794 | iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE, |
| 5795 | iptr->isn_arg.jump.jump_where); |
| 5796 | break; |
| 5797 | |
| 5798 | case ISN_FOR: |
| 5799 | { |
| 5800 | forloop_T *forloop = &iptr->isn_arg.forloop; |
| 5801 | |
| 5802 | smsg("%s%4d FOR $%d -> %d", pfx, current, |
| 5803 | forloop->for_idx, forloop->for_end); |
| 5804 | } |
| 5805 | break; |
| 5806 | |
| 5807 | case ISN_TRY: |
| 5808 | { |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 5809 | try_T *try = &iptr->isn_arg.tryref; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5810 | |
| 5811 | if (try->try_ref->try_finally == 0) |
| 5812 | smsg("%s%4d TRY catch -> %d, endtry -> %d", |
| 5813 | pfx, current, |
| 5814 | try->try_ref->try_catch, |
| 5815 | try->try_ref->try_endtry); |
| 5816 | else |
| 5817 | smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d", |
| 5818 | pfx, current, |
| 5819 | try->try_ref->try_catch, |
| 5820 | try->try_ref->try_finally, |
| 5821 | try->try_ref->try_endtry); |
| 5822 | } |
| 5823 | break; |
| 5824 | case ISN_CATCH: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5825 | smsg("%s%4d CATCH", pfx, current); |
| 5826 | break; |
| 5827 | case ISN_TRYCONT: |
| 5828 | { |
| 5829 | trycont_T *trycont = &iptr->isn_arg.trycont; |
| 5830 | |
| 5831 | smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current, |
| 5832 | trycont->tct_levels, |
| 5833 | trycont->tct_levels == 1 ? "" : "s", |
| 5834 | trycont->tct_where); |
| 5835 | } |
| 5836 | break; |
| 5837 | case ISN_FINALLY: |
| 5838 | smsg("%s%4d FINALLY", pfx, current); |
| 5839 | break; |
| 5840 | case ISN_ENDTRY: |
| 5841 | smsg("%s%4d ENDTRY", pfx, current); |
| 5842 | break; |
| 5843 | case ISN_THROW: |
| 5844 | smsg("%s%4d THROW", pfx, current); |
| 5845 | break; |
| 5846 | |
| 5847 | // expression operations on number |
| 5848 | case ISN_OPNR: |
| 5849 | case ISN_OPFLOAT: |
| 5850 | case ISN_OPANY: |
| 5851 | { |
| 5852 | char *what; |
| 5853 | char *ins; |
| 5854 | |
| 5855 | switch (iptr->isn_arg.op.op_type) |
| 5856 | { |
| 5857 | case EXPR_MULT: what = "*"; break; |
| 5858 | case EXPR_DIV: what = "/"; break; |
| 5859 | case EXPR_REM: what = "%"; break; |
| 5860 | case EXPR_SUB: what = "-"; break; |
| 5861 | case EXPR_ADD: what = "+"; break; |
| 5862 | default: what = "???"; break; |
| 5863 | } |
| 5864 | switch (iptr->isn_type) |
| 5865 | { |
| 5866 | case ISN_OPNR: ins = "OPNR"; break; |
| 5867 | case ISN_OPFLOAT: ins = "OPFLOAT"; break; |
| 5868 | case ISN_OPANY: ins = "OPANY"; break; |
| 5869 | default: ins = "???"; break; |
| 5870 | } |
| 5871 | smsg("%s%4d %s %s", pfx, current, ins, what); |
| 5872 | } |
| 5873 | break; |
| 5874 | |
| 5875 | case ISN_COMPAREBOOL: |
| 5876 | case ISN_COMPARESPECIAL: |
| 5877 | case ISN_COMPARENR: |
| 5878 | case ISN_COMPAREFLOAT: |
| 5879 | case ISN_COMPARESTRING: |
| 5880 | case ISN_COMPAREBLOB: |
| 5881 | case ISN_COMPARELIST: |
| 5882 | case ISN_COMPAREDICT: |
| 5883 | case ISN_COMPAREFUNC: |
| 5884 | case ISN_COMPAREANY: |
| 5885 | { |
| 5886 | char *p; |
| 5887 | char buf[10]; |
| 5888 | char *type; |
| 5889 | |
| 5890 | switch (iptr->isn_arg.op.op_type) |
| 5891 | { |
| 5892 | case EXPR_EQUAL: p = "=="; break; |
| 5893 | case EXPR_NEQUAL: p = "!="; break; |
| 5894 | case EXPR_GREATER: p = ">"; break; |
| 5895 | case EXPR_GEQUAL: p = ">="; break; |
| 5896 | case EXPR_SMALLER: p = "<"; break; |
| 5897 | case EXPR_SEQUAL: p = "<="; break; |
| 5898 | case EXPR_MATCH: p = "=~"; break; |
| 5899 | case EXPR_IS: p = "is"; break; |
| 5900 | case EXPR_ISNOT: p = "isnot"; break; |
| 5901 | case EXPR_NOMATCH: p = "!~"; break; |
| 5902 | default: p = "???"; break; |
| 5903 | } |
| 5904 | STRCPY(buf, p); |
| 5905 | if (iptr->isn_arg.op.op_ic == TRUE) |
| 5906 | strcat(buf, "?"); |
| 5907 | switch(iptr->isn_type) |
| 5908 | { |
| 5909 | case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; |
| 5910 | case ISN_COMPARESPECIAL: |
| 5911 | type = "COMPARESPECIAL"; break; |
| 5912 | case ISN_COMPARENR: type = "COMPARENR"; break; |
| 5913 | case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; |
| 5914 | case ISN_COMPARESTRING: |
| 5915 | type = "COMPARESTRING"; break; |
| 5916 | case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; |
| 5917 | case ISN_COMPARELIST: type = "COMPARELIST"; break; |
| 5918 | case ISN_COMPAREDICT: type = "COMPAREDICT"; break; |
| 5919 | case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; |
| 5920 | case ISN_COMPAREANY: type = "COMPAREANY"; break; |
| 5921 | default: type = "???"; break; |
| 5922 | } |
| 5923 | |
| 5924 | smsg("%s%4d %s %s", pfx, current, type, buf); |
| 5925 | } |
| 5926 | break; |
| 5927 | |
| 5928 | case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break; |
| 5929 | case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break; |
| 5930 | |
| 5931 | // expression operations |
| 5932 | case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break; |
| 5933 | case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break; |
| 5934 | case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break; |
| 5935 | case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break; |
| 5936 | case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break; |
| 5937 | case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break; |
| 5938 | case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break; |
| 5939 | case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break; |
| 5940 | case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break; |
| 5941 | case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break; |
| 5942 | case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break; |
| 5943 | case ISN_SLICE: smsg("%s%4d SLICE %lld", |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 5944 | pfx, current, iptr->isn_arg.number); break; |
Bram Moolenaar | 035bd1c | 2021-06-21 19:44:11 +0200 | [diff] [blame] | 5945 | case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current, |
| 5946 | iptr->isn_arg.getitem.gi_index, |
| 5947 | iptr->isn_arg.getitem.gi_with_op ? |
| 5948 | " with op" : ""); break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5949 | case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break; |
| 5950 | case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current, |
| 5951 | iptr->isn_arg.string); break; |
Bram Moolenaar | b1b6f4d | 2021-09-13 18:25:54 +0200 | [diff] [blame] | 5952 | case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break; |
| 5953 | case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break; |
| 5954 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5955 | case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break; |
| 5956 | |
| 5957 | case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break; |
| 5958 | case ISN_CHECKTYPE: |
| 5959 | { |
| 5960 | checktype_T *ct = &iptr->isn_arg.type; |
| 5961 | char *tofree; |
| 5962 | |
| 5963 | if (ct->ct_arg_idx == 0) |
| 5964 | smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current, |
| 5965 | type_name(ct->ct_type, &tofree), |
| 5966 | (int)ct->ct_off); |
| 5967 | else |
Bram Moolenaar | 4f0884d | 2021-08-11 21:49:23 +0200 | [diff] [blame] | 5968 | smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", |
| 5969 | pfx, current, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5970 | type_name(ct->ct_type, &tofree), |
| 5971 | (int)ct->ct_off, |
| 5972 | (int)ct->ct_arg_idx); |
| 5973 | vim_free(tofree); |
| 5974 | break; |
| 5975 | } |
| 5976 | case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current, |
| 5977 | iptr->isn_arg.checklen.cl_more_OK ? ">= " : "", |
| 5978 | iptr->isn_arg.checklen.cl_min_len); |
| 5979 | break; |
| 5980 | case ISN_SETTYPE: |
| 5981 | { |
| 5982 | char *tofree; |
| 5983 | |
| 5984 | smsg("%s%4d SETTYPE %s", pfx, current, |
| 5985 | type_name(iptr->isn_arg.type.ct_type, &tofree)); |
| 5986 | vim_free(tofree); |
| 5987 | break; |
| 5988 | } |
| 5989 | case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5990 | case ISN_2BOOL: if (iptr->isn_arg.tobool.invert) |
| 5991 | smsg("%s%4d INVERT %d (!val)", pfx, current, |
| 5992 | iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5993 | else |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5994 | smsg("%s%4d 2BOOL %d (!!val)", pfx, current, |
| 5995 | iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5996 | break; |
| 5997 | case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current, |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5998 | (varnumber_T)(iptr->isn_arg.tostring.offset)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5999 | break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 6000 | case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]", |
| 6001 | pfx, current, |
| 6002 | (varnumber_T)(iptr->isn_arg.tostring.offset)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6003 | break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 6004 | case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current, |
| 6005 | iptr->isn_arg.string); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6006 | break; |
| 6007 | case ISN_PUT: |
| 6008 | if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE) |
| 6009 | smsg("%s%4d PUT %c above range", |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 6010 | pfx, current, iptr->isn_arg.put.put_regname); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6011 | else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE) |
| 6012 | smsg("%s%4d PUT %c range", |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 6013 | pfx, current, iptr->isn_arg.put.put_regname); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6014 | else |
| 6015 | smsg("%s%4d PUT %c %ld", pfx, current, |
| 6016 | iptr->isn_arg.put.put_regname, |
| 6017 | (long)iptr->isn_arg.put.put_lnum); |
| 6018 | break; |
| 6019 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6020 | case ISN_CMDMOD: |
| 6021 | { |
| 6022 | char_u *buf; |
| 6023 | size_t len = produce_cmdmods( |
| 6024 | NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); |
| 6025 | |
| 6026 | buf = alloc(len + 1); |
Dominique Pelle | 5a9e584 | 2021-07-24 19:32:12 +0200 | [diff] [blame] | 6027 | if (likely(buf != NULL)) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6028 | { |
| 6029 | (void)produce_cmdmods( |
| 6030 | buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); |
| 6031 | smsg("%s%4d CMDMOD %s", pfx, current, buf); |
| 6032 | vim_free(buf); |
| 6033 | } |
| 6034 | break; |
| 6035 | } |
| 6036 | case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break; |
| 6037 | |
| 6038 | case ISN_PROF_START: |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6039 | smsg("%s%4d PROFILE START line %d", pfx, current, |
| 6040 | iptr->isn_lnum); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6041 | break; |
| 6042 | |
| 6043 | case ISN_PROF_END: |
| 6044 | smsg("%s%4d PROFILE END", pfx, current); |
| 6045 | break; |
| 6046 | |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6047 | case ISN_DEBUG: |
Bram Moolenaar | 8cec927 | 2021-06-23 20:20:53 +0200 | [diff] [blame] | 6048 | smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current, |
| 6049 | iptr->isn_arg.debug.dbg_break_lnum + 1, |
| 6050 | iptr->isn_lnum, |
| 6051 | iptr->isn_arg.debug.dbg_var_names_len); |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6052 | break; |
| 6053 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6054 | case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current, |
| 6055 | iptr->isn_arg.unpack.unp_count, |
| 6056 | iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : ""); |
| 6057 | break; |
| 6058 | case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current, |
| 6059 | iptr->isn_arg.shuffle.shfl_item, |
| 6060 | iptr->isn_arg.shuffle.shfl_up); |
| 6061 | break; |
| 6062 | case ISN_DROP: smsg("%s%4d DROP", pfx, current); break; |
| 6063 | |
| 6064 | case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE. |
| 6065 | return; |
| 6066 | } |
| 6067 | |
| 6068 | out_flush(); // output one line at a time |
| 6069 | ui_breakcheck(); |
| 6070 | if (got_int) |
| 6071 | break; |
| 6072 | } |
| 6073 | } |
| 6074 | |
| 6075 | /* |
Bram Moolenaar | 4ee9d8e | 2021-06-13 18:38:48 +0200 | [diff] [blame] | 6076 | * Handle command line completion for the :disassemble command. |
| 6077 | */ |
| 6078 | void |
| 6079 | set_context_in_disassemble_cmd(expand_T *xp, char_u *arg) |
| 6080 | { |
| 6081 | char_u *p; |
| 6082 | |
| 6083 | // Default: expand user functions, "debug" and "profile" |
| 6084 | xp->xp_context = EXPAND_DISASSEMBLE; |
| 6085 | xp->xp_pattern = arg; |
| 6086 | |
| 6087 | // first argument already typed: only user function names |
| 6088 | if (*arg != NUL && *(p = skiptowhite(arg)) != NUL) |
| 6089 | { |
| 6090 | xp->xp_context = EXPAND_USER_FUNC; |
| 6091 | xp->xp_pattern = skipwhite(p); |
| 6092 | } |
| 6093 | } |
| 6094 | |
| 6095 | /* |
| 6096 | * Function given to ExpandGeneric() to obtain the list of :disassemble |
| 6097 | * arguments. |
| 6098 | */ |
| 6099 | char_u * |
| 6100 | get_disassemble_argument(expand_T *xp, int idx) |
| 6101 | { |
| 6102 | if (idx == 0) |
| 6103 | return (char_u *)"debug"; |
| 6104 | if (idx == 1) |
| 6105 | return (char_u *)"profile"; |
| 6106 | return get_user_func_name(xp, idx - 2); |
| 6107 | } |
| 6108 | |
| 6109 | /* |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 6110 | * ":disassemble". |
Bram Moolenaar | 777770f | 2020-02-06 21:27:08 +0100 | [diff] [blame] | 6111 | * We don't really need this at runtime, but we do have tests that require it, |
| 6112 | * so always include this. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6113 | */ |
| 6114 | void |
| 6115 | ex_disassemble(exarg_T *eap) |
| 6116 | { |
Bram Moolenaar | 21456cd | 2020-02-13 21:29:32 +0100 | [diff] [blame] | 6117 | char_u *arg = eap->arg; |
Bram Moolenaar | 0f18b6d | 2020-02-02 17:22:27 +0100 | [diff] [blame] | 6118 | char_u *fname; |
| 6119 | ufunc_T *ufunc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6120 | dfunc_T *dfunc; |
| 6121 | isn_T *instr; |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 6122 | int instr_count; |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 6123 | int is_global = FALSE; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6124 | compiletype_T compile_type = CT_NONE; |
| 6125 | |
Bram Moolenaar | c7d5fc8 | 2021-12-05 17:20:24 +0000 | [diff] [blame] | 6126 | if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7])) |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6127 | { |
| 6128 | compile_type = CT_PROFILE; |
| 6129 | arg = skipwhite(arg + 7); |
| 6130 | } |
Bram Moolenaar | c7d5fc8 | 2021-12-05 17:20:24 +0000 | [diff] [blame] | 6131 | else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5])) |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6132 | { |
| 6133 | compile_type = CT_DEBUG; |
| 6134 | arg = skipwhite(arg + 5); |
| 6135 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6136 | |
Bram Moolenaar | bfd6558 | 2020-07-13 18:18:00 +0200 | [diff] [blame] | 6137 | if (STRNCMP(arg, "<lambda>", 8) == 0) |
| 6138 | { |
| 6139 | arg += 8; |
| 6140 | (void)getdigits(&arg); |
| 6141 | fname = vim_strnsave(eap->arg, arg - eap->arg); |
| 6142 | } |
| 6143 | else |
| 6144 | fname = trans_function_name(&arg, &is_global, FALSE, |
Bram Moolenaar | 32b3f82 | 2021-01-06 21:59:39 +0100 | [diff] [blame] | 6145 | TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL); |
Bram Moolenaar | 21456cd | 2020-02-13 21:29:32 +0100 | [diff] [blame] | 6146 | if (fname == NULL) |
| 6147 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 6148 | semsg(_(e_invalid_argument_str), eap->arg); |
Bram Moolenaar | 21456cd | 2020-02-13 21:29:32 +0100 | [diff] [blame] | 6149 | return; |
| 6150 | } |
| 6151 | |
Bram Moolenaar | d9d2fd0 | 2022-01-13 21:15:21 +0000 | [diff] [blame] | 6152 | ufunc = find_func(fname, is_global); |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 6153 | if (ufunc == NULL) |
| 6154 | { |
| 6155 | char_u *p = untrans_function_name(fname); |
| 6156 | |
| 6157 | if (p != NULL) |
| 6158 | // Try again without making it script-local. |
Bram Moolenaar | d9d2fd0 | 2022-01-13 21:15:21 +0000 | [diff] [blame] | 6159 | ufunc = find_func(p, FALSE); |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 6160 | } |
Bram Moolenaar | 0f18b6d | 2020-02-02 17:22:27 +0100 | [diff] [blame] | 6161 | vim_free(fname); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6162 | if (ufunc == NULL) |
| 6163 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6164 | semsg(_(e_cannot_find_function_str), eap->arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6165 | return; |
| 6166 | } |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6167 | if (func_needs_compiling(ufunc, compile_type) |
| 6168 | && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6169 | return; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6170 | if (ufunc->uf_def_status != UF_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6171 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6172 | semsg(_(e_function_is_not_compiled_str), eap->arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6173 | return; |
| 6174 | } |
Bram Moolenaar | 6db660b | 2021-08-01 14:08:54 +0200 | [diff] [blame] | 6175 | msg((char *)printable_func_name(ufunc)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6176 | |
| 6177 | dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6178 | switch (compile_type) |
| 6179 | { |
| 6180 | case CT_PROFILE: |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 6181 | #ifdef FEAT_PROFILE |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6182 | instr = dfunc->df_instr_prof; |
| 6183 | instr_count = dfunc->df_instr_prof_count; |
| 6184 | break; |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 6185 | #endif |
Bram Moolenaar | e99d422 | 2021-06-13 14:01:26 +0200 | [diff] [blame] | 6186 | // FALLTHROUGH |
| 6187 | case CT_NONE: |
| 6188 | instr = dfunc->df_instr; |
| 6189 | instr_count = dfunc->df_instr_count; |
| 6190 | break; |
| 6191 | case CT_DEBUG: |
| 6192 | instr = dfunc->df_instr_debug; |
| 6193 | instr_count = dfunc->df_instr_debug_count; |
| 6194 | break; |
| 6195 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6196 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 6197 | list_instructions("", instr, instr_count, ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6198 | } |
| 6199 | |
| 6200 | /* |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6201 | * 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] | 6202 | * list, etc. Mostly like what JavaScript does, except that empty list and |
| 6203 | * empty dictionary are FALSE. |
| 6204 | */ |
| 6205 | int |
| 6206 | tv2bool(typval_T *tv) |
| 6207 | { |
| 6208 | switch (tv->v_type) |
| 6209 | { |
| 6210 | case VAR_NUMBER: |
| 6211 | return tv->vval.v_number != 0; |
| 6212 | case VAR_FLOAT: |
| 6213 | #ifdef FEAT_FLOAT |
| 6214 | return tv->vval.v_float != 0.0; |
| 6215 | #else |
| 6216 | break; |
| 6217 | #endif |
| 6218 | case VAR_PARTIAL: |
| 6219 | return tv->vval.v_partial != NULL; |
| 6220 | case VAR_FUNC: |
| 6221 | case VAR_STRING: |
| 6222 | return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; |
| 6223 | case VAR_LIST: |
| 6224 | return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; |
| 6225 | case VAR_DICT: |
| 6226 | return tv->vval.v_dict != NULL |
| 6227 | && tv->vval.v_dict->dv_hashtab.ht_used > 0; |
| 6228 | case VAR_BOOL: |
| 6229 | case VAR_SPECIAL: |
| 6230 | return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; |
| 6231 | case VAR_JOB: |
| 6232 | #ifdef FEAT_JOB_CHANNEL |
| 6233 | return tv->vval.v_job != NULL; |
| 6234 | #else |
| 6235 | break; |
| 6236 | #endif |
| 6237 | case VAR_CHANNEL: |
| 6238 | #ifdef FEAT_JOB_CHANNEL |
| 6239 | return tv->vval.v_channel != NULL; |
| 6240 | #else |
| 6241 | break; |
| 6242 | #endif |
| 6243 | case VAR_BLOB: |
| 6244 | return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; |
| 6245 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 6246 | case VAR_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6247 | case VAR_VOID: |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 6248 | case VAR_INSTR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6249 | break; |
| 6250 | } |
| 6251 | return FALSE; |
| 6252 | } |
| 6253 | |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 6254 | void |
| 6255 | emsg_using_string_as(typval_T *tv, int as_number) |
| 6256 | { |
| 6257 | semsg(_(as_number ? e_using_string_as_number_str |
| 6258 | : e_using_string_as_bool_str), |
| 6259 | tv->vval.v_string == NULL |
| 6260 | ? (char_u *)"" : tv->vval.v_string); |
| 6261 | } |
| 6262 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6263 | /* |
| 6264 | * If "tv" is a string give an error and return FAIL. |
| 6265 | */ |
| 6266 | int |
| 6267 | check_not_string(typval_T *tv) |
| 6268 | { |
| 6269 | if (tv->v_type == VAR_STRING) |
| 6270 | { |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 6271 | emsg_using_string_as(tv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6272 | clear_tv(tv); |
| 6273 | return FAIL; |
| 6274 | } |
| 6275 | return OK; |
| 6276 | } |
| 6277 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6278 | |
| 6279 | #endif // FEAT_EVAL |